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