blob: 7920e6701477c4c2d1c74e3f8e2820a668f6e140 [file] [log] [blame]
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -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.grouphandler;
17
Saurav Dasfe0b05e2017-08-14 16:44:43 -070018import com.google.common.collect.ImmutableMap;
Pier Ventre229fd0b2016-10-31 16:49:19 -070019import com.google.common.collect.Iterables;
Saurav Dasfe0b05e2017-08-14 16:44:43 -070020import com.google.common.collect.Lists;
Saurav Das62ae6792017-05-15 15:34:25 -070021import com.google.common.collect.Sets;
22
Pier Ventre229fd0b2016-10-31 16:49:19 -070023import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070024import org.onlab.packet.MacAddress;
25import org.onlab.packet.MplsLabel;
Saurav Das62af8802015-12-04 10:52:59 -080026import org.onlab.packet.VlanId;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070027import org.onlab.util.KryoNamespace;
28import org.onosproject.core.ApplicationId;
Charles Chan10b0fb72017-02-02 16:20:42 -080029import org.onosproject.net.ConnectPoint;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
Saurav Das62af8802015-12-04 10:52:59 -080033import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070034import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das4c35fc42015-11-20 15:27:53 -080035import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070036import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chana4ee4f92016-04-23 14:48:16 -070038import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070039import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.NextObjective;
pierventrea3989be2021-01-08 16:43:17 +010041import org.onosproject.net.flowobjective.Objective;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070042import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070043import org.onosproject.net.link.LinkService;
Saurav Dasfbe74572017-08-03 18:30:35 -070044import org.onosproject.segmentrouting.DefaultRoutingHandler;
Saurav Das62af8802015-12-04 10:52:59 -080045import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan319d1a22015-11-03 10:42:14 -080046import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
47import org.onosproject.segmentrouting.config.DeviceProperties;
pierventre2b102f92020-09-08 16:45:36 +020048import org.onosproject.segmentrouting.DeviceConfiguration;
Saurav Das261c3002017-06-13 15:35:54 -070049import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070050import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080051import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000052import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070053import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070054import org.slf4j.Logger;
55
Pier Ventre229fd0b2016-10-31 16:49:19 -070056import java.net.URI;
Charles Chan90772a72017-02-08 15:52:08 -080057import java.util.Collection;
Pier Ventre229fd0b2016-10-31 16:49:19 -070058import java.util.Collections;
Saurav Das62ae6792017-05-15 15:34:25 -070059import java.util.HashMap;
Pier Ventre229fd0b2016-10-31 16:49:19 -070060import java.util.HashSet;
61import java.util.List;
62import java.util.Map;
63import java.util.Set;
pierventrea3989be2021-01-08 16:43:17 +010064import java.util.concurrent.CompletableFuture;
Pier Ventre229fd0b2016-10-31 16:49:19 -070065import java.util.concurrent.ConcurrentHashMap;
Saurav Das8a3022d2017-05-05 17:01:08 -070066import java.util.concurrent.ScheduledExecutorService;
67import java.util.concurrent.TimeUnit;
Pier Ventre229fd0b2016-10-31 16:49:19 -070068import java.util.stream.Collectors;
69
70import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das8a3022d2017-05-05 17:01:08 -070071import static java.util.concurrent.Executors.newScheduledThreadPool;
72import static org.onlab.util.Tools.groupedThreads;
Pier Ventre229fd0b2016-10-31 16:49:19 -070073import static org.slf4j.LoggerFactory.getLogger;
74
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070075/**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070076 * Default ECMP group handler creation module. This component creates a set of
77 * ECMP groups for every neighbor that this device is connected to based on
78 * whether the current device is an edge device or a transit device.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070079 */
80public class DefaultGroupHandler {
Ray Milkey5247fa92018-01-12 14:22:06 -080081 private static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070082
Saurav Dasfbe74572017-08-03 18:30:35 -070083 private static final long VERIFY_INTERVAL = 30; // secs
84
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070085 protected final DeviceId deviceId;
86 protected final ApplicationId appId;
87 protected final DeviceProperties deviceConfig;
88 protected final List<Integer> allSegmentIds;
Pier Ventreadb4ae62016-11-23 09:57:42 -080089 protected int ipv4NodeSegmentId = -1;
90 protected int ipv6NodeSegmentId = -1;
Charles Chan319d1a22015-11-03 10:42:14 -080091 protected boolean isEdgeRouter = false;
92 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070093 protected LinkService linkService;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070094 protected FlowObjectiveService flowObjectiveService;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000095 private DeviceConfiguration config;
96
Saurav Das62ae6792017-05-15 15:34:25 -070097 /**
98 * local store for neighbor-device-ids and the set of ports on this device
99 * that connect to the same neighbor.
100 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800101 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
102 new ConcurrentHashMap<>();
Saurav Das62ae6792017-05-15 15:34:25 -0700103 /**
104 * local store for ports on this device connected to neighbor-device-id.
105 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800106 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
107 new ConcurrentHashMap<>();
Saurav Das62ae6792017-05-15 15:34:25 -0700108
Saurav Das261c3002017-06-13 15:35:54 -0700109 // distributed store for (device+destination-set) mapped to next-id and neighbors
110 protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
111 dsNextObjStore = null;
Saurav Dasf0f592d2016-11-18 15:21:57 -0800112 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan10b0fb72017-02-02 16:20:42 -0800113 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
114 vlanNextObjStore = null;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000115 // distributed store for (device+mac+vlan+treatment) mapped to next-id
116 protected EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
117 macVlanNextObjStore = null;
Saurav Dasf0f592d2016-11-18 15:21:57 -0800118 // distributed store for (device+port+treatment) mapped to next-id
Charles Chanb7f75ac2016-01-11 18:28:54 -0800119 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
120 portNextObjStore = null;
Charles Chande6655c2015-12-23 00:15:11 -0800121 private SegmentRoutingManager srManager;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700122
Saurav Das8a3022d2017-05-05 17:01:08 -0700123 private ScheduledExecutorService executorService
Saurav Dasfbe74572017-08-03 18:30:35 -0700124 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das8a3022d2017-05-05 17:01:08 -0700125
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700126 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700127 .register(URI.class).register(HashSet.class)
Saurav Das261c3002017-06-13 15:35:54 -0700128 .register(DeviceId.class).register(PortNumber.class)
129 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700130 .register(PolicyGroupParams.class)
131 .register(GroupBucketIdentifier.class)
132 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700133
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700134 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
135 DeviceProperties config,
136 LinkService linkService,
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700137 FlowObjectiveService flowObjService,
Charles Chande6655c2015-12-23 00:15:11 -0800138 SegmentRoutingManager srManager) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700139 this.deviceId = checkNotNull(deviceId);
140 this.appId = checkNotNull(appId);
141 this.deviceConfig = checkNotNull(config);
142 this.linkService = checkNotNull(linkService);
Charles Chan319d1a22015-11-03 10:42:14 -0800143 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
144 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800145 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
146 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800147 this.isEdgeRouter = config.isEdgeDevice(deviceId);
148 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
149 } catch (DeviceConfigNotFoundException e) {
150 log.warn(e.getMessage()
151 + " Skipping value assignment in DefaultGroupHandler");
152 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700153 this.flowObjectiveService = flowObjService;
Saurav Das261c3002017-06-13 15:35:54 -0700154 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeyb85de082017-04-05 09:42:04 -0700155 this.vlanNextObjStore = srManager.vlanNextObjStore();
156 this.portNextObjStore = srManager.portNextObjStore();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000157 this.macVlanNextObjStore = srManager.macVlanNextObjStore();
Charles Chande6655c2015-12-23 00:15:11 -0800158 this.srManager = srManager;
Saurav Dasfbe74572017-08-03 18:30:35 -0700159 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
160 VERIFY_INTERVAL,
161 TimeUnit.SECONDS);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700162 populateNeighborMaps();
163 }
164
165 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700166 * Gracefully shuts down a groupHandler. Typically called when the handler is
167 * no longer needed.
168 */
169 public void shutdown() {
170 executorService.shutdown();
171 }
172
173 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700174 * Creates a group handler object.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700175 *
176 * @param deviceId device identifier
177 * @param appId application identifier
178 * @param config interface to retrieve the device properties
179 * @param linkService link service object
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700180 * @param flowObjService flow objective service object
Charles Chanb7f75ac2016-01-11 18:28:54 -0800181 * @param srManager segment routing manager
Charles Chan319d1a22015-11-03 10:42:14 -0800182 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700183 * @return default group handler type
184 */
Saurav Das2d94d312015-11-24 23:21:05 -0800185 public static DefaultGroupHandler createGroupHandler(
Saurav Dasfbe74572017-08-03 18:30:35 -0700186 DeviceId deviceId,
187 ApplicationId appId,
188 DeviceProperties config,
189 LinkService linkService,
190 FlowObjectiveService flowObjService,
191 SegmentRoutingManager srManager)
192 throws DeviceConfigNotFoundException {
Saurav Das62ae6792017-05-15 15:34:25 -0700193 return new DefaultGroupHandler(deviceId, appId, config,
194 linkService,
195 flowObjService,
196 srManager);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700197 }
198
199 /**
Saurav Das97241862018-02-14 14:14:54 -0800200 * Updates local stores for link-src-device/port to neighbor (link-dst) for
201 * link that has come up.
Saurav Das62ae6792017-05-15 15:34:25 -0700202 *
203 * @param link the infrastructure link
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700204 */
Saurav Das62ae6792017-05-15 15:34:25 -0700205 public void portUpForLink(Link link) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700206 if (!link.src().deviceId().equals(deviceId)) {
207 log.warn("linkUp: deviceId{} doesn't match with link src {}",
208 deviceId, link.src().deviceId());
209 return;
210 }
Saurav Das62ae6792017-05-15 15:34:25 -0700211
Saurav Dasfbe74572017-08-03 18:30:35 -0700212 log.info("* portUpForLink: Device {} linkUp at local port {} to "
213 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
214 // ensure local state is updated even if linkup is aborted later on
215 addNeighborAtPort(link.dst().deviceId(),
216 link.src().port());
217 }
Saurav Das62ae6792017-05-15 15:34:25 -0700218
Saurav Dasfbe74572017-08-03 18:30:35 -0700219 /**
Saurav Das97241862018-02-14 14:14:54 -0800220 * Updates local stores for link-src-device/port to neighbor (link-dst) for
221 * link that has gone down.
Saurav Dasfbe74572017-08-03 18:30:35 -0700222 *
Saurav Das97241862018-02-14 14:14:54 -0800223 * @param link the infrastructure link
Saurav Dasfbe74572017-08-03 18:30:35 -0700224 */
Saurav Das97241862018-02-14 14:14:54 -0800225 public void portDownForLink(Link link) {
226 PortNumber port = link.src().port();
Saurav Dasfbe74572017-08-03 18:30:35 -0700227 if (portDeviceMap.get(port) == null) {
228 log.warn("portDown: unknown port");
229 return;
230 }
Saurav Das62ae6792017-05-15 15:34:25 -0700231
Saurav Dasfbe74572017-08-03 18:30:35 -0700232 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
233 portDeviceMap.get(port));
234 devicePortMap.get(portDeviceMap.get(port)).remove(port);
235 portDeviceMap.remove(port);
236 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700237
238 /**
Saurav Das97241862018-02-14 14:14:54 -0800239 * Cleans up local stores for removed neighbor device.
240 *
241 * @param neighborId the device identifier for the neighbor device
242 */
243 public void cleanUpForNeighborDown(DeviceId neighborId) {
244 Set<PortNumber> ports = devicePortMap.remove(neighborId);
245 if (ports != null) {
246 ports.forEach(p -> portDeviceMap.remove(p));
247 }
248 }
249
250 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700251 * Checks all groups in the src-device of link for neighbor sets that include
252 * the dst-device of link, and edits the hash groups according to link up
253 * or down. Should only be called by the master instance of the src-switch
254 * of link. Typically used when there are no route-path changes due to the
255 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700256 *
Saurav Das62ae6792017-05-15 15:34:25 -0700257 * @param link the infrastructure link that has gone down or come up
258 * @param linkDown true if link has gone down
259 * @param firstTime true if link has come up for the first time i.e a link
260 * not seen-before
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700261 */
Saurav Das62ae6792017-05-15 15:34:25 -0700262 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700263 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -0800264 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700265 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan319d1a22015-11-03 10:42:14 -0800266 } catch (DeviceConfigNotFoundException e) {
Saurav Das62ae6792017-05-15 15:34:25 -0700267 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan319d1a22015-11-03 10:42:14 -0800268 return;
269 }
Saurav Das261c3002017-06-13 15:35:54 -0700270 // find all the destinationSets related to link
271 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700272 .stream()
Saurav Das261c3002017-06-13 15:35:54 -0700273 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigiecb09f42018-01-14 21:56:11 +0100274 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Das97241862018-02-14 14:14:54 -0800275 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
276 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
277 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
278 .filter(entry -> !entry.getKey().destinationSet().swap() ||
279 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Das261c3002017-06-13 15:35:54 -0700280 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
281 .map(entry -> entry.getKey())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700282 .collect(Collectors.toSet());
Saurav Das62ae6792017-05-15 15:34:25 -0700283
Saurav Das261c3002017-06-13 15:35:54 -0700284 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
285 deviceId, link.dst().deviceId(), dsKeySet);
286
287 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
288 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
289 if (nextHops == null) {
Saurav Das62ae6792017-05-15 15:34:25 -0700290 log.warn("retryHash in device {}, but global store has no record "
Saurav Das261c3002017-06-13 15:35:54 -0700291 + "for dsKey:{}", deviceId, dsKey);
Saurav Das62ae6792017-05-15 15:34:25 -0700292 continue;
293 }
Saurav Das261c3002017-06-13 15:35:54 -0700294 int nextId = nextHops.nextId();
295 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -0700296 if (!linkDown) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700297 List<PortLabel> pl = Lists.newArrayList();
Saurav Das62ae6792017-05-15 15:34:25 -0700298 if (firstTime) {
299 // some links may have come up before the next-objective was created
300 // we take this opportunity to ensure other ports to same next-hop-dst
301 // are part of the hash group (see CORD-1180). Duplicate additions
302 // to the same hash group are avoided by the driver.
303 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -0700304 dstSet.forEach(dst -> {
305 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chan223ae872018-08-22 17:56:47 -0700306 pl.add(new PortLabel(p, edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das261c3002017-06-13 15:35:54 -0700307 });
Saurav Das62ae6792017-05-15 15:34:25 -0700308 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700309 addToHashedNextObjective(pl, neighborMac, nextId);
310 } else {
311 // handle only the port that came up
312 dstSet.forEach(dst -> {
313 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chan223ae872018-08-22 17:56:47 -0700314 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700315 });
316 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das62ae6792017-05-15 15:34:25 -0700317 }
318 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700319 // linkdown
320 List<PortLabel> pl = Lists.newArrayList();
Saurav Das261c3002017-06-13 15:35:54 -0700321 dstSet.forEach(dst -> {
322 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chan223ae872018-08-22 17:56:47 -0700323 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das261c3002017-06-13 15:35:54 -0700324 });
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700325 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700326 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700327 }
Saurav Das8a3022d2017-05-05 17:01:08 -0700328 }
329
Saurav Das62ae6792017-05-15 15:34:25 -0700330 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700331 * Utility class for associating output ports and the corresponding MPLS
332 * labels to push. In dual-homing, there are different labels to push
333 * corresponding to the destination switches in an edge-pair. If both
334 * destinations are reachable via the same spine, then the output-port to
335 * the spine will be associated with two labels i.e. there will be two
336 * PortLabel objects for the same port but with different labels.
337 */
338 private class PortLabel {
339 PortNumber port;
340 int edgeLabel;
Charles Chan223ae872018-08-22 17:56:47 -0700341 boolean popVlan;
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700342
Charles Chan223ae872018-08-22 17:56:47 -0700343 PortLabel(PortNumber port, int edgeLabel, boolean popVlan) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700344 this.port = port;
345 this.edgeLabel = edgeLabel;
Charles Chan223ae872018-08-22 17:56:47 -0700346 this.popVlan = popVlan;
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700347 }
348
349 @Override
350 public String toString() {
Charles Chan223ae872018-08-22 17:56:47 -0700351 return port.toString() + "/" + String.valueOf(edgeLabel) + (popVlan ? "/popVlan" : "");
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700352 }
353 }
354
355 /**
356 * Makes a call to the FlowObjective service to add buckets to
357 * a hashed group. User must ensure that all the ports & labels are meant
358 * same neighbor (ie. dstMac).
Saurav Das62ae6792017-05-15 15:34:25 -0700359 *
Pier Luigiecb09f42018-01-14 21:56:11 +0100360 * @param portLabels a collection of port & label combinations to add
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700361 * to the hash group identified by the nextId
Saurav Das62ae6792017-05-15 15:34:25 -0700362 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700363 * @param nextId id for next-objective to which buckets will be added
Saurav Dasfbe74572017-08-03 18:30:35 -0700364 *
Saurav Das62ae6792017-05-15 15:34:25 -0700365 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700366 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
367 MacAddress dstMac, Integer nextId) {
Saurav Das8a3022d2017-05-05 17:01:08 -0700368 // setup metadata to pass to nextObjective - indicate the vlan on egress
369 // if needed by the switch pipeline. Since hashed next-hops are always to
370 // other neighboring routers, there is no subnet assigned on those ports.
371 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das9bf49582018-08-13 15:34:26 -0700372 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Das8a3022d2017-05-05 17:01:08 -0700373 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
374 .withId(nextId)
375 .withType(NextObjective.Type.HASHED)
Saurav Das8a3022d2017-05-05 17:01:08 -0700376 .withMeta(metabuilder.build())
377 .fromApp(appId);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700378 // Create the new buckets to be updated
379 portLabels.forEach(pl -> {
380 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
381 tBuilder.setOutput(pl.port)
382 .setEthDst(dstMac)
383 .setEthSrc(nodeMacAddr);
Charles Chan223ae872018-08-22 17:56:47 -0700384 if (pl.popVlan) {
385 tBuilder.popVlan();
386 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700387 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
388 tBuilder.pushMpls()
389 .copyTtlOut()
390 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
391 }
392 nextObjBuilder.addTreatment(tBuilder.build());
393 });
394
395 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
396 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das8a3022d2017-05-05 17:01:08 -0700397
398 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700399 (objective) -> log.debug("addToHash port/label {} addedTo "
400 + "NextObj {} on {}", portLabels, nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -0700401 (objective, error) -> {
402 log.warn("addToHash failed to add port/label {} to NextObj {} on {}: {}",
403 portLabels, nextId, deviceId, error);
404 srManager.invalidateNextObj(objective.id());
405 });
Saurav Das8a3022d2017-05-05 17:01:08 -0700406 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
407 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700408 }
409
410 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700411 * Makes a call to the FlowObjective service to remove buckets from
412 * a hash group. User must ensure that all the ports & labels are meant
413 * same neighbor (ie. dstMac).
Saurav Dasfbe74572017-08-03 18:30:35 -0700414 *
Pier Luigiecb09f42018-01-14 21:56:11 +0100415 * @param portLabels a collection of port & label combinations to remove
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700416 * from the hash group identified by the nextId
Saurav Dasfbe74572017-08-03 18:30:35 -0700417 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700418 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasfbe74572017-08-03 18:30:35 -0700419 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700420 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
421 MacAddress dstMac, Integer nextId) {
Charles Chan8471a8d2018-08-30 19:49:23 -0700422 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
423 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasfbe74572017-08-03 18:30:35 -0700424 NextObjective.Builder nextObjBuilder = DefaultNextObjective
425 .builder()
426 .withType(NextObjective.Type.HASHED) //same as original
Charles Chan8471a8d2018-08-30 19:49:23 -0700427 .withMeta(metabuilder.build())
Saurav Dasfbe74572017-08-03 18:30:35 -0700428 .withId(nextId)
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700429 .fromApp(appId);
430 // Create the buckets to be removed
431 portLabels.forEach(pl -> {
432 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
433 tBuilder.setOutput(pl.port)
434 .setEthDst(dstMac)
435 .setEthSrc(nodeMacAddr);
Charles Chan223ae872018-08-22 17:56:47 -0700436 if (pl.popVlan) {
437 tBuilder.popVlan();
438 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700439 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
440 tBuilder.pushMpls()
441 .copyTtlOut()
442 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
443 }
444 nextObjBuilder.addTreatment(tBuilder.build());
445 });
446 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
447 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Das62ae6792017-05-15 15:34:25 -0700448
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700449 ObjectiveContext context = new DefaultObjectiveContext(
450 (objective) -> log.debug("port/label {} removedFrom NextObj"
451 + " {} on {}", portLabels, nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -0700452 (objective, error) -> {
453 log.warn("port/label {} failed to removeFrom NextObj {} on {}: {}",
454 portLabels, nextId, deviceId, error);
455 srManager.invalidateNextObj(objective.id());
456 });
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700457 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasfbe74572017-08-03 18:30:35 -0700458 flowObjectiveService.next(deviceId, nextObjective);
459 }
Saurav Das62ae6792017-05-15 15:34:25 -0700460
461 /**
462 * Checks all the hash-groups in the target-switch meant for the destination
463 * switch, and either adds or removes buckets to make the neighbor-set
464 * match the given next-hops. Typically called by the master instance of the
465 * destination switch, which may be different from the master instance of the
466 * target switch where hash-group changes are made.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700467 *
Saurav Das62ae6792017-05-15 15:34:25 -0700468 * @param targetSw the switch in which the hash groups will be edited
469 * @param nextHops the current next hops for the target switch to reach
470 * the dest sw
471 * @param destSw the destination switch
472 * @param revoke true if hash groups need to remove buckets from the
473 * the groups to match the current next hops
474 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700475 */
Saurav Das62ae6792017-05-15 15:34:25 -0700476 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
477 DeviceId destSw, boolean revoke) {
478 // temporary storage of keys to be updated
Saurav Das261c3002017-06-13 15:35:54 -0700479 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Das62ae6792017-05-15 15:34:25 -0700480 new HashMap<>();
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700481 boolean foundNextObjective = false, success = true;
Charles Chan319d1a22015-11-03 10:42:14 -0800482
Saurav Das261c3002017-06-13 15:35:54 -0700483 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Das62ae6792017-05-15 15:34:25 -0700484 // with different neighbors than the given next-hops
Saurav Das261c3002017-06-13 15:35:54 -0700485 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
486 if (!dskey.deviceId().equals(targetSw) ||
487 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Das62ae6792017-05-15 15:34:25 -0700488 continue;
489 }
490 foundNextObjective = true;
Saurav Das261c3002017-06-13 15:35:54 -0700491 NextNeighbors nhops = dsNextObjStore.get(dskey);
492 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
493 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
494 Integer nextId = nhops.nextId();
Saurav Dascea556f2018-03-05 14:37:16 -0800495 if (currNeighbors == null || nextHops == null) {
496 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
497 + " in targetSw:{} for dstSw:{}", currNeighbors,
498 nextHops, targetSw, destSw);
499 success &= false;
500 continue;
501 }
Charles Chan319d1a22015-11-03 10:42:14 -0800502
Saurav Das97241862018-02-14 14:14:54 -0800503 // some store elements may not be hashed next-objectives - ignore them
504 if (isSimpleNextObjective(dskey)) {
505 log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}"
506 + " -> dstSw:{} with current nextHops:{} to new"
507 + " nextHops: {} in nextId:{}",
508 (revoke) ? "removal" : "addition", targetSw, destSw,
509 currNeighbors, nextHops, nextId);
Saurav Dascea556f2018-03-05 14:37:16 -0800510 if ((revoke && !nextHops.isEmpty())
511 || (!revoke && !nextHops.equals(currNeighbors))) {
Saurav Das68e1b6a2018-06-11 17:02:31 -0700512 log.debug("Simple next objective cannot be edited to "
Saurav Das97241862018-02-14 14:14:54 -0800513 + "move from {} to {}", currNeighbors, nextHops);
514 }
515 continue;
516 }
517
Saurav Das62ae6792017-05-15 15:34:25 -0700518 Set<DeviceId> diff;
519 if (revoke) {
520 diff = Sets.difference(currNeighbors, nextHops);
521 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
522 + "hops:{} ..removing {}", targetSw, destSw, nextId,
523 currNeighbors, diff);
524 } else {
525 diff = Sets.difference(nextHops, currNeighbors);
526 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
527 + "hops:{} ..adding {}", targetSw, destSw, nextId,
528 currNeighbors, diff);
529 }
Charles Chan223ae872018-08-22 17:56:47 -0700530 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, popVlanInHashGroup(dskey.destinationSet()),
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700531 revoke);
532 if (suc) {
533 // to update neighbor set with changes made
Saurav Das62ae6792017-05-15 15:34:25 -0700534 if (revoke) {
Saurav Das261c3002017-06-13 15:35:54 -0700535 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700536 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700537 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700538 }
sangho2165d222015-05-01 09:38:25 -0700539 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700540 success &= suc;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700541 }
542
Saurav Das62ae6792017-05-15 15:34:25 -0700543 if (!foundNextObjective) {
544 log.debug("Cannot find any nextObjectives for route targetSw:{} "
545 + "-> dstSw:{}", targetSw, destSw);
pier572d4a92019-04-25 18:51:51 +0200546 return false; // nothing to do, return false so re-route will be performed
Saurav Das62ae6792017-05-15 15:34:25 -0700547 }
548
Saurav Das261c3002017-06-13 15:35:54 -0700549 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
550 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700551 NextNeighbors currentNextHops = dsNextObjStore.get(key);
552 if (currentNextHops == null) {
553 log.warn("fixHashGroups could not update global store in "
554 + "device {} .. missing nextNeighbors for key {}",
555 deviceId, key);
Saurav Das62ae6792017-05-15 15:34:25 -0700556 continue;
557 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700558 Set<DeviceId> newNeighbors = new HashSet<>();
559 newNeighbors.addAll(tempStore.get(key));
560 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
561 ImmutableMap.copyOf(currentNextHops.dstNextHops());
562 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
563 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
564 targetSw, destSw, key, currentNextHops.nextId());
565 log.debug("Old dstNextHops: {}", oldDstNextHops);
566 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
567 // update global store
568 dsNextObjStore.put(key,
569 new NextNeighbors(currentNextHops.dstNextHops(),
570 currentNextHops.nextId()));
Saurav Das62ae6792017-05-15 15:34:25 -0700571 }
Saurav Das97241862018-02-14 14:14:54 -0800572
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700573 // even if one fails and others succeed, return false so ECMPspg not updated
574 return success;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700575 }
576
Saurav Dasfbe74572017-08-03 18:30:35 -0700577 /**
578 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
579 * that are not already in the store for the given DestinationSet. Note that
580 * this method does not remove existing next hops for the destinations in the
581 * DestinationSet.
582 *
583 * @param ds the DestinationSet for which the next hops need to be updated
584 * @param newDstNextHops a map of per-destination next hops to update the
585 * destinationSet with
586 * @return true if successful in updating all next hops
587 */
588 private boolean updateNextHops(DestinationSet ds,
Saurav Das261c3002017-06-13 15:35:54 -0700589 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
590 DestinationSetNextObjectiveStoreKey key =
591 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
592 NextNeighbors currNext = dsNextObjStore.get(key);
593 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
594
595 // add newDstNextHops to currDstNextHops for each dst
596 boolean success = true;
597 for (DeviceId dstSw : ds.getDestinationSwitches()) {
598 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
599 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
600 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
601 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
602 int edgeLabel = ds.getEdgeLabel(dstSw);
603 int nextId = currNext.nextId();
604
605 // new next hops should be added
606 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
Charles Chan223ae872018-08-22 17:56:47 -0700607 edgeLabel, nextId, popVlanInHashGroup(key.destinationSet()), false);
Saurav Das261c3002017-06-13 15:35:54 -0700608 if (suc) {
609 currNhops.addAll(newNhops);
610 currDstNextHops.put(dstSw, currNhops); // this is only a local change
611 }
612 success &= suc;
613 }
614
615 if (success) {
616 // update global store
617 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
618 currNext.nextId()));
619 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
620 dsNextObjStore.get(key));
621 }
622 return success;
623 }
624
Saurav Dasfbe74572017-08-03 18:30:35 -0700625 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700626 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
627 * needs to ensure that the given neighbors are all next hops towards the
628 * same destination (represented by the given edgeLabel).
Saurav Dasfbe74572017-08-03 18:30:35 -0700629 *
630 * @param neighbors set of neighbor device ids
631 * @param edgeLabel MPLS label to use in buckets
632 * @param nextId the nextObjective to change
Charles Chan223ae872018-08-22 17:56:47 -0700633 * @param popVlan this hash group bucket shuold includes a popVlan action
Saurav Dasfbe74572017-08-03 18:30:35 -0700634 * @param revoke true if buckets need to be removed, false if they need to
635 * be added
636 * @return true if successful in adding or removing buckets for all ports
637 * to the neighbors
638 */
639 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Charles Chan223ae872018-08-22 17:56:47 -0700640 int nextId, boolean popVlan, boolean revoke) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700641 for (DeviceId neighbor : neighbors) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700642 MacAddress neighborMac;
Saurav Das261c3002017-06-13 15:35:54 -0700643 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700644 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das261c3002017-06-13 15:35:54 -0700645 } catch (DeviceConfigNotFoundException e) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700646 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
647 + " for nextId:" + nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700648 return false;
649 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700650 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
651 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das261c3002017-06-13 15:35:54 -0700652 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700653 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das261c3002017-06-13 15:35:54 -0700654 deviceId, neighbor, nextId);
655 return false;
656 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700657 List<PortLabel> pl = Lists.newArrayList();
Charles Chan223ae872018-08-22 17:56:47 -0700658 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel, popVlan)));
Saurav Das261c3002017-06-13 15:35:54 -0700659 if (revoke) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700660 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
661 + "with Port/Label:{} to next object id {}",
662 deviceId, pl, nextId);
663 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700664 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700665 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
666 + "with Port/Label: {} to next object id {}",
667 deviceId, pl, nextId);
668 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700669 }
670 }
671 return true;
672 }
673
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700674 /**
Saurav Das97241862018-02-14 14:14:54 -0800675 * Returns true if the destination set is meant for swap or multi-labeled
676 * packet transport, and MPLS ECMP is not supported.
677 *
678 * @param dskey the key representing the destination set
679 * @return true if destination set is meant for simple next objectives
680 */
681 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
682 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
683 && !srManager.getMplsEcmp();
684 }
685
686 /**
Saurav Das3fb28272017-03-04 16:08:47 -0800687 * Adds or removes a port that has been configured with a vlan to a broadcast group
688 * for bridging. Should only be called by the master instance for this device.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800689 *
690 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Das3fb28272017-03-04 16:08:47 -0800691 * @param vlanId the vlan id corresponding to the broadcast domain/group
692 * @param popVlan indicates if packets should be sent out untagged or not out
693 * of the port. If true, indicates an access (untagged) or native vlan
694 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800695 * @param portUp true if port is enabled, false if disabled
Saurav Dasf0f592d2016-11-18 15:21:57 -0800696 */
Saurav Das3fb28272017-03-04 16:08:47 -0800697 public void processEdgePort(PortNumber port, VlanId vlanId,
698 boolean popVlan, boolean portUp) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800699 //get the next id for the subnet and edit it.
Charles Chan10b0fb72017-02-02 16:20:42 -0800700 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800701 if (nextId == -1) {
702 if (portUp) {
703 log.debug("**Creating flooding group for first port enabled in"
Saurav Das2b6a00f2017-12-05 15:00:23 -0800704 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan10b0fb72017-02-02 16:20:42 -0800705 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Dasf0f592d2016-11-18 15:21:57 -0800706 } else {
707 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan10b0fb72017-02-02 16:20:42 -0800708 + " removing port:{}", vlanId, deviceId, port);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800709 }
710 return;
711 }
712
713 log.info("**port{} in device {}: {} Bucket with Port {} to"
714 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
715 (portUp) ? "Adding" : "Removing",
716 port, nextId);
717 // Create the bucket to be added or removed
718 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Das3fb28272017-03-04 16:08:47 -0800719 if (popVlan) {
720 tBuilder.popVlan();
721 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800722 tBuilder.setOutput(port);
723
Saurav Dasf0f592d2016-11-18 15:21:57 -0800724 TrafficSelector metadata =
Saurav Das3fb28272017-03-04 16:08:47 -0800725 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Dasf0f592d2016-11-18 15:21:57 -0800726
727 NextObjective.Builder nextObjBuilder = DefaultNextObjective
728 .builder().withId(nextId)
729 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
730 .addTreatment(tBuilder.build())
731 .withMeta(metadata);
732
733 ObjectiveContext context = new DefaultObjectiveContext(
734 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
735 port, (portUp) ? "addedTo" : "removedFrom",
736 nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -0700737 (objective, error) -> {
738 log.warn("port {} failed to {} NextObj {} on {}: {}",
739 port, (portUp) ? "addTo" : "removeFrom", nextId, deviceId, error);
740 srManager.invalidateNextObj(objective.id());
741 });
Saurav Dasf0f592d2016-11-18 15:21:57 -0800742
743 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
744 : nextObjBuilder.removeFromExisting(context);
745 log.debug("edgePort processed: Submited next objective {} in device {}",
746 nextId, deviceId);
747 flowObjectiveService.next(deviceId, nextObj);
748 }
749
750 /**
Saurav Das97241862018-02-14 14:14:54 -0800751 * Returns the next objective of type hashed (or simple) associated with the
752 * destination set. In addition, updates the existing next-objective if new
753 * route-paths found have resulted in the addition of new next-hops to a
754 * particular destination. If there is no existing next objective for this
755 * destination set, this method would create a next objective and return the
756 * nextId. Optionally metadata can be passed in for the creation of the next
757 * objective. If the parameter simple is true then a simple next objective
758 * is created instead of a hashed one.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700759 *
Saurav Das261c3002017-06-13 15:35:54 -0700760 * @param ds destination set
761 * @param nextHops a map of per destination next hops
Saurav Das4c35fc42015-11-20 15:27:53 -0800762 * @param meta metadata passed into the creation of a Next Objective
Saurav Das97241862018-02-14 14:14:54 -0800763 * @param simple if true, a simple next objective will be created instead of
764 * a hashed next objective
Saurav Das4c35fc42015-11-20 15:27:53 -0800765 * @return int if found or -1 if there are errors in the creation of the
Saurav Das97241862018-02-14 14:14:54 -0800766 * neighbor set.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700767 */
Saurav Das261c3002017-06-13 15:35:54 -0700768 public int getNextObjectiveId(DestinationSet ds,
769 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800770 TrafficSelector meta, boolean simple) {
Saurav Das261c3002017-06-13 15:35:54 -0700771 NextNeighbors next = dsNextObjStore.
772 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
773 if (next == null) {
774 log.debug("getNextObjectiveId in device{}: Next objective id "
775 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700776 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
777 deviceId,
Saurav Das261c3002017-06-13 15:35:54 -0700778 dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700779 .stream()
780 .filter((nsStoreEntry) ->
781 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
782 .collect(Collectors.toList()));
Saurav Das261c3002017-06-13 15:35:54 -0700783
Saurav Das97241862018-02-14 14:14:54 -0800784 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das261c3002017-06-13 15:35:54 -0700785 next = dsNextObjStore.
786 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
787 if (next == null) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700788 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das261c3002017-06-13 15:35:54 -0700789 // failure in creating group
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700790 return -1;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700791 } else {
792 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700793 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700794 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700795 } else {
796 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700797 + "found for {}", deviceId, next.nextId(), ds);
798 // should fix hash groups too if next-hops have changed
799 if (!next.dstNextHops().equals(nextHops)) {
800 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
801 deviceId, next.nextId());
802 if (!updateNextHops(ds, nextHops)) {
803 // failure in updating group
804 return -1;
805 }
806 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700807 }
Saurav Das261c3002017-06-13 15:35:54 -0700808 return next.nextId();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700809 }
810
sangho4a5c42a2015-05-20 22:16:38 -0700811 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800812 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das2d94d312015-11-24 23:21:05 -0800813 * or -1 if no such objective exists. Note that this method does NOT create
814 * the next objective as a side-effect. It is expected that is objective is
Saurav Dasf0f592d2016-11-18 15:21:57 -0800815 * created at startup from network configuration. Typically this is used
816 * for L2 flooding within the subnet configured on the switch.
Charles Chan77277672015-10-20 16:24:19 -0700817 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800818 * @param vlanId vlan id
Charles Chan77277672015-10-20 16:24:19 -0700819 * @return int if found or -1
820 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800821 public int getVlanNextObjectiveId(VlanId vlanId) {
822 Integer nextId = vlanNextObjStore.
823 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chanc6ad7752015-10-29 14:58:10 -0700824
825 return (nextId != null) ? nextId : -1;
Charles Chan77277672015-10-20 16:24:19 -0700826 }
827
828 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000829 * Returns the next objective of type simple associated with the mac/vlan on the
830 * device, given the treatment. Different treatments to the same mac/vlan result
831 * in different next objectives. If no such objective exists, this method
832 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
833 * the creation of the objective. Typically this is used for L2 and L3 forwarding
834 * to compute nodes and containers/VMs on the compute nodes directly attached
835 * to the switch.
836 *
837 * @param macAddr the mac addr for the simple next objective
838 * @param vlanId the vlan for the simple next objective
Ruchi Sahota07869322019-05-09 17:26:14 -0400839 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000840 * @param createIfMissing true if a next object should be created if not found
841 * @return int if found or created, -1 if there are errors during the
842 * creation of the next objective.
843 */
Ruchi Sahota07869322019-05-09 17:26:14 -0400844 public int getMacVlanNextObjectiveId(MacAddress macAddr, VlanId vlanId, PortNumber port,
845 boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000846
847 Integer nextId = macVlanNextObjStore
848 .get(new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId));
849
850 if (nextId != null) {
851 return nextId;
852 }
853
854 log.debug("getMacVlanNextObjectiveId in device {}: Next objective id "
855 + "not found for host : {}/{} .. {}", deviceId, macAddr, vlanId,
856 (createIfMissing) ? "creating" : "aborting");
857
858 if (!createIfMissing) {
859 return -1;
860 }
861
Ruchi Sahota07869322019-05-09 17:26:14 -0400862 MacAddress deviceMac;
863 try {
864 deviceMac = deviceConfig.getDeviceMac(deviceId);
865 } catch (DeviceConfigNotFoundException e) {
866 log.warn(e.getMessage() + " in getMacVlanNextObjectiveId");
867 return -1;
868 }
869
870 // since we are creating now, port cannot be null
871 if (port == null) {
872 log.debug("getMacVlanNextObjectiveId : port information cannot be null "
873 + "for device {}, host {}/{}", deviceId, macAddr, vlanId);
874 return -1;
875 }
876
877 TrafficSelector.Builder meta = DefaultTrafficSelector.builder();
878
879 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
880 treatment.deferred()
881 .setEthDst(macAddr)
882 .setEthSrc(deviceMac)
883 .setOutput(port);
884
885 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
886 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
887 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
888 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
889
890 // Adjust the meta according to VLAN configuration
891 if (taggedVlans.contains(vlanId)) {
pierventre50e1bb82020-12-14 19:31:03 +0100892 meta.matchVlanId(vlanId);
Ruchi Sahota07869322019-05-09 17:26:14 -0400893 treatment.setVlanId(vlanId);
894 } else if (vlanId.equals(VlanId.NONE)) {
895 if (untaggedVlan != null) {
896 meta.matchVlanId(untaggedVlan);
pierventre50e1bb82020-12-14 19:31:03 +0100897 treatment.popVlan();
Ruchi Sahota07869322019-05-09 17:26:14 -0400898 } else if (nativeVlan != null) {
899 meta.matchVlanId(nativeVlan);
pierventre50e1bb82020-12-14 19:31:03 +0100900 treatment.popVlan();
Ruchi Sahota07869322019-05-09 17:26:14 -0400901 } else {
902 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
903 macAddr, vlanId, connectPoint);
904 return -1;
905 }
906 } else {
907 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
908 + " in tagged vlan", macAddr, vlanId, connectPoint);
909 return -1;
910 }
911
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000912 /* create missing next objective */
Ruchi Sahota07869322019-05-09 17:26:14 -0400913 nextId = createGroupFromMacVlan(macAddr, vlanId, treatment.build(), meta.build());
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000914 if (nextId == null) {
915 log.warn("getMacVlanNextObjectiveId: unable to create next obj"
916 + "for dev:{} host:{}/{}", deviceId, macAddr, vlanId);
917 return -1;
918 }
919 return nextId;
920 }
921
922
923 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800924 * Returns the next objective of type simple associated with the port on the
925 * device, given the treatment. Different treatments to the same port result
926 * in different next objectives. If no such objective exists, this method
Saurav Das2cb38292017-03-29 19:09:17 -0700927 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Dasf0f592d2016-11-18 15:21:57 -0800928 * the creation of the objective. Typically this is used for L2 and L3 forwarding
929 * to compute nodes and containers/VMs on the compute nodes directly attached
930 * to the switch.
Saurav Das2d94d312015-11-24 23:21:05 -0800931 *
932 * @param portNum the port number for the simple next objective
933 * @param treatment the actions to apply on the packets (should include outport)
934 * @param meta optional metadata passed into the creation of the next objective
Saurav Das2cb38292017-03-29 19:09:17 -0700935 * @param createIfMissing true if a next object should be created if not found
Saurav Das2d94d312015-11-24 23:21:05 -0800936 * @return int if found or created, -1 if there are errors during the
937 * creation of the next objective.
938 */
939 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700940 TrafficSelector meta, boolean createIfMissing) {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800941 Integer nextId = portNextObjStore
Saurav Das368cf212017-03-15 15:15:14 -0700942 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das2cb38292017-03-29 19:09:17 -0700943 if (nextId != null) {
944 return nextId;
945 }
946 log.debug("getPortNextObjectiveId in device {}: Next objective id "
947 + "not found for port: {} .. {}", deviceId, portNum,
948 (createIfMissing) ? "creating" : "aborting");
949 if (!createIfMissing) {
950 return -1;
951 }
952 // create missing next objective
953 createGroupFromPort(portNum, treatment, meta);
954 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
955 treatment, meta));
Saurav Das2d94d312015-11-24 23:21:05 -0800956 if (nextId == null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700957 log.warn("getPortNextObjectiveId: unable to create next obj"
958 + "for dev:{} port:{}", deviceId, portNum);
959 return -1;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800960 }
961 return nextId;
962 }
963
964 /**
sangho4a5c42a2015-05-20 22:16:38 -0700965 * Checks if the next objective ID (group) for the neighbor set exists or not.
966 *
967 * @param ns neighbor set to check
968 * @return true if it exists, false otherwise
969 */
Saurav Das261c3002017-06-13 15:35:54 -0700970 public boolean hasNextObjectiveId(DestinationSet ns) {
971 NextNeighbors nextHops = dsNextObjStore.
972 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
973 if (nextHops == null) {
sangho4a5c42a2015-05-20 22:16:38 -0700974 return false;
975 }
976
977 return true;
978 }
979
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700980 private void populateNeighborMaps() {
981 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700982 for (Link link : outgoingLinks) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700983 if (link.type() != Link.Type.DIRECT) {
984 continue;
985 }
986 addNeighborAtPort(link.dst().deviceId(), link.src().port());
987 }
988 }
989
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700990 protected void addNeighborAtPort(DeviceId neighborId,
991 PortNumber portToNeighbor) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700992 // Update DeviceToPort database
993 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
994 deviceId, neighborId, portToNeighbor);
Saurav Das4c35fc42015-11-20 15:27:53 -0800995 Set<PortNumber> ports = Collections
996 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
997 ports.add(portToNeighbor);
998 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
999 if (portnums != null) {
1000 portnums.add(portToNeighbor);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001001 }
1002
1003 // Update portToDevice database
Saurav Das97241862018-02-14 14:14:54 -08001004 // should always update as neighbor could have changed on this port
1005 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das4c35fc42015-11-20 15:27:53 -08001006 if (prev != null) {
Saurav Dasdebcf882018-04-06 20:16:01 -07001007 log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Das62ae6792017-05-15 15:34:25 -07001008 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001009 }
1010 }
1011
sangho27462c62015-05-14 00:39:53 -07001012 /**
Saurav Das261c3002017-06-13 15:35:54 -07001013 * Creates a NextObjective for a hash group in this device from a given
Saurav Das97241862018-02-14 14:14:54 -08001014 * DestinationSet. If the parameter simple is true, a simple next objective
1015 * is created instead.
sangho27462c62015-05-14 00:39:53 -07001016 *
Saurav Das261c3002017-06-13 15:35:54 -07001017 * @param ds the DestinationSet
1018 * @param neighbors a map for each destination and its next-hops
Saurav Das4c35fc42015-11-20 15:27:53 -08001019 * @param meta metadata passed into the creation of a Next Objective
Saurav Das97241862018-02-14 14:14:54 -08001020 * @param simple if true, a simple next objective will be created instead of
1021 * a hashed next objective
sangho27462c62015-05-14 00:39:53 -07001022 */
Saurav Das261c3002017-06-13 15:35:54 -07001023 public void createGroupFromDestinationSet(DestinationSet ds,
1024 Map<DeviceId, Set<DeviceId>> neighbors,
1025 TrafficSelector meta,
Saurav Das97241862018-02-14 14:14:54 -08001026 boolean simple) {
Saurav Das261c3002017-06-13 15:35:54 -07001027 int nextId = flowObjectiveService.allocateNextId();
Saurav Das97241862018-02-14 14:14:54 -08001028 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
1029 : NextObjective.Type.HASHED;
Saurav Das261c3002017-06-13 15:35:54 -07001030 if (neighbors == null || neighbors.isEmpty()) {
1031 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
1032 + "to create group in dev:{} for ds: {} with next-hops {}",
1033 deviceId, ds, neighbors);
1034 return;
1035 }
Saurav Das261c3002017-06-13 15:35:54 -07001036
1037 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1038 .builder()
1039 .withId(nextId)
1040 .withType(type)
1041 .fromApp(appId);
1042 if (meta != null) {
pierventre50e1bb82020-12-14 19:31:03 +01001043 // Udate the meta VLAN id to match the PW transport label
1044 if (!popVlanInHashGroup(ds)) {
1045 TrafficSelector newMeta = DefaultTrafficSelector.builder(meta)
1046 .matchVlanId(srManager.getPwTransportVlan())
1047 .build();
1048 meta = newMeta;
1049 }
Saurav Das261c3002017-06-13 15:35:54 -07001050 nextObjBuilder.withMeta(meta);
1051 }
1052
1053 // create treatment buckets for each neighbor for each dst Device
1054 // except in the special case where we only want to pick a single
Saurav Das97241862018-02-14 14:14:54 -08001055 // neighbor/port for a simple nextObj
Saurav Das261c3002017-06-13 15:35:54 -07001056 boolean foundSingleNeighbor = false;
1057 boolean treatmentAdded = false;
1058 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
1059 for (DeviceId dst : ds.getDestinationSwitches()) {
1060 Set<DeviceId> nextHops = neighbors.get(dst);
1061 if (nextHops == null || nextHops.isEmpty()) {
1062 continue;
Pier Ventre229fd0b2016-10-31 16:49:19 -07001063 }
Saurav Das261c3002017-06-13 15:35:54 -07001064
1065 if (foundSingleNeighbor) {
1066 break;
1067 }
1068
1069 for (DeviceId neighborId : nextHops) {
Saurav Das4c35fc42015-11-20 15:27:53 -08001070 if (devicePortMap.get(neighborId) == null) {
1071 log.warn("Neighbor {} is not in the port map yet for dev:{}",
1072 neighborId, deviceId);
sangho2165d222015-05-01 09:38:25 -07001073 return;
Jon Hall31d84782017-01-18 20:15:44 -08001074 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001075 log.warn("There are no ports for "
Saurav Das4c35fc42015-11-20 15:27:53 -08001076 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001077 return;
sangho2165d222015-05-01 09:38:25 -07001078 }
1079
Saurav Das4c35fc42015-11-20 15:27:53 -08001080 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -08001081 try {
Saurav Das4c35fc42015-11-20 15:27:53 -08001082 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan319d1a22015-11-03 10:42:14 -08001083 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -07001084 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan319d1a22015-11-03 10:42:14 -08001085 return;
1086 }
Saurav Das261c3002017-06-13 15:35:54 -07001087 // For each port to the neighbor, we create a new treatment
Pier Ventre229fd0b2016-10-31 16:49:19 -07001088 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Das97241862018-02-14 14:14:54 -08001089 // In this case we need a SIMPLE nextObj. We randomly pick a port
1090 if (simple) {
Pier Ventre229fd0b2016-10-31 16:49:19 -07001091 int size = devicePortMap.get(neighborId).size();
1092 int index = RandomUtils.nextInt(0, size);
1093 neighborPorts = Collections.singleton(
Saurav Das261c3002017-06-13 15:35:54 -07001094 Iterables.get(devicePortMap.get(neighborId),
1095 index));
1096 foundSingleNeighbor = true;
Pier Ventre229fd0b2016-10-31 16:49:19 -07001097 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -07001098
Pier Ventre229fd0b2016-10-31 16:49:19 -07001099 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001100 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
1101 .builder();
Saurav Das261c3002017-06-13 15:35:54 -07001102 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
1103 int edgeLabel = ds.getEdgeLabel(dst);
1104 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Das97241862018-02-14 14:14:54 -08001105 if (simple) {
1106 // swap label case
1107 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1108 } else {
1109 // ecmp with label push case
1110 tBuilder.pushMpls().copyTtlOut()
1111 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1112 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001113 }
Charles Chan81ddf6f2018-08-12 15:19:01 -07001114
1115 // Set VLAN ID for PW transport. Otherwise pop vlan
Charles Chan223ae872018-08-22 17:56:47 -07001116 if (!popVlanInHashGroup(ds)) {
Saurav Das9bf49582018-08-13 15:34:26 -07001117 tBuilder.setVlanId(srManager.getPwTransportVlan());
Charles Chan81ddf6f2018-08-12 15:19:01 -07001118 } else {
1119 tBuilder.popVlan();
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -07001120 }
Charles Chan81ddf6f2018-08-12 15:19:01 -07001121
Saurav Das4c35fc42015-11-20 15:27:53 -08001122 tBuilder.setOutput(sp);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001123 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das261c3002017-06-13 15:35:54 -07001124 treatmentAdded = true;
1125 //update store
1126 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
1127 if (existingNeighbors == null) {
1128 existingNeighbors = new HashSet<>();
1129 }
1130 existingNeighbors.add(neighborId);
1131 dstNextHops.put(dst, existingNeighbors);
1132 log.debug("creating treatment for port/label {}/{} in next:{}",
1133 sp, edgeLabel, nextId);
1134 }
1135
1136 if (foundSingleNeighbor) {
1137 break;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001138 }
1139 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001140 }
Saurav Das261c3002017-06-13 15:35:54 -07001141
1142 if (!treatmentAdded) {
1143 log.warn("Could not createGroup from DestinationSet {} without any"
1144 + "next hops {}", ds, neighbors);
1145 return;
1146 }
1147 ObjectiveContext context = new DefaultObjectiveContext(
1148 (objective) ->
1149 log.debug("createGroupsFromDestinationSet installed "
1150 + "NextObj {} on {}", nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001151 (objective, error) -> {
1152 log.warn("createGroupsFromDestinationSet failed to install NextObj {} on {}: {}",
1153 nextId, deviceId, error);
1154 srManager.invalidateNextObj(objective.id());
1155 });
Saurav Das261c3002017-06-13 15:35:54 -07001156 NextObjective nextObj = nextObjBuilder.add(context);
1157 log.debug(".. createGroupsFromDestinationSet: Submitted "
1158 + "next objective {} in device {}", nextId, deviceId);
1159 flowObjectiveService.next(deviceId, nextObj);
1160 //update store
1161 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1162 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001163 }
1164
Saurav Das2d94d312015-11-24 23:21:05 -08001165 /**
Saurav Dasf0f592d2016-11-18 15:21:57 -08001166 * Creates broadcast groups for all ports in the same subnet for
1167 * all configured subnets.
Saurav Das2d94d312015-11-24 23:21:05 -08001168 */
Charles Chan10b0fb72017-02-02 16:20:42 -08001169 public void createGroupsFromVlanConfig() {
Charles Chan90772a72017-02-08 15:52:08 -08001170 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001171 createBcastGroupFromVlan(vlanId, ports);
Pier Ventreb6a7f342016-11-26 21:05:22 -08001172 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001173 }
Charles Chanc6ad7752015-10-29 14:58:10 -07001174
Saurav Dasf0f592d2016-11-18 15:21:57 -08001175 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001176 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Dasf0f592d2016-11-18 15:21:57 -08001177 *
Charles Chan10b0fb72017-02-02 16:20:42 -08001178 * @param vlanId vlan id
Saurav Dasf0f592d2016-11-18 15:21:57 -08001179 * @param ports list of ports in the subnet
1180 */
Charles Chan90772a72017-02-08 15:52:08 -08001181 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001182 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chanc6ad7752015-10-29 14:58:10 -07001183
Charles Chan10b0fb72017-02-02 16:20:42 -08001184 if (vlanNextObjStore.containsKey(key)) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001185 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan10b0fb72017-02-02 16:20:42 -08001186 deviceId, vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001187 return;
1188 }
Charles Chande6655c2015-12-23 00:15:11 -08001189
Saurav Dasf0f592d2016-11-18 15:21:57 -08001190 TrafficSelector metadata =
Charles Chan10b0fb72017-02-02 16:20:42 -08001191 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chan77277672015-10-20 16:24:19 -07001192
Saurav Dasf0f592d2016-11-18 15:21:57 -08001193 int nextId = flowObjectiveService.allocateNextId();
Charles Chan77277672015-10-20 16:24:19 -07001194
Saurav Dasf0f592d2016-11-18 15:21:57 -08001195 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1196 .builder().withId(nextId)
1197 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1198 .withMeta(metadata);
Charles Chan77277672015-10-20 16:24:19 -07001199
Saurav Dasf0f592d2016-11-18 15:21:57 -08001200 ports.forEach(port -> {
1201 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan90772a72017-02-08 15:52:08 -08001202 if (toPopVlan(port, vlanId)) {
1203 tBuilder.popVlan();
1204 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001205 tBuilder.setOutput(port);
1206 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chan77277672015-10-20 16:24:19 -07001207 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001208
Saurav Das2cb38292017-03-29 19:09:17 -07001209 ObjectiveContext context = new DefaultObjectiveContext(
1210 (objective) ->
1211 log.debug("createBroadcastGroupFromVlan installed "
1212 + "NextObj {} on {}", nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001213 (objective, error) -> {
1214 log.warn("createBroadcastGroupFromVlan failed to install NextObj {} on {}: {}",
1215 nextId, deviceId, error);
1216 srManager.invalidateNextObj(objective.id());
1217 });
Saurav Das2cb38292017-03-29 19:09:17 -07001218 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001219 flowObjectiveService.next(deviceId, nextObj);
Saurav Das2b6a00f2017-12-05 15:00:23 -08001220 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1221 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001222
Charles Chan10b0fb72017-02-02 16:20:42 -08001223 vlanNextObjStore.put(key, nextId);
Charles Chan77277672015-10-20 16:24:19 -07001224 }
1225
Charles Chanb7f75ac2016-01-11 18:28:54 -08001226 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001227 * Removes a single broadcast group from a given vlan id.
1228 * The group should be empty.
1229 * @param deviceId device Id to remove the group
1230 * @param portNum port number related to the group
1231 * @param vlanId vlan id of the broadcast group to remove
1232 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1233 */
1234 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1235 VlanId vlanId, boolean popVlan) {
1236 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1237
1238 if (!vlanNextObjStore.containsKey(key)) {
1239 log.debug("Broadcast group for device {} and subnet {} does not exist",
1240 deviceId, vlanId);
1241 return;
1242 }
1243
1244 TrafficSelector metadata =
1245 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1246
1247 int nextId = vlanNextObjStore.get(key);
1248
1249 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1250 .builder().withId(nextId)
1251 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1252 .withMeta(metadata);
1253
1254 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1255 if (popVlan) {
1256 tBuilder.popVlan();
1257 }
1258 tBuilder.setOutput(portNum);
1259 nextObjBuilder.addTreatment(tBuilder.build());
1260
1261 ObjectiveContext context = new DefaultObjectiveContext(
1262 (objective) ->
1263 log.debug("removeBroadcastGroupFromVlan removed "
1264 + "NextObj {} on {}", nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001265 (objective, error) -> {
1266 log.warn("removeBroadcastGroupFromVlan failed to remove NextObj {} on {}: {}",
1267 nextId, deviceId, error);
1268 srManager.invalidateNextObj(objective.id());
1269 });
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001270 NextObjective nextObj = nextObjBuilder.remove(context);
1271 flowObjectiveService.next(deviceId, nextObj);
1272 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1273 nextId, deviceId);
1274
1275 vlanNextObjStore.remove(key, nextId);
1276 }
1277
1278 /**
Charles Chan90772a72017-02-08 15:52:08 -08001279 * Determine if we should pop given vlan before sending packets to the given port.
1280 *
1281 * @param portNumber port number
1282 * @param vlanId vlan id
1283 * @return true if the vlan id is not contained in any vlanTagged config
1284 */
1285 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das261c3002017-06-13 15:35:54 -07001286 return srManager.interfaceService
1287 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan90772a72017-02-08 15:52:08 -08001288 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1289 }
1290
1291 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001292 * Create simple next objective for an indirect host mac/vlan. The treatments can include
1293 * all outgoing actions that need to happen on the packet.
1294 *
1295 * @param macAddr the mac address of the host
1296 * @param vlanId the vlan of the host
1297 * @param treatment the actions to apply on the packets (should include outport)
1298 * @param meta optional data to pass to the driver
1299 * @return next objective ID
1300 */
1301 public int createGroupFromMacVlan(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment,
1302 TrafficSelector meta) {
1303 int nextId = flowObjectiveService.allocateNextId();
1304 MacVlanNextObjectiveStoreKey key = new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId);
1305
1306 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1307 .builder().withId(nextId)
1308 .withType(NextObjective.Type.SIMPLE)
1309 .addTreatment(treatment)
1310 .fromApp(appId)
1311 .withMeta(meta);
1312
1313 ObjectiveContext context = new DefaultObjectiveContext(
1314 (objective) ->
1315 log.debug("createGroupFromMacVlan installed "
1316 + "NextObj {} on {}", nextId, deviceId),
1317 (objective, error) -> {
1318 log.warn("createGroupFromMacVlan failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1319 srManager.invalidateNextObj(objective.id());
1320 });
1321 NextObjective nextObj = nextObjBuilder.add(context);
1322 flowObjectiveService.next(deviceId, nextObj);
1323 log.debug("createGroupFromMacVlan: Submited next objective {} in device {} "
1324 + "for host {}/{}", nextId, deviceId, macAddr, vlanId);
1325
1326 macVlanNextObjStore.put(key, nextId);
1327 return nextId;
1328 }
1329
1330 /**
Saurav Das2d94d312015-11-24 23:21:05 -08001331 * Create simple next objective for a single port. The treatments can include
1332 * all outgoing actions that need to happen on the packet.
1333 *
1334 * @param portNum the outgoing port on the device
1335 * @param treatment the actions to apply on the packets (should include outport)
1336 * @param meta optional data to pass to the driver
1337 */
1338 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1339 TrafficSelector meta) {
1340 int nextId = flowObjectiveService.allocateNextId();
1341 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das368cf212017-03-15 15:15:14 -07001342 deviceId, portNum, treatment, meta);
Saurav Das2d94d312015-11-24 23:21:05 -08001343
1344 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1345 .builder().withId(nextId)
1346 .withType(NextObjective.Type.SIMPLE)
1347 .addTreatment(treatment)
1348 .fromApp(appId)
1349 .withMeta(meta);
1350
Saurav Das2cb38292017-03-29 19:09:17 -07001351 ObjectiveContext context = new DefaultObjectiveContext(
1352 (objective) ->
1353 log.debug("createGroupFromPort installed "
1354 + "NextObj {} on {}", nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001355 (objective, error) -> {
1356 log.warn("createGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1357 srManager.invalidateNextObj(objective.id());
1358 });
Saurav Das2cb38292017-03-29 19:09:17 -07001359 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das2d94d312015-11-24 23:21:05 -08001360 flowObjectiveService.next(deviceId, nextObj);
1361 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1362 + "for port {}", nextId, deviceId, portNum);
1363
1364 portNextObjStore.put(key, nextId);
1365 }
1366
sangho27462c62015-05-14 00:39:53 -07001367 /**
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001368 * Creates simple next objective for a single port.
1369 *
1370 * @param deviceId device id that has the port to deal with
1371 * @param portNum the outgoing port on the device
1372 * @param vlanId vlan id associated with the port
1373 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1374 */
1375 public void createPortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1376 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1377 mbuilder.matchVlanId(vlanId);
1378
1379 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1380 tbuilder.immediate().setOutput(portNum);
1381 if (popVlan) {
1382 tbuilder.immediate().popVlan();
1383 }
1384
1385 createGroupFromPort(portNum, tbuilder.build(), mbuilder.build());
1386 }
1387
1388 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001389 * Removes simple next objective for a single port.
1390 *
1391 * @param deviceId device id that has the port to deal with
1392 * @param portNum the outgoing port on the device
1393 * @param vlanId vlan id associated with the port
1394 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1395 */
1396 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1397 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1398 mbuilder.matchVlanId(vlanId);
1399
1400 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1401 tbuilder.immediate().setOutput(portNum);
1402 if (popVlan) {
1403 tbuilder.immediate().popVlan();
1404 }
1405
1406 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1407 tbuilder.build(), mbuilder.build(), false);
1408
1409 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1410 deviceId, portNum, tbuilder.build(), mbuilder.build());
1411 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1412 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1413 .builder().withId(portNextObjId)
pierventre50e1bb82020-12-14 19:31:03 +01001414 .withType(NextObjective.Type.SIMPLE)
1415 .addTreatment(tbuilder.build())
1416 .fromApp(appId)
1417 .withMeta(mbuilder.build());
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001418 ObjectiveContext context = new DefaultObjectiveContext(
1419 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1420 portNextObjId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001421 (objective, error) -> {
1422 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1423 portNextObjId, deviceId, error);
1424 srManager.invalidateNextObj(objective.id());
1425 });
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001426 NextObjective nextObjective = nextObjBuilder.remove(context);
1427 log.info("**removePortNextObjective: Submitted "
1428 + "next objective {} in device {}",
1429 portNextObjId, deviceId);
1430 flowObjectiveService.next(deviceId, nextObjective);
1431
1432 portNextObjStore.remove(key);
1433 }
1434 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001435
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001436 /**
sangho27462c62015-05-14 00:39:53 -07001437 * Removes groups for the next objective ID given.
1438 *
1439 * @param objectiveId next objective ID to remove
1440 * @return true if succeeds, false otherwise
1441 */
1442 public boolean removeGroup(int objectiveId) {
Saurav Das261c3002017-06-13 15:35:54 -07001443 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1444 dsNextObjStore.entrySet()) {
1445 if (e.getValue().nextId() != objectiveId) {
1446 continue;
1447 }
Pier Luigiecb09f42018-01-14 21:56:11 +01001448 // Right now it is just used in TunnelHandler
1449 // remember in future that PW transit groups could
1450 // be Indirect groups
sangho27462c62015-05-14 00:39:53 -07001451 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1452 .builder().withId(objectiveId)
1453 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chana4ee4f92016-04-23 14:48:16 -07001454 ObjectiveContext context = new DefaultObjectiveContext(
1455 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1456 objectiveId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001457 (objective, error) -> {
1458 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", objectiveId, deviceId, error);
1459 srManager.invalidateNextObj(objective.id());
1460 });
Charles Chana4ee4f92016-04-23 14:48:16 -07001461 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das4c35fc42015-11-20 15:27:53 -08001462 log.info("**removeGroup: Submited "
1463 + "next objective {} in device {}",
1464 objectiveId, deviceId);
sangho27462c62015-05-14 00:39:53 -07001465 flowObjectiveService.next(deviceId, nextObjective);
1466
Saurav Das261c3002017-06-13 15:35:54 -07001467 dsNextObjStore.remove(e.getKey());
sangho4a5c42a2015-05-20 22:16:38 -07001468 return true;
sangho27462c62015-05-14 00:39:53 -07001469 }
1470
1471 return false;
1472 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001473 /**
1474 * Remove simple next objective for a single port. The treatments can include
1475 * all outgoing actions that need to happen on the packet.
1476 *
1477 * @param portNum the outgoing port on the device
1478 * @param treatment the actions applied on the packets (should include outport)
1479 * @param meta optional data to pass to the driver
pierventrea3989be2021-01-08 16:43:17 +01001480 * @return a completable future that completes when the port has been removed
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001481 */
pierventrea3989be2021-01-08 16:43:17 +01001482 public CompletableFuture<Objective> removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1483 TrafficSelector meta) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001484 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1485 deviceId, portNum, treatment, meta);
1486 Integer nextId = portNextObjStore.get(key);
pierventrea3989be2021-01-08 16:43:17 +01001487 CompletableFuture<Objective> future = new CompletableFuture<>();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001488
1489 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1490 .builder().withId(nextId)
1491 .withType(NextObjective.Type.SIMPLE)
1492 .addTreatment(treatment)
1493 .fromApp(appId)
1494 .withMeta(meta);
1495
1496 ObjectiveContext context = new DefaultObjectiveContext(
pierventrea3989be2021-01-08 16:43:17 +01001497 (objective) -> {
1498 log.info("removeGroupFromPort done " + "NextObj {} on {}", nextId, deviceId);
1499 future.complete(objective);
1500 },
Charles Chanfacfbef2018-08-23 14:30:33 -07001501 (objective, error) -> {
1502 log.warn("removeGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1503 srManager.invalidateNextObj(objective.id());
pierventrea3989be2021-01-08 16:43:17 +01001504 future.complete(null);
Charles Chanfacfbef2018-08-23 14:30:33 -07001505 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001506 );
1507 NextObjective nextObj = nextObjBuilder.remove(context);
1508 flowObjectiveService.next(deviceId, nextObj);
1509 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1510 + "for port {}", nextId, deviceId, portNum);
1511
1512 portNextObjStore.remove(key);
pierventrea3989be2021-01-08 16:43:17 +01001513 return future;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001514 }
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001515
Charles Chanb7f75ac2016-01-11 18:28:54 -08001516 /**
1517 * Removes all groups from all next objective stores.
1518 */
Saurav Das261c3002017-06-13 15:35:54 -07001519 /*public void removeAllGroups() {
1520 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das62af8802015-12-04 10:52:59 -08001521 nsNextObjStore.entrySet()) {
Saurav Das261c3002017-06-13 15:35:54 -07001522 removeGroup(entry.getValue().nextId());
Saurav Das62af8802015-12-04 10:52:59 -08001523 }
1524 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1525 portNextObjStore.entrySet()) {
1526 removeGroup(entry.getValue());
1527 }
Charles Chan10b0fb72017-02-02 16:20:42 -08001528 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1529 vlanNextObjStore.entrySet()) {
Saurav Das62af8802015-12-04 10:52:59 -08001530 removeGroup(entry.getValue());
1531 }
Saurav Das261c3002017-06-13 15:35:54 -07001532 }*/ //XXX revisit
1533
Saurav Dasfbe74572017-08-03 18:30:35 -07001534 /**
1535 * Triggers a one time bucket verification operation on all hash groups
1536 * on this device.
1537 */
1538 public void triggerBucketCorrector() {
1539 BucketCorrector bc = new BucketCorrector();
1540 bc.run();
1541 }
1542
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001543 /**
1544 * Modifies L2IG bucket when the interface configuration is updated, especially
1545 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1546 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1547 * TrafficTreatment in turn.
1548 *
1549 * @param portNumber the port on this device that needs to be updated
1550 * @param vlanId the vlan id corresponding to this port
1551 * @param pushVlan indicates if packets should be sent out untagged or not out
1552 * from the port. If true, updated TrafficTreatment involves
1553 * pop vlan tag action. If false, updated TrafficTreatment
1554 * does not involve pop vlan tag action.
1555 */
1556 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001557 TrafficTreatment.Builder oldTBuilder = DefaultTrafficTreatment.builder();
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001558 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001559 tBuilder.setOutput(portNumber);
1560 oldTBuilder.setOutput(portNumber);
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001561 if (pushVlan) {
1562 tBuilder.popVlan();
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001563 } else {
1564 oldTBuilder.popVlan();
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001565 }
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001566
1567 TrafficSelector metadata =
1568 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1569
Shibu Vijayakumar632dd642018-03-01 15:45:59 -08001570 // Update portNextObjStore with new L2IG
1571 int nextId = getPortNextObjectiveId(portNumber, oldTBuilder.build(), metadata, false);
1572 portNextObjStore.remove(new PortNextObjectiveStoreKey(deviceId, portNumber, oldTBuilder.build(), metadata));
1573 portNextObjStore.put(new PortNextObjectiveStoreKey(deviceId, portNumber, tBuilder.build(), metadata), nextId);
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001574
1575 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1576 .builder().withId(nextId)
1577 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1578 .addTreatment(tBuilder.build())
1579 .withMeta(metadata);
1580
1581 ObjectiveContext context = new DefaultObjectiveContext(
1582 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1583 portNumber, nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001584 (objective, error) -> {
1585 log.warn("port {} failed to updated NextObj {} on {}: {}", portNumber, nextId, deviceId, error);
1586 srManager.invalidateNextObj(objective.id());
1587 });
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001588
1589 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001590 }
1591
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001592 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001593 * Updates the next objective for the given nextId .
1594 *
1595 * @param hostMac mac of host for which Next obj is to be updated.
1596 * @param hostVlanId vlan of host for which Next obj is to be updated.
1597 * @param port port with which to update the Next Obj.
1598 * @param nextId of Next Obj which needs to be updated.
1599 */
1600 public void updateL3UcastGroupBucket(MacAddress hostMac, VlanId hostVlanId, PortNumber port, int nextId) {
1601
1602 MacAddress deviceMac;
1603 try {
1604 deviceMac = deviceConfig.getDeviceMac(deviceId);
1605 } catch (DeviceConfigNotFoundException e) {
1606 log.warn(e.getMessage() + " in updateL3UcastGroupBucket");
1607 return;
1608 }
1609
pierventre50e1bb82020-12-14 19:31:03 +01001610 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
1611 VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint);
1612 Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint);
1613 VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001614
pierventre50e1bb82020-12-14 19:31:03 +01001615 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1616 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
1617 .deferred()
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001618 .setEthDst(hostMac)
1619 .setEthSrc(deviceMac)
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001620 .setOutput(port);
1621
pierventre50e1bb82020-12-14 19:31:03 +01001622 if (taggedVlans.contains(hostVlanId)) {
1623 mbuilder.matchVlanId(hostVlanId);
1624 tbuilder.setVlanId(hostVlanId);
1625 } else if (hostVlanId.equals(VlanId.NONE)) {
1626 if (untaggedVlan != null) {
1627 mbuilder.matchVlanId(untaggedVlan);
1628 tbuilder.popVlan();
1629 } else if (nativeVlan != null) {
1630 mbuilder.matchVlanId(nativeVlan);
1631 tbuilder.popVlan();
1632 } else {
1633 log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan",
1634 hostMac, hostVlanId, connectPoint);
1635 return;
1636 }
pierventrea3989be2021-01-08 16:43:17 +01001637 } else {
1638 log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed"
1639 + " in tagged vlan", hostMac, hostVlanId, connectPoint);
1640 return;
pierventre50e1bb82020-12-14 19:31:03 +01001641 }
1642
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001643 log.debug(" update L3Ucast : deviceMac {}, port {}, host {}/{}, nextid {}, Treatment {} Meta {}",
pierventre50e1bb82020-12-14 19:31:03 +01001644 deviceMac, port, hostMac, hostVlanId, nextId, tbuilder.build(), mbuilder.build());
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001645
1646 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1647 .builder().withId(nextId)
1648 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1649 .addTreatment(tbuilder.build())
pierventre50e1bb82020-12-14 19:31:03 +01001650 .withMeta(mbuilder.build());
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001651
1652 ObjectiveContext context = new DefaultObjectiveContext(
1653 (objective) -> log.debug(" NextId {} successfully updated host {} vlan {} with port {}",
1654 nextId, hostMac, hostVlanId, port),
1655 (objective, error) -> {
1656 log.warn(" NextId {} failed to update host {} vlan {} with port {}, error : {}",
1657 nextId, hostMac, hostVlanId, port, error);
1658 srManager.invalidateNextObj(objective.id());
1659 });
1660
1661 NextObjective nextObj = nextObjBuilder.modify(context);
1662 flowObjectiveService.next(deviceId, nextObj);
1663
1664 }
1665
1666 /**
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001667 * Adds a single port to the L2FG or removes it from the L2FG.
1668 *
1669 * @param vlanId the vlan id corresponding to this port
1670 * @param portNum the port on this device to be updated
1671 * @param nextId the next objective ID for the given vlan id
1672 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1673 */
1674 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001675 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1676 if (toPopVlan(portNum, vlanId)) {
1677 tBuilder.popVlan();
1678 }
1679 tBuilder.setOutput(portNum);
1680
1681 TrafficSelector metadata =
1682 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1683
1684 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1685 .builder().withId(nextId)
1686 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1687 .addTreatment(tBuilder.build())
1688 .withMeta(metadata);
1689
1690 ObjectiveContext context = new DefaultObjectiveContext(
1691 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1692 portNum, nextId, deviceId),
Charles Chanfacfbef2018-08-23 14:30:33 -07001693 (objective, error) -> {
1694 log.warn("port {} failed to removedFrom NextObj {} on {}: {}", portNum, nextId, deviceId, error);
1695 srManager.invalidateNextObj(objective.id());
1696 });
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001697
1698 if (install) {
1699 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1700 } else {
1701 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1702 }
1703 }
Saurav Das8a3022d2017-05-05 17:01:08 -07001704
1705 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001706 * Performs bucket verification operation for all hash groups in this device.
1707 * Checks RouteHandler to ensure that routing is stable before attempting
1708 * verification. Verification involves creating a nextObjective with
1709 * operation VERIFY for existing next objectives in the store, and passing
1710 * it to the driver. It is the driver that actually performs the verification
1711 * by adding or removing buckets to match the verification next objective
1712 * created here.
Saurav Das8a3022d2017-05-05 17:01:08 -07001713 */
Saurav Dasfbe74572017-08-03 18:30:35 -07001714 protected final class BucketCorrector implements Runnable {
1715 Integer nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001716
Saurav Dasfbe74572017-08-03 18:30:35 -07001717 BucketCorrector() {
1718 this.nextId = null;
1719 }
1720
1721 BucketCorrector(Integer nextId) {
1722 this.nextId = nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001723 }
1724
1725 @Override
1726 public void run() {
Saurav Dasfbe74572017-08-03 18:30:35 -07001727 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1728 return;
Saurav Das8a3022d2017-05-05 17:01:08 -07001729 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001730 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1731 if (rh == null) {
1732 return;
1733 }
1734 if (!rh.isRoutingStable()) {
1735 return;
1736 }
1737 rh.acquireRoutingLock();
1738 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001739 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasfbe74572017-08-03 18:30:35 -07001740 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1741 .stream()
1742 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigiecb09f42018-01-14 21:56:11 +01001743 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Das97241862018-02-14 14:14:54 -08001744 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1745 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1746 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1747 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1748 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasfbe74572017-08-03 18:30:35 -07001749 .map(entry -> entry.getKey())
1750 .collect(Collectors.toSet());
1751 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1752 NextNeighbors next = dsNextObjStore.get(dsKey);
1753 if (next == null) {
1754 continue;
1755 }
1756 int nid = next.nextId();
1757 if (nextId != null && nextId != nid) {
1758 continue;
1759 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001760 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasfbe74572017-08-03 18:30:35 -07001761 deviceId, dsKey, next);
1762 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das9bf49582018-08-13 15:34:26 -07001763 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasfbe74572017-08-03 18:30:35 -07001764 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1765 .withId(nid)
1766 .withType(NextObjective.Type.HASHED)
1767 .withMeta(metabuilder.build())
1768 .fromApp(appId);
1769
1770 next.dstNextHops().forEach((dstDev, nextHops) -> {
1771 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1772 nextHops.forEach(neighbor -> {
1773 MacAddress neighborMac;
1774 try {
1775 neighborMac = deviceConfig.getDeviceMac(neighbor);
1776 } catch (DeviceConfigNotFoundException e) {
1777 log.warn(e.getMessage() + " Aborting neighbor"
1778 + neighbor);
1779 return;
1780 }
1781 devicePortMap.get(neighbor).forEach(port -> {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001782 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasfbe74572017-08-03 18:30:35 -07001783 + " port/label {}/{} to dst {} via {}",
1784 deviceId, nid, port, edgeLabel,
1785 dstDev, neighbor);
Saurav Das97241862018-02-14 14:14:54 -08001786 nextObjBuilder
pierventre02fd3b02021-06-15 18:49:41 +02001787 .addTreatment(treatmentBuilder(port, neighborMac, dsKey.destinationSet().swap(),
1788 edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Dasfbe74572017-08-03 18:30:35 -07001789 });
1790 });
1791 });
1792
1793 NextObjective nextObjective = nextObjBuilder.verify();
1794 flowObjectiveService.next(deviceId, nextObjective);
1795 }
1796 } finally {
1797 rh.releaseRoutingLock();
1798 }
1799
1800 }
1801
1802 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
pierventre02fd3b02021-06-15 18:49:41 +02001803 boolean swap, int edgeLabel, boolean popVlan) {
Saurav Dasfbe74572017-08-03 18:30:35 -07001804 TrafficTreatment.Builder tBuilder =
1805 DefaultTrafficTreatment.builder();
1806 tBuilder.setOutput(outport)
pierventre02fd3b02021-06-15 18:49:41 +02001807 .setEthDst(dstMac)
1808 .setEthSrc(nodeMacAddr);
1809
1810 if (popVlan) {
1811 tBuilder.popVlan();
1812 }
1813
Saurav Dasfbe74572017-08-03 18:30:35 -07001814 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Das97241862018-02-14 14:14:54 -08001815 if (swap) {
1816 // swap label case
1817 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1818 } else {
1819 // ecmp with label push case
1820 tBuilder.pushMpls()
1821 .copyTtlOut()
1822 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1823 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001824 }
1825 return tBuilder.build();
Saurav Das8a3022d2017-05-05 17:01:08 -07001826 }
1827 }
1828
Charles Chan223ae872018-08-22 17:56:47 -07001829 /**
1830 * Determines whether the hash group bucket should include a popVlan action.
1831 * We don't popVlan for PW.
1832 *
1833 * @param ds destination set
1834 * @return true if VLAN needs to be popped
1835 */
1836 private boolean popVlanInHashGroup(DestinationSet ds) {
1837 return (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.SWAP_NOT_BOS) &&
1838 (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.POP_NOT_BOS);
1839 }
Pier Luigiecb09f42018-01-14 21:56:11 +01001840}