blob: e59b33ce25e80b433a08bf572cf3066aa43b3258 [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.Dpid;
41import net.onrc.onos.core.util.FlowPath;
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
Ray Milkeyec838942014-04-09 11:28:43 -070061 private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Harte93aed42013-12-05 18:39:50 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 private final String callerId = "Forwarding";
Jonathan Harte93aed42013-12-05 18:39:50 -080064
Jonathan Hartf5bd2582014-04-09 17:43:41 -070065 private IPacketService packetService;
Ray Milkey269ffb92014-04-03 14:43:30 -070066 private IControllerRegistryService controllerRegistryService;
67
Jonathan Harte37e4e22014-05-13 19:12:02 -070068 private ITopologyService topologyService;
69 private Topology topology;
Ray Milkey269ffb92014-04-03 14:43:30 -070070 private IPathCalcRuntimeService pathRuntime;
TeruU9e530662014-05-18 11:49:37 -070071 private IntentMap pathIntentMap;
72 private IntentMap highLevelIntentMap;
Ray Milkey269ffb92014-04-03 14:43:30 -070073
74 // TODO it seems there is a Guava collection that will time out entries.
75 // We should see if this will work here.
76 private Map<Path, PushedFlow> pendingFlows;
77 private ListMultimap<String, PacketToPush> waitingPackets;
78
79 private final Object lock = new Object();
80
Jonathan Hart8ed69c52014-04-09 13:29:16 -070081 private static class PacketToPush {
Jonathan Hartf5bd2582014-04-09 17:43:41 -070082 public final Ethernet eth;
Ray Milkey269ffb92014-04-03 14:43:30 -070083 public final long dpid;
84
Jonathan Hartf5bd2582014-04-09 17:43:41 -070085 public PacketToPush(Ethernet eth, long dpid) {
86 this.eth = eth;
Ray Milkey269ffb92014-04-03 14:43:30 -070087 this.dpid = dpid;
88 }
89 }
90
Jonathan Hart8ed69c52014-04-09 13:29:16 -070091 private static class PushedFlow {
Ray Milkey269ffb92014-04-03 14:43:30 -070092 public final String intentId;
93 public boolean installed = false;
94 public short firstOutPort;
95
96 public PushedFlow(String flowId) {
97 this.intentId = flowId;
98 }
99 }
100
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700101 private static final class Path {
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 public final MACAddress srcMac;
103 public final MACAddress dstMac;
104
105 public Path(MACAddress srcMac, MACAddress dstMac) {
106 this.srcMac = srcMac;
107 this.dstMac = dstMac;
108 }
109
110 @Override
111 public boolean equals(Object other) {
112 if (!(other instanceof Path)) {
113 return false;
114 }
115
116 Path otherPath = (Path) other;
117 return srcMac.equals(otherPath.srcMac) &&
118 dstMac.equals(otherPath.dstMac);
119 }
120
121 @Override
122 public int hashCode() {
123 int hash = 17;
124 hash = 31 * hash + srcMac.hashCode();
125 hash = 31 * hash + dstMac.hashCode();
126 return hash;
127 }
128
129 @Override
130 public String toString() {
131 return "(" + srcMac + ") => (" + dstMac + ")";
132 }
133 }
134
135 @Override
136 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
137 List<Class<? extends IFloodlightService>> services =
138 new ArrayList<Class<? extends IFloodlightService>>(1);
139 services.add(IForwardingService.class);
140 return services;
141 }
142
143 @Override
144 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
145 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
146 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
147 impls.put(IForwardingService.class, this);
148 return impls;
149 }
150
151 @Override
152 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
153 List<Class<? extends IFloodlightService>> dependencies =
154 new ArrayList<Class<? extends IFloodlightService>>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 dependencies.add(IControllerRegistryService.class);
156 dependencies.add(IOnosDeviceService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700157 dependencies.add(ITopologyService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 dependencies.add(IPathCalcRuntimeService.class);
159 // We don't use the IProxyArpService directly, but reactive forwarding
160 // requires it to be loaded and answering ARP requests
161 dependencies.add(IProxyArpService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700162 dependencies.add(IPacketService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 return dependencies;
164 }
165
166 @Override
167 public void init(FloodlightModuleContext context) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700169 topologyService = context.getServiceImpl(ITopologyService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700170 pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700171 packetService = context.getServiceImpl(IPacketService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700172
Ray Milkey269ffb92014-04-03 14:43:30 -0700173 pendingFlows = new HashMap<Path, PushedFlow>();
174 waitingPackets = LinkedListMultimap.create();
175 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800176
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 @Override
178 public void startUp(FloodlightModuleContext context) {
TeruU30c0c932014-05-15 16:47:41 -0700179 Map<String, String> configOptions = context.getConfigParams(this);
180
181 try {
182 if (Integer.parseInt(configOptions.get("idletimeout")) > 0) {
183 idleTimeout = Integer.parseInt(configOptions.get("idletimeout"));
184 log.info("idle_timeout for Forwarding is set to {}.", idleTimeout);
185 } else {
186 log.info("idle_timeout for Forwarding is less than 0. Use default {}.", idleTimeout);
187 }
188 } catch (NumberFormatException e) {
189 log.info("idle_timeout related config options were not set. Use default.");
190 }
191
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700192 packetService.registerPacketListener(this);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800193
Jonathan Harte37e4e22014-05-13 19:12:02 -0700194 topology = topologyService.getTopology();
TeruU9e530662014-05-18 11:49:37 -0700195 highLevelIntentMap = pathRuntime.getHighLevelIntents();
196 pathIntentMap = pathRuntime.getPathIntents();
197 pathIntentMap.addChangeListener(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700198 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800199
Ray Milkey269ffb92014-04-03 14:43:30 -0700200 @Override
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700201 public void receive(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700202 if (log.isTraceEnabled()) {
203 log.trace("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber());
204 }
TeruU417fe022014-02-04 12:59:30 -0800205
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700206 if (eth.getEtherType() != Ethernet.TYPE_IPV4) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700207 // Only handle IPv4 packets right now
208 return;
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 }
TeruU417fe022014-02-04 12:59:30 -0800210
Ray Milkey269ffb92014-04-03 14:43:30 -0700211 if (eth.isBroadcast() || eth.isMulticast()) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700212 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 } else {
214 // Unicast
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700215 handlePacketIn(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700216 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 }
TeruU417fe022014-02-04 12:59:30 -0800218
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700219 private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700220 if (log.isTraceEnabled()) {
221 log.trace("Sending broadcast packet to other ONOS instances");
222 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800223
Jonathan Harte3702f22014-04-29 02:56:56 -0700224 packetService.broadcastPacketOutEdge(eth,
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700225 new SwitchPort(sw.getDpid(), inPort.getNumber().shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700226 }
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700227
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700228 private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700229 if (log.isTraceEnabled()) {
230 log.trace("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber());
231 }
TeruU7feef8a2014-04-03 00:15:49 -0700232
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 String destinationMac =
234 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart0444d932014-01-22 15:06:17 -0800235
Ray Milkey269ffb92014-04-03 14:43:30 -0700236 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
Jonathan Harte37e4e22014-05-13 19:12:02 -0700237 Device deviceObject = topology.getDeviceByMac(MACAddress.valueOf(destinationMac));
TeruU6464af02014-02-06 21:38:45 -0800238
Ray Milkey269ffb92014-04-03 14:43:30 -0700239 if (deviceObject == null) {
240 log.debug("No device entry found for {}",
241 destinationMac);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800242
Ray Milkey269ffb92014-04-03 14:43:30 -0700243 //Device is not in the DB, so wait it until the device is added.
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700244 EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, inPort, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
Ray Milkey269ffb92014-04-03 14:43:30 -0700245 return;
246 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800247
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700248 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700249 }
TeruU417fe022014-02-04 12:59:30 -0800250
Ray Milkey269ffb92014-04-03 14:43:30 -0700251 private class WaitDeviceArp implements Runnable {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700252 Switch sw;
253 Port inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700254 Ethernet eth;
TeruU417fe022014-02-04 12:59:30 -0800255
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700256 public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700257 super();
258 this.sw = sw;
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700259 this.inPort = inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700260 this.eth = eth;
261 }
TeruU417fe022014-02-04 12:59:30 -0800262
Ray Milkey269ffb92014-04-03 14:43:30 -0700263 @Override
264 public void run() {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700265 Device deviceObject = topology.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700266 if (deviceObject == null) {
267 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700268 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700269 return;
270 }
271 log.debug("wait {}ms and device {} was found, continue", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700272 continueHandlePacketIn(sw, inPort, eth, deviceObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 }
274 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800275
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700276 private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Device deviceObject) {
TeruU7feef8a2014-04-03 00:15:49 -0700277
TeruU9e530662014-05-18 11:49:37 -0700278 log.trace("Start continuehandlePacketIn");
TeruU7feef8a2014-04-03 00:15:49 -0700279
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
281 Iterator<net.onrc.onos.core.topology.Port> ports = deviceObject.getAttachmentPoints().iterator();
282 if (!ports.hasNext()) {
283 log.debug("No attachment point found for device {} - broadcasting packet",
284 deviceObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700285 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700286 return;
287 }
TeruU7feef8a2014-04-03 00:15:49 -0700288
Ray Milkey269ffb92014-04-03 14:43:30 -0700289 //This code assumes the device has only one port. It should be problem.
290 net.onrc.onos.core.topology.Port portObject = ports.next();
291 short destinationPort = portObject.getNumber().shortValue();
292 Switch switchObject = portObject.getSwitch();
293 long destinationDpid = switchObject.getDpid();
294
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700295 // TODO eliminate cast
Ray Milkey269ffb92014-04-03 14:43:30 -0700296 SwitchPort srcSwitchPort = new SwitchPort(
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700297 new Dpid(sw.getDpid()),
298 new net.onrc.onos.core.util.Port((short) inPort.getNumber().longValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700299 SwitchPort dstSwitchPort = new SwitchPort(
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700300 new Dpid(destinationDpid),
301 new net.onrc.onos.core.util.Port(destinationPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700302
303 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
304 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
TeruU9e530662014-05-18 11:49:37 -0700305 Path pathspec = new Path(srcMacAddress, dstMacAddress);
306 IntentOperationList operations = new IntentOperationList();
Ray Milkey269ffb92014-04-03 14:43:30 -0700307
308 synchronized (lock) {
309 //TODO check concurrency
TeruU9e530662014-05-18 11:49:37 -0700310
Ray Milkey269ffb92014-04-03 14:43:30 -0700311 PushedFlow existingFlow = pendingFlows.get(pathspec);
312
313 //A path is installed side by side to reduce a path timeout and a wrong state.
314 if (existingFlow != null) {
315 // We've already start to install a flow for this pair of MAC addresses
316 if (log.isDebugEnabled()) {
317 log.debug("Found existing the same pathspec {}, intent ID is {}",
318 pathspec,
319 existingFlow.intentId);
320 }
321
Ray Milkey269ffb92014-04-03 14:43:30 -0700322 // Find the correct port here. We just assume the PI is from
323 // the first hop switch, but this is definitely not always
324 // the case. We'll have to retrieve the flow from HZ every time
325 // because it could change (be rerouted) sometimes.
326 if (existingFlow.installed) {
327 // Flow has been sent to the switches so it is safe to
328 // send a packet out now
329
TeruU9e530662014-05-18 11:49:37 -0700330 // TODO Here highLevelIntentMap and pathIntentMap would be problem,
331 // because it doesn't have global information as of May 2014.
332 // However usually these lines here is used when we got packet-in and this class think
333 // the path for the packet is installed already, so it is pretty rare.
334 // I will leave it for now, and will work in the next step.
335 Intent highLevelIntent = highLevelIntentMap.getIntent(existingFlow.intentId);
336 if (highLevelIntent == null) {
337 log.debug("Intent ID {} is null in HighLevelIntentMap. return.", existingFlow.intentId);
338 return;
339 }
340
341 if (highLevelIntent.getState() != IntentState.INST_ACK) {
342 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
343 return;
344 }
345
346 ShortestPathIntent spfIntent = null;
347 if (highLevelIntent instanceof ShortestPathIntent) {
348 spfIntent = (ShortestPathIntent) highLevelIntent;
Ray Milkey269ffb92014-04-03 14:43:30 -0700349 } else {
TeruUf9111652014-05-14 23:10:35 -0700350 log.debug("Intent ID {} is not PathIntent or null. return.", existingFlow.intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700351 return;
352 }
353
TeruU9e530662014-05-18 11:49:37 -0700354 PathIntent pathIntent = (PathIntent) pathIntentMap.getIntent(spfIntent.getPathIntentId());
355 if (pathIntent == null) {
356 log.debug("PathIntent ID {} is null in PathIntentMap. return.", existingFlow.intentId);
357 return;
358 }
359
360 if (pathIntent.getState() != IntentState.INST_ACK) {
361 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
362 return;
363 }
364
365 boolean isflowEntryForThisSwitch = false;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700366 net.onrc.onos.core.intent.Path path = pathIntent.getPath();
TeruU9e530662014-05-18 11:49:37 -0700367 long outPort = -1;
368
369 if (spfIntent.getDstSwitchDpid() == sw.getDpid()) {
370 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
371 isflowEntryForThisSwitch = true;
372 outPort = spfIntent.getDstPortNumber();
373 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700374
Ray Milkey7f1567c2014-04-08 13:53:32 -0700375 for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) {
Ray Milkey149693c2014-05-20 14:58:53 -0700376 LinkEvent le = i.next();
TeruU9e530662014-05-18 11:49:37 -0700377
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700378 if (le.getSrc().dpid.equals(sw.getDpid())) {
TeruU9e530662014-05-18 11:49:37 -0700379 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700380 isflowEntryForThisSwitch = true;
TeruU9e530662014-05-18 11:49:37 -0700381 outPort = le.getSrc().getNumber();
Ray Milkey269ffb92014-04-03 14:43:30 -0700382 break;
383 }
384 }
385
Ray Milkey6c4f2fe2014-04-11 09:47:23 -0700386 if (!isflowEntryForThisSwitch) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700387 // If we don't find a flow entry for that switch, then we're
388 // in the middle of a rerouting (or something's gone wrong).
389 // This packet will be dropped as a victim of the rerouting.
390 log.debug("Dropping packet on flow {} between {}-{}",
391 existingFlow.intentId,
392 srcMacAddress, dstMacAddress);
393 } else {
TeruU9e530662014-05-18 11:49:37 -0700394 if (outPort < 0) {
395 outPort = existingFlow.firstOutPort;
396 }
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700397
TeruU9e530662014-05-18 11:49:37 -0700398 log.debug("Sending packet out from sw {}, outport{}", sw.getDpid(), outPort);
Jonathan Harte3702f22014-04-29 02:56:56 -0700399 packetService.sendPacket(eth, new SwitchPort(
TeruU9e530662014-05-18 11:49:37 -0700400 sw.getDpid(), (short) outPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700401 }
402 } else {
403 // Flow path has not yet been installed to switches so save the
404 // packet out for later
TeruU9e530662014-05-18 11:49:37 -0700405 log.trace("Put a packet into the waiting list. flowId {}", existingFlow.intentId);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700406 waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700407 }
408 return;
409 }
410
TeruU9e530662014-05-18 11:49:37 -0700411 String intentId = Long.toString(controllerRegistryService.getNextUniqueId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700412 ShortestPathIntent intent = new ShortestPathIntent(intentId,
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700413 sw.getDpid(), inPort.getNumber(), srcMacAddress.toLong(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700414 destinationDpid, destinationPort, dstMacAddress.toLong());
Ray Milkeyff735142014-05-22 19:06:02 -0700415
TeruU30c0c932014-05-15 16:47:41 -0700416 intent.setIdleTimeout(idleTimeout + SRC_SWITCH_TIMEOUT_ADJUST_SECOND);
417 intent.setFirstSwitchIdleTimeout(idleTimeout);
Ray Milkey269ffb92014-04-03 14:43:30 -0700418 IntentOperation.Operator operator = IntentOperation.Operator.ADD;
419 operations.add(operator, intent);
TeruU9e530662014-05-18 11:49:37 -0700420 log.debug("Adding new flow between {} at {} and {} at {}",
421 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Ray Milkey269ffb92014-04-03 14:43:30 -0700422
TeruU9e530662014-05-18 11:49:37 -0700423 // Add to waiting lists
424 waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid()));
425 log.trace("Put a Packet in the wating list. intent ID {}, related pathspec {}", intentId, pathspec);
426 pendingFlows.put(pathspec, new PushedFlow(intentId));
427 log.trace("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700428 }
TeruU9e530662014-05-18 11:49:37 -0700429 pathRuntime.executeIntentOperations(operations);
Ray Milkey269ffb92014-04-03 14:43:30 -0700430 }
431
Ray Milkey269ffb92014-04-03 14:43:30 -0700432 @Override
433 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
434 }
435
436 @Override
437 public void flowRemoved(FlowPath removedFlowPath) {
438 }
439
440 public void flowRemoved(PathIntent removedIntent) {
441 if (log.isTraceEnabled()) {
442 log.trace("Path {} was removed", removedIntent.getParentIntent().getId());
443 }
444
445 ShortestPathIntent spfIntent = (ShortestPathIntent) removedIntent.getParentIntent();
446 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
447 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
448 Path removedPath = new Path(srcMacAddress, dstMacAddress);
Ray Milkey269ffb92014-04-03 14:43:30 -0700449 synchronized (lock) {
450 // There *shouldn't* be any packets queued if the flow has
451 // just been removed.
452 List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId());
453 if (!packets.isEmpty()) {
454 log.warn("Removed flow {} has packets queued.", spfIntent.getId());
455 }
TeruU9e530662014-05-18 11:49:37 -0700456
Ray Milkey269ffb92014-04-03 14:43:30 -0700457 pendingFlows.remove(removedPath);
458 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId());
459 }
460 }
461
462 private void flowInstalled(PathIntent installedPath) {
463 if (log.isTraceEnabled()) {
TeruUf9111652014-05-14 23:10:35 -0700464 log.trace("Installed intent ID {}, path {}", installedPath.getParentIntent().getId(), installedPath.getPath());
Ray Milkey269ffb92014-04-03 14:43:30 -0700465 }
466
467 ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent();
468 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
469 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
470 Path path = new Path(srcMacAddress, dstMacAddress);
471 log.debug("Path spec {}", path);
472
473 // TODO waiting packets should time out. We could request a path that
474 // can't be installed right now because of a network partition. The path
475 // may eventually be installed, but we may have received thousands of
476 // packets in the meantime and probably don't want to send very old packets.
477
478 List<PacketToPush> packets = null;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700479 net.onrc.onos.core.intent.Path graphPath = installedPath.getPath();
Ray Milkey269ffb92014-04-03 14:43:30 -0700480
TeruU220c45e2014-04-10 18:56:26 -0700481 short outPort;
482 if (graphPath.isEmpty()) {
483 outPort = (short) spfIntent.getDstPortNumber();
484 log.debug("Path is empty. Maybe devices on the same switch. outPort {}", outPort);
485 } else {
486 outPort = graphPath.get(0).getSrc().getNumber().shortValue();
487 log.debug("path{}, outPort {}", graphPath, outPort);
488 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700489
490 PushedFlow existingFlow = null;
491
492 synchronized (lock) {
493 existingFlow = pendingFlows.get(path);
494
495 if (existingFlow != null) {
496 existingFlow.installed = true;
497 existingFlow.firstOutPort = outPort;
498 } else {
499 log.debug("ExistingFlow {} is null", path);
500 return;
501 }
502
503 //Check both existing flow are installed status.
504 if (existingFlow.installed) {
505 packets = waitingPackets.removeAll(existingFlow.intentId);
506 if (log.isDebugEnabled()) {
507 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
508 existingFlow.intentId, existingFlow.firstOutPort, packets.size());
509 }
510 } else {
511 log.debug("Forward or reverse flows hasn't been pushed yet. return");
512 return;
513 }
514 }
515
516 for (PacketToPush packet : packets) {
517 log.debug("Start packetToPush to sw {}, outPort {}, path {}", packet.dpid, existingFlow.firstOutPort, path);
Jonathan Harte3702f22014-04-29 02:56:56 -0700518 packetService.sendPacket(packet.eth, new SwitchPort(
519 packet.dpid, existingFlow.firstOutPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700520 }
521 }
522
Ray Milkey269ffb92014-04-03 14:43:30 -0700523 @Override
TeruUf9111652014-05-14 23:10:35 -0700524 public void intentsChange(LinkedList<ChangedEvent> events) {
525 for (ChangedEvent event : events) {
526 log.debug("path intent ID {}, eventType {}", event.intent.getId() , event.eventType);
TeruU9e530662014-05-18 11:49:37 -0700527 PathIntent pathIntent = (PathIntent) pathIntentMap.getIntent(event.intent.getId());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700528 if (pathIntent == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700529 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700530 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700531
Ray Milkeyb29e6262014-04-09 16:02:14 -0700532 if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700533 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700534 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700535
TeruUf9111652014-05-14 23:10:35 -0700536 switch(event.eventType) {
537 case ADDED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700538 break;
TeruUf9111652014-05-14 23:10:35 -0700539 case REMOVED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700540 break;
TeruUf9111652014-05-14 23:10:35 -0700541 case STATE_CHANGED:
542 IntentState state = pathIntent.getState();
543 switch (state) {
544 case INST_REQ:
545 break;
546 case INST_ACK:
547 flowInstalled(pathIntent);
548 break;
549 case INST_NACK:
550 break;
551 case DEL_REQ:
552 break;
553 case DEL_ACK:
554 flowRemoved(pathIntent);
555 break;
556 case DEL_PENDING:
557 break;
558 default:
559 break;
560 }
Ray Milkey149693c2014-05-20 14:58:53 -0700561 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700562 default:
563 break;
564 }
565 }
566 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800567}