blob: 9313f3458a5c7da2b7ba0bb2a1bd269ea3a530a1 [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;
41import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070042import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070043import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070045import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070047import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070049import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070051import org.onosproject.net.flow.criteria.Criteria;
52import org.onosproject.net.flowobjective.DefaultFilteringObjective;
53import org.onosproject.net.flowobjective.DefaultForwardingObjective;
54import org.onosproject.net.flowobjective.FilteringObjective;
55import org.onosproject.net.flowobjective.ForwardingObjective;
56import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070057import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070058import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
61import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070062import java.util.Collection;
63import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070064import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070065import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070066import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070067import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080068import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070069import java.util.Set;
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
136 */
137 void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
138 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false);
139 if (fob == null) {
140 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
141 return;
142 }
143
144 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700145 (objective) -> log.debug("Brigding rule for {}/{} populated", mac, vlanId),
146 (objective, error) -> log.warn("Failed to populate bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700147 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
148 }
149
150 /**
151 * Revoke a bridging rule on given deviceId that matches given mac, given vlan and
152 * output to given port.
153 *
154 * @param deviceId device ID
155 * @param port port
156 * @param mac mac address
157 * @param vlanId VLAN ID
158 */
159 void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
160 ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true);
161 if (fob == null) {
162 log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId);
163 return;
164 }
165
166 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das5ff8a3c2018-04-07 16:51:09 -0700167 (objective) -> log.debug("Brigding rule for {}/{} revoked", mac, vlanId),
168 (objective, error) -> log.warn("Failed to revoke bridging rule for {}/{}: {}", mac, vlanId, error));
Charles Chand66d6712018-03-29 16:03:41 -0700169 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
170 }
171
172 /**
173 * Generates a forwarding objective builder for bridging rules.
174 * <p>
175 * The forwarding objective bridges packets destined to a given MAC to
176 * given port on given device.
177 *
178 * @param deviceId Device that host attaches to
179 * @param mac MAC address of the host
180 * @param hostVlanId VLAN ID of the host
181 * @param outport Port that host attaches to
182 * @param revoke true if forwarding objective is meant to revoke forwarding rule
183 * @return Forwarding objective builder
184 */
185 private ForwardingObjective.Builder bridgingFwdObjBuilder(
186 DeviceId deviceId, MacAddress mac, VlanId hostVlanId, PortNumber outport, boolean revoke) {
187 ConnectPoint connectPoint = new ConnectPoint(deviceId, outport);
Charles Chan098ca202018-05-01 11:50:20 -0700188 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
189 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
190 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chand66d6712018-03-29 16:03:41 -0700191
192 // Create host selector
193 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
194 sbuilder.matchEthDst(mac);
195
196 // Create host treatment
197 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
198 tbuilder.immediate().setOutput(outport);
199
200 // Create host meta
201 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
202
203 // Adjust the selector, treatment and meta according to VLAN configuration
204 if (taggedVlans.contains(hostVlanId)) {
205 sbuilder.matchVlanId(hostVlanId);
206 mbuilder.matchVlanId(hostVlanId);
207 } else if (hostVlanId.equals(VlanId.NONE)) {
208 if (untaggedVlan != null) {
209 sbuilder.matchVlanId(untaggedVlan);
210 mbuilder.matchVlanId(untaggedVlan);
211 tbuilder.immediate().popVlan();
212 } else if (nativeVlan != null) {
213 sbuilder.matchVlanId(nativeVlan);
214 mbuilder.matchVlanId(nativeVlan);
215 tbuilder.immediate().popVlan();
216 } else {
217 log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" +
218 "vlan config", mac, hostVlanId, connectPoint);
219 return null;
220 }
221 } else {
222 log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan",
223 mac, hostVlanId, connectPoint);
224 return null;
225 }
226
227 // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed.
228 // If the objective is to revoke an existing rule, and for some reason
229 // the next-objective does not exist, then a new one should not be created
230 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport,
231 tbuilder.build(), mbuilder.build(), !revoke);
232 if (portNextObjId == -1) {
233 // Warning log will come from getPortNextObjective method
234 return null;
235 }
236
237 return DefaultForwardingObjective.builder()
238 .withFlag(ForwardingObjective.Flag.SPECIFIC)
239 .withSelector(sbuilder.build())
240 .nextStep(portNextObjId)
241 .withPriority(100)
242 .fromApp(srManager.appId)
243 .makePermanent();
244 }
245
246 /**
247 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
248 * and hostMAC connected to given port, and output to given port only when
249 * vlan information is valid.
250 *
251 * @param deviceId device ID that host attaches to
252 * @param portNum port number that host attaches to
253 * @param hostMac mac address of the host connected to the switch port
254 * @param vlanId Vlan ID configured on the switch port
255 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
256 * @param install true to populate the objective, false to revoke
257 */
258 // TODO Refactor. There are a lot of duplications between this method, populateBridging,
259 // revokeBridging and bridgingFwdObjBuilder.
260 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
261 VlanId vlanId, boolean popVlan, boolean install) {
262 // Create host selector
263 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
264 sbuilder.matchEthDst(hostMac);
265
266 // Create host meta
267 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
268
269 sbuilder.matchVlanId(vlanId);
270 mbuilder.matchVlanId(vlanId);
271
272 // Create host treatment
273 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
274 tbuilder.immediate().setOutput(portNum);
275
276 if (popVlan) {
277 tbuilder.immediate().popVlan();
278 }
279
280 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
281 tbuilder.build(), mbuilder.build(), install);
282 if (portNextObjId != -1) {
283 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
284 .withFlag(ForwardingObjective.Flag.SPECIFIC)
285 .withSelector(sbuilder.build())
286 .nextStep(portNextObjId)
287 .withPriority(100)
288 .fromApp(srManager.appId)
289 .makePermanent();
290
291 ObjectiveContext context = new DefaultObjectiveContext(
292 (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
293 install ? "populated" : "revoked"),
294 (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
295 install ? "populate" : "revoke", hostMac, vlanId, error));
296 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
297 } else {
298 log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
299 }
300 }
301
302 /**
303 * Populates IP rules for a route that has direct connection to the switch.
304 * This method should not be invoked directly without going through DefaultRoutingHandler.
sangho80f11cb2015-04-01 13:05:26 -0700305 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700306 * @param deviceId device ID of the device that next hop attaches to
307 * @param prefix IP prefix of the route
308 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800309 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700310 * @param outPort port where the next hop attaches to
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000311 * @param directHost host is of type direct or indirect
sangho80f11cb2015-04-01 13:05:26 -0700312 */
Charles Chan910be6a2017-08-23 14:46:43 -0700313 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000314 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Saurav Das261c3002017-06-13 15:35:54 -0700315 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700316 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800317 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800318 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700319 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -0700320 hostVlanId, outPort, null, null, directHost, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800321 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700322 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800323 return;
324 }
Saurav Das07c74602016-04-27 18:35:50 -0700325 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700326 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700327 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700328 return;
329 }
Charles Chan910be6a2017-08-23 14:46:43 -0700330
331 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700332 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700333 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
334 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700335 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700336 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700337 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700338 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800339 rulePopulationCounter.incrementAndGet();
340 }
341
Charles Chanb7f75ac2016-01-11 18:28:54 -0800342 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700343 * Removes IP rules for a route when the next hop is gone.
Charles Chand66d6712018-03-29 16:03:41 -0700344 * This method should not be invoked directly without going through DefaultRoutingHandler.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800345 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700346 * @param deviceId device ID of the device that next hop attaches to
347 * @param prefix IP prefix of the route
348 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800349 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700350 * @param outPort port that next hop attaches to
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000351 * @param directHost host is of type direct or indirect
Charles Chanb7f75ac2016-01-11 18:28:54 -0800352 */
Charles Chan910be6a2017-08-23 14:46:43 -0700353 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000354 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700355 log.debug("Revoke IP table entry for route {} at {}:{}",
356 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800357 ForwardingObjective.Builder fwdBuilder;
358 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700359 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -0700360 hostVlanId, outPort, null, null, directHost, true);
Charles Chanf4586112015-11-09 16:37:23 -0800361 } catch (DeviceConfigNotFoundException e) {
362 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
363 return;
364 }
Charles Chanea702b12016-11-30 11:55:05 -0800365 if (fwdBuilder == null) {
366 log.warn("Aborting host routing table entries due "
367 + "to error for dev:{} route:{}", deviceId, prefix);
368 return;
369 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700370 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700371 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700372 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700373 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700374 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800375 }
376
Charles Chanddac7fd2016-10-27 14:19:48 -0700377 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800378 * Returns a forwarding objective builder for routing rules.
379 * <p>
380 * The forwarding objective routes packets destined to a given prefix to
381 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700382 *
383 * @param deviceId device ID
384 * @param prefix prefix that need to be routed
385 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800386 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700387 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700388 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000389 * @param directHost host is direct or indirect
Charles Chanddac7fd2016-10-27 14:19:48 -0700390 * @return forwarding objective builder
391 * @throws DeviceConfigNotFoundException if given device is not configured
392 */
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000393
Charles Chan61c086d2019-07-26 17:46:15 -0700394 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700395 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700396 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
Charles Chan61c086d2019-07-26 17:46:15 -0700397 VlanId innerVlan, EthType outerTpid,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000398 boolean directHost, boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800399 throws DeviceConfigNotFoundException {
Charles Chan61c086d2019-07-26 17:46:15 -0700400 int nextObjId;
401 if (directHost) {
402 // if the objective is to revoke an existing rule, and for some reason
403 // the next-objective does not exist, then a new one should not be created
404 ImmutablePair<TrafficTreatment, TrafficSelector> treatmentAndMeta =
405 getTreatmentAndMeta(deviceId, hostMac, hostVlanId, outPort, innerVlan, outerTpid);
406 if (treatmentAndMeta == null) {
407 // Warning log will come from getTreatmentAndMeta method
408 return null;
409 }
410 nextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
411 treatmentAndMeta.getLeft(), treatmentAndMeta.getRight(), !revoke);
412 } else {
413 // if the objective is to revoke an existing rule, and for some reason
414 // the next-objective does not exist, then a new one should not be created
415 nextObjId = srManager.getMacVlanNextObjectiveId(deviceId, hostMac, hostVlanId,
416 outPort, !revoke);
417 }
418 if (nextObjId == -1) {
419 // Warning log will come from getMacVlanNextObjective method
420 return null;
421 }
422
423 return DefaultForwardingObjective.builder()
424 .withSelector(buildIpSelectorFromIpPrefix(prefix).build())
425 .nextStep(nextObjId)
426 .fromApp(srManager.appId).makePermanent()
427 .withPriority(getPriorityFromPrefix(prefix))
428 .withFlag(ForwardingObjective.Flag.SPECIFIC);
429 }
430
431 private ImmutablePair<TrafficTreatment, TrafficSelector> getTreatmentAndMeta(
432 DeviceId deviceId, MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
433 VlanId innerVlan, EthType outerTpid)
434 throws DeviceConfigNotFoundException {
435 MacAddress routerMac;
436 routerMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800437
Charles Chan90772a72017-02-08 15:52:08 -0800438 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
Charles Chan098ca202018-05-01 11:50:20 -0700439 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
440 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
441 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700442
Charles Chan90772a72017-02-08 15:52:08 -0800443 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700444 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700445 tbuilder.deferred()
446 .setEthDst(hostMac)
Charles Chan61c086d2019-07-26 17:46:15 -0700447 .setEthSrc(routerMac)
sangho80f11cb2015-04-01 13:05:26 -0700448 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800449
Charles Chan90772a72017-02-08 15:52:08 -0800450 // Create route meta
451 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800452
Charles Chan61c086d2019-07-26 17:46:15 -0700453 // Adjust treatment and meta according to VLAN configuration
Charles Chan90772a72017-02-08 15:52:08 -0800454 if (taggedVlans.contains(hostVlanId)) {
455 tbuilder.setVlanId(hostVlanId);
456 } else if (hostVlanId.equals(VlanId.NONE)) {
457 if (untaggedVlan != null) {
458 mbuilder.matchVlanId(untaggedVlan);
459 } else if (nativeVlan != null) {
460 mbuilder.matchVlanId(nativeVlan);
461 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700462 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
463 hostMac, hostVlanId, connectPoint);
464 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800465 }
466 } else {
Charles Chan61c086d2019-07-26 17:46:15 -0700467 // Double tagged hosts
468 if (innerVlan == null || outerTpid == null) {
469 log.warn("Failed to construct NextObj for double tagged hosts {}/{}. {} {}",
470 hostMac, hostVlanId,
471 (innerVlan == null) ? "innerVlan = null." : "",
472 (outerTpid == null) ? "outerTpid = null." : "");
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700473 return null;
474 }
Charles Chan61c086d2019-07-26 17:46:15 -0700475 tbuilder.setVlanId(innerVlan);
476 tbuilder.pushVlan(outerTpid);
477 tbuilder.setVlanId(hostVlanId);
478 mbuilder.matchVlanId(VlanId.ANY);
Saurav Das07c74602016-04-27 18:35:50 -0700479 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000480
Charles Chan61c086d2019-07-26 17:46:15 -0700481 return ImmutablePair.of(tbuilder.build(), mbuilder.build());
sangho80f11cb2015-04-01 13:05:26 -0700482 }
483
484 /**
Saurav Das261c3002017-06-13 15:35:54 -0700485 * Populates IP flow rules for all the given prefixes reachable from the
486 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700487 *
Saurav Das261c3002017-06-13 15:35:54 -0700488 * @param targetSw switch where rules are to be programmed
489 * @param subnets subnets/prefixes being added
490 * @param destSw1 destination switch where the prefixes are reachable
491 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
492 * Should be null if there is no paired destination switch (by config)
493 * or if the given prefixes are reachable only via destSw1
494 * @param nextHops a map containing a set of next-hops for each destination switch.
495 * If destSw2 is not null, then this map must contain an
496 * entry for destSw2 with its next-hops from the targetSw
497 * (although the next-hop set may be empty in certain scenarios).
498 * If destSw2 is null, there should not be an entry in this
499 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700500 * @return true if all rules are set successfully, false otherwise
501 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700502 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700503 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
psneha86e60d32019-03-26 06:31:41 -0400504 // Get pair device of the target switch
505 Optional<DeviceId> pairDev = srManager.getPairDeviceId(targetSw);
506 // Route simplification will be off in case of the nexthop location at target switch is down
507 // (routing through spine case)
508 boolean routeSimplOff = pairDev.isPresent() && pairDev.get().equals(destSw1) && destSw2 == null;
piera9941192019-04-24 16:12:47 +0200509 // Iterates over the routes. Checking:
psneha86e60d32019-03-26 06:31:41 -0400510 // If route simplification is enabled
511 // If the target device is another leaf in the network
512 if (srManager.routeSimplification && !routeSimplOff) {
piera9941192019-04-24 16:12:47 +0200513 Set<IpPrefix> subnetsToBePopulated = Sets.newHashSet();
psneha86e60d32019-03-26 06:31:41 -0400514 for (IpPrefix subnet : subnets) {
515 // Skip route programming on the target device
516 // If route simplification applies
517 if (routeSimplifierUtils.hasLeafExclusionEnabledForPrefix(subnet)) {
518 // XXX route simplification assumes that source of the traffic
519 // towards the nexthops are co-located with the nexthops. In different
520 // scenarios will not work properly.
521 continue;
522 }
523 // populate the route in the remaning scenarios
piera9941192019-04-24 16:12:47 +0200524 subnetsToBePopulated.add(subnet);
psneha86e60d32019-03-26 06:31:41 -0400525 }
piera9941192019-04-24 16:12:47 +0200526 subnets = subnetsToBePopulated;
sangho80f11cb2015-04-01 13:05:26 -0700527 }
piera9941192019-04-24 16:12:47 +0200528 // populate the remaining routes in the target switch
529 return populateIpRulesForRouter(targetSw, subnets, destSw1, destSw2, nextHops);
Charles Chanc22cef32016-04-29 14:38:22 -0700530 }
sangho80f11cb2015-04-01 13:05:26 -0700531
Charles Chanc22cef32016-04-29 14:38:22 -0700532 /**
Charles Chand66d6712018-03-29 16:03:41 -0700533 * Revokes IP flow rules for the subnets from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700534 *
Charles Chand66d6712018-03-29 16:03:41 -0700535 * @param targetSw target switch from which the subnets need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700536 * @param subnets subnet being removed
537 * @return true if all rules are removed successfully, false otherwise
538 */
Charles Chand66d6712018-03-29 16:03:41 -0700539 boolean revokeIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700540 for (IpPrefix subnet : subnets) {
Charles Chand66d6712018-03-29 16:03:41 -0700541 if (!revokeIpRuleForRouter(targetSw, subnet)) {
Charles Chanc22cef32016-04-29 14:38:22 -0700542 return false;
543 }
544 }
sangho80f11cb2015-04-01 13:05:26 -0700545 return true;
546 }
547
548 /**
piera9941192019-04-24 16:12:47 +0200549 * Populates IP flow rules for a set of IP prefix in the target device.
550 * The prefix are reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700551 *
Saurav Das261c3002017-06-13 15:35:54 -0700552 * @param targetSw target device ID to set the rules
piera9941192019-04-24 16:12:47 +0200553 * @param subnets the set of IP prefix
Saurav Das261c3002017-06-13 15:35:54 -0700554 * @param destSw1 destination switch where the prefixes are reachable
555 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
556 * Should be null if there is no paired destination switch (by config)
557 * or if the given prefixes are reachable only via destSw1
558 * @param nextHops map of destination switches and their next-hops.
559 * Should only contain destination switches that are
560 * actually meant to be routed to. If destSw2 is null, there
561 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700562 * @return true if all rules are set successfully, false otherwise
563 */
piera9941192019-04-24 16:12:47 +0200564 private boolean populateIpRulesForRouter(DeviceId targetSw,
565 Set<IpPrefix> subnets,
566 DeviceId destSw1, DeviceId destSw2,
567 Map<DeviceId, Set<DeviceId>> nextHops) {
568 // pre-compute the needed information
569 int segmentIdIPv41, segmentIdIPv42 = -1;
570 int segmentIdIPv61, segmentIdIPv62 = -1;
571 TrafficTreatment treatment = null;
572 DestinationSet dsIPv4, dsIPv6;
573 TrafficSelector metaIpv4Selector, metaIpv6Selector = null;
574 int nextIdIPv4, nextIdIPv6, nextId;
575 TrafficSelector selector;
576 // start with MPLS SIDs
Charles Chan319d1a22015-11-03 10:42:14 -0800577 try {
piera9941192019-04-24 16:12:47 +0200578 segmentIdIPv41 = config.getIPv4SegmentId(destSw1);
579 segmentIdIPv61 = config.getIPv6SegmentId(destSw1);
580 if (destSw2 != null) {
581 segmentIdIPv42 = config.getIPv4SegmentId(destSw2);
582 segmentIdIPv62 = config.getIPv6SegmentId(destSw2);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800583 }
Charles Chan319d1a22015-11-03 10:42:14 -0800584 } catch (DeviceConfigNotFoundException e) {
585 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
586 return false;
587 }
piera9941192019-04-24 16:12:47 +0200588 // build the IPv4 and IPv6 destination set
Saurav Das261c3002017-06-13 15:35:54 -0700589 if (destSw2 == null) {
590 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800591 // If the next hop is the same as the final destination, then MPLS
592 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700593 Set<DeviceId> nhd1 = nextHops.get(destSw1);
594 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
piera9941192019-04-24 16:12:47 +0200595 dsIPv4 = DestinationSet.createTypePushNone(destSw1);
596 dsIPv6 = DestinationSet.createTypePushNone(destSw1);
597 treatment = DefaultTrafficTreatment.builder()
598 .immediate()
599 .decNwTtl()
600 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700601 } else {
piera9941192019-04-24 16:12:47 +0200602 dsIPv4 = DestinationSet.createTypePushBos(segmentIdIPv41, destSw1);
603 dsIPv6 = DestinationSet.createTypePushBos(segmentIdIPv61, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700604 }
605 } else {
606 // dst pair - IP rules for dst-pairs are always from other edge nodes
607 // the destination set needs to have both destinations, even if there
608 // are no next hops to one of them
piera9941192019-04-24 16:12:47 +0200609 dsIPv4 = DestinationSet.createTypePushBos(segmentIdIPv41, destSw1, segmentIdIPv42, destSw2);
610 dsIPv6 = DestinationSet.createTypePushBos(segmentIdIPv61, destSw1, segmentIdIPv62, destSw2);
sangho80f11cb2015-04-01 13:05:26 -0700611 }
612
Saurav Das4c35fc42015-11-20 15:27:53 -0800613 // setup metadata to pass to nextObjective - indicate the vlan on egress
614 // if needed by the switch pipeline. Since neighbor sets are always to
615 // other neighboring routers, there is no subnet assigned on those ports.
piera9941192019-04-24 16:12:47 +0200616 metaIpv4Selector = buildIpv4Selector()
617 .matchVlanId(srManager.getDefaultInternalVlan())
618 .build();
619 metaIpv6Selector = buildIpv6Selector()
620 .matchVlanId(srManager.getDefaultInternalVlan())
621 .build();
622 // get the group handler of the target switch
Saurav Das261c3002017-06-13 15:35:54 -0700623 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700624 if (grpHandler == null) {
625 log.warn("populateIPRuleForRouter: groupHandler for device {} "
piera9941192019-04-24 16:12:47 +0200626 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700627 return false;
628 }
piera9941192019-04-24 16:12:47 +0200629 // get next id
630 nextIdIPv4 = grpHandler.getNextObjectiveId(dsIPv4, nextHops, metaIpv4Selector, false);
631 if (nextIdIPv4 <= 0) {
632 log.warn("No next objective in {} for ds: {}", targetSw, dsIPv4);
sangho2165d222015-05-01 09:38:25 -0700633 return false;
634 }
piera9941192019-04-24 16:12:47 +0200635 nextIdIPv6 = grpHandler.getNextObjectiveId(dsIPv6, nextHops, metaIpv6Selector, false);
636 if (nextIdIPv6 <= 0) {
637 log.warn("No next objective in {} for ds: {}", targetSw, dsIPv6);
638 return false;
Charles Chanf4586112015-11-09 16:37:23 -0800639 }
piera9941192019-04-24 16:12:47 +0200640 // build all the flow rules and send to the device
641 for (IpPrefix subnet : subnets) {
642 selector = buildIpSelectorFromIpPrefix(subnet).build();
643 if (subnet.isIp4()) {
644 nextId = nextIdIPv4;
645 } else {
646 nextId = nextIdIPv6;
647 }
648 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
649 .builder()
650 .fromApp(srManager.appId)
651 .makePermanent()
652 .nextStep(nextId)
653 .withSelector(selector)
654 .withPriority(getPriorityFromPrefix(subnet))
655 .withFlag(ForwardingObjective.Flag.SPECIFIC);
656 if (treatment != null) {
657 fwdBuilder.withTreatment(treatment);
658 }
659 log.debug("Installing {} forwarding objective for router IP/subnet {} "
660 + "in switch {} with nextId: {}", subnet.isIp4() ? "IPv4" : "IPv6",
661 subnet, targetSw, nextId);
662 ObjectiveContext context = new DefaultObjectiveContext(
663 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
664 subnet, targetSw),
665 (objective, error) -> log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
666 subnet, error, targetSw));
667 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
668 }
669 rulePopulationCounter.addAndGet(subnets.size());
sangho80f11cb2015-04-01 13:05:26 -0700670 return true;
671 }
672
sangho80f11cb2015-04-01 13:05:26 -0700673 /**
Charles Chand66d6712018-03-29 16:03:41 -0700674 * Revokes IP flow rules for the router IP address from given device.
Charles Chanc22cef32016-04-29 14:38:22 -0700675 *
Charles Chand66d6712018-03-29 16:03:41 -0700676 * @param targetSw target switch from which the ipPrefix need to be removed
Charles Chanc22cef32016-04-29 14:38:22 -0700677 * @param ipPrefix the IP address of the destination router
678 * @return true if all rules are removed successfully, false otherwise
679 */
Charles Chand66d6712018-03-29 16:03:41 -0700680 private boolean revokeIpRuleForRouter(DeviceId targetSw, IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800681 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700682 TrafficSelector selector = sbuilder.build();
683 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
684
685 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
686 .builder()
687 .fromApp(srManager.appId)
688 .makePermanent()
689 .withSelector(selector)
690 .withTreatment(dummyTreatment)
691 .withPriority(getPriorityFromPrefix(ipPrefix))
692 .withFlag(ForwardingObjective.Flag.SPECIFIC);
693
Charles Chand66d6712018-03-29 16:03:41 -0700694 ObjectiveContext context = new DefaultObjectiveContext(
695 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, targetSw),
696 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
697 ipPrefix, targetSw, error));
698 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.remove(context));
Charles Chanc22cef32016-04-29 14:38:22 -0700699
700 return true;
701 }
702
703 /**
Saurav Das97241862018-02-14 14:14:54 -0800704 * Populates MPLS flow rules in the target device to point towards the
705 * destination device.
706 *
707 * @param targetSwId target device ID of the switch to set the rules
708 * @param destSwId destination switch device ID
709 * @param nextHops next hops switch ID list
710 * @param routerIp the router ip of the destination switch
711 * @return true if all rules are set successfully, false otherwise
712 */
713 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
714 Set<DeviceId> nextHops, IpAddress routerIp) {
715 int segmentId;
716 try {
717 if (routerIp.isIp4()) {
718 segmentId = config.getIPv4SegmentId(destSwId);
719 } else {
720 segmentId = config.getIPv6SegmentId(destSwId);
721 }
722 } catch (DeviceConfigNotFoundException e) {
723 log.warn(e.getMessage() + " Aborting populateMplsRule.");
724 return false;
725 }
726
727 List<ForwardingObjective> fwdObjs = new ArrayList<>();
728 Collection<ForwardingObjective> fwdObjsMpls;
729 // Generates the transit rules used by the standard "routing".
730 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
731 routerIp, true);
732 if (fwdObjsMpls.isEmpty()) {
733 return false;
734 }
735 fwdObjs.addAll(fwdObjsMpls);
736
737 // Generates the transit rules used by the MPLS Pwaas.
738 int pwSrLabel;
739 try {
740 pwSrLabel = config.getPWRoutingLabel(destSwId);
741 } catch (DeviceConfigNotFoundException e) {
742 log.warn(e.getMessage()
743 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
744 return false;
745 }
746 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
747 routerIp, false);
748 if (fwdObjsMpls.isEmpty()) {
749 return false;
750 }
751 fwdObjs.addAll(fwdObjsMpls);
752
753 for (ForwardingObjective fwdObj : fwdObjs) {
754 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
755 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
756 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
757 rulePopulationCounter.incrementAndGet();
758 }
759
760 return true;
761 }
762
763 /**
764 * Differentiates between popping and swapping labels when building an MPLS
765 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700766 *
767 * @param targetSwId the target sw
768 * @param destSwId the destination sw
769 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800770 * @param segmentId the segmentId to match representing the destination
771 * switch
772 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700773 * @return a collection of fwdobjective
774 */
Saurav Das261c3002017-06-13 15:35:54 -0700775 private Collection<ForwardingObjective> handleMpls(
776 DeviceId targetSwId,
777 DeviceId destSwId,
778 Set<DeviceId> nextHops,
779 int segmentId,
780 IpAddress routerIp,
781 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700782
783 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
784 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800785 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700786 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
787 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
788 sbuilder.matchMplsBos(isMplsBos);
789 TrafficSelector selector = sbuilder.build();
790
791 // setup metadata to pass to nextObjective - indicate the vlan on egress
792 // if needed by the switch pipeline. Since mpls next-hops are always to
793 // other neighboring routers, there is no subnet assigned on those ports.
794 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Saurav Das9bf49582018-08-13 15:34:26 -0700795 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700796
797 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
798 // If the next hop is the destination router for the segment, do pop
799 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700800 + "label {} in switch {} with pop to next-hops {}",
801 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700802 ForwardingObjective.Builder fwdObjNoBosBuilder =
803 getMplsForwardingObjective(targetSwId,
804 nextHops,
805 true,
806 isMplsBos,
807 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700808 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800809 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700810 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700811 // Error case, we cannot handle, exit.
812 if (fwdObjNoBosBuilder == null) {
813 return Collections.emptyList();
814 }
815 fwdObjBuilders.add(fwdObjNoBosBuilder);
816
817 } else {
Saurav Das97241862018-02-14 14:14:54 -0800818 // next hop is not destination, irrespective of the number of next
819 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700820 log.debug("Installing MPLS forwarding objective for "
821 + "label {} in switch {} without pop to next-hops {}",
822 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700823 ForwardingObjective.Builder fwdObjNoBosBuilder =
824 getMplsForwardingObjective(targetSwId,
825 nextHops,
826 false,
827 isMplsBos,
828 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700829 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800830 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700831 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700832 // Error case, we cannot handle, exit.
833 if (fwdObjNoBosBuilder == null) {
834 return Collections.emptyList();
835 }
836 fwdObjBuilders.add(fwdObjNoBosBuilder);
837
838 }
839
840 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
841 // We add the final property to the fwdObjs.
842 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700843 ((Builder) ((Builder) fwdObjBuilder
844 .fromApp(srManager.appId)
845 .makePermanent())
846 .withSelector(selector)
847 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
848 .withFlag(ForwardingObjective.Flag.SPECIFIC);
849
850 ObjectiveContext context = new DefaultObjectiveContext(
851 (objective) ->
852 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
853 objective.id(), segmentId, targetSwId),
854 (objective, error) ->
855 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
856 objective.id(), segmentId, error, targetSwId));
857
858 ForwardingObjective fob = fwdObjBuilder.add(context);
859 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700860 }
861
862 return fwdObjs;
863 }
864
865 /**
Saurav Das97241862018-02-14 14:14:54 -0800866 * Returns a Forwarding Objective builder for the MPLS rule that references
867 * the desired Next Objective. Creates a DestinationSet that allows the
868 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700869 *
Saurav Das97241862018-02-14 14:14:54 -0800870 * @param targetSw the target sw
871 * @param nextHops the set of next hops
872 * @param phpRequired true if penultimate-hop-popping is required
873 * @param isBos true if matched label is bottom-of-stack
874 * @param meta metadata for creating next objective
875 * @param routerIp the router ip representing the destination switch
876 * @param destSw the destination sw
877 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700878 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800879 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700880 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800881 Set<DeviceId> nextHops,
882 boolean phpRequired,
883 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800884 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700885 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800886 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700887 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800888
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700889 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
890 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700891
892 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800893 DestinationSet ds = null;
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700894 DestinationSet.DestinationSetType dstType = null;
Saurav Das97241862018-02-14 14:14:54 -0800895 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700896 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800897 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700898 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700899 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700900 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800901 if (routerIp.isIp4()) {
902 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
903 } else {
904 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
905 }
906 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800907 // standard case -> BoS == True; pop results in IP packet and forwarding
908 // is via an ECMP group
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700909 ds = DestinationSet.createTypePopBos(destSw);
sangho80f11cb2015-04-01 13:05:26 -0700910 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800911 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
912 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800913 // double-label case -> BoS == False, pop results in MPLS packet
914 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700915 ds = DestinationSet.createTypePopNotBos(destSw);
916 if (!srManager.getMplsEcmp()) {
917 simple = true;
Saurav Das97241862018-02-14 14:14:54 -0800918 }
sangho80f11cb2015-04-01 13:05:26 -0700919 }
920 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800921 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800922 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700923 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800924 // swap results in MPLS packet with same BoS bit regardless of bit value
925 // depending on configuration we can ECMP this packet or choose one output
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700926 // differentiate here between swap with not bos or swap with bos
927 ds = isBos ? DestinationSet.createTypeSwapBos(segmentId, destSw) :
928 DestinationSet.createTypeSwapNotBos(segmentId, destSw);
929 if (!srManager.getMplsEcmp()) {
Saurav Das97241862018-02-14 14:14:54 -0800930 simple = true;
931 }
sangho80f11cb2015-04-01 13:05:26 -0700932 }
933
Saurav Das4c35fc42015-11-20 15:27:53 -0800934 fwdBuilder.withTreatment(tbuilder.build());
Saurav Das97241862018-02-14 14:14:54 -0800935 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
936 targetSw, ds);
Saurav Das261c3002017-06-13 15:35:54 -0700937 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
938 if (gh == null) {
939 log.warn("getNextObjectiveId query - groupHandler for device {} "
940 + "not found", targetSw);
941 return null;
942 }
Saurav Das97241862018-02-14 14:14:54 -0800943
Saurav Das261c3002017-06-13 15:35:54 -0700944 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
945 dstNextHops.put(destSw, nextHops);
Saurav Das97241862018-02-14 14:14:54 -0800946 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das4c35fc42015-11-20 15:27:53 -0800947 if (nextId <= 0) {
Saurav Das97241862018-02-14 14:14:54 -0800948 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das4c35fc42015-11-20 15:27:53 -0800949 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700950 } else {
Saurav Das97241862018-02-14 14:14:54 -0800951 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
952 nextId, targetSw, ds);
sangho80f11cb2015-04-01 13:05:26 -0700953 }
954
Saurav Das4c35fc42015-11-20 15:27:53 -0800955 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700956 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700957 }
958
959 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700960 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700961 * dstMac corresponding to the router's MAC address. For those pipelines
962 * that need to internally assign vlans to untagged packets, this method
963 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700964 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800965 * Note that the vlan assignment and filter programming should only be done by
966 * the master for a switch. This method is typically called at deviceAdd and
967 * programs filters only for the enabled ports of the device. For port-updates,
968 * that enable/disable ports after device add, singlePortFilter methods should
969 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700970 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700971 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800972 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700973 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700974 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700975 log.debug("Installing per-port filtering objective for untagged "
976 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800977
Saurav Das07c74602016-04-27 18:35:50 -0700978 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800979 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700980 log.warn("Device {} ports not available. Unable to add MacVlan filters",
981 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800982 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700983 }
Saurav Dasf9332192017-02-18 14:05:44 -0800984 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700985 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700986 if (!port.isEnabled()) {
987 disabledPorts++;
988 continue;
989 }
Charles Chan43be46b2017-02-26 22:59:35 -0800990 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800991 filteredPorts++;
992 } else {
993 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700994 }
Saurav Das7c305372015-10-28 12:39:42 -0700995 }
Charles Chan077314e2017-06-22 14:27:17 -0700996 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800997 deviceId, disabledPorts, errorPorts, filteredPorts);
Charles Chan9d2dd552018-06-19 20:56:33 -0700998 return new PortFilterInfo(disabledPorts, errorPorts, filteredPorts);
Saurav Dasf9332192017-02-18 14:05:44 -0800999 }
1000
1001 /**
Charles Chan43be46b2017-02-26 22:59:35 -08001002 * Creates or removes filtering objectives for a single port. Should only be
1003 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -08001004 *
1005 * @param deviceId device identifier
1006 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -08001007 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -08001008 * @return true if no errors occurred during the build of the filtering objective
1009 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001010 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -08001011 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
Charles Chan098ca202018-05-01 11:50:20 -07001012 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
1013 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
1014 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Charles Chan90772a72017-02-08 15:52:08 -08001015
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001016 // Do not configure filter for edge ports where double-tagged hosts are connected.
Charles Chan90772a72017-02-08 15:52:08 -08001017 if (taggedVlans.size() != 0) {
1018 // Filter for tagged vlans
Charles Chan098ca202018-05-01 11:50:20 -07001019 if (!srManager.interfaceService.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -08001020 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -08001021 return false;
1022 }
1023 if (nativeVlan != null) {
1024 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -08001025 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001026 return false;
1027 }
1028 }
1029 } else if (untaggedVlan != null) {
1030 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -08001031 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001032 return false;
1033 }
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -07001034 } else if (!hasIPConfiguration(connectPoint)) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001035 // Filter for unconfigured upstream port, using INTERNAL_VLAN
Saurav Das9bf49582018-08-13 15:34:26 -07001036 if (!processSinglePortFiltersInternal(deviceId, portnum, true,
1037 srManager.getDefaultInternalVlan(),
1038 install)) {
Charles Chan90772a72017-02-08 15:52:08 -08001039 return false;
1040 }
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -07001041 // Filter for receiveing pseudowire traffic
Saurav Das9bf49582018-08-13 15:34:26 -07001042 if (!processSinglePortFiltersInternal(deviceId, portnum, false,
1043 srManager.getPwTransportVlan(),
1044 install)) {
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -07001045 return false;
1046 }
Charles Chan90772a72017-02-08 15:52:08 -08001047 }
1048 return true;
1049 }
1050
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001051 /**
1052 * Updates filtering objectives for a single port. Should only be called by
1053 * the master for a switch
1054 * @param deviceId device identifier
1055 * @param portNum port identifier for port to be filtered
1056 * @param pushVlan true to push vlan, false otherwise
1057 * @param vlanId vlan identifier
1058 * @param install true to install the filtering objective, false to remove
1059 */
1060 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
1061 boolean pushVlan, VlanId vlanId, boolean install) {
1062 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
1063 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
1064 deviceId, portNum, vlanId);
1065 }
1066 }
1067
Charles Chan43be46b2017-02-26 22:59:35 -08001068 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
1069 boolean pushVlan, VlanId vlanId, boolean install) {
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001070 boolean doTMAC = true;
1071
1072 if (!pushVlan) {
1073 // Skip the tagged vlans belonging to an interface without an IP address
1074 Set<Interface> ifaces = srManager.interfaceService
1075 .getInterfacesByPort(new ConnectPoint(deviceId, portnum))
1076 .stream()
1077 .filter(intf -> intf.vlanTagged().contains(vlanId) && intf.ipAddressesList().isEmpty())
1078 .collect(Collectors.toSet());
1079 if (!ifaces.isEmpty()) {
1080 log.debug("processSinglePortFiltersInternal: skipping TMAC for vlan {} at {}/{} - no IP",
1081 vlanId, deviceId, portnum);
1082 doTMAC = false;
1083 }
1084 }
1085
1086 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId, doTMAC);
Saurav Dasf9332192017-02-18 14:05:44 -08001087 if (fob == null) {
1088 // error encountered during build
1089 return false;
1090 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001091 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -07001092 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -08001093 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -08001094 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -07001095 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -08001096 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -07001097 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -08001098 if (install) {
1099 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1100 } else {
1101 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -08001102 }
Charles Chan90772a72017-02-08 15:52:08 -08001103 return true;
Saurav Dasf9332192017-02-18 14:05:44 -08001104 }
1105
Charles Chan90772a72017-02-08 15:52:08 -08001106 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001107 boolean pushVlan, VlanId vlanId, boolean doTMAC) {
Saurav Dasf9332192017-02-18 14:05:44 -08001108 MacAddress deviceMac;
1109 try {
1110 deviceMac = config.getDeviceMac(deviceId);
1111 } catch (DeviceConfigNotFoundException e) {
1112 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
1113 return null;
1114 }
Saurav Dasf9332192017-02-18 14:05:44 -08001115 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
Daniel Ginsburg776789f2018-04-30 19:27:19 -04001116
1117 if (doTMAC) {
1118 fob.withKey(Criteria.matchInPort(portnum))
1119 .addCondition(Criteria.matchEthDst(deviceMac))
1120 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1121 } else {
1122 fob.withKey(Criteria.matchInPort(portnum))
1123 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1124 }
Charles Chan90772a72017-02-08 15:52:08 -08001125
Charles Chan17ca2202017-12-19 19:55:57 -08001126 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1127
Charles Chan90772a72017-02-08 15:52:08 -08001128 if (pushVlan) {
1129 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -08001130 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -08001131 } else {
1132 fob.addCondition(Criteria.matchVlanId(vlanId));
1133 }
1134
Charles Chan17ca2202017-12-19 19:55:57 -08001135 // NOTE: Some switch hardware share the same filtering flow among different ports.
1136 // We use this metadata to let the driver know that there is no more enabled port
1137 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -08001138 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -08001139 tBuilder.wipeDeferred();
1140 }
1141
1142 fob.withMeta(tBuilder.build());
1143
Saurav Dasf9332192017-02-18 14:05:44 -08001144 fob.permit().fromApp(srManager.appId);
1145 return fob;
sangho80f11cb2015-04-01 13:05:26 -07001146 }
1147
1148 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001149 * Creates or removes filtering objectives for a double-tagged host on a port.
1150 *
1151 * @param deviceId device identifier
1152 * @param portNum port identifier for port to be filtered
1153 * @param outerVlan outer VLAN ID
1154 * @param innerVlan inner VLAN ID
1155 * @param install true to install the filtering objective, false to remove
1156 */
1157 void processDoubleTaggedFilter(DeviceId deviceId, PortNumber portNum, VlanId outerVlan,
1158 VlanId innerVlan, boolean install) {
Daniele Moro8fc37b42019-10-29 18:48:35 -07001159 // We should trigger the removal of double tagged rules only when removing
1160 // the filtering objective and no other hosts are connected to the same device port.
Charles Chanf17fade2020-03-08 18:07:19 -07001161 boolean cleanupDoubleTaggedRules = !anyDoubleTaggedHost(deviceId, portNum) && !install;
Daniele Moro8fc37b42019-10-29 18:48:35 -07001162 FilteringObjective.Builder fob = buildDoubleTaggedFilteringObj(deviceId, portNum,
1163 outerVlan, innerVlan,
1164 cleanupDoubleTaggedRules);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001165 if (fob == null) {
1166 // error encountered during build
1167 return;
1168 }
1169 log.debug("{} double-tagged filtering objectives for dev/port: {}/{}",
1170 install ? "Installing" : "Removing", deviceId, portNum);
1171 ObjectiveContext context = new DefaultObjectiveContext(
1172 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portNum,
1173 install ? "installed" : "removed"),
1174 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
1175 install ? "install" : "remove", deviceId, portNum, error));
1176 if (install) {
1177 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
1178 } else {
1179 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
1180 }
1181 }
1182
Charles Chanf17fade2020-03-08 18:07:19 -07001183 /**
1184 * Checks if there is any double tagged host attached to given location.
1185 * This method will match on the effective location of a host.
1186 * That is, it will match on auxLocations when auxLocations is not null. Otherwise, it will match on locations.
1187 *
1188 * @param deviceId device ID
1189 * @param portNum port number
1190 * @return true if there is any host attached to given location.
1191 */
1192 private boolean anyDoubleTaggedHost(DeviceId deviceId, PortNumber portNum) {
1193 ConnectPoint cp = new ConnectPoint(deviceId, portNum);
1194 Set<Host> connectedHosts = srManager.hostService.getConnectedHosts(cp, false);
1195 Set<Host> auxConnectedHosts = srManager.hostService.getConnectedHosts(cp, true);
1196 return !auxConnectedHosts.isEmpty() ||
1197 connectedHosts.stream().anyMatch(host -> host.auxLocations() == null);
1198 }
1199
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001200 private FilteringObjective.Builder buildDoubleTaggedFilteringObj(DeviceId deviceId, PortNumber portNum,
Andreas Pantelopoulos59bd97e2018-06-28 17:06:14 -07001201 VlanId outerVlan, VlanId innerVlan,
1202 boolean cleanupDoubleTaggedRules) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001203 MacAddress deviceMac;
1204 try {
1205 deviceMac = config.getDeviceMac(deviceId);
1206 } catch (DeviceConfigNotFoundException e) {
1207 log.warn(e.getMessage() + " Processing DoubleTaggedFilters aborted");
1208 return null;
1209 }
1210 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
1211 // Outer vlan id match should be appeared before inner vlan id match.
1212 fob.withKey(Criteria.matchInPort(portNum))
1213 .addCondition(Criteria.matchEthDst(deviceMac))
1214 .addCondition(Criteria.matchVlanId(outerVlan))
Daniele Moro998f2df2019-07-12 17:58:54 -07001215 .addCondition(Criteria.matchInnerVlanId(innerVlan))
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001216 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
1217
1218 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1219 // Pop outer vlan
1220 tBuilder.popVlan();
1221
Andreas Pantelopoulos59bd97e2018-06-28 17:06:14 -07001222 // special metadata for driver
1223 if (cleanupDoubleTaggedRules) {
1224 tBuilder.writeMetadata(CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES, DOUBLE_TAGGED_METADATA_MASK);
1225 } else {
1226 tBuilder.writeMetadata(0, DOUBLE_TAGGED_METADATA_MASK);
1227 }
1228
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001229 // NOTE: Some switch hardware share the same filtering flow among different ports.
1230 // We use this metadata to let the driver know that there is no more enabled port
1231 // within the same VLAN on this device.
1232 if (noMoreEnabledPort(deviceId, outerVlan)) {
1233 tBuilder.wipeDeferred();
1234 }
1235
1236 fob.withMeta(tBuilder.build());
1237
1238 fob.permit().fromApp(srManager.appId);
1239 return fob;
1240 }
1241
1242 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -07001243 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -07001244 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -07001245 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -07001246 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -07001247 *
Saurav Das9f1c42e2015-10-23 10:51:11 -07001248 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -07001249 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001250 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -07001251 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -08001252 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -08001253 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -08001254 routerIpv4 = config.getRouterIpv4(deviceId);
1255 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -08001256 routerLinkLocalIpv6 = Ip6Address.valueOf(
1257 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
1258
Saurav Das261c3002017-06-13 15:35:54 -07001259 if (config.isPairedEdge(deviceId)) {
1260 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
1261 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
1262 }
Charles Chan319d1a22015-11-03 10:42:14 -08001263 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -08001264 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -08001265 return;
1266 }
1267
Saurav Dasc28b3432015-10-30 17:45:38 -07001268 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1269 log.debug("Not installing port-IP punts - not the master for dev:{} ",
1270 deviceId);
1271 return;
1272 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001273 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
1274 allIps.add(routerIpv4);
1275 if (routerIpv6 != null) {
1276 allIps.add(routerIpv6);
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001277 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001278 }
Saurav Das261c3002017-06-13 15:35:54 -07001279 if (pairRouterIpv4 != null) {
1280 allIps.add(pairRouterIpv4);
1281 }
1282 if (pairRouterIpv6 != null) {
1283 allIps.add(pairRouterIpv6);
1284 }
Pier Ventreadb4ae62016-11-23 09:57:42 -08001285 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001286 populateSingleIpPunts(deviceId, ipaddr);
1287 }
1288 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -08001289
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001290 /**
1291 * Creates a forwarding objective to punt all IP packets, destined to the
1292 * specified IP address, which should be router's port IP address.
1293 *
1294 * @param deviceId the switch dpid for the router
1295 * @param ipAddress the IP address of the router's port
1296 */
1297 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1298 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1299 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1300
1301 srManager.packetService.requestPackets(sbuilder.build(),
1302 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1303 }
1304
1305 /**
1306 * Removes a forwarding objective to punt all IP packets, destined to the
1307 * specified IP address, which should be router's port IP address.
1308 *
1309 * @param deviceId the switch dpid for the router
1310 * @param ipAddress the IP address of the router's port
1311 */
1312 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
1313 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
1314 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
1315
1316 try {
1317 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
1318 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
1319 srManager.packetService.cancelPackets(sbuilder.build(),
1320 PacketPriority.CONTROL, srManager.appId, optDeviceId);
1321 }
1322 } catch (DeviceConfigNotFoundException e) {
1323 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -07001324 }
sangho80f11cb2015-04-01 13:05:26 -07001325 }
1326
piera9941192019-04-24 16:12:47 +02001327 // Method for building an IPv4 selector
1328 private TrafficSelector.Builder buildIpv4Selector() {
1329 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
1330 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1331 return selectorBuilder;
1332 }
1333
1334 // Method for building an IPv6 selector
1335 private TrafficSelector.Builder buildIpv6Selector() {
1336 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
1337 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1338 return selectorBuilder;
1339 }
1340
1341 // Method for building an IPv4 or IPv6 selector from an IP address
Pier Ventreadb4ae62016-11-23 09:57:42 -08001342 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
1343 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
1344 }
1345
piera9941192019-04-24 16:12:47 +02001346 // Method for building an IPv4 or IPv6 selector from an IP prefix
Pier Ventreadb4ae62016-11-23 09:57:42 -08001347 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
1348 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -07001349 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -08001350 if (prefixToMatch.isIp4()) {
1351 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
1352 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
1353 return selectorBuilder;
1354 }
Pier Ventre229fd0b2016-10-31 16:49:19 -07001355 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -08001356 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
1357 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
1358 return selectorBuilder;
1359 }
1360
1361 /**
Pier Luigib9632ba2017-01-12 18:14:58 -08001362 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
1363 * Furthermore, these are applied only by the master instance. Deferred actions
1364 * are not cleared such that packets can be flooded in the cross connect use case
1365 *
1366 * @param deviceId the switch dpid for the router
1367 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001368 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001369 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -08001370 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1371 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
1372 deviceId);
1373 return;
1374 }
1375
Charles Chan3ed34d82017-06-22 18:03:14 -07001376 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001377 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001378 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001379 .add(new ObjectiveContext() {
1380 @Override
1381 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001382 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001383 deviceId, error);
1384 }
1385 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001386 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001387
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001388 if (isIpv6Configured(deviceId)) {
1389 // We punt all NDP packets towards the controller.
1390 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1391 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1392 @Override
1393 public void onError(Objective objective, ObjectiveError error) {
1394 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1395 deviceId, error);
1396 }
1397 });
1398 srManager.flowObjectiveService.forward(deviceId, obj);
Charles Chan051490d2018-01-11 11:48:18 -08001399 });
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001400 }
Charles Chan3ed34d82017-06-22 18:03:14 -07001401
Saurav Dasec683dc2018-04-27 18:42:30 -07001402 srManager.getPairLocalPort(deviceId).ifPresent(port -> {
Charles Chan3ed34d82017-06-22 18:03:14 -07001403 ForwardingObjective pairFwdObj;
1404 // Do not punt ARP packets from pair port
1405 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1406 .add(new ObjectiveContext() {
1407 @Override
1408 public void onError(Objective objective, ObjectiveError error) {
1409 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1410 deviceId, error);
1411 }
1412 });
1413 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1414
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001415 if (isIpv6Configured(deviceId)) {
1416 // Do not punt NDP packets from pair port
1417 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1418 ForwardingObjective obj = builder.add(new ObjectiveContext() {
Charles Chan3ed34d82017-06-22 18:03:14 -07001419 @Override
1420 public void onError(Objective objective, ObjectiveError error) {
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001421 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
Charles Chan3ed34d82017-06-22 18:03:14 -07001422 deviceId, error);
1423 }
1424 });
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001425 srManager.flowObjectiveService.forward(deviceId, obj);
1426 });
1427
1428 // Do not forward DAD packets from pair port
1429 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1430 .add(new ObjectiveContext() {
1431 @Override
1432 public void onError(Objective objective, ObjectiveError error) {
1433 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1434 deviceId, error);
1435 }
1436 });
1437 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1438 }
Charles Chan3ed34d82017-06-22 18:03:14 -07001439 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001440 }
1441
Charles Chan3ed34d82017-06-22 18:03:14 -07001442 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1443 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001444 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001445 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001446 .withSelector(selector)
1447 .fromApp(srManager.appId)
1448 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001449 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001450 .makePermanent();
1451 }
1452
Charles Chan3ed34d82017-06-22 18:03:14 -07001453 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001454 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1455 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001456 if (port != null) {
1457 sBuilder.matchInPort(port);
1458 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001459
Charles Chan3ed34d82017-06-22 18:03:14 -07001460 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1461 if (punt) {
1462 tBuilder.punt();
1463 }
1464 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001465 }
1466
Charles Chan051490d2018-01-11 11:48:18 -08001467 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1468 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001469
Charles Chan051490d2018-01-11 11:48:18 -08001470 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1471 .forEach(type -> {
1472 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1473 sBuilder.matchEthType(TYPE_IPV6)
1474 .matchIPProtocol(PROTOCOL_ICMP6)
1475 .matchIcmpv6Type(type);
1476 if (port != null) {
1477 sBuilder.matchInPort(port);
1478 }
1479
1480 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1481 if (punt) {
1482 tBuilder.punt();
1483 }
1484
1485 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1486 });
1487
1488 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001489 }
1490
1491 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1492 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1493 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001494 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1495 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1496 // .matchIPProtocol(PROTOCOL_ICMP6)
1497 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001498 if (port != null) {
1499 sBuilder.matchInPort(port);
1500 }
1501
1502 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1503 tBuilder.wipeDeferred();
1504 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001505 }
1506
1507 /**
Charles Chan155ec442018-09-16 14:30:19 -07001508 * Block given prefix in routing table.
Charles Chanf4586112015-11-09 16:37:23 -08001509 *
Andrea Campanella60ce2222018-04-30 11:48:55 +02001510 * @param address the address to block
1511 * @param deviceId switch ID to set the rules
1512 */
1513 void populateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1514 updateDefaultRouteBlackhole(deviceId, address, true);
1515 }
1516
1517 /**
Charles Chan155ec442018-09-16 14:30:19 -07001518 * Unblock given prefix in routing table.
Andrea Campanella60ce2222018-04-30 11:48:55 +02001519 *
1520 * @param address the address to block
1521 * @param deviceId switch ID to set the rules
1522 */
1523 void removeDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address) {
1524 updateDefaultRouteBlackhole(deviceId, address, false);
1525 }
1526
1527 private void updateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address, boolean install) {
1528 try {
1529 if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) {
1530
1531 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1532 if (address.isIp4()) {
1533 sbuilder.matchIPDst(address);
1534 sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort());
1535 } else {
1536 sbuilder.matchIPv6Dst(address);
1537 sbuilder.matchEthType(EthType.EtherType.IPV6.ethType().toShort());
1538 }
1539
1540 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Andrea Campanella60ce2222018-04-30 11:48:55 +02001541 tBuilder.wipeDeferred();
1542
1543 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1544 fob.withFlag(Flag.SPECIFIC)
1545 .withSelector(sbuilder.build())
1546 .withTreatment(tBuilder.build())
1547 .withPriority(getPriorityFromPrefix(address))
1548 .fromApp(srManager.appId)
1549 .makePermanent();
1550
1551 log.debug("{} blackhole forwarding objectives for dev: {}",
1552 install ? "Installing" : "Removing", deviceId);
1553 ObjectiveContext context = new DefaultObjectiveContext(
1554 (objective) -> log.debug("Forward for {} {}", deviceId,
1555 install ? "installed" : "removed"),
1556 (objective, error) -> log.warn("Failed to {} forward for {}: {}",
1557 install ? "install" : "remove", deviceId, error));
1558 if (install) {
1559 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1560 } else {
1561 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1562 }
1563 }
1564 } catch (DeviceConfigNotFoundException e) {
1565 log.info("Not populating blackhole for un-configured device {}", deviceId);
1566 }
1567
1568 }
1569
1570 /**
1571 * Populates a forwarding objective to send packets that miss other high
1572 * priority Bridging Table entries to a group that contains all ports of
1573 * its subnet.
1574 *
Charles Chanf4586112015-11-09 16:37:23 -08001575 * @param deviceId switch ID to set the rules
1576 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001577 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001578 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001579 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001580 });
1581 }
1582
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001583 /**
1584 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1585 * @param deviceId switch ID to set the rule
1586 * @param vlanId vlan ID to specify the subnet
1587 * @param install true to install the rule, false to revoke the rule
1588 */
1589 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1590 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1591
1592 if (nextId < 0) {
1593 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1594 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1595 return;
1596 }
1597
1598 // Driver should treat objective with MacAddress.NONE as the
1599 // subnet broadcast rule
1600 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1601 sbuilder.matchVlanId(vlanId);
1602 sbuilder.matchEthDst(MacAddress.NONE);
1603
1604 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1605 fob.withFlag(Flag.SPECIFIC)
1606 .withSelector(sbuilder.build())
1607 .nextStep(nextId)
1608 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1609 .fromApp(srManager.appId)
1610 .makePermanent();
1611 ObjectiveContext context = new DefaultObjectiveContext(
1612 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1613 (objective, error) ->
1614 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1615
1616 if (install) {
1617 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1618 } else {
1619 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1620 }
1621 }
1622
Charles Chan82ab1932016-01-30 23:22:37 -08001623 private int getPriorityFromPrefix(IpPrefix prefix) {
1624 return (prefix.isIp4()) ?
1625 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1626 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001627 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001628
1629 /**
1630 * Update Forwarding objective for each host and IP address connected to given port.
1631 * And create corresponding Simple Next objective if it does not exist.
1632 * Applied only when populating Forwarding objective
1633 * @param deviceId switch ID to set the rule
1634 * @param portNumber port number
1635 * @param prefix IP prefix of the route
1636 * @param hostMac MAC address of the next hop
1637 * @param vlanId Vlan ID of the port
1638 * @param popVlan true to pop vlan tag in TrafficTreatment
1639 * @param install true to populate the forwarding objective, false to revoke
1640 */
1641 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1642 VlanId vlanId, boolean popVlan, boolean install) {
1643 ForwardingObjective.Builder fob;
1644 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1645 MacAddress deviceMac;
1646 try {
1647 deviceMac = config.getDeviceMac(deviceId);
1648 } catch (DeviceConfigNotFoundException e) {
1649 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1650 return;
1651 }
1652
1653 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1654 tbuilder.deferred()
1655 .setEthDst(hostMac)
1656 .setEthSrc(deviceMac)
1657 .setOutput(portNumber);
1658
1659 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1660
1661 if (!popVlan) {
1662 tbuilder.setVlanId(vlanId);
1663 } else {
1664 mbuilder.matchVlanId(vlanId);
1665 }
1666
1667 // if the objective is to revoke an existing rule, and for some reason
1668 // the next-objective does not exist, then a new one should not be created
1669 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1670 tbuilder.build(), mbuilder.build(), install);
1671 if (portNextObjId == -1) {
1672 // Warning log will come from getPortNextObjective method
1673 return;
1674 }
1675
1676 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1677 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1678 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1679
1680 ObjectiveContext context = new DefaultObjectiveContext(
1681 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1682 (objective, error) ->
1683 log.warn("Failed to {} IP rule for route {}: {}",
1684 install ? "install" : "revoke", prefix, error));
1685 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1686
1687 if (!install) {
1688 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1689 if (grpHandler == null) {
1690 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1691 } else {
1692 // Remove L3UG for the given port and host
1693 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1694 }
1695 }
1696 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001697
1698 /**
1699 * Checks if there is other enabled port within the given VLAN on the given device.
1700 *
1701 * @param deviceId device ID
1702 * @param vlanId VLAN ID
1703 * @return true if there is no more port enabled within the given VLAN on the given device
1704 */
1705 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1706 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1707 .filter(Port::isEnabled)
1708 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1709 .collect(Collectors.toSet());
1710
1711 return enabledPorts.stream().noneMatch(cp ->
1712 // Given vlanId is included in the vlan-tagged configuration
Charles Chan098ca202018-05-01 11:50:20 -07001713 srManager.interfaceService.getTaggedVlanId(cp).contains(vlanId) ||
pieraac79e92019-10-04 15:40:34 +02001714 // Given vlanId is INTERNAL_VLAN or PSEUDOWIRE_VLAN and the interface is not configured
Charles Chan098ca202018-05-01 11:50:20 -07001715 (srManager.interfaceService.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
pieraac79e92019-10-04 15:40:34 +02001716 (vlanId.equals(srManager.getDefaultInternalVlan()) || vlanId.equals(srManager.getPwTransportVlan()))) ||
Charles Chanf17f66b2018-02-26 21:33:25 -08001717 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1718 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1719 );
1720 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001721
1722 /**
1723 * Returns a forwarding objective builder for egress forwarding rules.
1724 * <p>
1725 * The forwarding objective installs flow rules to egress pipeline to push
1726 * two vlan headers with given inner, outer vlan ids and outer tpid.
1727 *
1728 * @param portNumber port where the next hop attaches to
1729 * @param dummyVlanId vlan ID of the packet to match
1730 * @param innerVlan inner vlan ID of the next hop
1731 * @param outerVlan outer vlan ID of the next hop
1732 * @param outerTpid outer TPID of the next hop
1733 * @return forwarding objective builder
1734 */
1735 private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId,
1736 VlanId innerVlan, VlanId outerVlan, EthType outerTpid) {
1737 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1738 sbuilder.matchVlanId(dummyVlanId);
1739 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1740 tbuilder.setOutput(portNumber).setVlanId(innerVlan);
1741
1742 if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) {
1743 tbuilder.pushVlan(outerTpid);
1744 } else {
1745 tbuilder.pushVlan();
1746 }
1747
1748 tbuilder.setVlanId(outerVlan);
1749 return DefaultForwardingObjective.builder()
1750 .withSelector(sbuilder.build())
1751 .withTreatment(tbuilder.build())
1752 .fromApp(srManager.appId)
1753 .makePermanent()
1754 .withPriority(DEFAULT_PRIORITY)
1755 .withFlag(ForwardingObjective.Flag.EGRESS);
1756 }
1757
1758 /**
1759 * Populates IP rules for a route that has double-tagged next hop.
1760 *
1761 * @param deviceId device ID of the device that next hop attaches to
1762 * @param prefix IP prefix of the route
1763 * @param hostMac MAC address of the next hop
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001764 * @param innerVlan inner Vlan ID of the next hop
1765 * @param outerVlan outer Vlan ID of the next hop
1766 * @param outerTpid outer TPID of the next hop
1767 * @param outPort port where the next hop attaches to
1768 */
Charles Chan61c086d2019-07-26 17:46:15 -07001769 void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001770 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1771 ForwardingObjective.Builder fwdBuilder;
1772 log.debug("Populate direct routing entry for double-tagged host route {} at {}:{}",
1773 prefix, deviceId, outPort);
1774
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001775 try {
Charles Chan61c086d2019-07-26 17:46:15 -07001776 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, outerVlan, outPort, innerVlan, outerTpid,
1777 true, false);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001778 } catch (DeviceConfigNotFoundException e) {
1779 log.error(e.getMessage() + " Aborting populateDoubleTaggedRoute");
1780 return;
1781 }
1782 if (fwdBuilder == null) {
1783 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1784 deviceId, prefix);
1785 return;
1786 }
1787
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001788 int nextId = fwdBuilder.add().nextId();
1789 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
1790 log.debug("Direct routing rule for double-tagged host route {} populated. nextId={}", prefix, nextId);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001791 }, (objective, error) ->
1792 log.warn("Failed to populate direct routing rule for double-tagged host route {}: {}", prefix, error)
1793 );
1794 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
1795 rulePopulationCounter.incrementAndGet();
1796 }
1797
1798 /**
1799 * Revokes IP rules for a route that has double-tagged next hop.
1800 *
1801 * @param deviceId device ID of the device that next hop attaches to
1802 * @param prefix IP prefix of the route
1803 * @param hostMac MAC address of the next hop
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001804 * @param innerVlan inner Vlan ID of the next hop
1805 * @param outerVlan outer Vlan ID of the next hop
1806 * @param outerTpid outer TPID of the next hop
1807 * @param outPort port where the next hop attaches to
1808 */
1809 void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac,
Charles Chan61c086d2019-07-26 17:46:15 -07001810 VlanId innerVlan, VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1811 ForwardingObjective.Builder fwdBuilder;
1812 log.debug("Revoking direct routing entry for double-tagged host route {} at {}:{}",
1813 prefix, deviceId, outPort);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001814
Charles Chan61c086d2019-07-26 17:46:15 -07001815 try {
1816 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac, outerVlan, outPort, innerVlan, outerTpid,
1817 true, true);
1818 } catch (DeviceConfigNotFoundException e) {
1819 log.error(e.getMessage() + " Aborting revokeDoubleTaggedRoute");
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001820 return;
1821 }
Charles Chan61c086d2019-07-26 17:46:15 -07001822 if (fwdBuilder == null) {
1823 log.error("Aborting double-tagged host routing table entry due to error for dev:{} route:{}",
1824 deviceId, prefix);
1825 return;
1826 }
1827
1828 int nextId = fwdBuilder.remove().nextId();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001829 DefaultObjectiveContext context = new DefaultObjectiveContext(objective -> {
Charles Chan61c086d2019-07-26 17:46:15 -07001830 log.debug("Direct routing rule for double-tagged host route {} revoked. nextId={}", prefix, nextId);
1831
1832 // Try to remove next objective as well
1833 ImmutablePair<TrafficTreatment, TrafficSelector> treatmentAndMeta;
1834 try {
1835 treatmentAndMeta = getTreatmentAndMeta(deviceId, hostMac, outerVlan, outPort, innerVlan, outerTpid);
1836 } catch (DeviceConfigNotFoundException e) {
1837 log.error(e.getMessage() + " Aborting revokeDoubleTaggedRoute");
1838 return;
1839 }
1840
1841 if (treatmentAndMeta == null) {
1842 // Warning log will come from getTreatmentAndMeta method
1843 return;
1844 }
1845
1846 DefaultGroupHandler groupHandler = srManager.getGroupHandler(deviceId);
1847 if (groupHandler == null) {
1848 log.warn("Failed to revoke direct routing rule for double-tagged host route {}: " +
1849 "group handler not found for {}", prefix, deviceId);
1850 return;
1851 }
1852 groupHandler.removeGroupFromPort(outPort, treatmentAndMeta.getLeft(), treatmentAndMeta.getRight());
1853
1854 }, (objective, error) ->
1855 log.warn("Failed to revoke direct routing rule for double-tagged host route {}: {}", prefix, error)
1856 );
1857 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001858 }
1859
Jonghwan Hyun671a7ab2018-04-30 09:27:21 -07001860 /**
1861 * Checks whether the specified port has IP configuration or not.
1862 *
1863 * @param cp ConnectPoint to check the existance of IP configuration
1864 * @return true if the port has IP configuration; false otherwise.
1865 */
1866 private boolean hasIPConfiguration(ConnectPoint cp) {
1867 Set<Interface> interfaces = srManager.interfaceService.getInterfacesByPort(cp);
1868 return interfaces.stream().anyMatch(intf -> intf.ipAddressesList().size() > 0);
1869 }
Saurav Das9bf49582018-08-13 15:34:26 -07001870
1871 /**
1872 * Updates filtering rules for unconfigured ports on all devices for which
1873 * this controller instance is master.
1874 *
1875 * @param pushVlan true if the filtering rule requires a push vlan action
1876 * @param oldVlanId the vlanId to be removed
1877 * @param newVlanId the vlanId to be added
1878 */
1879 void updateSpecialVlanFilteringRules(boolean pushVlan, VlanId oldVlanId,
1880 VlanId newVlanId) {
1881 for (Device dev : srManager.deviceService.getAvailableDevices()) {
1882 if (srManager.mastershipService.isLocalMaster(dev.id())) {
1883 for (Port p : srManager.deviceService.getPorts(dev.id())) {
1884 if (!hasIPConfiguration(new ConnectPoint(dev.id(), p.number()))
1885 && p.isEnabled()) {
1886 updateSinglePortFilters(dev.id(), p.number(), pushVlan,
1887 oldVlanId, false);
1888 updateSinglePortFilters(dev.id(), p.number(), pushVlan,
1889 newVlanId, true);
1890 }
1891 }
1892 }
1893 }
1894 }
1895
Charles Chanf0f7b5c2018-08-29 14:55:53 -07001896 private boolean isIpv6Configured(DeviceId deviceId) {
1897 boolean isIpv6Configured;
1898 try {
1899 isIpv6Configured = (config.getRouterIpv6(deviceId) != null);
1900 } catch (DeviceConfigNotFoundException e) {
1901 isIpv6Configured = false;
1902 }
1903 return isIpv6Configured;
1904 }
sangho80f11cb2015-04-01 13:05:26 -07001905}