blob: a76e9667c0313f48d6a3390d35086fdc540e19c5 [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;
Saurav Das4c35fc42015-11-20 15:27:53 -080019import org.onlab.packet.EthType;
sangho80f11cb2015-04-01 13:05:26 -070020import org.onlab.packet.Ethernet;
Charles Chanef8d12e2017-12-05 21:07:38 -080021import org.onlab.packet.IPv6;
sangho80f11cb2015-04-01 13:05:26 -070022import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080023import org.onlab.packet.Ip6Address;
24import org.onlab.packet.IpAddress;
sangho80f11cb2015-04-01 13:05:26 -070025import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
sangho80f11cb2015-04-01 13:05:26 -070027import org.onlab.packet.MplsLabel;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070028import org.onlab.packet.VlanId;
Charles Chanb7f75ac2016-01-11 18:28:54 -080029import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070030import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080031import org.onosproject.net.flowobjective.Objective;
Charles Chan1eaf4802016-04-18 13:44:03 -070032import org.onosproject.net.flowobjective.ObjectiveContext;
Pier Luigib9632ba2017-01-12 18:14:58 -080033import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080034import org.onosproject.net.packet.PacketPriority;
Saurav Dasd1872b02016-12-02 15:43:47 -080035import org.onosproject.segmentrouting.DefaultRoutingHandler.PortFilterInfo;
Charles Chan319d1a22015-11-03 10:42:14 -080036import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
37import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das62ae6792017-05-15 15:34:25 -070038import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -070039import org.onosproject.segmentrouting.grouphandler.DestinationSet;
sangho80f11cb2015-04-01 13:05:26 -070040import org.onosproject.net.DeviceId;
Saurav Das7c305372015-10-28 12:39:42 -070041import org.onosproject.net.Port;
sangho80f11cb2015-04-01 13:05:26 -070042import org.onosproject.net.PortNumber;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
sangho80f11cb2015-04-01 13:05:26 -070045import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070047import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flowobjective.DefaultFilteringObjective;
49import org.onosproject.net.flowobjective.DefaultForwardingObjective;
50import org.onosproject.net.flowobjective.FilteringObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.ForwardingObjective.Builder;
Saurav Das9f1c42e2015-10-23 10:51:11 -070053import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
sangho80f11cb2015-04-01 13:05:26 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.ArrayList;
Pier Ventre229fd0b2016-10-31 16:49:19 -070058import java.util.Collection;
59import java.util.Collections;
Saurav Das261c3002017-06-13 15:35:54 -070060import java.util.HashMap;
Saurav Dasc28b3432015-10-30 17:45:38 -070061import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070062import java.util.List;
Saurav Das261c3002017-06-13 15:35:54 -070063import java.util.Map;
Charles Chan2d0bbcd2017-01-09 11:45:08 -080064import java.util.Optional;
sangho80f11cb2015-04-01 13:05:26 -070065import java.util.Set;
sanghofb7c7292015-04-13 15:15:58 -070066import java.util.concurrent.atomic.AtomicLong;
sangho80f11cb2015-04-01 13:05:26 -070067
68import static com.google.common.base.Preconditions.checkNotNull;
Pier Luigib9632ba2017-01-12 18:14:58 -080069import static org.onlab.packet.Ethernet.TYPE_ARP;
70import static org.onlab.packet.Ethernet.TYPE_IPV6;
71import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
72import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chan10b0fb72017-02-02 16:20:42 -080073import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
sangho80f11cb2015-04-01 13:05:26 -070074
Charles Chanb7f75ac2016-01-11 18:28:54 -080075/**
76 * Populator of segment routing flow rules.
77 */
sangho80f11cb2015-04-01 13:05:26 -070078public class RoutingRulePopulator {
Charles Chanef8d12e2017-12-05 21:07:38 -080079 private static final Logger log = LoggerFactory.getLogger(RoutingRulePopulator.class);
80
81 private static final int ARP_NDP_PRIORITY = 30000;
sangho80f11cb2015-04-01 13:05:26 -070082
sanghofb7c7292015-04-13 15:15:58 -070083 private AtomicLong rulePopulationCounter;
sangho9b169e32015-04-14 16:27:13 -070084 private SegmentRoutingManager srManager;
85 private DeviceConfiguration config;
Saurav Das9f1c42e2015-10-23 10:51:11 -070086
sangho80f11cb2015-04-01 13:05:26 -070087 /**
88 * Creates a RoutingRulePopulator object.
89 *
Thomas Vachuska8a075092015-04-15 18:20:08 -070090 * @param srManager segment routing manager reference
sangho80f11cb2015-04-01 13:05:26 -070091 */
Charles Chan3ed34d82017-06-22 18:03:14 -070092 RoutingRulePopulator(SegmentRoutingManager srManager) {
sangho80f11cb2015-04-01 13:05:26 -070093 this.srManager = srManager;
sangho9b169e32015-04-14 16:27:13 -070094 this.config = checkNotNull(srManager.deviceConfiguration);
sanghofb7c7292015-04-13 15:15:58 -070095 this.rulePopulationCounter = new AtomicLong(0);
96 }
97
98 /**
99 * Resets the population counter.
100 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700101 void resetCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700102 rulePopulationCounter.set(0);
103 }
104
105 /**
106 * Returns the number of rules populated.
Thomas Vachuska7cfc6202015-04-30 18:13:25 -0700107 *
108 * @return number of rules
sanghofb7c7292015-04-13 15:15:58 -0700109 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700110 long getCounter() {
sanghofb7c7292015-04-13 15:15:58 -0700111 return rulePopulationCounter.get();
sangho80f11cb2015-04-01 13:05:26 -0700112 }
113
114 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700115 * Populates IP rules for a route that has direct connection to the
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700116 * switch.
sangho80f11cb2015-04-01 13:05:26 -0700117 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700118 * @param deviceId device ID of the device that next hop attaches to
119 * @param prefix IP prefix of the route
120 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800121 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700122 * @param outPort port where the next hop attaches to
sangho80f11cb2015-04-01 13:05:26 -0700123 */
Charles Chan910be6a2017-08-23 14:46:43 -0700124 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800125 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Saurav Das261c3002017-06-13 15:35:54 -0700126 log.debug("Populate direct routing entry for route {} at {}:{}",
Charles Chanddac7fd2016-10-27 14:19:48 -0700127 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800128 ForwardingObjective.Builder fwdBuilder;
Charles Chan319d1a22015-11-03 10:42:14 -0800129 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700130 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
131 hostVlanId, outPort, false);
Charles Chan319d1a22015-11-03 10:42:14 -0800132 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700133 log.warn(e.getMessage() + " Aborting direct populateRoute");
Charles Chan319d1a22015-11-03 10:42:14 -0800134 return;
135 }
Saurav Das07c74602016-04-27 18:35:50 -0700136 if (fwdBuilder == null) {
Saurav Das368cf212017-03-15 15:15:14 -0700137 log.warn("Aborting host routing table entry due "
Charles Chanddac7fd2016-10-27 14:19:48 -0700138 + "to error for dev:{} route:{}", deviceId, prefix);
Saurav Das07c74602016-04-27 18:35:50 -0700139 return;
140 }
Charles Chan910be6a2017-08-23 14:46:43 -0700141
142 int nextId = fwdBuilder.add().nextId();
Charles Chan1eaf4802016-04-18 13:44:03 -0700143 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan910be6a2017-08-23 14:46:43 -0700144 (objective) -> log.debug("Direct routing rule for route {} populated. nextId={}",
145 prefix, nextId),
Charles Chan1eaf4802016-04-18 13:44:03 -0700146 (objective, error) ->
Saurav Das261c3002017-06-13 15:35:54 -0700147 log.warn("Failed to populate direct routing rule for route {}: {}",
Saurav Das368cf212017-03-15 15:15:14 -0700148 prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700149 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.add(context));
Charles Chanf4586112015-11-09 16:37:23 -0800150 rulePopulationCounter.incrementAndGet();
151 }
152
Charles Chanb7f75ac2016-01-11 18:28:54 -0800153 /**
Charles Chanddac7fd2016-10-27 14:19:48 -0700154 * Removes IP rules for a route when the next hop is gone.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800155 *
Charles Chanddac7fd2016-10-27 14:19:48 -0700156 * @param deviceId device ID of the device that next hop attaches to
157 * @param prefix IP prefix of the route
158 * @param hostMac MAC address of the next hop
Charles Chan90772a72017-02-08 15:52:08 -0800159 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700160 * @param outPort port that next hop attaches to
Charles Chanb7f75ac2016-01-11 18:28:54 -0800161 */
Charles Chan910be6a2017-08-23 14:46:43 -0700162 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Charles Chan90772a72017-02-08 15:52:08 -0800163 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort) {
Charles Chanddac7fd2016-10-27 14:19:48 -0700164 log.debug("Revoke IP table entry for route {} at {}:{}",
165 prefix, deviceId, outPort);
Charles Chanf4586112015-11-09 16:37:23 -0800166 ForwardingObjective.Builder fwdBuilder;
167 try {
Saurav Das2cb38292017-03-29 19:09:17 -0700168 fwdBuilder = routingFwdObjBuilder(deviceId, prefix, hostMac,
169 hostVlanId, outPort, true);
Charles Chanf4586112015-11-09 16:37:23 -0800170 } catch (DeviceConfigNotFoundException e) {
171 log.warn(e.getMessage() + " Aborting revokeIpRuleForHost.");
172 return;
173 }
Charles Chanea702b12016-11-30 11:55:05 -0800174 if (fwdBuilder == null) {
175 log.warn("Aborting host routing table entries due "
176 + "to error for dev:{} route:{}", deviceId, prefix);
177 return;
178 }
Charles Chan1eaf4802016-04-18 13:44:03 -0700179 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chanddac7fd2016-10-27 14:19:48 -0700180 (objective) -> log.debug("IP rule for route {} revoked", prefix),
Charles Chan1eaf4802016-04-18 13:44:03 -0700181 (objective, error) ->
Charles Chanddac7fd2016-10-27 14:19:48 -0700182 log.warn("Failed to revoke IP rule for route {}: {}", prefix, error));
Charles Chan1eaf4802016-04-18 13:44:03 -0700183 srManager.flowObjectiveService.forward(deviceId, fwdBuilder.remove(context));
Charles Chanf4586112015-11-09 16:37:23 -0800184 }
185
Charles Chanddac7fd2016-10-27 14:19:48 -0700186 /**
Charles Chan18fa4252017-02-08 16:10:40 -0800187 * Returns a forwarding objective builder for routing rules.
188 * <p>
189 * The forwarding objective routes packets destined to a given prefix to
190 * given port on given device with given destination MAC.
Charles Chanddac7fd2016-10-27 14:19:48 -0700191 *
192 * @param deviceId device ID
193 * @param prefix prefix that need to be routed
194 * @param hostMac MAC address of the nexthop
Charles Chan90772a72017-02-08 15:52:08 -0800195 * @param hostVlanId Vlan ID of the nexthop
Charles Chanddac7fd2016-10-27 14:19:48 -0700196 * @param outPort port where the nexthop attaches to
Saurav Das2cb38292017-03-29 19:09:17 -0700197 * @param revoke true if forwarding objective is meant to revoke forwarding rule
Charles Chanddac7fd2016-10-27 14:19:48 -0700198 * @return forwarding objective builder
199 * @throws DeviceConfigNotFoundException if given device is not configured
200 */
Charles Chan18fa4252017-02-08 16:10:40 -0800201 private ForwardingObjective.Builder routingFwdObjBuilder(
Charles Chanddac7fd2016-10-27 14:19:48 -0700202 DeviceId deviceId, IpPrefix prefix,
Saurav Das2cb38292017-03-29 19:09:17 -0700203 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort,
204 boolean revoke)
Charles Chanf4586112015-11-09 16:37:23 -0800205 throws DeviceConfigNotFoundException {
206 MacAddress deviceMac;
207 deviceMac = config.getDeviceMac(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800208
Charles Chan90772a72017-02-08 15:52:08 -0800209 ConnectPoint connectPoint = new ConnectPoint(deviceId, outPort);
210 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
211 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
212 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
sangho80f11cb2015-04-01 13:05:26 -0700213
Charles Chan90772a72017-02-08 15:52:08 -0800214 // Create route selector
215 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
216
217 // Create route treatment
Charles Chanddac7fd2016-10-27 14:19:48 -0700218 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
sangho27462c62015-05-14 00:39:53 -0700219 tbuilder.deferred()
220 .setEthDst(hostMac)
Charles Chan319d1a22015-11-03 10:42:14 -0800221 .setEthSrc(deviceMac)
sangho80f11cb2015-04-01 13:05:26 -0700222 .setOutput(outPort);
Saurav Das2d94d312015-11-24 23:21:05 -0800223
Charles Chan90772a72017-02-08 15:52:08 -0800224 // Create route meta
225 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
Charles Chan10b0fb72017-02-02 16:20:42 -0800226
Charles Chan90772a72017-02-08 15:52:08 -0800227 // Adjust the meta according to VLAN configuration
228 if (taggedVlans.contains(hostVlanId)) {
229 tbuilder.setVlanId(hostVlanId);
230 } else if (hostVlanId.equals(VlanId.NONE)) {
231 if (untaggedVlan != null) {
232 mbuilder.matchVlanId(untaggedVlan);
233 } else if (nativeVlan != null) {
234 mbuilder.matchVlanId(nativeVlan);
235 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700236 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
237 hostMac, hostVlanId, connectPoint);
238 return null;
Charles Chan90772a72017-02-08 15:52:08 -0800239 }
240 } else {
Saurav Das2cb38292017-03-29 19:09:17 -0700241 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
242 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
Saurav Das07c74602016-04-27 18:35:50 -0700243 return null;
244 }
Saurav Das2cb38292017-03-29 19:09:17 -0700245 // if the objective is to revoke an existing rule, and for some reason
246 // the next-objective does not exist, then a new one should not be created
Charles Chan90772a72017-02-08 15:52:08 -0800247 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outPort,
Saurav Das2cb38292017-03-29 19:09:17 -0700248 tbuilder.build(), mbuilder.build(), !revoke);
Charles Chan90772a72017-02-08 15:52:08 -0800249 if (portNextObjId == -1) {
250 // Warning log will come from getPortNextObjective method
251 return null;
252 }
253
Charles Chanf4586112015-11-09 16:37:23 -0800254 return DefaultForwardingObjective.builder()
Charles Chan90772a72017-02-08 15:52:08 -0800255 .withSelector(sbuilder.build())
Saurav Das2d94d312015-11-24 23:21:05 -0800256 .nextStep(portNextObjId)
Charles Chanf4586112015-11-09 16:37:23 -0800257 .fromApp(srManager.appId).makePermanent()
Charles Chanddac7fd2016-10-27 14:19:48 -0700258 .withPriority(getPriorityFromPrefix(prefix))
Charles Chan82ab1932016-01-30 23:22:37 -0800259 .withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700260 }
261
262 /**
Saurav Das261c3002017-06-13 15:35:54 -0700263 * Populates IP flow rules for all the given prefixes reachable from the
264 * destination switch(es).
sangho80f11cb2015-04-01 13:05:26 -0700265 *
Saurav Das261c3002017-06-13 15:35:54 -0700266 * @param targetSw switch where rules are to be programmed
267 * @param subnets subnets/prefixes being added
268 * @param destSw1 destination switch where the prefixes are reachable
269 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
270 * Should be null if there is no paired destination switch (by config)
271 * or if the given prefixes are reachable only via destSw1
272 * @param nextHops a map containing a set of next-hops for each destination switch.
273 * If destSw2 is not null, then this map must contain an
274 * entry for destSw2 with its next-hops from the targetSw
275 * (although the next-hop set may be empty in certain scenarios).
276 * If destSw2 is null, there should not be an entry in this
277 * map for destSw2.
sangho80f11cb2015-04-01 13:05:26 -0700278 * @return true if all rules are set successfully, false otherwise
279 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700280 boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
Saurav Das261c3002017-06-13 15:35:54 -0700281 DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700282 for (IpPrefix subnet : subnets) {
Saurav Das261c3002017-06-13 15:35:54 -0700283 if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
sangho80f11cb2015-04-01 13:05:26 -0700284 return false;
285 }
286 }
Charles Chanc22cef32016-04-29 14:38:22 -0700287 return true;
288 }
sangho80f11cb2015-04-01 13:05:26 -0700289
Charles Chanc22cef32016-04-29 14:38:22 -0700290 /**
Saurav Das261c3002017-06-13 15:35:54 -0700291 * Revokes IP flow rules for the subnets in each edge switch.
Charles Chanc22cef32016-04-29 14:38:22 -0700292 *
293 * @param subnets subnet being removed
294 * @return true if all rules are removed successfully, false otherwise
295 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700296 boolean revokeIpRuleForSubnet(Set<IpPrefix> subnets) {
Charles Chanc22cef32016-04-29 14:38:22 -0700297 for (IpPrefix subnet : subnets) {
298 if (!revokeIpRuleForRouter(subnet)) {
299 return false;
300 }
301 }
sangho80f11cb2015-04-01 13:05:26 -0700302 return true;
303 }
304
305 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700306 * Populates IP flow rules for an IP prefix in the target device. The prefix
Saurav Das261c3002017-06-13 15:35:54 -0700307 * is reachable via destination device(s).
sangho80f11cb2015-04-01 13:05:26 -0700308 *
Saurav Das261c3002017-06-13 15:35:54 -0700309 * @param targetSw target device ID to set the rules
310 * @param ipPrefix the IP prefix
311 * @param destSw1 destination switch where the prefixes are reachable
312 * @param destSw2 paired destination switch if one exists for the subnets/prefixes.
313 * Should be null if there is no paired destination switch (by config)
314 * or if the given prefixes are reachable only via destSw1
315 * @param nextHops map of destination switches and their next-hops.
316 * Should only contain destination switches that are
317 * actually meant to be routed to. If destSw2 is null, there
318 * should not be an entry for destSw2 in this map.
sangho80f11cb2015-04-01 13:05:26 -0700319 * @return true if all rules are set successfully, false otherwise
320 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700321 private boolean populateIpRuleForRouter(DeviceId targetSw,
Saurav Das261c3002017-06-13 15:35:54 -0700322 IpPrefix ipPrefix, DeviceId destSw1,
323 DeviceId destSw2,
324 Map<DeviceId, Set<DeviceId>> nextHops) {
325 int segmentId1, segmentId2 = -1;
Charles Chan319d1a22015-11-03 10:42:14 -0800326 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800327 if (ipPrefix.isIp4()) {
Saurav Das261c3002017-06-13 15:35:54 -0700328 segmentId1 = config.getIPv4SegmentId(destSw1);
329 if (destSw2 != null) {
330 segmentId2 = config.getIPv4SegmentId(destSw2);
331 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800332 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700333 segmentId1 = config.getIPv6SegmentId(destSw1);
334 if (destSw2 != null) {
335 segmentId2 = config.getIPv6SegmentId(destSw2);
336 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800337 }
Charles Chan319d1a22015-11-03 10:42:14 -0800338 } catch (DeviceConfigNotFoundException e) {
339 log.warn(e.getMessage() + " Aborting populateIpRuleForRouter.");
340 return false;
341 }
sangho80f11cb2015-04-01 13:05:26 -0700342
Pier Ventreadb4ae62016-11-23 09:57:42 -0800343 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanf4586112015-11-09 16:37:23 -0800344 TrafficSelector selector = sbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700345
Charles Chanf4586112015-11-09 16:37:23 -0800346 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
Saurav Das261c3002017-06-13 15:35:54 -0700347 DestinationSet ds;
Charles Chanf4586112015-11-09 16:37:23 -0800348 TrafficTreatment treatment;
sangho80f11cb2015-04-01 13:05:26 -0700349
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700350 // If the next hop is the same as the final destination, then MPLS label
351 // is not set.
Saurav Das261c3002017-06-13 15:35:54 -0700352 /*if (nextHops.size() == 1 && nextHops.toArray()[0].equals(destSw)) {
Charles Chanf4586112015-11-09 16:37:23 -0800353 tbuilder.immediate().decNwTtl();
Saurav Das261c3002017-06-13 15:35:54 -0700354 ds = new DestinationSet(false, destSw);
Charles Chanf4586112015-11-09 16:37:23 -0800355 treatment = tbuilder.build();
sangho80f11cb2015-04-01 13:05:26 -0700356 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700357 ds = new DestinationSet(false, segmentId, destSw);
358 treatment = null;
359 }*/
360 if (destSw2 == null) {
361 // single dst - create destination set based on next-hop
362 Set<DeviceId> nhd1 = nextHops.get(destSw1);
363 if (nhd1.size() == 1 && nhd1.iterator().next().equals(destSw1)) {
364 tbuilder.immediate().decNwTtl();
365 ds = new DestinationSet(false, destSw1);
366 treatment = tbuilder.build();
367 } else {
368 ds = new DestinationSet(false, segmentId1, destSw1);
369 treatment = null;
370 }
371 } else {
372 // dst pair - IP rules for dst-pairs are always from other edge nodes
373 // the destination set needs to have both destinations, even if there
374 // are no next hops to one of them
375 ds = new DestinationSet(false, segmentId1, destSw1, segmentId2, destSw2);
Charles Chanf4586112015-11-09 16:37:23 -0800376 treatment = null;
sangho80f11cb2015-04-01 13:05:26 -0700377 }
378
Saurav Das4c35fc42015-11-20 15:27:53 -0800379 // setup metadata to pass to nextObjective - indicate the vlan on egress
380 // if needed by the switch pipeline. Since neighbor sets are always to
381 // other neighboring routers, there is no subnet assigned on those ports.
382 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800383 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Saurav Das261c3002017-06-13 15:35:54 -0700384 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700385 if (grpHandler == null) {
386 log.warn("populateIPRuleForRouter: groupHandler for device {} "
Saurav Das261c3002017-06-13 15:35:54 -0700387 + "not found", targetSw);
Saurav Das62ae6792017-05-15 15:34:25 -0700388 return false;
389 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800390
Saurav Das261c3002017-06-13 15:35:54 -0700391 int nextId = grpHandler.getNextObjectiveId(ds, nextHops,
392 metabuilder.build(), true);
Saurav Das4c35fc42015-11-20 15:27:53 -0800393 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700394 log.warn("No next objective in {} for ds: {}", targetSw, ds);
sangho2165d222015-05-01 09:38:25 -0700395 return false;
396 }
397
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700398 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
399 .builder()
400 .fromApp(srManager.appId)
401 .makePermanent()
Saurav Das4c35fc42015-11-20 15:27:53 -0800402 .nextStep(nextId)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700403 .withSelector(selector)
Charles Chan82ab1932016-01-30 23:22:37 -0800404 .withPriority(getPriorityFromPrefix(ipPrefix))
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700405 .withFlag(ForwardingObjective.Flag.SPECIFIC);
Charles Chanf4586112015-11-09 16:37:23 -0800406 if (treatment != null) {
407 fwdBuilder.withTreatment(treatment);
408 }
Saurav Das62ae6792017-05-15 15:34:25 -0700409 log.debug("Installing IPv4 forwarding objective for router IP/subnet {} "
Saurav Das261c3002017-06-13 15:35:54 -0700410 + "in switch {} with nextId: {}", ipPrefix, targetSw, nextId);
Charles Chan1eaf4802016-04-18 13:44:03 -0700411 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dase0237a32016-05-27 13:54:07 -0700412 (objective) -> log.debug("IP rule for router {} populated in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700413 ipPrefix, targetSw),
Charles Chan1eaf4802016-04-18 13:44:03 -0700414 (objective, error) ->
Saurav Dase0237a32016-05-27 13:54:07 -0700415 log.warn("Failed to populate IP rule for router {}: {} in dev:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700416 ipPrefix, error, targetSw));
417 srManager.flowObjectiveService.forward(targetSw, fwdBuilder.add(context));
sanghofb7c7292015-04-13 15:15:58 -0700418 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700419
420 return true;
421 }
422
sangho80f11cb2015-04-01 13:05:26 -0700423 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700424 * Revokes IP flow rules for the router IP address.
425 *
426 * @param ipPrefix the IP address of the destination router
427 * @return true if all rules are removed successfully, false otherwise
428 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700429 private boolean revokeIpRuleForRouter(IpPrefix ipPrefix) {
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800430 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700431 TrafficSelector selector = sbuilder.build();
432 TrafficTreatment dummyTreatment = DefaultTrafficTreatment.builder().build();
433
434 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
435 .builder()
436 .fromApp(srManager.appId)
437 .makePermanent()
438 .withSelector(selector)
439 .withTreatment(dummyTreatment)
440 .withPriority(getPriorityFromPrefix(ipPrefix))
441 .withFlag(ForwardingObjective.Flag.SPECIFIC);
442
443 ObjectiveContext context = new DefaultObjectiveContext(
444 (objective) -> log.debug("IP rule for router {} revoked", ipPrefix),
445 (objective, error) ->
446 log.warn("Failed to revoke IP rule for router {}: {}", ipPrefix, error));
447
Charles Chan3ed34d82017-06-22 18:03:14 -0700448 srManager.deviceService.getAvailableDevices().forEach(device ->
449 srManager.flowObjectiveService.forward(device.id(), fwdBuilder.remove(context))
450 );
Charles Chanc22cef32016-04-29 14:38:22 -0700451
452 return true;
453 }
454
455 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700456 * Deals with !MPLS Bos use case.
457 *
458 * @param targetSwId the target sw
459 * @param destSwId the destination sw
460 * @param nextHops the set of next hops
461 * @param segmentId the segmentId to match
462 * @param routerIp the router ip
463 * @return a collection of fwdobjective
464 */
Saurav Das261c3002017-06-13 15:35:54 -0700465 private Collection<ForwardingObjective> handleMpls(
466 DeviceId targetSwId,
467 DeviceId destSwId,
468 Set<DeviceId> nextHops,
469 int segmentId,
470 IpAddress routerIp,
471 boolean isMplsBos) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700472
473 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
474 List<ForwardingObjective.Builder> fwdObjBuilders = Lists.newArrayList();
Pier Luigi0be3f0f2017-01-30 09:47:36 -0800475 // For the transport of Pwaas we can use two or three MPLS label
Pier Ventre229fd0b2016-10-31 16:49:19 -0700476 sbuilder.matchEthType(Ethernet.MPLS_UNICAST);
477 sbuilder.matchMplsLabel(MplsLabel.mplsLabel(segmentId));
478 sbuilder.matchMplsBos(isMplsBos);
479 TrafficSelector selector = sbuilder.build();
480
481 // setup metadata to pass to nextObjective - indicate the vlan on egress
482 // if needed by the switch pipeline. Since mpls next-hops are always to
483 // other neighboring routers, there is no subnet assigned on those ports.
484 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(selector);
Charles Chan10b0fb72017-02-02 16:20:42 -0800485 metabuilder.matchVlanId(SegmentRoutingManager.INTERNAL_VLAN);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700486
487 if (nextHops.size() == 1 && destSwId.equals(nextHops.toArray()[0])) {
488 // If the next hop is the destination router for the segment, do pop
489 log.debug("populateMplsRule: Installing MPLS forwarding objective for "
Saurav Das261c3002017-06-13 15:35:54 -0700490 + "label {} in switch {} with pop to next-hops {}",
491 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700492 // Not-bos pop case (php for the current label). If MPLS-ECMP
493 // has been configured, the application we will request the
494 // installation for an MPLS-ECMP group.
Saurav Das2cb38292017-03-29 19:09:17 -0700495 ForwardingObjective.Builder fwdObjNoBosBuilder =
496 getMplsForwardingObjective(targetSwId,
497 nextHops,
498 true,
499 isMplsBos,
500 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700501 routerIp,
502 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700503 // Error case, we cannot handle, exit.
504 if (fwdObjNoBosBuilder == null) {
505 return Collections.emptyList();
506 }
507 fwdObjBuilders.add(fwdObjNoBosBuilder);
508
509 } else {
510 // next hop is not destination, SR CONTINUE case (swap with self)
Saurav Das261c3002017-06-13 15:35:54 -0700511 log.debug("Installing MPLS forwarding objective for "
512 + "label {} in switch {} without pop to next-hops {}",
513 segmentId, targetSwId, nextHops);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700514 // Not-bos pop case. If MPLS-ECMP has been configured, the
515 // application we will request the installation for an MPLS-ECMP
516 // group.
Saurav Das2cb38292017-03-29 19:09:17 -0700517 ForwardingObjective.Builder fwdObjNoBosBuilder =
518 getMplsForwardingObjective(targetSwId,
519 nextHops,
520 false,
521 isMplsBos,
522 metabuilder.build(),
Saurav Das62ae6792017-05-15 15:34:25 -0700523 routerIp,
524 destSwId);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700525 // Error case, we cannot handle, exit.
526 if (fwdObjNoBosBuilder == null) {
527 return Collections.emptyList();
528 }
529 fwdObjBuilders.add(fwdObjNoBosBuilder);
530
531 }
532
533 List<ForwardingObjective> fwdObjs = Lists.newArrayList();
534 // We add the final property to the fwdObjs.
535 for (ForwardingObjective.Builder fwdObjBuilder : fwdObjBuilders) {
536
537 ((Builder) ((Builder) fwdObjBuilder
538 .fromApp(srManager.appId)
539 .makePermanent())
540 .withSelector(selector)
541 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY))
542 .withFlag(ForwardingObjective.Flag.SPECIFIC);
543
544 ObjectiveContext context = new DefaultObjectiveContext(
545 (objective) ->
546 log.debug("MPLS rule {} for SID {} populated in dev:{} ",
547 objective.id(), segmentId, targetSwId),
548 (objective, error) ->
549 log.warn("Failed to populate MPLS rule {} for SID {}: {} in dev:{}",
550 objective.id(), segmentId, error, targetSwId));
551
552 ForwardingObjective fob = fwdObjBuilder.add(context);
553 fwdObjs.add(fob);
554
555 }
556
557 return fwdObjs;
558 }
559
560 /**
Saurav Dase0237a32016-05-27 13:54:07 -0700561 * Populates MPLS flow rules in the target device to point towards the
562 * destination device.
sangho80f11cb2015-04-01 13:05:26 -0700563 *
Saurav Dase0237a32016-05-27 13:54:07 -0700564 * @param targetSwId target device ID of the switch to set the rules
sangho80f11cb2015-04-01 13:05:26 -0700565 * @param destSwId destination switch device ID
566 * @param nextHops next hops switch ID list
Pier Ventre229fd0b2016-10-31 16:49:19 -0700567 * @param routerIp the router ip
sangho80f11cb2015-04-01 13:05:26 -0700568 * @return true if all rules are set successfully, false otherwise
569 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700570 boolean populateMplsRule(DeviceId targetSwId, DeviceId destSwId,
Saurav Das261c3002017-06-13 15:35:54 -0700571 Set<DeviceId> nextHops,
572 IpAddress routerIp) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700573
Charles Chan319d1a22015-11-03 10:42:14 -0800574 int segmentId;
575 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800576 if (routerIp.isIp4()) {
577 segmentId = config.getIPv4SegmentId(destSwId);
578 } else {
579 segmentId = config.getIPv6SegmentId(destSwId);
580 }
Charles Chan319d1a22015-11-03 10:42:14 -0800581 } catch (DeviceConfigNotFoundException e) {
582 log.warn(e.getMessage() + " Aborting populateMplsRule.");
583 return false;
584 }
sangho80f11cb2015-04-01 13:05:26 -0700585
Pier Ventre229fd0b2016-10-31 16:49:19 -0700586 List<ForwardingObjective> fwdObjs = new ArrayList<>();
Charles Chan3ed34d82017-06-22 18:03:14 -0700587 Collection<ForwardingObjective> fwdObjsMpls;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700588 // Generates the transit rules used by the standard "routing".
589 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, segmentId, routerIp, true);
590 if (fwdObjsMpls.isEmpty()) {
591 return false;
sangho80f11cb2015-04-01 13:05:26 -0700592 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700593 fwdObjs.addAll(fwdObjsMpls);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700594
595 // Generates the transit rules used by the MPLS Pwaas.
596 int pwSrLabel;
597 try {
598 pwSrLabel = config.getPWRoutingLabel(destSwId);
599 } catch (DeviceConfigNotFoundException e) {
600 log.warn(e.getMessage() + " Aborting populateMplsRule. No label for PseudoWire traffic.");
601 return false;
602 }
603 fwdObjsMpls = handleMpls(targetSwId, destSwId, nextHops, pwSrLabel, routerIp, false);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700604 if (fwdObjsMpls.isEmpty()) {
605 return false;
606 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700607 fwdObjs.addAll(fwdObjsMpls);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700608
609 for (ForwardingObjective fwdObj : fwdObjs) {
Saurav Dase0237a32016-05-27 13:54:07 -0700610 log.debug("Sending MPLS fwd obj {} for SID {}-> next {} in sw: {}",
Pier Ventre229fd0b2016-10-31 16:49:19 -0700611 fwdObj.id(), segmentId, fwdObj.nextId(), targetSwId);
612 srManager.flowObjectiveService.forward(targetSwId, fwdObj);
sanghofb7c7292015-04-13 15:15:58 -0700613 rulePopulationCounter.incrementAndGet();
sangho80f11cb2015-04-01 13:05:26 -0700614 }
615
616 return true;
617 }
618
Saurav Das4c35fc42015-11-20 15:27:53 -0800619 private ForwardingObjective.Builder getMplsForwardingObjective(
Saurav Das261c3002017-06-13 15:35:54 -0700620 DeviceId targetSw,
Saurav Das4c35fc42015-11-20 15:27:53 -0800621 Set<DeviceId> nextHops,
622 boolean phpRequired,
623 boolean isBos,
Pier Ventreadb4ae62016-11-23 09:57:42 -0800624 TrafficSelector meta,
Saurav Das62ae6792017-05-15 15:34:25 -0700625 IpAddress routerIp,
626 DeviceId destSw) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800627
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700628 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
629 .builder().withFlag(ForwardingObjective.Flag.SPECIFIC);
sangho80f11cb2015-04-01 13:05:26 -0700630
631 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
632
633 if (phpRequired) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800634 // php case - pop should always be flow-action
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700635 log.debug("getMplsForwardingObjective: php required");
sangho27462c62015-05-14 00:39:53 -0700636 tbuilder.deferred().copyTtlIn();
sangho80f11cb2015-04-01 13:05:26 -0700637 if (isBos) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800638 if (routerIp.isIp4()) {
639 tbuilder.deferred().popMpls(EthType.EtherType.IPV4.ethType());
640 } else {
641 tbuilder.deferred().popMpls(EthType.EtherType.IPV6.ethType());
642 }
643 tbuilder.decNwTtl();
sangho80f11cb2015-04-01 13:05:26 -0700644 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800645 tbuilder.deferred().popMpls(EthType.EtherType.MPLS_UNICAST.ethType())
646 .decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700647 }
648 } else {
Saurav Das4c35fc42015-11-20 15:27:53 -0800649 // swap with self case - SR CONTINUE
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700650 log.debug("getMplsForwardingObjective: php not required");
sangho27462c62015-05-14 00:39:53 -0700651 tbuilder.deferred().decMplsTtl();
sangho80f11cb2015-04-01 13:05:26 -0700652 }
653
Saurav Das4c35fc42015-11-20 15:27:53 -0800654 fwdBuilder.withTreatment(tbuilder.build());
Pier Ventre229fd0b2016-10-31 16:49:19 -0700655 // if MPLS-ECMP == True we will build a standard NeighborSet.
656 // Otherwise a RandomNeighborSet.
Saurav Das261c3002017-06-13 15:35:54 -0700657 DestinationSet ns = DestinationSet.destinationSet(false, false, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700658 if (!isBos && this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700659 ns = DestinationSet.destinationSet(false, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700660 } else if (!isBos && !this.srManager.getMplsEcmp()) {
Saurav Das261c3002017-06-13 15:35:54 -0700661 ns = DestinationSet.destinationSet(true, true, destSw);
Pier Ventre229fd0b2016-10-31 16:49:19 -0700662 }
Saurav Das261c3002017-06-13 15:35:54 -0700663
Saurav Das9455d702017-03-24 19:03:58 -0700664 log.debug("Trying to get a nextObjId for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700665 targetSw, ns);
666 DefaultGroupHandler gh = srManager.getGroupHandler(targetSw);
667 if (gh == null) {
668 log.warn("getNextObjectiveId query - groupHandler for device {} "
669 + "not found", targetSw);
670 return null;
671 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700672 // If BoS == True, all forwarding is via L3 ECMP group.
673 // If Bos == False, the forwarding can be via MPLS-ECMP group or through
674 // MPLS-Interface group. This depends on the configuration of the option
675 // MPLS-ECMP.
676 // The metadata informs the driver that the next-Objective will be used
677 // by MPLS flows and if Bos == False the driver will use MPLS groups.
Saurav Das261c3002017-06-13 15:35:54 -0700678 Map<DeviceId, Set<DeviceId>> dstNextHops = new HashMap<>();
679 dstNextHops.put(destSw, nextHops);
680 int nextId = gh.getNextObjectiveId(ns, dstNextHops, meta, isBos);
Saurav Das4c35fc42015-11-20 15:27:53 -0800681 if (nextId <= 0) {
Saurav Das261c3002017-06-13 15:35:54 -0700682 log.warn("No next objective in {} for ns: {}", targetSw, ns);
Saurav Das4c35fc42015-11-20 15:27:53 -0800683 return null;
Saurav Dase0237a32016-05-27 13:54:07 -0700684 } else {
685 log.debug("nextObjId found:{} for mpls rule on device:{} to ns:{}",
Saurav Das261c3002017-06-13 15:35:54 -0700686 nextId, targetSw, ns);
sangho80f11cb2015-04-01 13:05:26 -0700687 }
688
Saurav Das4c35fc42015-11-20 15:27:53 -0800689 fwdBuilder.nextStep(nextId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700690 return fwdBuilder;
sangho80f11cb2015-04-01 13:05:26 -0700691 }
692
693 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700694 * Creates a filtering objective to permit all untagged packets with a
Saurav Das7c305372015-10-28 12:39:42 -0700695 * dstMac corresponding to the router's MAC address. For those pipelines
696 * that need to internally assign vlans to untagged packets, this method
697 * provides per-subnet vlan-ids as metadata.
Saurav Dasc28b3432015-10-30 17:45:38 -0700698 * <p>
Saurav Dasf9332192017-02-18 14:05:44 -0800699 * Note that the vlan assignment and filter programming should only be done by
700 * the master for a switch. This method is typically called at deviceAdd and
701 * programs filters only for the enabled ports of the device. For port-updates,
702 * that enable/disable ports after device add, singlePortFilter methods should
703 * be called.
sangho80f11cb2015-04-01 13:05:26 -0700704 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700705 * @param deviceId the switch dpid for the router
Saurav Dasd1872b02016-12-02 15:43:47 -0800706 * @return PortFilterInfo information about the processed ports
sangho80f11cb2015-04-01 13:05:26 -0700707 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700708 PortFilterInfo populateVlanMacFilters(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700709 log.debug("Installing per-port filtering objective for untagged "
710 + "packets in device {}", deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800711
Saurav Das07c74602016-04-27 18:35:50 -0700712 List<Port> devPorts = srManager.deviceService.getPorts(deviceId);
Jon Hall31d84782017-01-18 20:15:44 -0800713 if (devPorts == null || devPorts.isEmpty()) {
Saurav Das07c74602016-04-27 18:35:50 -0700714 log.warn("Device {} ports not available. Unable to add MacVlan filters",
715 deviceId);
Saurav Dasd1872b02016-12-02 15:43:47 -0800716 return null;
Saurav Das07c74602016-04-27 18:35:50 -0700717 }
Saurav Dasf9332192017-02-18 14:05:44 -0800718 int disabledPorts = 0, errorPorts = 0, filteredPorts = 0;
Saurav Das07c74602016-04-27 18:35:50 -0700719 for (Port port : devPorts) {
Saurav Dase0237a32016-05-27 13:54:07 -0700720 if (!port.isEnabled()) {
721 disabledPorts++;
722 continue;
723 }
Charles Chan43be46b2017-02-26 22:59:35 -0800724 if (processSinglePortFilters(deviceId, port.number(), true)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800725 filteredPorts++;
726 } else {
727 errorPorts++;
Saurav Dase0237a32016-05-27 13:54:07 -0700728 }
Saurav Das7c305372015-10-28 12:39:42 -0700729 }
Charles Chan077314e2017-06-22 14:27:17 -0700730 log.debug("Filtering on dev:{}, disabledPorts:{}, errorPorts:{}, filteredPorts:{}",
Saurav Dasf9332192017-02-18 14:05:44 -0800731 deviceId, disabledPorts, errorPorts, filteredPorts);
Saurav Dasd1872b02016-12-02 15:43:47 -0800732 return srManager.defaultRoutingHandler.new PortFilterInfo(disabledPorts,
Saurav Dasf9332192017-02-18 14:05:44 -0800733 errorPorts, filteredPorts);
734 }
735
736 /**
Charles Chan43be46b2017-02-26 22:59:35 -0800737 * Creates or removes filtering objectives for a single port. Should only be
738 * called by the master for a switch.
Saurav Dasf9332192017-02-18 14:05:44 -0800739 *
740 * @param deviceId device identifier
741 * @param portnum port identifier for port to be filtered
Charles Chan43be46b2017-02-26 22:59:35 -0800742 * @param install true to install the filtering objective, false to remove
Saurav Dasf9332192017-02-18 14:05:44 -0800743 * @return true if no errors occurred during the build of the filtering objective
744 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700745 boolean processSinglePortFilters(DeviceId deviceId, PortNumber portnum, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800746 ConnectPoint connectPoint = new ConnectPoint(deviceId, portnum);
747 VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint);
748 Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint);
749 VlanId nativeVlan = srManager.getNativeVlanId(connectPoint);
750
751 if (taggedVlans.size() != 0) {
752 // Filter for tagged vlans
753 if (!srManager.getTaggedVlanId(connectPoint).stream().allMatch(taggedVlanId ->
Charles Chan43be46b2017-02-26 22:59:35 -0800754 processSinglePortFiltersInternal(deviceId, portnum, false, taggedVlanId, install))) {
Charles Chan90772a72017-02-08 15:52:08 -0800755 return false;
756 }
757 if (nativeVlan != null) {
758 // Filter for native vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800759 if (!processSinglePortFiltersInternal(deviceId, portnum, true, nativeVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800760 return false;
761 }
762 }
763 } else if (untaggedVlan != null) {
764 // Filter for untagged vlan
Charles Chan43be46b2017-02-26 22:59:35 -0800765 if (!processSinglePortFiltersInternal(deviceId, portnum, true, untaggedVlan, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800766 return false;
767 }
768 } else {
Saurav Dasfbe74572017-08-03 18:30:35 -0700769 // Unconfigured port, use INTERNAL_VLAN
Charles Chan43be46b2017-02-26 22:59:35 -0800770 if (!processSinglePortFiltersInternal(deviceId, portnum, true, INTERNAL_VLAN, install)) {
Charles Chan90772a72017-02-08 15:52:08 -0800771 return false;
772 }
773 }
774 return true;
775 }
776
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700777 /**
778 * Updates filtering objectives for a single port. Should only be called by
779 * the master for a switch
780 * @param deviceId device identifier
781 * @param portNum port identifier for port to be filtered
782 * @param pushVlan true to push vlan, false otherwise
783 * @param vlanId vlan identifier
784 * @param install true to install the filtering objective, false to remove
785 */
786 void updateSinglePortFilters(DeviceId deviceId, PortNumber portNum,
787 boolean pushVlan, VlanId vlanId, boolean install) {
788 if (!processSinglePortFiltersInternal(deviceId, portNum, pushVlan, vlanId, install)) {
789 log.warn("Failed to update FilteringObjective for {}/{} with vlan {}",
790 deviceId, portNum, vlanId);
791 }
792 }
793
Charles Chan43be46b2017-02-26 22:59:35 -0800794 private boolean processSinglePortFiltersInternal(DeviceId deviceId, PortNumber portnum,
795 boolean pushVlan, VlanId vlanId, boolean install) {
Charles Chan90772a72017-02-08 15:52:08 -0800796 FilteringObjective.Builder fob = buildFilteringObjective(deviceId, portnum, pushVlan, vlanId);
Saurav Dasf9332192017-02-18 14:05:44 -0800797 if (fob == null) {
798 // error encountered during build
799 return false;
800 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700801 log.debug("{} filtering objectives for dev/port: {}/{}",
Saurav Das62ae6792017-05-15 15:34:25 -0700802 install ? "Installing" : "Removing", deviceId, portnum);
Saurav Dasf9332192017-02-18 14:05:44 -0800803 ObjectiveContext context = new DefaultObjectiveContext(
Charles Chan43be46b2017-02-26 22:59:35 -0800804 (objective) -> log.debug("Filter for {}/{} {}", deviceId, portnum,
Saurav Das62ae6792017-05-15 15:34:25 -0700805 install ? "installed" : "removed"),
Charles Chan43be46b2017-02-26 22:59:35 -0800806 (objective, error) -> log.warn("Failed to {} filter for {}/{}: {}",
Saurav Das62ae6792017-05-15 15:34:25 -0700807 install ? "install" : "remove", deviceId, portnum, error));
Charles Chan43be46b2017-02-26 22:59:35 -0800808 if (install) {
809 srManager.flowObjectiveService.filter(deviceId, fob.add(context));
810 } else {
811 srManager.flowObjectiveService.filter(deviceId, fob.remove(context));
Charles Chan90772a72017-02-08 15:52:08 -0800812 }
Charles Chan90772a72017-02-08 15:52:08 -0800813 return true;
Saurav Dasf9332192017-02-18 14:05:44 -0800814 }
815
Charles Chan90772a72017-02-08 15:52:08 -0800816 private FilteringObjective.Builder buildFilteringObjective(DeviceId deviceId, PortNumber portnum,
817 boolean pushVlan, VlanId vlanId) {
Saurav Dasf9332192017-02-18 14:05:44 -0800818 MacAddress deviceMac;
819 try {
820 deviceMac = config.getDeviceMac(deviceId);
821 } catch (DeviceConfigNotFoundException e) {
822 log.warn(e.getMessage() + " Processing SinglePortFilters aborted");
823 return null;
824 }
Saurav Dasf9332192017-02-18 14:05:44 -0800825 FilteringObjective.Builder fob = DefaultFilteringObjective.builder();
826 fob.withKey(Criteria.matchInPort(portnum))
827 .addCondition(Criteria.matchEthDst(deviceMac))
Saurav Dasf9332192017-02-18 14:05:44 -0800828 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
Charles Chan90772a72017-02-08 15:52:08 -0800829
Charles Chan17ca2202017-12-19 19:55:57 -0800830 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
831
Charles Chan90772a72017-02-08 15:52:08 -0800832 if (pushVlan) {
833 fob.addCondition(Criteria.matchVlanId(VlanId.NONE));
Charles Chan17ca2202017-12-19 19:55:57 -0800834 tBuilder.pushVlan().setVlanId(vlanId);
Charles Chan90772a72017-02-08 15:52:08 -0800835 } else {
836 fob.addCondition(Criteria.matchVlanId(vlanId));
837 }
838
Charles Chan17ca2202017-12-19 19:55:57 -0800839 // NOTE: Some switch hardware share the same filtering flow among different ports.
840 // We use this metadata to let the driver know that there is no more enabled port
841 // within the same VLAN on this device.
842 boolean noMoreEnabledPort = srManager.interfaceService.getInterfaces().stream()
843 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
844 .filter(intf -> intf.vlanTagged().contains(vlanId) ||
845 intf.vlanUntagged().equals(vlanId) ||
846 intf.vlanNative().equals(vlanId))
847 .noneMatch(intf -> {
848 Port port = srManager.deviceService.getPort(intf.connectPoint());
849 return port != null && port.isEnabled();
850 });
851 if (noMoreEnabledPort) {
852 tBuilder.wipeDeferred();
853 }
854
855 fob.withMeta(tBuilder.build());
856
Saurav Dasf9332192017-02-18 14:05:44 -0800857 fob.permit().fromApp(srManager.appId);
858 return fob;
sangho80f11cb2015-04-01 13:05:26 -0700859 }
860
861 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700862 * Creates a forwarding objective to punt all IP packets, destined to the
Saurav Dasc28b3432015-10-30 17:45:38 -0700863 * router's port IP addresses, to the controller. Note that the input
Saurav Das9f1c42e2015-10-23 10:51:11 -0700864 * port should not be matched on, as these packets can come from any input.
Saurav Dasc28b3432015-10-30 17:45:38 -0700865 * Furthermore, these are applied only by the master instance.
sangho80f11cb2015-04-01 13:05:26 -0700866 *
Saurav Das9f1c42e2015-10-23 10:51:11 -0700867 * @param deviceId the switch dpid for the router
sangho80f11cb2015-04-01 13:05:26 -0700868 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700869 void populateIpPunts(DeviceId deviceId) {
Saurav Das261c3002017-06-13 15:35:54 -0700870 Ip4Address routerIpv4, pairRouterIpv4 = null;
Charles Chanef8d12e2017-12-05 21:07:38 -0800871 Ip6Address routerIpv6, routerLinkLocalIpv6, pairRouterIpv6 = null;
Charles Chan319d1a22015-11-03 10:42:14 -0800872 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800873 routerIpv4 = config.getRouterIpv4(deviceId);
874 routerIpv6 = config.getRouterIpv6(deviceId);
Charles Chanef8d12e2017-12-05 21:07:38 -0800875 routerLinkLocalIpv6 = Ip6Address.valueOf(
876 IPv6.getLinkLocalAddress(config.getDeviceMac(deviceId).toBytes()));
877
Saurav Das261c3002017-06-13 15:35:54 -0700878 if (config.isPairedEdge(deviceId)) {
879 pairRouterIpv4 = config.getRouterIpv4(config.getPairDeviceId(deviceId));
880 pairRouterIpv6 = config.getRouterIpv6(config.getPairDeviceId(deviceId));
881 }
Charles Chan319d1a22015-11-03 10:42:14 -0800882 } catch (DeviceConfigNotFoundException e) {
Charles Chan18fa4252017-02-08 16:10:40 -0800883 log.warn(e.getMessage() + " Aborting populateIpPunts.");
Charles Chan319d1a22015-11-03 10:42:14 -0800884 return;
885 }
886
Saurav Dasc28b3432015-10-30 17:45:38 -0700887 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
888 log.debug("Not installing port-IP punts - not the master for dev:{} ",
889 deviceId);
890 return;
891 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800892 Set<IpAddress> allIps = new HashSet<>(config.getPortIPs(deviceId));
893 allIps.add(routerIpv4);
Charles Chanef8d12e2017-12-05 21:07:38 -0800894 allIps.add(routerLinkLocalIpv6);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800895 if (routerIpv6 != null) {
896 allIps.add(routerIpv6);
897 }
Saurav Das261c3002017-06-13 15:35:54 -0700898 if (pairRouterIpv4 != null) {
899 allIps.add(pairRouterIpv4);
900 }
901 if (pairRouterIpv6 != null) {
902 allIps.add(pairRouterIpv6);
903 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800904 for (IpAddress ipaddr : allIps) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700905 populateSingleIpPunts(deviceId, ipaddr);
906 }
907 }
Charles Chan2d0bbcd2017-01-09 11:45:08 -0800908
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700909 /**
910 * Creates a forwarding objective to punt all IP packets, destined to the
911 * specified IP address, which should be router's port IP address.
912 *
913 * @param deviceId the switch dpid for the router
914 * @param ipAddress the IP address of the router's port
915 */
916 void populateSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
917 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
918 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
919
920 srManager.packetService.requestPackets(sbuilder.build(),
921 PacketPriority.CONTROL, srManager.appId, optDeviceId);
922 }
923
924 /**
925 * Removes a forwarding objective to punt all IP packets, destined to the
926 * specified IP address, which should be router's port IP address.
927 *
928 * @param deviceId the switch dpid for the router
929 * @param ipAddress the IP address of the router's port
930 */
931 void revokeSingleIpPunts(DeviceId deviceId, IpAddress ipAddress) {
932 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpAddress(ipAddress);
933 Optional<DeviceId> optDeviceId = Optional.of(deviceId);
934
935 try {
936 if (!ipAddress.equals(config.getRouterIpv4(deviceId)) &&
937 !ipAddress.equals(config.getRouterIpv6(deviceId))) {
938 srManager.packetService.cancelPackets(sbuilder.build(),
939 PacketPriority.CONTROL, srManager.appId, optDeviceId);
940 }
941 } catch (DeviceConfigNotFoundException e) {
942 log.warn(e.getMessage() + " Aborting revokeSingleIpPunts");
Saurav Das9f1c42e2015-10-23 10:51:11 -0700943 }
sangho80f11cb2015-04-01 13:05:26 -0700944 }
945
Charles Chanf4586112015-11-09 16:37:23 -0800946 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800947 * Method to build IPv4 or IPv6 selector.
948 *
949 * @param addressToMatch the address to match
950 */
951 private TrafficSelector.Builder buildIpSelectorFromIpAddress(IpAddress addressToMatch) {
952 return buildIpSelectorFromIpPrefix(addressToMatch.toIpPrefix());
953 }
954
955 /**
956 * Method to build IPv4 or IPv6 selector.
957 *
958 * @param prefixToMatch the prefix to match
959 */
960 private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
961 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Pier Ventre229fd0b2016-10-31 16:49:19 -0700962 // If the prefix is IPv4
Pier Ventreadb4ae62016-11-23 09:57:42 -0800963 if (prefixToMatch.isIp4()) {
964 selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
965 selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
966 return selectorBuilder;
967 }
Pier Ventre229fd0b2016-10-31 16:49:19 -0700968 // If the prefix is IPv6
Pier Ventreadb4ae62016-11-23 09:57:42 -0800969 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
970 selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
971 return selectorBuilder;
972 }
973
974 /**
Pier Luigib9632ba2017-01-12 18:14:58 -0800975 * Creates forwarding objectives to punt ARP and NDP packets, to the controller.
976 * Furthermore, these are applied only by the master instance. Deferred actions
977 * are not cleared such that packets can be flooded in the cross connect use case
978 *
979 * @param deviceId the switch dpid for the router
980 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700981 void populateArpNdpPunts(DeviceId deviceId) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700982 // We are not the master just skip.
Pier Luigib9632ba2017-01-12 18:14:58 -0800983 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
984 log.debug("Not installing ARP/NDP punts - not the master for dev:{} ",
985 deviceId);
986 return;
987 }
988
Charles Chan3ed34d82017-06-22 18:03:14 -0700989 ForwardingObjective fwdObj;
Pier Luigib9632ba2017-01-12 18:14:58 -0800990 // We punt all ARP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -0800991 fwdObj = arpFwdObjective(null, true, ARP_NDP_PRIORITY)
Pier Luigib9632ba2017-01-12 18:14:58 -0800992 .add(new ObjectiveContext() {
993 @Override
994 public void onError(Objective objective, ObjectiveError error) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700995 log.warn("Failed to install forwarding objective to punt ARP to {}: {}",
Pier Luigib9632ba2017-01-12 18:14:58 -0800996 deviceId, error);
997 }
998 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700999 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Pier Luigib9632ba2017-01-12 18:14:58 -08001000
Pier Luigib9632ba2017-01-12 18:14:58 -08001001 // We punt all NDP packets towards the controller.
Charles Chanef8d12e2017-12-05 21:07:38 -08001002 fwdObj = ndpFwdObjective(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 NDP 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);
1011
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
1026 pairFwdObj = ndpFwdObjective(port, false, PacketPriority.CONTROL.priorityValue() + 1)
1027 .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, pairFwdObj);
1035
1036 // Do not forward DAD packets from pair port
1037 pairFwdObj = dad6FwdObjective(port, PacketPriority.CONTROL.priorityValue() + 2)
1038 .add(new ObjectiveContext() {
1039 @Override
1040 public void onError(Objective objective, ObjectiveError error) {
1041 log.warn("Failed to install forwarding objective to drop DAD to {}: {}",
1042 deviceId, error);
1043 }
1044 });
1045 srManager.flowObjectiveService.forward(deviceId, pairFwdObj);
1046 });
Pier Luigib9632ba2017-01-12 18:14:58 -08001047 }
1048
Charles Chan3ed34d82017-06-22 18:03:14 -07001049 private ForwardingObjective.Builder fwdObjBuilder(TrafficSelector selector,
1050 TrafficTreatment treatment, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001051 return DefaultForwardingObjective.builder()
Charles Chan3ed34d82017-06-22 18:03:14 -07001052 .withPriority(priority)
Pier Luigib9632ba2017-01-12 18:14:58 -08001053 .withSelector(selector)
1054 .fromApp(srManager.appId)
1055 .withFlag(ForwardingObjective.Flag.VERSATILE)
Charles Chan3ed34d82017-06-22 18:03:14 -07001056 .withTreatment(treatment)
Pier Luigib9632ba2017-01-12 18:14:58 -08001057 .makePermanent();
1058 }
1059
Charles Chan3ed34d82017-06-22 18:03:14 -07001060 private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001061 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1062 sBuilder.matchEthType(TYPE_ARP);
Charles Chan3ed34d82017-06-22 18:03:14 -07001063 if (port != null) {
1064 sBuilder.matchInPort(port);
1065 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001066
Charles Chan3ed34d82017-06-22 18:03:14 -07001067 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1068 if (punt) {
1069 tBuilder.punt();
1070 }
1071 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001072 }
1073
Charles Chan3ed34d82017-06-22 18:03:14 -07001074 private ForwardingObjective.Builder ndpFwdObjective(PortNumber port, boolean punt, int priority) {
Pier Luigib9632ba2017-01-12 18:14:58 -08001075 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1076 sBuilder.matchEthType(TYPE_IPV6)
1077 .matchIPProtocol(PROTOCOL_ICMP6)
Charles Chan3ed34d82017-06-22 18:03:14 -07001078 .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
1079 if (port != null) {
1080 sBuilder.matchInPort(port);
1081 }
Pier Luigib9632ba2017-01-12 18:14:58 -08001082
Charles Chan3ed34d82017-06-22 18:03:14 -07001083 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1084 if (punt) {
1085 tBuilder.punt();
1086 }
1087 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
1088 }
1089
1090 private ForwardingObjective.Builder dad6FwdObjective(PortNumber port, int priority) {
1091 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
1092 sBuilder.matchEthType(TYPE_IPV6)
Charles Chane6bda752017-08-07 12:39:03 -07001093 .matchIPv6Src(Ip6Address.ZERO.toIpPrefix());
1094 // TODO CORD-1672 Fix this when OFDPA can distinguish ::/0 and ::/128 correctly
1095 // .matchIPProtocol(PROTOCOL_ICMP6)
1096 // .matchIcmpv6Type(NEIGHBOR_SOLICITATION);
Charles Chan3ed34d82017-06-22 18:03:14 -07001097 if (port != null) {
1098 sBuilder.matchInPort(port);
1099 }
1100
1101 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1102 tBuilder.wipeDeferred();
1103 return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
Pier Luigib9632ba2017-01-12 18:14:58 -08001104 }
1105
1106 /**
Charles Chanf4586112015-11-09 16:37:23 -08001107 * Populates a forwarding objective to send packets that miss other high
1108 * priority Bridging Table entries to a group that contains all ports of
1109 * its subnet.
1110 *
Charles Chanf4586112015-11-09 16:37:23 -08001111 * @param deviceId switch ID to set the rules
1112 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001113 void populateSubnetBroadcastRule(DeviceId deviceId) {
Charles Chan90772a72017-02-08 15:52:08 -08001114 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001115 updateSubnetBroadcastRule(deviceId, vlanId, true);
Charles Chanf4586112015-11-09 16:37:23 -08001116 });
1117 }
1118
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001119 /**
1120 * Creates or removes a forwarding objective to broadcast packets to its subnet.
1121 * @param deviceId switch ID to set the rule
1122 * @param vlanId vlan ID to specify the subnet
1123 * @param install true to install the rule, false to revoke the rule
1124 */
1125 void updateSubnetBroadcastRule(DeviceId deviceId, VlanId vlanId, boolean install) {
1126 int nextId = srManager.getVlanNextObjectiveId(deviceId, vlanId);
1127
1128 if (nextId < 0) {
1129 log.error("Cannot install vlan {} broadcast rule in dev:{} due"
1130 + " to vlanId:{} or nextId:{}", vlanId, deviceId, vlanId, nextId);
1131 return;
1132 }
1133
1134 // Driver should treat objective with MacAddress.NONE as the
1135 // subnet broadcast rule
1136 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1137 sbuilder.matchVlanId(vlanId);
1138 sbuilder.matchEthDst(MacAddress.NONE);
1139
1140 ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
1141 fob.withFlag(Flag.SPECIFIC)
1142 .withSelector(sbuilder.build())
1143 .nextStep(nextId)
1144 .withPriority(SegmentRoutingService.FLOOD_PRIORITY)
1145 .fromApp(srManager.appId)
1146 .makePermanent();
1147 ObjectiveContext context = new DefaultObjectiveContext(
1148 (objective) -> log.debug("Vlan broadcast rule for {} populated", vlanId),
1149 (objective, error) ->
1150 log.warn("Failed to populate vlan broadcast rule for {}: {}", vlanId, error));
1151
1152 if (install) {
1153 srManager.flowObjectiveService.forward(deviceId, fob.add(context));
1154 } else {
1155 srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
1156 }
1157 }
1158
Charles Chan82ab1932016-01-30 23:22:37 -08001159 private int getPriorityFromPrefix(IpPrefix prefix) {
1160 return (prefix.isIp4()) ?
1161 2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
1162 500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001163 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001164
1165 /**
1166 * Update Forwarding objective for each host and IP address connected to given port.
1167 * And create corresponding Simple Next objective if it does not exist.
1168 * Applied only when populating Forwarding objective
1169 * @param deviceId switch ID to set the rule
1170 * @param portNumber port number
1171 * @param prefix IP prefix of the route
1172 * @param hostMac MAC address of the next hop
1173 * @param vlanId Vlan ID of the port
1174 * @param popVlan true to pop vlan tag in TrafficTreatment
1175 * @param install true to populate the forwarding objective, false to revoke
1176 */
1177 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1178 VlanId vlanId, boolean popVlan, boolean install) {
1179 ForwardingObjective.Builder fob;
1180 TrafficSelector.Builder sbuilder = buildIpSelectorFromIpPrefix(prefix);
1181 MacAddress deviceMac;
1182 try {
1183 deviceMac = config.getDeviceMac(deviceId);
1184 } catch (DeviceConfigNotFoundException e) {
1185 log.warn(e.getMessage() + " Aborting updateFwdObj.");
1186 return;
1187 }
1188
1189 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1190 tbuilder.deferred()
1191 .setEthDst(hostMac)
1192 .setEthSrc(deviceMac)
1193 .setOutput(portNumber);
1194
1195 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1196
1197 if (!popVlan) {
1198 tbuilder.setVlanId(vlanId);
1199 } else {
1200 mbuilder.matchVlanId(vlanId);
1201 }
1202
1203 // if the objective is to revoke an existing rule, and for some reason
1204 // the next-objective does not exist, then a new one should not be created
1205 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNumber,
1206 tbuilder.build(), mbuilder.build(), install);
1207 if (portNextObjId == -1) {
1208 // Warning log will come from getPortNextObjective method
1209 return;
1210 }
1211
1212 fob = DefaultForwardingObjective.builder().withSelector(sbuilder.build())
1213 .nextStep(portNextObjId).fromApp(srManager.appId).makePermanent()
1214 .withPriority(getPriorityFromPrefix(prefix)).withFlag(ForwardingObjective.Flag.SPECIFIC);
1215
1216 ObjectiveContext context = new DefaultObjectiveContext(
1217 (objective) -> log.debug("IP rule for route {} {}", prefix, install ? "installed" : "revoked"),
1218 (objective, error) ->
1219 log.warn("Failed to {} IP rule for route {}: {}",
1220 install ? "install" : "revoke", prefix, error));
1221 srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
1222
1223 if (!install) {
1224 DefaultGroupHandler grpHandler = srManager.getGroupHandler(deviceId);
1225 if (grpHandler == null) {
1226 log.warn("updateFwdObj: groupHandler for device {} not found", deviceId);
1227 } else {
1228 // Remove L3UG for the given port and host
1229 grpHandler.removeGroupFromPort(portNumber, tbuilder.build(), mbuilder.build());
1230 }
1231 }
1232 }
sangho80f11cb2015-04-01 13:05:26 -07001233}