blob: bd57248283d6bdb9a9e4032a44f32a36e0c2bae7 [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
Saurav Das261c3002017-06-13 15:35:54 -0700355 if (destSw2 == null) {
356 // single dst - create destination set based on next-hop
Saurav Das97241862018-02-14 14:14:54 -0800357 // If the next hop is the same as the final destination, then MPLS
358 // label is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700359 Set<DeviceId> nhd1 = nextHops.get(destSw1);
360 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
361 tbuilder.immediate().decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800362 ds = new DestinationSet(false, false, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700363 treatment = tbuilder.build();
364 } else {
Saurav Das97241862018-02-14 14:14:54 -0800365 ds = new DestinationSet(false, false, segmentId1, destSw1);
Saurav Das261c3002017-06-13 15:35:54 -0700366 treatment = null;
367 }
368 } else {
369 // dst pair - IP rules for dst-pairs are always from other edge nodes
370 // the destination set needs to have both destinations, even if there
371 // are no next hops to one of them
Saurav Das97241862018-02-14 14:14:54 -0800372 ds = new DestinationSet(false, false, segmentId1, destSw1,
373 segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800374 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700375 }
376
Saurav Das4c35fc42015-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 Chan10b0fb72017-02-02 16:20:42 -0800381 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700382 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700383 if (grpHandler == null) {
384 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700385 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700386 return false;
387 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800388
Saurav Das261c3002017-06-13 15:35:54 -0700389 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800390 metabuilder.build(), false);
Saurav Das4c35fc42015-11-20 15:27:53 -0800391 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700392 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700393 return false;
394 }
395
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700396 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
397 .builder()
398 .fromApp(srManager.appId)
399 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800400 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700401 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800402 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700403 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800404 if (treatment != null) {
405 fwdBuilder.withTreatment(treatment);
406 }
Saurav Das62ae6792017-05-15 15:34:25 -0700407 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700408 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700409 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700410 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700411 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700412 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700413 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700414 ipPrefix, error, targetSw));
415 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700416 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700417
418 return true;
419 }
420
sangho80f11cb2015-04-01 13:05:26 -0700421 /**
Charles Chanc22cef32016-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 Chan3ed34d82017-06-22 18:03:14 -0700427 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800428 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-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
Charles Chana8d91532018-01-30 13:31:30 -0800441 srManager.deviceService.getAvailableDevices().forEach(device -> {
442 if (srManager.mastershipService.isLocalMaster(device.id())) {
443 ObjectiveContext context = new DefaultObjectiveContext(
444 (objective) -> log.debug("IP rule for router {} revoked from {}", ipPrefix, device.id()),
445 (objective, error) -> log.warn("Failed to revoke IP rule for router {} from {}: {}",
446 ipPrefix, device.id(), error));
447 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context));
448 } else {
449 log.debug("Not the master of {}. Abort route {} removal", device.id(), ipPrefix);
450 }
451 });
Charles Chanc22cef32016-04-29 14:38:22 -0700452
453 return true;
454 }
455
456 /**
Saurav Das97241862018-02-14 14:14:54 -0800457 * Populates MPLS flow rules in the target device to point towards the
458 * destination device.
459 *
460 * @param targetSwId target device ID of the switch to set the rules
461 * @param destSwId destination switch device ID
462 * @param nextHops next hops switch ID list
463 * @param routerIp the router ip of the destination switch
464 * @return true if all rules are set successfully, false otherwise
465 */
466 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
467 Set<DeviceId> nextHops, IpAddress routerIp) {
468 int segmentId;
469 try {
470 if (routerIp.isIp4()) {
471 segmentId = config.getIPv4SegmentId(destSwId);
472 } else {
473 segmentId = config.getIPv6SegmentId(destSwId);
474 }
475 } catch (DeviceConfigNotFoundException e) {
476 log.warn(e.getMessage() + " Aborting populateMplsRule.");
477 return false;
478 }
479
480 List<ForwardingObjective> fwdObjs = new ArrayList<>();
481 Collection<ForwardingObjective> fwdObjsMpls;
482 // Generates the transit rules used by the standard "routing".
483 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId,
484 routerIp, true);
485 if (fwdObjsMpls.isEmpty()) {
486 return false;
487 }
488 fwdObjs.addAll(fwdObjsMpls);
489
490 // Generates the transit rules used by the MPLS Pwaas.
491 int pwSrLabel;
492 try {
493 pwSrLabel = config.getPWRoutingLabel(destSwId);
494 } catch (DeviceConfigNotFoundException e) {
495 log.warn(e.getMessage()
496 + " Aborting populateMplsRule. No label for PseudoWire traffic.");
497 return false;
498 }
499 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel,
500 routerIp, false);
501 if (fwdObjsMpls.isEmpty()) {
502 return false;
503 }
504 fwdObjs.addAll(fwdObjsMpls);
505
506 for (ForwardingObjective fwdObj : fwdObjs) {
507 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
508 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
509 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
510 rulePopulationCounter.incrementAndGet();
511 }
512
513 return true;
514 }
515
516 /**
517 * Differentiates between popping and swapping labels when building an MPLS
518 * forwarding objective.
Pier Ventre229fd0b2016-10-31 16:49:19 -0700519 *
520 * @param targetSwId the target sw
521 * @param destSwId the destination sw
522 * @param nextHops the set of next hops
Saurav Das97241862018-02-14 14:14:54 -0800523 * @param segmentId the segmentId to match representing the destination
524 * switch
525 * @param routerIp the router ip representing the destination switch
Pier Ventre229fd0b2016-10-31 16:49:19 -0700526 * @return a collection of fwdobjective
527 */
Saurav Das261c3002017-06-13 15:35:54 -0700528 private Collection<ForwardingObjective> handleMpls(
529 DeviceId targetSwId,
530 DeviceId destSwId,
531 Set<DeviceId> nextHops,
532 int segmentId,
533 IpAddress routerIp,
534 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700535
536 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
537 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800538 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700539 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
540 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
541 sbuilder.matchMplsBos(isMplsBos);
542 TrafficSelector selector = sbuilder.build();
543
544 // setup metadata to pass to nextObjective - indicate the vlan on egress
545 // if needed by the switch pipeline. Since mpls next-hops are always to
546 // other neighboring routers, there is no subnet assigned on those ports.
547 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800548 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700549
550 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
551 // If the next hop is the destination router for the segment, do pop
552 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700553 + "label {} in switch {} with pop to next-hops {}",
554 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700555 ForwardingObjective.Builder fwdObjNoBosBuilder =
556 getMplsForwardingObjective(targetSwId,
557 nextHops,
558 true,
559 isMplsBos,
560 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700561 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800562 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700563 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700564 // Error case, we cannot handle, exit.
565 if (fwdObjNoBosBuilder == null) {
566 return Collections.emptyList();
567 }
568 fwdObjBuilders.add(fwdObjNoBosBuilder);
569
570 } else {
Saurav Das97241862018-02-14 14:14:54 -0800571 // next hop is not destination, irrespective of the number of next
572 // hops (1 or more) -- SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700573 log.debug("Installing MPLS forwarding objective for "
574 + "label {} in switch {} without pop to next-hops {}",
575 segmentId, targetSwId, nextHops);
Saurav Das2cb38292017-03-29 19:09:17 -0700576 ForwardingObjective.Builder fwdObjNoBosBuilder =
577 getMplsForwardingObjective(targetSwId,
578 nextHops,
579 false,
580 isMplsBos,
581 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700582 routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800583 segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700584 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700585 // Error case, we cannot handle, exit.
586 if (fwdObjNoBosBuilder == null) {
587 return Collections.emptyList();
588 }
589 fwdObjBuilders.add(fwdObjNoBosBuilder);
590
591 }
592
593 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
594 // We add the final property to the fwdObjs.
595 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700596 ((Builder) ((Builder) fwdObjBuilder
597 .fromApp(srManager.appId)
598 .makePermanent())
599 .withSelector(selector)
600 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
601 .withFlag(ForwardingObjective.Flag.SPECIFIC);
602
603 ObjectiveContext context = new DefaultObjectiveContext(
604 (objective) ->
605 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
606 objective.id(), segmentId, targetSwId),
607 (objective, error) ->
608 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
609 objective.id(), segmentId, error, targetSwId));
610
611 ForwardingObjective fob = fwdObjBuilder.add(context);
612 fwdObjs.add(fob);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700613 }
614
615 return fwdObjs;
616 }
617
618 /**
Saurav Das97241862018-02-14 14:14:54 -0800619 * Returns a Forwarding Objective builder for the MPLS rule that references
620 * the desired Next Objective. Creates a DestinationSet that allows the
621 * groupHandler to create or find the required next objective.
sangho80f11cb2015-04-01 13:05:26 -0700622 *
Saurav Das97241862018-02-14 14:14:54 -0800623 * @param targetSw the target sw
624 * @param nextHops the set of next hops
625 * @param phpRequired true if penultimate-hop-popping is required
626 * @param isBos true if matched label is bottom-of-stack
627 * @param meta metadata for creating next objective
628 * @param routerIp the router ip representing the destination switch
629 * @param destSw the destination sw
630 * @return the mpls forwarding objective builder
sangho80f11cb2015-04-01 13:05:26 -0700631 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800632 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700633 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800634 Set<DeviceId> nextHops,
635 boolean phpRequired,
636 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800637 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700638 IpAddress routerIp,
Saurav Das97241862018-02-14 14:14:54 -0800639 int segmentId,
Saurav Das62ae6792017-05-15 15:34:25 -0700640 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800641
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700642 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
643 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700644
645 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das97241862018-02-14 14:14:54 -0800646 DestinationSet ds = null;
647 boolean simple = false;
sangho80f11cb2015-04-01 13:05:26 -0700648 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800649 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700650 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700651 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700652 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800653 if (routerIp.isIp4()) {
654 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
655 } else {
656 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
657 }
658 tbuilder.decNwTtl();
Saurav Das97241862018-02-14 14:14:54 -0800659 // standard case -> BoS == True; pop results in IP packet and forwarding
660 // is via an ECMP group
661 ds = new DestinationSet(false, false, destSw);
sangho80f11cb2015-04-01 13:05:26 -0700662 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800663 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
664 .decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800665 // double-label case -> BoS == False, pop results in MPLS packet
666 // depending on configuration we can ECMP this packet or choose one output
667 if (srManager.getMplsEcmp()) {
668 ds = new DestinationSet(true, false, destSw);
669 } else {
670 ds = new DestinationSet(true, false, destSw);
671 simple = true;
672 }
sangho80f11cb2015-04-01 13:05:26 -0700673 }
674 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800675 // swap with self case - SR CONTINUE
Saurav Das97241862018-02-14 14:14:54 -0800676 log.debug("getMplsForwardingObjective: swap with self");
sangho27462c62015-05-14 00:39:53 -0700677 tbuilder.deferred().decMplsTtl();
Saurav Das97241862018-02-14 14:14:54 -0800678 // swap results in MPLS packet with same BoS bit regardless of bit value
679 // depending on configuration we can ECMP this packet or choose one output
680 if (srManager.getMplsEcmp()) {
681 ds = new DestinationSet(false, true, segmentId, destSw);
682 } else {
683 ds = new DestinationSet(false, true, segmentId, destSw);
684 simple = true;
685 }
sangho80f11cb2015-04-01 13:05:26 -0700686 }
687
Saurav Das4c35fc42015-11-20 15:27:53 -0800688 fwdBuilder.withTreatment(tbuilder.build());
Saurav Das97241862018-02-14 14:14:54 -0800689 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ds:{}",
690 targetSw, ds);
Saurav Das261c3002017-06-13 15:35:54 -0700691 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
692 if (gh == null) {
693 log.warn("getNextObjectiveId query - groupHandler for device {} "
694 + "not found", targetSw);
695 return null;
696 }
Saurav Das97241862018-02-14 14:14:54 -0800697
Saurav Das261c3002017-06-13 15:35:54 -0700698 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
699 dstNextHops.put(destSw, nextHops);
Saurav Das97241862018-02-14 14:14:54 -0800700 int nextId = gh.getNextObjectiveId(ds, dstNextHops, meta, simple);
Saurav Das4c35fc42015-11-20 15:27:53 -0800701 if (nextId <= 0) {
Saurav Das97241862018-02-14 14:14:54 -0800702 log.warn("No next objective in {} for ds: {}", targetSw, ds);
Saurav Das4c35fc42015-11-20 15:27:53 -0800703 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700704 } else {
Saurav Das97241862018-02-14 14:14:54 -0800705 log.debug("nextObjId found:{} for mpls rule on device:{} to ds:{}",
706 nextId, targetSw, ds);
sangho80f11cb2015-04-01 13:05:26 -0700707 }
708
Saurav Das4c35fc42015-11-20 15:27:53 -0800709 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700710 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700711 }
712
713 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700714 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700715 * dstMac corresponding to the router's MAC address. For those pipelines
716 * that need to internally assign vlans to untagged packets, this method
717 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700718 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800719 * Note that the vlan assignment and filter programming should only be done by
720 * the master for a switch. This method is typically called at deviceAdd and
721 * programs filters only for the enabled ports of the device. For port-updates,
722 * that enable/disable ports after device add, singlePortFilter methods should
723 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700724 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700725 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800726 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700727 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700728 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700729 log.debug("Installing per-port filtering objective for untagged "
730 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800731
Saurav Das07c74602016-04-27 18:35:50 -0700732 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800733 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700734 log.warn("Device {} ports not available. Unable to add MacVlan filters",
735 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800736 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700737 }
Saurav Dasf9332192017-02-18 14:05:44 -0800738 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700739 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700740 if (!port.isEnabled()) {
741 disabledPorts++;
742 continue;
743 }
Charles Chan43be46b2017-02-26 22:59:35 -0800744 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800745 filteredPorts++;
746 } else {
747 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700748 }
Saurav Das7c305372015-10-28 12:39:42 -0700749 }
Charles Chan077314e2017-06-22 14:27:17 -0700750 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800751 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800752 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800753 errorPorts, filteredPorts);
754 }
755
756 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800757 * Creates or removes filtering objectives for a single port. Should only be
758 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800759 *
760 * @param deviceId device identifier
761 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800762 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800763 * @return true if no errors occurred during the build of the filtering objective
764 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700765 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800766 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
767 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
768 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
769 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
770
771 if (taggedVlans.size() != 0) {
772 // Filter for tagged vlans
773 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800774 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800775 return false;
776 }
777 if (nativeVlan != null) {
778 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800779 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800780 return false;
781 }
782 }
783 } else if (untaggedVlan != null) {
784 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800785 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800786 return false;
787 }
788 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700789 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800790 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800791 return false;
792 }
793 }
794 return true;
795 }
796
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700797 /**
798 * Updates filtering objectives for a single port. Should only be called by
799 * the master for a switch
800 * @param deviceId device identifier
801 * @param portNum port identifier for port to be filtered
802 * @param pushVlan true to push vlan, false otherwise
803 * @param vlanId vlan identifier
804 * @param install true to install the filtering objective, false to remove
805 */
806 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
807 boolean pushVlan, VlanId vlanId, boolean install) {
808 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
809 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
810 deviceId, portNum, vlanId);
811 }
812 }
813
Charles Chan43be46b2017-02-26 22:59:35 -0800814 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
815 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800816 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800817 if (fob == null) {
818 // error encountered during build
819 return false;
820 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700821 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700822 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800823 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800824 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700825 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800826 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700827 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800828 if (install) {
829 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
830 } else {
831 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800832 }
Charles Chan90772a72017-02-08 15:52:08 -0800833 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800834 }
835
Charles Chan90772a72017-02-08 15:52:08 -0800836 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
837 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800838 MacAddress deviceMac;
839 try {
840 deviceMac = config.getDeviceMac(deviceId);
841 } catch (DeviceConfigNotFoundException e) {
842 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
843 return null;
844 }
Saurav Dasf9332192017-02-18 14:05:44 -0800845 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
846 fob.withKey(Criteria.matchInPort(portnum))
847 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800848 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800849
Charles Chan17ca2202017-12-19 19:55:57 -0800850 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
851
Charles Chan90772a72017-02-08 15:52:08 -0800852 if (pushVlan) {
853 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -0800854 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -0800855 } else {
856 fob.addCondition(Criteria.matchVlanId(vlanId));
857 }
858
Charles Chan17ca2202017-12-19 19:55:57 -0800859 // NOTE: Some switch hardware share the same filtering flow among different ports.
860 // We use this metadata to let the driver know that there is no more enabled port
861 // within the same VLAN on this device.
Charles Chanf17f66b2018-02-26 21:33:25 -0800862 if (noMoreEnabledPort(deviceId, vlanId)) {
Charles Chan17ca2202017-12-19 19:55:57 -0800863 tBuilder.wipeDeferred();
864 }
865
866 fob.withMeta(tBuilder.build());
867
Saurav Dasf9332192017-02-18 14:05:44 -0800868 fob.permit().fromApp(srManager.appId);
869 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700870 }
871
872 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700873 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700874 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700875 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700876 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700877 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700878 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700879 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700880 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700881 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -0800882 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800883 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800884 routerIpv4 = config.getRouterIpv4(deviceId);
885 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -0800886 routerLinkLocalIpv6 = Ip6Address.valueOf(
887 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
888
Saurav Das261c3002017-06-13 15:35:54 -0700889 if (config.isPairedEdge(deviceId)) {
890 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
891 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
892 }
Charles Chan319d1a22015-11-03 10:42:14 -0800893 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800894 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800895 return;
896 }
897
Saurav Dasc28b3432015-10-30 17:45:38 -0700898 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
899 log.debug("Not installing port-IP punts - not the master for dev:{} ",
900 deviceId);
901 return;
902 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800903 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
904 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -0800905 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800906 if (routerIpv6 != null) {
907 allIps.add(routerIpv6);
908 }
Saurav Das261c3002017-06-13 15:35:54 -0700909 if (pairRouterIpv4 != null) {
910 allIps.add(pairRouterIpv4);
911 }
912 if (pairRouterIpv6 != null) {
913 allIps.add(pairRouterIpv6);
914 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800915 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700916 populateSingleIpPunts(deviceId, ipaddr);
917 }
918 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800919
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700920 /**
921 * Creates a forwarding objective to punt all IP packets, destined to the
922 * specified IP address, which should be router's port IP address.
923 *
924 * @param deviceId the switch dpid for the router
925 * @param ipAddress the IP address of the router's port
926 */
927 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
928 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
929 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
930
931 srManager.packetService.requestPackets(sbuilder.build(),
932 PacketPriority.CONTROL, srManager.appId, optDeviceId);
933 }
934
935 /**
936 * Removes a forwarding objective to punt all IP packets, destined to the
937 * specified IP address, which should be router's port IP address.
938 *
939 * @param deviceId the switch dpid for the router
940 * @param ipAddress the IP address of the router's port
941 */
942 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
943 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
944 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
945
946 try {
947 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
948 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
949 srManager.packetService.cancelPackets(sbuilder.build(),
950 PacketPriority.CONTROL, srManager.appId, optDeviceId);
951 }
952 } catch (DeviceConfigNotFoundException e) {
953 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700954 }
sangho80f11cb2015-04-01 13:05:26 -0700955 }
956
Charles Chanf4586112015-11-09 16:37:23 -0800957 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800958 * Method to build IPv4 or IPv6 selector.
959 *
960 * @param addressToMatch the address to match
961 */
962 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
963 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
964 }
965
966 /**
967 * Method to build IPv4 or IPv6 selector.
968 *
969 * @param prefixToMatch the prefix to match
970 */
971 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
972 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700973 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800974 if (prefixToMatch.isIp4()) {
975 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
976 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
977 return selectorBuilder;
978 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700979 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800980 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
981 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
982 return selectorBuilder;
983 }
984
985 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800986 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
987 * Furthermore, these are applied only by the master instance. Deferred actions
988 * are not cleared such that packets can be flooded in the cross connect use case
989 *
990 * @param deviceId the switch dpid for the router
991 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700992 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700993 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800994 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
995 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
996 deviceId);
997 return;
998 }
999
Charles Chan3ed34d82017-06-22 18:03:14 -07001000 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -08001001 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001002 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -08001003 .add(new ObjectiveContext() {
1004 @Override
1005 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001006 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -08001007 deviceId, error);
1008 }
1009 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001010 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001011
Pier Luigib9632ba2017-01-12 18:14:58 -08001012 // We punt all NDP packets towards the controller.
Charles Chan051490d2018-01-11 11:48:18 -08001013 ndpFwdObjective(null, true, ARP_NDP_PRIORITY).forEach(builder -> {
1014 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1015 @Override
1016 public void onError(Objective objective, ObjectiveError error) {
1017 log.warn("Failed to install forwarding objective to punt NDP to {}: {}",
1018 deviceId, error);
1019 }
1020 });
1021 srManager.flowObjectiveService.forward(deviceId, obj);
1022 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001023
1024 srManager.getPairLocalPorts(deviceId).ifPresent(port -> {
1025 ForwardingObjective pairFwdObj;
1026 // Do not punt ARP packets from pair port
1027 pairFwdObj = arpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1028 .add(new ObjectiveContext() {
1029 @Override
1030 public void onError(Objective objective, ObjectiveError error) {
1031 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1032 deviceId, error);
1033 }
1034 });
1035 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1036
1037 // Do not punt NDP packets from pair port
Charles Chan051490d2018-01-11 11:48:18 -08001038 ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1).forEach(builder -> {
1039 ForwardingObjective obj = builder.add(new ObjectiveContext() {
1040 @Override
1041 public void onError(Objective objective, ObjectiveError error) {
1042 log.warn("Failed to install forwarding objective to ignore ARP to {}: {}",
1043 deviceId, error);
1044 }
1045 });
1046 srManager.flowObjectiveService.forward(deviceId, obj);
1047 });
Charles Chan3ed34d82017-06-22 18:03:14 -07001048
1049 // Do not forward DAD packets from pair port
1050 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1051 .add(new ObjectiveContext() {
1052 @Override
1053 public void onError(Objective objective, ObjectiveError error) {
1054 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1055 deviceId, error);
1056 }
1057 });
1058 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1059 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001060 }
1061
Charles Chan3ed34d82017-06-22 18:03:14 -07001062 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1063 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001064 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001065 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001066 .withSelector(selector)
1067 .fromApp(srManager.appId)
1068 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001069 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001070 .makePermanent();
1071 }
1072
Charles Chan3ed34d82017-06-22 18:03:14 -07001073 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001074 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1075 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001076 if (port != null) {
1077 sBuilder.matchInPort(port);
1078 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001079
Charles Chan3ed34d82017-06-22 18:03:14 -07001080 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1081 if (punt) {
1082 tBuilder.punt();
1083 }
1084 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001085 }
1086
Charles Chan051490d2018-01-11 11:48:18 -08001087 private Set<ForwardingObjective.Builder> ndpFwdObjective(PortNumber port, boolean punt, int priority) {
1088 Set<ForwardingObjective.Builder> result = Sets.newHashSet();
Pier Luigib9632ba2017-01-12 18:14:58 -08001089
Charles Chan051490d2018-01-11 11:48:18 -08001090 Lists.newArrayList(NEIGHBOR_SOLICITATION, NEIGHBOR_ADVERTISEMENT, ROUTER_SOLICITATION, ROUTER_ADVERTISEMENT)
1091 .forEach(type -> {
1092 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1093 sBuilder.matchEthType(TYPE_IPV6)
1094 .matchIPProtocol(PROTOCOL_ICMP6)
1095 .matchIcmpv6Type(type);
1096 if (port != null) {
1097 sBuilder.matchInPort(port);
1098 }
1099
1100 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1101 if (punt) {
1102 tBuilder.punt();
1103 }
1104
1105 result.add(fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority));
1106 });
1107
1108 return result;
Charles Chan3ed34d82017-06-22 18:03:14 -07001109 }
1110
1111 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1112 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1113 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001114 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1115 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1116 // .matchIPProtocol(PROTOCOL_ICMP6)
1117 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001118 if (port != null) {
1119 sBuilder.matchInPort(port);
1120 }
1121
1122 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1123 tBuilder.wipeDeferred();
1124 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001125 }
1126
1127 /**
Charles Chanf4586112015-11-09 16:37:23 -08001128 * Populates a forwarding objective to send packets that miss other high
1129 * priority Bridging Table entries to a group that contains all ports of
1130 * its subnet.
1131 *
Charles Chanf4586112015-11-09 16:37:23 -08001132 * @param deviceId switch ID to set the rules
1133 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001134 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001135 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001136 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001137 });
1138 }
1139
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001140 /**
1141 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1142 * @param deviceId switch ID to set the rule
1143 * @param vlanId vlan ID to specify the subnet
1144 * @param install true to install the rule, false to revoke the rule
1145 */
1146 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1147 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1148
1149 if (nextId < 0) {
1150 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1151 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1152 return;
1153 }
1154
1155 // Driver should treat objective with MacAddress.NONE as the
1156 // subnet broadcast rule
1157 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1158 sbuilder.matchVlanId(vlanId);
1159 sbuilder.matchEthDst(MacAddress.NONE);
1160
1161 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1162 fob.withFlag(Flag.SPECIFIC)
1163 .withSelector(sbuilder.build())
1164 .nextStep(nextId)
1165 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1166 .fromApp(srManager.appId)
1167 .makePermanent();
1168 ObjectiveContext context = new DefaultObjectiveContext(
1169 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1170 (objective, error) ->
1171 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1172
1173 if (install) {
1174 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1175 } else {
1176 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1177 }
1178 }
1179
Charles Chan82ab1932016-01-30 23:22:37 -08001180 private int getPriorityFromPrefix(IpPrefix prefix) {
1181 return (prefix.isIp4()) ?
1182 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1183 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001184 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001185
1186 /**
1187 * Update Forwarding objective for each host and IP address connected to given port.
1188 * And create corresponding Simple Next objective if it does not exist.
1189 * Applied only when populating Forwarding objective
1190 * @param deviceId switch ID to set the rule
1191 * @param portNumber port number
1192 * @param prefix IP prefix of the route
1193 * @param hostMac MAC address of the next hop
1194 * @param vlanId Vlan ID of the port
1195 * @param popVlan true to pop vlan tag in TrafficTreatment
1196 * @param install true to populate the forwarding objective, false to revoke
1197 */
1198 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1199 VlanId vlanId, boolean popVlan, boolean install) {
1200 ForwardingObjective.Builder fob;
1201 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1202 MacAddress deviceMac;
1203 try {
1204 deviceMac = config.getDeviceMac(deviceId);
1205 } catch (DeviceConfigNotFoundException e) {
1206 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1207 return;
1208 }
1209
1210 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1211 tbuilder.deferred()
1212 .setEthDst(hostMac)
1213 .setEthSrc(deviceMac)
1214 .setOutput(portNumber);
1215
1216 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1217
1218 if (!popVlan) {
1219 tbuilder.setVlanId(vlanId);
1220 } else {
1221 mbuilder.matchVlanId(vlanId);
1222 }
1223
1224 // if the objective is to revoke an existing rule, and for some reason
1225 // the next-objective does not exist, then a new one should not be created
1226 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1227 tbuilder.build(), mbuilder.build(), install);
1228 if (portNextObjId == -1) {
1229 // Warning log will come from getPortNextObjective method
1230 return;
1231 }
1232
1233 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1234 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1235 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1236
1237 ObjectiveContext context = new DefaultObjectiveContext(
1238 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1239 (objective, error) ->
1240 log.warn("Failed to {} IP rule for route {}: {}",
1241 install ? "install" : "revoke", prefix, error));
1242 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1243
1244 if (!install) {
1245 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1246 if (grpHandler == null) {
1247 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1248 } else {
1249 // Remove L3UG for the given port and host
1250 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1251 }
1252 }
1253 }
Charles Chanf17f66b2018-02-26 21:33:25 -08001254
1255 /**
1256 * Checks if there is other enabled port within the given VLAN on the given device.
1257 *
1258 * @param deviceId device ID
1259 * @param vlanId VLAN ID
1260 * @return true if there is no more port enabled within the given VLAN on the given device
1261 */
1262 boolean noMoreEnabledPort(DeviceId deviceId, VlanId vlanId) {
1263 Set<ConnectPoint> enabledPorts = srManager.deviceService.getPorts(deviceId).stream()
1264 .filter(Port::isEnabled)
1265 .map(port -> new ConnectPoint(port.element().id(), port.number()))
1266 .collect(Collectors.toSet());
1267
1268 return enabledPorts.stream().noneMatch(cp ->
1269 // Given vlanId is included in the vlan-tagged configuration
1270 srManager.getTaggedVlanId(cp).contains(vlanId) ||
1271 // Given vlanId is INTERNAL_VLAN and the interface is not configured
1272 (srManager.getTaggedVlanId(cp).isEmpty() && srManager.getInternalVlanId(cp) == null &&
1273 vlanId.equals(INTERNAL_VLAN)) ||
1274 // interface is configured and either vlan-untagged or vlan-native matches given vlanId
1275 (srManager.getInternalVlanId(cp) != null && srManager.getInternalVlanId(cp).equals(vlanId))
1276 );
1277 }
sangho80f11cb2015-04-01 13:05:26 -07001278}