blob: 882d5fa2a30836e8a75e8a9aacc8a266a2fd9db1 [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;
Jonathan Hart1caaa932013-11-04 15:28:28 -08009
10import net.floodlightcontroller.core.FloodlightContext;
11import net.floodlightcontroller.core.IFloodlightProviderService;
12import net.floodlightcontroller.core.IOFMessageListener;
13import net.floodlightcontroller.core.IOFSwitch;
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.packet.Ethernet;
18import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdc3ad702013-11-14 11:34:59 -080019import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080020import net.onrc.onos.ofcontroller.core.IDeviceStorage;
21import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
22import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
23import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
24import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hartd857ad62013-12-14 18:08:17 -080025import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080026import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
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.topology.TopologyManager;
31import net.onrc.onos.ofcontroller.util.CallerId;
32import net.onrc.onos.ofcontroller.util.DataPath;
33import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080034import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080035import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080036import net.onrc.onos.ofcontroller.util.FlowId;
37import net.onrc.onos.ofcontroller.util.FlowPath;
38import net.onrc.onos.ofcontroller.util.FlowPathType;
39import net.onrc.onos.ofcontroller.util.FlowPathUserState;
40import net.onrc.onos.ofcontroller.util.Port;
41import net.onrc.onos.ofcontroller.util.SwitchPort;
42
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);
60
Jonathan Hart7e6df362013-12-10 23:33:59 -080061 private final int IDLE_TIMEOUT = 5; // seconds
62 private final int HARD_TIMEOUT = 0; // seconds
Jonathan Hart0444d932014-01-22 15:06:17 -080063
64 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080065
Jonathan Hart1caaa932013-11-04 15:28:28 -080066 private IFloodlightProviderService floodlightProvider;
67 private IFlowService flowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080068 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080069 private IDatagridService datagrid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080070
71 private IDeviceStorage deviceStorage;
72 private TopologyManager topologyService;
73
Jonathan Harte789d6e2013-12-17 17:50:11 -080074 // TODO it seems there is a Guava collection that will time out entries.
75 // We should see if this will work here.
76 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080077 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080078
Jonathan Hart7e6df362013-12-10 23:33:59 -080079 private final Object lock = new Object();
80
Jonathan Harte789d6e2013-12-17 17:50:11 -080081 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080082 public final OFPacketOut packet;
83 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080084
Jonathan Hart5e448782013-12-10 12:36:35 -080085 public PacketToPush(OFPacketOut packet, long dpid) {
86 this.packet = packet;
87 this.dpid = dpid;
88 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080089 }
90
Jonathan Harte789d6e2013-12-17 17:50:11 -080091 private class PushedFlow {
92 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -080093 public boolean installed = false;
Jonathan Harte789d6e2013-12-17 17:50:11 -080094
95 public PushedFlow(long flowId) {
96 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -080097 }
98 }
99
100 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800101 public final MACAddress srcMac;
102 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800103
Jonathan Hart0444d932014-01-22 15:06:17 -0800104 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800105 this.srcMac = srcMac;
106 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800107 }
108
109 @Override
110 public boolean equals(Object other) {
111 if (!(other instanceof Path)) {
112 return false;
113 }
114
115 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800116 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800117 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800118 }
119
120 @Override
121 public int hashCode() {
122 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800123 hash = 31 * hash + srcMac.hashCode();
124 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800125 return hash;
126 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800127
128 @Override
129 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800130 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800131 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800132 }
133
Jonathan Harte93aed42013-12-05 18:39:50 -0800134 @Override
135 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800136 List<Class<? extends IFloodlightService>> services =
137 new ArrayList<Class<? extends IFloodlightService>>(1);
138 services.add(IForwardingService.class);
139 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800140 }
141
142 @Override
143 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800144 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
145 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
146 impls.put(IForwardingService.class, this);
147 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800148 }
149
150 @Override
151 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
152 List<Class<? extends IFloodlightService>> dependencies =
153 new ArrayList<Class<? extends IFloodlightService>>();
154 dependencies.add(IFloodlightProviderService.class);
155 dependencies.add(IFlowService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800156 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800157 dependencies.add(IOnosDeviceService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800158 // We don't use the IProxyArpService directly, but reactive forwarding
159 // requires it to be loaded and answering ARP requests
160 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800161 return dependencies;
162 }
163
164 @Override
165 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800166 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800167 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800168 flowService = context.getServiceImpl(IFlowService.class);
169 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800170 datagrid = context.getServiceImpl(IDatagridService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800171
172 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800173
Jonathan Harte789d6e2013-12-17 17:50:11 -0800174 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800175 waitingPackets = LinkedListMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800176
Jonathan Hart1caaa932013-11-04 15:28:28 -0800177 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800178 deviceStorage.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800179 topologyService = new TopologyManager();
yoshitomob292c622013-11-23 14:35:58 -0800180 topologyService.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800181 }
182
Jonathan Harte93aed42013-12-05 18:39:50 -0800183 @Override
184 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800185 // no-op
186 }
187
188 @Override
189 public String getName() {
190 return "onosforwarding";
191 }
192
193 @Override
194 public boolean isCallbackOrderingPrereq(OFType type, String name) {
195 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800196 (name.equals("devicemanager") || name.equals("proxyarpmanager")
197 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800198 }
199
200 @Override
201 public boolean isCallbackOrderingPostreq(OFType type, String name) {
202 return false;
203 }
204
205 @Override
206 public Command receive(
207 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
208
209 if (msg.getType() != OFType.PACKET_IN) {
210 return Command.CONTINUE;
211 }
212
213 OFPacketIn pi = (OFPacketIn) msg;
214
215 Ethernet eth = IFloodlightProviderService.bcStore.
216 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
217
Jonathan Hart17672992013-12-12 16:15:16 -0800218 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800219 return Command.CONTINUE;
220 }
221
Jonathan Hart17672992013-12-12 16:15:16 -0800222 if (eth.isBroadcast() || eth.isMulticast()) {
223 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800224 }
225 else {
226 // Unicast
227 handlePacketIn(sw, pi, eth);
228 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800229
230 return Command.STOP;
231 }
232
Jonathan Hart17672992013-12-12 16:15:16 -0800233 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
234 if (log.isTraceEnabled()) {
235 log.trace("Sending broadcast packet to other ONOS instances");
236 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800237
238 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
239 eth.serialize(), sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800240 }
241
Jonathan Hart1caaa932013-11-04 15:28:28 -0800242 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800243 String destinationMac =
244 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800245
246 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
247 destinationMac);
248
249 if (deviceObject == null) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800250 log.debug("No device entry found for {} - broadcasting packet",
251 destinationMac);
252 handleBroadcast(sw, pi, eth);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800253 return;
254 }
255
Jonathan Hart5e448782013-12-10 12:36:35 -0800256 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800257 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800258 log.debug("No attachment point found for device {} - broadcasting packet",
259 destinationMac);
260 handleBroadcast(sw, pi, eth);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800261 return;
262 }
263 IPortObject portObject = ports.next();
264 short destinationPort = portObject.getNumber();
265 ISwitchObject switchObject = portObject.getSwitch();
266 long destinationDpid = HexString.toLong(switchObject.getDPID());
267
Jonathan Hart41d1e912013-11-24 16:50:25 -0800268 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800269 SwitchPort srcSwitchPort = new SwitchPort(
270 new Dpid(sw.getId()), new Port(pi.getInPort()));
271 SwitchPort dstSwitchPort = new SwitchPort(
272 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800273
274 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
275 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800276
Jonathan Hart7e6df362013-12-10 23:33:59 -0800277 FlowPath flowPath, reverseFlowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800278
Jonathan Hart0444d932014-01-22 15:06:17 -0800279 Path pathspec = new Path(srcMacAddress, dstMacAddress);
Jonathan Hart5e448782013-12-10 12:36:35 -0800280 // TODO check concurrency
Jonathan Hart7e6df362013-12-10 23:33:59 -0800281 synchronized (lock) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800282 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800283
284 if (existingFlow != null) {
285 // We've already installed a flow for this pair of MAC addresses
Jonathan Hart7e6df362013-12-10 23:33:59 -0800286 log.debug("Found existing flow {}",
Jonathan Harte789d6e2013-12-17 17:50:11 -0800287 HexString.toHexString(existingFlow.flowId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800288
289 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800290
Jonathan Hart0444d932014-01-22 15:06:17 -0800291 // Find the correct port here. We just assume the PI is from
292 // the first hop switch, but this is definitely not always
293 // the case. We'll have to retrieve the flow from HZ every time
294 // because it could change (be rerouted) sometimes.
295 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800296 // Flow has been sent to the switches so it is safe to
297 // send a packet out now
Jonathan Hart0444d932014-01-22 15:06:17 -0800298 FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
299 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800300
301 if (flow != null) {
302 for (FlowEntry flowEntry : flow.flowEntries()) {
303 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
304 flowEntryForThisSwitch = flowEntry;
305 break;
306 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800307 }
308 }
309
310 if (flowEntryForThisSwitch == null) {
311 // If we don't find a flow entry for that switch, then we're
312 // in the middle of a rerouting (or something's gone wrong).
313 // This packet will be dropped as a victim of the rerouting.
314 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
315 new Object[] {new FlowId(existingFlow.flowId),
316 srcMacAddress, dstMacAddress, flow});
317 }
318 else {
319 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
320 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800321 }
322 else {
323 // Flow has not yet been sent to switches so save the
324 // packet out for later
325 waitingPackets.put(existingFlow.flowId,
326 new PacketToPush(po, sw.getId()));
327 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800328 return;
329 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800330
Jonathan Hart7e6df362013-12-10 23:33:59 -0800331 log.debug("Adding new flow between {} at {} and {} at {}",
332 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800333
Jonathan Hart7e6df362013-12-10 23:33:59 -0800334 DataPath datapath = new DataPath();
335 datapath.setSrcPort(srcSwitchPort);
336 datapath.setDstPort(dstSwitchPort);
337
338 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800339 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800340
341 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
342 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
343 flowPath.setFlowEntryMatch(new FlowEntryMatch());
344 flowPath.setIdleTimeout(IDLE_TIMEOUT);
345 flowPath.setHardTimeout(HARD_TIMEOUT);
346 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
347 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
348 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
349 flowPath.setDataPath(datapath);
350
351
352 DataPath reverseDataPath = new DataPath();
353 // Reverse the ports for the reverse path
354 reverseDataPath.setSrcPort(dstSwitchPort);
355 reverseDataPath.setDstPort(srcSwitchPort);
356
357 // TODO implement copy constructor for FlowPath
358 reverseFlowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800359 reverseFlowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800360 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
361 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
362 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
363 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
364 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
365 // Reverse the MAC addresses for the reverse path
366 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
367 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
368 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
369 reverseFlowPath.setDataPath(reverseDataPath);
370 reverseFlowPath.dataPath().srcPort().dpid().toString();
Jonathan Hart0444d932014-01-22 15:06:17 -0800371
Jonathan Hart7e6df362013-12-10 23:33:59 -0800372 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
373 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
374
375 flowPath.setFlowId(flowId);
376 reverseFlowPath.setFlowId(reverseFlowId);
377
378 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart0444d932014-01-22 15:06:17 -0800379 Path reversePathSpec = new Path(dstMacAddress, srcMacAddress);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800380
381 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800382 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
383 pendingFlows.put(reversePathSpec, new PushedFlow(reverseFlowId.value()));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800384 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
Jonathan Hart4fb16d82013-11-07 22:41:32 -0800385
Jonathan Hart41d1e912013-11-24 16:50:25 -0800386 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800387
Jonathan Hart0444d932014-01-22 15:06:17 -0800388 log.debug("Adding reverse {} to {} flowid {}", new Object[] {
389 dstMacAddress, srcMacAddress, reverseFlowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800390 flowService.addFlow(reverseFlowPath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800391 log.debug("Adding forward {} to {} flowid {}", new Object[] {
392 srcMacAddress, dstMacAddress, flowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800393 flowService.addFlow(flowPath);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800394
Jonathan Hart1caaa932013-11-04 15:28:28 -0800395 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800396
Jonathan Hart7e6df362013-12-10 23:33:59 -0800397 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800398 OFPacketOut po = new OFPacketOut();
399 po.setInPort(OFPort.OFPP_NONE)
400 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800401 .setActions(new ArrayList<OFAction>())
402 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800403
404 if (sw.getBuffers() == 0) {
405 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
406 .setPacketData(pi.getPacketData())
407 .setLengthU(po.getLengthU() + po.getPacketData().length);
408 }
409 else {
410 po.setBufferId(pi.getBufferId());
411 }
412
Jonathan Hart5e448782013-12-10 12:36:35 -0800413 return po;
414 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800415
Jonathan Hart5e448782013-12-10 12:36:35 -0800416 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800417 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
418 for (FlowPath flowPath : installedFlowPaths) {
419 flowInstalled(flowPath);
420 }
421 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800422
423 @Override
424 public void flowRemoved(FlowPath removedFlowPath) {
425 if (!removedFlowPath.installerId().equals(callerId)) {
426 // Not our flow path, ignore
427 return;
428 }
429
430 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
431 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
432
433 Path removedPath = new Path(srcMacAddress, dstMacAddress);
434
435 synchronized (lock) {
436 pendingFlows.remove(removedPath);
437
438 // There *shouldn't* be any packets queued if the flow has
439 // just been removed.
440 List<PacketToPush> packets =
441 waitingPackets.removeAll(removedFlowPath.flowId().value());
442 if (!packets.isEmpty()) {
443 log.warn("Removed flow {} has packets queued",
444 removedFlowPath.flowId());
445 }
446 }
447 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800448
449 private void flowInstalled(FlowPath installedFlowPath) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800450 long flowId = installedFlowPath.flowId().value();
Jonathan Hart5e448782013-12-10 12:36:35 -0800451
Jonathan Hart0444d932014-01-22 15:06:17 -0800452 if (!installedFlowPath.installerId().equals(callerId)) {
453 // Not our flow path, ignore
454 return;
455 }
456
457 // TODO waiting packets should time out. We could request a path that
458 // can't be installed right now because of a network partition. The path
459 // may eventually be installed, but we may have received thousands of
460 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800461 short outPort =
462 installedFlowPath.flowEntries().get(0).outPort().value();
463
464 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
465 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
466
Jonathan Hart7e6df362013-12-10 23:33:59 -0800467 Collection<PacketToPush> packets;
468 synchronized (lock) {
469 packets = waitingPackets.removeAll(flowId);
470
Jonathan Hart0444d932014-01-22 15:06:17 -0800471 log.debug("Flow {} has been installed, sending {} queued packets",
472 installedFlowPath.flowId(), packets.size());
473
Jonathan Harte789d6e2013-12-17 17:50:11 -0800474 // remove pending flows entry
Jonathan Hart0444d932014-01-22 15:06:17 -0800475 Path installedPath = new Path(srcMacAddress, dstMacAddress);
Pavlin Radoslavov3e6a0bc2013-12-17 22:20:36 -0800476 PushedFlow existingFlow = pendingFlows.get(installedPath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800477 if (existingFlow != null) {
478 existingFlow.installed = true;
479 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800480 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800481
482 for (PacketToPush packet : packets) {
483 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
484
Jonathan Harte789d6e2013-12-17 17:50:11 -0800485 sendPacketOut(sw, packet.packet, outPort);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800486 }
487 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800488
489 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
490 po.getActions().add(new OFActionOutput(outPort));
491 po.setActionsLength((short)
492 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
493 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
494
495 flowPusher.add(sw, po);
496 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800497
Jonathan Hart1caaa932013-11-04 15:28:28 -0800498}