blob: 969b2b7f9e462744ec3910e6c2aae9966cce2062 [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 Hartaa380972014-04-03 10:24:46 -070021import net.onrc.onos.core.intent.Intent;
Jonathan Harta99ec672014-04-03 11:30:34 -070022import net.onrc.onos.core.intent.Intent.IntentState;
Jonathan Hartaa380972014-04-03 10:24:46 -070023import net.onrc.onos.core.intent.IntentMap;
TeruUf9111652014-05-14 23:10:35 -070024import net.onrc.onos.core.intent.IntentMap.ChangedEvent;
25import net.onrc.onos.core.intent.IntentMap.ChangedListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070026import net.onrc.onos.core.intent.IntentOperation;
27import net.onrc.onos.core.intent.IntentOperationList;
28import net.onrc.onos.core.intent.PathIntent;
29import net.onrc.onos.core.intent.ShortestPathIntent;
Jonathan Hartaa380972014-04-03 10:24:46 -070030import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070031import net.onrc.onos.core.packet.Ethernet;
32import net.onrc.onos.core.registry.IControllerRegistryService;
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070033import net.onrc.onos.core.topology.Host;
Jonathan Harte37e4e22014-05-13 19:12:02 -070034import net.onrc.onos.core.topology.ITopologyService;
Jonathan Hart472062d2014-04-03 10:56:48 -070035import net.onrc.onos.core.topology.LinkEvent;
Jonathan Hartf5bd2582014-04-09 17:43:41 -070036import net.onrc.onos.core.topology.Port;
Jonathan Hart472062d2014-04-03 10:56:48 -070037import net.onrc.onos.core.topology.Switch;
TeruUf9111652014-05-14 23:10:35 -070038import net.onrc.onos.core.topology.Topology;
Jonathan Hart23701d12014-04-03 10:45:48 -070039import net.onrc.onos.core.util.FlowPath;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070040import net.onrc.onos.core.util.PortNumber;
Jonathan Hart23701d12014-04-03 10:45:48 -070041import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080042
Jonathan Hart1caaa932013-11-04 15:28:28 -080043import org.openflow.util.HexString;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
Jonathan Hartd857ad62013-12-14 18:08:17 -080047import com.google.common.collect.LinkedListMultimap;
48import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080049
Jonathan Hartf5bd2582014-04-09 17:43:41 -070050public class Forwarding implements /*IOFMessageListener,*/ IFloodlightModule,
TeruUf9111652014-05-14 23:10:35 -070051 IForwardingService, IPacketListener, ChangedListener {
Ray Milkeyec838942014-04-09 11:28:43 -070052 private static final Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU7feef8a2014-04-03 00:15:49 -070053
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070054 private static final int SLEEP_TIME_FOR_DB_HOST_INSTALLED = 100; // milliseconds
Ray Milkeyec838942014-04-09 11:28:43 -070055 private static final int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
TeruU30c0c932014-05-15 16:47:41 -070056 private static final int SRC_SWITCH_TIMEOUT_ADJUST_SECOND = 2;
57 private static final int DEFAULT_IDLE_TIMEOUT = 5;
58 private int idleTimeout = DEFAULT_IDLE_TIMEOUT;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070059
Jonathan Hartc00f5c22014-06-10 15:14:40 -070060 private static final ScheduledExecutorService EXECUTOR_SERVICE =
61 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
TeruU435df322014-06-16 23:45:13 -070065 private final HighLevelIntentChangedHandler highLevelIntentChangedHandler =
66 new HighLevelIntentChangedHandler();
67
Jonathan Hartf5bd2582014-04-09 17:43:41 -070068 private IPacketService packetService;
Ray Milkey269ffb92014-04-03 14:43:30 -070069 private IControllerRegistryService controllerRegistryService;
70
Jonathan Harte37e4e22014-05-13 19:12:02 -070071 private ITopologyService topologyService;
72 private Topology topology;
Ray Milkey269ffb92014-04-03 14:43:30 -070073 private IPathCalcRuntimeService pathRuntime;
TeruU9e530662014-05-18 11:49:37 -070074 private IntentMap pathIntentMap;
75 private IntentMap highLevelIntentMap;
Ray Milkey269ffb92014-04-03 14:43:30 -070076
77 // TODO it seems there is a Guava collection that will time out entries.
78 // We should see if this will work here.
79 private Map<Path, PushedFlow> pendingFlows;
80 private ListMultimap<String, PacketToPush> waitingPackets;
81
82 private final Object lock = new Object();
83
TeruU435df322014-06-16 23:45:13 -070084 private class HighLevelIntentChangedHandler implements ChangedListener {
85
86 @Override
87 public void intentsChange(LinkedList<ChangedEvent> events) {
88 for (ChangedEvent event : events) {
89 ShortestPathIntent spfIntent = null;
90 if (event.intent instanceof ShortestPathIntent) {
91 spfIntent = (ShortestPathIntent) event.intent;
92 log.trace("ShortestPathIntent {}", spfIntent);
93 }
94
95 if (spfIntent == null) {
96 log.trace("ShortestPathIntent is null. Skip.");
97 continue;
98 }
99
100 switch(event.eventType) {
101 case ADDED:
102 break;
103 case REMOVED:
104 break;
105 case STATE_CHANGED:
106 if (spfIntent.getState() == IntentState.INST_NACK) {
107 flowRemoved(spfIntent);
108 }
109 break;
110 default:
111 break;
112 }
113 }
114 }
115
116 }
117
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700118 private static class PacketToPush {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700119 public final Ethernet eth;
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 public final long dpid;
121
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700122 public PacketToPush(Ethernet eth, long dpid) {
123 this.eth = eth;
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 this.dpid = dpid;
125 }
126 }
127
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700128 private static class PushedFlow {
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 public final String intentId;
130 public boolean installed = false;
131 public short firstOutPort;
132
133 public PushedFlow(String flowId) {
134 this.intentId = flowId;
135 }
136 }
137
Jonathan Hart8ed69c52014-04-09 13:29:16 -0700138 private static final class Path {
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 public final MACAddress srcMac;
140 public final MACAddress dstMac;
141
142 public Path(MACAddress srcMac, MACAddress dstMac) {
143 this.srcMac = srcMac;
144 this.dstMac = dstMac;
145 }
146
147 @Override
148 public boolean equals(Object other) {
149 if (!(other instanceof Path)) {
150 return false;
151 }
152
153 Path otherPath = (Path) other;
154 return srcMac.equals(otherPath.srcMac) &&
155 dstMac.equals(otherPath.dstMac);
156 }
157
158 @Override
159 public int hashCode() {
160 int hash = 17;
161 hash = 31 * hash + srcMac.hashCode();
162 hash = 31 * hash + dstMac.hashCode();
163 return hash;
164 }
165
166 @Override
167 public String toString() {
168 return "(" + srcMac + ") => (" + dstMac + ")";
169 }
170 }
171
172 @Override
173 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
174 List<Class<? extends IFloodlightService>> services =
175 new ArrayList<Class<? extends IFloodlightService>>(1);
176 services.add(IForwardingService.class);
177 return services;
178 }
179
180 @Override
181 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
182 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
183 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
184 impls.put(IForwardingService.class, this);
185 return impls;
186 }
187
188 @Override
189 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
190 List<Class<? extends IFloodlightService>> dependencies =
191 new ArrayList<Class<? extends IFloodlightService>>();
Ray Milkey269ffb92014-04-03 14:43:30 -0700192 dependencies.add(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700193 dependencies.add(ITopologyService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 dependencies.add(IPathCalcRuntimeService.class);
195 // We don't use the IProxyArpService directly, but reactive forwarding
196 // requires it to be loaded and answering ARP requests
197 dependencies.add(IProxyArpService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700198 dependencies.add(IPacketService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 return dependencies;
200 }
201
202 @Override
203 public void init(FloodlightModuleContext context) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700204 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Harte37e4e22014-05-13 19:12:02 -0700205 topologyService = context.getServiceImpl(ITopologyService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700206 pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700207 packetService = context.getServiceImpl(IPacketService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700208
Ray Milkey269ffb92014-04-03 14:43:30 -0700209 pendingFlows = new HashMap<Path, PushedFlow>();
210 waitingPackets = LinkedListMultimap.create();
211 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800212
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 @Override
214 public void startUp(FloodlightModuleContext context) {
TeruU30c0c932014-05-15 16:47:41 -0700215 Map<String, String> configOptions = context.getConfigParams(this);
216
217 try {
218 if (Integer.parseInt(configOptions.get("idletimeout")) > 0) {
219 idleTimeout = Integer.parseInt(configOptions.get("idletimeout"));
220 log.info("idle_timeout for Forwarding is set to {}.", idleTimeout);
221 } else {
222 log.info("idle_timeout for Forwarding is less than 0. Use default {}.", idleTimeout);
223 }
224 } catch (NumberFormatException e) {
225 log.info("idle_timeout related config options were not set. Use default.");
226 }
227
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700228 packetService.registerPacketListener(this);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800229
Jonathan Harte37e4e22014-05-13 19:12:02 -0700230 topology = topologyService.getTopology();
TeruU9e530662014-05-18 11:49:37 -0700231 highLevelIntentMap = pathRuntime.getHighLevelIntents();
TeruU435df322014-06-16 23:45:13 -0700232 highLevelIntentMap.addChangeListener(highLevelIntentChangedHandler);
TeruU9e530662014-05-18 11:49:37 -0700233 pathIntentMap = pathRuntime.getPathIntents();
234 pathIntentMap.addChangeListener(this);
Ray Milkey269ffb92014-04-03 14:43:30 -0700235 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800236
Ray Milkey269ffb92014-04-03 14:43:30 -0700237 @Override
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700238 public void receive(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700239 if (log.isTraceEnabled()) {
240 log.trace("Receive PACKET_IN swId {}, portId {}", sw.getDpid(), inPort.getNumber());
241 }
TeruU417fe022014-02-04 12:59:30 -0800242
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700243 if (eth.getEtherType() != Ethernet.TYPE_IPV4) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700244 // Only handle IPv4 packets right now
245 return;
Ray Milkey269ffb92014-04-03 14:43:30 -0700246 }
TeruU417fe022014-02-04 12:59:30 -0800247
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 if (eth.isBroadcast() || eth.isMulticast()) {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700249 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700250 } else {
251 // Unicast
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700252 handlePacketIn(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700253 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700254 }
TeruU417fe022014-02-04 12:59:30 -0800255
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700256 private void handleBroadcast(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700257 if (log.isTraceEnabled()) {
258 log.trace("Sending broadcast packet to other ONOS instances");
259 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800260
Jonathan Harte3702f22014-04-29 02:56:56 -0700261 packetService.broadcastPacketOutEdge(eth,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700262 new SwitchPort(sw.getDpid(), inPort.getNumber()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700263 }
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700264
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700265 private void handlePacketIn(Switch sw, Port inPort, Ethernet eth) {
TeruUf9111652014-05-14 23:10:35 -0700266 if (log.isTraceEnabled()) {
267 log.trace("Start handlePacketIn swId {}, portId {}", sw.getDpid(), inPort.getNumber());
268 }
TeruU7feef8a2014-04-03 00:15:49 -0700269
Ray Milkey269ffb92014-04-03 14:43:30 -0700270 String destinationMac =
271 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart0444d932014-01-22 15:06:17 -0800272
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700273 // FIXME #getHostByMac() is a blocking call, so it may be better way
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700274 // to handle it to avoid the condition.
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700275 Host hostObject = topology.getHostByMac(
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700276 MACAddress.valueOf(destinationMac));
TeruU6464af02014-02-06 21:38:45 -0800277
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700278 if (hostObject == null) {
279 log.debug("No host entry found for {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 destinationMac);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800281
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700282 //Host is not in the DB, so wait it until the host is added.
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700283 EXECUTOR_SERVICE.schedule(new WaitDeviceArp(sw, inPort, eth),
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700284 SLEEP_TIME_FOR_DB_HOST_INSTALLED, TimeUnit.MILLISECONDS);
Ray Milkey269ffb92014-04-03 14:43:30 -0700285 return;
286 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800287
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700288 continueHandlePacketIn(sw, inPort, eth, hostObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700289 }
TeruU417fe022014-02-04 12:59:30 -0800290
Ray Milkey269ffb92014-04-03 14:43:30 -0700291 private class WaitDeviceArp implements Runnable {
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700292 Switch sw;
293 Port inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700294 Ethernet eth;
TeruU417fe022014-02-04 12:59:30 -0800295
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700296 public WaitDeviceArp(Switch sw, Port inPort, Ethernet eth) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700297 super();
298 this.sw = sw;
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700299 this.inPort = inPort;
Ray Milkey269ffb92014-04-03 14:43:30 -0700300 this.eth = eth;
301 }
TeruU417fe022014-02-04 12:59:30 -0800302
Ray Milkey269ffb92014-04-03 14:43:30 -0700303 @Override
304 public void run() {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700305 Host hostObject = topology.getHostByMac(MACAddress.valueOf(eth.getDestinationMACAddress()));
306 if (hostObject == null) {
307 log.debug("wait {}ms and host was not found. " +
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700308 "Send broadcast packet and the thread finish.",
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700309 SLEEP_TIME_FOR_DB_HOST_INSTALLED);
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700310 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700311 return;
312 }
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700313 log.debug("wait {}ms and host {} was found, continue",
314 SLEEP_TIME_FOR_DB_HOST_INSTALLED, hostObject.getMacAddress());
315 continueHandlePacketIn(sw, inPort, eth, hostObject);
Ray Milkey269ffb92014-04-03 14:43:30 -0700316 }
317 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800318
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700319 private void continueHandlePacketIn(Switch sw, Port inPort, Ethernet eth, Host hostObject) {
TeruU7feef8a2014-04-03 00:15:49 -0700320
TeruU9e530662014-05-18 11:49:37 -0700321 log.trace("Start continuehandlePacketIn");
TeruU7feef8a2014-04-03 00:15:49 -0700322
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700323 //Iterator<IPortObject> ports = hostObject.getAttachedPorts().iterator();
324 Iterator<net.onrc.onos.core.topology.Port> ports = hostObject.getAttachmentPoints().iterator();
Ray Milkey269ffb92014-04-03 14:43:30 -0700325 if (!ports.hasNext()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700326 log.debug("No attachment point found for host {} - broadcasting packet",
327 hostObject.getMacAddress());
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700328 handleBroadcast(sw, inPort, eth);
Ray Milkey269ffb92014-04-03 14:43:30 -0700329 return;
330 }
TeruU7feef8a2014-04-03 00:15:49 -0700331
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700332 //This code assumes the host has only one port. It should be problem.
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700333 Port destinationPort = ports.next();
334 short destinationPortNum = destinationPort.getNumber().value();
335 Switch destinationSw = destinationPort.getSwitch();
336 long destinationDpid = destinationSw.getDpid().value();
Ray Milkey269ffb92014-04-03 14:43:30 -0700337
Ray Milkey269ffb92014-04-03 14:43:30 -0700338 SwitchPort srcSwitchPort = new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700339 sw.getDpid(),
340 inPort.getNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700341 SwitchPort dstSwitchPort = new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700342 destinationSw.getDpid(),
343 destinationPort.getNumber());
Ray Milkey269ffb92014-04-03 14:43:30 -0700344
345 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
346 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
TeruU9e530662014-05-18 11:49:37 -0700347 Path pathspec = new Path(srcMacAddress, dstMacAddress);
348 IntentOperationList operations = new IntentOperationList();
Ray Milkey269ffb92014-04-03 14:43:30 -0700349
350 synchronized (lock) {
351 //TODO check concurrency
TeruU9e530662014-05-18 11:49:37 -0700352
Ray Milkey269ffb92014-04-03 14:43:30 -0700353 PushedFlow existingFlow = pendingFlows.get(pathspec);
354
355 //A path is installed side by side to reduce a path timeout and a wrong state.
356 if (existingFlow != null) {
357 // We've already start to install a flow for this pair of MAC addresses
358 if (log.isDebugEnabled()) {
359 log.debug("Found existing the same pathspec {}, intent ID is {}",
360 pathspec,
361 existingFlow.intentId);
362 }
363
Ray Milkey269ffb92014-04-03 14:43:30 -0700364 // Find the correct port here. We just assume the PI is from
365 // the first hop switch, but this is definitely not always
366 // the case. We'll have to retrieve the flow from HZ every time
367 // because it could change (be rerouted) sometimes.
368 if (existingFlow.installed) {
369 // Flow has been sent to the switches so it is safe to
370 // send a packet out now
371
TeruU9e530662014-05-18 11:49:37 -0700372 // TODO Here highLevelIntentMap and pathIntentMap would be problem,
373 // because it doesn't have global information as of May 2014.
374 // However usually these lines here is used when we got packet-in and this class think
375 // the path for the packet is installed already, so it is pretty rare.
376 // I will leave it for now, and will work in the next step.
377 Intent highLevelIntent = highLevelIntentMap.getIntent(existingFlow.intentId);
378 if (highLevelIntent == null) {
379 log.debug("Intent ID {} is null in HighLevelIntentMap. return.", existingFlow.intentId);
380 return;
381 }
382
383 if (highLevelIntent.getState() != IntentState.INST_ACK) {
384 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
385 return;
386 }
387
388 ShortestPathIntent spfIntent = null;
389 if (highLevelIntent instanceof ShortestPathIntent) {
390 spfIntent = (ShortestPathIntent) highLevelIntent;
Ray Milkey269ffb92014-04-03 14:43:30 -0700391 } else {
TeruUf9111652014-05-14 23:10:35 -0700392 log.debug("Intent ID {} is not PathIntent or null. return.", existingFlow.intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700393 return;
394 }
395
TeruU9e530662014-05-18 11:49:37 -0700396 PathIntent pathIntent = (PathIntent) pathIntentMap.getIntent(spfIntent.getPathIntentId());
397 if (pathIntent == null) {
398 log.debug("PathIntent ID {} is null in PathIntentMap. return.", existingFlow.intentId);
399 return;
400 }
401
402 if (pathIntent.getState() != IntentState.INST_ACK) {
403 log.debug("Intent ID {}'s state is not INST_ACK. return.", existingFlow.intentId);
404 return;
405 }
406
407 boolean isflowEntryForThisSwitch = false;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700408 net.onrc.onos.core.intent.Path path = pathIntent.getPath();
TeruU9e530662014-05-18 11:49:37 -0700409 long outPort = -1;
410
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700411 if (spfIntent.getDstSwitchDpid() == sw.getDpid().value()) {
TeruU9e530662014-05-18 11:49:37 -0700412 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
413 isflowEntryForThisSwitch = true;
414 outPort = spfIntent.getDstPortNumber();
415 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700416
Ray Milkey7f1567c2014-04-08 13:53:32 -0700417 for (Iterator<LinkEvent> i = path.iterator(); i.hasNext();) {
Ray Milkey149693c2014-05-20 14:58:53 -0700418 LinkEvent le = i.next();
TeruU9e530662014-05-18 11:49:37 -0700419
Yuta HIGUCHI5c8cbeb2014-06-27 11:13:48 -0700420 if (le.getSrc().getDpid().equals(sw.getDpid())) {
TeruU9e530662014-05-18 11:49:37 -0700421 log.trace("The packet-in sw dpid {} is on the path.", sw.getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -0700422 isflowEntryForThisSwitch = true;
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700423 outPort = le.getSrc().getPortNumber().value();
Ray Milkey269ffb92014-04-03 14:43:30 -0700424 break;
425 }
426 }
427
Ray Milkey6c4f2fe2014-04-11 09:47:23 -0700428 if (!isflowEntryForThisSwitch) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700429 // If we don't find a flow entry for that switch, then we're
430 // in the middle of a rerouting (or something's gone wrong).
431 // This packet will be dropped as a victim of the rerouting.
432 log.debug("Dropping packet on flow {} between {}-{}",
433 existingFlow.intentId,
434 srcMacAddress, dstMacAddress);
435 } else {
TeruU9e530662014-05-18 11:49:37 -0700436 if (outPort < 0) {
437 outPort = existingFlow.firstOutPort;
438 }
Jonathan Hartf5bd2582014-04-09 17:43:41 -0700439
TeruU9e530662014-05-18 11:49:37 -0700440 log.debug("Sending packet out from sw {}, outport{}", sw.getDpid(), outPort);
Jonathan Harte3702f22014-04-29 02:56:56 -0700441 packetService.sendPacket(eth, new SwitchPort(
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700442 sw.getDpid(), new PortNumber((short) outPort)));
Ray Milkey269ffb92014-04-03 14:43:30 -0700443 }
444 } else {
445 // Flow path has not yet been installed to switches so save the
446 // packet out for later
TeruU9e530662014-05-18 11:49:37 -0700447 log.trace("Put a packet into the waiting list. flowId {}", existingFlow.intentId);
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700448 waitingPackets.put(existingFlow.intentId, new PacketToPush(eth, sw.getDpid().value()));
Ray Milkey269ffb92014-04-03 14:43:30 -0700449 }
450 return;
451 }
452
TeruU9e530662014-05-18 11:49:37 -0700453 String intentId = Long.toString(controllerRegistryService.getNextUniqueId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700454 ShortestPathIntent intent = new ShortestPathIntent(intentId,
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700455 sw.getDpid().value(), inPort.getNumber().value(), srcMacAddress.toLong(),
456 destinationDpid, destinationPortNum, dstMacAddress.toLong());
Ray Milkeyff735142014-05-22 19:06:02 -0700457
TeruU30c0c932014-05-15 16:47:41 -0700458 intent.setIdleTimeout(idleTimeout + SRC_SWITCH_TIMEOUT_ADJUST_SECOND);
459 intent.setFirstSwitchIdleTimeout(idleTimeout);
Ray Milkey269ffb92014-04-03 14:43:30 -0700460 IntentOperation.Operator operator = IntentOperation.Operator.ADD;
461 operations.add(operator, intent);
TeruU9e530662014-05-18 11:49:37 -0700462 log.debug("Adding new flow between {} at {} and {} at {}",
463 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Ray Milkey269ffb92014-04-03 14:43:30 -0700464
TeruU9e530662014-05-18 11:49:37 -0700465 // Add to waiting lists
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700466 waitingPackets.put(intentId, new PacketToPush(eth, sw.getDpid().value()));
TeruU9e530662014-05-18 11:49:37 -0700467 log.trace("Put a Packet in the wating list. intent ID {}, related pathspec {}", intentId, pathspec);
468 pendingFlows.put(pathspec, new PushedFlow(intentId));
469 log.trace("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId);
Ray Milkey269ffb92014-04-03 14:43:30 -0700470 }
TeruU9e530662014-05-18 11:49:37 -0700471 pathRuntime.executeIntentOperations(operations);
Ray Milkey269ffb92014-04-03 14:43:30 -0700472 }
473
Ray Milkey269ffb92014-04-03 14:43:30 -0700474 @Override
475 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
476 }
477
478 @Override
479 public void flowRemoved(FlowPath removedFlowPath) {
480 }
481
TeruU435df322014-06-16 23:45:13 -0700482 public void flowRemoved(ShortestPathIntent spfIntent) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700483 if (log.isTraceEnabled()) {
TeruU435df322014-06-16 23:45:13 -0700484 log.trace("ShortestPathIntent {} was removed", spfIntent.getId());
Ray Milkey269ffb92014-04-03 14:43:30 -0700485 }
486
Ray Milkey269ffb92014-04-03 14:43:30 -0700487 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
488 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
489 Path removedPath = new Path(srcMacAddress, dstMacAddress);
Ray Milkey269ffb92014-04-03 14:43:30 -0700490 synchronized (lock) {
491 // There *shouldn't* be any packets queued if the flow has
492 // just been removed.
493 List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId());
494 if (!packets.isEmpty()) {
495 log.warn("Removed flow {} has packets queued.", spfIntent.getId());
496 }
TeruU9e530662014-05-18 11:49:37 -0700497
Ray Milkey269ffb92014-04-03 14:43:30 -0700498 pendingFlows.remove(removedPath);
499 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId());
500 }
501 }
502
503 private void flowInstalled(PathIntent installedPath) {
504 if (log.isTraceEnabled()) {
Jonathan Hartc00f5c22014-06-10 15:14:40 -0700505 log.trace("Installed intent ID {}, path {}",
506 installedPath.getParentIntent().getId(), installedPath.getPath());
Ray Milkey269ffb92014-04-03 14:43:30 -0700507 }
508
509 ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent();
510 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
511 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
512 Path path = new Path(srcMacAddress, dstMacAddress);
513 log.debug("Path spec {}", path);
514
515 // TODO waiting packets should time out. We could request a path that
516 // can't be installed right now because of a network partition. The path
517 // may eventually be installed, but we may have received thousands of
518 // packets in the meantime and probably don't want to send very old packets.
519
520 List<PacketToPush> packets = null;
Yuta HIGUCHI1fc395e2014-05-13 14:06:28 -0700521 net.onrc.onos.core.intent.Path graphPath = installedPath.getPath();
Ray Milkey269ffb92014-04-03 14:43:30 -0700522
TeruU220c45e2014-04-10 18:56:26 -0700523 short outPort;
524 if (graphPath.isEmpty()) {
525 outPort = (short) spfIntent.getDstPortNumber();
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700526 log.debug("Path is empty. Maybe hosts on the same switch. outPort {}", outPort);
TeruU220c45e2014-04-10 18:56:26 -0700527 } else {
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -0700528 outPort = graphPath.get(0).getSrc().getPortNumber().value();
TeruU220c45e2014-04-10 18:56:26 -0700529 log.debug("path{}, outPort {}", graphPath, outPort);
530 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700531
532 PushedFlow existingFlow = null;
533
534 synchronized (lock) {
535 existingFlow = pendingFlows.get(path);
536
537 if (existingFlow != null) {
538 existingFlow.installed = true;
539 existingFlow.firstOutPort = outPort;
540 } else {
541 log.debug("ExistingFlow {} is null", path);
542 return;
543 }
544
545 //Check both existing flow are installed status.
546 if (existingFlow.installed) {
547 packets = waitingPackets.removeAll(existingFlow.intentId);
548 if (log.isDebugEnabled()) {
549 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
550 existingFlow.intentId, existingFlow.firstOutPort, packets.size());
551 }
552 } else {
553 log.debug("Forward or reverse flows hasn't been pushed yet. return");
554 return;
555 }
556 }
557
558 for (PacketToPush packet : packets) {
TeruUbd5b90e2014-06-16 14:29:33 -0700559 log.debug("Start packetToPush to sw {}, outPort {}, path {}",
560 packet.dpid, existingFlow.firstOutPort, path);
Jonathan Harte3702f22014-04-29 02:56:56 -0700561 packetService.sendPacket(packet.eth, new SwitchPort(
562 packet.dpid, existingFlow.firstOutPort));
Ray Milkey269ffb92014-04-03 14:43:30 -0700563 }
564 }
565
Ray Milkey269ffb92014-04-03 14:43:30 -0700566 @Override
TeruUf9111652014-05-14 23:10:35 -0700567 public void intentsChange(LinkedList<ChangedEvent> events) {
568 for (ChangedEvent event : events) {
569 log.debug("path intent ID {}, eventType {}", event.intent.getId() , event.eventType);
TeruUbd5b90e2014-06-16 14:29:33 -0700570
571 PathIntent pathIntent = null;
572 if (event.intent instanceof PathIntent) {
573 pathIntent = (PathIntent) event.intent;
574 log.trace("pathIntent {}", pathIntent);
575 }
576
Ray Milkeyb29e6262014-04-09 16:02:14 -0700577 if (pathIntent == null) {
TeruU435df322014-06-16 23:45:13 -0700578 log.trace("pathIntent is null. Skip.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700579 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700580 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700581
Ray Milkeyb29e6262014-04-09 16:02:14 -0700582 if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent)) {
TeruU435df322014-06-16 23:45:13 -0700583 log.trace("parentIntent is not ShortestPathIntent. Skip.");
Ray Milkey269ffb92014-04-03 14:43:30 -0700584 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700585 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700586
TeruUf9111652014-05-14 23:10:35 -0700587 switch(event.eventType) {
588 case ADDED:
Ray Milkey269ffb92014-04-03 14:43:30 -0700589 break;
TeruUf9111652014-05-14 23:10:35 -0700590 case REMOVED:
TeruU435df322014-06-16 23:45:13 -0700591 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
Ray Milkey269ffb92014-04-03 14:43:30 -0700592 break;
TeruUf9111652014-05-14 23:10:35 -0700593 case STATE_CHANGED:
594 IntentState state = pathIntent.getState();
TeruU5d2c9392014-06-09 20:02:02 -0700595 log.debug("This is STATE_CHANGED. intent id {} is now state {}",
596 pathIntent.getId() , state);
TeruUf9111652014-05-14 23:10:35 -0700597 switch (state) {
598 case INST_REQ:
599 break;
600 case INST_ACK:
601 flowInstalled(pathIntent);
602 break;
603 case INST_NACK:
TeruU435df322014-06-16 23:45:13 -0700604 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
TeruUf9111652014-05-14 23:10:35 -0700605 break;
606 case DEL_REQ:
607 break;
608 case DEL_ACK:
TeruU435df322014-06-16 23:45:13 -0700609 flowRemoved((ShortestPathIntent) pathIntent.getParentIntent());
TeruUf9111652014-05-14 23:10:35 -0700610 break;
611 case DEL_PENDING:
612 break;
TeruUbd5b90e2014-06-16 14:29:33 -0700613 case REROUTE_REQ:
614 break;
TeruUf9111652014-05-14 23:10:35 -0700615 default:
616 break;
617 }
Ray Milkey149693c2014-05-20 14:58:53 -0700618 break;
Ray Milkey269ffb92014-04-03 14:43:30 -0700619 default:
620 break;
621 }
622 }
623 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800624}