Jonathan Hart | 1caaa93 | 2013-11-04 15:28:28 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.forwarding; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | |
| 5 | import net.floodlightcontroller.core.FloodlightContext; |
| 6 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 7 | import net.floodlightcontroller.core.IOFMessageListener; |
| 8 | import net.floodlightcontroller.core.IOFSwitch; |
| 9 | import net.floodlightcontroller.packet.Ethernet; |
| 10 | import net.floodlightcontroller.util.MACAddress; |
| 11 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 12 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 13 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 14 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 15 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
| 16 | import net.onrc.onos.ofcontroller.flowmanager.FlowManager; |
| 17 | import net.onrc.onos.ofcontroller.flowmanager.IFlowService; |
| 18 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
| 19 | import net.onrc.onos.ofcontroller.util.CallerId; |
| 20 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 21 | import net.onrc.onos.ofcontroller.util.Dpid; |
| 22 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 23 | import net.onrc.onos.ofcontroller.util.FlowPath; |
| 24 | import net.onrc.onos.ofcontroller.util.FlowPathType; |
| 25 | import net.onrc.onos.ofcontroller.util.FlowPathUserState; |
| 26 | import net.onrc.onos.ofcontroller.util.Port; |
| 27 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
| 28 | |
| 29 | import org.openflow.protocol.OFMessage; |
| 30 | import org.openflow.protocol.OFPacketIn; |
| 31 | import org.openflow.protocol.OFType; |
| 32 | import org.openflow.util.HexString; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | public 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 | } |