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; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 6 | import java.util.Collection; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 7 | import java.util.EnumSet; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 8 | import java.util.HashMap; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 9 | import java.util.HashSet; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 10 | import java.util.LinkedList; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 11 | import java.util.List; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 12 | import java.util.Map; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 13 | import java.util.Queue; |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 14 | import java.util.Set; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ConcurrentHashMap; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 16 | import java.util.concurrent.ExecutionException; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 17 | import java.util.concurrent.locks.Condition; |
| 18 | import java.util.concurrent.locks.Lock; |
| 19 | import java.util.concurrent.locks.ReentrantLock; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 20 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 21 | import org.openflow.protocol.*; |
| 22 | import org.openflow.protocol.action.*; |
| 23 | import org.openflow.protocol.factory.BasicFactory; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 26 | |
| 27 | import net.floodlightcontroller.core.FloodlightContext; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 28 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 29 | import net.floodlightcontroller.core.IOFMessageListener; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 30 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 31 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 32 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 33 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 34 | import net.floodlightcontroller.util.MACAddress; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 35 | import net.floodlightcontroller.util.OFMessageDamper; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 37 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.*; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 41 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 42 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 43 | import net.onrc.onos.ofcontroller.util.Pair; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 44 | import net.onrc.onos.ofcontroller.util.Port; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 47 | * FlowPusher is a implementation of FlowPusherService. |
| 48 | * FlowPusher assigns one message queue instance for each one switch. |
| 49 | * Number of message processing threads is configurable by constructor, and |
| 50 | * one thread can handle multiple message queues. Each queue will be assigned to |
| 51 | * a thread according to hash function defined by getHash(). |
| 52 | * Each processing thread reads messages from queues and sends it to switches |
| 53 | * in round-robin. Processing thread also calculates rate of sending to suppress |
| 54 | * excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 55 | * @author Naoki Shiota |
| 56 | * |
| 57 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 58 | public class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 59 | private final static Logger log = LoggerFactory.getLogger(FlowPusher.class); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 60 | protected static final int DEFAULT_NUMBER_THREAD = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 61 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 62 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 63 | // The local copy should go away! |
| 64 | // |
| 65 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 66 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 67 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 68 | // Number of messages sent to switch at once |
| 69 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 70 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 71 | private static class SwitchQueueEntry { |
| 72 | OFMessage msg; |
| 73 | |
| 74 | public SwitchQueueEntry(OFMessage msg) { |
| 75 | this.msg = msg; |
| 76 | } |
| 77 | |
| 78 | public OFMessage getOFMessage() { |
| 79 | return msg; |
| 80 | } |
| 81 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 82 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 83 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 84 | * SwitchQueue represents message queue attached to a switch. |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 85 | * This consists of queue itself and variables used for limiting sending rate. |
| 86 | * @author Naoki Shiota |
| 87 | * |
| 88 | */ |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 89 | private class SwitchQueue { |
| 90 | List<Queue<SwitchQueueEntry>> raw_queues; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 91 | QueueState state; |
| 92 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 93 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 94 | long max_rate = 0; // 0 indicates no limitation |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 95 | long last_sent_time = 0; |
| 96 | long last_sent_size = 0; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 97 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 98 | // "To be deleted" flag |
| 99 | boolean toBeDeleted = false; |
| 100 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 101 | SwitchQueue() { |
| 102 | raw_queues = new ArrayList<Queue<SwitchQueueEntry>>( |
| 103 | MsgPriority.values().length); |
| 104 | for (int i = 0; i < MsgPriority.values().length; ++i) { |
| 105 | raw_queues.add(i, new ArrayDeque<SwitchQueueEntry>()); |
| 106 | } |
| 107 | |
| 108 | state = QueueState.READY; |
| 109 | } |
| 110 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Check if sending rate is within the rate |
| 113 | * @param current Current time |
| 114 | * @return true if within the rate |
| 115 | */ |
| 116 | boolean isSendable(long current) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 117 | if (max_rate == 0) { |
| 118 | // no limitation |
| 119 | return true; |
| 120 | } |
| 121 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 122 | if (current == last_sent_time) { |
| 123 | return false; |
| 124 | } |
| 125 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 126 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 127 | long rate = last_sent_size / (current - last_sent_time); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 128 | return (rate < max_rate); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Log time and size of last sent data. |
| 133 | * @param current Time to be sent. |
| 134 | * @param size Size of sent data (in bytes). |
| 135 | */ |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 136 | void logSentData(long current, long size) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 137 | last_sent_time = current; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 138 | last_sent_size = size; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 139 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 140 | |
| 141 | boolean add(SwitchQueueEntry entry, MsgPriority priority) { |
| 142 | Queue<SwitchQueueEntry> queue = getQueue(priority); |
| 143 | if (queue == null) { |
| 144 | log.error("Unexpected priority : ", priority); |
| 145 | return false; |
| 146 | } |
| 147 | return queue.add(entry); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Poll single appropriate entry object according to QueueState. |
| 152 | * @return Entry object. |
| 153 | */ |
| 154 | SwitchQueueEntry poll() { |
| 155 | switch (state) { |
| 156 | case READY: |
| 157 | { |
| 158 | for (int i = 0; i < raw_queues.size(); ++i) { |
| 159 | SwitchQueueEntry entry = raw_queues.get(i).poll(); |
| 160 | if (entry != null) { |
| 161 | return entry; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return null; |
| 166 | } |
| 167 | case SUSPENDED: |
| 168 | { |
| 169 | // Only polling from high priority queue |
| 170 | SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll(); |
| 171 | return entry; |
| 172 | } |
| 173 | default: |
| 174 | log.error("Unexpected QueueState : ", state); |
| 175 | return null; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check if this object has any messages in the queues to be sent |
| 181 | * @return True if there are some messages to be sent. |
| 182 | */ |
| 183 | boolean hasMessageToSend() { |
| 184 | switch (state) { |
| 185 | case READY: |
| 186 | for (Queue<SwitchQueueEntry> queue : raw_queues) { |
| 187 | if (! queue.isEmpty()) { |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | break; |
| 192 | case SUSPENDED: |
| 193 | // Only checking high priority queue |
| 194 | return (! getQueue(MsgPriority.HIGH).isEmpty()); |
| 195 | default: |
| 196 | log.error("Unexpected QueueState : ", state); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | Queue<SwitchQueueEntry> getQueue(MsgPriority priority) { |
| 204 | return raw_queues.get(priority.ordinal()); |
| 205 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * BarrierInfo holds information to specify barrier message sent to switch. |
| 210 | * @author Naoki |
| 211 | */ |
| 212 | private static class BarrierInfo { |
| 213 | final long dpid; |
| 214 | final int xid; |
| 215 | |
| 216 | static BarrierInfo create(IOFSwitch sw, OFBarrierRequest req) { |
| 217 | return new BarrierInfo(sw.getId(), req.getXid()); |
| 218 | } |
| 219 | |
| 220 | static BarrierInfo create(IOFSwitch sw, OFBarrierReply rpy) { |
| 221 | return new BarrierInfo(sw.getId(), rpy.getXid()); |
| 222 | } |
| 223 | |
| 224 | private BarrierInfo(long dpid, int xid) { |
| 225 | this.dpid = dpid; |
| 226 | this.xid = xid; |
| 227 | } |
| 228 | |
| 229 | // Auto generated code by Eclipse |
| 230 | @Override |
| 231 | public int hashCode() { |
| 232 | final int prime = 31; |
| 233 | int result = 1; |
| 234 | result = prime * result + (int) (dpid ^ (dpid >>> 32)); |
| 235 | result = prime * result + xid; |
| 236 | return result; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean equals(Object obj) { |
| 241 | if (this == obj) |
| 242 | return true; |
| 243 | if (obj == null) |
| 244 | return false; |
| 245 | if (getClass() != obj.getClass()) |
| 246 | return false; |
| 247 | |
| 248 | BarrierInfo other = (BarrierInfo) obj; |
| 249 | return (this.dpid == other.dpid) && (this.xid == other.xid); |
| 250 | } |
| 251 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 252 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 253 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 254 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 255 | private OFMessageDamper messageDamper = null; |
| 256 | private IThreadPoolService threadPool = null; |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 257 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 258 | private FloodlightContext context = null; |
| 259 | private BasicFactory factory = null; |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 260 | |
| 261 | // Map of threads versus dpid |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 262 | private Map<Long, FlowPusherThread> threadMap = null; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 263 | // Map from (DPID and transaction ID) to Future objects. |
| 264 | private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures |
| 265 | = new ConcurrentHashMap<BarrierInfo,OFBarrierReplyFuture>(); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 266 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 267 | private int number_thread; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 268 | |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 269 | /** |
| 270 | * Main thread that reads messages from queues and sends them to switches. |
| 271 | * @author Naoki Shiota |
| 272 | * |
| 273 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 274 | private class FlowPusherThread extends Thread { |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 275 | private Map<IOFSwitch,SwitchQueue> assignedQueues |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 276 | = new ConcurrentHashMap<IOFSwitch,SwitchQueue>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 277 | |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 278 | final Lock queuingLock = new ReentrantLock(); |
| 279 | final Condition messagePushed = queuingLock.newCondition(); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 280 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public void run() { |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 283 | this.setName("FlowPusherThread " + this.getId() ); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 284 | while (true) { |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 285 | while (! queuesHasMessageToSend()) { |
| 286 | queuingLock.lock(); |
| 287 | |
| 288 | try { |
| 289 | // wait for message pushed to queue |
| 290 | messagePushed.await(); |
| 291 | } catch (InterruptedException e) { |
| 292 | // Interrupted to be shut down (not an error) |
| 293 | log.debug("FlowPusherThread is interrupted"); |
| 294 | return; |
| 295 | } finally { |
| 296 | queuingLock.unlock(); |
| 297 | } |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 300 | // for safety of concurrent access, copy set of key objects |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 301 | Set<IOFSwitch> keys = new HashSet<IOFSwitch>(assignedQueues.size()); |
| 302 | for (IOFSwitch sw : assignedQueues.keySet()) { |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 303 | keys.add(sw); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 306 | for (IOFSwitch sw : keys) { |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 307 | SwitchQueue queue = assignedQueues.get(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 308 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 309 | if (sw == null || queue == null) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 310 | continue; |
| 311 | } |
| 312 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 313 | synchronized (queue) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 314 | processQueue(sw, queue, MAX_MESSAGE_SEND); |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 315 | if (queue.toBeDeleted && ! queue.hasMessageToSend()) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 316 | // remove queue if flagged to be. |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 317 | assignedQueues.remove(sw); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Read messages from queue and send them to the switch. |
| 326 | * If number of messages excess the limit, stop sending messages. |
| 327 | * @param sw Switch to which messages will be sent. |
| 328 | * @param queue Queue of messages. |
| 329 | * @param max_msg Limitation of number of messages to be sent. If set to 0, |
| 330 | * all messages in queue will be sent. |
| 331 | */ |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 332 | private void processQueue(IOFSwitch sw, SwitchQueue queue, int max_msg) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 333 | // check sending rate and determine it to be sent or not |
| 334 | long current_time = System.currentTimeMillis(); |
| 335 | long size = 0; |
| 336 | |
| 337 | if (queue.isSendable(current_time)) { |
| 338 | int i = 0; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 339 | while (queue.hasMessageToSend()) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 340 | // Number of messages excess the limit |
| 341 | if (0 < max_msg && max_msg <= i) { |
| 342 | break; |
| 343 | } |
| 344 | ++i; |
| 345 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 346 | SwitchQueueEntry queueEntry; |
| 347 | synchronized (queue) { |
| 348 | queueEntry = queue.poll(); |
| 349 | } |
| 350 | |
| 351 | OFMessage msg = queueEntry.getOFMessage(); |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 352 | try { |
| 353 | messageDamper.write(sw, msg, context); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 354 | if (log.isTraceEnabled()) { |
| 355 | log.trace("Pusher sends message : {}", msg); |
| 356 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 357 | size += msg.getLength(); |
| 358 | } catch (IOException e) { |
| 359 | e.printStackTrace(); |
| 360 | log.error("Exception in sending message ({}) : {}", msg, e); |
| 361 | } |
| 362 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 363 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 364 | sw.flush(); |
| 365 | queue.logSentData(current_time, size); |
| 366 | } |
| 367 | } |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 368 | |
| 369 | private boolean queuesHasMessageToSend() { |
| 370 | for (SwitchQueue queue : assignedQueues.values()) { |
| 371 | if (queue.hasMessageToSend()) { |
| 372 | return true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | private void notifyMessagePushed() { |
| 380 | queuingLock.lock(); |
| 381 | try { |
| 382 | messagePushed.signal(); |
| 383 | } finally { |
| 384 | queuingLock.unlock(); |
| 385 | } |
| 386 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Initialize object with one thread. |
| 391 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 392 | public FlowPusher() { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 393 | number_thread = DEFAULT_NUMBER_THREAD; |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 396 | /** |
| 397 | * Initialize object with threads of given number. |
| 398 | * @param number_thread Number of threads to handle messages. |
| 399 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 400 | public FlowPusher(int number_thread) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 401 | if (number_thread > 0) { |
| 402 | this.number_thread = number_thread; |
| 403 | } else { |
| 404 | this.number_thread = DEFAULT_NUMBER_THREAD; |
| 405 | } |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Set parameters needed for sending messages. |
| 410 | * @param context FloodlightContext used for sending messages. |
| 411 | * If null, FlowPusher uses default context. |
| 412 | * @param modContext FloodlightModuleContext used for acquiring |
| 413 | * ThreadPoolService and registering MessageListener. |
| 414 | * @param factory Factory object to create OFMessage objects. |
| 415 | * @param damper Message damper used for sending messages. |
| 416 | * If null, FlowPusher creates its own damper object. |
| 417 | */ |
| 418 | public void init(FloodlightContext context, |
| 419 | FloodlightModuleContext modContext, |
| 420 | BasicFactory factory, |
| 421 | OFMessageDamper damper) { |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 422 | this.context = context; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 423 | this.factory = factory; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 424 | this.threadPool = modContext.getServiceImpl(IThreadPoolService.class); |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 425 | IFloodlightProviderService flservice |
| 426 | = modContext.getServiceImpl(IFloodlightProviderService.class); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 427 | flservice.addOFMessageListener(OFType.BARRIER_REPLY, this); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 428 | |
| 429 | if (damper != null) { |
| 430 | messageDamper = damper; |
| 431 | } else { |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 432 | // use default values |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 433 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 434 | EnumSet.of(OFType.FLOW_MOD), |
| 435 | OFMESSAGE_DAMPER_TIMEOUT); |
| 436 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 437 | } |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 438 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 439 | /** |
| 440 | * Begin processing queue. |
| 441 | */ |
| 442 | public void start() { |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 443 | if (factory == null) { |
| 444 | log.error("FlowPusher not yet initialized."); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 448 | threadMap = new HashMap<Long,FlowPusherThread>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 449 | for (long i = 0; i < number_thread; ++i) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 450 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 451 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 452 | threadMap.put(i, thread); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 453 | thread.start(); |
| 454 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 457 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 458 | public boolean suspend(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 459 | SwitchQueue queue = getQueue(sw); |
| 460 | |
| 461 | if (queue == null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 462 | // create queue in case suspend is called before first message addition |
| 463 | queue = createQueueImpl(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | synchronized (queue) { |
| 467 | if (queue.state == QueueState.READY) { |
| 468 | queue.state = QueueState.SUSPENDED; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 469 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 470 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 471 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 475 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 476 | public boolean resume(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 477 | SwitchQueue queue = getQueue(sw); |
| 478 | |
| 479 | if (queue == null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 480 | log.error("No queue is attached to DPID : {}", sw.getId()); |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 481 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | synchronized (queue) { |
| 485 | if (queue.state == QueueState.SUSPENDED) { |
| 486 | queue.state = QueueState.READY; |
Naoki Shiota | 0aa947e | 2013-11-27 14:47:35 -0800 | [diff] [blame] | 487 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 488 | // Free the latch if queue has any messages |
| 489 | FlowPusherThread thread = getProcessingThread(sw); |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 490 | if (queue.hasMessageToSend()) { |
| 491 | thread.notifyMessagePushed(); |
Naoki Shiota | 0aa947e | 2013-11-27 14:47:35 -0800 | [diff] [blame] | 492 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 493 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 494 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 495 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 498 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 499 | @Override |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 500 | public QueueState getState(IOFSwitch sw) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 501 | SwitchQueue queue = getQueue(sw); |
| 502 | |
| 503 | if (queue == null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 504 | return QueueState.UNKNOWN; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 507 | return queue.state; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 508 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 509 | |
| 510 | /** |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 511 | * Stop processing queue and exit thread. |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 512 | */ |
| 513 | public void stop() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 514 | if (threadMap == null) { |
| 515 | return; |
| 516 | } |
| 517 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 518 | for (FlowPusherThread t : threadMap.values()) { |
| 519 | t.interrupt(); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 523 | @Override |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 524 | public void setRate(IOFSwitch sw, long rate) { |
| 525 | SwitchQueue queue = getQueue(sw); |
| 526 | if (queue == null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 527 | queue = createQueueImpl(sw); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (rate > 0) { |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 531 | log.debug("rate for {} is set to {}", sw.getId(), rate); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 532 | synchronized (queue) { |
| 533 | queue.max_rate = rate; |
| 534 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 535 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 536 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 537 | |
| 538 | @Override |
| 539 | public boolean createQueue(IOFSwitch sw) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 540 | SwitchQueue queue = createQueueImpl(sw); |
| 541 | |
| 542 | return (queue != null); |
| 543 | } |
| 544 | |
| 545 | protected SwitchQueue createQueueImpl(IOFSwitch sw) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 546 | SwitchQueue queue = getQueue(sw); |
| 547 | if (queue != null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 548 | return queue; |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 551 | FlowPusherThread proc = getProcessingThread(sw); |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 552 | queue = new SwitchQueue(); |
| 553 | queue.state = QueueState.READY; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 554 | proc.assignedQueues.put(sw, queue); |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 555 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 556 | return queue; |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public boolean deleteQueue(IOFSwitch sw) { |
| 561 | return deleteQueue(sw, false); |
| 562 | } |
| 563 | |
| 564 | @Override |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 565 | public boolean deleteQueue(IOFSwitch sw, boolean forceStop) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 566 | FlowPusherThread proc = getProcessingThread(sw); |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 567 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 568 | if (forceStop) { |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 569 | SwitchQueue queue = proc.assignedQueues.remove(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 570 | if (queue == null) { |
| 571 | return false; |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 572 | } |
| 573 | return true; |
| 574 | } else { |
| 575 | SwitchQueue queue = getQueue(sw); |
| 576 | if (queue == null) { |
| 577 | return false; |
| 578 | } |
| 579 | synchronized (queue) { |
| 580 | queue.toBeDeleted = true; |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 585 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 586 | @Override |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 587 | public boolean add(IOFSwitch sw, OFMessage msg) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 588 | return add(sw, msg, MsgPriority.NORMAL); |
| 589 | } |
| 590 | |
| 591 | @Override |
| 592 | public boolean add(IOFSwitch sw, OFMessage msg, MsgPriority priority) { |
| 593 | return addMessageImpl(sw, msg, priority); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 594 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 595 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 596 | @Override |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 597 | public void pushFlowEntries( |
| 598 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 599 | pushFlowEntries(entries, MsgPriority.NORMAL); |
| 600 | } |
| 601 | |
| 602 | @Override |
| 603 | public void pushFlowEntries( |
| 604 | Collection<Pair<IOFSwitch, FlowEntry>> entries, MsgPriority priority) { |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 605 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 606 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 607 | add(entry.first, entry.second, priority); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 608 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | @Override |
| 612 | public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 613 | pushFlowEntry(sw, flowEntry, MsgPriority.NORMAL); |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) { |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 618 | Collection<Pair<IOFSwitch, FlowEntry>> entries = |
| 619 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 620 | |
| 621 | entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry)); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 622 | pushFlowEntries(entries, priority); |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 623 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 624 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 625 | /** |
| 626 | * Create a message from FlowEntry and add it to the queue of the switch. |
| 627 | * @param sw Switch to which message is pushed. |
| 628 | * @param flowEntry FlowEntry object used for creating message. |
| 629 | * @return true if message is successfully added to a queue. |
| 630 | */ |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 631 | private boolean add(IOFSwitch sw, FlowEntry flowEntry, MsgPriority priority) { |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 632 | // |
| 633 | // Create the OpenFlow Flow Modification Entry to push |
| 634 | // |
| 635 | OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD); |
| 636 | long cookie = flowEntry.flowEntryId().value(); |
| 637 | |
| 638 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 639 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 640 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 641 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 642 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 643 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 644 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 645 | } else { |
| 646 | // Unknown user state. Ignore the entry |
| 647 | log.debug( |
| 648 | "Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 649 | flowEntry.flowEntryId(), |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 650 | flowEntry.flowEntryUserState()); |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | // |
| 655 | // Fetch the match conditions. |
| 656 | // |
| 657 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 658 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 659 | // condition set. |
| 660 | // |
| 661 | OFMatch match = new OFMatch(); |
| 662 | match.setWildcards(OFMatch.OFPFW_ALL); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 663 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 664 | |
| 665 | // Match the Incoming Port |
| 666 | Port matchInPort = flowEntryMatch.inPort(); |
| 667 | if (matchInPort != null) { |
| 668 | match.setInputPort(matchInPort.value()); |
| 669 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 670 | } |
| 671 | |
| 672 | // Match the Source MAC address |
| 673 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 674 | if (matchSrcMac != null) { |
| 675 | match.setDataLayerSource(matchSrcMac.toString()); |
| 676 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 677 | } |
| 678 | |
| 679 | // Match the Destination MAC address |
| 680 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 681 | if (matchDstMac != null) { |
| 682 | match.setDataLayerDestination(matchDstMac.toString()); |
| 683 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 684 | } |
| 685 | |
| 686 | // Match the Ethernet Frame Type |
| 687 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 688 | if (matchEthernetFrameType != null) { |
| 689 | match.setDataLayerType(matchEthernetFrameType); |
| 690 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 691 | } |
| 692 | |
| 693 | // Match the VLAN ID |
| 694 | Short matchVlanId = flowEntryMatch.vlanId(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 695 | if (matchVlanId != null) { |
| 696 | match.setDataLayerVirtualLan(matchVlanId); |
| 697 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 698 | } |
| 699 | |
| 700 | // Match the VLAN priority |
| 701 | Byte matchVlanPriority = flowEntryMatch.vlanPriority(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 702 | if (matchVlanPriority != null) { |
| 703 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 704 | match.setWildcards(match.getWildcards() |
| 705 | & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 706 | } |
| 707 | |
| 708 | // Match the Source IPv4 Network prefix |
| 709 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 710 | if (matchSrcIPv4Net != null) { |
| 711 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 712 | } |
| 713 | |
| 714 | // Natch the Destination IPv4 Network prefix |
| 715 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 716 | if (matchDstIPv4Net != null) { |
| 717 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 718 | } |
| 719 | |
| 720 | // Match the IP protocol |
| 721 | Byte matchIpProto = flowEntryMatch.ipProto(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 722 | if (matchIpProto != null) { |
| 723 | match.setNetworkProtocol(matchIpProto); |
| 724 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
| 725 | } |
| 726 | |
| 727 | // Match the IP ToS (DSCP field, 6 bits) |
| 728 | Byte matchIpToS = flowEntryMatch.ipToS(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 729 | if (matchIpToS != null) { |
| 730 | match.setNetworkTypeOfService(matchIpToS); |
| 731 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 732 | } |
| 733 | |
| 734 | // Match the Source TCP/UDP port |
| 735 | Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 736 | if (matchSrcTcpUdpPort != null) { |
| 737 | match.setTransportSource(matchSrcTcpUdpPort); |
| 738 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 739 | } |
| 740 | |
| 741 | // Match the Destination TCP/UDP port |
| 742 | Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 743 | if (matchDstTcpUdpPort != null) { |
| 744 | match.setTransportDestination(matchDstTcpUdpPort); |
| 745 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 746 | } |
| 747 | |
| 748 | // |
| 749 | // Fetch the actions |
| 750 | // |
| 751 | Short actionOutputPort = null; |
| 752 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 753 | int actionsLen = 0; |
| 754 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 755 | // |
| 756 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 757 | ActionOutput actionOutput = action.actionOutput(); |
| 758 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 759 | ActionSetVlanPriority actionSetVlanPriority = action |
| 760 | .actionSetVlanPriority(); |
| 761 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 762 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action |
| 763 | .actionSetEthernetSrcAddr(); |
| 764 | ActionSetEthernetAddr actionSetEthernetDstAddr = action |
| 765 | .actionSetEthernetDstAddr(); |
| 766 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action |
| 767 | .actionSetIPv4SrcAddr(); |
| 768 | ActionSetIPv4Addr actionSetIPv4DstAddr = action |
| 769 | .actionSetIPv4DstAddr(); |
| 770 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 771 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action |
| 772 | .actionSetTcpUdpSrcPort(); |
| 773 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action |
| 774 | .actionSetTcpUdpDstPort(); |
| 775 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 776 | |
| 777 | if (actionOutput != null) { |
| 778 | actionOutputPort = actionOutput.port().value(); |
| 779 | // XXX: The max length is hard-coded for now |
| 780 | OFActionOutput ofa = new OFActionOutput(actionOutput.port() |
| 781 | .value(), (short) 0xffff); |
| 782 | openFlowActions.add(ofa); |
| 783 | actionsLen += ofa.getLength(); |
| 784 | } |
| 785 | |
| 786 | if (actionSetVlanId != null) { |
| 787 | OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier( |
| 788 | actionSetVlanId.vlanId()); |
| 789 | openFlowActions.add(ofa); |
| 790 | actionsLen += ofa.getLength(); |
| 791 | } |
| 792 | |
| 793 | if (actionSetVlanPriority != null) { |
| 794 | OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint( |
| 795 | actionSetVlanPriority.vlanPriority()); |
| 796 | openFlowActions.add(ofa); |
| 797 | actionsLen += ofa.getLength(); |
| 798 | } |
| 799 | |
| 800 | if (actionStripVlan != null) { |
| 801 | if (actionStripVlan.stripVlan() == true) { |
| 802 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 803 | openFlowActions.add(ofa); |
| 804 | actionsLen += ofa.getLength(); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if (actionSetEthernetSrcAddr != null) { |
| 809 | OFActionDataLayerSource ofa = new OFActionDataLayerSource( |
| 810 | actionSetEthernetSrcAddr.addr().toBytes()); |
| 811 | openFlowActions.add(ofa); |
| 812 | actionsLen += ofa.getLength(); |
| 813 | } |
| 814 | |
| 815 | if (actionSetEthernetDstAddr != null) { |
| 816 | OFActionDataLayerDestination ofa = new OFActionDataLayerDestination( |
| 817 | actionSetEthernetDstAddr.addr().toBytes()); |
| 818 | openFlowActions.add(ofa); |
| 819 | actionsLen += ofa.getLength(); |
| 820 | } |
| 821 | |
| 822 | if (actionSetIPv4SrcAddr != null) { |
| 823 | OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource( |
| 824 | actionSetIPv4SrcAddr.addr().value()); |
| 825 | openFlowActions.add(ofa); |
| 826 | actionsLen += ofa.getLength(); |
| 827 | } |
| 828 | |
| 829 | if (actionSetIPv4DstAddr != null) { |
| 830 | OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination( |
| 831 | actionSetIPv4DstAddr.addr().value()); |
| 832 | openFlowActions.add(ofa); |
| 833 | actionsLen += ofa.getLength(); |
| 834 | } |
| 835 | |
| 836 | if (actionSetIpToS != null) { |
| 837 | OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService( |
| 838 | actionSetIpToS.ipToS()); |
| 839 | openFlowActions.add(ofa); |
| 840 | actionsLen += ofa.getLength(); |
| 841 | } |
| 842 | |
| 843 | if (actionSetTcpUdpSrcPort != null) { |
| 844 | OFActionTransportLayerSource ofa = new OFActionTransportLayerSource( |
| 845 | actionSetTcpUdpSrcPort.port()); |
| 846 | openFlowActions.add(ofa); |
| 847 | actionsLen += ofa.getLength(); |
| 848 | } |
| 849 | |
| 850 | if (actionSetTcpUdpDstPort != null) { |
| 851 | OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination( |
| 852 | actionSetTcpUdpDstPort.port()); |
| 853 | openFlowActions.add(ofa); |
| 854 | actionsLen += ofa.getLength(); |
| 855 | } |
| 856 | |
| 857 | if (actionEnqueue != null) { |
| 858 | OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port() |
| 859 | .value(), actionEnqueue.queueId()); |
| 860 | openFlowActions.add(ofa); |
| 861 | actionsLen += ofa.getLength(); |
| 862 | } |
| 863 | } |
| 864 | |
Pavlin Radoslavov | 1fe06a2 | 2013-12-10 14:12:23 -0800 | [diff] [blame] | 865 | fm.setIdleTimeout((short)flowEntry.idleTimeout()) |
| 866 | .setHardTimeout((short)flowEntry.hardTimeout()) |
Pavlin Radoslavov | afbf103 | 2014-02-04 10:37:52 -0800 | [diff] [blame] | 867 | .setPriority((short)flowEntry.priority()) |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 868 | .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie) |
| 869 | .setCommand(flowModCommand).setMatch(match) |
| 870 | .setActions(openFlowActions) |
| 871 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 872 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 873 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) |
| 874 | || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 875 | if (actionOutputPort != null) |
| 876 | fm.setOutPort(actionOutputPort); |
| 877 | } |
| 878 | |
| 879 | // |
Pavlin Radoslavov | b95379c | 2013-12-13 11:09:20 -0800 | [diff] [blame] | 880 | // Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not |
| 881 | // permanent. |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 882 | // |
Pavlin Radoslavov | b95379c | 2013-12-13 11:09:20 -0800 | [diff] [blame] | 883 | if ((flowEntry.idleTimeout() != 0) || |
| 884 | (flowEntry.hardTimeout() != 0)) { |
| 885 | fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 886 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 887 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 888 | if (log.isTraceEnabled()) { |
| 889 | log.trace("Installing flow entry {} into switch DPID: {} flowEntryId: {} srcMac: {} dstMac: {} inPort: {} outPort: {}" |
| 890 | , flowEntry.flowEntryUserState() |
| 891 | , sw.getStringId() |
| 892 | , flowEntry.flowEntryId() |
| 893 | , matchSrcMac |
| 894 | , matchDstMac |
| 895 | , matchInPort |
| 896 | , actionOutputPort |
| 897 | ); |
| 898 | } |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 899 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 900 | return addMessageImpl(sw, fm, priority); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Add message to queue |
| 905 | * @param sw |
| 906 | * @param msg |
| 907 | * @param flowEntryId |
| 908 | * @return |
| 909 | */ |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 910 | protected boolean addMessageImpl(IOFSwitch sw, OFMessage msg, MsgPriority priority) { |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 911 | FlowPusherThread thread = getProcessingThread(sw); |
| 912 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 913 | SwitchQueue queue = getQueue(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 914 | |
| 915 | // create queue at first addition of message |
| 916 | if (queue == null) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 917 | queue = createQueueImpl(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | SwitchQueueEntry entry = new SwitchQueueEntry(msg); |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 921 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 922 | synchronized (queue) { |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 923 | queue.add(entry,priority); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 924 | if (log.isTraceEnabled()) { |
| 925 | log.trace("Message is pushed : {}", entry.getOFMessage()); |
| 926 | } |
| 927 | } |
| 928 | |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 929 | thread.notifyMessagePushed(); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 930 | |
| 931 | return true; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 932 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 933 | |
| 934 | @Override |
| 935 | public OFBarrierReply barrier(IOFSwitch sw) { |
| 936 | OFMessageFuture<OFBarrierReply> future = barrierAsync(sw); |
| 937 | if (future == null) { |
| 938 | return null; |
| 939 | } |
| 940 | |
| 941 | try { |
| 942 | return future.get(); |
| 943 | } catch (InterruptedException e) { |
| 944 | e.printStackTrace(); |
| 945 | log.error("InterruptedException: {}", e); |
| 946 | return null; |
| 947 | } catch (ExecutionException e) { |
| 948 | e.printStackTrace(); |
| 949 | log.error("ExecutionException: {}", e); |
| 950 | return null; |
| 951 | } |
| 952 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 953 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 954 | @Override |
| 955 | public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) { |
| 956 | // TODO creation of message and future should be moved to OFSwitchImpl |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 957 | |
| 958 | if (sw == null) { |
| 959 | return null; |
| 960 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 961 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 962 | OFBarrierRequest msg = createBarrierRequest(sw); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 963 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 964 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid()); |
| 965 | barrierFutures.put(BarrierInfo.create(sw,msg), future); |
| 966 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 967 | addMessageImpl(sw, msg, MsgPriority.NORMAL); |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 968 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 969 | return future; |
| 970 | } |
| 971 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 972 | protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) { |
| 973 | OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST); |
| 974 | msg.setXid(sw.getNextTransactionId()); |
| 975 | |
| 976 | return msg; |
| 977 | } |
| 978 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 979 | /** |
| 980 | * Get a queue attached to a switch. |
| 981 | * @param sw Switch object |
| 982 | * @return Queue object |
| 983 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 984 | protected SwitchQueue getQueue(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 985 | if (sw == null) { |
| 986 | return null; |
| 987 | } |
| 988 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 989 | FlowPusherThread th = getProcessingThread(sw); |
| 990 | if (th == null) { |
| 991 | return null; |
| 992 | } |
| 993 | |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 994 | return th.assignedQueues.get(sw); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 997 | /** |
| 998 | * Get a hash value correspondent to a switch. |
| 999 | * @param sw Switch object |
| 1000 | * @return Hash value |
| 1001 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 1002 | protected long getHash(IOFSwitch sw) { |
| 1003 | // This code assumes DPID is sequentially assigned. |
| 1004 | // TODO consider equalization algorithm |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 1005 | return sw.getId() % number_thread; |
| 1006 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 1007 | |
| 1008 | /** |
| 1009 | * Get a Thread object which processes the queue attached to a switch. |
| 1010 | * @param sw Switch object |
| 1011 | * @return Thread object |
| 1012 | */ |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 1013 | protected FlowPusherThread getProcessingThread(IOFSwitch sw) { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 1014 | long hash = getHash(sw); |
| 1015 | |
| 1016 | return threadMap.get(hash); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 1017 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 1018 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 1019 | @Override |
| 1020 | public String getName() { |
| 1021 | return "flowpusher"; |
| 1022 | } |
| 1023 | |
| 1024 | @Override |
| 1025 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | @Override |
| 1035 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 1036 | if (log.isTraceEnabled()) { |
| 1037 | log.trace("Received BARRIER_REPLY from : {}", sw.getId()); |
| 1038 | } |
| 1039 | |
| 1040 | if (msg.getType() != OFType.BARRIER_REPLY) { |
| 1041 | log.error("Unexpected reply message : {}", msg.getType()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 1042 | return Command.CONTINUE; |
| 1043 | } |
| 1044 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 1045 | OFBarrierReply reply = (OFBarrierReply) msg; |
| 1046 | BarrierInfo info = BarrierInfo.create(sw,reply); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 1047 | |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 1048 | // Deliver future if exists |
| 1049 | OFBarrierReplyFuture future = barrierFutures.get(info); |
| 1050 | if (future != null) { |
| 1051 | future.deliverFuture(sw, msg); |
| 1052 | barrierFutures.remove(info); |
| 1053 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 1054 | |
| 1055 | return Command.CONTINUE; |
| 1056 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 1057 | } |