Naoki Shiota | aea8858 | 2013-11-12 17:58:34 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowprogrammer; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 2 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 4 | import java.util.ArrayDeque; |
| 5 | import java.util.ArrayList; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 6 | import java.util.Collection; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 7 | import java.util.EnumSet; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 8 | import java.util.HashMap; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 9 | import java.util.HashSet; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 10 | import java.util.LinkedList; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 11 | import java.util.List; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 12 | import java.util.Map; |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 13 | import java.util.Set; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 14 | import java.util.concurrent.ExecutionException; |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 15 | import java.util.concurrent.Semaphore; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 16 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 17 | import org.openflow.protocol.*; |
| 18 | import org.openflow.protocol.action.*; |
| 19 | import org.openflow.protocol.factory.BasicFactory; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 22 | |
| 23 | import net.floodlightcontroller.core.FloodlightContext; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 25 | import net.floodlightcontroller.core.IOFMessageListener; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.core.IOFSwitch; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 27 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
| 28 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 29 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 30 | import net.floodlightcontroller.util.MACAddress; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 31 | import net.floodlightcontroller.util.OFMessageDamper; |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.flowmanager.IFlowService; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 33 | import net.onrc.onos.ofcontroller.util.FlowEntryAction; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowEntryAction.*; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 35 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.util.FlowEntryActions; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
| 38 | import net.onrc.onos.ofcontroller.util.FlowEntryUserState; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.util.IPv4Net; |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.util.Pair; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 41 | import net.onrc.onos.ofcontroller.util.Port; |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 44 | * FlowPusher is a implementation of FlowPusherService. |
| 45 | * FlowPusher assigns one message queue instance for each one switch. |
| 46 | * Number of message processing threads is configurable by constructor, and |
| 47 | * one thread can handle multiple message queues. Each queue will be assigned to |
| 48 | * a thread according to hash function defined by getHash(). |
| 49 | * Each processing thread reads messages from queues and sends it to switches |
| 50 | * in round-robin. Processing thread also calculates rate of sending to suppress |
| 51 | * excessive message sending. |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 52 | * @author Naoki Shiota |
| 53 | * |
| 54 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 55 | public class FlowPusher implements IFlowPusherService, IOFMessageListener { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 56 | private final static Logger log = LoggerFactory.getLogger(FlowPusher.class); |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 57 | protected volatile IFlowService flowManager; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 58 | |
| 59 | // NOTE: Below are moved from FlowManager. |
| 60 | // TODO: Values copied from elsewhere (class LearningSwitch). |
| 61 | // The local copy should go away! |
| 62 | // |
| 63 | protected static final int OFMESSAGE_DAMPER_CAPACITY = 50000; // TODO: find sweet spot |
| 64 | protected static final int OFMESSAGE_DAMPER_TIMEOUT = 250; // ms |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 65 | |
Naoki Shiota | 7d0bcfa | 2013-11-13 10:43:33 -0800 | [diff] [blame] | 66 | // Number of messages sent to switch at once |
| 67 | protected static final int MAX_MESSAGE_SEND = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 68 | |
| 69 | public static final short PRIORITY_DEFAULT = 100; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 70 | |
| 71 | public enum QueueState { |
| 72 | READY, |
| 73 | SUSPENDED, |
| 74 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 75 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 76 | /** |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 77 | * SwitchQueue represents message queue attached to a switch. |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 78 | * This consists of queue itself and variables used for limiting sending rate. |
| 79 | * @author Naoki Shiota |
| 80 | * |
| 81 | */ |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 82 | private class SwitchQueue extends ArrayDeque<OFMessage> { |
Naoki Shiota | 991093a | 2013-12-10 14:47:18 -0800 | [diff] [blame] | 83 | private static final long serialVersionUID = 1L; |
| 84 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 85 | QueueState state; |
| 86 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 87 | // Max rate of sending message (bytes/ms). 0 implies no limitation. |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 88 | long max_rate = 0; // 0 indicates no limitation |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 89 | long last_sent_time = 0; |
| 90 | long last_sent_size = 0; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 91 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 92 | // "To be deleted" flag |
| 93 | boolean toBeDeleted = false; |
| 94 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Check if sending rate is within the rate |
| 97 | * @param current Current time |
| 98 | * @return true if within the rate |
| 99 | */ |
| 100 | boolean isSendable(long current) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 101 | if (max_rate == 0) { |
| 102 | // no limitation |
| 103 | return true; |
| 104 | } |
| 105 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 106 | if (current == last_sent_time) { |
| 107 | return false; |
| 108 | } |
| 109 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 110 | // Check if sufficient time (from aspect of rate) elapsed or not. |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 111 | long rate = last_sent_size / (current - last_sent_time); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 112 | return (rate < max_rate); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Log time and size of last sent data. |
| 117 | * @param current Time to be sent. |
| 118 | * @param size Size of sent data (in bytes). |
| 119 | */ |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 120 | void logSentData(long current, long size) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 121 | last_sent_time = current; |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 122 | last_sent_size = size; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 125 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 126 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 127 | private OFMessageDamper messageDamper = null; |
| 128 | private IThreadPoolService threadPool = null; |
Naoki Shiota | cf1acca | 2013-10-31 11:40:32 -0700 | [diff] [blame] | 129 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 130 | private FloodlightContext context = null; |
| 131 | private BasicFactory factory = null; |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 132 | |
| 133 | // Map of threads versus dpid |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 134 | private Map<Long, FlowPusherThread> threadMap = null; |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 135 | // Map of Future objects versus dpid and transaction ID. |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 136 | private Map<Long, Map<Integer, OFBarrierReplyFuture>> |
| 137 | barrierFutures = new HashMap<Long, Map<Integer, OFBarrierReplyFuture>>(); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 138 | |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 139 | private int number_thread = 1; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 140 | |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Main thread that reads messages from queues and sends them to switches. |
| 143 | * @author Naoki Shiota |
| 144 | * |
| 145 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 146 | private class FlowPusherThread extends Thread { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 147 | private Map<IOFSwitch,SwitchQueue> queues |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 148 | = new HashMap<IOFSwitch,SwitchQueue>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 149 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 150 | // reusable latch used for waiting for arrival of message |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 151 | private Semaphore mutex = new Semaphore(0); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 152 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public void run() { |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 155 | this.setName("FlowPusherThread " + this.getId() ); |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 156 | while (true) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 157 | try { |
| 158 | // wait for message pushed to queue |
| 159 | mutex.acquire(); |
| 160 | } catch (InterruptedException e) { |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 161 | // not an error |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 162 | log.debug("FlowPusherThread is interrupted"); |
| 163 | return; |
| 164 | } |
| 165 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 166 | // for safety of concurrent access, copy all key objects |
| 167 | Set<IOFSwitch> keys = new HashSet<IOFSwitch>(queues.size()); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 168 | synchronized (queues) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 169 | for (IOFSwitch sw : queues.keySet()) { |
| 170 | keys.add(sw); |
| 171 | } |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 174 | for (IOFSwitch sw : keys) { |
| 175 | SwitchQueue queue = queues.get(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 176 | |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 177 | // Skip if queue is suspended |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 178 | if (sw == null || queue == null || |
| 179 | queue.state != QueueState.READY) { |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 180 | continue; |
| 181 | } |
| 182 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 183 | synchronized (queue) { |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 184 | processQueue(sw, queue, MAX_MESSAGE_SEND); |
| 185 | if (queue.isEmpty()) { |
| 186 | // remove queue if flagged to be. |
| 187 | if (queue.toBeDeleted) { |
| 188 | synchronized (queues) { |
| 189 | queues.remove(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 192 | } else { |
| 193 | // if some messages remains in queue, latch down |
| 194 | if (mutex.availablePermits() == 0) { |
| 195 | mutex.release(); |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 196 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Read messages from queue and send them to the switch. |
| 205 | * If number of messages excess the limit, stop sending messages. |
| 206 | * @param sw Switch to which messages will be sent. |
| 207 | * @param queue Queue of messages. |
| 208 | * @param max_msg Limitation of number of messages to be sent. If set to 0, |
| 209 | * all messages in queue will be sent. |
| 210 | */ |
| 211 | private void processQueue(IOFSwitch sw, SwitchQueue queue, long max_msg) { |
| 212 | // check sending rate and determine it to be sent or not |
| 213 | long current_time = System.currentTimeMillis(); |
| 214 | long size = 0; |
| 215 | |
| 216 | if (queue.isSendable(current_time)) { |
| 217 | int i = 0; |
| 218 | while (! queue.isEmpty()) { |
| 219 | // Number of messages excess the limit |
| 220 | if (0 < max_msg && max_msg <= i) { |
| 221 | break; |
| 222 | } |
| 223 | ++i; |
| 224 | |
| 225 | OFMessage msg = queue.poll(); |
| 226 | try { |
| 227 | messageDamper.write(sw, msg, context); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 228 | // log.debug("Pusher sends message : {}", msg); |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 229 | size += msg.getLength(); |
| 230 | } catch (IOException e) { |
| 231 | e.printStackTrace(); |
| 232 | log.error("Exception in sending message ({}) : {}", msg, e); |
| 233 | } |
| 234 | } |
| 235 | sw.flush(); |
| 236 | queue.logSentData(current_time, size); |
| 237 | } |
| 238 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 241 | /** |
| 242 | * Initialize object with one thread. |
| 243 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 244 | public FlowPusher() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Initialize object with threads of given number. |
| 249 | * @param number_thread Number of threads to handle messages. |
| 250 | */ |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 251 | public FlowPusher(int number_thread) { |
| 252 | this.number_thread = number_thread; |
| 253 | } |
| 254 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Set parameters needed for sending messages. |
| 257 | * @param context FloodlightContext used for sending messages. |
| 258 | * If null, FlowPusher uses default context. |
| 259 | * @param modContext FloodlightModuleContext used for acquiring |
| 260 | * ThreadPoolService and registering MessageListener. |
| 261 | * @param factory Factory object to create OFMessage objects. |
| 262 | * @param damper Message damper used for sending messages. |
| 263 | * If null, FlowPusher creates its own damper object. |
| 264 | */ |
| 265 | public void init(FloodlightContext context, |
| 266 | FloodlightModuleContext modContext, |
| 267 | BasicFactory factory, |
| 268 | OFMessageDamper damper) { |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 269 | this.context = context; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 270 | this.factory = factory; |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 271 | this.threadPool = modContext.getServiceImpl(IThreadPoolService.class); |
| 272 | IFloodlightProviderService flservice = modContext.getServiceImpl(IFloodlightProviderService.class); |
| 273 | flservice.addOFMessageListener(OFType.BARRIER_REPLY, this); |
Pavlin Radoslavov | da0ab44 | 2013-12-04 14:08:58 -0800 | [diff] [blame] | 274 | flowManager = modContext.getServiceImpl(IFlowService.class); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 275 | |
| 276 | if (damper != null) { |
| 277 | messageDamper = damper; |
| 278 | } else { |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 279 | // use default values |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 280 | messageDamper = new OFMessageDamper(OFMESSAGE_DAMPER_CAPACITY, |
| 281 | EnumSet.of(OFType.FLOW_MOD), |
| 282 | OFMESSAGE_DAMPER_TIMEOUT); |
| 283 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 284 | } |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 285 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 286 | /** |
| 287 | * Begin processing queue. |
| 288 | */ |
| 289 | public void start() { |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 290 | if (factory == null) { |
| 291 | log.error("FlowPusher not yet initialized."); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 295 | threadMap = new HashMap<Long,FlowPusherThread>(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 296 | for (long i = 0; i < number_thread; ++i) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 297 | FlowPusherThread thread = new FlowPusherThread(); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 298 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 299 | threadMap.put(i, thread); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 300 | thread.start(); |
| 301 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 304 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 305 | public boolean suspend(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 306 | SwitchQueue queue = getQueue(sw); |
| 307 | |
| 308 | if (queue == null) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 309 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | synchronized (queue) { |
| 313 | if (queue.state == QueueState.READY) { |
| 314 | queue.state = QueueState.SUSPENDED; |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 315 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 316 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 317 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 321 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 322 | public boolean resume(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 323 | SwitchQueue queue = getQueue(sw); |
| 324 | |
| 325 | if (queue == null) { |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 326 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | synchronized (queue) { |
| 330 | if (queue.state == QueueState.SUSPENDED) { |
| 331 | queue.state = QueueState.READY; |
Naoki Shiota | 0aa947e | 2013-11-27 14:47:35 -0800 | [diff] [blame] | 332 | |
| 333 | // Latch down if queue is not empty |
| 334 | FlowPusherThread thread = getProcess(sw); |
| 335 | if (! queue.isEmpty() && |
| 336 | thread.mutex.availablePermits() == 0) { |
| 337 | thread.mutex.release(); |
| 338 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 339 | return true; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 340 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 341 | return false; |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 344 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 345 | @Override |
Naoki Shiota | 8ee48d5 | 2013-11-11 15:51:17 -0800 | [diff] [blame] | 346 | public boolean isSuspended(IOFSwitch sw) { |
| 347 | SwitchQueue queue = getQueue(sw); |
| 348 | |
| 349 | if (queue == null) { |
| 350 | // TODO Is true suitable for this case? |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | return (queue.state == QueueState.SUSPENDED); |
| 355 | } |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 356 | |
| 357 | /** |
Naoki Shiota | 8739faa | 2013-11-18 17:00:25 -0800 | [diff] [blame] | 358 | * Stop processing queue and exit thread. |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 359 | */ |
| 360 | public void stop() { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 361 | if (threadMap == null) { |
| 362 | return; |
| 363 | } |
| 364 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 365 | for (FlowPusherThread t : threadMap.values()) { |
| 366 | t.interrupt(); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 370 | @Override |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 371 | public void setRate(IOFSwitch sw, long rate) { |
| 372 | SwitchQueue queue = getQueue(sw); |
| 373 | if (queue == null) { |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | if (rate > 0) { |
Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 378 | log.debug("rate for {} is set to {}", sw.getId(), rate); |
Naoki Shiota | 5c8d19f | 2013-11-05 15:52:38 -0800 | [diff] [blame] | 379 | queue.max_rate = rate; |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 380 | } |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 381 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 382 | |
| 383 | @Override |
| 384 | public boolean createQueue(IOFSwitch sw) { |
| 385 | SwitchQueue queue = getQueue(sw); |
| 386 | if (queue != null) { |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | FlowPusherThread proc = getProcess(sw); |
| 391 | queue = new SwitchQueue(); |
| 392 | queue.state = QueueState.READY; |
Naoki Shiota | f03592e | 2013-11-27 11:20:39 -0800 | [diff] [blame] | 393 | synchronized (proc.queues) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 394 | proc.queues.put(sw, queue); |
| 395 | } |
| 396 | |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public boolean deleteQueue(IOFSwitch sw) { |
| 402 | return deleteQueue(sw, false); |
| 403 | } |
| 404 | |
| 405 | @Override |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 406 | public boolean deleteQueue(IOFSwitch sw, boolean forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 407 | FlowPusherThread proc = getProcess(sw); |
| 408 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 409 | if (forceStop) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 410 | synchronized (proc.queues) { |
| 411 | SwitchQueue queue = proc.queues.remove(sw); |
| 412 | if (queue == null) { |
| 413 | return false; |
| 414 | } |
| 415 | } |
| 416 | return true; |
| 417 | } else { |
| 418 | SwitchQueue queue = getQueue(sw); |
| 419 | if (queue == null) { |
| 420 | return false; |
| 421 | } |
| 422 | synchronized (queue) { |
| 423 | queue.toBeDeleted = true; |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | } |
Naoki Shiota | c2a699a | 2013-10-31 15:36:01 -0700 | [diff] [blame] | 428 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 429 | @Override |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 430 | public boolean add(IOFSwitch sw, OFMessage msg) { |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 431 | FlowPusherThread proc = getProcess(sw); |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 432 | SwitchQueue queue = proc.queues.get(sw); |
| 433 | |
Naoki Shiota | b485d41 | 2013-11-26 12:04:19 -0800 | [diff] [blame] | 434 | // create queue at first addition of message |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 435 | if (queue == null) { |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 436 | createQueue(sw); |
| 437 | queue = getQueue(sw); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | synchronized (queue) { |
| 441 | queue.add(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 442 | // log.debug("Message is pushed : {}", msg); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 445 | if (proc.mutex.availablePermits() == 0) { |
| 446 | proc.mutex.release(); |
| 447 | } |
| 448 | |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 449 | return true; |
| 450 | } |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 451 | |
Brian O'Connor | 8c166a7 | 2013-11-14 18:41:48 -0800 | [diff] [blame] | 452 | @Override |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 453 | public void pushFlowEntries( |
| 454 | Collection<Pair<IOFSwitch, FlowEntry>> entries) { |
| 455 | |
| 456 | List<Pair<IOFSwitch, FlowEntry>> pushedEntries = |
| 457 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 458 | |
| 459 | for (Pair<IOFSwitch, FlowEntry> entry : entries) { |
Pavlin Radoslavov | 4535bc1 | 2013-12-05 10:43:49 -0800 | [diff] [blame] | 460 | if (add(entry.first, entry.second)) { |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 461 | pushedEntries.add(entry); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // |
| 466 | // TODO: We should use the OpenFlow Barrier mechanism |
| 467 | // to check for errors, and update the SwitchState |
| 468 | // for a flow entry after the Barrier message is |
| 469 | // is received. |
| 470 | // Only after inform the Flow Manager that the entry is pushed. |
| 471 | // |
| 472 | flowManager.flowEntriesPushedToSwitch(pushedEntries); |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public void pushFlowEntry(IOFSwitch sw, FlowEntry flowEntry) { |
| 477 | Collection<Pair<IOFSwitch, FlowEntry>> entries = |
| 478 | new LinkedList<Pair<IOFSwitch, FlowEntry>>(); |
| 479 | |
| 480 | entries.add(new Pair<IOFSwitch, FlowEntry>(sw, flowEntry)); |
| 481 | pushFlowEntries(entries); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Create a message from FlowEntry and add it to the queue of the switch. |
| 486 | * @param sw Switch to which message is pushed. |
| 487 | * @param flowEntry FlowEntry object used for creating message. |
| 488 | * @return true if message is successfully added to a queue. |
| 489 | */ |
| 490 | private boolean add(IOFSwitch sw, FlowEntry flowEntry) { |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 491 | // |
| 492 | // Create the OpenFlow Flow Modification Entry to push |
| 493 | // |
| 494 | OFFlowMod fm = (OFFlowMod) factory.getMessage(OFType.FLOW_MOD); |
| 495 | long cookie = flowEntry.flowEntryId().value(); |
| 496 | |
| 497 | short flowModCommand = OFFlowMod.OFPFC_ADD; |
| 498 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_ADD) { |
| 499 | flowModCommand = OFFlowMod.OFPFC_ADD; |
| 500 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_MODIFY) { |
| 501 | flowModCommand = OFFlowMod.OFPFC_MODIFY_STRICT; |
| 502 | } else if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) { |
| 503 | flowModCommand = OFFlowMod.OFPFC_DELETE_STRICT; |
| 504 | } else { |
| 505 | // Unknown user state. Ignore the entry |
| 506 | log.debug( |
| 507 | "Flow Entry ignored (FlowEntryId = {}): unknown user state {}", |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 508 | flowEntry.flowEntryId(), |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 509 | flowEntry.flowEntryUserState()); |
| 510 | return false; |
| 511 | } |
| 512 | |
| 513 | // |
| 514 | // Fetch the match conditions. |
| 515 | // |
| 516 | // NOTE: The Flow matching conditions common for all Flow Entries are |
| 517 | // used ONLY if a Flow Entry does NOT have the corresponding matching |
| 518 | // condition set. |
| 519 | // |
| 520 | OFMatch match = new OFMatch(); |
| 521 | match.setWildcards(OFMatch.OFPFW_ALL); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 522 | FlowEntryMatch flowEntryMatch = flowEntry.flowEntryMatch(); |
| 523 | |
| 524 | // Match the Incoming Port |
| 525 | Port matchInPort = flowEntryMatch.inPort(); |
| 526 | if (matchInPort != null) { |
| 527 | match.setInputPort(matchInPort.value()); |
| 528 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_IN_PORT); |
| 529 | } |
| 530 | |
| 531 | // Match the Source MAC address |
| 532 | MACAddress matchSrcMac = flowEntryMatch.srcMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 533 | if (matchSrcMac != null) { |
| 534 | match.setDataLayerSource(matchSrcMac.toString()); |
| 535 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_SRC); |
| 536 | } |
| 537 | |
| 538 | // Match the Destination MAC address |
| 539 | MACAddress matchDstMac = flowEntryMatch.dstMac(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 540 | if (matchDstMac != null) { |
| 541 | match.setDataLayerDestination(matchDstMac.toString()); |
| 542 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 543 | } |
| 544 | |
| 545 | // Match the Ethernet Frame Type |
| 546 | Short matchEthernetFrameType = flowEntryMatch.ethernetFrameType(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 547 | if (matchEthernetFrameType != null) { |
| 548 | match.setDataLayerType(matchEthernetFrameType); |
| 549 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 550 | } |
| 551 | |
| 552 | // Match the VLAN ID |
| 553 | Short matchVlanId = flowEntryMatch.vlanId(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 554 | if (matchVlanId != null) { |
| 555 | match.setDataLayerVirtualLan(matchVlanId); |
| 556 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_VLAN); |
| 557 | } |
| 558 | |
| 559 | // Match the VLAN priority |
| 560 | Byte matchVlanPriority = flowEntryMatch.vlanPriority(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 561 | if (matchVlanPriority != null) { |
| 562 | match.setDataLayerVirtualLanPriorityCodePoint(matchVlanPriority); |
| 563 | match.setWildcards(match.getWildcards() |
| 564 | & ~OFMatch.OFPFW_DL_VLAN_PCP); |
| 565 | } |
| 566 | |
| 567 | // Match the Source IPv4 Network prefix |
| 568 | IPv4Net matchSrcIPv4Net = flowEntryMatch.srcIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 569 | if (matchSrcIPv4Net != null) { |
| 570 | match.setFromCIDR(matchSrcIPv4Net.toString(), OFMatch.STR_NW_SRC); |
| 571 | } |
| 572 | |
| 573 | // Natch the Destination IPv4 Network prefix |
| 574 | IPv4Net matchDstIPv4Net = flowEntryMatch.dstIPv4Net(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 575 | if (matchDstIPv4Net != null) { |
| 576 | match.setFromCIDR(matchDstIPv4Net.toString(), OFMatch.STR_NW_DST); |
| 577 | } |
| 578 | |
| 579 | // Match the IP protocol |
| 580 | Byte matchIpProto = flowEntryMatch.ipProto(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 581 | if (matchIpProto != null) { |
| 582 | match.setNetworkProtocol(matchIpProto); |
| 583 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_PROTO); |
| 584 | } |
| 585 | |
| 586 | // Match the IP ToS (DSCP field, 6 bits) |
| 587 | Byte matchIpToS = flowEntryMatch.ipToS(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 588 | if (matchIpToS != null) { |
| 589 | match.setNetworkTypeOfService(matchIpToS); |
| 590 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_NW_TOS); |
| 591 | } |
| 592 | |
| 593 | // Match the Source TCP/UDP port |
| 594 | Short matchSrcTcpUdpPort = flowEntryMatch.srcTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 595 | if (matchSrcTcpUdpPort != null) { |
| 596 | match.setTransportSource(matchSrcTcpUdpPort); |
| 597 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 598 | } |
| 599 | |
| 600 | // Match the Destination TCP/UDP port |
| 601 | Short matchDstTcpUdpPort = flowEntryMatch.dstTcpUdpPort(); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 602 | if (matchDstTcpUdpPort != null) { |
| 603 | match.setTransportDestination(matchDstTcpUdpPort); |
| 604 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 605 | } |
| 606 | |
| 607 | // |
| 608 | // Fetch the actions |
| 609 | // |
| 610 | Short actionOutputPort = null; |
| 611 | List<OFAction> openFlowActions = new ArrayList<OFAction>(); |
| 612 | int actionsLen = 0; |
| 613 | FlowEntryActions flowEntryActions = flowEntry.flowEntryActions(); |
| 614 | // |
| 615 | for (FlowEntryAction action : flowEntryActions.actions()) { |
| 616 | ActionOutput actionOutput = action.actionOutput(); |
| 617 | ActionSetVlanId actionSetVlanId = action.actionSetVlanId(); |
| 618 | ActionSetVlanPriority actionSetVlanPriority = action |
| 619 | .actionSetVlanPriority(); |
| 620 | ActionStripVlan actionStripVlan = action.actionStripVlan(); |
| 621 | ActionSetEthernetAddr actionSetEthernetSrcAddr = action |
| 622 | .actionSetEthernetSrcAddr(); |
| 623 | ActionSetEthernetAddr actionSetEthernetDstAddr = action |
| 624 | .actionSetEthernetDstAddr(); |
| 625 | ActionSetIPv4Addr actionSetIPv4SrcAddr = action |
| 626 | .actionSetIPv4SrcAddr(); |
| 627 | ActionSetIPv4Addr actionSetIPv4DstAddr = action |
| 628 | .actionSetIPv4DstAddr(); |
| 629 | ActionSetIpToS actionSetIpToS = action.actionSetIpToS(); |
| 630 | ActionSetTcpUdpPort actionSetTcpUdpSrcPort = action |
| 631 | .actionSetTcpUdpSrcPort(); |
| 632 | ActionSetTcpUdpPort actionSetTcpUdpDstPort = action |
| 633 | .actionSetTcpUdpDstPort(); |
| 634 | ActionEnqueue actionEnqueue = action.actionEnqueue(); |
| 635 | |
| 636 | if (actionOutput != null) { |
| 637 | actionOutputPort = actionOutput.port().value(); |
| 638 | // XXX: The max length is hard-coded for now |
| 639 | OFActionOutput ofa = new OFActionOutput(actionOutput.port() |
| 640 | .value(), (short) 0xffff); |
| 641 | openFlowActions.add(ofa); |
| 642 | actionsLen += ofa.getLength(); |
| 643 | } |
| 644 | |
| 645 | if (actionSetVlanId != null) { |
| 646 | OFActionVirtualLanIdentifier ofa = new OFActionVirtualLanIdentifier( |
| 647 | actionSetVlanId.vlanId()); |
| 648 | openFlowActions.add(ofa); |
| 649 | actionsLen += ofa.getLength(); |
| 650 | } |
| 651 | |
| 652 | if (actionSetVlanPriority != null) { |
| 653 | OFActionVirtualLanPriorityCodePoint ofa = new OFActionVirtualLanPriorityCodePoint( |
| 654 | actionSetVlanPriority.vlanPriority()); |
| 655 | openFlowActions.add(ofa); |
| 656 | actionsLen += ofa.getLength(); |
| 657 | } |
| 658 | |
| 659 | if (actionStripVlan != null) { |
| 660 | if (actionStripVlan.stripVlan() == true) { |
| 661 | OFActionStripVirtualLan ofa = new OFActionStripVirtualLan(); |
| 662 | openFlowActions.add(ofa); |
| 663 | actionsLen += ofa.getLength(); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (actionSetEthernetSrcAddr != null) { |
| 668 | OFActionDataLayerSource ofa = new OFActionDataLayerSource( |
| 669 | actionSetEthernetSrcAddr.addr().toBytes()); |
| 670 | openFlowActions.add(ofa); |
| 671 | actionsLen += ofa.getLength(); |
| 672 | } |
| 673 | |
| 674 | if (actionSetEthernetDstAddr != null) { |
| 675 | OFActionDataLayerDestination ofa = new OFActionDataLayerDestination( |
| 676 | actionSetEthernetDstAddr.addr().toBytes()); |
| 677 | openFlowActions.add(ofa); |
| 678 | actionsLen += ofa.getLength(); |
| 679 | } |
| 680 | |
| 681 | if (actionSetIPv4SrcAddr != null) { |
| 682 | OFActionNetworkLayerSource ofa = new OFActionNetworkLayerSource( |
| 683 | actionSetIPv4SrcAddr.addr().value()); |
| 684 | openFlowActions.add(ofa); |
| 685 | actionsLen += ofa.getLength(); |
| 686 | } |
| 687 | |
| 688 | if (actionSetIPv4DstAddr != null) { |
| 689 | OFActionNetworkLayerDestination ofa = new OFActionNetworkLayerDestination( |
| 690 | actionSetIPv4DstAddr.addr().value()); |
| 691 | openFlowActions.add(ofa); |
| 692 | actionsLen += ofa.getLength(); |
| 693 | } |
| 694 | |
| 695 | if (actionSetIpToS != null) { |
| 696 | OFActionNetworkTypeOfService ofa = new OFActionNetworkTypeOfService( |
| 697 | actionSetIpToS.ipToS()); |
| 698 | openFlowActions.add(ofa); |
| 699 | actionsLen += ofa.getLength(); |
| 700 | } |
| 701 | |
| 702 | if (actionSetTcpUdpSrcPort != null) { |
| 703 | OFActionTransportLayerSource ofa = new OFActionTransportLayerSource( |
| 704 | actionSetTcpUdpSrcPort.port()); |
| 705 | openFlowActions.add(ofa); |
| 706 | actionsLen += ofa.getLength(); |
| 707 | } |
| 708 | |
| 709 | if (actionSetTcpUdpDstPort != null) { |
| 710 | OFActionTransportLayerDestination ofa = new OFActionTransportLayerDestination( |
| 711 | actionSetTcpUdpDstPort.port()); |
| 712 | openFlowActions.add(ofa); |
| 713 | actionsLen += ofa.getLength(); |
| 714 | } |
| 715 | |
| 716 | if (actionEnqueue != null) { |
| 717 | OFActionEnqueue ofa = new OFActionEnqueue(actionEnqueue.port() |
| 718 | .value(), actionEnqueue.queueId()); |
| 719 | openFlowActions.add(ofa); |
| 720 | actionsLen += ofa.getLength(); |
| 721 | } |
| 722 | } |
| 723 | |
Pavlin Radoslavov | 1fe06a2 | 2013-12-10 14:12:23 -0800 | [diff] [blame] | 724 | fm.setIdleTimeout((short)flowEntry.idleTimeout()) |
| 725 | .setHardTimeout((short)flowEntry.hardTimeout()) |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 726 | .setPriority(PRIORITY_DEFAULT) |
| 727 | .setBufferId(OFPacketOut.BUFFER_ID_NONE).setCookie(cookie) |
| 728 | .setCommand(flowModCommand).setMatch(match) |
| 729 | .setActions(openFlowActions) |
| 730 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + actionsLen); |
| 731 | fm.setOutPort(OFPort.OFPP_NONE.getValue()); |
| 732 | if ((flowModCommand == OFFlowMod.OFPFC_DELETE) |
| 733 | || (flowModCommand == OFFlowMod.OFPFC_DELETE_STRICT)) { |
| 734 | if (actionOutputPort != null) |
| 735 | fm.setOutPort(actionOutputPort); |
| 736 | } |
| 737 | |
| 738 | // |
Pavlin Radoslavov | b95379c | 2013-12-13 11:09:20 -0800 | [diff] [blame] | 739 | // Set the OFPFF_SEND_FLOW_REM flag if the Flow Entry is not |
| 740 | // permanent. |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 741 | // |
Pavlin Radoslavov | b95379c | 2013-12-13 11:09:20 -0800 | [diff] [blame] | 742 | if ((flowEntry.idleTimeout() != 0) || |
| 743 | (flowEntry.hardTimeout() != 0)) { |
| 744 | fm.setFlags(OFFlowMod.OFPFF_SEND_FLOW_REM); |
| 745 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 746 | |
| 747 | // |
| 748 | // Write the message to the switch |
| 749 | // |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 750 | log.debug("Installing flow entry {} into switch DPID: {} flowEntryId: {} srcMac: {} dstMac: {} inPort: {} outPort: {}" |
| 751 | , flowEntry.flowEntryUserState() |
| 752 | , sw.getStringId() |
| 753 | , flowEntry.flowEntryId() |
| 754 | , matchSrcMac |
| 755 | , matchDstMac |
| 756 | , matchInPort |
| 757 | , actionOutputPort |
| 758 | ); |
| 759 | |
Pavlin Radoslavov | ab3f886 | 2013-12-04 18:35:53 -0800 | [diff] [blame] | 760 | return add(sw, fm); |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 761 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 762 | |
| 763 | @Override |
| 764 | public OFBarrierReply barrier(IOFSwitch sw) { |
| 765 | OFMessageFuture<OFBarrierReply> future = barrierAsync(sw); |
| 766 | if (future == null) { |
| 767 | return null; |
| 768 | } |
| 769 | |
| 770 | try { |
| 771 | return future.get(); |
| 772 | } catch (InterruptedException e) { |
| 773 | e.printStackTrace(); |
| 774 | log.error("InterruptedException: {}", e); |
| 775 | return null; |
| 776 | } catch (ExecutionException e) { |
| 777 | e.printStackTrace(); |
| 778 | log.error("ExecutionException: {}", e); |
| 779 | return null; |
| 780 | } |
| 781 | } |
Naoki Shiota | 2e2fc2b | 2013-11-12 11:21:36 -0800 | [diff] [blame] | 782 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 783 | @Override |
| 784 | public OFBarrierReplyFuture barrierAsync(IOFSwitch sw) { |
| 785 | // TODO creation of message and future should be moved to OFSwitchImpl |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 786 | |
| 787 | if (sw == null) { |
| 788 | return null; |
| 789 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 790 | |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 791 | OFBarrierRequest msg = (OFBarrierRequest) factory.getMessage(OFType.BARRIER_REQUEST); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 792 | msg.setXid(sw.getNextTransactionId()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 793 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 794 | OFBarrierReplyFuture future = new OFBarrierReplyFuture(threadPool, sw, msg.getXid()); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 795 | synchronized (barrierFutures) { |
| 796 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 797 | if (map == null) { |
| 798 | map = new HashMap<Integer,OFBarrierReplyFuture>(); |
| 799 | barrierFutures.put(sw.getId(), map); |
| 800 | } |
| 801 | map.put(msg.getXid(), future); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 804 | add(sw, msg); |
| 805 | |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 806 | return future; |
| 807 | } |
| 808 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 809 | /** |
| 810 | * Get a queue attached to a switch. |
| 811 | * @param sw Switch object |
| 812 | * @return Queue object |
| 813 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 814 | protected SwitchQueue getQueue(IOFSwitch sw) { |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 815 | if (sw == null) { |
| 816 | return null; |
| 817 | } |
| 818 | |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 819 | return getProcess(sw).queues.get(sw); |
| 820 | } |
| 821 | |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 822 | /** |
| 823 | * Get a hash value correspondent to a switch. |
| 824 | * @param sw Switch object |
| 825 | * @return Hash value |
| 826 | */ |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 827 | protected long getHash(IOFSwitch sw) { |
| 828 | // This code assumes DPID is sequentially assigned. |
| 829 | // TODO consider equalization algorithm |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 830 | return sw.getId() % number_thread; |
| 831 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 832 | |
| 833 | /** |
| 834 | * Get a Thread object which processes the queue attached to a switch. |
| 835 | * @param sw Switch object |
| 836 | * @return Thread object |
| 837 | */ |
Naoki Shiota | 81dbe30 | 2013-11-21 15:35:38 -0800 | [diff] [blame] | 838 | protected FlowPusherThread getProcess(IOFSwitch sw) { |
Naoki Shiota | f0cddbf | 2013-11-12 14:59:43 -0800 | [diff] [blame] | 839 | long hash = getHash(sw); |
| 840 | |
| 841 | return threadMap.get(hash); |
Naoki Shiota | 7d0cf27 | 2013-11-05 10:18:12 -0800 | [diff] [blame] | 842 | } |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 843 | |
| 844 | @Override |
| 845 | public String getName() { |
| 846 | return "flowpusher"; |
| 847 | } |
| 848 | |
| 849 | @Override |
| 850 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | @Override |
| 860 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 861 | Map<Integer,OFBarrierReplyFuture> map = barrierFutures.get(sw.getId()); |
| 862 | if (map == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 863 | log.debug("null map for {} : {}", sw.getId(), barrierFutures); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 864 | return Command.CONTINUE; |
| 865 | } |
| 866 | |
| 867 | OFBarrierReplyFuture future = map.get(msg.getXid()); |
| 868 | if (future == null) { |
Naoki Shiota | c2ec559 | 2013-11-27 12:10:33 -0800 | [diff] [blame] | 869 | log.debug("null future for {} : {}", msg.getXid(), map); |
Naoki Shiota | c1601d3 | 2013-11-20 10:47:34 -0800 | [diff] [blame] | 870 | return Command.CONTINUE; |
| 871 | } |
| 872 | |
| 873 | log.debug("Received BARRIER_REPLY : {}", msg); |
| 874 | future.deliverFuture(sw, msg); |
| 875 | |
| 876 | return Command.CONTINUE; |
| 877 | } |
Naoki Shiota | e319973 | 2013-11-25 16:14:43 -0800 | [diff] [blame] | 878 | |
Naoki Shiota | ed4eb5e | 2013-10-31 10:55:32 -0700 | [diff] [blame] | 879 | } |