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