blob: a660cd66a322de192265c429a28c39c9a1c7faac [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 Shiota47993102014-07-09 14:00:54 -07009import java.util.Iterator;
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 Shiota47993102014-07-09 14:00:54 -070013import java.util.Map.Entry;
Naoki Shiota8df97bc2014-03-13 18:42:23 -070014import java.util.Queue;
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;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070046import net.onrc.onos.core.util.PortNumber;
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
Naoki Shiota47993102014-07-09 14:00:54 -0700327 for (Iterator<Entry<IOFSwitch, SwitchQueue>> it = assignedQueues.entrySet().iterator();
328 it.hasNext();
329 ) {
330 Entry<IOFSwitch, SwitchQueue> entry = it.next();
331 IOFSwitch sw = entry.getKey();
332 SwitchQueue queue = entry.getValue();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800333
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -0700334 if (queue == null) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700335 continue;
336 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800337
Ray Milkey8e5170e2014-04-02 12:09:55 -0700338 synchronized (queue) {
339 processQueue(sw, queue, MAX_MESSAGE_SEND);
340 if (queue.toBeDeleted && !queue.hasMessageToSend()) {
341 // remove queue if flagged to be.
Naoki Shiota47993102014-07-09 14:00:54 -0700342 it.remove();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700343 }
344 }
345 }
346 }
347 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800348
Ray Milkey8e5170e2014-04-02 12:09:55 -0700349 /**
350 * Read messages from queue and send them to the switch.
351 * If number of messages excess the limit, stop sending messages.
352 *
353 * @param sw Switch to which messages will be sent.
354 * @param queue Queue of messages.
Ray Milkey9526d6f2014-04-10 14:54:15 -0700355 * @param maxMsg Limitation of number of messages to be sent. If set to 0,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700356 * all messages in queue will be sent.
357 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700358 private void processQueue(IOFSwitch sw, SwitchQueue queue, int maxMsg) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700359 // check sending rate and determine it to be sent or not
Ray Milkey2476cac2014-04-08 11:03:21 -0700360 long currentTime = System.currentTimeMillis();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700361 long size = 0;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800362
Naoki Shiota47993102014-07-09 14:00:54 -0700363 if (sw.isConnected() && queue.isSendable(currentTime)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700364 int i = 0;
365 while (queue.hasMessageToSend()) {
366 // Number of messages excess the limit
Ray Milkey9526d6f2014-04-10 14:54:15 -0700367 if (0 < maxMsg && maxMsg <= i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700368 break;
369 }
370 ++i;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800371
Ray Milkey8e5170e2014-04-02 12:09:55 -0700372 SwitchQueueEntry queueEntry;
373 synchronized (queue) {
374 queueEntry = queue.poll();
375 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800376
Ray Milkey8e5170e2014-04-02 12:09:55 -0700377 OFMessage msg = queueEntry.getOFMessage();
378 try {
379 messageDamper.write(sw, msg, context);
380 if (log.isTraceEnabled()) {
381 log.trace("Pusher sends message : {}", msg);
382 }
383 size += msg.getLength();
384 } catch (IOException e) {
385 e.printStackTrace();
386 log.error("Exception in sending message ({}) : {}", msg, e);
387 }
388 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800389
Ray Milkey8e5170e2014-04-02 12:09:55 -0700390 sw.flush();
Ray Milkey2476cac2014-04-08 11:03:21 -0700391 queue.logSentData(currentTime, size);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700392 }
393 }
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800394
Ray Milkey8e5170e2014-04-02 12:09:55 -0700395 private boolean queuesHasMessageToSend() {
396 for (SwitchQueue queue : assignedQueues.values()) {
397 if (queue.hasMessageToSend()) {
398 return true;
399 }
400 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700401
Ray Milkey8e5170e2014-04-02 12:09:55 -0700402 return false;
403 }
Naoki Shiota05334692014-03-18 16:06:36 -0700404
Ray Milkey8e5170e2014-04-02 12:09:55 -0700405 private void notifyMessagePushed() {
406 queuingLock.lock();
407 try {
408 messagePushed.signal();
409 } finally {
410 queuingLock.unlock();
411 }
412 }
413 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700414
Ray Milkey8e5170e2014-04-02 12:09:55 -0700415 /**
416 * Initialize object with one thread.
417 */
418 public FlowPusher() {
Ray Milkey2476cac2014-04-08 11:03:21 -0700419 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700420 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800421
Ray Milkey8e5170e2014-04-02 12:09:55 -0700422 /**
423 * Initialize object with threads of given number.
424 *
Ray Milkey9526d6f2014-04-10 14:54:15 -0700425 * @param numberThreadValue Number of threads to handle messages.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700426 */
Ray Milkey9526d6f2014-04-10 14:54:15 -0700427 public FlowPusher(int numberThreadValue) {
428 if (numberThreadValue > 0) {
429 numberThread = numberThreadValue;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700430 } else {
Ray Milkey9526d6f2014-04-10 14:54:15 -0700431 numberThread = DEFAULT_NUMBER_THREAD;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700432 }
433 }
Naoki Shiota81dbe302013-11-21 15:35:38 -0800434
Ray Milkey8e5170e2014-04-02 12:09:55 -0700435 /**
436 * Set parameters needed for sending messages.
437 *
Ray Milkey5df613b2014-04-15 10:50:56 -0700438 * @param floodlightContext FloodlightContext used for sending messages.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700439 * If null, FlowPusher uses default context.
440 * @param modContext FloodlightModuleContext used for acquiring
441 * ThreadPoolService and registering MessageListener.
Ray Milkey5df613b2014-04-15 10:50:56 -0700442 * @param basicFactory Factory object to create OFMessage objects.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700443 * @param damper Message damper used for sending messages.
444 * If null, FlowPusher creates its own damper object.
445 */
Ray Milkey5df613b2014-04-15 10:50:56 -0700446 public void init(FloodlightContext floodlightContext,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700447 FloodlightModuleContext modContext,
Ray Milkey5df613b2014-04-15 10:50:56 -0700448 BasicFactory basicFactory,
Ray Milkey8e5170e2014-04-02 12:09:55 -0700449 OFMessageDamper damper) {
Ray Milkey5df613b2014-04-15 10:50:56 -0700450 context = floodlightContext;
451 factory = basicFactory;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700452 this.threadPool = modContext.getServiceImpl(IThreadPoolService.class);
453 IFloodlightProviderService flservice
454 = modContext.getServiceImpl(IFloodlightProviderService.class);
455 flservice.addOFMessageListener(OFType.BARRIER_REPLY, this);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800456
Ray Milkey8e5170e2014-04-02 12:09:55 -0700457 if (damper != null) {
458 messageDamper = damper;
459 } else {
460 // use default values
461 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
462 EnumSet.of(OFType.FLOW_MOD),
463 OFMESSAGE_DAMPER_TIMEOUT);
464 }
465 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800466
Ray Milkey8e5170e2014-04-02 12:09:55 -0700467 /**
468 * Begin processing queue.
469 */
470 public void start() {
471 if (factory == null) {
472 log.error("FlowPusher not yet initialized.");
473 return;
474 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800475
Ray Milkey8e5170e2014-04-02 12:09:55 -0700476 threadMap = new HashMap<Long, FlowPusherThread>();
Ray Milkey2476cac2014-04-08 11:03:21 -0700477 for (long i = 0; i < numberThread; ++i) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700478 FlowPusherThread thread = new FlowPusherThread();
Naoki Shiotac1601d32013-11-20 10:47:34 -0800479
Ray Milkey8e5170e2014-04-02 12:09:55 -0700480 threadMap.put(i, thread);
481 thread.start();
482 }
483 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800484
Ray Milkey8e5170e2014-04-02 12:09:55 -0700485 @Override
486 public boolean suspend(IOFSwitch sw) {
487 SwitchQueue queue = getQueue(sw);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700488
Ray Milkey8e5170e2014-04-02 12:09:55 -0700489 if (queue == null) {
490 // create queue in case suspend is called before first message addition
491 queue = createQueueImpl(sw);
492 }
493
494 synchronized (queue) {
495 if (queue.state == QueueState.READY) {
496 queue.state = QueueState.SUSPENDED;
497 return true;
498 }
499 return false;
500 }
501 }
502
503 @Override
504 public boolean resume(IOFSwitch sw) {
505 SwitchQueue queue = getQueue(sw);
506
507 if (queue == null) {
508 log.error("No queue is attached to DPID : {}", sw.getId());
509 return false;
510 }
511
512 synchronized (queue) {
513 if (queue.state == QueueState.SUSPENDED) {
514 queue.state = QueueState.READY;
515
516 // Free the latch if queue has any messages
517 FlowPusherThread thread = getProcessingThread(sw);
518 if (queue.hasMessageToSend()) {
519 thread.notifyMessagePushed();
520 }
521 return true;
522 }
523 return false;
524 }
525 }
526
527 @Override
528 public QueueState getState(IOFSwitch sw) {
529 SwitchQueue queue = getQueue(sw);
530
531 if (queue == null) {
532 return QueueState.UNKNOWN;
533 }
534
535 return queue.state;
536 }
537
538 /**
539 * Stop processing queue and exit thread.
540 */
541 public void stop() {
542 if (threadMap == null) {
543 return;
544 }
545
546 for (FlowPusherThread t : threadMap.values()) {
547 t.interrupt();
548 }
549 }
550
551 @Override
552 public void setRate(IOFSwitch sw, long rate) {
553 SwitchQueue queue = getQueue(sw);
554 if (queue == null) {
555 queue = createQueueImpl(sw);
556 }
557
558 if (rate > 0) {
559 log.debug("rate for {} is set to {}", sw.getId(), rate);
560 synchronized (queue) {
Ray Milkey2476cac2014-04-08 11:03:21 -0700561 queue.maxRate = rate;
Ray Milkey8e5170e2014-04-02 12:09:55 -0700562 }
563 }
564 }
565
566 @Override
Pavlin Radoslavov9fc535a2014-04-11 13:00:12 -0700567 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
568 justification = "Future versions of createQueueImpl() might return null")
Ray Milkey8e5170e2014-04-02 12:09:55 -0700569 public boolean createQueue(IOFSwitch sw) {
570 SwitchQueue queue = createQueueImpl(sw);
571
572 return (queue != null);
573 }
574
575 protected SwitchQueue createQueueImpl(IOFSwitch sw) {
576 SwitchQueue queue = getQueue(sw);
577 if (queue != null) {
578 return queue;
579 }
580
581 FlowPusherThread proc = getProcessingThread(sw);
582 queue = new SwitchQueue();
583 queue.state = QueueState.READY;
584 proc.assignedQueues.put(sw, queue);
585
586 return queue;
587 }
588
589 @Override
590 public boolean deleteQueue(IOFSwitch sw) {
591 return deleteQueue(sw, false);
592 }
593
594 @Override
595 public boolean deleteQueue(IOFSwitch sw, boolean forceStop) {
596 FlowPusherThread proc = getProcessingThread(sw);
597
598 if (forceStop) {
599 SwitchQueue queue = proc.assignedQueues.remove(sw);
600 if (queue == null) {
601 return false;
602 }
603 return true;
604 } else {
605 SwitchQueue queue = getQueue(sw);
606 if (queue == null) {
607 return false;
608 }
609 synchronized (queue) {
610 queue.toBeDeleted = true;
611 }
612 return true;
613 }
614 }
615
616 @Override
617 public boolean add(IOFSwitch sw, OFMessage msg) {
618 return add(sw, msg, MsgPriority.NORMAL);
619 }
620
621 @Override
622 public boolean add(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
623 return addMessageImpl(sw, msg, priority);
624 }
625
626 @Override
627 public void pushFlowEntries(
628 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
629 pushFlowEntries(entries, MsgPriority.NORMAL);
630 }
631
632 @Override
633 public void pushFlowEntries(
634 Collection<Pair<IOFSwitch, FlowEntry>> entries, MsgPriority priority) {
635
636 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Sho SHIMIZU26d77892014-06-10 11:07:06 -0700637 add(entry.getFirst(), entry.getSecond(), priority);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700638 }
639 }
640
641 @Override
642 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) {
643 pushFlowEntry(sw, flowEntry, MsgPriority.NORMAL);
644 }
645
646 @Override
647 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
648 Collection<Pair<IOFSwitch, FlowEntry>> entries =
649 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
650
651 entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry));
652 pushFlowEntries(entries, priority);
653 }
654
655 /**
Ray Milkeyddcd4922014-04-17 11:21:20 -0700656 * Fetch the match conditions.
657 * NOTE: The Flow matching conditions common for all Flow Entries are
658 * used ONLY if a Flow Entry does NOT have the corresponding matching
659 * condition set.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700660 *
Ray Milkeyddcd4922014-04-17 11:21:20 -0700661 * @param flowEntryMatch Flow entry to create a matcher for
662 * @return open flow matcher for the given values
Ray Milkey8e5170e2014-04-02 12:09:55 -0700663 */
Ray Milkeyddcd4922014-04-17 11:21:20 -0700664 private OFMatch computeMatch(FlowEntryMatch flowEntryMatch) {
Ray Milkey8e5170e2014-04-02 12:09:55 -0700665 OFMatch match = new OFMatch();
666 match.setWildcards(OFMatch.OFPFW_ALL);
Ray Milkey8e5170e2014-04-02 12:09:55 -0700667
668 // Match the Incoming Port
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700669 PortNumber matchInPort = flowEntryMatch.inPort();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700670 if (matchInPort != null) {
671 match.setInputPort(matchInPort.value());
672 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
673 }
674
675 // Match the Source MAC address
676 MACAddress matchSrcMac = flowEntryMatch.srcMac();
677 if (matchSrcMac != null) {
678 match.setDataLayerSource(matchSrcMac.toString());
679 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
680 }
681
682 // Match the Destination MAC address
683 MACAddress matchDstMac = flowEntryMatch.dstMac();
684 if (matchDstMac != null) {
685 match.setDataLayerDestination(matchDstMac.toString());
686 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
687 }
688
689 // Match the Ethernet Frame Type
690 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
691 if (matchEthernetFrameType != null) {
692 match.setDataLayerType(matchEthernetFrameType);
693 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
694 }
695
696 // Match the VLAN ID
697 Short matchVlanId = flowEntryMatch.vlanId();
698 if (matchVlanId != null) {
699 match.setDataLayerVirtualLan(matchVlanId);
700 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN);
701 }
702
703 // Match the VLAN priority
704 Byte matchVlanPriority = flowEntryMatch.vlanPriority();
705 if (matchVlanPriority != null) {
706 match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority);
707 match.setWildcards(match.getWildcards()
708 & ~OFMatch.OFPFW_DL_VLAN_PCP);
709 }
710
711 // Match the Source IPv4 Network prefix
712 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
713 if (matchSrcIPv4Net != null) {
714 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
715 }
716
717 // Natch the Destination IPv4 Network prefix
718 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
719 if (matchDstIPv4Net != null) {
720 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
721 }
722
723 // Match the IP protocol
724 Byte matchIpProto = flowEntryMatch.ipProto();
725 if (matchIpProto != null) {
726 match.setNetworkProtocol(matchIpProto);
727 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO);
728 }
729
730 // Match the IP ToS (DSCP field, 6 bits)
731 Byte matchIpToS = flowEntryMatch.ipToS();
732 if (matchIpToS != null) {
733 match.setNetworkTypeOfService(matchIpToS);
734 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS);
735 }
736
737 // Match the Source TCP/UDP port
738 Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort();
739 if (matchSrcTcpUdpPort != null) {
740 match.setTransportSource(matchSrcTcpUdpPort);
741 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
742 }
743
744 // Match the Destination TCP/UDP port
745 Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort();
746 if (matchDstTcpUdpPort != null) {
747 match.setTransportDestination(matchDstTcpUdpPort);
748 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST);
749 }
750
Ray Milkeyddcd4922014-04-17 11:21:20 -0700751 return match;
752 }
753
754
755 /**
756 * Wrapper object to hold a port number. Used to pass around output ports.
757 */
758 private static class OutputPort {
759 private Short portNumber;
760 }
761
762 /**
763 * Process a flow action entry, putting the resulting flow
764 * actions into a list. Will also set the actionOutputPort
765 * if one is encountered while processing an action.
766 *
767 * @param action Flow Entry Action to process
768 * @param openFlowActions actions to perform get added to this list
769 * @param actionOutputPort this will get set if an action output
770 * port is found
771 */
772 private void processAction(final FlowEntryAction action,
773 final List<OFAction> openFlowActions,
774 final OutputPort actionOutputPort) {
775 ActionOutput actionOutput = action.actionOutput();
776 ActionSetVlanId actionSetVlanId = action.actionSetVlanId();
777 ActionSetVlanPriority actionSetVlanPriority = action
778 .actionSetVlanPriority();
779 ActionStripVlan actionStripVlan = action.actionStripVlan();
780 ActionSetEthernetAddr actionSetEthernetSrcAddr = action
781 .actionSetEthernetSrcAddr();
782 ActionSetEthernetAddr actionSetEthernetDstAddr = action
783 .actionSetEthernetDstAddr();
784 ActionSetIPv4Addr actionSetIPv4SrcAddr = action
785 .actionSetIPv4SrcAddr();
786 ActionSetIPv4Addr actionSetIPv4DstAddr = action
787 .actionSetIPv4DstAddr();
788 ActionSetIpToS actionSetIpToS = action.actionSetIpToS();
789 ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action
790 .actionSetTcpUdpSrcPort();
791 ActionSetTcpUdpPort actionSetTcpUdpDstPort = action
792 .actionSetTcpUdpDstPort();
793 ActionEnqueue actionEnqueue = action.actionEnqueue();
794
795 if (actionOutput != null) {
796 actionOutputPort.portNumber = actionOutput.port().value();
797 // XXX: The max length is hard-coded for now
798 OFActionOutput ofa = new OFActionOutput(actionOutput.port()
799 .value(), (short) 0xffff);
800 openFlowActions.add(ofa);
801 }
802
803 if (actionSetVlanId != null) {
804 OFActionVirtualLanIdentifier ofa =
805 new OFActionVirtualLanIdentifier(actionSetVlanId.vlanId());
806 openFlowActions.add(ofa);
807 }
808
809 if (actionSetVlanPriority != null) {
810 OFActionVirtualLanPriorityCodePoint ofa =
811 new OFActionVirtualLanPriorityCodePoint(actionSetVlanPriority.vlanPriority());
812 openFlowActions.add(ofa);
813 }
814
815 if (actionStripVlan != null) {
816 if (actionStripVlan.stripVlan()) {
817 OFActionStripVirtualLan ofa = new OFActionStripVirtualLan();
818 openFlowActions.add(ofa);
819 }
820 }
821
822 if (actionSetEthernetSrcAddr != null) {
823 OFActionDataLayerSource ofa =
824 new OFActionDataLayerSource(actionSetEthernetSrcAddr.addr().toBytes());
825 openFlowActions.add(ofa);
826 }
827
828 if (actionSetEthernetDstAddr != null) {
829 OFActionDataLayerDestination ofa =
830 new OFActionDataLayerDestination(actionSetEthernetDstAddr.addr().toBytes());
831 openFlowActions.add(ofa);
832 }
833
834 if (actionSetIPv4SrcAddr != null) {
835 OFActionNetworkLayerSource ofa =
836 new OFActionNetworkLayerSource(actionSetIPv4SrcAddr.addr().value());
837 openFlowActions.add(ofa);
838 }
839
840 if (actionSetIPv4DstAddr != null) {
841 OFActionNetworkLayerDestination ofa =
842 new OFActionNetworkLayerDestination(actionSetIPv4DstAddr.addr().value());
843 openFlowActions.add(ofa);
844 }
845
846 if (actionSetIpToS != null) {
847 OFActionNetworkTypeOfService ofa =
848 new OFActionNetworkTypeOfService(actionSetIpToS.ipToS());
849 openFlowActions.add(ofa);
850 }
851
852 if (actionSetTcpUdpSrcPort != null) {
853 OFActionTransportLayerSource ofa =
854 new OFActionTransportLayerSource(actionSetTcpUdpSrcPort.port());
855 openFlowActions.add(ofa);
856 }
857
858 if (actionSetTcpUdpDstPort != null) {
859 OFActionTransportLayerDestination ofa =
860 new OFActionTransportLayerDestination(actionSetTcpUdpDstPort.port());
861 openFlowActions.add(ofa);
862 }
863
864 if (actionEnqueue != null) {
865 OFActionEnqueue ofa =
866 new OFActionEnqueue(actionEnqueue.port().value(), actionEnqueue.queueId());
867 openFlowActions.add(ofa);
868 }
869 }
870
871
872 /**
873 * Create a message from FlowEntry and add it to the queue of the switch.
874 *
875 * @param sw Switch to which message is pushed.
876 * @param flowEntry FlowEntry object used for creating message.
877 * @return true if message is successfully added to a queue.
878 */
879 private boolean add(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) {
880 //
881 // Create the OpenFlow Flow Modification Entry to push
882 //
883 OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD);
884 long cookie = flowEntry.flowEntryId().value();
885
886 short flowModCommand = OFFlowMod.OFPFC_ADD;
887 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
888 flowModCommand = OFFlowMod.OFPFC_ADD;
889 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
890 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
891 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
892 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
893 } else {
894 // Unknown user state. Ignore the entry
895 log.debug(
896 "Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
897 flowEntry.flowEntryId(),
898 flowEntry.flowEntryUserState());
899 return false;
900 }
901
902 final FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
903 final OFMatch match = computeMatch(flowEntryMatch);
904
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700905 final PortNumber matchInPort = flowEntryMatch.inPort();
Ray Milkeyddcd4922014-04-17 11:21:20 -0700906 final MACAddress matchSrcMac = flowEntryMatch.srcMac();
907 final MACAddress matchDstMac = flowEntryMatch.dstMac();
908
Ray Milkey8e5170e2014-04-02 12:09:55 -0700909 //
910 // Fetch the actions
911 //
Ray Milkeyddcd4922014-04-17 11:21:20 -0700912 final List<OFAction> openFlowActions = new ArrayList<OFAction>();
913 final FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700914 //
Ray Milkeyddcd4922014-04-17 11:21:20 -0700915 final OutputPort actionOutputPort = new OutputPort();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700916 for (FlowEntryAction action : flowEntryActions.actions()) {
Ray Milkeyddcd4922014-04-17 11:21:20 -0700917 processAction(action, openFlowActions, actionOutputPort);
918 }
919 int actionsLen = 0;
920 for (OFAction ofa : openFlowActions) {
921 actionsLen += ofa.getLength();
Ray Milkey8e5170e2014-04-02 12:09:55 -0700922 }
923
924 fm.setIdleTimeout((short) flowEntry.idleTimeout())
925 .setHardTimeout((short) flowEntry.hardTimeout())
926 .setPriority((short) flowEntry.priority())
927 .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie)
928 .setCommand(flowModCommand).setMatch(match)
929 .setActions(openFlowActions)
930 .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen);
931 fm.setOutPort(OFPort.OFPP_NONE.getValue());
932 if ((flowModCommand == OFFlowMod.OFPFC_DELETE)
933 || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
Ray Milkeyddcd4922014-04-17 11:21:20 -0700934 if (actionOutputPort.portNumber != null) {
935 fm.setOutPort(actionOutputPort.portNumber);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700936 }
Ray Milkey8e5170e2014-04-02 12:09:55 -0700937 }
938
939 //
940 // Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not
941 // permanent.
942 //
943 if ((flowEntry.idleTimeout() != 0) ||
944 (flowEntry.hardTimeout() != 0)) {
945 fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
946 }
947
948 if (log.isTraceEnabled()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700949 log.trace("Installing flow entry {} into switch DPID: {} " +
950 "flowEntryId: {} srcMac: {} dstMac: {} inPort: {} outPort: {}"
Ray Milkey8e5170e2014-04-02 12:09:55 -0700951 , flowEntry.flowEntryUserState()
952 , sw.getStringId()
953 , flowEntry.flowEntryId()
954 , matchSrcMac
955 , matchDstMac
956 , matchInPort
957 , actionOutputPort
958 );
959 }
960
961 return addMessageImpl(sw, fm, priority);
962 }
963
964 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700965 * Add message to queue.
Ray Milkey8e5170e2014-04-02 12:09:55 -0700966 *
967 * @param sw
968 * @param msg
Sho SHIMIZUa1199fa2014-06-10 18:11:12 -0700969 * @param priority
Jonathan Hart99ff20a2014-06-15 16:53:00 -0700970 * @return true if the message was added successfully, otherwise false
Ray Milkey8e5170e2014-04-02 12:09:55 -0700971 */
972 protected boolean addMessageImpl(IOFSwitch sw, OFMessage msg, MsgPriority priority) {
973 FlowPusherThread thread = getProcessingThread(sw);
974
975 SwitchQueue queue = getQueue(sw);
976
977 // create queue at first addition of message
978 if (queue == null) {
979 queue = createQueueImpl(sw);
980 }
981
982 SwitchQueueEntry entry = new SwitchQueueEntry(msg);
983
984 synchronized (queue) {
985 queue.add(entry, priority);
986 if (log.isTraceEnabled()) {
987 log.trace("Message is pushed : {}", entry.getOFMessage());
988 }
989 }
990
991 thread.notifyMessagePushed();
992
993 return true;
994 }
995
996 @Override
997 public OFBarrierReply barrier(IOFSwitch sw) {
998 OFMessageFuture<OFBarrierReply> future = barrierAsync(sw);
999 if (future == null) {
1000 return null;
1001 }
1002
1003 try {
1004 return future.get();
1005 } catch (InterruptedException e) {
1006 e.printStackTrace();
1007 log.error("InterruptedException: {}", e);
1008 return null;
1009 } catch (ExecutionException e) {
1010 e.printStackTrace();
1011 log.error("ExecutionException: {}", e);
1012 return null;
1013 }
1014 }
1015
1016 @Override
1017 public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) {
1018 // TODO creation of message and future should be moved to OFSwitchImpl
1019
1020 if (sw == null) {
1021 return null;
1022 }
1023
1024 OFBarrierRequest msg = createBarrierRequest(sw);
1025
1026 OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid());
1027 barrierFutures.put(BarrierInfo.create(sw, msg), future);
1028
1029 addMessageImpl(sw, msg, MsgPriority.NORMAL);
1030
1031 return future;
1032 }
1033
1034 protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) {
1035 OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST);
1036 msg.setXid(sw.getNextTransactionId());
1037
1038 return msg;
1039 }
1040
1041 /**
1042 * Get a queue attached to a switch.
1043 *
1044 * @param sw Switch object
1045 * @return Queue object
1046 */
1047 protected SwitchQueue getQueue(IOFSwitch sw) {
1048 if (sw == null) {
1049 return null;
1050 }
1051
1052 FlowPusherThread th = getProcessingThread(sw);
1053 if (th == null) {
1054 return null;
1055 }
1056
1057 return th.assignedQueues.get(sw);
1058 }
1059
1060 /**
1061 * Get a hash value correspondent to a switch.
1062 *
1063 * @param sw Switch object
1064 * @return Hash value
1065 */
1066 protected long getHash(IOFSwitch sw) {
1067 // This code assumes DPID is sequentially assigned.
1068 // TODO consider equalization algorithm
Ray Milkey2476cac2014-04-08 11:03:21 -07001069 return sw.getId() % numberThread;
Ray Milkey8e5170e2014-04-02 12:09:55 -07001070 }
1071
1072 /**
1073 * Get a Thread object which processes the queue attached to a switch.
1074 *
1075 * @param sw Switch object
1076 * @return Thread object
1077 */
1078 protected FlowPusherThread getProcessingThread(IOFSwitch sw) {
1079 long hash = getHash(sw);
1080
1081 return threadMap.get(hash);
1082 }
1083
1084 @Override
1085 public String getName() {
1086 return "flowpusher";
1087 }
1088
1089 @Override
1090 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1091 return false;
1092 }
1093
1094 @Override
1095 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1096 return false;
1097 }
1098
1099 @Override
1100 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
1101 if (log.isTraceEnabled()) {
1102 log.trace("Received BARRIER_REPLY from : {}", sw.getId());
1103 }
1104
Pavlin Radoslavov0b88a262014-04-10 15:43:27 -07001105 if ((msg.getType() != OFType.BARRIER_REPLY) ||
1106 !(msg instanceof OFBarrierReply)) {
Ray Milkey8e5170e2014-04-02 12:09:55 -07001107 log.error("Unexpected reply message : {}", msg.getType());
1108 return Command.CONTINUE;
1109 }
1110
1111 OFBarrierReply reply = (OFBarrierReply) msg;
1112 BarrierInfo info = BarrierInfo.create(sw, reply);
1113
1114 // Deliver future if exists
1115 OFBarrierReplyFuture future = barrierFutures.get(info);
1116 if (future != null) {
1117 future.deliverFuture(sw, msg);
1118 barrierFutures.remove(info);
1119 }
1120
1121 return Command.CONTINUE;
1122 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07001123}