blob: 23709369fb3e85945c3550c604e2a5ca2141b0d7 [file] [log] [blame]
Jonathan Hart1caaa932013-11-04 15:28:28 -08001package net.onrc.onos.ofcontroller.forwarding;
2
Jonathan Harte93aed42013-12-05 18:39:50 -08003import java.util.ArrayList;
4import 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 Harte93aed42013-12-05 18:39:50 -08007import java.util.List;
8import java.util.Map;
Jonathan Hart5e448782013-12-10 12:36:35 -08009import java.util.concurrent.ConcurrentHashMap;
Jonathan Hart1caaa932013-11-04 15:28:28 -080010
11import net.floodlightcontroller.core.FloodlightContext;
12import net.floodlightcontroller.core.IFloodlightProviderService;
13import net.floodlightcontroller.core.IOFMessageListener;
14import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080015import net.floodlightcontroller.core.module.FloodlightModuleContext;
16import net.floodlightcontroller.core.module.IFloodlightModule;
17import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080018import net.floodlightcontroller.packet.Ethernet;
19import net.floodlightcontroller.util.MACAddress;
20import net.onrc.onos.ofcontroller.core.IDeviceStorage;
21import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
22import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
23import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
24import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart1caaa932013-11-04 15:28:28 -080025import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080026import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080027import net.onrc.onos.ofcontroller.topology.TopologyManager;
28import net.onrc.onos.ofcontroller.util.CallerId;
29import net.onrc.onos.ofcontroller.util.DataPath;
30import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080031import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080032import net.onrc.onos.ofcontroller.util.FlowId;
33import net.onrc.onos.ofcontroller.util.FlowPath;
34import net.onrc.onos.ofcontroller.util.FlowPathType;
35import net.onrc.onos.ofcontroller.util.FlowPathUserState;
36import net.onrc.onos.ofcontroller.util.Port;
37import net.onrc.onos.ofcontroller.util.SwitchPort;
38
39import org.openflow.protocol.OFMessage;
40import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080041import org.openflow.protocol.OFPacketOut;
42import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080043import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080044import org.openflow.protocol.action.OFAction;
45import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080046import org.openflow.util.HexString;
47import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
Jonathan Hart5e448782013-12-10 12:36:35 -080050import com.google.common.collect.HashMultimap;
51import com.google.common.collect.Multimap;
Jonathan Hart5e448782013-12-10 12:36:35 -080052
53public class Forwarding implements IOFMessageListener, IFloodlightModule,
54 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080055 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
56
Jonathan Hart7e6df362013-12-10 23:33:59 -080057 private final int IDLE_TIMEOUT = 5; // seconds
58 private final int HARD_TIMEOUT = 0; // seconds
59
Jonathan Hart1caaa932013-11-04 15:28:28 -080060 private IFloodlightProviderService floodlightProvider;
61 private IFlowService flowService;
Jonathan Hart7e6df362013-12-10 23:33:59 -080062 private IFlowPusherService flowPusher;
Jonathan Hart1caaa932013-11-04 15:28:28 -080063
64 private IDeviceStorage deviceStorage;
65 private TopologyManager topologyService;
66
Jonathan Hart5e448782013-12-10 12:36:35 -080067 private Map<Path, Long> pendingFlows;
68 private Multimap<Long, PacketToPush> waitingPackets;
69
Jonathan Hart7e6df362013-12-10 23:33:59 -080070 private final Object lock = new Object();
71
Jonathan Hart5e448782013-12-10 12:36:35 -080072 public class PacketToPush {
73 public final OFPacketOut packet;
74 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080075
Jonathan Hart5e448782013-12-10 12:36:35 -080076 public PacketToPush(OFPacketOut packet, long dpid) {
77 this.packet = packet;
78 this.dpid = dpid;
79 }
80 }
81
82 public final class Path {
83 public final SwitchPort srcPort;
84 public final SwitchPort dstPort;
85
86 public Path(SwitchPort src, SwitchPort dst) {
87 srcPort = new SwitchPort(new Dpid(src.dpid().value()),
88 new Port(src.port().value()));
89 dstPort = new SwitchPort(new Dpid(dst.dpid().value()),
90 new Port(dst.port().value()));
91 }
92
93 @Override
94 public boolean equals(Object other) {
95 if (!(other instanceof Path)) {
96 return false;
97 }
98
99 Path otherPath = (Path) other;
100 return srcPort.equals(otherPath.srcPort) &&
101 dstPort.equals(otherPath.dstPort);
102 }
103
104 @Override
105 public int hashCode() {
106 int hash = 17;
107 hash = 31 * hash + srcPort.hashCode();
108 hash = 31 * hash + dstPort.hashCode();
109 return hash;
110 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800111 }
112
Jonathan Harte93aed42013-12-05 18:39:50 -0800113 @Override
114 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800115 List<Class<? extends IFloodlightService>> services =
116 new ArrayList<Class<? extends IFloodlightService>>(1);
117 services.add(IForwardingService.class);
118 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800119 }
120
121 @Override
122 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800123 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
124 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
125 impls.put(IForwardingService.class, this);
126 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800127 }
128
129 @Override
130 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
131 List<Class<? extends IFloodlightService>> dependencies =
132 new ArrayList<Class<? extends IFloodlightService>>();
133 dependencies.add(IFloodlightProviderService.class);
134 dependencies.add(IFlowService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800135 dependencies.add(IFlowPusherService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800136 return dependencies;
137 }
138
139 @Override
140 public void init(FloodlightModuleContext context) {
Jonathan Hart7e6df362013-12-10 23:33:59 -0800141 floodlightProvider =
Jonathan Harte93aed42013-12-05 18:39:50 -0800142 context.getServiceImpl(IFloodlightProviderService.class);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800143 flowService = context.getServiceImpl(IFlowService.class);
144 flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800145
146 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
147
Jonathan Hart5e448782013-12-10 12:36:35 -0800148 pendingFlows = new ConcurrentHashMap<Path, Long>();
Jonathan Hart7e6df362013-12-10 23:33:59 -0800149 //waitingPackets = Multimaps.synchronizedSetMultimap(
150 //HashMultimap.<Long, PacketToPush>create());
151 waitingPackets = HashMultimap.create();
Jonathan Hart5e448782013-12-10 12:36:35 -0800152
Jonathan Hart1caaa932013-11-04 15:28:28 -0800153 deviceStorage = new DeviceStorageImpl();
154 deviceStorage.init("");
155 topologyService = new TopologyManager();
156 topologyService.init("");
157 }
158
Jonathan Harte93aed42013-12-05 18:39:50 -0800159 @Override
160 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800161 // no-op
162 }
163
164 @Override
165 public String getName() {
166 return "onosforwarding";
167 }
168
169 @Override
170 public boolean isCallbackOrderingPrereq(OFType type, String name) {
171 return (type == OFType.PACKET_IN) &&
172 (name.equals("devicemanager") || name.equals("proxyarpmanager"));
173 }
174
175 @Override
176 public boolean isCallbackOrderingPostreq(OFType type, String name) {
177 return false;
178 }
179
180 @Override
181 public Command receive(
182 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
183
184 if (msg.getType() != OFType.PACKET_IN) {
185 return Command.CONTINUE;
186 }
187
188 OFPacketIn pi = (OFPacketIn) msg;
189
190 Ethernet eth = IFloodlightProviderService.bcStore.
191 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
192
Jonathan Hart4fb16d82013-11-07 22:41:32 -0800193 // We only want to handle unicast IPv4
194 if (eth.isBroadcast() || eth.isMulticast() ||
195 eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800196 return Command.CONTINUE;
197 }
198
199 handlePacketIn(sw, pi, eth);
200
201 return Command.STOP;
202 }
203
204 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800205 String destinationMac =
206 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800207
208 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
209 destinationMac);
210
211 if (deviceObject == null) {
212 log.debug("No device entry found for {}", destinationMac);
213 return;
214 }
215
Jonathan Hart5e448782013-12-10 12:36:35 -0800216 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800217 if (!ports.hasNext()) {
218 log.debug("No attachment point found for device {}", destinationMac);
219 return;
220 }
221 IPortObject portObject = ports.next();
222 short destinationPort = portObject.getNumber();
223 ISwitchObject switchObject = portObject.getSwitch();
224 long destinationDpid = HexString.toLong(switchObject.getDPID());
225
Jonathan Hart41d1e912013-11-24 16:50:25 -0800226 // TODO SwitchPort, Dpid and Port should probably be immutable
227 // (also, are Dpid and Port are even necessary?)
Jonathan Hart1caaa932013-11-04 15:28:28 -0800228 SwitchPort srcSwitchPort = new SwitchPort(
229 new Dpid(sw.getId()), new Port(pi.getInPort()));
230 SwitchPort dstSwitchPort = new SwitchPort(
231 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800232
233 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
234 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800235
Jonathan Hart5e448782013-12-10 12:36:35 -0800236
Jonathan Hart7e6df362013-12-10 23:33:59 -0800237 FlowPath flowPath, reverseFlowPath;
Jonathan Hart5e448782013-12-10 12:36:35 -0800238
239 Path pathspec = new Path(srcSwitchPort, dstSwitchPort);
240 // TODO check concurrency
Jonathan Hart7e6df362013-12-10 23:33:59 -0800241 synchronized (lock) {
242 Long existingFlowId = pendingFlows.get(pathspec);
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800243
Jonathan Hart7e6df362013-12-10 23:33:59 -0800244 if (existingFlowId != null) {
245 log.debug("Found existing flow {}",
246 HexString.toHexString(existingFlowId));
247
248 OFPacketOut po = constructPacketOut(pi, sw);
249 waitingPackets.put(existingFlowId, new PacketToPush(po, sw.getId()));
250 return;
251 }
252
253
254 log.debug("Adding new flow between {} at {} and {} at {}",
255 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
256
257
258 CallerId callerId = new CallerId("Forwarding");
259
260 DataPath datapath = new DataPath();
261 datapath.setSrcPort(srcSwitchPort);
262 datapath.setDstPort(dstSwitchPort);
263
264 flowPath = new FlowPath();
265 flowPath.setInstallerId(callerId);
266
267 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
268 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
269 flowPath.setFlowEntryMatch(new FlowEntryMatch());
270 flowPath.setIdleTimeout(IDLE_TIMEOUT);
271 flowPath.setHardTimeout(HARD_TIMEOUT);
272 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
273 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
274 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
275 flowPath.setDataPath(datapath);
276
277
278 DataPath reverseDataPath = new DataPath();
279 // Reverse the ports for the reverse path
280 reverseDataPath.setSrcPort(dstSwitchPort);
281 reverseDataPath.setDstPort(srcSwitchPort);
282
283 // TODO implement copy constructor for FlowPath
284 reverseFlowPath = new FlowPath();
285 reverseFlowPath.setInstallerId(callerId);
286 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
287 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
288 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
289 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
290 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
291 // Reverse the MAC addresses for the reverse path
292 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
293 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
294 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
295 reverseFlowPath.setDataPath(reverseDataPath);
296 reverseFlowPath.dataPath().srcPort().dpid().toString();
297
298 // TODO what happens if no path exists? cleanup
299
300 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
301 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
302
303 flowPath.setFlowId(flowId);
304 reverseFlowPath.setFlowId(reverseFlowId);
305
306 OFPacketOut po = constructPacketOut(pi, sw);
307 Path reversePathSpec = new Path(dstSwitchPort, srcSwitchPort);
308
309 // Add to waiting lists
310 pendingFlows.put(pathspec, flowId.value());
311 pendingFlows.put(reversePathSpec, reverseFlowId.value());
312 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
313
Jonathan Hart41d1e912013-11-24 16:50:25 -0800314 }
315
Jonathan Hart5e448782013-12-10 12:36:35 -0800316 flowService.addFlow(reverseFlowPath);
317 flowService.addFlow(flowPath);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800318
Jonathan Hart1caaa932013-11-04 15:28:28 -0800319 }
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800320
Jonathan Hart5e448782013-12-10 12:36:35 -0800321 /*
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800322 private boolean flowExists(SwitchPort srcPort, MACAddress srcMac,
323 SwitchPort dstPort, MACAddress dstMac) {
324 for (FlowPath flow : datagridService.getAllFlows()) {
325 FlowEntryMatch match = flow.flowEntryMatch();
326 // TODO implement FlowEntryMatch.equals();
327 // This is painful to do properly without support in the FlowEntryMatch
328 boolean same = true;
329 if (!match.srcMac().equals(srcMac) ||
330 !match.dstMac().equals(dstMac)) {
331 same = false;
332 }
333 if (!flow.dataPath().srcPort().equals(srcPort) ||
334 !flow.dataPath().dstPort().equals(dstPort)) {
335 same = false;
336 }
337
338 if (same) {
339 log.debug("found flow entry that's the same {}-{}:::{}-{}",
340 new Object[] {srcPort, srcMac, dstPort, dstMac});
341 return true;
342 }
343 }
344
345 return false;
346 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800347 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800348
Jonathan Hart7e6df362013-12-10 23:33:59 -0800349 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800350 OFPacketOut po = new OFPacketOut();
351 po.setInPort(OFPort.OFPP_NONE)
352 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800353 .setActions(new ArrayList<OFAction>())
354 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800355
356 if (sw.getBuffers() == 0) {
357 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
358 .setPacketData(pi.getPacketData())
359 .setLengthU(po.getLengthU() + po.getPacketData().length);
360 }
361 else {
362 po.setBufferId(pi.getBufferId());
363 }
364
Jonathan Hart5e448782013-12-10 12:36:35 -0800365 return po;
366 }
367
368 @Override
369 public void flowInstalled(FlowPath installedFlowPath) {
370 // TODO check concurrency
371 // will need to sync and access both collections at once.
372 long flowId = installedFlowPath.flowId().value();
Jonathan Hart5e448782013-12-10 12:36:35 -0800373
Jonathan Hart7e6df362013-12-10 23:33:59 -0800374 Collection<PacketToPush> packets;
375 synchronized (lock) {
376 packets = waitingPackets.removeAll(flowId);
377
378 //remove pending flows entry
379 Path pathToRemove = new Path(installedFlowPath.dataPath().srcPort(),
380 installedFlowPath.dataPath().dstPort());
381 pendingFlows.remove(pathToRemove);
382
383 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800384
385 for (PacketToPush packet : packets) {
386 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
387
388 OFPacketOut po = packet.packet;
389 short outPort =
390 installedFlowPath.flowEntries().get(0).outPort().value();
391 po.getActions().add(new OFActionOutput(outPort));
392 po.setActionsLength((short)
393 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
394 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
395
Jonathan Hart7e6df362013-12-10 23:33:59 -0800396 flowPusher.add(sw, po);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800397 }
398 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800399}