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