blob: 18f5d0a7ca38a135c5eba160424dd69734234a01 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabib030111e2014-09-15 15:56:42 -070016package org.onlab.onos.fwd;
17
Jonathan Hartbc4a7932014-10-21 11:46:00 -070018import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Dictionary;
21import java.util.Set;
22
alshabib030111e2014-09-15 15:56:42 -070023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
tomc16656f2014-10-15 18:30:31 -070026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
alshabib030111e2014-09-15 15:56:42 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070030import org.onlab.onos.core.ApplicationId;
31import org.onlab.onos.core.CoreService;
tomc370ebd2014-09-16 01:25:21 -070032import org.onlab.onos.net.Host;
33import org.onlab.onos.net.HostId;
34import org.onlab.onos.net.Path;
35import org.onlab.onos.net.PortNumber;
alshabib7b2748f2014-09-16 20:21:11 -070036import org.onlab.onos.net.flow.DefaultFlowRule;
37import org.onlab.onos.net.flow.DefaultTrafficSelector;
38import org.onlab.onos.net.flow.DefaultTrafficTreatment;
39import org.onlab.onos.net.flow.FlowRule;
40import org.onlab.onos.net.flow.FlowRuleService;
41import org.onlab.onos.net.flow.TrafficSelector;
42import org.onlab.onos.net.flow.TrafficTreatment;
alshabib8aef1ad2014-09-15 17:47:31 -070043import org.onlab.onos.net.host.HostService;
tomc370ebd2014-09-16 01:25:21 -070044import org.onlab.onos.net.packet.InboundPacket;
45import org.onlab.onos.net.packet.PacketContext;
alshabib030111e2014-09-15 15:56:42 -070046import org.onlab.onos.net.packet.PacketProcessor;
47import org.onlab.onos.net.packet.PacketService;
48import org.onlab.onos.net.topology.TopologyService;
alshabib7b2748f2014-09-16 20:21:11 -070049import org.onlab.packet.Ethernet;
tomc16656f2014-10-15 18:30:31 -070050import org.osgi.service.component.ComponentContext;
tomc370ebd2014-09-16 01:25:21 -070051import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070052
tomc370ebd2014-09-16 01:25:21 -070053/**
54 * Sample reactive forwarding application.
55 */
56@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070057public class ReactiveForwarding {
58
alshabibba5ac482014-10-02 17:15:20 -070059 private static final int TIMEOUT = 10;
alshabiba0e04982014-10-03 13:03:19 -070060 private static final int PRIORITY = 10;
alshabibba5ac482014-10-02 17:15:20 -070061
tomc370ebd2014-09-16 01:25:21 -070062 private final Logger log = getLogger(getClass());
63
alshabib030111e2014-09-15 15:56:42 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected TopologyService topologyService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected PacketService packetService;
69
alshabib8aef1ad2014-09-15 17:47:31 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected HostService hostService;
72
alshabib7b2748f2014-09-16 20:21:11 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected FlowRuleService flowRuleService;
75
alshabib92c65ad2014-10-08 21:56:05 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected CoreService coreService;
78
tomc370ebd2014-09-16 01:25:21 -070079 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070080
alshabiba68eb962014-09-24 20:34:13 -070081 private ApplicationId appId;
82
Thomas Vachuska28dfb602014-10-16 08:49:17 -070083 @Property(name = "enabled", boolValue = true,
84 label = "Enable forwarding; default is true")
tomc16656f2014-10-15 18:30:31 -070085 private boolean isEnabled = true;
86
alshabib030111e2014-09-15 15:56:42 -070087 @Activate
88 public void activate() {
alshabib92c65ad2014-10-08 21:56:05 -070089 appId = coreService.registerApplication("org.onlab.onos.fwd");
alshabibc274c902014-10-03 14:58:27 -070090 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
alshabiba68eb962014-09-24 20:34:13 -070091 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -070092 }
93
94 @Deactivate
95 public void deactivate() {
alshabiba68eb962014-09-24 20:34:13 -070096 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -070097 packetService.removeProcessor(processor);
98 processor = null;
tomc370ebd2014-09-16 01:25:21 -070099 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -0700100 }
tomc370ebd2014-09-16 01:25:21 -0700101
tomc16656f2014-10-15 18:30:31 -0700102 @Modified
103 public void modified(ComponentContext context) {
104 Dictionary properties = context.getProperties();
105 String flag = (String) properties.get("enabled");
106 if (flag != null) {
107 boolean enabled = flag.equals("true");
108 if (isEnabled != enabled) {
109 isEnabled = enabled;
110 if (!isEnabled) {
111 flowRuleService.removeFlowRulesById(appId);
112 }
Thomas Vachuska28dfb602014-10-16 08:49:17 -0700113 log.info("Reconfigured. Forwarding is {}",
114 isEnabled ? "enabled" : "disabled");
tomc16656f2014-10-15 18:30:31 -0700115 }
116 }
117 }
tomc370ebd2014-09-16 01:25:21 -0700118
119 /**
120 * Packet processor responsible for forwarding packets along their paths.
121 */
122 private class ReactivePacketProcessor implements PacketProcessor {
123
124 @Override
125 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700126 // Stop processing if the packet has been handled, since we
127 // can't do any more to it.
tomc16656f2014-10-15 18:30:31 -0700128 if (!isEnabled || context.isHandled()) {
alshabib7b2748f2014-09-16 20:21:11 -0700129 return;
130 }
tomdc95b8a2014-09-17 08:07:26 -0700131
tomc370ebd2014-09-16 01:25:21 -0700132 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -0700133 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -0700134
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800135 // Bail if this is deemed to be a control packet.
136 if (isControlPacket(ethPkt)) {
137 return;
138 }
139
tom642b2262014-09-17 13:52:55 -0700140 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700141
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700142 // Do not process link-local addresses in any way.
143 if (id.mac().isLinkLocal()) {
144 return;
145 }
146
tomc370ebd2014-09-16 01:25:21 -0700147 // Do we know who this is for? If not, flood and bail.
148 Host dst = hostService.getHost(id);
149 if (dst == null) {
150 flood(context);
151 return;
152 }
153
154 // Are we on an edge switch that our destination is on? If so,
155 // simply forward out to the destination and bail.
156 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700157 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
158 installRule(context, dst.location().port());
159 }
tomc370ebd2014-09-16 01:25:21 -0700160 return;
161 }
162
163 // Otherwise, get a set of paths that lead from here to the
164 // destination edge switch.
165 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700166 pkt.receivedFrom().deviceId(),
167 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700168 if (paths.isEmpty()) {
169 // If there are no paths, flood and bail.
170 flood(context);
171 return;
172 }
173
174 // Otherwise, pick a path that does not lead back to where we
175 // came from; if no such path, flood and bail.
176 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
177 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700178 log.warn("Doh... don't know where to go... {} -> {} received on {}",
tomc16656f2014-10-15 18:30:31 -0700179 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
180 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700181 flood(context);
182 return;
183 }
184
185 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700186 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700187 }
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800188
189 }
190
191 // Indicates whether this is a control packet, e.g. LLDP, BDDP
192 private boolean isControlPacket(Ethernet eth) {
193 short type = eth.getEtherType();
194 return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN;
tomc370ebd2014-09-16 01:25:21 -0700195 }
196
197 // Selects a path from the given set that does not lead back to the
198 // specified port.
199 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
200 for (Path path : paths) {
201 if (!path.src().port().equals(notToPort)) {
202 return path;
203 }
204 }
205 return null;
206 }
207
tom642b2262014-09-17 13:52:55 -0700208 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700209 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700210 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700211 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700212 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700213 } else {
214 context.block();
215 }
216 }
217
tom642b2262014-09-17 13:52:55 -0700218 // Sends a packet out the specified port.
219 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700220 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700221 context.send();
222 }
223
224 // Install a rule forwarding the packet to the specified port.
225 private void installRule(PacketContext context, PortNumber portNumber) {
tom642b2262014-09-17 13:52:55 -0700226 // We don't yet support bufferids in the flowservice so packet out first.
227 packetOut(context, portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700228
alshabibc274c902014-10-03 14:58:27 -0700229 // Install the flow rule to handle this type of message from now on.
230 Ethernet inPkt = context.inPacket().parsed();
231 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
232 builder.matchEthType(inPkt.getEtherType())
tomc16656f2014-10-15 18:30:31 -0700233 .matchEthSrc(inPkt.getSourceMAC())
234 .matchEthDst(inPkt.getDestinationMAC())
235 .matchInport(context.inPacket().receivedFrom().port());
alshabib7b2748f2014-09-16 20:21:11 -0700236
alshabibc274c902014-10-03 14:58:27 -0700237 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
238 treat.setOutput(portNumber);
alshabib6eb438a2014-10-01 16:39:37 -0700239
alshabibc274c902014-10-03 14:58:27 -0700240 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
Jonathan Hartbc4a7932014-10-21 11:46:00 -0700241 builder.build(), treat.build(), PRIORITY, appId, TIMEOUT, false);
alshabibc274c902014-10-03 14:58:27 -0700242
243 flowRuleService.applyFlowRules(f);
tomc370ebd2014-09-16 01:25:21 -0700244 }
245
alshabib030111e2014-09-15 15:56:42 -0700246}
247
tomc370ebd2014-09-16 01:25:21 -0700248