blob: bb29e655cd8967787afaaa2db309760ce52a972c [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
Sahil Lelea69534f2015-07-16 15:14:57 -070018import com.google.common.collect.ImmutableSet;
alshabib030111e2014-09-15 15:56:42 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
tomc16656f2014-10-15 18:30:31 -070022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabib030111e2014-09-15 15:56:42 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte8600eb2015-01-12 10:30:45 -080026import org.onlab.packet.Ethernet;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010027import org.onlab.packet.ICMP;
28import org.onlab.packet.ICMP6;
Jonathan Hart430223a2015-04-22 17:39:02 -070029import org.onlab.packet.IPv4;
30import org.onlab.packet.IPv6;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010031import org.onlab.packet.Ip4Prefix;
32import org.onlab.packet.Ip6Prefix;
Sahil Lelea69534f2015-07-16 15:14:57 -070033import org.onlab.packet.MacAddress;
Jonathan Hart430223a2015-04-22 17:39:02 -070034import org.onlab.packet.TCP;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070035import org.onlab.packet.TpPort;
Jonathan Hart430223a2015-04-22 17:39:02 -070036import org.onlab.packet.UDP;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010037import org.onlab.packet.VlanId;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070038import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
Sahil Lelea69534f2015-07-16 15:14:57 -070041import org.onosproject.event.Event;
42import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import org.onosproject.net.Host;
45import org.onosproject.net.HostId;
Sahil Lelea69534f2015-07-16 15:14:57 -070046import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.Path;
48import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.DefaultTrafficSelector;
50import org.onosproject.net.flow.DefaultTrafficTreatment;
Sahil Lelea69534f2015-07-16 15:14:57 -070051import org.onosproject.net.flow.FlowEntry;
52import org.onosproject.net.flow.FlowRule;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.flow.FlowRuleService;
54import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
Sahil Lelea69534f2015-07-16 15:14:57 -070056import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.instructions.Instruction;
59import org.onosproject.net.flow.instructions.Instructions;
Jonathan Hart3b881aa2015-04-22 18:03:50 -070060import org.onosproject.net.flowobjective.DefaultForwardingObjective;
61import org.onosproject.net.flowobjective.FlowObjectiveService;
62import org.onosproject.net.flowobjective.ForwardingObjective;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.host.HostService;
Sahil Lelea69534f2015-07-16 15:14:57 -070064import org.onosproject.net.link.LinkEvent;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import org.onosproject.net.packet.InboundPacket;
66import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080067import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080068import org.onosproject.net.packet.PacketProcessor;
69import org.onosproject.net.packet.PacketService;
Sahil Lelea69534f2015-07-16 15:14:57 -070070import org.onosproject.net.topology.TopologyEvent;
71import org.onosproject.net.topology.TopologyListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import org.onosproject.net.topology.TopologyService;
tomc16656f2014-10-15 18:30:31 -070073import org.osgi.service.component.ComponentContext;
tomc370ebd2014-09-16 01:25:21 -070074import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070075
Jonathan Hart430223a2015-04-22 17:39:02 -070076import java.util.Dictionary;
Sahil Lele7b961662015-07-20 15:06:04 -070077import java.util.HashMap;
Sahil Lelea69534f2015-07-16 15:14:57 -070078import java.util.List;
Sahil Lele7b961662015-07-20 15:06:04 -070079import java.util.Map;
Sahil Lelea69534f2015-07-16 15:14:57 -070080import java.util.Objects;
Jonathan Hart430223a2015-04-22 17:39:02 -070081import java.util.Set;
82
83import static com.google.common.base.Strings.isNullOrEmpty;
84import static org.slf4j.LoggerFactory.getLogger;
85
tomc370ebd2014-09-16 01:25:21 -070086/**
87 * Sample reactive forwarding application.
88 */
89@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070090public class ReactiveForwarding {
91
Dusan Pajin0d1d48f2015-02-20 16:05:11 +010092 private static final int DEFAULT_TIMEOUT = 10;
93 private static final int DEFAULT_PRIORITY = 10;
alshabibba5ac482014-10-02 17:15:20 -070094
tomc370ebd2014-09-16 01:25:21 -070095 private final Logger log = getLogger(getClass());
96
alshabib030111e2014-09-15 15:56:42 -070097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected TopologyService topologyService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected PacketService packetService;
102
alshabib8aef1ad2014-09-15 17:47:31 -0700103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected HostService hostService;
105
alshabib7b2748f2014-09-16 20:21:11 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected FlowRuleService flowRuleService;
108
alshabib92c65ad2014-10-08 21:56:05 -0700109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700110 protected FlowObjectiveService flowObjectiveService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -0700113 protected CoreService coreService;
114
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
116 protected ComponentConfigService cfgService;
117
tomc370ebd2014-09-16 01:25:21 -0700118 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -0700119
alshabiba68eb962014-09-24 20:34:13 -0700120 private ApplicationId appId;
121
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800122 @Property(name = "packetOutOnly", boolValue = false,
123 label = "Enable packet-out only forwarding; default is false")
124 private boolean packetOutOnly = false;
tomc16656f2014-10-15 18:30:31 -0700125
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100126 @Property(name = "packetOutOfppTable", boolValue = false,
127 label = "Enable first packet forwarding using OFPP_TABLE port " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700128 "instead of PacketOut with actual port; default is false")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100129 private boolean packetOutOfppTable = false;
130
131 @Property(name = "flowTimeout", intValue = DEFAULT_TIMEOUT,
132 label = "Configure Flow Timeout for installed flow rules; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700133 "default is 10 sec")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100134 private int flowTimeout = DEFAULT_TIMEOUT;
135
136 @Property(name = "flowPriority", intValue = DEFAULT_PRIORITY,
137 label = "Configure Flow Priority for installed flow rules; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700138 "default is 10")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100139 private int flowPriority = DEFAULT_PRIORITY;
140
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900141 @Property(name = "ipv6Forwarding", boolValue = false,
142 label = "Enable IPv6 forwarding; default is false")
143 private boolean ipv6Forwarding = false;
144
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100145 @Property(name = "matchDstMacOnly", boolValue = false,
146 label = "Enable matching Dst Mac Only; default is false")
147 private boolean matchDstMacOnly = false;
148
149 @Property(name = "matchVlanId", boolValue = false,
150 label = "Enable matching Vlan ID; default is false")
151 private boolean matchVlanId = false;
152
153 @Property(name = "matchIpv4Address", boolValue = false,
154 label = "Enable matching IPv4 Addresses; default is false")
155 private boolean matchIpv4Address = false;
156
157 @Property(name = "matchIpv4Dscp", boolValue = false,
158 label = "Enable matching IPv4 DSCP and ECN; default is false")
159 private boolean matchIpv4Dscp = false;
160
161 @Property(name = "matchIpv6Address", boolValue = false,
162 label = "Enable matching IPv6 Addresses; default is false")
163 private boolean matchIpv6Address = false;
164
165 @Property(name = "matchIpv6FlowLabel", boolValue = false,
166 label = "Enable matching IPv6 FlowLabel; default is false")
167 private boolean matchIpv6FlowLabel = false;
168
169 @Property(name = "matchTcpUdpPorts", boolValue = false,
170 label = "Enable matching TCP/UDP ports; default is false")
171 private boolean matchTcpUdpPorts = false;
172
173 @Property(name = "matchIcmpFields", boolValue = false,
174 label = "Enable matching ICMPv4 and ICMPv6 fields; " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700175 "default is false")
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100176 private boolean matchIcmpFields = false;
177
Sahil Lelea69534f2015-07-16 15:14:57 -0700178
Rusty Eddy29acad62015-07-07 19:33:47 -0700179 @Property(name = "ignoreIPv4Multicast", boolValue = false,
180 label = "Ignore (do not forward) IPv4 multicast packets; default is false")
181 private boolean ignoreIpv4McastPackets = false;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100182
Sahil Lelea69534f2015-07-16 15:14:57 -0700183 private final TopologyListener topologyListener = new InternalTopologyListener();
184
185
alshabib030111e2014-09-15 15:56:42 -0700186 @Activate
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900187 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700188 cfgService.registerProperties(getClass());
Brian O'Connorabafb502014-12-02 22:26:20 -0800189 appId = coreService.registerApplication("org.onosproject.fwd");
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800190
Brian O'Connor3b783262015-07-29 17:49:24 -0700191 packetService.addProcessor(processor, PacketProcessor.director(2));
Sahil Lelea69534f2015-07-16 15:14:57 -0700192 topologyService.addListener(topologyListener);
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900193 readComponentConfiguration(context);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700194 requestIntercepts();
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800195
Thomas Vachuska8d033672015-07-21 16:15:04 -0700196 log.info("Started", appId.id());
Charles M.C. Chane148de82015-05-06 12:38:21 +0800197 }
198
199 @Deactivate
200 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800201 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700202 withdrawIntercepts();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800203 flowRuleService.removeFlowRulesById(appId);
204 packetService.removeProcessor(processor);
Sahil Lelea69534f2015-07-16 15:14:57 -0700205 topologyService.removeListener(topologyListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800206 processor = null;
207 log.info("Stopped");
208 }
209
210 @Modified
211 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800212 readComponentConfiguration(context);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700213 requestIntercepts();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800214 }
215
216 /**
Thomas Vachuska8d033672015-07-21 16:15:04 -0700217 * Request packet in via packet service.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800218 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700219 private void requestIntercepts() {
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800220 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
221 selector.matchEthType(Ethernet.TYPE_IPV4);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700222 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100223 selector.matchEthType(Ethernet.TYPE_ARP);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700224 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100225
Thomas Vachuska27bee092015-06-23 19:03:10 -0700226 selector.matchEthType(Ethernet.TYPE_IPV6);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100227 if (ipv6Forwarding) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700228 packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId);
229 } else {
230 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100231 }
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900232 }
233
234 /**
Thomas Vachuska8d033672015-07-21 16:15:04 -0700235 * Cancel request for packet in via packet service.
Thomas Vachuska27bee092015-06-23 19:03:10 -0700236 */
237 private void withdrawIntercepts() {
238 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
239 selector.matchEthType(Ethernet.TYPE_IPV4);
240 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
241 selector.matchEthType(Ethernet.TYPE_ARP);
242 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
243 selector.matchEthType(Ethernet.TYPE_IPV6);
244 packetService.cancelPackets(selector.build(), PacketPriority.REACTIVE, appId);
245 }
246
247 /**
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900248 * Extracts properties from the component configuration context.
249 *
250 * @param context the component context
251 */
252 private void readComponentConfiguration(ComponentContext context) {
253 Dictionary<?, ?> properties = context.getProperties();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100254 boolean packetOutOnlyEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700255 isPropertyEnabled(properties, "packetOutOnly");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900256 if (packetOutOnly != packetOutOnlyEnabled) {
257 packetOutOnly = packetOutOnlyEnabled;
258 log.info("Configured. Packet-out only forwarding is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700259 packetOutOnly ? "enabled" : "disabled");
tomc16656f2014-10-15 18:30:31 -0700260 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100261 boolean packetOutOfppTableEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700262 isPropertyEnabled(properties, "packetOutOfppTable");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100263 if (packetOutOfppTable != packetOutOfppTableEnabled) {
264 packetOutOfppTable = packetOutOfppTableEnabled;
265 log.info("Configured. Forwarding using OFPP_TABLE port is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700266 packetOutOfppTable ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100267 }
268 boolean ipv6ForwardingEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700269 isPropertyEnabled(properties, "ipv6Forwarding");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900270 if (ipv6Forwarding != ipv6ForwardingEnabled) {
271 ipv6Forwarding = ipv6ForwardingEnabled;
272 log.info("Configured. IPv6 forwarding is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700273 ipv6Forwarding ? "enabled" : "disabled");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900274 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100275 boolean matchDstMacOnlyEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700276 isPropertyEnabled(properties, "matchDstMacOnly");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100277 if (matchDstMacOnly != matchDstMacOnlyEnabled) {
278 matchDstMacOnly = matchDstMacOnlyEnabled;
279 log.info("Configured. Match Dst MAC Only is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700280 matchDstMacOnly ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100281 }
282 boolean matchVlanIdEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700283 isPropertyEnabled(properties, "matchVlanId");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100284 if (matchVlanId != matchVlanIdEnabled) {
285 matchVlanId = matchVlanIdEnabled;
286 log.info("Configured. Matching Vlan ID is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700287 matchVlanId ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100288 }
289 boolean matchIpv4AddressEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700290 isPropertyEnabled(properties, "matchIpv4Address");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100291 if (matchIpv4Address != matchIpv4AddressEnabled) {
292 matchIpv4Address = matchIpv4AddressEnabled;
293 log.info("Configured. Matching IPv4 Addresses is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700294 matchIpv4Address ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100295 }
296 boolean matchIpv4DscpEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700297 isPropertyEnabled(properties, "matchIpv4Dscp");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100298 if (matchIpv4Dscp != matchIpv4DscpEnabled) {
299 matchIpv4Dscp = matchIpv4DscpEnabled;
300 log.info("Configured. Matching IPv4 DSCP and ECN is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700301 matchIpv4Dscp ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100302 }
303 boolean matchIpv6AddressEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700304 isPropertyEnabled(properties, "matchIpv6Address");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100305 if (matchIpv6Address != matchIpv6AddressEnabled) {
306 matchIpv6Address = matchIpv6AddressEnabled;
307 log.info("Configured. Matching IPv6 Addresses is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700308 matchIpv6Address ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100309 }
310 boolean matchIpv6FlowLabelEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700311 isPropertyEnabled(properties, "matchIpv6FlowLabel");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100312 if (matchIpv6FlowLabel != matchIpv6FlowLabelEnabled) {
313 matchIpv6FlowLabel = matchIpv6FlowLabelEnabled;
314 log.info("Configured. Matching IPv6 FlowLabel is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700315 matchIpv6FlowLabel ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100316 }
317 boolean matchTcpUdpPortsEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700318 isPropertyEnabled(properties, "matchTcpUdpPorts");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100319 if (matchTcpUdpPorts != matchTcpUdpPortsEnabled) {
320 matchTcpUdpPorts = matchTcpUdpPortsEnabled;
321 log.info("Configured. Matching TCP/UDP fields is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700322 matchTcpUdpPorts ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100323 }
324 boolean matchIcmpFieldsEnabled =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700325 isPropertyEnabled(properties, "matchIcmpFields");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100326 if (matchIcmpFields != matchIcmpFieldsEnabled) {
327 matchIcmpFields = matchIcmpFieldsEnabled;
328 log.info("Configured. Matching ICMP (v4 and v6) fields is {}",
Thomas Vachuska27bee092015-06-23 19:03:10 -0700329 matchIcmpFields ? "enabled" : "disabled");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100330 }
331 Integer flowTimeoutConfigured =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700332 getIntegerProperty(properties, "flowTimeout");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100333 if (flowTimeoutConfigured == null) {
Antonio Marsicob8b8d752015-12-17 16:28:34 +0100334 flowTimeout = DEFAULT_TIMEOUT;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100335 log.info("Flow Timeout is not configured, default value is {}",
336 flowTimeout);
337 } else {
338 flowTimeout = flowTimeoutConfigured;
339 log.info("Configured. Flow Timeout is configured to {}",
340 flowTimeout, " seconds");
341 }
342 Integer flowPriorityConfigured =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700343 getIntegerProperty(properties, "flowPriority");
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100344 if (flowPriorityConfigured == null) {
Antonio Marsicob8b8d752015-12-17 16:28:34 +0100345 flowPriority = DEFAULT_PRIORITY;
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100346 log.info("Flow Priority is not configured, default value is {}",
347 flowPriority);
348 } else {
349 flowPriority = flowPriorityConfigured;
350 log.info("Configured. Flow Priority is configured to {}",
351 flowPriority);
352 }
Rusty Eddy29acad62015-07-07 19:33:47 -0700353
354 boolean ignoreIpv4McastPacketsEnabled =
355 isPropertyEnabled(properties, "ignoreIpv4McastPackets");
356 if (ignoreIpv4McastPackets != ignoreIpv4McastPacketsEnabled) {
357 ignoreIpv4McastPackets = ignoreIpv4McastPacketsEnabled;
358 log.info("Configured. Ignore IPv4 multicast packets is {}",
Thomas Vachuska8d033672015-07-21 16:15:04 -0700359 ignoreIpv4McastPackets ? "enabled" : "disabled");
Rusty Eddy29acad62015-07-07 19:33:47 -0700360 }
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100361 }
362
363 /**
364 * Get Integer property from the propertyName
365 * Return null if propertyName is not found.
366 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700367 * @param properties properties to be looked up
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100368 * @param propertyName the name of the property to look up
369 * @return value when the propertyName is defined or return null
370 */
371 private static Integer getIntegerProperty(Dictionary<?, ?> properties,
372 String propertyName) {
373 Integer value = null;
374 try {
375 String s = (String) properties.get(propertyName);
376 value = isNullOrEmpty(s) ? value : Integer.parseInt(s.trim());
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800377 } catch (NumberFormatException | ClassCastException e) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100378 value = null;
379 }
380 return value;
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900381 }
382
383 /**
384 * Check property name is defined and set to true.
385 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700386 * @param properties properties to be looked up
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900387 * @param propertyName the name of the property to look up
388 * @return true when the propertyName is defined and set to true
389 */
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100390 private static boolean isPropertyEnabled(Dictionary<?, ?> properties,
391 String propertyName) {
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900392 boolean enabled = false;
393 try {
394 String flag = (String) properties.get(propertyName);
395 if (flag != null) {
Ayaka Koshibe8851ed92015-01-22 12:07:24 -0800396 enabled = flag.trim().equals("true");
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900397 }
398 } catch (ClassCastException e) {
399 // No propertyName defined.
400 enabled = false;
401 }
402 return enabled;
tomc16656f2014-10-15 18:30:31 -0700403 }
tomc370ebd2014-09-16 01:25:21 -0700404
405 /**
406 * Packet processor responsible for forwarding packets along their paths.
407 */
408 private class ReactivePacketProcessor implements PacketProcessor {
409
410 @Override
411 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700412 // Stop processing if the packet has been handled, since we
413 // can't do any more to it.
Sahil Lelea69534f2015-07-16 15:14:57 -0700414
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800415 if (context.isHandled()) {
alshabib7b2748f2014-09-16 20:21:11 -0700416 return;
417 }
tomdc95b8a2014-09-17 08:07:26 -0700418
tomc370ebd2014-09-16 01:25:21 -0700419 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -0700420 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -0700421
Jonathan Harte8600eb2015-01-12 10:30:45 -0800422 if (ethPkt == null) {
423 return;
424 }
425
Kunihiro Ishigurod37c9ca2014-12-31 16:05:40 +0900426 // Bail if this is deemed to be a control packet.
427 if (isControlPacket(ethPkt)) {
428 return;
429 }
430
431 // Skip IPv6 multicast packet when IPv6 forward is disabled.
432 if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) {
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800433 return;
434 }
435
tom642b2262014-09-17 13:52:55 -0700436 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700437
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700438 // Do not process link-local addresses in any way.
439 if (id.mac().isLinkLocal()) {
440 return;
441 }
442
Rusty Eddy29acad62015-07-07 19:33:47 -0700443 // Do not process IPv4 multicast packets, let mfwd handle them
444 if (ignoreIpv4McastPackets && ethPkt.getEtherType() == Ethernet.TYPE_IPV4) {
445 if (id.mac().isMulticast()) {
446 return;
447 }
448 }
449
tomc370ebd2014-09-16 01:25:21 -0700450 // Do we know who this is for? If not, flood and bail.
451 Host dst = hostService.getHost(id);
452 if (dst == null) {
453 flood(context);
454 return;
455 }
456
457 // Are we on an edge switch that our destination is on? If so,
458 // simply forward out to the destination and bail.
459 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700460 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
461 installRule(context, dst.location().port());
462 }
tomc370ebd2014-09-16 01:25:21 -0700463 return;
464 }
465
466 // Otherwise, get a set of paths that lead from here to the
467 // destination edge switch.
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100468 Set<Path> paths =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700469 topologyService.getPaths(topologyService.currentTopology(),
470 pkt.receivedFrom().deviceId(),
471 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700472 if (paths.isEmpty()) {
473 // If there are no paths, flood and bail.
474 flood(context);
475 return;
476 }
477
478 // Otherwise, pick a path that does not lead back to where we
479 // came from; if no such path, flood and bail.
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700480 Path path = pickForwardPathIfPossible(paths, pkt.receivedFrom().port());
tomc370ebd2014-09-16 01:25:21 -0700481 if (path == null) {
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700482 log.warn("Don't know where to go from here {} for {} -> {}",
483 pkt.receivedFrom(), ethPkt.getSourceMAC(), ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700484 flood(context);
485 return;
486 }
487
488 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700489 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700490 }
Thomas Vachuska01a6ec02014-11-05 09:54:09 -0800491
492 }
493
494 // Indicates whether this is a control packet, e.g. LLDP, BDDP
495 private boolean isControlPacket(Ethernet eth) {
496 short type = eth.getEtherType();
497 return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN;
tomc370ebd2014-09-16 01:25:21 -0700498 }
499
Thomas Vachuska5dd52f72014-11-28 19:27:45 -0800500 // Indicated whether this is an IPv6 multicast packet.
501 private boolean isIpv6Multicast(Ethernet eth) {
502 return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast();
503 }
504
tomc370ebd2014-09-16 01:25:21 -0700505 // Selects a path from the given set that does not lead back to the
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700506 // specified port if possible.
507 private Path pickForwardPathIfPossible(Set<Path> paths, PortNumber notToPort) {
508 Path lastPath = null;
tomc370ebd2014-09-16 01:25:21 -0700509 for (Path path : paths) {
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700510 lastPath = path;
tomc370ebd2014-09-16 01:25:21 -0700511 if (!path.src().port().equals(notToPort)) {
512 return path;
513 }
514 }
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700515 return lastPath;
tomc370ebd2014-09-16 01:25:21 -0700516 }
517
tom642b2262014-09-17 13:52:55 -0700518 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700519 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700520 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
tomc16656f2014-10-15 18:30:31 -0700521 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700522 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700523 } else {
524 context.block();
525 }
526 }
527
tom642b2262014-09-17 13:52:55 -0700528 // Sends a packet out the specified port.
529 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700530 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700531 context.send();
532 }
533
534 // Install a rule forwarding the packet to the specified port.
535 private void installRule(PacketContext context, PortNumber portNumber) {
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100536 //
537 // We don't support (yet) buffer IDs in the Flow Service so
538 // packet out first.
539 //
540 Ethernet inPkt = context.inPacket().parsed();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700541 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100542
543 // If PacketOutOnly or ARP packet than forward directly to output port
544 if (packetOutOnly || inPkt.getEtherType() == Ethernet.TYPE_ARP) {
545 packetOut(context, portNumber);
546 return;
547 }
548
549 //
550 // If matchDstMacOnly
551 // Create flows matching dstMac only
552 // Else
553 // Create flows with default matching and include configured fields
554 //
555 if (matchDstMacOnly) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700556 selectorBuilder.matchEthDst(inPkt.getDestinationMAC());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100557 } else {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700558 selectorBuilder.matchInPort(context.inPacket().receivedFrom().port())
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800559 .matchEthSrc(inPkt.getSourceMAC())
Jonathan Hart430223a2015-04-22 17:39:02 -0700560 .matchEthDst(inPkt.getDestinationMAC());
alshabib7b2748f2014-09-16 20:21:11 -0700561
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100562 // If configured Match Vlan ID
563 if (matchVlanId && inPkt.getVlanID() != Ethernet.VLAN_UNTAGGED) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700564 selectorBuilder.matchVlanId(VlanId.vlanId(inPkt.getVlanID()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100565 }
alshabib7b2748f2014-09-16 20:21:11 -0700566
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100567 //
568 // If configured and EtherType is IPv4 - Match IPv4 and
569 // TCP/UDP/ICMP fields
570 //
571 if (matchIpv4Address && inPkt.getEtherType() == Ethernet.TYPE_IPV4) {
572 IPv4 ipv4Packet = (IPv4) inPkt.getPayload();
573 byte ipv4Protocol = ipv4Packet.getProtocol();
574 Ip4Prefix matchIp4SrcPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700575 Ip4Prefix.valueOf(ipv4Packet.getSourceAddress(),
576 Ip4Prefix.MAX_MASK_LENGTH);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100577 Ip4Prefix matchIp4DstPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700578 Ip4Prefix.valueOf(ipv4Packet.getDestinationAddress(),
579 Ip4Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800580 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
Jonathan Hart430223a2015-04-22 17:39:02 -0700581 .matchIPSrc(matchIp4SrcPrefix)
582 .matchIPDst(matchIp4DstPrefix);
alshabib6eb438a2014-10-01 16:39:37 -0700583
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100584 if (matchIpv4Dscp) {
Pavlin Radoslavovbf23c552015-02-20 14:20:30 -0800585 byte dscp = ipv4Packet.getDscp();
586 byte ecn = ipv4Packet.getEcn();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700587 selectorBuilder.matchIPDscp(dscp).matchIPEcn(ecn);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100588 }
589
590 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_TCP) {
591 TCP tcpPacket = (TCP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700592 selectorBuilder.matchIPProtocol(ipv4Protocol)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700593 .matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort()))
594 .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100595 }
596 if (matchTcpUdpPorts && ipv4Protocol == IPv4.PROTOCOL_UDP) {
597 UDP udpPacket = (UDP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700598 selectorBuilder.matchIPProtocol(ipv4Protocol)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700599 .matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort()))
600 .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100601 }
602 if (matchIcmpFields && ipv4Protocol == IPv4.PROTOCOL_ICMP) {
603 ICMP icmpPacket = (ICMP) ipv4Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700604 selectorBuilder.matchIPProtocol(ipv4Protocol)
Jonathan Hart430223a2015-04-22 17:39:02 -0700605 .matchIcmpType(icmpPacket.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100606 .matchIcmpCode(icmpPacket.getIcmpCode());
607 }
608 }
609
610 //
611 // If configured and EtherType is IPv6 - Match IPv6 and
612 // TCP/UDP/ICMP fields
613 //
614 if (matchIpv6Address && inPkt.getEtherType() == Ethernet.TYPE_IPV6) {
615 IPv6 ipv6Packet = (IPv6) inPkt.getPayload();
616 byte ipv6NextHeader = ipv6Packet.getNextHeader();
617 Ip6Prefix matchIp6SrcPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700618 Ip6Prefix.valueOf(ipv6Packet.getSourceAddress(),
619 Ip6Prefix.MAX_MASK_LENGTH);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100620 Ip6Prefix matchIp6DstPrefix =
Thomas Vachuska27bee092015-06-23 19:03:10 -0700621 Ip6Prefix.valueOf(ipv6Packet.getDestinationAddress(),
622 Ip6Prefix.MAX_MASK_LENGTH);
Charles M.C. Chan7b8a9212015-04-26 01:25:53 +0800623 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6)
624 .matchIPv6Src(matchIp6SrcPrefix)
Jonathan Hart430223a2015-04-22 17:39:02 -0700625 .matchIPv6Dst(matchIp6DstPrefix);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100626
627 if (matchIpv6FlowLabel) {
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700628 selectorBuilder.matchIPv6FlowLabel(ipv6Packet.getFlowLabel());
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100629 }
630
631 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_TCP) {
632 TCP tcpPacket = (TCP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700633 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700634 .matchTcpSrc(TpPort.tpPort(tcpPacket.getSourcePort()))
635 .matchTcpDst(TpPort.tpPort(tcpPacket.getDestinationPort()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100636 }
637 if (matchTcpUdpPorts && ipv6NextHeader == IPv6.PROTOCOL_UDP) {
638 UDP udpPacket = (UDP) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700639 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700640 .matchUdpSrc(TpPort.tpPort(udpPacket.getSourcePort()))
641 .matchUdpDst(TpPort.tpPort(udpPacket.getDestinationPort()));
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100642 }
643 if (matchIcmpFields && ipv6NextHeader == IPv6.PROTOCOL_ICMP6) {
644 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700645 selectorBuilder.matchIPProtocol(ipv6NextHeader)
Jonathan Hart430223a2015-04-22 17:39:02 -0700646 .matchIcmpv6Type(icmp6Packet.getIcmpType())
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100647 .matchIcmpv6Code(icmp6Packet.getIcmpCode());
648 }
649 }
650 }
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700651 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
652 .setOutput(portNumber)
653 .build();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100654
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700655 ForwardingObjective forwardingObjective = DefaultForwardingObjective.builder()
656 .withSelector(selectorBuilder.build())
657 .withTreatment(treatment)
658 .withPriority(flowPriority)
659 .withFlag(ForwardingObjective.Flag.VERSATILE)
660 .fromApp(appId)
661 .makeTemporary(flowTimeout)
662 .add();
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100663
Jonathan Hart3b881aa2015-04-22 18:03:50 -0700664 flowObjectiveService.forward(context.inPacket().receivedFrom().deviceId(),
665 forwardingObjective);
Dusan Pajin0d1d48f2015-02-20 16:05:11 +0100666
667 //
668 // If packetOutOfppTable
669 // Send packet back to the OpenFlow pipeline to match installed flow
670 // Else
671 // Send packet direction on the appropriate port
672 //
673 if (packetOutOfppTable) {
674 packetOut(context, PortNumber.TABLE);
675 } else {
676 packetOut(context, portNumber);
Thomas Vachuskabd7f4b32014-12-04 20:54:55 -0800677 }
tomc370ebd2014-09-16 01:25:21 -0700678 }
Sahil Lelea69534f2015-07-16 15:14:57 -0700679
680 private class InternalTopologyListener implements TopologyListener {
681 @Override
682 public void event(TopologyEvent event) {
683 List<Event> reasons = event.reasons();
684 if (reasons != null) {
685 reasons.forEach(re -> {
686 if (re instanceof LinkEvent) {
687 LinkEvent le = (LinkEvent) re;
688 if (le.type() == LinkEvent.Type.LINK_REMOVED) {
689 fixBlackhole(le.subject().src());
690 }
691 }
692 });
693 }
694 }
695 }
696
697 private void fixBlackhole(ConnectPoint egress) {
Thomas Vachuska8d033672015-07-21 16:15:04 -0700698 Set<FlowEntry> rules = getFlowRulesFrom(egress);
Sahil Lelea69534f2015-07-16 15:14:57 -0700699 Set<SrcDstPair> pairs = findSrcDstPairs(rules);
700
Sahil Lele7b961662015-07-20 15:06:04 -0700701 Map<DeviceId, Set<Path>> srcPaths = new HashMap<>();
702
Thomas Vachuska8d033672015-07-21 16:15:04 -0700703 for (SrcDstPair sd : pairs) {
Sahil Lelea69534f2015-07-16 15:14:57 -0700704 // get the edge deviceID for the src host
Sahil Lele0f8d00e2015-07-24 11:05:43 -0700705 Host srcHost = hostService.getHost(HostId.hostId(sd.src));
706 Host dstHost = hostService.getHost(HostId.hostId(sd.dst));
707 if (srcHost != null && dstHost != null) {
708 DeviceId srcId = srcHost.location().deviceId();
709 DeviceId dstId = dstHost.location().deviceId();
710 log.trace("SRC ID is " + srcId + ", DST ID is " + dstId);
Sahil Lelea69534f2015-07-16 15:14:57 -0700711
Sahil Lele0f8d00e2015-07-24 11:05:43 -0700712 cleanFlowRules(sd, egress.deviceId());
Sahil Lelea69534f2015-07-16 15:14:57 -0700713
Sahil Lele0f8d00e2015-07-24 11:05:43 -0700714 Set<Path> shortestPaths = srcPaths.get(srcId);
715 if (shortestPaths == null) {
716 shortestPaths = topologyService.getPaths(topologyService.currentTopology(),
717 egress.deviceId(), srcId);
718 srcPaths.put(srcId, shortestPaths);
719 }
720 backTrackBadNodes(shortestPaths, dstId, sd);
Sahil Lele7b961662015-07-20 15:06:04 -0700721 }
Sahil Lelea69534f2015-07-16 15:14:57 -0700722 }
723 }
724
725 // Backtracks from link down event to remove flows that lead to blackhole
726 private void backTrackBadNodes(Set<Path> shortestPaths, DeviceId dstId, SrcDstPair sd) {
Thomas Vachuska8d033672015-07-21 16:15:04 -0700727 for (Path p : shortestPaths) {
Sahil Lelea69534f2015-07-16 15:14:57 -0700728 List<Link> pathLinks = p.links();
729 for (int i = 0; i < pathLinks.size(); i = i + 1) {
730 Link curLink = pathLinks.get(i);
731 DeviceId curDevice = curLink.src().deviceId();
Sahil Lelea69534f2015-07-16 15:14:57 -0700732
733 // skipping the first link because this link's src has already been pruned beforehand
734 if (i != 0) {
735 cleanFlowRules(sd, curDevice);
736 }
737
Thomas Vachuska8d033672015-07-21 16:15:04 -0700738 Set<Path> pathsFromCurDevice =
739 topologyService.getPaths(topologyService.currentTopology(),
740 curDevice, dstId);
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700741 if (pickForwardPathIfPossible(pathsFromCurDevice, curLink.src().port()) != null) {
Sahil Lelea69534f2015-07-16 15:14:57 -0700742 break;
743 } else {
744 if (i + 1 == pathLinks.size()) {
745 cleanFlowRules(sd, curLink.dst().deviceId());
746 }
747 }
748 }
749 }
750 }
751
752 // Removes flow rules off specified device with specific SrcDstPair
753 private void cleanFlowRules(SrcDstPair pair, DeviceId id) {
754 log.trace("Searching for flow rules to remove from: " + id);
755 log.trace("Removing flows w/ SRC=" + pair.src + ", DST=" + pair.dst);
756 for (FlowEntry r : flowRuleService.getFlowEntries(id)) {
757 boolean matchesSrc = false, matchesDst = false;
758 for (Instruction i : r.treatment().allInstructions()) {
759 if (i.type() == Instruction.Type.OUTPUT) {
Thomas Vachuska8d033672015-07-21 16:15:04 -0700760 // if the flow has matching src and dst
Sahil Lelea69534f2015-07-16 15:14:57 -0700761 for (Criterion cr : r.selector().criteria()) {
762 if (cr.type() == Criterion.Type.ETH_DST) {
763 if (((EthCriterion) cr).mac().equals(pair.dst)) {
764 matchesDst = true;
765 }
766 } else if (cr.type() == Criterion.Type.ETH_SRC) {
767 if (((EthCriterion) cr).mac().equals(pair.src)) {
768 matchesSrc = true;
769 }
770 }
771 }
772 }
773 }
774 if (matchesDst && matchesSrc) {
775 log.trace("Removed flow rule from device: " + id);
776 flowRuleService.removeFlowRules((FlowRule) r);
777 }
778 }
779
780 }
781
782 // Returns a set of src/dst MAC pairs extracted from the specified set of flow entries
783 private Set<SrcDstPair> findSrcDstPairs(Set<FlowEntry> rules) {
784 ImmutableSet.Builder<SrcDstPair> builder = ImmutableSet.builder();
Thomas Vachuska8d033672015-07-21 16:15:04 -0700785 for (FlowEntry r : rules) {
Sahil Lelea69534f2015-07-16 15:14:57 -0700786 MacAddress src = null, dst = null;
Thomas Vachuska8d033672015-07-21 16:15:04 -0700787 for (Criterion cr : r.selector().criteria()) {
Sahil Lelea69534f2015-07-16 15:14:57 -0700788 if (cr.type() == Criterion.Type.ETH_DST) {
789 dst = ((EthCriterion) cr).mac();
790 } else if (cr.type() == Criterion.Type.ETH_SRC) {
791 src = ((EthCriterion) cr).mac();
792 }
793 }
794 builder.add(new SrcDstPair(src, dst));
795 }
796 return builder.build();
797 }
798
Thomas Vachuska320c58f2015-08-05 10:42:32 -0700799 // Returns set of flow entries which were created by this application and
800 // which egress from the specified connection port
Sahil Lelea69534f2015-07-16 15:14:57 -0700801 private Set<FlowEntry> getFlowRulesFrom(ConnectPoint egress) {
802 ImmutableSet.Builder<FlowEntry> builder = ImmutableSet.builder();
803 flowRuleService.getFlowEntries(egress.deviceId()).forEach(r -> {
804 if (r.appId() == appId.id()) {
805 r.treatment().allInstructions().forEach(i -> {
806 if (i.type() == Instruction.Type.OUTPUT) {
807 if (((Instructions.OutputInstruction) i).port().equals(egress.port())) {
808 builder.add(r);
809 }
810 }
811 });
812 }
813 });
814
815 return builder.build();
816 }
817
818 // Wrapper class for a source and destination pair of MAC addresses
819 private final class SrcDstPair {
820 final MacAddress src;
821 final MacAddress dst;
822
823 private SrcDstPair(MacAddress src, MacAddress dst) {
824 this.src = src;
825 this.dst = dst;
826 }
827
828 @Override
829 public boolean equals(Object o) {
830 if (this == o) {
831 return true;
832 }
833 if (o == null || getClass() != o.getClass()) {
834 return false;
835 }
836 SrcDstPair that = (SrcDstPair) o;
837 return Objects.equals(src, that.src) &&
838 Objects.equals(dst, that.dst);
839 }
840
841 @Override
842 public int hashCode() {
843 return Objects.hash(src, dst);
844 }
845 }
alshabib030111e2014-09-15 15:56:42 -0700846}