tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.ifwd; |
| 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; |
| 8 | import org.onlab.onos.ApplicationId; |
| 9 | import org.onlab.onos.net.Host; |
| 10 | import org.onlab.onos.net.HostId; |
| 11 | import org.onlab.onos.net.PortNumber; |
| 12 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 13 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 14 | import org.onlab.onos.net.flow.TrafficSelector; |
| 15 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 16 | import org.onlab.onos.net.host.HostService; |
| 17 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 18 | import org.onlab.onos.net.intent.IntentId; |
| 19 | import org.onlab.onos.net.intent.IntentService; |
| 20 | import org.onlab.onos.net.packet.InboundPacket; |
| 21 | import org.onlab.onos.net.packet.PacketContext; |
| 22 | import org.onlab.onos.net.packet.PacketProcessor; |
| 23 | import org.onlab.onos.net.packet.PacketService; |
| 24 | import org.onlab.onos.net.topology.TopologyService; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | /** |
| 31 | * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework. |
| 32 | */ |
| 33 | @Component(immediate = true) |
| 34 | public class IntentReactiveForwarding { |
| 35 | |
| 36 | private static final int TIMEOUT = 10; |
| 37 | private static final int PRIORITY = 10; |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected TopologyService topologyService; |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected PacketService packetService; |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected IntentService intentService; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected HostService hostService; |
| 52 | |
| 53 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
| 54 | |
| 55 | private ApplicationId appId; |
| 56 | private static long intentId = 1; |
| 57 | |
| 58 | @Activate |
| 59 | public void activate() { |
| 60 | appId = ApplicationId.getAppId(); |
| 61 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
| 62 | log.info("Started with Application ID {}", appId.id()); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | packetService.removeProcessor(processor); |
| 68 | processor = null; |
| 69 | log.info("Stopped"); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Packet processor responsible for forwarding packets along their paths. |
| 75 | */ |
| 76 | private class ReactivePacketProcessor implements PacketProcessor { |
| 77 | |
| 78 | @Override |
| 79 | public void process(PacketContext context) { |
| 80 | // Stop processing if the packet has been handled, since we |
| 81 | // can't do any more to it. |
| 82 | if (context.isHandled()) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | InboundPacket pkt = context.inPacket(); |
| 87 | Ethernet ethPkt = pkt.parsed(); |
| 88 | |
| 89 | HostId srcId = HostId.hostId(ethPkt.getSourceMAC()); |
| 90 | HostId dstId = HostId.hostId(ethPkt.getDestinationMAC()); |
| 91 | |
| 92 | // Do we know who this is for? If not, flood and bail. |
| 93 | Host dst = hostService.getHost(dstId); |
| 94 | if (dst == null) { |
| 95 | flood(context); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Otherwise forward and be done with it. |
| 100 | setUpConnectivity(context, srcId, dstId); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Floods the specified packet if permissible. |
| 105 | private void flood(PacketContext context) { |
| 106 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| 107 | context.inPacket().receivedFrom())) { |
| 108 | packetOut(context, PortNumber.FLOOD); |
| 109 | } else { |
| 110 | context.block(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Sends a packet out the specified port. |
| 115 | private void packetOut(PacketContext context, PortNumber portNumber) { |
| 116 | context.treatmentBuilder().setOutput(portNumber); |
| 117 | context.send(); |
| 118 | } |
| 119 | |
| 120 | // Install a rule forwarding the packet to the specified port. |
| 121 | private void setUpConnectivity(PacketContext context, HostId srcId, HostId dstId) { |
| 122 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 123 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 124 | |
| 125 | HostToHostIntent intent = |
| 126 | new HostToHostIntent(new IntentId(intentId++), srcId, dstId, |
| 127 | selector, treatment); |
| 128 | |
| 129 | intentService.submit(intent); |
| 130 | } |
| 131 | |
| 132 | } |
| 133 | |
| 134 | |