blob: b332d2c9427eedd9e23016e3a4c8a24bdc24b120 [file] [log] [blame]
alshabib030111e2014-09-15 15:56:42 -07001package org.onlab.onos.fwd;
2
alshabibe9d3a322014-09-23 15:18:33 -07003import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Set;
6
alshabib030111e2014-09-15 15:56:42 -07007import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Reference;
11import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabiba68eb962014-09-24 20:34:13 -070012import org.onlab.onos.ApplicationId;
tomc370ebd2014-09-16 01:25:21 -070013import org.onlab.onos.net.Host;
14import org.onlab.onos.net.HostId;
15import org.onlab.onos.net.Path;
16import org.onlab.onos.net.PortNumber;
alshabib7b2748f2014-09-16 20:21:11 -070017import org.onlab.onos.net.flow.DefaultFlowRule;
18import org.onlab.onos.net.flow.DefaultTrafficSelector;
19import org.onlab.onos.net.flow.DefaultTrafficTreatment;
20import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.FlowRuleService;
22import org.onlab.onos.net.flow.TrafficSelector;
23import org.onlab.onos.net.flow.TrafficTreatment;
alshabib8aef1ad2014-09-15 17:47:31 -070024import org.onlab.onos.net.host.HostService;
tomc370ebd2014-09-16 01:25:21 -070025import org.onlab.onos.net.packet.InboundPacket;
26import org.onlab.onos.net.packet.PacketContext;
alshabib030111e2014-09-15 15:56:42 -070027import org.onlab.onos.net.packet.PacketProcessor;
28import org.onlab.onos.net.packet.PacketService;
29import org.onlab.onos.net.topology.TopologyService;
alshabib7b2748f2014-09-16 20:21:11 -070030import org.onlab.packet.Ethernet;
tomc370ebd2014-09-16 01:25:21 -070031import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070032
tomc370ebd2014-09-16 01:25:21 -070033/**
34 * Sample reactive forwarding application.
35 */
36@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070037public class ReactiveForwarding {
38
tomc370ebd2014-09-16 01:25:21 -070039 private final Logger log = getLogger(getClass());
40
alshabib030111e2014-09-15 15:56:42 -070041 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected TopologyService topologyService;
43
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected PacketService packetService;
46
alshabib8aef1ad2014-09-15 17:47:31 -070047 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected HostService hostService;
49
alshabib7b2748f2014-09-16 20:21:11 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected FlowRuleService flowRuleService;
52
tomc370ebd2014-09-16 01:25:21 -070053 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070054
alshabiba68eb962014-09-24 20:34:13 -070055 private ApplicationId appId;
56
alshabib030111e2014-09-15 15:56:42 -070057 @Activate
58 public void activate() {
alshabiba68eb962014-09-24 20:34:13 -070059 appId = ApplicationId.getAppId();
alshabib030111e2014-09-15 15:56:42 -070060 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
alshabiba68eb962014-09-24 20:34:13 -070061 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -070062 }
63
64 @Deactivate
65 public void deactivate() {
alshabiba68eb962014-09-24 20:34:13 -070066 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -070067 packetService.removeProcessor(processor);
68 processor = null;
tomc370ebd2014-09-16 01:25:21 -070069 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -070070 }
tomc370ebd2014-09-16 01:25:21 -070071
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) {
tomdc95b8a2014-09-17 08:07:26 -070080 // Stop processing if the packet has been handled, since we
81 // can't do any more to it.
alshabib7b2748f2014-09-16 20:21:11 -070082 if (context.isHandled()) {
83 return;
84 }
tomdc95b8a2014-09-17 08:07:26 -070085
tomc370ebd2014-09-16 01:25:21 -070086 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -070087 Ethernet ethPkt = pkt.parsed();
88 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -070089
90 // Do we know who this is for? If not, flood and bail.
91 Host dst = hostService.getHost(id);
92 if (dst == null) {
93 flood(context);
94 return;
95 }
96
97 // Are we on an edge switch that our destination is on? If so,
98 // simply forward out to the destination and bail.
99 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib7b2748f2014-09-16 20:21:11 -0700100 installRule(context, dst.location().port());
tomc370ebd2014-09-16 01:25:21 -0700101 return;
102 }
103
104 // Otherwise, get a set of paths that lead from here to the
105 // destination edge switch.
106 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700107 pkt.receivedFrom().deviceId(),
108 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700109 if (paths.isEmpty()) {
110 // If there are no paths, flood and bail.
111 flood(context);
112 return;
113 }
114
115 // Otherwise, pick a path that does not lead back to where we
116 // came from; if no such path, flood and bail.
117 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
118 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700119 log.warn("Doh... don't know where to go... {} -> {} received on {}",
alshabibe9d3a322014-09-23 15:18:33 -0700120 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
121 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700122 flood(context);
123 return;
124 }
125
126 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700127 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700128 }
129 }
130
131 // Selects a path from the given set that does not lead back to the
132 // specified port.
133 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
134 for (Path path : paths) {
135 if (!path.src().port().equals(notToPort)) {
136 return path;
137 }
138 }
139 return null;
140 }
141
tom642b2262014-09-17 13:52:55 -0700142 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700143 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700144 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700145 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700146 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700147 } else {
148 context.block();
149 }
150 }
151
tom642b2262014-09-17 13:52:55 -0700152 // Sends a packet out the specified port.
153 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700154 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700155 context.send();
156 }
157
158 // Install a rule forwarding the packet to the specified port.
159 private void installRule(PacketContext context, PortNumber portNumber) {
tom642b2262014-09-17 13:52:55 -0700160 // We don't yet support bufferids in the flowservice so packet out first.
161 packetOut(context, portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700162
tom642b2262014-09-17 13:52:55 -0700163 // Install the flow rule to handle this type of message from now on.
alshabib7b2748f2014-09-16 20:21:11 -0700164 Ethernet inPkt = context.inPacket().parsed();
165 TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
alshabib010c31d2014-09-26 10:01:12 -0700166 builder.matchEthType(inPkt.getEtherType())
167 .matchEthSrc(inPkt.getSourceMAC())
168 .matchEthDst(inPkt.getDestinationMAC())
169 .matchInport(context.inPacket().receivedFrom().port());
alshabib7b2748f2014-09-16 20:21:11 -0700170
171 TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
alshabib010c31d2014-09-26 10:01:12 -0700172 treat.setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700173
174 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
alshabiba68eb962014-09-24 20:34:13 -0700175 builder.build(), treat.build(), 0, appId);
alshabib7b2748f2014-09-16 20:21:11 -0700176
177 flowRuleService.applyFlowRules(f);
tomc370ebd2014-09-16 01:25:21 -0700178 }
179
alshabib030111e2014-09-15 15:56:42 -0700180}
181
tomc370ebd2014-09-16 01:25:21 -0700182