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; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 8 | import org.onlab.onos.ApplicationId; |
| 9 | import org.onlab.onos.CoreService; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.Host; |
| 11 | import org.onlab.onos.net.HostId; |
| 12 | import org.onlab.onos.net.PortNumber; |
| 13 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 14 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 15 | import org.onlab.onos.net.flow.TrafficSelector; |
| 16 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 17 | import org.onlab.onos.net.host.HostService; |
| 18 | import org.onlab.onos.net.intent.HostToHostIntent; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.intent.IntentService; |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.packet.DefaultOutboundPacket; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.packet.InboundPacket; |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.packet.OutboundPacket; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.packet.PacketContext; |
| 24 | import org.onlab.onos.net.packet.PacketProcessor; |
| 25 | import org.onlab.onos.net.packet.PacketService; |
| 26 | import org.onlab.onos.net.topology.TopologyService; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.slf4j.Logger; |
| 29 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 32 | /** |
| 33 | * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework. |
| 34 | */ |
| 35 | @Component(immediate = true) |
| 36 | public class IntentReactiveForwarding { |
| 37 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
| 40 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 41 | protected CoreService coreService; |
| 42 | |
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 44 | protected TopologyService topologyService; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected PacketService packetService; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected IntentService intentService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected HostService hostService; |
| 54 | |
| 55 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 56 | private ApplicationId appId; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 57 | |
| 58 | @Activate |
| 59 | public void activate() { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 60 | appId = coreService.registerApplication("org.onlab.onos.ifwd"); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 61 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
tom | 0511a52 | 2014-10-04 12:06:02 -0700 | [diff] [blame] | 62 | log.info("Started"); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | packetService.removeProcessor(processor); |
| 68 | processor = null; |
| 69 | log.info("Stopped"); |
| 70 | } |
| 71 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Packet processor responsible for forwarding packets along their paths. |
| 74 | */ |
| 75 | private class ReactivePacketProcessor implements PacketProcessor { |
| 76 | |
| 77 | @Override |
| 78 | public void process(PacketContext context) { |
| 79 | // Stop processing if the packet has been handled, since we |
| 80 | // can't do any more to it. |
| 81 | if (context.isHandled()) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | InboundPacket pkt = context.inPacket(); |
| 86 | Ethernet ethPkt = pkt.parsed(); |
| 87 | |
| 88 | HostId srcId = HostId.hostId(ethPkt.getSourceMAC()); |
| 89 | HostId dstId = HostId.hostId(ethPkt.getDestinationMAC()); |
| 90 | |
| 91 | // Do we know who this is for? If not, flood and bail. |
| 92 | Host dst = hostService.getHost(dstId); |
| 93 | if (dst == null) { |
| 94 | flood(context); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // Otherwise forward and be done with it. |
| 99 | setUpConnectivity(context, srcId, dstId); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 100 | forwardPacketToDst(context, dst); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 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 | |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 120 | private void forwardPacketToDst(PacketContext context, Host dst) { |
| 121 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(dst.location().port()).build(); |
| 122 | OutboundPacket packet = new DefaultOutboundPacket(dst.location().deviceId(), |
tom | 0511a52 | 2014-10-04 12:06:02 -0700 | [diff] [blame] | 123 | treatment, context.inPacket().unparsed()); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 124 | packetService.emit(packet); |
| 125 | log.info("sending packet: {}", packet); |
| 126 | } |
| 127 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 128 | // Install a rule forwarding the packet to the specified port. |
| 129 | private void setUpConnectivity(PacketContext context, HostId srcId, HostId dstId) { |
| 130 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 131 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 132 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 133 | HostToHostIntent intent = new HostToHostIntent(appId, srcId, dstId, |
| 134 | selector, treatment); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 135 | |
| 136 | intentService.submit(intent); |
| 137 | } |
| 138 | |
| 139 | } |