blob: bb92cf9bf892a18c59b2083eab293e6511d8b203 [file] [log] [blame]
Jonathan Hart1caaa932013-11-04 15:28:28 -08001package net.onrc.onos.ofcontroller.forwarding;
2
Jonathan Harte93aed42013-12-05 18:39:50 -08003import java.util.ArrayList;
4import 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 Harte93aed42013-12-05 18:39:50 -08007import java.util.List;
8import 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 Hart17672992013-12-12 16:15:16 -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 }
89 }
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();
178 deviceStorage.init("");
179 topologyService = new TopologyManager();
180 topologyService.init("");
181 }
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;
300 for (FlowEntry flowEntry : flow.flowEntries()) {
301 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
302 flowEntryForThisSwitch = flowEntry;
303 break;
304 }
305 }
306
307 if (flowEntryForThisSwitch == null) {
308 // If we don't find a flow entry for that switch, then we're
309 // in the middle of a rerouting (or something's gone wrong).
310 // This packet will be dropped as a victim of the rerouting.
311 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
312 new Object[] {new FlowId(existingFlow.flowId),
313 srcMacAddress, dstMacAddress, flow});
314 }
315 else {
316 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
317 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800318 }
319 else {
320 // Flow has not yet been sent to switches so save the
321 // packet out for later
322 waitingPackets.put(existingFlow.flowId,
323 new PacketToPush(po, sw.getId()));
324 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800325 return;
326 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800327
Jonathan Hart7e6df362013-12-10 23:33:59 -0800328 log.debug("Adding new flow between {} at {} and {} at {}",
329 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
330
Jonathan Hart7e6df362013-12-10 23:33:59 -0800331 DataPath datapath = new DataPath();
332 datapath.setSrcPort(srcSwitchPort);
333 datapath.setDstPort(dstSwitchPort);
334
335 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800336 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800337
338 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
339 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
340 flowPath.setFlowEntryMatch(new FlowEntryMatch());
341 flowPath.setIdleTimeout(IDLE_TIMEOUT);
342 flowPath.setHardTimeout(HARD_TIMEOUT);
343 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
344 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
345 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
346 flowPath.setDataPath(datapath);
347
348
349 DataPath reverseDataPath = new DataPath();
350 // Reverse the ports for the reverse path
351 reverseDataPath.setSrcPort(dstSwitchPort);
352 reverseDataPath.setDstPort(srcSwitchPort);
353
354 // TODO implement copy constructor for FlowPath
355 reverseFlowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800356 reverseFlowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800357 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
358 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
359 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
360 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
361 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
362 // Reverse the MAC addresses for the reverse path
363 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
364 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
365 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
366 reverseFlowPath.setDataPath(reverseDataPath);
367 reverseFlowPath.dataPath().srcPort().dpid().toString();
Jonathan Hart0444d932014-01-22 15:06:17 -0800368
Jonathan Hart7e6df362013-12-10 23:33:59 -0800369 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
370 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
371
372 flowPath.setFlowId(flowId);
373 reverseFlowPath.setFlowId(reverseFlowId);
374
375 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart0444d932014-01-22 15:06:17 -0800376 Path reversePathSpec = new Path(dstMacAddress, srcMacAddress);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800377
378 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800379 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
380 pendingFlows.put(reversePathSpec, new PushedFlow(reverseFlowId.value()));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800381 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
382
Jonathan Hart41d1e912013-11-24 16:50:25 -0800383 }
384
Jonathan Hart0444d932014-01-22 15:06:17 -0800385 log.debug("Adding reverse {} to {} flowid {}", new Object[] {
386 dstMacAddress, srcMacAddress, reverseFlowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800387 flowService.addFlow(reverseFlowPath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800388 log.debug("Adding forward {} to {} flowid {}", new Object[] {
389 srcMacAddress, dstMacAddress, flowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800390 flowService.addFlow(flowPath);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800391
Jonathan Hart1caaa932013-11-04 15:28:28 -0800392 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800393
Jonathan Hart7e6df362013-12-10 23:33:59 -0800394 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800395 OFPacketOut po = new OFPacketOut();
396 po.setInPort(OFPort.OFPP_NONE)
397 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800398 .setActions(new ArrayList<OFAction>())
399 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800400
401 if (sw.getBuffers() == 0) {
402 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
403 .setPacketData(pi.getPacketData())
404 .setLengthU(po.getLengthU() + po.getPacketData().length);
405 }
406 else {
407 po.setBufferId(pi.getBufferId());
408 }
409
Jonathan Hart5e448782013-12-10 12:36:35 -0800410 return po;
411 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800412
Jonathan Hart5e448782013-12-10 12:36:35 -0800413 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800414 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
415 for (FlowPath flowPath : installedFlowPaths) {
416 flowInstalled(flowPath);
417 }
418 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800419
420 @Override
421 public void flowRemoved(FlowPath removedFlowPath) {
422 if (!removedFlowPath.installerId().equals(callerId)) {
423 // Not our flow path, ignore
424 return;
425 }
426
427 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
428 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
429
430 Path removedPath = new Path(srcMacAddress, dstMacAddress);
431
432 synchronized (lock) {
433 pendingFlows.remove(removedPath);
434
435 // There *shouldn't* be any packets queued if the flow has
436 // just been removed.
437 List<PacketToPush> packets =
438 waitingPackets.removeAll(removedFlowPath.flowId().value());
439 if (!packets.isEmpty()) {
440 log.warn("Removed flow {} has packets queued",
441 removedFlowPath.flowId());
442 }
443 }
444 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800445
446 private void flowInstalled(FlowPath installedFlowPath) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800447 long flowId = installedFlowPath.flowId().value();
Jonathan Hart5e448782013-12-10 12:36:35 -0800448
Jonathan Hart0444d932014-01-22 15:06:17 -0800449 if (!installedFlowPath.installerId().equals(callerId)) {
450 // Not our flow path, ignore
451 return;
452 }
453
454 // TODO waiting packets should time out. We could request a path that
455 // can't be installed right now because of a network partition. The path
456 // may eventually be installed, but we may have received thousands of
457 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800458 short outPort =
459 installedFlowPath.flowEntries().get(0).outPort().value();
460
461 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
462 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
463
Jonathan Hart7e6df362013-12-10 23:33:59 -0800464 Collection<PacketToPush> packets;
465 synchronized (lock) {
466 packets = waitingPackets.removeAll(flowId);
467
Jonathan Hart0444d932014-01-22 15:06:17 -0800468 log.debug("Flow {} has been installed, sending {} queued packets",
469 installedFlowPath.flowId(), packets.size());
470
Jonathan Harte789d6e2013-12-17 17:50:11 -0800471 // remove pending flows entry
Jonathan Hart0444d932014-01-22 15:06:17 -0800472 Path installedPath = new Path(srcMacAddress, dstMacAddress);
Pavlin Radoslavov3e6a0bc2013-12-17 22:20:36 -0800473 PushedFlow existingFlow = pendingFlows.get(installedPath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800474 if (existingFlow != null) {
475 existingFlow.installed = true;
476 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800477 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800478
479 for (PacketToPush packet : packets) {
480 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
481
Jonathan Harte789d6e2013-12-17 17:50:11 -0800482 sendPacketOut(sw, packet.packet, outPort);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800483 }
484 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800485
486 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
487 po.getActions().add(new OFActionOutput(outPort));
488 po.setActionsLength((short)
489 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
490 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
491
492 flowPusher.add(sw, po);
493 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800494
Jonathan Hart1caaa932013-11-04 15:28:28 -0800495}