blob: 06f7ef062beb97d13b6110392b1adfa1e92f2afd [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-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 Ventre917127a2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Charles Chan09bf2692018-01-11 11:48:18 -080019import com.google.common.collect.Sets;
Saurav Das8a0732e2015-11-20 15:27:53 -080020import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070021import org.onlab.packet.Ethernet;
Charles Chan78ec5052017-12-05 21:07:38 -080022import org.onlab.packet.IPv6;
sanghob35a6192015-04-01 13:05:26 -070023import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080024import org.onlab.packet.Ip6Address;
25import org.onlab.packet.IpAddress;
sanghob35a6192015-04-01 13:05:26 -070026import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070028import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070029import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080030import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070033import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080034import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080035import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080036import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080037import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
38import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Dasc88d4662017-05-15 15:34:25 -070039import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070040import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sanghob35a6192015-04-01 13:05:26 -070041import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070042import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070044import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070048import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.FilteringObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070054import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070059import java.util.Collection;
60import java.util.Collections;
Saurav Das7bcbe702017-06-13 15:35:54 -070061import java.util.HashMap;
Saurav Das837e0bb2015-10-30 17:45:38 -070062import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070063import java.util.List;
Saurav Das7bcbe702017-06-13 15:35:54 -070064import java.util.Map;
Charles Chan2df0e8a2017-01-09 11:45:08 -080065import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070066import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070067import java.util.concurrent.atomic.AtomicLong;
Charles Chandd33be52018-02-26 21:33:25 -080068import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -070069
70import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080071import static org.onlab.packet.Ethernet.TYPE_ARP;
72import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan09bf2692018-01-11 11:48:18 -080073import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080074import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan09bf2692018-01-11 11:48:18 -080075import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
76import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080077import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan59cc16d2017-02-02 16:20:42 -080078import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070079
Charles Chane849c192016-01-11 18:28:54 -080080/**
81 * Populator of segment routing flow rules.
82 */
sanghob35a6192015-04-01 13:05:26 -070083public class RoutingRulePopulator {
Charles Chan78ec5052017-12-05 21:07:38 -080084 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
85
86 private static final int ARP_NDP_PRIORITY = 30000;
sanghob35a6192015-04-01 13:05:26 -070087
sangho20eff1d2015-04-13 15:15:58 -070088 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070089 private SegmentRoutingManager srManager;
90 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070091
sanghob35a6192015-04-01 13:05:26 -070092 /**
93 * Creates a RoutingRulePopulator object.
94 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070095 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070096 */
Charles Chan65238242017-06-22 18:03:14 -070097 RoutingRulePopulator(SegmentRoutingManager srManager) {
sanghob35a6192015-04-01 13:05:26 -070098 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070099 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -0700100 this.rulePopulationCounter = new AtomicLong(0);
101 }
102
103 /**
104 * Resets the population counter.
105 */
Charles Chan65238242017-06-22 18:03:14 -0700106 void resetCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700107 rulePopulationCounter.set(0);
108 }
109
110 /**
111 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -0700112 *
113 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700114 */
Charles Chan65238242017-06-22 18:03:14 -0700115 long getCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700116 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700117 }
118
119 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700120 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700121 * switch.
sanghob35a6192015-04-01 13:05:26 -0700122 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700123 * @param deviceId device ID of the device that next hop attaches to
124 * @param prefix IP prefix of the route
125 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800126 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700127 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700128 */
Charles Chan2fde6d42017-08-23 14:46:43 -0700129 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800130 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700131 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chan1cdecff2016-10-27 14:19:48 -0700132 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800133 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800134 try {
Saurav Das961beb22017-03-29 19:09:17 -0700135 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
136 hostVlanId, outPort, false);
Charles Chan0b4e6182015-11-03 10:42:14 -0800137 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700138 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan0b4e6182015-11-03 10:42:14 -0800139 return;
140 }
Saurav Das59232cf2016-04-27 18:35:50 -0700141 if (fwdBuilder == null) {
Saurav Das76ae6812017-03-15 15:15:14 -0700142 log.warn("Aborting host routing table entry due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700143 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700144 return;
145 }
Charles Chan2fde6d42017-08-23 14:46:43 -0700146
147 int nextId = fwdBuilder.add().nextId();
Charles Chand2990362016-04-18 13:44:03 -0700148 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan2fde6d42017-08-23 14:46:43 -0700149 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
150 prefix, nextId),
Charles Chand2990362016-04-18 13:44:03 -0700151 (objective, error) ->
Saurav Das7bcbe702017-06-13 15:35:54 -0700152 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das76ae6812017-03-15 15:15:14 -0700153 prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700154 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800155 rulePopulationCounter.incrementAndGet();
156 }
157
Charles Chane849c192016-01-11 18:28:54 -0800158 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700159 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800160 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700161 * @param deviceId device ID of the device that next hop attaches to
162 * @param prefix IP prefix of the route
163 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800164 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700165 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800166 */
Charles Chan2fde6d42017-08-23 14:46:43 -0700167 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800168 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700169 log.debug("Revoke IP table entry for route {} at {}:{}",
170 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800171 ForwardingObjective.Builder fwdBuilder;
172 try {
Saurav Das961beb22017-03-29 19:09:17 -0700173 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
174 hostVlanId, outPort, true);
Charles Chan68aa62d2015-11-09 16:37:23 -0800175 } catch (DeviceConfigNotFoundException e) {
176 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
177 return;
178 }
Charles Chan458b8262016-11-30 11:55:05 -0800179 if (fwdBuilder == null) {
180 log.warn("Aborting host routing table entries due "
181 + "to error for dev:{} route:{}", deviceId, prefix);
182 return;
183 }
Charles Chand2990362016-04-18 13:44:03 -0700184 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700185 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700186 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700187 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700188 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800189 }
190
Charles Chan1cdecff2016-10-27 14:19:48 -0700191 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800192 * Returns a forwarding objective builder for routing rules.
193 * <p>
194 * The forwarding objective routes packets destined to a given prefix to
195 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700196 *
197 * @param deviceId device ID
198 * @param prefix prefix that need to be routed
199 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800200 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700201 * @param outPort port where the nexthop attaches to
Saurav Das961beb22017-03-29 19:09:17 -0700202 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chan1cdecff2016-10-27 14:19:48 -0700203 * @return forwarding objective builder
204 * @throws DeviceConfigNotFoundException if given device is not configured
205 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800206 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700207 DeviceId deviceId, IpPrefix prefix,
Saurav Das961beb22017-03-29 19:09:17 -0700208 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
209 boolean revoke)
Charles Chan68aa62d2015-11-09 16:37:23 -0800210 throws DeviceConfigNotFoundException {
211 MacAddress deviceMac;
212 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800213
Charles Chan7ffd81f2017-02-08 15:52:08 -0800214 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
215 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
216 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
217 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700218
Charles Chan7ffd81f2017-02-08 15:52:08 -0800219 // Create route selector
220 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
221
222 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700223 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700224 tbuilder.deferred()
225 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800226 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700227 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800228
Charles Chan7ffd81f2017-02-08 15:52:08 -0800229 // Create route meta
230 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800231
Charles Chan7ffd81f2017-02-08 15:52:08 -0800232 // Adjust the meta according to VLAN configuration
233 if (taggedVlans.contains(hostVlanId)) {
234 tbuilder.setVlanId(hostVlanId);
235 } else if (hostVlanId.equals(VlanId.NONE)) {
236 if (untaggedVlan != null) {
237 mbuilder.matchVlanId(untaggedVlan);
238 } else if (nativeVlan != null) {
239 mbuilder.matchVlanId(nativeVlan);
240 } else {
Charles Chan65238242017-06-22 18:03:14 -0700241 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
242 hostMac, hostVlanId, connectPoint);
243 return null;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800244 }
245 } else {
Saurav Das961beb22017-03-29 19:09:17 -0700246 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
247 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das59232cf2016-04-27 18:35:50 -0700248 return null;
249 }
Saurav Das961beb22017-03-29 19:09:17 -0700250 // if the objective is to revoke an existing rule, and for some reason
251 // the next-objective does not exist, then a new one should not be created
Charles Chan7ffd81f2017-02-08 15:52:08 -0800252 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das961beb22017-03-29 19:09:17 -0700253 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800254 if (portNextObjId == -1) {
255 // Warning log will come from getPortNextObjective method
256 return null;
257 }
258
Charles Chan68aa62d2015-11-09 16:37:23 -0800259 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800260 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800261 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800262 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700263 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800264 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700265 }
266
267 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700268 * Populates IP flow rules for all the given prefixes reachable from the
269 * destination switch(es).
sanghob35a6192015-04-01 13:05:26 -0700270 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700271 * @param targetSw switch where rules are to be programmed
272 * @param subnets subnets/prefixes being added
273 * @param destSw1 destination switch where the prefixes are reachable
274 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
275 * Should be null if there is no paired destination switch (by config)
276 * or if the given prefixes are reachable only via destSw1
277 * @param nextHops a map containing a set of next-hops for each destination switch.
278 * If destSw2 is not null, then this map must contain an
279 * entry for destSw2 with its next-hops from the targetSw
280 * (although the next-hop set may be empty in certain scenarios).
281 * If destSw2 is null, there should not be an entry in this
282 * map for destSw2.
sanghob35a6192015-04-01 13:05:26 -0700283 * @return true if all rules are set successfully, false otherwise
284 */
Charles Chan65238242017-06-22 18:03:14 -0700285 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das7bcbe702017-06-13 15:35:54 -0700286 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700287 for (IpPrefix subnet : subnets) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700288 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sanghob35a6192015-04-01 13:05:26 -0700289 return false;
290 }
291 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700292 return true;
293 }
sanghob35a6192015-04-01 13:05:26 -0700294
Charles Chan93e71ba2016-04-29 14:38:22 -0700295 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700296 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chan93e71ba2016-04-29 14:38:22 -0700297 *
298 * @param subnets subnet being removed
299 * @return true if all rules are removed successfully, false otherwise
300 */
Charles Chan65238242017-06-22 18:03:14 -0700301 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700302 for (IpPrefix subnet : subnets) {
303 if (!revokeIpRuleForRouter(subnet)) {
304 return false;
305 }
306 }
sanghob35a6192015-04-01 13:05:26 -0700307 return true;
308 }
309
310 /**
Saurav Das25190812016-05-27 13:54:07 -0700311 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das7bcbe702017-06-13 15:35:54 -0700312 * is reachable via destination device(s).
sanghob35a6192015-04-01 13:05:26 -0700313 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700314 * @param targetSw target device ID to set the rules
315 * @param ipPrefix the IP prefix
316 * @param destSw1 destination switch where the prefixes are reachable
317 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
318 * Should be null if there is no paired destination switch (by config)
319 * or if the given prefixes are reachable only via destSw1
320 * @param nextHops map of destination switches and their next-hops.
321 * Should only contain destination switches that are
322 * actually meant to be routed to. If destSw2 is null, there
323 * should not be an entry for destSw2 in this map.
sanghob35a6192015-04-01 13:05:26 -0700324 * @return true if all rules are set successfully, false otherwise
325 */
Charles Chan65238242017-06-22 18:03:14 -0700326 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das7bcbe702017-06-13 15:35:54 -0700327 IpPrefix ipPrefix, DeviceId destSw1,
328 DeviceId destSw2,
329 Map<DeviceId, Set<DeviceId>> nextHops) {
330 int segmentId1, segmentId2 = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -0800331 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800332 if (ipPrefix.isIp4()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700333 segmentId1 = config.getIPv4SegmentId(destSw1);
334 if (destSw2 != null) {
335 segmentId2 = config.getIPv4SegmentId(destSw2);
336 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800337 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700338 segmentId1 = config.getIPv6SegmentId(destSw1);
339 if (destSw2 != null) {
340 segmentId2 = config.getIPv6SegmentId(destSw2);
341 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800342 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800343 } catch (DeviceConfigNotFoundException e) {
344 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
345 return false;
346 }
sanghob35a6192015-04-01 13:05:26 -0700347
Pier Ventree0ae7a32016-11-23 09:57:42 -0800348 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800349 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700350
Charles Chan68aa62d2015-11-09 16:37:23 -0800351 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700352 DestinationSet ds;
Charles Chan68aa62d2015-11-09 16:37:23 -0800353 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700354
Saurav Das7bcbe702017-06-13 15:35:54 -0700355 if (destSw2 == null) {
356 // single dst - create destination set based on next-hop
Saurav Dasa4020382018-02-14 14:14:54 -0800357 // If the next hop is the same as the final destination, then MPLS
358 // label is not set.
Saurav Das7bcbe702017-06-13 15:35:54 -0700359 Set<DeviceId> nhd1 = nextHops.get(destSw1);
360 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
361 tbuilder.immediate().decNwTtl();
Saurav Dasa4020382018-02-14 14:14:54 -0800362 ds = new DestinationSet(false, false, destSw1);
Saurav Das7bcbe702017-06-13 15:35:54 -0700363 treatment = tbuilder.build();
364 } else {
Saurav Dasa4020382018-02-14 14:14:54 -0800365 ds = new DestinationSet(false, false, segmentId1, destSw1);
Saurav Das7bcbe702017-06-13 15:35:54 -0700366 treatment = null;
367 }
368 } else {
369 // dst pair - IP rules for dst-pairs are always from other edge nodes
370 // the destination set needs to have both destinations, even if there
371 // are no next hops to one of them
Saurav Dasa4020382018-02-14 14:14:54 -0800372 ds = new DestinationSet(false, false, segmentId1, destSw1,
373 segmentId2, destSw2);
Charles Chan68aa62d2015-11-09 16:37:23 -0800374 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700375 }
376
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 // setup metadata to pass to nextObjective - indicate the vlan on egress
378 // if needed by the switch pipeline. Since neighbor sets are always to
379 // other neighboring routers, there is no subnet assigned on those ports.
380 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800381 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das7bcbe702017-06-13 15:35:54 -0700382 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700383 if (grpHandler == null) {
384 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700385 + "not found", targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700386 return false;
387 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800388
Saurav Das7bcbe702017-06-13 15:35:54 -0700389 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
Saurav Dasa4020382018-02-14 14:14:54 -0800390 metabuilder.build(), false);
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700392 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho834e4b02015-05-01 09:38:25 -0700393 return false;
394 }
395
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700396 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
397 .builder()
398 .fromApp(srManager.appId)
399 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800402 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700403 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800404 if (treatment != null) {
405 fwdBuilder.withTreatment(treatment);
406 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700407 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700408 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chand2990362016-04-18 13:44:03 -0700409 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700410 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700411 ipPrefix, targetSw),
Charles Chand2990362016-04-18 13:44:03 -0700412 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700413 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700414 ipPrefix, error, targetSw));
415 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700416 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700417
418 return true;
419 }
420
sanghob35a6192015-04-01 13:05:26 -0700421 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700422 * Revokes IP flow rules for the router IP address.
423 *
424 * @param ipPrefix the IP address of the destination router
425 * @return true if all rules are removed successfully, false otherwise
426 */
Charles Chan65238242017-06-22 18:03:14 -0700427 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800428 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700429 TrafficSelector selector = sbuilder.build();
430 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
431
432 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
433 .builder()
434 .fromApp(srManager.appId)
435 .makePermanent()
436 .withSelector(selector)
437 .withTreatment(dummyTreatment)
438 .withPriority(getPriorityFromPrefix(ipPrefix))
439 .withFlag(ForwardingObjective.Flag.SPECIFIC);
440
Charles Chan5f7577e2018-01-30 13:31:30 -0800441 srManager.deviceService.getAvailableDevices().forEach(device -> {
442 if (srManager.mastershipService.isLocalMaster(device.id())) {
443 ObjectiveContext context = new DefaultObjectiveContext(
444 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
445 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
446 ipPrefix, device.id(), error));
447 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
448 } else {
449 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
450 }
451 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700452
453 return true;
454 }
455
456 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800457 * Populates MPLS flow rules in the target device to point towards the
458 * destination device.
459 *
460 * @param targetSwId target device ID of the switch to set the rules
461 * @param destSwId destination switch device ID
462 * @param nextHops next hops switch ID list
463 * @param routerIp the router ip of the destination switch
464 * @return true if all rules are set successfully, false otherwise
465 */
466 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
467 Set<DeviceId> nextHops, IpAddress routerIp) {
468 int segmentId;
469 try {
470 if (routerIp.isIp4()) {
471 segmentId = config.getIPv4SegmentId(destSwId);
472 } else {
473 segmentId = config.getIPv6SegmentId(destSwId);
474 }
475 } catch (DeviceConfigNotFoundException e) {
476 log.warn(e.getMessage() + " Aborting populateMplsRule.");
477 return false;
478 }
479
480 List<ForwardingObjective> fwdObjs = new ArrayList<>();
481 Collection<ForwardingObjective> fwdObjsMpls;
482 // Generates the transit rules used by the standard "routing".
483 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
484 routerIp, true);
485 if (fwdObjsMpls.isEmpty()) {
486 return false;
487 }
488 fwdObjs.addAll(fwdObjsMpls);
489
490 // Generates the transit rules used by the MPLS Pwaas.
491 int pwSrLabel;
492 try {
493 pwSrLabel = config.getPWRoutingLabel(destSwId);
494 } catch (DeviceConfigNotFoundException e) {
495 log.warn(e.getMessage()
496 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
497 return false;
498 }
499 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
500 routerIp, false);
501 if (fwdObjsMpls.isEmpty()) {
502 return false;
503 }
504 fwdObjs.addAll(fwdObjsMpls);
505
506 for (ForwardingObjective fwdObj : fwdObjs) {
507 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
508 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
509 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
510 rulePopulationCounter.incrementAndGet();
511 }
512
513 return true;
514 }
515
516 /**
517 * Differentiates between popping and swapping labels when building an MPLS
518 * forwarding objective.
Pier Ventre917127a2016-10-31 16:49:19 -0700519 *
520 * @param targetSwId the target sw
521 * @param destSwId the destination sw
522 * @param nextHops the set of next hops
Saurav Dasa4020382018-02-14 14:14:54 -0800523 * @param segmentId the segmentId to match representing the destination
524 * switch
525 * @param routerIp the router ip representing the destination switch
Pier Ventre917127a2016-10-31 16:49:19 -0700526 * @return a collection of fwdobjective
527 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700528 private Collection<ForwardingObjective> handleMpls(
529 DeviceId targetSwId,
530 DeviceId destSwId,
531 Set<DeviceId> nextHops,
532 int segmentId,
533 IpAddress routerIp,
534 boolean isMplsBos) {
Pier Ventre917127a2016-10-31 16:49:19 -0700535
536 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
537 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800538 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700539 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
540 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
541 sbuilder.matchMplsBos(isMplsBos);
542 TrafficSelector selector = sbuilder.build();
543
544 // setup metadata to pass to nextObjective - indicate the vlan on egress
545 // if needed by the switch pipeline. Since mpls next-hops are always to
546 // other neighboring routers, there is no subnet assigned on those ports.
547 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800548 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700549
550 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
551 // If the next hop is the destination router for the segment, do pop
552 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das7bcbe702017-06-13 15:35:54 -0700553 + "label {} in switch {} with pop to next-hops {}",
554 segmentId, targetSwId, nextHops);
Saurav Das961beb22017-03-29 19:09:17 -0700555 ForwardingObjective.Builder fwdObjNoBosBuilder =
556 getMplsForwardingObjective(targetSwId,
557 nextHops,
558 true,
559 isMplsBos,
560 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700561 routerIp,
Saurav Dasa4020382018-02-14 14:14:54 -0800562 segmentId,
Saurav Dasc88d4662017-05-15 15:34:25 -0700563 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700564 // Error case, we cannot handle, exit.
565 if (fwdObjNoBosBuilder == null) {
566 return Collections.emptyList();
567 }
568 fwdObjBuilders.add(fwdObjNoBosBuilder);
569
570 } else {
Saurav Dasa4020382018-02-14 14:14:54 -0800571 // next hop is not destination, irrespective of the number of next
572 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das7bcbe702017-06-13 15:35:54 -0700573 log.debug("Installing MPLS forwarding objective for "
574 + "label {} in switch {} without pop to next-hops {}",
575 segmentId, targetSwId, nextHops);
Saurav Das961beb22017-03-29 19:09:17 -0700576 ForwardingObjective.Builder fwdObjNoBosBuilder =
577 getMplsForwardingObjective(targetSwId,
578 nextHops,
579 false,
580 isMplsBos,
581 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700582 routerIp,
Saurav Dasa4020382018-02-14 14:14:54 -0800583 segmentId,
Saurav Dasc88d4662017-05-15 15:34:25 -0700584 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700585 // Error case, we cannot handle, exit.
586 if (fwdObjNoBosBuilder == null) {
587 return Collections.emptyList();
588 }
589 fwdObjBuilders.add(fwdObjNoBosBuilder);
590
591 }
592
593 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
594 // We add the final property to the fwdObjs.
595 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre917127a2016-10-31 16:49:19 -0700596 ((Builder) ((Builder) fwdObjBuilder
597 .fromApp(srManager.appId)
598 .makePermanent())
599 .withSelector(selector)
600 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
601 .withFlag(ForwardingObjective.Flag.SPECIFIC);
602
603 ObjectiveContext context = new DefaultObjectiveContext(
604 (objective) ->
605 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
606 objective.id(), segmentId, targetSwId),
607 (objective, error) ->
608 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
609 objective.id(), segmentId, error, targetSwId));
610
611 ForwardingObjective fob = fwdObjBuilder.add(context);
612 fwdObjs.add(fob);
Pier Ventre917127a2016-10-31 16:49:19 -0700613 }
614
615 return fwdObjs;
616 }
617
618 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800619 * Returns a Forwarding Objective builder for the MPLS rule that references
620 * the desired Next Objective. Creates a DestinationSet that allows the
621 * groupHandler to create or find the required next objective.
sanghob35a6192015-04-01 13:05:26 -0700622 *
Saurav Dasa4020382018-02-14 14:14:54 -0800623 * @param targetSw the target sw
624 * @param nextHops the set of next hops
625 * @param phpRequired true if penultimate-hop-popping is required
626 * @param isBos true if matched label is bottom-of-stack
627 * @param meta metadata for creating next objective
628 * @param routerIp the router ip representing the destination switch
629 * @param destSw the destination sw
630 * @return the mpls forwarding objective builder
sanghob35a6192015-04-01 13:05:26 -0700631 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800632 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das7bcbe702017-06-13 15:35:54 -0700633 DeviceId targetSw,
Saurav Das8a0732e2015-11-20 15:27:53 -0800634 Set<DeviceId> nextHops,
635 boolean phpRequired,
636 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800637 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700638 IpAddress routerIp,
Saurav Dasa4020382018-02-14 14:14:54 -0800639 int segmentId,
Saurav Dasc88d4662017-05-15 15:34:25 -0700640 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800641
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700642 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
643 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700644
645 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Dasa4020382018-02-14 14:14:54 -0800646 DestinationSet ds = null;
647 boolean simple = false;
sanghob35a6192015-04-01 13:05:26 -0700648 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800649 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700650 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700651 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700652 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800653 if (routerIp.isIp4()) {
654 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
655 } else {
656 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
657 }
658 tbuilder.decNwTtl();
Saurav Dasa4020382018-02-14 14:14:54 -0800659 // standard case -> BoS == True; pop results in IP packet and forwarding
660 // is via an ECMP group
661 ds = new DestinationSet(false, false, destSw);
sanghob35a6192015-04-01 13:05:26 -0700662 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800663 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
664 .decMplsTtl();
Saurav Dasa4020382018-02-14 14:14:54 -0800665 // double-label case -> BoS == False, pop results in MPLS packet
666 // depending on configuration we can ECMP this packet or choose one output
667 if (srManager.getMplsEcmp()) {
668 ds = new DestinationSet(true, false, destSw);
669 } else {
670 ds = new DestinationSet(true, false, destSw);
671 simple = true;
672 }
sanghob35a6192015-04-01 13:05:26 -0700673 }
674 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800675 // swap with self case - SR CONTINUE
Saurav Dasa4020382018-02-14 14:14:54 -0800676 log.debug("getMplsForwardingObjective: swap with self");
sangho1e575652015-05-14 00:39:53 -0700677 tbuilder.deferred().decMplsTtl();
Saurav Dasa4020382018-02-14 14:14:54 -0800678 // swap results in MPLS packet with same BoS bit regardless of bit value
679 // depending on configuration we can ECMP this packet or choose one output
Saurav Dase0d4c872018-03-05 14:37:16 -0800680 // XXX reconsider types
Saurav Dasa4020382018-02-14 14:14:54 -0800681 if (srManager.getMplsEcmp()) {
682 ds = new DestinationSet(false, true, segmentId, destSw);
683 } else {
684 ds = new DestinationSet(false, true, segmentId, destSw);
685 simple = true;
686 }
sanghob35a6192015-04-01 13:05:26 -0700687 }
688
Saurav Das8a0732e2015-11-20 15:27:53 -0800689 fwdBuilder.withTreatment(tbuilder.build());
Saurav Dasa4020382018-02-14 14:14:54 -0800690 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
691 targetSw, ds);
Saurav Das7bcbe702017-06-13 15:35:54 -0700692 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
693 if (gh == null) {
694 log.warn("getNextObjectiveId query - groupHandler for device {} "
695 + "not found", targetSw);
696 return null;
697 }
Saurav Dasa4020382018-02-14 14:14:54 -0800698
Saurav Das7bcbe702017-06-13 15:35:54 -0700699 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
700 dstNextHops.put(destSw, nextHops);
Saurav Dasa4020382018-02-14 14:14:54 -0800701 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das8a0732e2015-11-20 15:27:53 -0800702 if (nextId <= 0) {
Saurav Dasa4020382018-02-14 14:14:54 -0800703 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das8a0732e2015-11-20 15:27:53 -0800704 return null;
Saurav Das25190812016-05-27 13:54:07 -0700705 } else {
Saurav Dasa4020382018-02-14 14:14:54 -0800706 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
707 nextId, targetSw, ds);
sanghob35a6192015-04-01 13:05:26 -0700708 }
709
Saurav Das8a0732e2015-11-20 15:27:53 -0800710 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700711 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700712 }
713
714 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700715 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700716 * dstMac corresponding to the router's MAC address. For those pipelines
717 * that need to internally assign vlans to untagged packets, this method
718 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700719 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800720 * Note that the vlan assignment and filter programming should only be done by
721 * the master for a switch. This method is typically called at deviceAdd and
722 * programs filters only for the enabled ports of the device. For port-updates,
723 * that enable/disable ports after device add, singlePortFilter methods should
724 * be called.
sanghob35a6192015-04-01 13:05:26 -0700725 *
Saurav Das822c4e22015-10-23 10:51:11 -0700726 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800727 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700728 */
Charles Chan65238242017-06-22 18:03:14 -0700729 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700730 log.debug("Installing per-port filtering objective for untagged "
731 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800732
Saurav Das59232cf2016-04-27 18:35:50 -0700733 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800734 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700735 log.warn("Device {} ports not available. Unable to add MacVlan filters",
736 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800737 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700738 }
Saurav Das018605f2017-02-18 14:05:44 -0800739 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700740 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700741 if (!port.isEnabled()) {
742 disabledPorts++;
743 continue;
744 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800745 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800746 filteredPorts++;
747 } else {
748 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700749 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700750 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700751 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800752 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800753 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800754 errorPorts, filteredPorts);
755 }
756
757 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800758 * Creates or removes filtering objectives for a single port. Should only be
759 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800760 *
761 * @param deviceId device identifier
762 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800763 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800764 * @return true if no errors occurred during the build of the filtering objective
765 */
Charles Chan65238242017-06-22 18:03:14 -0700766 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800767 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
768 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
769 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
770 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
771
772 if (taggedVlans.size() != 0) {
773 // Filter for tagged vlans
774 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800775 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800776 return false;
777 }
778 if (nativeVlan != null) {
779 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800780 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800781 return false;
782 }
783 }
784 } else if (untaggedVlan != null) {
785 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800786 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800787 return false;
788 }
789 } else {
Saurav Dasceccf242017-08-03 18:30:35 -0700790 // Unconfigured port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800791 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800792 return false;
793 }
794 }
795 return true;
796 }
797
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700798 /**
799 * Updates filtering objectives for a single port. Should only be called by
800 * the master for a switch
801 * @param deviceId device identifier
802 * @param portNum port identifier for port to be filtered
803 * @param pushVlan true to push vlan, false otherwise
804 * @param vlanId vlan identifier
805 * @param install true to install the filtering objective, false to remove
806 */
807 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
808 boolean pushVlan, VlanId vlanId, boolean install) {
809 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
810 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
811 deviceId, portNum, vlanId);
812 }
813 }
814
Charles Chan7e4f8192017-02-26 22:59:35 -0800815 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
816 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800817 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800818 if (fob == null) {
819 // error encountered during build
820 return false;
821 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700822 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700823 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800824 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800825 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700826 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800827 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700828 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800829 if (install) {
830 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
831 } else {
832 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800833 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800834 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800835 }
836
Charles Chan7ffd81f2017-02-08 15:52:08 -0800837 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
838 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800839 MacAddress deviceMac;
840 try {
841 deviceMac = config.getDeviceMac(deviceId);
842 } catch (DeviceConfigNotFoundException e) {
843 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
844 return null;
845 }
Saurav Das018605f2017-02-18 14:05:44 -0800846 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
847 fob.withKey(Criteria.matchInPort(portnum))
848 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800849 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800850
Charles Chanc550f2e2017-12-19 19:55:57 -0800851 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
852
Charles Chan7ffd81f2017-02-08 15:52:08 -0800853 if (pushVlan) {
854 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chanc550f2e2017-12-19 19:55:57 -0800855 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800856 } else {
857 fob.addCondition(Criteria.matchVlanId(vlanId));
858 }
859
Charles Chanc550f2e2017-12-19 19:55:57 -0800860 // NOTE: Some switch hardware share the same filtering flow among different ports.
861 // We use this metadata to let the driver know that there is no more enabled port
862 // within the same VLAN on this device.
Charles Chandd33be52018-02-26 21:33:25 -0800863 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chanc550f2e2017-12-19 19:55:57 -0800864 tBuilder.wipeDeferred();
865 }
866
867 fob.withMeta(tBuilder.build());
868
Saurav Das018605f2017-02-18 14:05:44 -0800869 fob.permit().fromApp(srManager.appId);
870 return fob;
sanghob35a6192015-04-01 13:05:26 -0700871 }
872
873 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700874 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700875 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700876 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700877 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700878 *
Saurav Das822c4e22015-10-23 10:51:11 -0700879 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700880 */
Charles Chan65238242017-06-22 18:03:14 -0700881 void populateIpPunts(DeviceId deviceId) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700882 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chan78ec5052017-12-05 21:07:38 -0800883 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800884 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800885 routerIpv4 = config.getRouterIpv4(deviceId);
886 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan78ec5052017-12-05 21:07:38 -0800887 routerLinkLocalIpv6 = Ip6Address.valueOf(
888 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
889
Saurav Das7bcbe702017-06-13 15:35:54 -0700890 if (config.isPairedEdge(deviceId)) {
891 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
892 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
893 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800894 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800895 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800896 return;
897 }
898
Saurav Das837e0bb2015-10-30 17:45:38 -0700899 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
900 log.debug("Not installing port-IP punts - not the master for dev:{} ",
901 deviceId);
902 return;
903 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800904 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
905 allIps.add(routerIpv4);
Charles Chan78ec5052017-12-05 21:07:38 -0800906 allIps.add(routerLinkLocalIpv6);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800907 if (routerIpv6 != null) {
908 allIps.add(routerIpv6);
909 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700910 if (pairRouterIpv4 != null) {
911 allIps.add(pairRouterIpv4);
912 }
913 if (pairRouterIpv6 != null) {
914 allIps.add(pairRouterIpv6);
915 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800916 for (IpAddress ipaddr : allIps) {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700917 populateSingleIpPunts(deviceId, ipaddr);
918 }
919 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800920
Jonghwan Hyun42fe1052017-08-25 17:48:36 -0700921 /**
922 * Creates a forwarding objective to punt all IP packets, destined to the
923 * specified IP address, which should be router's port IP address.
924 *
925 * @param deviceId the switch dpid for the router
926 * @param ipAddress the IP address of the router's port
927 */
928 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
929 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
930 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
931
932 srManager.packetService.requestPackets(sbuilder.build(),
933 PacketPriority.CONTROL, srManager.appId, optDeviceId);
934 }
935
936 /**
937 * Removes a forwarding objective to punt all IP packets, destined to the
938 * specified IP address, which should be router's port IP address.
939 *
940 * @param deviceId the switch dpid for the router
941 * @param ipAddress the IP address of the router's port
942 */
943 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
944 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
945 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
946
947 try {
948 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
949 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
950 srManager.packetService.cancelPackets(sbuilder.build(),
951 PacketPriority.CONTROL, srManager.appId, optDeviceId);
952 }
953 } catch (DeviceConfigNotFoundException e) {
954 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das822c4e22015-10-23 10:51:11 -0700955 }
sanghob35a6192015-04-01 13:05:26 -0700956 }
957
Charles Chan68aa62d2015-11-09 16:37:23 -0800958 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800959 * Method to build IPv4 or IPv6 selector.
960 *
961 * @param addressToMatch the address to match
962 */
963 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
964 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
965 }
966
967 /**
968 * Method to build IPv4 or IPv6 selector.
969 *
970 * @param prefixToMatch the prefix to match
971 */
972 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
973 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700974 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800975 if (prefixToMatch.isIp4()) {
976 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
977 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
978 return selectorBuilder;
979 }
Pier Ventre917127a2016-10-31 16:49:19 -0700980 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800981 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
982 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
983 return selectorBuilder;
984 }
985
986 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800987 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
988 * Furthermore, these are applied only by the master instance. Deferred actions
989 * are not cleared such that packets can be flooded in the cross connect use case
990 *
991 * @param deviceId the switch dpid for the router
992 */
Charles Chan65238242017-06-22 18:03:14 -0700993 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700994 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800995 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
996 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
997 deviceId);
998 return;
999 }
1000
Charles Chan65238242017-06-22 18:03:14 -07001001 ForwardingObjective fwdObj;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001002 // We punt all ARP packets towards the controller.
Charles Chan78ec5052017-12-05 21:07:38 -08001003 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001004 .add(new ObjectiveContext() {
1005 @Override
1006 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -07001007 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001008 deviceId, error);
1009 }
1010 });
Charles Chan65238242017-06-22 18:03:14 -07001011 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001012
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001013 // We punt all NDP packets towards the controller.
Charles Chan09bf2692018-01-11 11:48:18 -08001014 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1015 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1016 @Override
1017 public void onError(Objective objective, ObjectiveError error) {
1018 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1019 deviceId, error);
1020 }
1021 });
1022 srManager.flowObjectiveService.forward(deviceId, obj);
1023 });
Charles Chan65238242017-06-22 18:03:14 -07001024
1025 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1026 ForwardingObjective pairFwdObj;
1027 // Do not punt ARP packets from pair port
1028 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1029 .add(new ObjectiveContext() {
1030 @Override
1031 public void onError(Objective objective, ObjectiveError error) {
1032 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1033 deviceId, error);
1034 }
1035 });
1036 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1037
1038 // Do not punt NDP packets from pair port
Charles Chan09bf2692018-01-11 11:48:18 -08001039 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1040 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1041 @Override
1042 public void onError(Objective objective, ObjectiveError error) {
1043 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1044 deviceId, error);
1045 }
1046 });
1047 srManager.flowObjectiveService.forward(deviceId, obj);
1048 });
Charles Chan65238242017-06-22 18:03:14 -07001049
1050 // Do not forward DAD packets from pair port
1051 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1052 .add(new ObjectiveContext() {
1053 @Override
1054 public void onError(Objective objective, ObjectiveError error) {
1055 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1056 deviceId, error);
1057 }
1058 });
1059 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1060 });
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001061 }
1062
Charles Chan65238242017-06-22 18:03:14 -07001063 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1064 TrafficTreatment treatment, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001065 return DefaultForwardingObjective.builder()
Charles Chan65238242017-06-22 18:03:14 -07001066 .withPriority(priority)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001067 .withSelector(selector)
1068 .fromApp(srManager.appId)
1069 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan65238242017-06-22 18:03:14 -07001070 .withTreatment(treatment)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001071 .makePermanent();
1072 }
1073
Charles Chan65238242017-06-22 18:03:14 -07001074 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001075 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1076 sBuilder.matchEthType(TYPE_ARP);
Charles Chan65238242017-06-22 18:03:14 -07001077 if (port != null) {
1078 sBuilder.matchInPort(port);
1079 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001080
Charles Chan65238242017-06-22 18:03:14 -07001081 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1082 if (punt) {
1083 tBuilder.punt();
1084 }
1085 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001086 }
1087
Charles Chan09bf2692018-01-11 11:48:18 -08001088 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1089 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001090
Charles Chan09bf2692018-01-11 11:48:18 -08001091 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1092 .forEach(type -> {
1093 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1094 sBuilder.matchEthType(TYPE_IPV6)
1095 .matchIPProtocol(PROTOCOL_ICMP6)
1096 .matchIcmpv6Type(type);
1097 if (port != null) {
1098 sBuilder.matchInPort(port);
1099 }
1100
1101 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1102 if (punt) {
1103 tBuilder.punt();
1104 }
1105
1106 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1107 });
1108
1109 return result;
Charles Chan65238242017-06-22 18:03:14 -07001110 }
1111
1112 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1113 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1114 sBuilder.matchEthType(TYPE_IPV6)
Charles Chanc7e36aa2017-08-07 12:39:03 -07001115 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1116 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1117 // .matchIPProtocol(PROTOCOL_ICMP6)
1118 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan65238242017-06-22 18:03:14 -07001119 if (port != null) {
1120 sBuilder.matchInPort(port);
1121 }
1122
1123 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1124 tBuilder.wipeDeferred();
1125 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001126 }
1127
1128 /**
Charles Chan68aa62d2015-11-09 16:37:23 -08001129 * Populates a forwarding objective to send packets that miss other high
1130 * priority Bridging Table entries to a group that contains all ports of
1131 * its subnet.
1132 *
Charles Chan68aa62d2015-11-09 16:37:23 -08001133 * @param deviceId switch ID to set the rules
1134 */
Charles Chan65238242017-06-22 18:03:14 -07001135 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001136 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001137 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chan68aa62d2015-11-09 16:37:23 -08001138 });
1139 }
1140
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001141 /**
1142 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1143 * @param deviceId switch ID to set the rule
1144 * @param vlanId vlan ID to specify the subnet
1145 * @param install true to install the rule, false to revoke the rule
1146 */
1147 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1148 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1149
1150 if (nextId < 0) {
1151 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1152 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1153 return;
1154 }
1155
1156 // Driver should treat objective with MacAddress.NONE as the
1157 // subnet broadcast rule
1158 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1159 sbuilder.matchVlanId(vlanId);
1160 sbuilder.matchEthDst(MacAddress.NONE);
1161
1162 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1163 fob.withFlag(Flag.SPECIFIC)
1164 .withSelector(sbuilder.build())
1165 .nextStep(nextId)
1166 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1167 .fromApp(srManager.appId)
1168 .makePermanent();
1169 ObjectiveContext context = new DefaultObjectiveContext(
1170 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1171 (objective, error) ->
1172 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1173
1174 if (install) {
1175 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1176 } else {
1177 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1178 }
1179 }
1180
Charles Chan5270ed02016-01-30 23:22:37 -08001181 private int getPriorityFromPrefix(IpPrefix prefix) {
1182 return (prefix.isIp4()) ?
1183 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1184 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001185 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001186
1187 /**
1188 * Update Forwarding objective for each host and IP address connected to given port.
1189 * And create corresponding Simple Next objective if it does not exist.
1190 * Applied only when populating Forwarding objective
1191 * @param deviceId switch ID to set the rule
1192 * @param portNumber port number
1193 * @param prefix IP prefix of the route
1194 * @param hostMac MAC address of the next hop
1195 * @param vlanId Vlan ID of the port
1196 * @param popVlan true to pop vlan tag in TrafficTreatment
1197 * @param install true to populate the forwarding objective, false to revoke
1198 */
1199 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1200 VlanId vlanId, boolean popVlan, boolean install) {
1201 ForwardingObjective.Builder fob;
1202 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1203 MacAddress deviceMac;
1204 try {
1205 deviceMac = config.getDeviceMac(deviceId);
1206 } catch (DeviceConfigNotFoundException e) {
1207 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1208 return;
1209 }
1210
1211 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1212 tbuilder.deferred()
1213 .setEthDst(hostMac)
1214 .setEthSrc(deviceMac)
1215 .setOutput(portNumber);
1216
1217 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1218
1219 if (!popVlan) {
1220 tbuilder.setVlanId(vlanId);
1221 } else {
1222 mbuilder.matchVlanId(vlanId);
1223 }
1224
1225 // if the objective is to revoke an existing rule, and for some reason
1226 // the next-objective does not exist, then a new one should not be created
1227 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1228 tbuilder.build(), mbuilder.build(), install);
1229 if (portNextObjId == -1) {
1230 // Warning log will come from getPortNextObjective method
1231 return;
1232 }
1233
1234 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1235 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1236 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1237
1238 ObjectiveContext context = new DefaultObjectiveContext(
1239 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1240 (objective, error) ->
1241 log.warn("Failed to {} IP rule for route {}: {}",
1242 install ? "install" : "revoke", prefix, error));
1243 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1244
1245 if (!install) {
1246 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1247 if (grpHandler == null) {
1248 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1249 } else {
1250 // Remove L3UG for the given port and host
1251 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1252 }
1253 }
1254 }
Charles Chandd33be52018-02-26 21:33:25 -08001255
1256 /**
1257 * Checks if there is other enabled port within the given VLAN on the given device.
1258 *
1259 * @param deviceId device ID
1260 * @param vlanId VLAN ID
1261 * @return true if there is no more port enabled within the given VLAN on the given device
1262 */
1263 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1264 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1265 .filter(Port::isEnabled)
1266 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1267 .collect(Collectors.toSet());
1268
1269 return enabledPorts.stream().noneMatch(cp ->
1270 // Given vlanId is included in the vlan-tagged configuration
1271 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1272 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1273 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1274 vlanId.equals(INTERNAL_VLAN)) ||
1275 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1276 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1277 );
1278 }
sanghob35a6192015-04-01 13:05:26 -07001279}