blob: bd41a95e52cc7d7d8561e080b295e2190fc3ba2f [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
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070021import net.floodlightcontroller.core.FloodlightContext;
Naoki Shiotac1601d32013-11-20 10:47:34 -080022import net.floodlightcontroller.core.IFloodlightProviderService;
23import net.floodlightcontroller.core.IOFMessageListener;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070024import net.floodlightcontroller.core.IOFSwitch;
Naoki Shiotac1601d32013-11-20 10:47:34 -080025import net.floodlightcontroller.core.internal.OFMessageFuture;
26import net.floodlightcontroller.core.module.FloodlightModuleContext;
27import net.floodlightcontroller.threadpool.IThreadPoolService;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080028import net.floodlightcontroller.util.MACAddress;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080029import net.floodlightcontroller.util.OFMessageDamper;
Jonathan Hart23701d12014-04-03 10:45:48 -070030import net.onrc.onos.core.util.FlowEntry;
31import net.onrc.onos.core.util.FlowEntryAction;
Jonathan Harta99ec672014-04-03 11:30:34 -070032import net.onrc.onos.core.util.FlowEntryAction.ActionEnqueue;
33import net.onrc.onos.core.util.FlowEntryAction.ActionOutput;
34import net.onrc.onos.core.util.FlowEntryAction.ActionSetEthernetAddr;
35import net.onrc.onos.core.util.FlowEntryAction.ActionSetIPv4Addr;
36import net.onrc.onos.core.util.FlowEntryAction.ActionSetIpToS;
37import net.onrc.onos.core.util.FlowEntryAction.ActionSetTcpUdpPort;
38import net.onrc.onos.core.util.FlowEntryAction.ActionSetVlanId;
39import net.onrc.onos.core.util.FlowEntryAction.ActionSetVlanPriority;
40import net.onrc.onos.core.util.FlowEntryAction.ActionStripVlan;
Jonathan Hart23701d12014-04-03 10:45:48 -070041import net.onrc.onos.core.util.FlowEntryActions;
42import net.onrc.onos.core.util.FlowEntryMatch;
43import net.onrc.onos.core.util.FlowEntryUserState;
44import net.onrc.onos.core.util.IPv4Net;
45import net.onrc.onos.core.util.Pair;
46import net.onrc.onos.core.util.Port;
Jonathan Harta99ec672014-04-03 11:30:34 -070047
48import org.openflow.protocol.OFBarrierReply;
49import org.openflow.protocol.OFBarrierRequest;
50import org.openflow.protocol.OFFlowMod;
51import org.openflow.protocol.OFMatch;
52import org.openflow.protocol.OFMessage;
53import org.openflow.protocol.OFPacketOut;
54import org.openflow.protocol.OFPort;
55import org.openflow.protocol.OFType;
56import org.openflow.protocol.action.OFAction;
57import org.openflow.protocol.action.OFActionDataLayerDestination;
58import org.openflow.protocol.action.OFActionDataLayerSource;
59import org.openflow.protocol.action.OFActionEnqueue;
60import org.openflow.protocol.action.OFActionNetworkLayerDestination;
61import org.openflow.protocol.action.OFActionNetworkLayerSource;
62import org.openflow.protocol.action.OFActionNetworkTypeOfService;
63import org.openflow.protocol.action.OFActionOutput;
64import org.openflow.protocol.action.OFActionStripVirtualLan;
65import org.openflow.protocol.action.OFActionTransportLayerDestination;
66import org.openflow.protocol.action.OFActionTransportLayerSource;
67import org.openflow.protocol.action.OFActionVirtualLanIdentifier;
68import org.openflow.protocol.action.OFActionVirtualLanPriorityCodePoint;
69import org.openflow.protocol.factory.BasicFactory;
70import org.slf4j.Logger;
71import org.slf4j.LoggerFactory;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070072
Jonathan Hartc00f5c22014-06-10 15:14:40 -070073import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
74
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070075/**
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 Milkey1584ec82014-04-10 11:58:30 -070085public final class FlowPusher implements IFlowPusherService, IOFMessageListener {
Ray Milkeyec838942014-04-09 11:28:43 -070086 private static final Logger log = LoggerFactory.getLogger(FlowPusher.class);
Naoki Shiota8df97bc2014-03-13 18:42:23 -070087 protected static final int DEFAULT_NUMBER_THREAD = 1;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080088
Naoki Shiota7d0cf272013-11-05 10:18:12 -080089 // TODO: Values copied from elsewhere (class LearningSwitch).
90 // The local copy should go away!
91 //
Yuta HIGUCHI7930d8a2014-06-09 11:32:37 -070092 protected static final int OFMESSAGE_DAMPER_CAPACITY = 10000; // TODO: find sweet spot
Ray Milkey8e5170e2014-04-02 12:09:55 -070093 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
94
Naoki Shiota7d0bcfa2013-11-13 10:43:33 -080095 // Number of messages sent to switch at once
96 protected static final int MAX_MESSAGE_SEND = 100;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080097
Ray Milkey8e5170e2014-04-02 12:09:55 -070098 private static class SwitchQueueEntry {
99 OFMessage msg;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700100
Ray Milkey8e5170e2014-04-02 12:09:55 -0700101 public SwitchQueueEntry(OFMessage msg) {
102 this.msg = msg;
103 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700104
Ray Milkey8e5170e2014-04-02 12:09:55 -0700105 public OFMessage getOFMessage() {
106 return msg;
107 }
108 }
Naoki Shiotacf1acca2013-10-31 11:40:32 -0700109
Ray Milkey8e5170e2014-04-02 12:09:55 -0700110 /**
111 * SwitchQueue represents message queue attached to a switch.
112 * This consists of queue itself and variables used for limiting sending rate.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700113 */
Pavlin Radoslavovfee80982014-04-10 12:12:04 -0700114 private static class SwitchQueue {
Ray Milkey2476cac2014-04-08 11:03:21 -0700115 List<Queue<SwitchQueueEntry>> rawQueues;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700116 QueueState state;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800117
Ray Milkey8e5170e2014-04-02 12:09:55 -0700118 // Max rate of sending message (bytes/ms). 0 implies no limitation.
Ray Milkey2476cac2014-04-08 11:03:21 -0700119 long maxRate = 0; // 0 indicates no limitation
120 long lastSentTime = 0;
121 long lastSentSize = 0;
Naoki Shiota05334692014-03-18 16:06:36 -0700122
Ray Milkey8e5170e2014-04-02 12:09:55 -0700123 // "To be deleted" flag
124 boolean toBeDeleted = false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800125
Ray Milkey8e5170e2014-04-02 12:09:55 -0700126 SwitchQueue() {
Ray Milkey2476cac2014-04-08 11:03:21 -0700127 rawQueues = new ArrayList<Queue<SwitchQueueEntry>>(
Ray Milkey8e5170e2014-04-02 12:09:55 -0700128 MsgPriority.values().length);
129 for (int i = 0; i < MsgPriority.values().length; ++i) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700130 rawQueues.add(i, new ArrayDeque<SwitchQueueEntry>());
Ray Milkey8e5170e2014-04-02 12:09:55 -0700131 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800132
Ray Milkey8e5170e2014-04-02 12:09:55 -0700133 state = QueueState.READY;
134 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800135
Ray Milkey8e5170e2014-04-02 12:09:55 -0700136 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700137 * Check if sending rate is within the rate.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700138 *
139 * @param current Current time
140 * @return true if within the rate
141 */
142 boolean isSendable(long current) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700143 if (maxRate == 0) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700144 // no limitation
145 return true;
146 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800147
Ray Milkey2476cac2014-04-08 11:03:21 -0700148 if (current == lastSentTime) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700149 return false;
150 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800151
Ray Milkey8e5170e2014-04-02 12:09:55 -0700152 // Check if sufficient time (from aspect of rate) elapsed or not.
Ray Milkey2476cac2014-04-08 11:03:21 -0700153 long rate = lastSentSize / (current - lastSentTime);
154 return (rate < maxRate);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700155 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800156
Ray Milkey8e5170e2014-04-02 12:09:55 -0700157 /**
158 * Log time and size of last sent data.
159 *
160 * @param current Time to be sent.
161 * @param size Size of sent data (in bytes).
162 */
163 void logSentData(long current, long size) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700164 lastSentTime = current;
165 lastSentSize = size;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700166 }
Naoki Shiota8df97bc2014-03-13 18:42:23 -0700167
Ray Milkey8e5170e2014-04-02 12:09:55 -0700168 boolean add(SwitchQueueEntry entry, MsgPriority priority) {
169 Queue<SwitchQueueEntry> queue = getQueue(priority);
170 if (queue == null) {
171 log.error("Unexpected priority : ", priority);
172 return false;
173 }
174 return queue.add(entry);
175 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800176
Ray Milkey8e5170e2014-04-02 12:09:55 -0700177 /**
178 * Poll single appropriate entry object according to QueueState.
179 *
180 * @return Entry object.
181 */
182 SwitchQueueEntry poll() {
183 switch (state) {
184 case READY: {
Ray Milkey2476cac2014-04-08 11:03:21 -0700185 for (int i = 0; i < rawQueues.size(); ++i) {
186 SwitchQueueEntry entry = rawQueues.get(i).poll();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700187 if (entry != null) {
188 return entry;
189 }
190 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800191
Ray Milkey8e5170e2014-04-02 12:09:55 -0700192 return null;
193 }
194 case SUSPENDED: {
195 // Only polling from high priority queue
196 SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll();
197 return entry;
198 }
199 default:
200 log.error("Unexpected QueueState : ", state);
201 return null;
202 }
203 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800204
Ray Milkey8e5170e2014-04-02 12:09:55 -0700205 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700206 * Check if this object has any messages in the queues to be sent.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700207 *
208 * @return True if there are some messages to be sent.
209 */
210 boolean hasMessageToSend() {
211 switch (state) {
212 case READY:
Ray Milkey2476cac2014-04-08 11:03:21 -0700213 for (Queue<SwitchQueueEntry> queue : rawQueues) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700214 if (!queue.isEmpty()) {
215 return true;
216 }
217 }
218 break;
219 case SUSPENDED:
220 // Only checking high priority queue
221 return (!getQueue(MsgPriority.HIGH).isEmpty());
222 default:
223 log.error("Unexpected QueueState : ", state);
224 return false;
225 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800226
Ray Milkey8e5170e2014-04-02 12:09:55 -0700227 return false;
228 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800229
Ray Milkey8e5170e2014-04-02 12:09:55 -0700230 Queue<SwitchQueueEntry> getQueue(MsgPriority priority) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700231 return rawQueues.get(priority.ordinal());
Ray Milkey8e5170e2014-04-02 12:09:55 -0700232 }
233 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800234
Ray Milkey8e5170e2014-04-02 12:09:55 -0700235 /**
236 * BarrierInfo holds information to specify barrier message sent to switch.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700237 */
Ray Milkey1584ec82014-04-10 11:58:30 -0700238 private static final class BarrierInfo {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700239 final long dpid;
240 final int xid;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800241
Ray Milkey8e5170e2014-04-02 12:09:55 -0700242 static BarrierInfo create(IOFSwitch sw, OFBarrierRequest req) {
243 return new BarrierInfo(sw.getId(), req.getXid());
244 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800245
Ray Milkey8e5170e2014-04-02 12:09:55 -0700246 static BarrierInfo create(IOFSwitch sw, OFBarrierReply rpy) {
247 return new BarrierInfo(sw.getId(), rpy.getXid());
248 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800249
Ray Milkey8e5170e2014-04-02 12:09:55 -0700250 private BarrierInfo(long dpid, int xid) {
251 this.dpid = dpid;
252 this.xid = xid;
253 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800254
Ray Milkey8e5170e2014-04-02 12:09:55 -0700255 // Auto generated code by Eclipse
256 @Override
257 public int hashCode() {
258 final int prime = 31;
259 int result = 1;
260 result = prime * result + (int) (dpid ^ (dpid >>> 32));
261 result = prime * result + xid;
262 return result;
263 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800264
Ray Milkey8e5170e2014-04-02 12:09:55 -0700265 @Override
266 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700267 if (this == obj) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700268 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700269 }
270 if (obj == null) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700271 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700272 }
273 if (getClass() != obj.getClass()) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700274 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700275 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800276
Ray Milkey8e5170e2014-04-02 12:09:55 -0700277 BarrierInfo other = (BarrierInfo) obj;
278 return (this.dpid == other.dpid) && (this.xid == other.xid);
279 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800280
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800281
Ray Milkey8e5170e2014-04-02 12:09:55 -0700282 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800283
Ray Milkey8e5170e2014-04-02 12:09:55 -0700284 private OFMessageDamper messageDamper = null;
285 private IThreadPoolService threadPool = null;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800286
Ray Milkey8e5170e2014-04-02 12:09:55 -0700287 private FloodlightContext context = null;
288 private BasicFactory factory = null;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800289
Ray Milkey8e5170e2014-04-02 12:09:55 -0700290 // Map of threads versus dpid
291 private Map<Long, FlowPusherThread> threadMap = null;
292 // Map from (DPID and transaction ID) to Future objects.
293 private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures
294 = new ConcurrentHashMap<BarrierInfo, OFBarrierReplyFuture>();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800295
Ray Milkey2476cac2014-04-08 11:03:21 -0700296 private int numberThread;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800297
Ray Milkey8e5170e2014-04-02 12:09:55 -0700298 /**
299 * Main thread that reads messages from queues and sends them to switches.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700300 */
301 private class FlowPusherThread extends Thread {
302 private Map<IOFSwitch, SwitchQueue> assignedQueues
303 = new ConcurrentHashMap<IOFSwitch, SwitchQueue>();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800304
Ray Milkey8e5170e2014-04-02 12:09:55 -0700305 final Lock queuingLock = new ReentrantLock();
306 final Condition messagePushed = queuingLock.newCondition();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800307
Ray Milkey8e5170e2014-04-02 12:09:55 -0700308 @Override
309 public void run() {
310 this.setName("FlowPusherThread " + this.getId());
311 while (true) {
312 while (!queuesHasMessageToSend()) {
313 queuingLock.lock();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800314
Ray Milkey8e5170e2014-04-02 12:09:55 -0700315 try {
316 // wait for message pushed to queue
317 messagePushed.await();
318 } catch (InterruptedException e) {
319 // Interrupted to be shut down (not an error)
320 log.debug("FlowPusherThread is interrupted");
321 return;
322 } finally {
323 queuingLock.unlock();
324 }
325 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800326
Ray Milkey8e5170e2014-04-02 12:09:55 -0700327 // for safety of concurrent access, copy set of key objects
328 Set<IOFSwitch> keys = new HashSet<IOFSwitch>(assignedQueues.size());
329 for (IOFSwitch sw : assignedQueues.keySet()) {
330 keys.add(sw);
331 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800332
Ray Milkey8e5170e2014-04-02 12:09:55 -0700333 for (IOFSwitch sw : keys) {
334 SwitchQueue queue = assignedQueues.get(sw);
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800335
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700336 if (queue == null) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700337 continue;
338 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800339
Ray Milkey8e5170e2014-04-02 12:09:55 -0700340 synchronized (queue) {
341 processQueue(sw, queue, MAX_MESSAGE_SEND);
342 if (queue.toBeDeleted && !queue.hasMessageToSend()) {
343 // remove queue if flagged to be.
344 assignedQueues.remove(sw);
345 }
346 }
347 }
348 }
349 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800350
Ray Milkey8e5170e2014-04-02 12:09:55 -0700351 /**
352 * Read messages from queue and send them to the switch.
353 * If number of messages excess the limit, stop sending messages.
354 *
355 * @param sw Switch to which messages will be sent.
356 * @param queue Queue of messages.
Ray Milkey9526d6f2014-04-10 14:54:15 -0700357 * @param maxMsg Limitation of number of messages to be sent. If set to 0,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700358 * all messages in queue will be sent.
359 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700360 private void processQueue(IOFSwitch sw, SwitchQueue queue, int maxMsg) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700361 // check sending rate and determine it to be sent or not
Ray Milkey2476cac2014-04-08 11:03:21 -0700362 long currentTime = System.currentTimeMillis();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700363 long size = 0;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800364
Ray Milkey2476cac2014-04-08 11:03:21 -0700365 if (queue.isSendable(currentTime)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700366 int i = 0;
367 while (queue.hasMessageToSend()) {
368 // Number of messages excess the limit
Ray Milkey9526d6f2014-04-10 14:54:15 -0700369 if (0 < maxMsg && maxMsg <= i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700370 break;
371 }
372 ++i;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800373
Ray Milkey8e5170e2014-04-02 12:09:55 -0700374 SwitchQueueEntry queueEntry;
375 synchronized (queue) {
376 queueEntry = queue.poll();
377 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800378
Ray Milkey8e5170e2014-04-02 12:09:55 -0700379 OFMessage msg = queueEntry.getOFMessage();
380 try {
381 messageDamper.write(sw, msg, context);
382 if (log.isTraceEnabled()) {
383 log.trace("Pusher sends message : {}", msg);
384 }
385 size += msg.getLength();
386 } catch (IOException e) {
387 e.printStackTrace();
388 log.error("Exception in sending message ({}) : {}", msg, e);
389 }
390 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800391
Ray Milkey8e5170e2014-04-02 12:09:55 -0700392 sw.flush();
Ray Milkey2476cac2014-04-08 11:03:21 -0700393 queue.logSentData(currentTime, size);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700394 }
395 }
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800396
Ray Milkey8e5170e2014-04-02 12:09:55 -0700397 private boolean queuesHasMessageToSend() {
398 for (SwitchQueue queue : assignedQueues.values()) {
399 if (queue.hasMessageToSend()) {
400 return true;
401 }
402 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700403
Ray Milkey8e5170e2014-04-02 12:09:55 -0700404 return false;
405 }
Naoki Shiota05334692014-03-18 16:06:36 -0700406
Ray Milkey8e5170e2014-04-02 12:09:55 -0700407 private void notifyMessagePushed() {
408 queuingLock.lock();
409 try {
410 messagePushed.signal();
411 } finally {
412 queuingLock.unlock();
413 }
414 }
415 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700416
Ray Milkey8e5170e2014-04-02 12:09:55 -0700417 /**
418 * Initialize object with one thread.
419 */
420 public FlowPusher() {
Ray Milkey2476cac2014-04-08 11:03:21 -0700421 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700422 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800423
Ray Milkey8e5170e2014-04-02 12:09:55 -0700424 /**
425 * Initialize object with threads of given number.
426 *
Ray Milkey9526d6f2014-04-10 14:54:15 -0700427 * @param numberThreadValue Number of threads to handle messages.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700428 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700429 public FlowPusher(int numberThreadValue) {
430 if (numberThreadValue > 0) {
431 numberThread = numberThreadValue;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700432 } else {
Ray Milkey9526d6f2014-04-10 14:54:15 -0700433 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700434 }
435 }
Naoki Shiota81dbe302013-11-21 15:35:38 -0800436
Ray Milkey8e5170e2014-04-02 12:09:55 -0700437 /**
438 * Set parameters needed for sending messages.
439 *
Ray Milkey5df613b2014-04-15 10:50:56 -0700440 * @param floodlightContext FloodlightContext used for sending messages.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700441 * If null, FlowPusher uses default context.
442 * @param modContext FloodlightModuleContext used for acquiring
443 * ThreadPoolService and registering MessageListener.
Ray Milkey5df613b2014-04-15 10:50:56 -0700444 * @param basicFactory Factory object to create OFMessage objects.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700445 * @param damper Message damper used for sending messages.
446 * If null, FlowPusher creates its own damper object.
447 */
Ray Milkey5df613b2014-04-15 10:50:56 -0700448 public void init(FloodlightContext floodlightContext,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700449 FloodlightModuleContext modContext,
Ray Milkey5df613b2014-04-15 10:50:56 -0700450 BasicFactory basicFactory,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700451 OFMessageDamper damper) {
Ray Milkey5df613b2014-04-15 10:50:56 -0700452 context = floodlightContext;
453 factory = basicFactory;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700454 this.threadPool = modContext.getServiceImpl(IThreadPoolService.class);
455 IFloodlightProviderService flservice
456 = modContext.getServiceImpl(IFloodlightProviderService.class);
457 flservice.addOFMessageListener(OFType.BARRIER_REPLY, this);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800458
Ray Milkey8e5170e2014-04-02 12:09:55 -0700459 if (damper != null) {
460 messageDamper = damper;
461 } else {
462 // use default values
463 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
464 EnumSet.of(OFType.FLOW_MOD),
465 OFMESSAGE_DAMPER_TIMEOUT);
466 }
467 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800468
Ray Milkey8e5170e2014-04-02 12:09:55 -0700469 /**
470 * Begin processing queue.
471 */
472 public void start() {
473 if (factory == null) {
474 log.error("FlowPusher not yet initialized.");
475 return;
476 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800477
Ray Milkey8e5170e2014-04-02 12:09:55 -0700478 threadMap = new HashMap<Long, FlowPusherThread>();
Ray Milkey2476cac2014-04-08 11:03:21 -0700479 for (long i = 0; i < numberThread; ++i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700480 FlowPusherThread thread = new FlowPusherThread();
Naoki Shiotac1601d32013-11-20 10:47:34 -0800481
Ray Milkey8e5170e2014-04-02 12:09:55 -0700482 threadMap.put(i, thread);
483 thread.start();
484 }
485 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800486
Ray Milkey8e5170e2014-04-02 12:09:55 -0700487 @Override
488 public boolean suspend(IOFSwitch sw) {
489 SwitchQueue queue = getQueue(sw);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700490
Ray Milkey8e5170e2014-04-02 12:09:55 -0700491 if (queue == null) {
492 // create queue in case suspend is called before first message addition
493 queue = createQueueImpl(sw);
494 }
495
496 synchronized (queue) {
497 if (queue.state == QueueState.READY) {
498 queue.state = QueueState.SUSPENDED;
499 return true;
500 }
501 return false;
502 }
503 }
504
505 @Override
506 public boolean resume(IOFSwitch sw) {
507 SwitchQueue queue = getQueue(sw);
508
509 if (queue == null) {
510 log.error("No queue is attached to DPID : {}", sw.getId());
511 return false;
512 }
513
514 synchronized (queue) {
515 if (queue.state == QueueState.SUSPENDED) {
516 queue.state = QueueState.READY;
517
518 // Free the latch if queue has any messages
519 FlowPusherThread thread = getProcessingThread(sw);
520 if (queue.hasMessageToSend()) {
521 thread.notifyMessagePushed();
522 }
523 return true;
524 }
525 return false;
526 }
527 }
528
529 @Override
530 public QueueState getState(IOFSwitch sw) {
531 SwitchQueue queue = getQueue(sw);
532
533 if (queue == null) {
534 return QueueState.UNKNOWN;
535 }
536
537 return queue.state;
538 }
539
540 /**
541 * Stop processing queue and exit thread.
542 */
543 public void stop() {
544 if (threadMap == null) {
545 return;
546 }
547
548 for (FlowPusherThread t : threadMap.values()) {
549 t.interrupt();
550 }
551 }
552
553 @Override
554 public void setRate(IOFSwitch sw, long rate) {
555 SwitchQueue queue = getQueue(sw);
556 if (queue == null) {
557 queue = createQueueImpl(sw);
558 }
559
560 if (rate > 0) {
561 log.debug("rate for {} is set to {}", sw.getId(), rate);
562 synchronized (queue) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700563 queue.maxRate = rate;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700564 }
565 }
566 }
567
568 @Override
Pavlin Radoslavov9fc535a2014-04-11 13:00:12 -0700569 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
570 justification = "Future versions of createQueueImpl() might return null")
Ray Milkey8e5170e2014-04-02 12:09:55 -0700571 public boolean createQueue(IOFSwitch sw) {
572 SwitchQueue queue = createQueueImpl(sw);
573
574 return (queue != null);
575 }
576
577 protected SwitchQueue createQueueImpl(IOFSwitch sw) {
578 SwitchQueue queue = getQueue(sw);
579 if (queue != null) {
580 return queue;
581 }
582
583 FlowPusherThread proc = getProcessingThread(sw);
584 queue = new SwitchQueue();
585 queue.state = QueueState.READY;
586 proc.assignedQueues.put(sw, queue);
587
588 return queue;
589 }
590
591 @Override
592 public boolean deleteQueue(IOFSwitch sw) {
593 return deleteQueue(sw, false);
594 }
595
596 @Override
597 public boolean deleteQueue(IOFSwitch sw, boolean forceStop) {
598 FlowPusherThread proc = getProcessingThread(sw);
599
600 if (forceStop) {
601 SwitchQueue queue = proc.assignedQueues.remove(sw);
602 if (queue == null) {
603 return false;
604 }
605 return true;
606 } else {
607 SwitchQueue queue = getQueue(sw);
608 if (queue == null) {
609 return false;
610 }
611 synchronized (queue) {
612 queue.toBeDeleted = true;
613 }
614 return true;
615 }
616 }
617
618 @Override
619 public boolean add(IOFSwitch sw, OFMessage msg) {
620 return add(sw, msg, MsgPriority.NORMAL);
621 }
622
623 @Override
624 public boolean add(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
625 return addMessageImpl(sw, msg, priority);
626 }
627
628 @Override
629 public void pushFlowEntries(
630 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
631 pushFlowEntries(entries, MsgPriority.NORMAL);
632 }
633
634 @Override
635 public void pushFlowEntries(
636 Collection<Pair<IOFSwitch, FlowEntry>> entries, MsgPriority priority) {
637
638 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Sho SHIMIZU26d77892014-06-10 11:07:06 -0700639 add(entry.getFirst(), entry.getSecond(), priority);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700640 }
641 }
642
643 @Override
644 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) {
645 pushFlowEntry(sw, flowEntry, MsgPriority.NORMAL);
646 }
647
648 @Override
649 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
650 Collection<Pair<IOFSwitch, FlowEntry>> entries =
651 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
652
653 entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry));
654 pushFlowEntries(entries, priority);
655 }
656
657 /**
Ray Milkeyddcd4922014-04-17 11:21:20 -0700658 * Fetch the match conditions.
659 * NOTE: The Flow matching conditions common for all Flow Entries are
660 * used ONLY if a Flow Entry does NOT have the corresponding matching
661 * condition set.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700662 *
Ray Milkeyddcd4922014-04-17 11:21:20 -0700663 * @param flowEntryMatch Flow entry to create a matcher for
664 * @return open flow matcher for the given values
Ray Milkey8e5170e2014-04-02 12:09:55 -0700665 */
Ray Milkeyddcd4922014-04-17 11:21:20 -0700666 private OFMatch computeMatch(FlowEntryMatch flowEntryMatch) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700667 OFMatch match = new OFMatch();
668 match.setWildcards(OFMatch.OFPFW_ALL);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700669
670 // Match the Incoming Port
671 Port matchInPort = flowEntryMatch.inPort();
672 if (matchInPort != null) {
673 match.setInputPort(matchInPort.value());
674 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
675 }
676
677 // Match the Source MAC address
678 MACAddress matchSrcMac = flowEntryMatch.srcMac();
679 if (matchSrcMac != null) {
680 match.setDataLayerSource(matchSrcMac.toString());
681 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
682 }
683
684 // Match the Destination MAC address
685 MACAddress matchDstMac = flowEntryMatch.dstMac();
686 if (matchDstMac != null) {
687 match.setDataLayerDestination(matchDstMac.toString());
688 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
689 }
690
691 // Match the Ethernet Frame Type
692 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
693 if (matchEthernetFrameType != null) {
694 match.setDataLayerType(matchEthernetFrameType);
695 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
696 }
697
698 // Match the VLAN ID
699 Short matchVlanId = flowEntryMatch.vlanId();
700 if (matchVlanId != null) {
701 match.setDataLayerVirtualLan(matchVlanId);
702 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN);
703 }
704
705 // Match the VLAN priority
706 Byte matchVlanPriority = flowEntryMatch.vlanPriority();
707 if (matchVlanPriority != null) {
708 match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority);
709 match.setWildcards(match.getWildcards()
710 & ~OFMatch.OFPFW_DL_VLAN_PCP);
711 }
712
713 // Match the Source IPv4 Network prefix
714 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
715 if (matchSrcIPv4Net != null) {
716 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
717 }
718
719 // Natch the Destination IPv4 Network prefix
720 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
721 if (matchDstIPv4Net != null) {
722 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
723 }
724
725 // Match the IP protocol
726 Byte matchIpProto = flowEntryMatch.ipProto();
727 if (matchIpProto != null) {
728 match.setNetworkProtocol(matchIpProto);
729 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO);
730 }
731
732 // Match the IP ToS (DSCP field, 6 bits)
733 Byte matchIpToS = flowEntryMatch.ipToS();
734 if (matchIpToS != null) {
735 match.setNetworkTypeOfService(matchIpToS);
736 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS);
737 }
738
739 // Match the Source TCP/UDP port
740 Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort();
741 if (matchSrcTcpUdpPort != null) {
742 match.setTransportSource(matchSrcTcpUdpPort);
743 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
744 }
745
746 // Match the Destination TCP/UDP port
747 Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort();
748 if (matchDstTcpUdpPort != null) {
749 match.setTransportDestination(matchDstTcpUdpPort);
750 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST);
751 }
752
Ray Milkeyddcd4922014-04-17 11:21:20 -0700753 return match;
754 }
755
756
757 /**
758 * Wrapper object to hold a port number. Used to pass around output ports.
759 */
760 private static class OutputPort {
761 private Short portNumber;
762 }
763
764 /**
765 * Process a flow action entry, putting the resulting flow
766 * actions into a list. Will also set the actionOutputPort
767 * if one is encountered while processing an action.
768 *
769 * @param action Flow Entry Action to process
770 * @param openFlowActions actions to perform get added to this list
771 * @param actionOutputPort this will get set if an action output
772 * port is found
773 */
774 private void processAction(final FlowEntryAction action,
775 final List<OFAction> openFlowActions,
776 final OutputPort actionOutputPort) {
777 ActionOutput actionOutput = action.actionOutput();
778 ActionSetVlanId actionSetVlanId = action.actionSetVlanId();
779 ActionSetVlanPriority actionSetVlanPriority = action
780 .actionSetVlanPriority();
781 ActionStripVlan actionStripVlan = action.actionStripVlan();
782 ActionSetEthernetAddr actionSetEthernetSrcAddr = action
783 .actionSetEthernetSrcAddr();
784 ActionSetEthernetAddr actionSetEthernetDstAddr = action
785 .actionSetEthernetDstAddr();
786 ActionSetIPv4Addr actionSetIPv4SrcAddr = action
787 .actionSetIPv4SrcAddr();
788 ActionSetIPv4Addr actionSetIPv4DstAddr = action
789 .actionSetIPv4DstAddr();
790 ActionSetIpToS actionSetIpToS = action.actionSetIpToS();
791 ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action
792 .actionSetTcpUdpSrcPort();
793 ActionSetTcpUdpPort actionSetTcpUdpDstPort = action
794 .actionSetTcpUdpDstPort();
795 ActionEnqueue actionEnqueue = action.actionEnqueue();
796
797 if (actionOutput != null) {
798 actionOutputPort.portNumber = actionOutput.port().value();
799 // XXX: The max length is hard-coded for now
800 OFActionOutput ofa = new OFActionOutput(actionOutput.port()
801 .value(), (short) 0xffff);
802 openFlowActions.add(ofa);
803 }
804
805 if (actionSetVlanId != null) {
806 OFActionVirtualLanIdentifier ofa =
807 new OFActionVirtualLanIdentifier(actionSetVlanId.vlanId());
808 openFlowActions.add(ofa);
809 }
810
811 if (actionSetVlanPriority != null) {
812 OFActionVirtualLanPriorityCodePoint ofa =
813 new OFActionVirtualLanPriorityCodePoint(actionSetVlanPriority.vlanPriority());
814 openFlowActions.add(ofa);
815 }
816
817 if (actionStripVlan != null) {
818 if (actionStripVlan.stripVlan()) {
819 OFActionStripVirtualLan ofa = new OFActionStripVirtualLan();
820 openFlowActions.add(ofa);
821 }
822 }
823
824 if (actionSetEthernetSrcAddr != null) {
825 OFActionDataLayerSource ofa =
826 new OFActionDataLayerSource(actionSetEthernetSrcAddr.addr().toBytes());
827 openFlowActions.add(ofa);
828 }
829
830 if (actionSetEthernetDstAddr != null) {
831 OFActionDataLayerDestination ofa =
832 new OFActionDataLayerDestination(actionSetEthernetDstAddr.addr().toBytes());
833 openFlowActions.add(ofa);
834 }
835
836 if (actionSetIPv4SrcAddr != null) {
837 OFActionNetworkLayerSource ofa =
838 new OFActionNetworkLayerSource(actionSetIPv4SrcAddr.addr().value());
839 openFlowActions.add(ofa);
840 }
841
842 if (actionSetIPv4DstAddr != null) {
843 OFActionNetworkLayerDestination ofa =
844 new OFActionNetworkLayerDestination(actionSetIPv4DstAddr.addr().value());
845 openFlowActions.add(ofa);
846 }
847
848 if (actionSetIpToS != null) {
849 OFActionNetworkTypeOfService ofa =
850 new OFActionNetworkTypeOfService(actionSetIpToS.ipToS());
851 openFlowActions.add(ofa);
852 }
853
854 if (actionSetTcpUdpSrcPort != null) {
855 OFActionTransportLayerSource ofa =
856 new OFActionTransportLayerSource(actionSetTcpUdpSrcPort.port());
857 openFlowActions.add(ofa);
858 }
859
860 if (actionSetTcpUdpDstPort != null) {
861 OFActionTransportLayerDestination ofa =
862 new OFActionTransportLayerDestination(actionSetTcpUdpDstPort.port());
863 openFlowActions.add(ofa);
864 }
865
866 if (actionEnqueue != null) {
867 OFActionEnqueue ofa =
868 new OFActionEnqueue(actionEnqueue.port().value(), actionEnqueue.queueId());
869 openFlowActions.add(ofa);
870 }
871 }
872
873
874 /**
875 * Create a message from FlowEntry and add it to the queue of the switch.
876 *
877 * @param sw Switch to which message is pushed.
878 * @param flowEntry FlowEntry object used for creating message.
879 * @return true if message is successfully added to a queue.
880 */
881 private boolean add(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
882 //
883 // Create the OpenFlow Flow Modification Entry to push
884 //
885 OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD);
886 long cookie = flowEntry.flowEntryId().value();
887
888 short flowModCommand = OFFlowMod.OFPFC_ADD;
889 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
890 flowModCommand = OFFlowMod.OFPFC_ADD;
891 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
892 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
893 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
894 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
895 } else {
896 // Unknown user state. Ignore the entry
897 log.debug(
898 "Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
899 flowEntry.flowEntryId(),
900 flowEntry.flowEntryUserState());
901 return false;
902 }
903
904 final FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
905 final OFMatch match = computeMatch(flowEntryMatch);
906
907 final Port matchInPort = flowEntryMatch.inPort();
908 final MACAddress matchSrcMac = flowEntryMatch.srcMac();
909 final MACAddress matchDstMac = flowEntryMatch.dstMac();
910
Ray Milkey8e5170e2014-04-02 12:09:55 -0700911 //
912 // Fetch the actions
913 //
Ray Milkeyddcd4922014-04-17 11:21:20 -0700914 final List<OFAction> openFlowActions = new ArrayList<OFAction>();
915 final FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700916 //
Ray Milkeyddcd4922014-04-17 11:21:20 -0700917 final OutputPort actionOutputPort = new OutputPort();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700918 for (FlowEntryAction action : flowEntryActions.actions()) {
Ray Milkeyddcd4922014-04-17 11:21:20 -0700919 processAction(action, openFlowActions, actionOutputPort);
920 }
921 int actionsLen = 0;
922 for (OFAction ofa : openFlowActions) {
923 actionsLen += ofa.getLength();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700924 }
925
926 fm.setIdleTimeout((short) flowEntry.idleTimeout())
927 .setHardTimeout((short) flowEntry.hardTimeout())
928 .setPriority((short) flowEntry.priority())
929 .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie)
930 .setCommand(flowModCommand).setMatch(match)
931 .setActions(openFlowActions)
932 .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen);
933 fm.setOutPort(OFPort.OFPP_NONE.getValue());
934 if ((flowModCommand == OFFlowMod.OFPFC_DELETE)
935 || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
Ray Milkeyddcd4922014-04-17 11:21:20 -0700936 if (actionOutputPort.portNumber != null) {
937 fm.setOutPort(actionOutputPort.portNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700938 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700939 }
940
941 //
942 // Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not
943 // permanent.
944 //
945 if ((flowEntry.idleTimeout() != 0) ||
946 (flowEntry.hardTimeout() != 0)) {
947 fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
948 }
949
950 if (log.isTraceEnabled()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700951 log.trace("Installing flow entry {} into switch DPID: {} " +
952 "flowEntryId: {} srcMac: {} dstMac: {} inPort: {} outPort: {}"
Ray Milkey8e5170e2014-04-02 12:09:55 -0700953 , flowEntry.flowEntryUserState()
954 , sw.getStringId()
955 , flowEntry.flowEntryId()
956 , matchSrcMac
957 , matchDstMac
958 , matchInPort
959 , actionOutputPort
960 );
961 }
962
963 return addMessageImpl(sw, fm, priority);
964 }
965
966 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700967 * Add message to queue.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700968 *
969 * @param sw
970 * @param msg
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -0700971 * @param priority
Jonathan Hart99ff20a2014-06-15 16:53:00 -0700972 * @return true if the message was added successfully, otherwise false
Ray Milkey8e5170e2014-04-02 12:09:55 -0700973 */
974 protected boolean addMessageImpl(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
975 FlowPusherThread thread = getProcessingThread(sw);
976
977 SwitchQueue queue = getQueue(sw);
978
979 // create queue at first addition of message
980 if (queue == null) {
981 queue = createQueueImpl(sw);
982 }
983
984 SwitchQueueEntry entry = new SwitchQueueEntry(msg);
985
986 synchronized (queue) {
987 queue.add(entry, priority);
988 if (log.isTraceEnabled()) {
989 log.trace("Message is pushed : {}", entry.getOFMessage());
990 }
991 }
992
993 thread.notifyMessagePushed();
994
995 return true;
996 }
997
998 @Override
999 public OFBarrierReply barrier(IOFSwitch sw) {
1000 OFMessageFuture<OFBarrierReply> future = barrierAsync(sw);
1001 if (future == null) {
1002 return null;
1003 }
1004
1005 try {
1006 return future.get();
1007 } catch (InterruptedException e) {
1008 e.printStackTrace();
1009 log.error("InterruptedException: {}", e);
1010 return null;
1011 } catch (ExecutionException e) {
1012 e.printStackTrace();
1013 log.error("ExecutionException: {}", e);
1014 return null;
1015 }
1016 }
1017
1018 @Override
1019 public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) {
1020 // TODO creation of message and future should be moved to OFSwitchImpl
1021
1022 if (sw == null) {
1023 return null;
1024 }
1025
1026 OFBarrierRequest msg = createBarrierRequest(sw);
1027
1028 OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid());
1029 barrierFutures.put(BarrierInfo.create(sw, msg), future);
1030
1031 addMessageImpl(sw, msg, MsgPriority.NORMAL);
1032
1033 return future;
1034 }
1035
1036 protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) {
1037 OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST);
1038 msg.setXid(sw.getNextTransactionId());
1039
1040 return msg;
1041 }
1042
1043 /**
1044 * Get a queue attached to a switch.
1045 *
1046 * @param sw Switch object
1047 * @return Queue object
1048 */
1049 protected SwitchQueue getQueue(IOFSwitch sw) {
1050 if (sw == null) {
1051 return null;
1052 }
1053
1054 FlowPusherThread th = getProcessingThread(sw);
1055 if (th == null) {
1056 return null;
1057 }
1058
1059 return th.assignedQueues.get(sw);
1060 }
1061
1062 /**
1063 * Get a hash value correspondent to a switch.
1064 *
1065 * @param sw Switch object
1066 * @return Hash value
1067 */
1068 protected long getHash(IOFSwitch sw) {
1069 // This code assumes DPID is sequentially assigned.
1070 // TODO consider equalization algorithm
Ray Milkey2476cac2014-04-08 11:03:21 -07001071 return sw.getId() % numberThread;
Ray Milkey8e5170e2014-04-02 12:09:55 -07001072 }
1073
1074 /**
1075 * Get a Thread object which processes the queue attached to a switch.
1076 *
1077 * @param sw Switch object
1078 * @return Thread object
1079 */
1080 protected FlowPusherThread getProcessingThread(IOFSwitch sw) {
1081 long hash = getHash(sw);
1082
1083 return threadMap.get(hash);
1084 }
1085
1086 @Override
1087 public String getName() {
1088 return "flowpusher";
1089 }
1090
1091 @Override
1092 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1093 return false;
1094 }
1095
1096 @Override
1097 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1098 return false;
1099 }
1100
1101 @Override
1102 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
1103 if (log.isTraceEnabled()) {
1104 log.trace("Received BARRIER_REPLY from : {}", sw.getId());
1105 }
1106
Pavlin Radoslavov0b88a262014-04-10 15:43:27 -07001107 if ((msg.getType() != OFType.BARRIER_REPLY) ||
1108 !(msg instanceof OFBarrierReply)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -07001109 log.error("Unexpected reply message : {}", msg.getType());
1110 return Command.CONTINUE;
1111 }
1112
1113 OFBarrierReply reply = (OFBarrierReply) msg;
1114 BarrierInfo info = BarrierInfo.create(sw, reply);
1115
1116 // Deliver future if exists
1117 OFBarrierReplyFuture future = barrierFutures.get(info);
1118 if (future != null) {
1119 future.deliverFuture(sw, msg);
1120 barrierFutures.remove(info);
1121 }
1122
1123 return Command.CONTINUE;
1124 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07001125}