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 | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 8 | import java.util.Iterator; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 9 | import java.util.LinkedList; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 10 | import java.util.List; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 12 | import java.util.Map.Entry; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 13 | import java.util.Queue; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ConcurrentHashMap; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 15 | import java.util.concurrent.ExecutionException; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 16 | import java.util.concurrent.locks.Condition; |
| 17 | import java.util.concurrent.locks.Lock; |
| 18 | import java.util.concurrent.locks.ReentrantLock; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 19 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.FloodlightContext; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 22 | import net.floodlightcontroller.core.IOFMessageListener; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 25 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 26 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 27 | import net.floodlightcontroller.util.MACAddress; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.intent.FlowEntry; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 29 | import net.onrc.onos.core.matchaction.MatchAction; |
| 30 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
| 31 | import net.onrc.onos.core.matchaction.MatchActionOperations.Operator; |
| 32 | import net.onrc.onos.core.matchaction.action.Action; |
| 33 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 34 | import net.onrc.onos.core.matchaction.action.ModifySrcMacAction; |
| 35 | import net.onrc.onos.core.matchaction.action.OutputAction; |
| 36 | import net.onrc.onos.core.matchaction.match.Match; |
| 37 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.util.Dpid; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.util.IPv4Net; |
| 40 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 41 | |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 42 | import org.apache.commons.lang3.tuple.Pair; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.OFActionType; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 45 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 46 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 47 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 48 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 49 | import org.projectfloodlight.openflow.protocol.OFType; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 51 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 52 | import org.projectfloodlight.openflow.protocol.action.OFActions; |
| 53 | import org.projectfloodlight.openflow.protocol.match.Match.Builder; |
| 54 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 55 | import org.projectfloodlight.openflow.types.EthType; |
| 56 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 57 | import org.projectfloodlight.openflow.types.IpProtocol; |
| 58 | import org.projectfloodlight.openflow.types.MacAddress; |
| 59 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 60 | import org.projectfloodlight.openflow.types.OFPort; |
| 61 | import org.projectfloodlight.openflow.types.TransportPort; |
| 62 | import org.projectfloodlight.openflow.types.U64; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 63 | import org.slf4j.Logger; |
| 64 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 65 | |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 66 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 67 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 68 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 69 | * FlowPusher is a implementation of FlowPusherService. FlowPusher assigns one |
| 70 | * message queue instance for each one switch. Number of message processing |
| 71 | * threads is configurable by constructor, and one thread can handle multiple |
| 72 | * message queues. Each queue will be assigned to a thread according to hash |
| 73 | * function defined by getHash(). Each processing thread reads messages from |
| 74 | * queues and sends it to switches in round-robin. Processing thread also |
| 75 | * calculates rate of sending to suppress excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 76 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 77 | public final class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 78 | private static final Logger log = LoggerFactory.getLogger(FlowPusher.class); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 79 | private static final int DEFAULT_NUMBER_THREAD = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 80 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 81 | // Number of messages sent to switch at once |
| 82 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 83 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 84 | private FloodlightModuleContext context = null; |
| 85 | private IThreadPoolService threadPool = null; |
| 86 | private IFloodlightProviderService floodlightProvider = null; |
| 87 | |
| 88 | // Map of threads versus dpid |
| 89 | private Map<Long, FlowPusherThread> threadMap = null; |
| 90 | // Map from (DPID and transaction ID) to Future objects. |
| 91 | private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures = |
| 92 | new ConcurrentHashMap<BarrierInfo, OFBarrierReplyFuture>(); |
| 93 | |
| 94 | private int numberThread; |
| 95 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 96 | private static class SwitchQueueEntry { |
| 97 | OFMessage msg; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 98 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 99 | public SwitchQueueEntry(OFMessage msg) { |
| 100 | this.msg = msg; |
| 101 | } |
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 OFMessage getOFMessage() { |
| 104 | return msg; |
| 105 | } |
| 106 | } |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 107 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 108 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 109 | * SwitchQueue represents message queue attached to a switch. This consists |
| 110 | * of queue itself and variables used for limiting sending rate. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 111 | */ |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 112 | private static class SwitchQueue { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 113 | List<Queue<SwitchQueueEntry>> rawQueues; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 114 | QueueState state; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 115 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 116 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 117 | long maxRate = 0; // 0 indicates no limitation |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 118 | long lastSentTime = 0; |
| 119 | long lastSentSize = 0; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 120 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 121 | // "To be deleted" flag |
| 122 | boolean toBeDeleted = false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 123 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 124 | SwitchQueue() { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 125 | rawQueues = new ArrayList<>(MsgPriority.values().length); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 126 | for (int i = 0; i < MsgPriority.values().length; ++i) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 127 | rawQueues.add(i, new ArrayDeque<SwitchQueueEntry>()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 128 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 129 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 130 | state = QueueState.READY; |
| 131 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 132 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 133 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 134 | * Check if sending rate is within the rate. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 135 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 136 | * @param current Current time |
| 137 | * @return true if within the rate |
| 138 | */ |
| 139 | boolean isSendable(long current) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 140 | if (maxRate == 0) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 141 | // no limitation |
| 142 | return true; |
| 143 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 144 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 145 | if (current == lastSentTime) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 146 | return false; |
| 147 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 148 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 149 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 150 | long rate = lastSentSize / (current - lastSentTime); |
| 151 | return (rate < maxRate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 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 | /** |
| 155 | * Log time and size of last sent data. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 156 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 157 | * @param current Time to be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 158 | * @param size Size of sent data (in bytes). |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 159 | */ |
| 160 | void logSentData(long current, long size) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 161 | lastSentTime = current; |
| 162 | lastSentSize = size; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 163 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 164 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 165 | boolean add(SwitchQueueEntry entry, MsgPriority priority) { |
| 166 | Queue<SwitchQueueEntry> queue = getQueue(priority); |
| 167 | if (queue == null) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 168 | log.error("Unexpected priority: {}", priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | return queue.add(entry); |
| 172 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 173 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Poll single appropriate entry object according to QueueState. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 176 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 177 | * @return Entry object. |
| 178 | */ |
| 179 | SwitchQueueEntry poll() { |
| 180 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 181 | case READY: { |
| 182 | for (int i = 0; i < rawQueues.size(); ++i) { |
| 183 | SwitchQueueEntry entry = rawQueues.get(i).poll(); |
| 184 | if (entry != null) { |
| 185 | return entry; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 186 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 187 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 188 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 189 | return null; |
| 190 | } |
| 191 | case SUSPENDED: { |
| 192 | // Only polling from high priority queue |
| 193 | SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll(); |
| 194 | return entry; |
| 195 | } |
| 196 | default: |
| 197 | log.error("Unexpected QueueState: {}", state); |
| 198 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 201 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 202 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 203 | * Check if this object has any messages in the queues to be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 204 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 205 | * @return True if there are some messages to be sent. |
| 206 | */ |
| 207 | boolean hasMessageToSend() { |
| 208 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 209 | case READY: |
| 210 | for (Queue<SwitchQueueEntry> queue : rawQueues) { |
| 211 | if (!queue.isEmpty()) { |
| 212 | return true; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 213 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 214 | } |
| 215 | break; |
| 216 | case SUSPENDED: |
| 217 | // Only checking high priority queue |
| 218 | return (!getQueue(MsgPriority.HIGH).isEmpty()); |
| 219 | default: |
| 220 | log.error("Unexpected QueueState: {}", state); |
| 221 | return false; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 222 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 223 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 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 | Queue<SwitchQueueEntry> getQueue(MsgPriority priority) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 228 | return rawQueues.get(priority.ordinal()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 229 | } |
| 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 | /** |
| 233 | * BarrierInfo holds information to specify barrier message sent to switch. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 234 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 235 | private static final class BarrierInfo { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 236 | final long dpid; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 237 | final long xid; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 239 | static BarrierInfo create(long dpid, OFBarrierRequest req) { |
| 240 | return new BarrierInfo(dpid, req.getXid()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 241 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 242 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 243 | static BarrierInfo create(long dpid, OFBarrierReply rpy) { |
| 244 | return new BarrierInfo(dpid, rpy.getXid()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 245 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 246 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 247 | private BarrierInfo(long dpid, long xid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 248 | this.dpid = dpid; |
| 249 | this.xid = xid; |
| 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 | // Auto generated code by Eclipse |
| 253 | @Override |
| 254 | public int hashCode() { |
| 255 | final int prime = 31; |
| 256 | int result = 1; |
| 257 | result = prime * result + (int) (dpid ^ (dpid >>> 32)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 258 | result = prime * result + (int) (xid ^ (xid >>> 32)); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 259 | return result; |
| 260 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 261 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 262 | @Override |
| 263 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 264 | if (this == obj) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 265 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 266 | } |
| 267 | if (obj == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 268 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 269 | } |
| 270 | if (getClass() != obj.getClass()) { |
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 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 273 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 274 | BarrierInfo other = (BarrierInfo) obj; |
| 275 | return (this.dpid == other.dpid) && (this.xid == other.xid); |
| 276 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 277 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 278 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 279 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 280 | /** |
| 281 | * Main thread that reads messages from queues and sends them to switches. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 282 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 283 | private class FlowPusherThread extends Thread { |
| 284 | private Map<Dpid, SwitchQueue> assignedQueues = new ConcurrentHashMap<>(); |
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 | final Lock queuingLock = new ReentrantLock(); |
| 287 | final Condition messagePushed = queuingLock.newCondition(); |
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 | @Override |
| 290 | public void run() { |
| 291 | this.setName("FlowPusherThread " + this.getId()); |
| 292 | while (true) { |
| 293 | while (!queuesHasMessageToSend()) { |
| 294 | queuingLock.lock(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 295 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 296 | try { |
| 297 | // wait for message pushed to queue |
| 298 | messagePushed.await(); |
| 299 | } catch (InterruptedException e) { |
| 300 | // Interrupted to be shut down (not an error) |
| 301 | log.debug("FlowPusherThread is interrupted"); |
| 302 | return; |
| 303 | } finally { |
| 304 | queuingLock.unlock(); |
| 305 | } |
| 306 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 307 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 308 | for (Iterator<Entry<Dpid, SwitchQueue>> it = assignedQueues |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 309 | .entrySet().iterator(); it.hasNext();) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 310 | Entry<Dpid, SwitchQueue> entry = it.next(); |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 311 | SwitchQueue queue = entry.getValue(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 312 | |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 313 | if (queue == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 314 | continue; |
| 315 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 316 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 317 | synchronized (queue) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 318 | processQueue(entry.getKey(), queue, MAX_MESSAGE_SEND); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 319 | if (queue.toBeDeleted && !queue.hasMessageToSend()) { |
| 320 | // remove queue if flagged to be. |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 321 | it.remove(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 327 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 328 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 329 | * Read messages from queue and send them to the switch. If number of |
| 330 | * messages excess the limit, stop sending messages. |
| 331 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 332 | * @param dpid DPID of the switch to which messages will be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 333 | * @param queue Queue of messages. |
| 334 | * @param maxMsg Limitation of number of messages to be sent. If set to |
| 335 | * 0, all messages in queue will be sent. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 336 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 337 | private void processQueue(Dpid dpid, SwitchQueue queue, int maxMsg) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 338 | // check sending rate and determine it to be sent or not |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 339 | long currentTime = System.currentTimeMillis(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 340 | long size = 0; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 341 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 342 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
| 343 | if (sw == null) { |
| 344 | // FlowPusher state for this switch will get cleaned up soon |
| 345 | // due to the switchDisconnected event |
| 346 | log.debug("Switch {} not found when processing queue", dpid); |
| 347 | return; |
| 348 | } |
| 349 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 350 | if (sw.isConnected() && queue.isSendable(currentTime)) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 351 | int i = 0; |
| 352 | while (queue.hasMessageToSend()) { |
| 353 | // Number of messages excess the limit |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 354 | if (0 < maxMsg && maxMsg <= i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | ++i; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 358 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 359 | SwitchQueueEntry queueEntry; |
| 360 | synchronized (queue) { |
| 361 | queueEntry = queue.poll(); |
| 362 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 363 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 364 | OFMessage msg = queueEntry.getOFMessage(); |
| 365 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 366 | sw.write(msg, null); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 367 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 368 | log.trace("Pusher sends message to switch {}: {}", sw.getStringId(), msg); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 369 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 370 | // TODO BOC how do we get the size? |
| 371 | // size += msg.getLength(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 372 | } catch (IOException e) { |
Yuta HIGUCHI | e933a28 | 2014-07-13 21:02:29 -0700 | [diff] [blame] | 373 | log.error("Exception in sending message (" + msg + "):", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
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 | sw.flush(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 378 | queue.logSentData(currentTime, size); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 381 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 382 | private boolean queuesHasMessageToSend() { |
| 383 | for (SwitchQueue queue : assignedQueues.values()) { |
| 384 | if (queue.hasMessageToSend()) { |
| 385 | return true; |
| 386 | } |
| 387 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 388 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 389 | return false; |
| 390 | } |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 391 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 392 | private void notifyMessagePushed() { |
| 393 | queuingLock.lock(); |
| 394 | try { |
| 395 | messagePushed.signal(); |
| 396 | } finally { |
| 397 | queuingLock.unlock(); |
| 398 | } |
| 399 | } |
| 400 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 401 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 402 | /** |
| 403 | * Initialize object with one thread. |
| 404 | */ |
| 405 | public FlowPusher() { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 406 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 407 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 408 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 409 | /** |
| 410 | * Initialize object with threads of given number. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 411 | * <p> |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 412 | * @param numberThreadValue Number of threads to handle messages. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 413 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 414 | public FlowPusher(int numberThreadValue) { |
| 415 | if (numberThreadValue > 0) { |
| 416 | numberThread = numberThreadValue; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 417 | } else { |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 418 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 421 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 422 | /** |
| 423 | * Set parameters needed for sending messages. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 424 | * <p> |
| 425 | * @param floodlightContext FloodlightModuleContext used for acquiring |
| 426 | * ThreadPoolService and registering MessageListener. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 427 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 428 | public void init(FloodlightModuleContext floodlightContext) { |
| 429 | this.context = floodlightContext; |
| 430 | this.floodlightProvider = context |
| 431 | .getServiceImpl(IFloodlightProviderService.class); |
| 432 | this.threadPool = context.getServiceImpl(IThreadPoolService.class); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 433 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 434 | floodlightProvider.addOFMessageListener(OFType.BARRIER_REPLY, this); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 435 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 436 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 437 | /** |
| 438 | * Begin processing queue. |
| 439 | */ |
| 440 | public void start() { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 441 | threadMap = new HashMap<>(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 442 | for (long i = 0; i < numberThread; ++i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 443 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 444 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 445 | threadMap.put(i, thread); |
| 446 | thread.start(); |
| 447 | } |
| 448 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 449 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 450 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 451 | public boolean suspend(Dpid dpid) { |
| 452 | SwitchQueue queue = getQueue(dpid); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 453 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 454 | if (queue == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 455 | // create queue in case suspend is called before first message |
| 456 | // addition |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 457 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | synchronized (queue) { |
| 461 | if (queue.state == QueueState.READY) { |
| 462 | queue.state = QueueState.SUSPENDED; |
| 463 | return true; |
| 464 | } |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 470 | public boolean resume(Dpid dpid) { |
| 471 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 472 | |
| 473 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 474 | log.error("No queue is attached to DPID: {}", dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
| 478 | synchronized (queue) { |
| 479 | if (queue.state == QueueState.SUSPENDED) { |
| 480 | queue.state = QueueState.READY; |
| 481 | |
| 482 | // Free the latch if queue has any messages |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 483 | FlowPusherThread thread = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 484 | if (queue.hasMessageToSend()) { |
| 485 | thread.notifyMessagePushed(); |
| 486 | } |
| 487 | return true; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 494 | public QueueState getState(Dpid dpid) { |
| 495 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 496 | |
| 497 | if (queue == null) { |
| 498 | return QueueState.UNKNOWN; |
| 499 | } |
| 500 | |
| 501 | return queue.state; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Stop processing queue and exit thread. |
| 506 | */ |
| 507 | public void stop() { |
| 508 | if (threadMap == null) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | for (FlowPusherThread t : threadMap.values()) { |
| 513 | t.interrupt(); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 518 | public void setRate(Dpid dpid, long rate) { |
| 519 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 520 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 521 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (rate > 0) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 525 | log.debug("rate for {} is set to {}", dpid, rate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 526 | synchronized (queue) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 527 | queue.maxRate = rate; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | @Override |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 533 | @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 534 | justification = "Future versions of createQueueImpl() might return null") |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 535 | public boolean createQueue(Dpid dpid) { |
| 536 | SwitchQueue queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 537 | |
| 538 | return (queue != null); |
| 539 | } |
| 540 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 541 | protected SwitchQueue createQueueImpl(Dpid dpid) { |
| 542 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 543 | if (queue != null) { |
| 544 | return queue; |
| 545 | } |
| 546 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 547 | FlowPusherThread proc = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 548 | queue = new SwitchQueue(); |
| 549 | queue.state = QueueState.READY; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 550 | proc.assignedQueues.put(dpid, queue); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 551 | |
| 552 | return queue; |
| 553 | } |
| 554 | |
| 555 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 556 | public boolean deleteQueue(Dpid dpid) { |
| 557 | return deleteQueue(dpid, false); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 561 | public boolean deleteQueue(Dpid dpid, boolean forceStop) { |
| 562 | FlowPusherThread proc = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 563 | |
| 564 | if (forceStop) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 565 | SwitchQueue queue = proc.assignedQueues.remove(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 566 | if (queue == null) { |
| 567 | return false; |
| 568 | } |
| 569 | return true; |
| 570 | } else { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 571 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 572 | if (queue == null) { |
| 573 | return false; |
| 574 | } |
| 575 | synchronized (queue) { |
| 576 | queue.toBeDeleted = true; |
| 577 | } |
| 578 | return true; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 583 | public boolean add(Dpid dpid, OFMessage msg) { |
| 584 | return add(dpid, msg, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 588 | public boolean add(Dpid dpid, OFMessage msg, MsgPriority priority) { |
| 589 | return addMessageImpl(dpid, msg, priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | @Override |
| 593 | public void pushFlowEntries( |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 594 | Collection<Pair<Dpid, FlowEntry>> entries) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 595 | pushFlowEntries(entries, MsgPriority.NORMAL); |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public void pushFlowEntries( |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 600 | Collection<Pair<Dpid, FlowEntry>> entries, MsgPriority priority) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 601 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 602 | for (Pair<Dpid, FlowEntry> entry : entries) { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 603 | add(entry.getLeft(), entry.getRight(), priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 608 | public void pushFlowEntry(Dpid dpid, FlowEntry flowEntry) { |
| 609 | pushFlowEntry(dpid, flowEntry, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 613 | public void pushFlowEntry(Dpid dpid, FlowEntry flowEntry, MsgPriority priority) { |
| 614 | Collection<Pair<Dpid, FlowEntry>> entries = new LinkedList<>(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 615 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 616 | entries.add(Pair.of(dpid, flowEntry)); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 617 | pushFlowEntries(entries, priority); |
| 618 | } |
| 619 | |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 620 | public static final int PRIORITY_DEFAULT = 32768; // Default Flow Priority |
| 621 | |
| 622 | @Override |
| 623 | public void pushMatchAction(MatchActionOperationEntry matchActionOp) { |
| 624 | final MatchAction matchAction = matchActionOp.getTarget(); |
| 625 | |
| 626 | // Get the switch and its OFFactory |
| 627 | final SwitchPort srcPort = matchAction.getSwitchPort(); |
| 628 | final Dpid dpid = srcPort.getDpid(); |
| 629 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
| 630 | if (sw == null) { |
| 631 | log.warn("Couldn't find switch {} when pushing message", dpid); |
| 632 | return; |
| 633 | } |
| 634 | OFFactory factory = sw.getFactory(); |
| 635 | |
| 636 | // Build Match |
| 637 | final Match match = matchAction.getMatch(); |
| 638 | Builder matchBuilder = factory.buildMatch(); |
| 639 | if (match instanceof PacketMatch) { |
| 640 | final PacketMatch packetMatch = (PacketMatch) match; |
| 641 | final MACAddress srcMac = packetMatch.getSrcMacAddress(); |
| 642 | final MACAddress dstMac = packetMatch.getDstMacAddress(); |
| 643 | final Short etherType = packetMatch.getEtherType(); |
| 644 | final IPv4Net srcIp = packetMatch.getSrcIpAddress(); |
| 645 | final IPv4Net dstIp = packetMatch.getDstIpAddress(); |
| 646 | final Byte ipProto = packetMatch.getIpProtocolNumber(); |
| 647 | final Short srcTcpPort = packetMatch.getSrcTcpPortNumber(); |
| 648 | final Short dstTcpPort = packetMatch.getDstTcpPortNumber(); |
| 649 | |
| 650 | if (srcMac != null) { |
| 651 | matchBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(srcMac.toLong())); |
| 652 | } |
| 653 | if (dstMac != null) { |
| 654 | matchBuilder.setExact(MatchField.ETH_DST, MacAddress.of(dstMac.toLong())); |
| 655 | } |
| 656 | if (etherType != null) { |
| 657 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.of(etherType)); |
| 658 | } |
| 659 | if (srcIp != null) { |
| 660 | matchBuilder.setMasked(MatchField.IPV4_SRC, |
| 661 | IPv4Address.of(srcIp.address().value()) |
| 662 | .withMaskOfLength(srcIp.prefixLen())); |
| 663 | } |
| 664 | if (dstIp != null) { |
| 665 | matchBuilder.setMasked(MatchField.IPV4_DST, |
| 666 | IPv4Address.of(dstIp.address().value()) |
| 667 | .withMaskOfLength(dstIp.prefixLen())); |
| 668 | } |
| 669 | if (ipProto != null) { |
| 670 | matchBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(ipProto)); |
| 671 | } |
| 672 | if (srcTcpPort != null) { |
| 673 | matchBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(srcTcpPort)); |
| 674 | } |
| 675 | if (dstTcpPort != null) { |
| 676 | matchBuilder.setExact(MatchField.TCP_DST, TransportPort.of(dstTcpPort)); |
| 677 | } |
| 678 | matchBuilder.setExact(MatchField.IN_PORT, |
| 679 | OFPort.of(srcPort.getPortNumber().shortValue())); |
| 680 | } else { |
| 681 | log.warn("Unsupported Match type: {}", match.getClass().getName()); |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | // Build Actions |
| 686 | List<OFAction> actionList = new ArrayList<>(matchAction.getActions().size()); |
| 687 | OFActions ofActionTypes = factory.actions(); |
| 688 | for (Action action : matchAction.getActions()) { |
| 689 | OFAction ofAction = null; |
| 690 | if (action instanceof OutputAction) { |
| 691 | OutputAction outputAction = (OutputAction) action; |
| 692 | // short or int? |
| 693 | OFPort port = OFPort.of((int) outputAction.getPortNumber().value()); |
| 694 | ofAction = ofActionTypes.output(port, Short.MAX_VALUE); |
| 695 | } else if (action instanceof ModifyDstMacAction) { |
| 696 | ModifyDstMacAction dstMacAction = (ModifyDstMacAction) action; |
| 697 | ofActionTypes.setDlDst(MacAddress.of(dstMacAction.getDstMac().toLong())); |
| 698 | } else if (action instanceof ModifySrcMacAction) { |
| 699 | ModifySrcMacAction srcMacAction = (ModifySrcMacAction) action; |
| 700 | ofActionTypes.setDlSrc(MacAddress.of(srcMacAction.getSrcMac().toLong())); |
| 701 | } else { |
| 702 | log.warn("Unsupported Action type: {}", action.getClass().getName()); |
| 703 | continue; |
| 704 | } |
| 705 | actionList.add(ofAction); |
| 706 | } |
| 707 | |
| 708 | // Construct a FlowMod message builder |
| 709 | OFFlowMod.Builder fmBuilder = null; |
| 710 | switch (matchActionOp.getOperator()) { |
| 711 | case ADD: |
| 712 | fmBuilder = factory.buildFlowAdd(); |
| 713 | break; |
| 714 | case REMOVE: |
| 715 | fmBuilder = factory.buildFlowDeleteStrict(); |
| 716 | break; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 717 | case MODIFY: |
| 718 | fmBuilder = factory.buildFlowModifyStrict(); |
| 719 | break; |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 720 | default: |
| 721 | log.warn("Unsupported MatchAction Operator: {}", matchActionOp.getOperator()); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | // Add output port for OF1.0 |
| 726 | OFPort outp = OFPort.of((short) 0xffff); // OF1.0 OFPP.NONE |
| 727 | if (matchActionOp.getOperator() == Operator.REMOVE) { |
| 728 | if (actionList.size() == 1) { |
| 729 | if (actionList.get(0).getType() == OFActionType.OUTPUT) { |
| 730 | OFActionOutput oa = (OFActionOutput) actionList.get(0); |
| 731 | outp = oa.getPort(); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | // Build OFFlowMod |
| 738 | fmBuilder.setMatch(matchBuilder.build()) |
| 739 | .setActions(actionList) |
| 740 | .setIdleTimeout(0) // hardcoded to zero for now |
| 741 | .setHardTimeout(0) // hardcoded to zero for now |
Ray Milkey | a313cde | 2014-09-05 09:02:52 -0700 | [diff] [blame] | 742 | .setCookie(U64.of(matchAction.getId().getValue())) |
Brian O'Connor | aa5a7b9 | 2014-08-29 14:45:18 -0700 | [diff] [blame] | 743 | .setBufferId(OFBufferId.NO_BUFFER) |
| 744 | .setPriority(PRIORITY_DEFAULT) |
| 745 | .setOutPort(outp); |
| 746 | |
| 747 | // Build the message and add it to the queue |
| 748 | add(dpid, fmBuilder.build()); |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public void pushMatchActions(Collection<MatchActionOperationEntry> matchActionOps) { |
| 753 | for (MatchActionOperationEntry matchActionOp : matchActionOps) { |
| 754 | pushMatchAction(matchActionOp); |
| 755 | } |
| 756 | } |
| 757 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 758 | /** |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 759 | * Create a message from FlowEntry and add it to the queue of the switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 760 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 761 | * @param dpid DPID of the switch to which the message is pushed. |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 762 | * @param flowEntry FlowEntry object used for creating message. |
| 763 | * @return true if message is successfully added to a queue. |
| 764 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 765 | private boolean add(Dpid dpid, FlowEntry flowEntry, MsgPriority priority) { |
| 766 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
| 767 | if (sw == null) { |
| 768 | log.warn("Couldn't find switch {} when pushing message", dpid); |
| 769 | return false; |
| 770 | } |
| 771 | |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 772 | // |
| 773 | // Create the OpenFlow Flow Modification Entry to push |
| 774 | // |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 775 | OFFlowMod fm = flowEntry.buildFlowMod(sw.getFactory()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 776 | // log.trace("Pushing flow mod {}", fm); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 777 | return addMessageImpl(dpid, fm, priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 781 | * Add message to queue. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 782 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 783 | * @param dpid DPID of the switch to which the message is sent |
| 784 | * @param msg message to send to the switch |
| 785 | * @param priority priority of the message |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 786 | * @return true if the message was added successfully, otherwise false |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 787 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 788 | protected boolean addMessageImpl(Dpid dpid, OFMessage msg, MsgPriority priority) { |
| 789 | FlowPusherThread thread = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 790 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 791 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 792 | |
| 793 | // create queue at first addition of message |
| 794 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 795 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | SwitchQueueEntry entry = new SwitchQueueEntry(msg); |
| 799 | |
| 800 | synchronized (queue) { |
| 801 | queue.add(entry, priority); |
| 802 | if (log.isTraceEnabled()) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 803 | log.trace("Message is pushed to switch {}: {}", |
| 804 | dpid, entry.getOFMessage()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
| 808 | thread.notifyMessagePushed(); |
| 809 | |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 814 | public OFBarrierReply barrier(Dpid dpid) { |
| 815 | OFMessageFuture<OFBarrierReply> future = barrierAsync(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 816 | if (future == null) { |
| 817 | return null; |
| 818 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 819 | try { |
| 820 | return future.get(); |
| 821 | } catch (InterruptedException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 822 | log.error("InterruptedException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 823 | } catch (ExecutionException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 824 | log.error("ExecutionException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 825 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 826 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 830 | public OFMessageFuture<OFBarrierReply> barrierAsync(Dpid dpid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 831 | // TODO creation of message and future should be moved to OFSwitchImpl |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 832 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 833 | if (sw == null) { |
| 834 | return null; |
| 835 | } |
| 836 | |
| 837 | OFBarrierRequest msg = createBarrierRequest(sw); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 838 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, |
| 839 | (int) msg.getXid()); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 840 | barrierFutures.put(BarrierInfo.create(dpid.value(), msg), future); |
| 841 | addMessageImpl(dpid, msg, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 842 | return future; |
| 843 | } |
| 844 | |
| 845 | protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) { |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 846 | OFFactory factory = sw.getFactory(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 847 | if (factory == null) { |
| 848 | log.error("No OF Message Factory for switch {} with OFVersion {}", sw, |
| 849 | sw.getOFVersion()); |
| 850 | return null; |
| 851 | } |
| 852 | return factory.buildBarrierRequest() |
| 853 | .setXid(sw.getNextTransactionId()) |
| 854 | .build(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Get a queue attached to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 859 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 860 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 861 | * @return Queue object |
| 862 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 863 | protected SwitchQueue getQueue(Dpid dpid) { |
| 864 | if (dpid == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 865 | return null; |
| 866 | } |
| 867 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 868 | FlowPusherThread th = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 869 | if (th == null) { |
| 870 | return null; |
| 871 | } |
| 872 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 873 | return th.assignedQueues.get(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /** |
| 877 | * Get a hash value correspondent to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 878 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 879 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 880 | * @return Hash value |
| 881 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 882 | protected long getHash(long dpid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 883 | // This code assumes DPID is sequentially assigned. |
| 884 | // TODO consider equalization algorithm |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 885 | return dpid % numberThread; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Get a Thread object which processes the queue attached to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 890 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 891 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 892 | * @return Thread object |
| 893 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 894 | protected FlowPusherThread getProcessingThread(Dpid dpid) { |
| 895 | long hash = getHash(dpid.value()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 896 | |
| 897 | return threadMap.get(hash); |
| 898 | } |
| 899 | |
| 900 | @Override |
| 901 | public String getName() { |
| 902 | return "flowpusher"; |
| 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 917 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 918 | log.trace("Received BARRIER_REPLY from : {}", sw.getStringId()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 921 | if ((msg.getType() != OFType.BARRIER_REPLY) || |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 922 | !(msg instanceof OFBarrierReply)) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 923 | log.error("Unexpected reply message: {}", msg.getType()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 924 | return Command.CONTINUE; |
| 925 | } |
| 926 | |
| 927 | OFBarrierReply reply = (OFBarrierReply) msg; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 928 | BarrierInfo info = BarrierInfo.create(sw.getId(), reply); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 929 | // Deliver future if exists |
| 930 | OFBarrierReplyFuture future = barrierFutures.get(info); |
| 931 | if (future != null) { |
| 932 | future.deliverFuture(sw, msg); |
| 933 | barrierFutures.remove(info); |
| 934 | } |
| 935 | |
| 936 | return Command.CONTINUE; |
| 937 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 938 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 939 | } |