alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.fwd; |
| 2 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Set; |
| 6 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 12 | import org.onlab.onos.ApplicationId; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 13 | import org.onlab.onos.CoreService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.Host; |
| 15 | import org.onlab.onos.net.HostId; |
| 16 | import org.onlab.onos.net.Path; |
| 17 | import org.onlab.onos.net.PortNumber; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 19 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 20 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 21 | import org.onlab.onos.net.flow.FlowRule; |
| 22 | import org.onlab.onos.net.flow.FlowRuleService; |
| 23 | import org.onlab.onos.net.flow.TrafficSelector; |
| 24 | import org.onlab.onos.net.flow.TrafficTreatment; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.host.HostService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.packet.InboundPacket; |
| 27 | import org.onlab.onos.net.packet.PacketContext; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.packet.PacketProcessor; |
| 29 | import org.onlab.onos.net.packet.PacketService; |
| 30 | import org.onlab.onos.net.topology.TopologyService; |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 31 | import org.onlab.packet.Ethernet; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 33 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Sample reactive forwarding application. |
| 36 | */ |
| 37 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 38 | public class ReactiveForwarding { |
| 39 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 40 | private static final int TIMEOUT = 10; |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 41 | private static final int PRIORITY = 10; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 42 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected TopologyService topologyService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected PacketService packetService; |
| 50 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected HostService hostService; |
| 53 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected FlowRuleService flowRuleService; |
| 56 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected CoreService coreService; |
| 59 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 60 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 61 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 62 | private ApplicationId appId; |
| 63 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 64 | @Activate |
| 65 | public void activate() { |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 66 | appId = coreService.registerApplication("org.onlab.onos.fwd"); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 67 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 68 | log.info("Started with Application ID {}", appId.id()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 73 | flowRuleService.removeFlowRulesById(appId); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 74 | packetService.removeProcessor(processor); |
| 75 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 76 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 77 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | /** |
| 81 | * Packet processor responsible for forwarding packets along their paths. |
| 82 | */ |
| 83 | private class ReactivePacketProcessor implements PacketProcessor { |
| 84 | |
| 85 | @Override |
| 86 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 87 | // Stop processing if the packet has been handled, since we |
| 88 | // can't do any more to it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 89 | if (context.isHandled()) { |
| 90 | return; |
| 91 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 92 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 93 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 94 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 95 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 96 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 97 | |
| 98 | // Do we know who this is for? If not, flood and bail. |
| 99 | Host dst = hostService.getHost(id); |
| 100 | if (dst == null) { |
| 101 | flood(context); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // Are we on an edge switch that our destination is on? If so, |
| 106 | // simply forward out to the destination and bail. |
| 107 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 108 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 109 | installRule(context, dst.location().port()); |
| 110 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | |
| 114 | // Otherwise, get a set of paths that lead from here to the |
| 115 | // destination edge switch. |
| 116 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 117 | pkt.receivedFrom().deviceId(), |
| 118 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 119 | if (paths.isEmpty()) { |
| 120 | // If there are no paths, flood and bail. |
| 121 | flood(context); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Otherwise, pick a path that does not lead back to where we |
| 126 | // came from; if no such path, flood and bail. |
| 127 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 128 | if (path == null) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 129 | log.warn("Doh... don't know where to go... {} -> {} received on {}", |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 130 | ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 131 | pkt.receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 132 | flood(context); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 137 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | // Selects a path from the given set that does not lead back to the |
| 142 | // specified port. |
| 143 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 144 | for (Path path : paths) { |
| 145 | if (!path.src().port().equals(notToPort)) { |
| 146 | return path; |
| 147 | } |
| 148 | } |
| 149 | return null; |
| 150 | } |
| 151 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 152 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 153 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 154 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 155 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 156 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 157 | } else { |
| 158 | context.block(); |
| 159 | } |
| 160 | } |
| 161 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 162 | // Sends a packet out the specified port. |
| 163 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 164 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 165 | context.send(); |
| 166 | } |
| 167 | |
| 168 | // Install a rule forwarding the packet to the specified port. |
| 169 | private void installRule(PacketContext context, PortNumber portNumber) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 170 | // We don't yet support bufferids in the flowservice so packet out first. |
| 171 | packetOut(context, portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 172 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 173 | // Install the flow rule to handle this type of message from now on. |
| 174 | Ethernet inPkt = context.inPacket().parsed(); |
| 175 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 176 | builder.matchEthType(inPkt.getEtherType()) |
| 177 | .matchEthSrc(inPkt.getSourceMAC()) |
| 178 | .matchEthDst(inPkt.getDestinationMAC()) |
| 179 | .matchInport(context.inPacket().receivedFrom().port()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 180 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 181 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 182 | treat.setOutput(portNumber); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 183 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 184 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| 185 | builder.build(), treat.build(), PRIORITY, appId, TIMEOUT); |
| 186 | |
| 187 | flowRuleService.applyFlowRules(f); |
| 188 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 193 | |