blob: 0ee446670e552feb47458c6d9978c67f603722b8 [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.util.MACAddress;
Jonathan Hartdc3ad702013-11-14 11:34:59 -080021import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hartd857ad62013-12-14 18:08:17 -080022import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080023import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart7804bea2014-01-07 10:50:52 -080024import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0444d932014-01-22 15:06:17 -080025import net.onrc.onos.ofcontroller.proxyarp.IProxyArpService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080026import net.onrc.onos.ofcontroller.util.CallerId;
27import net.onrc.onos.ofcontroller.util.DataPath;
28import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080029import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080030import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080031import net.onrc.onos.ofcontroller.util.FlowId;
32import net.onrc.onos.ofcontroller.util.FlowPath;
33import net.onrc.onos.ofcontroller.util.FlowPathType;
34import net.onrc.onos.ofcontroller.util.FlowPathUserState;
35import net.onrc.onos.ofcontroller.util.Port;
36import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hart96892d12014-03-26 20:21:29 -070037import net.onrc.onos.packet.Ethernet;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070038import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080039
40import org.openflow.protocol.OFMessage;
41import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080042import org.openflow.protocol.OFPacketOut;
43import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080044import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080045import org.openflow.protocol.action.OFAction;
46import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080047import org.openflow.util.HexString;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Jonathan Hartd857ad62013-12-14 18:08:17 -080051import com.google.common.collect.LinkedListMultimap;
52import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080053
54public class Forwarding implements IOFMessageListener, IFloodlightModule,
55 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080056 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080057
Jonathan Hart7e6df362013-12-10 23:33:59 -080058 private final int IDLE_TIMEOUT = 5; // seconds
59 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080060 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
61 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
62
63 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080064
65 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080066
Jonathan Hart1caaa932013-11-04 15:28:28 -080067 private IFloodlightProviderService floodlightProvider;
Jonathan Hart7e6df362013-12-10 23:33:59 -080068 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080069 private IDatagridService datagrid;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070070 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080071
Jonathan Harte789d6e2013-12-17 17:50:11 -080072 // TODO it seems there is a Guava collection that will time out entries.
73 // We should see if this will work here.
74 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080075 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080076
Jonathan Hart7e6df362013-12-10 23:33:59 -080077 private final Object lock = new Object();
78
Jonathan Harte789d6e2013-12-17 17:50:11 -080079 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080080 public final OFPacketOut packet;
81 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080082
Jonathan Hart5e448782013-12-10 12:36:35 -080083 public PacketToPush(OFPacketOut packet, long dpid) {
84 this.packet = packet;
85 this.dpid = dpid;
86 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080087 }
88
Jonathan Harte789d6e2013-12-17 17:50:11 -080089 private class PushedFlow {
90 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -080091 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -080092 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -080093
94 public PushedFlow(long flowId) {
95 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -080096 }
97 }
98
99 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800100 public final MACAddress srcMac;
101 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800102
Jonathan Hart0444d932014-01-22 15:06:17 -0800103 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800104 this.srcMac = srcMac;
105 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800106 }
107
108 @Override
109 public boolean equals(Object other) {
110 if (!(other instanceof Path)) {
111 return false;
112 }
113
114 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800115 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800116 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800117 }
118
119 @Override
120 public int hashCode() {
121 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800122 hash = 31 * hash + srcMac.hashCode();
123 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800124 return hash;
125 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800126
127 @Override
128 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800129 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800130 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800131 }
132
Jonathan Harte93aed42013-12-05 18:39:50 -0800133 @Override
134 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800135 List<Class<? extends IFloodlightService>> services =
136 new ArrayList<Class<? extends IFloodlightService>>(1);
137 services.add(IForwardingService.class);
138 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800139 }
140
141 @Override
142 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800143 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
144 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
145 impls.put(IForwardingService.class, this);
146 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800147 }
148
149 @Override
150 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
151 List<Class<? extends IFloodlightService>> dependencies =
152 new ArrayList<Class<? extends IFloodlightService>>();
153 dependencies.add(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800154 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800155 dependencies.add(IOnosDeviceService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700156 dependencies.add(IControllerRegistryService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800157 // We don't use the IProxyArpService directly, but reactive forwarding
158 // requires it to be loaded and answering ARP requests
159 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800160 return dependencies;
161 }
162
163 @Override
164 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800165 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800166 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800167 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800168 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700169 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800170
171 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800172
Jonathan Harte789d6e2013-12-17 17:50:11 -0800173 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800174 waitingPackets = LinkedListMultimap.create();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800175 }
176
Jonathan Harte93aed42013-12-05 18:39:50 -0800177 @Override
178 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800179 // no-op
180 }
181
182 @Override
183 public String getName() {
184 return "onosforwarding";
185 }
186
187 @Override
188 public boolean isCallbackOrderingPrereq(OFType type, String name) {
189 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800190 (name.equals("devicemanager") || name.equals("proxyarpmanager")
191 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800192 }
193
194 @Override
195 public boolean isCallbackOrderingPostreq(OFType type, String name) {
196 return false;
197 }
198
199 @Override
200 public Command receive(
201 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
202
203 if (msg.getType() != OFType.PACKET_IN) {
204 return Command.CONTINUE;
205 }
206
207 OFPacketIn pi = (OFPacketIn) msg;
208
209 Ethernet eth = IFloodlightProviderService.bcStore.
210 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
211
TeruU6464af02014-02-06 21:38:45 -0800212 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
213
Jonathan Hart17672992013-12-12 16:15:16 -0800214 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800215 return Command.CONTINUE;
216 }
217
Jonathan Hart17672992013-12-12 16:15:16 -0800218 if (eth.isBroadcast() || eth.isMulticast()) {
219 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800220 }
221 else {
222 // Unicast
223 handlePacketIn(sw, pi, eth);
224 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800225
226 return Command.STOP;
227 }
228
Jonathan Hart17672992013-12-12 16:15:16 -0800229 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
230 if (log.isTraceEnabled()) {
231 log.trace("Sending broadcast packet to other ONOS instances");
232 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800233
234 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800235 eth.serialize(), null, sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800236 }
237
TeruU417fe022014-02-04 12:59:30 -0800238 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
239 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
240
Jonathan Hart5e448782013-12-10 12:36:35 -0800241 String destinationMac =
242 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800243
TeruUd1ba0e22014-02-10 11:44:15 -0800244 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700245 // TODO: Fix the code below after deviceStorage was removed
246 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800247 try{
248 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800249 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800250 if (deviceObject == null) {
251 log.debug("No device entry found for {}",
252 destinationMac);
253
254 //Device is not in the DB, so wait it until the device is added.
255 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
256 return;
257 }
258
259 continueHandlePacketIn(sw, pi, eth, deviceObject);
260 } finally {
261 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800262 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700263 */
TeruU417fe022014-02-04 12:59:30 -0800264 }
265
266 private class WaitDeviceArp implements Runnable {
267 IOFSwitch sw;
268 OFPacketIn pi;
269 Ethernet eth;
270
271 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
272 super();
273 this.sw = sw;
274 this.pi = pi;
275 this.eth = eth;
276 }
277
278 @Override
279 public void run() {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700280 // TODO: Fix the code below after deviceStorage was removed
281 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800282 try {
TeruU417fe022014-02-04 12:59:30 -0800283 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800284 if(deviceObject == null){
285 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
286 handleBroadcast(sw, pi, eth);
287 return;
288 }
289 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
290 continueHandlePacketIn(sw, pi, eth, deviceObject);
291 } finally {
292 deviceStorage.rollback();
293 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700294 */
TeruU417fe022014-02-04 12:59:30 -0800295 }
296 }
297
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -0700298 // TODO: Fix the code below because IDeviceObject was removed
299 /*
TeruU417fe022014-02-04 12:59:30 -0800300 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
301 log.debug("Start continuehandlePacketIn");
302
Jonathan Hart5e448782013-12-10 12:36:35 -0800303 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800304 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800305 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800306 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800307 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800308 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800309 }
TeruU417fe022014-02-04 12:59:30 -0800310
TeruU6464af02014-02-06 21:38:45 -0800311
TeruU417fe022014-02-04 12:59:30 -0800312 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800313 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800314
Jonathan Hart1caaa932013-11-04 15:28:28 -0800315 short destinationPort = portObject.getNumber();
316 ISwitchObject switchObject = portObject.getSwitch();
317 long destinationDpid = HexString.toLong(switchObject.getDPID());
318
Jonathan Hart41d1e912013-11-24 16:50:25 -0800319 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800320 SwitchPort srcSwitchPort = new SwitchPort(
321 new Dpid(sw.getId()), new Port(pi.getInPort()));
322 SwitchPort dstSwitchPort = new SwitchPort(
323 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800324
325 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
326 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800327
TeruU6464af02014-02-06 21:38:45 -0800328 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800329
Jonathan Hart7e6df362013-12-10 23:33:59 -0800330 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800331 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800332 Path pathspec = new Path(srcMacAddress, dstMacAddress);
333
Jonathan Harte789d6e2013-12-17 17:50:11 -0800334 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800335
TeruU6464af02014-02-06 21:38:45 -0800336 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800337 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800338 // We've already start to install a flow for this pair of MAC addresses
339 if(log.isDebugEnabled()) {
340 log.debug("Found existing the same pathspec {}, Flow ID is {}",
341 pathspec,
342 HexString.toHexString(existingFlow.flowId));
343 }
344
Jonathan Hart7e6df362013-12-10 23:33:59 -0800345 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800346
Jonathan Hart0444d932014-01-22 15:06:17 -0800347 // Find the correct port here. We just assume the PI is from
348 // the first hop switch, but this is definitely not always
349 // the case. We'll have to retrieve the flow from HZ every time
350 // because it could change (be rerouted) sometimes.
351 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800352 // Flow has been sent to the switches so it is safe to
353 // send a packet out now
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700354
355 //
356 // TODO: The getFlow() call below needs
357 // to be updated to the new Path Intent
358 // framework.
359 //
360 // FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
361 FlowPath flow = null;
Jonathan Hart0444d932014-01-22 15:06:17 -0800362 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800363
364 if (flow != null) {
365 for (FlowEntry flowEntry : flow.flowEntries()) {
366 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
367 flowEntryForThisSwitch = flowEntry;
368 break;
369 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800370 }
371 }
372
373 if (flowEntryForThisSwitch == null) {
374 // If we don't find a flow entry for that switch, then we're
375 // in the middle of a rerouting (or something's gone wrong).
376 // This packet will be dropped as a victim of the rerouting.
377 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
378 new Object[] {new FlowId(existingFlow.flowId),
379 srcMacAddress, dstMacAddress, flow});
380 }
381 else {
TeruU417fe022014-02-04 12:59:30 -0800382 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800383 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
384 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800385 }
386 else {
TeruU6464af02014-02-06 21:38:45 -0800387 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800388 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800389 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
390 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800391 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800392 return;
393 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800394
Jonathan Hart7e6df362013-12-10 23:33:59 -0800395 log.debug("Adding new flow between {} at {} and {} at {}",
396 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800397
Jonathan Hart7e6df362013-12-10 23:33:59 -0800398 DataPath datapath = new DataPath();
399 datapath.setSrcPort(srcSwitchPort);
400 datapath.setDstPort(dstSwitchPort);
401
402 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800403 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800404
405 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
406 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
407 flowPath.setFlowEntryMatch(new FlowEntryMatch());
408 flowPath.setIdleTimeout(IDLE_TIMEOUT);
409 flowPath.setHardTimeout(HARD_TIMEOUT);
410 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
411 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
412 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
413 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800414
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700415 FlowId flowId = new FlowId(controllerRegistryService.getNextUniqueId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800416
417 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800418
Jonathan Hart7e6df362013-12-10 23:33:59 -0800419 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800420
421 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800422 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800423 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800424 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
425 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800426 }
TeruU6464af02014-02-06 21:38:45 -0800427
TeruU417fe022014-02-04 12:59:30 -0800428 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800429 srcMacAddress, dstMacAddress, flowPath.flowId()});
Pavlin Radoslavov39f0f2e2014-03-20 12:04:57 -0700430 // TODO: Add the flow by using the new Path Intent framework
431 // flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800432 }
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -0700433 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800434
Jonathan Hart7e6df362013-12-10 23:33:59 -0800435 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800436 OFPacketOut po = new OFPacketOut();
437 po.setInPort(OFPort.OFPP_NONE)
438 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800439 .setActions(new ArrayList<OFAction>())
440 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800441
442 if (sw.getBuffers() == 0) {
443 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
444 .setPacketData(pi.getPacketData())
445 .setLengthU(po.getLengthU() + po.getPacketData().length);
446 }
447 else {
448 po.setBufferId(pi.getBufferId());
449 }
450
Jonathan Hart5e448782013-12-10 12:36:35 -0800451 return po;
452 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800453
Jonathan Hart5e448782013-12-10 12:36:35 -0800454 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800455 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
456 for (FlowPath flowPath : installedFlowPaths) {
457 flowInstalled(flowPath);
458 }
459 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800460
461 @Override
462 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800463 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800464 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800465 }
466
467
Jonathan Hart0444d932014-01-22 15:06:17 -0800468 if (!removedFlowPath.installerId().equals(callerId)) {
469 // Not our flow path, ignore
470 return;
471 }
TeruU417fe022014-02-04 12:59:30 -0800472
Jonathan Hart0444d932014-01-22 15:06:17 -0800473 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
474 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
475
476 Path removedPath = new Path(srcMacAddress, dstMacAddress);
477
478 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800479 // There *shouldn't* be any packets queued if the flow has
480 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800481 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800482 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800483 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800484 }
TeruU417fe022014-02-04 12:59:30 -0800485 pendingFlows.remove(removedPath);
486 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800487 }
488 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800489
TeruU417fe022014-02-04 12:59:30 -0800490 private void flowInstalled(FlowPath installedFlowPath) {
491 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800492
Jonathan Hart0444d932014-01-22 15:06:17 -0800493 if (!installedFlowPath.installerId().equals(callerId)) {
494 // Not our flow path, ignore
495 return;
TeruU417fe022014-02-04 12:59:30 -0800496 }
497
498 if(installedFlowPath.flowEntries().isEmpty()){
499 //If there is no flowEntry, ignore
500 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
501 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800502 }
503
TeruU417fe022014-02-04 12:59:30 -0800504 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
505 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
506 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800507
Jonathan Hart0444d932014-01-22 15:06:17 -0800508 // TODO waiting packets should time out. We could request a path that
509 // can't be installed right now because of a network partition. The path
510 // may eventually be installed, but we may have received thousands of
511 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800512
TeruU417fe022014-02-04 12:59:30 -0800513 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800514 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
515
516 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800517
Jonathan Hart7e6df362013-12-10 23:33:59 -0800518 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800519 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800520
Jonathan Hart0444d932014-01-22 15:06:17 -0800521 if (existingFlow != null) {
522 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800523 existingFlow.firstOutPort = outPort;
524 } else {
525 log.debug("ExistingFlow {} is null", installedPath);
526 return;
527 }
528
TeruU6464af02014-02-06 21:38:45 -0800529 //Check both existing flow are installed status.
530 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800531 packets = waitingPackets.removeAll(existingFlow.flowId);
532 if(log.isDebugEnabled()){
533 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
534 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
535 }
TeruU417fe022014-02-04 12:59:30 -0800536 }else{
537 log.debug("Forward or reverse flows hasn't been pushed yet. return");
538 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800539 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800540 }
TeruU417fe022014-02-04 12:59:30 -0800541
Jonathan Hart5e448782013-12-10 12:36:35 -0800542 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800543 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800544 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800545 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
546 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800547 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800548
549 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
550 po.getActions().add(new OFActionOutput(outPort));
551 po.setActionsLength((short)
552 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
553 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
554
555 flowPusher.add(sw, po);
556 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800557
Jonathan Hart1caaa932013-11-04 15:28:28 -0800558}