blob: 479f1704017f6acc4012e86be0b2c17fddd4b67b [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 Hart1caaa932013-11-04 15:28:28 -080029import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080030import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart7804bea2014-01-07 10:50:52 -080031import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0444d932014-01-22 15:06:17 -080032import net.onrc.onos.ofcontroller.proxyarp.IProxyArpService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080033import net.onrc.onos.ofcontroller.topology.TopologyManager;
34import net.onrc.onos.ofcontroller.util.CallerId;
35import net.onrc.onos.ofcontroller.util.DataPath;
36import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080037import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080038import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080039import net.onrc.onos.ofcontroller.util.FlowId;
40import net.onrc.onos.ofcontroller.util.FlowPath;
41import net.onrc.onos.ofcontroller.util.FlowPathType;
42import net.onrc.onos.ofcontroller.util.FlowPathUserState;
43import net.onrc.onos.ofcontroller.util.Port;
44import net.onrc.onos.ofcontroller.util.SwitchPort;
45
46import org.openflow.protocol.OFMessage;
47import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080048import org.openflow.protocol.OFPacketOut;
49import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080050import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080051import org.openflow.protocol.action.OFAction;
52import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080053import org.openflow.util.HexString;
54import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
Jonathan Hartd857ad62013-12-14 18:08:17 -080057import com.google.common.collect.LinkedListMultimap;
58import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080059
60public class Forwarding implements IOFMessageListener, IFloodlightModule,
61 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080062 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080063
Jonathan Hart7e6df362013-12-10 23:33:59 -080064 private final int IDLE_TIMEOUT = 5; // seconds
65 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080066 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
67 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
68
69 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080070
71 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080072
Jonathan Hart1caaa932013-11-04 15:28:28 -080073 private IFloodlightProviderService floodlightProvider;
74 private IFlowService flowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080075 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080076 private IDatagridService datagrid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080077
78 private IDeviceStorage deviceStorage;
79 private TopologyManager topologyService;
80
Jonathan Harte789d6e2013-12-17 17:50:11 -080081 // TODO it seems there is a Guava collection that will time out entries.
82 // We should see if this will work here.
83 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080084 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080085
Jonathan Hart7e6df362013-12-10 23:33:59 -080086 private final Object lock = new Object();
87
Jonathan Harte789d6e2013-12-17 17:50:11 -080088 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080089 public final OFPacketOut packet;
90 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080091
Jonathan Hart5e448782013-12-10 12:36:35 -080092 public PacketToPush(OFPacketOut packet, long dpid) {
93 this.packet = packet;
94 this.dpid = dpid;
95 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080096 }
97
Jonathan Harte789d6e2013-12-17 17:50:11 -080098 private class PushedFlow {
99 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -0800100 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -0800101 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800102
103 public PushedFlow(long flowId) {
104 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800105 }
106 }
107
108 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800109 public final MACAddress srcMac;
110 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800111
Jonathan Hart0444d932014-01-22 15:06:17 -0800112 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800113 this.srcMac = srcMac;
114 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800115 }
116
117 @Override
118 public boolean equals(Object other) {
119 if (!(other instanceof Path)) {
120 return false;
121 }
122
123 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800124 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800125 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800126 }
127
128 @Override
129 public int hashCode() {
130 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800131 hash = 31 * hash + srcMac.hashCode();
132 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800133 return hash;
134 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800135
136 @Override
137 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800138 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800139 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800140 }
141
Jonathan Harte93aed42013-12-05 18:39:50 -0800142 @Override
143 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800144 List<Class<? extends IFloodlightService>> services =
145 new ArrayList<Class<? extends IFloodlightService>>(1);
146 services.add(IForwardingService.class);
147 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800148 }
149
150 @Override
151 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800152 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
153 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
154 impls.put(IForwardingService.class, this);
155 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800156 }
157
158 @Override
159 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
160 List<Class<? extends IFloodlightService>> dependencies =
161 new ArrayList<Class<? extends IFloodlightService>>();
162 dependencies.add(IFloodlightProviderService.class);
163 dependencies.add(IFlowService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800164 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800165 dependencies.add(IOnosDeviceService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800166 // We don't use the IProxyArpService directly, but reactive forwarding
167 // requires it to be loaded and answering ARP requests
168 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800169 return dependencies;
170 }
171
172 @Override
173 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800174 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800175 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800176 flowService = context.getServiceImpl(IFlowService.class);
177 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800178 datagrid = context.getServiceImpl(IDatagridService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800179
180 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800181
Jonathan Harte789d6e2013-12-17 17:50:11 -0800182 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800183 waitingPackets = LinkedListMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800184
Jonathan Hart1caaa932013-11-04 15:28:28 -0800185 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800186 deviceStorage.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800187 topologyService = new TopologyManager();
yoshitomob292c622013-11-23 14:35:58 -0800188 topologyService.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800189 }
190
Jonathan Harte93aed42013-12-05 18:39:50 -0800191 @Override
192 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800193 // no-op
194 }
195
196 @Override
197 public String getName() {
198 return "onosforwarding";
199 }
200
201 @Override
202 public boolean isCallbackOrderingPrereq(OFType type, String name) {
203 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800204 (name.equals("devicemanager") || name.equals("proxyarpmanager")
205 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800206 }
207
208 @Override
209 public boolean isCallbackOrderingPostreq(OFType type, String name) {
210 return false;
211 }
212
213 @Override
214 public Command receive(
215 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
216
217 if (msg.getType() != OFType.PACKET_IN) {
218 return Command.CONTINUE;
219 }
220
221 OFPacketIn pi = (OFPacketIn) msg;
222
223 Ethernet eth = IFloodlightProviderService.bcStore.
224 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
225
Jonathan Hart17672992013-12-12 16:15:16 -0800226 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800227 return Command.CONTINUE;
228 }
229
Jonathan Hart17672992013-12-12 16:15:16 -0800230 if (eth.isBroadcast() || eth.isMulticast()) {
231 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800232 }
233 else {
234 // Unicast
235 handlePacketIn(sw, pi, eth);
236 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800237
238 return Command.STOP;
239 }
240
Jonathan Hart17672992013-12-12 16:15:16 -0800241 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
242 if (log.isTraceEnabled()) {
243 log.trace("Sending broadcast packet to other ONOS instances");
244 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800245
246 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
247 eth.serialize(), sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800248 }
249
TeruU417fe022014-02-04 12:59:30 -0800250 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
251 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
252
Jonathan Hart5e448782013-12-10 12:36:35 -0800253 String destinationMac =
254 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800255
TeruU417fe022014-02-04 12:59:30 -0800256 //FIXME TitanTransaction opened here probably needs to be either commit()/rollback() to avoid transaction leak, before exiting run().
257 //But it seems that IDeviceStorage does not provide a way to close transaction properly.
258 //Also getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800259 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
260 destinationMac);
261
262 if (deviceObject == null) {
TeruU417fe022014-02-04 12:59:30 -0800263 log.debug("No device entry found for {}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800264 destinationMac);
TeruU417fe022014-02-04 12:59:30 -0800265
266 //Device is not in the DB, so wait it until the device is added.
267 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800268 return;
269 }
270
TeruU417fe022014-02-04 12:59:30 -0800271 continueHandlePacketIn(sw, pi, eth, deviceObject);
272 }
273
274 private class WaitDeviceArp implements Runnable {
275 IOFSwitch sw;
276 OFPacketIn pi;
277 Ethernet eth;
278
279 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
280 super();
281 this.sw = sw;
282 this.pi = pi;
283 this.eth = eth;
284 }
285
286 @Override
287 public void run() {
288 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
289 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 }
297 }
298
299 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
300 log.debug("Start continuehandlePacketIn");
301
Jonathan Hart5e448782013-12-10 12:36:35 -0800302 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800303 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800304 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800305 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800306 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800307 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800308 }
TeruU417fe022014-02-04 12:59:30 -0800309
310 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800311 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800312
Jonathan Hart1caaa932013-11-04 15:28:28 -0800313 short destinationPort = portObject.getNumber();
314 ISwitchObject switchObject = portObject.getSwitch();
315 long destinationDpid = HexString.toLong(switchObject.getDPID());
316
Jonathan Hart41d1e912013-11-24 16:50:25 -0800317 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800318 SwitchPort srcSwitchPort = new SwitchPort(
319 new Dpid(sw.getId()), new Port(pi.getInPort()));
320 SwitchPort dstSwitchPort = new SwitchPort(
321 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800322
323 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
324 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800325
Jonathan Hart7e6df362013-12-10 23:33:59 -0800326 FlowPath flowPath, reverseFlowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800327
Jonathan Hart7e6df362013-12-10 23:33:59 -0800328 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800329 //TODO check concurrency
330 Path pathspec = new Path(srcMacAddress, dstMacAddress);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800331 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800332
333 if (existingFlow != null) {
334 // We've already installed a flow for this pair of MAC addresses
TeruU417fe022014-02-04 12:59:30 -0800335 log.debug("Found existing same pathspec {}, Flow ID is {}",
336 pathspec, HexString.toHexString(existingFlow.flowId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800337 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800338
Jonathan Hart0444d932014-01-22 15:06:17 -0800339 // Find the correct port here. We just assume the PI is from
340 // the first hop switch, but this is definitely not always
341 // the case. We'll have to retrieve the flow from HZ every time
342 // because it could change (be rerouted) sometimes.
343 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800344 // Flow has been sent to the switches so it is safe to
345 // send a packet out now
Jonathan Hart0444d932014-01-22 15:06:17 -0800346 FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
347 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800348
349 if (flow != null) {
350 for (FlowEntry flowEntry : flow.flowEntries()) {
351 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
352 flowEntryForThisSwitch = flowEntry;
353 break;
354 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800355 }
356 }
357
358 if (flowEntryForThisSwitch == null) {
359 // If we don't find a flow entry for that switch, then we're
360 // in the middle of a rerouting (or something's gone wrong).
361 // This packet will be dropped as a victim of the rerouting.
362 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
363 new Object[] {new FlowId(existingFlow.flowId),
364 srcMacAddress, dstMacAddress, flow});
365 }
366 else {
TeruU417fe022014-02-04 12:59:30 -0800367 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800368 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
369 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800370 }
371 else {
TeruU417fe022014-02-04 12:59:30 -0800372 //log.debug("Existing Flow ID {} is not installed. Continue to overwrite.",Long.toHexString(existingFlow.flowId) );
373 // Flow has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800374 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800375 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
376 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800377 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800378 return;
379 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800380
Jonathan Hart7e6df362013-12-10 23:33:59 -0800381 log.debug("Adding new flow between {} at {} and {} at {}",
382 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800383
Jonathan Hart7e6df362013-12-10 23:33:59 -0800384 DataPath datapath = new DataPath();
385 datapath.setSrcPort(srcSwitchPort);
386 datapath.setDstPort(dstSwitchPort);
387
388 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800389 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800390
391 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
392 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
393 flowPath.setFlowEntryMatch(new FlowEntryMatch());
394 flowPath.setIdleTimeout(IDLE_TIMEOUT);
395 flowPath.setHardTimeout(HARD_TIMEOUT);
396 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
397 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
398 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
399 flowPath.setDataPath(datapath);
400
401
402 DataPath reverseDataPath = new DataPath();
403 // Reverse the ports for the reverse path
404 reverseDataPath.setSrcPort(dstSwitchPort);
405 reverseDataPath.setDstPort(srcSwitchPort);
406
407 // TODO implement copy constructor for FlowPath
408 reverseFlowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800409 reverseFlowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800410 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
411 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
412 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
413 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
414 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
415 // Reverse the MAC addresses for the reverse path
416 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
417 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
418 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
419 reverseFlowPath.setDataPath(reverseDataPath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800420
Jonathan Hart7e6df362013-12-10 23:33:59 -0800421 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
422 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
423
424 flowPath.setFlowId(flowId);
425 reverseFlowPath.setFlowId(reverseFlowId);
426
427 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart0444d932014-01-22 15:06:17 -0800428 Path reversePathSpec = new Path(dstMacAddress, srcMacAddress);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800429
430 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800431 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800432 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800433 pendingFlows.put(reversePathSpec, new PushedFlow(reverseFlowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800434 log.debug("Put a Path {} in the pending flow, Flow ID {}", reversePathSpec, reverseFlowId);
435 PacketToPush pp = new PacketToPush(po, sw.getId());
436 waitingPackets.put(flowId.value(), pp);
437
438 log.debug("Put a Packet in the wating list. relatedflowId {}, realatedReversedFlowId {}",
439 flowId, reverseFlowId);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800440 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800441
TeruU417fe022014-02-04 12:59:30 -0800442 log.debug("Adding reverse {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800443 dstMacAddress, srcMacAddress, reverseFlowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800444 flowService.addFlow(reverseFlowPath);
TeruU417fe022014-02-04 12:59:30 -0800445 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800446 srcMacAddress, dstMacAddress, flowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800447 flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800448 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800449
Jonathan Hart7e6df362013-12-10 23:33:59 -0800450 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800451 OFPacketOut po = new OFPacketOut();
452 po.setInPort(OFPort.OFPP_NONE)
453 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800454 .setActions(new ArrayList<OFAction>())
455 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800456
457 if (sw.getBuffers() == 0) {
458 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
459 .setPacketData(pi.getPacketData())
460 .setLengthU(po.getLengthU() + po.getPacketData().length);
461 }
462 else {
463 po.setBufferId(pi.getBufferId());
464 }
465
Jonathan Hart5e448782013-12-10 12:36:35 -0800466 return po;
467 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800468
Jonathan Hart5e448782013-12-10 12:36:35 -0800469 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800470 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
471 for (FlowPath flowPath : installedFlowPaths) {
472 flowInstalled(flowPath);
473 }
474 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800475
476 @Override
477 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800478 if(log.isDebugEnabled()){
479 log.debug("Flow {} was removed, having {} queued packets",
480 removedFlowPath.flowId(), waitingPackets.get(removedFlowPath.flowId().value()).size());
481 }
482
483
Jonathan Hart0444d932014-01-22 15:06:17 -0800484 if (!removedFlowPath.installerId().equals(callerId)) {
485 // Not our flow path, ignore
486 return;
487 }
TeruU417fe022014-02-04 12:59:30 -0800488
Jonathan Hart0444d932014-01-22 15:06:17 -0800489 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
490 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
491
492 Path removedPath = new Path(srcMacAddress, dstMacAddress);
493
494 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800495 // There *shouldn't* be any packets queued if the flow has
496 // just been removed.
497 List<PacketToPush> packets =
498 waitingPackets.removeAll(removedFlowPath.flowId().value());
499 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800500 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800501 }
TeruU417fe022014-02-04 12:59:30 -0800502 pendingFlows.remove(removedPath);
503 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800504 }
505 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800506
TeruU417fe022014-02-04 12:59:30 -0800507 private void flowInstalled(FlowPath installedFlowPath) {
508 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800509
Jonathan Hart0444d932014-01-22 15:06:17 -0800510 if (!installedFlowPath.installerId().equals(callerId)) {
511 // Not our flow path, ignore
512 return;
TeruU417fe022014-02-04 12:59:30 -0800513 }
514
515 if(installedFlowPath.flowEntries().isEmpty()){
516 //If there is no flowEntry, ignore
517 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
518 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800519 }
520
TeruU417fe022014-02-04 12:59:30 -0800521 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
522 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
523 Path installedPath = new Path(srcMacAddress, dstMacAddress);
524 Path reversedInstalledPath = new Path(dstMacAddress, srcMacAddress);
525
Jonathan Hart0444d932014-01-22 15:06:17 -0800526 // TODO waiting packets should time out. We could request a path that
527 // can't be installed right now because of a network partition. The path
528 // may eventually be installed, but we may have received thousands of
529 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800530
TeruU417fe022014-02-04 12:59:30 -0800531 List<PacketToPush> packets;
532 List<PacketToPush> reversedPackets;
533 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
534
535 PushedFlow existingFlow;
536 PushedFlow reversedExistingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800537
Jonathan Hart7e6df362013-12-10 23:33:59 -0800538 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800539 existingFlow = pendingFlows.get(installedPath);
540 reversedExistingFlow = pendingFlows.get(reversedInstalledPath);
541
Jonathan Hart0444d932014-01-22 15:06:17 -0800542 if (existingFlow != null) {
543 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800544 existingFlow.firstOutPort = outPort;
545 } else {
546 log.debug("ExistingFlow {} is null", installedPath);
547 return;
548 }
549
550 if(reversedExistingFlow == null) {
551 log.debug("ReversedExistingFlow {} is null", reversedInstalledPath);
552 return;
553 }
554
555 //Check both existing flow and reversedExisting flow are installed status.
556 if(reversedExistingFlow.installed){
557 packets = waitingPackets.removeAll(existingFlow.flowId);
558 if(log.isDebugEnabled()){
559 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
560 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
561 }
562 reversedPackets = waitingPackets.removeAll(reversedExistingFlow.flowId);
563 if(log.isDebugEnabled()){
564 log.debug("removed my reversed packets {} to push from waitingPackets. outPort {} size {}",
565 Long.toHexString(reversedExistingFlow.flowId), reversedExistingFlow.firstOutPort, reversedPackets.size());
566 }
567 }else{
568 log.debug("Forward or reverse flows hasn't been pushed yet. return");
569 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800570 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800571 }
TeruU417fe022014-02-04 12:59:30 -0800572
Jonathan Hart5e448782013-12-10 12:36:35 -0800573 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800574 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800575 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800576 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
577 }
578
579 for (PacketToPush packet : reversedPackets) {
580 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, reversedExistingFlow.firstOutPort);
581 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
582 sendPacketOut(sw, packet.packet, reversedExistingFlow.firstOutPort);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800583 }
584 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800585
586 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
587 po.getActions().add(new OFActionOutput(outPort));
588 po.setActionsLength((short)
589 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
590 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
591
592 flowPusher.add(sw, po);
593 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800594
Jonathan Hart1caaa932013-11-04 15:28:28 -0800595}