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 | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.intent.FlowEntry; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.Dpid; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 29 | |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 30 | import org.apache.commons.lang3.tuple.Pair; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 32 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 35 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 36 | import org.projectfloodlight.openflow.protocol.OFType; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 40 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 41 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 42 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | * FlowPusher is a implementation of FlowPusherService. FlowPusher assigns one |
| 44 | * message queue instance for each one switch. Number of message processing |
| 45 | * threads is configurable by constructor, and one thread can handle multiple |
| 46 | * message queues. Each queue will be assigned to a thread according to hash |
| 47 | * function defined by getHash(). Each processing thread reads messages from |
| 48 | * queues and sends it to switches in round-robin. Processing thread also |
| 49 | * calculates rate of sending to suppress excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 50 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 51 | public final class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(FlowPusher.class); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 53 | private static final int DEFAULT_NUMBER_THREAD = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 54 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 55 | // Number of messages sent to switch at once |
| 56 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 57 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 58 | private FloodlightModuleContext context = null; |
| 59 | private IThreadPoolService threadPool = null; |
| 60 | private IFloodlightProviderService floodlightProvider = null; |
| 61 | |
| 62 | // Map of threads versus dpid |
| 63 | private Map<Long, FlowPusherThread> threadMap = null; |
| 64 | // Map from (DPID and transaction ID) to Future objects. |
| 65 | private Map<BarrierInfo, OFBarrierReplyFuture> barrierFutures = |
| 66 | new ConcurrentHashMap<BarrierInfo, OFBarrierReplyFuture>(); |
| 67 | |
| 68 | private int numberThread; |
| 69 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 70 | private static class SwitchQueueEntry { |
| 71 | OFMessage msg; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 72 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 73 | public SwitchQueueEntry(OFMessage msg) { |
| 74 | this.msg = msg; |
| 75 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 76 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 77 | public OFMessage getOFMessage() { |
| 78 | return msg; |
| 79 | } |
| 80 | } |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 81 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 82 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 83 | * SwitchQueue represents message queue attached to a switch. This consists |
| 84 | * of queue itself and variables used for limiting sending rate. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 85 | */ |
Pavlin Radoslavov | fee8098 | 2014-04-10 12:12:04 -0700 | [diff] [blame] | 86 | private static class SwitchQueue { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 87 | List<Queue<SwitchQueueEntry>> rawQueues; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 88 | QueueState state; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 89 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 90 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 91 | long maxRate = 0; // 0 indicates no limitation |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 92 | long lastSentTime = 0; |
| 93 | long lastSentSize = 0; |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 94 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 95 | // "To be deleted" flag |
| 96 | boolean toBeDeleted = false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 98 | SwitchQueue() { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 99 | rawQueues = new ArrayList<>(MsgPriority.values().length); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 100 | for (int i = 0; i < MsgPriority.values().length; ++i) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 101 | rawQueues.add(i, new ArrayDeque<SwitchQueueEntry>()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 102 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 103 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 104 | state = QueueState.READY; |
| 105 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 106 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 107 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 108 | * Check if sending rate is within the rate. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 109 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 110 | * @param current Current time |
| 111 | * @return true if within the rate |
| 112 | */ |
| 113 | boolean isSendable(long current) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 114 | if (maxRate == 0) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 115 | // no limitation |
| 116 | return true; |
| 117 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 118 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 119 | if (current == lastSentTime) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 122 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 123 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 124 | long rate = lastSentSize / (current - lastSentTime); |
| 125 | return (rate < maxRate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 126 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 127 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Log time and size of last sent data. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 130 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 131 | * @param current Time to be sent. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 132 | * @param size Size of sent data (in bytes). |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 133 | */ |
| 134 | void logSentData(long current, long size) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 135 | lastSentTime = current; |
| 136 | lastSentSize = size; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 137 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 138 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 139 | boolean add(SwitchQueueEntry entry, MsgPriority priority) { |
| 140 | Queue<SwitchQueueEntry> queue = getQueue(priority); |
| 141 | if (queue == null) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 142 | log.error("Unexpected priority: {}", priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | return queue.add(entry); |
| 146 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 147 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Poll single appropriate entry object according to QueueState. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 150 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 151 | * @return Entry object. |
| 152 | */ |
| 153 | SwitchQueueEntry poll() { |
| 154 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 155 | case READY: { |
| 156 | for (int i = 0; i < rawQueues.size(); ++i) { |
| 157 | SwitchQueueEntry entry = rawQueues.get(i).poll(); |
| 158 | if (entry != null) { |
| 159 | return entry; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 160 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 161 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 162 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 163 | return null; |
| 164 | } |
| 165 | case SUSPENDED: { |
| 166 | // Only polling from high priority queue |
| 167 | SwitchQueueEntry entry = getQueue(MsgPriority.HIGH).poll(); |
| 168 | return entry; |
| 169 | } |
| 170 | default: |
| 171 | log.error("Unexpected QueueState: {}", state); |
| 172 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 175 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 176 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 177 | * 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] | 178 | * <p> |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 179 | * @return True if there are some messages to be sent. |
| 180 | */ |
| 181 | boolean hasMessageToSend() { |
| 182 | switch (state) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 183 | case READY: |
| 184 | for (Queue<SwitchQueueEntry> queue : rawQueues) { |
| 185 | if (!queue.isEmpty()) { |
| 186 | return true; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 187 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case SUSPENDED: |
| 191 | // Only checking high priority queue |
| 192 | return (!getQueue(MsgPriority.HIGH).isEmpty()); |
| 193 | default: |
| 194 | log.error("Unexpected QueueState: {}", state); |
| 195 | return false; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 196 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 197 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 200 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 201 | Queue<SwitchQueueEntry> getQueue(MsgPriority priority) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 202 | return rawQueues.get(priority.ordinal()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 205 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 206 | /** |
| 207 | * BarrierInfo holds information to specify barrier message sent to switch. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 208 | */ |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 209 | private static final class BarrierInfo { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 210 | final long dpid; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 211 | final long xid; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 212 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 213 | static BarrierInfo create(long dpid, OFBarrierRequest req) { |
| 214 | return new BarrierInfo(dpid, req.getXid()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 215 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 216 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 217 | static BarrierInfo create(long dpid, OFBarrierReply rpy) { |
| 218 | return new BarrierInfo(dpid, rpy.getXid()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 219 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 220 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 221 | private BarrierInfo(long dpid, long xid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 222 | this.dpid = dpid; |
| 223 | this.xid = xid; |
| 224 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 225 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 226 | // Auto generated code by Eclipse |
| 227 | @Override |
| 228 | public int hashCode() { |
| 229 | final int prime = 31; |
| 230 | int result = 1; |
| 231 | result = prime * result + (int) (dpid ^ (dpid >>> 32)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 232 | result = prime * result + (int) (xid ^ (xid >>> 32)); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 233 | return result; |
| 234 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 235 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 236 | @Override |
| 237 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 238 | if (this == obj) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 239 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 240 | } |
| 241 | if (obj == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 242 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 243 | } |
| 244 | if (getClass() != obj.getClass()) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 245 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 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 | BarrierInfo other = (BarrierInfo) obj; |
| 249 | return (this.dpid == other.dpid) && (this.xid == other.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 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 253 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Main thread that reads messages from queues and sends them to switches. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 256 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 257 | private class FlowPusherThread extends Thread { |
| 258 | private Map<Dpid, SwitchQueue> assignedQueues = new ConcurrentHashMap<>(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 259 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 260 | final Lock queuingLock = new ReentrantLock(); |
| 261 | final Condition messagePushed = queuingLock.newCondition(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 262 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 263 | @Override |
| 264 | public void run() { |
| 265 | this.setName("FlowPusherThread " + this.getId()); |
| 266 | while (true) { |
| 267 | while (!queuesHasMessageToSend()) { |
| 268 | queuingLock.lock(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 269 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 270 | try { |
| 271 | // wait for message pushed to queue |
| 272 | messagePushed.await(); |
| 273 | } catch (InterruptedException e) { |
| 274 | // Interrupted to be shut down (not an error) |
| 275 | log.debug("FlowPusherThread is interrupted"); |
| 276 | return; |
| 277 | } finally { |
| 278 | queuingLock.unlock(); |
| 279 | } |
| 280 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 281 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 282 | for (Iterator<Entry<Dpid, SwitchQueue>> it = assignedQueues |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 283 | .entrySet().iterator(); it.hasNext();) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 284 | Entry<Dpid, SwitchQueue> entry = it.next(); |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 285 | SwitchQueue queue = entry.getValue(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 286 | |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 287 | if (queue == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 288 | continue; |
| 289 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 290 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 291 | synchronized (queue) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 292 | processQueue(entry.getKey(), queue, MAX_MESSAGE_SEND); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 293 | if (queue.toBeDeleted && !queue.hasMessageToSend()) { |
| 294 | // remove queue if flagged to be. |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 295 | it.remove(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 301 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 302 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 303 | * Read messages from queue and send them to the switch. If number of |
| 304 | * messages excess the limit, stop sending messages. |
| 305 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 306 | * @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] | 307 | * @param queue Queue of messages. |
| 308 | * @param maxMsg Limitation of number of messages to be sent. If set to |
| 309 | * 0, all messages in queue will be sent. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 310 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 311 | private void processQueue(Dpid dpid, SwitchQueue queue, int maxMsg) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 312 | // check sending rate and determine it to be sent or not |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 313 | long currentTime = System.currentTimeMillis(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 314 | long size = 0; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 315 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 316 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
| 317 | if (sw == null) { |
| 318 | // FlowPusher state for this switch will get cleaned up soon |
| 319 | // due to the switchDisconnected event |
| 320 | log.debug("Switch {} not found when processing queue", dpid); |
| 321 | return; |
| 322 | } |
| 323 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 324 | if (sw.isConnected() && queue.isSendable(currentTime)) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 325 | int i = 0; |
| 326 | while (queue.hasMessageToSend()) { |
| 327 | // Number of messages excess the limit |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 328 | if (0 < maxMsg && maxMsg <= i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | ++i; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 332 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 333 | SwitchQueueEntry queueEntry; |
| 334 | synchronized (queue) { |
| 335 | queueEntry = queue.poll(); |
| 336 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 337 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 338 | OFMessage msg = queueEntry.getOFMessage(); |
| 339 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 340 | sw.write(msg, null); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 341 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 342 | log.trace("Pusher sends message to switch {}: {}", sw.getStringId(), msg); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 343 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 344 | // TODO BOC how do we get the size? |
| 345 | // size += msg.getLength(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 346 | } catch (IOException e) { |
Yuta HIGUCHI | e933a28 | 2014-07-13 21:02:29 -0700 | [diff] [blame] | 347 | log.error("Exception in sending message (" + msg + "):", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 350 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 351 | sw.flush(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 352 | queue.logSentData(currentTime, size); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 355 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 356 | private boolean queuesHasMessageToSend() { |
| 357 | for (SwitchQueue queue : assignedQueues.values()) { |
| 358 | if (queue.hasMessageToSend()) { |
| 359 | return true; |
| 360 | } |
| 361 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 362 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 363 | return false; |
| 364 | } |
Naoki Shiota | 0533469 | 2014-03-18 16:06:36 -0700 | [diff] [blame] | 365 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 366 | private void notifyMessagePushed() { |
| 367 | queuingLock.lock(); |
| 368 | try { |
| 369 | messagePushed.signal(); |
| 370 | } finally { |
| 371 | queuingLock.unlock(); |
| 372 | } |
| 373 | } |
| 374 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 375 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 376 | /** |
| 377 | * Initialize object with one thread. |
| 378 | */ |
| 379 | public FlowPusher() { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 380 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 381 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 382 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 383 | /** |
| 384 | * Initialize object with threads of given number. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 385 | * <p> |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 386 | * @param numberThreadValue Number of threads to handle messages. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 387 | */ |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 388 | public FlowPusher(int numberThreadValue) { |
| 389 | if (numberThreadValue > 0) { |
| 390 | numberThread = numberThreadValue; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 391 | } else { |
Ray Milkey | 9526d6f | 2014-04-10 14:54:15 -0700 | [diff] [blame] | 392 | numberThread = DEFAULT_NUMBER_THREAD; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 395 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 396 | /** |
| 397 | * Set parameters needed for sending messages. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 398 | * <p> |
| 399 | * @param floodlightContext FloodlightModuleContext used for acquiring |
| 400 | * ThreadPoolService and registering MessageListener. |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 401 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 402 | public void init(FloodlightModuleContext floodlightContext) { |
| 403 | this.context = floodlightContext; |
| 404 | this.floodlightProvider = context |
| 405 | .getServiceImpl(IFloodlightProviderService.class); |
| 406 | this.threadPool = context.getServiceImpl(IThreadPoolService.class); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 407 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 408 | floodlightProvider.addOFMessageListener(OFType.BARRIER_REPLY, this); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 409 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 410 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 411 | /** |
| 412 | * Begin processing queue. |
| 413 | */ |
| 414 | public void start() { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 415 | threadMap = new HashMap<>(); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 416 | for (long i = 0; i < numberThread; ++i) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 417 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 418 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 419 | threadMap.put(i, thread); |
| 420 | thread.start(); |
| 421 | } |
| 422 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 423 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 424 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 425 | public boolean suspend(Dpid dpid) { |
| 426 | SwitchQueue queue = getQueue(dpid); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 427 | |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 428 | if (queue == null) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 429 | // create queue in case suspend is called before first message |
| 430 | // addition |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 431 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | synchronized (queue) { |
| 435 | if (queue.state == QueueState.READY) { |
| 436 | queue.state = QueueState.SUSPENDED; |
| 437 | return true; |
| 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 444 | public boolean resume(Dpid dpid) { |
| 445 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 446 | |
| 447 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 448 | log.error("No queue is attached to DPID: {}", dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
| 452 | synchronized (queue) { |
| 453 | if (queue.state == QueueState.SUSPENDED) { |
| 454 | queue.state = QueueState.READY; |
| 455 | |
| 456 | // Free the latch if queue has any messages |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 457 | FlowPusherThread thread = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 458 | if (queue.hasMessageToSend()) { |
| 459 | thread.notifyMessagePushed(); |
| 460 | } |
| 461 | return true; |
| 462 | } |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 468 | public QueueState getState(Dpid dpid) { |
| 469 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 470 | |
| 471 | if (queue == null) { |
| 472 | return QueueState.UNKNOWN; |
| 473 | } |
| 474 | |
| 475 | return queue.state; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Stop processing queue and exit thread. |
| 480 | */ |
| 481 | public void stop() { |
| 482 | if (threadMap == null) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | for (FlowPusherThread t : threadMap.values()) { |
| 487 | t.interrupt(); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 492 | public void setRate(Dpid dpid, long rate) { |
| 493 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 494 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 495 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (rate > 0) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 499 | log.debug("rate for {} is set to {}", dpid, rate); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 500 | synchronized (queue) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 501 | queue.maxRate = rate; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | @Override |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 507 | @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 508 | justification = "Future versions of createQueueImpl() might return null") |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 509 | public boolean createQueue(Dpid dpid) { |
| 510 | SwitchQueue queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 511 | |
| 512 | return (queue != null); |
| 513 | } |
| 514 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 515 | protected SwitchQueue createQueueImpl(Dpid dpid) { |
| 516 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 517 | if (queue != null) { |
| 518 | return queue; |
| 519 | } |
| 520 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 521 | FlowPusherThread proc = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 522 | queue = new SwitchQueue(); |
| 523 | queue.state = QueueState.READY; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 524 | proc.assignedQueues.put(dpid, queue); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 525 | |
| 526 | return queue; |
| 527 | } |
| 528 | |
| 529 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 530 | public boolean deleteQueue(Dpid dpid) { |
| 531 | return deleteQueue(dpid, false); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 535 | public boolean deleteQueue(Dpid dpid, boolean forceStop) { |
| 536 | FlowPusherThread proc = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 537 | |
| 538 | if (forceStop) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 539 | SwitchQueue queue = proc.assignedQueues.remove(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 540 | if (queue == null) { |
| 541 | return false; |
| 542 | } |
| 543 | return true; |
| 544 | } else { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 545 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 546 | if (queue == null) { |
| 547 | return false; |
| 548 | } |
| 549 | synchronized (queue) { |
| 550 | queue.toBeDeleted = true; |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 557 | public boolean add(Dpid dpid, OFMessage msg) { |
| 558 | return add(dpid, msg, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 562 | public boolean add(Dpid dpid, OFMessage msg, MsgPriority priority) { |
| 563 | return addMessageImpl(dpid, msg, priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | @Override |
| 567 | public void pushFlowEntries( |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 568 | Collection<Pair<Dpid, FlowEntry>> entries) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 569 | pushFlowEntries(entries, MsgPriority.NORMAL); |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void pushFlowEntries( |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 574 | Collection<Pair<Dpid, FlowEntry>> entries, MsgPriority priority) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 575 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 576 | for (Pair<Dpid, FlowEntry> entry : entries) { |
Pavlin Radoslavov | b05aac9 | 2014-08-26 18:26:10 -0700 | [diff] [blame] | 577 | add(entry.getLeft(), entry.getRight(), priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 582 | public void pushFlowEntry(Dpid dpid, FlowEntry flowEntry) { |
| 583 | pushFlowEntry(dpid, flowEntry, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 587 | public void pushFlowEntry(Dpid dpid, FlowEntry flowEntry, MsgPriority priority) { |
| 588 | Collection<Pair<Dpid, FlowEntry>> entries = new LinkedList<>(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 589 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 590 | entries.add(Pair.of(dpid, flowEntry)); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 591 | pushFlowEntries(entries, priority); |
| 592 | } |
| 593 | |
| 594 | /** |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 595 | * 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] | 596 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 597 | * @param dpid DPID of the switch to which the message is pushed. |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 598 | * @param flowEntry FlowEntry object used for creating message. |
| 599 | * @return true if message is successfully added to a queue. |
| 600 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 601 | private boolean add(Dpid dpid, FlowEntry flowEntry, MsgPriority priority) { |
| 602 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
| 603 | if (sw == null) { |
| 604 | log.warn("Couldn't find switch {} when pushing message", dpid); |
| 605 | return false; |
| 606 | } |
| 607 | |
Ray Milkey | ddcd492 | 2014-04-17 11:21:20 -0700 | [diff] [blame] | 608 | // |
| 609 | // Create the OpenFlow Flow Modification Entry to push |
| 610 | // |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 611 | OFFlowMod fm = flowEntry.buildFlowMod(sw.getFactory()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 612 | // log.trace("Pushing flow mod {}", fm); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 613 | return addMessageImpl(dpid, fm, priority); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 617 | * Add message to queue. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 618 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 619 | * @param dpid DPID of the switch to which the message is sent |
| 620 | * @param msg message to send to the switch |
| 621 | * @param priority priority of the message |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 622 | * @return true if the message was added successfully, otherwise false |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 623 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 624 | protected boolean addMessageImpl(Dpid dpid, OFMessage msg, MsgPriority priority) { |
| 625 | FlowPusherThread thread = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 626 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 627 | SwitchQueue queue = getQueue(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 628 | |
| 629 | // create queue at first addition of message |
| 630 | if (queue == null) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 631 | queue = createQueueImpl(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | SwitchQueueEntry entry = new SwitchQueueEntry(msg); |
| 635 | |
| 636 | synchronized (queue) { |
| 637 | queue.add(entry, priority); |
| 638 | if (log.isTraceEnabled()) { |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 639 | log.trace("Message is pushed to switch {}: {}", |
| 640 | dpid, entry.getOFMessage()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | thread.notifyMessagePushed(); |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 650 | public OFBarrierReply barrier(Dpid dpid) { |
| 651 | OFMessageFuture<OFBarrierReply> future = barrierAsync(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 652 | if (future == null) { |
| 653 | return null; |
| 654 | } |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 655 | try { |
| 656 | return future.get(); |
| 657 | } catch (InterruptedException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 658 | log.error("InterruptedException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 659 | } catch (ExecutionException e) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 660 | log.error("ExecutionException", e); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 661 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 662 | return null; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | @Override |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 666 | public OFMessageFuture<OFBarrierReply> barrierAsync(Dpid dpid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 667 | // TODO creation of message and future should be moved to OFSwitchImpl |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 668 | IOFSwitch sw = floodlightProvider.getMasterSwitch(dpid.value()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 669 | if (sw == null) { |
| 670 | return null; |
| 671 | } |
| 672 | |
| 673 | OFBarrierRequest msg = createBarrierRequest(sw); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 674 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, |
| 675 | (int) msg.getXid()); |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 676 | barrierFutures.put(BarrierInfo.create(dpid.value(), msg), future); |
| 677 | addMessageImpl(dpid, msg, MsgPriority.NORMAL); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 678 | return future; |
| 679 | } |
| 680 | |
| 681 | protected OFBarrierRequest createBarrierRequest(IOFSwitch sw) { |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 682 | OFFactory factory = sw.getFactory(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 683 | if (factory == null) { |
| 684 | log.error("No OF Message Factory for switch {} with OFVersion {}", sw, |
| 685 | sw.getOFVersion()); |
| 686 | return null; |
| 687 | } |
| 688 | return factory.buildBarrierRequest() |
| 689 | .setXid(sw.getNextTransactionId()) |
| 690 | .build(); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Get a queue attached to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 695 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 696 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 697 | * @return Queue object |
| 698 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 699 | protected SwitchQueue getQueue(Dpid dpid) { |
| 700 | if (dpid == null) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 701 | return null; |
| 702 | } |
| 703 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 704 | FlowPusherThread th = getProcessingThread(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 705 | if (th == null) { |
| 706 | return null; |
| 707 | } |
| 708 | |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 709 | return th.assignedQueues.get(dpid); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Get a hash value correspondent to a switch. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 714 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 715 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 716 | * @return Hash value |
| 717 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 718 | protected long getHash(long dpid) { |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 719 | // This code assumes DPID is sequentially assigned. |
| 720 | // TODO consider equalization algorithm |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 721 | return dpid % numberThread; |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
| 725 | * 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] | 726 | * <p> |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 727 | * @param dpid DPID of the switch |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 728 | * @return Thread object |
| 729 | */ |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 730 | protected FlowPusherThread getProcessingThread(Dpid dpid) { |
| 731 | long hash = getHash(dpid.value()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 732 | |
| 733 | return threadMap.get(hash); |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public String getName() { |
| 738 | return "flowpusher"; |
| 739 | } |
| 740 | |
| 741 | @Override |
| 742 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 753 | if (log.isTraceEnabled()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 754 | log.trace("Received BARRIER_REPLY from : {}", sw.getStringId()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 757 | if ((msg.getType() != OFType.BARRIER_REPLY) || |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 758 | !(msg instanceof OFBarrierReply)) { |
Naoki Shiota | 72afb33 | 2014-07-22 17:39:00 -0700 | [diff] [blame] | 759 | log.error("Unexpected reply message: {}", msg.getType()); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 760 | return Command.CONTINUE; |
| 761 | } |
| 762 | |
| 763 | OFBarrierReply reply = (OFBarrierReply) msg; |
Jonathan Hart | 5302b6c | 2014-08-13 15:57:59 -0700 | [diff] [blame] | 764 | BarrierInfo info = BarrierInfo.create(sw.getId(), reply); |
Ray Milkey | 8e5170e | 2014-04-02 12:09:55 -0700 | [diff] [blame] | 765 | // Deliver future if exists |
| 766 | OFBarrierReplyFuture future = barrierFutures.get(info); |
| 767 | if (future != null) { |
| 768 | future.deliverFuture(sw, msg); |
| 769 | barrierFutures.remove(info); |
| 770 | } |
| 771 | |
| 772 | return Command.CONTINUE; |
| 773 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 774 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 775 | } |