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