blob: fe29efd4545cf43263ff66a6c127bb2c8e2f3364 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.flowprogrammer;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07002
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07003import java.io.IOException;
Naoki Shiota7d0cf272013-11-05 10:18:12 -08004import java.util.ArrayDeque;
5import java.util.ArrayList;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -08006import java.util.Collection;
Naoki Shiota7d0cf272013-11-05 10:18:12 -08007import java.util.EnumSet;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07008import java.util.HashMap;
Naoki Shiotaf03592e2013-11-27 11:20:39 -08009import java.util.HashSet;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -080010import java.util.LinkedList;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080011import java.util.List;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070012import java.util.Map;
Naoki Shiota8df97bc2014-03-13 18:42:23 -070013import java.util.Queue;
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080014import java.util.Set;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070015import java.util.concurrent.ConcurrentHashMap;
Naoki Shiotac1601d32013-11-20 10:47:34 -080016import java.util.concurrent.ExecutionException;
Naoki Shiota05334692014-03-18 16:06:36 -070017import java.util.concurrent.locks.Condition;
18import java.util.concurrent.locks.Lock;
19import java.util.concurrent.locks.ReentrantLock;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070020
Pavlin Radoslavov9fc535a2014-04-11 13:00:12 -070021import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070023import net.floodlightcontroller.core.FloodlightContext;
Naoki Shiotac1601d32013-11-20 10:47:34 -080024import net.floodlightcontroller.core.IFloodlightProviderService;
25import net.floodlightcontroller.core.IOFMessageListener;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070026import net.floodlightcontroller.core.IOFSwitch;
Naoki Shiotac1601d32013-11-20 10:47:34 -080027import net.floodlightcontroller.core.internal.OFMessageFuture;
28import net.floodlightcontroller.core.module.FloodlightModuleContext;
29import net.floodlightcontroller.threadpool.IThreadPoolService;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080030import net.floodlightcontroller.util.MACAddress;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080031import net.floodlightcontroller.util.OFMessageDamper;
Jonathan Hart23701d12014-04-03 10:45:48 -070032import net.onrc.onos.core.util.FlowEntry;
33import net.onrc.onos.core.util.FlowEntryAction;
Jonathan Harta99ec672014-04-03 11:30:34 -070034import net.onrc.onos.core.util.FlowEntryAction.ActionEnqueue;
35import net.onrc.onos.core.util.FlowEntryAction.ActionOutput;
36import net.onrc.onos.core.util.FlowEntryAction.ActionSetEthernetAddr;
37import net.onrc.onos.core.util.FlowEntryAction.ActionSetIPv4Addr;
38import net.onrc.onos.core.util.FlowEntryAction.ActionSetIpToS;
39import net.onrc.onos.core.util.FlowEntryAction.ActionSetTcpUdpPort;
40import net.onrc.onos.core.util.FlowEntryAction.ActionSetVlanId;
41import net.onrc.onos.core.util.FlowEntryAction.ActionSetVlanPriority;
42import net.onrc.onos.core.util.FlowEntryAction.ActionStripVlan;
Jonathan Hart23701d12014-04-03 10:45:48 -070043import net.onrc.onos.core.util.FlowEntryActions;
44import net.onrc.onos.core.util.FlowEntryMatch;
45import net.onrc.onos.core.util.FlowEntryUserState;
46import net.onrc.onos.core.util.IPv4Net;
47import net.onrc.onos.core.util.Pair;
48import net.onrc.onos.core.util.Port;
Jonathan Harta99ec672014-04-03 11:30:34 -070049
50import org.openflow.protocol.OFBarrierReply;
51import org.openflow.protocol.OFBarrierRequest;
52import org.openflow.protocol.OFFlowMod;
53import org.openflow.protocol.OFMatch;
54import org.openflow.protocol.OFMessage;
55import org.openflow.protocol.OFPacketOut;
56import org.openflow.protocol.OFPort;
57import org.openflow.protocol.OFType;
58import org.openflow.protocol.action.OFAction;
59import org.openflow.protocol.action.OFActionDataLayerDestination;
60import org.openflow.protocol.action.OFActionDataLayerSource;
61import org.openflow.protocol.action.OFActionEnqueue;
62import org.openflow.protocol.action.OFActionNetworkLayerDestination;
63import org.openflow.protocol.action.OFActionNetworkLayerSource;
64import org.openflow.protocol.action.OFActionNetworkTypeOfService;
65import org.openflow.protocol.action.OFActionOutput;
66import org.openflow.protocol.action.OFActionStripVirtualLan;
67import org.openflow.protocol.action.OFActionTransportLayerDestination;
68import org.openflow.protocol.action.OFActionTransportLayerSource;
69import org.openflow.protocol.action.OFActionVirtualLanIdentifier;
70import org.openflow.protocol.action.OFActionVirtualLanPriorityCodePoint;
71import org.openflow.protocol.factory.BasicFactory;
72import org.slf4j.Logger;
73import org.slf4j.LoggerFactory;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070074
75/**
Naoki Shiotab485d412013-11-26 12:04:19 -080076 * FlowPusher is a implementation of FlowPusherService.
77 * FlowPusher assigns one message queue instance for each one switch.
78 * Number of message processing threads is configurable by constructor, and
Ray Milkey8e5170e2014-04-02 12:09:55 -070079 * one thread can handle multiple message queues. Each queue will be assigned to
Naoki Shiotab485d412013-11-26 12:04:19 -080080 * a thread according to hash function defined by getHash().
81 * Each processing thread reads messages from queues and sends it to switches
82 * in round-robin. Processing thread also calculates rate of sending to suppress
83 * excessive message sending.
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070084 *
Ray Milkey8e5170e2014-04-02 12:09:55 -070085 * @author Naoki Shiota
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070086 */
Ray Milkey1584ec82014-04-10 11:58:30 -070087public final class FlowPusher implements IFlowPusherService, IOFMessageListener {
Ray Milkeyec838942014-04-09 11:28:43 -070088 private static final Logger log = LoggerFactory.getLogger(FlowPusher.class);
Naoki Shiota8df97bc2014-03-13 18:42:23 -070089 protected static final int DEFAULT_NUMBER_THREAD = 1;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080090
Naoki Shiota7d0cf272013-11-05 10:18:12 -080091 // TODO: Values copied from elsewhere (class LearningSwitch).
92 // The local copy should go away!
93 //
94 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
Ray Milkey8e5170e2014-04-02 12:09:55 -070095 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
96
Naoki Shiota7d0bcfa2013-11-13 10:43:33 -080097 // Number of messages sent to switch at once
98 protected static final int MAX_MESSAGE_SEND = 100;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080099
Ray Milkey8e5170e2014-04-02 12:09:55 -0700100 private static class SwitchQueueEntry {
101 OFMessage msg;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700102
Ray Milkey8e5170e2014-04-02 12:09:55 -0700103 public SwitchQueueEntry(OFMessage msg) {
104 this.msg = msg;
105 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700106
Ray Milkey8e5170e2014-04-02 12:09:55 -0700107 public OFMessage getOFMessage() {
108 return msg;
109 }
110 }
Naoki Shiotacf1acca2013-10-31 11:40:32 -0700111
Ray Milkey8e5170e2014-04-02 12:09:55 -0700112 /**
113 * SwitchQueue represents message queue attached to a switch.
114 * This consists of queue itself and variables used for limiting sending rate.
115 *
116 * @author Naoki Shiota
117 */
Pavlin Radoslavovfee80982014-04-10 12:12:04 -0700118 private static class SwitchQueue {
Ray Milkey2476cac2014-04-08 11:03:21 -0700119 List<Queue<SwitchQueueEntry>> rawQueues;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700120 QueueState state;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800121
Ray Milkey8e5170e2014-04-02 12:09:55 -0700122 // Max rate of sending message (bytes/ms). 0 implies no limitation.
Ray Milkey2476cac2014-04-08 11:03:21 -0700123 long maxRate = 0; // 0 indicates no limitation
124 long lastSentTime = 0;
125 long lastSentSize = 0;
Naoki Shiota05334692014-03-18 16:06:36 -0700126
Ray Milkey8e5170e2014-04-02 12:09:55 -0700127 // "To be deleted" flag
128 boolean toBeDeleted = false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800129
Ray Milkey8e5170e2014-04-02 12:09:55 -0700130 SwitchQueue() {
Ray Milkey2476cac2014-04-08 11:03:21 -0700131 rawQueues = new ArrayList<Queue<SwitchQueueEntry>>(
Ray Milkey8e5170e2014-04-02 12:09:55 -0700132 MsgPriority.values().length);
133 for (int i = 0; i < MsgPriority.values().length; ++i) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700134 rawQueues.add(i, new ArrayDeque<SwitchQueueEntry>());
Ray Milkey8e5170e2014-04-02 12:09:55 -0700135 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800136
Ray Milkey8e5170e2014-04-02 12:09:55 -0700137 state = QueueState.READY;
138 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800139
Ray Milkey8e5170e2014-04-02 12:09:55 -0700140 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700141 * Check if sending rate is within the rate.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700142 *
143 * @param current Current time
144 * @return true if within the rate
145 */
146 boolean isSendable(long current) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700147 if (maxRate == 0) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700148 // no limitation
149 return true;
150 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800151
Ray Milkey2476cac2014-04-08 11:03:21 -0700152 if (current == lastSentTime) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700153 return false;
154 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800155
Ray Milkey8e5170e2014-04-02 12:09:55 -0700156 // Check if sufficient time (from aspect of rate) elapsed or not.
Ray Milkey2476cac2014-04-08 11:03:21 -0700157 long rate = lastSentSize / (current - lastSentTime);
158 return (rate < maxRate);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700159 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800160
Ray Milkey8e5170e2014-04-02 12:09:55 -0700161 /**
162 * Log time and size of last sent data.
163 *
164 * @param current Time to be sent.
165 * @param size Size of sent data (in bytes).
166 */
167 void logSentData(long current, long size) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700168 lastSentTime = current;
169 lastSentSize = size;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700170 }
Naoki Shiota8df97bc2014-03-13 18:42:23 -0700171
Ray Milkey8e5170e2014-04-02 12:09:55 -0700172 boolean add(SwitchQueueEntry entry, MsgPriority priority) {
173 Queue<SwitchQueueEntry> queue = getQueue(priority);
174 if (queue == null) {
175 log.error("Unexpected priority : ", priority);
176 return false;
177 }
178 return queue.add(entry);
179 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800180
Ray Milkey8e5170e2014-04-02 12:09:55 -0700181 /**
182 * Poll single appropriate entry object according to QueueState.
183 *
184 * @return Entry object.
185 */
186 SwitchQueueEntry poll() {
187 switch (state) {
188 case READY: {
Ray Milkey2476cac2014-04-08 11:03:21 -0700189 for (int i = 0; i < rawQueues.size(); ++i) {
190 SwitchQueueEntry entry = rawQueues.get(i).poll();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700191 if (entry != null) {
192 return entry;
193 }
194 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800195
Ray Milkey8e5170e2014-04-02 12:09:55 -0700196 return null;
197 }
198 case SUSPENDED: {
199 // Only polling from high priority queue
200 SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll();
201 return entry;
202 }
203 default:
204 log.error("Unexpected QueueState : ", state);
205 return null;
206 }
207 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800208
Ray Milkey8e5170e2014-04-02 12:09:55 -0700209 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700210 * Check if this object has any messages in the queues to be sent.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700211 *
212 * @return True if there are some messages to be sent.
213 */
214 boolean hasMessageToSend() {
215 switch (state) {
216 case READY:
Ray Milkey2476cac2014-04-08 11:03:21 -0700217 for (Queue<SwitchQueueEntry> queue : rawQueues) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700218 if (!queue.isEmpty()) {
219 return true;
220 }
221 }
222 break;
223 case SUSPENDED:
224 // Only checking high priority queue
225 return (!getQueue(MsgPriority.HIGH).isEmpty());
226 default:
227 log.error("Unexpected QueueState : ", state);
228 return false;
229 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800230
Ray Milkey8e5170e2014-04-02 12:09:55 -0700231 return false;
232 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800233
Ray Milkey8e5170e2014-04-02 12:09:55 -0700234 Queue<SwitchQueueEntry> getQueue(MsgPriority priority) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700235 return rawQueues.get(priority.ordinal());
Ray Milkey8e5170e2014-04-02 12:09:55 -0700236 }
237 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800238
Ray Milkey8e5170e2014-04-02 12:09:55 -0700239 /**
240 * BarrierInfo holds information to specify barrier message sent to switch.
241 *
242 * @author Naoki
243 */
Ray Milkey1584ec82014-04-10 11:58:30 -0700244 private static final class BarrierInfo {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700245 final long dpid;
246 final int xid;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800247
Ray Milkey8e5170e2014-04-02 12:09:55 -0700248 static BarrierInfo create(IOFSwitch sw, OFBarrierRequest req) {
249 return new BarrierInfo(sw.getId(), req.getXid());
250 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800251
Ray Milkey8e5170e2014-04-02 12:09:55 -0700252 static BarrierInfo create(IOFSwitch sw, OFBarrierReply rpy) {
253 return new BarrierInfo(sw.getId(), rpy.getXid());
254 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800255
Ray Milkey8e5170e2014-04-02 12:09:55 -0700256 private BarrierInfo(long dpid, int xid) {
257 this.dpid = dpid;
258 this.xid = xid;
259 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800260
Ray Milkey8e5170e2014-04-02 12:09:55 -0700261 // Auto generated code by Eclipse
262 @Override
263 public int hashCode() {
264 final int prime = 31;
265 int result = 1;
266 result = prime * result + (int) (dpid ^ (dpid >>> 32));
267 result = prime * result + xid;
268 return result;
269 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800270
Ray Milkey8e5170e2014-04-02 12:09:55 -0700271 @Override
272 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700273 if (this == obj) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700274 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700275 }
276 if (obj == null) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700277 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700278 }
279 if (getClass() != obj.getClass()) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700280 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700281 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800282
Ray Milkey8e5170e2014-04-02 12:09:55 -0700283 BarrierInfo other = (BarrierInfo) obj;
284 return (this.dpid == other.dpid) && (this.xid == other.xid);
285 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800286
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800287
Ray Milkey8e5170e2014-04-02 12:09:55 -0700288 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800289
Ray Milkey8e5170e2014-04-02 12:09:55 -0700290 private OFMessageDamper messageDamper = null;
291 private IThreadPoolService threadPool = null;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800292
Ray Milkey8e5170e2014-04-02 12:09:55 -0700293 private FloodlightContext context = null;
294 private BasicFactory factory = null;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800295
Ray Milkey8e5170e2014-04-02 12:09:55 -0700296 // Map of threads versus dpid
297 private Map<Long, FlowPusherThread> threadMap = null;
298 // Map from (DPID and transaction ID) to Future objects.
299 private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures
300 = new ConcurrentHashMap<BarrierInfo, OFBarrierReplyFuture>();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800301
Ray Milkey2476cac2014-04-08 11:03:21 -0700302 private int numberThread;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800303
Ray Milkey8e5170e2014-04-02 12:09:55 -0700304 /**
305 * Main thread that reads messages from queues and sends them to switches.
306 *
307 * @author Naoki Shiota
308 */
309 private class FlowPusherThread extends Thread {
310 private Map<IOFSwitch, SwitchQueue> assignedQueues
311 = new ConcurrentHashMap<IOFSwitch, SwitchQueue>();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800312
Ray Milkey8e5170e2014-04-02 12:09:55 -0700313 final Lock queuingLock = new ReentrantLock();
314 final Condition messagePushed = queuingLock.newCondition();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800315
Ray Milkey8e5170e2014-04-02 12:09:55 -0700316 @Override
317 public void run() {
318 this.setName("FlowPusherThread " + this.getId());
319 while (true) {
320 while (!queuesHasMessageToSend()) {
321 queuingLock.lock();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800322
Ray Milkey8e5170e2014-04-02 12:09:55 -0700323 try {
324 // wait for message pushed to queue
325 messagePushed.await();
326 } catch (InterruptedException e) {
327 // Interrupted to be shut down (not an error)
328 log.debug("FlowPusherThread is interrupted");
329 return;
330 } finally {
331 queuingLock.unlock();
332 }
333 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800334
Ray Milkey8e5170e2014-04-02 12:09:55 -0700335 // for safety of concurrent access, copy set of key objects
336 Set<IOFSwitch> keys = new HashSet<IOFSwitch>(assignedQueues.size());
337 for (IOFSwitch sw : assignedQueues.keySet()) {
338 keys.add(sw);
339 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800340
Ray Milkey8e5170e2014-04-02 12:09:55 -0700341 for (IOFSwitch sw : keys) {
342 SwitchQueue queue = assignedQueues.get(sw);
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800343
Ray Milkey8e5170e2014-04-02 12:09:55 -0700344 if (sw == null || queue == null) {
345 continue;
346 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800347
Ray Milkey8e5170e2014-04-02 12:09:55 -0700348 synchronized (queue) {
349 processQueue(sw, queue, MAX_MESSAGE_SEND);
350 if (queue.toBeDeleted && !queue.hasMessageToSend()) {
351 // remove queue if flagged to be.
352 assignedQueues.remove(sw);
353 }
354 }
355 }
356 }
357 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800358
Ray Milkey8e5170e2014-04-02 12:09:55 -0700359 /**
360 * Read messages from queue and send them to the switch.
361 * If number of messages excess the limit, stop sending messages.
362 *
363 * @param sw Switch to which messages will be sent.
364 * @param queue Queue of messages.
Ray Milkey9526d6f2014-04-10 14:54:15 -0700365 * @param maxMsg Limitation of number of messages to be sent. If set to 0,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700366 * all messages in queue will be sent.
367 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700368 private void processQueue(IOFSwitch sw, SwitchQueue queue, int maxMsg) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700369 // check sending rate and determine it to be sent or not
Ray Milkey2476cac2014-04-08 11:03:21 -0700370 long currentTime = System.currentTimeMillis();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700371 long size = 0;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800372
Ray Milkey2476cac2014-04-08 11:03:21 -0700373 if (queue.isSendable(currentTime)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700374 int i = 0;
375 while (queue.hasMessageToSend()) {
376 // Number of messages excess the limit
Ray Milkey9526d6f2014-04-10 14:54:15 -0700377 if (0 < maxMsg && maxMsg <= i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700378 break;
379 }
380 ++i;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800381
Ray Milkey8e5170e2014-04-02 12:09:55 -0700382 SwitchQueueEntry queueEntry;
383 synchronized (queue) {
384 queueEntry = queue.poll();
385 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800386
Ray Milkey8e5170e2014-04-02 12:09:55 -0700387 OFMessage msg = queueEntry.getOFMessage();
388 try {
389 messageDamper.write(sw, msg, context);
390 if (log.isTraceEnabled()) {
391 log.trace("Pusher sends message : {}", msg);
392 }
393 size += msg.getLength();
394 } catch (IOException e) {
395 e.printStackTrace();
396 log.error("Exception in sending message ({}) : {}", msg, e);
397 }
398 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800399
Ray Milkey8e5170e2014-04-02 12:09:55 -0700400 sw.flush();
Ray Milkey2476cac2014-04-08 11:03:21 -0700401 queue.logSentData(currentTime, size);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700402 }
403 }
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800404
Ray Milkey8e5170e2014-04-02 12:09:55 -0700405 private boolean queuesHasMessageToSend() {
406 for (SwitchQueue queue : assignedQueues.values()) {
407 if (queue.hasMessageToSend()) {
408 return true;
409 }
410 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700411
Ray Milkey8e5170e2014-04-02 12:09:55 -0700412 return false;
413 }
Naoki Shiota05334692014-03-18 16:06:36 -0700414
Ray Milkey8e5170e2014-04-02 12:09:55 -0700415 private void notifyMessagePushed() {
416 queuingLock.lock();
417 try {
418 messagePushed.signal();
419 } finally {
420 queuingLock.unlock();
421 }
422 }
423 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700424
Ray Milkey8e5170e2014-04-02 12:09:55 -0700425 /**
426 * Initialize object with one thread.
427 */
428 public FlowPusher() {
Ray Milkey2476cac2014-04-08 11:03:21 -0700429 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700430 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800431
Ray Milkey8e5170e2014-04-02 12:09:55 -0700432 /**
433 * Initialize object with threads of given number.
434 *
Ray Milkey9526d6f2014-04-10 14:54:15 -0700435 * @param numberThreadValue Number of threads to handle messages.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700436 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700437 public FlowPusher(int numberThreadValue) {
438 if (numberThreadValue > 0) {
439 numberThread = numberThreadValue;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700440 } else {
Ray Milkey9526d6f2014-04-10 14:54:15 -0700441 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700442 }
443 }
Naoki Shiota81dbe302013-11-21 15:35:38 -0800444
Ray Milkey8e5170e2014-04-02 12:09:55 -0700445 /**
446 * Set parameters needed for sending messages.
447 *
448 * @param context FloodlightContext used for sending messages.
449 * If null, FlowPusher uses default context.
450 * @param modContext FloodlightModuleContext used for acquiring
451 * ThreadPoolService and registering MessageListener.
452 * @param factory Factory object to create OFMessage objects.
453 * @param damper Message damper used for sending messages.
454 * If null, FlowPusher creates its own damper object.
455 */
456 public void init(FloodlightContext context,
457 FloodlightModuleContext modContext,
458 BasicFactory factory,
459 OFMessageDamper damper) {
460 this.context = context;
461 this.factory = factory;
462 this.threadPool = modContext.getServiceImpl(IThreadPoolService.class);
463 IFloodlightProviderService flservice
464 = modContext.getServiceImpl(IFloodlightProviderService.class);
465 flservice.addOFMessageListener(OFType.BARRIER_REPLY, this);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800466
Ray Milkey8e5170e2014-04-02 12:09:55 -0700467 if (damper != null) {
468 messageDamper = damper;
469 } else {
470 // use default values
471 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
472 EnumSet.of(OFType.FLOW_MOD),
473 OFMESSAGE_DAMPER_TIMEOUT);
474 }
475 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800476
Ray Milkey8e5170e2014-04-02 12:09:55 -0700477 /**
478 * Begin processing queue.
479 */
480 public void start() {
481 if (factory == null) {
482 log.error("FlowPusher not yet initialized.");
483 return;
484 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800485
Ray Milkey8e5170e2014-04-02 12:09:55 -0700486 threadMap = new HashMap<Long, FlowPusherThread>();
Ray Milkey2476cac2014-04-08 11:03:21 -0700487 for (long i = 0; i < numberThread; ++i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700488 FlowPusherThread thread = new FlowPusherThread();
Naoki Shiotac1601d32013-11-20 10:47:34 -0800489
Ray Milkey8e5170e2014-04-02 12:09:55 -0700490 threadMap.put(i, thread);
491 thread.start();
492 }
493 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800494
Ray Milkey8e5170e2014-04-02 12:09:55 -0700495 @Override
496 public boolean suspend(IOFSwitch sw) {
497 SwitchQueue queue = getQueue(sw);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700498
Ray Milkey8e5170e2014-04-02 12:09:55 -0700499 if (queue == null) {
500 // create queue in case suspend is called before first message addition
501 queue = createQueueImpl(sw);
502 }
503
504 synchronized (queue) {
505 if (queue.state == QueueState.READY) {
506 queue.state = QueueState.SUSPENDED;
507 return true;
508 }
509 return false;
510 }
511 }
512
513 @Override
514 public boolean resume(IOFSwitch sw) {
515 SwitchQueue queue = getQueue(sw);
516
517 if (queue == null) {
518 log.error("No queue is attached to DPID : {}", sw.getId());
519 return false;
520 }
521
522 synchronized (queue) {
523 if (queue.state == QueueState.SUSPENDED) {
524 queue.state = QueueState.READY;
525
526 // Free the latch if queue has any messages
527 FlowPusherThread thread = getProcessingThread(sw);
528 if (queue.hasMessageToSend()) {
529 thread.notifyMessagePushed();
530 }
531 return true;
532 }
533 return false;
534 }
535 }
536
537 @Override
538 public QueueState getState(IOFSwitch sw) {
539 SwitchQueue queue = getQueue(sw);
540
541 if (queue == null) {
542 return QueueState.UNKNOWN;
543 }
544
545 return queue.state;
546 }
547
548 /**
549 * Stop processing queue and exit thread.
550 */
551 public void stop() {
552 if (threadMap == null) {
553 return;
554 }
555
556 for (FlowPusherThread t : threadMap.values()) {
557 t.interrupt();
558 }
559 }
560
561 @Override
562 public void setRate(IOFSwitch sw, long rate) {
563 SwitchQueue queue = getQueue(sw);
564 if (queue == null) {
565 queue = createQueueImpl(sw);
566 }
567
568 if (rate > 0) {
569 log.debug("rate for {} is set to {}", sw.getId(), rate);
570 synchronized (queue) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700571 queue.maxRate = rate;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700572 }
573 }
574 }
575
576 @Override
Pavlin Radoslavov9fc535a2014-04-11 13:00:12 -0700577 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
578 justification = "Future versions of createQueueImpl() might return null")
Ray Milkey8e5170e2014-04-02 12:09:55 -0700579 public boolean createQueue(IOFSwitch sw) {
580 SwitchQueue queue = createQueueImpl(sw);
581
582 return (queue != null);
583 }
584
585 protected SwitchQueue createQueueImpl(IOFSwitch sw) {
586 SwitchQueue queue = getQueue(sw);
587 if (queue != null) {
588 return queue;
589 }
590
591 FlowPusherThread proc = getProcessingThread(sw);
592 queue = new SwitchQueue();
593 queue.state = QueueState.READY;
594 proc.assignedQueues.put(sw, queue);
595
596 return queue;
597 }
598
599 @Override
600 public boolean deleteQueue(IOFSwitch sw) {
601 return deleteQueue(sw, false);
602 }
603
604 @Override
605 public boolean deleteQueue(IOFSwitch sw, boolean forceStop) {
606 FlowPusherThread proc = getProcessingThread(sw);
607
608 if (forceStop) {
609 SwitchQueue queue = proc.assignedQueues.remove(sw);
610 if (queue == null) {
611 return false;
612 }
613 return true;
614 } else {
615 SwitchQueue queue = getQueue(sw);
616 if (queue == null) {
617 return false;
618 }
619 synchronized (queue) {
620 queue.toBeDeleted = true;
621 }
622 return true;
623 }
624 }
625
626 @Override
627 public boolean add(IOFSwitch sw, OFMessage msg) {
628 return add(sw, msg, MsgPriority.NORMAL);
629 }
630
631 @Override
632 public boolean add(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
633 return addMessageImpl(sw, msg, priority);
634 }
635
636 @Override
637 public void pushFlowEntries(
638 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
639 pushFlowEntries(entries, MsgPriority.NORMAL);
640 }
641
642 @Override
643 public void pushFlowEntries(
644 Collection<Pair<IOFSwitch, FlowEntry>> entries, MsgPriority priority) {
645
646 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
647 add(entry.first, entry.second, priority);
648 }
649 }
650
651 @Override
652 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) {
653 pushFlowEntry(sw, flowEntry, MsgPriority.NORMAL);
654 }
655
656 @Override
657 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
658 Collection<Pair<IOFSwitch, FlowEntry>> entries =
659 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
660
661 entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry));
662 pushFlowEntries(entries, priority);
663 }
664
665 /**
666 * Create a message from FlowEntry and add it to the queue of the switch.
667 *
668 * @param sw Switch to which message is pushed.
669 * @param flowEntry FlowEntry object used for creating message.
670 * @return true if message is successfully added to a queue.
671 */
672 private boolean add(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
673 //
674 // Create the OpenFlow Flow Modification Entry to push
675 //
676 OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD);
677 long cookie = flowEntry.flowEntryId().value();
678
679 short flowModCommand = OFFlowMod.OFPFC_ADD;
680 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
681 flowModCommand = OFFlowMod.OFPFC_ADD;
682 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
683 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
684 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
685 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
686 } else {
687 // Unknown user state. Ignore the entry
688 log.debug(
689 "Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
690 flowEntry.flowEntryId(),
691 flowEntry.flowEntryUserState());
692 return false;
693 }
694
695 //
696 // Fetch the match conditions.
697 //
698 // NOTE: The Flow matching conditions common for all Flow Entries are
699 // used ONLY if a Flow Entry does NOT have the corresponding matching
700 // condition set.
701 //
702 OFMatch match = new OFMatch();
703 match.setWildcards(OFMatch.OFPFW_ALL);
704 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
705
706 // Match the Incoming Port
707 Port matchInPort = flowEntryMatch.inPort();
708 if (matchInPort != null) {
709 match.setInputPort(matchInPort.value());
710 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
711 }
712
713 // Match the Source MAC address
714 MACAddress matchSrcMac = flowEntryMatch.srcMac();
715 if (matchSrcMac != null) {
716 match.setDataLayerSource(matchSrcMac.toString());
717 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
718 }
719
720 // Match the Destination MAC address
721 MACAddress matchDstMac = flowEntryMatch.dstMac();
722 if (matchDstMac != null) {
723 match.setDataLayerDestination(matchDstMac.toString());
724 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
725 }
726
727 // Match the Ethernet Frame Type
728 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
729 if (matchEthernetFrameType != null) {
730 match.setDataLayerType(matchEthernetFrameType);
731 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
732 }
733
734 // Match the VLAN ID
735 Short matchVlanId = flowEntryMatch.vlanId();
736 if (matchVlanId != null) {
737 match.setDataLayerVirtualLan(matchVlanId);
738 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN);
739 }
740
741 // Match the VLAN priority
742 Byte matchVlanPriority = flowEntryMatch.vlanPriority();
743 if (matchVlanPriority != null) {
744 match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority);
745 match.setWildcards(match.getWildcards()
746 & ~OFMatch.OFPFW_DL_VLAN_PCP);
747 }
748
749 // Match the Source IPv4 Network prefix
750 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
751 if (matchSrcIPv4Net != null) {
752 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
753 }
754
755 // Natch the Destination IPv4 Network prefix
756 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
757 if (matchDstIPv4Net != null) {
758 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
759 }
760
761 // Match the IP protocol
762 Byte matchIpProto = flowEntryMatch.ipProto();
763 if (matchIpProto != null) {
764 match.setNetworkProtocol(matchIpProto);
765 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO);
766 }
767
768 // Match the IP ToS (DSCP field, 6 bits)
769 Byte matchIpToS = flowEntryMatch.ipToS();
770 if (matchIpToS != null) {
771 match.setNetworkTypeOfService(matchIpToS);
772 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS);
773 }
774
775 // Match the Source TCP/UDP port
776 Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort();
777 if (matchSrcTcpUdpPort != null) {
778 match.setTransportSource(matchSrcTcpUdpPort);
779 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
780 }
781
782 // Match the Destination TCP/UDP port
783 Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort();
784 if (matchDstTcpUdpPort != null) {
785 match.setTransportDestination(matchDstTcpUdpPort);
786 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST);
787 }
788
789 //
790 // Fetch the actions
791 //
792 Short actionOutputPort = null;
793 List<OFAction> openFlowActions = new ArrayList<OFAction>();
794 int actionsLen = 0;
795 FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
796 //
797 for (FlowEntryAction action : flowEntryActions.actions()) {
798 ActionOutput actionOutput = action.actionOutput();
799 ActionSetVlanId actionSetVlanId = action.actionSetVlanId();
800 ActionSetVlanPriority actionSetVlanPriority = action
801 .actionSetVlanPriority();
802 ActionStripVlan actionStripVlan = action.actionStripVlan();
803 ActionSetEthernetAddr actionSetEthernetSrcAddr = action
804 .actionSetEthernetSrcAddr();
805 ActionSetEthernetAddr actionSetEthernetDstAddr = action
806 .actionSetEthernetDstAddr();
807 ActionSetIPv4Addr actionSetIPv4SrcAddr = action
808 .actionSetIPv4SrcAddr();
809 ActionSetIPv4Addr actionSetIPv4DstAddr = action
810 .actionSetIPv4DstAddr();
811 ActionSetIpToS actionSetIpToS = action.actionSetIpToS();
812 ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action
813 .actionSetTcpUdpSrcPort();
814 ActionSetTcpUdpPort actionSetTcpUdpDstPort = action
815 .actionSetTcpUdpDstPort();
816 ActionEnqueue actionEnqueue = action.actionEnqueue();
817
818 if (actionOutput != null) {
819 actionOutputPort = actionOutput.port().value();
820 // XXX: The max length is hard-coded for now
821 OFActionOutput ofa = new OFActionOutput(actionOutput.port()
822 .value(), (short) 0xffff);
823 openFlowActions.add(ofa);
824 actionsLen += ofa.getLength();
825 }
826
827 if (actionSetVlanId != null) {
828 OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier(
829 actionSetVlanId.vlanId());
830 openFlowActions.add(ofa);
831 actionsLen += ofa.getLength();
832 }
833
834 if (actionSetVlanPriority != null) {
835 OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint(
836 actionSetVlanPriority.vlanPriority());
837 openFlowActions.add(ofa);
838 actionsLen += ofa.getLength();
839 }
840
841 if (actionStripVlan != null) {
Ray Milkey6c4f2fe2014-04-11 09:47:23 -0700842 if (actionStripVlan.stripVlan()) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700843 OFActionStripVirtualLan ofa = new OFActionStripVirtualLan();
844 openFlowActions.add(ofa);
845 actionsLen += ofa.getLength();
846 }
847 }
848
849 if (actionSetEthernetSrcAddr != null) {
850 OFActionDataLayerSource ofa = new OFActionDataLayerSource(
851 actionSetEthernetSrcAddr.addr().toBytes());
852 openFlowActions.add(ofa);
853 actionsLen += ofa.getLength();
854 }
855
856 if (actionSetEthernetDstAddr != null) {
857 OFActionDataLayerDestination ofa = new OFActionDataLayerDestination(
858 actionSetEthernetDstAddr.addr().toBytes());
859 openFlowActions.add(ofa);
860 actionsLen += ofa.getLength();
861 }
862
863 if (actionSetIPv4SrcAddr != null) {
864 OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource(
865 actionSetIPv4SrcAddr.addr().value());
866 openFlowActions.add(ofa);
867 actionsLen += ofa.getLength();
868 }
869
870 if (actionSetIPv4DstAddr != null) {
871 OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination(
872 actionSetIPv4DstAddr.addr().value());
873 openFlowActions.add(ofa);
874 actionsLen += ofa.getLength();
875 }
876
877 if (actionSetIpToS != null) {
878 OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService(
879 actionSetIpToS.ipToS());
880 openFlowActions.add(ofa);
881 actionsLen += ofa.getLength();
882 }
883
884 if (actionSetTcpUdpSrcPort != null) {
885 OFActionTransportLayerSource ofa = new OFActionTransportLayerSource(
886 actionSetTcpUdpSrcPort.port());
887 openFlowActions.add(ofa);
888 actionsLen += ofa.getLength();
889 }
890
891 if (actionSetTcpUdpDstPort != null) {
892 OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination(
893 actionSetTcpUdpDstPort.port());
894 openFlowActions.add(ofa);
895 actionsLen += ofa.getLength();
896 }
897
898 if (actionEnqueue != null) {
899 OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port()
900 .value(), actionEnqueue.queueId());
901 openFlowActions.add(ofa);
902 actionsLen += ofa.getLength();
903 }
904 }
905
906 fm.setIdleTimeout((short) flowEntry.idleTimeout())
907 .setHardTimeout((short) flowEntry.hardTimeout())
908 .setPriority((short) flowEntry.priority())
909 .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie)
910 .setCommand(flowModCommand).setMatch(match)
911 .setActions(openFlowActions)
912 .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen);
913 fm.setOutPort(OFPort.OFPP_NONE.getValue());
914 if ((flowModCommand == OFFlowMod.OFPFC_DELETE)
915 || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700916 if (actionOutputPort != null) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700917 fm.setOutPort(actionOutputPort);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700918 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700919 }
920
921 //
922 // Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not
923 // permanent.
924 //
925 if ((flowEntry.idleTimeout() != 0) ||
926 (flowEntry.hardTimeout() != 0)) {
927 fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
928 }
929
930 if (log.isTraceEnabled()) {
931 log.trace("Installing flow entry {} into switch DPID: {} flowEntryId: {} srcMac: {} dstMac: {} inPort: {} outPort: {}"
932 , flowEntry.flowEntryUserState()
933 , sw.getStringId()
934 , flowEntry.flowEntryId()
935 , matchSrcMac
936 , matchDstMac
937 , matchInPort
938 , actionOutputPort
939 );
940 }
941
942 return addMessageImpl(sw, fm, priority);
943 }
944
945 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700946 * Add message to queue.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700947 *
948 * @param sw
949 * @param msg
950 * @param flowEntryId
951 * @return
952 */
953 protected boolean addMessageImpl(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
954 FlowPusherThread thread = getProcessingThread(sw);
955
956 SwitchQueue queue = getQueue(sw);
957
958 // create queue at first addition of message
959 if (queue == null) {
960 queue = createQueueImpl(sw);
961 }
962
963 SwitchQueueEntry entry = new SwitchQueueEntry(msg);
964
965 synchronized (queue) {
966 queue.add(entry, priority);
967 if (log.isTraceEnabled()) {
968 log.trace("Message is pushed : {}", entry.getOFMessage());
969 }
970 }
971
972 thread.notifyMessagePushed();
973
974 return true;
975 }
976
977 @Override
978 public OFBarrierReply barrier(IOFSwitch sw) {
979 OFMessageFuture<OFBarrierReply> future = barrierAsync(sw);
980 if (future == null) {
981 return null;
982 }
983
984 try {
985 return future.get();
986 } catch (InterruptedException e) {
987 e.printStackTrace();
988 log.error("InterruptedException: {}", e);
989 return null;
990 } catch (ExecutionException e) {
991 e.printStackTrace();
992 log.error("ExecutionException: {}", e);
993 return null;
994 }
995 }
996
997 @Override
998 public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) {
999 // TODO creation of message and future should be moved to OFSwitchImpl
1000
1001 if (sw == null) {
1002 return null;
1003 }
1004
1005 OFBarrierRequest msg = createBarrierRequest(sw);
1006
1007 OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid());
1008 barrierFutures.put(BarrierInfo.create(sw, msg), future);
1009
1010 addMessageImpl(sw, msg, MsgPriority.NORMAL);
1011
1012 return future;
1013 }
1014
1015 protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) {
1016 OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST);
1017 msg.setXid(sw.getNextTransactionId());
1018
1019 return msg;
1020 }
1021
1022 /**
1023 * Get a queue attached to a switch.
1024 *
1025 * @param sw Switch object
1026 * @return Queue object
1027 */
1028 protected SwitchQueue getQueue(IOFSwitch sw) {
1029 if (sw == null) {
1030 return null;
1031 }
1032
1033 FlowPusherThread th = getProcessingThread(sw);
1034 if (th == null) {
1035 return null;
1036 }
1037
1038 return th.assignedQueues.get(sw);
1039 }
1040
1041 /**
1042 * Get a hash value correspondent to a switch.
1043 *
1044 * @param sw Switch object
1045 * @return Hash value
1046 */
1047 protected long getHash(IOFSwitch sw) {
1048 // This code assumes DPID is sequentially assigned.
1049 // TODO consider equalization algorithm
Ray Milkey2476cac2014-04-08 11:03:21 -07001050 return sw.getId() % numberThread;
Ray Milkey8e5170e2014-04-02 12:09:55 -07001051 }
1052
1053 /**
1054 * Get a Thread object which processes the queue attached to a switch.
1055 *
1056 * @param sw Switch object
1057 * @return Thread object
1058 */
1059 protected FlowPusherThread getProcessingThread(IOFSwitch sw) {
1060 long hash = getHash(sw);
1061
1062 return threadMap.get(hash);
1063 }
1064
1065 @Override
1066 public String getName() {
1067 return "flowpusher";
1068 }
1069
1070 @Override
1071 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1072 return false;
1073 }
1074
1075 @Override
1076 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1077 return false;
1078 }
1079
1080 @Override
1081 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
1082 if (log.isTraceEnabled()) {
1083 log.trace("Received BARRIER_REPLY from : {}", sw.getId());
1084 }
1085
Pavlin Radoslavov0b88a262014-04-10 15:43:27 -07001086 if ((msg.getType() != OFType.BARRIER_REPLY) ||
1087 !(msg instanceof OFBarrierReply)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -07001088 log.error("Unexpected reply message : {}", msg.getType());
1089 return Command.CONTINUE;
1090 }
1091
1092 OFBarrierReply reply = (OFBarrierReply) msg;
1093 BarrierInfo info = BarrierInfo.create(sw, reply);
1094
1095 // Deliver future if exists
1096 OFBarrierReplyFuture future = barrierFutures.get(info);
1097 if (future != null) {
1098 future.deliverFuture(sw, msg);
1099 barrierFutures.remove(info);
1100 }
1101
1102 return Command.CONTINUE;
1103 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07001104}