blob: e846155ee4224a5e770f1a5bb5469c86469bff6d [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.IDeviceStorage;
24import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
26import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
27import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hartd857ad62013-12-14 18:08:17 -080028import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080029import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart7804bea2014-01-07 10:50:52 -080030import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0444d932014-01-22 15:06:17 -080031import net.onrc.onos.ofcontroller.proxyarp.IProxyArpService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080032import net.onrc.onos.ofcontroller.util.CallerId;
33import net.onrc.onos.ofcontroller.util.DataPath;
34import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080035import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080036import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080037import net.onrc.onos.ofcontroller.util.FlowId;
38import net.onrc.onos.ofcontroller.util.FlowPath;
39import net.onrc.onos.ofcontroller.util.FlowPathType;
40import net.onrc.onos.ofcontroller.util.FlowPathUserState;
41import net.onrc.onos.ofcontroller.util.Port;
42import net.onrc.onos.ofcontroller.util.SwitchPort;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070043import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080044
45import org.openflow.protocol.OFMessage;
46import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080047import org.openflow.protocol.OFPacketOut;
48import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080049import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080050import org.openflow.protocol.action.OFAction;
51import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080052import org.openflow.util.HexString;
53import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
Jonathan Hartd857ad62013-12-14 18:08:17 -080056import com.google.common.collect.LinkedListMultimap;
57import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080058
59public class Forwarding implements IOFMessageListener, IFloodlightModule,
60 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080061 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080062
Jonathan Hart7e6df362013-12-10 23:33:59 -080063 private final int IDLE_TIMEOUT = 5; // seconds
64 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080065 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
66 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
67
68 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080069
70 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080071
Jonathan Hart1caaa932013-11-04 15:28:28 -080072 private IFloodlightProviderService floodlightProvider;
Jonathan Hart7e6df362013-12-10 23:33:59 -080073 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080074 private IDatagridService datagrid;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070075 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080076
77 private IDeviceStorage deviceStorage;
Jonathan Hart1caaa932013-11-04 15:28:28 -080078
Jonathan Harte789d6e2013-12-17 17:50:11 -080079 // TODO it seems there is a Guava collection that will time out entries.
80 // We should see if this will work here.
81 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080082 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080083
Jonathan Hart7e6df362013-12-10 23:33:59 -080084 private final Object lock = new Object();
85
Jonathan Harte789d6e2013-12-17 17:50:11 -080086 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080087 public final OFPacketOut packet;
88 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080089
Jonathan Hart5e448782013-12-10 12:36:35 -080090 public PacketToPush(OFPacketOut packet, long dpid) {
91 this.packet = packet;
92 this.dpid = dpid;
93 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080094 }
95
Jonathan Harte789d6e2013-12-17 17:50:11 -080096 private class PushedFlow {
97 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -080098 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -080099 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800100
101 public PushedFlow(long flowId) {
102 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800103 }
104 }
105
106 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800107 public final MACAddress srcMac;
108 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800109
Jonathan Hart0444d932014-01-22 15:06:17 -0800110 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800111 this.srcMac = srcMac;
112 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800113 }
114
115 @Override
116 public boolean equals(Object other) {
117 if (!(other instanceof Path)) {
118 return false;
119 }
120
121 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800122 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800123 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800124 }
125
126 @Override
127 public int hashCode() {
128 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800129 hash = 31 * hash + srcMac.hashCode();
130 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800131 return hash;
132 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800133
134 @Override
135 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800136 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800137 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800138 }
139
Jonathan Harte93aed42013-12-05 18:39:50 -0800140 @Override
141 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800142 List<Class<? extends IFloodlightService>> services =
143 new ArrayList<Class<? extends IFloodlightService>>(1);
144 services.add(IForwardingService.class);
145 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800146 }
147
148 @Override
149 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800150 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
151 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
152 impls.put(IForwardingService.class, this);
153 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800154 }
155
156 @Override
157 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
158 List<Class<? extends IFloodlightService>> dependencies =
159 new ArrayList<Class<? extends IFloodlightService>>();
160 dependencies.add(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800161 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800162 dependencies.add(IOnosDeviceService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700163 dependencies.add(IControllerRegistryService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800164 // We don't use the IProxyArpService directly, but reactive forwarding
165 // requires it to be loaded and answering ARP requests
166 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800167 return dependencies;
168 }
169
170 @Override
171 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800172 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800173 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800174 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800175 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700176 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800177
178 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800179
Jonathan Harte789d6e2013-12-17 17:50:11 -0800180 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800181 waitingPackets = LinkedListMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800182
Jonathan Hart1caaa932013-11-04 15:28:28 -0800183 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800184 deviceStorage.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800185 }
186
Jonathan Harte93aed42013-12-05 18:39:50 -0800187 @Override
188 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800189 // no-op
190 }
191
192 @Override
193 public String getName() {
194 return "onosforwarding";
195 }
196
197 @Override
198 public boolean isCallbackOrderingPrereq(OFType type, String name) {
199 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800200 (name.equals("devicemanager") || name.equals("proxyarpmanager")
201 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800202 }
203
204 @Override
205 public boolean isCallbackOrderingPostreq(OFType type, String name) {
206 return false;
207 }
208
209 @Override
210 public Command receive(
211 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
212
213 if (msg.getType() != OFType.PACKET_IN) {
214 return Command.CONTINUE;
215 }
216
217 OFPacketIn pi = (OFPacketIn) msg;
218
219 Ethernet eth = IFloodlightProviderService.bcStore.
220 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
221
TeruU6464af02014-02-06 21:38:45 -0800222 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
223
Jonathan Hart17672992013-12-12 16:15:16 -0800224 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800225 return Command.CONTINUE;
226 }
227
Jonathan Hart17672992013-12-12 16:15:16 -0800228 if (eth.isBroadcast() || eth.isMulticast()) {
229 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800230 }
231 else {
232 // Unicast
233 handlePacketIn(sw, pi, eth);
234 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800235
236 return Command.STOP;
237 }
238
Jonathan Hart17672992013-12-12 16:15:16 -0800239 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
240 if (log.isTraceEnabled()) {
241 log.trace("Sending broadcast packet to other ONOS instances");
242 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800243
244 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800245 eth.serialize(), null, sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800246 }
247
TeruU417fe022014-02-04 12:59:30 -0800248 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
249 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
250
Jonathan Hart5e448782013-12-10 12:36:35 -0800251 String destinationMac =
252 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800253
TeruUd1ba0e22014-02-10 11:44:15 -0800254 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
255 try{
256 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800257 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800258 if (deviceObject == null) {
259 log.debug("No device entry found for {}",
260 destinationMac);
261
262 //Device is not in the DB, so wait it until the device is added.
263 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
264 return;
265 }
266
267 continueHandlePacketIn(sw, pi, eth, deviceObject);
268 } finally {
269 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800270 }
TeruU417fe022014-02-04 12:59:30 -0800271 }
272
273 private class WaitDeviceArp implements Runnable {
274 IOFSwitch sw;
275 OFPacketIn pi;
276 Ethernet eth;
277
278 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
279 super();
280 this.sw = sw;
281 this.pi = pi;
282 this.eth = eth;
283 }
284
285 @Override
286 public void run() {
TeruUd1ba0e22014-02-10 11:44:15 -0800287 try {
TeruU417fe022014-02-04 12:59:30 -0800288 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800289 if(deviceObject == null){
290 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
291 handleBroadcast(sw, pi, eth);
292 return;
293 }
294 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
295 continueHandlePacketIn(sw, pi, eth, deviceObject);
296 } finally {
297 deviceStorage.rollback();
298 }
TeruU417fe022014-02-04 12:59:30 -0800299 }
300 }
301
302 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
303 log.debug("Start continuehandlePacketIn");
304
Jonathan Hart5e448782013-12-10 12:36:35 -0800305 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800306 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800307 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800308 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800309 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800310 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800311 }
TeruU417fe022014-02-04 12:59:30 -0800312
TeruU6464af02014-02-06 21:38:45 -0800313
TeruU417fe022014-02-04 12:59:30 -0800314 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800315 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800316
Jonathan Hart1caaa932013-11-04 15:28:28 -0800317 short destinationPort = portObject.getNumber();
318 ISwitchObject switchObject = portObject.getSwitch();
319 long destinationDpid = HexString.toLong(switchObject.getDPID());
320
Jonathan Hart41d1e912013-11-24 16:50:25 -0800321 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800322 SwitchPort srcSwitchPort = new SwitchPort(
323 new Dpid(sw.getId()), new Port(pi.getInPort()));
324 SwitchPort dstSwitchPort = new SwitchPort(
325 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800326
327 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
328 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800329
TeruU6464af02014-02-06 21:38:45 -0800330 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800331
Jonathan Hart7e6df362013-12-10 23:33:59 -0800332 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800333 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800334 Path pathspec = new Path(srcMacAddress, dstMacAddress);
335
Jonathan Harte789d6e2013-12-17 17:50:11 -0800336 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800337
TeruU6464af02014-02-06 21:38:45 -0800338 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800339 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800340 // We've already start to install a flow for this pair of MAC addresses
341 if(log.isDebugEnabled()) {
342 log.debug("Found existing the same pathspec {}, Flow ID is {}",
343 pathspec,
344 HexString.toHexString(existingFlow.flowId));
345 }
346
Jonathan Hart7e6df362013-12-10 23:33:59 -0800347 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800348
Jonathan Hart0444d932014-01-22 15:06:17 -0800349 // Find the correct port here. We just assume the PI is from
350 // the first hop switch, but this is definitely not always
351 // the case. We'll have to retrieve the flow from HZ every time
352 // because it could change (be rerouted) sometimes.
353 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800354 // Flow has been sent to the switches so it is safe to
355 // send a packet out now
Jonathan Hart0444d932014-01-22 15:06:17 -0800356 FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
357 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800358
359 if (flow != null) {
360 for (FlowEntry flowEntry : flow.flowEntries()) {
361 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
362 flowEntryForThisSwitch = flowEntry;
363 break;
364 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800365 }
366 }
367
368 if (flowEntryForThisSwitch == null) {
369 // If we don't find a flow entry for that switch, then we're
370 // in the middle of a rerouting (or something's gone wrong).
371 // This packet will be dropped as a victim of the rerouting.
372 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
373 new Object[] {new FlowId(existingFlow.flowId),
374 srcMacAddress, dstMacAddress, flow});
375 }
376 else {
TeruU417fe022014-02-04 12:59:30 -0800377 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800378 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
379 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800380 }
381 else {
TeruU6464af02014-02-06 21:38:45 -0800382 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800383 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800384 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
385 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800386 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800387 return;
388 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800389
Jonathan Hart7e6df362013-12-10 23:33:59 -0800390 log.debug("Adding new flow between {} at {} and {} at {}",
391 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800392
Jonathan Hart7e6df362013-12-10 23:33:59 -0800393 DataPath datapath = new DataPath();
394 datapath.setSrcPort(srcSwitchPort);
395 datapath.setDstPort(dstSwitchPort);
396
397 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800398 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800399
400 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
401 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
402 flowPath.setFlowEntryMatch(new FlowEntryMatch());
403 flowPath.setIdleTimeout(IDLE_TIMEOUT);
404 flowPath.setHardTimeout(HARD_TIMEOUT);
405 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
406 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
407 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
408 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800409
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700410 FlowId flowId = new FlowId(controllerRegistryService.getNextUniqueId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800411
412 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800413
Jonathan Hart7e6df362013-12-10 23:33:59 -0800414 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800415
416 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800417 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800418 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800419 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
420 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800421 }
TeruU6464af02014-02-06 21:38:45 -0800422
TeruU417fe022014-02-04 12:59:30 -0800423 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800424 srcMacAddress, dstMacAddress, flowPath.flowId()});
Pavlin Radoslavov39f0f2e2014-03-20 12:04:57 -0700425 // TODO: Add the flow by using the new Path Intent framework
426 // flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800427 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800428
Jonathan Hart7e6df362013-12-10 23:33:59 -0800429 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800430 OFPacketOut po = new OFPacketOut();
431 po.setInPort(OFPort.OFPP_NONE)
432 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800433 .setActions(new ArrayList<OFAction>())
434 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800435
436 if (sw.getBuffers() == 0) {
437 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
438 .setPacketData(pi.getPacketData())
439 .setLengthU(po.getLengthU() + po.getPacketData().length);
440 }
441 else {
442 po.setBufferId(pi.getBufferId());
443 }
444
Jonathan Hart5e448782013-12-10 12:36:35 -0800445 return po;
446 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800447
Jonathan Hart5e448782013-12-10 12:36:35 -0800448 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800449 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
450 for (FlowPath flowPath : installedFlowPaths) {
451 flowInstalled(flowPath);
452 }
453 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800454
455 @Override
456 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800457 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800458 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800459 }
460
461
Jonathan Hart0444d932014-01-22 15:06:17 -0800462 if (!removedFlowPath.installerId().equals(callerId)) {
463 // Not our flow path, ignore
464 return;
465 }
TeruU417fe022014-02-04 12:59:30 -0800466
Jonathan Hart0444d932014-01-22 15:06:17 -0800467 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
468 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
469
470 Path removedPath = new Path(srcMacAddress, dstMacAddress);
471
472 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800473 // There *shouldn't* be any packets queued if the flow has
474 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800475 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800476 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800477 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800478 }
TeruU417fe022014-02-04 12:59:30 -0800479 pendingFlows.remove(removedPath);
480 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800481 }
482 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800483
TeruU417fe022014-02-04 12:59:30 -0800484 private void flowInstalled(FlowPath installedFlowPath) {
485 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800486
Jonathan Hart0444d932014-01-22 15:06:17 -0800487 if (!installedFlowPath.installerId().equals(callerId)) {
488 // Not our flow path, ignore
489 return;
TeruU417fe022014-02-04 12:59:30 -0800490 }
491
492 if(installedFlowPath.flowEntries().isEmpty()){
493 //If there is no flowEntry, ignore
494 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
495 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800496 }
497
TeruU417fe022014-02-04 12:59:30 -0800498 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
499 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
500 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800501
Jonathan Hart0444d932014-01-22 15:06:17 -0800502 // TODO waiting packets should time out. We could request a path that
503 // can't be installed right now because of a network partition. The path
504 // may eventually be installed, but we may have received thousands of
505 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800506
TeruU417fe022014-02-04 12:59:30 -0800507 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800508 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
509
510 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800511
Jonathan Hart7e6df362013-12-10 23:33:59 -0800512 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800513 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800514
Jonathan Hart0444d932014-01-22 15:06:17 -0800515 if (existingFlow != null) {
516 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800517 existingFlow.firstOutPort = outPort;
518 } else {
519 log.debug("ExistingFlow {} is null", installedPath);
520 return;
521 }
522
TeruU6464af02014-02-06 21:38:45 -0800523 //Check both existing flow are installed status.
524 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800525 packets = waitingPackets.removeAll(existingFlow.flowId);
526 if(log.isDebugEnabled()){
527 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
528 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
529 }
TeruU417fe022014-02-04 12:59:30 -0800530 }else{
531 log.debug("Forward or reverse flows hasn't been pushed yet. return");
532 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800533 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800534 }
TeruU417fe022014-02-04 12:59:30 -0800535
Jonathan Hart5e448782013-12-10 12:36:35 -0800536 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800537 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800538 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800539 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
540 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800541 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800542
543 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
544 po.getActions().add(new OFActionOutput(outPort));
545 po.setActionsLength((short)
546 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
547 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
548
549 flowPusher.add(sw, po);
550 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800551
Jonathan Hart1caaa932013-11-04 15:28:28 -0800552}