blob: d6bac5c72596b1939bb46ce088ce242044cc01d6 [file] [log] [blame]
Jonathan Hart1caaa932013-11-04 15:28:28 -08001package net.onrc.onos.ofcontroller.forwarding;
2
3import java.util.Iterator;
4
5import net.floodlightcontroller.core.FloodlightContext;
6import net.floodlightcontroller.core.IFloodlightProviderService;
7import net.floodlightcontroller.core.IOFMessageListener;
8import net.floodlightcontroller.core.IOFSwitch;
9import net.floodlightcontroller.packet.Ethernet;
10import net.floodlightcontroller.util.MACAddress;
11import net.onrc.onos.ofcontroller.core.IDeviceStorage;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
13import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
14import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
15import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
16import net.onrc.onos.ofcontroller.flowmanager.FlowManager;
17import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
18import net.onrc.onos.ofcontroller.topology.TopologyManager;
19import net.onrc.onos.ofcontroller.util.CallerId;
20import net.onrc.onos.ofcontroller.util.DataPath;
21import net.onrc.onos.ofcontroller.util.Dpid;
22import net.onrc.onos.ofcontroller.util.FlowId;
23import net.onrc.onos.ofcontroller.util.FlowPath;
24import net.onrc.onos.ofcontroller.util.FlowPathType;
25import net.onrc.onos.ofcontroller.util.FlowPathUserState;
26import net.onrc.onos.ofcontroller.util.Port;
27import net.onrc.onos.ofcontroller.util.SwitchPort;
28
29import org.openflow.protocol.OFMessage;
30import org.openflow.protocol.OFPacketIn;
31import org.openflow.protocol.OFType;
32import org.openflow.util.HexString;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36public class Forwarding implements IOFMessageListener {
37 private final static Logger log = LoggerFactory.getLogger(Forwarding.class);
38
39 private IFloodlightProviderService floodlightProvider;
40 private IFlowService flowService;
41
42 private IDeviceStorage deviceStorage;
43 private TopologyManager topologyService;
44
45 public Forwarding() {
46
47 }
48
49 public void init(IFloodlightProviderService floodlightProvider,
50 IFlowService flowService) {
51 this.floodlightProvider = floodlightProvider;
52 this.flowService = flowService;
53
54 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
55
56 deviceStorage = new DeviceStorageImpl();
57 deviceStorage.init("");
58 topologyService = new TopologyManager();
59 topologyService.init("");
60 }
61
62 public void startUp() {
63 // no-op
64 }
65
66 @Override
67 public String getName() {
68 return "onosforwarding";
69 }
70
71 @Override
72 public boolean isCallbackOrderingPrereq(OFType type, String name) {
73 return (type == OFType.PACKET_IN) &&
74 (name.equals("devicemanager") || name.equals("proxyarpmanager"));
75 }
76
77 @Override
78 public boolean isCallbackOrderingPostreq(OFType type, String name) {
79 return false;
80 }
81
82 @Override
83 public Command receive(
84 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
85
86 if (msg.getType() != OFType.PACKET_IN) {
87 return Command.CONTINUE;
88 }
89
90 OFPacketIn pi = (OFPacketIn) msg;
91
92 Ethernet eth = IFloodlightProviderService.bcStore.
93 get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
94
95 // We don't want to handle broadcast traffic
96 if (eth.isBroadcast()) {
97 return Command.CONTINUE;
98 }
99
100 handlePacketIn(sw, pi, eth);
101
102 return Command.STOP;
103 }
104
105 private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) {
106 String destinationMac = HexString.toHexString(eth.getDestinationMACAddress());
107
108 IDeviceObject deviceObject = deviceStorage.getDeviceByMac(
109 destinationMac);
110
111 if (deviceObject == null) {
112 log.debug("No device entry found for {}", destinationMac);
113 return;
114 }
115
116 Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator();
117 if (!ports.hasNext()) {
118 log.debug("No attachment point found for device {}", destinationMac);
119 return;
120 }
121 IPortObject portObject = ports.next();
122 short destinationPort = portObject.getNumber();
123 ISwitchObject switchObject = portObject.getSwitch();
124 long destinationDpid = HexString.toLong(switchObject.getDPID());
125
126 SwitchPort srcSwitchPort = new SwitchPort(
127 new Dpid(sw.getId()), new Port(pi.getInPort()));
128 SwitchPort dstSwitchPort = new SwitchPort(
129 new Dpid(destinationDpid), new Port(destinationPort));
130 DataPath shortestPath =
131 topologyService.getDatabaseShortestPath(srcSwitchPort, dstSwitchPort);
132
133 if (shortestPath == null) {
134 log.debug("Shortest path not found between {} and {}",
135 srcSwitchPort, dstSwitchPort);
136 return;
137 }
138
139 MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress());
140 MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress());
141
142 FlowId flowId = new FlowId(1L); //dummy flow ID
143 FlowPath flowPath = new FlowPath();
144 flowPath.setFlowId(flowId);
145 flowPath.setInstallerId(new CallerId("Forwarding"));
146 flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH);
147 flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD);
148 flowPath.flowEntryMatch().enableSrcMac(srcMacAddress);
149 flowPath.flowEntryMatch().enableDstMac(dstMacAddress);
150 // For now just forward IPv4 packets. This prevents accidentally
151 // other stuff like ARP.
152 flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4);
153 flowService.addFlow(flowPath, flowId);
154 //flowService.addAndMaintainShortestPathFlow(shortestPath.)
155 }
156
157}