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