blob: 5ddee15ccc1953c9121277481a0b825ab3a3fb93 [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;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070045import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080046
47import org.openflow.protocol.OFMessage;
48import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080049import org.openflow.protocol.OFPacketOut;
50import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080051import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080052import org.openflow.protocol.action.OFAction;
53import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080054import org.openflow.util.HexString;
55import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
Jonathan Hartd857ad62013-12-14 18:08:17 -080058import com.google.common.collect.LinkedListMultimap;
59import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080060
61public class Forwarding implements IOFMessageListener, IFloodlightModule,
62 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080063 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080064
Jonathan Hart7e6df362013-12-10 23:33:59 -080065 private final int IDLE_TIMEOUT = 5; // seconds
66 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080067 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
68 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
69
70 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080071
72 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080073
Jonathan Hart1caaa932013-11-04 15:28:28 -080074 private IFloodlightProviderService floodlightProvider;
75 private IFlowService flowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080076 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080077 private IDatagridService datagrid;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070078 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080079
80 private IDeviceStorage deviceStorage;
81 private TopologyManager topologyService;
82
Jonathan Harte789d6e2013-12-17 17:50:11 -080083 // TODO it seems there is a Guava collection that will time out entries.
84 // We should see if this will work here.
85 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080086 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080087
Jonathan Hart7e6df362013-12-10 23:33:59 -080088 private final Object lock = new Object();
89
Jonathan Harte789d6e2013-12-17 17:50:11 -080090 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080091 public final OFPacketOut packet;
92 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080093
Jonathan Hart5e448782013-12-10 12:36:35 -080094 public PacketToPush(OFPacketOut packet, long dpid) {
95 this.packet = packet;
96 this.dpid = dpid;
97 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080098 }
99
Jonathan Harte789d6e2013-12-17 17:50:11 -0800100 private class PushedFlow {
101 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -0800102 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -0800103 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800104
105 public PushedFlow(long flowId) {
106 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800107 }
108 }
109
110 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800111 public final MACAddress srcMac;
112 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800113
Jonathan Hart0444d932014-01-22 15:06:17 -0800114 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800115 this.srcMac = srcMac;
116 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800117 }
118
119 @Override
120 public boolean equals(Object other) {
121 if (!(other instanceof Path)) {
122 return false;
123 }
124
125 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800126 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800127 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800128 }
129
130 @Override
131 public int hashCode() {
132 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800133 hash = 31 * hash + srcMac.hashCode();
134 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800135 return hash;
136 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800137
138 @Override
139 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800140 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800141 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800142 }
143
Jonathan Harte93aed42013-12-05 18:39:50 -0800144 @Override
145 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800146 List<Class<? extends IFloodlightService>> services =
147 new ArrayList<Class<? extends IFloodlightService>>(1);
148 services.add(IForwardingService.class);
149 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800150 }
151
152 @Override
153 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800154 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
155 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
156 impls.put(IForwardingService.class, this);
157 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800158 }
159
160 @Override
161 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
162 List<Class<? extends IFloodlightService>> dependencies =
163 new ArrayList<Class<? extends IFloodlightService>>();
164 dependencies.add(IFloodlightProviderService.class);
165 dependencies.add(IFlowService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800166 dependencies.add(IFlowPusherService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800167 dependencies.add(IOnosDeviceService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700168 dependencies.add(IControllerRegistryService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800169 // We don't use the IProxyArpService directly, but reactive forwarding
170 // requires it to be loaded and answering ARP requests
171 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800172 return dependencies;
173 }
174
175 @Override
176 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800177 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800178 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800179 flowService = context.getServiceImpl(IFlowService.class);
180 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800181 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700182 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800183
184 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800185
Jonathan Harte789d6e2013-12-17 17:50:11 -0800186 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800187 waitingPackets = LinkedListMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800188
Jonathan Hart1caaa932013-11-04 15:28:28 -0800189 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800190 deviceStorage.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800191 topologyService = new TopologyManager();
yoshitomob292c622013-11-23 14:35:58 -0800192 topologyService.init("","");
Jonathan Hart1caaa932013-11-04 15:28:28 -0800193 }
194
Jonathan Harte93aed42013-12-05 18:39:50 -0800195 @Override
196 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800197 // no-op
198 }
199
200 @Override
201 public String getName() {
202 return "onosforwarding";
203 }
204
205 @Override
206 public boolean isCallbackOrderingPrereq(OFType type, String name) {
207 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800208 (name.equals("devicemanager") || name.equals("proxyarpmanager")
209 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800210 }
211
212 @Override
213 public boolean isCallbackOrderingPostreq(OFType type, String name) {
214 return false;
215 }
216
217 @Override
218 public Command receive(
219 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
220
221 if (msg.getType() != OFType.PACKET_IN) {
222 return Command.CONTINUE;
223 }
224
225 OFPacketIn pi = (OFPacketIn) msg;
226
227 Ethernet eth = IFloodlightProviderService.bcStore.
228 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
229
TeruU6464af02014-02-06 21:38:45 -0800230 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
231
Jonathan Hart17672992013-12-12 16:15:16 -0800232 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800233 return Command.CONTINUE;
234 }
235
Jonathan Hart17672992013-12-12 16:15:16 -0800236 if (eth.isBroadcast() || eth.isMulticast()) {
237 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800238 }
239 else {
240 // Unicast
241 handlePacketIn(sw, pi, eth);
242 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800243
244 return Command.STOP;
245 }
246
Jonathan Hart17672992013-12-12 16:15:16 -0800247 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
248 if (log.isTraceEnabled()) {
249 log.trace("Sending broadcast packet to other ONOS instances");
250 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800251
252 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800253 eth.serialize(), null, sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800254 }
255
TeruU417fe022014-02-04 12:59:30 -0800256 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
257 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
258
Jonathan Hart5e448782013-12-10 12:36:35 -0800259 String destinationMac =
260 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800261
TeruUd1ba0e22014-02-10 11:44:15 -0800262 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
263 try{
264 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800265 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800266 if (deviceObject == null) {
267 log.debug("No device entry found for {}",
268 destinationMac);
269
270 //Device is not in the DB, so wait it until the device is added.
271 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
272 return;
273 }
274
275 continueHandlePacketIn(sw, pi, eth, deviceObject);
276 } finally {
277 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800278 }
TeruU417fe022014-02-04 12:59:30 -0800279 }
280
281 private class WaitDeviceArp implements Runnable {
282 IOFSwitch sw;
283 OFPacketIn pi;
284 Ethernet eth;
285
286 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
287 super();
288 this.sw = sw;
289 this.pi = pi;
290 this.eth = eth;
291 }
292
293 @Override
294 public void run() {
TeruUd1ba0e22014-02-10 11:44:15 -0800295 try {
TeruU417fe022014-02-04 12:59:30 -0800296 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800297 if(deviceObject == null){
298 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
299 handleBroadcast(sw, pi, eth);
300 return;
301 }
302 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
303 continueHandlePacketIn(sw, pi, eth, deviceObject);
304 } finally {
305 deviceStorage.rollback();
306 }
TeruU417fe022014-02-04 12:59:30 -0800307 }
308 }
309
310 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
311 log.debug("Start continuehandlePacketIn");
312
Jonathan Hart5e448782013-12-10 12:36:35 -0800313 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800314 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800315 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800316 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800317 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800318 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800319 }
TeruU417fe022014-02-04 12:59:30 -0800320
TeruU6464af02014-02-06 21:38:45 -0800321
TeruU417fe022014-02-04 12:59:30 -0800322 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800323 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800324
Jonathan Hart1caaa932013-11-04 15:28:28 -0800325 short destinationPort = portObject.getNumber();
326 ISwitchObject switchObject = portObject.getSwitch();
327 long destinationDpid = HexString.toLong(switchObject.getDPID());
328
Jonathan Hart41d1e912013-11-24 16:50:25 -0800329 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800330 SwitchPort srcSwitchPort = new SwitchPort(
331 new Dpid(sw.getId()), new Port(pi.getInPort()));
332 SwitchPort dstSwitchPort = new SwitchPort(
333 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800334
335 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
336 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800337
TeruU6464af02014-02-06 21:38:45 -0800338 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800339
Jonathan Hart7e6df362013-12-10 23:33:59 -0800340 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800341 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800342 Path pathspec = new Path(srcMacAddress, dstMacAddress);
343
Jonathan Harte789d6e2013-12-17 17:50:11 -0800344 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800345
TeruU6464af02014-02-06 21:38:45 -0800346 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800347 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800348 // We've already start to install a flow for this pair of MAC addresses
349 if(log.isDebugEnabled()) {
350 log.debug("Found existing the same pathspec {}, Flow ID is {}",
351 pathspec,
352 HexString.toHexString(existingFlow.flowId));
353 }
354
Jonathan Hart7e6df362013-12-10 23:33:59 -0800355 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800356
Jonathan Hart0444d932014-01-22 15:06:17 -0800357 // Find the correct port here. We just assume the PI is from
358 // the first hop switch, but this is definitely not always
359 // the case. We'll have to retrieve the flow from HZ every time
360 // because it could change (be rerouted) sometimes.
361 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800362 // Flow has been sent to the switches so it is safe to
363 // send a packet out now
Jonathan Hart0444d932014-01-22 15:06:17 -0800364 FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
365 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800366
367 if (flow != null) {
368 for (FlowEntry flowEntry : flow.flowEntries()) {
369 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
370 flowEntryForThisSwitch = flowEntry;
371 break;
372 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800373 }
374 }
375
376 if (flowEntryForThisSwitch == null) {
377 // If we don't find a flow entry for that switch, then we're
378 // in the middle of a rerouting (or something's gone wrong).
379 // This packet will be dropped as a victim of the rerouting.
380 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
381 new Object[] {new FlowId(existingFlow.flowId),
382 srcMacAddress, dstMacAddress, flow});
383 }
384 else {
TeruU417fe022014-02-04 12:59:30 -0800385 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800386 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
387 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800388 }
389 else {
TeruU6464af02014-02-06 21:38:45 -0800390 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800391 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800392 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
393 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800394 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800395 return;
396 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800397
Jonathan Hart7e6df362013-12-10 23:33:59 -0800398 log.debug("Adding new flow between {} at {} and {} at {}",
399 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800400
Jonathan Hart7e6df362013-12-10 23:33:59 -0800401 DataPath datapath = new DataPath();
402 datapath.setSrcPort(srcSwitchPort);
403 datapath.setDstPort(dstSwitchPort);
404
405 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800406 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800407
408 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
409 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
410 flowPath.setFlowEntryMatch(new FlowEntryMatch());
411 flowPath.setIdleTimeout(IDLE_TIMEOUT);
412 flowPath.setHardTimeout(HARD_TIMEOUT);
413 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
414 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
415 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
416 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800417
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700418 FlowId flowId = new FlowId(controllerRegistryService.getNextUniqueId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800419
420 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800421
Jonathan Hart7e6df362013-12-10 23:33:59 -0800422 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800423
424 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800425 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800426 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800427 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
428 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800429 }
TeruU6464af02014-02-06 21:38:45 -0800430
TeruU417fe022014-02-04 12:59:30 -0800431 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800432 srcMacAddress, dstMacAddress, flowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800433 flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800434 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800435
Jonathan Hart7e6df362013-12-10 23:33:59 -0800436 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800437 OFPacketOut po = new OFPacketOut();
438 po.setInPort(OFPort.OFPP_NONE)
439 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800440 .setActions(new ArrayList<OFAction>())
441 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800442
443 if (sw.getBuffers() == 0) {
444 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
445 .setPacketData(pi.getPacketData())
446 .setLengthU(po.getLengthU() + po.getPacketData().length);
447 }
448 else {
449 po.setBufferId(pi.getBufferId());
450 }
451
Jonathan Hart5e448782013-12-10 12:36:35 -0800452 return po;
453 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800454
Jonathan Hart5e448782013-12-10 12:36:35 -0800455 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800456 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
457 for (FlowPath flowPath : installedFlowPaths) {
458 flowInstalled(flowPath);
459 }
460 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800461
462 @Override
463 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800464 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800465 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800466 }
467
468
Jonathan Hart0444d932014-01-22 15:06:17 -0800469 if (!removedFlowPath.installerId().equals(callerId)) {
470 // Not our flow path, ignore
471 return;
472 }
TeruU417fe022014-02-04 12:59:30 -0800473
Jonathan Hart0444d932014-01-22 15:06:17 -0800474 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
475 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
476
477 Path removedPath = new Path(srcMacAddress, dstMacAddress);
478
479 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800480 // There *shouldn't* be any packets queued if the flow has
481 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800482 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800483 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800484 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800485 }
TeruU417fe022014-02-04 12:59:30 -0800486 pendingFlows.remove(removedPath);
487 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800488 }
489 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800490
TeruU417fe022014-02-04 12:59:30 -0800491 private void flowInstalled(FlowPath installedFlowPath) {
492 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800493
Jonathan Hart0444d932014-01-22 15:06:17 -0800494 if (!installedFlowPath.installerId().equals(callerId)) {
495 // Not our flow path, ignore
496 return;
TeruU417fe022014-02-04 12:59:30 -0800497 }
498
499 if(installedFlowPath.flowEntries().isEmpty()){
500 //If there is no flowEntry, ignore
501 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
502 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800503 }
504
TeruU417fe022014-02-04 12:59:30 -0800505 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
506 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
507 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800508
Jonathan Hart0444d932014-01-22 15:06:17 -0800509 // TODO waiting packets should time out. We could request a path that
510 // can't be installed right now because of a network partition. The path
511 // may eventually be installed, but we may have received thousands of
512 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800513
TeruU417fe022014-02-04 12:59:30 -0800514 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800515 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
516
517 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800518
Jonathan Hart7e6df362013-12-10 23:33:59 -0800519 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800520 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800521
Jonathan Hart0444d932014-01-22 15:06:17 -0800522 if (existingFlow != null) {
523 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800524 existingFlow.firstOutPort = outPort;
525 } else {
526 log.debug("ExistingFlow {} is null", installedPath);
527 return;
528 }
529
TeruU6464af02014-02-06 21:38:45 -0800530 //Check both existing flow are installed status.
531 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800532 packets = waitingPackets.removeAll(existingFlow.flowId);
533 if(log.isDebugEnabled()){
534 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
535 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
536 }
TeruU417fe022014-02-04 12:59:30 -0800537 }else{
538 log.debug("Forward or reverse flows hasn't been pushed yet. return");
539 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800540 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800541 }
TeruU417fe022014-02-04 12:59:30 -0800542
Jonathan Hart5e448782013-12-10 12:36:35 -0800543 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800544 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800545 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800546 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
547 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800548 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800549
550 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
551 po.getActions().add(new OFActionOutput(outPort));
552 po.setActionsLength((short)
553 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
554 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
555
556 flowPusher.add(sw, po);
557 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800558
Jonathan Hart1caaa932013-11-04 15:28:28 -0800559}