blob: 39f5a68026d5c67701b316d83b5c8d3307341391 [file] [log] [blame]
alshabib030111e2014-09-15 15:56:42 -07001package org.onlab.onos.fwd;
2
3import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
tomc16656f2014-10-15 18:30:31 -07006import org.apache.felix.scr.annotations.Modified;
7import org.apache.felix.scr.annotations.Property;
alshabib030111e2014-09-15 15:56:42 -07008import org.apache.felix.scr.annotations.Reference;
9import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabiba68eb962014-09-24 20:34:13 -070010import org.onlab.onos.ApplicationId;
alshabib92c65ad2014-10-08 21:56:05 -070011import org.onlab.onos.CoreService;
tomc370ebd2014-09-16 01:25:21 -070012import org.onlab.onos.net.Host;
13import org.onlab.onos.net.HostId;
14import org.onlab.onos.net.Path;
15import org.onlab.onos.net.PortNumber;
alshabib7b2748f2014-09-16 20:21:11 -070016import org.onlab.onos.net.flow.DefaultFlowRule;
17import org.onlab.onos.net.flow.DefaultTrafficSelector;
18import org.onlab.onos.net.flow.DefaultTrafficTreatment;
19import org.onlab.onos.net.flow.FlowRule;
20import org.onlab.onos.net.flow.FlowRuleService;
21import org.onlab.onos.net.flow.TrafficSelector;
22import org.onlab.onos.net.flow.TrafficTreatment;
alshabib8aef1ad2014-09-15 17:47:31 -070023import org.onlab.onos.net.host.HostService;
tomc370ebd2014-09-16 01:25:21 -070024import org.onlab.onos.net.packet.InboundPacket;
25import org.onlab.onos.net.packet.PacketContext;
alshabib030111e2014-09-15 15:56:42 -070026import org.onlab.onos.net.packet.PacketProcessor;
27import org.onlab.onos.net.packet.PacketService;
28import org.onlab.onos.net.topology.TopologyService;
alshabib7b2748f2014-09-16 20:21:11 -070029import org.onlab.packet.Ethernet;
tomc16656f2014-10-15 18:30:31 -070030import org.osgi.service.component.ComponentContext;
tomc370ebd2014-09-16 01:25:21 -070031import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070032
tomc16656f2014-10-15 18:30:31 -070033import java.util.Dictionary;
34import java.util.Set;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
tomc370ebd2014-09-16 01:25:21 -070038/**
39 * Sample reactive forwarding application.
40 */
41@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070042public class ReactiveForwarding {
43
alshabibba5ac482014-10-02 17:15:20 -070044 private static final int TIMEOUT = 10;
alshabiba0e04982014-10-03 13:03:19 -070045 private static final int PRIORITY = 10;
alshabibba5ac482014-10-02 17:15:20 -070046
tomc370ebd2014-09-16 01:25:21 -070047 private final Logger log = getLogger(getClass());
48
alshabib030111e2014-09-15 15:56:42 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected TopologyService topologyService;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected PacketService packetService;
54
alshabib8aef1ad2014-09-15 17:47:31 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected HostService hostService;
57
alshabib7b2748f2014-09-16 20:21:11 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected FlowRuleService flowRuleService;
60
alshabib92c65ad2014-10-08 21:56:05 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected CoreService coreService;
63
tomc370ebd2014-09-16 01:25:21 -070064 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070065
alshabiba68eb962014-09-24 20:34:13 -070066 private ApplicationId appId;
67
tomc16656f2014-10-15 18:30:31 -070068 @Property(name = "enabled", boolValue = true, label = "Forwarding enabled")
69 private boolean isEnabled = true;
70
alshabib030111e2014-09-15 15:56:42 -070071 @Activate
72 public void activate() {
alshabib92c65ad2014-10-08 21:56:05 -070073 appId = coreService.registerApplication("org.onlab.onos.fwd");
alshabibc274c902014-10-03 14:58:27 -070074 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
alshabiba68eb962014-09-24 20:34:13 -070075 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -070076 }
77
78 @Deactivate
79 public void deactivate() {
alshabiba68eb962014-09-24 20:34:13 -070080 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -070081 packetService.removeProcessor(processor);
82 processor = null;
tomc370ebd2014-09-16 01:25:21 -070083 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -070084 }
tomc370ebd2014-09-16 01:25:21 -070085
tomc16656f2014-10-15 18:30:31 -070086 @Modified
87 public void modified(ComponentContext context) {
88 Dictionary properties = context.getProperties();
89 String flag = (String) properties.get("enabled");
90 if (flag != null) {
91 boolean enabled = flag.equals("true");
92 if (isEnabled != enabled) {
93 isEnabled = enabled;
94 if (!isEnabled) {
95 flowRuleService.removeFlowRulesById(appId);
96 }
97 log.info("Reconfigured enabled = {}", isEnabled);
98 }
99 }
100 }
tomc370ebd2014-09-16 01:25:21 -0700101
102 /**
103 * Packet processor responsible for forwarding packets along their paths.
104 */
105 private class ReactivePacketProcessor implements PacketProcessor {
106
107 @Override
108 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700109 // Stop processing if the packet has been handled, since we
110 // can't do any more to it.
tomc16656f2014-10-15 18:30:31 -0700111 if (!isEnabled || context.isHandled()) {
alshabib7b2748f2014-09-16 20:21:11 -0700112 return;
113 }
tomdc95b8a2014-09-17 08:07:26 -0700114
tomc370ebd2014-09-16 01:25:21 -0700115 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -0700116 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -0700117
tom642b2262014-09-17 13:52:55 -0700118 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700119
120 // Do we know who this is for? If not, flood and bail.
121 Host dst = hostService.getHost(id);
122 if (dst == null) {
123 flood(context);
124 return;
125 }
126
127 // Are we on an edge switch that our destination is on? If so,
128 // simply forward out to the destination and bail.
129 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700130 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
131 installRule(context, dst.location().port());
132 }
tomc370ebd2014-09-16 01:25:21 -0700133 return;
134 }
135
136 // Otherwise, get a set of paths that lead from here to the
137 // destination edge switch.
138 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700139 pkt.receivedFrom().deviceId(),
140 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700141 if (paths.isEmpty()) {
142 // If there are no paths, flood and bail.
143 flood(context);
144 return;
145 }
146
147 // Otherwise, pick a path that does not lead back to where we
148 // came from; if no such path, flood and bail.
149 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
150 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700151 log.warn("Doh... don't know where to go... {} -> {} received on {}",
tomc16656f2014-10-15 18:30:31 -0700152 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
153 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700154 flood(context);
155 return;
156 }
157
158 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700159 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700160 }
161 }
162
163 // Selects a path from the given set that does not lead back to the
164 // specified port.
165 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
166 for (Path path : paths) {
167 if (!path.src().port().equals(notToPort)) {
168 return path;
169 }
170 }
171 return null;
172 }
173
tom642b2262014-09-17 13:52:55 -0700174 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700175 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700176 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700177 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700178 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700179 } else {
180 context.block();
181 }
182 }
183
tom642b2262014-09-17 13:52:55 -0700184 // Sends a packet out the specified port.
185 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700186 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700187 context.send();
188 }
189
190 // Install a rule forwarding the packet to the specified port.
191 private void installRule(PacketContext context, PortNumber portNumber) {
tom642b2262014-09-17 13:52:55 -0700192 // We don't yet support bufferids in the flowservice so packet out first.
193 packetOut(context, portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700194
alshabibc274c902014-10-03 14:58:27 -0700195 // Install the flow rule to handle this type of message from now on.
196 Ethernet inPkt = context.inPacket().parsed();
197 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
198 builder.matchEthType(inPkt.getEtherType())
tomc16656f2014-10-15 18:30:31 -0700199 .matchEthSrc(inPkt.getSourceMAC())
200 .matchEthDst(inPkt.getDestinationMAC())
201 .matchInport(context.inPacket().receivedFrom().port());
alshabib7b2748f2014-09-16 20:21:11 -0700202
alshabibc274c902014-10-03 14:58:27 -0700203 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
204 treat.setOutput(portNumber);
alshabib6eb438a2014-10-01 16:39:37 -0700205
alshabibc274c902014-10-03 14:58:27 -0700206 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
tomc16656f2014-10-15 18:30:31 -0700207 builder.build(), treat.build(), PRIORITY, appId, TIMEOUT);
alshabibc274c902014-10-03 14:58:27 -0700208
209 flowRuleService.applyFlowRules(f);
210
tomc370ebd2014-09-16 01:25:21 -0700211 }
212
alshabib030111e2014-09-15 15:56:42 -0700213}
214
tomc370ebd2014-09-16 01:25:21 -0700215