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