Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 2 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 4 | import java.util.ArrayDeque; |
| 5 | import java.util.ArrayList; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 6 | import java.util.Collection; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 8 | import java.util.Iterator; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 9 | import java.util.LinkedList; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 10 | import java.util.List; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 12 | import java.util.Map.Entry; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 13 | import java.util.Queue; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ConcurrentHashMap; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 15 | import java.util.concurrent.ExecutionException; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 16 | import java.util.concurrent.locks.Condition; |
| 17 | import java.util.concurrent.locks.Lock; |
| 18 | import java.util.concurrent.locks.ReentrantLock; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 19 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.FloodlightContext; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 22 | import net.floodlightcontroller.core.IOFMessageListener; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 25 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 26 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 27 | import net.floodlightcontroller.util.OFMessageDamper; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 28 | import net.onrc.onos.core.intent.FlowEntry; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.util.Pair; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 30 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 31 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 32 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 35 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 36 | import org.projectfloodlight.openflow.protocol.OFType; |
| 37 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 41 | import com.google.common.cache.CacheBuilder; |
| 42 | |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 43 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 44 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 45 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 46 | * FlowPusher is a implementation of FlowPusherService. FlowPusher assigns one |
| 47 | * message queue instance for each one switch. Number of message processing |
| 48 | * threads is configurable by constructor, and one thread can handle multiple |
| 49 | * message queues. Each queue will be assigned to a thread according to hash |
| 50 | * function defined by getHash(). Each processing thread reads messages from |
| 51 | * queues and sends it to switches in round-robin. Processing thread also |
| 52 | * calculates rate of sending to suppress excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 53 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 54 | public final class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(FlowPusher.class); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 56 | protected static final int DEFAULT_NUMBER_THREAD = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 57 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 58 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 59 | // The local copy should go away! |
| 60 | // |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 61 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 10000; // TODO: find |
| 62 | // sweet spot |
| 63 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 64 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 65 | // Number of messages sent to switch at once |
| 66 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 67 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 68 | private static class SwitchQueueEntry { |
| 69 | OFMessage msg; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 71 | public SwitchQueueEntry(OFMessage msg) { |
| 72 | this.msg = msg; |
| 73 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 74 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 75 | public OFMessage getOFMessage() { |
| 76 | return msg; |
| 77 | } |
| 78 | } |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 79 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 80 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 81 | * SwitchQueue represents message queue attached to a switch. This consists |
| 82 | * of queue itself and variables used for limiting sending rate. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 83 | */ |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 84 | private static class SwitchQueue { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 85 | List<Queue<SwitchQueueEntry>> rawQueues; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 86 | QueueState state; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 87 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 88 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 89 | long maxRate = 0; // 0 indicates no limitation |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 90 | long lastSentTime = 0; |
| 91 | long lastSentSize = 0; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 92 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 93 | // "To be deleted" flag |
| 94 | boolean toBeDeleted = false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 96 | SwitchQueue() { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 97 | rawQueues = new ArrayList<Queue<SwitchQueueEntry>>( |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 98 | MsgPriority.values().length); |
| 99 | for (int i = 0; i < MsgPriority.values().length; ++i) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 100 | rawQueues.add(i, new ArrayDeque<SwitchQueueEntry>()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 101 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 102 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 103 | state = QueueState.READY; |
| 104 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 105 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 106 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 107 | * Check if sending rate is within the rate. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 108 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 109 | * @param current Current time |
| 110 | * @return true if within the rate |
| 111 | */ |
| 112 | boolean isSendable(long current) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 113 | if (maxRate == 0) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 114 | // no limitation |
| 115 | return true; |
| 116 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 117 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 118 | if (current == lastSentTime) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 121 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 122 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 123 | long rate = lastSentSize / (current - lastSentTime); |
| 124 | return (rate < maxRate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 125 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 126 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Log time and size of last sent data. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 129 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 130 | * @param current Time to be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 131 | * @param size Size of sent data (in bytes). |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 132 | */ |
| 133 | void logSentData(long current, long size) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 134 | lastSentTime = current; |
| 135 | lastSentSize = size; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 136 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 137 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 138 | boolean add(SwitchQueueEntry entry, MsgPriority priority) { |
| 139 | Queue<SwitchQueueEntry> queue = getQueue(priority); |
| 140 | if (queue == null) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 141 | log.error("Unexpected priority: {}", priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | return queue.add(entry); |
| 145 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Poll single appropriate entry object according to QueueState. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 149 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 150 | * @return Entry object. |
| 151 | */ |
| 152 | SwitchQueueEntry poll() { |
| 153 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 154 | case READY: { |
| 155 | for (int i = 0; i < rawQueues.size(); ++i) { |
| 156 | SwitchQueueEntry entry = rawQueues.get(i).poll(); |
| 157 | if (entry != null) { |
| 158 | return entry; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 159 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 160 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 161 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 162 | return null; |
| 163 | } |
| 164 | case SUSPENDED: { |
| 165 | // Only polling from high priority queue |
| 166 | SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll(); |
| 167 | return entry; |
| 168 | } |
| 169 | default: |
| 170 | log.error("Unexpected QueueState: {}", state); |
| 171 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 174 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 175 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 176 | * Check if this object has any messages in the queues to be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 177 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 178 | * @return True if there are some messages to be sent. |
| 179 | */ |
| 180 | boolean hasMessageToSend() { |
| 181 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 182 | case READY: |
| 183 | for (Queue<SwitchQueueEntry> queue : rawQueues) { |
| 184 | if (!queue.isEmpty()) { |
| 185 | return true; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 186 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 187 | } |
| 188 | break; |
| 189 | case SUSPENDED: |
| 190 | // Only checking high priority queue |
| 191 | return (!getQueue(MsgPriority.HIGH).isEmpty()); |
| 192 | default: |
| 193 | log.error("Unexpected QueueState: {}", state); |
| 194 | return false; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 195 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 196 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 199 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 200 | Queue<SwitchQueueEntry> getQueue(MsgPriority priority) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 201 | return rawQueues.get(priority.ordinal()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 204 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 205 | /** |
| 206 | * BarrierInfo holds information to specify barrier message sent to switch. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 207 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 208 | private static final class BarrierInfo { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 209 | final long dpid; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 210 | final long xid; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 211 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 212 | static BarrierInfo create(IOFSwitch sw, OFBarrierRequest req) { |
| 213 | return new BarrierInfo(sw.getId(), req.getXid()); |
| 214 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 215 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 216 | static BarrierInfo create(IOFSwitch sw, OFBarrierReply rpy) { |
| 217 | return new BarrierInfo(sw.getId(), rpy.getXid()); |
| 218 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 219 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 220 | private BarrierInfo(long dpid, long xid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 221 | this.dpid = dpid; |
| 222 | this.xid = xid; |
| 223 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 224 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 225 | // Auto generated code by Eclipse |
| 226 | @Override |
| 227 | public int hashCode() { |
| 228 | final int prime = 31; |
| 229 | int result = 1; |
| 230 | result = prime * result + (int) (dpid ^ (dpid >>> 32)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 231 | result = prime * result + (int) (xid ^ (xid >>> 32)); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 232 | return result; |
| 233 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 234 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 235 | @Override |
| 236 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 237 | if (this == obj) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 238 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 239 | } |
| 240 | if (obj == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 241 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 242 | } |
| 243 | if (getClass() != obj.getClass()) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 244 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 245 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 246 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 247 | BarrierInfo other = (BarrierInfo) obj; |
| 248 | return (this.dpid == other.dpid) && (this.xid == other.xid); |
| 249 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 250 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 251 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 252 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 253 | private FloodlightModuleContext context = null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 254 | private OFMessageDamper messageDamper = null; |
| 255 | private IThreadPoolService threadPool = null; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 256 | private IFloodlightProviderService floodlightProvider = null; |
| 257 | protected Map<OFVersion, OFFactory> ofFactoryMap = null; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 258 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 259 | // Map of threads versus dpid |
| 260 | private Map<Long, FlowPusherThread> threadMap = null; |
| 261 | // Map from (DPID and transaction ID) to Future objects. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 262 | private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures = |
| 263 | new ConcurrentHashMap<BarrierInfo, OFBarrierReplyFuture>(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 264 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 265 | private int numberThread; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 266 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Main thread that reads messages from queues and sends them to switches. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 269 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 270 | private static class FlowPusherThread extends Thread { |
| 271 | // Weak ConcurrentHashMap |
| 272 | private Map<IOFSwitch, SwitchQueue> assignedQueues = CacheBuilder.newBuilder() |
| 273 | .weakKeys() |
| 274 | .<IOFSwitch, SwitchQueue>build().asMap(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 275 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 276 | final Lock queuingLock = new ReentrantLock(); |
| 277 | final Condition messagePushed = queuingLock.newCondition(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 278 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 279 | @Override |
| 280 | public void run() { |
| 281 | this.setName("FlowPusherThread " + this.getId()); |
| 282 | while (true) { |
| 283 | while (!queuesHasMessageToSend()) { |
| 284 | queuingLock.lock(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 285 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 286 | try { |
| 287 | // wait for message pushed to queue |
| 288 | messagePushed.await(); |
| 289 | } catch (InterruptedException e) { |
| 290 | // Interrupted to be shut down (not an error) |
| 291 | log.debug("FlowPusherThread is interrupted"); |
| 292 | return; |
| 293 | } finally { |
| 294 | queuingLock.unlock(); |
| 295 | } |
| 296 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 297 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 298 | for (Iterator<Entry<IOFSwitch, SwitchQueue>> it = assignedQueues |
| 299 | .entrySet().iterator(); it.hasNext();) { |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 300 | Entry<IOFSwitch, SwitchQueue> entry = it.next(); |
| 301 | IOFSwitch sw = entry.getKey(); |
| 302 | SwitchQueue queue = entry.getValue(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 303 | |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 304 | if (queue == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 305 | continue; |
| 306 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 307 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 308 | synchronized (queue) { |
| 309 | processQueue(sw, queue, MAX_MESSAGE_SEND); |
| 310 | if (queue.toBeDeleted && !queue.hasMessageToSend()) { |
| 311 | // remove queue if flagged to be. |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 312 | it.remove(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 318 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 319 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 320 | * Read messages from queue and send them to the switch. If number of |
| 321 | * messages excess the limit, stop sending messages. |
| 322 | * <p> |
| 323 | * @param sw Switch to which messages will be sent. |
| 324 | * @param queue Queue of messages. |
| 325 | * @param maxMsg Limitation of number of messages to be sent. If set to |
| 326 | * 0, all messages in queue will be sent. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 327 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 328 | private void processQueue(IOFSwitch sw, SwitchQueue queue, int maxMsg) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 329 | // check sending rate and determine it to be sent or not |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 330 | long currentTime = System.currentTimeMillis(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 331 | long size = 0; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 332 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 333 | if (sw.isConnected() && queue.isSendable(currentTime)) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 334 | int i = 0; |
| 335 | while (queue.hasMessageToSend()) { |
| 336 | // Number of messages excess the limit |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 337 | if (0 < maxMsg && maxMsg <= i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | ++i; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 341 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 342 | SwitchQueueEntry queueEntry; |
| 343 | synchronized (queue) { |
| 344 | queueEntry = queue.poll(); |
| 345 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 346 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 347 | OFMessage msg = queueEntry.getOFMessage(); |
| 348 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 349 | // TODO BOC do we need to use the message damper? |
| 350 | // messageDamper.write(sw, msg, context); |
| 351 | sw.write(msg, null); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 352 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 353 | log.trace("Pusher sends message to switch {}: {}", sw.getStringId(), msg); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 354 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 355 | // TODO BOC how do we get the size? |
| 356 | // size += msg.getLength(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 357 | } catch (IOException e) { |
Yuta HIGUCHI | e933a28 | 2014-07-13 21:02:29 -0700 | [diff] [blame] | 358 | log.error("Exception in sending message (" + msg + "):", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 361 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 362 | sw.flush(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 363 | queue.logSentData(currentTime, size); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 366 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 367 | private boolean queuesHasMessageToSend() { |
| 368 | for (SwitchQueue queue : assignedQueues.values()) { |
| 369 | if (queue.hasMessageToSend()) { |
| 370 | return true; |
| 371 | } |
| 372 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 373 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 374 | return false; |
| 375 | } |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 376 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 377 | private void notifyMessagePushed() { |
| 378 | queuingLock.lock(); |
| 379 | try { |
| 380 | messagePushed.signal(); |
| 381 | } finally { |
| 382 | queuingLock.unlock(); |
| 383 | } |
| 384 | } |
| 385 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 386 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 387 | /** |
| 388 | * Initialize object with one thread. |
| 389 | */ |
| 390 | public FlowPusher() { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 391 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 392 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 393 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Initialize object with threads of given number. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 396 | * <p> |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 397 | * @param numberThreadValue Number of threads to handle messages. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 398 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 399 | public FlowPusher(int numberThreadValue) { |
| 400 | if (numberThreadValue > 0) { |
| 401 | numberThread = numberThreadValue; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 402 | } else { |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 403 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 406 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 407 | /** |
| 408 | * Set parameters needed for sending messages. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 409 | * <p> |
| 410 | * @param floodlightContext FloodlightModuleContext used for acquiring |
| 411 | * ThreadPoolService and registering MessageListener. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 412 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 413 | public void init(FloodlightModuleContext floodlightContext) { |
| 414 | this.context = floodlightContext; |
| 415 | this.floodlightProvider = context |
| 416 | .getServiceImpl(IFloodlightProviderService.class); |
| 417 | this.threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 418 | this.messageDamper = null; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 419 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 420 | ofFactoryMap = new HashMap<>(); |
| 421 | ofFactoryMap.put(OFVersion.OF_10, floodlightProvider.getOFMessageFactory_10()); |
| 422 | ofFactoryMap.put(OFVersion.OF_13, floodlightProvider.getOFMessageFactory_13()); |
| 423 | floodlightProvider.addOFMessageListener(OFType.BARRIER_REPLY, this); |
| 424 | |
| 425 | // TODO BOC message damper may not be needed... |
| 426 | // if (damper != null) { |
| 427 | // messageDamper = damper; |
| 428 | // } else { |
| 429 | // use default values |
| 430 | /*messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 431 | EnumSet.of(OFType.FLOW_MOD), |
| 432 | OFMESSAGE_DAMPER_TIMEOUT);*/ |
| 433 | // } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 434 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 435 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Begin processing queue. |
| 438 | */ |
| 439 | public void start() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 440 | // TODO BOC |
| 441 | // if (factory == null) { |
| 442 | // log.error("FlowPusher not yet initialized."); |
| 443 | // return; |
| 444 | // } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 445 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 446 | threadMap = new HashMap<Long, FlowPusherThread>(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 447 | for (long i = 0; i < numberThread; ++i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 448 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 449 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 450 | threadMap.put(i, thread); |
| 451 | thread.start(); |
| 452 | } |
| 453 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 454 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 455 | @Override |
| 456 | public boolean suspend(IOFSwitch sw) { |
| 457 | SwitchQueue queue = getQueue(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 458 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 459 | if (queue == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 460 | // create queue in case suspend is called before first message |
| 461 | // addition |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 462 | queue = createQueueImpl(sw); |
| 463 | } |
| 464 | |
| 465 | synchronized (queue) { |
| 466 | if (queue.state == QueueState.READY) { |
| 467 | queue.state = QueueState.SUSPENDED; |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public boolean resume(IOFSwitch sw) { |
| 476 | SwitchQueue queue = getQueue(sw); |
| 477 | |
| 478 | if (queue == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 479 | log.error("No queue is attached to DPID: {}", sw.getStringId()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | |
| 483 | synchronized (queue) { |
| 484 | if (queue.state == QueueState.SUSPENDED) { |
| 485 | queue.state = QueueState.READY; |
| 486 | |
| 487 | // Free the latch if queue has any messages |
| 488 | FlowPusherThread thread = getProcessingThread(sw); |
| 489 | if (queue.hasMessageToSend()) { |
| 490 | thread.notifyMessagePushed(); |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | return false; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public QueueState getState(IOFSwitch sw) { |
| 500 | SwitchQueue queue = getQueue(sw); |
| 501 | |
| 502 | if (queue == null) { |
| 503 | return QueueState.UNKNOWN; |
| 504 | } |
| 505 | |
| 506 | return queue.state; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Stop processing queue and exit thread. |
| 511 | */ |
| 512 | public void stop() { |
| 513 | if (threadMap == null) { |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | for (FlowPusherThread t : threadMap.values()) { |
| 518 | t.interrupt(); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void setRate(IOFSwitch sw, long rate) { |
| 524 | SwitchQueue queue = getQueue(sw); |
| 525 | if (queue == null) { |
| 526 | queue = createQueueImpl(sw); |
| 527 | } |
| 528 | |
| 529 | if (rate > 0) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 530 | log.debug("rate for {} is set to {}", sw.getStringId(), rate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 531 | synchronized (queue) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 532 | queue.maxRate = rate; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | @Override |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 538 | @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 539 | justification = "Future versions of createQueueImpl() might return null") |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 540 | public boolean createQueue(IOFSwitch sw) { |
| 541 | SwitchQueue queue = createQueueImpl(sw); |
| 542 | |
| 543 | return (queue != null); |
| 544 | } |
| 545 | |
| 546 | protected SwitchQueue createQueueImpl(IOFSwitch sw) { |
| 547 | SwitchQueue queue = getQueue(sw); |
| 548 | if (queue != null) { |
| 549 | return queue; |
| 550 | } |
| 551 | |
| 552 | FlowPusherThread proc = getProcessingThread(sw); |
| 553 | queue = new SwitchQueue(); |
| 554 | queue.state = QueueState.READY; |
| 555 | proc.assignedQueues.put(sw, queue); |
| 556 | |
| 557 | return queue; |
| 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public boolean deleteQueue(IOFSwitch sw) { |
| 562 | return deleteQueue(sw, false); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public boolean deleteQueue(IOFSwitch sw, boolean forceStop) { |
| 567 | FlowPusherThread proc = getProcessingThread(sw); |
| 568 | |
| 569 | if (forceStop) { |
| 570 | SwitchQueue queue = proc.assignedQueues.remove(sw); |
| 571 | if (queue == null) { |
| 572 | return false; |
| 573 | } |
| 574 | return true; |
| 575 | } else { |
| 576 | SwitchQueue queue = getQueue(sw); |
| 577 | if (queue == null) { |
| 578 | return false; |
| 579 | } |
| 580 | synchronized (queue) { |
| 581 | queue.toBeDeleted = true; |
| 582 | } |
| 583 | return true; |
| 584 | } |
| 585 | } |
| 586 | |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 587 | /** |
| 588 | * Invalidate. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 589 | * <p> |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 590 | * @param sw switch |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 591 | * @see OFMessageDamper#invalidate(IOFSwitch) |
| 592 | */ |
| 593 | public void invalidate(IOFSwitch sw) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 594 | // messageDamper.invalidate(sw); currently a null ptr - commenting out |
Yuta HIGUCHI | afadeda | 2014-07-24 17:11:07 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 597 | @Override |
| 598 | public boolean add(IOFSwitch sw, OFMessage msg) { |
| 599 | return add(sw, msg, MsgPriority.NORMAL); |
| 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public boolean add(IOFSwitch sw, OFMessage msg, MsgPriority priority) { |
| 604 | return addMessageImpl(sw, msg, priority); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void pushFlowEntries( |
| 609 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
| 610 | pushFlowEntries(entries, MsgPriority.NORMAL); |
| 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public void pushFlowEntries( |
| 615 | Collection<Pair<IOFSwitch, FlowEntry>> entries, MsgPriority priority) { |
| 616 | |
| 617 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
Sho SHIMIZU | 26d7789 | 2014-06-10 11:07:06 -0700 | [diff] [blame] | 618 | add(entry.getFirst(), entry.getSecond(), priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) { |
| 624 | pushFlowEntry(sw, flowEntry, MsgPriority.NORMAL); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) { |
| 629 | Collection<Pair<IOFSwitch, FlowEntry>> entries = |
| 630 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 631 | |
| 632 | entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry)); |
| 633 | pushFlowEntries(entries, priority); |
| 634 | } |
| 635 | |
| 636 | /** |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 637 | * Create a message from FlowEntry and add it to the queue of the switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 638 | * <p> |
| 639 | * @param sw Switch to which message is pushed. |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 640 | * @param flowEntry FlowEntry object used for creating message. |
| 641 | * @return true if message is successfully added to a queue. |
| 642 | */ |
| 643 | private boolean add(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) { |
| 644 | // |
| 645 | // Create the OpenFlow Flow Modification Entry to push |
| 646 | // |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 647 | OFFlowMod fm = flowEntry.buildFlowMod(ofFactoryMap.get(sw.getOFVersion())); |
| 648 | // log.trace("Pushing flow mod {}", fm); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 649 | return addMessageImpl(sw, fm, priority); |
| 650 | } |
| 651 | |
| 652 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 653 | * Add message to queue. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 654 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 655 | * @param sw |
| 656 | * @param msg |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 657 | * @param priority |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 658 | * @return true if the message was added successfully, otherwise false |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 659 | */ |
| 660 | protected boolean addMessageImpl(IOFSwitch sw, OFMessage msg, MsgPriority priority) { |
| 661 | FlowPusherThread thread = getProcessingThread(sw); |
| 662 | |
| 663 | SwitchQueue queue = getQueue(sw); |
| 664 | |
| 665 | // create queue at first addition of message |
| 666 | if (queue == null) { |
| 667 | queue = createQueueImpl(sw); |
| 668 | } |
| 669 | |
| 670 | SwitchQueueEntry entry = new SwitchQueueEntry(msg); |
| 671 | |
| 672 | synchronized (queue) { |
| 673 | queue.add(entry, priority); |
| 674 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 675 | log.trace("Message is pushed to switch {}: {}", sw.getStringId(), entry.getOFMessage()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | thread.notifyMessagePushed(); |
| 680 | |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public OFBarrierReply barrier(IOFSwitch sw) { |
| 686 | OFMessageFuture<OFBarrierReply> future = barrierAsync(sw); |
| 687 | if (future == null) { |
| 688 | return null; |
| 689 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 690 | try { |
| 691 | return future.get(); |
| 692 | } catch (InterruptedException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 693 | log.error("InterruptedException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 694 | } catch (ExecutionException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 695 | log.error("ExecutionException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 696 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 697 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 701 | public OFMessageFuture<OFBarrierReply> barrierAsync(IOFSwitch sw) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 702 | // TODO creation of message and future should be moved to OFSwitchImpl |
| 703 | |
| 704 | if (sw == null) { |
| 705 | return null; |
| 706 | } |
| 707 | |
| 708 | OFBarrierRequest msg = createBarrierRequest(sw); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 709 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, |
| 710 | (int) msg.getXid()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 711 | barrierFutures.put(BarrierInfo.create(sw, msg), future); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 712 | addMessageImpl(sw, msg, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 713 | return future; |
| 714 | } |
| 715 | |
| 716 | protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 717 | OFFactory factory = ofFactoryMap.get(sw.getOFVersion()); |
| 718 | if (factory == null) { |
| 719 | log.error("No OF Message Factory for switch {} with OFVersion {}", sw, |
| 720 | sw.getOFVersion()); |
| 721 | return null; |
| 722 | } |
| 723 | return factory.buildBarrierRequest() |
| 724 | .setXid(sw.getNextTransactionId()) |
| 725 | .build(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Get a queue attached to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 730 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 731 | * @param sw Switch object |
| 732 | * @return Queue object |
| 733 | */ |
| 734 | protected SwitchQueue getQueue(IOFSwitch sw) { |
| 735 | if (sw == null) { |
| 736 | return null; |
| 737 | } |
| 738 | |
| 739 | FlowPusherThread th = getProcessingThread(sw); |
| 740 | if (th == null) { |
| 741 | return null; |
| 742 | } |
| 743 | |
| 744 | return th.assignedQueues.get(sw); |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Get a hash value correspondent to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 749 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 750 | * @param sw Switch object |
| 751 | * @return Hash value |
| 752 | */ |
| 753 | protected long getHash(IOFSwitch sw) { |
| 754 | // This code assumes DPID is sequentially assigned. |
| 755 | // TODO consider equalization algorithm |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 756 | return sw.getId() % numberThread; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
| 760 | * Get a Thread object which processes the queue attached to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 761 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 762 | * @param sw Switch object |
| 763 | * @return Thread object |
| 764 | */ |
| 765 | protected FlowPusherThread getProcessingThread(IOFSwitch sw) { |
| 766 | long hash = getHash(sw); |
| 767 | |
| 768 | return threadMap.get(hash); |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public String getName() { |
| 773 | return "flowpusher"; |
| 774 | } |
| 775 | |
| 776 | @Override |
| 777 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | @Override |
| 782 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | @Override |
| 787 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 788 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 789 | log.trace("Received BARRIER_REPLY from : {}", sw.getStringId()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 792 | if ((msg.getType() != OFType.BARRIER_REPLY) || |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 793 | !(msg instanceof OFBarrierReply)) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 794 | log.error("Unexpected reply message: {}", msg.getType()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 795 | return Command.CONTINUE; |
| 796 | } |
| 797 | |
| 798 | OFBarrierReply reply = (OFBarrierReply) msg; |
| 799 | BarrierInfo info = BarrierInfo.create(sw, reply); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 800 | // Deliver future if exists |
| 801 | OFBarrierReplyFuture future = barrierFutures.get(info); |
| 802 | if (future != null) { |
| 803 | future.deliverFuture(sw, msg); |
| 804 | barrierFutures.remove(info); |
| 805 | } |
| 806 | |
| 807 | return Command.CONTINUE; |
| 808 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame^] | 809 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 810 | } |