blob: c70d4db2619e7702e846664eda49b05ca5e72a9b [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
Jonathan Hart7e6df362013-12-10 23:33:59 -0800253 log.debug("Adding new flow between {} at {} and {} at {}",
254 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
255
256
257 CallerId callerId = new CallerId("Forwarding");
258
259 DataPath datapath = new DataPath();
260 datapath.setSrcPort(srcSwitchPort);
261 datapath.setDstPort(dstSwitchPort);
262
263 flowPath = new FlowPath();
264 flowPath.setInstallerId(callerId);
265
266 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
267 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
268 flowPath.setFlowEntryMatch(new FlowEntryMatch());
269 flowPath.setIdleTimeout(IDLE_TIMEOUT);
270 flowPath.setHardTimeout(HARD_TIMEOUT);
271 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
272 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
273 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
274 flowPath.setDataPath(datapath);
275
276
277 DataPath reverseDataPath = new DataPath();
278 // Reverse the ports for the reverse path
279 reverseDataPath.setSrcPort(dstSwitchPort);
280 reverseDataPath.setDstPort(srcSwitchPort);
281
282 // TODO implement copy constructor for FlowPath
283 reverseFlowPath = new FlowPath();
284 reverseFlowPath.setInstallerId(callerId);
285 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
286 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
287 reverseFlowPath.setIdleTimeout(IDLE_TIMEOUT);
288 reverseFlowPath.setHardTimeout(HARD_TIMEOUT);
289 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
290 // Reverse the MAC addresses for the reverse path
291 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
292 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
293 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
294 reverseFlowPath.setDataPath(reverseDataPath);
295 reverseFlowPath.dataPath().srcPort().dpid().toString();
296
297 // TODO what happens if no path exists? cleanup
298
299 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
300 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
301
302 flowPath.setFlowId(flowId);
303 reverseFlowPath.setFlowId(reverseFlowId);
304
305 OFPacketOut po = constructPacketOut(pi, sw);
306 Path reversePathSpec = new Path(dstSwitchPort, srcSwitchPort);
307
308 // Add to waiting lists
309 pendingFlows.put(pathspec, flowId.value());
310 pendingFlows.put(reversePathSpec, reverseFlowId.value());
311 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
312
Jonathan Hart41d1e912013-11-24 16:50:25 -0800313 }
314
Jonathan Hart5e448782013-12-10 12:36:35 -0800315 flowService.addFlow(reverseFlowPath);
316 flowService.addFlow(flowPath);
Jonathan Hart7e6df362013-12-10 23:33:59 -0800317
Jonathan Hart1caaa932013-11-04 15:28:28 -0800318 }
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800319
Jonathan Hart5e448782013-12-10 12:36:35 -0800320 /*
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800321 private boolean flowExists(SwitchPort srcPort, MACAddress srcMac,
322 SwitchPort dstPort, MACAddress dstMac) {
323 for (FlowPath flow : datagridService.getAllFlows()) {
324 FlowEntryMatch match = flow.flowEntryMatch();
325 // TODO implement FlowEntryMatch.equals();
326 // This is painful to do properly without support in the FlowEntryMatch
327 boolean same = true;
328 if (!match.srcMac().equals(srcMac) ||
329 !match.dstMac().equals(dstMac)) {
330 same = false;
331 }
332 if (!flow.dataPath().srcPort().equals(srcPort) ||
333 !flow.dataPath().dstPort().equals(dstPort)) {
334 same = false;
335 }
336
337 if (same) {
338 log.debug("found flow entry that's the same {}-{}:::{}-{}",
339 new Object[] {srcPort, srcMac, dstPort, dstMac});
340 return true;
341 }
342 }
343
344 return false;
345 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800346 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800347
Jonathan Hart7e6df362013-12-10 23:33:59 -0800348 private OFPacketOut constructPacketOut(OFPacketIn pi, IOFSwitch sw) {
Jonathan Hart41d1e912013-11-24 16:50:25 -0800349 OFPacketOut po = new OFPacketOut();
350 po.setInPort(OFPort.OFPP_NONE)
351 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800352 .setActions(new ArrayList<OFAction>())
353 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800354
355 if (sw.getBuffers() == 0) {
356 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
357 .setPacketData(pi.getPacketData())
358 .setLengthU(po.getLengthU() + po.getPacketData().length);
359 }
360 else {
361 po.setBufferId(pi.getBufferId());
362 }
363
Jonathan Hart5e448782013-12-10 12:36:35 -0800364 return po;
365 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800366
Jonathan Hart5e448782013-12-10 12:36:35 -0800367 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800368 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
369 for (FlowPath flowPath : installedFlowPaths) {
370 flowInstalled(flowPath);
371 }
372 }
373
374 private void flowInstalled(FlowPath installedFlowPath) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800375 // TODO check concurrency
376 // will need to sync and access both collections at once.
377 long flowId = installedFlowPath.flowId().value();
Jonathan Hart5e448782013-12-10 12:36:35 -0800378
Jonathan Hart7e6df362013-12-10 23:33:59 -0800379 Collection<PacketToPush> packets;
380 synchronized (lock) {
381 packets = waitingPackets.removeAll(flowId);
382
383 //remove pending flows entry
384 Path pathToRemove = new Path(installedFlowPath.dataPath().srcPort(),
385 installedFlowPath.dataPath().dstPort());
386 pendingFlows.remove(pathToRemove);
387
388 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800389
390 for (PacketToPush packet : packets) {
391 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
392
393 OFPacketOut po = packet.packet;
394 short outPort =
395 installedFlowPath.flowEntries().get(0).outPort().value();
396 po.getActions().add(new OFActionOutput(outPort));
397 po.setActionsLength((short)
398 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
399 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
400
Jonathan Hart7e6df362013-12-10 23:33:59 -0800401 flowPusher.add(sw, po);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800402 }
403 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800404}