blob: 56b55ace560442cb03b89732d02b06e552d9e2bd [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;
Charles Chan1abe0702018-02-26 21:33:25 -080067import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -070068
69import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080070import static org.onlab.packet.Ethernet.TYPE_ARP;
71import static org.onlab.packet.Ethernet.TYPE_IPV6;
72import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
73import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan59cc16d2017-02-02 16:20:42 -080074import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070075
Charles Chane849c192016-01-11 18:28:54 -080076/**
77 * Populator of segment routing flow rules.
78 */
sanghob35a6192015-04-01 13:05:26 -070079public class RoutingRulePopulator {
Charles Chan1a13c172017-12-05 21:07:38 -080080 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
81
82 private static final int ARP_NDP_PRIORITY = 30000;
sanghob35a6192015-04-01 13:05:26 -070083
sangho20eff1d2015-04-13 15:15:58 -070084 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070085 private SegmentRoutingManager srManager;
86 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070087
sanghob35a6192015-04-01 13:05:26 -070088 /**
89 * Creates a RoutingRulePopulator object.
90 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070091 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070092 */
Charles Chan65238242017-06-22 18:03:14 -070093 RoutingRulePopulator(SegmentRoutingManager srManager) {
sanghob35a6192015-04-01 13:05:26 -070094 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070095 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070096 this.rulePopulationCounter = new AtomicLong(0);
97 }
98
99 /**
100 * Resets the population counter.
101 */
Charles Chan65238242017-06-22 18:03:14 -0700102 void resetCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700103 rulePopulationCounter.set(0);
104 }
105
106 /**
107 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -0700108 *
109 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700110 */
Charles Chan65238242017-06-22 18:03:14 -0700111 long getCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700112 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700113 }
114
115 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700116 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700117 * switch.
sanghob35a6192015-04-01 13:05:26 -0700118 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700119 * @param deviceId device ID of the device that next hop attaches to
120 * @param prefix IP prefix of the route
121 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800122 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700123 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700124 */
Charles Chan23686832017-08-23 14:46:43 -0700125 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800126 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700127 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chan1cdecff2016-10-27 14:19:48 -0700128 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800129 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800130 try {
Saurav Das961beb22017-03-29 19:09:17 -0700131 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
132 hostVlanId, outPort, false);
Charles Chan0b4e6182015-11-03 10:42:14 -0800133 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700134 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan0b4e6182015-11-03 10:42:14 -0800135 return;
136 }
Saurav Das59232cf2016-04-27 18:35:50 -0700137 if (fwdBuilder == null) {
Saurav Das76ae6812017-03-15 15:15:14 -0700138 log.warn("Aborting host routing table entry due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700139 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700140 return;
141 }
Charles Chan23686832017-08-23 14:46:43 -0700142
143 int nextId = fwdBuilder.add().nextId();
Charles Chand2990362016-04-18 13:44:03 -0700144 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan23686832017-08-23 14:46:43 -0700145 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
146 prefix, nextId),
Charles Chand2990362016-04-18 13:44:03 -0700147 (objective, error) ->
Saurav Das7bcbe702017-06-13 15:35:54 -0700148 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das76ae6812017-03-15 15:15:14 -0700149 prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700150 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800151 rulePopulationCounter.incrementAndGet();
152 }
153
Charles Chane849c192016-01-11 18:28:54 -0800154 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700155 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800156 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700157 * @param deviceId device ID of the device that next hop attaches to
158 * @param prefix IP prefix of the route
159 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800160 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700161 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800162 */
Charles Chan23686832017-08-23 14:46:43 -0700163 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800164 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700165 log.debug("Revoke IP table entry for route {} at {}:{}",
166 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800167 ForwardingObjective.Builder fwdBuilder;
168 try {
Saurav Das961beb22017-03-29 19:09:17 -0700169 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
170 hostVlanId, outPort, true);
Charles Chan68aa62d2015-11-09 16:37:23 -0800171 } catch (DeviceConfigNotFoundException e) {
172 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
173 return;
174 }
Charles Chan458b8262016-11-30 11:55:05 -0800175 if (fwdBuilder == null) {
176 log.warn("Aborting host routing table entries due "
177 + "to error for dev:{} route:{}", deviceId, prefix);
178 return;
179 }
Charles Chand2990362016-04-18 13:44:03 -0700180 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700181 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700182 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700183 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700184 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800185 }
186
Charles Chan1cdecff2016-10-27 14:19:48 -0700187 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800188 * Returns a forwarding objective builder for routing rules.
189 * <p>
190 * The forwarding objective routes packets destined to a given prefix to
191 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700192 *
193 * @param deviceId device ID
194 * @param prefix prefix that need to be routed
195 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800196 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700197 * @param outPort port where the nexthop attaches to
Saurav Das961beb22017-03-29 19:09:17 -0700198 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chan1cdecff2016-10-27 14:19:48 -0700199 * @return forwarding objective builder
200 * @throws DeviceConfigNotFoundException if given device is not configured
201 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800202 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700203 DeviceId deviceId, IpPrefix prefix,
Saurav Das961beb22017-03-29 19:09:17 -0700204 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
205 boolean revoke)
Charles Chan68aa62d2015-11-09 16:37:23 -0800206 throws DeviceConfigNotFoundException {
207 MacAddress deviceMac;
208 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800209
Charles Chan7ffd81f2017-02-08 15:52:08 -0800210 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
211 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
212 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
213 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700214
Charles Chan7ffd81f2017-02-08 15:52:08 -0800215 // Create route selector
216 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
217
218 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700219 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700220 tbuilder.deferred()
221 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800222 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700223 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800224
Charles Chan7ffd81f2017-02-08 15:52:08 -0800225 // Create route meta
226 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800227
Charles Chan7ffd81f2017-02-08 15:52:08 -0800228 // Adjust the meta according to VLAN configuration
229 if (taggedVlans.contains(hostVlanId)) {
230 tbuilder.setVlanId(hostVlanId);
231 } else if (hostVlanId.equals(VlanId.NONE)) {
232 if (untaggedVlan != null) {
233 mbuilder.matchVlanId(untaggedVlan);
234 } else if (nativeVlan != null) {
235 mbuilder.matchVlanId(nativeVlan);
236 } else {
Charles Chan65238242017-06-22 18:03:14 -0700237 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
238 hostMac, hostVlanId, connectPoint);
239 return null;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800240 }
241 } else {
Saurav Das961beb22017-03-29 19:09:17 -0700242 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
243 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das59232cf2016-04-27 18:35:50 -0700244 return null;
245 }
Saurav Das961beb22017-03-29 19:09:17 -0700246 // if the objective is to revoke an existing rule, and for some reason
247 // the next-objective does not exist, then a new one should not be created
Charles Chan7ffd81f2017-02-08 15:52:08 -0800248 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das961beb22017-03-29 19:09:17 -0700249 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800250 if (portNextObjId == -1) {
251 // Warning log will come from getPortNextObjective method
252 return null;
253 }
254
Charles Chan68aa62d2015-11-09 16:37:23 -0800255 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800256 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800257 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800258 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700259 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800260 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700261 }
262
263 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700264 * Populates IP flow rules for all the given prefixes reachable from the
265 * destination switch(es).
sanghob35a6192015-04-01 13:05:26 -0700266 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700267 * @param targetSw switch where rules are to be programmed
268 * @param subnets subnets/prefixes being added
269 * @param destSw1 destination switch where the prefixes are reachable
270 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
271 * Should be null if there is no paired destination switch (by config)
272 * or if the given prefixes are reachable only via destSw1
273 * @param nextHops a map containing a set of next-hops for each destination switch.
274 * If destSw2 is not null, then this map must contain an
275 * entry for destSw2 with its next-hops from the targetSw
276 * (although the next-hop set may be empty in certain scenarios).
277 * If destSw2 is null, there should not be an entry in this
278 * map for destSw2.
sanghob35a6192015-04-01 13:05:26 -0700279 * @return true if all rules are set successfully, false otherwise
280 */
Charles Chan65238242017-06-22 18:03:14 -0700281 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das7bcbe702017-06-13 15:35:54 -0700282 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700283 for (IpPrefix subnet : subnets) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700284 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sanghob35a6192015-04-01 13:05:26 -0700285 return false;
286 }
287 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700288 return true;
289 }
sanghob35a6192015-04-01 13:05:26 -0700290
Charles Chan93e71ba2016-04-29 14:38:22 -0700291 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700292 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chan93e71ba2016-04-29 14:38:22 -0700293 *
294 * @param subnets subnet being removed
295 * @return true if all rules are removed successfully, false otherwise
296 */
Charles Chan65238242017-06-22 18:03:14 -0700297 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700298 for (IpPrefix subnet : subnets) {
299 if (!revokeIpRuleForRouter(subnet)) {
300 return false;
301 }
302 }
sanghob35a6192015-04-01 13:05:26 -0700303 return true;
304 }
305
306 /**
Saurav Das25190812016-05-27 13:54:07 -0700307 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das7bcbe702017-06-13 15:35:54 -0700308 * is reachable via destination device(s).
sanghob35a6192015-04-01 13:05:26 -0700309 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700310 * @param targetSw target device ID to set the rules
311 * @param ipPrefix the IP prefix
312 * @param destSw1 destination switch where the prefixes are reachable
313 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
314 * Should be null if there is no paired destination switch (by config)
315 * or if the given prefixes are reachable only via destSw1
316 * @param nextHops map of destination switches and their next-hops.
317 * Should only contain destination switches that are
318 * actually meant to be routed to. If destSw2 is null, there
319 * should not be an entry for destSw2 in this map.
sanghob35a6192015-04-01 13:05:26 -0700320 * @return true if all rules are set successfully, false otherwise
321 */
Charles Chan65238242017-06-22 18:03:14 -0700322 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das7bcbe702017-06-13 15:35:54 -0700323 IpPrefix ipPrefix, DeviceId destSw1,
324 DeviceId destSw2,
325 Map<DeviceId, Set<DeviceId>> nextHops) {
326 int segmentId1, segmentId2 = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -0800327 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800328 if (ipPrefix.isIp4()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700329 segmentId1 = config.getIPv4SegmentId(destSw1);
330 if (destSw2 != null) {
331 segmentId2 = config.getIPv4SegmentId(destSw2);
332 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800333 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700334 segmentId1 = config.getIPv6SegmentId(destSw1);
335 if (destSw2 != null) {
336 segmentId2 = config.getIPv6SegmentId(destSw2);
337 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800338 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800339 } catch (DeviceConfigNotFoundException e) {
340 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
341 return false;
342 }
sanghob35a6192015-04-01 13:05:26 -0700343
Pier Ventree0ae7a32016-11-23 09:57:42 -0800344 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800345 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700346
Charles Chan68aa62d2015-11-09 16:37:23 -0800347 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700348 DestinationSet ds;
Charles Chan68aa62d2015-11-09 16:37:23 -0800349 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700350
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700351 // If the next hop is the same as the final destination, then MPLS label
352 // is not set.
Saurav Das7bcbe702017-06-13 15:35:54 -0700353 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800354 tbuilder.immediate().decNwTtl();
Saurav Das7bcbe702017-06-13 15:35:54 -0700355 ds = new DestinationSet(false, destSw);
Charles Chan68aa62d2015-11-09 16:37:23 -0800356 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700357 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700358 ds = new DestinationSet(false, segmentId, destSw);
359 treatment = null;
360 }*/
361 if (destSw2 == null) {
362 // single dst - create destination set based on next-hop
363 Set<DeviceId> nhd1 = nextHops.get(destSw1);
364 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
365 tbuilder.immediate().decNwTtl();
366 ds = new DestinationSet(false, destSw1);
367 treatment = tbuilder.build();
368 } else {
369 ds = new DestinationSet(false, segmentId1, destSw1);
370 treatment = null;
371 }
372 } else {
373 // dst pair - IP rules for dst-pairs are always from other edge nodes
374 // the destination set needs to have both destinations, even if there
375 // are no next hops to one of them
376 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chan68aa62d2015-11-09 16:37:23 -0800377 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700378 }
379
Saurav Das8a0732e2015-11-20 15:27:53 -0800380 // setup metadata to pass to nextObjective - indicate the vlan on egress
381 // if needed by the switch pipeline. Since neighbor sets are always to
382 // other neighboring routers, there is no subnet assigned on those ports.
383 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800384 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das7bcbe702017-06-13 15:35:54 -0700385 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700386 if (grpHandler == null) {
387 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700388 + "not found", targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700389 return false;
390 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800391
Saurav Das7bcbe702017-06-13 15:35:54 -0700392 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
393 metabuilder.build(), true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800394 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700395 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho834e4b02015-05-01 09:38:25 -0700396 return false;
397 }
398
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700399 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
400 .builder()
401 .fromApp(srManager.appId)
402 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700404 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800405 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700406 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800407 if (treatment != null) {
408 fwdBuilder.withTreatment(treatment);
409 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700410 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700411 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chand2990362016-04-18 13:44:03 -0700412 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700413 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700414 ipPrefix, targetSw),
Charles Chand2990362016-04-18 13:44:03 -0700415 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700416 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700417 ipPrefix, error, targetSw));
418 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700419 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700420
421 return true;
422 }
423
sanghob35a6192015-04-01 13:05:26 -0700424 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700425 * Revokes IP flow rules for the router IP address.
426 *
427 * @param ipPrefix the IP address of the destination router
428 * @return true if all rules are removed successfully, false otherwise
429 */
Charles Chan65238242017-06-22 18:03:14 -0700430 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800431 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700432 TrafficSelector selector = sbuilder.build();
433 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
434
435 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
436 .builder()
437 .fromApp(srManager.appId)
438 .makePermanent()
439 .withSelector(selector)
440 .withTreatment(dummyTreatment)
441 .withPriority(getPriorityFromPrefix(ipPrefix))
442 .withFlag(ForwardingObjective.Flag.SPECIFIC);
443
Charles Chan1e848472018-01-30 13:31:30 -0800444 srManager.deviceService.getAvailableDevices().forEach(device -> {
445 if (srManager.mastershipService.isLocalMaster(device.id())) {
446 ObjectiveContext context = new DefaultObjectiveContext(
447 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
448 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
449 ipPrefix, device.id(), error));
450 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
451 } else {
452 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
453 }
454 });
Charles Chan93e71ba2016-04-29 14:38:22 -0700455
456 return true;
457 }
458
459 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700460 * Deals with !MPLS Bos use case.
461 *
462 * @param targetSwId the target sw
463 * @param destSwId the destination sw
464 * @param nextHops the set of next hops
465 * @param segmentId the segmentId to match
466 * @param routerIp the router ip
467 * @return a collection of fwdobjective
468 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700469 private Collection<ForwardingObjective> handleMpls(
470 DeviceId targetSwId,
471 DeviceId destSwId,
472 Set<DeviceId> nextHops,
473 int segmentId,
474 IpAddress routerIp,
475 boolean isMplsBos) {
Pier Ventre917127a2016-10-31 16:49:19 -0700476
477 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
478 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800479 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700480 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
481 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
482 sbuilder.matchMplsBos(isMplsBos);
483 TrafficSelector selector = sbuilder.build();
484
485 // setup metadata to pass to nextObjective - indicate the vlan on egress
486 // if needed by the switch pipeline. Since mpls next-hops are always to
487 // other neighboring routers, there is no subnet assigned on those ports.
488 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800489 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700490
491 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
492 // If the next hop is the destination router for the segment, do pop
493 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das7bcbe702017-06-13 15:35:54 -0700494 + "label {} in switch {} with pop to next-hops {}",
495 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700496 // Not-bos pop case (php for the current label). If MPLS-ECMP
497 // has been configured, the application we will request the
498 // installation for an MPLS-ECMP group.
Saurav Das961beb22017-03-29 19:09:17 -0700499 ForwardingObjective.Builder fwdObjNoBosBuilder =
500 getMplsForwardingObjective(targetSwId,
501 nextHops,
502 true,
503 isMplsBos,
504 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700505 routerIp,
506 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700507 // Error case, we cannot handle, exit.
508 if (fwdObjNoBosBuilder == null) {
509 return Collections.emptyList();
510 }
511 fwdObjBuilders.add(fwdObjNoBosBuilder);
512
513 } else {
514 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das7bcbe702017-06-13 15:35:54 -0700515 log.debug("Installing MPLS forwarding objective for "
516 + "label {} in switch {} without pop to next-hops {}",
517 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700518 // Not-bos pop case. If MPLS-ECMP has been configured, the
519 // application we will request the installation for an MPLS-ECMP
520 // group.
Saurav Das961beb22017-03-29 19:09:17 -0700521 ForwardingObjective.Builder fwdObjNoBosBuilder =
522 getMplsForwardingObjective(targetSwId,
523 nextHops,
524 false,
525 isMplsBos,
526 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700527 routerIp,
528 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700529 // Error case, we cannot handle, exit.
530 if (fwdObjNoBosBuilder == null) {
531 return Collections.emptyList();
532 }
533 fwdObjBuilders.add(fwdObjNoBosBuilder);
534
535 }
536
537 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
538 // We add the final property to the fwdObjs.
539 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
540
541 ((Builder) ((Builder) fwdObjBuilder
542 .fromApp(srManager.appId)
543 .makePermanent())
544 .withSelector(selector)
545 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
546 .withFlag(ForwardingObjective.Flag.SPECIFIC);
547
548 ObjectiveContext context = new DefaultObjectiveContext(
549 (objective) ->
550 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
551 objective.id(), segmentId, targetSwId),
552 (objective, error) ->
553 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
554 objective.id(), segmentId, error, targetSwId));
555
556 ForwardingObjective fob = fwdObjBuilder.add(context);
557 fwdObjs.add(fob);
558
559 }
560
561 return fwdObjs;
562 }
563
564 /**
Saurav Das25190812016-05-27 13:54:07 -0700565 * Populates MPLS flow rules in the target device to point towards the
566 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700567 *
Saurav Das25190812016-05-27 13:54:07 -0700568 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700569 * @param destSwId destination switch device ID
570 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700571 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700572 * @return true if all rules are set successfully, false otherwise
573 */
Charles Chan65238242017-06-22 18:03:14 -0700574 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700575 Set<DeviceId> nextHops,
576 IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700577
Charles Chan0b4e6182015-11-03 10:42:14 -0800578 int segmentId;
579 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800580 if (routerIp.isIp4()) {
581 segmentId = config.getIPv4SegmentId(destSwId);
582 } else {
583 segmentId = config.getIPv6SegmentId(destSwId);
584 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800585 } catch (DeviceConfigNotFoundException e) {
586 log.warn(e.getMessage() + " Aborting populateMplsRule.");
587 return false;
588 }
sanghob35a6192015-04-01 13:05:26 -0700589
Pier Ventre917127a2016-10-31 16:49:19 -0700590 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan65238242017-06-22 18:03:14 -0700591 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre917127a2016-10-31 16:49:19 -0700592 // Generates the transit rules used by the standard "routing".
593 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
594 if (fwdObjsMpls.isEmpty()) {
595 return false;
sanghob35a6192015-04-01 13:05:26 -0700596 }
Pier Ventre917127a2016-10-31 16:49:19 -0700597 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800598 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700599 // the only case !BoS supported.
Charles Chan65238242017-06-22 18:03:14 -0700600 /*
601 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700602 if (fwdObjsMpls.isEmpty()) {
603 return false;
604 }
Charles Chan65238242017-06-22 18:03:14 -0700605 fwdObjs.addAll(fwdObjsMpls);
606 */
Pier Ventre917127a2016-10-31 16:49:19 -0700607
608 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700609 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700610 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
611 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700612 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700613 }
614
615 return true;
616 }
617
Saurav Das8a0732e2015-11-20 15:27:53 -0800618 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das7bcbe702017-06-13 15:35:54 -0700619 DeviceId targetSw,
Saurav Das8a0732e2015-11-20 15:27:53 -0800620 Set<DeviceId> nextHops,
621 boolean phpRequired,
622 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800623 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700624 IpAddress routerIp,
625 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800626
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700627 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
628 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700629
630 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
631
632 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800633 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700634 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700635 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700636 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800637 if (routerIp.isIp4()) {
638 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
639 } else {
640 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
641 }
642 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700643 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800644 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
645 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700646 }
647 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800648 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700649 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700650 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700651 }
652
Saurav Das8a0732e2015-11-20 15:27:53 -0800653 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700654 // if MPLS-ECMP == True we will build a standard NeighborSet.
655 // Otherwise a RandomNeighborSet.
Saurav Das7bcbe702017-06-13 15:35:54 -0700656 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700657 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700658 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700659 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700660 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700661 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700662
Saurav Dasb28d5dd2017-03-24 19:03:58 -0700663 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700664 targetSw, ns);
665 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
666 if (gh == null) {
667 log.warn("getNextObjectiveId query - groupHandler for device {} "
668 + "not found", targetSw);
669 return null;
670 }
Pier Ventre917127a2016-10-31 16:49:19 -0700671 // If BoS == True, all forwarding is via L3 ECMP group.
672 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
673 // MPLS-Interface group. This depends on the configuration of the option
674 // MPLS-ECMP.
675 // The metadata informs the driver that the next-Objective will be used
676 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das7bcbe702017-06-13 15:35:54 -0700677 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
678 dstNextHops.put(destSw, nextHops);
679 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800680 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700681 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das8a0732e2015-11-20 15:27:53 -0800682 return null;
Saurav Das25190812016-05-27 13:54:07 -0700683 } else {
684 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700685 nextId, targetSw, ns);
sanghob35a6192015-04-01 13:05:26 -0700686 }
687
Saurav Das8a0732e2015-11-20 15:27:53 -0800688 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700689 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700690 }
691
692 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700693 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700694 * dstMac corresponding to the router's MAC address. For those pipelines
695 * that need to internally assign vlans to untagged packets, this method
696 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700697 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800698 * Note that the vlan assignment and filter programming should only be done by
699 * the master for a switch. This method is typically called at deviceAdd and
700 * programs filters only for the enabled ports of the device. For port-updates,
701 * that enable/disable ports after device add, singlePortFilter methods should
702 * be called.
sanghob35a6192015-04-01 13:05:26 -0700703 *
Saurav Das822c4e22015-10-23 10:51:11 -0700704 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800705 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700706 */
Charles Chan65238242017-06-22 18:03:14 -0700707 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700708 log.debug("Installing per-port filtering objective for untagged "
709 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800710
Saurav Das59232cf2016-04-27 18:35:50 -0700711 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800712 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700713 log.warn("Device {} ports not available. Unable to add MacVlan filters",
714 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800715 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700716 }
Saurav Das018605f2017-02-18 14:05:44 -0800717 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700718 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700719 if (!port.isEnabled()) {
720 disabledPorts++;
721 continue;
722 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800723 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800724 filteredPorts++;
725 } else {
726 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700727 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700728 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700729 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800730 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800731 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800732 errorPorts, filteredPorts);
733 }
734
735 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800736 * Creates or removes filtering objectives for a single port. Should only be
737 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800738 *
739 * @param deviceId device identifier
740 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800741 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800742 * @return true if no errors occurred during the build of the filtering objective
743 */
Charles Chan65238242017-06-22 18:03:14 -0700744 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800745 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
746 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
747 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
748 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
749
750 if (taggedVlans.size() != 0) {
751 // Filter for tagged vlans
752 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800753 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800754 return false;
755 }
756 if (nativeVlan != null) {
757 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800758 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800759 return false;
760 }
761 }
762 } else if (untaggedVlan != null) {
763 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800764 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800765 return false;
766 }
767 } else {
Saurav Dasceccf242017-08-03 18:30:35 -0700768 // Unconfigured port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800769 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800770 return false;
771 }
772 }
773 return true;
774 }
775
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700776 /**
777 * Updates filtering objectives for a single port. Should only be called by
778 * the master for a switch
779 * @param deviceId device identifier
780 * @param portNum port identifier for port to be filtered
781 * @param pushVlan true to push vlan, false otherwise
782 * @param vlanId vlan identifier
783 * @param install true to install the filtering objective, false to remove
784 */
785 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
786 boolean pushVlan, VlanId vlanId, boolean install) {
787 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
788 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
789 deviceId, portNum, vlanId);
790 }
791 }
792
Charles Chan7e4f8192017-02-26 22:59:35 -0800793 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
794 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800795 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800796 if (fob == null) {
797 // error encountered during build
798 return false;
799 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700800 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700801 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800802 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800803 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700804 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800805 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700806 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800807 if (install) {
808 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
809 } else {
810 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800811 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800812 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800813 }
814
Charles Chan7ffd81f2017-02-08 15:52:08 -0800815 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
816 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800817 MacAddress deviceMac;
818 try {
819 deviceMac = config.getDeviceMac(deviceId);
820 } catch (DeviceConfigNotFoundException e) {
821 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
822 return null;
823 }
Saurav Das018605f2017-02-18 14:05:44 -0800824 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
825 fob.withKey(Criteria.matchInPort(portnum))
826 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800827 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800828
Charles Chan0d6db512017-12-19 19:55:57 -0800829 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
830
Charles Chan7ffd81f2017-02-08 15:52:08 -0800831 if (pushVlan) {
832 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan0d6db512017-12-19 19:55:57 -0800833 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800834 } else {
835 fob.addCondition(Criteria.matchVlanId(vlanId));
836 }
837
Charles Chan0d6db512017-12-19 19:55:57 -0800838 // NOTE: Some switch hardware share the same filtering flow among different ports.
839 // We use this metadata to let the driver know that there is no more enabled port
840 // within the same VLAN on this device.
Charles Chan1abe0702018-02-26 21:33:25 -0800841 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan0d6db512017-12-19 19:55:57 -0800842 tBuilder.wipeDeferred();
843 }
844
845 fob.withMeta(tBuilder.build());
846
Saurav Das018605f2017-02-18 14:05:44 -0800847 fob.permit().fromApp(srManager.appId);
848 return fob;
sanghob35a6192015-04-01 13:05:26 -0700849 }
850
851 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700852 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700853 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700854 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700855 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700856 *
Saurav Das822c4e22015-10-23 10:51:11 -0700857 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700858 */
Charles Chan65238242017-06-22 18:03:14 -0700859 void populateIpPunts(DeviceId deviceId) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700860 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chan1a13c172017-12-05 21:07:38 -0800861 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800862 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800863 routerIpv4 = config.getRouterIpv4(deviceId);
864 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chan1a13c172017-12-05 21:07:38 -0800865 routerLinkLocalIpv6 = Ip6Address.valueOf(
866 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
867
Saurav Das7bcbe702017-06-13 15:35:54 -0700868 if (config.isPairedEdge(deviceId)) {
869 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
870 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
871 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800872 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800873 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800874 return;
875 }
876
Saurav Das837e0bb2015-10-30 17:45:38 -0700877 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
878 log.debug("Not installing port-IP punts - not the master for dev:{} ",
879 deviceId);
880 return;
881 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800882 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
883 allIps.add(routerIpv4);
Charles Chan1a13c172017-12-05 21:07:38 -0800884 allIps.add(routerLinkLocalIpv6);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800885 if (routerIpv6 != null) {
886 allIps.add(routerIpv6);
887 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700888 if (pairRouterIpv4 != null) {
889 allIps.add(pairRouterIpv4);
890 }
891 if (pairRouterIpv6 != null) {
892 allIps.add(pairRouterIpv6);
893 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800894 for (IpAddress ipaddr : allIps) {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700895 populateSingleIpPunts(deviceId, ipaddr);
896 }
897 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800898
Jonghwan Hyun5f1def82017-08-25 17:48:36 -0700899 /**
900 * Creates a forwarding objective to punt all IP packets, destined to the
901 * specified IP address, which should be router's port IP address.
902 *
903 * @param deviceId the switch dpid for the router
904 * @param ipAddress the IP address of the router's port
905 */
906 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
907 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
908 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
909
910 srManager.packetService.requestPackets(sbuilder.build(),
911 PacketPriority.CONTROL, srManager.appId, optDeviceId);
912 }
913
914 /**
915 * Removes a forwarding objective to punt all IP packets, destined to the
916 * specified IP address, which should be router's port IP address.
917 *
918 * @param deviceId the switch dpid for the router
919 * @param ipAddress the IP address of the router's port
920 */
921 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
922 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
923 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
924
925 try {
926 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
927 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
928 srManager.packetService.cancelPackets(sbuilder.build(),
929 PacketPriority.CONTROL, srManager.appId, optDeviceId);
930 }
931 } catch (DeviceConfigNotFoundException e) {
932 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das822c4e22015-10-23 10:51:11 -0700933 }
sanghob35a6192015-04-01 13:05:26 -0700934 }
935
Charles Chan68aa62d2015-11-09 16:37:23 -0800936 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800937 * Method to build IPv4 or IPv6 selector.
938 *
939 * @param addressToMatch the address to match
940 */
941 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
942 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
943 }
944
945 /**
946 * Method to build IPv4 or IPv6 selector.
947 *
948 * @param prefixToMatch the prefix to match
949 */
950 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
951 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700952 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800953 if (prefixToMatch.isIp4()) {
954 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
955 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
956 return selectorBuilder;
957 }
Pier Ventre917127a2016-10-31 16:49:19 -0700958 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800959 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
960 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
961 return selectorBuilder;
962 }
963
964 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800965 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
966 * Furthermore, these are applied only by the master instance. Deferred actions
967 * are not cleared such that packets can be flooded in the cross connect use case
968 *
969 * @param deviceId the switch dpid for the router
970 */
Charles Chan65238242017-06-22 18:03:14 -0700971 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700972 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800973 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
974 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
975 deviceId);
976 return;
977 }
978
Charles Chan65238242017-06-22 18:03:14 -0700979 ForwardingObjective fwdObj;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800980 // We punt all ARP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -0800981 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800982 .add(new ObjectiveContext() {
983 @Override
984 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700985 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800986 deviceId, error);
987 }
988 });
Charles Chan65238242017-06-22 18:03:14 -0700989 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800990
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800991 // We punt all NDP packets towards the controller.
Charles Chan1a13c172017-12-05 21:07:38 -0800992 fwdObj = ndpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800993 .add(new ObjectiveContext() {
994 @Override
995 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700996 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800997 deviceId, error);
998 }
999 });
Charles Chan65238242017-06-22 18:03:14 -07001000 srManager.flowObjectiveService.forward(deviceId, fwdObj);
1001
1002 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1003 ForwardingObjective pairFwdObj;
1004 // Do not punt ARP packets from pair port
1005 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1006 .add(new ObjectiveContext() {
1007 @Override
1008 public void onError(Objective objective, ObjectiveError error) {
1009 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1010 deviceId, error);
1011 }
1012 });
1013 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1014
1015 // Do not punt NDP packets from pair port
1016 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1017 .add(new ObjectiveContext() {
1018 @Override
1019 public void onError(Objective objective, ObjectiveError error) {
1020 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1021 deviceId, error);
1022 }
1023 });
1024 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1025
1026 // Do not forward DAD packets from pair port
1027 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1028 .add(new ObjectiveContext() {
1029 @Override
1030 public void onError(Objective objective, ObjectiveError error) {
1031 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1032 deviceId, error);
1033 }
1034 });
1035 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1036 });
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001037 }
1038
Charles Chan65238242017-06-22 18:03:14 -07001039 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1040 TrafficTreatment treatment, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001041 return DefaultForwardingObjective.builder()
Charles Chan65238242017-06-22 18:03:14 -07001042 .withPriority(priority)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001043 .withSelector(selector)
1044 .fromApp(srManager.appId)
1045 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan65238242017-06-22 18:03:14 -07001046 .withTreatment(treatment)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001047 .makePermanent();
1048 }
1049
Charles Chan65238242017-06-22 18:03:14 -07001050 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001051 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1052 sBuilder.matchEthType(TYPE_ARP);
Charles Chan65238242017-06-22 18:03:14 -07001053 if (port != null) {
1054 sBuilder.matchInPort(port);
1055 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001056
Charles Chan65238242017-06-22 18:03:14 -07001057 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1058 if (punt) {
1059 tBuilder.punt();
1060 }
1061 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001062 }
1063
Charles Chan65238242017-06-22 18:03:14 -07001064 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001065 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1066 sBuilder.matchEthType(TYPE_IPV6)
1067 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan65238242017-06-22 18:03:14 -07001068 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1069 if (port != null) {
1070 sBuilder.matchInPort(port);
1071 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001072
Charles Chan65238242017-06-22 18:03:14 -07001073 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1074 if (punt) {
1075 tBuilder.punt();
1076 }
1077 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1078 }
1079
1080 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1081 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1082 sBuilder.matchEthType(TYPE_IPV6)
Charles Chanc7e36aa2017-08-07 12:39:03 -07001083 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1084 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1085 // .matchIPProtocol(PROTOCOL_ICMP6)
1086 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan65238242017-06-22 18:03:14 -07001087 if (port != null) {
1088 sBuilder.matchInPort(port);
1089 }
1090
1091 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1092 tBuilder.wipeDeferred();
1093 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001094 }
1095
1096 /**
Charles Chan68aa62d2015-11-09 16:37:23 -08001097 * Populates a forwarding objective to send packets that miss other high
1098 * priority Bridging Table entries to a group that contains all ports of
1099 * its subnet.
1100 *
Charles Chan68aa62d2015-11-09 16:37:23 -08001101 * @param deviceId switch ID to set the rules
1102 */
Charles Chan65238242017-06-22 18:03:14 -07001103 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001104 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001105 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chan68aa62d2015-11-09 16:37:23 -08001106 });
1107 }
1108
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001109 /**
1110 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1111 * @param deviceId switch ID to set the rule
1112 * @param vlanId vlan ID to specify the subnet
1113 * @param install true to install the rule, false to revoke the rule
1114 */
1115 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1116 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1117
1118 if (nextId < 0) {
1119 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1120 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1121 return;
1122 }
1123
1124 // Driver should treat objective with MacAddress.NONE as the
1125 // subnet broadcast rule
1126 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1127 sbuilder.matchVlanId(vlanId);
1128 sbuilder.matchEthDst(MacAddress.NONE);
1129
1130 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1131 fob.withFlag(Flag.SPECIFIC)
1132 .withSelector(sbuilder.build())
1133 .nextStep(nextId)
1134 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1135 .fromApp(srManager.appId)
1136 .makePermanent();
1137 ObjectiveContext context = new DefaultObjectiveContext(
1138 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1139 (objective, error) ->
1140 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1141
1142 if (install) {
1143 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1144 } else {
1145 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1146 }
1147 }
1148
Charles Chan5270ed02016-01-30 23:22:37 -08001149 private int getPriorityFromPrefix(IpPrefix prefix) {
1150 return (prefix.isIp4()) ?
1151 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1152 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001153 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001154
1155 /**
1156 * Update Forwarding objective for each host and IP address connected to given port.
1157 * And create corresponding Simple Next objective if it does not exist.
1158 * Applied only when populating Forwarding objective
1159 * @param deviceId switch ID to set the rule
1160 * @param portNumber port number
1161 * @param prefix IP prefix of the route
1162 * @param hostMac MAC address of the next hop
1163 * @param vlanId Vlan ID of the port
1164 * @param popVlan true to pop vlan tag in TrafficTreatment
1165 * @param install true to populate the forwarding objective, false to revoke
1166 */
1167 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1168 VlanId vlanId, boolean popVlan, boolean install) {
1169 ForwardingObjective.Builder fob;
1170 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1171 MacAddress deviceMac;
1172 try {
1173 deviceMac = config.getDeviceMac(deviceId);
1174 } catch (DeviceConfigNotFoundException e) {
1175 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1176 return;
1177 }
1178
1179 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1180 tbuilder.deferred()
1181 .setEthDst(hostMac)
1182 .setEthSrc(deviceMac)
1183 .setOutput(portNumber);
1184
1185 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1186
1187 if (!popVlan) {
1188 tbuilder.setVlanId(vlanId);
1189 } else {
1190 mbuilder.matchVlanId(vlanId);
1191 }
1192
1193 // if the objective is to revoke an existing rule, and for some reason
1194 // the next-objective does not exist, then a new one should not be created
1195 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1196 tbuilder.build(), mbuilder.build(), install);
1197 if (portNextObjId == -1) {
1198 // Warning log will come from getPortNextObjective method
1199 return;
1200 }
1201
1202 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1203 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1204 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1205
1206 ObjectiveContext context = new DefaultObjectiveContext(
1207 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1208 (objective, error) ->
1209 log.warn("Failed to {} IP rule for route {}: {}",
1210 install ? "install" : "revoke", prefix, error));
1211 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1212
1213 if (!install) {
1214 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1215 if (grpHandler == null) {
1216 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1217 } else {
1218 // Remove L3UG for the given port and host
1219 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1220 }
1221 }
1222 }
Charles Chan1abe0702018-02-26 21:33:25 -08001223
1224 /**
1225 * Checks if there is other enabled port within the given VLAN on the given device.
1226 *
1227 * @param deviceId device ID
1228 * @param vlanId VLAN ID
1229 * @return true if there is no more port enabled within the given VLAN on the given device
1230 */
1231 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1232 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1233 .filter(Port::isEnabled)
1234 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1235 .collect(Collectors.toSet());
1236
1237 return enabledPorts.stream().noneMatch(cp ->
1238 // Given vlanId is included in the vlan-tagged configuration
1239 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1240 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1241 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1242 vlanId.equals(INTERNAL_VLAN)) ||
1243 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1244 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1245 );
1246 }
sanghob35a6192015-04-01 13:05:26 -07001247}