blob: 294e1a552adb359aba9d7d15ee3c2f758bcc004e [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;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070022import net.onrc.onos.datagrid.IEventChannel;
Jonathan Hartd857ad62013-12-14 18:08:17 -080023import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080024import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart7804bea2014-01-07 10:50:52 -080025import net.onrc.onos.ofcontroller.proxyarp.BroadcastPacketOutNotification;
Jonathan Hart0444d932014-01-22 15:06:17 -080026import net.onrc.onos.ofcontroller.proxyarp.IProxyArpService;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070027import net.onrc.onos.ofcontroller.proxyarp.PacketOutNotification;
Jonathan Hart1caaa932013-11-04 15:28:28 -080028import net.onrc.onos.ofcontroller.util.CallerId;
29import net.onrc.onos.ofcontroller.util.DataPath;
30import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart0444d932014-01-22 15:06:17 -080031import net.onrc.onos.ofcontroller.util.FlowEntry;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080032import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080033import net.onrc.onos.ofcontroller.util.FlowId;
34import net.onrc.onos.ofcontroller.util.FlowPath;
35import net.onrc.onos.ofcontroller.util.FlowPathType;
36import net.onrc.onos.ofcontroller.util.FlowPathUserState;
37import net.onrc.onos.ofcontroller.util.Port;
38import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hart96892d12014-03-26 20:21:29 -070039import net.onrc.onos.packet.Ethernet;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070040import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080041
42import org.openflow.protocol.OFMessage;
43import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080044import org.openflow.protocol.OFPacketOut;
45import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080046import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080047import org.openflow.protocol.action.OFAction;
48import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080049import org.openflow.util.HexString;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
Jonathan Hartd857ad62013-12-14 18:08:17 -080053import com.google.common.collect.LinkedListMultimap;
54import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080055
56public class Forwarding implements IOFMessageListener, IFloodlightModule,
57 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080058 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080059
Jonathan Hart7e6df362013-12-10 23:33:59 -080060 private final int IDLE_TIMEOUT = 5; // seconds
61 private final int HARD_TIMEOUT = 0; // seconds
TeruU417fe022014-02-04 12:59:30 -080062 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
63 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
64
65 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080066
67 private final CallerId callerId = new CallerId("Forwarding");
Jonathan Hart7e6df362013-12-10 23:33:59 -080068
Jonathan Hart1caaa932013-11-04 15:28:28 -080069 private IFloodlightProviderService floodlightProvider;
Jonathan Hart7e6df362013-12-10 23:33:59 -080070 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080071 private IDatagridService datagrid;
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070072 //
73 // TODO: Using PacketOutNotification as both the key and the
74 // value is a hack that should be removed when this module is
75 // refactored.
76 //
77 private IEventChannel<PacketOutNotification, PacketOutNotification> eventChannel;
78 private static final String PACKET_OUT_CHANNEL_NAME = "onos.packet_out";
79
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070080 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080081
Jonathan Harte789d6e2013-12-17 17:50:11 -080082 // TODO it seems there is a Guava collection that will time out entries.
83 // We should see if this will work here.
84 private Map<Path, PushedFlow> pendingFlows;
Jonathan Hartd857ad62013-12-14 18:08:17 -080085 private ListMultimap<Long, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080086
Jonathan Hart7e6df362013-12-10 23:33:59 -080087 private final Object lock = new Object();
88
Jonathan Harte789d6e2013-12-17 17:50:11 -080089 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080090 public final OFPacketOut packet;
91 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080092
Jonathan Hart5e448782013-12-10 12:36:35 -080093 public PacketToPush(OFPacketOut packet, long dpid) {
94 this.packet = packet;
95 this.dpid = dpid;
96 }
Jonathan Hart1caaa932013-11-04 15:28:28 -080097 }
98
Jonathan Harte789d6e2013-12-17 17:50:11 -080099 private class PushedFlow {
100 public final long flowId;
Jonathan Hart0444d932014-01-22 15:06:17 -0800101 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -0800102 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800103
104 public PushedFlow(long flowId) {
105 this.flowId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800106 }
107 }
108
109 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800110 public final MACAddress srcMac;
111 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800112
Jonathan Hart0444d932014-01-22 15:06:17 -0800113 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800114 this.srcMac = srcMac;
115 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800116 }
117
118 @Override
119 public boolean equals(Object other) {
120 if (!(other instanceof Path)) {
121 return false;
122 }
123
124 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800125 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800126 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800127 }
128
129 @Override
130 public int hashCode() {
131 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800132 hash = 31 * hash + srcMac.hashCode();
133 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800134 return hash;
135 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800136
137 @Override
138 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800139 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800140 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800141 }
142
Jonathan Harte93aed42013-12-05 18:39:50 -0800143 @Override
144 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800145 List<Class<? extends IFloodlightService>> services =
146 new ArrayList<Class<? extends IFloodlightService>>(1);
147 services.add(IForwardingService.class);
148 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800149 }
150
151 @Override
152 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800153 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
154 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
155 impls.put(IForwardingService.class, this);
156 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800157 }
158
159 @Override
160 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
161 List<Class<? extends IFloodlightService>> dependencies =
162 new ArrayList<Class<? extends IFloodlightService>>();
163 dependencies.add(IFloodlightProviderService.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);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700166 dependencies.add(IControllerRegistryService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800167 // We don't use the IProxyArpService directly, but reactive forwarding
168 // requires it to be loaded and answering ARP requests
169 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800170 return dependencies;
171 }
172
173 @Override
174 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800175 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800176 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800177 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800178 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700179 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800180
181 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800182
Jonathan Harte789d6e2013-12-17 17:50:11 -0800183 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800184 waitingPackets = LinkedListMultimap.create();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800185 }
186
Jonathan Harte93aed42013-12-05 18:39:50 -0800187 @Override
188 public void startUp(FloodlightModuleContext context) {
Pavlin Radoslavov902fe522014-03-31 10:11:31 -0700189 //
190 // TODO: Using PacketOutNotification as both the key and the
191 // value is a hack that should be removed when this module is
192 // refactored.
193 //
194 eventChannel = datagrid.createChannel(PACKET_OUT_CHANNEL_NAME,
195 PacketOutNotification.class,
196 PacketOutNotification.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800197 }
198
199 @Override
200 public String getName() {
201 return "onosforwarding";
202 }
203
204 @Override
205 public boolean isCallbackOrderingPrereq(OFType type, String name) {
206 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800207 (name.equals("devicemanager") || name.equals("proxyarpmanager")
208 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800209 }
210
211 @Override
212 public boolean isCallbackOrderingPostreq(OFType type, String name) {
213 return false;
214 }
215
216 @Override
217 public Command receive(
218 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
219
220 if (msg.getType() != OFType.PACKET_IN) {
221 return Command.CONTINUE;
222 }
223
224 OFPacketIn pi = (OFPacketIn) msg;
225
226 Ethernet eth = IFloodlightProviderService.bcStore.
227 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
228
TeruU6464af02014-02-06 21:38:45 -0800229 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
230
Jonathan Hart17672992013-12-12 16:15:16 -0800231 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800232 return Command.CONTINUE;
233 }
234
Jonathan Hart17672992013-12-12 16:15:16 -0800235 if (eth.isBroadcast() || eth.isMulticast()) {
236 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800237 }
238 else {
239 // Unicast
240 handlePacketIn(sw, pi, eth);
241 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800242
243 return Command.STOP;
244 }
245
Jonathan Hart17672992013-12-12 16:15:16 -0800246 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
247 if (log.isTraceEnabled()) {
248 log.trace("Sending broadcast packet to other ONOS instances");
249 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800250
Pavlin Radoslavov902fe522014-03-31 10:11:31 -0700251 PacketOutNotification key =
252 new BroadcastPacketOutNotification(
253 eth.serialize(),
254 null, sw.getId(),
255 pi.getInPort());
256 //
257 // TODO: Using PacketOutNotification as both the key and the
258 // value is a hack that should be removed when this module is
259 // refactored.
260 //
261 eventChannel.addTransientEntry(key, key);
Jonathan Hart17672992013-12-12 16:15:16 -0800262 }
263
TeruU417fe022014-02-04 12:59:30 -0800264 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
265 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
266
Jonathan Hart5e448782013-12-10 12:36:35 -0800267 String destinationMac =
268 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800269
TeruUd1ba0e22014-02-10 11:44:15 -0800270 //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 -0700271 // TODO: Fix the code below after deviceStorage was removed
272 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800273 try{
274 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800275 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800276 if (deviceObject == null) {
277 log.debug("No device entry found for {}",
278 destinationMac);
279
280 //Device is not in the DB, so wait it until the device is added.
281 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
282 return;
283 }
284
285 continueHandlePacketIn(sw, pi, eth, deviceObject);
286 } finally {
287 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800288 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700289 */
TeruU417fe022014-02-04 12:59:30 -0800290 }
291
292 private class WaitDeviceArp implements Runnable {
293 IOFSwitch sw;
294 OFPacketIn pi;
295 Ethernet eth;
296
297 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
298 super();
299 this.sw = sw;
300 this.pi = pi;
301 this.eth = eth;
302 }
303
304 @Override
305 public void run() {
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700306 // TODO: Fix the code below after deviceStorage was removed
307 /*
TeruUd1ba0e22014-02-10 11:44:15 -0800308 try {
TeruU417fe022014-02-04 12:59:30 -0800309 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800310 if(deviceObject == null){
311 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
312 handleBroadcast(sw, pi, eth);
313 return;
314 }
315 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
316 continueHandlePacketIn(sw, pi, eth, deviceObject);
317 } finally {
318 deviceStorage.rollback();
319 }
Pavlin Radoslavov5fe71882014-03-21 16:27:21 -0700320 */
TeruU417fe022014-02-04 12:59:30 -0800321 }
322 }
323
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -0700324 // TODO: Fix the code below because IDeviceObject was removed
325 /*
TeruU417fe022014-02-04 12:59:30 -0800326 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
327 log.debug("Start continuehandlePacketIn");
328
Jonathan Hart5e448782013-12-10 12:36:35 -0800329 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800330 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800331 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800332 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800333 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800334 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800335 }
TeruU417fe022014-02-04 12:59:30 -0800336
TeruU6464af02014-02-06 21:38:45 -0800337
TeruU417fe022014-02-04 12:59:30 -0800338 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800339 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800340
Jonathan Hart1caaa932013-11-04 15:28:28 -0800341 short destinationPort = portObject.getNumber();
342 ISwitchObject switchObject = portObject.getSwitch();
343 long destinationDpid = HexString.toLong(switchObject.getDPID());
344
Jonathan Hart41d1e912013-11-24 16:50:25 -0800345 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800346 SwitchPort srcSwitchPort = new SwitchPort(
347 new Dpid(sw.getId()), new Port(pi.getInPort()));
348 SwitchPort dstSwitchPort = new SwitchPort(
349 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800350
351 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
352 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800353
TeruU6464af02014-02-06 21:38:45 -0800354 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800355
Jonathan Hart7e6df362013-12-10 23:33:59 -0800356 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800357 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800358 Path pathspec = new Path(srcMacAddress, dstMacAddress);
359
Jonathan Harte789d6e2013-12-17 17:50:11 -0800360 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800361
TeruU6464af02014-02-06 21:38:45 -0800362 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800363 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800364 // We've already start to install a flow for this pair of MAC addresses
365 if(log.isDebugEnabled()) {
366 log.debug("Found existing the same pathspec {}, Flow ID is {}",
367 pathspec,
368 HexString.toHexString(existingFlow.flowId));
369 }
370
Jonathan Hart7e6df362013-12-10 23:33:59 -0800371 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800372
Jonathan Hart0444d932014-01-22 15:06:17 -0800373 // Find the correct port here. We just assume the PI is from
374 // the first hop switch, but this is definitely not always
375 // the case. We'll have to retrieve the flow from HZ every time
376 // because it could change (be rerouted) sometimes.
377 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800378 // Flow has been sent to the switches so it is safe to
379 // send a packet out now
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700380
381 //
382 // TODO: The getFlow() call below needs
383 // to be updated to the new Path Intent
384 // framework.
385 //
386 // FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
387 FlowPath flow = null;
Jonathan Hart0444d932014-01-22 15:06:17 -0800388 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800389
390 if (flow != null) {
391 for (FlowEntry flowEntry : flow.flowEntries()) {
392 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
393 flowEntryForThisSwitch = flowEntry;
394 break;
395 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800396 }
397 }
398
399 if (flowEntryForThisSwitch == null) {
400 // If we don't find a flow entry for that switch, then we're
401 // in the middle of a rerouting (or something's gone wrong).
402 // This packet will be dropped as a victim of the rerouting.
403 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
404 new Object[] {new FlowId(existingFlow.flowId),
405 srcMacAddress, dstMacAddress, flow});
406 }
407 else {
TeruU417fe022014-02-04 12:59:30 -0800408 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800409 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
410 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800411 }
412 else {
TeruU6464af02014-02-06 21:38:45 -0800413 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800414 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800415 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
416 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800417 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800418 return;
419 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800420
Jonathan Hart7e6df362013-12-10 23:33:59 -0800421 log.debug("Adding new flow between {} at {} and {} at {}",
422 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800423
Jonathan Hart7e6df362013-12-10 23:33:59 -0800424 DataPath datapath = new DataPath();
425 datapath.setSrcPort(srcSwitchPort);
426 datapath.setDstPort(dstSwitchPort);
427
428 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800429 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800430
431 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
432 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
433 flowPath.setFlowEntryMatch(new FlowEntryMatch());
434 flowPath.setIdleTimeout(IDLE_TIMEOUT);
435 flowPath.setHardTimeout(HARD_TIMEOUT);
436 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
437 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
438 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
439 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800440
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700441 FlowId flowId = new FlowId(controllerRegistryService.getNextUniqueId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800442
443 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800444
Jonathan Hart7e6df362013-12-10 23:33:59 -0800445 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800446
447 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800448 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800449 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800450 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
451 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800452 }
TeruU6464af02014-02-06 21:38:45 -0800453
TeruU417fe022014-02-04 12:59:30 -0800454 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800455 srcMacAddress, dstMacAddress, flowPath.flowId()});
Pavlin Radoslavov39f0f2e2014-03-20 12:04:57 -0700456 // TODO: Add the flow by using the new Path Intent framework
457 // flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800458 }
Pavlin Radoslavov4cf8ee52014-03-26 18:19:58 -0700459 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800460
Jonathan Hart7e6df362013-12-10 23:33:59 -0800461 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800462 OFPacketOut po = new OFPacketOut();
463 po.setInPort(OFPort.OFPP_NONE)
464 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800465 .setActions(new ArrayList<OFAction>())
466 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800467
468 if (sw.getBuffers() == 0) {
469 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
470 .setPacketData(pi.getPacketData())
471 .setLengthU(po.getLengthU() + po.getPacketData().length);
472 }
473 else {
474 po.setBufferId(pi.getBufferId());
475 }
476
Jonathan Hart5e448782013-12-10 12:36:35 -0800477 return po;
478 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800479
Jonathan Hart5e448782013-12-10 12:36:35 -0800480 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800481 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
482 for (FlowPath flowPath : installedFlowPaths) {
483 flowInstalled(flowPath);
484 }
485 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800486
487 @Override
488 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800489 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800490 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800491 }
492
493
Jonathan Hart0444d932014-01-22 15:06:17 -0800494 if (!removedFlowPath.installerId().equals(callerId)) {
495 // Not our flow path, ignore
496 return;
497 }
TeruU417fe022014-02-04 12:59:30 -0800498
Jonathan Hart0444d932014-01-22 15:06:17 -0800499 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
500 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
501
502 Path removedPath = new Path(srcMacAddress, dstMacAddress);
503
504 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800505 // There *shouldn't* be any packets queued if the flow has
506 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800507 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800508 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800509 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800510 }
TeruU417fe022014-02-04 12:59:30 -0800511 pendingFlows.remove(removedPath);
512 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800513 }
514 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800515
TeruU417fe022014-02-04 12:59:30 -0800516 private void flowInstalled(FlowPath installedFlowPath) {
517 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800518
Jonathan Hart0444d932014-01-22 15:06:17 -0800519 if (!installedFlowPath.installerId().equals(callerId)) {
520 // Not our flow path, ignore
521 return;
TeruU417fe022014-02-04 12:59:30 -0800522 }
523
524 if(installedFlowPath.flowEntries().isEmpty()){
525 //If there is no flowEntry, ignore
526 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
527 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800528 }
529
TeruU417fe022014-02-04 12:59:30 -0800530 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
531 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
532 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800533
Jonathan Hart0444d932014-01-22 15:06:17 -0800534 // TODO waiting packets should time out. We could request a path that
535 // can't be installed right now because of a network partition. The path
536 // may eventually be installed, but we may have received thousands of
537 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800538
TeruU417fe022014-02-04 12:59:30 -0800539 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800540 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
541
542 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800543
Jonathan Hart7e6df362013-12-10 23:33:59 -0800544 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800545 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800546
Jonathan Hart0444d932014-01-22 15:06:17 -0800547 if (existingFlow != null) {
548 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800549 existingFlow.firstOutPort = outPort;
550 } else {
551 log.debug("ExistingFlow {} is null", installedPath);
552 return;
553 }
554
TeruU6464af02014-02-06 21:38:45 -0800555 //Check both existing flow are installed status.
556 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800557 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 }
TeruU417fe022014-02-04 12:59:30 -0800562 }else{
563 log.debug("Forward or reverse flows hasn't been pushed yet. return");
564 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800565 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800566 }
TeruU417fe022014-02-04 12:59:30 -0800567
Jonathan Hart5e448782013-12-10 12:36:35 -0800568 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800569 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800570 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800571 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
572 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800573 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800574
575 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
576 po.getActions().add(new OFActionOutput(outPort));
577 po.setActionsLength((short)
578 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
579 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
580
581 flowPusher.add(sw, po);
582 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800583
Jonathan Hart1caaa932013-11-04 15:28:28 -0800584}