blob: 3fb38ba869843ae0d836aa9d8348c006d51fbacc [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;
21import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
sanghob35a6192015-04-01 13:05:26 -070026import org.onlab.packet.MplsLabel;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070027import org.onlab.packet.VlanId;
Charles Chane849c192016-01-11 18:28:54 -080028import org.onosproject.net.ConnectPoint;
Charles Chand2990362016-04-18 13:44:03 -070029import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080030import org.onosproject.net.flowobjective.Objective;
Charles Chand2990362016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2df0e8a2017-01-09 11:45:08 -080033import org.onosproject.net.packet.PacketPriority;
Saurav Dasd2fded02016-12-02 15:43:47 -080034import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan0b4e6182015-11-03 10:42:14 -080035import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
36import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Dasc88d4662017-05-15 15:34:25 -070037import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070038import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.net.DeviceId;
Saurav Das0e99e2b2015-10-28 12:39:42 -070040import org.onosproject.net.Port;
sanghob35a6192015-04-01 13:05:26 -070041import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070042import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
sanghob35a6192015-04-01 13:05:26 -070044import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070046import org.onosproject.net.flow.criteria.Criteria;
47import org.onosproject.net.flowobjective.DefaultFilteringObjective;
48import org.onosproject.net.flowobjective.DefaultForwardingObjective;
49import org.onosproject.net.flowobjective.FilteringObjective;
50import org.onosproject.net.flowobjective.ForwardingObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das822c4e22015-10-23 10:51:11 -070052import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sanghob35a6192015-04-01 13:05:26 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import java.util.ArrayList;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.Collection;
58import java.util.Collections;
Saurav Das7bcbe702017-06-13 15:35:54 -070059import java.util.HashMap;
Saurav Das837e0bb2015-10-30 17:45:38 -070060import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070061import java.util.List;
Saurav Das7bcbe702017-06-13 15:35:54 -070062import java.util.Map;
Charles Chan2df0e8a2017-01-09 11:45:08 -080063import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070064import java.util.Set;
sangho20eff1d2015-04-13 15:15:58 -070065import java.util.concurrent.atomic.AtomicLong;
sanghob35a6192015-04-01 13:05:26 -070066
67import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -080068import static org.onlab.packet.Ethernet.TYPE_ARP;
69import static org.onlab.packet.Ethernet.TYPE_IPV6;
70import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
71import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan59cc16d2017-02-02 16:20:42 -080072import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sanghob35a6192015-04-01 13:05:26 -070073
Charles Chane849c192016-01-11 18:28:54 -080074/**
75 * Populator of segment routing flow rules.
76 */
sanghob35a6192015-04-01 13:05:26 -070077public class RoutingRulePopulator {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070078 private static final Logger log = LoggerFactory
79 .getLogger(RoutingRulePopulator.class);
sanghob35a6192015-04-01 13:05:26 -070080
sangho20eff1d2015-04-13 15:15:58 -070081 private AtomicLong rulePopulationCounter;
sangho666cd6d2015-04-14 16:27:13 -070082 private SegmentRoutingManager srManager;
83 private DeviceConfiguration config;
Saurav Das822c4e22015-10-23 10:51:11 -070084
sanghob35a6192015-04-01 13:05:26 -070085 /**
86 * Creates a RoutingRulePopulator object.
87 *
Thomas Vachuskae10f56b2015-04-15 18:20:08 -070088 * @param srManager segment routing manager reference
sanghob35a6192015-04-01 13:05:26 -070089 */
Charles Chan65238242017-06-22 18:03:14 -070090 RoutingRulePopulator(SegmentRoutingManager srManager) {
sanghob35a6192015-04-01 13:05:26 -070091 this.srManager = srManager;
sangho666cd6d2015-04-14 16:27:13 -070092 this.config = checkNotNull(srManager.deviceConfiguration);
sangho20eff1d2015-04-13 15:15:58 -070093 this.rulePopulationCounter = new AtomicLong(0);
94 }
95
96 /**
97 * Resets the population counter.
98 */
Charles Chan65238242017-06-22 18:03:14 -070099 void resetCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700100 rulePopulationCounter.set(0);
101 }
102
103 /**
104 * Returns the number of rules populated.
Thomas Vachuska266b4432015-04-30 18:13:25 -0700105 *
106 * @return number of rules
sangho20eff1d2015-04-13 15:15:58 -0700107 */
Charles Chan65238242017-06-22 18:03:14 -0700108 long getCounter() {
sangho20eff1d2015-04-13 15:15:58 -0700109 return rulePopulationCounter.get();
sanghob35a6192015-04-01 13:05:26 -0700110 }
111
112 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700113 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700114 * switch.
sanghob35a6192015-04-01 13:05:26 -0700115 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700116 * @param deviceId device ID of the device that next hop attaches to
117 * @param prefix IP prefix of the route
118 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800119 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700120 * @param outPort port where the next hop attaches to
sanghob35a6192015-04-01 13:05:26 -0700121 */
Charles Chan23686832017-08-23 14:46:43 -0700122 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800123 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700124 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chan1cdecff2016-10-27 14:19:48 -0700125 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800126 ForwardingObjective.Builder fwdBuilder;
Charles Chan0b4e6182015-11-03 10:42:14 -0800127 try {
Saurav Das961beb22017-03-29 19:09:17 -0700128 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
129 hostVlanId, outPort, false);
Charles Chan0b4e6182015-11-03 10:42:14 -0800130 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700131 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan0b4e6182015-11-03 10:42:14 -0800132 return;
133 }
Saurav Das59232cf2016-04-27 18:35:50 -0700134 if (fwdBuilder == null) {
Saurav Das76ae6812017-03-15 15:15:14 -0700135 log.warn("Aborting host routing table entry due "
Charles Chan1cdecff2016-10-27 14:19:48 -0700136 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das59232cf2016-04-27 18:35:50 -0700137 return;
138 }
Charles Chan23686832017-08-23 14:46:43 -0700139
140 int nextId = fwdBuilder.add().nextId();
Charles Chand2990362016-04-18 13:44:03 -0700141 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan23686832017-08-23 14:46:43 -0700142 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
143 prefix, nextId),
Charles Chand2990362016-04-18 13:44:03 -0700144 (objective, error) ->
Saurav Das7bcbe702017-06-13 15:35:54 -0700145 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das76ae6812017-03-15 15:15:14 -0700146 prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700147 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800148 rulePopulationCounter.incrementAndGet();
149 }
150
Charles Chane849c192016-01-11 18:28:54 -0800151 /**
Charles Chan1cdecff2016-10-27 14:19:48 -0700152 * Removes IP rules for a route when the next hop is gone.
Charles Chane849c192016-01-11 18:28:54 -0800153 *
Charles Chan1cdecff2016-10-27 14:19:48 -0700154 * @param deviceId device ID of the device that next hop attaches to
155 * @param prefix IP prefix of the route
156 * @param hostMac MAC address of the next hop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800157 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700158 * @param outPort port that next hop attaches to
Charles Chane849c192016-01-11 18:28:54 -0800159 */
Charles Chan23686832017-08-23 14:46:43 -0700160 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -0800161 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chan1cdecff2016-10-27 14:19:48 -0700162 log.debug("Revoke IP table entry for route {} at {}:{}",
163 prefix, deviceId, outPort);
Charles Chan68aa62d2015-11-09 16:37:23 -0800164 ForwardingObjective.Builder fwdBuilder;
165 try {
Saurav Das961beb22017-03-29 19:09:17 -0700166 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
167 hostVlanId, outPort, true);
Charles Chan68aa62d2015-11-09 16:37:23 -0800168 } catch (DeviceConfigNotFoundException e) {
169 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
170 return;
171 }
Charles Chan458b8262016-11-30 11:55:05 -0800172 if (fwdBuilder == null) {
173 log.warn("Aborting host routing table entries due "
174 + "to error for dev:{} route:{}", deviceId, prefix);
175 return;
176 }
Charles Chand2990362016-04-18 13:44:03 -0700177 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan1cdecff2016-10-27 14:19:48 -0700178 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chand2990362016-04-18 13:44:03 -0700179 (objective, error) ->
Charles Chan1cdecff2016-10-27 14:19:48 -0700180 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chand2990362016-04-18 13:44:03 -0700181 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chan68aa62d2015-11-09 16:37:23 -0800182 }
183
Charles Chan1cdecff2016-10-27 14:19:48 -0700184 /**
Charles Chanf6ec1532017-02-08 16:10:40 -0800185 * Returns a forwarding objective builder for routing rules.
186 * <p>
187 * The forwarding objective routes packets destined to a given prefix to
188 * given port on given device with given destination MAC.
Charles Chan1cdecff2016-10-27 14:19:48 -0700189 *
190 * @param deviceId device ID
191 * @param prefix prefix that need to be routed
192 * @param hostMac MAC address of the nexthop
Charles Chan7ffd81f2017-02-08 15:52:08 -0800193 * @param hostVlanId Vlan ID of the nexthop
Charles Chan1cdecff2016-10-27 14:19:48 -0700194 * @param outPort port where the nexthop attaches to
Saurav Das961beb22017-03-29 19:09:17 -0700195 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chan1cdecff2016-10-27 14:19:48 -0700196 * @return forwarding objective builder
197 * @throws DeviceConfigNotFoundException if given device is not configured
198 */
Charles Chanf6ec1532017-02-08 16:10:40 -0800199 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chan1cdecff2016-10-27 14:19:48 -0700200 DeviceId deviceId, IpPrefix prefix,
Saurav Das961beb22017-03-29 19:09:17 -0700201 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
202 boolean revoke)
Charles Chan68aa62d2015-11-09 16:37:23 -0800203 throws DeviceConfigNotFoundException {
204 MacAddress deviceMac;
205 deviceMac = config.getDeviceMac(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800206
Charles Chan7ffd81f2017-02-08 15:52:08 -0800207 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
208 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
209 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
210 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sanghob35a6192015-04-01 13:05:26 -0700211
Charles Chan7ffd81f2017-02-08 15:52:08 -0800212 // Create route selector
213 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
214
215 // Create route treatment
Charles Chan1cdecff2016-10-27 14:19:48 -0700216 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho1e575652015-05-14 00:39:53 -0700217 tbuilder.deferred()
218 .setEthDst(hostMac)
Charles Chan0b4e6182015-11-03 10:42:14 -0800219 .setEthSrc(deviceMac)
sanghob35a6192015-04-01 13:05:26 -0700220 .setOutput(outPort);
Saurav Das4ce45962015-11-24 23:21:05 -0800221
Charles Chan7ffd81f2017-02-08 15:52:08 -0800222 // Create route meta
223 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan59cc16d2017-02-02 16:20:42 -0800224
Charles Chan7ffd81f2017-02-08 15:52:08 -0800225 // Adjust the meta according to VLAN configuration
226 if (taggedVlans.contains(hostVlanId)) {
227 tbuilder.setVlanId(hostVlanId);
228 } else if (hostVlanId.equals(VlanId.NONE)) {
229 if (untaggedVlan != null) {
230 mbuilder.matchVlanId(untaggedVlan);
231 } else if (nativeVlan != null) {
232 mbuilder.matchVlanId(nativeVlan);
233 } else {
Charles Chan65238242017-06-22 18:03:14 -0700234 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
235 hostMac, hostVlanId, connectPoint);
236 return null;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800237 }
238 } else {
Saurav Das961beb22017-03-29 19:09:17 -0700239 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
240 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das59232cf2016-04-27 18:35:50 -0700241 return null;
242 }
Saurav Das961beb22017-03-29 19:09:17 -0700243 // if the objective is to revoke an existing rule, and for some reason
244 // the next-objective does not exist, then a new one should not be created
Charles Chan7ffd81f2017-02-08 15:52:08 -0800245 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das961beb22017-03-29 19:09:17 -0700246 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800247 if (portNextObjId == -1) {
248 // Warning log will come from getPortNextObjective method
249 return null;
250 }
251
Charles Chan68aa62d2015-11-09 16:37:23 -0800252 return DefaultForwardingObjective.builder()
Charles Chan7ffd81f2017-02-08 15:52:08 -0800253 .withSelector(sbuilder.build())
Saurav Das4ce45962015-11-24 23:21:05 -0800254 .nextStep(portNextObjId)
Charles Chan68aa62d2015-11-09 16:37:23 -0800255 .fromApp(srManager.appId).makePermanent()
Charles Chan1cdecff2016-10-27 14:19:48 -0700256 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan5270ed02016-01-30 23:22:37 -0800257 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700258 }
259
260 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700261 * Populates IP flow rules for all the given prefixes reachable from the
262 * destination switch(es).
sanghob35a6192015-04-01 13:05:26 -0700263 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700264 * @param targetSw switch where rules are to be programmed
265 * @param subnets subnets/prefixes being added
266 * @param destSw1 destination switch where the prefixes are reachable
267 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
268 * Should be null if there is no paired destination switch (by config)
269 * or if the given prefixes are reachable only via destSw1
270 * @param nextHops a map containing a set of next-hops for each destination switch.
271 * If destSw2 is not null, then this map must contain an
272 * entry for destSw2 with its next-hops from the targetSw
273 * (although the next-hop set may be empty in certain scenarios).
274 * If destSw2 is null, there should not be an entry in this
275 * map for destSw2.
sanghob35a6192015-04-01 13:05:26 -0700276 * @return true if all rules are set successfully, false otherwise
277 */
Charles Chan65238242017-06-22 18:03:14 -0700278 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das7bcbe702017-06-13 15:35:54 -0700279 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700280 for (IpPrefix subnet : subnets) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700281 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sanghob35a6192015-04-01 13:05:26 -0700282 return false;
283 }
284 }
Charles Chan93e71ba2016-04-29 14:38:22 -0700285 return true;
286 }
sanghob35a6192015-04-01 13:05:26 -0700287
Charles Chan93e71ba2016-04-29 14:38:22 -0700288 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700289 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chan93e71ba2016-04-29 14:38:22 -0700290 *
291 * @param subnets subnet being removed
292 * @return true if all rules are removed successfully, false otherwise
293 */
Charles Chan65238242017-06-22 18:03:14 -0700294 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700295 for (IpPrefix subnet : subnets) {
296 if (!revokeIpRuleForRouter(subnet)) {
297 return false;
298 }
299 }
sanghob35a6192015-04-01 13:05:26 -0700300 return true;
301 }
302
303 /**
Saurav Das25190812016-05-27 13:54:07 -0700304 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das7bcbe702017-06-13 15:35:54 -0700305 * is reachable via destination device(s).
sanghob35a6192015-04-01 13:05:26 -0700306 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700307 * @param targetSw target device ID to set the rules
308 * @param ipPrefix the IP prefix
309 * @param destSw1 destination switch where the prefixes are reachable
310 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
311 * Should be null if there is no paired destination switch (by config)
312 * or if the given prefixes are reachable only via destSw1
313 * @param nextHops map of destination switches and their next-hops.
314 * Should only contain destination switches that are
315 * actually meant to be routed to. If destSw2 is null, there
316 * should not be an entry for destSw2 in this map.
sanghob35a6192015-04-01 13:05:26 -0700317 * @return true if all rules are set successfully, false otherwise
318 */
Charles Chan65238242017-06-22 18:03:14 -0700319 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das7bcbe702017-06-13 15:35:54 -0700320 IpPrefix ipPrefix, DeviceId destSw1,
321 DeviceId destSw2,
322 Map<DeviceId, Set<DeviceId>> nextHops) {
323 int segmentId1, segmentId2 = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -0800324 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800325 if (ipPrefix.isIp4()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700326 segmentId1 = config.getIPv4SegmentId(destSw1);
327 if (destSw2 != null) {
328 segmentId2 = config.getIPv4SegmentId(destSw2);
329 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800330 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700331 segmentId1 = config.getIPv6SegmentId(destSw1);
332 if (destSw2 != null) {
333 segmentId2 = config.getIPv6SegmentId(destSw2);
334 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800335 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800336 } catch (DeviceConfigNotFoundException e) {
337 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
338 return false;
339 }
sanghob35a6192015-04-01 13:05:26 -0700340
Pier Ventree0ae7a32016-11-23 09:57:42 -0800341 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan68aa62d2015-11-09 16:37:23 -0800342 TrafficSelector selector = sbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700343
Charles Chan68aa62d2015-11-09 16:37:23 -0800344 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700345 DestinationSet ds;
Charles Chan68aa62d2015-11-09 16:37:23 -0800346 TrafficTreatment treatment;
sanghob35a6192015-04-01 13:05:26 -0700347
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700348 // If the next hop is the same as the final destination, then MPLS label
349 // is not set.
Saurav Das7bcbe702017-06-13 15:35:54 -0700350 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800351 tbuilder.immediate().decNwTtl();
Saurav Das7bcbe702017-06-13 15:35:54 -0700352 ds = new DestinationSet(false, destSw);
Charles Chan68aa62d2015-11-09 16:37:23 -0800353 treatment = tbuilder.build();
sanghob35a6192015-04-01 13:05:26 -0700354 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700355 ds = new DestinationSet(false, segmentId, destSw);
356 treatment = null;
357 }*/
358 if (destSw2 == null) {
359 // single dst - create destination set based on next-hop
360 Set<DeviceId> nhd1 = nextHops.get(destSw1);
361 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
362 tbuilder.immediate().decNwTtl();
363 ds = new DestinationSet(false, destSw1);
364 treatment = tbuilder.build();
365 } else {
366 ds = new DestinationSet(false, segmentId1, destSw1);
367 treatment = null;
368 }
369 } else {
370 // dst pair - IP rules for dst-pairs are always from other edge nodes
371 // the destination set needs to have both destinations, even if there
372 // are no next hops to one of them
373 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chan68aa62d2015-11-09 16:37:23 -0800374 treatment = null;
sanghob35a6192015-04-01 13:05:26 -0700375 }
376
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 // setup metadata to pass to nextObjective - indicate the vlan on egress
378 // if needed by the switch pipeline. Since neighbor sets are always to
379 // other neighboring routers, there is no subnet assigned on those ports.
380 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800381 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das7bcbe702017-06-13 15:35:54 -0700382 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700383 if (grpHandler == null) {
384 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700385 + "not found", targetSw);
Saurav Dasc88d4662017-05-15 15:34:25 -0700386 return false;
387 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800388
Saurav Das7bcbe702017-06-13 15:35:54 -0700389 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
390 metabuilder.build(), true);
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700392 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho834e4b02015-05-01 09:38:25 -0700393 return false;
394 }
395
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700396 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
397 .builder()
398 .fromApp(srManager.appId)
399 .makePermanent()
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 .nextStep(nextId)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700401 .withSelector(selector)
Charles Chan5270ed02016-01-30 23:22:37 -0800402 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700403 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chan68aa62d2015-11-09 16:37:23 -0800404 if (treatment != null) {
405 fwdBuilder.withTreatment(treatment);
406 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700407 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700408 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chand2990362016-04-18 13:44:03 -0700409 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das25190812016-05-27 13:54:07 -0700410 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700411 ipPrefix, targetSw),
Charles Chand2990362016-04-18 13:44:03 -0700412 (objective, error) ->
Saurav Das25190812016-05-27 13:54:07 -0700413 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700414 ipPrefix, error, targetSw));
415 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sangho20eff1d2015-04-13 15:15:58 -0700416 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700417
418 return true;
419 }
420
sanghob35a6192015-04-01 13:05:26 -0700421 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700422 * Revokes IP flow rules for the router IP address.
423 *
424 * @param ipPrefix the IP address of the destination router
425 * @return true if all rules are removed successfully, false otherwise
426 */
Charles Chan65238242017-06-22 18:03:14 -0700427 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre968da122016-12-09 17:26:04 -0800428 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700429 TrafficSelector selector = sbuilder.build();
430 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
431
432 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
433 .builder()
434 .fromApp(srManager.appId)
435 .makePermanent()
436 .withSelector(selector)
437 .withTreatment(dummyTreatment)
438 .withPriority(getPriorityFromPrefix(ipPrefix))
439 .withFlag(ForwardingObjective.Flag.SPECIFIC);
440
441 ObjectiveContext context = new DefaultObjectiveContext(
442 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
443 (objective, error) ->
444 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
445
Charles Chan65238242017-06-22 18:03:14 -0700446 srManager.deviceService.getAvailableDevices().forEach(device ->
447 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context))
448 );
Charles Chan93e71ba2016-04-29 14:38:22 -0700449
450 return true;
451 }
452
453 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700454 * Deals with !MPLS Bos use case.
455 *
456 * @param targetSwId the target sw
457 * @param destSwId the destination sw
458 * @param nextHops the set of next hops
459 * @param segmentId the segmentId to match
460 * @param routerIp the router ip
461 * @return a collection of fwdobjective
462 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700463 private Collection<ForwardingObjective> handleMpls(
464 DeviceId targetSwId,
465 DeviceId destSwId,
466 Set<DeviceId> nextHops,
467 int segmentId,
468 IpAddress routerIp,
469 boolean isMplsBos) {
Pier Ventre917127a2016-10-31 16:49:19 -0700470
471 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
472 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800473 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre917127a2016-10-31 16:49:19 -0700474 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
475 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
476 sbuilder.matchMplsBos(isMplsBos);
477 TrafficSelector selector = sbuilder.build();
478
479 // setup metadata to pass to nextObjective - indicate the vlan on egress
480 // if needed by the switch pipeline. Since mpls next-hops are always to
481 // other neighboring routers, there is no subnet assigned on those ports.
482 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan59cc16d2017-02-02 16:20:42 -0800483 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre917127a2016-10-31 16:49:19 -0700484
485 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
486 // If the next hop is the destination router for the segment, do pop
487 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das7bcbe702017-06-13 15:35:54 -0700488 + "label {} in switch {} with pop to next-hops {}",
489 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700490 // Not-bos pop case (php for the current label). If MPLS-ECMP
491 // has been configured, the application we will request the
492 // installation for an MPLS-ECMP group.
Saurav Das961beb22017-03-29 19:09:17 -0700493 ForwardingObjective.Builder fwdObjNoBosBuilder =
494 getMplsForwardingObjective(targetSwId,
495 nextHops,
496 true,
497 isMplsBos,
498 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700499 routerIp,
500 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700501 // Error case, we cannot handle, exit.
502 if (fwdObjNoBosBuilder == null) {
503 return Collections.emptyList();
504 }
505 fwdObjBuilders.add(fwdObjNoBosBuilder);
506
507 } else {
508 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das7bcbe702017-06-13 15:35:54 -0700509 log.debug("Installing MPLS forwarding objective for "
510 + "label {} in switch {} without pop to next-hops {}",
511 segmentId, targetSwId, nextHops);
Pier Ventre917127a2016-10-31 16:49:19 -0700512 // Not-bos pop case. If MPLS-ECMP has been configured, the
513 // application we will request the installation for an MPLS-ECMP
514 // group.
Saurav Das961beb22017-03-29 19:09:17 -0700515 ForwardingObjective.Builder fwdObjNoBosBuilder =
516 getMplsForwardingObjective(targetSwId,
517 nextHops,
518 false,
519 isMplsBos,
520 metabuilder.build(),
Saurav Dasc88d4662017-05-15 15:34:25 -0700521 routerIp,
522 destSwId);
Pier Ventre917127a2016-10-31 16:49:19 -0700523 // Error case, we cannot handle, exit.
524 if (fwdObjNoBosBuilder == null) {
525 return Collections.emptyList();
526 }
527 fwdObjBuilders.add(fwdObjNoBosBuilder);
528
529 }
530
531 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
532 // We add the final property to the fwdObjs.
533 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
534
535 ((Builder) ((Builder) fwdObjBuilder
536 .fromApp(srManager.appId)
537 .makePermanent())
538 .withSelector(selector)
539 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
540 .withFlag(ForwardingObjective.Flag.SPECIFIC);
541
542 ObjectiveContext context = new DefaultObjectiveContext(
543 (objective) ->
544 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
545 objective.id(), segmentId, targetSwId),
546 (objective, error) ->
547 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
548 objective.id(), segmentId, error, targetSwId));
549
550 ForwardingObjective fob = fwdObjBuilder.add(context);
551 fwdObjs.add(fob);
552
553 }
554
555 return fwdObjs;
556 }
557
558 /**
Saurav Das25190812016-05-27 13:54:07 -0700559 * Populates MPLS flow rules in the target device to point towards the
560 * destination device.
sanghob35a6192015-04-01 13:05:26 -0700561 *
Saurav Das25190812016-05-27 13:54:07 -0700562 * @param targetSwId target device ID of the switch to set the rules
sanghob35a6192015-04-01 13:05:26 -0700563 * @param destSwId destination switch device ID
564 * @param nextHops next hops switch ID list
Pier Ventre917127a2016-10-31 16:49:19 -0700565 * @param routerIp the router ip
sanghob35a6192015-04-01 13:05:26 -0700566 * @return true if all rules are set successfully, false otherwise
567 */
Charles Chan65238242017-06-22 18:03:14 -0700568 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700569 Set<DeviceId> nextHops,
570 IpAddress routerIp) {
Pier Ventre917127a2016-10-31 16:49:19 -0700571
Charles Chan0b4e6182015-11-03 10:42:14 -0800572 int segmentId;
573 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800574 if (routerIp.isIp4()) {
575 segmentId = config.getIPv4SegmentId(destSwId);
576 } else {
577 segmentId = config.getIPv6SegmentId(destSwId);
578 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800579 } catch (DeviceConfigNotFoundException e) {
580 log.warn(e.getMessage() + " Aborting populateMplsRule.");
581 return false;
582 }
sanghob35a6192015-04-01 13:05:26 -0700583
Pier Ventre917127a2016-10-31 16:49:19 -0700584 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan65238242017-06-22 18:03:14 -0700585 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre917127a2016-10-31 16:49:19 -0700586 // Generates the transit rules used by the standard "routing".
587 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
588 if (fwdObjsMpls.isEmpty()) {
589 return false;
sanghob35a6192015-04-01 13:05:26 -0700590 }
Pier Ventre917127a2016-10-31 16:49:19 -0700591 fwdObjs.addAll(fwdObjsMpls);
Pier Luigi3bfe32c2017-01-30 09:47:36 -0800592 // Generates the transit rules used by the MPLS Pwaas. For now it is
Pier Ventre917127a2016-10-31 16:49:19 -0700593 // the only case !BoS supported.
Charles Chan65238242017-06-22 18:03:14 -0700594 /*
595 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, false);
Pier Ventre917127a2016-10-31 16:49:19 -0700596 if (fwdObjsMpls.isEmpty()) {
597 return false;
598 }
Charles Chan65238242017-06-22 18:03:14 -0700599 fwdObjs.addAll(fwdObjsMpls);
600 */
Pier Ventre917127a2016-10-31 16:49:19 -0700601
602 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Das25190812016-05-27 13:54:07 -0700603 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre917127a2016-10-31 16:49:19 -0700604 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
605 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sangho20eff1d2015-04-13 15:15:58 -0700606 rulePopulationCounter.incrementAndGet();
sanghob35a6192015-04-01 13:05:26 -0700607 }
608
609 return true;
610 }
611
Saurav Das8a0732e2015-11-20 15:27:53 -0800612 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das7bcbe702017-06-13 15:35:54 -0700613 DeviceId targetSw,
Saurav Das8a0732e2015-11-20 15:27:53 -0800614 Set<DeviceId> nextHops,
615 boolean phpRequired,
616 boolean isBos,
Pier Ventree0ae7a32016-11-23 09:57:42 -0800617 TrafficSelector meta,
Saurav Dasc88d4662017-05-15 15:34:25 -0700618 IpAddress routerIp,
619 DeviceId destSw) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800620
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700621 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
622 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sanghob35a6192015-04-01 13:05:26 -0700623
624 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
625
626 if (phpRequired) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800627 // php case - pop should always be flow-action
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700628 log.debug("getMplsForwardingObjective: php required");
sangho1e575652015-05-14 00:39:53 -0700629 tbuilder.deferred().copyTtlIn();
sanghob35a6192015-04-01 13:05:26 -0700630 if (isBos) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800631 if (routerIp.isIp4()) {
632 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
633 } else {
634 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
635 }
636 tbuilder.decNwTtl();
sanghob35a6192015-04-01 13:05:26 -0700637 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800638 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
639 .decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700640 }
641 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800642 // swap with self case - SR CONTINUE
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700643 log.debug("getMplsForwardingObjective: php not required");
sangho1e575652015-05-14 00:39:53 -0700644 tbuilder.deferred().decMplsTtl();
sanghob35a6192015-04-01 13:05:26 -0700645 }
646
Saurav Das8a0732e2015-11-20 15:27:53 -0800647 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre917127a2016-10-31 16:49:19 -0700648 // if MPLS-ECMP == True we will build a standard NeighborSet.
649 // Otherwise a RandomNeighborSet.
Saurav Das7bcbe702017-06-13 15:35:54 -0700650 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700651 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700652 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700653 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700654 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre917127a2016-10-31 16:49:19 -0700655 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700656
Saurav Dasb28d5dd2017-03-24 19:03:58 -0700657 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700658 targetSw, ns);
659 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
660 if (gh == null) {
661 log.warn("getNextObjectiveId query - groupHandler for device {} "
662 + "not found", targetSw);
663 return null;
664 }
Pier Ventre917127a2016-10-31 16:49:19 -0700665 // If BoS == True, all forwarding is via L3 ECMP group.
666 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
667 // MPLS-Interface group. This depends on the configuration of the option
668 // MPLS-ECMP.
669 // The metadata informs the driver that the next-Objective will be used
670 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das7bcbe702017-06-13 15:35:54 -0700671 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
672 dstNextHops.put(destSw, nextHops);
673 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das8a0732e2015-11-20 15:27:53 -0800674 if (nextId <= 0) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700675 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das8a0732e2015-11-20 15:27:53 -0800676 return null;
Saurav Das25190812016-05-27 13:54:07 -0700677 } else {
678 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700679 nextId, targetSw, ns);
sanghob35a6192015-04-01 13:05:26 -0700680 }
681
Saurav Das8a0732e2015-11-20 15:27:53 -0800682 fwdBuilder.nextStep(nextId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700683 return fwdBuilder;
sanghob35a6192015-04-01 13:05:26 -0700684 }
685
686 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700687 * Creates a filtering objective to permit all untagged packets with a
Saurav Das0e99e2b2015-10-28 12:39:42 -0700688 * dstMac corresponding to the router's MAC address. For those pipelines
689 * that need to internally assign vlans to untagged packets, this method
690 * provides per-subnet vlan-ids as metadata.
Saurav Das837e0bb2015-10-30 17:45:38 -0700691 * <p>
Saurav Das018605f2017-02-18 14:05:44 -0800692 * Note that the vlan assignment and filter programming should only be done by
693 * the master for a switch. This method is typically called at deviceAdd and
694 * programs filters only for the enabled ports of the device. For port-updates,
695 * that enable/disable ports after device add, singlePortFilter methods should
696 * be called.
sanghob35a6192015-04-01 13:05:26 -0700697 *
Saurav Das822c4e22015-10-23 10:51:11 -0700698 * @param deviceId the switch dpid for the router
Saurav Dasd2fded02016-12-02 15:43:47 -0800699 * @return PortFilterInfo information about the processed ports
sanghob35a6192015-04-01 13:05:26 -0700700 */
Charles Chan65238242017-06-22 18:03:14 -0700701 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700702 log.debug("Installing per-port filtering objective for untagged "
703 + "packets in device {}", deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800704
Saurav Das59232cf2016-04-27 18:35:50 -0700705 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hallcbd1b392017-01-18 20:15:44 -0800706 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das59232cf2016-04-27 18:35:50 -0700707 log.warn("Device {} ports not available. Unable to add MacVlan filters",
708 deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -0800709 return null;
Saurav Das59232cf2016-04-27 18:35:50 -0700710 }
Saurav Das018605f2017-02-18 14:05:44 -0800711 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das59232cf2016-04-27 18:35:50 -0700712 for (Port port : devPorts) {
Saurav Das25190812016-05-27 13:54:07 -0700713 if (!port.isEnabled()) {
714 disabledPorts++;
715 continue;
716 }
Charles Chan7e4f8192017-02-26 22:59:35 -0800717 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Das018605f2017-02-18 14:05:44 -0800718 filteredPorts++;
719 } else {
720 errorPorts++;
Saurav Das25190812016-05-27 13:54:07 -0700721 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700722 }
Charles Chan7f9737b2017-06-22 14:27:17 -0700723 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Das018605f2017-02-18 14:05:44 -0800724 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd2fded02016-12-02 15:43:47 -0800725 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Das018605f2017-02-18 14:05:44 -0800726 errorPorts, filteredPorts);
727 }
728
729 /**
Charles Chan7e4f8192017-02-26 22:59:35 -0800730 * Creates or removes filtering objectives for a single port. Should only be
731 * called by the master for a switch.
Saurav Das018605f2017-02-18 14:05:44 -0800732 *
733 * @param deviceId device identifier
734 * @param portnum port identifier for port to be filtered
Charles Chan7e4f8192017-02-26 22:59:35 -0800735 * @param install true to install the filtering objective, false to remove
Saurav Das018605f2017-02-18 14:05:44 -0800736 * @return true if no errors occurred during the build of the filtering objective
737 */
Charles Chan65238242017-06-22 18:03:14 -0700738 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800739 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
740 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
741 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
742 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
743
744 if (taggedVlans.size() != 0) {
745 // Filter for tagged vlans
746 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan7e4f8192017-02-26 22:59:35 -0800747 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800748 return false;
749 }
750 if (nativeVlan != null) {
751 // Filter for native vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800752 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800753 return false;
754 }
755 }
756 } else if (untaggedVlan != null) {
757 // Filter for untagged vlan
Charles Chan7e4f8192017-02-26 22:59:35 -0800758 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800759 return false;
760 }
761 } else {
Saurav Dasceccf242017-08-03 18:30:35 -0700762 // Unconfigured port, use INTERNAL_VLAN
Charles Chan7e4f8192017-02-26 22:59:35 -0800763 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800764 return false;
765 }
766 }
767 return true;
768 }
769
Charles Chan7e4f8192017-02-26 22:59:35 -0800770 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
771 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800772 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Das018605f2017-02-18 14:05:44 -0800773 if (fob == null) {
774 // error encountered during build
775 return false;
776 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700777 log.debug("{} filtering objectives for dev/port:{}/{}",
778 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Das018605f2017-02-18 14:05:44 -0800779 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan7e4f8192017-02-26 22:59:35 -0800780 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Dasc88d4662017-05-15 15:34:25 -0700781 install ? "installed" : "removed"),
Charles Chan7e4f8192017-02-26 22:59:35 -0800782 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Dasc88d4662017-05-15 15:34:25 -0700783 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan7e4f8192017-02-26 22:59:35 -0800784 if (install) {
785 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
786 } else {
787 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan7ffd81f2017-02-08 15:52:08 -0800788 }
Charles Chan7ffd81f2017-02-08 15:52:08 -0800789 return true;
Saurav Das018605f2017-02-18 14:05:44 -0800790 }
791
Charles Chan7ffd81f2017-02-08 15:52:08 -0800792 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
793 boolean pushVlan, VlanId vlanId) {
Saurav Das018605f2017-02-18 14:05:44 -0800794 MacAddress deviceMac;
795 try {
796 deviceMac = config.getDeviceMac(deviceId);
797 } catch (DeviceConfigNotFoundException e) {
798 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
799 return null;
800 }
Saurav Das018605f2017-02-18 14:05:44 -0800801 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
802 fob.withKey(Criteria.matchInPort(portnum))
803 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Das018605f2017-02-18 14:05:44 -0800804 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan7ffd81f2017-02-08 15:52:08 -0800805
806 if (pushVlan) {
807 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
808 TrafficTreatment tt = DefaultTrafficTreatment.builder()
809 .pushVlan().setVlanId(vlanId).build();
810 fob.withMeta(tt);
811 } else {
812 fob.addCondition(Criteria.matchVlanId(vlanId));
813 }
814
Saurav Das018605f2017-02-18 14:05:44 -0800815 fob.permit().fromApp(srManager.appId);
816 return fob;
sanghob35a6192015-04-01 13:05:26 -0700817 }
818
819 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700820 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Das837e0bb2015-10-30 17:45:38 -0700821 * router's port IP addresses, to the controller. Note that the input
Saurav Das822c4e22015-10-23 10:51:11 -0700822 * port should not be matched on, as these packets can come from any input.
Saurav Das837e0bb2015-10-30 17:45:38 -0700823 * Furthermore, these are applied only by the master instance.
sanghob35a6192015-04-01 13:05:26 -0700824 *
Saurav Das822c4e22015-10-23 10:51:11 -0700825 * @param deviceId the switch dpid for the router
sanghob35a6192015-04-01 13:05:26 -0700826 */
Charles Chan65238242017-06-22 18:03:14 -0700827 void populateIpPunts(DeviceId deviceId) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700828 Ip4Address routerIpv4, pairRouterIpv4 = null;
829 Ip6Address routerIpv6, pairRouterIpv6 = null;
Charles Chan0b4e6182015-11-03 10:42:14 -0800830 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800831 routerIpv4 = config.getRouterIpv4(deviceId);
832 routerIpv6 = config.getRouterIpv6(deviceId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700833 if (config.isPairedEdge(deviceId)) {
834 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
835 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
836 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800837 } catch (DeviceConfigNotFoundException e) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800838 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800839 return;
840 }
841
Saurav Das837e0bb2015-10-30 17:45:38 -0700842 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
843 log.debug("Not installing port-IP punts - not the master for dev:{} ",
844 deviceId);
845 return;
846 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800847 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
848 allIps.add(routerIpv4);
849 if (routerIpv6 != null) {
850 allIps.add(routerIpv6);
851 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700852 if (pairRouterIpv4 != null) {
853 allIps.add(pairRouterIpv4);
854 }
855 if (pairRouterIpv6 != null) {
856 allIps.add(pairRouterIpv6);
857 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800858 for (IpAddress ipaddr : allIps) {
859 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipaddr);
Charles Chan2df0e8a2017-01-09 11:45:08 -0800860 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
861
862 srManager.packetService.requestPackets(sbuilder.build(),
863 PacketPriority.CONTROL, srManager.appId, optDeviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700864 }
sanghob35a6192015-04-01 13:05:26 -0700865 }
866
Charles Chan68aa62d2015-11-09 16:37:23 -0800867 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800868 * Method to build IPv4 or IPv6 selector.
869 *
870 * @param addressToMatch the address to match
871 */
872 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
873 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
874 }
875
876 /**
877 * Method to build IPv4 or IPv6 selector.
878 *
879 * @param prefixToMatch the prefix to match
880 */
881 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
882 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre917127a2016-10-31 16:49:19 -0700883 // If the prefix is IPv4
Pier Ventree0ae7a32016-11-23 09:57:42 -0800884 if (prefixToMatch.isIp4()) {
885 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
886 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
887 return selectorBuilder;
888 }
Pier Ventre917127a2016-10-31 16:49:19 -0700889 // If the prefix is IPv6
Pier Ventree0ae7a32016-11-23 09:57:42 -0800890 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
891 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
892 return selectorBuilder;
893 }
894
895 /**
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800896 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
897 * Furthermore, these are applied only by the master instance. Deferred actions
898 * are not cleared such that packets can be flooded in the cross connect use case
899 *
900 * @param deviceId the switch dpid for the router
901 */
Charles Chan65238242017-06-22 18:03:14 -0700902 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre917127a2016-10-31 16:49:19 -0700903 // We are not the master just skip.
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800904 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
905 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
906 deviceId);
907 return;
908 }
909
Charles Chan65238242017-06-22 18:03:14 -0700910 ForwardingObjective fwdObj;
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800911 // We punt all ARP packets towards the controller.
Charles Chan65238242017-06-22 18:03:14 -0700912 fwdObj = arpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800913 .add(new ObjectiveContext() {
914 @Override
915 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700916 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800917 deviceId, error);
918 }
919 });
Charles Chan65238242017-06-22 18:03:14 -0700920 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800921
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800922 // We punt all NDP packets towards the controller.
Charles Chan65238242017-06-22 18:03:14 -0700923 fwdObj = ndpFwdObjective(null, true, PacketPriority.CONTROL.priorityValue())
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800924 .add(new ObjectiveContext() {
925 @Override
926 public void onError(Objective objective, ObjectiveError error) {
Charles Chan65238242017-06-22 18:03:14 -0700927 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800928 deviceId, error);
929 }
930 });
Charles Chan65238242017-06-22 18:03:14 -0700931 srManager.flowObjectiveService.forward(deviceId, fwdObj);
932
933 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
934 ForwardingObjective pairFwdObj;
935 // Do not punt ARP packets from pair port
936 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
937 .add(new ObjectiveContext() {
938 @Override
939 public void onError(Objective objective, ObjectiveError error) {
940 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
941 deviceId, error);
942 }
943 });
944 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
945
946 // Do not punt NDP packets from pair port
947 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
948 .add(new ObjectiveContext() {
949 @Override
950 public void onError(Objective objective, ObjectiveError error) {
951 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
952 deviceId, error);
953 }
954 });
955 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
956
957 // Do not forward DAD packets from pair port
958 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
959 .add(new ObjectiveContext() {
960 @Override
961 public void onError(Objective objective, ObjectiveError error) {
962 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
963 deviceId, error);
964 }
965 });
966 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
967 });
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800968 }
969
Charles Chan65238242017-06-22 18:03:14 -0700970 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
971 TrafficTreatment treatment, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800972 return DefaultForwardingObjective.builder()
Charles Chan65238242017-06-22 18:03:14 -0700973 .withPriority(priority)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800974 .withSelector(selector)
975 .fromApp(srManager.appId)
976 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan65238242017-06-22 18:03:14 -0700977 .withTreatment(treatment)
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800978 .makePermanent();
979 }
980
Charles Chan65238242017-06-22 18:03:14 -0700981 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800982 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
983 sBuilder.matchEthType(TYPE_ARP);
Charles Chan65238242017-06-22 18:03:14 -0700984 if (port != null) {
985 sBuilder.matchInPort(port);
986 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800987
Charles Chan65238242017-06-22 18:03:14 -0700988 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
989 if (punt) {
990 tBuilder.punt();
991 }
992 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800993 }
994
Charles Chan65238242017-06-22 18:03:14 -0700995 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800996 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
997 sBuilder.matchEthType(TYPE_IPV6)
998 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan65238242017-06-22 18:03:14 -0700999 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1000 if (port != null) {
1001 sBuilder.matchInPort(port);
1002 }
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001003
Charles Chan65238242017-06-22 18:03:14 -07001004 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1005 if (punt) {
1006 tBuilder.punt();
1007 }
1008 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1009 }
1010
1011 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1012 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1013 sBuilder.matchEthType(TYPE_IPV6)
Charles Chanc7e36aa2017-08-07 12:39:03 -07001014 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1015 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1016 // .matchIPProtocol(PROTOCOL_ICMP6)
1017 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan65238242017-06-22 18:03:14 -07001018 if (port != null) {
1019 sBuilder.matchInPort(port);
1020 }
1021
1022 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1023 tBuilder.wipeDeferred();
1024 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigi9e5c5ca2017-01-12 18:14:58 -08001025 }
1026
1027 /**
Charles Chan68aa62d2015-11-09 16:37:23 -08001028 * Populates a forwarding objective to send packets that miss other high
1029 * priority Bridging Table entries to a group that contains all ports of
1030 * its subnet.
1031 *
Charles Chan68aa62d2015-11-09 16:37:23 -08001032 * @param deviceId switch ID to set the rules
1033 */
Charles Chan65238242017-06-22 18:03:14 -07001034 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001035 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001036 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1037
1038 if (nextId < 0) {
1039 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1040 + "to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
Saurav Das4ce45962015-11-24 23:21:05 -08001041 return;
1042 }
1043
Pier Ventre917127a2016-10-31 16:49:19 -07001044 // Driver should treat objective with MacAddress.NONE as the
1045 // subnet broadcast rule
Charles Chan68aa62d2015-11-09 16:37:23 -08001046 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1047 sbuilder.matchVlanId(vlanId);
1048 sbuilder.matchEthDst(MacAddress.NONE);
1049
1050 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1051 fob.withFlag(Flag.SPECIFIC)
1052 .withSelector(sbuilder.build())
1053 .nextStep(nextId)
Charles Chan5270ed02016-01-30 23:22:37 -08001054 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
Charles Chan68aa62d2015-11-09 16:37:23 -08001055 .fromApp(srManager.appId)
1056 .makePermanent();
Charles Chand2990362016-04-18 13:44:03 -07001057 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan59cc16d2017-02-02 16:20:42 -08001058 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
Charles Chand2990362016-04-18 13:44:03 -07001059 (objective, error) ->
Charles Chan59cc16d2017-02-02 16:20:42 -08001060 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
Charles Chand2990362016-04-18 13:44:03 -07001061 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
Charles Chan68aa62d2015-11-09 16:37:23 -08001062 });
1063 }
1064
Charles Chan5270ed02016-01-30 23:22:37 -08001065 private int getPriorityFromPrefix(IpPrefix prefix) {
1066 return (prefix.isIp4()) ?
1067 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1068 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001069 }
sanghob35a6192015-04-01 13:05:26 -07001070}