blob: 9c3b5ee186b9ab32508ca976cef5c412c7befb90 [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
TeruU6464af02014-02-06 21:38:45 -0800226 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
227
Jonathan Hart17672992013-12-12 16:15:16 -0800228 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800229 return Command.CONTINUE;
230 }
231
Jonathan Hart17672992013-12-12 16:15:16 -0800232 if (eth.isBroadcast() || eth.isMulticast()) {
233 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800234 }
235 else {
236 // Unicast
237 handlePacketIn(sw, pi, eth);
238 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800239
240 return Command.STOP;
241 }
242
Jonathan Hart17672992013-12-12 16:15:16 -0800243 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
244 if (log.isTraceEnabled()) {
245 log.trace("Sending broadcast packet to other ONOS instances");
246 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800247
248 datagrid.sendPacketOutNotification(new BroadcastPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800249 eth.serialize(), null, sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800250 }
251
TeruU417fe022014-02-04 12:59:30 -0800252 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
253 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
254
Jonathan Hart5e448782013-12-10 12:36:35 -0800255 String destinationMac =
256 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800257
TeruUd1ba0e22014-02-10 11:44:15 -0800258 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
259 try{
260 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
Jonathan Hart1caaa932013-11-04 15:28:28 -0800261 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800262 if (deviceObject == null) {
263 log.debug("No device entry found for {}",
264 destinationMac);
265
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);
268 return;
269 }
270
271 continueHandlePacketIn(sw, pi, eth, deviceObject);
272 } finally {
273 deviceStorage.rollback();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800274 }
TeruU417fe022014-02-04 12:59:30 -0800275 }
276
277 private class WaitDeviceArp implements Runnable {
278 IOFSwitch sw;
279 OFPacketIn pi;
280 Ethernet eth;
281
282 public WaitDeviceArp(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
283 super();
284 this.sw = sw;
285 this.pi = pi;
286 this.eth = eth;
287 }
288
289 @Override
290 public void run() {
TeruUd1ba0e22014-02-10 11:44:15 -0800291 try {
TeruU417fe022014-02-04 12:59:30 -0800292 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(HexString.toHexString(eth.getDestinationMACAddress()));
TeruUd1ba0e22014-02-10 11:44:15 -0800293 if(deviceObject == null){
294 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
295 handleBroadcast(sw, pi, eth);
296 return;
297 }
298 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMACAddress());
299 continueHandlePacketIn(sw, pi, eth, deviceObject);
300 } finally {
301 deviceStorage.rollback();
302 }
TeruU417fe022014-02-04 12:59:30 -0800303 }
304 }
305
306 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, IDeviceObject deviceObject) {
307 log.debug("Start continuehandlePacketIn");
308
Jonathan Hart5e448782013-12-10 12:36:35 -0800309 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800310 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800311 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU417fe022014-02-04 12:59:30 -0800312 deviceObject.getMACAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800313 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800314 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800315 }
TeruU417fe022014-02-04 12:59:30 -0800316
TeruU6464af02014-02-06 21:38:45 -0800317
TeruU417fe022014-02-04 12:59:30 -0800318 //This code assumes the device has only one port. It should be problem.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800319 IPortObject portObject = ports.next();
TeruU417fe022014-02-04 12:59:30 -0800320
Jonathan Hart1caaa932013-11-04 15:28:28 -0800321 short destinationPort = portObject.getNumber();
322 ISwitchObject switchObject = portObject.getSwitch();
323 long destinationDpid = HexString.toLong(switchObject.getDPID());
324
Jonathan Hart41d1e912013-11-24 16:50:25 -0800325 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800326 SwitchPort srcSwitchPort = new SwitchPort(
327 new Dpid(sw.getId()), new Port(pi.getInPort()));
328 SwitchPort dstSwitchPort = new SwitchPort(
329 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800330
331 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
332 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800333
TeruU6464af02014-02-06 21:38:45 -0800334 FlowPath flowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800335
Jonathan Hart7e6df362013-12-10 23:33:59 -0800336 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800337 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800338 Path pathspec = new Path(srcMacAddress, dstMacAddress);
339
Jonathan Harte789d6e2013-12-17 17:50:11 -0800340 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800341
TeruU6464af02014-02-06 21:38:45 -0800342 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800343 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800344 // We've already start to install a flow for this pair of MAC addresses
345 if(log.isDebugEnabled()) {
346 log.debug("Found existing the same pathspec {}, Flow ID is {}",
347 pathspec,
348 HexString.toHexString(existingFlow.flowId));
349 }
350
Jonathan Hart7e6df362013-12-10 23:33:59 -0800351 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800352
Jonathan Hart0444d932014-01-22 15:06:17 -0800353 // Find the correct port here. We just assume the PI is from
354 // the first hop switch, but this is definitely not always
355 // the case. We'll have to retrieve the flow from HZ every time
356 // because it could change (be rerouted) sometimes.
357 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800358 // Flow has been sent to the switches so it is safe to
359 // send a packet out now
Jonathan Hart0444d932014-01-22 15:06:17 -0800360 FlowPath flow = datagrid.getFlow(new FlowId(existingFlow.flowId));
361 FlowEntry flowEntryForThisSwitch = null;
Jonathan Hart84198d32014-01-22 17:14:37 -0800362
363 if (flow != null) {
364 for (FlowEntry flowEntry : flow.flowEntries()) {
365 if (flowEntry.dpid().equals(new Dpid(sw.getId()))) {
366 flowEntryForThisSwitch = flowEntry;
367 break;
368 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800369 }
370 }
371
372 if (flowEntryForThisSwitch == null) {
373 // If we don't find a flow entry for that switch, then we're
374 // in the middle of a rerouting (or something's gone wrong).
375 // This packet will be dropped as a victim of the rerouting.
376 log.debug("Dropping packet on flow {} between {}-{}, flow path {}",
377 new Object[] {new FlowId(existingFlow.flowId),
378 srcMacAddress, dstMacAddress, flow});
379 }
380 else {
TeruU417fe022014-02-04 12:59:30 -0800381 log.debug("Sending packet out from sw {}, outport{}", sw, flowEntryForThisSwitch.outPort().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800382 sendPacketOut(sw, po, flowEntryForThisSwitch.outPort().value());
383 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800384 }
385 else {
TeruU6464af02014-02-06 21:38:45 -0800386 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800387 // packet out for later
TeruU417fe022014-02-04 12:59:30 -0800388 log.debug("Put a packet into the waitng list. flowId {}", Long.toHexString(existingFlow.flowId));
389 waitingPackets.put(existingFlow.flowId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800390 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800391 return;
392 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800393
Jonathan Hart7e6df362013-12-10 23:33:59 -0800394 log.debug("Adding new flow between {} at {} and {} at {}",
395 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800396
Jonathan Hart7e6df362013-12-10 23:33:59 -0800397 DataPath datapath = new DataPath();
398 datapath.setSrcPort(srcSwitchPort);
399 datapath.setDstPort(dstSwitchPort);
400
401 flowPath = new FlowPath();
Jonathan Hart0444d932014-01-22 15:06:17 -0800402 flowPath.setInstallerId(new CallerId(callerId));
Jonathan Hart7e6df362013-12-10 23:33:59 -0800403
404 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
405 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
406 flowPath.setFlowEntryMatch(new FlowEntryMatch());
407 flowPath.setIdleTimeout(IDLE_TIMEOUT);
408 flowPath.setHardTimeout(HARD_TIMEOUT);
409 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
410 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
411 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
412 flowPath.setDataPath(datapath);
Jonathan Hart0444d932014-01-22 15:06:17 -0800413
Jonathan Hart7e6df362013-12-10 23:33:59 -0800414 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
Jonathan Hart7e6df362013-12-10 23:33:59 -0800415
416 flowPath.setFlowId(flowId);
TeruU6464af02014-02-06 21:38:45 -0800417
Jonathan Hart7e6df362013-12-10 23:33:59 -0800418 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800419
420 // Add to waiting lists
Jonathan Harte789d6e2013-12-17 17:50:11 -0800421 pendingFlows.put(pathspec, new PushedFlow(flowId.value()));
TeruU417fe022014-02-04 12:59:30 -0800422 log.debug("Put a Path {} in the pending flow, Flow ID {}", pathspec, flowId);
TeruU6464af02014-02-06 21:38:45 -0800423 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
424 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800425 }
TeruU6464af02014-02-06 21:38:45 -0800426
TeruU417fe022014-02-04 12:59:30 -0800427 log.debug("Adding forward {} to {}. Flow ID {}", new Object[] {
Jonathan Hart0444d932014-01-22 15:06:17 -0800428 srcMacAddress, dstMacAddress, flowPath.flowId()});
Jonathan Hart5e448782013-12-10 12:36:35 -0800429 flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800430 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800431
Jonathan Hart7e6df362013-12-10 23:33:59 -0800432 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800433 OFPacketOut po = new OFPacketOut();
434 po.setInPort(OFPort.OFPP_NONE)
435 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800436 .setActions(new ArrayList<OFAction>())
437 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800438
439 if (sw.getBuffers() == 0) {
440 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
441 .setPacketData(pi.getPacketData())
442 .setLengthU(po.getLengthU() + po.getPacketData().length);
443 }
444 else {
445 po.setBufferId(pi.getBufferId());
446 }
447
Jonathan Hart5e448782013-12-10 12:36:35 -0800448 return po;
449 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800450
Jonathan Hart5e448782013-12-10 12:36:35 -0800451 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800452 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
453 for (FlowPath flowPath : installedFlowPaths) {
454 flowInstalled(flowPath);
455 }
456 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800457
458 @Override
459 public void flowRemoved(FlowPath removedFlowPath) {
TeruU417fe022014-02-04 12:59:30 -0800460 if(log.isDebugEnabled()){
TeruU6464af02014-02-06 21:38:45 -0800461 log.debug("Flow {} was removed", removedFlowPath.flowId());
TeruU417fe022014-02-04 12:59:30 -0800462 }
463
464
Jonathan Hart0444d932014-01-22 15:06:17 -0800465 if (!removedFlowPath.installerId().equals(callerId)) {
466 // Not our flow path, ignore
467 return;
468 }
TeruU417fe022014-02-04 12:59:30 -0800469
Jonathan Hart0444d932014-01-22 15:06:17 -0800470 MACAddress srcMacAddress = removedFlowPath.flowEntryMatch().srcMac();
471 MACAddress dstMacAddress = removedFlowPath.flowEntryMatch().dstMac();
472
473 Path removedPath = new Path(srcMacAddress, dstMacAddress);
474
475 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800476 // There *shouldn't* be any packets queued if the flow has
477 // just been removed.
TeruU6464af02014-02-06 21:38:45 -0800478 List<PacketToPush> packets = waitingPackets.removeAll(removedFlowPath.flowId().value());
Jonathan Hart0444d932014-01-22 15:06:17 -0800479 if (!packets.isEmpty()) {
TeruU417fe022014-02-04 12:59:30 -0800480 log.warn("Removed flow {} has packets queued.", removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800481 }
TeruU417fe022014-02-04 12:59:30 -0800482 pendingFlows.remove(removedPath);
483 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, removedFlowPath.flowId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800484 }
485 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800486
TeruU417fe022014-02-04 12:59:30 -0800487 private void flowInstalled(FlowPath installedFlowPath) {
488 log.debug("Flow {} was installed", installedFlowPath.flowId());
Jonathan Hart5e448782013-12-10 12:36:35 -0800489
Jonathan Hart0444d932014-01-22 15:06:17 -0800490 if (!installedFlowPath.installerId().equals(callerId)) {
491 // Not our flow path, ignore
492 return;
TeruU417fe022014-02-04 12:59:30 -0800493 }
494
495 if(installedFlowPath.flowEntries().isEmpty()){
496 //If there is no flowEntry, ignore
497 log.warn("There is no flowEntry in the installedFlowPath id {}.return.", installedFlowPath.flowId());
498 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800499 }
500
TeruU417fe022014-02-04 12:59:30 -0800501 MACAddress srcMacAddress = installedFlowPath.flowEntryMatch().srcMac();
502 MACAddress dstMacAddress = installedFlowPath.flowEntryMatch().dstMac();
503 Path installedPath = new Path(srcMacAddress, dstMacAddress);
TeruU417fe022014-02-04 12:59:30 -0800504
Jonathan Hart0444d932014-01-22 15:06:17 -0800505 // TODO waiting packets should time out. We could request a path that
506 // can't be installed right now because of a network partition. The path
507 // may eventually be installed, but we may have received thousands of
508 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800509
TeruU417fe022014-02-04 12:59:30 -0800510 List<PacketToPush> packets;
TeruU417fe022014-02-04 12:59:30 -0800511 Short outPort = installedFlowPath.flowEntries().get(0).outPort().value();
512
513 PushedFlow existingFlow;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800514
Jonathan Hart7e6df362013-12-10 23:33:59 -0800515 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800516 existingFlow = pendingFlows.get(installedPath);
TeruU417fe022014-02-04 12:59:30 -0800517
Jonathan Hart0444d932014-01-22 15:06:17 -0800518 if (existingFlow != null) {
519 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800520 existingFlow.firstOutPort = outPort;
521 } else {
522 log.debug("ExistingFlow {} is null", installedPath);
523 return;
524 }
525
TeruU6464af02014-02-06 21:38:45 -0800526 //Check both existing flow are installed status.
527 if(existingFlow.installed){
TeruU417fe022014-02-04 12:59:30 -0800528 packets = waitingPackets.removeAll(existingFlow.flowId);
529 if(log.isDebugEnabled()){
530 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
531 Long.toHexString(existingFlow.flowId), existingFlow.firstOutPort, packets.size());
532 }
TeruU417fe022014-02-04 12:59:30 -0800533 }else{
534 log.debug("Forward or reverse flows hasn't been pushed yet. return");
535 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800536 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800537 }
TeruU417fe022014-02-04 12:59:30 -0800538
Jonathan Hart5e448782013-12-10 12:36:35 -0800539 for (PacketToPush packet : packets) {
TeruU417fe022014-02-04 12:59:30 -0800540 log.debug("Start packetToPush to sw {}, outPort {}", packet.dpid, existingFlow.firstOutPort);
Jonathan Hart5e448782013-12-10 12:36:35 -0800541 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800542 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
543 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800544 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800545
546 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
547 po.getActions().add(new OFActionOutput(outPort));
548 po.setActionsLength((short)
549 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
550 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
551
552 flowPusher.add(sw, po);
553 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800554
Jonathan Hart1caaa932013-11-04 15:28:28 -0800555}