blob: 792511cccebd2f7e3bd0c50415c9ed2650b601fc [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;
6import java.util.EnumSet;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -07007import java.util.HashMap;
Naoki Shiotaf03592e2013-11-27 11:20:39 -08008import java.util.HashSet;
Naoki Shiota7d0cf272013-11-05 10:18:12 -08009import java.util.List;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070010import java.util.Map;
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080011import java.util.Set;
Naoki Shiotac1601d32013-11-20 10:47:34 -080012import java.util.concurrent.ExecutionException;
Naoki Shiota81dbe302013-11-21 15:35:38 -080013import java.util.concurrent.Semaphore;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070014
Naoki Shiota7d0cf272013-11-05 10:18:12 -080015import org.openflow.protocol.*;
16import org.openflow.protocol.action.*;
17import org.openflow.protocol.factory.BasicFactory;
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070020
21import 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;
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -080030import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080031import net.onrc.onos.ofcontroller.util.FlowEntryAction;
32import net.onrc.onos.ofcontroller.util.FlowEntryAction.*;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080033import net.onrc.onos.ofcontroller.util.FlowEntry;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080034import net.onrc.onos.ofcontroller.util.FlowEntryActions;
35import net.onrc.onos.ofcontroller.util.FlowEntryId;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080036import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
37import net.onrc.onos.ofcontroller.util.FlowEntryUserState;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -080038import net.onrc.onos.ofcontroller.util.IPv4Net;
39import net.onrc.onos.ofcontroller.util.Port;
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070040
41/**
Naoki Shiotab485d412013-11-26 12:04:19 -080042 * FlowPusher is a implementation of FlowPusherService.
43 * FlowPusher assigns one message queue instance for each one switch.
44 * Number of message processing threads is configurable by constructor, and
45 * one thread can handle multiple message queues. Each queue will be assigned to
46 * a thread according to hash function defined by getHash().
47 * Each processing thread reads messages from queues and sends it to switches
48 * in round-robin. Processing thread also calculates rate of sending to suppress
49 * excessive message sending.
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070050 * @author Naoki Shiota
51 *
52 */
Naoki Shiotac1601d32013-11-20 10:47:34 -080053public class FlowPusher implements IFlowPusherService, IOFMessageListener {
Naoki Shiota7d0cf272013-11-05 10:18:12 -080054 private final static Logger log = LoggerFactory.getLogger(FlowPusher.class);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -080055 protected volatile IFlowService flowManager;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080056
57 // NOTE: Below are moved from FlowManager.
58 // TODO: Values copied from elsewhere (class LearningSwitch).
59 // The local copy should go away!
60 //
61 protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot
62 protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms
Naoki Shiota5c8d19f2013-11-05 15:52:38 -080063
Naoki Shiota7d0bcfa2013-11-13 10:43:33 -080064 // Number of messages sent to switch at once
65 protected static final int MAX_MESSAGE_SEND = 100;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080066
67 public static final short PRIORITY_DEFAULT = 100;
68 public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity
69 public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite
70
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 Shiota8739faa2013-11-18 17:00:25 -080082 @SuppressWarnings("serial")
Naoki Shiota7d0cf272013-11-05 10:18:12 -080083 private class SwitchQueue extends ArrayDeque<OFMessage> {
84 QueueState state;
85
Naoki Shiotae3199732013-11-25 16:14:43 -080086 // Max rate of sending message (bytes/ms). 0 implies no limitation.
Naoki Shiota8ee48d52013-11-11 15:51:17 -080087 long max_rate = 0; // 0 indicates no limitation
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -070088 long last_sent_time = 0;
89 long last_sent_size = 0;
Naoki Shiota7d0cf272013-11-05 10:18:12 -080090
Naoki Shiotae3199732013-11-25 16:14:43 -080091 // "To be deleted" flag
92 boolean toBeDeleted = false;
93
Naoki Shiota7d0cf272013-11-05 10:18:12 -080094 /**
95 * Check if sending rate is within the rate
96 * @param current Current time
97 * @return true if within the rate
98 */
99 boolean isSendable(long current) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800100 if (max_rate == 0) {
101 // no limitation
102 return true;
103 }
104
Naoki Shiota81dbe302013-11-21 15:35:38 -0800105 if (current == last_sent_time) {
106 return false;
107 }
108
Naoki Shiotac1601d32013-11-20 10:47:34 -0800109 // Check if sufficient time (from aspect of rate) elapsed or not.
Naoki Shiotab485d412013-11-26 12:04:19 -0800110 long rate = last_sent_size / (current - last_sent_time);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800111 return (rate < max_rate);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800112 }
113
Naoki Shiota81dbe302013-11-21 15:35:38 -0800114 /**
115 * Log time and size of last sent data.
116 * @param current Time to be sent.
117 * @param size Size of sent data (in bytes).
118 */
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800119 void logSentData(long current, long size) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800120 last_sent_time = current;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800121 last_sent_size = size;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800122 }
123
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700124 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800125
Naoki Shiotac1601d32013-11-20 10:47:34 -0800126 private OFMessageDamper messageDamper = null;
127 private IThreadPoolService threadPool = null;
Naoki Shiotacf1acca2013-10-31 11:40:32 -0700128
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800129 private FloodlightContext context = null;
130 private BasicFactory factory = null;
Naoki Shiotab485d412013-11-26 12:04:19 -0800131
132 // Map of threads versus dpid
Naoki Shiota81dbe302013-11-21 15:35:38 -0800133 private Map<Long, FlowPusherThread> threadMap = null;
Naoki Shiotab485d412013-11-26 12:04:19 -0800134 // Map of Future objects versus dpid and transaction ID.
Naoki Shiotac1601d32013-11-20 10:47:34 -0800135 private Map<Long, Map<Integer, OFBarrierReplyFuture>>
136 barrierFutures = new HashMap<Long, Map<Integer, OFBarrierReplyFuture>>();
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800137
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800138 private int number_thread = 1;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800139
Naoki Shiota8739faa2013-11-18 17:00:25 -0800140 /**
141 * Main thread that reads messages from queues and sends them to switches.
142 * @author Naoki Shiota
143 *
144 */
Naoki Shiota81dbe302013-11-21 15:35:38 -0800145 private class FlowPusherThread extends Thread {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800146 private Map<IOFSwitch,SwitchQueue> queues
Naoki Shiotab485d412013-11-26 12:04:19 -0800147 = new HashMap<IOFSwitch,SwitchQueue>();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800148
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800149 // reusable latch used for waiting for arrival of message
Naoki Shiota81dbe302013-11-21 15:35:38 -0800150 private Semaphore mutex = new Semaphore(0);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800151
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700152 @Override
153 public void run() {
154 while (true) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800155 try {
156 // wait for message pushed to queue
157 mutex.acquire();
158 } catch (InterruptedException e) {
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800159 // not an error
Naoki Shiota81dbe302013-11-21 15:35:38 -0800160 log.debug("FlowPusherThread is interrupted");
161 return;
162 }
163
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800164 // for safety of concurrent access, copy all key objects
165 Set<IOFSwitch> keys = new HashSet<IOFSwitch>(queues.size());
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800166 synchronized (queues) {
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800167 for (IOFSwitch sw : queues.keySet()) {
168 keys.add(sw);
169 }
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800170 }
171
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800172 for (IOFSwitch sw : keys) {
173 SwitchQueue queue = queues.get(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800174
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700175 // Skip if queue is suspended
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800176 if (sw == null || queue == null ||
177 queue.state != QueueState.READY) {
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700178 continue;
179 }
180
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800181 synchronized (queue) {
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800182 processQueue(sw, queue, MAX_MESSAGE_SEND);
183 if (queue.isEmpty()) {
184 // remove queue if flagged to be.
185 if (queue.toBeDeleted) {
186 synchronized (queues) {
187 queues.remove(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800188 }
189 }
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800190 } else {
191 // if some messages remains in queue, latch down
192 if (mutex.availablePermits() == 0) {
193 mutex.release();
Naoki Shiota81dbe302013-11-21 15:35:38 -0800194 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700195 }
196 }
197 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700198 }
199 }
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800200
201 /**
202 * Read messages from queue and send them to the switch.
203 * If number of messages excess the limit, stop sending messages.
204 * @param sw Switch to which messages will be sent.
205 * @param queue Queue of messages.
206 * @param max_msg Limitation of number of messages to be sent. If set to 0,
207 * all messages in queue will be sent.
208 */
209 private void processQueue(IOFSwitch sw, SwitchQueue queue, long max_msg) {
210 // check sending rate and determine it to be sent or not
211 long current_time = System.currentTimeMillis();
212 long size = 0;
213
214 if (queue.isSendable(current_time)) {
215 int i = 0;
216 while (! queue.isEmpty()) {
217 // Number of messages excess the limit
218 if (0 < max_msg && max_msg <= i) {
219 break;
220 }
221 ++i;
222
223 OFMessage msg = queue.poll();
224 try {
225 messageDamper.write(sw, msg, context);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800226// log.debug("Pusher sends message : {}", msg);
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800227 size += msg.getLength();
228 } catch (IOException e) {
229 e.printStackTrace();
230 log.error("Exception in sending message ({}) : {}", msg, e);
231 }
232 }
233 sw.flush();
234 queue.logSentData(current_time, size);
235 }
236 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700237 }
238
Naoki Shiotac1601d32013-11-20 10:47:34 -0800239 /**
240 * Initialize object with one thread.
241 */
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800242 public FlowPusher() {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800243 }
244
Naoki Shiotac1601d32013-11-20 10:47:34 -0800245 /**
246 * Initialize object with threads of given number.
247 * @param number_thread Number of threads to handle messages.
248 */
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800249 public FlowPusher(int number_thread) {
250 this.number_thread = number_thread;
251 }
252
Naoki Shiotac1601d32013-11-20 10:47:34 -0800253 /**
254 * Set parameters needed for sending messages.
255 * @param context FloodlightContext used for sending messages.
256 * If null, FlowPusher uses default context.
257 * @param modContext FloodlightModuleContext used for acquiring
258 * ThreadPoolService and registering MessageListener.
259 * @param factory Factory object to create OFMessage objects.
260 * @param damper Message damper used for sending messages.
261 * If null, FlowPusher creates its own damper object.
262 */
263 public void init(FloodlightContext context,
264 FloodlightModuleContext modContext,
265 BasicFactory factory,
266 OFMessageDamper damper) {
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700267 this.context = context;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800268 this.factory = factory;
Naoki Shiotac1601d32013-11-20 10:47:34 -0800269 this.threadPool = modContext.getServiceImpl(IThreadPoolService.class);
270 IFloodlightProviderService flservice = modContext.getServiceImpl(IFloodlightProviderService.class);
271 flservice.addOFMessageListener(OFType.BARRIER_REPLY, this);
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800272 flowManager = modContext.getServiceImpl(IFlowService.class);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800273
274 if (damper != null) {
275 messageDamper = damper;
276 } else {
Naoki Shiotab485d412013-11-26 12:04:19 -0800277 // use default values
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800278 messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY,
279 EnumSet.of(OFType.FLOW_MOD),
280 OFMESSAGE_DAMPER_TIMEOUT);
281 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700282 }
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800283
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800284 /**
285 * Begin processing queue.
286 */
287 public void start() {
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800288 if (factory == null) {
289 log.error("FlowPusher not yet initialized.");
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800290 return;
291 }
292
Naoki Shiota81dbe302013-11-21 15:35:38 -0800293 threadMap = new HashMap<Long,FlowPusherThread>();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800294 for (long i = 0; i < number_thread; ++i) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800295 FlowPusherThread thread = new FlowPusherThread();
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800296
Naoki Shiota81dbe302013-11-21 15:35:38 -0800297 threadMap.put(i, thread);
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800298 thread.start();
299 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700300 }
301
Brian O'Connor8c166a72013-11-14 18:41:48 -0800302 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800303 public boolean suspend(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800304 SwitchQueue queue = getQueue(sw);
305
306 if (queue == null) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800307 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800308 }
309
310 synchronized (queue) {
311 if (queue.state == QueueState.READY) {
312 queue.state = QueueState.SUSPENDED;
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800313 return true;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800314 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800315 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800316 }
317 }
318
Brian O'Connor8c166a72013-11-14 18:41:48 -0800319 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800320 public boolean resume(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800321 SwitchQueue queue = getQueue(sw);
322
323 if (queue == null) {
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800324 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800325 }
326
327 synchronized (queue) {
328 if (queue.state == QueueState.SUSPENDED) {
329 queue.state = QueueState.READY;
Naoki Shiota0aa947e2013-11-27 14:47:35 -0800330
331 // Latch down if queue is not empty
332 FlowPusherThread thread = getProcess(sw);
333 if (! queue.isEmpty() &&
334 thread.mutex.availablePermits() == 0) {
335 thread.mutex.release();
336 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800337 return true;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800338 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800339 return false;
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800340 }
341 }
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800342
Brian O'Connor8c166a72013-11-14 18:41:48 -0800343 @Override
Naoki Shiota8ee48d52013-11-11 15:51:17 -0800344 public boolean isSuspended(IOFSwitch sw) {
345 SwitchQueue queue = getQueue(sw);
346
347 if (queue == null) {
348 // TODO Is true suitable for this case?
349 return true;
350 }
351
352 return (queue.state == QueueState.SUSPENDED);
353 }
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800354
355 /**
Naoki Shiota8739faa2013-11-18 17:00:25 -0800356 * Stop processing queue and exit thread.
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800357 */
358 public void stop() {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800359 if (threadMap == null) {
360 return;
361 }
362
Naoki Shiota81dbe302013-11-21 15:35:38 -0800363 for (FlowPusherThread t : threadMap.values()) {
364 t.interrupt();
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800365 }
366 }
367
Naoki Shiotae3199732013-11-25 16:14:43 -0800368 @Override
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800369 public void setRate(IOFSwitch sw, long rate) {
370 SwitchQueue queue = getQueue(sw);
371 if (queue == null) {
372 return;
373 }
374
375 if (rate > 0) {
Naoki Shiota2a35b442013-11-26 19:17:38 -0800376 log.debug("rate for {} is set to {}", sw.getId(), rate);
Naoki Shiota5c8d19f2013-11-05 15:52:38 -0800377 queue.max_rate = rate;
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700378 }
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700379 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800380
381 @Override
382 public boolean createQueue(IOFSwitch sw) {
383 SwitchQueue queue = getQueue(sw);
384 if (queue != null) {
385 return false;
386 }
387
388 FlowPusherThread proc = getProcess(sw);
389 queue = new SwitchQueue();
390 queue.state = QueueState.READY;
Naoki Shiotaf03592e2013-11-27 11:20:39 -0800391 synchronized (proc.queues) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800392 proc.queues.put(sw, queue);
393 }
394
395 return true;
396 }
397
398 @Override
399 public boolean deleteQueue(IOFSwitch sw) {
400 return deleteQueue(sw, false);
401 }
402
403 @Override
Naoki Shiotab485d412013-11-26 12:04:19 -0800404 public boolean deleteQueue(IOFSwitch sw, boolean forceStop) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800405 FlowPusherThread proc = getProcess(sw);
406
Naoki Shiotab485d412013-11-26 12:04:19 -0800407 if (forceStop) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800408 synchronized (proc.queues) {
409 SwitchQueue queue = proc.queues.remove(sw);
410 if (queue == null) {
411 return false;
412 }
413 }
414 return true;
415 } else {
416 SwitchQueue queue = getQueue(sw);
417 if (queue == null) {
418 return false;
419 }
420 synchronized (queue) {
421 queue.toBeDeleted = true;
422 }
423 return true;
424 }
425 }
Naoki Shiotac2a699a2013-10-31 15:36:01 -0700426
Brian O'Connor8c166a72013-11-14 18:41:48 -0800427 @Override
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800428 public boolean add(IOFSwitch sw, OFMessage msg) {
Naoki Shiota81dbe302013-11-21 15:35:38 -0800429 FlowPusherThread proc = getProcess(sw);
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800430 SwitchQueue queue = proc.queues.get(sw);
431
Naoki Shiotab485d412013-11-26 12:04:19 -0800432 // create queue at first addition of message
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800433 if (queue == null) {
Naoki Shiotae3199732013-11-25 16:14:43 -0800434 createQueue(sw);
435 queue = getQueue(sw);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800436 }
437
438 synchronized (queue) {
439 queue.add(msg);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800440// log.debug("Message is pushed : {}", msg);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800441 }
442
Naoki Shiota81dbe302013-11-21 15:35:38 -0800443 if (proc.mutex.availablePermits() == 0) {
444 proc.mutex.release();
445 }
446
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800447 return true;
448 }
449
Brian O'Connor8c166a72013-11-14 18:41:48 -0800450 @Override
Pavlin Radoslavov6bfaea62013-12-03 14:55:57 -0800451 public boolean add(IOFSwitch sw, FlowEntry flowEntry) {
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800452 //
453 // Create the OpenFlow Flow Modification Entry to push
454 //
455 OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD);
456 long cookie = flowEntry.flowEntryId().value();
457
458 short flowModCommand = OFFlowMod.OFPFC_ADD;
459 if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) {
460 flowModCommand = OFFlowMod.OFPFC_ADD;
461 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) {
462 flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT;
463 } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) {
464 flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT;
465 } else {
466 // Unknown user state. Ignore the entry
467 log.debug(
468 "Flow Entry ignored (FlowEntryId = {}): unknown user state {}",
469 flowEntry.flowEntryId().toString(),
470 flowEntry.flowEntryUserState());
471 return false;
472 }
473
474 //
475 // Fetch the match conditions.
476 //
477 // NOTE: The Flow matching conditions common for all Flow Entries are
478 // used ONLY if a Flow Entry does NOT have the corresponding matching
479 // condition set.
480 //
481 OFMatch match = new OFMatch();
482 match.setWildcards(OFMatch.OFPFW_ALL);
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800483 FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch();
484
485 // Match the Incoming Port
486 Port matchInPort = flowEntryMatch.inPort();
487 if (matchInPort != null) {
488 match.setInputPort(matchInPort.value());
489 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT);
490 }
491
492 // Match the Source MAC address
493 MACAddress matchSrcMac = flowEntryMatch.srcMac();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800494 if (matchSrcMac != null) {
495 match.setDataLayerSource(matchSrcMac.toString());
496 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC);
497 }
498
499 // Match the Destination MAC address
500 MACAddress matchDstMac = flowEntryMatch.dstMac();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800501 if (matchDstMac != null) {
502 match.setDataLayerDestination(matchDstMac.toString());
503 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST);
504 }
505
506 // Match the Ethernet Frame Type
507 Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800508 if (matchEthernetFrameType != null) {
509 match.setDataLayerType(matchEthernetFrameType);
510 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE);
511 }
512
513 // Match the VLAN ID
514 Short matchVlanId = flowEntryMatch.vlanId();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800515 if (matchVlanId != null) {
516 match.setDataLayerVirtualLan(matchVlanId);
517 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN);
518 }
519
520 // Match the VLAN priority
521 Byte matchVlanPriority = flowEntryMatch.vlanPriority();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800522 if (matchVlanPriority != null) {
523 match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority);
524 match.setWildcards(match.getWildcards()
525 & ~OFMatch.OFPFW_DL_VLAN_PCP);
526 }
527
528 // Match the Source IPv4 Network prefix
529 IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800530 if (matchSrcIPv4Net != null) {
531 match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC);
532 }
533
534 // Natch the Destination IPv4 Network prefix
535 IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800536 if (matchDstIPv4Net != null) {
537 match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST);
538 }
539
540 // Match the IP protocol
541 Byte matchIpProto = flowEntryMatch.ipProto();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800542 if (matchIpProto != null) {
543 match.setNetworkProtocol(matchIpProto);
544 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO);
545 }
546
547 // Match the IP ToS (DSCP field, 6 bits)
548 Byte matchIpToS = flowEntryMatch.ipToS();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800549 if (matchIpToS != null) {
550 match.setNetworkTypeOfService(matchIpToS);
551 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS);
552 }
553
554 // Match the Source TCP/UDP port
555 Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800556 if (matchSrcTcpUdpPort != null) {
557 match.setTransportSource(matchSrcTcpUdpPort);
558 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC);
559 }
560
561 // Match the Destination TCP/UDP port
562 Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort();
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800563 if (matchDstTcpUdpPort != null) {
564 match.setTransportDestination(matchDstTcpUdpPort);
565 match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST);
566 }
567
568 //
569 // Fetch the actions
570 //
571 Short actionOutputPort = null;
572 List<OFAction> openFlowActions = new ArrayList<OFAction>();
573 int actionsLen = 0;
574 FlowEntryActions flowEntryActions = flowEntry.flowEntryActions();
575 //
576 for (FlowEntryAction action : flowEntryActions.actions()) {
577 ActionOutput actionOutput = action.actionOutput();
578 ActionSetVlanId actionSetVlanId = action.actionSetVlanId();
579 ActionSetVlanPriority actionSetVlanPriority = action
580 .actionSetVlanPriority();
581 ActionStripVlan actionStripVlan = action.actionStripVlan();
582 ActionSetEthernetAddr actionSetEthernetSrcAddr = action
583 .actionSetEthernetSrcAddr();
584 ActionSetEthernetAddr actionSetEthernetDstAddr = action
585 .actionSetEthernetDstAddr();
586 ActionSetIPv4Addr actionSetIPv4SrcAddr = action
587 .actionSetIPv4SrcAddr();
588 ActionSetIPv4Addr actionSetIPv4DstAddr = action
589 .actionSetIPv4DstAddr();
590 ActionSetIpToS actionSetIpToS = action.actionSetIpToS();
591 ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action
592 .actionSetTcpUdpSrcPort();
593 ActionSetTcpUdpPort actionSetTcpUdpDstPort = action
594 .actionSetTcpUdpDstPort();
595 ActionEnqueue actionEnqueue = action.actionEnqueue();
596
597 if (actionOutput != null) {
598 actionOutputPort = actionOutput.port().value();
599 // XXX: The max length is hard-coded for now
600 OFActionOutput ofa = new OFActionOutput(actionOutput.port()
601 .value(), (short) 0xffff);
602 openFlowActions.add(ofa);
603 actionsLen += ofa.getLength();
604 }
605
606 if (actionSetVlanId != null) {
607 OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier(
608 actionSetVlanId.vlanId());
609 openFlowActions.add(ofa);
610 actionsLen += ofa.getLength();
611 }
612
613 if (actionSetVlanPriority != null) {
614 OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint(
615 actionSetVlanPriority.vlanPriority());
616 openFlowActions.add(ofa);
617 actionsLen += ofa.getLength();
618 }
619
620 if (actionStripVlan != null) {
621 if (actionStripVlan.stripVlan() == true) {
622 OFActionStripVirtualLan ofa = new OFActionStripVirtualLan();
623 openFlowActions.add(ofa);
624 actionsLen += ofa.getLength();
625 }
626 }
627
628 if (actionSetEthernetSrcAddr != null) {
629 OFActionDataLayerSource ofa = new OFActionDataLayerSource(
630 actionSetEthernetSrcAddr.addr().toBytes());
631 openFlowActions.add(ofa);
632 actionsLen += ofa.getLength();
633 }
634
635 if (actionSetEthernetDstAddr != null) {
636 OFActionDataLayerDestination ofa = new OFActionDataLayerDestination(
637 actionSetEthernetDstAddr.addr().toBytes());
638 openFlowActions.add(ofa);
639 actionsLen += ofa.getLength();
640 }
641
642 if (actionSetIPv4SrcAddr != null) {
643 OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource(
644 actionSetIPv4SrcAddr.addr().value());
645 openFlowActions.add(ofa);
646 actionsLen += ofa.getLength();
647 }
648
649 if (actionSetIPv4DstAddr != null) {
650 OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination(
651 actionSetIPv4DstAddr.addr().value());
652 openFlowActions.add(ofa);
653 actionsLen += ofa.getLength();
654 }
655
656 if (actionSetIpToS != null) {
657 OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService(
658 actionSetIpToS.ipToS());
659 openFlowActions.add(ofa);
660 actionsLen += ofa.getLength();
661 }
662
663 if (actionSetTcpUdpSrcPort != null) {
664 OFActionTransportLayerSource ofa = new OFActionTransportLayerSource(
665 actionSetTcpUdpSrcPort.port());
666 openFlowActions.add(ofa);
667 actionsLen += ofa.getLength();
668 }
669
670 if (actionSetTcpUdpDstPort != null) {
671 OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination(
672 actionSetTcpUdpDstPort.port());
673 openFlowActions.add(ofa);
674 actionsLen += ofa.getLength();
675 }
676
677 if (actionEnqueue != null) {
678 OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port()
679 .value(), actionEnqueue.queueId());
680 openFlowActions.add(ofa);
681 actionsLen += ofa.getLength();
682 }
683 }
684
685 fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT)
686 .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT)
687 .setPriority(PRIORITY_DEFAULT)
688 .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie)
689 .setCommand(flowModCommand).setMatch(match)
690 .setActions(openFlowActions)
691 .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen);
692 fm.setOutPort(OFPort.OFPP_NONE.getValue());
693 if ((flowModCommand == OFFlowMod.OFPFC_DELETE)
694 || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) {
695 if (actionOutputPort != null)
696 fm.setOutPort(actionOutputPort);
697 }
698
699 //
700 // TODO: Set the following flag
701 // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM);
702 // See method ForwardingBase::pushRoute()
703 //
704
705 //
706 // Write the message to the switch
707 //
Pavlin Radoslavovf0678902013-12-03 15:06:56 -0800708 log.debug("Installing flow entry "
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800709 + flowEntry.flowEntryUserState() + " into switch DPID: "
710 + sw.getStringId() + " flowEntryId: "
711 + flowEntry.flowEntryId().toString() + " srcMac: "
712 + matchSrcMac + " dstMac: " + matchDstMac + " inPort: "
713 + matchInPort + " outPort: " + actionOutputPort);
714
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800715 if (add(sw, fm) != true)
716 return false;
717
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800718 //
719 // TODO: We should use the OpenFlow Barrier mechanism
720 // to check for errors, and update the SwitchState
721 // for a flow entry after the Barrier message is
722 // is received.
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800723 // Only after inform the Flow Manager that the entry is pushed.
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800724 //
Pavlin Radoslavovda0ab442013-12-04 14:08:58 -0800725 flowManager.flowEntryPushedToSwitch(sw, flowEntry);
726
727 return true;
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800728 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800729
730 @Override
731 public OFBarrierReply barrier(IOFSwitch sw) {
732 OFMessageFuture<OFBarrierReply> future = barrierAsync(sw);
733 if (future == null) {
734 return null;
735 }
736
737 try {
738 return future.get();
739 } catch (InterruptedException e) {
740 e.printStackTrace();
741 log.error("InterruptedException: {}", e);
742 return null;
743 } catch (ExecutionException e) {
744 e.printStackTrace();
745 log.error("ExecutionException: {}", e);
746 return null;
747 }
748 }
Naoki Shiota2e2fc2b2013-11-12 11:21:36 -0800749
Naoki Shiotac1601d32013-11-20 10:47:34 -0800750 @Override
751 public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) {
752 // TODO creation of message and future should be moved to OFSwitchImpl
Naoki Shiota81dbe302013-11-21 15:35:38 -0800753
754 if (sw == null) {
755 return null;
756 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800757
Naoki Shiota81dbe302013-11-21 15:35:38 -0800758 OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800759 msg.setXid(sw.getNextTransactionId());
Naoki Shiotac1601d32013-11-20 10:47:34 -0800760
Naoki Shiotac1601d32013-11-20 10:47:34 -0800761 OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid());
Naoki Shiotac1601d32013-11-20 10:47:34 -0800762 synchronized (barrierFutures) {
763 Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId());
764 if (map == null) {
765 map = new HashMap<Integer,OFBarrierReplyFuture>();
766 barrierFutures.put(sw.getId(), map);
767 }
768 map.put(msg.getXid(), future);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800769 }
770
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800771 add(sw, msg);
772
Naoki Shiotac1601d32013-11-20 10:47:34 -0800773 return future;
774 }
775
Naoki Shiotae3199732013-11-25 16:14:43 -0800776 /**
777 * Get a queue attached to a switch.
778 * @param sw Switch object
779 * @return Queue object
780 */
Naoki Shiotac1601d32013-11-20 10:47:34 -0800781 protected SwitchQueue getQueue(IOFSwitch sw) {
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800782 if (sw == null) {
783 return null;
784 }
785
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800786 return getProcess(sw).queues.get(sw);
787 }
788
Naoki Shiotae3199732013-11-25 16:14:43 -0800789 /**
790 * Get a hash value correspondent to a switch.
791 * @param sw Switch object
792 * @return Hash value
793 */
Naoki Shiotac1601d32013-11-20 10:47:34 -0800794 protected long getHash(IOFSwitch sw) {
795 // This code assumes DPID is sequentially assigned.
796 // TODO consider equalization algorithm
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800797 return sw.getId() % number_thread;
798 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800799
800 /**
801 * Get a Thread object which processes the queue attached to a switch.
802 * @param sw Switch object
803 * @return Thread object
804 */
Naoki Shiota81dbe302013-11-21 15:35:38 -0800805 protected FlowPusherThread getProcess(IOFSwitch sw) {
Naoki Shiotaf0cddbf2013-11-12 14:59:43 -0800806 long hash = getHash(sw);
807
808 return threadMap.get(hash);
Naoki Shiota7d0cf272013-11-05 10:18:12 -0800809 }
Naoki Shiotac1601d32013-11-20 10:47:34 -0800810
811 @Override
812 public String getName() {
813 return "flowpusher";
814 }
815
816 @Override
817 public boolean isCallbackOrderingPrereq(OFType type, String name) {
818 return false;
819 }
820
821 @Override
822 public boolean isCallbackOrderingPostreq(OFType type, String name) {
823 return false;
824 }
825
826 @Override
827 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
Naoki Shiotac1601d32013-11-20 10:47:34 -0800828 Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId());
829 if (map == null) {
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800830 log.debug("null map for {} : {}", sw.getId(), barrierFutures);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800831 return Command.CONTINUE;
832 }
833
834 OFBarrierReplyFuture future = map.get(msg.getXid());
835 if (future == null) {
Naoki Shiotac2ec5592013-11-27 12:10:33 -0800836 log.debug("null future for {} : {}", msg.getXid(), map);
Naoki Shiotac1601d32013-11-20 10:47:34 -0800837 return Command.CONTINUE;
838 }
839
840 log.debug("Received BARRIER_REPLY : {}", msg);
841 future.deliverFuture(sw, msg);
842
843 return Command.CONTINUE;
844 }
Naoki Shiotae3199732013-11-25 16:14:43 -0800845
Naoki Shiotaed4eb5e2013-10-31 10:55:32 -0700846}