blob: c0c36c926f13870e8ac702b33c355d0796fa6f1d [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.fwd;
alshabib030111e2014-09-15 15:56:42 -070017
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
tomc16656f2014-10-15 18:30:31 -070021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabib030111e2014-09-15 15:56:42 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte8600eb2015-01-12 10:30:45 -080025import org.onlab.packet.Ethernet;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010026import org.onlab.packet.ICMP;
27import org.onlab.packet.ICMP6;
Jonathan Hart430223a2015-04-22 17:39:02 -070028import org.onlab.packet.IPv4;
29import org.onlab.packet.IPv6;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010030import org.onlab.packet.Ip4Prefix;
31import org.onlab.packet.Ip6Prefix;
Jonathan Hart430223a2015-04-22 17:39:02 -070032import org.onlab.packet.TCP;
33import org.onlab.packet.UDP;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010034import org.onlab.packet.VlanId;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070035import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
38import org.onosproject.net.Host;
39import org.onosproject.net.HostId;
40import org.onosproject.net.Path;
41import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart3b881aa2015-04-22 18:03:50 -070047import org.onosproject.net.flowobjective.DefaultForwardingObjective;
48import org.onosproject.net.flowobjective.FlowObjectiveService;
49import org.onosproject.net.flowobjective.ForwardingObjective;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.host.HostService;
51import org.onosproject.net.packet.InboundPacket;
52import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080053import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.packet.PacketProcessor;
55import org.onosproject.net.packet.PacketService;
56import org.onosproject.net.topology.TopologyService;
tomc16656f2014-10-15 18:30:31 -070057import org.osgi.service.component.ComponentContext;
tomc370ebd2014-09-16 01:25:21 -070058import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070059
Jonathan Hart430223a2015-04-22 17:39:02 -070060import java.util.Dictionary;
61import java.util.Set;
62
63import static com.google.common.base.Strings.isNullOrEmpty;
64import static org.slf4j.LoggerFactory.getLogger;
65
tomc370ebd2014-09-16 01:25:21 -070066/**
67 * Sample reactive forwarding application.
68 */
69@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070070public class ReactiveForwarding {
71
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010072 private static final int DEFAULT_TIMEOUT = 10;
73 private static final int DEFAULT_PRIORITY = 10;
alshabibba5ac482014-10-02 17:15:20 -070074
tomc370ebd2014-09-16 01:25:21 -070075 private final Logger log = getLogger(getClass());
76
alshabib030111e2014-09-15 15:56:42 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected TopologyService topologyService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected PacketService packetService;
82
alshabib8aef1ad2014-09-15 17:47:31 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected HostService hostService;
85
alshabib7b2748f2014-09-16 20:21:11 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected FlowRuleService flowRuleService;
88
alshabib92c65ad2014-10-08 21:56:05 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart3b881aa2015-04-22 18:03:50 -070090 protected FlowObjectiveService flowObjectiveService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -070093 protected CoreService coreService;
94
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected ComponentConfigService cfgService;
97
tomc370ebd2014-09-16 01:25:21 -070098 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070099
alshabiba68eb962014-09-24 20:34:13 -0700100 private ApplicationId appId;
101
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800102 @Property(name = "packetOutOnly", boolValue = false,
103 label = "Enable packet-out only forwarding; default is false")
104 private boolean packetOutOnly = false;
tomc16656f2014-10-15 18:30:31 -0700105
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100106 @Property(name = "packetOutOfppTable", boolValue = false,
107 label = "Enable first packet forwarding using OFPP_TABLE port " +
108 "instead of PacketOut with actual port; default is false")
109 private boolean packetOutOfppTable = false;
110
111 @Property(name = "flowTimeout", intValue = DEFAULT_TIMEOUT,
112 label = "Configure Flow Timeout for installed flow rules; " +
113 "default is 10 sec")
114 private int flowTimeout = DEFAULT_TIMEOUT;
115
116 @Property(name = "flowPriority", intValue = DEFAULT_PRIORITY,
117 label = "Configure Flow Priority for installed flow rules; " +
118 "default is 10")
119 private int flowPriority = DEFAULT_PRIORITY;
120
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900121 @Property(name = "ipv6Forwarding", boolValue = false,
122 label = "Enable IPv6 forwarding; default is false")
123 private boolean ipv6Forwarding = false;
124
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100125 @Property(name = "matchDstMacOnly", boolValue = false,
126 label = "Enable matching Dst Mac Only; default is false")
127 private boolean matchDstMacOnly = false;
128
129 @Property(name = "matchVlanId", boolValue = false,
130 label = "Enable matching Vlan ID; default is false")
131 private boolean matchVlanId = false;
132
133 @Property(name = "matchIpv4Address", boolValue = false,
134 label = "Enable matching IPv4 Addresses; default is false")
135 private boolean matchIpv4Address = false;
136
137 @Property(name = "matchIpv4Dscp", boolValue = false,
138 label = "Enable matching IPv4 DSCP and ECN; default is false")
139 private boolean matchIpv4Dscp = false;
140
141 @Property(name = "matchIpv6Address", boolValue = false,
142 label = "Enable matching IPv6 Addresses; default is false")
143 private boolean matchIpv6Address = false;
144
145 @Property(name = "matchIpv6FlowLabel", boolValue = false,
146 label = "Enable matching IPv6 FlowLabel; default is false")
147 private boolean matchIpv6FlowLabel = false;
148
149 @Property(name = "matchTcpUdpPorts", boolValue = false,
150 label = "Enable matching TCP/UDP ports; default is false")
151 private boolean matchTcpUdpPorts = false;
152
153 @Property(name = "matchIcmpFields", boolValue = false,
154 label = "Enable matching ICMPv4 and ICMPv6 fields; " +
155 "default is false")
156 private boolean matchIcmpFields = false;
157
158
alshabib030111e2014-09-15 15:56:42 -0700159 @Activate
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900160 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700161 cfgService.registerProperties(getClass());
Brian O'Connorabafb502014-12-02 22:26:20 -0800162 appId = coreService.registerApplication("org.onosproject.fwd");
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800163
alshabibc274c902014-10-03 14:58:27 -0700164 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900165 readComponentConfiguration(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800166
167 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
168 selector.matchEthType(Ethernet.TYPE_IPV4);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100169 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE,
170 appId);
171 selector.matchEthType(Ethernet.TYPE_ARP);
172 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE,
173 appId);
174
175 if (ipv6Forwarding) {
176 selector.matchEthType(Ethernet.TYPE_IPV6);
177 packetService.requestPackets(selector.build(),
178 PacketPriority.REACTIVE, appId);
179 }
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800180
alshabiba68eb962014-09-24 20:34:13 -0700181 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -0700182 }
183
184 @Deactivate
185 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700186 cfgService.unregisterProperties(getClass(), false);
alshabiba68eb962014-09-24 20:34:13 -0700187 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -0700188 packetService.removeProcessor(processor);
189 processor = null;
tomc370ebd2014-09-16 01:25:21 -0700190 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -0700191 }
tomc370ebd2014-09-16 01:25:21 -0700192
tomc16656f2014-10-15 18:30:31 -0700193 @Modified
194 public void modified(ComponentContext context) {
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900195 readComponentConfiguration(context);
196 }
197
198 /**
199 * Extracts properties from the component configuration context.
200 *
201 * @param context the component context
202 */
203 private void readComponentConfiguration(ComponentContext context) {
204 Dictionary<?, ?> properties = context.getProperties();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100205 boolean packetOutOnlyEnabled =
206 isPropertyEnabled(properties, "packetOutOnly");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900207 if (packetOutOnly != packetOutOnlyEnabled) {
208 packetOutOnly = packetOutOnlyEnabled;
209 log.info("Configured. Packet-out only forwarding is {}",
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100210 packetOutOnly ? "enabled" : "disabled");
tomc16656f2014-10-15 18:30:31 -0700211 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100212 boolean packetOutOfppTableEnabled =
213 isPropertyEnabled(properties, "packetOutOfppTable");
214 if (packetOutOfppTable != packetOutOfppTableEnabled) {
215 packetOutOfppTable = packetOutOfppTableEnabled;
216 log.info("Configured. Forwarding using OFPP_TABLE port is {}",
217 packetOutOfppTable ? "enabled" : "disabled");
218 }
219 boolean ipv6ForwardingEnabled =
220 isPropertyEnabled(properties, "ipv6Forwarding");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900221 if (ipv6Forwarding != ipv6ForwardingEnabled) {
222 ipv6Forwarding = ipv6ForwardingEnabled;
223 log.info("Configured. IPv6 forwarding is {}",
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100224 ipv6Forwarding ? "enabled" : "disabled");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900225 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100226 boolean matchDstMacOnlyEnabled =
227 isPropertyEnabled(properties, "matchDstMacOnly");
228 if (matchDstMacOnly != matchDstMacOnlyEnabled) {
229 matchDstMacOnly = matchDstMacOnlyEnabled;
230 log.info("Configured. Match Dst MAC Only is {}",
231 matchDstMacOnly ? "enabled" : "disabled");
232 }
233 boolean matchVlanIdEnabled =
234 isPropertyEnabled(properties, "matchVlanId");
235 if (matchVlanId != matchVlanIdEnabled) {
236 matchVlanId = matchVlanIdEnabled;
237 log.info("Configured. Matching Vlan ID is {}",
238 matchVlanId ? "enabled" : "disabled");
239 }
240 boolean matchIpv4AddressEnabled =
241 isPropertyEnabled(properties, "matchIpv4Address");
242 if (matchIpv4Address != matchIpv4AddressEnabled) {
243 matchIpv4Address = matchIpv4AddressEnabled;
244 log.info("Configured. Matching IPv4 Addresses is {}",
245 matchIpv4Address ? "enabled" : "disabled");
246 }
247 boolean matchIpv4DscpEnabled =
248 isPropertyEnabled(properties, "matchIpv4Dscp");
249 if (matchIpv4Dscp != matchIpv4DscpEnabled) {
250 matchIpv4Dscp = matchIpv4DscpEnabled;
251 log.info("Configured. Matching IPv4 DSCP and ECN is {}",
252 matchIpv4Dscp ? "enabled" : "disabled");
253 }
254 boolean matchIpv6AddressEnabled =
255 isPropertyEnabled(properties, "matchIpv6Address");
256 if (matchIpv6Address != matchIpv6AddressEnabled) {
257 matchIpv6Address = matchIpv6AddressEnabled;
258 log.info("Configured. Matching IPv6 Addresses is {}",
259 matchIpv6Address ? "enabled" : "disabled");
260 }
261 boolean matchIpv6FlowLabelEnabled =
262 isPropertyEnabled(properties, "matchIpv6FlowLabel");
263 if (matchIpv6FlowLabel != matchIpv6FlowLabelEnabled) {
264 matchIpv6FlowLabel = matchIpv6FlowLabelEnabled;
265 log.info("Configured. Matching IPv6 FlowLabel is {}",
266 matchIpv6FlowLabel ? "enabled" : "disabled");
267 }
268 boolean matchTcpUdpPortsEnabled =
269 isPropertyEnabled(properties, "matchTcpUdpPorts");
270 if (matchTcpUdpPorts != matchTcpUdpPortsEnabled) {
271 matchTcpUdpPorts = matchTcpUdpPortsEnabled;
272 log.info("Configured. Matching TCP/UDP fields is {}",
273 matchTcpUdpPorts ? "enabled" : "disabled");
274 }
275 boolean matchIcmpFieldsEnabled =
276 isPropertyEnabled(properties, "matchIcmpFields");
277 if (matchIcmpFields != matchIcmpFieldsEnabled) {
278 matchIcmpFields = matchIcmpFieldsEnabled;
279 log.info("Configured. Matching ICMP (v4 and v6) fields is {}",
280 matchIcmpFields ? "enabled" : "disabled");
281 }
282 Integer flowTimeoutConfigured =
283 getIntegerProperty(properties, "flowTimeout");
284 if (flowTimeoutConfigured == null) {
285 log.info("Flow Timeout is not configured, default value is {}",
286 flowTimeout);
287 } else {
288 flowTimeout = flowTimeoutConfigured;
289 log.info("Configured. Flow Timeout is configured to {}",
290 flowTimeout, " seconds");
291 }
292 Integer flowPriorityConfigured =
293 getIntegerProperty(properties, "flowPriority");
294 if (flowPriorityConfigured == null) {
295 log.info("Flow Priority is not configured, default value is {}",
296 flowPriority);
297 } else {
298 flowPriority = flowPriorityConfigured;
299 log.info("Configured. Flow Priority is configured to {}",
300 flowPriority);
301 }
302 }
303
304 /**
305 * Get Integer property from the propertyName
306 * Return null if propertyName is not found.
307 *
308 * @param properties properties to be looked up
309 * @param propertyName the name of the property to look up
310 * @return value when the propertyName is defined or return null
311 */
312 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
313 String propertyName) {
314 Integer value = null;
315 try {
316 String s = (String) properties.get(propertyName);
317 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800318 } catch (NumberFormatException | ClassCastException e) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100319 value = null;
320 }
321 return value;
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900322 }
323
324 /**
325 * Check property name is defined and set to true.
326 *
327 * @param properties properties to be looked up
328 * @param propertyName the name of the property to look up
329 * @return true when the propertyName is defined and set to true
330 */
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100331 private static boolean isPropertyEnabled(Dictionary<?, ?> properties,
332 String propertyName) {
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900333 boolean enabled = false;
334 try {
335 String flag = (String) properties.get(propertyName);
336 if (flag != null) {
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800337 enabled = flag.trim().equals("true");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900338 }
339 } catch (ClassCastException e) {
340 // No propertyName defined.
341 enabled = false;
342 }
343 return enabled;
tomc16656f2014-10-15 18:30:31 -0700344 }
tomc370ebd2014-09-16 01:25:21 -0700345
346 /**
347 * Packet processor responsible for forwarding packets along their paths.
348 */
349 private class ReactivePacketProcessor implements PacketProcessor {
350
351 @Override
352 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700353 // Stop processing if the packet has been handled, since we
354 // can't do any more to it.
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800355 if (context.isHandled()) {
alshabib7b2748f2014-09-16 20:21:11 -0700356 return;
357 }
tomdc95b8a2014-09-17 08:07:26 -0700358
tomc370ebd2014-09-16 01:25:21 -0700359 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -0700360 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -0700361
Jonathan Harte8600eb2015-01-12 10:30:45 -0800362 if (ethPkt == null) {
363 return;
364 }
365
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900366 // Bail if this is deemed to be a control packet.
367 if (isControlPacket(ethPkt)) {
368 return;
369 }
370
371 // Skip IPv6 multicast packet when IPv6 forward is disabled.
372 if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) {
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800373 return;
374 }
375
tom642b2262014-09-17 13:52:55 -0700376 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700377
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700378 // Do not process link-local addresses in any way.
379 if (id.mac().isLinkLocal()) {
380 return;
381 }
382
tomc370ebd2014-09-16 01:25:21 -0700383 // Do we know who this is for? If not, flood and bail.
384 Host dst = hostService.getHost(id);
385 if (dst == null) {
386 flood(context);
387 return;
388 }
389
390 // Are we on an edge switch that our destination is on? If so,
391 // simply forward out to the destination and bail.
392 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700393 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
394 installRule(context, dst.location().port());
395 }
tomc370ebd2014-09-16 01:25:21 -0700396 return;
397 }
398
399 // Otherwise, get a set of paths that lead from here to the
400 // destination edge switch.
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100401 Set<Path> paths =
402 topologyService.getPaths(topologyService.currentTopology(),
403 pkt.receivedFrom().deviceId(),
404 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700405 if (paths.isEmpty()) {
406 // If there are no paths, flood and bail.
407 flood(context);
408 return;
409 }
410
411 // Otherwise, pick a path that does not lead back to where we
412 // came from; if no such path, flood and bail.
413 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
414 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700415 log.warn("Doh... don't know where to go... {} -> {} received on {}",
tomc16656f2014-10-15 18:30:31 -0700416 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
417 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700418 flood(context);
419 return;
420 }
421
422 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700423 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700424 }
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800425
426 }
427
428 // Indicates whether this is a control packet, e.g. LLDP, BDDP
429 private boolean isControlPacket(Ethernet eth) {
430 short type = eth.getEtherType();
431 return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN;
tomc370ebd2014-09-16 01:25:21 -0700432 }
433
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800434 // Indicated whether this is an IPv6 multicast packet.
435 private boolean isIpv6Multicast(Ethernet eth) {
436 return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast();
437 }
438
tomc370ebd2014-09-16 01:25:21 -0700439 // Selects a path from the given set that does not lead back to the
440 // specified port.
441 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
442 for (Path path : paths) {
443 if (!path.src().port().equals(notToPort)) {
444 return path;
445 }
446 }
447 return null;
448 }
449
tom642b2262014-09-17 13:52:55 -0700450 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700451 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700452 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700453 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700454 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700455 } else {
456 context.block();
457 }
458 }
459
tom642b2262014-09-17 13:52:55 -0700460 // Sends a packet out the specified port.
461 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700462 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700463 context.send();
464 }
465
466 // Install a rule forwarding the packet to the specified port.
467 private void installRule(PacketContext context, PortNumber portNumber) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100468 //
469 // We don't support (yet) buffer IDs in the Flow Service so
470 // packet out first.
471 //
472 Ethernet inPkt = context.inPacket().parsed();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700473 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100474
475 // If PacketOutOnly or ARP packet than forward directly to output port
476 if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
477 packetOut(context, portNumber);
478 return;
479 }
480
481 //
482 // If matchDstMacOnly
483 // Create flows matching dstMac only
484 // Else
485 // Create flows with default matching and include configured fields
486 //
487 if (matchDstMacOnly) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700488 selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100489 } else {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700490 selectorBuilder.matchInPort(context.inPacket().receivedFrom().port())
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800491 .matchEthSrc(inPkt.getSourceMAC())
Jonathan Hart430223a2015-04-22 17:39:02 -0700492 .matchEthDst(inPkt.getDestinationMAC());
alshabib7b2748f2014-09-16 20:21:11 -0700493
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100494 // If configured Match Vlan ID
495 if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700496 selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100497 }
alshabib7b2748f2014-09-16 20:21:11 -0700498
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100499 //
500 // If configured and EtherType is IPv4 - Match IPv4 and
501 // TCP/UDP/ICMP fields
502 //
503 if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
504 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
505 byte ipv4Protocol = ipv4Packet.getProtocol();
506 Ip4Prefix matchIp4SrcPrefix =
507 Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(),
508 Ip4Prefix.MAX_MASK_LENGTH);
509 Ip4Prefix matchIp4DstPrefix =
510 Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(),
511 Ip4Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800512 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
Jonathan Hart430223a2015-04-22 17:39:02 -0700513 .matchIPSrc(matchIp4SrcPrefix)
514 .matchIPDst(matchIp4DstPrefix);
alshabib6eb438a2014-10-01 16:39:37 -0700515
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100516 if (matchIpv4Dscp) {
Pavlin Radoslavovbf23c552015-02-20 14:20:30 -0800517 byte dscp = ipv4Packet.getDscp();
518 byte ecn = ipv4Packet.getEcn();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700519 selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100520 }
521
522 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
523 TCP tcpPacket = (TCP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700524 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700525 .matchTcpSrc(tcpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100526 .matchTcpDst(tcpPacket.getDestinationPort());
527 }
528 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
529 UDP udpPacket = (UDP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700530 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700531 .matchUdpSrc(udpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100532 .matchUdpDst(udpPacket.getDestinationPort());
533 }
534 if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
535 ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700536 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700537 .matchIcmpType(icmpPacket.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100538 .matchIcmpCode(icmpPacket.getIcmpCode());
539 }
540 }
541
542 //
543 // If configured and EtherType is IPv6 - Match IPv6 and
544 // TCP/UDP/ICMP fields
545 //
546 if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
547 IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
548 byte ipv6NextHeader = ipv6Packet.getNextHeader();
549 Ip6Prefix matchIp6SrcPrefix =
550 Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(),
551 Ip6Prefix.MAX_MASK_LENGTH);
552 Ip6Prefix matchIp6DstPrefix =
553 Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(),
554 Ip6Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800555 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6)
556 .matchIPv6Src(matchIp6SrcPrefix)
Jonathan Hart430223a2015-04-22 17:39:02 -0700557 .matchIPv6Dst(matchIp6DstPrefix);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100558
559 if (matchIpv6FlowLabel) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700560 selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100561 }
562
563 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
564 TCP tcpPacket = (TCP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700565 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700566 .matchTcpSrc(tcpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100567 .matchTcpDst(tcpPacket.getDestinationPort());
568 }
569 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
570 UDP udpPacket = (UDP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700571 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700572 .matchUdpSrc(udpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100573 .matchUdpDst(udpPacket.getDestinationPort());
574 }
575 if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
576 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700577 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700578 .matchIcmpv6Type(icmp6Packet.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100579 .matchIcmpv6Code(icmp6Packet.getIcmpCode());
580 }
581 }
582 }
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700583 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
584 .setOutput(portNumber)
585 .build();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100586
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700587 ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder()
588 .withSelector(selectorBuilder.build())
589 .withTreatment(treatment)
590 .withPriority(flowPriority)
591 .withFlag(ForwardingObjective.Flag.VERSATILE)
592 .fromApp(appId)
593 .makeTemporary(flowTimeout)
594 .add();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100595
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700596 flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(),
597 forwardingObjective);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100598
599 //
600 // If packetOutOfppTable
601 // Send packet back to the OpenFlow pipeline to match installed flow
602 // Else
603 // Send packet direction on the appropriate port
604 //
605 if (packetOutOfppTable) {
606 packetOut(context, PortNumber.TABLE);
607 } else {
608 packetOut(context, portNumber);
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800609 }
tomc370ebd2014-09-16 01:25:21 -0700610 }
alshabib030111e2014-09-15 15:56:42 -0700611}