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