blob: 9509f82ebeeaf99b489d6017990fedc9457382d4 [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;
Saurav Das8a0732e2015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sanghob35a6192015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
Charles Chan1a13c172017-12-05 21:07:38 -080021import org.onlab.packet.IPv6;
sanghob35a6192015-04-01 13:05:26 -070022import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080023import org.onlab.packet.Ip6Address;
24import org.onlab.packet.IpAddress;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070027import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070028import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080029import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070030import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080031import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070032import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080033import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080034import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080035import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080036import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Dasc88d4662017-05-15 15:34:25 -070038import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070039import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sanghob35a6192015-04-01 13:05:26 -070040import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070041import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070042import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070045import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070047import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flowobjective.DefaultFilteringObjective;
49import org.onosproject.net.flowobjective.DefaultForwardingObjective;
50import org.onosproject.net.flowobjective.FilteringObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070053import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070058import java.util.Collection;
59import java.util.Collections;
Saurav Das7bcbe702017-06-13 15:35:54 -070060import java.util.HashMap;
Saurav Das837e0bb2015-10-30 17:45:38 -070061import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070062import java.util.List;
Saurav Das7bcbe702017-06-13 15:35:54 -070063import java.util.Map;
Charles Chan2df0e8a2017-01-09 11:45:08 -080064import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070065import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070066import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070067
68import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080069import static org.onlab.packet.Ethernet.TYPE_ARP;
70import static org.onlab.packet.Ethernet.TYPE_IPV6;
71import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
72import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan59cc16d2017-02-02 16:20:42 -080073import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070074
Charles Chane849c192016-01-11 18:28:54 -080075/**
76 * Populator of segment routing flow rules.
77 */
sanghob35a6192015-04-01 13:05:26 -070078public class RoutingRulePopulator {
Charles Chan1a13c172017-12-05 21:07:38 -080079 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
80
81 private static final int ARP_NDP_PRIORITY = 30000;
sanghob35a6192015-04-01 13:05:26 -070082
sangho20eff1d2015-04-13 15:15:58 -070083 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070084 private SegmentRoutingManager srManager;
85 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070086
sanghob35a6192015-04-01 13:05:26 -070087 /**
88 * Creates a RoutingRulePopulator object.
89 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070090 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070091 */
Charles Chan65238242017-06-22 18:03:14 -070092 RoutingRulePopulator(SegmentRoutingManager srManager) {
sanghob35a6192015-04-01 13:05:26 -070093 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070094 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070095 this.rulePopulationCounter = new AtomicLong(0);
96 }
97
98 /**
99 * Resets the population counter.
100 */
Charles Chan65238242017-06-22 18:03:14 -0700101 void resetCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700102 rulePopulationCounter.set(0);
103 }
104
105 /**
106 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -0700107 *
108 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700109 */
Charles Chan65238242017-06-22 18:03:14 -0700110 long getCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700111 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700112 }
113
114 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700115 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700116 * switch.
sanghob35a6192015-04-01 13:05:26 -0700117 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700118 * @param deviceId device ID of the device that next hop attaches to
119 * @param prefix IP prefix of the route
120 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800121 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700122 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700123 */
Charles Chan23686832017-08-23 14:46:43 -0700124 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800125 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700126 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chan1cdecff2016-10-27 14:19:48 -0700127 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800128 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800129 try {
Saurav Das961beb22017-03-29 19:09:17 -0700130 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
131 hostVlanId, outPort, false);
Charles Chan0b4e6182015-11-03 10:42:14 -0800132 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700133 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan0b4e6182015-11-03 10:42:14 -0800134 return;
135 }
Saurav Das59232cf2016-04-27 18:35:50 -0700136 if (fwdBuilder == null) {
Saurav Das76ae6812017-03-15 15:15:14 -0700137 log.warn("Aborting host routing table entry due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700138 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700139 return;
140 }
Charles Chan23686832017-08-23 14:46:43 -0700141
142 int nextId = fwdBuilder.add().nextId();
Charles Chand2990362016-04-18 13:44:03 -0700143 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan23686832017-08-23 14:46:43 -0700144 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
145 prefix, nextId),
Charles Chand2990362016-04-18 13:44:03 -0700146 (objective, error) ->
Saurav Das7bcbe702017-06-13 15:35:54 -0700147 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das76ae6812017-03-15 15:15:14 -0700148 prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700149 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800150 rulePopulationCounter.incrementAndGet();
151 }
152
Charles Chane849c192016-01-11 18:28:54 -0800153 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700154 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800155 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700156 * @param deviceId device ID of the device that next hop attaches to
157 * @param prefix IP prefix of the route
158 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800159 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700160 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800161 */
Charles Chan23686832017-08-23 14:46:43 -0700162 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800163 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700164 log.debug("Revoke IP table entry for route {} at {}:{}",
165 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800166 ForwardingObjective.Builder fwdBuilder;
167 try {
Saurav Das961beb22017-03-29 19:09:17 -0700168 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
169 hostVlanId, outPort, true);
Charles Chan68aa62d2015-11-09 16:37:23 -0800170 } catch (DeviceConfigNotFoundException e) {
171 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
172 return;
173 }
Charles Chan458b8262016-11-30 11:55:05 -0800174 if (fwdBuilder == null) {
175 log.warn("Aborting host routing table entries due "
176 + "to error for dev:{} route:{}", deviceId, prefix);
177 return;
178 }
Charles Chand2990362016-04-18 13:44:03 -0700179 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700180 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700181 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700182 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700183 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800184 }
185
Charles Chan1cdecff2016-10-27 14:19:48 -0700186 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800187 * Returns a forwarding objective builder for routing rules.
188 * <p>
189 * The forwarding objective routes packets destined to a given prefix to
190 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700191 *
192 * @param deviceId device ID
193 * @param prefix prefix that need to be routed
194 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800195 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700196 * @param outPort port where the nexthop attaches to
Saurav Das961beb22017-03-29 19:09:17 -0700197 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chan1cdecff2016-10-27 14:19:48 -0700198 * @return forwarding objective builder
199 * @throws DeviceConfigNotFoundException if given device is not configured
200 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800201 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700202 DeviceId deviceId, IpPrefix prefix,
Saurav Das961beb22017-03-29 19:09:17 -0700203 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
204 boolean revoke)
Charles Chan68aa62d2015-11-09 16:37:23 -0800205 throws DeviceConfigNotFoundException {
206 MacAddress deviceMac;
207 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800208
Charles Chan7ffd81f2017-02-08 15:52:08 -0800209 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
210 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
211 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
212 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700213
Charles Chan7ffd81f2017-02-08 15:52:08 -0800214 // Create route selector
215 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
216
217 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700218 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700219 tbuilder.deferred()
220 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800221 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700222 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800223
Charles Chan7ffd81f2017-02-08 15:52:08 -0800224 // Create route meta
225 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800226
Charles Chan7ffd81f2017-02-08 15:52:08 -0800227 // Adjust the meta according to VLAN configuration
228 if (taggedVlans.contains(hostVlanId)) {
229 tbuilder.setVlanId(hostVlanId);
230 } else if (hostVlanId.equals(VlanId.NONE)) {
231 if (untaggedVlan != null) {
232 mbuilder.matchVlanId(untaggedVlan);
233 } else if (nativeVlan != null) {
234 mbuilder.matchVlanId(nativeVlan);
235 } else {
Charles Chan65238242017-06-22 18:03:14 -0700236 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
237 hostMac, hostVlanId, connectPoint);
238 return null;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800239 }
240 } else {
Saurav Das961beb22017-03-29 19:09:17 -0700241 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
242 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das59232cf2016-04-27 18:35:50 -0700243 return null;
244 }
Saurav Das961beb22017-03-29 19:09:17 -0700245 // if the objective is to revoke an existing rule, and for some reason
246 // the next-objective does not exist, then a new one should not be created
Charles Chan7ffd81f2017-02-08 15:52:08 -0800247 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das961beb22017-03-29 19:09:17 -0700248 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800249 if (portNextObjId == -1) {
250 // Warning log will come from getPortNextObjective method
251 return null;
252 }
253
Charles Chan68aa62d2015-11-09 16:37:23 -0800254 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800255 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800256 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800257 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700258 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800259 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700260 }
261
262 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700263 * Populates IP flow rules for all the given prefixes reachable from the
264 * destination switch(es).
sanghob35a6192015-04-01 13:05:26 -0700265 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700266 * @param targetSw switch where rules are to be programmed
267 * @param subnets subnets/prefixes being added
268 * @param destSw1 destination switch where the prefixes are reachable
269 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
270 * Should be null if there is no paired destination switch (by config)
271 * or if the given prefixes are reachable only via destSw1
272 * @param nextHops a map containing a set of next-hops for each destination switch.
273 * If destSw2 is not null, then this map must contain an
274 * entry for destSw2 with its next-hops from the targetSw
275 * (although the next-hop set may be empty in certain scenarios).
276 * If destSw2 is null, there should not be an entry in this
277 * map for destSw2.
sanghob35a6192015-04-01 13:05:26 -0700278 * @return true if all rules are set successfully, false otherwise
279 */
Charles Chan65238242017-06-22 18:03:14 -0700280 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das7bcbe702017-06-13 15:35:54 -0700281 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700282 for (IpPrefix subnet : subnets) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700283 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sanghob35a6192015-04-01 13:05:26 -0700284 return false;
285 }
286 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700287 return true;
288 }
sanghob35a6192015-04-01 13:05:26 -0700289
Charles Chan93e71ba2016-04-29 14:38:22 -0700290 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700291 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chan93e71ba2016-04-29 14:38:22 -0700292 *
293 * @param subnets subnet being removed
294 * @return true if all rules are removed successfully, false otherwise
295 */
Charles Chan65238242017-06-22 18:03:14 -0700296 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700297 for (IpPrefix subnet : subnets) {
298 if (!revokeIpRuleForRouter(subnet)) {
299 return false;
300 }
301 }
sanghob35a6192015-04-01 13:05:26 -0700302 return true;
303 }
304
305 /**
Saurav Das25190812016-05-27 13:54:07 -0700306 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das7bcbe702017-06-13 15:35:54 -0700307 * is reachable via destination device(s).
sanghob35a6192015-04-01 13:05:26 -0700308 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700309 * @param targetSw target device ID to set the rules
310 * @param ipPrefix the IP prefix
311 * @param destSw1 destination switch where the prefixes are reachable
312 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
313 * Should be null if there is no paired destination switch (by config)
314 * or if the given prefixes are reachable only via destSw1
315 * @param nextHops map of destination switches and their next-hops.
316 * Should only contain destination switches that are
317 * actually meant to be routed to. If destSw2 is null, there
318 * should not be an entry for destSw2 in this map.
sanghob35a6192015-04-01 13:05:26 -0700319 * @return true if all rules are set successfully, false otherwise
320 */
Charles Chan65238242017-06-22 18:03:14 -0700321 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das7bcbe702017-06-13 15:35:54 -0700322 IpPrefix ipPrefix, DeviceId destSw1,
323 DeviceId destSw2,
324 Map<DeviceId, Set<DeviceId>> nextHops) {
325 int segmentId1, segmentId2 = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -0800326 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800327 if (ipPrefix.isIp4()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700328 segmentId1 = config.getIPv4SegmentId(destSw1);
329 if (destSw2 != null) {
330 segmentId2 = config.getIPv4SegmentId(destSw2);
331 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800332 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700333 segmentId1 = config.getIPv6SegmentId(destSw1);
334 if (destSw2 != null) {
335 segmentId2 = config.getIPv6SegmentId(destSw2);
336 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800337 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800338 } catch (DeviceConfigNotFoundException e) {
339 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
340 return false;
341 }
sanghob35a6192015-04-01 13:05:26 -0700342
Pier Ventree0ae7a32016-11-23 09:57:42 -0800343 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800344 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700345
Charles Chan68aa62d2015-11-09 16:37:23 -0800346 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700347 DestinationSet ds;
Charles Chan68aa62d2015-11-09 16:37:23 -0800348 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700349
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700350 // If the next hop is the same as the final destination, then MPLS label
351 // is not set.
Saurav Das7bcbe702017-06-13 15:35:54 -0700352 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800353 tbuilder.immediate().decNwTtl();
Saurav Das7bcbe702017-06-13 15:35:54 -0700354 ds = new DestinationSet(false, destSw);
Charles Chan68aa62d2015-11-09 16:37:23 -0800355 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700356 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700357 ds = new DestinationSet(false, segmentId, destSw);
358 treatment = null;
359 }*/
360 if (destSw2 == null) {
361 // single dst - create destination set based on next-hop
362 Set<DeviceId> nhd1 = nextHops.get(destSw1);
363 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
364 tbuilder.immediate().decNwTtl();
365 ds = new DestinationSet(false, destSw1);
366 treatment = tbuilder.build();
367 } else {
368 ds = new DestinationSet(false, segmentId1, destSw1);
369 treatment = null;
370 }
371 } else {
372 // dst pair - IP rules for dst-pairs are always from other edge nodes
373 // the destination set needs to have both destinations, even if there
374 // are no next hops to one of them
375 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chan68aa62d2015-11-09 16:37:23 -0800376 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700377 }
378
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 // setup metadata to pass to nextObjective - indicate the vlan on egress
380 // if needed by the switch pipeline. Since neighbor sets are always to
381 // other neighboring routers, there is no subnet assigned on those ports.
382 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800383 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das7bcbe702017-06-13 15:35:54 -0700384 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700385 if (grpHandler == null) {
386 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700387 + "not found", targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700388 return false;
389 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800390
Saurav Das7bcbe702017-06-13 15:35:54 -0700391 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
392 metabuilder.build(), true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700394 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho834e4b02015-05-01 09:38:25 -0700395 return false;
396 }
397
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700398 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
399 .builder()
400 .fromApp(srManager.appId)
401 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700403 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800404 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700405 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800406 if (treatment != null) {
407 fwdBuilder.withTreatment(treatment);
408 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700409 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700410 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chand2990362016-04-18 13:44:03 -0700411 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700412 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700413 ipPrefix, targetSw),
Charles Chand2990362016-04-18 13:44:03 -0700414 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700415 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700416 ipPrefix, error, targetSw));
417 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700418 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700419
420 return true;
421 }
422
sanghob35a6192015-04-01 13:05:26 -0700423 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700424 * Revokes IP flow rules for the router IP address.
425 *
426 * @param ipPrefix the IP address of the destination router
427 * @return true if all rules are removed successfully, false otherwise
428 */
Charles Chan65238242017-06-22 18:03:14 -0700429 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800430 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700431 TrafficSelector selector = sbuilder.build();
432 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
433
434 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
435 .builder()
436 .fromApp(srManager.appId)
437 .makePermanent()
438 .withSelector(selector)
439 .withTreatment(dummyTreatment)
440 .withPriority(getPriorityFromPrefix(ipPrefix))
441 .withFlag(ForwardingObjective.Flag.SPECIFIC);
442
443 ObjectiveContext context = new DefaultObjectiveContext(
444 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
445 (objective, error) ->
446 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
447
Charles Chan65238242017-06-22 18:03:14 -0700448 srManager.deviceService.getAvailableDevices().forEach(device ->
449 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context))
450 );
Charles Chan93e71ba2016-04-29 14:38:22 -0700451
452 return true;
453 }
454
455 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700456 * Deals with !MPLS Bos use case.
457 *
458 * @param targetSwId the target sw
459 * @param destSwId the destination sw
460 * @param nextHops the set of next hops
461 * @param segmentId the segmentId to match
462 * @param routerIp the router ip
463 * @return a collection of fwdobjective
464 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700465 private Collection<ForwardingObjective> handleMpls(
466 DeviceId targetSwId,
467 DeviceId destSwId,
468 Set<DeviceId> nextHops,
469 int segmentId,
470 IpAddress routerIp,
471 boolean isMplsBos) {
Pier Ventre917127a2016-10-31 16:49:19 -0700472
473 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
474 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800475 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700476 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
477 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
478 sbuilder.matchMplsBos(isMplsBos);
479 TrafficSelector selector = sbuilder.build();
480
481 // setup metadata to pass to nextObjective - indicate the vlan on egress
482 // if needed by the switch pipeline. Since mpls next-hops are always to
483 // other neighboring routers, there is no subnet assigned on those ports.
484 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800485 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700486
487 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
488 // If the next hop is the destination router for the segment, do pop
489 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das7bcbe702017-06-13 15:35:54 -0700490 + "label {} in switch {} with pop to next-hops {}",
491 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700492 // Not-bos pop case (php for the current label). If MPLS-ECMP
493 // has been configured, the application we will request the
494 // installation for an MPLS-ECMP group.
Saurav Das961beb22017-03-29 19:09:17 -0700495 ForwardingObjective.Builder fwdObjNoBosBuilder =
496 getMplsForwardingObjective(targetSwId,
497 nextHops,
498 true,
499 isMplsBos,
500 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700501 routerIp,
502 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700503 // Error case, we cannot handle, exit.
504 if (fwdObjNoBosBuilder == null) {
505 return Collections.emptyList();
506 }
507 fwdObjBuilders.add(fwdObjNoBosBuilder);
508
509 } else {
510 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das7bcbe702017-06-13 15:35:54 -0700511 log.debug("Installing MPLS forwarding objective for "
512 + "label {} in switch {} without pop to next-hops {}",
513 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700514 // Not-bos pop case. If MPLS-ECMP has been configured, the
515 // application we will request the installation for an MPLS-ECMP
516 // group.
Saurav Das961beb22017-03-29 19:09:17 -0700517 ForwardingObjective.Builder fwdObjNoBosBuilder =
518 getMplsForwardingObjective(targetSwId,
519 nextHops,
520 false,
521 isMplsBos,
522 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700523 routerIp,
524 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700525 // Error case, we cannot handle, exit.
526 if (fwdObjNoBosBuilder == null) {
527 return Collections.emptyList();
528 }
529 fwdObjBuilders.add(fwdObjNoBosBuilder);
530
531 }
532
533 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
534 // We add the final property to the fwdObjs.
535 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
536
537 ((Builder) ((Builder) fwdObjBuilder
538 .fromApp(srManager.appId)
539 .makePermanent())
540 .withSelector(selector)
541 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
542 .withFlag(ForwardingObjective.Flag.SPECIFIC);
543
544 ObjectiveContext context = new DefaultObjectiveContext(
545 (objective) ->
546 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
547 objective.id(), segmentId, targetSwId),
548 (objective, error) ->
549 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
550 objective.id(), segmentId, error, targetSwId));
551
552 ForwardingObjective fob = fwdObjBuilder.add(context);
553 fwdObjs.add(fob);
554
555 }
556
557 return fwdObjs;
558 }
559
560 /**
Saurav Das25190812016-05-27 13:54:07 -0700561 * Populates MPLS flow rules in the target device to point towards the
562 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700563 *
Saurav Das25190812016-05-27 13:54:07 -0700564 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700565 * @param destSwId destination switch device ID
566 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700567 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700568 * @return true if all rules are set successfully, false otherwise
569 */
Charles Chan65238242017-06-22 18:03:14 -0700570 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700571 Set<DeviceId> nextHops,
572 IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700573
Charles Chan0b4e6182015-11-03 10:42:14 -0800574 int segmentId;
575 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800576 if (routerIp.isIp4()) {
577 segmentId = config.getIPv4SegmentId(destSwId);
578 } else {
579 segmentId = config.getIPv6SegmentId(destSwId);
580 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800581 } catch (DeviceConfigNotFoundException e) {
582 log.warn(e.getMessage() + " Aborting populateMplsRule.");
583 return false;
584 }
sanghob35a6192015-04-01 13:05:26 -0700585
Pier Ventre917127a2016-10-31 16:49:19 -0700586 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan65238242017-06-22 18:03:14 -0700587 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre917127a2016-10-31 16:49:19 -0700588 // Generates the transit rules used by the standard "routing".
589 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
590 if (fwdObjsMpls.isEmpty()) {
591 return false;
sanghob35a6192015-04-01 13:05:26 -0700592 }
Pier Ventre917127a2016-10-31 16:49:19 -0700593 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800594 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700595 // the only case !BoS supported.
Charles Chan65238242017-06-22 18:03:14 -0700596 /*
597 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700598 if (fwdObjsMpls.isEmpty()) {
599 return false;
600 }
Charles Chan65238242017-06-22 18:03:14 -0700601 fwdObjs.addAll(fwdObjsMpls);
602 */
Pier Ventre917127a2016-10-31 16:49:19 -0700603
604 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700605 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700606 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
607 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700608 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700609 }
610
611 return true;
612 }
613
Saurav Das8a0732e2015-11-20 15:27:53 -0800614 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das7bcbe702017-06-13 15:35:54 -0700615 DeviceId targetSw,
Saurav Das8a0732e2015-11-20 15:27:53 -0800616 Set<DeviceId> nextHops,
617 boolean phpRequired,
618 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800619 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700620 IpAddress routerIp,
621 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800622
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700623 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
624 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700625
626 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
627
628 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800629 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700630 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700631 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700632 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800633 if (routerIp.isIp4()) {
634 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
635 } else {
636 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
637 }
638 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700639 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800640 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
641 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700642 }
643 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800644 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700645 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700646 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700647 }
648
Saurav Das8a0732e2015-11-20 15:27:53 -0800649 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700650 // if MPLS-ECMP == True we will build a standard NeighborSet.
651 // Otherwise a RandomNeighborSet.
Saurav Das7bcbe702017-06-13 15:35:54 -0700652 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700653 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700654 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700655 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700656 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700657 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700658
Saurav Dasb28d5dd2017-03-24 19:03:58 -0700659 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700660 targetSw, ns);
661 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
662 if (gh == null) {
663 log.warn("getNextObjectiveId query - groupHandler for device {} "
664 + "not found", targetSw);
665 return null;
666 }
Pier Ventre917127a2016-10-31 16:49:19 -0700667 // If BoS == True, all forwarding is via L3 ECMP group.
668 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
669 // MPLS-Interface group. This depends on the configuration of the option
670 // MPLS-ECMP.
671 // The metadata informs the driver that the next-Objective will be used
672 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das7bcbe702017-06-13 15:35:54 -0700673 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
674 dstNextHops.put(destSw, nextHops);
675 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800676 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700677 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das8a0732e2015-11-20 15:27:53 -0800678 return null;
Saurav Das25190812016-05-27 13:54:07 -0700679 } else {
680 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700681 nextId, targetSw, ns);
sanghob35a6192015-04-01 13:05:26 -0700682 }
683
Saurav Das8a0732e2015-11-20 15:27:53 -0800684 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700685 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700686 }
687
688 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700689 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700690 * dstMac corresponding to the router's MAC address. For those pipelines
691 * that need to internally assign vlans to untagged packets, this method
692 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700693 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800694 * Note that the vlan assignment and filter programming should only be done by
695 * the master for a switch. This method is typically called at deviceAdd and
696 * programs filters only for the enabled ports of the device. For port-updates,
697 * that enable/disable ports after device add, singlePortFilter methods should
698 * be called.
sanghob35a6192015-04-01 13:05:26 -0700699 *
Saurav Das822c4e22015-10-23 10:51:11 -0700700 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800701 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700702 */
Charles Chan65238242017-06-22 18:03:14 -0700703 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700704 log.debug("Installing per-port filtering objective for untagged "
705 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800706
Saurav Das59232cf2016-04-27 18:35:50 -0700707 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800708 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700709 log.warn("Device {} ports not available. Unable to add MacVlan filters",
710 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800711 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700712 }
Saurav Das018605f2017-02-18 14:05:44 -0800713 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700714 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700715 if (!port.isEnabled()) {
716 disabledPorts++;
717 continue;
718 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800719 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800720 filteredPorts++;
721 } else {
722 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700723 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700724 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700725 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800726 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800727 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800728 errorPorts, filteredPorts);
729 }
730
731 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800732 * Creates or removes filtering objectives for a single port. Should only be
733 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800734 *
735 * @param deviceId device identifier
736 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800737 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800738 * @return true if no errors occurred during the build of the filtering objective
739 */
Charles Chan65238242017-06-22 18:03:14 -0700740 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800741 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
742 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
743 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
744 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
745
746 if (taggedVlans.size() != 0) {
747 // Filter for tagged vlans
748 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800749 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800750 return false;
751 }
752 if (nativeVlan != null) {
753 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800754 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800755 return false;
756 }
757 }
758 } else if (untaggedVlan != null) {
759 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800760 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800761 return false;
762 }
763 } else {
Saurav Dasceccf242017-08-03 18:30:35 -0700764 // Unconfigured port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800765 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800766 return false;
767 }
768 }
769 return true;
770 }
771
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700772 /**
773 * Updates filtering objectives for a single port. Should only be called by
774 * the master for a switch
775 * @param deviceId device identifier
776 * @param portNum port identifier for port to be filtered
777 * @param pushVlan true to push vlan, false otherwise
778 * @param vlanId vlan identifier
779 * @param install true to install the filtering objective, false to remove
780 */
781 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
782 boolean pushVlan, VlanId vlanId, boolean install) {
783 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
784 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
785 deviceId, portNum, vlanId);
786 }
787 }
788
Charles Chan7e4f8192017-02-26 22:59:35 -0800789 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
790 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800791 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800792 if (fob == null) {
793 // error encountered during build
794 return false;
795 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700796 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700797 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800798 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800799 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700800 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800801 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700802 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800803 if (install) {
804 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
805 } else {
806 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800807 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800808 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800809 }
810
Charles Chan7ffd81f2017-02-08 15:52:08 -0800811 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
812 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800813 MacAddress deviceMac;
814 try {
815 deviceMac = config.getDeviceMac(deviceId);
816 } catch (DeviceConfigNotFoundException e) {
817 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
818 return null;
819 }
Saurav Das018605f2017-02-18 14:05:44 -0800820 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
821 fob.withKey(Criteria.matchInPort(portnum))
822 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800823 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800824
Charles Chan0d6db512017-12-19 19:55:57 -0800825 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
826
Charles Chan7ffd81f2017-02-08 15:52:08 -0800827 if (pushVlan) {
828 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan0d6db512017-12-19 19:55:57 -0800829 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800830 } else {
831 fob.addCondition(Criteria.matchVlanId(vlanId));
832 }
833
Charles Chan0d6db512017-12-19 19:55:57 -0800834 // NOTE: Some switch hardware share the same filtering flow among different ports.
835 // We use this metadata to let the driver know that there is no more enabled port
836 // within the same VLAN on this device.
837 boolean noMoreEnabledPort = srManager.interfaceService.getInterfaces().stream()
838 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
839 .filter(intf -> intf.vlanTagged().contains(vlanId) ||
840 intf.vlanUntagged().equals(vlanId) ||
841 intf.vlanNative().equals(vlanId))
842 .noneMatch(intf -> {
843 Port port = srManager.deviceService.getPort(intf.connectPoint());
844 return port != null && port.isEnabled();
845 });
846 if (noMoreEnabledPort) {
847 tBuilder.wipeDeferred();
848 }
849
850 fob.withMeta(tBuilder.build());
851
Saurav Das018605f2017-02-18 14:05:44 -0800852 fob.permit().fromApp(srManager.appId);
853 return fob;
sanghob35a6192015-04-01 13:05:26 -0700854 }
855
856 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700857 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700858 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700859 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700860 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700861 *
Saurav Das822c4e22015-10-23 10:51:11 -0700862 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700863 */
Charles Chan65238242017-06-22 18:03:14 -0700864 void populateIpPunts(DeviceId deviceId) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700865 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chan1a13c172017-12-05 21:07:38 -0800866 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800867 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800868 routerIpv4 = config.getRouterIpv4(deviceId);
869 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan1a13c172017-12-05 21:07:38 -0800870 routerLinkLocalIpv6 = Ip6Address.valueOf(
871 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
872
Saurav Das7bcbe702017-06-13 15:35:54 -0700873 if (config.isPairedEdge(deviceId)) {
874 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
875 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
876 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800877 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800878 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800879 return;
880 }
881
Saurav Das837e0bb2015-10-30 17:45:38 -0700882 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
883 log.debug("Not installing port-IP punts - not the master for dev:{} ",
884 deviceId);
885 return;
886 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800887 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
888 allIps.add(routerIpv4);
Charles Chan1a13c172017-12-05 21:07:38 -0800889 allIps.add(routerLinkLocalIpv6);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800890 if (routerIpv6 != null) {
891 allIps.add(routerIpv6);
892 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700893 if (pairRouterIpv4 != null) {
894 allIps.add(pairRouterIpv4);
895 }
896 if (pairRouterIpv6 != null) {
897 allIps.add(pairRouterIpv6);
898 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800899 for (IpAddress ipaddr : allIps) {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700900 populateSingleIpPunts(deviceId, ipaddr);
901 }
902 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800903
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700904 /**
905 * Creates a forwarding objective to punt all IP packets, destined to the
906 * specified IP address, which should be router's port IP address.
907 *
908 * @param deviceId the switch dpid for the router
909 * @param ipAddress the IP address of the router's port
910 */
911 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
912 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
913 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
914
915 srManager.packetService.requestPackets(sbuilder.build(),
916 PacketPriority.CONTROL, srManager.appId, optDeviceId);
917 }
918
919 /**
920 * Removes a forwarding objective to punt all IP packets, destined to the
921 * specified IP address, which should be router's port IP address.
922 *
923 * @param deviceId the switch dpid for the router
924 * @param ipAddress the IP address of the router's port
925 */
926 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
927 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
928 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
929
930 try {
931 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
932 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
933 srManager.packetService.cancelPackets(sbuilder.build(),
934 PacketPriority.CONTROL, srManager.appId, optDeviceId);
935 }
936 } catch (DeviceConfigNotFoundException e) {
937 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das822c4e22015-10-23 10:51:11 -0700938 }
sanghob35a6192015-04-01 13:05:26 -0700939 }
940
Charles Chan68aa62d2015-11-09 16:37:23 -0800941 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800942 * Method to build IPv4 or IPv6 selector.
943 *
944 * @param addressToMatch the address to match
945 */
946 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
947 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
948 }
949
950 /**
951 * Method to build IPv4 or IPv6 selector.
952 *
953 * @param prefixToMatch the prefix to match
954 */
955 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
956 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700957 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800958 if (prefixToMatch.isIp4()) {
959 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
960 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
961 return selectorBuilder;
962 }
Pier Ventre917127a2016-10-31 16:49:19 -0700963 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800964 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
965 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
966 return selectorBuilder;
967 }
968
969 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800970 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
971 * Furthermore, these are applied only by the master instance. Deferred actions
972 * are not cleared such that packets can be flooded in the cross connect use case
973 *
974 * @param deviceId the switch dpid for the router
975 */
Charles Chan65238242017-06-22 18:03:14 -0700976 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700977 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800978 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
979 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
980 deviceId);
981 return;
982 }
983
Charles Chan65238242017-06-22 18:03:14 -0700984 ForwardingObjective fwdObj;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800985 // We punt all ARP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -0800986 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800987 .add(new ObjectiveContext() {
988 @Override
989 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700990 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800991 deviceId, error);
992 }
993 });
Charles Chan65238242017-06-22 18:03:14 -0700994 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800995
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800996 // We punt all NDP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -0800997 fwdObj = ndpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800998 .add(new ObjectiveContext() {
999 @Override
1000 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -07001001 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001002 deviceId, error);
1003 }
1004 });
Charles Chan65238242017-06-22 18:03:14 -07001005 srManager.flowObjectiveService.forward(deviceId, fwdObj);
1006
1007 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1008 ForwardingObjective pairFwdObj;
1009 // Do not punt ARP packets from pair port
1010 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1011 .add(new ObjectiveContext() {
1012 @Override
1013 public void onError(Objective objective, ObjectiveError error) {
1014 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1015 deviceId, error);
1016 }
1017 });
1018 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1019
1020 // Do not punt NDP packets from pair port
1021 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1022 .add(new ObjectiveContext() {
1023 @Override
1024 public void onError(Objective objective, ObjectiveError error) {
1025 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1026 deviceId, error);
1027 }
1028 });
1029 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1030
1031 // Do not forward DAD packets from pair port
1032 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1033 .add(new ObjectiveContext() {
1034 @Override
1035 public void onError(Objective objective, ObjectiveError error) {
1036 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1037 deviceId, error);
1038 }
1039 });
1040 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1041 });
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001042 }
1043
Charles Chan65238242017-06-22 18:03:14 -07001044 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1045 TrafficTreatment treatment, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001046 return DefaultForwardingObjective.builder()
Charles Chan65238242017-06-22 18:03:14 -07001047 .withPriority(priority)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001048 .withSelector(selector)
1049 .fromApp(srManager.appId)
1050 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan65238242017-06-22 18:03:14 -07001051 .withTreatment(treatment)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001052 .makePermanent();
1053 }
1054
Charles Chan65238242017-06-22 18:03:14 -07001055 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001056 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1057 sBuilder.matchEthType(TYPE_ARP);
Charles Chan65238242017-06-22 18:03:14 -07001058 if (port != null) {
1059 sBuilder.matchInPort(port);
1060 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001061
Charles Chan65238242017-06-22 18:03:14 -07001062 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1063 if (punt) {
1064 tBuilder.punt();
1065 }
1066 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001067 }
1068
Charles Chan65238242017-06-22 18:03:14 -07001069 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001070 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1071 sBuilder.matchEthType(TYPE_IPV6)
1072 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan65238242017-06-22 18:03:14 -07001073 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1074 if (port != null) {
1075 sBuilder.matchInPort(port);
1076 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001077
Charles Chan65238242017-06-22 18:03:14 -07001078 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1079 if (punt) {
1080 tBuilder.punt();
1081 }
1082 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1083 }
1084
1085 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1086 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1087 sBuilder.matchEthType(TYPE_IPV6)
Charles Chanc7e36aa2017-08-07 12:39:03 -07001088 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1089 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1090 // .matchIPProtocol(PROTOCOL_ICMP6)
1091 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan65238242017-06-22 18:03:14 -07001092 if (port != null) {
1093 sBuilder.matchInPort(port);
1094 }
1095
1096 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1097 tBuilder.wipeDeferred();
1098 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001099 }
1100
1101 /**
Charles Chan68aa62d2015-11-09 16:37:23 -08001102 * Populates a forwarding objective to send packets that miss other high
1103 * priority Bridging Table entries to a group that contains all ports of
1104 * its subnet.
1105 *
Charles Chan68aa62d2015-11-09 16:37:23 -08001106 * @param deviceId switch ID to set the rules
1107 */
Charles Chan65238242017-06-22 18:03:14 -07001108 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001109 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001110 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chan68aa62d2015-11-09 16:37:23 -08001111 });
1112 }
1113
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001114 /**
1115 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1116 * @param deviceId switch ID to set the rule
1117 * @param vlanId vlan ID to specify the subnet
1118 * @param install true to install the rule, false to revoke the rule
1119 */
1120 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1121 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1122
1123 if (nextId < 0) {
1124 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1125 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1126 return;
1127 }
1128
1129 // Driver should treat objective with MacAddress.NONE as the
1130 // subnet broadcast rule
1131 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1132 sbuilder.matchVlanId(vlanId);
1133 sbuilder.matchEthDst(MacAddress.NONE);
1134
1135 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1136 fob.withFlag(Flag.SPECIFIC)
1137 .withSelector(sbuilder.build())
1138 .nextStep(nextId)
1139 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1140 .fromApp(srManager.appId)
1141 .makePermanent();
1142 ObjectiveContext context = new DefaultObjectiveContext(
1143 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1144 (objective, error) ->
1145 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1146
1147 if (install) {
1148 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1149 } else {
1150 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1151 }
1152 }
1153
Charles Chan5270ed02016-01-30 23:22:37 -08001154 private int getPriorityFromPrefix(IpPrefix prefix) {
1155 return (prefix.isIp4()) ?
1156 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1157 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001158 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001159
1160 /**
1161 * Update Forwarding objective for each host and IP address connected to given port.
1162 * And create corresponding Simple Next objective if it does not exist.
1163 * Applied only when populating Forwarding objective
1164 * @param deviceId switch ID to set the rule
1165 * @param portNumber port number
1166 * @param prefix IP prefix of the route
1167 * @param hostMac MAC address of the next hop
1168 * @param vlanId Vlan ID of the port
1169 * @param popVlan true to pop vlan tag in TrafficTreatment
1170 * @param install true to populate the forwarding objective, false to revoke
1171 */
1172 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1173 VlanId vlanId, boolean popVlan, boolean install) {
1174 ForwardingObjective.Builder fob;
1175 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1176 MacAddress deviceMac;
1177 try {
1178 deviceMac = config.getDeviceMac(deviceId);
1179 } catch (DeviceConfigNotFoundException e) {
1180 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1181 return;
1182 }
1183
1184 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1185 tbuilder.deferred()
1186 .setEthDst(hostMac)
1187 .setEthSrc(deviceMac)
1188 .setOutput(portNumber);
1189
1190 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1191
1192 if (!popVlan) {
1193 tbuilder.setVlanId(vlanId);
1194 } else {
1195 mbuilder.matchVlanId(vlanId);
1196 }
1197
1198 // if the objective is to revoke an existing rule, and for some reason
1199 // the next-objective does not exist, then a new one should not be created
1200 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1201 tbuilder.build(), mbuilder.build(), install);
1202 if (portNextObjId == -1) {
1203 // Warning log will come from getPortNextObjective method
1204 return;
1205 }
1206
1207 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1208 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1209 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1210
1211 ObjectiveContext context = new DefaultObjectiveContext(
1212 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1213 (objective, error) ->
1214 log.warn("Failed to {} IP rule for route {}: {}",
1215 install ? "install" : "revoke", prefix, error));
1216 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1217
1218 if (!install) {
1219 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1220 if (grpHandler == null) {
1221 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1222 } else {
1223 // Remove L3UG for the given port and host
1224 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1225 }
1226 }
1227 }
sanghob35a6192015-04-01 13:05:26 -07001228}