blob: bd51fdc807f1c3d1e0307bc6888006806472db7e [file] [log] [blame]
Jonathan Hart1caaa932013-11-04 15:28:28 -08001package net.onrc.onos.ofcontroller.forwarding;
2
Jonathan Hart17672992013-12-12 16:15:16 -08003import java.net.InetAddress;
Jonathan Harte93aed42013-12-05 18:39:50 -08004import java.util.ArrayList;
5import java.util.Collection;
Jonathan Hartd33a6cf2013-12-10 14:29:08 -08006import java.util.HashMap;
Jonathan Hart1caaa932013-11-04 15:28:28 -08007import java.util.Iterator;
Jonathan Harte93aed42013-12-05 18:39:50 -08008import java.util.List;
9import java.util.Map;
Jonathan Hart5e448782013-12-10 12:36:35 -080010import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart1caaa932013-11-04 15:28:28 -080011
12import net.floodlightcontroller.core.FloodlightContext;
13import net.floodlightcontroller.core.IFloodlightProviderService;
14import net.floodlightcontroller.core.IOFMessageListener;
15import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080016import net.floodlightcontroller.core.module.FloodlightModuleContext;
17import net.floodlightcontroller.core.module.IFloodlightModule;
18import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080019import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart17672992013-12-12 16:15:16 -080020import net.floodlightcontroller.packet.IPv4;
Jonathan Hart1caaa932013-11-04 15:28:28 -080021import net.floodlightcontroller.util.MACAddress;
Jonathan Hart17672992013-12-12 16:15:16 -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 Hart1caaa932013-11-04 15:28:28 -080028import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080029import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart17672992013-12-12 16:15:16 -080030import net.onrc.onos.ofcontroller.proxyarp.ArpMessage;
Jonathan Hart1caaa932013-11-04 15:28:28 -080031import net.onrc.onos.ofcontroller.topology.TopologyManager;
32import net.onrc.onos.ofcontroller.util.CallerId;
33import net.onrc.onos.ofcontroller.util.DataPath;
34import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080035import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080036import net.onrc.onos.ofcontroller.util.FlowId;
37import net.onrc.onos.ofcontroller.util.FlowPath;
38import net.onrc.onos.ofcontroller.util.FlowPathType;
39import net.onrc.onos.ofcontroller.util.FlowPathUserState;
40import net.onrc.onos.ofcontroller.util.Port;
41import net.onrc.onos.ofcontroller.util.SwitchPort;
42
43import org.openflow.protocol.OFMessage;
44import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080045import org.openflow.protocol.OFPacketOut;
46import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080047import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080048import org.openflow.protocol.action.OFAction;
49import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080050import org.openflow.util.HexString;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Jonathan Hart5e448782013-12-10 12:36:35 -080054import com.google.common.collect.HashMultimap;
55import com.google.common.collect.Multimap;
Jonathan Hart17672992013-12-12 16:15:16 -080056import com.google.common.net.InetAddresses;
Jonathan Hart5e448782013-12-10 12:36:35 -080057
58public class Forwarding implements IOFMessageListener, IFloodlightModule,
59 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080060 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
61
Jonathan Hart7e6df362013-12-10 23:33:59 -080062 private final int IDLE_TIMEOUT = 5; // seconds
63 private final int HARD_TIMEOUT = 0; // seconds
64
Jonathan Hart1caaa932013-11-04 15:28:28 -080065 private IFloodlightProviderService floodlightProvider;
66 private IFlowService flowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080067 private IFlowPusherService flowPusher;
Jonathan Hart17672992013-12-12 16:15:16 -080068 private IDatagridService datagrid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080069
70 private IDeviceStorage deviceStorage;
71 private TopologyManager topologyService;
72
Jonathan Hart5e448782013-12-10 12:36:35 -080073 private Map<Path, Long> pendingFlows;
74 private Multimap<Long, PacketToPush> waitingPackets;
75
Jonathan Hart7e6df362013-12-10 23:33:59 -080076 private final Object lock = new Object();
77
Jonathan Hart5e448782013-12-10 12:36:35 -080078 public class PacketToPush {
79 public final OFPacketOut packet;
80 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080081
Jonathan Hart5e448782013-12-10 12:36:35 -080082 public PacketToPush(OFPacketOut packet, long dpid) {
83 this.packet = packet;
84 this.dpid = dpid;
85 }
86 }
87
88 public final class Path {
89 public final SwitchPort srcPort;
90 public final SwitchPort dstPort;
91
92 public Path(SwitchPort src, SwitchPort dst) {
93 srcPort = new SwitchPort(new Dpid(src.dpid().value()),
94 new Port(src.port().value()));
95 dstPort = new SwitchPort(new Dpid(dst.dpid().value()),
96 new Port(dst.port().value()));
97 }
98
99 @Override
100 public boolean equals(Object other) {
101 if (!(other instanceof Path)) {
102 return false;
103 }
104
105 Path otherPath = (Path) other;
106 return srcPort.equals(otherPath.srcPort) &&
107 dstPort.equals(otherPath.dstPort);
108 }
109
110 @Override
111 public int hashCode() {
112 int hash = 17;
113 hash = 31 * hash + srcPort.hashCode();
114 hash = 31 * hash + dstPort.hashCode();
115 return hash;
116 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800117 }
118
Jonathan Harte93aed42013-12-05 18:39:50 -0800119 @Override
120 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800121 List<Class<? extends IFloodlightService>> services =
122 new ArrayList<Class<? extends IFloodlightService>>(1);
123 services.add(IForwardingService.class);
124 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800125 }
126
127 @Override
128 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800129 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
130 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
131 impls.put(IForwardingService.class, this);
132 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800133 }
134
135 @Override
136 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
137 List<Class<? extends IFloodlightService>> dependencies =
138 new ArrayList<Class<? extends IFloodlightService>>();
139 dependencies.add(IFloodlightProviderService.class);
140 dependencies.add(IFlowService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800141 dependencies.add(IFlowPusherService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800142 return dependencies;
143 }
144
145 @Override
146 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800147 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800148 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800149 flowService = context.getServiceImpl(IFlowService.class);
150 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart17672992013-12-12 16:15:16 -0800151 datagrid = context.getServiceImpl(IDatagridService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800152
153 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
154
Jonathan Hart5e448782013-12-10 12:36:35 -0800155 pendingFlows = new ConcurrentHashMap<Path, Long>();
Jonathan Hart7e6df362013-12-10 23:33:59 -0800156 //waitingPackets = Multimaps.synchronizedSetMultimap(
157 //HashMultimap.<Long, PacketToPush>create());
158 waitingPackets = HashMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800159
Jonathan Hart1caaa932013-11-04 15:28:28 -0800160 deviceStorage = new DeviceStorageImpl();
161 deviceStorage.init("");
162 topologyService = new TopologyManager();
163 topologyService.init("");
164 }
165
Jonathan Harte93aed42013-12-05 18:39:50 -0800166 @Override
167 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800168 // no-op
169 }
170
171 @Override
172 public String getName() {
173 return "onosforwarding";
174 }
175
176 @Override
177 public boolean isCallbackOrderingPrereq(OFType type, String name) {
178 return (type == OFType.PACKET_IN) &&
179 (name.equals("devicemanager") || name.equals("proxyarpmanager"));
180 }
181
182 @Override
183 public boolean isCallbackOrderingPostreq(OFType type, String name) {
184 return false;
185 }
186
187 @Override
188 public Command receive(
189 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
190
191 if (msg.getType() != OFType.PACKET_IN) {
192 return Command.CONTINUE;
193 }
194
195 OFPacketIn pi = (OFPacketIn) msg;
196
197 Ethernet eth = IFloodlightProviderService.bcStore.
198 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
199
Jonathan Hart17672992013-12-12 16:15:16 -0800200 if (eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800201 return Command.CONTINUE;
202 }
203
Jonathan Hart17672992013-12-12 16:15:16 -0800204 if (eth.isBroadcast() || eth.isMulticast()) {
205 handleBroadcast(sw, pi, eth);
206 //return Command.CONTINUE;
207 }
208 else {
209 // Unicast
210 handlePacketIn(sw, pi, eth);
211 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800212
213 return Command.STOP;
214 }
215
Jonathan Hart17672992013-12-12 16:15:16 -0800216 private void handleBroadcast(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
217 if (log.isTraceEnabled()) {
218 log.trace("Sending broadcast packet to other ONOS instances");
219 }
220
221 IPv4 ipv4Packet = (IPv4) eth.getPayload();
222
223 // TODO We'll put the destination address here, because the current
224 // architecture needs an address. Addresses are only used for replies
225 // however, which don't apply to non-ARP packets. The ArpMessage class
226 // has become a bit too overloaded and should be refactored to
227 // handle all use cases nicely.
228 InetAddress targetAddress =
229 InetAddresses.fromInteger(ipv4Packet.getDestinationAddress());
230
231 // Piggy-back on the ARP mechanism to broadcast this packet out the
232 // edge. Luckily the ARP module doesn't check that the packet is
233 // actually ARP before broadcasting, so we can trick it into sending
234 // our non-ARP packets.
235 // TODO This should be refactored later to account for the new use case.
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800236 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(),
237 sw.getId(), pi.getInPort()));
Jonathan Hart17672992013-12-12 16:15:16 -0800238 }
239
Jonathan Hart1caaa932013-11-04 15:28:28 -0800240 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800241 String destinationMac =
242 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800243
244 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
245 destinationMac);
246
247 if (deviceObject == null) {
248 log.debug("No device entry found for {}", destinationMac);
249 return;
250 }
251
Jonathan Hart5e448782013-12-10 12:36:35 -0800252 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800253 if (!ports.hasNext()) {
254 log.debug("No attachment point found for device {}", destinationMac);
255 return;
256 }
257 IPortObject portObject = ports.next();
258 short destinationPort = portObject.getNumber();
259 ISwitchObject switchObject = portObject.getSwitch();
260 long destinationDpid = HexString.toLong(switchObject.getDPID());
261
Jonathan Hart41d1e912013-11-24 16:50:25 -0800262 // TODO SwitchPort, Dpid and Port should probably be immutable
263 // (also, are Dpid and Port are even necessary?)
Jonathan Hart1caaa932013-11-04 15:28:28 -0800264 SwitchPort srcSwitchPort = new SwitchPort(
265 new Dpid(sw.getId()), new Port(pi.getInPort()));
266 SwitchPort dstSwitchPort = new SwitchPort(
267 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800268
269 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
270 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800271
Jonathan Hart5e448782013-12-10 12:36:35 -0800272
Jonathan Hart7e6df362013-12-10 23:33:59 -0800273 FlowPath flowPath, reverseFlowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800274
275 Path pathspec = new Path(srcSwitchPort, dstSwitchPort);
276 // TODO check concurrency
Jonathan Hart7e6df362013-12-10 23:33:59 -0800277 synchronized (lock) {
278 Long existingFlowId = pendingFlows.get(pathspec);
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800279
Jonathan Hart7e6df362013-12-10 23:33:59 -0800280 if (existingFlowId != null) {
281 log.debug("Found existing flow {}",
282 HexString.toHexString(existingFlowId));
283
284 OFPacketOut po = constructPacketOut(pi, sw);
285 waitingPackets.put(existingFlowId, new PacketToPush(po, sw.getId()));
286 return;
287 }
288
Jonathan Hart7e6df362013-12-10 23:33:59 -0800289 log.debug("Adding new flow between {} at {} and {} at {}",
290 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
291
292
293 CallerId callerId = new CallerId("Forwarding");
294
295 DataPath datapath = new DataPath();
296 datapath.setSrcPort(srcSwitchPort);
297 datapath.setDstPort(dstSwitchPort);
298
299 flowPath = new FlowPath();
300 flowPath.setInstallerId(callerId);
301
302 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
303 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
304 flowPath.setFlowEntryMatch(new FlowEntryMatch());
305 flowPath.setIdleTimeout(IDLE_TIMEOUT);
306 flowPath.setHardTimeout(HARD_TIMEOUT);
307 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
308 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
309 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
310 flowPath.setDataPath(datapath);
311
312
313 DataPath reverseDataPath = new DataPath();
314 // Reverse the ports for the reverse path
315 reverseDataPath.setSrcPort(dstSwitchPort);
316 reverseDataPath.setDstPort(srcSwitchPort);
317
318 // TODO implement copy constructor for FlowPath
319 reverseFlowPath = new FlowPath();
320 reverseFlowPath.setInstallerId(callerId);
321 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
322 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
323 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
324 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
325 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
326 // Reverse the MAC addresses for the reverse path
327 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
328 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
329 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
330 reverseFlowPath.setDataPath(reverseDataPath);
331 reverseFlowPath.dataPath().srcPort().dpid().toString();
332
333 // TODO what happens if no path exists? cleanup
334
335 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
336 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
337
338 flowPath.setFlowId(flowId);
339 reverseFlowPath.setFlowId(reverseFlowId);
340
341 OFPacketOut po = constructPacketOut(pi, sw);
342 Path reversePathSpec = new Path(dstSwitchPort, srcSwitchPort);
343
344 // Add to waiting lists
345 pendingFlows.put(pathspec, flowId.value());
346 pendingFlows.put(reversePathSpec, reverseFlowId.value());
347 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
348
Jonathan Hart41d1e912013-11-24 16:50:25 -0800349 }
350
Jonathan Hart5e448782013-12-10 12:36:35 -0800351 flowService.addFlow(reverseFlowPath);
352 flowService.addFlow(flowPath);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800353
Jonathan Hart1caaa932013-11-04 15:28:28 -0800354 }
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800355
Jonathan Hart5e448782013-12-10 12:36:35 -0800356 /*
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800357 private boolean flowExists(SwitchPort srcPort, MACAddress srcMac,
358 SwitchPort dstPort, MACAddress dstMac) {
359 for (FlowPath flow : datagridService.getAllFlows()) {
360 FlowEntryMatch match = flow.flowEntryMatch();
361 // TODO implement FlowEntryMatch.equals();
362 // This is painful to do properly without support in the FlowEntryMatch
363 boolean same = true;
364 if (!match.srcMac().equals(srcMac) ||
365 !match.dstMac().equals(dstMac)) {
366 same = false;
367 }
368 if (!flow.dataPath().srcPort().equals(srcPort) ||
369 !flow.dataPath().dstPort().equals(dstPort)) {
370 same = false;
371 }
372
373 if (same) {
374 log.debug("found flow entry that's the same {}-{}:::{}-{}",
375 new Object[] {srcPort, srcMac, dstPort, dstMac});
376 return true;
377 }
378 }
379
380 return false;
381 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800382 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800383
Jonathan Hart7e6df362013-12-10 23:33:59 -0800384 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800385 OFPacketOut po = new OFPacketOut();
386 po.setInPort(OFPort.OFPP_NONE)
387 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800388 .setActions(new ArrayList<OFAction>())
389 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800390
391 if (sw.getBuffers() == 0) {
392 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
393 .setPacketData(pi.getPacketData())
394 .setLengthU(po.getLengthU() + po.getPacketData().length);
395 }
396 else {
397 po.setBufferId(pi.getBufferId());
398 }
399
Jonathan Hart5e448782013-12-10 12:36:35 -0800400 return po;
401 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800402
Jonathan Hart5e448782013-12-10 12:36:35 -0800403 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800404 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
405 for (FlowPath flowPath : installedFlowPaths) {
406 flowInstalled(flowPath);
407 }
408 }
409
410 private void flowInstalled(FlowPath installedFlowPath) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800411 // TODO check concurrency
412 // will need to sync and access both collections at once.
413 long flowId = installedFlowPath.flowId().value();
Jonathan Hart5e448782013-12-10 12:36:35 -0800414
Jonathan Hart7e6df362013-12-10 23:33:59 -0800415 Collection<PacketToPush> packets;
416 synchronized (lock) {
417 packets = waitingPackets.removeAll(flowId);
418
419 //remove pending flows entry
420 Path pathToRemove = new Path(installedFlowPath.dataPath().srcPort(),
421 installedFlowPath.dataPath().dstPort());
422 pendingFlows.remove(pathToRemove);
423
424 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800425
426 for (PacketToPush packet : packets) {
427 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
428
429 OFPacketOut po = packet.packet;
430 short outPort =
431 installedFlowPath.flowEntries().get(0).outPort().value();
432 po.getActions().add(new OFActionOutput(outPort));
433 po.setActionsLength((short)
434 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
435 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
436
Jonathan Hart7e6df362013-12-10 23:33:59 -0800437 flowPusher.add(sw, po);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800438 }
439 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800440}