blob: c152b99b9cdc4fdf39ac70f1b83d77c4c5d57a61 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-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 Ventre229fd0b2016-10-31 16:49:19 -070018import com.google.common.collect.Lists;
Charles Chan051490d2018-01-11 11:48:18 -080019import com.google.common.collect.Sets;
Saurav Das4c35fc42015-11-20 15:27:53 -080020import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070021import org.onlab.packet.Ethernet;
Charles Chanef8d12e2017-12-05 21:07:38 -080022import org.onlab.packet.IPv6;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080024import org.onlab.packet.Ip6Address;
25import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070026import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070029import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080030import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070031import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080032import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070033import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080034import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080035import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080036import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080037import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
38import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070039import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070040import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070041import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070042import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070044import org.onosproject.net.flow.DefaultTrafficSelector;
45import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070048import org.onosproject.net.flow.criteria.Criteria;
49import org.onosproject.net.flowobjective.DefaultFilteringObjective;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.FilteringObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070054import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
58import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070059import java.util.Collection;
60import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070061import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070062import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070063import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070064import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080065import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070066import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070067import java.util.concurrent.atomic.AtomicLong;
Charles Chanf17f66b2018-02-26 21:33:25 -080068import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -070069
70import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080071import static org.onlab.packet.Ethernet.TYPE_ARP;
72import static org.onlab.packet.Ethernet.TYPE_IPV6;
Charles Chan051490d2018-01-11 11:48:18 -080073import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
Pier Luigib9632ba2017-01-12 18:14:58 -080074import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Charles Chan051490d2018-01-11 11:48:18 -080075import static org.onlab.packet.ICMP6.ROUTER_ADVERTISEMENT;
76import static org.onlab.packet.ICMP6.ROUTER_SOLICITATION;
Pier Luigib9632ba2017-01-12 18:14:58 -080077import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080078import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070079
Charles Chanb7f75ac2016-01-11 18:28:54 -080080/**
81 * Populator of segment routing flow rules.
82 */
sangho80f11cb2015-04-01 13:05:26 -070083public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080084 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
85
86 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070087
sanghofb7c7292015-04-13 15:15:58 -070088 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070089 private SegmentRoutingManager srManager;
90 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070091
sangho80f11cb2015-04-01 13:05:26 -070092 /**
93 * Creates a RoutingRulePopulator object.
94 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070095 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070096 */
Charles Chan3ed34d82017-06-22 18:03:14 -070097 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -070098 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070099 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -0700100 this.rulePopulationCounter = new AtomicLong(0);
101 }
102
103 /**
104 * Resets the population counter.
105 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700106 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700107 rulePopulationCounter.set(0);
108 }
109
110 /**
111 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700112 *
113 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700114 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700115 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700116 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700117 }
118
119 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700120 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700121 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700122 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700123 * @param deviceId device ID of the device that next hop attaches to
124 * @param prefix IP prefix of the route
125 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800126 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700127 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700128 */
Charles Chan910be6a2017-08-23 14:46:43 -0700129 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800130 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700131 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700132 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800133 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800134 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700135 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
136 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800137 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700138 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800139 return;
140 }
Saurav Das07c74602016-04-27 18:35:50 -0700141 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700142 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700143 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700144 return;
145 }
Charles Chan910be6a2017-08-23 14:46:43 -0700146
147 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700148 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700149 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
150 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700151 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700152 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700153 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700154 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800155 rulePopulationCounter.incrementAndGet();
156 }
157
Charles Chanb7f75ac2016-01-11 18:28:54 -0800158 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700159 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800160 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700161 * @param deviceId device ID of the device that next hop attaches to
162 * @param prefix IP prefix of the route
163 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800164 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700165 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800166 */
Charles Chan910be6a2017-08-23 14:46:43 -0700167 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800168 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700169 log.debug("Revoke IP table entry for route {} at {}:{}",
170 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800171 ForwardingObjective.Builder fwdBuilder;
172 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700173 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
174 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800175 } catch (DeviceConfigNotFoundException e) {
176 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
177 return;
178 }
Charles Chanea702b12016-11-30 11:55:05 -0800179 if (fwdBuilder == null) {
180 log.warn("Aborting host routing table entries due "
181 + "to error for dev:{} route:{}", deviceId, prefix);
182 return;
183 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700184 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700185 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700186 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700187 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700188 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800189 }
190
Charles Chanddac7fd2016-10-27 14:19:48 -0700191 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800192 * Returns a forwarding objective builder for routing rules.
193 * <p>
194 * The forwarding objective routes packets destined to a given prefix to
195 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700196 *
197 * @param deviceId device ID
198 * @param prefix prefix that need to be routed
199 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800200 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700201 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700202 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700203 * @return forwarding objective builder
204 * @throws DeviceConfigNotFoundException if given device is not configured
205 */
Charles Chan18fa4252017-02-08 16:10:40 -0800206 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700207 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700208 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
209 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800210 throws DeviceConfigNotFoundException {
211 MacAddress deviceMac;
212 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800213
Charles Chan90772a72017-02-08 15:52:08 -0800214 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
215 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
216 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
217 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700218
Charles Chan90772a72017-02-08 15:52:08 -0800219 // Create route selector
220 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
221
222 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700223 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700224 tbuilder.deferred()
225 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800226 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700227 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800228
Charles Chan90772a72017-02-08 15:52:08 -0800229 // Create route meta
230 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800231
Charles Chan90772a72017-02-08 15:52:08 -0800232 // Adjust the meta according to VLAN configuration
233 if (taggedVlans.contains(hostVlanId)) {
234 tbuilder.setVlanId(hostVlanId);
235 } else if (hostVlanId.equals(VlanId.NONE)) {
236 if (untaggedVlan != null) {
237 mbuilder.matchVlanId(untaggedVlan);
238 } else if (nativeVlan != null) {
239 mbuilder.matchVlanId(nativeVlan);
240 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700241 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
242 hostMac, hostVlanId, connectPoint);
243 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800244 }
245 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700246 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
247 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700248 return null;
249 }
Saurav Das2cb38292017-03-29 19:09:17 -0700250 // if the objective is to revoke an existing rule, and for some reason
251 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800252 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700253 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800254 if (portNextObjId == -1) {
255 // Warning log will come from getPortNextObjective method
256 return null;
257 }
258
Charles Chanf4586112015-11-09 16:37:23 -0800259 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800260 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800261 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800262 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700263 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800264 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700265 }
266
267 /**
Saurav Das261c3002017-06-13 15:35:54 -0700268 * Populates IP flow rules for all the given prefixes reachable from the
269 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700270 *
Saurav Das261c3002017-06-13 15:35:54 -0700271 * @param targetSw switch where rules are to be programmed
272 * @param subnets subnets/prefixes being added
273 * @param destSw1 destination switch where the prefixes are reachable
274 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
275 * Should be null if there is no paired destination switch (by config)
276 * or if the given prefixes are reachable only via destSw1
277 * @param nextHops a map containing a set of next-hops for each destination switch.
278 * If destSw2 is not null, then this map must contain an
279 * entry for destSw2 with its next-hops from the targetSw
280 * (although the next-hop set may be empty in certain scenarios).
281 * If destSw2 is null, there should not be an entry in this
282 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700283 * @return true if all rules are set successfully, false otherwise
284 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700285 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700286 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700287 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700288 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700289 return false;
290 }
291 }
Charles Chanc22cef32016-04-29 14:38:22 -0700292 return true;
293 }
sangho80f11cb2015-04-01 13:05:26 -0700294
Charles Chanc22cef32016-04-29 14:38:22 -0700295 /**
Saurav Das261c3002017-06-13 15:35:54 -0700296 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chanc22cef32016-04-29 14:38:22 -0700297 *
298 * @param subnets subnet being removed
299 * @return true if all rules are removed successfully, false otherwise
300 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700301 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700302 for (IpPrefix subnet : subnets) {
303 if (!revokeIpRuleForRouter(subnet)) {
304 return false;
305 }
306 }
sangho80f11cb2015-04-01 13:05:26 -0700307 return true;
308 }
309
310 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700311 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700312 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700313 *
Saurav Das261c3002017-06-13 15:35:54 -0700314 * @param targetSw target device ID to set the rules
315 * @param ipPrefix the IP prefix
316 * @param destSw1 destination switch where the prefixes are reachable
317 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
318 * Should be null if there is no paired destination switch (by config)
319 * or if the given prefixes are reachable only via destSw1
320 * @param nextHops map of destination switches and their next-hops.
321 * Should only contain destination switches that are
322 * actually meant to be routed to. If destSw2 is null, there
323 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700324 * @return true if all rules are set successfully, false otherwise
325 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700326 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700327 IpPrefix ipPrefix, DeviceId destSw1,
328 DeviceId destSw2,
329 Map<DeviceId, Set<DeviceId>> nextHops) {
330 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800331 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800332 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700333 segmentId1 = config.getIPv4SegmentId(destSw1);
334 if (destSw2 != null) {
335 segmentId2 = config.getIPv4SegmentId(destSw2);
336 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800337 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700338 segmentId1 = config.getIPv6SegmentId(destSw1);
339 if (destSw2 != null) {
340 segmentId2 = config.getIPv6SegmentId(destSw2);
341 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800342 }
Charles Chan319d1a22015-11-03 10:42:14 -0800343 } catch (DeviceConfigNotFoundException e) {
344 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
345 return false;
346 }
sangho80f11cb2015-04-01 13:05:26 -0700347
Pier Ventreadb4ae62016-11-23 09:57:42 -0800348 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800349 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700350
Charles Chanf4586112015-11-09 16:37:23 -0800351 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700352 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800353 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700354
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700355 // If the next hop is the same as the final destination, then MPLS label
356 // is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700357 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800358 tbuilder.immediate().decNwTtl();
Saurav Das261c3002017-06-13 15:35:54 -0700359 ds = new DestinationSet(false, destSw);
Charles Chanf4586112015-11-09 16:37:23 -0800360 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700361 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700362 ds = new DestinationSet(false, segmentId, destSw);
363 treatment = null;
364 }*/
365 if (destSw2 == null) {
366 // single dst - create destination set based on next-hop
367 Set<DeviceId> nhd1 = nextHops.get(destSw1);
368 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
369 tbuilder.immediate().decNwTtl();
370 ds = new DestinationSet(false, destSw1);
371 treatment = tbuilder.build();
372 } else {
373 ds = new DestinationSet(false, segmentId1, destSw1);
374 treatment = null;
375 }
376 } else {
377 // dst pair - IP rules for dst-pairs are always from other edge nodes
378 // the destination set needs to have both destinations, even if there
379 // are no next hops to one of them
380 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800381 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700382 }
383
Saurav Das4c35fc42015-11-20 15:27:53 -0800384 // setup metadata to pass to nextObjective - indicate the vlan on egress
385 // if needed by the switch pipeline. Since neighbor sets are always to
386 // other neighboring routers, there is no subnet assigned on those ports.
387 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800388 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700389 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700390 if (grpHandler == null) {
391 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700392 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700393 return false;
394 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800395
Saurav Das261c3002017-06-13 15:35:54 -0700396 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
397 metabuilder.build(), true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800398 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700399 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700400 return false;
401 }
402
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700403 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
404 .builder()
405 .fromApp(srManager.appId)
406 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800407 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700408 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800409 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700410 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800411 if (treatment != null) {
412 fwdBuilder.withTreatment(treatment);
413 }
Saurav Das62ae6792017-05-15 15:34:25 -0700414 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700415 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700416 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700417 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700418 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700419 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700420 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700421 ipPrefix, error, targetSw));
422 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700423 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700424
425 return true;
426 }
427
sangho80f11cb2015-04-01 13:05:26 -0700428 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700429 * Revokes IP flow rules for the router IP address.
430 *
431 * @param ipPrefix the IP address of the destination router
432 * @return true if all rules are removed successfully, false otherwise
433 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700434 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800435 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700436 TrafficSelector selector = sbuilder.build();
437 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
438
439 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
440 .builder()
441 .fromApp(srManager.appId)
442 .makePermanent()
443 .withSelector(selector)
444 .withTreatment(dummyTreatment)
445 .withPriority(getPriorityFromPrefix(ipPrefix))
446 .withFlag(ForwardingObjective.Flag.SPECIFIC);
447
Charles Chana8d91532018-01-30 13:31:30 -0800448 srManager.deviceService.getAvailableDevices().forEach(device -> {
449 if (srManager.mastershipService.isLocalMaster(device.id())) {
450 ObjectiveContext context = new DefaultObjectiveContext(
451 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
452 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
453 ipPrefix, device.id(), error));
454 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
455 } else {
456 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
457 }
458 });
Charles Chanc22cef32016-04-29 14:38:22 -0700459
460 return true;
461 }
462
463 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700464 * Deals with !MPLS Bos use case.
465 *
466 * @param targetSwId the target sw
467 * @param destSwId the destination sw
468 * @param nextHops the set of next hops
469 * @param segmentId the segmentId to match
470 * @param routerIp the router ip
471 * @return a collection of fwdobjective
472 */
Saurav Das261c3002017-06-13 15:35:54 -0700473 private Collection<ForwardingObjective> handleMpls(
474 DeviceId targetSwId,
475 DeviceId destSwId,
476 Set<DeviceId> nextHops,
477 int segmentId,
478 IpAddress routerIp,
479 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700480
481 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
482 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800483 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700484 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
485 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
486 sbuilder.matchMplsBos(isMplsBos);
487 TrafficSelector selector = sbuilder.build();
488
489 // setup metadata to pass to nextObjective - indicate the vlan on egress
490 // if needed by the switch pipeline. Since mpls next-hops are always to
491 // other neighboring routers, there is no subnet assigned on those ports.
492 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800493 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700494
495 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
496 // If the next hop is the destination router for the segment, do pop
497 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700498 + "label {} in switch {} with pop to next-hops {}",
499 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700500 // Not-bos pop case (php for the current label). If MPLS-ECMP
501 // has been configured, the application we will request the
502 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700503 ForwardingObjective.Builder fwdObjNoBosBuilder =
504 getMplsForwardingObjective(targetSwId,
505 nextHops,
506 true,
507 isMplsBos,
508 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700509 routerIp,
510 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700511 // Error case, we cannot handle, exit.
512 if (fwdObjNoBosBuilder == null) {
513 return Collections.emptyList();
514 }
515 fwdObjBuilders.add(fwdObjNoBosBuilder);
516
517 } else {
518 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700519 log.debug("Installing MPLS forwarding objective for "
520 + "label {} in switch {} without pop to next-hops {}",
521 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700522 // Not-bos pop case. If MPLS-ECMP has been configured, the
523 // application we will request the installation for an MPLS-ECMP
524 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700525 ForwardingObjective.Builder fwdObjNoBosBuilder =
526 getMplsForwardingObjective(targetSwId,
527 nextHops,
528 false,
529 isMplsBos,
530 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700531 routerIp,
532 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700533 // Error case, we cannot handle, exit.
534 if (fwdObjNoBosBuilder == null) {
535 return Collections.emptyList();
536 }
537 fwdObjBuilders.add(fwdObjNoBosBuilder);
538
539 }
540
541 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
542 // We add the final property to the fwdObjs.
543 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
544
545 ((Builder) ((Builder) fwdObjBuilder
546 .fromApp(srManager.appId)
547 .makePermanent())
548 .withSelector(selector)
549 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
550 .withFlag(ForwardingObjective.Flag.SPECIFIC);
551
552 ObjectiveContext context = new DefaultObjectiveContext(
553 (objective) ->
554 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
555 objective.id(), segmentId, targetSwId),
556 (objective, error) ->
557 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
558 objective.id(), segmentId, error, targetSwId));
559
560 ForwardingObjective fob = fwdObjBuilder.add(context);
561 fwdObjs.add(fob);
562
563 }
564
565 return fwdObjs;
566 }
567
568 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700569 * Populates MPLS flow rules in the target device to point towards the
570 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700571 *
Saurav Dase0237a32016-05-27 13:54:07 -0700572 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700573 * @param destSwId destination switch device ID
574 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700575 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700576 * @return true if all rules are set successfully, false otherwise
577 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700578 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700579 Set<DeviceId> nextHops,
580 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700581
Charles Chan319d1a22015-11-03 10:42:14 -0800582 int segmentId;
583 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800584 if (routerIp.isIp4()) {
585 segmentId = config.getIPv4SegmentId(destSwId);
586 } else {
587 segmentId = config.getIPv6SegmentId(destSwId);
588 }
Charles Chan319d1a22015-11-03 10:42:14 -0800589 } catch (DeviceConfigNotFoundException e) {
590 log.warn(e.getMessage() + " Aborting populateMplsRule.");
591 return false;
592 }
sangho80f11cb2015-04-01 13:05:26 -0700593
Pier Ventre229fd0b2016-10-31 16:49:19 -0700594 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan3ed34d82017-06-22 18:03:14 -0700595 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700596 // Generates the transit rules used by the standard "routing".
597 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
598 if (fwdObjsMpls.isEmpty()) {
599 return false;
sangho80f11cb2015-04-01 13:05:26 -0700600 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700601 fwdObjs.addAll(fwdObjsMpls);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700602
603 // Generates the transit rules used by the MPLS Pwaas.
604 int pwSrLabel;
605 try {
606 pwSrLabel = config.getPWRoutingLabel(destSwId);
607 } catch (DeviceConfigNotFoundException e) {
608 log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic.");
609 return false;
610 }
611 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700612 if (fwdObjsMpls.isEmpty()) {
613 return false;
614 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700615 fwdObjs.addAll(fwdObjsMpls);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700616
617 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700618 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700619 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
620 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700621 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700622 }
623
624 return true;
625 }
626
Saurav Das4c35fc42015-11-20 15:27:53 -0800627 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700628 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800629 Set<DeviceId> nextHops,
630 boolean phpRequired,
631 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800632 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700633 IpAddress routerIp,
634 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800635
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700636 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
637 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700638
639 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
640
641 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800642 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700643 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700644 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700645 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800646 if (routerIp.isIp4()) {
647 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
648 } else {
649 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
650 }
651 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700652 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800653 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
654 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700655 }
656 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800657 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700658 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700659 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700660 }
661
Saurav Das4c35fc42015-11-20 15:27:53 -0800662 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700663 // if MPLS-ECMP == True we will build a standard NeighborSet.
664 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700665 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700666 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700667 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700668 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700669 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700670 }
Saurav Das261c3002017-06-13 15:35:54 -0700671
Saurav Das9455d702017-03-24 19:03:58 -0700672 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700673 targetSw, ns);
674 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
675 if (gh == null) {
676 log.warn("getNextObjectiveId query - groupHandler for device {} "
677 + "not found", targetSw);
678 return null;
679 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700680 // If BoS == True, all forwarding is via L3 ECMP group.
681 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
682 // MPLS-Interface group. This depends on the configuration of the option
683 // MPLS-ECMP.
684 // The metadata informs the driver that the next-Objective will be used
685 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700686 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
687 dstNextHops.put(destSw, nextHops);
688 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800689 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700690 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800691 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700692 } else {
693 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700694 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700695 }
696
Saurav Das4c35fc42015-11-20 15:27:53 -0800697 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700698 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700699 }
700
701 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700702 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700703 * dstMac corresponding to the router's MAC address. For those pipelines
704 * that need to internally assign vlans to untagged packets, this method
705 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700706 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800707 * Note that the vlan assignment and filter programming should only be done by
708 * the master for a switch. This method is typically called at deviceAdd and
709 * programs filters only for the enabled ports of the device. For port-updates,
710 * that enable/disable ports after device add, singlePortFilter methods should
711 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700712 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700713 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800714 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700715 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700716 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700717 log.debug("Installing per-port filtering objective for untagged "
718 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800719
Saurav Das07c74602016-04-27 18:35:50 -0700720 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800721 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700722 log.warn("Device {} ports not available. Unable to add MacVlan filters",
723 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800724 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700725 }
Saurav Dasf9332192017-02-18 14:05:44 -0800726 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700727 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700728 if (!port.isEnabled()) {
729 disabledPorts++;
730 continue;
731 }
Charles Chan43be46b2017-02-26 22:59:35 -0800732 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800733 filteredPorts++;
734 } else {
735 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700736 }
Saurav Das7c305372015-10-28 12:39:42 -0700737 }
Charles Chan077314e2017-06-22 14:27:17 -0700738 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800739 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800740 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800741 errorPorts, filteredPorts);
742 }
743
744 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800745 * Creates or removes filtering objectives for a single port. Should only be
746 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800747 *
748 * @param deviceId device identifier
749 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800750 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800751 * @return true if no errors occurred during the build of the filtering objective
752 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700753 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800754 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
755 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
756 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
757 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
758
759 if (taggedVlans.size() != 0) {
760 // Filter for tagged vlans
761 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800762 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800763 return false;
764 }
765 if (nativeVlan != null) {
766 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800767 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800768 return false;
769 }
770 }
771 } else if (untaggedVlan != null) {
772 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800773 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800774 return false;
775 }
776 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700777 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800778 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800779 return false;
780 }
781 }
782 return true;
783 }
784
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700785 /**
786 * Updates filtering objectives for a single port. Should only be called by
787 * the master for a switch
788 * @param deviceId device identifier
789 * @param portNum port identifier for port to be filtered
790 * @param pushVlan true to push vlan, false otherwise
791 * @param vlanId vlan identifier
792 * @param install true to install the filtering objective, false to remove
793 */
794 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
795 boolean pushVlan, VlanId vlanId, boolean install) {
796 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
797 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
798 deviceId, portNum, vlanId);
799 }
800 }
801
Charles Chan43be46b2017-02-26 22:59:35 -0800802 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
803 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800804 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800805 if (fob == null) {
806 // error encountered during build
807 return false;
808 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700809 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700810 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800811 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800812 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700813 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800814 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700815 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800816 if (install) {
817 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
818 } else {
819 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800820 }
Charles Chan90772a72017-02-08 15:52:08 -0800821 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800822 }
823
Charles Chan90772a72017-02-08 15:52:08 -0800824 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
825 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800826 MacAddress deviceMac;
827 try {
828 deviceMac = config.getDeviceMac(deviceId);
829 } catch (DeviceConfigNotFoundException e) {
830 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
831 return null;
832 }
Saurav Dasf9332192017-02-18 14:05:44 -0800833 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
834 fob.withKey(Criteria.matchInPort(portnum))
835 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800836 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800837
Charles Chan17ca2202017-12-19 19:55:57 -0800838 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
839
Charles Chan90772a72017-02-08 15:52:08 -0800840 if (pushVlan) {
841 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -0800842 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -0800843 } else {
844 fob.addCondition(Criteria.matchVlanId(vlanId));
845 }
846
Charles Chan17ca2202017-12-19 19:55:57 -0800847 // NOTE: Some switch hardware share the same filtering flow among different ports.
848 // We use this metadata to let the driver know that there is no more enabled port
849 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -0800850 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -0800851 tBuilder.wipeDeferred();
852 }
853
854 fob.withMeta(tBuilder.build());
855
Saurav Dasf9332192017-02-18 14:05:44 -0800856 fob.permit().fromApp(srManager.appId);
857 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700858 }
859
860 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700861 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700862 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700863 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700864 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700865 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700866 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700867 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700868 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700869 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -0800870 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800871 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800872 routerIpv4 = config.getRouterIpv4(deviceId);
873 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -0800874 routerLinkLocalIpv6 = Ip6Address.valueOf(
875 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
876
Saurav Das261c3002017-06-13 15:35:54 -0700877 if (config.isPairedEdge(deviceId)) {
878 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
879 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
880 }
Charles Chan319d1a22015-11-03 10:42:14 -0800881 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800882 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800883 return;
884 }
885
Saurav Dasc28b3432015-10-30 17:45:38 -0700886 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
887 log.debug("Not installing port-IP punts - not the master for dev:{} ",
888 deviceId);
889 return;
890 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800891 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
892 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -0800893 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800894 if (routerIpv6 != null) {
895 allIps.add(routerIpv6);
896 }
Saurav Das261c3002017-06-13 15:35:54 -0700897 if (pairRouterIpv4 != null) {
898 allIps.add(pairRouterIpv4);
899 }
900 if (pairRouterIpv6 != null) {
901 allIps.add(pairRouterIpv6);
902 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800903 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700904 populateSingleIpPunts(deviceId, ipaddr);
905 }
906 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800907
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700908 /**
909 * Creates a forwarding objective to punt all IP packets, destined to the
910 * specified IP address, which should be router's port IP address.
911 *
912 * @param deviceId the switch dpid for the router
913 * @param ipAddress the IP address of the router's port
914 */
915 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
916 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
917 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
918
919 srManager.packetService.requestPackets(sbuilder.build(),
920 PacketPriority.CONTROL, srManager.appId, optDeviceId);
921 }
922
923 /**
924 * Removes a forwarding objective to punt all IP packets, destined to the
925 * specified IP address, which should be router's port IP address.
926 *
927 * @param deviceId the switch dpid for the router
928 * @param ipAddress the IP address of the router's port
929 */
930 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
931 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
932 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
933
934 try {
935 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
936 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
937 srManager.packetService.cancelPackets(sbuilder.build(),
938 PacketPriority.CONTROL, srManager.appId, optDeviceId);
939 }
940 } catch (DeviceConfigNotFoundException e) {
941 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700942 }
sangho80f11cb2015-04-01 13:05:26 -0700943 }
944
Charles Chanf4586112015-11-09 16:37:23 -0800945 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800946 * Method to build IPv4 or IPv6 selector.
947 *
948 * @param addressToMatch the address to match
949 */
950 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
951 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
952 }
953
954 /**
955 * Method to build IPv4 or IPv6 selector.
956 *
957 * @param prefixToMatch the prefix to match
958 */
959 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
960 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700961 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800962 if (prefixToMatch.isIp4()) {
963 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
964 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
965 return selectorBuilder;
966 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700967 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800968 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
969 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
970 return selectorBuilder;
971 }
972
973 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800974 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
975 * Furthermore, these are applied only by the master instance. Deferred actions
976 * are not cleared such that packets can be flooded in the cross connect use case
977 *
978 * @param deviceId the switch dpid for the router
979 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700980 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700981 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800982 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
983 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
984 deviceId);
985 return;
986 }
987
Charles Chan3ed34d82017-06-22 18:03:14 -0700988 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800989 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -0800990 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -0800991 .add(new ObjectiveContext() {
992 @Override
993 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700994 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800995 deviceId, error);
996 }
997 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700998 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -0800999
Pier Luigib9632ba2017-01-12 18:14:58 -08001000 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001001 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1002 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1003 @Override
1004 public void onError(Objective objective, ObjectiveError error) {
1005 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1006 deviceId, error);
1007 }
1008 });
1009 srManager.flowObjectiveService.forward(deviceId, obj);
1010 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001011
1012 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1013 ForwardingObjective pairFwdObj;
1014 // Do not punt ARP packets from pair port
1015 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1016 .add(new ObjectiveContext() {
1017 @Override
1018 public void onError(Objective objective, ObjectiveError error) {
1019 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1020 deviceId, error);
1021 }
1022 });
1023 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1024
1025 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001026 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1027 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1028 @Override
1029 public void onError(Objective objective, ObjectiveError error) {
1030 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1031 deviceId, error);
1032 }
1033 });
1034 srManager.flowObjectiveService.forward(deviceId, obj);
1035 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001036
1037 // Do not forward DAD packets from pair port
1038 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1039 .add(new ObjectiveContext() {
1040 @Override
1041 public void onError(Objective objective, ObjectiveError error) {
1042 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1043 deviceId, error);
1044 }
1045 });
1046 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1047 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001048 }
1049
Charles Chan3ed34d82017-06-22 18:03:14 -07001050 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1051 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001052 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001053 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001054 .withSelector(selector)
1055 .fromApp(srManager.appId)
1056 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001057 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001058 .makePermanent();
1059 }
1060
Charles Chan3ed34d82017-06-22 18:03:14 -07001061 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001062 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1063 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001064 if (port != null) {
1065 sBuilder.matchInPort(port);
1066 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001067
Charles Chan3ed34d82017-06-22 18:03:14 -07001068 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1069 if (punt) {
1070 tBuilder.punt();
1071 }
1072 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001073 }
1074
Charles Chan051490d2018-01-11 11:48:18 -08001075 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1076 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001077
Charles Chan051490d2018-01-11 11:48:18 -08001078 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1079 .forEach(type -> {
1080 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1081 sBuilder.matchEthType(TYPE_IPV6)
1082 .matchIPProtocol(PROTOCOL_ICMP6)
1083 .matchIcmpv6Type(type);
1084 if (port != null) {
1085 sBuilder.matchInPort(port);
1086 }
1087
1088 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1089 if (punt) {
1090 tBuilder.punt();
1091 }
1092
1093 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1094 });
1095
1096 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001097 }
1098
1099 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1100 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1101 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001102 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1103 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1104 // .matchIPProtocol(PROTOCOL_ICMP6)
1105 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001106 if (port != null) {
1107 sBuilder.matchInPort(port);
1108 }
1109
1110 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1111 tBuilder.wipeDeferred();
1112 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001113 }
1114
1115 /**
Charles Chanf4586112015-11-09 16:37:23 -08001116 * Populates a forwarding objective to send packets that miss other high
1117 * priority Bridging Table entries to a group that contains all ports of
1118 * its subnet.
1119 *
Charles Chanf4586112015-11-09 16:37:23 -08001120 * @param deviceId switch ID to set the rules
1121 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001122 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001123 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001124 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001125 });
1126 }
1127
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001128 /**
1129 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1130 * @param deviceId switch ID to set the rule
1131 * @param vlanId vlan ID to specify the subnet
1132 * @param install true to install the rule, false to revoke the rule
1133 */
1134 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1135 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1136
1137 if (nextId < 0) {
1138 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1139 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1140 return;
1141 }
1142
1143 // Driver should treat objective with MacAddress.NONE as the
1144 // subnet broadcast rule
1145 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1146 sbuilder.matchVlanId(vlanId);
1147 sbuilder.matchEthDst(MacAddress.NONE);
1148
1149 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1150 fob.withFlag(Flag.SPECIFIC)
1151 .withSelector(sbuilder.build())
1152 .nextStep(nextId)
1153 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1154 .fromApp(srManager.appId)
1155 .makePermanent();
1156 ObjectiveContext context = new DefaultObjectiveContext(
1157 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1158 (objective, error) ->
1159 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1160
1161 if (install) {
1162 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1163 } else {
1164 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1165 }
1166 }
1167
Charles Chan82ab1932016-01-30 23:22:37 -08001168 private int getPriorityFromPrefix(IpPrefix prefix) {
1169 return (prefix.isIp4()) ?
1170 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1171 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001172 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001173
1174 /**
1175 * Update Forwarding objective for each host and IP address connected to given port.
1176 * And create corresponding Simple Next objective if it does not exist.
1177 * Applied only when populating Forwarding objective
1178 * @param deviceId switch ID to set the rule
1179 * @param portNumber port number
1180 * @param prefix IP prefix of the route
1181 * @param hostMac MAC address of the next hop
1182 * @param vlanId Vlan ID of the port
1183 * @param popVlan true to pop vlan tag in TrafficTreatment
1184 * @param install true to populate the forwarding objective, false to revoke
1185 */
1186 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1187 VlanId vlanId, boolean popVlan, boolean install) {
1188 ForwardingObjective.Builder fob;
1189 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1190 MacAddress deviceMac;
1191 try {
1192 deviceMac = config.getDeviceMac(deviceId);
1193 } catch (DeviceConfigNotFoundException e) {
1194 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1195 return;
1196 }
1197
1198 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1199 tbuilder.deferred()
1200 .setEthDst(hostMac)
1201 .setEthSrc(deviceMac)
1202 .setOutput(portNumber);
1203
1204 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1205
1206 if (!popVlan) {
1207 tbuilder.setVlanId(vlanId);
1208 } else {
1209 mbuilder.matchVlanId(vlanId);
1210 }
1211
1212 // if the objective is to revoke an existing rule, and for some reason
1213 // the next-objective does not exist, then a new one should not be created
1214 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1215 tbuilder.build(), mbuilder.build(), install);
1216 if (portNextObjId == -1) {
1217 // Warning log will come from getPortNextObjective method
1218 return;
1219 }
1220
1221 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1222 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1223 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1224
1225 ObjectiveContext context = new DefaultObjectiveContext(
1226 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1227 (objective, error) ->
1228 log.warn("Failed to {} IP rule for route {}: {}",
1229 install ? "install" : "revoke", prefix, error));
1230 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1231
1232 if (!install) {
1233 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1234 if (grpHandler == null) {
1235 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1236 } else {
1237 // Remove L3UG for the given port and host
1238 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1239 }
1240 }
1241 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001242
1243 /**
1244 * Checks if there is other enabled port within the given VLAN on the given device.
1245 *
1246 * @param deviceId device ID
1247 * @param vlanId VLAN ID
1248 * @return true if there is no more port enabled within the given VLAN on the given device
1249 */
1250 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1251 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1252 .filter(Port::isEnabled)
1253 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1254 .collect(Collectors.toSet());
1255
1256 return enabledPorts.stream().noneMatch(cp ->
1257 // Given vlanId is included in the vlan-tagged configuration
1258 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1259 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1260 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1261 vlanId.equals(INTERNAL_VLAN)) ||
1262 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1263 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1264 );
1265 }
sangho80f11cb2015-04-01 13:05:26 -07001266}