blob: 97b3e28873ed6a77afcd0c2cb8ddb2b33babbdab [file] [log] [blame]
Jonathan Hart1caaa932013-11-04 15:28:28 -08001package net.onrc.onos.ofcontroller.forwarding;
2
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;
Jonathan Hart41d1e912013-11-24 16:50:25 -08007import java.util.List;
Jonathan Harte93aed42013-12-05 18:39:50 -08008import java.util.Map;
TeruU417fe022014-02-04 12:59:30 -08009import java.util.concurrent.Executors;
10import java.util.concurrent.ScheduledExecutorService;
11import java.util.concurrent.TimeUnit;
Jonathan Hart1caaa932013-11-04 15:28:28 -080012
13import net.floodlightcontroller.core.FloodlightContext;
14import net.floodlightcontroller.core.IFloodlightProviderService;
15import net.floodlightcontroller.core.IOFMessageListener;
16import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080017import net.floodlightcontroller.core.module.FloodlightModuleContext;
18import net.floodlightcontroller.core.module.IFloodlightModule;
19import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080020import net.floodlightcontroller.packet.Ethernet;
21import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdc3ad702013-11-14 11:34:59 -080022import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080023import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
24import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hartd857ad62013-12-14 18:08:17 -080026import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080027import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart7804bea2014-01-07 10:50:52 -080028import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0444d932014-01-22 15:06:17 -080029import net.onrc.onos.ofcontroller.proxyarp.IProxyArpService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080030import net.onrc.onos.ofcontroller.util.CallerId;
31import net.onrc.onos.ofcontroller.util.DataPath;
32import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080033import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080034import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080035import net.onrc.onos.ofcontroller.util.FlowId;
36import net.onrc.onos.ofcontroller.util.FlowPath;
37import net.onrc.onos.ofcontroller.util.FlowPathType;
38import net.onrc.onos.ofcontroller.util.FlowPathUserState;
39import net.onrc.onos.ofcontroller.util.Port;
40import net.onrc.onos.ofcontroller.util.SwitchPort;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070041import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080042
43import org.openflow.protocol.OFMessage;
44import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080045import org.openflow.protocol.OFPacketOut;
46import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080047import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080048import org.openflow.protocol.action.OFAction;
49import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080050import org.openflow.util.HexString;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Jonathan Hartd857ad62013-12-14 18:08:17 -080054import com.google.common.collect.LinkedListMultimap;
55import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080056
57public class Forwarding implements IOFMessageListener, IFloodlightModule,
58 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080059 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080060
Jonathan Hart7e6df362013-12-10 23:33:59 -080061 private final int IDLE_TIMEOUT = 5; // seconds
62 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080063 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
64 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
65
66 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080067
68 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080069
Jonathan Hart1caaa932013-11-04 15:28:28 -080070 private IFloodlightProviderService floodlightProvider;
Jonathan Hart7e6df362013-12-10 23:33:59 -080071 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080072 private IDatagridService datagrid;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070073 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080074
Jonathan Harte789d6e2013-12-17 17:50:11 -080075 // TODO it seems there is a Guava collection that will time out entries.
76 // We should see if this will work here.
77 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080078 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080079
Jonathan Hart7e6df362013-12-10 23:33:59 -080080 private final Object lock = new Object();
81
Jonathan Harte789d6e2013-12-17 17:50:11 -080082 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080083 public final OFPacketOut packet;
84 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080085
Jonathan Hart5e448782013-12-10 12:36:35 -080086 public PacketToPush(OFPacketOut packet, long dpid) {
87 this.packet = packet;
88 this.dpid = dpid;
89 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080090 }
91
Jonathan Harte789d6e2013-12-17 17:50:11 -080092 private class PushedFlow {
93 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -080094 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -080095 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -080096
97 public PushedFlow(long flowId) {
98 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -080099 }
100 }
101
102 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800103 public final MACAddress srcMac;
104 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800105
Jonathan Hart0444d932014-01-22 15:06:17 -0800106 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800107 this.srcMac = srcMac;
108 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800109 }
110
111 @Override
112 public boolean equals(Object other) {
113 if (!(other instanceof Path)) {
114 return false;
115 }
116
117 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800118 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800119 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800120 }
121
122 @Override
123 public int hashCode() {
124 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800125 hash = 31 * hash + srcMac.hashCode();
126 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800127 return hash;
128 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800129
130 @Override
131 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800132 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800133 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800134 }
135
Jonathan Harte93aed42013-12-05 18:39:50 -0800136 @Override
137 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800138 List<Class<? extends IFloodlightService>> services =
139 new ArrayList<Class<? extends IFloodlightService>>(1);
140 services.add(IForwardingService.class);
141 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800142 }
143
144 @Override
145 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800146 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
147 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
148 impls.put(IForwardingService.class, this);
149 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800150 }
151
152 @Override
153 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
154 List<Class<? extends IFloodlightService>> dependencies =
155 new ArrayList<Class<? extends IFloodlightService>>();
156 dependencies.add(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800157 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800158 dependencies.add(IOnosDeviceService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700159 dependencies.add(IControllerRegistryService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800160 // We don't use the IProxyArpService directly, but reactive forwarding
161 // requires it to be loaded and answering ARP requests
162 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800163 return dependencies;
164 }
165
166 @Override
167 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800168 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800169 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800170 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800171 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700172 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800173
174 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800175
Jonathan Harte789d6e2013-12-17 17:50:11 -0800176 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800177 waitingPackets = LinkedListMultimap.create();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800178 }
179
Jonathan Harte93aed42013-12-05 18:39:50 -0800180 @Override
181 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800182 // no-op
183 }
184
185 @Override
186 public String getName() {
187 return "onosforwarding";
188 }
189
190 @Override
191 public boolean isCallbackOrderingPrereq(OFType type, String name) {
192 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800193 (name.equals("devicemanager") || name.equals("proxyarpmanager")
194 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800195 }
196
197 @Override
198 public boolean isCallbackOrderingPostreq(OFType type, String name) {
199 return false;
200 }
201
202 @Override
203 public Command receive(
204 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
205
206 if (msg.getType() != OFType.PACKET_IN) {
207 return Command.CONTINUE;
208 }
209
210 OFPacketIn pi = (OFPacketIn) msg;
211
212 Ethernet eth = IFloodlightProviderService.bcStore.
213 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
214
TeruU6464af02014-02-06 21:38:45 -0800215 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
216
Jonathan Hart17672992013-12-12 16:15:16 -0800217 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800218 return Command.CONTINUE;
219 }
220
Jonathan Hart17672992013-12-12 16:15:16 -0800221 if (eth.isBroadcast() || eth.isMulticast()) {
222 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800223 }
224 else {
225 // Unicast
226 handlePacketIn(sw, pi, eth);
227 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800228
229 return Command.STOP;
230 }
231
Jonathan Hart17672992013-12-12 16:15:16 -0800232 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
233 if (log.isTraceEnabled()) {
234 log.trace("Sending broadcast packet to other ONOS instances");
235 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800236
237 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800238 eth.serialize(), null, sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800239 }
240
TeruU417fe022014-02-04 12:59:30 -0800241 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
242 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
243
Jonathan Hart5e448782013-12-10 12:36:35 -0800244 String destinationMac =
245 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800246
TeruUd1ba0e22014-02-10 11:44:15 -0800247 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700248 // TODO: Fix the code below after deviceStorage was removed
249 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800250 try{
251 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800252 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800253 if (deviceObject == null) {
254 log.debug("No device entry found for {}",
255 destinationMac);
256
257 //Device is not in the DB, so wait it until the device is added.
258 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
259 return;
260 }
261
262 continueHandlePacketIn(sw, pi, eth, deviceObject);
263 } finally {
264 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800265 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700266 */
TeruU417fe022014-02-04 12:59:30 -0800267 }
268
269 private class WaitDeviceArp implements Runnable {
270 IOFSwitch sw;
271 OFPacketIn pi;
272 Ethernet eth;
273
274 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
275 super();
276 this.sw = sw;
277 this.pi = pi;
278 this.eth = eth;
279 }
280
281 @Override
282 public void run() {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700283 // TODO: Fix the code below after deviceStorage was removed
284 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800285 try {
TeruU417fe022014-02-04 12:59:30 -0800286 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800287 if(deviceObject == null){
288 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
289 handleBroadcast(sw, pi, eth);
290 return;
291 }
292 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
293 continueHandlePacketIn(sw, pi, eth, deviceObject);
294 } finally {
295 deviceStorage.rollback();
296 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700297 */
TeruU417fe022014-02-04 12:59:30 -0800298 }
299 }
300
301 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
302 log.debug("Start continuehandlePacketIn");
303
Jonathan Hart5e448782013-12-10 12:36:35 -0800304 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800305 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800306 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800307 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800308 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800309 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800310 }
TeruU417fe022014-02-04 12:59:30 -0800311
TeruU6464af02014-02-06 21:38:45 -0800312
TeruU417fe022014-02-04 12:59:30 -0800313 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800314 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800315
Jonathan Hart1caaa932013-11-04 15:28:28 -0800316 short destinationPort = portObject.getNumber();
317 ISwitchObject switchObject = portObject.getSwitch();
318 long destinationDpid = HexString.toLong(switchObject.getDPID());
319
Jonathan Hart41d1e912013-11-24 16:50:25 -0800320 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800321 SwitchPort srcSwitchPort = new SwitchPort(
322 new Dpid(sw.getId()), new Port(pi.getInPort()));
323 SwitchPort dstSwitchPort = new SwitchPort(
324 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800325
326 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
327 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800328
TeruU6464af02014-02-06 21:38:45 -0800329 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800330
Jonathan Hart7e6df362013-12-10 23:33:59 -0800331 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800332 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800333 Path pathspec = new Path(srcMacAddress, dstMacAddress);
334
Jonathan Harte789d6e2013-12-17 17:50:11 -0800335 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800336
TeruU6464af02014-02-06 21:38:45 -0800337 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800338 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800339 // We've already start to install a flow for this pair of MAC addresses
340 if(log.isDebugEnabled()) {
341 log.debug("Found existing the same pathspec {}, Flow ID is {}",
342 pathspec,
343 HexString.toHexString(existingFlow.flowId));
344 }
345
Jonathan Hart7e6df362013-12-10 23:33:59 -0800346 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800347
Jonathan Hart0444d932014-01-22 15:06:17 -0800348 // Find the correct port here. We just assume the PI is from
349 // the first hop switch, but this is definitely not always
350 // the case. We'll have to retrieve the flow from HZ every time
351 // because it could change (be rerouted) sometimes.
352 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800353 // Flow has been sent to the switches so it is safe to
354 // send a packet out now
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700355
356 //
357 // TODO: The getFlow() call below needs
358 // to be updated to the new Path Intent
359 // framework.
360 //
361 // FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
362 FlowPath flow = null;
Jonathan Hart0444d932014-01-22 15:06:17 -0800363 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800364
365 if (flow != null) {
366 for (FlowEntry flowEntry : flow.flowEntries()) {
367 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
368 flowEntryForThisSwitch = flowEntry;
369 break;
370 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800371 }
372 }
373
374 if (flowEntryForThisSwitch == null) {
375 // If we don't find a flow entry for that switch, then we're
376 // in the middle of a rerouting (or something's gone wrong).
377 // This packet will be dropped as a victim of the rerouting.
378 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
379 new Object[] {new FlowId(existingFlow.flowId),
380 srcMacAddress, dstMacAddress, flow});
381 }
382 else {
TeruU417fe022014-02-04 12:59:30 -0800383 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800384 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
385 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800386 }
387 else {
TeruU6464af02014-02-06 21:38:45 -0800388 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800389 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800390 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
391 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800392 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800393 return;
394 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800395
Jonathan Hart7e6df362013-12-10 23:33:59 -0800396 log.debug("Adding new flow between {} at {} and {} at {}",
397 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800398
Jonathan Hart7e6df362013-12-10 23:33:59 -0800399 DataPath datapath = new DataPath();
400 datapath.setSrcPort(srcSwitchPort);
401 datapath.setDstPort(dstSwitchPort);
402
403 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800404 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800405
406 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
407 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
408 flowPath.setFlowEntryMatch(new FlowEntryMatch());
409 flowPath.setIdleTimeout(IDLE_TIMEOUT);
410 flowPath.setHardTimeout(HARD_TIMEOUT);
411 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
412 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
413 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
414 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800415
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700416 FlowId flowId = new FlowId(controllerRegistryService.getNextUniqueId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800417
418 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800419
Jonathan Hart7e6df362013-12-10 23:33:59 -0800420 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800421
422 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800423 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800424 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800425 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
426 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800427 }
TeruU6464af02014-02-06 21:38:45 -0800428
TeruU417fe022014-02-04 12:59:30 -0800429 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800430 srcMacAddress, dstMacAddress, flowPath.flowId()});
Pavlin Radoslavov39f0f2e2014-03-20 12:04:57 -0700431 // TODO: Add the flow by using the new Path Intent framework
432 // flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800433 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800434
Jonathan Hart7e6df362013-12-10 23:33:59 -0800435 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800436 OFPacketOut po = new OFPacketOut();
437 po.setInPort(OFPort.OFPP_NONE)
438 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800439 .setActions(new ArrayList<OFAction>())
440 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800441
442 if (sw.getBuffers() == 0) {
443 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
444 .setPacketData(pi.getPacketData())
445 .setLengthU(po.getLengthU() + po.getPacketData().length);
446 }
447 else {
448 po.setBufferId(pi.getBufferId());
449 }
450
Jonathan Hart5e448782013-12-10 12:36:35 -0800451 return po;
452 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800453
Jonathan Hart5e448782013-12-10 12:36:35 -0800454 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800455 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
456 for (FlowPath flowPath : installedFlowPaths) {
457 flowInstalled(flowPath);
458 }
459 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800460
461 @Override
462 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800463 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800464 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800465 }
466
467
Jonathan Hart0444d932014-01-22 15:06:17 -0800468 if (!removedFlowPath.installerId().equals(callerId)) {
469 // Not our flow path, ignore
470 return;
471 }
TeruU417fe022014-02-04 12:59:30 -0800472
Jonathan Hart0444d932014-01-22 15:06:17 -0800473 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
474 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
475
476 Path removedPath = new Path(srcMacAddress, dstMacAddress);
477
478 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800479 // There *shouldn't* be any packets queued if the flow has
480 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800481 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800482 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800483 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800484 }
TeruU417fe022014-02-04 12:59:30 -0800485 pendingFlows.remove(removedPath);
486 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800487 }
488 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800489
TeruU417fe022014-02-04 12:59:30 -0800490 private void flowInstalled(FlowPath installedFlowPath) {
491 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800492
Jonathan Hart0444d932014-01-22 15:06:17 -0800493 if (!installedFlowPath.installerId().equals(callerId)) {
494 // Not our flow path, ignore
495 return;
TeruU417fe022014-02-04 12:59:30 -0800496 }
497
498 if(installedFlowPath.flowEntries().isEmpty()){
499 //If there is no flowEntry, ignore
500 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
501 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800502 }
503
TeruU417fe022014-02-04 12:59:30 -0800504 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
505 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
506 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800507
Jonathan Hart0444d932014-01-22 15:06:17 -0800508 // TODO waiting packets should time out. We could request a path that
509 // can't be installed right now because of a network partition. The path
510 // may eventually be installed, but we may have received thousands of
511 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800512
TeruU417fe022014-02-04 12:59:30 -0800513 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800514 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
515
516 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800517
Jonathan Hart7e6df362013-12-10 23:33:59 -0800518 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800519 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800520
Jonathan Hart0444d932014-01-22 15:06:17 -0800521 if (existingFlow != null) {
522 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800523 existingFlow.firstOutPort = outPort;
524 } else {
525 log.debug("ExistingFlow {} is null", installedPath);
526 return;
527 }
528
TeruU6464af02014-02-06 21:38:45 -0800529 //Check both existing flow are installed status.
530 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800531 packets = waitingPackets.removeAll(existingFlow.flowId);
532 if(log.isDebugEnabled()){
533 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
534 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
535 }
TeruU417fe022014-02-04 12:59:30 -0800536 }else{
537 log.debug("Forward or reverse flows hasn't been pushed yet. return");
538 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800539 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800540 }
TeruU417fe022014-02-04 12:59:30 -0800541
Jonathan Hart5e448782013-12-10 12:36:35 -0800542 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800543 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800544 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800545 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
546 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800547 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800548
549 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
550 po.getActions().add(new OFActionOutput(outPort));
551 po.setActionsLength((short)
552 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
553 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
554
555 flowPusher.add(sw, po);
556 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800557
Jonathan Hart1caaa932013-11-04 15:28:28 -0800558}