Naoki Shiota | aea8858 | 2013-11-12 17:58:34 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowprogrammer; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 2 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 4 | import java.util.ArrayDeque; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.EnumSet; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 8 | import java.util.HashSet; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 9 | import java.util.List; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 10 | import java.util.Map; |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 11 | import java.util.Set; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 12 | import java.util.concurrent.ExecutionException; |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 13 | import java.util.concurrent.Semaphore; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 14 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 15 | import org.openflow.protocol.*; |
| 16 | import org.openflow.protocol.action.*; |
| 17 | import org.openflow.protocol.factory.BasicFactory; |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 20 | |
| 21 | import net.floodlightcontroller.core.FloodlightContext; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 23 | import net.floodlightcontroller.core.IOFMessageListener; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 26 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 27 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 28 | import net.floodlightcontroller.util.MACAddress; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 29 | import net.floodlightcontroller.util.OFMessageDamper; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 31 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.*; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 33 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowEntryId; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 36 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
| 38 | import net.onrc.onos.ofcontroller.util.Port; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 41 | * FlowPusher is a implementation of FlowPusherService. |
| 42 | * FlowPusher assigns one message queue instance for each one switch. |
| 43 | * Number of message processing threads is configurable by constructor, and |
| 44 | * one thread can handle multiple message queues. Each queue will be assigned to |
| 45 | * a thread according to hash function defined by getHash(). |
| 46 | * Each processing thread reads messages from queues and sends it to switches |
| 47 | * in round-robin. Processing thread also calculates rate of sending to suppress |
| 48 | * excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 49 | * @author Naoki Shiota |
| 50 | * |
| 51 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 52 | public class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 53 | private final static Logger log = LoggerFactory.getLogger(FlowPusher.class); |
| 54 | |
| 55 | // NOTE: Below are moved from FlowManager. |
| 56 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 57 | // The local copy should go away! |
| 58 | // |
| 59 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 60 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 61 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 62 | // Number of messages sent to switch at once |
| 63 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 64 | |
| 65 | public static final short PRIORITY_DEFAULT = 100; |
| 66 | public static final short FLOWMOD_DEFAULT_IDLE_TIMEOUT = 0; // infinity |
| 67 | public static final short FLOWMOD_DEFAULT_HARD_TIMEOUT = 0; // infinite |
| 68 | |
| 69 | public enum QueueState { |
| 70 | READY, |
| 71 | SUSPENDED, |
| 72 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 73 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 74 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 75 | * SwitchQueue represents message queue attached to a switch. |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 76 | * This consists of queue itself and variables used for limiting sending rate. |
| 77 | * @author Naoki Shiota |
| 78 | * |
| 79 | */ |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 80 | @SuppressWarnings("serial") |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 81 | private class SwitchQueue extends ArrayDeque<OFMessage> { |
| 82 | QueueState state; |
| 83 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 84 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 85 | long max_rate = 0; // 0 indicates no limitation |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 86 | long last_sent_time = 0; |
| 87 | long last_sent_size = 0; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 88 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 89 | // "To be deleted" flag |
| 90 | boolean toBeDeleted = false; |
| 91 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Check if sending rate is within the rate |
| 94 | * @param current Current time |
| 95 | * @return true if within the rate |
| 96 | */ |
| 97 | boolean isSendable(long current) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 98 | if (max_rate == 0) { |
| 99 | // no limitation |
| 100 | return true; |
| 101 | } |
| 102 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 103 | if (current == last_sent_time) { |
| 104 | return false; |
| 105 | } |
| 106 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 107 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 108 | long rate = last_sent_size / (current - last_sent_time); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 109 | return (rate < max_rate); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Log time and size of last sent data. |
| 114 | * @param current Time to be sent. |
| 115 | * @param size Size of sent data (in bytes). |
| 116 | */ |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 117 | void logSentData(long current, long size) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 118 | last_sent_time = current; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 119 | last_sent_size = size; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 122 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 123 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 124 | private OFMessageDamper messageDamper = null; |
| 125 | private IThreadPoolService threadPool = null; |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 126 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 127 | private FloodlightContext context = null; |
| 128 | private BasicFactory factory = null; |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 129 | |
| 130 | // Map of threads versus dpid |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 131 | private Map<Long, FlowPusherThread> threadMap = null; |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 132 | // Map of Future objects versus dpid and transaction ID. |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 133 | private Map<Long, Map<Integer, OFBarrierReplyFuture>> |
| 134 | barrierFutures = new HashMap<Long, Map<Integer, OFBarrierReplyFuture>>(); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 135 | |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 136 | private int number_thread = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 137 | |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Main thread that reads messages from queues and sends them to switches. |
| 140 | * @author Naoki Shiota |
| 141 | * |
| 142 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 143 | private class FlowPusherThread extends Thread { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 144 | private Map<IOFSwitch,SwitchQueue> queues |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 145 | = new HashMap<IOFSwitch,SwitchQueue>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 146 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 147 | // reusable latch used for waiting for arrival of message |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 148 | private Semaphore mutex = new Semaphore(0); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 149 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void run() { |
| 152 | while (true) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 153 | try { |
| 154 | // wait for message pushed to queue |
| 155 | mutex.acquire(); |
| 156 | } catch (InterruptedException e) { |
| 157 | e.printStackTrace(); |
| 158 | log.debug("FlowPusherThread is interrupted"); |
| 159 | return; |
| 160 | } |
| 161 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 162 | // for safety of concurrent access, copy all key objects |
| 163 | Set<IOFSwitch> keys = new HashSet<IOFSwitch>(queues.size()); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 164 | synchronized (queues) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 165 | for (IOFSwitch sw : queues.keySet()) { |
| 166 | keys.add(sw); |
| 167 | } |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 170 | for (IOFSwitch sw : keys) { |
| 171 | SwitchQueue queue = queues.get(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 172 | |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 173 | // Skip if queue is suspended |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 174 | if (sw == null || queue == null || |
| 175 | queue.state != QueueState.READY) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 176 | continue; |
| 177 | } |
| 178 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 179 | synchronized (queue) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 180 | processQueue(sw, queue, MAX_MESSAGE_SEND); |
| 181 | if (queue.isEmpty()) { |
| 182 | // remove queue if flagged to be. |
| 183 | if (queue.toBeDeleted) { |
| 184 | synchronized (queues) { |
| 185 | queues.remove(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 188 | } else { |
| 189 | // if some messages remains in queue, latch down |
| 190 | if (mutex.availablePermits() == 0) { |
| 191 | mutex.release(); |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 192 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Read messages from queue and send them to the switch. |
| 201 | * If number of messages excess the limit, stop sending messages. |
| 202 | * @param sw Switch to which messages will be sent. |
| 203 | * @param queue Queue of messages. |
| 204 | * @param max_msg Limitation of number of messages to be sent. If set to 0, |
| 205 | * all messages in queue will be sent. |
| 206 | */ |
| 207 | private void processQueue(IOFSwitch sw, SwitchQueue queue, long max_msg) { |
| 208 | // check sending rate and determine it to be sent or not |
| 209 | long current_time = System.currentTimeMillis(); |
| 210 | long size = 0; |
| 211 | |
| 212 | if (queue.isSendable(current_time)) { |
| 213 | int i = 0; |
| 214 | while (! queue.isEmpty()) { |
| 215 | // Number of messages excess the limit |
| 216 | if (0 < max_msg && max_msg <= i) { |
| 217 | break; |
| 218 | } |
| 219 | ++i; |
| 220 | |
| 221 | OFMessage msg = queue.poll(); |
| 222 | try { |
| 223 | messageDamper.write(sw, msg, context); |
| 224 | log.debug("Pusher sends message : {}", msg); |
| 225 | size += msg.getLength(); |
| 226 | } catch (IOException e) { |
| 227 | e.printStackTrace(); |
| 228 | log.error("Exception in sending message ({}) : {}", msg, e); |
| 229 | } |
| 230 | } |
| 231 | sw.flush(); |
| 232 | queue.logSentData(current_time, size); |
| 233 | } |
| 234 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Initialize object with one thread. |
| 239 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 240 | public FlowPusher() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 243 | /** |
| 244 | * Initialize object with threads of given number. |
| 245 | * @param number_thread Number of threads to handle messages. |
| 246 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 247 | public FlowPusher(int number_thread) { |
| 248 | this.number_thread = number_thread; |
| 249 | } |
| 250 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 251 | /** |
| 252 | * Set parameters needed for sending messages. |
| 253 | * @param context FloodlightContext used for sending messages. |
| 254 | * If null, FlowPusher uses default context. |
| 255 | * @param modContext FloodlightModuleContext used for acquiring |
| 256 | * ThreadPoolService and registering MessageListener. |
| 257 | * @param factory Factory object to create OFMessage objects. |
| 258 | * @param damper Message damper used for sending messages. |
| 259 | * If null, FlowPusher creates its own damper object. |
| 260 | */ |
| 261 | public void init(FloodlightContext context, |
| 262 | FloodlightModuleContext modContext, |
| 263 | BasicFactory factory, |
| 264 | OFMessageDamper damper) { |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 265 | this.context = context; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 266 | this.factory = factory; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 267 | this.threadPool = modContext.getServiceImpl(IThreadPoolService.class); |
| 268 | IFloodlightProviderService flservice = modContext.getServiceImpl(IFloodlightProviderService.class); |
| 269 | flservice.addOFMessageListener(OFType.BARRIER_REPLY, this); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 270 | |
| 271 | if (damper != null) { |
| 272 | messageDamper = damper; |
| 273 | } else { |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 274 | // use default values |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 275 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 276 | EnumSet.of(OFType.FLOW_MOD), |
| 277 | OFMESSAGE_DAMPER_TIMEOUT); |
| 278 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 279 | } |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 280 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 281 | /** |
| 282 | * Begin processing queue. |
| 283 | */ |
| 284 | public void start() { |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 285 | if (factory == null) { |
| 286 | log.error("FlowPusher not yet initialized."); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 287 | return; |
| 288 | } |
| 289 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 290 | threadMap = new HashMap<Long,FlowPusherThread>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 291 | for (long i = 0; i < number_thread; ++i) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 292 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 293 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 294 | threadMap.put(i, thread); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 295 | thread.start(); |
| 296 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 299 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 300 | public boolean suspend(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 301 | SwitchQueue queue = getQueue(sw); |
| 302 | |
| 303 | if (queue == null) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 304 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | synchronized (queue) { |
| 308 | if (queue.state == QueueState.READY) { |
| 309 | queue.state = QueueState.SUSPENDED; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 310 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 311 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 312 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 316 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 317 | public boolean resume(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 318 | SwitchQueue queue = getQueue(sw); |
| 319 | |
| 320 | if (queue == null) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 321 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | synchronized (queue) { |
| 325 | if (queue.state == QueueState.SUSPENDED) { |
| 326 | queue.state = QueueState.READY; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 327 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 328 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 329 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 332 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 333 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 334 | public boolean isSuspended(IOFSwitch sw) { |
| 335 | SwitchQueue queue = getQueue(sw); |
| 336 | |
| 337 | if (queue == null) { |
| 338 | // TODO Is true suitable for this case? |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | return (queue.state == QueueState.SUSPENDED); |
| 343 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 344 | |
| 345 | /** |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 346 | * Stop processing queue and exit thread. |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 347 | */ |
| 348 | public void stop() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 349 | if (threadMap == null) { |
| 350 | return; |
| 351 | } |
| 352 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 353 | for (FlowPusherThread t : threadMap.values()) { |
| 354 | t.interrupt(); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 358 | @Override |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 359 | public void setRate(IOFSwitch sw, long rate) { |
| 360 | SwitchQueue queue = getQueue(sw); |
| 361 | if (queue == null) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | if (rate > 0) { |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 366 | log.debug("rate for {} is set to {}", sw.getId(), rate); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 367 | queue.max_rate = rate; |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 368 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 369 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 370 | |
| 371 | @Override |
| 372 | public boolean createQueue(IOFSwitch sw) { |
| 373 | SwitchQueue queue = getQueue(sw); |
| 374 | if (queue != null) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | FlowPusherThread proc = getProcess(sw); |
| 379 | queue = new SwitchQueue(); |
| 380 | queue.state = QueueState.READY; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 381 | synchronized (proc.queues) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 382 | proc.queues.put(sw, queue); |
| 383 | } |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public boolean deleteQueue(IOFSwitch sw) { |
| 390 | return deleteQueue(sw, false); |
| 391 | } |
| 392 | |
| 393 | @Override |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 394 | public boolean deleteQueue(IOFSwitch sw, boolean forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 395 | FlowPusherThread proc = getProcess(sw); |
| 396 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 397 | if (forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 398 | synchronized (proc.queues) { |
| 399 | SwitchQueue queue = proc.queues.remove(sw); |
| 400 | if (queue == null) { |
| 401 | return false; |
| 402 | } |
| 403 | } |
| 404 | return true; |
| 405 | } else { |
| 406 | SwitchQueue queue = getQueue(sw); |
| 407 | if (queue == null) { |
| 408 | return false; |
| 409 | } |
| 410 | synchronized (queue) { |
| 411 | queue.toBeDeleted = true; |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 416 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 417 | @Override |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 418 | public boolean add(IOFSwitch sw, OFMessage msg) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 419 | FlowPusherThread proc = getProcess(sw); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 420 | SwitchQueue queue = proc.queues.get(sw); |
| 421 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 422 | // create queue at first addition of message |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 423 | if (queue == null) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 424 | createQueue(sw); |
| 425 | queue = getQueue(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | synchronized (queue) { |
| 429 | queue.add(msg); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 430 | log.debug("Message is pushed : {}", msg); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 433 | if (proc.mutex.availablePermits() == 0) { |
| 434 | proc.mutex.release(); |
| 435 | } |
| 436 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 440 | @Override |
Pavlin Radoslavov | 6bfaea6 | 2013-12-03 14:55:57 -0800 | [diff] [blame^] | 441 | public boolean add(IOFSwitch sw, FlowEntry flowEntry) { |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 442 | // |
| 443 | // Create the OpenFlow Flow Modification Entry to push |
| 444 | // |
| 445 | OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD); |
| 446 | long cookie = flowEntry.flowEntryId().value(); |
| 447 | |
| 448 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 449 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 450 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 451 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 452 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 453 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 454 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 455 | } else { |
| 456 | // Unknown user state. Ignore the entry |
| 457 | log.debug( |
| 458 | "Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 459 | flowEntry.flowEntryId().toString(), |
| 460 | flowEntry.flowEntryUserState()); |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // |
| 465 | // Fetch the match conditions. |
| 466 | // |
| 467 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 468 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 469 | // condition set. |
| 470 | // |
| 471 | OFMatch match = new OFMatch(); |
| 472 | match.setWildcards(OFMatch.OFPFW_ALL); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 473 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 474 | |
| 475 | // Match the Incoming Port |
| 476 | Port matchInPort = flowEntryMatch.inPort(); |
| 477 | if (matchInPort != null) { |
| 478 | match.setInputPort(matchInPort.value()); |
| 479 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 480 | } |
| 481 | |
| 482 | // Match the Source MAC address |
| 483 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 484 | if (matchSrcMac != null) { |
| 485 | match.setDataLayerSource(matchSrcMac.toString()); |
| 486 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 487 | } |
| 488 | |
| 489 | // Match the Destination MAC address |
| 490 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 491 | if (matchDstMac != null) { |
| 492 | match.setDataLayerDestination(matchDstMac.toString()); |
| 493 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 494 | } |
| 495 | |
| 496 | // Match the Ethernet Frame Type |
| 497 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 498 | if (matchEthernetFrameType != null) { |
| 499 | match.setDataLayerType(matchEthernetFrameType); |
| 500 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 501 | } |
| 502 | |
| 503 | // Match the VLAN ID |
| 504 | Short matchVlanId = flowEntryMatch.vlanId(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 505 | if (matchVlanId != null) { |
| 506 | match.setDataLayerVirtualLan(matchVlanId); |
| 507 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 508 | } |
| 509 | |
| 510 | // Match the VLAN priority |
| 511 | Byte matchVlanPriority = flowEntryMatch.vlanPriority(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 512 | if (matchVlanPriority != null) { |
| 513 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 514 | match.setWildcards(match.getWildcards() |
| 515 | & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 516 | } |
| 517 | |
| 518 | // Match the Source IPv4 Network prefix |
| 519 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 520 | if (matchSrcIPv4Net != null) { |
| 521 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 522 | } |
| 523 | |
| 524 | // Natch the Destination IPv4 Network prefix |
| 525 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 526 | if (matchDstIPv4Net != null) { |
| 527 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 528 | } |
| 529 | |
| 530 | // Match the IP protocol |
| 531 | Byte matchIpProto = flowEntryMatch.ipProto(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 532 | if (matchIpProto != null) { |
| 533 | match.setNetworkProtocol(matchIpProto); |
| 534 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
| 535 | } |
| 536 | |
| 537 | // Match the IP ToS (DSCP field, 6 bits) |
| 538 | Byte matchIpToS = flowEntryMatch.ipToS(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 539 | if (matchIpToS != null) { |
| 540 | match.setNetworkTypeOfService(matchIpToS); |
| 541 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 542 | } |
| 543 | |
| 544 | // Match the Source TCP/UDP port |
| 545 | Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 546 | if (matchSrcTcpUdpPort != null) { |
| 547 | match.setTransportSource(matchSrcTcpUdpPort); |
| 548 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 549 | } |
| 550 | |
| 551 | // Match the Destination TCP/UDP port |
| 552 | Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 553 | if (matchDstTcpUdpPort != null) { |
| 554 | match.setTransportDestination(matchDstTcpUdpPort); |
| 555 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 556 | } |
| 557 | |
| 558 | // |
| 559 | // Fetch the actions |
| 560 | // |
| 561 | Short actionOutputPort = null; |
| 562 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 563 | int actionsLen = 0; |
| 564 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 565 | // |
| 566 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 567 | ActionOutput actionOutput = action.actionOutput(); |
| 568 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 569 | ActionSetVlanPriority actionSetVlanPriority = action |
| 570 | .actionSetVlanPriority(); |
| 571 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 572 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action |
| 573 | .actionSetEthernetSrcAddr(); |
| 574 | ActionSetEthernetAddr actionSetEthernetDstAddr = action |
| 575 | .actionSetEthernetDstAddr(); |
| 576 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action |
| 577 | .actionSetIPv4SrcAddr(); |
| 578 | ActionSetIPv4Addr actionSetIPv4DstAddr = action |
| 579 | .actionSetIPv4DstAddr(); |
| 580 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 581 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action |
| 582 | .actionSetTcpUdpSrcPort(); |
| 583 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action |
| 584 | .actionSetTcpUdpDstPort(); |
| 585 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 586 | |
| 587 | if (actionOutput != null) { |
| 588 | actionOutputPort = actionOutput.port().value(); |
| 589 | // XXX: The max length is hard-coded for now |
| 590 | OFActionOutput ofa = new OFActionOutput(actionOutput.port() |
| 591 | .value(), (short) 0xffff); |
| 592 | openFlowActions.add(ofa); |
| 593 | actionsLen += ofa.getLength(); |
| 594 | } |
| 595 | |
| 596 | if (actionSetVlanId != null) { |
| 597 | OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier( |
| 598 | actionSetVlanId.vlanId()); |
| 599 | openFlowActions.add(ofa); |
| 600 | actionsLen += ofa.getLength(); |
| 601 | } |
| 602 | |
| 603 | if (actionSetVlanPriority != null) { |
| 604 | OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint( |
| 605 | actionSetVlanPriority.vlanPriority()); |
| 606 | openFlowActions.add(ofa); |
| 607 | actionsLen += ofa.getLength(); |
| 608 | } |
| 609 | |
| 610 | if (actionStripVlan != null) { |
| 611 | if (actionStripVlan.stripVlan() == true) { |
| 612 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 613 | openFlowActions.add(ofa); |
| 614 | actionsLen += ofa.getLength(); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (actionSetEthernetSrcAddr != null) { |
| 619 | OFActionDataLayerSource ofa = new OFActionDataLayerSource( |
| 620 | actionSetEthernetSrcAddr.addr().toBytes()); |
| 621 | openFlowActions.add(ofa); |
| 622 | actionsLen += ofa.getLength(); |
| 623 | } |
| 624 | |
| 625 | if (actionSetEthernetDstAddr != null) { |
| 626 | OFActionDataLayerDestination ofa = new OFActionDataLayerDestination( |
| 627 | actionSetEthernetDstAddr.addr().toBytes()); |
| 628 | openFlowActions.add(ofa); |
| 629 | actionsLen += ofa.getLength(); |
| 630 | } |
| 631 | |
| 632 | if (actionSetIPv4SrcAddr != null) { |
| 633 | OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource( |
| 634 | actionSetIPv4SrcAddr.addr().value()); |
| 635 | openFlowActions.add(ofa); |
| 636 | actionsLen += ofa.getLength(); |
| 637 | } |
| 638 | |
| 639 | if (actionSetIPv4DstAddr != null) { |
| 640 | OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination( |
| 641 | actionSetIPv4DstAddr.addr().value()); |
| 642 | openFlowActions.add(ofa); |
| 643 | actionsLen += ofa.getLength(); |
| 644 | } |
| 645 | |
| 646 | if (actionSetIpToS != null) { |
| 647 | OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService( |
| 648 | actionSetIpToS.ipToS()); |
| 649 | openFlowActions.add(ofa); |
| 650 | actionsLen += ofa.getLength(); |
| 651 | } |
| 652 | |
| 653 | if (actionSetTcpUdpSrcPort != null) { |
| 654 | OFActionTransportLayerSource ofa = new OFActionTransportLayerSource( |
| 655 | actionSetTcpUdpSrcPort.port()); |
| 656 | openFlowActions.add(ofa); |
| 657 | actionsLen += ofa.getLength(); |
| 658 | } |
| 659 | |
| 660 | if (actionSetTcpUdpDstPort != null) { |
| 661 | OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination( |
| 662 | actionSetTcpUdpDstPort.port()); |
| 663 | openFlowActions.add(ofa); |
| 664 | actionsLen += ofa.getLength(); |
| 665 | } |
| 666 | |
| 667 | if (actionEnqueue != null) { |
| 668 | OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port() |
| 669 | .value(), actionEnqueue.queueId()); |
| 670 | openFlowActions.add(ofa); |
| 671 | actionsLen += ofa.getLength(); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 676 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 677 | .setPriority(PRIORITY_DEFAULT) |
| 678 | .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie) |
| 679 | .setCommand(flowModCommand).setMatch(match) |
| 680 | .setActions(openFlowActions) |
| 681 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 682 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 683 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) |
| 684 | || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 685 | if (actionOutputPort != null) |
| 686 | fm.setOutPort(actionOutputPort); |
| 687 | } |
| 688 | |
| 689 | // |
| 690 | // TODO: Set the following flag |
| 691 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 692 | // See method ForwardingBase::pushRoute() |
| 693 | // |
| 694 | |
| 695 | // |
| 696 | // Write the message to the switch |
| 697 | // |
| 698 | log.debug("MEASUREMENT: Installing flow entry " |
| 699 | + flowEntry.flowEntryUserState() + " into switch DPID: " |
| 700 | + sw.getStringId() + " flowEntryId: " |
| 701 | + flowEntry.flowEntryId().toString() + " srcMac: " |
| 702 | + matchSrcMac + " dstMac: " + matchDstMac + " inPort: " |
| 703 | + matchInPort + " outPort: " + actionOutputPort); |
| 704 | |
| 705 | // |
| 706 | // TODO: We should use the OpenFlow Barrier mechanism |
| 707 | // to check for errors, and update the SwitchState |
| 708 | // for a flow entry after the Barrier message is |
| 709 | // is received. |
| 710 | // |
| 711 | // TODO: The FlowEntry Object in Titan should be set |
| 712 | // to FE_SWITCH_UPDATED. |
| 713 | // |
| 714 | |
| 715 | return add(sw,fm); |
| 716 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 717 | |
| 718 | @Override |
| 719 | public OFBarrierReply barrier(IOFSwitch sw) { |
| 720 | OFMessageFuture<OFBarrierReply> future = barrierAsync(sw); |
| 721 | if (future == null) { |
| 722 | return null; |
| 723 | } |
| 724 | |
| 725 | try { |
| 726 | return future.get(); |
| 727 | } catch (InterruptedException e) { |
| 728 | e.printStackTrace(); |
| 729 | log.error("InterruptedException: {}", e); |
| 730 | return null; |
| 731 | } catch (ExecutionException e) { |
| 732 | e.printStackTrace(); |
| 733 | log.error("ExecutionException: {}", e); |
| 734 | return null; |
| 735 | } |
| 736 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 737 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 738 | @Override |
| 739 | public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) { |
| 740 | // TODO creation of message and future should be moved to OFSwitchImpl |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 741 | |
| 742 | if (sw == null) { |
| 743 | return null; |
| 744 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 745 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 746 | OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 747 | msg.setXid(sw.getNextTransactionId()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 748 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 749 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 750 | synchronized (barrierFutures) { |
| 751 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 752 | if (map == null) { |
| 753 | map = new HashMap<Integer,OFBarrierReplyFuture>(); |
| 754 | barrierFutures.put(sw.getId(), map); |
| 755 | } |
| 756 | map.put(msg.getXid(), future); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 759 | add(sw, msg); |
| 760 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 761 | return future; |
| 762 | } |
| 763 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 764 | /** |
| 765 | * Get a queue attached to a switch. |
| 766 | * @param sw Switch object |
| 767 | * @return Queue object |
| 768 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 769 | protected SwitchQueue getQueue(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 770 | if (sw == null) { |
| 771 | return null; |
| 772 | } |
| 773 | |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 774 | return getProcess(sw).queues.get(sw); |
| 775 | } |
| 776 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 777 | /** |
| 778 | * Get a hash value correspondent to a switch. |
| 779 | * @param sw Switch object |
| 780 | * @return Hash value |
| 781 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 782 | protected long getHash(IOFSwitch sw) { |
| 783 | // This code assumes DPID is sequentially assigned. |
| 784 | // TODO consider equalization algorithm |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 785 | return sw.getId() % number_thread; |
| 786 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 787 | |
| 788 | /** |
| 789 | * Get a Thread object which processes the queue attached to a switch. |
| 790 | * @param sw Switch object |
| 791 | * @return Thread object |
| 792 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 793 | protected FlowPusherThread getProcess(IOFSwitch sw) { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 794 | long hash = getHash(sw); |
| 795 | |
| 796 | return threadMap.get(hash); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 797 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 798 | |
| 799 | @Override |
| 800 | public String getName() { |
| 801 | return "flowpusher"; |
| 802 | } |
| 803 | |
| 804 | @Override |
| 805 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | @Override |
| 810 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 816 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 817 | if (map == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 818 | log.debug("null map for {} : {}", sw.getId(), barrierFutures); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 819 | return Command.CONTINUE; |
| 820 | } |
| 821 | |
| 822 | OFBarrierReplyFuture future = map.get(msg.getXid()); |
| 823 | if (future == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 824 | log.debug("null future for {} : {}", msg.getXid(), map); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 825 | return Command.CONTINUE; |
| 826 | } |
| 827 | |
| 828 | log.debug("Received BARRIER_REPLY : {}", msg); |
| 829 | future.deliverFuture(sw, msg); |
| 830 | |
| 831 | return Command.CONTINUE; |
| 832 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 833 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 834 | } |