blob: 3f61248af9202198dc3f362bca72f248b7c56bfe [file] [log] [blame]
Naoki Shiotaaea88582013-11-12 17:58:34 -08001package net.onrc.onos.ofcontroller.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 Shiota5c8d19f2013-11-05 15:52:38 -080013import java.util.Set;
Naoki Shiotac1601d32013-11-20 10:47:34 -080014import java.util.concurrent.ExecutionException;
Naoki Shiota81dbe302013-11-21 15:35:38 -080015import java.util.concurrent.Semaphore;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070016
Naoki Shiota7d0cf272013-11-05 10:18:12 -080017import org.openflow.protocol.*;
18import org.openflow.protocol.action.*;
19import org.openflow.protocol.factory.BasicFactory;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070022
23import 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;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -080032import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080033import net.onrc.onos.ofcontroller.util.FlowEntryAction;
34import net.onrc.onos.ofcontroller.util.FlowEntryAction.*;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080035import net.onrc.onos.ofcontroller.util.FlowEntry;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080036import net.onrc.onos.ofcontroller.util.FlowEntryActions;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080037import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
38import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080039import net.onrc.onos.ofcontroller.util.IPv4Net;
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -080040import net.onrc.onos.ofcontroller.util.Pair;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080041import net.onrc.onos.ofcontroller.util.Port;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070042
43/**
Naoki Shiotab485d412013-11-26 12:04:19 -080044 * FlowPusher is a implementation of FlowPusherService.
45 * FlowPusher assigns one message queue instance for each one switch.
46 * Number of message processing threads is configurable by constructor, and
47 * one thread can handle multiple message queues. Each queue will be assigned to
48 * a thread according to hash function defined by getHash().
49 * Each processing thread reads messages from queues and sends it to switches
50 * in round-robin. Processing thread also calculates rate of sending to suppress
51 * excessive message sending.
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070052 * @author Naoki Shiota
53 *
54 */
Naoki Shiotac1601d32013-11-20 10:47:34 -080055public class FlowPusher implements IFlowPusherService, IOFMessageListener {
Naoki Shiota7d0cf272013-11-05 10:18:12 -080056 private final static Logger log = LoggerFactory.getLogger(FlowPusher.class);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -080057 protected volatile IFlowService flowManager;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080058
59 // NOTE: Below are moved from FlowManager.
60 // TODO: Values copied from elsewhere (class LearningSwitch).
61 // The local copy should go away!
62 //
63 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
64 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080065
Naoki Shiota7d0bcfa2013-11-13 10:43:33 -080066 // Number of messages sent to switch at once
67 protected static final int MAX_MESSAGE_SEND = 100;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080068
69 public static final short PRIORITY_DEFAULT = 100;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080070
71 public enum QueueState {
72 READY,
73 SUSPENDED,
74 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070075
Naoki Shiotac1601d32013-11-20 10:47:34 -080076 /**
Naoki Shiotab485d412013-11-26 12:04:19 -080077 * SwitchQueue represents message queue attached to a switch.
Naoki Shiotac1601d32013-11-20 10:47:34 -080078 * This consists of queue itself and variables used for limiting sending rate.
79 * @author Naoki Shiota
80 *
81 */
Naoki Shiota7d0cf272013-11-05 10:18:12 -080082 private class SwitchQueue extends ArrayDeque<OFMessage> {
Naoki Shiota991093a2013-12-10 14:47:18 -080083 private static final long serialVersionUID = 1L;
84
Naoki Shiota7d0cf272013-11-05 10:18:12 -080085 QueueState state;
86
Naoki Shiotae3199732013-11-25 16:14:43 -080087 // Max rate of sending message (bytes/ms). 0 implies no limitation.
Naoki Shiota8ee48d52013-11-11 15:51:17 -080088 long max_rate = 0; // 0 indicates no limitation
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070089 long last_sent_time = 0;
90 long last_sent_size = 0;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080091
Naoki Shiotae3199732013-11-25 16:14:43 -080092 // "To be deleted" flag
93 boolean toBeDeleted = false;
94
Naoki Shiota7d0cf272013-11-05 10:18:12 -080095 /**
96 * Check if sending rate is within the rate
97 * @param current Current time
98 * @return true if within the rate
99 */
100 boolean isSendable(long current) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800101 if (max_rate == 0) {
102 // no limitation
103 return true;
104 }
105
Naoki Shiota81dbe302013-11-21 15:35:38 -0800106 if (current == last_sent_time) {
107 return false;
108 }
109
Naoki Shiotac1601d32013-11-20 10:47:34 -0800110 // Check if sufficient time (from aspect of rate) elapsed or not.
Naoki Shiotab485d412013-11-26 12:04:19 -0800111 long rate = last_sent_size / (current - last_sent_time);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800112 return (rate < max_rate);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800113 }
114
Naoki Shiota81dbe302013-11-21 15:35:38 -0800115 /**
116 * Log time and size of last sent data.
117 * @param current Time to be sent.
118 * @param size Size of sent data (in bytes).
119 */
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800120 void logSentData(long current, long size) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800121 last_sent_time = current;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800122 last_sent_size = size;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800123 }
124
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700125 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800126
Naoki Shiotac1601d32013-11-20 10:47:34 -0800127 private OFMessageDamper messageDamper = null;
128 private IThreadPoolService threadPool = null;
Naoki Shiotacf1acca2013-10-31 11:40:32 -0700129
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800130 private FloodlightContext context = null;
131 private BasicFactory factory = null;
Naoki Shiotab485d412013-11-26 12:04:19 -0800132
133 // Map of threads versus dpid
Naoki Shiota81dbe302013-11-21 15:35:38 -0800134 private Map<Long, FlowPusherThread> threadMap = null;
Naoki Shiotab485d412013-11-26 12:04:19 -0800135 // Map of Future objects versus dpid and transaction ID.
Naoki Shiotac1601d32013-11-20 10:47:34 -0800136 private Map<Long, Map<Integer, OFBarrierReplyFuture>>
137 barrierFutures = new HashMap<Long, Map<Integer, OFBarrierReplyFuture>>();
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800138
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800139 private int number_thread = 1;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800140
Naoki Shiota8739faa2013-11-18 17:00:25 -0800141 /**
142 * Main thread that reads messages from queues and sends them to switches.
143 * @author Naoki Shiota
144 *
145 */
Naoki Shiota81dbe302013-11-21 15:35:38 -0800146 private class FlowPusherThread extends Thread {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800147 private Map<IOFSwitch,SwitchQueue> queues
Naoki Shiotab485d412013-11-26 12:04:19 -0800148 = new HashMap<IOFSwitch,SwitchQueue>();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800149
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800150 // reusable latch used for waiting for arrival of message
Naoki Shiota81dbe302013-11-21 15:35:38 -0800151 private Semaphore mutex = new Semaphore(0);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800152
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700153 @Override
154 public void run() {
155 while (true) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800156 try {
157 // wait for message pushed to queue
158 mutex.acquire();
159 } catch (InterruptedException e) {
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800160 // not an error
Naoki Shiota81dbe302013-11-21 15:35:38 -0800161 log.debug("FlowPusherThread is interrupted");
162 return;
163 }
164
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800165 // for safety of concurrent access, copy all key objects
166 Set<IOFSwitch> keys = new HashSet<IOFSwitch>(queues.size());
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800167 synchronized (queues) {
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800168 for (IOFSwitch sw : queues.keySet()) {
169 keys.add(sw);
170 }
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800171 }
172
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800173 for (IOFSwitch sw : keys) {
174 SwitchQueue queue = queues.get(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800175
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700176 // Skip if queue is suspended
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800177 if (sw == null || queue == null ||
178 queue.state != QueueState.READY) {
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700179 continue;
180 }
181
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800182 synchronized (queue) {
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800183 processQueue(sw, queue, MAX_MESSAGE_SEND);
184 if (queue.isEmpty()) {
185 // remove queue if flagged to be.
186 if (queue.toBeDeleted) {
187 synchronized (queues) {
188 queues.remove(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800189 }
190 }
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800191 } else {
192 // if some messages remains in queue, latch down
193 if (mutex.availablePermits() == 0) {
194 mutex.release();
Naoki Shiota81dbe302013-11-21 15:35:38 -0800195 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700196 }
197 }
198 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700199 }
200 }
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800201
202 /**
203 * Read messages from queue and send them to the switch.
204 * If number of messages excess the limit, stop sending messages.
205 * @param sw Switch to which messages will be sent.
206 * @param queue Queue of messages.
207 * @param max_msg Limitation of number of messages to be sent. If set to 0,
208 * all messages in queue will be sent.
209 */
210 private void processQueue(IOFSwitch sw, SwitchQueue queue, long max_msg) {
211 // check sending rate and determine it to be sent or not
212 long current_time = System.currentTimeMillis();
213 long size = 0;
214
215 if (queue.isSendable(current_time)) {
216 int i = 0;
217 while (! queue.isEmpty()) {
218 // Number of messages excess the limit
219 if (0 < max_msg && max_msg <= i) {
220 break;
221 }
222 ++i;
223
224 OFMessage msg = queue.poll();
225 try {
226 messageDamper.write(sw, msg, context);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800227// log.debug("Pusher sends message : {}", msg);
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800228 size += msg.getLength();
229 } catch (IOException e) {
230 e.printStackTrace();
231 log.error("Exception in sending message ({}) : {}", msg, e);
232 }
233 }
234 sw.flush();
235 queue.logSentData(current_time, size);
236 }
237 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700238 }
239
Naoki Shiotac1601d32013-11-20 10:47:34 -0800240 /**
241 * Initialize object with one thread.
242 */
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800243 public FlowPusher() {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800244 }
245
Naoki Shiotac1601d32013-11-20 10:47:34 -0800246 /**
247 * Initialize object with threads of given number.
248 * @param number_thread Number of threads to handle messages.
249 */
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800250 public FlowPusher(int number_thread) {
251 this.number_thread = number_thread;
252 }
253
Naoki Shiotac1601d32013-11-20 10:47:34 -0800254 /**
255 * Set parameters needed for sending messages.
256 * @param context FloodlightContext used for sending messages.
257 * If null, FlowPusher uses default context.
258 * @param modContext FloodlightModuleContext used for acquiring
259 * ThreadPoolService and registering MessageListener.
260 * @param factory Factory object to create OFMessage objects.
261 * @param damper Message damper used for sending messages.
262 * If null, FlowPusher creates its own damper object.
263 */
264 public void init(FloodlightContext context,
265 FloodlightModuleContext modContext,
266 BasicFactory factory,
267 OFMessageDamper damper) {
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700268 this.context = context;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800269 this.factory = factory;
Naoki Shiotac1601d32013-11-20 10:47:34 -0800270 this.threadPool = modContext.getServiceImpl(IThreadPoolService.class);
271 IFloodlightProviderService flservice = modContext.getServiceImpl(IFloodlightProviderService.class);
272 flservice.addOFMessageListener(OFType.BARRIER_REPLY, this);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800273 flowManager = modContext.getServiceImpl(IFlowService.class);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800274
275 if (damper != null) {
276 messageDamper = damper;
277 } else {
Naoki Shiotab485d412013-11-26 12:04:19 -0800278 // use default values
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800279 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
280 EnumSet.of(OFType.FLOW_MOD),
281 OFMESSAGE_DAMPER_TIMEOUT);
282 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700283 }
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800284
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800285 /**
286 * Begin processing queue.
287 */
288 public void start() {
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800289 if (factory == null) {
290 log.error("FlowPusher not yet initialized.");
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800291 return;
292 }
293
Naoki Shiota81dbe302013-11-21 15:35:38 -0800294 threadMap = new HashMap<Long,FlowPusherThread>();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800295 for (long i = 0; i < number_thread; ++i) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800296 FlowPusherThread thread = new FlowPusherThread();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800297
Naoki Shiota81dbe302013-11-21 15:35:38 -0800298 threadMap.put(i, thread);
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800299 thread.start();
300 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700301 }
302
Brian O'Connor8c166a72013-11-14 18:41:48 -0800303 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800304 public boolean suspend(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800305 SwitchQueue queue = getQueue(sw);
306
307 if (queue == null) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800308 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800309 }
310
311 synchronized (queue) {
312 if (queue.state == QueueState.READY) {
313 queue.state = QueueState.SUSPENDED;
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800314 return true;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800315 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800316 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800317 }
318 }
319
Brian O'Connor8c166a72013-11-14 18:41:48 -0800320 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800321 public boolean resume(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800322 SwitchQueue queue = getQueue(sw);
323
324 if (queue == null) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800325 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800326 }
327
328 synchronized (queue) {
329 if (queue.state == QueueState.SUSPENDED) {
330 queue.state = QueueState.READY;
Naoki Shiota0aa947e2013-11-27 14:47:35 -0800331
332 // Latch down if queue is not empty
333 FlowPusherThread thread = getProcess(sw);
334 if (! queue.isEmpty() &&
335 thread.mutex.availablePermits() == 0) {
336 thread.mutex.release();
337 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800338 return true;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800339 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800340 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800341 }
342 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800343
Brian O'Connor8c166a72013-11-14 18:41:48 -0800344 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800345 public boolean isSuspended(IOFSwitch sw) {
346 SwitchQueue queue = getQueue(sw);
347
348 if (queue == null) {
349 // TODO Is true suitable for this case?
350 return true;
351 }
352
353 return (queue.state == QueueState.SUSPENDED);
354 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800355
356 /**
Naoki Shiota8739faa2013-11-18 17:00:25 -0800357 * Stop processing queue and exit thread.
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800358 */
359 public void stop() {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800360 if (threadMap == null) {
361 return;
362 }
363
Naoki Shiota81dbe302013-11-21 15:35:38 -0800364 for (FlowPusherThread t : threadMap.values()) {
365 t.interrupt();
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800366 }
367 }
368
Naoki Shiotae3199732013-11-25 16:14:43 -0800369 @Override
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800370 public void setRate(IOFSwitch sw, long rate) {
371 SwitchQueue queue = getQueue(sw);
372 if (queue == null) {
373 return;
374 }
375
376 if (rate > 0) {
Naoki Shiota2a35b442013-11-26 19:17:38 -0800377 log.debug("rate for {} is set to {}", sw.getId(), rate);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800378 queue.max_rate = rate;
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700379 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700380 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800381
382 @Override
383 public boolean createQueue(IOFSwitch sw) {
384 SwitchQueue queue = getQueue(sw);
385 if (queue != null) {
386 return false;
387 }
388
389 FlowPusherThread proc = getProcess(sw);
390 queue = new SwitchQueue();
391 queue.state = QueueState.READY;
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800392 synchronized (proc.queues) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800393 proc.queues.put(sw, queue);
394 }
395
396 return true;
397 }
398
399 @Override
400 public boolean deleteQueue(IOFSwitch sw) {
401 return deleteQueue(sw, false);
402 }
403
404 @Override
Naoki Shiotab485d412013-11-26 12:04:19 -0800405 public boolean deleteQueue(IOFSwitch sw, boolean forceStop) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800406 FlowPusherThread proc = getProcess(sw);
407
Naoki Shiotab485d412013-11-26 12:04:19 -0800408 if (forceStop) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800409 synchronized (proc.queues) {
410 SwitchQueue queue = proc.queues.remove(sw);
411 if (queue == null) {
412 return false;
413 }
414 }
415 return true;
416 } else {
417 SwitchQueue queue = getQueue(sw);
418 if (queue == null) {
419 return false;
420 }
421 synchronized (queue) {
422 queue.toBeDeleted = true;
423 }
424 return true;
425 }
426 }
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700427
Brian O'Connor8c166a72013-11-14 18:41:48 -0800428 @Override
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800429 public boolean add(IOFSwitch sw, OFMessage msg) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800430 FlowPusherThread proc = getProcess(sw);
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800431 SwitchQueue queue = proc.queues.get(sw);
432
Naoki Shiotab485d412013-11-26 12:04:19 -0800433 // create queue at first addition of message
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800434 if (queue == null) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800435 createQueue(sw);
436 queue = getQueue(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800437 }
438
439 synchronized (queue) {
440 queue.add(msg);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800441// log.debug("Message is pushed : {}", msg);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800442 }
443
Naoki Shiota81dbe302013-11-21 15:35:38 -0800444 if (proc.mutex.availablePermits() == 0) {
445 proc.mutex.release();
446 }
447
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800448 return true;
449 }
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800450
Brian O'Connor8c166a72013-11-14 18:41:48 -0800451 @Override
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800452 public void pushFlowEntries(
453 Collection<Pair<IOFSwitch, FlowEntry>> entries) {
454
455 List<Pair<IOFSwitch, FlowEntry>> pushedEntries =
456 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
457
458 for (Pair<IOFSwitch, FlowEntry> entry : entries) {
Pavlin Radoslavov4535bc12013-12-05 10:43:49 -0800459 if (add(entry.first, entry.second)) {
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800460 pushedEntries.add(entry);
461 }
462 }
463
464 //
465 // TODO: We should use the OpenFlow Barrier mechanism
466 // to check for errors, and update the SwitchState
467 // for a flow entry after the Barrier message is
468 // is received.
469 // Only after inform the Flow Manager that the entry is pushed.
470 //
471 flowManager.flowEntriesPushedToSwitch(pushedEntries);
472 }
473
474 @Override
475 public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) {
476 Collection<Pair<IOFSwitch, FlowEntry>> entries =
477 new LinkedList<Pair<IOFSwitch, FlowEntry>>();
478
479 entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry));
480 pushFlowEntries(entries);
481 }
482
483 /**
484 * Create a message from FlowEntry and add it to the queue of the switch.
485 * @param sw Switch to which message is pushed.
486 * @param flowEntry FlowEntry object used for creating message.
487 * @return true if message is successfully added to a queue.
488 */
489 private boolean add(IOFSwitch sw, FlowEntry flowEntry) {
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800490 //
491 // Create the OpenFlow Flow Modification Entry to push
492 //
493 OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD);
494 long cookie = flowEntry.flowEntryId().value();
495
496 short flowModCommand = OFFlowMod.OFPFC_ADD;
497 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
498 flowModCommand = OFFlowMod.OFPFC_ADD;
499 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
500 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
501 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
502 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
503 } else {
504 // Unknown user state. Ignore the entry
505 log.debug(
506 "Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
507 flowEntry.flowEntryId().toString(),
508 flowEntry.flowEntryUserState());
509 return false;
510 }
511
512 //
513 // Fetch the match conditions.
514 //
515 // NOTE: The Flow matching conditions common for all Flow Entries are
516 // used ONLY if a Flow Entry does NOT have the corresponding matching
517 // condition set.
518 //
519 OFMatch match = new OFMatch();
520 match.setWildcards(OFMatch.OFPFW_ALL);
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800521 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
522
523 // Match the Incoming Port
524 Port matchInPort = flowEntryMatch.inPort();
525 if (matchInPort != null) {
526 match.setInputPort(matchInPort.value());
527 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
528 }
529
530 // Match the Source MAC address
531 MACAddress matchSrcMac = flowEntryMatch.srcMac();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800532 if (matchSrcMac != null) {
533 match.setDataLayerSource(matchSrcMac.toString());
534 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
535 }
536
537 // Match the Destination MAC address
538 MACAddress matchDstMac = flowEntryMatch.dstMac();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800539 if (matchDstMac != null) {
540 match.setDataLayerDestination(matchDstMac.toString());
541 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
542 }
543
544 // Match the Ethernet Frame Type
545 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800546 if (matchEthernetFrameType != null) {
547 match.setDataLayerType(matchEthernetFrameType);
548 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
549 }
550
551 // Match the VLAN ID
552 Short matchVlanId = flowEntryMatch.vlanId();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800553 if (matchVlanId != null) {
554 match.setDataLayerVirtualLan(matchVlanId);
555 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN);
556 }
557
558 // Match the VLAN priority
559 Byte matchVlanPriority = flowEntryMatch.vlanPriority();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800560 if (matchVlanPriority != null) {
561 match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority);
562 match.setWildcards(match.getWildcards()
563 & ~OFMatch.OFPFW_DL_VLAN_PCP);
564 }
565
566 // Match the Source IPv4 Network prefix
567 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800568 if (matchSrcIPv4Net != null) {
569 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
570 }
571
572 // Natch the Destination IPv4 Network prefix
573 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800574 if (matchDstIPv4Net != null) {
575 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
576 }
577
578 // Match the IP protocol
579 Byte matchIpProto = flowEntryMatch.ipProto();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800580 if (matchIpProto != null) {
581 match.setNetworkProtocol(matchIpProto);
582 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO);
583 }
584
585 // Match the IP ToS (DSCP field, 6 bits)
586 Byte matchIpToS = flowEntryMatch.ipToS();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800587 if (matchIpToS != null) {
588 match.setNetworkTypeOfService(matchIpToS);
589 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS);
590 }
591
592 // Match the Source TCP/UDP port
593 Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800594 if (matchSrcTcpUdpPort != null) {
595 match.setTransportSource(matchSrcTcpUdpPort);
596 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
597 }
598
599 // Match the Destination TCP/UDP port
600 Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800601 if (matchDstTcpUdpPort != null) {
602 match.setTransportDestination(matchDstTcpUdpPort);
603 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST);
604 }
605
606 //
607 // Fetch the actions
608 //
609 Short actionOutputPort = null;
610 List<OFAction> openFlowActions = new ArrayList<OFAction>();
611 int actionsLen = 0;
612 FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
613 //
614 for (FlowEntryAction action : flowEntryActions.actions()) {
615 ActionOutput actionOutput = action.actionOutput();
616 ActionSetVlanId actionSetVlanId = action.actionSetVlanId();
617 ActionSetVlanPriority actionSetVlanPriority = action
618 .actionSetVlanPriority();
619 ActionStripVlan actionStripVlan = action.actionStripVlan();
620 ActionSetEthernetAddr actionSetEthernetSrcAddr = action
621 .actionSetEthernetSrcAddr();
622 ActionSetEthernetAddr actionSetEthernetDstAddr = action
623 .actionSetEthernetDstAddr();
624 ActionSetIPv4Addr actionSetIPv4SrcAddr = action
625 .actionSetIPv4SrcAddr();
626 ActionSetIPv4Addr actionSetIPv4DstAddr = action
627 .actionSetIPv4DstAddr();
628 ActionSetIpToS actionSetIpToS = action.actionSetIpToS();
629 ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action
630 .actionSetTcpUdpSrcPort();
631 ActionSetTcpUdpPort actionSetTcpUdpDstPort = action
632 .actionSetTcpUdpDstPort();
633 ActionEnqueue actionEnqueue = action.actionEnqueue();
634
635 if (actionOutput != null) {
636 actionOutputPort = actionOutput.port().value();
637 // XXX: The max length is hard-coded for now
638 OFActionOutput ofa = new OFActionOutput(actionOutput.port()
639 .value(), (short) 0xffff);
640 openFlowActions.add(ofa);
641 actionsLen += ofa.getLength();
642 }
643
644 if (actionSetVlanId != null) {
645 OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier(
646 actionSetVlanId.vlanId());
647 openFlowActions.add(ofa);
648 actionsLen += ofa.getLength();
649 }
650
651 if (actionSetVlanPriority != null) {
652 OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint(
653 actionSetVlanPriority.vlanPriority());
654 openFlowActions.add(ofa);
655 actionsLen += ofa.getLength();
656 }
657
658 if (actionStripVlan != null) {
659 if (actionStripVlan.stripVlan() == true) {
660 OFActionStripVirtualLan ofa = new OFActionStripVirtualLan();
661 openFlowActions.add(ofa);
662 actionsLen += ofa.getLength();
663 }
664 }
665
666 if (actionSetEthernetSrcAddr != null) {
667 OFActionDataLayerSource ofa = new OFActionDataLayerSource(
668 actionSetEthernetSrcAddr.addr().toBytes());
669 openFlowActions.add(ofa);
670 actionsLen += ofa.getLength();
671 }
672
673 if (actionSetEthernetDstAddr != null) {
674 OFActionDataLayerDestination ofa = new OFActionDataLayerDestination(
675 actionSetEthernetDstAddr.addr().toBytes());
676 openFlowActions.add(ofa);
677 actionsLen += ofa.getLength();
678 }
679
680 if (actionSetIPv4SrcAddr != null) {
681 OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource(
682 actionSetIPv4SrcAddr.addr().value());
683 openFlowActions.add(ofa);
684 actionsLen += ofa.getLength();
685 }
686
687 if (actionSetIPv4DstAddr != null) {
688 OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination(
689 actionSetIPv4DstAddr.addr().value());
690 openFlowActions.add(ofa);
691 actionsLen += ofa.getLength();
692 }
693
694 if (actionSetIpToS != null) {
695 OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService(
696 actionSetIpToS.ipToS());
697 openFlowActions.add(ofa);
698 actionsLen += ofa.getLength();
699 }
700
701 if (actionSetTcpUdpSrcPort != null) {
702 OFActionTransportLayerSource ofa = new OFActionTransportLayerSource(
703 actionSetTcpUdpSrcPort.port());
704 openFlowActions.add(ofa);
705 actionsLen += ofa.getLength();
706 }
707
708 if (actionSetTcpUdpDstPort != null) {
709 OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination(
710 actionSetTcpUdpDstPort.port());
711 openFlowActions.add(ofa);
712 actionsLen += ofa.getLength();
713 }
714
715 if (actionEnqueue != null) {
716 OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port()
717 .value(), actionEnqueue.queueId());
718 openFlowActions.add(ofa);
719 actionsLen += ofa.getLength();
720 }
721 }
722
Pavlin Radoslavov1fe06a22013-12-10 14:12:23 -0800723 fm.setIdleTimeout((short)flowEntry.idleTimeout())
724 .setHardTimeout((short)flowEntry.hardTimeout())
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800725 .setPriority(PRIORITY_DEFAULT)
726 .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie)
727 .setCommand(flowModCommand).setMatch(match)
728 .setActions(openFlowActions)
729 .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen);
730 fm.setOutPort(OFPort.OFPP_NONE.getValue());
731 if ((flowModCommand == OFFlowMod.OFPFC_DELETE)
732 || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
733 if (actionOutputPort != null)
734 fm.setOutPort(actionOutputPort);
735 }
736
737 //
738 // TODO: Set the following flag
739 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
740 // See method ForwardingBase::pushRoute()
741 //
742
743 //
744 // Write the message to the switch
745 //
Pavlin Radoslavovf0678902013-12-03 15:06:56 -0800746 log.debug("Installing flow entry "
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800747 + flowEntry.flowEntryUserState() + " into switch DPID: "
748 + sw.getStringId() + " flowEntryId: "
749 + flowEntry.flowEntryId().toString() + " srcMac: "
750 + matchSrcMac + " dstMac: " + matchDstMac + " inPort: "
751 + matchInPort + " outPort: " + actionOutputPort);
752
Pavlin Radoslavovab3f8862013-12-04 18:35:53 -0800753 return add(sw, fm);
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800754 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800755
756 @Override
757 public OFBarrierReply barrier(IOFSwitch sw) {
758 OFMessageFuture<OFBarrierReply> future = barrierAsync(sw);
759 if (future == null) {
760 return null;
761 }
762
763 try {
764 return future.get();
765 } catch (InterruptedException e) {
766 e.printStackTrace();
767 log.error("InterruptedException: {}", e);
768 return null;
769 } catch (ExecutionException e) {
770 e.printStackTrace();
771 log.error("ExecutionException: {}", e);
772 return null;
773 }
774 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800775
Naoki Shiotac1601d32013-11-20 10:47:34 -0800776 @Override
777 public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) {
778 // TODO creation of message and future should be moved to OFSwitchImpl
Naoki Shiota81dbe302013-11-21 15:35:38 -0800779
780 if (sw == null) {
781 return null;
782 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800783
Naoki Shiota81dbe302013-11-21 15:35:38 -0800784 OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800785 msg.setXid(sw.getNextTransactionId());
Naoki Shiotac1601d32013-11-20 10:47:34 -0800786
Naoki Shiotac1601d32013-11-20 10:47:34 -0800787 OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid());
Naoki Shiotac1601d32013-11-20 10:47:34 -0800788 synchronized (barrierFutures) {
789 Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId());
790 if (map == null) {
791 map = new HashMap<Integer,OFBarrierReplyFuture>();
792 barrierFutures.put(sw.getId(), map);
793 }
794 map.put(msg.getXid(), future);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800795 }
796
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800797 add(sw, msg);
798
Naoki Shiotac1601d32013-11-20 10:47:34 -0800799 return future;
800 }
801
Naoki Shiotae3199732013-11-25 16:14:43 -0800802 /**
803 * Get a queue attached to a switch.
804 * @param sw Switch object
805 * @return Queue object
806 */
Naoki Shiotac1601d32013-11-20 10:47:34 -0800807 protected SwitchQueue getQueue(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800808 if (sw == null) {
809 return null;
810 }
811
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800812 return getProcess(sw).queues.get(sw);
813 }
814
Naoki Shiotae3199732013-11-25 16:14:43 -0800815 /**
816 * Get a hash value correspondent to a switch.
817 * @param sw Switch object
818 * @return Hash value
819 */
Naoki Shiotac1601d32013-11-20 10:47:34 -0800820 protected long getHash(IOFSwitch sw) {
821 // This code assumes DPID is sequentially assigned.
822 // TODO consider equalization algorithm
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800823 return sw.getId() % number_thread;
824 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800825
826 /**
827 * Get a Thread object which processes the queue attached to a switch.
828 * @param sw Switch object
829 * @return Thread object
830 */
Naoki Shiota81dbe302013-11-21 15:35:38 -0800831 protected FlowPusherThread getProcess(IOFSwitch sw) {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800832 long hash = getHash(sw);
833
834 return threadMap.get(hash);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800835 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800836
837 @Override
838 public String getName() {
839 return "flowpusher";
840 }
841
842 @Override
843 public boolean isCallbackOrderingPrereq(OFType type, String name) {
844 return false;
845 }
846
847 @Override
848 public boolean isCallbackOrderingPostreq(OFType type, String name) {
849 return false;
850 }
851
852 @Override
853 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
Naoki Shiotac1601d32013-11-20 10:47:34 -0800854 Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId());
855 if (map == null) {
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800856 log.debug("null map for {} : {}", sw.getId(), barrierFutures);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800857 return Command.CONTINUE;
858 }
859
860 OFBarrierReplyFuture future = map.get(msg.getXid());
861 if (future == null) {
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800862 log.debug("null future for {} : {}", msg.getXid(), map);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800863 return Command.CONTINUE;
864 }
865
866 log.debug("Received BARRIER_REPLY : {}", msg);
867 future.deliverFuture(sw, msg);
868
869 return Command.CONTINUE;
870 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800871
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700872}