blob: fc30f127fda87d68a01d5b500333d46c0b228f07 [file] [log] [blame]
Jonathan Hart0961fe82014-04-03 09:56:25 -07001package net.onrc.onos.apps.forwarding;
Jonathan Hart1caaa932013-11-04 15:28:28 -08002
Jonathan Hart41d1e912013-11-24 16:50:25 -08003import java.util.ArrayList;
Jonathan Harte93aed42013-12-05 18:39:50 -08004import java.util.Collection;
Jonathan Hartd33a6cf2013-12-10 14:29:08 -08005import java.util.HashMap;
Jonathan Hart1caaa932013-11-04 15:28:28 -08006import java.util.Iterator;
TeruUf9111652014-05-14 23:10:35 -07007import java.util.LinkedList;
Jonathan Hart41d1e912013-11-24 16:50:25 -08008import java.util.List;
Jonathan Harte93aed42013-12-05 18:39:50 -08009import java.util.Map;
TeruU417fe022014-02-04 12:59:30 -080010import java.util.concurrent.Executors;
11import java.util.concurrent.ScheduledExecutorService;
12import java.util.concurrent.TimeUnit;
Jonathan Hart1caaa932013-11-04 15:28:28 -080013
Jonathan Harte93aed42013-12-05 18:39:50 -080014import net.floodlightcontroller.core.module.FloodlightModuleContext;
15import net.floodlightcontroller.core.module.IFloodlightModule;
16import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080017import net.floodlightcontroller.util.MACAddress;
Jonathan Hartf5bd2582014-04-09 17:43:41 -070018import net.onrc.onos.api.packet.IPacketListener;
19import net.onrc.onos.api.packet.IPacketService;
Jonathan Hart0961fe82014-04-03 09:56:25 -070020import net.onrc.onos.apps.proxyarp.IProxyArpService;
Jonathan Hart23701d12014-04-03 10:45:48 -070021import net.onrc.onos.core.devicemanager.IOnosDeviceService;
Jonathan Hartaa380972014-04-03 10:24:46 -070022import net.onrc.onos.core.intent.Intent;
Jonathan Harta99ec672014-04-03 11:30:34 -070023import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hartaa380972014-04-03 10:24:46 -070024import net.onrc.onos.core.intent.IntentMap;
TeruUf9111652014-05-14 23:10:35 -070025import net.onrc.onos.core.intent.IntentMap.ChangedEvent;
26import net.onrc.onos.core.intent.IntentMap.ChangedListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070027import net.onrc.onos.core.intent.IntentOperation;
28import net.onrc.onos.core.intent.IntentOperationList;
29import net.onrc.onos.core.intent.PathIntent;
30import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hartaa380972014-04-03 10:24:46 -070031import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070032import net.onrc.onos.core.packet.Ethernet;
33import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart472062d2014-04-03 10:56:48 -070034import net.onrc.onos.core.topology.Device;
Jonathan Harte37e4e22014-05-13 19:12:02 -070035import net.onrc.onos.core.topology.ITopologyService;
Jonathan Hart472062d2014-04-03 10:56:48 -070036import net.onrc.onos.core.topology.LinkEvent;
Jonathan Hartf5bd2582014-04-09 17:43:41 -070037import net.onrc.onos.core.topology.Port;
Jonathan Hart472062d2014-04-03 10:56:48 -070038import net.onrc.onos.core.topology.Switch;
TeruUf9111652014-05-14 23:10:35 -070039import net.onrc.onos.core.topology.Topology;
Jonathan Hart23701d12014-04-03 10:45:48 -070040import net.onrc.onos.core.util.FlowPath;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070041import net.onrc.onos.core.util.PortNumber;
Jonathan Hart23701d12014-04-03 10:45:48 -070042import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080043
Jonathan Hart1caaa932013-11-04 15:28:28 -080044import org.openflow.util.HexString;
45import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
Jonathan Hartd857ad62013-12-14 18:08:17 -080048import com.google.common.collect.LinkedListMultimap;
49import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080050
Jonathan Hartf5bd2582014-04-09 17:43:41 -070051public class Forwarding implements /*IOFMessageListener,*/ IFloodlightModule,
TeruUf9111652014-05-14 23:10:35 -070052 IForwardingService, IPacketListener, ChangedListener {
Ray Milkeyec838942014-04-09 11:28:43 -070053 private static final Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU7feef8a2014-04-03 00:15:49 -070054
Ray Milkey2476cac2014-04-08 11:03:21 -070055 private static final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
Ray Milkeyec838942014-04-09 11:28:43 -070056 private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
TeruU30c0c932014-05-15 16:47:41 -070057 private static final int SRC_SWITCH_TIMEOUT_ADJUST_SECOND = 2;
58 private static final int DEFAULT_IDLE_TIMEOUT = 5;
59 private int idleTimeout = DEFAULT_IDLE_TIMEOUT;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070060
Jonathan Hartc00f5c22014-06-10 15:14:40 -070061 private static final ScheduledExecutorService EXECUTOR_SERVICE =
62 Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Harte93aed42013-12-05 18:39:50 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 private final String callerId = "Forwarding";
Jonathan Harte93aed42013-12-05 18:39:50 -080065
TeruU435df322014-06-16 23:45:13 -070066 private final HighLevelIntentChangedHandler highLevelIntentChangedHandler =
67 new HighLevelIntentChangedHandler();
68
Jonathan Hartf5bd2582014-04-09 17:43:41 -070069 private IPacketService packetService;
Ray Milkey269ffb92014-04-03 14:43:30 -070070 private IControllerRegistryService controllerRegistryService;
71
Jonathan Harte37e4e22014-05-13 19:12:02 -070072 private ITopologyService topologyService;
73 private Topology topology;
Ray Milkey269ffb92014-04-03 14:43:30 -070074 private IPathCalcRuntimeService pathRuntime;
TeruU9e530662014-05-18 11:49:37 -070075 private IntentMap pathIntentMap;
76 private IntentMap highLevelIntentMap;
Ray Milkey269ffb92014-04-03 14:43:30 -070077
78 // TODO it seems there is a Guava collection that will time out entries.
79 // We should see if this will work here.
80 private Map<Path, PushedFlow> pendingFlows;
81 private ListMultimap<String, PacketToPush> waitingPackets;
82
83 private final Object lock = new Object();
84
TeruU435df322014-06-16 23:45:13 -070085 private class HighLevelIntentChangedHandler implements ChangedListener {
86
87 @Override
88 public void intentsChange(LinkedList<ChangedEvent> events) {
89 for (ChangedEvent event : events) {
90 ShortestPathIntent spfIntent = null;
91 if (event.intent instanceof ShortestPathIntent) {
92 spfIntent = (ShortestPathIntent) event.intent;
93 log.trace("ShortestPathIntent {}", spfIntent);
94 }
95
96 if (spfIntent == null) {
97 log.trace("ShortestPathIntent is null. Skip.");
98 continue;
99 }
100
101 switch(event.eventType) {
102 case ADDED:
103 break;
104 case REMOVED:
105 break;
106 case STATE_CHANGED:
107 if (spfIntent.getState() == IntentState.INST_NACK) {
108 flowRemoved(spfIntent);
109 }
110 break;
111 default:
112 break;
113 }
114 }
115 }
116
117 }
118
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700119 private static class PacketToPush {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700120 public final Ethernet eth;
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 public final long dpid;
122
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700123 public PacketToPush(Ethernet eth, long dpid) {
124 this.eth = eth;
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 this.dpid = dpid;
126 }
127 }
128
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700129 private static class PushedFlow {
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 public final String intentId;
131 public boolean installed = false;
132 public short firstOutPort;
133
134 public PushedFlow(String flowId) {
135 this.intentId = flowId;
136 }
137 }
138
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700139 private static final class Path {
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 public final MACAddress srcMac;
141 public final MACAddress dstMac;
142
143 public Path(MACAddress srcMac, MACAddress dstMac) {
144 this.srcMac = srcMac;
145 this.dstMac = dstMac;
146 }
147
148 @Override
149 public boolean equals(Object other) {
150 if (!(other instanceof Path)) {
151 return false;
152 }
153
154 Path otherPath = (Path) other;
155 return srcMac.equals(otherPath.srcMac) &&
156 dstMac.equals(otherPath.dstMac);
157 }
158
159 @Override
160 public int hashCode() {
161 int hash = 17;
162 hash = 31 * hash + srcMac.hashCode();
163 hash = 31 * hash + dstMac.hashCode();
164 return hash;
165 }
166
167 @Override
168 public String toString() {
169 return "(" + srcMac + ") => (" + dstMac + ")";
170 }
171 }
172
173 @Override
174 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
175 List<Class<? extends IFloodlightService>> services =
176 new ArrayList<Class<? extends IFloodlightService>>(1);
177 services.add(IForwardingService.class);
178 return services;
179 }
180
181 @Override
182 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
183 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
184 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
185 impls.put(IForwardingService.class, this);
186 return impls;
187 }
188
189 @Override
190 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
191 List<Class<? extends IFloodlightService>> dependencies =
192 new ArrayList<Class<? extends IFloodlightService>>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700193 dependencies.add(IControllerRegistryService.class);
194 dependencies.add(IOnosDeviceService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700195 dependencies.add(ITopologyService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 dependencies.add(IPathCalcRuntimeService.class);
197 // We don't use the IProxyArpService directly, but reactive forwarding
198 // requires it to be loaded and answering ARP requests
199 dependencies.add(IProxyArpService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700200 dependencies.add(IPacketService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700201 return dependencies;
202 }
203
204 @Override
205 public void init(FloodlightModuleContext context) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700206 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700207 topologyService = context.getServiceImpl(ITopologyService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700208 pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700209 packetService = context.getServiceImpl(IPacketService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700210
Ray Milkey269ffb92014-04-03 14:43:30 -0700211 pendingFlows = new HashMap<Path, PushedFlow>();
212 waitingPackets = LinkedListMultimap.create();
213 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800214
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 @Override
216 public void startUp(FloodlightModuleContext context) {
TeruU30c0c932014-05-15 16:47:41 -0700217 Map<String, String> configOptions = context.getConfigParams(this);
218
219 try {
220 if (Integer.parseInt(configOptions.get("idletimeout")) > 0) {
221 idleTimeout = Integer.parseInt(configOptions.get("idletimeout"));
222 log.info("idle_timeout for Forwarding is set to {}.", idleTimeout);
223 } else {
224 log.info("idle_timeout for Forwarding is less than 0. Use default {}.", idleTimeout);
225 }
226 } catch (NumberFormatException e) {
227 log.info("idle_timeout related config options were not set. Use default.");
228 }
229
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700230 packetService.registerPacketListener(this);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800231
Jonathan Harte37e4e22014-05-13 19:12:02 -0700232 topology = topologyService.getTopology();
TeruU9e530662014-05-18 11:49:37 -0700233 highLevelIntentMap = pathRuntime.getHighLevelIntents();
TeruU435df322014-06-16 23:45:13 -0700234 highLevelIntentMap.addChangeListener(highLevelIntentChangedHandler);
TeruU9e530662014-05-18 11:49:37 -0700235 pathIntentMap = pathRuntime.getPathIntents();
236 pathIntentMap.addChangeListener(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700237 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800238
Ray Milkey269ffb92014-04-03 14:43:30 -0700239 @Override
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700240 public void receive(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700241 if (log.isTraceEnabled()) {
242 log.trace("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber());
243 }
TeruU417fe022014-02-04 12:59:30 -0800244
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700245 if (eth.getEtherType() != Ethernet.TYPE_IPV4) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700246 // Only handle IPv4 packets right now
247 return;
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 }
TeruU417fe022014-02-04 12:59:30 -0800249
Ray Milkey269ffb92014-04-03 14:43:30 -0700250 if (eth.isBroadcast() || eth.isMulticast()) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700251 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700252 } else {
253 // Unicast
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700254 handlePacketIn(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700255 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700256 }
TeruU417fe022014-02-04 12:59:30 -0800257
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700258 private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700259 if (log.isTraceEnabled()) {
260 log.trace("Sending broadcast packet to other ONOS instances");
261 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800262
Jonathan Harte3702f22014-04-29 02:56:56 -0700263 packetService.broadcastPacketOutEdge(eth,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700264 new SwitchPort(sw.getDpid(), inPort.getNumber()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700265 }
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700266
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700267 private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700268 if (log.isTraceEnabled()) {
269 log.trace("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber());
270 }
TeruU7feef8a2014-04-03 00:15:49 -0700271
Ray Milkey269ffb92014-04-03 14:43:30 -0700272 String destinationMac =
273 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart0444d932014-01-22 15:06:17 -0800274
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700275 // FIXME getDeviceByMac() is a blocking call, so it may be better way
276 // to handle it to avoid the condition.
277 Device deviceObject = topology.getDeviceByMac(
278 MACAddress.valueOf(destinationMac));
TeruU6464af02014-02-06 21:38:45 -0800279
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 if (deviceObject == null) {
281 log.debug("No device entry found for {}",
282 destinationMac);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800283
Ray Milkey269ffb92014-04-03 14:43:30 -0700284 //Device is not in the DB, so wait it until the device is added.
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700285 EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, inPort, eth),
286 SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
Ray Milkey269ffb92014-04-03 14:43:30 -0700287 return;
288 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800289
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700290 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700291 }
TeruU417fe022014-02-04 12:59:30 -0800292
Ray Milkey269ffb92014-04-03 14:43:30 -0700293 private class WaitDeviceArp implements Runnable {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700294 Switch sw;
295 Port inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700296 Ethernet eth;
TeruU417fe022014-02-04 12:59:30 -0800297
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700298 public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700299 super();
300 this.sw = sw;
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700301 this.inPort = inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700302 this.eth = eth;
303 }
TeruU417fe022014-02-04 12:59:30 -0800304
Ray Milkey269ffb92014-04-03 14:43:30 -0700305 @Override
306 public void run() {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700307 Device deviceObject = topology.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700308 if (deviceObject == null) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700309 log.debug("wait {}ms and device was not found. " +
310 "Send broadcast packet and the thread finish.",
311 SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700312 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700313 return;
314 }
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700315 log.debug("wait {}ms and device {} was found, continue",
316 SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700317 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700318 }
319 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800320
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700321 private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Device deviceObject) {
TeruU7feef8a2014-04-03 00:15:49 -0700322
TeruU9e530662014-05-18 11:49:37 -0700323 log.trace("Start continuehandlePacketIn");
TeruU7feef8a2014-04-03 00:15:49 -0700324
Ray Milkey269ffb92014-04-03 14:43:30 -0700325 //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
326 Iterator<net.onrc.onos.core.topology.Port> ports = deviceObject.getAttachmentPoints().iterator();
327 if (!ports.hasNext()) {
328 log.debug("No attachment point found for device {} - broadcasting packet",
329 deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700330 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700331 return;
332 }
TeruU7feef8a2014-04-03 00:15:49 -0700333
Ray Milkey269ffb92014-04-03 14:43:30 -0700334 //This code assumes the device has only one port. It should be problem.
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700335 Port destinationPort = ports.next();
336 short destinationPortNum = destinationPort.getNumber().value();
337 Switch destinationSw = destinationPort.getSwitch();
338 long destinationDpid = destinationSw.getDpid().value();
Ray Milkey269ffb92014-04-03 14:43:30 -0700339
Ray Milkey269ffb92014-04-03 14:43:30 -0700340 SwitchPort srcSwitchPort = new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700341 sw.getDpid(),
342 inPort.getNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700343 SwitchPort dstSwitchPort = new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700344 destinationSw.getDpid(),
345 destinationPort.getNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700346
347 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
348 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
TeruU9e530662014-05-18 11:49:37 -0700349 Path pathspec = new Path(srcMacAddress, dstMacAddress);
350 IntentOperationList operations = new IntentOperationList();
Ray Milkey269ffb92014-04-03 14:43:30 -0700351
352 synchronized (lock) {
353 //TODO check concurrency
TeruU9e530662014-05-18 11:49:37 -0700354
Ray Milkey269ffb92014-04-03 14:43:30 -0700355 PushedFlow existingFlow = pendingFlows.get(pathspec);
356
357 //A path is installed side by side to reduce a path timeout and a wrong state.
358 if (existingFlow != null) {
359 // We've already start to install a flow for this pair of MAC addresses
360 if (log.isDebugEnabled()) {
361 log.debug("Found existing the same pathspec {}, intent ID is {}",
362 pathspec,
363 existingFlow.intentId);
364 }
365
Ray Milkey269ffb92014-04-03 14:43:30 -0700366 // Find the correct port here. We just assume the PI is from
367 // the first hop switch, but this is definitely not always
368 // the case. We'll have to retrieve the flow from HZ every time
369 // because it could change (be rerouted) sometimes.
370 if (existingFlow.installed) {
371 // Flow has been sent to the switches so it is safe to
372 // send a packet out now
373
TeruU9e530662014-05-18 11:49:37 -0700374 // TODO Here highLevelIntentMap and pathIntentMap would be problem,
375 // because it doesn't have global information as of May 2014.
376 // However usually these lines here is used when we got packet-in and this class think
377 // the path for the packet is installed already, so it is pretty rare.
378 // I will leave it for now, and will work in the next step.
379 Intent highLevelIntent = highLevelIntentMap.getIntent(existingFlow.intentId);
380 if (highLevelIntent == null) {
381 log.debug("Intent ID {} is null in HighLevelIntentMap. return.", existingFlow.intentId);
382 return;
383 }
384
385 if (highLevelIntent.getState() != IntentState.INST_ACK) {
386 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
387 return;
388 }
389
390 ShortestPathIntent spfIntent = null;
391 if (highLevelIntent instanceof ShortestPathIntent) {
392 spfIntent = (ShortestPathIntent) highLevelIntent;
Ray Milkey269ffb92014-04-03 14:43:30 -0700393 } else {
TeruUf9111652014-05-14 23:10:35 -0700394 log.debug("Intent ID {} is not PathIntent or null. return.", existingFlow.intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700395 return;
396 }
397
TeruU9e530662014-05-18 11:49:37 -0700398 PathIntent pathIntent = (PathIntent) pathIntentMap.getIntent(spfIntent.getPathIntentId());
399 if (pathIntent == null) {
400 log.debug("PathIntent ID {} is null in PathIntentMap. return.", existingFlow.intentId);
401 return;
402 }
403
404 if (pathIntent.getState() != IntentState.INST_ACK) {
405 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
406 return;
407 }
408
409 boolean isflowEntryForThisSwitch = false;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700410 net.onrc.onos.core.intent.Path path = pathIntent.getPath();
TeruU9e530662014-05-18 11:49:37 -0700411 long outPort = -1;
412
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700413 if (spfIntent.getDstSwitchDpid() == sw.getDpid().value()) {
TeruU9e530662014-05-18 11:49:37 -0700414 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
415 isflowEntryForThisSwitch = true;
416 outPort = spfIntent.getDstPortNumber();
417 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700418
Ray Milkey7f1567c2014-04-08 13:53:32 -0700419 for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) {
Ray Milkey149693c2014-05-20 14:58:53 -0700420 LinkEvent le = i.next();
TeruU9e530662014-05-18 11:49:37 -0700421
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700422 if (le.getSrc().getDpid().equals(sw.getDpid())) {
TeruU9e530662014-05-18 11:49:37 -0700423 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700424 isflowEntryForThisSwitch = true;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700425 outPort = le.getSrc().getNumber().value();
Ray Milkey269ffb92014-04-03 14:43:30 -0700426 break;
427 }
428 }
429
Ray Milkey6c4f2fe2014-04-11 09:47:23 -0700430 if (!isflowEntryForThisSwitch) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700431 // If we don't find a flow entry for that switch, then we're
432 // in the middle of a rerouting (or something's gone wrong).
433 // This packet will be dropped as a victim of the rerouting.
434 log.debug("Dropping packet on flow {} between {}-{}",
435 existingFlow.intentId,
436 srcMacAddress, dstMacAddress);
437 } else {
TeruU9e530662014-05-18 11:49:37 -0700438 if (outPort < 0) {
439 outPort = existingFlow.firstOutPort;
440 }
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700441
TeruU9e530662014-05-18 11:49:37 -0700442 log.debug("Sending packet out from sw {}, outport{}", sw.getDpid(), outPort);
Jonathan Harte3702f22014-04-29 02:56:56 -0700443 packetService.sendPacket(eth, new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700444 sw.getDpid(), new PortNumber((short) outPort)));
Ray Milkey269ffb92014-04-03 14:43:30 -0700445 }
446 } else {
447 // Flow path has not yet been installed to switches so save the
448 // packet out for later
TeruU9e530662014-05-18 11:49:37 -0700449 log.trace("Put a packet into the waiting list. flowId {}", existingFlow.intentId);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700450 waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid().value()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700451 }
452 return;
453 }
454
TeruU9e530662014-05-18 11:49:37 -0700455 String intentId = Long.toString(controllerRegistryService.getNextUniqueId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700456 ShortestPathIntent intent = new ShortestPathIntent(intentId,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700457 sw.getDpid().value(), inPort.getNumber().value(), srcMacAddress.toLong(),
458 destinationDpid, destinationPortNum, dstMacAddress.toLong());
Ray Milkeyff735142014-05-22 19:06:02 -0700459
TeruU30c0c932014-05-15 16:47:41 -0700460 intent.setIdleTimeout(idleTimeout + SRC_SWITCH_TIMEOUT_ADJUST_SECOND);
461 intent.setFirstSwitchIdleTimeout(idleTimeout);
Ray Milkey269ffb92014-04-03 14:43:30 -0700462 IntentOperation.Operator operator = IntentOperation.Operator.ADD;
463 operations.add(operator, intent);
TeruU9e530662014-05-18 11:49:37 -0700464 log.debug("Adding new flow between {} at {} and {} at {}",
465 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Ray Milkey269ffb92014-04-03 14:43:30 -0700466
TeruU9e530662014-05-18 11:49:37 -0700467 // Add to waiting lists
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700468 waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid().value()));
TeruU9e530662014-05-18 11:49:37 -0700469 log.trace("Put a Packet in the wating list. intent ID {}, related pathspec {}", intentId, pathspec);
470 pendingFlows.put(pathspec, new PushedFlow(intentId));
471 log.trace("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700472 }
TeruU9e530662014-05-18 11:49:37 -0700473 pathRuntime.executeIntentOperations(operations);
Ray Milkey269ffb92014-04-03 14:43:30 -0700474 }
475
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 @Override
477 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
478 }
479
480 @Override
481 public void flowRemoved(FlowPath removedFlowPath) {
482 }
483
TeruU435df322014-06-16 23:45:13 -0700484 public void flowRemoved(ShortestPathIntent spfIntent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700485 if (log.isTraceEnabled()) {
TeruU435df322014-06-16 23:45:13 -0700486 log.trace("ShortestPathIntent {} was removed", spfIntent.getId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700487 }
488
Ray Milkey269ffb92014-04-03 14:43:30 -0700489 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
490 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
491 Path removedPath = new Path(srcMacAddress, dstMacAddress);
Ray Milkey269ffb92014-04-03 14:43:30 -0700492 synchronized (lock) {
493 // There *shouldn't* be any packets queued if the flow has
494 // just been removed.
495 List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId());
496 if (!packets.isEmpty()) {
497 log.warn("Removed flow {} has packets queued.", spfIntent.getId());
498 }
TeruU9e530662014-05-18 11:49:37 -0700499
Ray Milkey269ffb92014-04-03 14:43:30 -0700500 pendingFlows.remove(removedPath);
501 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId());
502 }
503 }
504
505 private void flowInstalled(PathIntent installedPath) {
506 if (log.isTraceEnabled()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700507 log.trace("Installed intent ID {}, path {}",
508 installedPath.getParentIntent().getId(), installedPath.getPath());
Ray Milkey269ffb92014-04-03 14:43:30 -0700509 }
510
511 ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent();
512 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
513 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
514 Path path = new Path(srcMacAddress, dstMacAddress);
515 log.debug("Path spec {}", path);
516
517 // TODO waiting packets should time out. We could request a path that
518 // can't be installed right now because of a network partition. The path
519 // may eventually be installed, but we may have received thousands of
520 // packets in the meantime and probably don't want to send very old packets.
521
522 List<PacketToPush> packets = null;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700523 net.onrc.onos.core.intent.Path graphPath = installedPath.getPath();
Ray Milkey269ffb92014-04-03 14:43:30 -0700524
TeruU220c45e2014-04-10 18:56:26 -0700525 short outPort;
526 if (graphPath.isEmpty()) {
527 outPort = (short) spfIntent.getDstPortNumber();
528 log.debug("Path is empty. Maybe devices on the same switch. outPort {}", outPort);
529 } else {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700530 outPort = graphPath.get(0).getSrc().getNumber().value();
TeruU220c45e2014-04-10 18:56:26 -0700531 log.debug("path{}, outPort {}", graphPath, outPort);
532 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700533
534 PushedFlow existingFlow = null;
535
536 synchronized (lock) {
537 existingFlow = pendingFlows.get(path);
538
539 if (existingFlow != null) {
540 existingFlow.installed = true;
541 existingFlow.firstOutPort = outPort;
542 } else {
543 log.debug("ExistingFlow {} is null", path);
544 return;
545 }
546
547 //Check both existing flow are installed status.
548 if (existingFlow.installed) {
549 packets = waitingPackets.removeAll(existingFlow.intentId);
550 if (log.isDebugEnabled()) {
551 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
552 existingFlow.intentId, existingFlow.firstOutPort, packets.size());
553 }
554 } else {
555 log.debug("Forward or reverse flows hasn't been pushed yet. return");
556 return;
557 }
558 }
559
560 for (PacketToPush packet : packets) {
TeruUbd5b90e2014-06-16 14:29:33 -0700561 log.debug("Start packetToPush to sw {}, outPort {}, path {}",
562 packet.dpid, existingFlow.firstOutPort, path);
Jonathan Harte3702f22014-04-29 02:56:56 -0700563 packetService.sendPacket(packet.eth, new SwitchPort(
564 packet.dpid, existingFlow.firstOutPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700565 }
566 }
567
Ray Milkey269ffb92014-04-03 14:43:30 -0700568 @Override
TeruUf9111652014-05-14 23:10:35 -0700569 public void intentsChange(LinkedList<ChangedEvent> events) {
570 for (ChangedEvent event : events) {
571 log.debug("path intent ID {}, eventType {}", event.intent.getId() , event.eventType);
TeruUbd5b90e2014-06-16 14:29:33 -0700572
573 PathIntent pathIntent = null;
574 if (event.intent instanceof PathIntent) {
575 pathIntent = (PathIntent) event.intent;
576 log.trace("pathIntent {}", pathIntent);
577 }
578
Ray Milkeyb29e6262014-04-09 16:02:14 -0700579 if (pathIntent == null) {
TeruU435df322014-06-16 23:45:13 -0700580 log.trace("pathIntent is null. Skip.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700581 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700582 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700583
Ray Milkeyb29e6262014-04-09 16:02:14 -0700584 if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) {
TeruU435df322014-06-16 23:45:13 -0700585 log.trace("parentIntent is not ShortestPathIntent. Skip.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700586 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700587 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700588
TeruUf9111652014-05-14 23:10:35 -0700589 switch(event.eventType) {
590 case ADDED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700591 break;
TeruUf9111652014-05-14 23:10:35 -0700592 case REMOVED:
TeruU435df322014-06-16 23:45:13 -0700593 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
Ray Milkey269ffb92014-04-03 14:43:30 -0700594 break;
TeruUf9111652014-05-14 23:10:35 -0700595 case STATE_CHANGED:
596 IntentState state = pathIntent.getState();
TeruU5d2c9392014-06-09 20:02:02 -0700597 log.debug("This is STATE_CHANGED. intent id {} is now state {}",
598 pathIntent.getId() , state);
TeruUf9111652014-05-14 23:10:35 -0700599 switch (state) {
600 case INST_REQ:
601 break;
602 case INST_ACK:
603 flowInstalled(pathIntent);
604 break;
605 case INST_NACK:
TeruU435df322014-06-16 23:45:13 -0700606 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
TeruUf9111652014-05-14 23:10:35 -0700607 break;
608 case DEL_REQ:
609 break;
610 case DEL_ACK:
TeruU435df322014-06-16 23:45:13 -0700611 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
TeruUf9111652014-05-14 23:10:35 -0700612 break;
613 case DEL_PENDING:
614 break;
TeruUbd5b90e2014-06-16 14:29:33 -0700615 case REROUTE_REQ:
616 break;
TeruUf9111652014-05-14 23:10:35 -0700617 default:
618 break;
619 }
Ray Milkey149693c2014-05-20 14:58:53 -0700620 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700621 default:
622 break;
623 }
624 }
625 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800626}