blob: 42d77d55994afbd425d3aac09e34438241a13082 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.segmentrouting;
17
Pier Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Charles Chan051490d2018-01-11 11:48:18 -080019import com.google.common.collect.Sets;
Charles Chan61c086d2019-07-26 17:46:15 -070020import org.apache.commons.lang3.tuple.ImmutablePair;
Saurav Das4c35fc42015-11-20 15:27:53 -080021import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070022import org.onlab.packet.Ethernet;
Charles Chanef8d12e2017-12-05 21:07:38 -080023import org.onlab.packet.IPv6;
sangho80f11cb2015-04-01 13:05:26 -070024import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080025import org.onlab.packet.Ip6Address;
26import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070027import org.onlab.packet.IpPrefix;
28import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070029import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070030import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080031import org.onosproject.net.ConnectPoint;
Saurav Das9bf49582018-08-13 15:34:26 -070032import org.onosproject.net.Device;
Charles Chanf17fade2020-03-08 18:07:19 -070033import org.onosproject.net.Host;
Charles Chan1eaf4802016-04-18 13:44:03 -070034import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080035import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070036import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080037import org.onosproject.net.flowobjective.ObjectiveError;
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -070038import org.onosproject.net.intf.Interface;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080039import org.onosproject.net.packet.PacketPriority;
Charles Chan319d1a22015-11-03 10:42:14 -080040import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Saurav Das62ae6792017-05-15 15:34:25 -070041import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070042import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070044import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070045import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070048import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070050import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flowobjective.DefaultFilteringObjective;
52import org.onosproject.net.flowobjective.DefaultForwardingObjective;
53import org.onosproject.net.flowobjective.FilteringObjective;
54import org.onosproject.net.flowobjective.ForwardingObjective;
55import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070056import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
59
60import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070061import java.util.Collection;
62import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070063import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070064import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070065import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070066import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080067import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070068import java.util.Set;
Charles Chan12a8a842020-02-14 13:23:57 -080069import java.util.concurrent.CompletableFuture;
sanghofb7c7292015-04-13 15:15:58 -070070import java.util.concurrent.atomic.AtomicLong;
Charles Chanf17f66b2018-02-26 21:33:25 -080071import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -070072
73import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080074import static org.onlab.packet.Ethernet.TYPE_ARP;
75import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan051490d2018-01-11 11:48:18 -080076import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigib9632ba2017-01-12 18:14:58 -080077import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan051490d2018-01-11 11:48:18 -080078import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
79import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigib9632ba2017-01-12 18:14:58 -080080import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070081import static org.onosproject.segmentrouting.SegmentRoutingService.DEFAULT_PRIORITY;
sangho80f11cb2015-04-01 13:05:26 -070082
Charles Chanb7f75ac2016-01-11 18:28:54 -080083/**
84 * Populator of segment routing flow rules.
85 */
sangho80f11cb2015-04-01 13:05:26 -070086public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080087 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
88
89 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070090
sanghofb7c7292015-04-13 15:15:58 -070091 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070092 private SegmentRoutingManager srManager;
93 private DeviceConfiguration config;
psneha86e60d32019-03-26 06:31:41 -040094 private RouteSimplifierUtils routeSimplifierUtils;
Saurav Das9f1c42e2015-10-23 10:51:11 -070095
Andreas Pantelopoulos59bd97e2018-06-28 17:06:14 -070096 // used for signalling the driver to remove vlan table and tmac entry also
97 private static final long CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES = 1;
98 private static final long DOUBLE_TAGGED_METADATA_MASK = 0xffffffffffffffffL;
99
sangho80f11cb2015-04-01 13:05:26 -0700100 /**
101 * Creates a RoutingRulePopulator object.
102 *
Thomas Vachuska8a075092015-04-15 18:20:08 -0700103 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -0700104 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700105 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -0700106 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -0700107 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -0700108 this.rulePopulationCounter = new AtomicLong(0);
psneha86e60d32019-03-26 06:31:41 -0400109 this.routeSimplifierUtils = new RouteSimplifierUtils(srManager);
sanghofb7c7292015-04-13 15:15:58 -0700110 }
111
112 /**
113 * Resets the population counter.
114 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700115 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700116 rulePopulationCounter.set(0);
117 }
118
119 /**
120 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700121 *
122 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700123 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700124 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700125 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700126 }
127
128 /**
Charles Chand66d6712018-03-29 16:03:41 -0700129 * Populate a bridging rule on given deviceId that matches given mac, given vlan and
130 * output to given port.
131 *
132 * @param deviceId device ID
133 * @param port port
134 * @param mac mac address
135 * @param vlanId VLAN ID
Charles Chan12a8a842020-02-14 13:23:57 -0800136 * @return future that carries the flow objective if succeeded, null if otherwise
Charles Chand66d6712018-03-29 16:03:41 -0700137 */
Charles Chan12a8a842020-02-14 13:23:57 -0800138 CompletableFuture<Objective> populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
Charles Chand66d6712018-03-29 16:03:41 -0700139 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false);
140 if (fob == null) {
141 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
Charles Chan12a8a842020-02-14 13:23:57 -0800142 return CompletableFuture.completedFuture(null);
Charles Chand66d6712018-03-29 16:03:41 -0700143 }
144
Charles Chan12a8a842020-02-14 13:23:57 -0800145 CompletableFuture<Objective> future = new CompletableFuture<>();
Charles Chand66d6712018-03-29 16:03:41 -0700146 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan12a8a842020-02-14 13:23:57 -0800147 (objective) -> {
148 log.debug("Brigding rule for {}/{} populated", mac, vlanId);
149 future.complete(objective);
150 },
151 (objective, error) -> {
152 log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error);
153 future.complete(null);
154 });
Charles Chand66d6712018-03-29 16:03:41 -0700155 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan12a8a842020-02-14 13:23:57 -0800156 return future;
Charles Chand66d6712018-03-29 16:03:41 -0700157 }
158
159 /**
160 * Revoke a bridging rule on given deviceId that matches given mac, given vlan and
161 * output to given port.
162 *
163 * @param deviceId device ID
164 * @param port port
165 * @param mac mac address
166 * @param vlanId VLAN ID
Charles Chan12a8a842020-02-14 13:23:57 -0800167 * @return future that carries the flow objective if succeeded, null if otherwise
Charles Chand66d6712018-03-29 16:03:41 -0700168 */
Charles Chan12a8a842020-02-14 13:23:57 -0800169 CompletableFuture<Objective> revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
Charles Chand66d6712018-03-29 16:03:41 -0700170 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true);
171 if (fob == null) {
172 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
Charles Chan12a8a842020-02-14 13:23:57 -0800173 return CompletableFuture.completedFuture(null);
Charles Chand66d6712018-03-29 16:03:41 -0700174 }
175
Charles Chan12a8a842020-02-14 13:23:57 -0800176 CompletableFuture<Objective> future = new CompletableFuture<>();
Charles Chand66d6712018-03-29 16:03:41 -0700177 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan12a8a842020-02-14 13:23:57 -0800178 (objective) -> {
179 log.debug("Brigding rule for {}/{} revoked", mac, vlanId);
180 future.complete(objective);
181 },
182 (objective, error) -> {
183 log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error);
184 future.complete(null);
185 });
Charles Chand66d6712018-03-29 16:03:41 -0700186 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
Charles Chan12a8a842020-02-14 13:23:57 -0800187 return future;
Charles Chand66d6712018-03-29 16:03:41 -0700188 }
189
190 /**
191 * Generates a forwarding objective builder for bridging rules.
192 * <p>
193 * The forwarding objective bridges packets destined to a given MAC to
194 * given port on given device.
195 *
196 * @param deviceId Device that host attaches to
197 * @param mac MAC address of the host
198 * @param hostVlanId VLAN ID of the host
199 * @param outport Port that host attaches to
200 * @param revoke true if forwarding objective is meant to revoke forwarding rule
201 * @return Forwarding objective builder
202 */
203 private ForwardingObjective.Builder bridgingFwdObjBuilder(
204 DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) {
205 ConnectPoint connectPoint = new ConnectPoint(deviceId, outport);
Charles Chan098ca202018-05-01 11:50:20 -0700206 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
207 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
208 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chand66d6712018-03-29 16:03:41 -0700209
210 // Create host selector
211 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
212 sbuilder.matchEthDst(mac);
213
214 // Create host treatment
215 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
216 tbuilder.immediate().setOutput(outport);
217
218 // Create host meta
219 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
220
221 // Adjust the selector, treatment and meta according to VLAN configuration
222 if (taggedVlans.contains(hostVlanId)) {
223 sbuilder.matchVlanId(hostVlanId);
224 mbuilder.matchVlanId(hostVlanId);
225 } else if (hostVlanId.equals(VlanId.NONE)) {
226 if (untaggedVlan != null) {
227 sbuilder.matchVlanId(untaggedVlan);
228 mbuilder.matchVlanId(untaggedVlan);
229 tbuilder.immediate().popVlan();
230 } else if (nativeVlan != null) {
231 sbuilder.matchVlanId(nativeVlan);
232 mbuilder.matchVlanId(nativeVlan);
233 tbuilder.immediate().popVlan();
234 } else {
235 log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" +
236 "vlan config", mac, hostVlanId, connectPoint);
237 return null;
238 }
239 } else {
240 log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan",
241 mac, hostVlanId, connectPoint);
242 return null;
243 }
244
245 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
246 // If the objective is to revoke an existing rule, and for some reason
247 // the next-objective does not exist, then a new one should not be created
248 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport,
249 tbuilder.build(), mbuilder.build(), !revoke);
250 if (portNextObjId == -1) {
251 // Warning log will come from getPortNextObjective method
252 return null;
253 }
254
255 return DefaultForwardingObjective.builder()
256 .withFlag(ForwardingObjective.Flag.SPECIFIC)
257 .withSelector(sbuilder.build())
258 .nextStep(portNextObjId)
259 .withPriority(100)
260 .fromApp(srManager.appId)
261 .makePermanent();
262 }
263
264 /**
265 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
266 * and hostMAC connected to given port, and output to given port only when
267 * vlan information is valid.
268 *
269 * @param deviceId device ID that host attaches to
270 * @param portNum port number that host attaches to
271 * @param hostMac mac address of the host connected to the switch port
272 * @param vlanId Vlan ID configured on the switch port
273 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
274 * @param install true to populate the objective, false to revoke
275 */
276 // TODO Refactor. There are a lot of duplications between this method, populateBridging,
277 // revokeBridging and bridgingFwdObjBuilder.
278 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
279 VlanId vlanId, boolean popVlan, boolean install) {
280 // Create host selector
281 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
282 sbuilder.matchEthDst(hostMac);
283
284 // Create host meta
285 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
286
287 sbuilder.matchVlanId(vlanId);
288 mbuilder.matchVlanId(vlanId);
289
290 // Create host treatment
291 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
292 tbuilder.immediate().setOutput(portNum);
293
294 if (popVlan) {
295 tbuilder.immediate().popVlan();
296 }
297
298 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
299 tbuilder.build(), mbuilder.build(), install);
300 if (portNextObjId != -1) {
301 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
302 .withFlag(ForwardingObjective.Flag.SPECIFIC)
303 .withSelector(sbuilder.build())
304 .nextStep(portNextObjId)
305 .withPriority(100)
306 .fromApp(srManager.appId)
307 .makePermanent();
308
309 ObjectiveContext context = new DefaultObjectiveContext(
310 (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
311 install ? "populated" : "revoked"),
312 (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
313 install ? "populate" : "revoke", hostMac, vlanId, error));
314 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
315 } else {
316 log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
317 }
318 }
319
320 /**
321 * Populates IP rules for a route that has direct connection to the switch.
322 * This method should not be invoked directly without going through DefaultRoutingHandler.
sangho80f11cb2015-04-01 13:05:26 -0700323 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700324 * @param deviceId device ID of the device that next hop attaches to
325 * @param prefix IP prefix of the route
326 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800327 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700328 * @param outPort port where the next hop attaches to
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000329 * @param directHost host is of type direct or indirect
Charles Chan12a8a842020-02-14 13:23:57 -0800330 * @return future that carries the flow objective if succeeded, null if otherwise
sangho80f11cb2015-04-01 13:05:26 -0700331 */
Charles Chan12a8a842020-02-14 13:23:57 -0800332 CompletableFuture<Objective> populateRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000333 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Saurav Das261c3002017-06-13 15:35:54 -0700334 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700335 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800336 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800337 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700338 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -0700339 hostVlanId, outPort, null, null, directHost, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800340 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700341 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan12a8a842020-02-14 13:23:57 -0800342 return CompletableFuture.completedFuture(null);
Charles Chan319d1a22015-11-03 10:42:14 -0800343 }
Saurav Das07c74602016-04-27 18:35:50 -0700344 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700345 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700346 + "to error for dev:{} route:{}", deviceId, prefix);
Charles Chan12a8a842020-02-14 13:23:57 -0800347 return CompletableFuture.completedFuture(null);
Saurav Das07c74602016-04-27 18:35:50 -0700348 }
Charles Chan910be6a2017-08-23 14:46:43 -0700349
350 int nextId = fwdBuilder.add().nextId();
Charles Chan12a8a842020-02-14 13:23:57 -0800351 CompletableFuture<Objective> future = new CompletableFuture<>();
Charles Chan1eaf4802016-04-18 13:44:03 -0700352 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan12a8a842020-02-14 13:23:57 -0800353 (objective) -> {
354 log.debug("Direct routing rule for route {} populated. nextId={}", prefix, nextId);
355 future.complete(objective);
356 },
357 (objective, error) -> {
358 log.warn("Failed to populate direct routing rule for route {}: {}", prefix, error);
359 future.complete(null);
360 });
Charles Chan1eaf4802016-04-18 13:44:03 -0700361 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800362 rulePopulationCounter.incrementAndGet();
Charles Chan12a8a842020-02-14 13:23:57 -0800363 return future;
Charles Chanf4586112015-11-09 16:37:23 -0800364 }
365
Charles Chanb7f75ac2016-01-11 18:28:54 -0800366 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700367 * Removes IP rules for a route when the next hop is gone.
Charles Chand66d6712018-03-29 16:03:41 -0700368 * This method should not be invoked directly without going through DefaultRoutingHandler.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800369 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700370 * @param deviceId device ID of the device that next hop attaches to
371 * @param prefix IP prefix of the route
372 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800373 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700374 * @param outPort port that next hop attaches to
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000375 * @param directHost host is of type direct or indirect
Charles Chan12a8a842020-02-14 13:23:57 -0800376 * @return future that carries the flow objective if succeeded, null if otherwise
Charles Chanb7f75ac2016-01-11 18:28:54 -0800377 */
Charles Chan12a8a842020-02-14 13:23:57 -0800378 CompletableFuture<Objective> revokeRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000379 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700380 log.debug("Revoke IP table entry for route {} at {}:{}",
381 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800382 ForwardingObjective.Builder fwdBuilder;
383 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700384 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -0700385 hostVlanId, outPort, null, null, directHost, true);
Charles Chanf4586112015-11-09 16:37:23 -0800386 } catch (DeviceConfigNotFoundException e) {
387 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
Charles Chan12a8a842020-02-14 13:23:57 -0800388 return CompletableFuture.completedFuture(null);
Charles Chanf4586112015-11-09 16:37:23 -0800389 }
Charles Chanea702b12016-11-30 11:55:05 -0800390 if (fwdBuilder == null) {
391 log.warn("Aborting host routing table entries due "
392 + "to error for dev:{} route:{}", deviceId, prefix);
Charles Chan12a8a842020-02-14 13:23:57 -0800393 return CompletableFuture.completedFuture(null);
Charles Chanea702b12016-11-30 11:55:05 -0800394 }
Charles Chan12a8a842020-02-14 13:23:57 -0800395
396 CompletableFuture<Objective> future = new CompletableFuture<>();
Charles Chan1eaf4802016-04-18 13:44:03 -0700397 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan12a8a842020-02-14 13:23:57 -0800398 (objective) -> {
399 log.debug("IP rule for route {} revoked", prefix);
400 future.complete(objective);
401 },
402 (objective, error) -> {
403 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error);
404 future.complete(null);
405 });
Charles Chan1eaf4802016-04-18 13:44:03 -0700406 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan12a8a842020-02-14 13:23:57 -0800407 return future;
Charles Chanf4586112015-11-09 16:37:23 -0800408 }
409
Charles Chanddac7fd2016-10-27 14:19:48 -0700410 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800411 * Returns a forwarding objective builder for routing rules.
412 * <p>
413 * The forwarding objective routes packets destined to a given prefix to
414 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700415 *
416 * @param deviceId device ID
417 * @param prefix prefix that need to be routed
418 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800419 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700420 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700421 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000422 * @param directHost host is direct or indirect
Charles Chanddac7fd2016-10-27 14:19:48 -0700423 * @return forwarding objective builder
424 * @throws DeviceConfigNotFoundException if given device is not configured
425 */
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000426
Charles Chan61c086d2019-07-26 17:46:15 -0700427 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700428 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700429 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
Charles Chan61c086d2019-07-26 17:46:15 -0700430 VlanId innerVlan, EthType outerTpid,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000431 boolean directHost, boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800432 throws DeviceConfigNotFoundException {
Charles Chan61c086d2019-07-26 17:46:15 -0700433 int nextObjId;
434 if (directHost) {
435 // if the objective is to revoke an existing rule, and for some reason
436 // the next-objective does not exist, then a new one should not be created
437 ImmutablePair<TrafficTreatment, TrafficSelector> treatmentAndMeta =
438 getTreatmentAndMeta(deviceId, hostMac, hostVlanId, outPort, innerVlan, outerTpid);
439 if (treatmentAndMeta == null) {
440 // Warning log will come from getTreatmentAndMeta method
441 return null;
442 }
443 nextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
444 treatmentAndMeta.getLeft(), treatmentAndMeta.getRight(), !revoke);
445 } else {
446 // if the objective is to revoke an existing rule, and for some reason
447 // the next-objective does not exist, then a new one should not be created
448 nextObjId = srManager.getMacVlanNextObjectiveId(deviceId, hostMac, hostVlanId,
449 outPort, !revoke);
450 }
451 if (nextObjId == -1) {
452 // Warning log will come from getMacVlanNextObjective method
453 return null;
454 }
455
456 return DefaultForwardingObjective.builder()
457 .withSelector(buildIpSelectorFromIpPrefix(prefix).build())
458 .nextStep(nextObjId)
459 .fromApp(srManager.appId).makePermanent()
460 .withPriority(getPriorityFromPrefix(prefix))
461 .withFlag(ForwardingObjective.Flag.SPECIFIC);
462 }
463
464 private ImmutablePair<TrafficTreatment, TrafficSelector> getTreatmentAndMeta(
465 DeviceId deviceId, MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
466 VlanId innerVlan, EthType outerTpid)
467 throws DeviceConfigNotFoundException {
468 MacAddress routerMac;
469 routerMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800470
Charles Chan90772a72017-02-08 15:52:08 -0800471 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
Charles Chan098ca202018-05-01 11:50:20 -0700472 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
473 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
474 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700475
Charles Chan90772a72017-02-08 15:52:08 -0800476 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700477 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700478 tbuilder.deferred()
479 .setEthDst(hostMac)
Charles Chan61c086d2019-07-26 17:46:15 -0700480 .setEthSrc(routerMac)
sangho80f11cb2015-04-01 13:05:26 -0700481 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800482
Charles Chan90772a72017-02-08 15:52:08 -0800483 // Create route meta
484 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800485
Charles Chan61c086d2019-07-26 17:46:15 -0700486 // Adjust treatment and meta according to VLAN configuration
Charles Chan90772a72017-02-08 15:52:08 -0800487 if (taggedVlans.contains(hostVlanId)) {
488 tbuilder.setVlanId(hostVlanId);
489 } else if (hostVlanId.equals(VlanId.NONE)) {
490 if (untaggedVlan != null) {
491 mbuilder.matchVlanId(untaggedVlan);
492 } else if (nativeVlan != null) {
493 mbuilder.matchVlanId(nativeVlan);
494 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700495 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
496 hostMac, hostVlanId, connectPoint);
497 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800498 }
499 } else {
Charles Chan61c086d2019-07-26 17:46:15 -0700500 // Double tagged hosts
501 if (innerVlan == null || outerTpid == null) {
502 log.warn("Failed to construct NextObj for double tagged hosts {}/{}. {} {}",
503 hostMac, hostVlanId,
504 (innerVlan == null) ? "innerVlan = null." : "",
505 (outerTpid == null) ? "outerTpid = null." : "");
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700506 return null;
507 }
Charles Chan61c086d2019-07-26 17:46:15 -0700508 tbuilder.setVlanId(innerVlan);
509 tbuilder.pushVlan(outerTpid);
510 tbuilder.setVlanId(hostVlanId);
511 mbuilder.matchVlanId(VlanId.ANY);
Saurav Das07c74602016-04-27 18:35:50 -0700512 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000513
Charles Chan61c086d2019-07-26 17:46:15 -0700514 return ImmutablePair.of(tbuilder.build(), mbuilder.build());
sangho80f11cb2015-04-01 13:05:26 -0700515 }
516
517 /**
Saurav Das261c3002017-06-13 15:35:54 -0700518 * Populates IP flow rules for all the given prefixes reachable from the
519 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700520 *
Saurav Das261c3002017-06-13 15:35:54 -0700521 * @param targetSw switch where rules are to be programmed
522 * @param subnets subnets/prefixes being added
523 * @param destSw1 destination switch where the prefixes are reachable
524 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
525 * Should be null if there is no paired destination switch (by config)
526 * or if the given prefixes are reachable only via destSw1
527 * @param nextHops a map containing a set of next-hops for each destination switch.
528 * If destSw2 is not null, then this map must contain an
529 * entry for destSw2 with its next-hops from the targetSw
530 * (although the next-hop set may be empty in certain scenarios).
531 * If destSw2 is null, there should not be an entry in this
532 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700533 * @return true if all rules are set successfully, false otherwise
534 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700535 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700536 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
psneha86e60d32019-03-26 06:31:41 -0400537 // Get pair device of the target switch
538 Optional<DeviceId> pairDev = srManager.getPairDeviceId(targetSw);
539 // Route simplification will be off in case of the nexthop location at target switch is down
540 // (routing through spine case)
541 boolean routeSimplOff = pairDev.isPresent() && pairDev.get().equals(destSw1) && destSw2 == null;
piera9941192019-04-24 16:12:47 +0200542 // Iterates over the routes. Checking:
psneha86e60d32019-03-26 06:31:41 -0400543 // If route simplification is enabled
544 // If the target device is another leaf in the network
545 if (srManager.routeSimplification && !routeSimplOff) {
piera9941192019-04-24 16:12:47 +0200546 Set<IpPrefix> subnetsToBePopulated = Sets.newHashSet();
psneha86e60d32019-03-26 06:31:41 -0400547 for (IpPrefix subnet : subnets) {
548 // Skip route programming on the target device
549 // If route simplification applies
550 if (routeSimplifierUtils.hasLeafExclusionEnabledForPrefix(subnet)) {
551 // XXX route simplification assumes that source of the traffic
552 // towards the nexthops are co-located with the nexthops. In different
553 // scenarios will not work properly.
554 continue;
555 }
556 // populate the route in the remaning scenarios
piera9941192019-04-24 16:12:47 +0200557 subnetsToBePopulated.add(subnet);
psneha86e60d32019-03-26 06:31:41 -0400558 }
piera9941192019-04-24 16:12:47 +0200559 subnets = subnetsToBePopulated;
sangho80f11cb2015-04-01 13:05:26 -0700560 }
piera9941192019-04-24 16:12:47 +0200561 // populate the remaining routes in the target switch
562 return populateIpRulesForRouter(targetSw, subnets, destSw1, destSw2, nextHops);
Charles Chanc22cef32016-04-29 14:38:22 -0700563 }
sangho80f11cb2015-04-01 13:05:26 -0700564
Charles Chanc22cef32016-04-29 14:38:22 -0700565 /**
Charles Chand66d6712018-03-29 16:03:41 -0700566 * Revokes IP flow rules for the subnets from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700567 *
Charles Chand66d6712018-03-29 16:03:41 -0700568 * @param targetSw target switch from which the subnets need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700569 * @param subnets subnet being removed
570 * @return true if all rules are removed successfully, false otherwise
571 */
Charles Chand66d6712018-03-29 16:03:41 -0700572 boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700573 for (IpPrefix subnet : subnets) {
Charles Chand66d6712018-03-29 16:03:41 -0700574 if (!revokeIpRuleForRouter(targetSw, subnet)) {
Charles Chanc22cef32016-04-29 14:38:22 -0700575 return false;
576 }
577 }
sangho80f11cb2015-04-01 13:05:26 -0700578 return true;
579 }
580
581 /**
piera9941192019-04-24 16:12:47 +0200582 * Populates IP flow rules for a set of IP prefix in the target device.
583 * The prefix are reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700584 *
Saurav Das261c3002017-06-13 15:35:54 -0700585 * @param targetSw target device ID to set the rules
piera9941192019-04-24 16:12:47 +0200586 * @param subnets the set of IP prefix
Saurav Das261c3002017-06-13 15:35:54 -0700587 * @param destSw1 destination switch where the prefixes are reachable
588 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
589 * Should be null if there is no paired destination switch (by config)
590 * or if the given prefixes are reachable only via destSw1
591 * @param nextHops map of destination switches and their next-hops.
592 * Should only contain destination switches that are
593 * actually meant to be routed to. If destSw2 is null, there
594 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700595 * @return true if all rules are set successfully, false otherwise
596 */
piera9941192019-04-24 16:12:47 +0200597 private boolean populateIpRulesForRouter(DeviceId targetSw,
598 Set<IpPrefix> subnets,
599 DeviceId destSw1, DeviceId destSw2,
600 Map<DeviceId, Set<DeviceId>> nextHops) {
601 // pre-compute the needed information
602 int segmentIdIPv41, segmentIdIPv42 = -1;
603 int segmentIdIPv61, segmentIdIPv62 = -1;
604 TrafficTreatment treatment = null;
605 DestinationSet dsIPv4, dsIPv6;
606 TrafficSelector metaIpv4Selector, metaIpv6Selector = null;
607 int nextIdIPv4, nextIdIPv6, nextId;
608 TrafficSelector selector;
609 // start with MPLS SIDs
Charles Chan319d1a22015-11-03 10:42:14 -0800610 try {
piera9941192019-04-24 16:12:47 +0200611 segmentIdIPv41 = config.getIPv4SegmentId(destSw1);
612 segmentIdIPv61 = config.getIPv6SegmentId(destSw1);
613 if (destSw2 != null) {
614 segmentIdIPv42 = config.getIPv4SegmentId(destSw2);
615 segmentIdIPv62 = config.getIPv6SegmentId(destSw2);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800616 }
Charles Chan319d1a22015-11-03 10:42:14 -0800617 } catch (DeviceConfigNotFoundException e) {
618 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
619 return false;
620 }
piera9941192019-04-24 16:12:47 +0200621 // build the IPv4 and IPv6 destination set
Saurav Das261c3002017-06-13 15:35:54 -0700622 if (destSw2 == null) {
623 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800624 // If the next hop is the same as the final destination, then MPLS
625 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700626 Set<DeviceId> nhd1 = nextHops.get(destSw1);
627 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
piera9941192019-04-24 16:12:47 +0200628 dsIPv4 = DestinationSet.createTypePushNone(destSw1);
629 dsIPv6 = DestinationSet.createTypePushNone(destSw1);
630 treatment = DefaultTrafficTreatment.builder()
631 .immediate()
632 .decNwTtl()
633 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700634 } else {
piera9941192019-04-24 16:12:47 +0200635 dsIPv4 = DestinationSet.createTypePushBos(segmentIdIPv41, destSw1);
636 dsIPv6 = DestinationSet.createTypePushBos(segmentIdIPv61, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700637 }
638 } else {
639 // dst pair - IP rules for dst-pairs are always from other edge nodes
640 // the destination set needs to have both destinations, even if there
641 // are no next hops to one of them
piera9941192019-04-24 16:12:47 +0200642 dsIPv4 = DestinationSet.createTypePushBos(segmentIdIPv41, destSw1, segmentIdIPv42, destSw2);
643 dsIPv6 = DestinationSet.createTypePushBos(segmentIdIPv61, destSw1, segmentIdIPv62, destSw2);
sangho80f11cb2015-04-01 13:05:26 -0700644 }
645
Saurav Das4c35fc42015-11-20 15:27:53 -0800646 // setup metadata to pass to nextObjective - indicate the vlan on egress
647 // if needed by the switch pipeline. Since neighbor sets are always to
648 // other neighboring routers, there is no subnet assigned on those ports.
piera9941192019-04-24 16:12:47 +0200649 metaIpv4Selector = buildIpv4Selector()
650 .matchVlanId(srManager.getDefaultInternalVlan())
651 .build();
652 metaIpv6Selector = buildIpv6Selector()
653 .matchVlanId(srManager.getDefaultInternalVlan())
654 .build();
655 // get the group handler of the target switch
Saurav Das261c3002017-06-13 15:35:54 -0700656 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700657 if (grpHandler == null) {
658 log.warn("populateIPRuleForRouter: groupHandler for device {} "
piera9941192019-04-24 16:12:47 +0200659 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700660 return false;
661 }
piera9941192019-04-24 16:12:47 +0200662 // get next id
663 nextIdIPv4 = grpHandler.getNextObjectiveId(dsIPv4, nextHops, metaIpv4Selector, false);
664 if (nextIdIPv4 <= 0) {
665 log.warn("No next objective in {} for ds: {}", targetSw, dsIPv4);
sangho2165d222015-05-01 09:38:25 -0700666 return false;
667 }
piera9941192019-04-24 16:12:47 +0200668 nextIdIPv6 = grpHandler.getNextObjectiveId(dsIPv6, nextHops, metaIpv6Selector, false);
669 if (nextIdIPv6 <= 0) {
670 log.warn("No next objective in {} for ds: {}", targetSw, dsIPv6);
671 return false;
Charles Chanf4586112015-11-09 16:37:23 -0800672 }
piera9941192019-04-24 16:12:47 +0200673 // build all the flow rules and send to the device
674 for (IpPrefix subnet : subnets) {
675 selector = buildIpSelectorFromIpPrefix(subnet).build();
676 if (subnet.isIp4()) {
677 nextId = nextIdIPv4;
678 } else {
679 nextId = nextIdIPv6;
680 }
681 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
682 .builder()
683 .fromApp(srManager.appId)
684 .makePermanent()
685 .nextStep(nextId)
686 .withSelector(selector)
687 .withPriority(getPriorityFromPrefix(subnet))
688 .withFlag(ForwardingObjective.Flag.SPECIFIC);
689 if (treatment != null) {
690 fwdBuilder.withTreatment(treatment);
691 }
692 log.debug("Installing {} forwarding objective for router IP/subnet {} "
693 + "in switch {} with nextId: {}", subnet.isIp4() ? "IPv4" : "IPv6",
694 subnet, targetSw, nextId);
695 ObjectiveContext context = new DefaultObjectiveContext(
696 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
697 subnet, targetSw),
698 (objective, error) -> log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
699 subnet, error, targetSw));
700 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
701 }
702 rulePopulationCounter.addAndGet(subnets.size());
sangho80f11cb2015-04-01 13:05:26 -0700703 return true;
704 }
705
sangho80f11cb2015-04-01 13:05:26 -0700706 /**
Charles Chand66d6712018-03-29 16:03:41 -0700707 * Revokes IP flow rules for the router IP address from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700708 *
Charles Chand66d6712018-03-29 16:03:41 -0700709 * @param targetSw target switch from which the ipPrefix need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700710 * @param ipPrefix the IP address of the destination router
711 * @return true if all rules are removed successfully, false otherwise
712 */
Charles Chand66d6712018-03-29 16:03:41 -0700713 private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800714 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700715 TrafficSelector selector = sbuilder.build();
716 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
717
718 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
719 .builder()
720 .fromApp(srManager.appId)
721 .makePermanent()
722 .withSelector(selector)
723 .withTreatment(dummyTreatment)
724 .withPriority(getPriorityFromPrefix(ipPrefix))
725 .withFlag(ForwardingObjective.Flag.SPECIFIC);
726
Charles Chand66d6712018-03-29 16:03:41 -0700727 ObjectiveContext context = new DefaultObjectiveContext(
728 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw),
729 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
730 ipPrefix, targetSw, error));
731 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context));
Charles Chanc22cef32016-04-29 14:38:22 -0700732
733 return true;
734 }
735
736 /**
Saurav Das97241862018-02-14 14:14:54 -0800737 * Populates MPLS flow rules in the target device to point towards the
738 * destination device.
739 *
740 * @param targetSwId target device ID of the switch to set the rules
741 * @param destSwId destination switch device ID
742 * @param nextHops next hops switch ID list
743 * @param routerIp the router ip of the destination switch
744 * @return true if all rules are set successfully, false otherwise
745 */
746 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
747 Set<DeviceId> nextHops, IpAddress routerIp) {
748 int segmentId;
749 try {
750 if (routerIp.isIp4()) {
751 segmentId = config.getIPv4SegmentId(destSwId);
752 } else {
753 segmentId = config.getIPv6SegmentId(destSwId);
754 }
755 } catch (DeviceConfigNotFoundException e) {
756 log.warn(e.getMessage() + " Aborting populateMplsRule.");
757 return false;
758 }
759
760 List<ForwardingObjective> fwdObjs = new ArrayList<>();
761 Collection<ForwardingObjective> fwdObjsMpls;
762 // Generates the transit rules used by the standard "routing".
763 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
764 routerIp, true);
765 if (fwdObjsMpls.isEmpty()) {
766 return false;
767 }
768 fwdObjs.addAll(fwdObjsMpls);
769
770 // Generates the transit rules used by the MPLS Pwaas.
771 int pwSrLabel;
772 try {
773 pwSrLabel = config.getPWRoutingLabel(destSwId);
774 } catch (DeviceConfigNotFoundException e) {
775 log.warn(e.getMessage()
776 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
777 return false;
778 }
779 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
780 routerIp, false);
781 if (fwdObjsMpls.isEmpty()) {
782 return false;
783 }
784 fwdObjs.addAll(fwdObjsMpls);
785
786 for (ForwardingObjective fwdObj : fwdObjs) {
787 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
788 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
789 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
790 rulePopulationCounter.incrementAndGet();
791 }
792
793 return true;
794 }
795
796 /**
797 * Differentiates between popping and swapping labels when building an MPLS
798 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700799 *
800 * @param targetSwId the target sw
801 * @param destSwId the destination sw
802 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800803 * @param segmentId the segmentId to match representing the destination
804 * switch
805 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700806 * @return a collection of fwdobjective
807 */
Saurav Das261c3002017-06-13 15:35:54 -0700808 private Collection<ForwardingObjective> handleMpls(
809 DeviceId targetSwId,
810 DeviceId destSwId,
811 Set<DeviceId> nextHops,
812 int segmentId,
813 IpAddress routerIp,
814 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700815
816 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
817 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800818 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700819 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
820 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
821 sbuilder.matchMplsBos(isMplsBos);
822 TrafficSelector selector = sbuilder.build();
823
824 // setup metadata to pass to nextObjective - indicate the vlan on egress
825 // if needed by the switch pipeline. Since mpls next-hops are always to
826 // other neighboring routers, there is no subnet assigned on those ports.
827 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Saurav Das9bf49582018-08-13 15:34:26 -0700828 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700829
830 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
831 // If the next hop is the destination router for the segment, do pop
832 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700833 + "label {} in switch {} with pop to next-hops {}",
834 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700835 ForwardingObjective.Builder fwdObjNoBosBuilder =
836 getMplsForwardingObjective(targetSwId,
837 nextHops,
838 true,
839 isMplsBos,
840 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700841 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800842 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700843 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700844 // Error case, we cannot handle, exit.
845 if (fwdObjNoBosBuilder == null) {
846 return Collections.emptyList();
847 }
848 fwdObjBuilders.add(fwdObjNoBosBuilder);
849
850 } else {
Saurav Das97241862018-02-14 14:14:54 -0800851 // next hop is not destination, irrespective of the number of next
852 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700853 log.debug("Installing MPLS forwarding objective for "
854 + "label {} in switch {} without pop to next-hops {}",
855 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700856 ForwardingObjective.Builder fwdObjNoBosBuilder =
857 getMplsForwardingObjective(targetSwId,
858 nextHops,
859 false,
860 isMplsBos,
861 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700862 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800863 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700864 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700865 // Error case, we cannot handle, exit.
866 if (fwdObjNoBosBuilder == null) {
867 return Collections.emptyList();
868 }
869 fwdObjBuilders.add(fwdObjNoBosBuilder);
870
871 }
872
873 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
874 // We add the final property to the fwdObjs.
875 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700876 ((Builder) ((Builder) fwdObjBuilder
877 .fromApp(srManager.appId)
878 .makePermanent())
879 .withSelector(selector)
880 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
881 .withFlag(ForwardingObjective.Flag.SPECIFIC);
882
883 ObjectiveContext context = new DefaultObjectiveContext(
884 (objective) ->
885 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
886 objective.id(), segmentId, targetSwId),
887 (objective, error) ->
888 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
889 objective.id(), segmentId, error, targetSwId));
890
891 ForwardingObjective fob = fwdObjBuilder.add(context);
892 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700893 }
894
895 return fwdObjs;
896 }
897
898 /**
Saurav Das97241862018-02-14 14:14:54 -0800899 * Returns a Forwarding Objective builder for the MPLS rule that references
900 * the desired Next Objective. Creates a DestinationSet that allows the
901 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700902 *
Saurav Das97241862018-02-14 14:14:54 -0800903 * @param targetSw the target sw
904 * @param nextHops the set of next hops
905 * @param phpRequired true if penultimate-hop-popping is required
906 * @param isBos true if matched label is bottom-of-stack
907 * @param meta metadata for creating next objective
908 * @param routerIp the router ip representing the destination switch
909 * @param destSw the destination sw
910 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700911 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800912 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700913 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800914 Set<DeviceId> nextHops,
915 boolean phpRequired,
916 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800917 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700918 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800919 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700920 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800921
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700922 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
923 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700924
925 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800926 DestinationSet ds = null;
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700927 DestinationSet.DestinationSetType dstType = null;
Saurav Das97241862018-02-14 14:14:54 -0800928 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700929 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800930 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700931 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700932 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700933 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800934 if (routerIp.isIp4()) {
935 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
936 } else {
937 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
938 }
939 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800940 // standard case -> BoS == True; pop results in IP packet and forwarding
941 // is via an ECMP group
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700942 ds = DestinationSet.createTypePopBos(destSw);
sangho80f11cb2015-04-01 13:05:26 -0700943 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800944 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
945 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800946 // double-label case -> BoS == False, pop results in MPLS packet
947 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700948 ds = DestinationSet.createTypePopNotBos(destSw);
949 if (!srManager.getMplsEcmp()) {
950 simple = true;
Saurav Das97241862018-02-14 14:14:54 -0800951 }
sangho80f11cb2015-04-01 13:05:26 -0700952 }
953 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800954 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800955 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700956 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800957 // swap results in MPLS packet with same BoS bit regardless of bit value
958 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700959 // differentiate here between swap with not bos or swap with bos
960 ds = isBos ? DestinationSet.createTypeSwapBos(segmentId, destSw) :
961 DestinationSet.createTypeSwapNotBos(segmentId, destSw);
962 if (!srManager.getMplsEcmp()) {
Saurav Das97241862018-02-14 14:14:54 -0800963 simple = true;
964 }
sangho80f11cb2015-04-01 13:05:26 -0700965 }
966
Saurav Das4c35fc42015-11-20 15:27:53 -0800967 fwdBuilder.withTreatment(tbuilder.build());
Saurav Das97241862018-02-14 14:14:54 -0800968 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
969 targetSw, ds);
Saurav Das261c3002017-06-13 15:35:54 -0700970 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
971 if (gh == null) {
972 log.warn("getNextObjectiveId query - groupHandler for device {} "
973 + "not found", targetSw);
974 return null;
975 }
Saurav Das97241862018-02-14 14:14:54 -0800976
Saurav Das261c3002017-06-13 15:35:54 -0700977 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
978 dstNextHops.put(destSw, nextHops);
Saurav Das97241862018-02-14 14:14:54 -0800979 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das4c35fc42015-11-20 15:27:53 -0800980 if (nextId <= 0) {
Saurav Das97241862018-02-14 14:14:54 -0800981 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das4c35fc42015-11-20 15:27:53 -0800982 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700983 } else {
Saurav Das97241862018-02-14 14:14:54 -0800984 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
985 nextId, targetSw, ds);
sangho80f11cb2015-04-01 13:05:26 -0700986 }
987
Saurav Das4c35fc42015-11-20 15:27:53 -0800988 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700989 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700990 }
991
992 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700993 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700994 * dstMac corresponding to the router's MAC address. For those pipelines
995 * that need to internally assign vlans to untagged packets, this method
996 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700997 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800998 * Note that the vlan assignment and filter programming should only be done by
999 * the master for a switch. This method is typically called at deviceAdd and
1000 * programs filters only for the enabled ports of the device. For port-updates,
1001 * that enable/disable ports after device add, singlePortFilter methods should
1002 * be called.
sangho80f11cb2015-04-01 13:05:26 -07001003 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001004 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -08001005 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -07001006 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001007 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -07001008 log.debug("Installing per-port filtering objective for untagged "
1009 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -08001010
Saurav Das07c74602016-04-27 18:35:50 -07001011 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -08001012 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -07001013 log.warn("Device {} ports not available. Unable to add MacVlan filters",
1014 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -08001015 return null;
Saurav Das07c74602016-04-27 18:35:50 -07001016 }
Saurav Dasf9332192017-02-18 14:05:44 -08001017 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -07001018 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -07001019 if (!port.isEnabled()) {
1020 disabledPorts++;
1021 continue;
1022 }
Charles Chan43be46b2017-02-26 22:59:35 -08001023 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001024 filteredPorts++;
1025 } else {
1026 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -07001027 }
Saurav Das7c305372015-10-28 12:39:42 -07001028 }
Charles Chan077314e2017-06-22 14:27:17 -07001029 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -08001030 deviceId, disabledPorts, errorPorts, filteredPorts);
Charles Chan9d2dd552018-06-19 20:56:33 -07001031 return new PortFilterInfo(disabledPorts, errorPorts, filteredPorts);
Saurav Dasf9332192017-02-18 14:05:44 -08001032 }
1033
1034 /**
Charles Chan43be46b2017-02-26 22:59:35 -08001035 * Creates or removes filtering objectives for a single port. Should only be
1036 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -08001037 *
1038 * @param deviceId device identifier
1039 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -08001040 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -08001041 * @return true if no errors occurred during the build of the filtering objective
1042 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001043 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -08001044 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Charles Chan098ca202018-05-01 11:50:20 -07001045 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
1046 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
1047 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chan90772a72017-02-08 15:52:08 -08001048
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001049 // Do not configure filter for edge ports where double-tagged hosts are connected.
Charles Chan90772a72017-02-08 15:52:08 -08001050 if (taggedVlans.size() != 0) {
1051 // Filter for tagged vlans
Charles Chan098ca202018-05-01 11:50:20 -07001052 if (!srManager.interfaceService.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -08001053 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -08001054 return false;
1055 }
1056 if (nativeVlan != null) {
1057 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -08001058 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001059 return false;
1060 }
1061 }
1062 } else if (untaggedVlan != null) {
1063 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -08001064 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001065 return false;
1066 }
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -07001067 } else if (!hasIPConfiguration(connectPoint)) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001068 // Filter for unconfigured upstream port, using INTERNAL_VLAN
Saurav Das9bf49582018-08-13 15:34:26 -07001069 if (!processSinglePortFiltersInternal(deviceId, portnum, true,
1070 srManager.getDefaultInternalVlan(),
1071 install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001072 return false;
1073 }
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -07001074 // Filter for receiveing pseudowire traffic
Saurav Das9bf49582018-08-13 15:34:26 -07001075 if (!processSinglePortFiltersInternal(deviceId, portnum, false,
1076 srManager.getPwTransportVlan(),
1077 install)) {
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -07001078 return false;
1079 }
Charles Chan90772a72017-02-08 15:52:08 -08001080 }
1081 return true;
1082 }
1083
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001084 /**
1085 * Updates filtering objectives for a single port. Should only be called by
1086 * the master for a switch
1087 * @param deviceId device identifier
1088 * @param portNum port identifier for port to be filtered
1089 * @param pushVlan true to push vlan, false otherwise
1090 * @param vlanId vlan identifier
1091 * @param install true to install the filtering objective, false to remove
1092 */
1093 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
1094 boolean pushVlan, VlanId vlanId, boolean install) {
1095 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
1096 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
1097 deviceId, portNum, vlanId);
1098 }
1099 }
1100
Charles Chan43be46b2017-02-26 22:59:35 -08001101 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
1102 boolean pushVlan, VlanId vlanId, boolean install) {
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001103 boolean doTMAC = true;
1104
1105 if (!pushVlan) {
1106 // Skip the tagged vlans belonging to an interface without an IP address
1107 Set<Interface> ifaces = srManager.interfaceService
1108 .getInterfacesByPort(new ConnectPoint(deviceId, portnum))
1109 .stream()
1110 .filter(intf -> intf.vlanTagged().contains(vlanId) && intf.ipAddressesList().isEmpty())
1111 .collect(Collectors.toSet());
1112 if (!ifaces.isEmpty()) {
1113 log.debug("processSinglePortFiltersInternal: skipping TMAC for vlan {} at {}/{} - no IP",
1114 vlanId, deviceId, portnum);
1115 doTMAC = false;
1116 }
1117 }
1118
1119 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId, doTMAC);
Saurav Dasf9332192017-02-18 14:05:44 -08001120 if (fob == null) {
1121 // error encountered during build
1122 return false;
1123 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001124 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -07001125 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -08001126 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -08001127 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -07001128 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -08001129 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -07001130 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -08001131 if (install) {
1132 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1133 } else {
1134 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -08001135 }
Charles Chan90772a72017-02-08 15:52:08 -08001136 return true;
Saurav Dasf9332192017-02-18 14:05:44 -08001137 }
1138
Charles Chan90772a72017-02-08 15:52:08 -08001139 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001140 boolean pushVlan, VlanId vlanId, boolean doTMAC) {
Saurav Dasf9332192017-02-18 14:05:44 -08001141 MacAddress deviceMac;
1142 try {
1143 deviceMac = config.getDeviceMac(deviceId);
1144 } catch (DeviceConfigNotFoundException e) {
1145 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
1146 return null;
1147 }
Saurav Dasf9332192017-02-18 14:05:44 -08001148 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001149
1150 if (doTMAC) {
1151 fob.withKey(Criteria.matchInPort(portnum))
1152 .addCondition(Criteria.matchEthDst(deviceMac))
1153 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1154 } else {
1155 fob.withKey(Criteria.matchInPort(portnum))
1156 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1157 }
Charles Chan90772a72017-02-08 15:52:08 -08001158
Charles Chan17ca2202017-12-19 19:55:57 -08001159 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1160
Charles Chan90772a72017-02-08 15:52:08 -08001161 if (pushVlan) {
1162 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -08001163 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -08001164 } else {
1165 fob.addCondition(Criteria.matchVlanId(vlanId));
1166 }
1167
Charles Chan17ca2202017-12-19 19:55:57 -08001168 // NOTE: Some switch hardware share the same filtering flow among different ports.
1169 // We use this metadata to let the driver know that there is no more enabled port
1170 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -08001171 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -08001172 tBuilder.wipeDeferred();
1173 }
1174
1175 fob.withMeta(tBuilder.build());
1176
Saurav Dasf9332192017-02-18 14:05:44 -08001177 fob.permit().fromApp(srManager.appId);
1178 return fob;
sangho80f11cb2015-04-01 13:05:26 -07001179 }
1180
1181 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001182 * Creates or removes filtering objectives for a double-tagged host on a port.
1183 *
1184 * @param deviceId device identifier
1185 * @param portNum port identifier for port to be filtered
1186 * @param outerVlan outer VLAN ID
1187 * @param innerVlan inner VLAN ID
1188 * @param install true to install the filtering objective, false to remove
1189 */
1190 void processDoubleTaggedFilter(DeviceId deviceId, PortNumber portNum, VlanId outerVlan,
1191 VlanId innerVlan, boolean install) {
Daniele Moro8fc37b42019-10-29 18:48:35 -07001192 // We should trigger the removal of double tagged rules only when removing
1193 // the filtering objective and no other hosts are connected to the same device port.
Charles Chanf17fade2020-03-08 18:07:19 -07001194 boolean cleanupDoubleTaggedRules = !anyDoubleTaggedHost(deviceId, portNum) && !install;
Daniele Moro8fc37b42019-10-29 18:48:35 -07001195 FilteringObjective.Builder fob = buildDoubleTaggedFilteringObj(deviceId, portNum,
1196 outerVlan, innerVlan,
1197 cleanupDoubleTaggedRules);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001198 if (fob == null) {
1199 // error encountered during build
1200 return;
1201 }
1202 log.debug("{} double-tagged filtering objectives for dev/port: {}/{}",
1203 install ? "Installing" : "Removing", deviceId, portNum);
1204 ObjectiveContext context = new DefaultObjectiveContext(
1205 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portNum,
1206 install ? "installed" : "removed"),
1207 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
1208 install ? "install" : "remove", deviceId, portNum, error));
1209 if (install) {
1210 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1211 } else {
1212 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
1213 }
1214 }
1215
Charles Chanf17fade2020-03-08 18:07:19 -07001216 /**
1217 * Checks if there is any double tagged host attached to given location.
1218 * This method will match on the effective location of a host.
1219 * That is, it will match on auxLocations when auxLocations is not null. Otherwise, it will match on locations.
1220 *
1221 * @param deviceId device ID
1222 * @param portNum port number
1223 * @return true if there is any host attached to given location.
1224 */
1225 private boolean anyDoubleTaggedHost(DeviceId deviceId, PortNumber portNum) {
1226 ConnectPoint cp = new ConnectPoint(deviceId, portNum);
1227 Set<Host> connectedHosts = srManager.hostService.getConnectedHosts(cp, false);
1228 Set<Host> auxConnectedHosts = srManager.hostService.getConnectedHosts(cp, true);
1229 return !auxConnectedHosts.isEmpty() ||
1230 connectedHosts.stream().anyMatch(host -> host.auxLocations() == null);
1231 }
1232
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001233 private FilteringObjective.Builder buildDoubleTaggedFilteringObj(DeviceId deviceId, PortNumber portNum,
Andreas Pantelopoulos59bd97e2018-06-28 17:06:14 -07001234 VlanId outerVlan, VlanId innerVlan,
1235 boolean cleanupDoubleTaggedRules) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001236 MacAddress deviceMac;
1237 try {
1238 deviceMac = config.getDeviceMac(deviceId);
1239 } catch (DeviceConfigNotFoundException e) {
1240 log.warn(e.getMessage() + " Processing DoubleTaggedFilters aborted");
1241 return null;
1242 }
1243 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1244 // Outer vlan id match should be appeared before inner vlan id match.
1245 fob.withKey(Criteria.matchInPort(portNum))
1246 .addCondition(Criteria.matchEthDst(deviceMac))
1247 .addCondition(Criteria.matchVlanId(outerVlan))
Daniele Moro998f2df2019-07-12 17:58:54 -07001248 .addCondition(Criteria.matchInnerVlanId(innerVlan))
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001249 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1250
1251 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1252 // Pop outer vlan
1253 tBuilder.popVlan();
1254
Andreas Pantelopoulos59bd97e2018-06-28 17:06:14 -07001255 // special metadata for driver
1256 if (cleanupDoubleTaggedRules) {
1257 tBuilder.writeMetadata(CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES, DOUBLE_TAGGED_METADATA_MASK);
1258 } else {
1259 tBuilder.writeMetadata(0, DOUBLE_TAGGED_METADATA_MASK);
1260 }
1261
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001262 // NOTE: Some switch hardware share the same filtering flow among different ports.
1263 // We use this metadata to let the driver know that there is no more enabled port
1264 // within the same VLAN on this device.
1265 if (noMoreEnabledPort(deviceId, outerVlan)) {
1266 tBuilder.wipeDeferred();
1267 }
1268
1269 fob.withMeta(tBuilder.build());
1270
1271 fob.permit().fromApp(srManager.appId);
1272 return fob;
1273 }
1274
1275 /**
Danny Chang44a7b952020-09-08 15:27:04 -07001276 * Creates packet requests to punt all IP packets for the router.
1277 * @param deviceId the switch dpid for the router
1278 */
1279 void populateIpPunts(DeviceId deviceId) {
1280 manageIpPunts(deviceId, true);
1281 }
1282
1283 /**
1284 * Creates a packet request to punt all IP packets, destined to the
1285 * specified IP address, which should be router's port IP address.
1286 *
1287 * @param deviceId the switch dpid for the router
1288 * @param ipAddress the IP address of the router's port
1289 */
1290 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1291 manageSingleIpPunts(deviceId, ipAddress, true);
1292 }
1293
1294 /**
1295 * Revokes packet requests for all devices.
1296 */
1297 void revokePacketsPunts() {
1298 srManager.deviceService.getDevices().forEach(device -> {
1299 manageIpPunts(device.id(), false);
1300 });
1301 }
1302
1303 /**
1304 * Revokes the packet request to punt all IP packets, destined to the
1305 * router's port IP address, except for the router's IP address.
1306 *
1307 * @param deviceId the switch dpid for the router
1308 * @param ipAddress the IP address of the router's port
1309 */
1310 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1311 try {
1312 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
1313 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
1314 manageSingleIpPunts(deviceId, ipAddress, false);
1315 }
1316 } catch (DeviceConfigNotFoundException e) {
1317 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
1318 }
1319 }
1320
1321 /**
1322 * Creates or removes forwarding objectives ( packet-requests ) to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -07001323 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -07001324 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -07001325 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -07001326 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001327 * @param deviceId the switch dpid for the router
Danny Chang44a7b952020-09-08 15:27:04 -07001328 * @param request true to create a packet request, false to remove
sangho80f11cb2015-04-01 13:05:26 -07001329 */
Danny Chang44a7b952020-09-08 15:27:04 -07001330 void manageIpPunts(DeviceId deviceId, boolean request) {
Saurav Das261c3002017-06-13 15:35:54 -07001331 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -08001332 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -08001333 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -08001334 routerIpv4 = config.getRouterIpv4(deviceId);
1335 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -08001336 routerLinkLocalIpv6 = Ip6Address.valueOf(
1337 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
1338
Saurav Das261c3002017-06-13 15:35:54 -07001339 if (config.isPairedEdge(deviceId)) {
1340 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
1341 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
1342 }
Charles Chan319d1a22015-11-03 10:42:14 -08001343 } catch (DeviceConfigNotFoundException e) {
Danny Chang44a7b952020-09-08 15:27:04 -07001344 log.warn(e.getMessage() + " Aborting manageIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -08001345 return;
1346 }
1347
Danny Chang44a7b952020-09-08 15:27:04 -07001348 if (request && !srManager.mastershipService.isLocalMaster(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001349 log.debug("Not installing port-IP punts - not the master for dev:{} ",
1350 deviceId);
1351 return;
1352 }
Danny Chang44a7b952020-09-08 15:27:04 -07001353
Pier Ventreadb4ae62016-11-23 09:57:42 -08001354 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
1355 allIps.add(routerIpv4);
1356 if (routerIpv6 != null) {
1357 allIps.add(routerIpv6);
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001358 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001359 }
Saurav Das261c3002017-06-13 15:35:54 -07001360 if (pairRouterIpv4 != null) {
1361 allIps.add(pairRouterIpv4);
1362 }
1363 if (pairRouterIpv6 != null) {
1364 allIps.add(pairRouterIpv6);
1365 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001366 for (IpAddress ipaddr : allIps) {
Danny Chang44a7b952020-09-08 15:27:04 -07001367 manageSingleIpPunts(deviceId, ipaddr, request);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001368 }
1369 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -08001370
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001371 /**
Danny Chang44a7b952020-09-08 15:27:04 -07001372 * Creates or removes a forwarding objective ( packet-request ) to punt all IP packets, destined to the
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001373 * specified IP address, which should be router's port IP address.
1374 *
1375 * @param deviceId the switch dpid for the router
1376 * @param ipAddress the IP address of the router's port
Danny Chang44a7b952020-09-08 15:27:04 -07001377 * @param request true to create a packet request, false to remove
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001378 */
Danny Chang44a7b952020-09-08 15:27:04 -07001379 void manageSingleIpPunts(DeviceId deviceId, IpAddress ipAddress, boolean request) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001380 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1381 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1382
Danny Chang44a7b952020-09-08 15:27:04 -07001383 if (request) {
1384 srManager.packetService.requestPackets(sbuilder.build(),
1385 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1386 } else {
1387 srManager.packetService.cancelPackets(sbuilder.build(),
1388 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das9f1c42e2015-10-23 10:51:11 -07001389 }
sangho80f11cb2015-04-01 13:05:26 -07001390 }
1391
piera9941192019-04-24 16:12:47 +02001392 // Method for building an IPv4 selector
1393 private TrafficSelector.Builder buildIpv4Selector() {
1394 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
1395 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1396 return selectorBuilder;
1397 }
1398
1399 // Method for building an IPv6 selector
1400 private TrafficSelector.Builder buildIpv6Selector() {
1401 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
1402 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1403 return selectorBuilder;
1404 }
1405
1406 // Method for building an IPv4 or IPv6 selector from an IP address
Pier Ventreadb4ae62016-11-23 09:57:42 -08001407 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
1408 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
1409 }
1410
piera9941192019-04-24 16:12:47 +02001411 // Method for building an IPv4 or IPv6 selector from an IP prefix
Pier Ventreadb4ae62016-11-23 09:57:42 -08001412 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
1413 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -07001414 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -08001415 if (prefixToMatch.isIp4()) {
1416 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1417 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
1418 return selectorBuilder;
1419 }
Pier Ventre229fd0b2016-10-31 16:49:19 -07001420 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -08001421 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1422 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
1423 return selectorBuilder;
1424 }
1425
1426 /**
Pier Luigib9632ba2017-01-12 18:14:58 -08001427 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
1428 * Furthermore, these are applied only by the master instance. Deferred actions
1429 * are not cleared such that packets can be flooded in the cross connect use case
1430 *
1431 * @param deviceId the switch dpid for the router
1432 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001433 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001434 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -08001435 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1436 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
1437 deviceId);
1438 return;
1439 }
1440
Charles Chan3ed34d82017-06-22 18:03:14 -07001441 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001442 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001443 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001444 .add(new ObjectiveContext() {
1445 @Override
1446 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001447 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001448 deviceId, error);
1449 }
1450 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001451 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001452
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001453 if (isIpv6Configured(deviceId)) {
1454 // We punt all NDP packets towards the controller.
1455 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1456 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1457 @Override
1458 public void onError(Objective objective, ObjectiveError error) {
1459 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1460 deviceId, error);
1461 }
1462 });
1463 srManager.flowObjectiveService.forward(deviceId, obj);
Charles Chan051490d2018-01-11 11:48:18 -08001464 });
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001465 }
Charles Chan3ed34d82017-06-22 18:03:14 -07001466
Saurav Dasec683dc2018-04-27 18:42:30 -07001467 srManager.getPairLocalPort(deviceId).ifPresent(port -> {
Charles Chan3ed34d82017-06-22 18:03:14 -07001468 ForwardingObjective pairFwdObj;
1469 // Do not punt ARP packets from pair port
1470 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1471 .add(new ObjectiveContext() {
1472 @Override
1473 public void onError(Objective objective, ObjectiveError error) {
1474 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1475 deviceId, error);
1476 }
1477 });
1478 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1479
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001480 if (isIpv6Configured(deviceId)) {
1481 // Do not punt NDP packets from pair port
1482 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1483 ForwardingObjective obj = builder.add(new ObjectiveContext() {
Charles Chan3ed34d82017-06-22 18:03:14 -07001484 @Override
1485 public void onError(Objective objective, ObjectiveError error) {
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001486 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
Charles Chan3ed34d82017-06-22 18:03:14 -07001487 deviceId, error);
1488 }
1489 });
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001490 srManager.flowObjectiveService.forward(deviceId, obj);
1491 });
1492
1493 // Do not forward DAD packets from pair port
1494 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1495 .add(new ObjectiveContext() {
1496 @Override
1497 public void onError(Objective objective, ObjectiveError error) {
1498 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1499 deviceId, error);
1500 }
1501 });
1502 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1503 }
Charles Chan3ed34d82017-06-22 18:03:14 -07001504 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001505 }
1506
Charles Chan3ed34d82017-06-22 18:03:14 -07001507 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1508 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001509 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001510 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001511 .withSelector(selector)
1512 .fromApp(srManager.appId)
1513 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001514 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001515 .makePermanent();
1516 }
1517
Charles Chan3ed34d82017-06-22 18:03:14 -07001518 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001519 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1520 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001521 if (port != null) {
1522 sBuilder.matchInPort(port);
1523 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001524
Charles Chan3ed34d82017-06-22 18:03:14 -07001525 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1526 if (punt) {
1527 tBuilder.punt();
1528 }
1529 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001530 }
1531
Charles Chan051490d2018-01-11 11:48:18 -08001532 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1533 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001534
Charles Chan051490d2018-01-11 11:48:18 -08001535 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1536 .forEach(type -> {
1537 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1538 sBuilder.matchEthType(TYPE_IPV6)
1539 .matchIPProtocol(PROTOCOL_ICMP6)
1540 .matchIcmpv6Type(type);
1541 if (port != null) {
1542 sBuilder.matchInPort(port);
1543 }
1544
1545 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1546 if (punt) {
1547 tBuilder.punt();
1548 }
1549
1550 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1551 });
1552
1553 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001554 }
1555
1556 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1557 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1558 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001559 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1560 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1561 // .matchIPProtocol(PROTOCOL_ICMP6)
1562 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001563 if (port != null) {
1564 sBuilder.matchInPort(port);
1565 }
1566
1567 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1568 tBuilder.wipeDeferred();
1569 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001570 }
1571
1572 /**
Charles Chan155ec442018-09-16 14:30:19 -07001573 * Block given prefix in routing table.
Charles Chanf4586112015-11-09 16:37:23 -08001574 *
Andrea Campanella60ce2222018-04-30 11:48:55 +02001575 * @param address the address to block
1576 * @param deviceId switch ID to set the rules
1577 */
1578 void populateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1579 updateDefaultRouteBlackhole(deviceId, address, true);
1580 }
1581
1582 /**
Charles Chan155ec442018-09-16 14:30:19 -07001583 * Unblock given prefix in routing table.
Andrea Campanella60ce2222018-04-30 11:48:55 +02001584 *
1585 * @param address the address to block
1586 * @param deviceId switch ID to set the rules
1587 */
1588 void removeDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1589 updateDefaultRouteBlackhole(deviceId, address, false);
1590 }
1591
1592 private void updateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address, boolean install) {
1593 try {
1594 if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) {
1595
1596 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1597 if (address.isIp4()) {
1598 sbuilder.matchIPDst(address);
1599 sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort());
1600 } else {
1601 sbuilder.matchIPv6Dst(address);
1602 sbuilder.matchEthType(EthType.EtherType.IPV6.ethType().toShort());
1603 }
1604
1605 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Andrea Campanella60ce2222018-04-30 11:48:55 +02001606 tBuilder.wipeDeferred();
1607
1608 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1609 fob.withFlag(Flag.SPECIFIC)
1610 .withSelector(sbuilder.build())
1611 .withTreatment(tBuilder.build())
1612 .withPriority(getPriorityFromPrefix(address))
1613 .fromApp(srManager.appId)
1614 .makePermanent();
1615
1616 log.debug("{} blackhole forwarding objectives for dev: {}",
1617 install ? "Installing" : "Removing", deviceId);
1618 ObjectiveContext context = new DefaultObjectiveContext(
1619 (objective) -> log.debug("Forward for {} {}", deviceId,
1620 install ? "installed" : "removed"),
1621 (objective, error) -> log.warn("Failed to {} forward for {}: {}",
1622 install ? "install" : "remove", deviceId, error));
1623 if (install) {
1624 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1625 } else {
1626 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1627 }
1628 }
1629 } catch (DeviceConfigNotFoundException e) {
1630 log.info("Not populating blackhole for un-configured device {}", deviceId);
1631 }
1632
1633 }
1634
1635 /**
1636 * Populates a forwarding objective to send packets that miss other high
1637 * priority Bridging Table entries to a group that contains all ports of
1638 * its subnet.
1639 *
Charles Chanf4586112015-11-09 16:37:23 -08001640 * @param deviceId switch ID to set the rules
1641 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001642 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001643 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001644 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001645 });
1646 }
1647
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001648 /**
1649 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1650 * @param deviceId switch ID to set the rule
1651 * @param vlanId vlan ID to specify the subnet
1652 * @param install true to install the rule, false to revoke the rule
1653 */
1654 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1655 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1656
1657 if (nextId < 0) {
1658 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1659 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1660 return;
1661 }
1662
1663 // Driver should treat objective with MacAddress.NONE as the
1664 // subnet broadcast rule
1665 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1666 sbuilder.matchVlanId(vlanId);
1667 sbuilder.matchEthDst(MacAddress.NONE);
1668
1669 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1670 fob.withFlag(Flag.SPECIFIC)
1671 .withSelector(sbuilder.build())
1672 .nextStep(nextId)
1673 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1674 .fromApp(srManager.appId)
1675 .makePermanent();
1676 ObjectiveContext context = new DefaultObjectiveContext(
1677 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1678 (objective, error) ->
1679 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1680
1681 if (install) {
1682 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1683 } else {
1684 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1685 }
1686 }
1687
Charles Chan82ab1932016-01-30 23:22:37 -08001688 private int getPriorityFromPrefix(IpPrefix prefix) {
1689 return (prefix.isIp4()) ?
1690 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1691 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001692 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001693
1694 /**
1695 * Update Forwarding objective for each host and IP address connected to given port.
1696 * And create corresponding Simple Next objective if it does not exist.
1697 * Applied only when populating Forwarding objective
1698 * @param deviceId switch ID to set the rule
1699 * @param portNumber port number
1700 * @param prefix IP prefix of the route
1701 * @param hostMac MAC address of the next hop
1702 * @param vlanId Vlan ID of the port
1703 * @param popVlan true to pop vlan tag in TrafficTreatment
1704 * @param install true to populate the forwarding objective, false to revoke
1705 */
1706 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1707 VlanId vlanId, boolean popVlan, boolean install) {
1708 ForwardingObjective.Builder fob;
1709 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1710 MacAddress deviceMac;
1711 try {
1712 deviceMac = config.getDeviceMac(deviceId);
1713 } catch (DeviceConfigNotFoundException e) {
1714 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1715 return;
1716 }
1717
1718 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1719 tbuilder.deferred()
1720 .setEthDst(hostMac)
1721 .setEthSrc(deviceMac)
1722 .setOutput(portNumber);
1723
1724 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1725
1726 if (!popVlan) {
1727 tbuilder.setVlanId(vlanId);
1728 } else {
1729 mbuilder.matchVlanId(vlanId);
1730 }
1731
1732 // if the objective is to revoke an existing rule, and for some reason
1733 // the next-objective does not exist, then a new one should not be created
1734 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1735 tbuilder.build(), mbuilder.build(), install);
1736 if (portNextObjId == -1) {
1737 // Warning log will come from getPortNextObjective method
1738 return;
1739 }
1740
1741 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1742 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1743 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1744
1745 ObjectiveContext context = new DefaultObjectiveContext(
1746 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1747 (objective, error) ->
1748 log.warn("Failed to {} IP rule for route {}: {}",
1749 install ? "install" : "revoke", prefix, error));
1750 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1751
1752 if (!install) {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001753 if (!srManager.getVlanPortMap(deviceId).containsKey(vlanId) ||
1754 !srManager.getVlanPortMap(deviceId).get(vlanId).contains(portNumber)) {
1755 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1756 if (grpHandler == null) {
1757 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1758 } else {
1759 // Remove L3UG for the given port and host
1760 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1761 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001762 }
1763 }
1764 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001765
1766 /**
1767 * Checks if there is other enabled port within the given VLAN on the given device.
1768 *
1769 * @param deviceId device ID
1770 * @param vlanId VLAN ID
1771 * @return true if there is no more port enabled within the given VLAN on the given device
1772 */
1773 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1774 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1775 .filter(Port::isEnabled)
1776 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1777 .collect(Collectors.toSet());
1778
1779 return enabledPorts.stream().noneMatch(cp ->
1780 // Given vlanId is included in the vlan-tagged configuration
Charles Chan098ca202018-05-01 11:50:20 -07001781 srManager.interfaceService.getTaggedVlanId(cp).contains(vlanId) ||
pieraac79e92019-10-04 15:40:34 +02001782 // Given vlanId is INTERNAL_VLAN or PSEUDOWIRE_VLAN and the interface is not configured
Charles Chan098ca202018-05-01 11:50:20 -07001783 (srManager.interfaceService.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
pieraac79e92019-10-04 15:40:34 +02001784 (vlanId.equals(srManager.getDefaultInternalVlan()) || vlanId.equals(srManager.getPwTransportVlan()))) ||
Charles Chanf17f66b2018-02-26 21:33:25 -08001785 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1786 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1787 );
1788 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001789
1790 /**
1791 * Returns a forwarding objective builder for egress forwarding rules.
1792 * <p>
1793 * The forwarding objective installs flow rules to egress pipeline to push
1794 * two vlan headers with given inner, outer vlan ids and outer tpid.
1795 *
1796 * @param portNumber port where the next hop attaches to
1797 * @param dummyVlanId vlan ID of the packet to match
1798 * @param innerVlan inner vlan ID of the next hop
1799 * @param outerVlan outer vlan ID of the next hop
1800 * @param outerTpid outer TPID of the next hop
1801 * @return forwarding objective builder
1802 */
1803 private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId,
1804 VlanId innerVlan, VlanId outerVlan, EthType outerTpid) {
1805 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1806 sbuilder.matchVlanId(dummyVlanId);
1807 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1808 tbuilder.setOutput(portNumber).setVlanId(innerVlan);
1809
1810 if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) {
1811 tbuilder.pushVlan(outerTpid);
1812 } else {
1813 tbuilder.pushVlan();
1814 }
1815
1816 tbuilder.setVlanId(outerVlan);
1817 return DefaultForwardingObjective.builder()
1818 .withSelector(sbuilder.build())
1819 .withTreatment(tbuilder.build())
1820 .fromApp(srManager.appId)
1821 .makePermanent()
1822 .withPriority(DEFAULT_PRIORITY)
1823 .withFlag(ForwardingObjective.Flag.EGRESS);
1824 }
1825
1826 /**
1827 * Populates IP rules for a route that has double-tagged next hop.
1828 *
1829 * @param deviceId device ID of the device that next hop attaches to
1830 * @param prefix IP prefix of the route
1831 * @param hostMac MAC address of the next hop
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001832 * @param innerVlan inner Vlan ID of the next hop
1833 * @param outerVlan outer Vlan ID of the next hop
1834 * @param outerTpid outer TPID of the next hop
1835 * @param outPort port where the next hop attaches to
1836 */
Charles Chan61c086d2019-07-26 17:46:15 -07001837 void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001838 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1839 ForwardingObjective.Builder fwdBuilder;
1840 log.debug("Populate direct routing entry for double-tagged host route {} at {}:{}",
1841 prefix, deviceId, outPort);
1842
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001843 try {
Charles Chan61c086d2019-07-26 17:46:15 -07001844 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, outerVlan, outPort, innerVlan, outerTpid,
1845 true, false);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001846 } catch (DeviceConfigNotFoundException e) {
1847 log.error(e.getMessage() + " Aborting populateDoubleTaggedRoute");
1848 return;
1849 }
1850 if (fwdBuilder == null) {
1851 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1852 deviceId, prefix);
1853 return;
1854 }
1855
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001856 int nextId = fwdBuilder.add().nextId();
1857 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1858 log.debug("Direct routing rule for double-tagged host route {} populated. nextId={}", prefix, nextId);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001859 }, (objective, error) ->
1860 log.warn("Failed to populate direct routing rule for double-tagged host route {}: {}", prefix, error)
1861 );
1862 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
1863 rulePopulationCounter.incrementAndGet();
1864 }
1865
1866 /**
1867 * Revokes IP rules for a route that has double-tagged next hop.
1868 *
1869 * @param deviceId device ID of the device that next hop attaches to
1870 * @param prefix IP prefix of the route
1871 * @param hostMac MAC address of the next hop
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001872 * @param innerVlan inner Vlan ID of the next hop
1873 * @param outerVlan outer Vlan ID of the next hop
1874 * @param outerTpid outer TPID of the next hop
1875 * @param outPort port where the next hop attaches to
1876 */
1877 void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -07001878 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1879 ForwardingObjective.Builder fwdBuilder;
1880 log.debug("Revoking direct routing entry for double-tagged host route {} at {}:{}",
1881 prefix, deviceId, outPort);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001882
Charles Chan61c086d2019-07-26 17:46:15 -07001883 try {
1884 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, outerVlan, outPort, innerVlan, outerTpid,
1885 true, true);
1886 } catch (DeviceConfigNotFoundException e) {
1887 log.error(e.getMessage() + " Aborting revokeDoubleTaggedRoute");
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001888 return;
1889 }
Charles Chan61c086d2019-07-26 17:46:15 -07001890 if (fwdBuilder == null) {
1891 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1892 deviceId, prefix);
1893 return;
1894 }
1895
1896 int nextId = fwdBuilder.remove().nextId();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001897 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
Charles Chan61c086d2019-07-26 17:46:15 -07001898 log.debug("Direct routing rule for double-tagged host route {} revoked. nextId={}", prefix, nextId);
1899
1900 // Try to remove next objective as well
1901 ImmutablePair<TrafficTreatment, TrafficSelector> treatmentAndMeta;
1902 try {
1903 treatmentAndMeta = getTreatmentAndMeta(deviceId, hostMac, outerVlan, outPort, innerVlan, outerTpid);
1904 } catch (DeviceConfigNotFoundException e) {
1905 log.error(e.getMessage() + " Aborting revokeDoubleTaggedRoute");
1906 return;
1907 }
1908
1909 if (treatmentAndMeta == null) {
1910 // Warning log will come from getTreatmentAndMeta method
1911 return;
1912 }
1913
1914 DefaultGroupHandler groupHandler = srManager.getGroupHandler(deviceId);
1915 if (groupHandler == null) {
1916 log.warn("Failed to revoke direct routing rule for double-tagged host route {}: " +
1917 "group handler not found for {}", prefix, deviceId);
1918 return;
1919 }
1920 groupHandler.removeGroupFromPort(outPort, treatmentAndMeta.getLeft(), treatmentAndMeta.getRight());
1921
1922 }, (objective, error) ->
1923 log.warn("Failed to revoke direct routing rule for double-tagged host route {}: {}", prefix, error)
1924 );
1925 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001926 }
1927
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -07001928 /**
1929 * Checks whether the specified port has IP configuration or not.
1930 *
1931 * @param cp ConnectPoint to check the existance of IP configuration
1932 * @return true if the port has IP configuration; false otherwise.
1933 */
1934 private boolean hasIPConfiguration(ConnectPoint cp) {
1935 Set<Interface> interfaces = srManager.interfaceService.getInterfacesByPort(cp);
1936 return interfaces.stream().anyMatch(intf -> intf.ipAddressesList().size() > 0);
1937 }
Saurav Das9bf49582018-08-13 15:34:26 -07001938
1939 /**
1940 * Updates filtering rules for unconfigured ports on all devices for which
1941 * this controller instance is master.
1942 *
1943 * @param pushVlan true if the filtering rule requires a push vlan action
1944 * @param oldVlanId the vlanId to be removed
1945 * @param newVlanId the vlanId to be added
1946 */
1947 void updateSpecialVlanFilteringRules(boolean pushVlan, VlanId oldVlanId,
1948 VlanId newVlanId) {
1949 for (Device dev : srManager.deviceService.getAvailableDevices()) {
1950 if (srManager.mastershipService.isLocalMaster(dev.id())) {
1951 for (Port p : srManager.deviceService.getPorts(dev.id())) {
1952 if (!hasIPConfiguration(new ConnectPoint(dev.id(), p.number()))
1953 && p.isEnabled()) {
1954 updateSinglePortFilters(dev.id(), p.number(), pushVlan,
1955 oldVlanId, false);
1956 updateSinglePortFilters(dev.id(), p.number(), pushVlan,
1957 newVlanId, true);
1958 }
1959 }
1960 }
1961 }
1962 }
1963
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001964 private boolean isIpv6Configured(DeviceId deviceId) {
1965 boolean isIpv6Configured;
1966 try {
1967 isIpv6Configured = (config.getRouterIpv6(deviceId) != null);
1968 } catch (DeviceConfigNotFoundException e) {
1969 isIpv6Configured = false;
1970 }
1971 return isIpv6Configured;
1972 }
sangho80f11cb2015-04-01 13:05:26 -07001973}