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