blob: 3533ebfc9d00360251efe7ada692c5021da06609 [file] [log] [blame]
Jonathan Hart0961fe82014-04-03 09:56:25 -07001package net.onrc.onos.apps.forwarding;
Jonathan Hart1caaa932013-11-04 15:28:28 -08002
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;
TeruU7feef8a2014-04-03 00:15:49 -07009import java.util.Map.Entry;
TeruU417fe022014-02-04 12:59:30 -080010import java.util.concurrent.Executors;
11import java.util.concurrent.ScheduledExecutorService;
12import java.util.concurrent.TimeUnit;
Jonathan Hart1caaa932013-11-04 15:28:28 -080013
14import net.floodlightcontroller.core.FloodlightContext;
15import net.floodlightcontroller.core.IFloodlightProviderService;
16import net.floodlightcontroller.core.IOFMessageListener;
17import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080018import net.floodlightcontroller.core.module.FloodlightModuleContext;
19import net.floodlightcontroller.core.module.IFloodlightModule;
20import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080021import net.floodlightcontroller.util.MACAddress;
Jonathan Hart0961fe82014-04-03 09:56:25 -070022import net.onrc.onos.apps.proxyarp.BroadcastPacketOutNotification;
23import net.onrc.onos.apps.proxyarp.IProxyArpService;
Jonathan Hart6df90172014-04-03 10:13:11 -070024import net.onrc.onos.core.datagrid.IDatagridService;
25import net.onrc.onos.core.datagrid.IEventChannel;
26import net.onrc.onos.core.datagrid.IEventChannelListener;
Jonathan Hartaa380972014-04-03 10:24:46 -070027import net.onrc.onos.core.intent.Intent;
28import net.onrc.onos.core.intent.IntentMap;
29import net.onrc.onos.core.intent.IntentOperation;
30import net.onrc.onos.core.intent.IntentOperationList;
31import net.onrc.onos.core.intent.PathIntent;
32import net.onrc.onos.core.intent.ShortestPathIntent;
33import net.onrc.onos.core.intent.Intent.IntentState;
34import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
35import net.onrc.onos.core.intent.runtime.IntentStateList;
Jonathan Hartd857ad62013-12-14 18:08:17 -080036import net.onrc.onos.ofcontroller.devicemanager.IOnosDeviceService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080037import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
TeruU7feef8a2014-04-03 00:15:49 -070038import net.onrc.onos.ofcontroller.networkgraph.Device;
39import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
40import net.onrc.onos.ofcontroller.networkgraph.LinkEvent;
41import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
42import net.onrc.onos.ofcontroller.networkgraph.Switch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080043import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080044import net.onrc.onos.ofcontroller.util.FlowPath;
Jonathan Hart1caaa932013-11-04 15:28:28 -080045import net.onrc.onos.ofcontroller.util.Port;
46import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hart96892d12014-03-26 20:21:29 -070047import net.onrc.onos.packet.Ethernet;
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070048import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080049
50import org.openflow.protocol.OFMessage;
51import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080052import org.openflow.protocol.OFPacketOut;
53import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080054import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080055import org.openflow.protocol.action.OFAction;
56import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080057import org.openflow.util.HexString;
58import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
Jonathan Hartd857ad62013-12-14 18:08:17 -080061import com.google.common.collect.LinkedListMultimap;
62import com.google.common.collect.ListMultimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080063
64public class Forwarding implements IOFMessageListener, IFloodlightModule,
TeruU7feef8a2014-04-03 00:15:49 -070065 IForwardingService, IEventChannelListener<Long, IntentStateList> {
Jonathan Hart1caaa932013-11-04 15:28:28 -080066 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
TeruU417fe022014-02-04 12:59:30 -080067
TeruU417fe022014-02-04 12:59:30 -080068 private final int SLEEP_TIME_FOR_DB_DEVICE_INSTALLED = 100; // milliseconds
69 private final static int NUMBER_OF_THREAD_FOR_EXECUTOR = 1;
70
71 private final static ScheduledExecutorService executor = Executors.newScheduledThreadPool(NUMBER_OF_THREAD_FOR_EXECUTOR);
Jonathan Hart0444d932014-01-22 15:06:17 -080072
TeruU7feef8a2014-04-03 00:15:49 -070073 private final String callerId = "Forwarding";
Jonathan Hart7e6df362013-12-10 23:33:59 -080074
Jonathan Hart1caaa932013-11-04 15:28:28 -080075 private IFloodlightProviderService floodlightProvider;
Jonathan Hart7e6df362013-12-10 23:33:59 -080076 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080077 private IDatagridService datagrid;
TeruU7feef8a2014-04-03 00:15:49 -070078
79 private IEventChannel<Long, BroadcastPacketOutNotification> eventChannel;
80 private static final String SINGLE_PACKET_OUT_CHANNEL_NAME = "onos.forwarding.packet_out";
Pavlin Radoslavov902fe522014-03-31 10:11:31 -070081
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -070082 private IControllerRegistryService controllerRegistryService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080083
TeruU7feef8a2014-04-03 00:15:49 -070084 private INetworkGraphService networkGraphService;
85 private NetworkGraph networkGraph;
86 private IPathCalcRuntimeService pathRuntime;
87 private IntentMap intentMap;
88
Jonathan Harte789d6e2013-12-17 17:50:11 -080089 // TODO it seems there is a Guava collection that will time out entries.
90 // We should see if this will work here.
91 private Map<Path, PushedFlow> pendingFlows;
TeruU7feef8a2014-04-03 00:15:49 -070092 private ListMultimap<String, PacketToPush> waitingPackets;
Jonathan Hart5e448782013-12-10 12:36:35 -080093
Jonathan Hart7e6df362013-12-10 23:33:59 -080094 private final Object lock = new Object();
95
Jonathan Harte789d6e2013-12-17 17:50:11 -080096 private class PacketToPush {
Jonathan Hart5e448782013-12-10 12:36:35 -080097 public final OFPacketOut packet;
98 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080099
Jonathan Hart5e448782013-12-10 12:36:35 -0800100 public PacketToPush(OFPacketOut packet, long dpid) {
101 this.packet = packet;
102 this.dpid = dpid;
103 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800104 }
105
Jonathan Harte789d6e2013-12-17 17:50:11 -0800106 private class PushedFlow {
TeruU7feef8a2014-04-03 00:15:49 -0700107 public final String intentId;
Jonathan Hart0444d932014-01-22 15:06:17 -0800108 public boolean installed = false;
TeruU417fe022014-02-04 12:59:30 -0800109 public short firstOutPort;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800110
TeruU7feef8a2014-04-03 00:15:49 -0700111 public PushedFlow(String flowId) {
112 this.intentId = flowId;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800113 }
114 }
115
116 private final class Path {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800117 public final MACAddress srcMac;
118 public final MACAddress dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800119
Jonathan Hart0444d932014-01-22 15:06:17 -0800120 public Path(MACAddress srcMac, MACAddress dstMac) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800121 this.srcMac = srcMac;
122 this.dstMac = dstMac;
Jonathan Hart5e448782013-12-10 12:36:35 -0800123 }
124
125 @Override
126 public boolean equals(Object other) {
127 if (!(other instanceof Path)) {
128 return false;
129 }
130
131 Path otherPath = (Path) other;
Jonathan Hart0444d932014-01-22 15:06:17 -0800132 return srcMac.equals(otherPath.srcMac) &&
Jonathan Harte789d6e2013-12-17 17:50:11 -0800133 dstMac.equals(otherPath.dstMac);
Jonathan Hart5e448782013-12-10 12:36:35 -0800134 }
135
136 @Override
137 public int hashCode() {
138 int hash = 17;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800139 hash = 31 * hash + srcMac.hashCode();
140 hash = 31 * hash + dstMac.hashCode();
Jonathan Hart5e448782013-12-10 12:36:35 -0800141 return hash;
142 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800143
144 @Override
145 public String toString() {
Jonathan Hart0444d932014-01-22 15:06:17 -0800146 return "(" + srcMac + ") => (" + dstMac + ")";
Jonathan Harte789d6e2013-12-17 17:50:11 -0800147 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800148 }
149
Jonathan Harte93aed42013-12-05 18:39:50 -0800150 @Override
151 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800152 List<Class<? extends IFloodlightService>> services =
153 new ArrayList<Class<? extends IFloodlightService>>(1);
154 services.add(IForwardingService.class);
155 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800156 }
157
158 @Override
159 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800160 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
161 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
162 impls.put(IForwardingService.class, this);
163 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800164 }
165
166 @Override
167 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
168 List<Class<? extends IFloodlightService>> dependencies =
169 new ArrayList<Class<? extends IFloodlightService>>();
170 dependencies.add(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800171 dependencies.add(IFlowPusherService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700172 dependencies.add(IControllerRegistryService.class);
Jonathan Hartd857ad62013-12-14 18:08:17 -0800173 dependencies.add(IOnosDeviceService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700174 dependencies.add(IDatagridService.class);
175 dependencies.add(INetworkGraphService.class);
176 dependencies.add(IPathCalcRuntimeService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800177 // We don't use the IProxyArpService directly, but reactive forwarding
178 // requires it to be loaded and answering ARP requests
179 dependencies.add(IProxyArpService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800180 return dependencies;
181 }
182
183 @Override
184 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800185 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800186 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800187 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800188 datagrid = context.getServiceImpl(IDatagridService.class);
Pavlin Radoslavov52163ed2014-03-19 11:39:34 -0700189 controllerRegistryService = context.getServiceImpl(IControllerRegistryService.class);
TeruU7feef8a2014-04-03 00:15:49 -0700190 networkGraphService = context.getServiceImpl(INetworkGraphService.class);
191 pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800192
193 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart0444d932014-01-22 15:06:17 -0800194
Jonathan Harte789d6e2013-12-17 17:50:11 -0800195 pendingFlows = new HashMap<Path, PushedFlow>();
Jonathan Hartd857ad62013-12-14 18:08:17 -0800196 waitingPackets = LinkedListMultimap.create();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800197 }
198
Jonathan Harte93aed42013-12-05 18:39:50 -0800199 @Override
200 public void startUp(FloodlightModuleContext context) {
TeruU7feef8a2014-04-03 00:15:49 -0700201
202 eventChannel = datagrid.createChannel(SINGLE_PACKET_OUT_CHANNEL_NAME,
203 Long.class,
204 BroadcastPacketOutNotification.class);
205 networkGraph = networkGraphService.getNetworkGraph();
206 intentMap = pathRuntime.getPathIntents();
207 datagrid.addListener("onos.pathintent_state", this, Long.class, IntentStateList.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800208 }
209
210 @Override
211 public String getName() {
212 return "onosforwarding";
213 }
214
215 @Override
216 public boolean isCallbackOrderingPrereq(OFType type, String name) {
217 return (type == OFType.PACKET_IN) &&
Jonathan Hartd857ad62013-12-14 18:08:17 -0800218 (name.equals("devicemanager") || name.equals("proxyarpmanager")
219 || name.equals("onosdevicemanager"));
Jonathan Hart1caaa932013-11-04 15:28:28 -0800220 }
221
222 @Override
223 public boolean isCallbackOrderingPostreq(OFType type, String name) {
224 return false;
225 }
226
227 @Override
228 public Command receive(
229 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
230
231 if (msg.getType() != OFType.PACKET_IN) {
232 return Command.CONTINUE;
233 }
234
235 OFPacketIn pi = (OFPacketIn) msg;
236
237 Ethernet eth = IFloodlightProviderService.bcStore.
238 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
239
TeruU6464af02014-02-06 21:38:45 -0800240 log.debug("Receive PACKET_IN swId {}, portId {}", sw.getId(), pi.getInPort());
241
Jonathan Hart17672992013-12-12 16:15:16 -0800242 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800243 return Command.CONTINUE;
244 }
245
Jonathan Hart17672992013-12-12 16:15:16 -0800246 if (eth.isBroadcast() || eth.isMulticast()) {
247 handleBroadcast(sw, pi, eth);
Jonathan Hart17672992013-12-12 16:15:16 -0800248 }
249 else {
250 // Unicast
251 handlePacketIn(sw, pi, eth);
252 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800253
254 return Command.STOP;
255 }
256
Jonathan Hart17672992013-12-12 16:15:16 -0800257 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
258 if (log.isTraceEnabled()) {
259 log.trace("Sending broadcast packet to other ONOS instances");
260 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800261
TeruU7feef8a2014-04-03 00:15:49 -0700262 //We don't use address information, so 0 is put into the third argument.
263 BroadcastPacketOutNotification key =
Pavlin Radoslavov902fe522014-03-31 10:11:31 -0700264 new BroadcastPacketOutNotification(
265 eth.serialize(),
TeruU7feef8a2014-04-03 00:15:49 -0700266 0, sw.getId(),
Pavlin Radoslavov902fe522014-03-31 10:11:31 -0700267 pi.getInPort());
TeruU7feef8a2014-04-03 00:15:49 -0700268 eventChannel.addTransientEntry(eth.getDestinationMAC().toLong(), key);
Jonathan Hart17672992013-12-12 16:15:16 -0800269 }
270
TeruU417fe022014-02-04 12:59:30 -0800271 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth){
272 log.debug("Start handlePacketIn swId {}, portId {}", sw.getId(), pi.getInPort());
273
Jonathan Hart5e448782013-12-10 12:36:35 -0800274 String destinationMac =
275 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800276
TeruUd1ba0e22014-02-10 11:44:15 -0800277 //FIXME getDeviceByMac() is a blocking call, so it may be better way to handle it to avoid the condition.
TeruU7feef8a2014-04-03 00:15:49 -0700278 Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(destinationMac));
TeruUd1ba0e22014-02-10 11:44:15 -0800279
TeruU7feef8a2014-04-03 00:15:49 -0700280 if (deviceObject == null) {
281 log.debug("No device entry found for {}",
282 destinationMac);
TeruUd1ba0e22014-02-10 11:44:15 -0800283
TeruU7feef8a2014-04-03 00:15:49 -0700284 //Device is not in the DB, so wait it until the device is added.
285 executor.schedule(new WaitDeviceArp(sw, pi, eth), SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, TimeUnit.MILLISECONDS);
286 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800287 }
TeruU7feef8a2014-04-03 00:15:49 -0700288
289 continueHandlePacketIn(sw, pi, eth, deviceObject);
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() {
TeruU7feef8a2014-04-03 00:15:49 -0700306 Device deviceObject = networkGraph.getDeviceByMac(MACAddress.valueOf(eth.getDestinationMACAddress()));
307 if(deviceObject == null){
308 log.debug("wait {}ms and device was not found. Send broadcast packet and the thread finish.", SLEEP_TIME_FOR_DB_DEVICE_INSTALLED);
309 handleBroadcast(sw, pi, eth);
310 return;
311 }
312 log.debug("wait {}ms and device {} was found, continue",SLEEP_TIME_FOR_DB_DEVICE_INSTALLED, deviceObject.getMacAddress());
313 continueHandlePacketIn(sw, pi, eth, deviceObject);
TeruU417fe022014-02-04 12:59:30 -0800314 }
315 }
316
TeruU7feef8a2014-04-03 00:15:49 -0700317 private void continueHandlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth, Device deviceObject) {
TeruU417fe022014-02-04 12:59:30 -0800318
TeruU7feef8a2014-04-03 00:15:49 -0700319 log.debug("Start continuehandlePacketIn");
320
321 //Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
322 Iterator<net.onrc.onos.ofcontroller.networkgraph.Port> ports = deviceObject.getAttachmentPoints().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800323 if (!ports.hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800324 log.debug("No attachment point found for device {} - broadcasting packet",
TeruU7feef8a2014-04-03 00:15:49 -0700325 deviceObject.getMacAddress());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800326 handleBroadcast(sw, pi, eth);
TeruU417fe022014-02-04 12:59:30 -0800327 return;
Jonathan Hart1caaa932013-11-04 15:28:28 -0800328 }
TeruU417fe022014-02-04 12:59:30 -0800329
330 //This code assumes the device has only one port. It should be problem.
TeruU7feef8a2014-04-03 00:15:49 -0700331 net.onrc.onos.ofcontroller.networkgraph.Port portObject = ports.next();
332 short destinationPort = portObject.getNumber().shortValue();
333 Switch switchObject = portObject.getSwitch();
334 long destinationDpid = switchObject.getDpid();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800335
Jonathan Hart41d1e912013-11-24 16:50:25 -0800336 // TODO SwitchPort, Dpid and Port should probably be immutable
Jonathan Hart1caaa932013-11-04 15:28:28 -0800337 SwitchPort srcSwitchPort = new SwitchPort(
338 new Dpid(sw.getId()), new Port(pi.getInPort()));
339 SwitchPort dstSwitchPort = new SwitchPort(
340 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800341
342 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
343 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800344
Jonathan Hart7e6df362013-12-10 23:33:59 -0800345 synchronized (lock) {
TeruU417fe022014-02-04 12:59:30 -0800346 //TODO check concurrency
TeruU6464af02014-02-06 21:38:45 -0800347 Path pathspec = new Path(srcMacAddress, dstMacAddress);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800348 PushedFlow existingFlow = pendingFlows.get(pathspec);
Jonathan Hart0444d932014-01-22 15:06:17 -0800349
TeruU6464af02014-02-06 21:38:45 -0800350 //A path is installed side by side to reduce a path timeout and a wrong state.
Jonathan Hart0444d932014-01-22 15:06:17 -0800351 if (existingFlow != null) {
TeruU6464af02014-02-06 21:38:45 -0800352 // We've already start to install a flow for this pair of MAC addresses
353 if(log.isDebugEnabled()) {
TeruU7feef8a2014-04-03 00:15:49 -0700354 log.debug("Found existing the same pathspec {}, intent ID is {}",
TeruU6464af02014-02-06 21:38:45 -0800355 pathspec,
TeruU7feef8a2014-04-03 00:15:49 -0700356 existingFlow.intentId);
TeruU6464af02014-02-06 21:38:45 -0800357 }
358
Jonathan Hart7e6df362013-12-10 23:33:59 -0800359 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Harte789d6e2013-12-17 17:50:11 -0800360
Jonathan Hart0444d932014-01-22 15:06:17 -0800361 // Find the correct port here. We just assume the PI is from
362 // the first hop switch, but this is definitely not always
363 // the case. We'll have to retrieve the flow from HZ every time
364 // because it could change (be rerouted) sometimes.
365 if (existingFlow.installed) {
Jonathan Harte789d6e2013-12-17 17:50:11 -0800366 // Flow has been sent to the switches so it is safe to
367 // send a packet out now
Pavlin Radoslavova3818db2014-03-20 19:26:08 -0700368
TeruU7feef8a2014-04-03 00:15:49 -0700369 Intent intent = intentMap.getIntent(existingFlow.intentId);
370 PathIntent pathIntent = null;
371 if(intent instanceof PathIntent) {
372 pathIntent = (PathIntent)intent;
373 } else {
374 log.debug("Intent {} is not PathIntent. Return.", intent.getId());
375 return;
376 }
Jonathan Hart84198d32014-01-22 17:14:37 -0800377
TeruU7feef8a2014-04-03 00:15:49 -0700378 Boolean isflowEntryForThisSwitch = false;
379 net.onrc.onos.ofcontroller.networkgraph.Path path = pathIntent.getPath();
380
381 for(Iterator<LinkEvent> i = path.iterator(); i.hasNext();) {
382 LinkEvent le = (LinkEvent)i.next();
383 if(le.getSrc().dpid == sw.getId()) {
384 log.debug("src {} dst {}", le.getSrc(), le.getDst());
385 isflowEntryForThisSwitch = true;
386 break;
Jonathan Hart0444d932014-01-22 15:06:17 -0800387 }
388 }
389
TeruU7feef8a2014-04-03 00:15:49 -0700390 if (isflowEntryForThisSwitch == false) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800391 // If we don't find a flow entry for that switch, then we're
392 // in the middle of a rerouting (or something's gone wrong).
393 // This packet will be dropped as a victim of the rerouting.
TeruU7feef8a2014-04-03 00:15:49 -0700394 log.debug("Dropping packet on flow {} between {}-{}",
395 existingFlow.intentId,
396 srcMacAddress, dstMacAddress);
397 } else {
398 log.debug("Sending packet out from sw {}, outport{}", sw, existingFlow.firstOutPort);
399 sendPacketOut(sw, po, existingFlow.firstOutPort);
Jonathan Hart0444d932014-01-22 15:06:17 -0800400 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800401 }
402 else {
TeruU6464af02014-02-06 21:38:45 -0800403 // Flow path has not yet been installed to switches so save the
Jonathan Harte789d6e2013-12-17 17:50:11 -0800404 // packet out for later
TeruU7feef8a2014-04-03 00:15:49 -0700405 log.debug("Put a packet into the waitng list. flowId {}", existingFlow.intentId);
406 waitingPackets.put(existingFlow.intentId, new PacketToPush(po, sw.getId()));
Jonathan Harte789d6e2013-12-17 17:50:11 -0800407 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800408 return;
409 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800410
Jonathan Hart7e6df362013-12-10 23:33:59 -0800411 log.debug("Adding new flow between {} at {} and {} at {}",
412 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
Jonathan Hart48c2d312013-12-05 19:09:59 -0800413
TeruU7feef8a2014-04-03 00:15:49 -0700414 String intentId = callerId + ":" + controllerRegistryService.getNextUniqueId();
415 IntentOperationList operations = new IntentOperationList();
416 ShortestPathIntent intent = new ShortestPathIntent(intentId,
417 sw.getId(), pi.getInPort(), srcMacAddress.toLong(),
418 destinationDpid, destinationPort, dstMacAddress.toLong());
419 IntentOperation.Operator operator = IntentOperation.Operator.ADD;
420 operations.add(operator, intent);
421 pathRuntime.executeIntentOperations(operations);
TeruU6464af02014-02-06 21:38:45 -0800422
Jonathan Hart7e6df362013-12-10 23:33:59 -0800423 OFPacketOut po = constructPacketOut(pi, sw);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800424
425 // Add to waiting lists
TeruU7feef8a2014-04-03 00:15:49 -0700426 pendingFlows.put(pathspec, new PushedFlow(intentId));
427 log.debug("Put a Path {} in the pending flow, intent ID {}", pathspec, intentId);
428 waitingPackets.put(intentId, new PacketToPush(po, sw.getId()));
TeruU6464af02014-02-06 21:38:45 -0800429 log.debug("Put a Packet in the wating list. related pathspec {}", pathspec);
TeruU7feef8a2014-04-03 00:15:49 -0700430
Jonathan Hart41d1e912013-11-24 16:50:25 -0800431 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800432 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800433
Jonathan Hart7e6df362013-12-10 23:33:59 -0800434 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800435 OFPacketOut po = new OFPacketOut();
436 po.setInPort(OFPort.OFPP_NONE)
437 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800438 .setActions(new ArrayList<OFAction>())
439 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800440
441 if (sw.getBuffers() == 0) {
442 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
443 .setPacketData(pi.getPacketData())
444 .setLengthU(po.getLengthU() + po.getPacketData().length);
445 }
446 else {
447 po.setBufferId(pi.getBufferId());
448 }
449
Jonathan Hart5e448782013-12-10 12:36:35 -0800450 return po;
451 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800452
Jonathan Hart5e448782013-12-10 12:36:35 -0800453 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800454 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800455 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800456
457 @Override
458 public void flowRemoved(FlowPath removedFlowPath) {
TeruU7feef8a2014-04-03 00:15:49 -0700459 }
460
461 public void flowRemoved(PathIntent removedIntent) {
462 if(log.isTraceEnabled()){
463 log.trace("Path {} was removed", removedIntent.getParentIntent().getId());
TeruU417fe022014-02-04 12:59:30 -0800464 }
465
TeruU7feef8a2014-04-03 00:15:49 -0700466 ShortestPathIntent spfIntent = (ShortestPathIntent) removedIntent.getParentIntent();
467 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
468 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
Jonathan Hart0444d932014-01-22 15:06:17 -0800469 Path removedPath = new Path(srcMacAddress, dstMacAddress);
470
471 synchronized (lock) {
Jonathan Hart0444d932014-01-22 15:06:17 -0800472 // There *shouldn't* be any packets queued if the flow has
473 // just been removed.
TeruU7feef8a2014-04-03 00:15:49 -0700474 List<PacketToPush> packets = waitingPackets.removeAll(spfIntent.getId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800475 if (!packets.isEmpty()) {
TeruU7feef8a2014-04-03 00:15:49 -0700476 log.warn("Removed flow {} has packets queued.", spfIntent.getId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800477 }
TeruU417fe022014-02-04 12:59:30 -0800478 pendingFlows.remove(removedPath);
TeruU7feef8a2014-04-03 00:15:49 -0700479 log.debug("Removed from the pendingFlow: Path {}, Flow ID {}", removedPath, spfIntent.getId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800480 }
481 }
TeruU7feef8a2014-04-03 00:15:49 -0700482
483 private void flowInstalled(PathIntent installedPath) {
484 if(log.isTraceEnabled()){
485 log.trace("Path {} was installed", installedPath.getParentIntent().getId());
Jonathan Hart0444d932014-01-22 15:06:17 -0800486 }
487
TeruU7feef8a2014-04-03 00:15:49 -0700488 ShortestPathIntent spfIntent = (ShortestPathIntent) installedPath.getParentIntent();
489 MACAddress srcMacAddress = MACAddress.valueOf(spfIntent.getSrcMac());
490 MACAddress dstMacAddress = MACAddress.valueOf(spfIntent.getDstMac());
491 Path path = new Path(srcMacAddress, dstMacAddress);
492 log.debug("Path spec {}", path);
TeruU417fe022014-02-04 12:59:30 -0800493
Jonathan Hart0444d932014-01-22 15:06:17 -0800494 // TODO waiting packets should time out. We could request a path that
495 // can't be installed right now because of a network partition. The path
496 // may eventually be installed, but we may have received thousands of
497 // packets in the meantime and probably don't want to send very old packets.
Jonathan Harte789d6e2013-12-17 17:50:11 -0800498
TeruU7feef8a2014-04-03 00:15:49 -0700499 List<PacketToPush> packets = null;
500 net.onrc.onos.ofcontroller.networkgraph.Path graphPath = installedPath.getPath();
501
502 log.debug("path{}", graphPath);
503 Short outPort = graphPath.get(0).getSrc().getNumber().shortValue();
504
505 PushedFlow existingFlow = null;
Jonathan Harte789d6e2013-12-17 17:50:11 -0800506
Jonathan Hart7e6df362013-12-10 23:33:59 -0800507 synchronized (lock) {
TeruU7feef8a2014-04-03 00:15:49 -0700508 existingFlow = pendingFlows.get(path);
TeruU417fe022014-02-04 12:59:30 -0800509
Jonathan Hart0444d932014-01-22 15:06:17 -0800510 if (existingFlow != null) {
511 existingFlow.installed = true;
TeruU417fe022014-02-04 12:59:30 -0800512 existingFlow.firstOutPort = outPort;
513 } else {
TeruU7feef8a2014-04-03 00:15:49 -0700514 log.debug("ExistingFlow {} is null", path);
TeruU417fe022014-02-04 12:59:30 -0800515 return;
516 }
517
TeruU6464af02014-02-06 21:38:45 -0800518 //Check both existing flow are installed status.
519 if(existingFlow.installed){
TeruU7feef8a2014-04-03 00:15:49 -0700520 packets = waitingPackets.removeAll(existingFlow.intentId);
TeruU417fe022014-02-04 12:59:30 -0800521 if(log.isDebugEnabled()){
522 log.debug("removed my packets {} to push from waitingPackets. outPort {} size {}",
TeruU7feef8a2014-04-03 00:15:49 -0700523 existingFlow.intentId, existingFlow.firstOutPort, packets.size());
TeruU417fe022014-02-04 12:59:30 -0800524 }
TeruU417fe022014-02-04 12:59:30 -0800525 }else{
526 log.debug("Forward or reverse flows hasn't been pushed yet. return");
527 return;
Jonathan Hart0444d932014-01-22 15:06:17 -0800528 }
Jonathan Hart7e6df362013-12-10 23:33:59 -0800529 }
TeruU7feef8a2014-04-03 00:15:49 -0700530
Jonathan Hart5e448782013-12-10 12:36:35 -0800531 for (PacketToPush packet : packets) {
TeruU7feef8a2014-04-03 00:15:49 -0700532 log.debug("Start packetToPush to sw {}, outPort {}, path {}", packet.dpid, existingFlow.firstOutPort, path);
Jonathan Hart5e448782013-12-10 12:36:35 -0800533 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
TeruU417fe022014-02-04 12:59:30 -0800534 sendPacketOut(sw, packet.packet, existingFlow.firstOutPort);
535 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800536 }
Jonathan Harte789d6e2013-12-17 17:50:11 -0800537
538 private void sendPacketOut(IOFSwitch sw, OFPacketOut po, short outPort) {
539 po.getActions().add(new OFActionOutput(outPort));
540 po.setActionsLength((short)
541 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
542 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
543
544 flowPusher.add(sw, po);
545 }
Jonathan Hart0444d932014-01-22 15:06:17 -0800546
TeruU7feef8a2014-04-03 00:15:49 -0700547 @Override
548 public void entryAdded(IntentStateList value) {
549 entryUpdated(value);
550
551 }
552
553 @Override
554 public void entryRemoved(IntentStateList value) {
555 //no-op
556 }
557
558 @Override
559 public void entryUpdated(IntentStateList value) {
560 for (Entry<String, IntentState> entry: value.entrySet()) {
561 log.debug("path intent key {}, value {}", entry.getKey(), entry.getValue());
562 PathIntent pathIntent = (PathIntent) intentMap.getIntent(entry.getKey());
563 if (pathIntent == null)
564 continue;
565
566 if (!(pathIntent.getParentIntent() instanceof ShortestPathIntent))
567 continue;
568
569 IntentState state = entry.getValue();
570 switch (state) {
571 case INST_REQ:
572 break;
573 case INST_ACK:
574 flowInstalled(pathIntent);
575 break;
576 case INST_NACK:
577 break;
578 case DEL_REQ:
579 break;
580 case DEL_ACK:
581 flowRemoved(pathIntent);
582 break;
583 case DEL_PENDING:
584 break;
585 default:
586 break;
587 }
588 }
589 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800590}