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 | 0aa947e | 2013-11-27 14:47:35 -0800 | [diff] [blame^] | 327 | |
| 328 | // Latch down if queue is not empty |
| 329 | FlowPusherThread thread = getProcess(sw); |
| 330 | if (! queue.isEmpty() && |
| 331 | thread.mutex.availablePermits() == 0) { |
| 332 | thread.mutex.release(); |
| 333 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 334 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 335 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 336 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 339 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 340 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 341 | public boolean isSuspended(IOFSwitch sw) { |
| 342 | SwitchQueue queue = getQueue(sw); |
| 343 | |
| 344 | if (queue == null) { |
| 345 | // TODO Is true suitable for this case? |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | return (queue.state == QueueState.SUSPENDED); |
| 350 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 351 | |
| 352 | /** |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 353 | * Stop processing queue and exit thread. |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 354 | */ |
| 355 | public void stop() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 356 | if (threadMap == null) { |
| 357 | return; |
| 358 | } |
| 359 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 360 | for (FlowPusherThread t : threadMap.values()) { |
| 361 | t.interrupt(); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 365 | @Override |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 366 | public void setRate(IOFSwitch sw, long rate) { |
| 367 | SwitchQueue queue = getQueue(sw); |
| 368 | if (queue == null) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | if (rate > 0) { |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 373 | log.debug("rate for {} is set to {}", sw.getId(), rate); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 374 | queue.max_rate = rate; |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 375 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 376 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 377 | |
| 378 | @Override |
| 379 | public boolean createQueue(IOFSwitch sw) { |
| 380 | SwitchQueue queue = getQueue(sw); |
| 381 | if (queue != null) { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | FlowPusherThread proc = getProcess(sw); |
| 386 | queue = new SwitchQueue(); |
| 387 | queue.state = QueueState.READY; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 388 | synchronized (proc.queues) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 389 | proc.queues.put(sw, queue); |
| 390 | } |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public boolean deleteQueue(IOFSwitch sw) { |
| 397 | return deleteQueue(sw, false); |
| 398 | } |
| 399 | |
| 400 | @Override |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 401 | public boolean deleteQueue(IOFSwitch sw, boolean forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 402 | FlowPusherThread proc = getProcess(sw); |
| 403 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 404 | if (forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 405 | synchronized (proc.queues) { |
| 406 | SwitchQueue queue = proc.queues.remove(sw); |
| 407 | if (queue == null) { |
| 408 | return false; |
| 409 | } |
| 410 | } |
| 411 | return true; |
| 412 | } else { |
| 413 | SwitchQueue queue = getQueue(sw); |
| 414 | if (queue == null) { |
| 415 | return false; |
| 416 | } |
| 417 | synchronized (queue) { |
| 418 | queue.toBeDeleted = true; |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 423 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 424 | @Override |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 425 | public boolean add(IOFSwitch sw, OFMessage msg) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 426 | FlowPusherThread proc = getProcess(sw); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 427 | SwitchQueue queue = proc.queues.get(sw); |
| 428 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 429 | // create queue at first addition of message |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 430 | if (queue == null) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 431 | createQueue(sw); |
| 432 | queue = getQueue(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | synchronized (queue) { |
| 436 | queue.add(msg); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 437 | log.debug("Message is pushed : {}", msg); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 440 | if (proc.mutex.availablePermits() == 0) { |
| 441 | proc.mutex.release(); |
| 442 | } |
| 443 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 444 | return true; |
| 445 | } |
| 446 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 447 | @Override |
Pavlin Radoslavov | 6bfaea6 | 2013-12-03 14:55:57 -0800 | [diff] [blame] | 448 | public boolean add(IOFSwitch sw, FlowEntry flowEntry) { |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 449 | // |
| 450 | // Create the OpenFlow Flow Modification Entry to push |
| 451 | // |
| 452 | OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD); |
| 453 | long cookie = flowEntry.flowEntryId().value(); |
| 454 | |
| 455 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 456 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 457 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 458 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 459 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 460 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 461 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 462 | } else { |
| 463 | // Unknown user state. Ignore the entry |
| 464 | log.debug( |
| 465 | "Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
| 466 | flowEntry.flowEntryId().toString(), |
| 467 | flowEntry.flowEntryUserState()); |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // |
| 472 | // Fetch the match conditions. |
| 473 | // |
| 474 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 475 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 476 | // condition set. |
| 477 | // |
| 478 | OFMatch match = new OFMatch(); |
| 479 | match.setWildcards(OFMatch.OFPFW_ALL); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 480 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 481 | |
| 482 | // Match the Incoming Port |
| 483 | Port matchInPort = flowEntryMatch.inPort(); |
| 484 | if (matchInPort != null) { |
| 485 | match.setInputPort(matchInPort.value()); |
| 486 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 487 | } |
| 488 | |
| 489 | // Match the Source MAC address |
| 490 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 491 | if (matchSrcMac != null) { |
| 492 | match.setDataLayerSource(matchSrcMac.toString()); |
| 493 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 494 | } |
| 495 | |
| 496 | // Match the Destination MAC address |
| 497 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 498 | if (matchDstMac != null) { |
| 499 | match.setDataLayerDestination(matchDstMac.toString()); |
| 500 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 501 | } |
| 502 | |
| 503 | // Match the Ethernet Frame Type |
| 504 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 505 | if (matchEthernetFrameType != null) { |
| 506 | match.setDataLayerType(matchEthernetFrameType); |
| 507 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 508 | } |
| 509 | |
| 510 | // Match the VLAN ID |
| 511 | Short matchVlanId = flowEntryMatch.vlanId(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 512 | if (matchVlanId != null) { |
| 513 | match.setDataLayerVirtualLan(matchVlanId); |
| 514 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 515 | } |
| 516 | |
| 517 | // Match the VLAN priority |
| 518 | Byte matchVlanPriority = flowEntryMatch.vlanPriority(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 519 | if (matchVlanPriority != null) { |
| 520 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 521 | match.setWildcards(match.getWildcards() |
| 522 | & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 523 | } |
| 524 | |
| 525 | // Match the Source IPv4 Network prefix |
| 526 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 527 | if (matchSrcIPv4Net != null) { |
| 528 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 529 | } |
| 530 | |
| 531 | // Natch the Destination IPv4 Network prefix |
| 532 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 533 | if (matchDstIPv4Net != null) { |
| 534 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 535 | } |
| 536 | |
| 537 | // Match the IP protocol |
| 538 | Byte matchIpProto = flowEntryMatch.ipProto(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 539 | if (matchIpProto != null) { |
| 540 | match.setNetworkProtocol(matchIpProto); |
| 541 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
| 542 | } |
| 543 | |
| 544 | // Match the IP ToS (DSCP field, 6 bits) |
| 545 | Byte matchIpToS = flowEntryMatch.ipToS(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 546 | if (matchIpToS != null) { |
| 547 | match.setNetworkTypeOfService(matchIpToS); |
| 548 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 549 | } |
| 550 | |
| 551 | // Match the Source TCP/UDP port |
| 552 | Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 553 | if (matchSrcTcpUdpPort != null) { |
| 554 | match.setTransportSource(matchSrcTcpUdpPort); |
| 555 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 556 | } |
| 557 | |
| 558 | // Match the Destination TCP/UDP port |
| 559 | Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 560 | if (matchDstTcpUdpPort != null) { |
| 561 | match.setTransportDestination(matchDstTcpUdpPort); |
| 562 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 563 | } |
| 564 | |
| 565 | // |
| 566 | // Fetch the actions |
| 567 | // |
| 568 | Short actionOutputPort = null; |
| 569 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 570 | int actionsLen = 0; |
| 571 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 572 | // |
| 573 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 574 | ActionOutput actionOutput = action.actionOutput(); |
| 575 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 576 | ActionSetVlanPriority actionSetVlanPriority = action |
| 577 | .actionSetVlanPriority(); |
| 578 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 579 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action |
| 580 | .actionSetEthernetSrcAddr(); |
| 581 | ActionSetEthernetAddr actionSetEthernetDstAddr = action |
| 582 | .actionSetEthernetDstAddr(); |
| 583 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action |
| 584 | .actionSetIPv4SrcAddr(); |
| 585 | ActionSetIPv4Addr actionSetIPv4DstAddr = action |
| 586 | .actionSetIPv4DstAddr(); |
| 587 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 588 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action |
| 589 | .actionSetTcpUdpSrcPort(); |
| 590 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action |
| 591 | .actionSetTcpUdpDstPort(); |
| 592 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 593 | |
| 594 | if (actionOutput != null) { |
| 595 | actionOutputPort = actionOutput.port().value(); |
| 596 | // XXX: The max length is hard-coded for now |
| 597 | OFActionOutput ofa = new OFActionOutput(actionOutput.port() |
| 598 | .value(), (short) 0xffff); |
| 599 | openFlowActions.add(ofa); |
| 600 | actionsLen += ofa.getLength(); |
| 601 | } |
| 602 | |
| 603 | if (actionSetVlanId != null) { |
| 604 | OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier( |
| 605 | actionSetVlanId.vlanId()); |
| 606 | openFlowActions.add(ofa); |
| 607 | actionsLen += ofa.getLength(); |
| 608 | } |
| 609 | |
| 610 | if (actionSetVlanPriority != null) { |
| 611 | OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint( |
| 612 | actionSetVlanPriority.vlanPriority()); |
| 613 | openFlowActions.add(ofa); |
| 614 | actionsLen += ofa.getLength(); |
| 615 | } |
| 616 | |
| 617 | if (actionStripVlan != null) { |
| 618 | if (actionStripVlan.stripVlan() == true) { |
| 619 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 620 | openFlowActions.add(ofa); |
| 621 | actionsLen += ofa.getLength(); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (actionSetEthernetSrcAddr != null) { |
| 626 | OFActionDataLayerSource ofa = new OFActionDataLayerSource( |
| 627 | actionSetEthernetSrcAddr.addr().toBytes()); |
| 628 | openFlowActions.add(ofa); |
| 629 | actionsLen += ofa.getLength(); |
| 630 | } |
| 631 | |
| 632 | if (actionSetEthernetDstAddr != null) { |
| 633 | OFActionDataLayerDestination ofa = new OFActionDataLayerDestination( |
| 634 | actionSetEthernetDstAddr.addr().toBytes()); |
| 635 | openFlowActions.add(ofa); |
| 636 | actionsLen += ofa.getLength(); |
| 637 | } |
| 638 | |
| 639 | if (actionSetIPv4SrcAddr != null) { |
| 640 | OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource( |
| 641 | actionSetIPv4SrcAddr.addr().value()); |
| 642 | openFlowActions.add(ofa); |
| 643 | actionsLen += ofa.getLength(); |
| 644 | } |
| 645 | |
| 646 | if (actionSetIPv4DstAddr != null) { |
| 647 | OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination( |
| 648 | actionSetIPv4DstAddr.addr().value()); |
| 649 | openFlowActions.add(ofa); |
| 650 | actionsLen += ofa.getLength(); |
| 651 | } |
| 652 | |
| 653 | if (actionSetIpToS != null) { |
| 654 | OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService( |
| 655 | actionSetIpToS.ipToS()); |
| 656 | openFlowActions.add(ofa); |
| 657 | actionsLen += ofa.getLength(); |
| 658 | } |
| 659 | |
| 660 | if (actionSetTcpUdpSrcPort != null) { |
| 661 | OFActionTransportLayerSource ofa = new OFActionTransportLayerSource( |
| 662 | actionSetTcpUdpSrcPort.port()); |
| 663 | openFlowActions.add(ofa); |
| 664 | actionsLen += ofa.getLength(); |
| 665 | } |
| 666 | |
| 667 | if (actionSetTcpUdpDstPort != null) { |
| 668 | OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination( |
| 669 | actionSetTcpUdpDstPort.port()); |
| 670 | openFlowActions.add(ofa); |
| 671 | actionsLen += ofa.getLength(); |
| 672 | } |
| 673 | |
| 674 | if (actionEnqueue != null) { |
| 675 | OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port() |
| 676 | .value(), actionEnqueue.queueId()); |
| 677 | openFlowActions.add(ofa); |
| 678 | actionsLen += ofa.getLength(); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | fm.setIdleTimeout(FLOWMOD_DEFAULT_IDLE_TIMEOUT) |
| 683 | .setHardTimeout(FLOWMOD_DEFAULT_HARD_TIMEOUT) |
| 684 | .setPriority(PRIORITY_DEFAULT) |
| 685 | .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie) |
| 686 | .setCommand(flowModCommand).setMatch(match) |
| 687 | .setActions(openFlowActions) |
| 688 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 689 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 690 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) |
| 691 | || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 692 | if (actionOutputPort != null) |
| 693 | fm.setOutPort(actionOutputPort); |
| 694 | } |
| 695 | |
| 696 | // |
| 697 | // TODO: Set the following flag |
| 698 | // fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 699 | // See method ForwardingBase::pushRoute() |
| 700 | // |
| 701 | |
| 702 | // |
| 703 | // Write the message to the switch |
| 704 | // |
Pavlin Radoslavov | f067890 | 2013-12-03 15:06:56 -0800 | [diff] [blame] | 705 | log.debug("Installing flow entry " |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 706 | + flowEntry.flowEntryUserState() + " into switch DPID: " |
| 707 | + sw.getStringId() + " flowEntryId: " |
| 708 | + flowEntry.flowEntryId().toString() + " srcMac: " |
| 709 | + matchSrcMac + " dstMac: " + matchDstMac + " inPort: " |
| 710 | + matchInPort + " outPort: " + actionOutputPort); |
| 711 | |
| 712 | // |
| 713 | // TODO: We should use the OpenFlow Barrier mechanism |
| 714 | // to check for errors, and update the SwitchState |
| 715 | // for a flow entry after the Barrier message is |
| 716 | // is received. |
| 717 | // |
| 718 | // TODO: The FlowEntry Object in Titan should be set |
| 719 | // to FE_SWITCH_UPDATED. |
| 720 | // |
| 721 | |
| 722 | return add(sw,fm); |
| 723 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 724 | |
| 725 | @Override |
| 726 | public OFBarrierReply barrier(IOFSwitch sw) { |
| 727 | OFMessageFuture<OFBarrierReply> future = barrierAsync(sw); |
| 728 | if (future == null) { |
| 729 | return null; |
| 730 | } |
| 731 | |
| 732 | try { |
| 733 | return future.get(); |
| 734 | } catch (InterruptedException e) { |
| 735 | e.printStackTrace(); |
| 736 | log.error("InterruptedException: {}", e); |
| 737 | return null; |
| 738 | } catch (ExecutionException e) { |
| 739 | e.printStackTrace(); |
| 740 | log.error("ExecutionException: {}", e); |
| 741 | return null; |
| 742 | } |
| 743 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 744 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 745 | @Override |
| 746 | public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) { |
| 747 | // TODO creation of message and future should be moved to OFSwitchImpl |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 748 | |
| 749 | if (sw == null) { |
| 750 | return null; |
| 751 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 752 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 753 | OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 754 | msg.setXid(sw.getNextTransactionId()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 755 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 756 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 757 | synchronized (barrierFutures) { |
| 758 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 759 | if (map == null) { |
| 760 | map = new HashMap<Integer,OFBarrierReplyFuture>(); |
| 761 | barrierFutures.put(sw.getId(), map); |
| 762 | } |
| 763 | map.put(msg.getXid(), future); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 766 | add(sw, msg); |
| 767 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 768 | return future; |
| 769 | } |
| 770 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 771 | /** |
| 772 | * Get a queue attached to a switch. |
| 773 | * @param sw Switch object |
| 774 | * @return Queue object |
| 775 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 776 | protected SwitchQueue getQueue(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 777 | if (sw == null) { |
| 778 | return null; |
| 779 | } |
| 780 | |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 781 | return getProcess(sw).queues.get(sw); |
| 782 | } |
| 783 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 784 | /** |
| 785 | * Get a hash value correspondent to a switch. |
| 786 | * @param sw Switch object |
| 787 | * @return Hash value |
| 788 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 789 | protected long getHash(IOFSwitch sw) { |
| 790 | // This code assumes DPID is sequentially assigned. |
| 791 | // TODO consider equalization algorithm |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 792 | return sw.getId() % number_thread; |
| 793 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * Get a Thread object which processes the queue attached to a switch. |
| 797 | * @param sw Switch object |
| 798 | * @return Thread object |
| 799 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 800 | protected FlowPusherThread getProcess(IOFSwitch sw) { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 801 | long hash = getHash(sw); |
| 802 | |
| 803 | return threadMap.get(hash); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 804 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 805 | |
| 806 | @Override |
| 807 | public String getName() { |
| 808 | return "flowpusher"; |
| 809 | } |
| 810 | |
| 811 | @Override |
| 812 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | @Override |
| 817 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | @Override |
| 822 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 823 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 824 | if (map == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 825 | log.debug("null map for {} : {}", sw.getId(), barrierFutures); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 826 | return Command.CONTINUE; |
| 827 | } |
| 828 | |
| 829 | OFBarrierReplyFuture future = map.get(msg.getXid()); |
| 830 | if (future == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 831 | log.debug("null future for {} : {}", msg.getXid(), map); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 832 | return Command.CONTINUE; |
| 833 | } |
| 834 | |
| 835 | log.debug("Received BARRIER_REPLY : {}", msg); |
| 836 | future.deliverFuture(sw, msg); |
| 837 | |
| 838 | return Command.CONTINUE; |
| 839 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 840 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 841 | } |