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