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