Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.forwarding; |
| 2 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 6 | import java.util.Iterator; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 7 | import java.util.List; |
| 8 | import java.util.Map; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 9 | |
| 10 | import net.floodlightcontroller.core.FloodlightContext; |
| 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFMessageListener; |
| 13 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.packet.Ethernet; |
| 18 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 19 | import net.onrc.onos.datagrid.IDatagridService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 20 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 21 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 22 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 23 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 24 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.flowmanager.IFlowService; |
| 26 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
| 27 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 28 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 29 | import net.onrc.onos.ofcontroller.util.Dpid; |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.util.FlowEntryMatch; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 32 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 33 | import net.onrc.onos.ofcontroller.util.FlowPathType; |
| 34 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
| 35 | import net.onrc.onos.ofcontroller.util.Port; |
| 36 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
| 37 | |
| 38 | import org.openflow.protocol.OFMessage; |
| 39 | import org.openflow.protocol.OFPacketIn; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 40 | import org.openflow.protocol.OFPacketOut; |
| 41 | import org.openflow.protocol.OFPort; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 42 | import org.openflow.protocol.OFType; |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 43 | import org.openflow.protocol.action.OFAction; |
| 44 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 45 | import org.openflow.util.HexString; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 49 | public class Forwarding implements IOFMessageListener, IFloodlightModule { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 50 | private final static Logger log = LoggerFactory.getLogger(Forwarding.class); |
| 51 | |
| 52 | private IFloodlightProviderService floodlightProvider; |
| 53 | private IFlowService flowService; |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 54 | private IDatagridService datagridService; |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 55 | |
| 56 | private IDeviceStorage deviceStorage; |
| 57 | private TopologyManager topologyService; |
| 58 | |
| 59 | public Forwarding() { |
| 60 | |
| 61 | } |
| 62 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 63 | @Override |
| 64 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 70 | return null; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 75 | List<Class<? extends IFloodlightService>> dependencies = |
| 76 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 77 | dependencies.add(IFloodlightProviderService.class); |
| 78 | dependencies.add(IFlowService.class); |
| 79 | dependencies.add(IDatagridService.class); |
| 80 | return dependencies; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void init(FloodlightModuleContext context) { |
| 85 | this.floodlightProvider = |
| 86 | context.getServiceImpl(IFloodlightProviderService.class); |
| 87 | this.flowService = context.getServiceImpl(IFlowService.class); |
| 88 | this.datagridService = context.getServiceImpl(IDatagridService.class); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 89 | |
| 90 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 91 | |
| 92 | deviceStorage = new DeviceStorageImpl(); |
| 93 | deviceStorage.init(""); |
| 94 | topologyService = new TopologyManager(); |
| 95 | topologyService.init(""); |
| 96 | } |
| 97 | |
Jonathan Hart | e93aed4 | 2013-12-05 18:39:50 -0800 | [diff] [blame] | 98 | @Override |
| 99 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 100 | // no-op |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String getName() { |
| 105 | return "onosforwarding"; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 110 | return (type == OFType.PACKET_IN) && |
| 111 | (name.equals("devicemanager") || name.equals("proxyarpmanager")); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Command receive( |
| 121 | IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 122 | |
| 123 | if (msg.getType() != OFType.PACKET_IN) { |
| 124 | return Command.CONTINUE; |
| 125 | } |
| 126 | |
| 127 | OFPacketIn pi = (OFPacketIn) msg; |
| 128 | |
| 129 | Ethernet eth = IFloodlightProviderService.bcStore. |
| 130 | get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 131 | |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 132 | // We only want to handle unicast IPv4 |
| 133 | if (eth.isBroadcast() || eth.isMulticast() || |
| 134 | eth.getEtherType() != Ethernet.TYPE_IPv4) { |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 135 | return Command.CONTINUE; |
| 136 | } |
| 137 | |
| 138 | handlePacketIn(sw, pi, eth); |
| 139 | |
| 140 | return Command.STOP; |
| 141 | } |
| 142 | |
| 143 | private void handlePacketIn(IOFSwitch sw, OFPacketIn pi, Ethernet eth) { |
| 144 | String destinationMac = HexString.toHexString(eth.getDestinationMACAddress()); |
| 145 | |
| 146 | IDeviceObject deviceObject = deviceStorage.getDeviceByMac( |
| 147 | destinationMac); |
| 148 | |
| 149 | if (deviceObject == null) { |
| 150 | log.debug("No device entry found for {}", destinationMac); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | Iterator<IPortObject> ports = deviceObject.getAttachedPorts().iterator(); |
| 155 | if (!ports.hasNext()) { |
| 156 | log.debug("No attachment point found for device {}", destinationMac); |
| 157 | return; |
| 158 | } |
| 159 | IPortObject portObject = ports.next(); |
| 160 | short destinationPort = portObject.getNumber(); |
| 161 | ISwitchObject switchObject = portObject.getSwitch(); |
| 162 | long destinationDpid = HexString.toLong(switchObject.getDPID()); |
| 163 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 164 | // TODO SwitchPort, Dpid and Port should probably be immutable |
| 165 | // (also, are Dpid and Port are even necessary?) |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 166 | SwitchPort srcSwitchPort = new SwitchPort( |
| 167 | new Dpid(sw.getId()), new Port(pi.getInPort())); |
| 168 | SwitchPort dstSwitchPort = new SwitchPort( |
| 169 | new Dpid(destinationDpid), new Port(destinationPort)); |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 170 | |
| 171 | MACAddress srcMacAddress = MACAddress.valueOf(eth.getSourceMACAddress()); |
| 172 | MACAddress dstMacAddress = MACAddress.valueOf(eth.getDestinationMACAddress()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 173 | |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 174 | if (flowExists(srcSwitchPort, srcMacAddress, |
| 175 | dstSwitchPort, dstMacAddress)) { |
| 176 | log.debug("Not adding flow because it already exists"); |
| 177 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 178 | // TODO check reverse flow as well |
| 179 | |
| 180 | DataPath shortestPath = |
| 181 | topologyService.getDatabaseShortestPath(srcSwitchPort, dstSwitchPort); |
| 182 | |
| 183 | if (shortestPath == null || shortestPath.flowEntries().isEmpty()) { |
| 184 | log.warn("No path found between {} and {} - not handling packet", |
| 185 | srcSwitchPort, dstSwitchPort); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | Port outPort = shortestPath.flowEntries().get(0).outPort(); |
| 190 | forwardPacket(pi, sw, outPort.value()); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Calculate a shortest path before pushing flow mods. |
| 195 | // This will be used later by the packet-out processing, but it uses |
| 196 | // the database so will be slow, and we should do it before flow mods. |
| 197 | DataPath shortestPath = |
| 198 | topologyService.getDatabaseShortestPath(srcSwitchPort, dstSwitchPort); |
| 199 | |
| 200 | if (shortestPath == null || shortestPath.flowEntries().isEmpty()) { |
| 201 | log.warn("No path found between {} and {} - not handling packet", |
| 202 | srcSwitchPort, dstSwitchPort); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 206 | log.debug("Adding new flow between {} at {} and {} at {}", |
| 207 | new Object[]{srcMacAddress, srcSwitchPort, dstMacAddress, dstSwitchPort}); |
| 208 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 209 | |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 210 | DataPath dataPath = new DataPath(); |
| 211 | dataPath.setSrcPort(srcSwitchPort); |
| 212 | dataPath.setDstPort(dstSwitchPort); |
| 213 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 214 | CallerId callerId = new CallerId("Forwarding"); |
| 215 | |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 216 | //FlowId flowId = new FlowId(flowService.getNextFlowEntryId()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 217 | FlowPath flowPath = new FlowPath(); |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 218 | //flowPath.setFlowId(flowId); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 219 | flowPath.setInstallerId(callerId); |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 220 | |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 221 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 222 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 223 | flowPath.setFlowEntryMatch(new FlowEntryMatch()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 224 | flowPath.flowEntryMatch().enableSrcMac(srcMacAddress); |
| 225 | flowPath.flowEntryMatch().enableDstMac(dstMacAddress); |
| 226 | // For now just forward IPv4 packets. This prevents accidentally |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 227 | // forwarding other stuff like ARP. |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 228 | flowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4); |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 229 | flowPath.setDataPath(dataPath); |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 230 | |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 231 | FlowId flowId = flowService.addFlow(flowPath); |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 232 | //flowService.addFlow(flowPath, flowId); |
Jonathan Hart | 4fb16d8 | 2013-11-07 22:41:32 -0800 | [diff] [blame] | 233 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 234 | |
| 235 | DataPath reverseDataPath = new DataPath(); |
| 236 | // Reverse the ports for the reverse path |
| 237 | reverseDataPath.setSrcPort(dstSwitchPort); |
| 238 | reverseDataPath.setDstPort(srcSwitchPort); |
| 239 | |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 240 | //FlowId reverseFlowId = new FlowId(flowService.getNextFlowEntryId()); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 241 | // TODO implement copy constructor for FlowPath |
| 242 | FlowPath reverseFlowPath = new FlowPath(); |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 243 | //reverseFlowPath.setFlowId(reverseFlowId); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 244 | reverseFlowPath.setInstallerId(callerId); |
| 245 | reverseFlowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 246 | reverseFlowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
| 247 | reverseFlowPath.setFlowEntryMatch(new FlowEntryMatch()); |
| 248 | // Reverse the MAC addresses for the reverse path |
| 249 | reverseFlowPath.flowEntryMatch().enableSrcMac(dstMacAddress); |
| 250 | reverseFlowPath.flowEntryMatch().enableDstMac(srcMacAddress); |
| 251 | reverseFlowPath.flowEntryMatch().enableEthernetFrameType(Ethernet.TYPE_IPv4); |
| 252 | reverseFlowPath.setDataPath(reverseDataPath); |
| 253 | reverseFlowPath.dataPath().srcPort().dpid().toString(); |
| 254 | |
| 255 | // TODO what happens if no path exists? |
Jonathan Hart | 48c2d31 | 2013-12-05 19:09:59 -0800 | [diff] [blame] | 256 | //flowService.addFlow(reverseFlowPath, reverseFlowId); |
| 257 | FlowId reverseFlowId = flowService.addFlow(reverseFlowPath); |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 258 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 259 | Port outPort = shortestPath.flowEntries().get(0).outPort(); |
| 260 | forwardPacket(pi, sw, outPort.value()); |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 261 | } |
Jonathan Hart | dc3ad70 | 2013-11-14 11:34:59 -0800 | [diff] [blame] | 262 | |
| 263 | private boolean flowExists(SwitchPort srcPort, MACAddress srcMac, |
| 264 | SwitchPort dstPort, MACAddress dstMac) { |
| 265 | for (FlowPath flow : datagridService.getAllFlows()) { |
| 266 | FlowEntryMatch match = flow.flowEntryMatch(); |
| 267 | // TODO implement FlowEntryMatch.equals(); |
| 268 | // This is painful to do properly without support in the FlowEntryMatch |
| 269 | boolean same = true; |
| 270 | if (!match.srcMac().equals(srcMac) || |
| 271 | !match.dstMac().equals(dstMac)) { |
| 272 | same = false; |
| 273 | } |
| 274 | if (!flow.dataPath().srcPort().equals(srcPort) || |
| 275 | !flow.dataPath().dstPort().equals(dstPort)) { |
| 276 | same = false; |
| 277 | } |
| 278 | |
| 279 | if (same) { |
| 280 | log.debug("found flow entry that's the same {}-{}:::{}-{}", |
| 281 | new Object[] {srcPort, srcMac, dstPort, dstMac}); |
| 282 | return true; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return false; |
| 287 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 288 | |
Jonathan Hart | 41d1e91 | 2013-11-24 16:50:25 -0800 | [diff] [blame] | 289 | private void forwardPacket(OFPacketIn pi, IOFSwitch sw, short port) { |
| 290 | List<OFAction> actions = new ArrayList<OFAction>(1); |
| 291 | actions.add(new OFActionOutput(port)); |
| 292 | |
| 293 | OFPacketOut po = new OFPacketOut(); |
| 294 | po.setInPort(OFPort.OFPP_NONE) |
| 295 | .setInPort(pi.getInPort()) |
| 296 | .setActions(actions) |
| 297 | .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH) |
| 298 | .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 299 | |
| 300 | if (sw.getBuffers() == 0) { |
| 301 | po.setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 302 | .setPacketData(pi.getPacketData()) |
| 303 | .setLengthU(po.getLengthU() + po.getPacketData().length); |
| 304 | } |
| 305 | else { |
| 306 | po.setBufferId(pi.getBufferId()); |
| 307 | } |
| 308 | |
| 309 | try { |
| 310 | sw.write(po, null); |
| 311 | sw.flush(); |
| 312 | } catch (IOException e) { |
| 313 | log.error("Error writing packet out to switch: {}", e); |
| 314 | } |
| 315 | } |
Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 316 | } |