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