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 | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame^] | 12 | import org.onlab.onos.net.flow.instructions.Instructions; |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.host.HostService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.packet.InboundPacket; |
| 15 | import org.onlab.onos.net.packet.PacketContext; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.packet.PacketProcessor; |
| 17 | import org.onlab.onos.net.packet.PacketService; |
| 18 | import org.onlab.onos.net.topology.TopologyService; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 20 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 21 | import java.util.Set; |
| 22 | |
| 23 | import static org.slf4j.LoggerFactory.getLogger; |
| 24 | |
| 25 | /** |
| 26 | * Sample reactive forwarding application. |
| 27 | */ |
| 28 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 29 | public class ReactiveForwarding { |
| 30 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 31 | private final Logger log = getLogger(getClass()); |
| 32 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 33 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 34 | protected TopologyService topologyService; |
| 35 | |
| 36 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 37 | protected PacketService packetService; |
| 38 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 39 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 40 | protected HostService hostService; |
| 41 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 42 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 43 | |
| 44 | @Activate |
| 45 | public void activate() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 46 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 47 | log.info("Started"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Deactivate |
| 51 | public void deactivate() { |
| 52 | packetService.removeProcessor(processor); |
| 53 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 54 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 55 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | /** |
| 59 | * Packet processor responsible for forwarding packets along their paths. |
| 60 | */ |
| 61 | private class ReactivePacketProcessor implements PacketProcessor { |
| 62 | |
| 63 | @Override |
| 64 | public void process(PacketContext context) { |
| 65 | InboundPacket pkt = context.inPacket(); |
| 66 | HostId id = HostId.hostId(pkt.parsed().getDestinationMAC()); |
| 67 | |
| 68 | // Do we know who this is for? If not, flood and bail. |
| 69 | Host dst = hostService.getHost(id); |
| 70 | if (dst == null) { |
| 71 | flood(context); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // Are we on an edge switch that our destination is on? If so, |
| 76 | // simply forward out to the destination and bail. |
| 77 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
| 78 | forward(context, dst.location().port()); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Otherwise, get a set of paths that lead from here to the |
| 83 | // destination edge switch. |
| 84 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
| 85 | context.inPacket().receivedFrom().deviceId(), |
| 86 | dst.location().deviceId()); |
| 87 | if (paths.isEmpty()) { |
| 88 | // If there are no paths, flood and bail. |
| 89 | flood(context); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Otherwise, pick a path that does not lead back to where we |
| 94 | // came from; if no such path, flood and bail. |
| 95 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 96 | if (path == null) { |
| 97 | log.warn("Doh... don't know where to go..."); |
| 98 | flood(context); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Otherwise forward and be done with it. |
| 103 | forward(context, path.src().port()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Selects a path from the given set that does not lead back to the |
| 108 | // specified port. |
| 109 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 110 | for (Path path : paths) { |
| 111 | if (!path.src().port().equals(notToPort)) { |
| 112 | return path; |
| 113 | } |
| 114 | } |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | // Floods the specified packet. |
| 119 | private void flood(PacketContext context) { |
| 120 | boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| 121 | context.inPacket().receivedFrom()); |
| 122 | if (canBcast) { |
| 123 | forward(context, PortNumber.FLOOD); |
| 124 | } else { |
| 125 | context.block(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Forwards the packet to the specified port. |
| 130 | private void forward(PacketContext context, PortNumber portNumber) { |
| 131 | context.treatmentBuilder().add(Instructions.createOutput(portNumber)); |
| 132 | context.send(); |
| 133 | } |
| 134 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 135 | } |
| 136 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 137 | |