blob: 3725d5e44dbde9cef81b8adc1653d900494090ec [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.io.IOException;
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;
20import net.floodlightcontroller.util.MACAddress;
Jonathan Hartdc3ad702013-11-14 11:34:59 -080021import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart1caaa932013-11-04 15:28:28 -080022import net.onrc.onos.ofcontroller.core.IDeviceStorage;
23import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
24import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
25import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
26import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart1caaa932013-11-04 15:28:28 -080027import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
28import net.onrc.onos.ofcontroller.topology.TopologyManager;
29import net.onrc.onos.ofcontroller.util.CallerId;
30import net.onrc.onos.ofcontroller.util.DataPath;
31import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart4fb16d82013-11-07 22:41:32 -080032import net.onrc.onos.ofcontroller.util.FlowEntryMatch;
Jonathan Hart1caaa932013-11-04 15:28:28 -080033import net.onrc.onos.ofcontroller.util.FlowId;
34import net.onrc.onos.ofcontroller.util.FlowPath;
35import net.onrc.onos.ofcontroller.util.FlowPathType;
36import net.onrc.onos.ofcontroller.util.FlowPathUserState;
37import net.onrc.onos.ofcontroller.util.Port;
38import net.onrc.onos.ofcontroller.util.SwitchPort;
39
40import org.openflow.protocol.OFMessage;
41import org.openflow.protocol.OFPacketIn;
Jonathan Hart41d1e912013-11-24 16:50:25 -080042import org.openflow.protocol.OFPacketOut;
43import org.openflow.protocol.OFPort;
Jonathan Hart1caaa932013-11-04 15:28:28 -080044import org.openflow.protocol.OFType;
Jonathan Hart41d1e912013-11-24 16:50:25 -080045import org.openflow.protocol.action.OFAction;
46import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart1caaa932013-11-04 15:28:28 -080047import org.openflow.util.HexString;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Jonathan Hart5e448782013-12-10 12:36:35 -080051import com.google.common.collect.HashMultimap;
52import com.google.common.collect.Multimap;
53import com.google.common.collect.Multimaps;
54
55public class Forwarding implements IOFMessageListener, IFloodlightModule,
56 IForwardingService {
Jonathan Hart1caaa932013-11-04 15:28:28 -080057 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
58
59 private IFloodlightProviderService floodlightProvider;
60 private IFlowService flowService;
Naoki Shiotae2f4da72013-12-09 16:34:17 -080061 @SuppressWarnings("unused")
Jonathan Hartdc3ad702013-11-14 11:34:59 -080062 private IDatagridService datagridService;
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
70 public class PacketToPush {
71 public final OFPacketOut packet;
72 public final long dpid;
Jonathan Hart1caaa932013-11-04 15:28:28 -080073
Jonathan Hart5e448782013-12-10 12:36:35 -080074 public PacketToPush(OFPacketOut packet, long dpid) {
75 this.packet = packet;
76 this.dpid = dpid;
77 }
78 }
79
80 public final class Path {
81 public final SwitchPort srcPort;
82 public final SwitchPort dstPort;
83
84 public Path(SwitchPort src, SwitchPort dst) {
85 srcPort = new SwitchPort(new Dpid(src.dpid().value()),
86 new Port(src.port().value()));
87 dstPort = new SwitchPort(new Dpid(dst.dpid().value()),
88 new Port(dst.port().value()));
89 }
90
91 @Override
92 public boolean equals(Object other) {
93 if (!(other instanceof Path)) {
94 return false;
95 }
96
97 Path otherPath = (Path) other;
98 return srcPort.equals(otherPath.srcPort) &&
99 dstPort.equals(otherPath.dstPort);
100 }
101
102 @Override
103 public int hashCode() {
104 int hash = 17;
105 hash = 31 * hash + srcPort.hashCode();
106 hash = 31 * hash + dstPort.hashCode();
107 return hash;
108 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800109 }
110
Jonathan Harte93aed42013-12-05 18:39:50 -0800111 @Override
112 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800113 List<Class<? extends IFloodlightService>> services =
114 new ArrayList<Class<? extends IFloodlightService>>(1);
115 services.add(IForwardingService.class);
116 return services;
Jonathan Harte93aed42013-12-05 18:39:50 -0800117 }
118
119 @Override
120 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Jonathan Hartd33a6cf2013-12-10 14:29:08 -0800121 Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
122 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(1);
123 impls.put(IForwardingService.class, this);
124 return impls;
Jonathan Harte93aed42013-12-05 18:39:50 -0800125 }
126
127 @Override
128 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
129 List<Class<? extends IFloodlightService>> dependencies =
130 new ArrayList<Class<? extends IFloodlightService>>();
131 dependencies.add(IFloodlightProviderService.class);
132 dependencies.add(IFlowService.class);
133 dependencies.add(IDatagridService.class);
134 return dependencies;
135 }
136
137 @Override
138 public void init(FloodlightModuleContext context) {
139 this.floodlightProvider =
140 context.getServiceImpl(IFloodlightProviderService.class);
141 this.flowService = context.getServiceImpl(IFlowService.class);
142 this.datagridService = context.getServiceImpl(IDatagridService.class);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800143
144 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
145
Jonathan Hart5e448782013-12-10 12:36:35 -0800146 pendingFlows = new ConcurrentHashMap<Path, Long>();
147 waitingPackets = Multimaps.synchronizedSetMultimap(
148 HashMultimap.<Long, PacketToPush>create());
149
Jonathan Hart1caaa932013-11-04 15:28:28 -0800150 deviceStorage = new DeviceStorageImpl();
151 deviceStorage.init("");
152 topologyService = new TopologyManager();
153 topologyService.init("");
154 }
155
Jonathan Harte93aed42013-12-05 18:39:50 -0800156 @Override
157 public void startUp(FloodlightModuleContext context) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800158 // no-op
159 }
160
161 @Override
162 public String getName() {
163 return "onosforwarding";
164 }
165
166 @Override
167 public boolean isCallbackOrderingPrereq(OFType type, String name) {
168 return (type == OFType.PACKET_IN) &&
169 (name.equals("devicemanager") || name.equals("proxyarpmanager"));
170 }
171
172 @Override
173 public boolean isCallbackOrderingPostreq(OFType type, String name) {
174 return false;
175 }
176
177 @Override
178 public Command receive(
179 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
180
181 if (msg.getType() != OFType.PACKET_IN) {
182 return Command.CONTINUE;
183 }
184
185 OFPacketIn pi = (OFPacketIn) msg;
186
187 Ethernet eth = IFloodlightProviderService.bcStore.
188 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
189
Jonathan Hart4fb16d82013-11-07 22:41:32 -0800190 // We only want to handle unicast IPv4
191 if (eth.isBroadcast() || eth.isMulticast() ||
192 eth.getEtherType() != Ethernet.TYPE_IPv4) {
Jonathan Hart1caaa932013-11-04 15:28:28 -0800193 return Command.CONTINUE;
194 }
195
196 handlePacketIn(sw, pi, eth);
197
198 return Command.STOP;
199 }
200
201 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800202 String destinationMac =
203 HexString.toHexString(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800204
205 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
206 destinationMac);
207
208 if (deviceObject == null) {
209 log.debug("No device entry found for {}", destinationMac);
210 return;
211 }
212
Jonathan Hart5e448782013-12-10 12:36:35 -0800213 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
Jonathan Hart1caaa932013-11-04 15:28:28 -0800214 if (!ports.hasNext()) {
215 log.debug("No attachment point found for device {}", destinationMac);
216 return;
217 }
218 IPortObject portObject = ports.next();
219 short destinationPort = portObject.getNumber();
220 ISwitchObject switchObject = portObject.getSwitch();
221 long destinationDpid = HexString.toLong(switchObject.getDPID());
222
Jonathan Hart41d1e912013-11-24 16:50:25 -0800223 // TODO SwitchPort, Dpid and Port should probably be immutable
224 // (also, are Dpid and Port are even necessary?)
Jonathan Hart1caaa932013-11-04 15:28:28 -0800225 SwitchPort srcSwitchPort = new SwitchPort(
226 new Dpid(sw.getId()), new Port(pi.getInPort()));
227 SwitchPort dstSwitchPort = new SwitchPort(
228 new Dpid(destinationDpid), new Port(destinationPort));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800229
230 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
231 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800232
Jonathan Hart5e448782013-12-10 12:36:35 -0800233
234 DataPath datapath = new DataPath();
235 datapath.setSrcPort(srcSwitchPort);
236 datapath.setDstPort(dstSwitchPort);
237
238
239
240 Path pathspec = new Path(srcSwitchPort, dstSwitchPort);
241 // TODO check concurrency
242 Long existingFlowId = pendingFlows.get(pathspec);
243
244 if (existingFlowId != null) {
245 log.debug("Found existing flow {}",
246 HexString.toHexString(existingFlowId));
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800247
Jonathan Hart5e448782013-12-10 12:36:35 -0800248 // TODO do stuff.
249 OFPacketOut po = constructPacketOut(datapath, pi, sw);
250 waitingPackets.put(existingFlowId, new PacketToPush(po, sw.getId()));
Jonathan Hart41d1e912013-11-24 16:50:25 -0800251 return;
252 }
253
Jonathan Hart1caaa932013-11-04 15:28:28 -0800254
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800255 log.debug("Adding new flow between {} at {} and {} at {}",
256 new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort});
257
Jonathan Hart1caaa932013-11-04 15:28:28 -0800258
Jonathan Hart41d1e912013-11-24 16:50:25 -0800259 CallerId callerId = new CallerId("Forwarding");
260
Jonathan Hart48c2d312013-12-05 19:09:59 -0800261 //FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800262 FlowPath flowPath = new FlowPath();
Jonathan Hart48c2d312013-12-05 19:09:59 -0800263 //flowPath.setFlowId(flowId);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800264 flowPath.setInstallerId(callerId);
Jonathan Hart48c2d312013-12-05 19:09:59 -0800265
Jonathan Hart1caaa932013-11-04 15:28:28 -0800266 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
267 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
Jonathan Hart4fb16d82013-11-07 22:41:32 -0800268 flowPath.setFlowEntryMatch(new FlowEntryMatch());
Jonathan Hart1caaa932013-11-04 15:28:28 -0800269 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
270 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
271 // For now just forward IPv4 packets. This prevents accidentally
Jonathan Hart41d1e912013-11-24 16:50:25 -0800272 // forwarding other stuff like ARP.
Jonathan Hart1caaa932013-11-04 15:28:28 -0800273 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
Jonathan Hart5e448782013-12-10 12:36:35 -0800274 flowPath.setDataPath(datapath);
Jonathan Hart48c2d312013-12-05 19:09:59 -0800275
Jonathan Hart41d1e912013-11-24 16:50:25 -0800276 DataPath reverseDataPath = new DataPath();
277 // Reverse the ports for the reverse path
278 reverseDataPath.setSrcPort(dstSwitchPort);
279 reverseDataPath.setDstPort(srcSwitchPort);
280
Jonathan Hart41d1e912013-11-24 16:50:25 -0800281 // TODO implement copy constructor for FlowPath
282 FlowPath reverseFlowPath = new FlowPath();
Jonathan Hart48c2d312013-12-05 19:09:59 -0800283 //reverseFlowPath.setFlowId(reverseFlowId);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800284 reverseFlowPath.setInstallerId(callerId);
285 reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
286 reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
287 reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch());
288 // Reverse the MAC addresses for the reverse path
289 reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress);
290 reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress);
291 reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
292 reverseFlowPath.setDataPath(reverseDataPath);
293 reverseFlowPath.dataPath().srcPort().dpid().toString();
294
295 // TODO what happens if no path exists?
Jonathan Hart5e448782013-12-10 12:36:35 -0800296 FlowId flowId = new FlowId(flowService.getNextFlowEntryId());
297 FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId());
298
299 flowPath.setFlowId(flowId);
300 reverseFlowPath.setFlowId(reverseFlowId);
301
302 OFPacketOut po = constructPacketOut(datapath, pi, sw);
303 Path reversePathSpec = new Path(dstSwitchPort, srcSwitchPort);
304
305 // Add to waiting lists
306 pendingFlows.put(pathspec, flowId.value());
307 pendingFlows.put(reversePathSpec, reverseFlowId.value());
308 waitingPackets.put(flowId.value(), new PacketToPush(po, sw.getId()));
309
Jonathan Hart5e448782013-12-10 12:36:35 -0800310 flowService.addFlow(reverseFlowPath);
311 flowService.addFlow(flowPath);
Jonathan Hart1caaa932013-11-04 15:28:28 -0800312 }
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800313
Jonathan Hart5e448782013-12-10 12:36:35 -0800314 /*
Jonathan Hartdc3ad702013-11-14 11:34:59 -0800315 private boolean flowExists(SwitchPort srcPort, MACAddress srcMac,
316 SwitchPort dstPort, MACAddress dstMac) {
317 for (FlowPath flow : datagridService.getAllFlows()) {
318 FlowEntryMatch match = flow.flowEntryMatch();
319 // TODO implement FlowEntryMatch.equals();
320 // This is painful to do properly without support in the FlowEntryMatch
321 boolean same = true;
322 if (!match.srcMac().equals(srcMac) ||
323 !match.dstMac().equals(dstMac)) {
324 same = false;
325 }
326 if (!flow.dataPath().srcPort().equals(srcPort) ||
327 !flow.dataPath().dstPort().equals(dstPort)) {
328 same = false;
329 }
330
331 if (same) {
332 log.debug("found flow entry that's the same {}-{}:::{}-{}",
333 new Object[] {srcPort, srcMac, dstPort, dstMac});
334 return true;
335 }
336 }
337
338 return false;
339 }
Jonathan Hart5e448782013-12-10 12:36:35 -0800340 */
Jonathan Hart1caaa932013-11-04 15:28:28 -0800341
Jonathan Hart5e448782013-12-10 12:36:35 -0800342 private OFPacketOut constructPacketOut(DataPath datapath, OFPacketIn pi,
343 IOFSwitch sw) {
344 //List<OFAction> actions = new ArrayList<OFAction>(1);
345 //actions.add(new OFActionOutput(port));
Jonathan Hart41d1e912013-11-24 16:50:25 -0800346
347 OFPacketOut po = new OFPacketOut();
348 po.setInPort(OFPort.OFPP_NONE)
349 .setInPort(pi.getInPort())
Jonathan Hart5e448782013-12-10 12:36:35 -0800350 .setActions(new ArrayList<OFAction>())
351 .setLengthU(OFPacketOut.MINIMUM_LENGTH);
Jonathan Hart41d1e912013-11-24 16:50:25 -0800352
353 if (sw.getBuffers() == 0) {
354 po.setBufferId(OFPacketOut.BUFFER_ID_NONE)
355 .setPacketData(pi.getPacketData())
356 .setLengthU(po.getLengthU() + po.getPacketData().length);
357 }
358 else {
359 po.setBufferId(pi.getBufferId());
360 }
361
Jonathan Hart5e448782013-12-10 12:36:35 -0800362 return po;
363 }
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800364
Jonathan Hart5e448782013-12-10 12:36:35 -0800365 @Override
Pavlin Radoslavov7208e9a2013-12-11 14:31:07 -0800366 public void flowsInstalled(Collection<FlowPath> installedFlowPaths) {
367 for (FlowPath flowPath : installedFlowPaths) {
368 flowInstalled(flowPath);
369 }
370 }
371
372 private void flowInstalled(FlowPath installedFlowPath) {
Jonathan Hart5e448782013-12-10 12:36:35 -0800373 // TODO check concurrency
374 // will need to sync and access both collections at once.
375 long flowId = installedFlowPath.flowId().value();
376 Collection<PacketToPush> 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 for (PacketToPush packet : packets) {
384 IOFSwitch sw = floodlightProvider.getSwitches().get(packet.dpid);
385
386 OFPacketOut po = packet.packet;
387 short outPort =
388 installedFlowPath.flowEntries().get(0).outPort().value();
389 po.getActions().add(new OFActionOutput(outPort));
390 po.setActionsLength((short)
391 (po.getActionsLength() + OFActionOutput.MINIMUM_LENGTH));
392 po.setLengthU(po.getLengthU() + OFActionOutput.MINIMUM_LENGTH);
393
394 try {
395 sw.write(packet.packet, null);
396 sw.flush();
397 } catch (IOException e) {
398 log.error("Error writing packet out to switch {}:",
399 sw.getId(), e);
400 }
Jonathan Hart41d1e912013-11-24 16:50:25 -0800401 }
402 }
Jonathan Hart1caaa932013-11-04 15:28:28 -0800403}