blob: 1a9d3be0c738862715944b52821a91e31946dc48 [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 " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700108 "instead of PacketOut with actual port; default is false")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100109 private boolean packetOutOfppTable = false;
110
111 @Property(name = "flowTimeout", intValue = DEFAULT_TIMEOUT,
112 label = "Configure Flow Timeout for installed flow rules; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700113 "default is 10 sec")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100114 private int flowTimeout = DEFAULT_TIMEOUT;
115
116 @Property(name = "flowPriority", intValue = DEFAULT_PRIORITY,
117 label = "Configure Flow Priority for installed flow rules; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700118 "default is 10")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100119 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; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700155 "default is false")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100156 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);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700166 requestIntercepts();
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800167
Charles M.C. Chane148de82015-05-06 12:38:21 +0800168 log.info("Started with Application ID {}", appId.id());
169 }
170
171 @Deactivate
172 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800173 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700174 withdrawIntercepts();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800175 flowRuleService.removeFlowRulesById(appId);
176 packetService.removeProcessor(processor);
177 processor = null;
178 log.info("Stopped");
179 }
180
181 @Modified
182 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800183 readComponentConfiguration(context);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700184 requestIntercepts();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800185 }
186
187 /**
188 * Request packet in via PacketService.
189 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700190 private void requestIntercepts() {
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800191 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
192 selector.matchEthType(Ethernet.TYPE_IPV4);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700193 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100194 selector.matchEthType(Ethernet.TYPE_ARP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700195 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100196
Thomas Vachuska27bee092015-06-23 19:03:10 -0700197 selector.matchEthType(Ethernet.TYPE_IPV6);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100198 if (ipv6Forwarding) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700199 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
200 } else {
201 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100202 }
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900203 }
204
205 /**
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700206 * Cancel request for packet in via PacketService.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700207 */
208 private void withdrawIntercepts() {
209 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
210 selector.matchEthType(Ethernet.TYPE_IPV4);
211 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
212 selector.matchEthType(Ethernet.TYPE_ARP);
213 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
214 selector.matchEthType(Ethernet.TYPE_IPV6);
215 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
216 }
217
218 /**
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900219 * Extracts properties from the component configuration context.
220 *
221 * @param context the component context
222 */
223 private void readComponentConfiguration(ComponentContext context) {
224 Dictionary<?, ?> properties = context.getProperties();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100225 boolean packetOutOnlyEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700226 isPropertyEnabled(properties, "packetOutOnly");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900227 if (packetOutOnly != packetOutOnlyEnabled) {
228 packetOutOnly = packetOutOnlyEnabled;
229 log.info("Configured. Packet-out only forwarding is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700230 packetOutOnly ? "enabled" : "disabled");
tomc16656f2014-10-15 18:30:31 -0700231 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100232 boolean packetOutOfppTableEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700233 isPropertyEnabled(properties, "packetOutOfppTable");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100234 if (packetOutOfppTable != packetOutOfppTableEnabled) {
235 packetOutOfppTable = packetOutOfppTableEnabled;
236 log.info("Configured. Forwarding using OFPP_TABLE port is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700237 packetOutOfppTable ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100238 }
239 boolean ipv6ForwardingEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700240 isPropertyEnabled(properties, "ipv6Forwarding");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900241 if (ipv6Forwarding != ipv6ForwardingEnabled) {
242 ipv6Forwarding = ipv6ForwardingEnabled;
243 log.info("Configured. IPv6 forwarding is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700244 ipv6Forwarding ? "enabled" : "disabled");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900245 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100246 boolean matchDstMacOnlyEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700247 isPropertyEnabled(properties, "matchDstMacOnly");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100248 if (matchDstMacOnly != matchDstMacOnlyEnabled) {
249 matchDstMacOnly = matchDstMacOnlyEnabled;
250 log.info("Configured. Match Dst MAC Only is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700251 matchDstMacOnly ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100252 }
253 boolean matchVlanIdEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700254 isPropertyEnabled(properties, "matchVlanId");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100255 if (matchVlanId != matchVlanIdEnabled) {
256 matchVlanId = matchVlanIdEnabled;
257 log.info("Configured. Matching Vlan ID is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700258 matchVlanId ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100259 }
260 boolean matchIpv4AddressEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700261 isPropertyEnabled(properties, "matchIpv4Address");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100262 if (matchIpv4Address != matchIpv4AddressEnabled) {
263 matchIpv4Address = matchIpv4AddressEnabled;
264 log.info("Configured. Matching IPv4 Addresses is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700265 matchIpv4Address ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100266 }
267 boolean matchIpv4DscpEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700268 isPropertyEnabled(properties, "matchIpv4Dscp");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100269 if (matchIpv4Dscp != matchIpv4DscpEnabled) {
270 matchIpv4Dscp = matchIpv4DscpEnabled;
271 log.info("Configured. Matching IPv4 DSCP and ECN is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700272 matchIpv4Dscp ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100273 }
274 boolean matchIpv6AddressEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700275 isPropertyEnabled(properties, "matchIpv6Address");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100276 if (matchIpv6Address != matchIpv6AddressEnabled) {
277 matchIpv6Address = matchIpv6AddressEnabled;
278 log.info("Configured. Matching IPv6 Addresses is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700279 matchIpv6Address ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100280 }
281 boolean matchIpv6FlowLabelEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700282 isPropertyEnabled(properties, "matchIpv6FlowLabel");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100283 if (matchIpv6FlowLabel != matchIpv6FlowLabelEnabled) {
284 matchIpv6FlowLabel = matchIpv6FlowLabelEnabled;
285 log.info("Configured. Matching IPv6 FlowLabel is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700286 matchIpv6FlowLabel ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100287 }
288 boolean matchTcpUdpPortsEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700289 isPropertyEnabled(properties, "matchTcpUdpPorts");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100290 if (matchTcpUdpPorts != matchTcpUdpPortsEnabled) {
291 matchTcpUdpPorts = matchTcpUdpPortsEnabled;
292 log.info("Configured. Matching TCP/UDP fields is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700293 matchTcpUdpPorts ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100294 }
295 boolean matchIcmpFieldsEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700296 isPropertyEnabled(properties, "matchIcmpFields");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100297 if (matchIcmpFields != matchIcmpFieldsEnabled) {
298 matchIcmpFields = matchIcmpFieldsEnabled;
299 log.info("Configured. Matching ICMP (v4 and v6) fields is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700300 matchIcmpFields ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100301 }
302 Integer flowTimeoutConfigured =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700303 getIntegerProperty(properties, "flowTimeout");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100304 if (flowTimeoutConfigured == null) {
305 log.info("Flow Timeout is not configured, default value is {}",
306 flowTimeout);
307 } else {
308 flowTimeout = flowTimeoutConfigured;
309 log.info("Configured. Flow Timeout is configured to {}",
310 flowTimeout, " seconds");
311 }
312 Integer flowPriorityConfigured =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700313 getIntegerProperty(properties, "flowPriority");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100314 if (flowPriorityConfigured == null) {
315 log.info("Flow Priority is not configured, default value is {}",
316 flowPriority);
317 } else {
318 flowPriority = flowPriorityConfigured;
319 log.info("Configured. Flow Priority is configured to {}",
320 flowPriority);
321 }
322 }
323
324 /**
325 * Get Integer property from the propertyName
326 * Return null if propertyName is not found.
327 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700328 * @param properties properties to be looked up
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100329 * @param propertyName the name of the property to look up
330 * @return value when the propertyName is defined or return null
331 */
332 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
333 String propertyName) {
334 Integer value = null;
335 try {
336 String s = (String) properties.get(propertyName);
337 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800338 } catch (NumberFormatException | ClassCastException e) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100339 value = null;
340 }
341 return value;
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900342 }
343
344 /**
345 * Check property name is defined and set to true.
346 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700347 * @param properties properties to be looked up
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900348 * @param propertyName the name of the property to look up
349 * @return true when the propertyName is defined and set to true
350 */
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100351 private static boolean isPropertyEnabled(Dictionary<?, ?> properties,
352 String propertyName) {
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900353 boolean enabled = false;
354 try {
355 String flag = (String) properties.get(propertyName);
356 if (flag != null) {
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800357 enabled = flag.trim().equals("true");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900358 }
359 } catch (ClassCastException e) {
360 // No propertyName defined.
361 enabled = false;
362 }
363 return enabled;
tomc16656f2014-10-15 18:30:31 -0700364 }
tomc370ebd2014-09-16 01:25:21 -0700365
366 /**
367 * Packet processor responsible for forwarding packets along their paths.
368 */
369 private class ReactivePacketProcessor implements PacketProcessor {
370
371 @Override
372 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700373 // Stop processing if the packet has been handled, since we
374 // can't do any more to it.
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800375 if (context.isHandled()) {
alshabib7b2748f2014-09-16 20:21:11 -0700376 return;
377 }
tomdc95b8a2014-09-17 08:07:26 -0700378
tomc370ebd2014-09-16 01:25:21 -0700379 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -0700380 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -0700381
Jonathan Harte8600eb2015-01-12 10:30:45 -0800382 if (ethPkt == null) {
383 return;
384 }
385
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900386 // Bail if this is deemed to be a control packet.
387 if (isControlPacket(ethPkt)) {
388 return;
389 }
390
391 // Skip IPv6 multicast packet when IPv6 forward is disabled.
392 if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) {
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800393 return;
394 }
395
tom642b2262014-09-17 13:52:55 -0700396 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700397
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700398 // Do not process link-local addresses in any way.
399 if (id.mac().isLinkLocal()) {
400 return;
401 }
402
tomc370ebd2014-09-16 01:25:21 -0700403 // Do we know who this is for? If not, flood and bail.
404 Host dst = hostService.getHost(id);
405 if (dst == null) {
406 flood(context);
407 return;
408 }
409
410 // Are we on an edge switch that our destination is on? If so,
411 // simply forward out to the destination and bail.
412 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700413 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
414 installRule(context, dst.location().port());
415 }
tomc370ebd2014-09-16 01:25:21 -0700416 return;
417 }
418
419 // Otherwise, get a set of paths that lead from here to the
420 // destination edge switch.
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100421 Set<Path> paths =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700422 topologyService.getPaths(topologyService.currentTopology(),
423 pkt.receivedFrom().deviceId(),
424 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700425 if (paths.isEmpty()) {
426 // If there are no paths, flood and bail.
427 flood(context);
428 return;
429 }
430
431 // Otherwise, pick a path that does not lead back to where we
432 // came from; if no such path, flood and bail.
433 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
434 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700435 log.warn("Doh... don't know where to go... {} -> {} received on {}",
tomc16656f2014-10-15 18:30:31 -0700436 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
437 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700438 flood(context);
439 return;
440 }
441
442 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700443 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700444 }
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800445
446 }
447
448 // Indicates whether this is a control packet, e.g. LLDP, BDDP
449 private boolean isControlPacket(Ethernet eth) {
450 short type = eth.getEtherType();
451 return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN;
tomc370ebd2014-09-16 01:25:21 -0700452 }
453
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800454 // Indicated whether this is an IPv6 multicast packet.
455 private boolean isIpv6Multicast(Ethernet eth) {
456 return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast();
457 }
458
tomc370ebd2014-09-16 01:25:21 -0700459 // Selects a path from the given set that does not lead back to the
460 // specified port.
461 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
462 for (Path path : paths) {
463 if (!path.src().port().equals(notToPort)) {
464 return path;
465 }
466 }
467 return null;
468 }
469
tom642b2262014-09-17 13:52:55 -0700470 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700471 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700472 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700473 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700474 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700475 } else {
476 context.block();
477 }
478 }
479
tom642b2262014-09-17 13:52:55 -0700480 // Sends a packet out the specified port.
481 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700482 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700483 context.send();
484 }
485
486 // Install a rule forwarding the packet to the specified port.
487 private void installRule(PacketContext context, PortNumber portNumber) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100488 //
489 // We don't support (yet) buffer IDs in the Flow Service so
490 // packet out first.
491 //
492 Ethernet inPkt = context.inPacket().parsed();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700493 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100494
495 // If PacketOutOnly or ARP packet than forward directly to output port
496 if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
497 packetOut(context, portNumber);
498 return;
499 }
500
501 //
502 // If matchDstMacOnly
503 // Create flows matching dstMac only
504 // Else
505 // Create flows with default matching and include configured fields
506 //
507 if (matchDstMacOnly) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700508 selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100509 } else {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700510 selectorBuilder.matchInPort(context.inPacket().receivedFrom().port())
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800511 .matchEthSrc(inPkt.getSourceMAC())
Jonathan Hart430223a2015-04-22 17:39:02 -0700512 .matchEthDst(inPkt.getDestinationMAC());
alshabib7b2748f2014-09-16 20:21:11 -0700513
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100514 // If configured Match Vlan ID
515 if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700516 selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100517 }
alshabib7b2748f2014-09-16 20:21:11 -0700518
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100519 //
520 // If configured and EtherType is IPv4 - Match IPv4 and
521 // TCP/UDP/ICMP fields
522 //
523 if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
524 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
525 byte ipv4Protocol = ipv4Packet.getProtocol();
526 Ip4Prefix matchIp4SrcPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700527 Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(),
528 Ip4Prefix.MAX_MASK_LENGTH);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100529 Ip4Prefix matchIp4DstPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700530 Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(),
531 Ip4Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800532 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
Jonathan Hart430223a2015-04-22 17:39:02 -0700533 .matchIPSrc(matchIp4SrcPrefix)
534 .matchIPDst(matchIp4DstPrefix);
alshabib6eb438a2014-10-01 16:39:37 -0700535
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100536 if (matchIpv4Dscp) {
Pavlin Radoslavovbf23c552015-02-20 14:20:30 -0800537 byte dscp = ipv4Packet.getDscp();
538 byte ecn = ipv4Packet.getEcn();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700539 selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100540 }
541
542 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
543 TCP tcpPacket = (TCP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700544 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700545 .matchTcpSrc(tcpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100546 .matchTcpDst(tcpPacket.getDestinationPort());
547 }
548 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
549 UDP udpPacket = (UDP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700550 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700551 .matchUdpSrc(udpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100552 .matchUdpDst(udpPacket.getDestinationPort());
553 }
554 if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
555 ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700556 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700557 .matchIcmpType(icmpPacket.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100558 .matchIcmpCode(icmpPacket.getIcmpCode());
559 }
560 }
561
562 //
563 // If configured and EtherType is IPv6 - Match IPv6 and
564 // TCP/UDP/ICMP fields
565 //
566 if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
567 IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
568 byte ipv6NextHeader = ipv6Packet.getNextHeader();
569 Ip6Prefix matchIp6SrcPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700570 Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(),
571 Ip6Prefix.MAX_MASK_LENGTH);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100572 Ip6Prefix matchIp6DstPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700573 Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(),
574 Ip6Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800575 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6)
576 .matchIPv6Src(matchIp6SrcPrefix)
Jonathan Hart430223a2015-04-22 17:39:02 -0700577 .matchIPv6Dst(matchIp6DstPrefix);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100578
579 if (matchIpv6FlowLabel) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700580 selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100581 }
582
583 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
584 TCP tcpPacket = (TCP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700585 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700586 .matchTcpSrc(tcpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100587 .matchTcpDst(tcpPacket.getDestinationPort());
588 }
589 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
590 UDP udpPacket = (UDP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700591 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700592 .matchUdpSrc(udpPacket.getSourcePort())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100593 .matchUdpDst(udpPacket.getDestinationPort());
594 }
595 if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
596 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700597 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700598 .matchIcmpv6Type(icmp6Packet.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100599 .matchIcmpv6Code(icmp6Packet.getIcmpCode());
600 }
601 }
602 }
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700603 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
604 .setOutput(portNumber)
605 .build();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100606
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700607 ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder()
608 .withSelector(selectorBuilder.build())
609 .withTreatment(treatment)
610 .withPriority(flowPriority)
611 .withFlag(ForwardingObjective.Flag.VERSATILE)
612 .fromApp(appId)
613 .makeTemporary(flowTimeout)
614 .add();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100615
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700616 flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(),
617 forwardingObjective);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100618
619 //
620 // If packetOutOfppTable
621 // Send packet back to the OpenFlow pipeline to match installed flow
622 // Else
623 // Send packet direction on the appropriate port
624 //
625 if (packetOutOfppTable) {
626 packetOut(context, PortNumber.TABLE);
627 } else {
628 packetOut(context, portNumber);
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800629 }
tomc370ebd2014-09-16 01:25:21 -0700630 }
alshabib030111e2014-09-15 15:56:42 -0700631}