blob: 94aed906dfe5c1fcb8d6075549ea93c23dc0cbb0 [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
Charles Chan1e848472018-01-30 13:31:30 -0800443 srManager.deviceService.getAvailableDevices().forEach(device -> {
444 if (srManager.mastershipService.isLocalMaster(device.id())) {
445 ObjectiveContext context = new DefaultObjectiveContext(
446 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
447 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
448 ipPrefix, device.id(), error));
449 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
450 } else {
451 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
452 }
453 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700454
455 return true;
456 }
457
458 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700459 * Deals with !MPLS Bos use case.
460 *
461 * @param targetSwId the target sw
462 * @param destSwId the destination sw
463 * @param nextHops the set of next hops
464 * @param segmentId the segmentId to match
465 * @param routerIp the router ip
466 * @return a collection of fwdobjective
467 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700468 private Collection<ForwardingObjective> handleMpls(
469 DeviceId targetSwId,
470 DeviceId destSwId,
471 Set<DeviceId> nextHops,
472 int segmentId,
473 IpAddress routerIp,
474 boolean isMplsBos) {
Pier Ventre917127a2016-10-31 16:49:19 -0700475
476 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
477 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800478 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700479 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
480 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
481 sbuilder.matchMplsBos(isMplsBos);
482 TrafficSelector selector = sbuilder.build();
483
484 // setup metadata to pass to nextObjective - indicate the vlan on egress
485 // if needed by the switch pipeline. Since mpls next-hops are always to
486 // other neighboring routers, there is no subnet assigned on those ports.
487 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800488 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700489
490 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
491 // If the next hop is the destination router for the segment, do pop
492 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das7bcbe702017-06-13 15:35:54 -0700493 + "label {} in switch {} with pop to next-hops {}",
494 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700495 // Not-bos pop case (php for the current label). If MPLS-ECMP
496 // has been configured, the application we will request the
497 // installation for an MPLS-ECMP group.
Saurav Das961beb22017-03-29 19:09:17 -0700498 ForwardingObjective.Builder fwdObjNoBosBuilder =
499 getMplsForwardingObjective(targetSwId,
500 nextHops,
501 true,
502 isMplsBos,
503 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700504 routerIp,
505 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700506 // Error case, we cannot handle, exit.
507 if (fwdObjNoBosBuilder == null) {
508 return Collections.emptyList();
509 }
510 fwdObjBuilders.add(fwdObjNoBosBuilder);
511
512 } else {
513 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das7bcbe702017-06-13 15:35:54 -0700514 log.debug("Installing MPLS forwarding objective for "
515 + "label {} in switch {} without pop to next-hops {}",
516 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700517 // Not-bos pop case. If MPLS-ECMP has been configured, the
518 // application we will request the installation for an MPLS-ECMP
519 // group.
Saurav Das961beb22017-03-29 19:09:17 -0700520 ForwardingObjective.Builder fwdObjNoBosBuilder =
521 getMplsForwardingObjective(targetSwId,
522 nextHops,
523 false,
524 isMplsBos,
525 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700526 routerIp,
527 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700528 // Error case, we cannot handle, exit.
529 if (fwdObjNoBosBuilder == null) {
530 return Collections.emptyList();
531 }
532 fwdObjBuilders.add(fwdObjNoBosBuilder);
533
534 }
535
536 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
537 // We add the final property to the fwdObjs.
538 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
539
540 ((Builder) ((Builder) fwdObjBuilder
541 .fromApp(srManager.appId)
542 .makePermanent())
543 .withSelector(selector)
544 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
545 .withFlag(ForwardingObjective.Flag.SPECIFIC);
546
547 ObjectiveContext context = new DefaultObjectiveContext(
548 (objective) ->
549 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
550 objective.id(), segmentId, targetSwId),
551 (objective, error) ->
552 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
553 objective.id(), segmentId, error, targetSwId));
554
555 ForwardingObjective fob = fwdObjBuilder.add(context);
556 fwdObjs.add(fob);
557
558 }
559
560 return fwdObjs;
561 }
562
563 /**
Saurav Das25190812016-05-27 13:54:07 -0700564 * Populates MPLS flow rules in the target device to point towards the
565 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700566 *
Saurav Das25190812016-05-27 13:54:07 -0700567 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700568 * @param destSwId destination switch device ID
569 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700570 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700571 * @return true if all rules are set successfully, false otherwise
572 */
Charles Chan65238242017-06-22 18:03:14 -0700573 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700574 Set<DeviceId> nextHops,
575 IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700576
Charles Chan0b4e6182015-11-03 10:42:14 -0800577 int segmentId;
578 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800579 if (routerIp.isIp4()) {
580 segmentId = config.getIPv4SegmentId(destSwId);
581 } else {
582 segmentId = config.getIPv6SegmentId(destSwId);
583 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800584 } catch (DeviceConfigNotFoundException e) {
585 log.warn(e.getMessage() + " Aborting populateMplsRule.");
586 return false;
587 }
sanghob35a6192015-04-01 13:05:26 -0700588
Pier Ventre917127a2016-10-31 16:49:19 -0700589 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan65238242017-06-22 18:03:14 -0700590 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre917127a2016-10-31 16:49:19 -0700591 // Generates the transit rules used by the standard "routing".
592 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
593 if (fwdObjsMpls.isEmpty()) {
594 return false;
sanghob35a6192015-04-01 13:05:26 -0700595 }
Pier Ventre917127a2016-10-31 16:49:19 -0700596 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800597 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700598 // the only case !BoS supported.
Charles Chan65238242017-06-22 18:03:14 -0700599 /*
600 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700601 if (fwdObjsMpls.isEmpty()) {
602 return false;
603 }
Charles Chan65238242017-06-22 18:03:14 -0700604 fwdObjs.addAll(fwdObjsMpls);
605 */
Pier Ventre917127a2016-10-31 16:49:19 -0700606
607 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700608 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700609 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
610 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700611 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700612 }
613
614 return true;
615 }
616
Saurav Das8a0732e2015-11-20 15:27:53 -0800617 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das7bcbe702017-06-13 15:35:54 -0700618 DeviceId targetSw,
Saurav Das8a0732e2015-11-20 15:27:53 -0800619 Set<DeviceId> nextHops,
620 boolean phpRequired,
621 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800622 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700623 IpAddress routerIp,
624 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800625
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700626 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
627 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700628
629 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
630
631 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800632 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700633 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700634 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700635 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800636 if (routerIp.isIp4()) {
637 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
638 } else {
639 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
640 }
641 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700642 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800643 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
644 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700645 }
646 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800647 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700648 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700649 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700650 }
651
Saurav Das8a0732e2015-11-20 15:27:53 -0800652 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700653 // if MPLS-ECMP == True we will build a standard NeighborSet.
654 // Otherwise a RandomNeighborSet.
Saurav Das7bcbe702017-06-13 15:35:54 -0700655 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700656 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700657 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700658 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700659 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700660 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700661
Saurav Dasb28d5dd2017-03-24 19:03:58 -0700662 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700663 targetSw, ns);
664 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
665 if (gh == null) {
666 log.warn("getNextObjectiveId query - groupHandler for device {} "
667 + "not found", targetSw);
668 return null;
669 }
Pier Ventre917127a2016-10-31 16:49:19 -0700670 // If BoS == True, all forwarding is via L3 ECMP group.
671 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
672 // MPLS-Interface group. This depends on the configuration of the option
673 // MPLS-ECMP.
674 // The metadata informs the driver that the next-Objective will be used
675 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das7bcbe702017-06-13 15:35:54 -0700676 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
677 dstNextHops.put(destSw, nextHops);
678 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800679 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700680 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das8a0732e2015-11-20 15:27:53 -0800681 return null;
Saurav Das25190812016-05-27 13:54:07 -0700682 } else {
683 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700684 nextId, targetSw, ns);
sanghob35a6192015-04-01 13:05:26 -0700685 }
686
Saurav Das8a0732e2015-11-20 15:27:53 -0800687 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700688 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700689 }
690
691 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700692 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700693 * dstMac corresponding to the router's MAC address. For those pipelines
694 * that need to internally assign vlans to untagged packets, this method
695 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700696 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800697 * Note that the vlan assignment and filter programming should only be done by
698 * the master for a switch. This method is typically called at deviceAdd and
699 * programs filters only for the enabled ports of the device. For port-updates,
700 * that enable/disable ports after device add, singlePortFilter methods should
701 * be called.
sanghob35a6192015-04-01 13:05:26 -0700702 *
Saurav Das822c4e22015-10-23 10:51:11 -0700703 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800704 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700705 */
Charles Chan65238242017-06-22 18:03:14 -0700706 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700707 log.debug("Installing per-port filtering objective for untagged "
708 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800709
Saurav Das59232cf2016-04-27 18:35:50 -0700710 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800711 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700712 log.warn("Device {} ports not available. Unable to add MacVlan filters",
713 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800714 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700715 }
Saurav Das018605f2017-02-18 14:05:44 -0800716 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700717 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700718 if (!port.isEnabled()) {
719 disabledPorts++;
720 continue;
721 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800722 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800723 filteredPorts++;
724 } else {
725 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700726 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700727 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700728 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800729 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800730 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800731 errorPorts, filteredPorts);
732 }
733
734 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800735 * Creates or removes filtering objectives for a single port. Should only be
736 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800737 *
738 * @param deviceId device identifier
739 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800740 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800741 * @return true if no errors occurred during the build of the filtering objective
742 */
Charles Chan65238242017-06-22 18:03:14 -0700743 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800744 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
745 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
746 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
747 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
748
749 if (taggedVlans.size() != 0) {
750 // Filter for tagged vlans
751 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800752 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800753 return false;
754 }
755 if (nativeVlan != null) {
756 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800757 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800758 return false;
759 }
760 }
761 } else if (untaggedVlan != null) {
762 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800763 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800764 return false;
765 }
766 } else {
Saurav Dasceccf242017-08-03 18:30:35 -0700767 // Unconfigured port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800768 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800769 return false;
770 }
771 }
772 return true;
773 }
774
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700775 /**
776 * Updates filtering objectives for a single port. Should only be called by
777 * the master for a switch
778 * @param deviceId device identifier
779 * @param portNum port identifier for port to be filtered
780 * @param pushVlan true to push vlan, false otherwise
781 * @param vlanId vlan identifier
782 * @param install true to install the filtering objective, false to remove
783 */
784 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
785 boolean pushVlan, VlanId vlanId, boolean install) {
786 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
787 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
788 deviceId, portNum, vlanId);
789 }
790 }
791
Charles Chan7e4f8192017-02-26 22:59:35 -0800792 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
793 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800794 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800795 if (fob == null) {
796 // error encountered during build
797 return false;
798 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700799 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700800 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800801 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800802 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700803 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800804 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700805 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800806 if (install) {
807 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
808 } else {
809 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800810 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800811 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800812 }
813
Charles Chan7ffd81f2017-02-08 15:52:08 -0800814 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
815 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800816 MacAddress deviceMac;
817 try {
818 deviceMac = config.getDeviceMac(deviceId);
819 } catch (DeviceConfigNotFoundException e) {
820 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
821 return null;
822 }
Saurav Das018605f2017-02-18 14:05:44 -0800823 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
824 fob.withKey(Criteria.matchInPort(portnum))
825 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800826 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800827
Charles Chan0d6db512017-12-19 19:55:57 -0800828 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
829
Charles Chan7ffd81f2017-02-08 15:52:08 -0800830 if (pushVlan) {
831 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan0d6db512017-12-19 19:55:57 -0800832 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800833 } else {
834 fob.addCondition(Criteria.matchVlanId(vlanId));
835 }
836
Charles Chan0d6db512017-12-19 19:55:57 -0800837 // NOTE: Some switch hardware share the same filtering flow among different ports.
838 // We use this metadata to let the driver know that there is no more enabled port
839 // within the same VLAN on this device.
840 boolean noMoreEnabledPort = srManager.interfaceService.getInterfaces().stream()
841 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
842 .filter(intf -> intf.vlanTagged().contains(vlanId) ||
843 intf.vlanUntagged().equals(vlanId) ||
844 intf.vlanNative().equals(vlanId))
845 .noneMatch(intf -> {
846 Port port = srManager.deviceService.getPort(intf.connectPoint());
847 return port != null && port.isEnabled();
848 });
849 if (noMoreEnabledPort) {
850 tBuilder.wipeDeferred();
851 }
852
853 fob.withMeta(tBuilder.build());
854
Saurav Das018605f2017-02-18 14:05:44 -0800855 fob.permit().fromApp(srManager.appId);
856 return fob;
sanghob35a6192015-04-01 13:05:26 -0700857 }
858
859 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700860 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700861 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700862 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700863 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700864 *
Saurav Das822c4e22015-10-23 10:51:11 -0700865 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700866 */
Charles Chan65238242017-06-22 18:03:14 -0700867 void populateIpPunts(DeviceId deviceId) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700868 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chan1a13c172017-12-05 21:07:38 -0800869 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800870 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800871 routerIpv4 = config.getRouterIpv4(deviceId);
872 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan1a13c172017-12-05 21:07:38 -0800873 routerLinkLocalIpv6 = Ip6Address.valueOf(
874 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
875
Saurav Das7bcbe702017-06-13 15:35:54 -0700876 if (config.isPairedEdge(deviceId)) {
877 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
878 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
879 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800880 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800881 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800882 return;
883 }
884
Saurav Das837e0bb2015-10-30 17:45:38 -0700885 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
886 log.debug("Not installing port-IP punts - not the master for dev:{} ",
887 deviceId);
888 return;
889 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800890 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
891 allIps.add(routerIpv4);
Charles Chan1a13c172017-12-05 21:07:38 -0800892 allIps.add(routerLinkLocalIpv6);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800893 if (routerIpv6 != null) {
894 allIps.add(routerIpv6);
895 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700896 if (pairRouterIpv4 != null) {
897 allIps.add(pairRouterIpv4);
898 }
899 if (pairRouterIpv6 != null) {
900 allIps.add(pairRouterIpv6);
901 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800902 for (IpAddress ipaddr : allIps) {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700903 populateSingleIpPunts(deviceId, ipaddr);
904 }
905 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800906
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700907 /**
908 * Creates a forwarding objective to punt all IP packets, destined to the
909 * specified IP address, which should be router's port IP address.
910 *
911 * @param deviceId the switch dpid for the router
912 * @param ipAddress the IP address of the router's port
913 */
914 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
915 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
916 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
917
918 srManager.packetService.requestPackets(sbuilder.build(),
919 PacketPriority.CONTROL, srManager.appId, optDeviceId);
920 }
921
922 /**
923 * Removes a forwarding objective to punt all IP packets, destined to the
924 * specified IP address, which should be router's port IP address.
925 *
926 * @param deviceId the switch dpid for the router
927 * @param ipAddress the IP address of the router's port
928 */
929 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
930 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
931 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
932
933 try {
934 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
935 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
936 srManager.packetService.cancelPackets(sbuilder.build(),
937 PacketPriority.CONTROL, srManager.appId, optDeviceId);
938 }
939 } catch (DeviceConfigNotFoundException e) {
940 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das822c4e22015-10-23 10:51:11 -0700941 }
sanghob35a6192015-04-01 13:05:26 -0700942 }
943
Charles Chan68aa62d2015-11-09 16:37:23 -0800944 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800945 * Method to build IPv4 or IPv6 selector.
946 *
947 * @param addressToMatch the address to match
948 */
949 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
950 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
951 }
952
953 /**
954 * Method to build IPv4 or IPv6 selector.
955 *
956 * @param prefixToMatch the prefix to match
957 */
958 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
959 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700960 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800961 if (prefixToMatch.isIp4()) {
962 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
963 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
964 return selectorBuilder;
965 }
Pier Ventre917127a2016-10-31 16:49:19 -0700966 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800967 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
968 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
969 return selectorBuilder;
970 }
971
972 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800973 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
974 * Furthermore, these are applied only by the master instance. Deferred actions
975 * are not cleared such that packets can be flooded in the cross connect use case
976 *
977 * @param deviceId the switch dpid for the router
978 */
Charles Chan65238242017-06-22 18:03:14 -0700979 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700980 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800981 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
982 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
983 deviceId);
984 return;
985 }
986
Charles Chan65238242017-06-22 18:03:14 -0700987 ForwardingObjective fwdObj;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800988 // We punt all ARP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -0800989 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800990 .add(new ObjectiveContext() {
991 @Override
992 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700993 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800994 deviceId, error);
995 }
996 });
Charles Chan65238242017-06-22 18:03:14 -0700997 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800998
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800999 // We punt all NDP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -08001000 fwdObj = ndpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001001 .add(new ObjectiveContext() {
1002 @Override
1003 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -07001004 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001005 deviceId, error);
1006 }
1007 });
Charles Chan65238242017-06-22 18:03:14 -07001008 srManager.flowObjectiveService.forward(deviceId, fwdObj);
1009
1010 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1011 ForwardingObjective pairFwdObj;
1012 // Do not punt ARP packets from pair port
1013 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1014 .add(new ObjectiveContext() {
1015 @Override
1016 public void onError(Objective objective, ObjectiveError error) {
1017 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1018 deviceId, error);
1019 }
1020 });
1021 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1022
1023 // Do not punt NDP packets from pair port
1024 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1025 .add(new ObjectiveContext() {
1026 @Override
1027 public void onError(Objective objective, ObjectiveError error) {
1028 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1029 deviceId, error);
1030 }
1031 });
1032 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1033
1034 // Do not forward DAD packets from pair port
1035 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1036 .add(new ObjectiveContext() {
1037 @Override
1038 public void onError(Objective objective, ObjectiveError error) {
1039 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1040 deviceId, error);
1041 }
1042 });
1043 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1044 });
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001045 }
1046
Charles Chan65238242017-06-22 18:03:14 -07001047 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1048 TrafficTreatment treatment, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001049 return DefaultForwardingObjective.builder()
Charles Chan65238242017-06-22 18:03:14 -07001050 .withPriority(priority)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001051 .withSelector(selector)
1052 .fromApp(srManager.appId)
1053 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan65238242017-06-22 18:03:14 -07001054 .withTreatment(treatment)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001055 .makePermanent();
1056 }
1057
Charles Chan65238242017-06-22 18:03:14 -07001058 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001059 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1060 sBuilder.matchEthType(TYPE_ARP);
Charles Chan65238242017-06-22 18:03:14 -07001061 if (port != null) {
1062 sBuilder.matchInPort(port);
1063 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001064
Charles Chan65238242017-06-22 18:03:14 -07001065 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1066 if (punt) {
1067 tBuilder.punt();
1068 }
1069 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001070 }
1071
Charles Chan65238242017-06-22 18:03:14 -07001072 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001073 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1074 sBuilder.matchEthType(TYPE_IPV6)
1075 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan65238242017-06-22 18:03:14 -07001076 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1077 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);
1086 }
1087
1088 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1089 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1090 sBuilder.matchEthType(TYPE_IPV6)
Charles Chanc7e36aa2017-08-07 12:39:03 -07001091 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1092 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1093 // .matchIPProtocol(PROTOCOL_ICMP6)
1094 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan65238242017-06-22 18:03:14 -07001095 if (port != null) {
1096 sBuilder.matchInPort(port);
1097 }
1098
1099 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1100 tBuilder.wipeDeferred();
1101 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001102 }
1103
1104 /**
Charles Chan68aa62d2015-11-09 16:37:23 -08001105 * Populates a forwarding objective to send packets that miss other high
1106 * priority Bridging Table entries to a group that contains all ports of
1107 * its subnet.
1108 *
Charles Chan68aa62d2015-11-09 16:37:23 -08001109 * @param deviceId switch ID to set the rules
1110 */
Charles Chan65238242017-06-22 18:03:14 -07001111 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001112 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001113 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chan68aa62d2015-11-09 16:37:23 -08001114 });
1115 }
1116
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001117 /**
1118 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1119 * @param deviceId switch ID to set the rule
1120 * @param vlanId vlan ID to specify the subnet
1121 * @param install true to install the rule, false to revoke the rule
1122 */
1123 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1124 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1125
1126 if (nextId < 0) {
1127 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1128 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1129 return;
1130 }
1131
1132 // Driver should treat objective with MacAddress.NONE as the
1133 // subnet broadcast rule
1134 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1135 sbuilder.matchVlanId(vlanId);
1136 sbuilder.matchEthDst(MacAddress.NONE);
1137
1138 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1139 fob.withFlag(Flag.SPECIFIC)
1140 .withSelector(sbuilder.build())
1141 .nextStep(nextId)
1142 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1143 .fromApp(srManager.appId)
1144 .makePermanent();
1145 ObjectiveContext context = new DefaultObjectiveContext(
1146 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1147 (objective, error) ->
1148 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1149
1150 if (install) {
1151 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1152 } else {
1153 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1154 }
1155 }
1156
Charles Chan5270ed02016-01-30 23:22:37 -08001157 private int getPriorityFromPrefix(IpPrefix prefix) {
1158 return (prefix.isIp4()) ?
1159 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1160 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001161 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001162
1163 /**
1164 * Update Forwarding objective for each host and IP address connected to given port.
1165 * And create corresponding Simple Next objective if it does not exist.
1166 * Applied only when populating Forwarding objective
1167 * @param deviceId switch ID to set the rule
1168 * @param portNumber port number
1169 * @param prefix IP prefix of the route
1170 * @param hostMac MAC address of the next hop
1171 * @param vlanId Vlan ID of the port
1172 * @param popVlan true to pop vlan tag in TrafficTreatment
1173 * @param install true to populate the forwarding objective, false to revoke
1174 */
1175 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1176 VlanId vlanId, boolean popVlan, boolean install) {
1177 ForwardingObjective.Builder fob;
1178 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1179 MacAddress deviceMac;
1180 try {
1181 deviceMac = config.getDeviceMac(deviceId);
1182 } catch (DeviceConfigNotFoundException e) {
1183 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1184 return;
1185 }
1186
1187 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1188 tbuilder.deferred()
1189 .setEthDst(hostMac)
1190 .setEthSrc(deviceMac)
1191 .setOutput(portNumber);
1192
1193 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1194
1195 if (!popVlan) {
1196 tbuilder.setVlanId(vlanId);
1197 } else {
1198 mbuilder.matchVlanId(vlanId);
1199 }
1200
1201 // if the objective is to revoke an existing rule, and for some reason
1202 // the next-objective does not exist, then a new one should not be created
1203 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1204 tbuilder.build(), mbuilder.build(), install);
1205 if (portNextObjId == -1) {
1206 // Warning log will come from getPortNextObjective method
1207 return;
1208 }
1209
1210 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1211 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1212 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1213
1214 ObjectiveContext context = new DefaultObjectiveContext(
1215 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1216 (objective, error) ->
1217 log.warn("Failed to {} IP rule for route {}: {}",
1218 install ? "install" : "revoke", prefix, error));
1219 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1220
1221 if (!install) {
1222 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1223 if (grpHandler == null) {
1224 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1225 } else {
1226 // Remove L3UG for the given port and host
1227 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1228 }
1229 }
1230 }
sanghob35a6192015-04-01 13:05:26 -07001231}