blob: 21d07cb7acc1d25d48c5c63384565f2470cb85c7 [file] [log] [blame]
toma9a77c22014-10-03 17:05:20 -07001package org.onlab.onos.ifwd;
2
3import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Reference;
7import org.apache.felix.scr.annotations.ReferenceCardinality;
8import org.onlab.onos.ApplicationId;
9import org.onlab.onos.net.Host;
10import org.onlab.onos.net.HostId;
11import org.onlab.onos.net.PortNumber;
12import org.onlab.onos.net.flow.DefaultTrafficSelector;
13import org.onlab.onos.net.flow.DefaultTrafficTreatment;
14import org.onlab.onos.net.flow.TrafficSelector;
15import org.onlab.onos.net.flow.TrafficTreatment;
16import org.onlab.onos.net.host.HostService;
17import org.onlab.onos.net.intent.HostToHostIntent;
18import org.onlab.onos.net.intent.IntentId;
19import org.onlab.onos.net.intent.IntentService;
20import org.onlab.onos.net.packet.InboundPacket;
21import org.onlab.onos.net.packet.PacketContext;
22import org.onlab.onos.net.packet.PacketProcessor;
23import org.onlab.onos.net.packet.PacketService;
24import org.onlab.onos.net.topology.TopologyService;
25import org.onlab.packet.Ethernet;
26import org.slf4j.Logger;
27
28import static org.slf4j.LoggerFactory.getLogger;
29
30/**
31 * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework.
32 */
33@Component(immediate = true)
34public 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