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