blob: 669e136798b637b7e218919ce898c685dddb0e6b [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08003 *
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 */
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
Saurav Das9df5b7c2017-08-14 16:44:43 -070018import com.google.common.collect.ImmutableMap;
Pier Ventre917127a2016-10-31 16:49:19 -070019import com.google.common.collect.Iterables;
Saurav Das9df5b7c2017-08-14 16:44:43 -070020import com.google.common.collect.Lists;
Saurav Dasc88d4662017-05-15 15:34:25 -070021import com.google.common.collect.Sets;
22
Pier Ventre917127a2016-10-31 16:49:19 -070023import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080025import org.onlab.packet.MplsLabel;
Saurav Das423fe2b2015-12-04 10:52:59 -080026import org.onlab.packet.VlanId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070027import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import org.onosproject.core.ApplicationId;
Charles Chan59cc16d2017-02-02 16:20:42 -080029import org.onosproject.net.ConnectPoint;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
Saurav Das423fe2b2015-12-04 10:52:59 -080033import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das8a0732e2015-11-20 15:27:53 -080035import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan216e3c82016-04-23 14:48:16 -070038import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070039import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080042import org.onosproject.net.link.LinkService;
Saurav Dasceccf242017-08-03 18:30:35 -070043import org.onosproject.segmentrouting.DefaultRoutingHandler;
Saurav Das423fe2b2015-12-04 10:52:59 -080044import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080045import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
46import org.onosproject.segmentrouting.config.DeviceProperties;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +000047import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das7bcbe702017-06-13 15:35:54 -070048import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070049import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080050import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +000051import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070052import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080053import org.slf4j.Logger;
54
Pier Ventre917127a2016-10-31 16:49:19 -070055import java.net.URI;
Charles Chan7ffd81f2017-02-08 15:52:08 -080056import java.util.Collection;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.Collections;
Saurav Dasc88d4662017-05-15 15:34:25 -070058import java.util.HashMap;
Pier Ventre917127a2016-10-31 16:49:19 -070059import java.util.HashSet;
60import java.util.List;
61import java.util.Map;
62import java.util.Set;
63import java.util.concurrent.ConcurrentHashMap;
Saurav Das1547b3f2017-05-05 17:01:08 -070064import java.util.concurrent.ScheduledExecutorService;
65import java.util.concurrent.TimeUnit;
Pier Ventre917127a2016-10-31 16:49:19 -070066import java.util.stream.Collectors;
67
68import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das1547b3f2017-05-05 17:01:08 -070069import static java.util.concurrent.Executors.newScheduledThreadPool;
70import static org.onlab.util.Tools.groupedThreads;
Pier Ventre917127a2016-10-31 16:49:19 -070071import static org.slf4j.LoggerFactory.getLogger;
72
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080073/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070074 * Default ECMP group handler creation module. This component creates a set of
75 * ECMP groups for every neighbor that this device is connected to based on
76 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080077 */
78public class DefaultGroupHandler {
Ray Milkey9c9cde42018-01-12 14:22:06 -080079 private static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080080
Saurav Dasceccf242017-08-03 18:30:35 -070081 private static final long VERIFY_INTERVAL = 30; // secs
82
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080083 protected final DeviceId deviceId;
84 protected final ApplicationId appId;
85 protected final DeviceProperties deviceConfig;
86 protected final List<Integer> allSegmentIds;
Pier Ventree0ae7a32016-11-23 09:57:42 -080087 protected int ipv4NodeSegmentId = -1;
88 protected int ipv6NodeSegmentId = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -080089 protected boolean isEdgeRouter = false;
90 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080091 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070092 protected FlowObjectiveService flowObjectiveService;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +000093 private DeviceConfiguration config;
94
Saurav Dasc88d4662017-05-15 15:34:25 -070095 /**
96 * local store for neighbor-device-ids and the set of ports on this device
97 * that connect to the same neighbor.
98 */
Saurav Das8a0732e2015-11-20 15:27:53 -080099 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
100 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700101 /**
102 * local store for ports on this device connected to neighbor-device-id.
103 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800104 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
105 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700106
Saurav Das7bcbe702017-06-13 15:35:54 -0700107 // distributed store for (device+destination-set) mapped to next-id and neighbors
108 protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
109 dsNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800110 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan59cc16d2017-02-02 16:20:42 -0800111 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
112 vlanNextObjStore = null;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000113 // distributed store for (device+mac+vlan+treatment) mapped to next-id
114 protected EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
115 macVlanNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800116 // distributed store for (device+port+treatment) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -0800117 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
118 portNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800119 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800120
Saurav Das1547b3f2017-05-05 17:01:08 -0700121 private ScheduledExecutorService executorService
Saurav Dasceccf242017-08-03 18:30:35 -0700122 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das1547b3f2017-05-05 17:01:08 -0700123
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700124 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700125 .register(URI.class).register(HashSet.class)
Saurav Das7bcbe702017-06-13 15:35:54 -0700126 .register(DeviceId.class).register(PortNumber.class)
127 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700128 .register(PolicyGroupParams.class)
129 .register(GroupBucketIdentifier.class)
130 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800131
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700132 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
133 DeviceProperties config,
134 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700135 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800136 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800137 this.deviceId = checkNotNull(deviceId);
138 this.appId = checkNotNull(appId);
139 this.deviceConfig = checkNotNull(config);
140 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800141 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
142 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800143 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
144 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800145 this.isEdgeRouter = config.isEdgeDevice(deviceId);
146 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
147 } catch (DeviceConfigNotFoundException e) {
148 log.warn(e.getMessage()
149 + " Skipping value assignment in DefaultGroupHandler");
150 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700151 this.flowObjectiveService = flowObjService;
Saurav Das7bcbe702017-06-13 15:35:54 -0700152 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeye4afdb52017-04-05 09:42:04 -0700153 this.vlanNextObjStore = srManager.vlanNextObjStore();
154 this.portNextObjStore = srManager.portNextObjStore();
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000155 this.macVlanNextObjStore = srManager.macVlanNextObjStore();
Charles Chan188ebf52015-12-23 00:15:11 -0800156 this.srManager = srManager;
Saurav Dasceccf242017-08-03 18:30:35 -0700157 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
158 VERIFY_INTERVAL,
159 TimeUnit.SECONDS);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800160 populateNeighborMaps();
161 }
162
163 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700164 * Gracefully shuts down a groupHandler. Typically called when the handler is
165 * no longer needed.
166 */
167 public void shutdown() {
168 executorService.shutdown();
169 }
170
171 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700172 * Creates a group handler object.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800173 *
174 * @param deviceId device identifier
175 * @param appId application identifier
176 * @param config interface to retrieve the device properties
177 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800179 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800180 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800181 * @return default group handler type
182 */
Saurav Das4ce45962015-11-24 23:21:05 -0800183 public static DefaultGroupHandler createGroupHandler(
Saurav Dasceccf242017-08-03 18:30:35 -0700184 DeviceId deviceId,
185 ApplicationId appId,
186 DeviceProperties config,
187 LinkService linkService,
188 FlowObjectiveService flowObjService,
189 SegmentRoutingManager srManager)
190 throws DeviceConfigNotFoundException {
Saurav Dasc88d4662017-05-15 15:34:25 -0700191 return new DefaultGroupHandler(deviceId, appId, config,
192 linkService,
193 flowObjService,
194 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800195 }
196
197 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800198 * Updates local stores for link-src-device/port to neighbor (link-dst) for
199 * link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -0700200 *
201 * @param link the infrastructure link
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800202 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700203 public void portUpForLink(Link link) {
Saurav Dasceccf242017-08-03 18:30:35 -0700204 if (!link.src().deviceId().equals(deviceId)) {
205 log.warn("linkUp: deviceId{} doesn't match with link src {}",
206 deviceId, link.src().deviceId());
207 return;
208 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700209
Saurav Dasceccf242017-08-03 18:30:35 -0700210 log.info("* portUpForLink: Device {} linkUp at local port {} to "
211 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
212 // ensure local state is updated even if linkup is aborted later on
213 addNeighborAtPort(link.dst().deviceId(),
214 link.src().port());
215 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700216
Saurav Dasceccf242017-08-03 18:30:35 -0700217 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800218 * Updates local stores for link-src-device/port to neighbor (link-dst) for
219 * link that has gone down.
Saurav Dasceccf242017-08-03 18:30:35 -0700220 *
Saurav Dasa4020382018-02-14 14:14:54 -0800221 * @param link the infrastructure link
Saurav Dasceccf242017-08-03 18:30:35 -0700222 */
Saurav Dasa4020382018-02-14 14:14:54 -0800223 public void portDownForLink(Link link) {
224 PortNumber port = link.src().port();
Saurav Dasceccf242017-08-03 18:30:35 -0700225 if (portDeviceMap.get(port) == null) {
226 log.warn("portDown: unknown port");
227 return;
228 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700229
Saurav Dasceccf242017-08-03 18:30:35 -0700230 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
231 portDeviceMap.get(port));
232 devicePortMap.get(portDeviceMap.get(port)).remove(port);
233 portDeviceMap.remove(port);
234 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800235
236 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800237 * Cleans up local stores for removed neighbor device.
238 *
239 * @param neighborId the device identifier for the neighbor device
240 */
241 public void cleanUpForNeighborDown(DeviceId neighborId) {
242 Set<PortNumber> ports = devicePortMap.remove(neighborId);
243 if (ports != null) {
244 ports.forEach(p -> portDeviceMap.remove(p));
245 }
246 }
247
248 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700249 * Checks all groups in the src-device of link for neighbor sets that include
250 * the dst-device of link, and edits the hash groups according to link up
251 * or down. Should only be called by the master instance of the src-switch
252 * of link. Typically used when there are no route-path changes due to the
253 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800254 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700255 * @param link the infrastructure link that has gone down or come up
256 * @param linkDown true if link has gone down
257 * @param firstTime true if link has come up for the first time i.e a link
258 * not seen-before
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800259 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700260 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700261 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800262 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700263 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800264 } catch (DeviceConfigNotFoundException e) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700265 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800266 return;
267 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700268 // find all the destinationSets related to link
269 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700270 .stream()
Saurav Das7bcbe702017-06-13 15:35:54 -0700271 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +0100272 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -0800273 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
274 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
275 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
276 .filter(entry -> !entry.getKey().destinationSet().swap() ||
277 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Das7bcbe702017-06-13 15:35:54 -0700278 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
279 .map(entry -> entry.getKey())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700280 .collect(Collectors.toSet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700281
Saurav Das7bcbe702017-06-13 15:35:54 -0700282 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
283 deviceId, link.dst().deviceId(), dsKeySet);
284
285 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
286 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
287 if (nextHops == null) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700288 log.warn("retryHash in device {}, but global store has no record "
Saurav Das7bcbe702017-06-13 15:35:54 -0700289 + "for dsKey:{}", deviceId, dsKey);
Saurav Dasc88d4662017-05-15 15:34:25 -0700290 continue;
291 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700292 int nextId = nextHops.nextId();
293 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -0700294 if (!linkDown) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700295 List<PortLabel> pl = Lists.newArrayList();
Saurav Dasc88d4662017-05-15 15:34:25 -0700296 if (firstTime) {
297 // some links may have come up before the next-objective was created
298 // we take this opportunity to ensure other ports to same next-hop-dst
299 // are part of the hash group (see CORD-1180). Duplicate additions
300 // to the same hash group are avoided by the driver.
301 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700302 dstSet.forEach(dst -> {
303 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700304 pl.add(new PortLabel(p, edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das7bcbe702017-06-13 15:35:54 -0700305 });
Saurav Dasc88d4662017-05-15 15:34:25 -0700306 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700307 addToHashedNextObjective(pl, neighborMac, nextId);
308 } else {
309 // handle only the port that came up
310 dstSet.forEach(dst -> {
311 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700312 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das9df5b7c2017-08-14 16:44:43 -0700313 });
314 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700315 }
316 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700317 // linkdown
318 List<PortLabel> pl = Lists.newArrayList();
Saurav Das7bcbe702017-06-13 15:35:54 -0700319 dstSet.forEach(dst -> {
320 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700321 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das7bcbe702017-06-13 15:35:54 -0700322 });
Saurav Das9df5b7c2017-08-14 16:44:43 -0700323 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700324 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800325 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700326 }
327
Saurav Dasc88d4662017-05-15 15:34:25 -0700328 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700329 * Utility class for associating output ports and the corresponding MPLS
330 * labels to push. In dual-homing, there are different labels to push
331 * corresponding to the destination switches in an edge-pair. If both
332 * destinations are reachable via the same spine, then the output-port to
333 * the spine will be associated with two labels i.e. there will be two
334 * PortLabel objects for the same port but with different labels.
335 */
336 private class PortLabel {
337 PortNumber port;
338 int edgeLabel;
Charles Chane0260e12018-08-22 17:56:47 -0700339 boolean popVlan;
Saurav Das9df5b7c2017-08-14 16:44:43 -0700340
Charles Chane0260e12018-08-22 17:56:47 -0700341 PortLabel(PortNumber port, int edgeLabel, boolean popVlan) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700342 this.port = port;
343 this.edgeLabel = edgeLabel;
Charles Chane0260e12018-08-22 17:56:47 -0700344 this.popVlan = popVlan;
Saurav Das9df5b7c2017-08-14 16:44:43 -0700345 }
346
347 @Override
348 public String toString() {
Charles Chane0260e12018-08-22 17:56:47 -0700349 return port.toString() + "/" + String.valueOf(edgeLabel) + (popVlan ? "/popVlan" : "");
Saurav Das9df5b7c2017-08-14 16:44:43 -0700350 }
351 }
352
353 /**
354 * Makes a call to the FlowObjective service to add buckets to
355 * a hashed group. User must ensure that all the ports & labels are meant
356 * same neighbor (ie. dstMac).
Saurav Dasc88d4662017-05-15 15:34:25 -0700357 *
Pier Luigi63edd932018-01-14 21:56:11 +0100358 * @param portLabels a collection of port & label combinations to add
Saurav Das9df5b7c2017-08-14 16:44:43 -0700359 * to the hash group identified by the nextId
Saurav Dasc88d4662017-05-15 15:34:25 -0700360 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700361 * @param nextId id for next-objective to which buckets will be added
Saurav Dasceccf242017-08-03 18:30:35 -0700362 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700363 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700364 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
365 MacAddress dstMac, Integer nextId) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700366 // setup metadata to pass to nextObjective - indicate the vlan on egress
367 // if needed by the switch pipeline. Since hashed next-hops are always to
368 // other neighboring routers, there is no subnet assigned on those ports.
369 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das09c2c4d2018-08-13 15:34:26 -0700370 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Das1547b3f2017-05-05 17:01:08 -0700371 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
372 .withId(nextId)
373 .withType(NextObjective.Type.HASHED)
Saurav Das1547b3f2017-05-05 17:01:08 -0700374 .withMeta(metabuilder.build())
375 .fromApp(appId);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700376 // Create the new buckets to be updated
377 portLabels.forEach(pl -> {
378 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
379 tBuilder.setOutput(pl.port)
380 .setEthDst(dstMac)
381 .setEthSrc(nodeMacAddr);
Charles Chane0260e12018-08-22 17:56:47 -0700382 if (pl.popVlan) {
383 tBuilder.popVlan();
384 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700385 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
386 tBuilder.pushMpls()
387 .copyTtlOut()
388 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
389 }
390 nextObjBuilder.addTreatment(tBuilder.build());
391 });
392
393 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
394 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700395
396 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das9df5b7c2017-08-14 16:44:43 -0700397 (objective) -> log.debug("addToHash port/label {} addedTo "
398 + "NextObj {} on {}", portLabels, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700399 (objective, error) -> {
400 log.warn("addToHash failed to add port/label {} to NextObj {} on {}: {}",
401 portLabels, nextId, deviceId, error);
402 srManager.invalidateNextObj(objective.id());
403 });
Saurav Das1547b3f2017-05-05 17:01:08 -0700404 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
405 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800406 }
407
408 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700409 * Makes a call to the FlowObjective service to remove buckets from
410 * a hash group. User must ensure that all the ports & labels are meant
411 * same neighbor (ie. dstMac).
Saurav Dasceccf242017-08-03 18:30:35 -0700412 *
Pier Luigi63edd932018-01-14 21:56:11 +0100413 * @param portLabels a collection of port & label combinations to remove
Saurav Das9df5b7c2017-08-14 16:44:43 -0700414 * from the hash group identified by the nextId
Saurav Dasceccf242017-08-03 18:30:35 -0700415 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700416 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasceccf242017-08-03 18:30:35 -0700417 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700418 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
419 MacAddress dstMac, Integer nextId) {
Charles Chan67ef2552018-08-30 19:49:23 -0700420 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
421 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasceccf242017-08-03 18:30:35 -0700422 NextObjective.Builder nextObjBuilder = DefaultNextObjective
423 .builder()
424 .withType(NextObjective.Type.HASHED) //same as original
Charles Chan67ef2552018-08-30 19:49:23 -0700425 .withMeta(metabuilder.build())
Saurav Dasceccf242017-08-03 18:30:35 -0700426 .withId(nextId)
Saurav Das9df5b7c2017-08-14 16:44:43 -0700427 .fromApp(appId);
428 // Create the buckets to be removed
429 portLabels.forEach(pl -> {
430 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
431 tBuilder.setOutput(pl.port)
432 .setEthDst(dstMac)
433 .setEthSrc(nodeMacAddr);
Charles Chane0260e12018-08-22 17:56:47 -0700434 if (pl.popVlan) {
435 tBuilder.popVlan();
436 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700437 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
438 tBuilder.pushMpls()
439 .copyTtlOut()
440 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
441 }
442 nextObjBuilder.addTreatment(tBuilder.build());
443 });
444 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
445 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700446
Saurav Das9df5b7c2017-08-14 16:44:43 -0700447 ObjectiveContext context = new DefaultObjectiveContext(
448 (objective) -> log.debug("port/label {} removedFrom NextObj"
449 + " {} on {}", portLabels, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700450 (objective, error) -> {
451 log.warn("port/label {} failed to removeFrom NextObj {} on {}: {}",
452 portLabels, nextId, deviceId, error);
453 srManager.invalidateNextObj(objective.id());
454 });
Saurav Das9df5b7c2017-08-14 16:44:43 -0700455 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasceccf242017-08-03 18:30:35 -0700456 flowObjectiveService.next(deviceId, nextObjective);
457 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700458
459 /**
460 * Checks all the hash-groups in the target-switch meant for the destination
461 * switch, and either adds or removes buckets to make the neighbor-set
462 * match the given next-hops. Typically called by the master instance of the
463 * destination switch, which may be different from the master instance of the
464 * target switch where hash-group changes are made.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800465 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700466 * @param targetSw the switch in which the hash groups will be edited
467 * @param nextHops the current next hops for the target switch to reach
468 * the dest sw
469 * @param destSw the destination switch
470 * @param revoke true if hash groups need to remove buckets from the
471 * the groups to match the current next hops
472 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800473 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700474 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
475 DeviceId destSw, boolean revoke) {
476 // temporary storage of keys to be updated
Saurav Das7bcbe702017-06-13 15:35:54 -0700477 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Dasc88d4662017-05-15 15:34:25 -0700478 new HashMap<>();
Saurav Das9df5b7c2017-08-14 16:44:43 -0700479 boolean foundNextObjective = false, success = true;
Charles Chan0b4e6182015-11-03 10:42:14 -0800480
Saurav Das7bcbe702017-06-13 15:35:54 -0700481 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Dasc88d4662017-05-15 15:34:25 -0700482 // with different neighbors than the given next-hops
Saurav Das7bcbe702017-06-13 15:35:54 -0700483 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
484 if (!dskey.deviceId().equals(targetSw) ||
485 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700486 continue;
487 }
488 foundNextObjective = true;
Saurav Das7bcbe702017-06-13 15:35:54 -0700489 NextNeighbors nhops = dsNextObjStore.get(dskey);
490 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
491 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
492 Integer nextId = nhops.nextId();
Saurav Dase0d4c872018-03-05 14:37:16 -0800493 if (currNeighbors == null || nextHops == null) {
494 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
495 + " in targetSw:{} for dstSw:{}", currNeighbors,
496 nextHops, targetSw, destSw);
497 success &= false;
498 continue;
499 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800500
Saurav Dasa4020382018-02-14 14:14:54 -0800501 // some store elements may not be hashed next-objectives - ignore them
502 if (isSimpleNextObjective(dskey)) {
503 log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}"
504 + " -> dstSw:{} with current nextHops:{} to new"
505 + " nextHops: {} in nextId:{}",
506 (revoke) ? "removal" : "addition", targetSw, destSw,
507 currNeighbors, nextHops, nextId);
Saurav Dase0d4c872018-03-05 14:37:16 -0800508 if ((revoke && !nextHops.isEmpty())
509 || (!revoke && !nextHops.equals(currNeighbors))) {
Saurav Das137f27f2018-06-11 17:02:31 -0700510 log.debug("Simple next objective cannot be edited to "
Saurav Dasa4020382018-02-14 14:14:54 -0800511 + "move from {} to {}", currNeighbors, nextHops);
512 }
513 continue;
514 }
515
Saurav Dasc88d4662017-05-15 15:34:25 -0700516 Set<DeviceId> diff;
517 if (revoke) {
518 diff = Sets.difference(currNeighbors, nextHops);
519 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
520 + "hops:{} ..removing {}", targetSw, destSw, nextId,
521 currNeighbors, diff);
522 } else {
523 diff = Sets.difference(nextHops, currNeighbors);
524 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
525 + "hops:{} ..adding {}", targetSw, destSw, nextId,
526 currNeighbors, diff);
527 }
Charles Chane0260e12018-08-22 17:56:47 -0700528 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, popVlanInHashGroup(dskey.destinationSet()),
Saurav Das9df5b7c2017-08-14 16:44:43 -0700529 revoke);
530 if (suc) {
531 // to update neighbor set with changes made
Saurav Dasc88d4662017-05-15 15:34:25 -0700532 if (revoke) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700533 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700534 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700535 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700536 }
sangho834e4b02015-05-01 09:38:25 -0700537 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700538 success &= suc;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800539 }
540
Saurav Dasc88d4662017-05-15 15:34:25 -0700541 if (!foundNextObjective) {
542 log.debug("Cannot find any nextObjectives for route targetSw:{} "
543 + "-> dstSw:{}", targetSw, destSw);
544 return true; // nothing to do, return true so ECMPspg is updated
545 }
546
Saurav Das7bcbe702017-06-13 15:35:54 -0700547 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
548 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700549 NextNeighbors currentNextHops = dsNextObjStore.get(key);
550 if (currentNextHops == null) {
551 log.warn("fixHashGroups could not update global store in "
552 + "device {} .. missing nextNeighbors for key {}",
553 deviceId, key);
Saurav Dasc88d4662017-05-15 15:34:25 -0700554 continue;
555 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700556 Set<DeviceId> newNeighbors = new HashSet<>();
557 newNeighbors.addAll(tempStore.get(key));
558 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
559 ImmutableMap.copyOf(currentNextHops.dstNextHops());
560 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
561 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
562 targetSw, destSw, key, currentNextHops.nextId());
563 log.debug("Old dstNextHops: {}", oldDstNextHops);
564 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
565 // update global store
566 dsNextObjStore.put(key,
567 new NextNeighbors(currentNextHops.dstNextHops(),
568 currentNextHops.nextId()));
Saurav Dasc88d4662017-05-15 15:34:25 -0700569 }
Saurav Dasa4020382018-02-14 14:14:54 -0800570
Saurav Das9df5b7c2017-08-14 16:44:43 -0700571 // even if one fails and others succeed, return false so ECMPspg not updated
572 return success;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800573 }
574
Saurav Dasceccf242017-08-03 18:30:35 -0700575 /**
576 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
577 * that are not already in the store for the given DestinationSet. Note that
578 * this method does not remove existing next hops for the destinations in the
579 * DestinationSet.
580 *
581 * @param ds the DestinationSet for which the next hops need to be updated
582 * @param newDstNextHops a map of per-destination next hops to update the
583 * destinationSet with
584 * @return true if successful in updating all next hops
585 */
586 private boolean updateNextHops(DestinationSet ds,
Saurav Das7bcbe702017-06-13 15:35:54 -0700587 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
588 DestinationSetNextObjectiveStoreKey key =
589 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
590 NextNeighbors currNext = dsNextObjStore.get(key);
591 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
592
593 // add newDstNextHops to currDstNextHops for each dst
594 boolean success = true;
595 for (DeviceId dstSw : ds.getDestinationSwitches()) {
596 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
597 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
598 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
599 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
600 int edgeLabel = ds.getEdgeLabel(dstSw);
601 int nextId = currNext.nextId();
602
603 // new next hops should be added
604 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
Charles Chane0260e12018-08-22 17:56:47 -0700605 edgeLabel, nextId, popVlanInHashGroup(key.destinationSet()), false);
Saurav Das7bcbe702017-06-13 15:35:54 -0700606 if (suc) {
607 currNhops.addAll(newNhops);
608 currDstNextHops.put(dstSw, currNhops); // this is only a local change
609 }
610 success &= suc;
611 }
612
613 if (success) {
614 // update global store
615 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
616 currNext.nextId()));
617 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
618 dsNextObjStore.get(key));
619 }
620 return success;
621 }
622
Saurav Dasceccf242017-08-03 18:30:35 -0700623 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700624 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
625 * needs to ensure that the given neighbors are all next hops towards the
626 * same destination (represented by the given edgeLabel).
Saurav Dasceccf242017-08-03 18:30:35 -0700627 *
628 * @param neighbors set of neighbor device ids
629 * @param edgeLabel MPLS label to use in buckets
630 * @param nextId the nextObjective to change
Charles Chane0260e12018-08-22 17:56:47 -0700631 * @param popVlan this hash group bucket shuold includes a popVlan action
Saurav Dasceccf242017-08-03 18:30:35 -0700632 * @param revoke true if buckets need to be removed, false if they need to
633 * be added
634 * @return true if successful in adding or removing buckets for all ports
635 * to the neighbors
636 */
637 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Charles Chane0260e12018-08-22 17:56:47 -0700638 int nextId, boolean popVlan, boolean revoke) {
Saurav Dasceccf242017-08-03 18:30:35 -0700639 for (DeviceId neighbor : neighbors) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700640 MacAddress neighborMac;
Saurav Das7bcbe702017-06-13 15:35:54 -0700641 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700642 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das7bcbe702017-06-13 15:35:54 -0700643 } catch (DeviceConfigNotFoundException e) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700644 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
645 + " for nextId:" + nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700646 return false;
647 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700648 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
649 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700650 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Das9df5b7c2017-08-14 16:44:43 -0700651 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700652 deviceId, neighbor, nextId);
653 return false;
654 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700655 List<PortLabel> pl = Lists.newArrayList();
Charles Chane0260e12018-08-22 17:56:47 -0700656 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel, popVlan)));
Saurav Das7bcbe702017-06-13 15:35:54 -0700657 if (revoke) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700658 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
659 + "with Port/Label:{} to next object id {}",
660 deviceId, pl, nextId);
661 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700662 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700663 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
664 + "with Port/Label: {} to next object id {}",
665 deviceId, pl, nextId);
666 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700667 }
668 }
669 return true;
670 }
671
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800672 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800673 * Returns true if the destination set is meant for swap or multi-labeled
674 * packet transport, and MPLS ECMP is not supported.
675 *
676 * @param dskey the key representing the destination set
677 * @return true if destination set is meant for simple next objectives
678 */
679 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
680 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
681 && !srManager.getMplsEcmp();
682 }
683
684 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800685 * Adds or removes a port that has been configured with a vlan to a broadcast group
686 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800687 *
688 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800689 * @param vlanId the vlan id corresponding to the broadcast domain/group
690 * @param popVlan indicates if packets should be sent out untagged or not out
691 * of the port. If true, indicates an access (untagged) or native vlan
692 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800693 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800694 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800695 public void processEdgePort(PortNumber port, VlanId vlanId,
696 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800697 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800698 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800699 if (nextId == -1) {
700 if (portUp) {
701 log.debug("**Creating flooding group for first port enabled in"
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800702 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan59cc16d2017-02-02 16:20:42 -0800703 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800704 } else {
705 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800706 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800707 }
708 return;
709 }
710
711 log.info("**port{} in device {}: {} Bucket with Port {} to"
712 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
713 (portUp) ? "Adding" : "Removing",
714 port, nextId);
715 // Create the bucket to be added or removed
716 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800717 if (popVlan) {
718 tBuilder.popVlan();
719 }
Saurav Das1a129a02016-11-18 15:21:57 -0800720 tBuilder.setOutput(port);
721
Saurav Das1a129a02016-11-18 15:21:57 -0800722 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800723 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800724
725 NextObjective.Builder nextObjBuilder = DefaultNextObjective
726 .builder().withId(nextId)
727 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
728 .addTreatment(tBuilder.build())
729 .withMeta(metadata);
730
731 ObjectiveContext context = new DefaultObjectiveContext(
732 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
733 port, (portUp) ? "addedTo" : "removedFrom",
734 nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700735 (objective, error) -> {
736 log.warn("port {} failed to {} NextObj {} on {}: {}",
737 port, (portUp) ? "addTo" : "removeFrom", nextId, deviceId, error);
738 srManager.invalidateNextObj(objective.id());
739 });
Saurav Das1a129a02016-11-18 15:21:57 -0800740
741 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
742 : nextObjBuilder.removeFromExisting(context);
743 log.debug("edgePort processed: Submited next objective {} in device {}",
744 nextId, deviceId);
745 flowObjectiveService.next(deviceId, nextObj);
746 }
747
748 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800749 * Returns the next objective of type hashed (or simple) associated with the
750 * destination set. In addition, updates the existing next-objective if new
751 * route-paths found have resulted in the addition of new next-hops to a
752 * particular destination. If there is no existing next objective for this
753 * destination set, this method would create a next objective and return the
754 * nextId. Optionally metadata can be passed in for the creation of the next
755 * objective. If the parameter simple is true then a simple next objective
756 * is created instead of a hashed one.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800757 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700758 * @param ds destination set
759 * @param nextHops a map of per destination next hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800760 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800761 * @param simple if true, a simple next objective will be created instead of
762 * a hashed next objective
Saurav Das8a0732e2015-11-20 15:27:53 -0800763 * @return int if found or -1 if there are errors in the creation of the
Saurav Dasa4020382018-02-14 14:14:54 -0800764 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800765 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700766 public int getNextObjectiveId(DestinationSet ds,
767 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Dasa4020382018-02-14 14:14:54 -0800768 TrafficSelector meta, boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700769 NextNeighbors next = dsNextObjStore.
770 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
771 if (next == null) {
772 log.debug("getNextObjectiveId in device{}: Next objective id "
773 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700774 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
775 deviceId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700776 dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700777 .stream()
778 .filter((nsStoreEntry) ->
779 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
780 .collect(Collectors.toList()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700781
Saurav Dasa4020382018-02-14 14:14:54 -0800782 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das7bcbe702017-06-13 15:35:54 -0700783 next = dsNextObjStore.
784 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
785 if (next == null) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700786 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das7bcbe702017-06-13 15:35:54 -0700787 // failure in creating group
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700788 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700789 } else {
790 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700791 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700792 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700793 } else {
794 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700795 + "found for {}", deviceId, next.nextId(), ds);
796 // should fix hash groups too if next-hops have changed
797 if (!next.dstNextHops().equals(nextHops)) {
798 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
799 deviceId, next.nextId());
800 if (!updateNextHops(ds, nextHops)) {
801 // failure in updating group
802 return -1;
803 }
804 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700805 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700806 return next.nextId();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800807 }
808
sangho0b2b6d12015-05-20 22:16:38 -0700809 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800810 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800811 * or -1 if no such objective exists. Note that this method does NOT create
812 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800813 * created at startup from network configuration. Typically this is used
814 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700815 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800816 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700817 * @return int if found or -1
818 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800819 public int getVlanNextObjectiveId(VlanId vlanId) {
820 Integer nextId = vlanNextObjStore.
821 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700822
823 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700824 }
825
826 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000827 * Returns the next objective of type simple associated with the mac/vlan on the
828 * device, given the treatment. Different treatments to the same mac/vlan result
829 * in different next objectives. If no such objective exists, this method
830 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
831 * the creation of the objective. Typically this is used for L2 and L3 forwarding
832 * to compute nodes and containers/VMs on the compute nodes directly attached
833 * to the switch.
834 *
835 * @param macAddr the mac addr for the simple next objective
836 * @param vlanId the vlan for the simple next objective
837 * @param treatment the actions to apply on the packets (should include outport)
838 * @param meta optional metadata passed into the creation of the next objective
839 * @param createIfMissing true if a next object should be created if not found
840 * @return int if found or created, -1 if there are errors during the
841 * creation of the next objective.
842 */
843 public int getMacVlanNextObjectiveId(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment,
844 TrafficSelector meta, boolean createIfMissing) {
845
846 Integer nextId = macVlanNextObjStore
847 .get(new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId));
848
849 if (nextId != null) {
850 return nextId;
851 }
852
853 log.debug("getMacVlanNextObjectiveId in device {}: Next objective id "
854 + "not found for host : {}/{} .. {}", deviceId, macAddr, vlanId,
855 (createIfMissing) ? "creating" : "aborting");
856
857 if (!createIfMissing) {
858 return -1;
859 }
860
861 /* create missing next objective */
862 nextId = createGroupFromMacVlan(macAddr, vlanId, treatment, meta);
863 if (nextId == null) {
864 log.warn("getMacVlanNextObjectiveId: unable to create next obj"
865 + "for dev:{} host:{}/{}", deviceId, macAddr, vlanId);
866 return -1;
867 }
868 return nextId;
869 }
870
871
872 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800873 * Returns the next objective of type simple associated with the port on the
874 * device, given the treatment. Different treatments to the same port result
875 * in different next objectives. If no such objective exists, this method
Saurav Das961beb22017-03-29 19:09:17 -0700876 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800877 * the creation of the objective. Typically this is used for L2 and L3 forwarding
878 * to compute nodes and containers/VMs on the compute nodes directly attached
879 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800880 *
881 * @param portNum the port number for the simple next objective
882 * @param treatment the actions to apply on the packets (should include outport)
883 * @param meta optional metadata passed into the creation of the next objective
Saurav Das961beb22017-03-29 19:09:17 -0700884 * @param createIfMissing true if a next object should be created if not found
Saurav Das4ce45962015-11-24 23:21:05 -0800885 * @return int if found or created, -1 if there are errors during the
886 * creation of the next objective.
887 */
888 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700889 TrafficSelector meta, boolean createIfMissing) {
Charles Chane849c192016-01-11 18:28:54 -0800890 Integer nextId = portNextObjStore
Saurav Das76ae6812017-03-15 15:15:14 -0700891 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das961beb22017-03-29 19:09:17 -0700892 if (nextId != null) {
893 return nextId;
894 }
895 log.debug("getPortNextObjectiveId in device {}: Next objective id "
896 + "not found for port: {} .. {}", deviceId, portNum,
897 (createIfMissing) ? "creating" : "aborting");
898 if (!createIfMissing) {
899 return -1;
900 }
901 // create missing next objective
902 createGroupFromPort(portNum, treatment, meta);
903 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
904 treatment, meta));
Saurav Das4ce45962015-11-24 23:21:05 -0800905 if (nextId == null) {
Saurav Das961beb22017-03-29 19:09:17 -0700906 log.warn("getPortNextObjectiveId: unable to create next obj"
907 + "for dev:{} port:{}", deviceId, portNum);
908 return -1;
Charles Chane849c192016-01-11 18:28:54 -0800909 }
910 return nextId;
911 }
912
913 /**
sangho0b2b6d12015-05-20 22:16:38 -0700914 * Checks if the next objective ID (group) for the neighbor set exists or not.
915 *
916 * @param ns neighbor set to check
917 * @return true if it exists, false otherwise
918 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700919 public boolean hasNextObjectiveId(DestinationSet ns) {
920 NextNeighbors nextHops = dsNextObjStore.
921 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
922 if (nextHops == null) {
sangho0b2b6d12015-05-20 22:16:38 -0700923 return false;
924 }
925
926 return true;
927 }
928
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800929 private void populateNeighborMaps() {
930 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700931 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800932 if (link.type() != Link.Type.DIRECT) {
933 continue;
934 }
935 addNeighborAtPort(link.dst().deviceId(), link.src().port());
936 }
937 }
938
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700939 protected void addNeighborAtPort(DeviceId neighborId,
940 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800941 // Update DeviceToPort database
942 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
943 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800944 Set<PortNumber> ports = Collections
945 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
946 ports.add(portToNeighbor);
947 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
948 if (portnums != null) {
949 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800950 }
951
952 // Update portToDevice database
Saurav Dasa4020382018-02-14 14:14:54 -0800953 // should always update as neighbor could have changed on this port
954 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800955 if (prev != null) {
Saurav Das5a356042018-04-06 20:16:01 -0700956 log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -0700957 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800958 }
959 }
960
sangho1e575652015-05-14 00:39:53 -0700961 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700962 * Creates a NextObjective for a hash group in this device from a given
Saurav Dasa4020382018-02-14 14:14:54 -0800963 * DestinationSet. If the parameter simple is true, a simple next objective
964 * is created instead.
sangho1e575652015-05-14 00:39:53 -0700965 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700966 * @param ds the DestinationSet
967 * @param neighbors a map for each destination and its next-hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800968 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800969 * @param simple if true, a simple next objective will be created instead of
970 * a hashed next objective
sangho1e575652015-05-14 00:39:53 -0700971 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700972 public void createGroupFromDestinationSet(DestinationSet ds,
973 Map<DeviceId, Set<DeviceId>> neighbors,
974 TrafficSelector meta,
Saurav Dasa4020382018-02-14 14:14:54 -0800975 boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700976 int nextId = flowObjectiveService.allocateNextId();
Saurav Dasa4020382018-02-14 14:14:54 -0800977 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
978 : NextObjective.Type.HASHED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700979 if (neighbors == null || neighbors.isEmpty()) {
980 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
981 + "to create group in dev:{} for ds: {} with next-hops {}",
982 deviceId, ds, neighbors);
983 return;
984 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700985
986 NextObjective.Builder nextObjBuilder = DefaultNextObjective
987 .builder()
988 .withId(nextId)
989 .withType(type)
990 .fromApp(appId);
991 if (meta != null) {
992 nextObjBuilder.withMeta(meta);
993 }
994
995 // create treatment buckets for each neighbor for each dst Device
996 // except in the special case where we only want to pick a single
Saurav Dasa4020382018-02-14 14:14:54 -0800997 // neighbor/port for a simple nextObj
Saurav Das7bcbe702017-06-13 15:35:54 -0700998 boolean foundSingleNeighbor = false;
999 boolean treatmentAdded = false;
1000 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
1001 for (DeviceId dst : ds.getDestinationSwitches()) {
1002 Set<DeviceId> nextHops = neighbors.get(dst);
1003 if (nextHops == null || nextHops.isEmpty()) {
1004 continue;
Pier Ventre917127a2016-10-31 16:49:19 -07001005 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001006
1007 if (foundSingleNeighbor) {
1008 break;
1009 }
1010
1011 for (DeviceId neighborId : nextHops) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001012 if (devicePortMap.get(neighborId) == null) {
1013 log.warn("Neighbor {} is not in the port map yet for dev:{}",
1014 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -07001015 return;
Jon Hallcbd1b392017-01-18 20:15:44 -08001016 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001017 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -08001018 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001019 return;
sangho834e4b02015-05-01 09:38:25 -07001020 }
1021
Saurav Das8a0732e2015-11-20 15:27:53 -08001022 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -08001023 try {
Saurav Das8a0732e2015-11-20 15:27:53 -08001024 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -08001025 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001026 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan0b4e6182015-11-03 10:42:14 -08001027 return;
1028 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001029 // For each port to the neighbor, we create a new treatment
Pier Ventre917127a2016-10-31 16:49:19 -07001030 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Dasa4020382018-02-14 14:14:54 -08001031 // In this case we need a SIMPLE nextObj. We randomly pick a port
1032 if (simple) {
Pier Ventre917127a2016-10-31 16:49:19 -07001033 int size = devicePortMap.get(neighborId).size();
1034 int index = RandomUtils.nextInt(0, size);
1035 neighborPorts = Collections.singleton(
Saurav Das7bcbe702017-06-13 15:35:54 -07001036 Iterables.get(devicePortMap.get(neighborId),
1037 index));
1038 foundSingleNeighbor = true;
Pier Ventre917127a2016-10-31 16:49:19 -07001039 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001040
Pier Ventre917127a2016-10-31 16:49:19 -07001041 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001042 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
1043 .builder();
Saurav Das7bcbe702017-06-13 15:35:54 -07001044 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
1045 int edgeLabel = ds.getEdgeLabel(dst);
1046 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001047 if (simple) {
1048 // swap label case
1049 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1050 } else {
1051 // ecmp with label push case
1052 tBuilder.pushMpls().copyTtlOut()
1053 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1054 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001055 }
Charles Chan69b7ae62018-08-12 15:19:01 -07001056
1057 // Set VLAN ID for PW transport. Otherwise pop vlan
Charles Chane0260e12018-08-22 17:56:47 -07001058 if (!popVlanInHashGroup(ds)) {
Saurav Das09c2c4d2018-08-13 15:34:26 -07001059 tBuilder.setVlanId(srManager.getPwTransportVlan());
Charles Chan69b7ae62018-08-12 15:19:01 -07001060 } else {
1061 tBuilder.popVlan();
Andreas Pantelopoulos192284c2018-05-01 14:56:05 -07001062 }
Charles Chan69b7ae62018-08-12 15:19:01 -07001063
Saurav Das8a0732e2015-11-20 15:27:53 -08001064 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001065 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das7bcbe702017-06-13 15:35:54 -07001066 treatmentAdded = true;
1067 //update store
1068 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
1069 if (existingNeighbors == null) {
1070 existingNeighbors = new HashSet<>();
1071 }
1072 existingNeighbors.add(neighborId);
1073 dstNextHops.put(dst, existingNeighbors);
1074 log.debug("creating treatment for port/label {}/{} in next:{}",
1075 sp, edgeLabel, nextId);
1076 }
1077
1078 if (foundSingleNeighbor) {
1079 break;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001080 }
1081 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001082 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001083
1084 if (!treatmentAdded) {
1085 log.warn("Could not createGroup from DestinationSet {} without any"
1086 + "next hops {}", ds, neighbors);
1087 return;
1088 }
1089 ObjectiveContext context = new DefaultObjectiveContext(
1090 (objective) ->
1091 log.debug("createGroupsFromDestinationSet installed "
1092 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001093 (objective, error) -> {
1094 log.warn("createGroupsFromDestinationSet failed to install NextObj {} on {}: {}",
1095 nextId, deviceId, error);
1096 srManager.invalidateNextObj(objective.id());
1097 });
Saurav Das7bcbe702017-06-13 15:35:54 -07001098 NextObjective nextObj = nextObjBuilder.add(context);
1099 log.debug(".. createGroupsFromDestinationSet: Submitted "
1100 + "next objective {} in device {}", nextId, deviceId);
1101 flowObjectiveService.next(deviceId, nextObj);
1102 //update store
1103 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1104 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001105 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001106
Saurav Das4ce45962015-11-24 23:21:05 -08001107 /**
Saurav Das1a129a02016-11-18 15:21:57 -08001108 * Creates broadcast groups for all ports in the same subnet for
1109 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -08001110 */
Charles Chan59cc16d2017-02-02 16:20:42 -08001111 public void createGroupsFromVlanConfig() {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001112 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001113 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -08001114 });
Saurav Das1a129a02016-11-18 15:21:57 -08001115 }
Charles Chan9f676b62015-10-29 14:58:10 -07001116
Saurav Das1a129a02016-11-18 15:21:57 -08001117 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001118 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -08001119 *
Charles Chan59cc16d2017-02-02 16:20:42 -08001120 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -08001121 * @param ports list of ports in the subnet
1122 */
Charles Chan7ffd81f2017-02-08 15:52:08 -08001123 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001124 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -07001125
Charles Chan59cc16d2017-02-02 16:20:42 -08001126 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -08001127 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -08001128 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001129 return;
1130 }
Charles Chan188ebf52015-12-23 00:15:11 -08001131
Saurav Das1a129a02016-11-18 15:21:57 -08001132 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -08001133 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -07001134
Saurav Das1a129a02016-11-18 15:21:57 -08001135 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -07001136
Saurav Das1a129a02016-11-18 15:21:57 -08001137 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1138 .builder().withId(nextId)
1139 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1140 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -07001141
Saurav Das1a129a02016-11-18 15:21:57 -08001142 ports.forEach(port -> {
1143 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -08001144 if (toPopVlan(port, vlanId)) {
1145 tBuilder.popVlan();
1146 }
Saurav Das1a129a02016-11-18 15:21:57 -08001147 tBuilder.setOutput(port);
1148 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -07001149 });
Saurav Das1a129a02016-11-18 15:21:57 -08001150
Saurav Das961beb22017-03-29 19:09:17 -07001151 ObjectiveContext context = new DefaultObjectiveContext(
1152 (objective) ->
1153 log.debug("createBroadcastGroupFromVlan installed "
1154 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001155 (objective, error) -> {
1156 log.warn("createBroadcastGroupFromVlan failed to install NextObj {} on {}: {}",
1157 nextId, deviceId, error);
1158 srManager.invalidateNextObj(objective.id());
1159 });
Saurav Das961beb22017-03-29 19:09:17 -07001160 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das1a129a02016-11-18 15:21:57 -08001161 flowObjectiveService.next(deviceId, nextObj);
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001162 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1163 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001164
Charles Chan59cc16d2017-02-02 16:20:42 -08001165 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001166 }
1167
Charles Chane849c192016-01-11 18:28:54 -08001168 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001169 * Removes a single broadcast group from a given vlan id.
1170 * The group should be empty.
1171 * @param deviceId device Id to remove the group
1172 * @param portNum port number related to the group
1173 * @param vlanId vlan id of the broadcast group to remove
1174 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1175 */
1176 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1177 VlanId vlanId, boolean popVlan) {
1178 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1179
1180 if (!vlanNextObjStore.containsKey(key)) {
1181 log.debug("Broadcast group for device {} and subnet {} does not exist",
1182 deviceId, vlanId);
1183 return;
1184 }
1185
1186 TrafficSelector metadata =
1187 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1188
1189 int nextId = vlanNextObjStore.get(key);
1190
1191 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1192 .builder().withId(nextId)
1193 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1194 .withMeta(metadata);
1195
1196 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1197 if (popVlan) {
1198 tBuilder.popVlan();
1199 }
1200 tBuilder.setOutput(portNum);
1201 nextObjBuilder.addTreatment(tBuilder.build());
1202
1203 ObjectiveContext context = new DefaultObjectiveContext(
1204 (objective) ->
1205 log.debug("removeBroadcastGroupFromVlan removed "
1206 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001207 (objective, error) -> {
1208 log.warn("removeBroadcastGroupFromVlan failed to remove NextObj {} on {}: {}",
1209 nextId, deviceId, error);
1210 srManager.invalidateNextObj(objective.id());
1211 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001212 NextObjective nextObj = nextObjBuilder.remove(context);
1213 flowObjectiveService.next(deviceId, nextObj);
1214 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1215 nextId, deviceId);
1216
1217 vlanNextObjStore.remove(key, nextId);
1218 }
1219
1220 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001221 * Determine if we should pop given vlan before sending packets to the given port.
1222 *
1223 * @param portNumber port number
1224 * @param vlanId vlan id
1225 * @return true if the vlan id is not contained in any vlanTagged config
1226 */
1227 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001228 return srManager.interfaceService
1229 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan7ffd81f2017-02-08 15:52:08 -08001230 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1231 }
1232
1233 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001234 * Create simple next objective for an indirect host mac/vlan. The treatments can include
1235 * all outgoing actions that need to happen on the packet.
1236 *
1237 * @param macAddr the mac address of the host
1238 * @param vlanId the vlan of the host
1239 * @param treatment the actions to apply on the packets (should include outport)
1240 * @param meta optional data to pass to the driver
1241 * @return next objective ID
1242 */
1243 public int createGroupFromMacVlan(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment,
1244 TrafficSelector meta) {
1245 int nextId = flowObjectiveService.allocateNextId();
1246 MacVlanNextObjectiveStoreKey key = new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId);
1247
1248 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1249 .builder().withId(nextId)
1250 .withType(NextObjective.Type.SIMPLE)
1251 .addTreatment(treatment)
1252 .fromApp(appId)
1253 .withMeta(meta);
1254
1255 ObjectiveContext context = new DefaultObjectiveContext(
1256 (objective) ->
1257 log.debug("createGroupFromMacVlan installed "
1258 + "NextObj {} on {}", nextId, deviceId),
1259 (objective, error) -> {
1260 log.warn("createGroupFromMacVlan failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1261 srManager.invalidateNextObj(objective.id());
1262 });
1263 NextObjective nextObj = nextObjBuilder.add(context);
1264 flowObjectiveService.next(deviceId, nextObj);
1265 log.debug("createGroupFromMacVlan: Submited next objective {} in device {} "
1266 + "for host {}/{}", nextId, deviceId, macAddr, vlanId);
1267
1268 macVlanNextObjStore.put(key, nextId);
1269 return nextId;
1270 }
1271
1272 /**
Saurav Das4ce45962015-11-24 23:21:05 -08001273 * Create simple next objective for a single port. The treatments can include
1274 * all outgoing actions that need to happen on the packet.
1275 *
1276 * @param portNum the outgoing port on the device
1277 * @param treatment the actions to apply on the packets (should include outport)
1278 * @param meta optional data to pass to the driver
1279 */
1280 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1281 TrafficSelector meta) {
1282 int nextId = flowObjectiveService.allocateNextId();
1283 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das76ae6812017-03-15 15:15:14 -07001284 deviceId, portNum, treatment, meta);
Saurav Das4ce45962015-11-24 23:21:05 -08001285
1286 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1287 .builder().withId(nextId)
1288 .withType(NextObjective.Type.SIMPLE)
1289 .addTreatment(treatment)
1290 .fromApp(appId)
1291 .withMeta(meta);
1292
Saurav Das961beb22017-03-29 19:09:17 -07001293 ObjectiveContext context = new DefaultObjectiveContext(
1294 (objective) ->
1295 log.debug("createGroupFromPort installed "
1296 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001297 (objective, error) -> {
1298 log.warn("createGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1299 srManager.invalidateNextObj(objective.id());
1300 });
Saurav Das961beb22017-03-29 19:09:17 -07001301 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das4ce45962015-11-24 23:21:05 -08001302 flowObjectiveService.next(deviceId, nextObj);
1303 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1304 + "for port {}", nextId, deviceId, portNum);
1305
1306 portNextObjStore.put(key, nextId);
1307 }
1308
sangho1e575652015-05-14 00:39:53 -07001309 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001310 * Removes simple next objective for a single port.
1311 *
1312 * @param deviceId device id that has the port to deal with
1313 * @param portNum the outgoing port on the device
1314 * @param vlanId vlan id associated with the port
1315 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1316 */
1317 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1318 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1319 mbuilder.matchVlanId(vlanId);
1320
1321 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1322 tbuilder.immediate().setOutput(portNum);
1323 if (popVlan) {
1324 tbuilder.immediate().popVlan();
1325 }
1326
1327 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1328 tbuilder.build(), mbuilder.build(), false);
1329
1330 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1331 deviceId, portNum, tbuilder.build(), mbuilder.build());
1332 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1333 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1334 .builder().withId(portNextObjId)
1335 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1336 ObjectiveContext context = new DefaultObjectiveContext(
1337 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1338 portNextObjId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001339 (objective, error) -> {
1340 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1341 portNextObjId, deviceId, error);
1342 srManager.invalidateNextObj(objective.id());
1343 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001344 NextObjective nextObjective = nextObjBuilder.remove(context);
1345 log.info("**removePortNextObjective: Submitted "
1346 + "next objective {} in device {}",
1347 portNextObjId, deviceId);
1348 flowObjectiveService.next(deviceId, nextObjective);
1349
1350 portNextObjStore.remove(key);
1351 }
1352 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001353
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001354 /**
sangho1e575652015-05-14 00:39:53 -07001355 * Removes groups for the next objective ID given.
1356 *
1357 * @param objectiveId next objective ID to remove
1358 * @return true if succeeds, false otherwise
1359 */
1360 public boolean removeGroup(int objectiveId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001361 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1362 dsNextObjStore.entrySet()) {
1363 if (e.getValue().nextId() != objectiveId) {
1364 continue;
1365 }
Pier Luigi63edd932018-01-14 21:56:11 +01001366 // Right now it is just used in TunnelHandler
1367 // remember in future that PW transit groups could
1368 // be Indirect groups
sangho1e575652015-05-14 00:39:53 -07001369 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1370 .builder().withId(objectiveId)
1371 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -07001372 ObjectiveContext context = new DefaultObjectiveContext(
1373 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1374 objectiveId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001375 (objective, error) -> {
1376 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", objectiveId, deviceId, error);
1377 srManager.invalidateNextObj(objective.id());
1378 });
Charles Chan216e3c82016-04-23 14:48:16 -07001379 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -08001380 log.info("**removeGroup: Submited "
1381 + "next objective {} in device {}",
1382 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -07001383 flowObjectiveService.next(deviceId, nextObjective);
1384
Saurav Das7bcbe702017-06-13 15:35:54 -07001385 dsNextObjStore.remove(e.getKey());
sangho0b2b6d12015-05-20 22:16:38 -07001386 return true;
sangho1e575652015-05-14 00:39:53 -07001387 }
1388
1389 return false;
1390 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001391 /**
1392 * Remove simple next objective for a single port. The treatments can include
1393 * all outgoing actions that need to happen on the packet.
1394 *
1395 * @param portNum the outgoing port on the device
1396 * @param treatment the actions applied on the packets (should include outport)
1397 * @param meta optional data to pass to the driver
1398 */
1399 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1400 TrafficSelector meta) {
1401 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1402 deviceId, portNum, treatment, meta);
1403 Integer nextId = portNextObjStore.get(key);
1404
1405 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1406 .builder().withId(nextId)
1407 .withType(NextObjective.Type.SIMPLE)
1408 .addTreatment(treatment)
1409 .fromApp(appId)
1410 .withMeta(meta);
1411
1412 ObjectiveContext context = new DefaultObjectiveContext(
1413 (objective) ->
1414 log.info("removeGroupFromPort installed "
1415 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001416 (objective, error) -> {
1417 log.warn("removeGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1418 srManager.invalidateNextObj(objective.id());
1419 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001420 );
1421 NextObjective nextObj = nextObjBuilder.remove(context);
1422 flowObjectiveService.next(deviceId, nextObj);
1423 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1424 + "for port {}", nextId, deviceId, portNum);
1425
1426 portNextObjStore.remove(key);
1427 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001428
Charles Chane849c192016-01-11 18:28:54 -08001429 /**
1430 * Removes all groups from all next objective stores.
1431 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001432 /*public void removeAllGroups() {
1433 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das423fe2b2015-12-04 10:52:59 -08001434 nsNextObjStore.entrySet()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001435 removeGroup(entry.getValue().nextId());
Saurav Das423fe2b2015-12-04 10:52:59 -08001436 }
1437 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1438 portNextObjStore.entrySet()) {
1439 removeGroup(entry.getValue());
1440 }
Charles Chan59cc16d2017-02-02 16:20:42 -08001441 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1442 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001443 removeGroup(entry.getValue());
1444 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001445 }*/ //XXX revisit
1446
Saurav Dasceccf242017-08-03 18:30:35 -07001447 /**
1448 * Triggers a one time bucket verification operation on all hash groups
1449 * on this device.
1450 */
1451 public void triggerBucketCorrector() {
1452 BucketCorrector bc = new BucketCorrector();
1453 bc.run();
1454 }
1455
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001456 /**
1457 * Modifies L2IG bucket when the interface configuration is updated, especially
1458 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1459 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1460 * TrafficTreatment in turn.
1461 *
1462 * @param portNumber the port on this device that needs to be updated
1463 * @param vlanId the vlan id corresponding to this port
1464 * @param pushVlan indicates if packets should be sent out untagged or not out
1465 * from the port. If true, updated TrafficTreatment involves
1466 * pop vlan tag action. If false, updated TrafficTreatment
1467 * does not involve pop vlan tag action.
1468 */
1469 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
1470 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1471 if (pushVlan) {
1472 tBuilder.popVlan();
1473 }
1474 tBuilder.setOutput(portNumber);
1475
1476 TrafficSelector metadata =
1477 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1478
1479 int nextId = getVlanNextObjectiveId(vlanId);
1480
1481 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1482 .builder().withId(nextId)
1483 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1484 .addTreatment(tBuilder.build())
1485 .withMeta(metadata);
1486
1487 ObjectiveContext context = new DefaultObjectiveContext(
1488 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1489 portNumber, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001490 (objective, error) -> {
1491 log.warn("port {} failed to updated NextObj {} on {}: {}", portNumber, nextId, deviceId, error);
1492 srManager.invalidateNextObj(objective.id());
1493 });
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001494
1495 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001496 }
1497
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001498 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001499 * Returns the next ID for the given host port from the store.
1500 *
1501 * @param hostMac mac of host for which Next ID is required.
1502 * @param hostVlanId vlan of host for which Next ID is required.
1503 * @param port port of device for which next ID is required.
1504 * @return next objective ID or -1 if an error occurred during retrieval
1505 */
1506 public int getNextIdForHostPort(MacAddress hostMac, VlanId hostVlanId, PortNumber port) {
1507
1508 MacAddress deviceMac;
1509 try {
1510 deviceMac = deviceConfig.getDeviceMac(deviceId);
1511 } catch (DeviceConfigNotFoundException e) {
1512 log.warn(e.getMessage() + " in getNextIdForHostPort");
1513 return -1;
1514 }
1515
1516 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1517 tbuilder.deferred()
1518 .setEthDst(hostMac)
1519 .setEthSrc(deviceMac)
1520 .setVlanId(hostVlanId)
1521 .setOutput(port);
1522
1523 TrafficSelector metadata =
1524 DefaultTrafficSelector.builder().matchVlanId(hostVlanId).build();
1525
1526 int nextId = getMacVlanNextObjectiveId(hostMac, hostVlanId,
1527 tbuilder.build(), metadata, true);
1528
1529 log.debug("getNextId : hostMac {}, deviceMac {}, port {}, hostVlanId {} Treatment {} Meta {} nextId {} ",
1530 hostMac, deviceMac, port, hostVlanId, tbuilder.build(), metadata, nextId);
1531
1532 return nextId;
1533 }
1534
1535 /**
1536 * Updates the next objective for the given nextId .
1537 *
1538 * @param hostMac mac of host for which Next obj is to be updated.
1539 * @param hostVlanId vlan of host for which Next obj is to be updated.
1540 * @param port port with which to update the Next Obj.
1541 * @param nextId of Next Obj which needs to be updated.
1542 */
1543 public void updateL3UcastGroupBucket(MacAddress hostMac, VlanId hostVlanId, PortNumber port, int nextId) {
1544
1545 MacAddress deviceMac;
1546 try {
1547 deviceMac = deviceConfig.getDeviceMac(deviceId);
1548 } catch (DeviceConfigNotFoundException e) {
1549 log.warn(e.getMessage() + " in updateL3UcastGroupBucket");
1550 return;
1551 }
1552
1553 TrafficSelector metadata =
1554 DefaultTrafficSelector.builder().matchVlanId(hostVlanId).build();
1555
1556 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1557 tbuilder.deferred()
1558 .setEthDst(hostMac)
1559 .setEthSrc(deviceMac)
1560 .setVlanId(hostVlanId)
1561 .setOutput(port);
1562
1563 log.debug(" update L3Ucast : deviceMac {}, port {}, host {}/{}, nextid {}, Treatment {} Meta {}",
1564 deviceMac, port, hostMac, hostVlanId, nextId, tbuilder.build(), metadata);
1565
1566 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1567 .builder().withId(nextId)
1568 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1569 .addTreatment(tbuilder.build())
1570 .withMeta(metadata);
1571
1572 ObjectiveContext context = new DefaultObjectiveContext(
1573 (objective) -> log.debug(" NextId {} successfully updated host {} vlan {} with port {}",
1574 nextId, hostMac, hostVlanId, port),
1575 (objective, error) -> {
1576 log.warn(" NextId {} failed to update host {} vlan {} with port {}, error : {}",
1577 nextId, hostMac, hostVlanId, port, error);
1578 srManager.invalidateNextObj(objective.id());
1579 });
1580
1581 NextObjective nextObj = nextObjBuilder.modify(context);
1582 flowObjectiveService.next(deviceId, nextObj);
1583
1584 }
1585
1586 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001587 * Adds a single port to the L2FG or removes it from the L2FG.
1588 *
1589 * @param vlanId the vlan id corresponding to this port
1590 * @param portNum the port on this device to be updated
1591 * @param nextId the next objective ID for the given vlan id
1592 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1593 */
1594 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001595 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1596 if (toPopVlan(portNum, vlanId)) {
1597 tBuilder.popVlan();
1598 }
1599 tBuilder.setOutput(portNum);
1600
1601 TrafficSelector metadata =
1602 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1603
1604 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1605 .builder().withId(nextId)
1606 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1607 .addTreatment(tBuilder.build())
1608 .withMeta(metadata);
1609
1610 ObjectiveContext context = new DefaultObjectiveContext(
1611 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1612 portNum, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001613 (objective, error) -> {
1614 log.warn("port {} failed to removedFrom NextObj {} on {}: {}", portNum, nextId, deviceId, error);
1615 srManager.invalidateNextObj(objective.id());
1616 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001617
1618 if (install) {
1619 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1620 } else {
1621 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1622 }
1623 }
Saurav Das1547b3f2017-05-05 17:01:08 -07001624
1625 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -07001626 * Performs bucket verification operation for all hash groups in this device.
1627 * Checks RouteHandler to ensure that routing is stable before attempting
1628 * verification. Verification involves creating a nextObjective with
1629 * operation VERIFY for existing next objectives in the store, and passing
1630 * it to the driver. It is the driver that actually performs the verification
1631 * by adding or removing buckets to match the verification next objective
1632 * created here.
Saurav Das1547b3f2017-05-05 17:01:08 -07001633 */
Saurav Dasceccf242017-08-03 18:30:35 -07001634 protected final class BucketCorrector implements Runnable {
1635 Integer nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001636
Saurav Dasceccf242017-08-03 18:30:35 -07001637 BucketCorrector() {
1638 this.nextId = null;
1639 }
1640
1641 BucketCorrector(Integer nextId) {
1642 this.nextId = nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001643 }
1644
1645 @Override
1646 public void run() {
Saurav Dasceccf242017-08-03 18:30:35 -07001647 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1648 return;
Saurav Das1547b3f2017-05-05 17:01:08 -07001649 }
Saurav Dasceccf242017-08-03 18:30:35 -07001650 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1651 if (rh == null) {
1652 return;
1653 }
1654 if (!rh.isRoutingStable()) {
1655 return;
1656 }
1657 rh.acquireRoutingLock();
1658 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001659 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07001660 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1661 .stream()
1662 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +01001663 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -08001664 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1665 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1666 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1667 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1668 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasceccf242017-08-03 18:30:35 -07001669 .map(entry -> entry.getKey())
1670 .collect(Collectors.toSet());
1671 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1672 NextNeighbors next = dsNextObjStore.get(dsKey);
1673 if (next == null) {
1674 continue;
1675 }
1676 int nid = next.nextId();
1677 if (nextId != null && nextId != nid) {
1678 continue;
1679 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001680 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasceccf242017-08-03 18:30:35 -07001681 deviceId, dsKey, next);
1682 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das09c2c4d2018-08-13 15:34:26 -07001683 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasceccf242017-08-03 18:30:35 -07001684 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1685 .withId(nid)
1686 .withType(NextObjective.Type.HASHED)
1687 .withMeta(metabuilder.build())
1688 .fromApp(appId);
1689
1690 next.dstNextHops().forEach((dstDev, nextHops) -> {
1691 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1692 nextHops.forEach(neighbor -> {
1693 MacAddress neighborMac;
1694 try {
1695 neighborMac = deviceConfig.getDeviceMac(neighbor);
1696 } catch (DeviceConfigNotFoundException e) {
1697 log.warn(e.getMessage() + " Aborting neighbor"
1698 + neighbor);
1699 return;
1700 }
1701 devicePortMap.get(neighbor).forEach(port -> {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001702 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasceccf242017-08-03 18:30:35 -07001703 + " port/label {}/{} to dst {} via {}",
1704 deviceId, nid, port, edgeLabel,
1705 dstDev, neighbor);
Saurav Dasa4020382018-02-14 14:14:54 -08001706 nextObjBuilder
1707 .addTreatment(treatmentBuilder(port,
1708 neighborMac,
1709 dsKey.destinationSet().swap(),
1710 edgeLabel));
Saurav Dasceccf242017-08-03 18:30:35 -07001711 });
1712 });
1713 });
1714
1715 NextObjective nextObjective = nextObjBuilder.verify();
1716 flowObjectiveService.next(deviceId, nextObjective);
1717 }
1718 } finally {
1719 rh.releaseRoutingLock();
1720 }
1721
1722 }
1723
1724 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
Saurav Dasa4020382018-02-14 14:14:54 -08001725 boolean swap, int edgeLabel) {
Saurav Dasceccf242017-08-03 18:30:35 -07001726 TrafficTreatment.Builder tBuilder =
1727 DefaultTrafficTreatment.builder();
1728 tBuilder.setOutput(outport)
1729 .setEthDst(dstMac)
1730 .setEthSrc(nodeMacAddr);
1731 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001732 if (swap) {
1733 // swap label case
1734 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1735 } else {
1736 // ecmp with label push case
1737 tBuilder.pushMpls()
1738 .copyTtlOut()
1739 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1740 }
Saurav Dasceccf242017-08-03 18:30:35 -07001741 }
1742 return tBuilder.build();
Saurav Das1547b3f2017-05-05 17:01:08 -07001743 }
1744 }
1745
Charles Chane0260e12018-08-22 17:56:47 -07001746 /**
1747 * Determines whether the hash group bucket should include a popVlan action.
1748 * We don't popVlan for PW.
1749 *
1750 * @param ds destination set
1751 * @return true if VLAN needs to be popped
1752 */
1753 private boolean popVlanInHashGroup(DestinationSet ds) {
1754 return (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.SWAP_NOT_BOS) &&
1755 (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.POP_NOT_BOS);
1756 }
Pier Luigi63edd932018-01-14 21:56:11 +01001757}