blob: 1ffd8e8a0786d85a782340a7ceb1ded3cb962029 [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;
Saurav Das7bcbe702017-06-13 15:35:54 -070047import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070048import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080049import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070050import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080051import org.slf4j.Logger;
52
Pier Ventre917127a2016-10-31 16:49:19 -070053import java.net.URI;
Charles Chan7ffd81f2017-02-08 15:52:08 -080054import java.util.Collection;
Pier Ventre917127a2016-10-31 16:49:19 -070055import java.util.Collections;
Saurav Dasc88d4662017-05-15 15:34:25 -070056import java.util.HashMap;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.HashSet;
58import java.util.List;
59import java.util.Map;
60import java.util.Set;
61import java.util.concurrent.ConcurrentHashMap;
Saurav Das1547b3f2017-05-05 17:01:08 -070062import java.util.concurrent.ScheduledExecutorService;
63import java.util.concurrent.TimeUnit;
Pier Ventre917127a2016-10-31 16:49:19 -070064import java.util.stream.Collectors;
65
66import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das1547b3f2017-05-05 17:01:08 -070067import static java.util.concurrent.Executors.newScheduledThreadPool;
68import static org.onlab.util.Tools.groupedThreads;
Pier Ventre917127a2016-10-31 16:49:19 -070069import static org.slf4j.LoggerFactory.getLogger;
70
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080071/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070072 * Default ECMP group handler creation module. This component creates a set of
73 * ECMP groups for every neighbor that this device is connected to based on
74 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080075 */
76public class DefaultGroupHandler {
Ray Milkey9c9cde42018-01-12 14:22:06 -080077 private static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080078
Saurav Dasceccf242017-08-03 18:30:35 -070079 private static final long VERIFY_INTERVAL = 30; // secs
80
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080081 protected final DeviceId deviceId;
82 protected final ApplicationId appId;
83 protected final DeviceProperties deviceConfig;
84 protected final List<Integer> allSegmentIds;
Pier Ventree0ae7a32016-11-23 09:57:42 -080085 protected int ipv4NodeSegmentId = -1;
86 protected int ipv6NodeSegmentId = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -080087 protected boolean isEdgeRouter = false;
88 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080089 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070090 protected FlowObjectiveService flowObjectiveService;
Saurav Dasc88d4662017-05-15 15:34:25 -070091 /**
92 * local store for neighbor-device-ids and the set of ports on this device
93 * that connect to the same neighbor.
94 */
Saurav Das8a0732e2015-11-20 15:27:53 -080095 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
96 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -070097 /**
98 * local store for ports on this device connected to neighbor-device-id.
99 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800100 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
101 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700102
Saurav Das7bcbe702017-06-13 15:35:54 -0700103 // distributed store for (device+destination-set) mapped to next-id and neighbors
104 protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
105 dsNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800106 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan59cc16d2017-02-02 16:20:42 -0800107 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
108 vlanNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800109 // distributed store for (device+port+treatment) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -0800110 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
111 portNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800112 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800113
Saurav Das1547b3f2017-05-05 17:01:08 -0700114 private ScheduledExecutorService executorService
Saurav Dasceccf242017-08-03 18:30:35 -0700115 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das1547b3f2017-05-05 17:01:08 -0700116
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700117 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700118 .register(URI.class).register(HashSet.class)
Saurav Das7bcbe702017-06-13 15:35:54 -0700119 .register(DeviceId.class).register(PortNumber.class)
120 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700121 .register(PolicyGroupParams.class)
122 .register(GroupBucketIdentifier.class)
123 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800124
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700125 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
126 DeviceProperties config,
127 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700128 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800129 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800130 this.deviceId = checkNotNull(deviceId);
131 this.appId = checkNotNull(appId);
132 this.deviceConfig = checkNotNull(config);
133 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800134 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
135 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800136 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
137 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800138 this.isEdgeRouter = config.isEdgeDevice(deviceId);
139 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
140 } catch (DeviceConfigNotFoundException e) {
141 log.warn(e.getMessage()
142 + " Skipping value assignment in DefaultGroupHandler");
143 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700144 this.flowObjectiveService = flowObjService;
Saurav Das7bcbe702017-06-13 15:35:54 -0700145 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeye4afdb52017-04-05 09:42:04 -0700146 this.vlanNextObjStore = srManager.vlanNextObjStore();
147 this.portNextObjStore = srManager.portNextObjStore();
Charles Chan188ebf52015-12-23 00:15:11 -0800148 this.srManager = srManager;
Saurav Dasceccf242017-08-03 18:30:35 -0700149 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
150 VERIFY_INTERVAL,
151 TimeUnit.SECONDS);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800152 populateNeighborMaps();
153 }
154
155 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700156 * Gracefully shuts down a groupHandler. Typically called when the handler is
157 * no longer needed.
158 */
159 public void shutdown() {
160 executorService.shutdown();
161 }
162
163 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700164 * Creates a group handler object.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800165 *
166 * @param deviceId device identifier
167 * @param appId application identifier
168 * @param config interface to retrieve the device properties
169 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700170 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800171 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800172 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800173 * @return default group handler type
174 */
Saurav Das4ce45962015-11-24 23:21:05 -0800175 public static DefaultGroupHandler createGroupHandler(
Saurav Dasceccf242017-08-03 18:30:35 -0700176 DeviceId deviceId,
177 ApplicationId appId,
178 DeviceProperties config,
179 LinkService linkService,
180 FlowObjectiveService flowObjService,
181 SegmentRoutingManager srManager)
182 throws DeviceConfigNotFoundException {
Saurav Dasc88d4662017-05-15 15:34:25 -0700183 return new DefaultGroupHandler(deviceId, appId, config,
184 linkService,
185 flowObjService,
186 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800187 }
188
189 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800190 * Updates local stores for link-src-device/port to neighbor (link-dst) for
191 * link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -0700192 *
193 * @param link the infrastructure link
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800194 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700195 public void portUpForLink(Link link) {
Saurav Dasceccf242017-08-03 18:30:35 -0700196 if (!link.src().deviceId().equals(deviceId)) {
197 log.warn("linkUp: deviceId{} doesn't match with link src {}",
198 deviceId, link.src().deviceId());
199 return;
200 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700201
Saurav Dasceccf242017-08-03 18:30:35 -0700202 log.info("* portUpForLink: Device {} linkUp at local port {} to "
203 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
204 // ensure local state is updated even if linkup is aborted later on
205 addNeighborAtPort(link.dst().deviceId(),
206 link.src().port());
207 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700208
Saurav Dasceccf242017-08-03 18:30:35 -0700209 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800210 * Updates local stores for link-src-device/port to neighbor (link-dst) for
211 * link that has gone down.
Saurav Dasceccf242017-08-03 18:30:35 -0700212 *
Saurav Dasa4020382018-02-14 14:14:54 -0800213 * @param link the infrastructure link
Saurav Dasceccf242017-08-03 18:30:35 -0700214 */
Saurav Dasa4020382018-02-14 14:14:54 -0800215 public void portDownForLink(Link link) {
216 PortNumber port = link.src().port();
Saurav Dasceccf242017-08-03 18:30:35 -0700217 if (portDeviceMap.get(port) == null) {
218 log.warn("portDown: unknown port");
219 return;
220 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700221
Saurav Dasceccf242017-08-03 18:30:35 -0700222 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
223 portDeviceMap.get(port));
224 devicePortMap.get(portDeviceMap.get(port)).remove(port);
225 portDeviceMap.remove(port);
226 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800227
228 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800229 * Cleans up local stores for removed neighbor device.
230 *
231 * @param neighborId the device identifier for the neighbor device
232 */
233 public void cleanUpForNeighborDown(DeviceId neighborId) {
234 Set<PortNumber> ports = devicePortMap.remove(neighborId);
235 if (ports != null) {
236 ports.forEach(p -> portDeviceMap.remove(p));
237 }
238 }
239
240 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700241 * Checks all groups in the src-device of link for neighbor sets that include
242 * the dst-device of link, and edits the hash groups according to link up
243 * or down. Should only be called by the master instance of the src-switch
244 * of link. Typically used when there are no route-path changes due to the
245 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800246 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700247 * @param link the infrastructure link that has gone down or come up
248 * @param linkDown true if link has gone down
249 * @param firstTime true if link has come up for the first time i.e a link
250 * not seen-before
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800251 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700252 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700253 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800254 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700255 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800256 } catch (DeviceConfigNotFoundException e) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700257 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800258 return;
259 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700260 // find all the destinationSets related to link
261 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700262 .stream()
Saurav Das7bcbe702017-06-13 15:35:54 -0700263 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +0100264 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -0800265 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
266 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
267 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
268 .filter(entry -> !entry.getKey().destinationSet().swap() ||
269 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Das7bcbe702017-06-13 15:35:54 -0700270 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
271 .map(entry -> entry.getKey())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700272 .collect(Collectors.toSet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700273
Saurav Das7bcbe702017-06-13 15:35:54 -0700274 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
275 deviceId, link.dst().deviceId(), dsKeySet);
276
277 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
278 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
279 if (nextHops == null) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700280 log.warn("retryHash in device {}, but global store has no record "
Saurav Das7bcbe702017-06-13 15:35:54 -0700281 + "for dsKey:{}", deviceId, dsKey);
Saurav Dasc88d4662017-05-15 15:34:25 -0700282 continue;
283 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700284 int nextId = nextHops.nextId();
285 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -0700286 if (!linkDown) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700287 List<PortLabel> pl = Lists.newArrayList();
Saurav Dasc88d4662017-05-15 15:34:25 -0700288 if (firstTime) {
289 // some links may have come up before the next-objective was created
290 // we take this opportunity to ensure other ports to same next-hop-dst
291 // are part of the hash group (see CORD-1180). Duplicate additions
292 // to the same hash group are avoided by the driver.
293 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700294 dstSet.forEach(dst -> {
295 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700296 pl.add(new PortLabel(p, edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das7bcbe702017-06-13 15:35:54 -0700297 });
Saurav Dasc88d4662017-05-15 15:34:25 -0700298 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700299 addToHashedNextObjective(pl, neighborMac, nextId);
300 } else {
301 // handle only the port that came up
302 dstSet.forEach(dst -> {
303 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700304 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das9df5b7c2017-08-14 16:44:43 -0700305 });
306 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700307 }
308 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700309 // linkdown
310 List<PortLabel> pl = Lists.newArrayList();
Saurav Das7bcbe702017-06-13 15:35:54 -0700311 dstSet.forEach(dst -> {
312 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Charles Chane0260e12018-08-22 17:56:47 -0700313 pl.add(new PortLabel(link.src().port(), edgeLabel, popVlanInHashGroup(dsKey.destinationSet())));
Saurav Das7bcbe702017-06-13 15:35:54 -0700314 });
Saurav Das9df5b7c2017-08-14 16:44:43 -0700315 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700316 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800317 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700318 }
319
Saurav Dasc88d4662017-05-15 15:34:25 -0700320 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700321 * Utility class for associating output ports and the corresponding MPLS
322 * labels to push. In dual-homing, there are different labels to push
323 * corresponding to the destination switches in an edge-pair. If both
324 * destinations are reachable via the same spine, then the output-port to
325 * the spine will be associated with two labels i.e. there will be two
326 * PortLabel objects for the same port but with different labels.
327 */
328 private class PortLabel {
329 PortNumber port;
330 int edgeLabel;
Charles Chane0260e12018-08-22 17:56:47 -0700331 boolean popVlan;
Saurav Das9df5b7c2017-08-14 16:44:43 -0700332
Charles Chane0260e12018-08-22 17:56:47 -0700333 PortLabel(PortNumber port, int edgeLabel, boolean popVlan) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700334 this.port = port;
335 this.edgeLabel = edgeLabel;
Charles Chane0260e12018-08-22 17:56:47 -0700336 this.popVlan = popVlan;
Saurav Das9df5b7c2017-08-14 16:44:43 -0700337 }
338
339 @Override
340 public String toString() {
Charles Chane0260e12018-08-22 17:56:47 -0700341 return port.toString() + "/" + String.valueOf(edgeLabel) + (popVlan ? "/popVlan" : "");
Saurav Das9df5b7c2017-08-14 16:44:43 -0700342 }
343 }
344
345 /**
346 * Makes a call to the FlowObjective service to add buckets to
347 * a hashed group. User must ensure that all the ports & labels are meant
348 * same neighbor (ie. dstMac).
Saurav Dasc88d4662017-05-15 15:34:25 -0700349 *
Pier Luigi63edd932018-01-14 21:56:11 +0100350 * @param portLabels a collection of port & label combinations to add
Saurav Das9df5b7c2017-08-14 16:44:43 -0700351 * to the hash group identified by the nextId
Saurav Dasc88d4662017-05-15 15:34:25 -0700352 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700353 * @param nextId id for next-objective to which buckets will be added
Saurav Dasceccf242017-08-03 18:30:35 -0700354 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700355 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700356 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
357 MacAddress dstMac, Integer nextId) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700358 // setup metadata to pass to nextObjective - indicate the vlan on egress
359 // if needed by the switch pipeline. Since hashed next-hops are always to
360 // other neighboring routers, there is no subnet assigned on those ports.
361 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das09c2c4d2018-08-13 15:34:26 -0700362 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Das1547b3f2017-05-05 17:01:08 -0700363 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
364 .withId(nextId)
365 .withType(NextObjective.Type.HASHED)
Saurav Das1547b3f2017-05-05 17:01:08 -0700366 .withMeta(metabuilder.build())
367 .fromApp(appId);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700368 // Create the new buckets to be updated
369 portLabels.forEach(pl -> {
370 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
371 tBuilder.setOutput(pl.port)
372 .setEthDst(dstMac)
373 .setEthSrc(nodeMacAddr);
Charles Chane0260e12018-08-22 17:56:47 -0700374 if (pl.popVlan) {
375 tBuilder.popVlan();
376 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700377 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
378 tBuilder.pushMpls()
379 .copyTtlOut()
380 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
381 }
382 nextObjBuilder.addTreatment(tBuilder.build());
383 });
384
385 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
386 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700387
388 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das9df5b7c2017-08-14 16:44:43 -0700389 (objective) -> log.debug("addToHash port/label {} addedTo "
390 + "NextObj {} on {}", portLabels, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700391 (objective, error) -> {
392 log.warn("addToHash failed to add port/label {} to NextObj {} on {}: {}",
393 portLabels, nextId, deviceId, error);
394 srManager.invalidateNextObj(objective.id());
395 });
Saurav Das1547b3f2017-05-05 17:01:08 -0700396 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
397 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800398 }
399
400 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700401 * Makes a call to the FlowObjective service to remove buckets from
402 * a hash group. User must ensure that all the ports & labels are meant
403 * same neighbor (ie. dstMac).
Saurav Dasceccf242017-08-03 18:30:35 -0700404 *
Pier Luigi63edd932018-01-14 21:56:11 +0100405 * @param portLabels a collection of port & label combinations to remove
Saurav Das9df5b7c2017-08-14 16:44:43 -0700406 * from the hash group identified by the nextId
Saurav Dasceccf242017-08-03 18:30:35 -0700407 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700408 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasceccf242017-08-03 18:30:35 -0700409 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700410 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
411 MacAddress dstMac, Integer nextId) {
Saurav Dasceccf242017-08-03 18:30:35 -0700412 NextObjective.Builder nextObjBuilder = DefaultNextObjective
413 .builder()
414 .withType(NextObjective.Type.HASHED) //same as original
415 .withId(nextId)
Saurav Das9df5b7c2017-08-14 16:44:43 -0700416 .fromApp(appId);
417 // Create the buckets to be removed
418 portLabels.forEach(pl -> {
419 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
420 tBuilder.setOutput(pl.port)
421 .setEthDst(dstMac)
422 .setEthSrc(nodeMacAddr);
Charles Chane0260e12018-08-22 17:56:47 -0700423 if (pl.popVlan) {
424 tBuilder.popVlan();
425 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700426 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
427 tBuilder.pushMpls()
428 .copyTtlOut()
429 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
430 }
431 nextObjBuilder.addTreatment(tBuilder.build());
432 });
433 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
434 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700435
Saurav Das9df5b7c2017-08-14 16:44:43 -0700436 ObjectiveContext context = new DefaultObjectiveContext(
437 (objective) -> log.debug("port/label {} removedFrom NextObj"
438 + " {} on {}", portLabels, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700439 (objective, error) -> {
440 log.warn("port/label {} failed to removeFrom NextObj {} on {}: {}",
441 portLabels, nextId, deviceId, error);
442 srManager.invalidateNextObj(objective.id());
443 });
Saurav Das9df5b7c2017-08-14 16:44:43 -0700444 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasceccf242017-08-03 18:30:35 -0700445 flowObjectiveService.next(deviceId, nextObjective);
446 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700447
448 /**
449 * Checks all the hash-groups in the target-switch meant for the destination
450 * switch, and either adds or removes buckets to make the neighbor-set
451 * match the given next-hops. Typically called by the master instance of the
452 * destination switch, which may be different from the master instance of the
453 * target switch where hash-group changes are made.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800454 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700455 * @param targetSw the switch in which the hash groups will be edited
456 * @param nextHops the current next hops for the target switch to reach
457 * the dest sw
458 * @param destSw the destination switch
459 * @param revoke true if hash groups need to remove buckets from the
460 * the groups to match the current next hops
461 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800462 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700463 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
464 DeviceId destSw, boolean revoke) {
465 // temporary storage of keys to be updated
Saurav Das7bcbe702017-06-13 15:35:54 -0700466 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Dasc88d4662017-05-15 15:34:25 -0700467 new HashMap<>();
Saurav Das9df5b7c2017-08-14 16:44:43 -0700468 boolean foundNextObjective = false, success = true;
Charles Chan0b4e6182015-11-03 10:42:14 -0800469
Saurav Das7bcbe702017-06-13 15:35:54 -0700470 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Dasc88d4662017-05-15 15:34:25 -0700471 // with different neighbors than the given next-hops
Saurav Das7bcbe702017-06-13 15:35:54 -0700472 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
473 if (!dskey.deviceId().equals(targetSw) ||
474 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700475 continue;
476 }
477 foundNextObjective = true;
Saurav Das7bcbe702017-06-13 15:35:54 -0700478 NextNeighbors nhops = dsNextObjStore.get(dskey);
479 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
480 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
481 Integer nextId = nhops.nextId();
Saurav Dase0d4c872018-03-05 14:37:16 -0800482 if (currNeighbors == null || nextHops == null) {
483 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
484 + " in targetSw:{} for dstSw:{}", currNeighbors,
485 nextHops, targetSw, destSw);
486 success &= false;
487 continue;
488 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800489
Saurav Dasa4020382018-02-14 14:14:54 -0800490 // some store elements may not be hashed next-objectives - ignore them
491 if (isSimpleNextObjective(dskey)) {
492 log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}"
493 + " -> dstSw:{} with current nextHops:{} to new"
494 + " nextHops: {} in nextId:{}",
495 (revoke) ? "removal" : "addition", targetSw, destSw,
496 currNeighbors, nextHops, nextId);
Saurav Dase0d4c872018-03-05 14:37:16 -0800497 if ((revoke && !nextHops.isEmpty())
498 || (!revoke && !nextHops.equals(currNeighbors))) {
Saurav Das137f27f2018-06-11 17:02:31 -0700499 log.debug("Simple next objective cannot be edited to "
Saurav Dasa4020382018-02-14 14:14:54 -0800500 + "move from {} to {}", currNeighbors, nextHops);
501 }
502 continue;
503 }
504
Saurav Dasc88d4662017-05-15 15:34:25 -0700505 Set<DeviceId> diff;
506 if (revoke) {
507 diff = Sets.difference(currNeighbors, nextHops);
508 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
509 + "hops:{} ..removing {}", targetSw, destSw, nextId,
510 currNeighbors, diff);
511 } else {
512 diff = Sets.difference(nextHops, currNeighbors);
513 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
514 + "hops:{} ..adding {}", targetSw, destSw, nextId,
515 currNeighbors, diff);
516 }
Charles Chane0260e12018-08-22 17:56:47 -0700517 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, popVlanInHashGroup(dskey.destinationSet()),
Saurav Das9df5b7c2017-08-14 16:44:43 -0700518 revoke);
519 if (suc) {
520 // to update neighbor set with changes made
Saurav Dasc88d4662017-05-15 15:34:25 -0700521 if (revoke) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700522 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700523 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700524 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700525 }
sangho834e4b02015-05-01 09:38:25 -0700526 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700527 success &= suc;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800528 }
529
Saurav Dasc88d4662017-05-15 15:34:25 -0700530 if (!foundNextObjective) {
531 log.debug("Cannot find any nextObjectives for route targetSw:{} "
532 + "-> dstSw:{}", targetSw, destSw);
533 return true; // nothing to do, return true so ECMPspg is updated
534 }
535
Saurav Das7bcbe702017-06-13 15:35:54 -0700536 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
537 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700538 NextNeighbors currentNextHops = dsNextObjStore.get(key);
539 if (currentNextHops == null) {
540 log.warn("fixHashGroups could not update global store in "
541 + "device {} .. missing nextNeighbors for key {}",
542 deviceId, key);
Saurav Dasc88d4662017-05-15 15:34:25 -0700543 continue;
544 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700545 Set<DeviceId> newNeighbors = new HashSet<>();
546 newNeighbors.addAll(tempStore.get(key));
547 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
548 ImmutableMap.copyOf(currentNextHops.dstNextHops());
549 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
550 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
551 targetSw, destSw, key, currentNextHops.nextId());
552 log.debug("Old dstNextHops: {}", oldDstNextHops);
553 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
554 // update global store
555 dsNextObjStore.put(key,
556 new NextNeighbors(currentNextHops.dstNextHops(),
557 currentNextHops.nextId()));
Saurav Dasc88d4662017-05-15 15:34:25 -0700558 }
Saurav Dasa4020382018-02-14 14:14:54 -0800559
Saurav Das9df5b7c2017-08-14 16:44:43 -0700560 // even if one fails and others succeed, return false so ECMPspg not updated
561 return success;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800562 }
563
Saurav Dasceccf242017-08-03 18:30:35 -0700564 /**
565 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
566 * that are not already in the store for the given DestinationSet. Note that
567 * this method does not remove existing next hops for the destinations in the
568 * DestinationSet.
569 *
570 * @param ds the DestinationSet for which the next hops need to be updated
571 * @param newDstNextHops a map of per-destination next hops to update the
572 * destinationSet with
573 * @return true if successful in updating all next hops
574 */
575 private boolean updateNextHops(DestinationSet ds,
Saurav Das7bcbe702017-06-13 15:35:54 -0700576 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
577 DestinationSetNextObjectiveStoreKey key =
578 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
579 NextNeighbors currNext = dsNextObjStore.get(key);
580 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
581
582 // add newDstNextHops to currDstNextHops for each dst
583 boolean success = true;
584 for (DeviceId dstSw : ds.getDestinationSwitches()) {
585 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
586 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
587 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
588 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
589 int edgeLabel = ds.getEdgeLabel(dstSw);
590 int nextId = currNext.nextId();
591
592 // new next hops should be added
593 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
Charles Chane0260e12018-08-22 17:56:47 -0700594 edgeLabel, nextId, popVlanInHashGroup(key.destinationSet()), false);
Saurav Das7bcbe702017-06-13 15:35:54 -0700595 if (suc) {
596 currNhops.addAll(newNhops);
597 currDstNextHops.put(dstSw, currNhops); // this is only a local change
598 }
599 success &= suc;
600 }
601
602 if (success) {
603 // update global store
604 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
605 currNext.nextId()));
606 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
607 dsNextObjStore.get(key));
608 }
609 return success;
610 }
611
Saurav Dasceccf242017-08-03 18:30:35 -0700612 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700613 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
614 * needs to ensure that the given neighbors are all next hops towards the
615 * same destination (represented by the given edgeLabel).
Saurav Dasceccf242017-08-03 18:30:35 -0700616 *
617 * @param neighbors set of neighbor device ids
618 * @param edgeLabel MPLS label to use in buckets
619 * @param nextId the nextObjective to change
Charles Chane0260e12018-08-22 17:56:47 -0700620 * @param popVlan this hash group bucket shuold includes a popVlan action
Saurav Dasceccf242017-08-03 18:30:35 -0700621 * @param revoke true if buckets need to be removed, false if they need to
622 * be added
623 * @return true if successful in adding or removing buckets for all ports
624 * to the neighbors
625 */
626 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Charles Chane0260e12018-08-22 17:56:47 -0700627 int nextId, boolean popVlan, boolean revoke) {
Saurav Dasceccf242017-08-03 18:30:35 -0700628 for (DeviceId neighbor : neighbors) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700629 MacAddress neighborMac;
Saurav Das7bcbe702017-06-13 15:35:54 -0700630 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700631 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das7bcbe702017-06-13 15:35:54 -0700632 } catch (DeviceConfigNotFoundException e) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700633 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
634 + " for nextId:" + nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700635 return false;
636 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700637 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
638 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700639 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Das9df5b7c2017-08-14 16:44:43 -0700640 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700641 deviceId, neighbor, nextId);
642 return false;
643 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700644 List<PortLabel> pl = Lists.newArrayList();
Charles Chane0260e12018-08-22 17:56:47 -0700645 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel, popVlan)));
Saurav Das7bcbe702017-06-13 15:35:54 -0700646 if (revoke) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700647 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
648 + "with Port/Label:{} to next object id {}",
649 deviceId, pl, nextId);
650 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700651 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700652 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
653 + "with Port/Label: {} to next object id {}",
654 deviceId, pl, nextId);
655 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700656 }
657 }
658 return true;
659 }
660
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800661 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800662 * Returns true if the destination set is meant for swap or multi-labeled
663 * packet transport, and MPLS ECMP is not supported.
664 *
665 * @param dskey the key representing the destination set
666 * @return true if destination set is meant for simple next objectives
667 */
668 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
669 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
670 && !srManager.getMplsEcmp();
671 }
672
673 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800674 * Adds or removes a port that has been configured with a vlan to a broadcast group
675 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800676 *
677 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800678 * @param vlanId the vlan id corresponding to the broadcast domain/group
679 * @param popVlan indicates if packets should be sent out untagged or not out
680 * of the port. If true, indicates an access (untagged) or native vlan
681 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800682 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800683 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800684 public void processEdgePort(PortNumber port, VlanId vlanId,
685 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800686 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800687 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800688 if (nextId == -1) {
689 if (portUp) {
690 log.debug("**Creating flooding group for first port enabled in"
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800691 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan59cc16d2017-02-02 16:20:42 -0800692 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800693 } else {
694 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800695 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800696 }
697 return;
698 }
699
700 log.info("**port{} in device {}: {} Bucket with Port {} to"
701 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
702 (portUp) ? "Adding" : "Removing",
703 port, nextId);
704 // Create the bucket to be added or removed
705 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800706 if (popVlan) {
707 tBuilder.popVlan();
708 }
Saurav Das1a129a02016-11-18 15:21:57 -0800709 tBuilder.setOutput(port);
710
Saurav Das1a129a02016-11-18 15:21:57 -0800711 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800712 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800713
714 NextObjective.Builder nextObjBuilder = DefaultNextObjective
715 .builder().withId(nextId)
716 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
717 .addTreatment(tBuilder.build())
718 .withMeta(metadata);
719
720 ObjectiveContext context = new DefaultObjectiveContext(
721 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
722 port, (portUp) ? "addedTo" : "removedFrom",
723 nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -0700724 (objective, error) -> {
725 log.warn("port {} failed to {} NextObj {} on {}: {}",
726 port, (portUp) ? "addTo" : "removeFrom", nextId, deviceId, error);
727 srManager.invalidateNextObj(objective.id());
728 });
Saurav Das1a129a02016-11-18 15:21:57 -0800729
730 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
731 : nextObjBuilder.removeFromExisting(context);
732 log.debug("edgePort processed: Submited next objective {} in device {}",
733 nextId, deviceId);
734 flowObjectiveService.next(deviceId, nextObj);
735 }
736
737 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800738 * Returns the next objective of type hashed (or simple) associated with the
739 * destination set. In addition, updates the existing next-objective if new
740 * route-paths found have resulted in the addition of new next-hops to a
741 * particular destination. If there is no existing next objective for this
742 * destination set, this method would create a next objective and return the
743 * nextId. Optionally metadata can be passed in for the creation of the next
744 * objective. If the parameter simple is true then a simple next objective
745 * is created instead of a hashed one.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800746 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700747 * @param ds destination set
748 * @param nextHops a map of per destination next hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800749 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800750 * @param simple if true, a simple next objective will be created instead of
751 * a hashed next objective
Saurav Das8a0732e2015-11-20 15:27:53 -0800752 * @return int if found or -1 if there are errors in the creation of the
Saurav Dasa4020382018-02-14 14:14:54 -0800753 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800754 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700755 public int getNextObjectiveId(DestinationSet ds,
756 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Dasa4020382018-02-14 14:14:54 -0800757 TrafficSelector meta, boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700758 NextNeighbors next = dsNextObjStore.
759 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
760 if (next == null) {
761 log.debug("getNextObjectiveId in device{}: Next objective id "
762 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700763 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
764 deviceId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700765 dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700766 .stream()
767 .filter((nsStoreEntry) ->
768 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
769 .collect(Collectors.toList()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700770
Saurav Dasa4020382018-02-14 14:14:54 -0800771 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das7bcbe702017-06-13 15:35:54 -0700772 next = dsNextObjStore.
773 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
774 if (next == null) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700775 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das7bcbe702017-06-13 15:35:54 -0700776 // failure in creating group
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700777 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700778 } else {
779 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700780 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700781 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700782 } else {
783 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700784 + "found for {}", deviceId, next.nextId(), ds);
785 // should fix hash groups too if next-hops have changed
786 if (!next.dstNextHops().equals(nextHops)) {
787 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
788 deviceId, next.nextId());
789 if (!updateNextHops(ds, nextHops)) {
790 // failure in updating group
791 return -1;
792 }
793 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700794 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700795 return next.nextId();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800796 }
797
sangho0b2b6d12015-05-20 22:16:38 -0700798 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800799 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800800 * or -1 if no such objective exists. Note that this method does NOT create
801 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800802 * created at startup from network configuration. Typically this is used
803 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700804 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800805 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700806 * @return int if found or -1
807 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800808 public int getVlanNextObjectiveId(VlanId vlanId) {
809 Integer nextId = vlanNextObjStore.
810 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700811
812 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700813 }
814
815 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800816 * Returns the next objective of type simple associated with the port on the
817 * device, given the treatment. Different treatments to the same port result
818 * in different next objectives. If no such objective exists, this method
Saurav Das961beb22017-03-29 19:09:17 -0700819 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800820 * the creation of the objective. Typically this is used for L2 and L3 forwarding
821 * to compute nodes and containers/VMs on the compute nodes directly attached
822 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800823 *
824 * @param portNum the port number for the simple next objective
825 * @param treatment the actions to apply on the packets (should include outport)
826 * @param meta optional metadata passed into the creation of the next objective
Saurav Das961beb22017-03-29 19:09:17 -0700827 * @param createIfMissing true if a next object should be created if not found
Saurav Das4ce45962015-11-24 23:21:05 -0800828 * @return int if found or created, -1 if there are errors during the
829 * creation of the next objective.
830 */
831 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700832 TrafficSelector meta, boolean createIfMissing) {
Charles Chane849c192016-01-11 18:28:54 -0800833 Integer nextId = portNextObjStore
Saurav Das76ae6812017-03-15 15:15:14 -0700834 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das961beb22017-03-29 19:09:17 -0700835 if (nextId != null) {
836 return nextId;
837 }
838 log.debug("getPortNextObjectiveId in device {}: Next objective id "
839 + "not found for port: {} .. {}", deviceId, portNum,
840 (createIfMissing) ? "creating" : "aborting");
841 if (!createIfMissing) {
842 return -1;
843 }
844 // create missing next objective
845 createGroupFromPort(portNum, treatment, meta);
846 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
847 treatment, meta));
Saurav Das4ce45962015-11-24 23:21:05 -0800848 if (nextId == null) {
Saurav Das961beb22017-03-29 19:09:17 -0700849 log.warn("getPortNextObjectiveId: unable to create next obj"
850 + "for dev:{} port:{}", deviceId, portNum);
851 return -1;
Charles Chane849c192016-01-11 18:28:54 -0800852 }
853 return nextId;
854 }
855
856 /**
sangho0b2b6d12015-05-20 22:16:38 -0700857 * Checks if the next objective ID (group) for the neighbor set exists or not.
858 *
859 * @param ns neighbor set to check
860 * @return true if it exists, false otherwise
861 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700862 public boolean hasNextObjectiveId(DestinationSet ns) {
863 NextNeighbors nextHops = dsNextObjStore.
864 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
865 if (nextHops == null) {
sangho0b2b6d12015-05-20 22:16:38 -0700866 return false;
867 }
868
869 return true;
870 }
871
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800872 private void populateNeighborMaps() {
873 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700874 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800875 if (link.type() != Link.Type.DIRECT) {
876 continue;
877 }
878 addNeighborAtPort(link.dst().deviceId(), link.src().port());
879 }
880 }
881
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700882 protected void addNeighborAtPort(DeviceId neighborId,
883 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800884 // Update DeviceToPort database
885 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
886 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800887 Set<PortNumber> ports = Collections
888 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
889 ports.add(portToNeighbor);
890 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
891 if (portnums != null) {
892 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800893 }
894
895 // Update portToDevice database
Saurav Dasa4020382018-02-14 14:14:54 -0800896 // should always update as neighbor could have changed on this port
897 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800898 if (prev != null) {
Saurav Das5a356042018-04-06 20:16:01 -0700899 log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -0700900 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800901 }
902 }
903
sangho1e575652015-05-14 00:39:53 -0700904 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700905 * Creates a NextObjective for a hash group in this device from a given
Saurav Dasa4020382018-02-14 14:14:54 -0800906 * DestinationSet. If the parameter simple is true, a simple next objective
907 * is created instead.
sangho1e575652015-05-14 00:39:53 -0700908 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700909 * @param ds the DestinationSet
910 * @param neighbors a map for each destination and its next-hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800911 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800912 * @param simple if true, a simple next objective will be created instead of
913 * a hashed next objective
sangho1e575652015-05-14 00:39:53 -0700914 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700915 public void createGroupFromDestinationSet(DestinationSet ds,
916 Map<DeviceId, Set<DeviceId>> neighbors,
917 TrafficSelector meta,
Saurav Dasa4020382018-02-14 14:14:54 -0800918 boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700919 int nextId = flowObjectiveService.allocateNextId();
Saurav Dasa4020382018-02-14 14:14:54 -0800920 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
921 : NextObjective.Type.HASHED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700922 if (neighbors == null || neighbors.isEmpty()) {
923 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
924 + "to create group in dev:{} for ds: {} with next-hops {}",
925 deviceId, ds, neighbors);
926 return;
927 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700928
929 NextObjective.Builder nextObjBuilder = DefaultNextObjective
930 .builder()
931 .withId(nextId)
932 .withType(type)
933 .fromApp(appId);
934 if (meta != null) {
935 nextObjBuilder.withMeta(meta);
936 }
937
938 // create treatment buckets for each neighbor for each dst Device
939 // except in the special case where we only want to pick a single
Saurav Dasa4020382018-02-14 14:14:54 -0800940 // neighbor/port for a simple nextObj
Saurav Das7bcbe702017-06-13 15:35:54 -0700941 boolean foundSingleNeighbor = false;
942 boolean treatmentAdded = false;
943 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
944 for (DeviceId dst : ds.getDestinationSwitches()) {
945 Set<DeviceId> nextHops = neighbors.get(dst);
946 if (nextHops == null || nextHops.isEmpty()) {
947 continue;
Pier Ventre917127a2016-10-31 16:49:19 -0700948 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700949
950 if (foundSingleNeighbor) {
951 break;
952 }
953
954 for (DeviceId neighborId : nextHops) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800955 if (devicePortMap.get(neighborId) == null) {
956 log.warn("Neighbor {} is not in the port map yet for dev:{}",
957 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700958 return;
Jon Hallcbd1b392017-01-18 20:15:44 -0800959 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700960 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800961 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700962 return;
sangho834e4b02015-05-01 09:38:25 -0700963 }
964
Saurav Das8a0732e2015-11-20 15:27:53 -0800965 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800966 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800967 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800968 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700969 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800970 return;
971 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700972 // For each port to the neighbor, we create a new treatment
Pier Ventre917127a2016-10-31 16:49:19 -0700973 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Dasa4020382018-02-14 14:14:54 -0800974 // In this case we need a SIMPLE nextObj. We randomly pick a port
975 if (simple) {
Pier Ventre917127a2016-10-31 16:49:19 -0700976 int size = devicePortMap.get(neighborId).size();
977 int index = RandomUtils.nextInt(0, size);
978 neighborPorts = Collections.singleton(
Saurav Das7bcbe702017-06-13 15:35:54 -0700979 Iterables.get(devicePortMap.get(neighborId),
980 index));
981 foundSingleNeighbor = true;
Pier Ventre917127a2016-10-31 16:49:19 -0700982 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700983
Pier Ventre917127a2016-10-31 16:49:19 -0700984 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700985 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
986 .builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700987 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
988 int edgeLabel = ds.getEdgeLabel(dst);
989 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -0800990 if (simple) {
991 // swap label case
992 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
993 } else {
994 // ecmp with label push case
995 tBuilder.pushMpls().copyTtlOut()
996 .setMpls(MplsLabel.mplsLabel(edgeLabel));
997 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700998 }
Charles Chan69b7ae62018-08-12 15:19:01 -0700999
1000 // Set VLAN ID for PW transport. Otherwise pop vlan
Charles Chane0260e12018-08-22 17:56:47 -07001001 if (!popVlanInHashGroup(ds)) {
Saurav Das09c2c4d2018-08-13 15:34:26 -07001002 tBuilder.setVlanId(srManager.getPwTransportVlan());
Charles Chan69b7ae62018-08-12 15:19:01 -07001003 } else {
1004 tBuilder.popVlan();
Andreas Pantelopoulos192284c2018-05-01 14:56:05 -07001005 }
Charles Chan69b7ae62018-08-12 15:19:01 -07001006
Saurav Das8a0732e2015-11-20 15:27:53 -08001007 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001008 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das7bcbe702017-06-13 15:35:54 -07001009 treatmentAdded = true;
1010 //update store
1011 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
1012 if (existingNeighbors == null) {
1013 existingNeighbors = new HashSet<>();
1014 }
1015 existingNeighbors.add(neighborId);
1016 dstNextHops.put(dst, existingNeighbors);
1017 log.debug("creating treatment for port/label {}/{} in next:{}",
1018 sp, edgeLabel, nextId);
1019 }
1020
1021 if (foundSingleNeighbor) {
1022 break;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001023 }
1024 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001025 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001026
1027 if (!treatmentAdded) {
1028 log.warn("Could not createGroup from DestinationSet {} without any"
1029 + "next hops {}", ds, neighbors);
1030 return;
1031 }
1032 ObjectiveContext context = new DefaultObjectiveContext(
1033 (objective) ->
1034 log.debug("createGroupsFromDestinationSet installed "
1035 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001036 (objective, error) -> {
1037 log.warn("createGroupsFromDestinationSet failed to install NextObj {} on {}: {}",
1038 nextId, deviceId, error);
1039 srManager.invalidateNextObj(objective.id());
1040 });
Saurav Das7bcbe702017-06-13 15:35:54 -07001041 NextObjective nextObj = nextObjBuilder.add(context);
1042 log.debug(".. createGroupsFromDestinationSet: Submitted "
1043 + "next objective {} in device {}", nextId, deviceId);
1044 flowObjectiveService.next(deviceId, nextObj);
1045 //update store
1046 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1047 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001048 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001049
Saurav Das4ce45962015-11-24 23:21:05 -08001050 /**
Saurav Das1a129a02016-11-18 15:21:57 -08001051 * Creates broadcast groups for all ports in the same subnet for
1052 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -08001053 */
Charles Chan59cc16d2017-02-02 16:20:42 -08001054 public void createGroupsFromVlanConfig() {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001055 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001056 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -08001057 });
Saurav Das1a129a02016-11-18 15:21:57 -08001058 }
Charles Chan9f676b62015-10-29 14:58:10 -07001059
Saurav Das1a129a02016-11-18 15:21:57 -08001060 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001061 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -08001062 *
Charles Chan59cc16d2017-02-02 16:20:42 -08001063 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -08001064 * @param ports list of ports in the subnet
1065 */
Charles Chan7ffd81f2017-02-08 15:52:08 -08001066 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001067 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -07001068
Charles Chan59cc16d2017-02-02 16:20:42 -08001069 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -08001070 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -08001071 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001072 return;
1073 }
Charles Chan188ebf52015-12-23 00:15:11 -08001074
Saurav Das1a129a02016-11-18 15:21:57 -08001075 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -08001076 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -07001077
Saurav Das1a129a02016-11-18 15:21:57 -08001078 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -07001079
Saurav Das1a129a02016-11-18 15:21:57 -08001080 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1081 .builder().withId(nextId)
1082 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1083 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -07001084
Saurav Das1a129a02016-11-18 15:21:57 -08001085 ports.forEach(port -> {
1086 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -08001087 if (toPopVlan(port, vlanId)) {
1088 tBuilder.popVlan();
1089 }
Saurav Das1a129a02016-11-18 15:21:57 -08001090 tBuilder.setOutput(port);
1091 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -07001092 });
Saurav Das1a129a02016-11-18 15:21:57 -08001093
Saurav Das961beb22017-03-29 19:09:17 -07001094 ObjectiveContext context = new DefaultObjectiveContext(
1095 (objective) ->
1096 log.debug("createBroadcastGroupFromVlan installed "
1097 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001098 (objective, error) -> {
1099 log.warn("createBroadcastGroupFromVlan failed to install NextObj {} on {}: {}",
1100 nextId, deviceId, error);
1101 srManager.invalidateNextObj(objective.id());
1102 });
Saurav Das961beb22017-03-29 19:09:17 -07001103 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das1a129a02016-11-18 15:21:57 -08001104 flowObjectiveService.next(deviceId, nextObj);
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001105 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1106 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001107
Charles Chan59cc16d2017-02-02 16:20:42 -08001108 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001109 }
1110
Charles Chane849c192016-01-11 18:28:54 -08001111 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001112 * Removes a single broadcast group from a given vlan id.
1113 * The group should be empty.
1114 * @param deviceId device Id to remove the group
1115 * @param portNum port number related to the group
1116 * @param vlanId vlan id of the broadcast group to remove
1117 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1118 */
1119 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1120 VlanId vlanId, boolean popVlan) {
1121 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1122
1123 if (!vlanNextObjStore.containsKey(key)) {
1124 log.debug("Broadcast group for device {} and subnet {} does not exist",
1125 deviceId, vlanId);
1126 return;
1127 }
1128
1129 TrafficSelector metadata =
1130 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1131
1132 int nextId = vlanNextObjStore.get(key);
1133
1134 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1135 .builder().withId(nextId)
1136 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1137 .withMeta(metadata);
1138
1139 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1140 if (popVlan) {
1141 tBuilder.popVlan();
1142 }
1143 tBuilder.setOutput(portNum);
1144 nextObjBuilder.addTreatment(tBuilder.build());
1145
1146 ObjectiveContext context = new DefaultObjectiveContext(
1147 (objective) ->
1148 log.debug("removeBroadcastGroupFromVlan removed "
1149 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001150 (objective, error) -> {
1151 log.warn("removeBroadcastGroupFromVlan failed to remove NextObj {} on {}: {}",
1152 nextId, deviceId, error);
1153 srManager.invalidateNextObj(objective.id());
1154 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001155 NextObjective nextObj = nextObjBuilder.remove(context);
1156 flowObjectiveService.next(deviceId, nextObj);
1157 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1158 nextId, deviceId);
1159
1160 vlanNextObjStore.remove(key, nextId);
1161 }
1162
1163 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001164 * Determine if we should pop given vlan before sending packets to the given port.
1165 *
1166 * @param portNumber port number
1167 * @param vlanId vlan id
1168 * @return true if the vlan id is not contained in any vlanTagged config
1169 */
1170 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001171 return srManager.interfaceService
1172 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan7ffd81f2017-02-08 15:52:08 -08001173 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1174 }
1175
1176 /**
Saurav Das4ce45962015-11-24 23:21:05 -08001177 * Create simple next objective for a single port. The treatments can include
1178 * all outgoing actions that need to happen on the packet.
1179 *
1180 * @param portNum the outgoing port on the device
1181 * @param treatment the actions to apply on the packets (should include outport)
1182 * @param meta optional data to pass to the driver
1183 */
1184 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1185 TrafficSelector meta) {
1186 int nextId = flowObjectiveService.allocateNextId();
1187 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das76ae6812017-03-15 15:15:14 -07001188 deviceId, portNum, treatment, meta);
Saurav Das4ce45962015-11-24 23:21:05 -08001189
1190 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1191 .builder().withId(nextId)
1192 .withType(NextObjective.Type.SIMPLE)
1193 .addTreatment(treatment)
1194 .fromApp(appId)
1195 .withMeta(meta);
1196
Saurav Das961beb22017-03-29 19:09:17 -07001197 ObjectiveContext context = new DefaultObjectiveContext(
1198 (objective) ->
1199 log.debug("createGroupFromPort installed "
1200 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001201 (objective, error) -> {
1202 log.warn("createGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1203 srManager.invalidateNextObj(objective.id());
1204 });
Saurav Das961beb22017-03-29 19:09:17 -07001205 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das4ce45962015-11-24 23:21:05 -08001206 flowObjectiveService.next(deviceId, nextObj);
1207 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1208 + "for port {}", nextId, deviceId, portNum);
1209
1210 portNextObjStore.put(key, nextId);
1211 }
1212
sangho1e575652015-05-14 00:39:53 -07001213 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001214 * Removes simple next objective for a single port.
1215 *
1216 * @param deviceId device id that has the port to deal with
1217 * @param portNum the outgoing port on the device
1218 * @param vlanId vlan id associated with the port
1219 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1220 */
1221 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1222 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1223 mbuilder.matchVlanId(vlanId);
1224
1225 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1226 tbuilder.immediate().setOutput(portNum);
1227 if (popVlan) {
1228 tbuilder.immediate().popVlan();
1229 }
1230
1231 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1232 tbuilder.build(), mbuilder.build(), false);
1233
1234 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1235 deviceId, portNum, tbuilder.build(), mbuilder.build());
1236 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1237 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1238 .builder().withId(portNextObjId)
1239 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1240 ObjectiveContext context = new DefaultObjectiveContext(
1241 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1242 portNextObjId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001243 (objective, error) -> {
1244 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1245 portNextObjId, deviceId, error);
1246 srManager.invalidateNextObj(objective.id());
1247 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001248 NextObjective nextObjective = nextObjBuilder.remove(context);
1249 log.info("**removePortNextObjective: Submitted "
1250 + "next objective {} in device {}",
1251 portNextObjId, deviceId);
1252 flowObjectiveService.next(deviceId, nextObjective);
1253
1254 portNextObjStore.remove(key);
1255 }
1256 }
1257 /**
sangho1e575652015-05-14 00:39:53 -07001258 * Removes groups for the next objective ID given.
1259 *
1260 * @param objectiveId next objective ID to remove
1261 * @return true if succeeds, false otherwise
1262 */
1263 public boolean removeGroup(int objectiveId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001264 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1265 dsNextObjStore.entrySet()) {
1266 if (e.getValue().nextId() != objectiveId) {
1267 continue;
1268 }
Pier Luigi63edd932018-01-14 21:56:11 +01001269 // Right now it is just used in TunnelHandler
1270 // remember in future that PW transit groups could
1271 // be Indirect groups
sangho1e575652015-05-14 00:39:53 -07001272 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1273 .builder().withId(objectiveId)
1274 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -07001275 ObjectiveContext context = new DefaultObjectiveContext(
1276 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1277 objectiveId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001278 (objective, error) -> {
1279 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", objectiveId, deviceId, error);
1280 srManager.invalidateNextObj(objective.id());
1281 });
Charles Chan216e3c82016-04-23 14:48:16 -07001282 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -08001283 log.info("**removeGroup: Submited "
1284 + "next objective {} in device {}",
1285 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -07001286 flowObjectiveService.next(deviceId, nextObjective);
1287
Saurav Das7bcbe702017-06-13 15:35:54 -07001288 dsNextObjStore.remove(e.getKey());
sangho0b2b6d12015-05-20 22:16:38 -07001289 return true;
sangho1e575652015-05-14 00:39:53 -07001290 }
1291
1292 return false;
1293 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001294 /**
1295 * Remove simple next objective for a single port. The treatments can include
1296 * all outgoing actions that need to happen on the packet.
1297 *
1298 * @param portNum the outgoing port on the device
1299 * @param treatment the actions applied on the packets (should include outport)
1300 * @param meta optional data to pass to the driver
1301 */
1302 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1303 TrafficSelector meta) {
1304 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1305 deviceId, portNum, treatment, meta);
1306 Integer nextId = portNextObjStore.get(key);
1307
1308 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1309 .builder().withId(nextId)
1310 .withType(NextObjective.Type.SIMPLE)
1311 .addTreatment(treatment)
1312 .fromApp(appId)
1313 .withMeta(meta);
1314
1315 ObjectiveContext context = new DefaultObjectiveContext(
1316 (objective) ->
1317 log.info("removeGroupFromPort installed "
1318 + "NextObj {} on {}", nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001319 (objective, error) -> {
1320 log.warn("removeGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1321 srManager.invalidateNextObj(objective.id());
1322 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001323 );
1324 NextObjective nextObj = nextObjBuilder.remove(context);
1325 flowObjectiveService.next(deviceId, nextObj);
1326 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1327 + "for port {}", nextId, deviceId, portNum);
1328
1329 portNextObjStore.remove(key);
1330 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001331
Charles Chane849c192016-01-11 18:28:54 -08001332 /**
1333 * Removes all groups from all next objective stores.
1334 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001335 /*public void removeAllGroups() {
1336 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das423fe2b2015-12-04 10:52:59 -08001337 nsNextObjStore.entrySet()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001338 removeGroup(entry.getValue().nextId());
Saurav Das423fe2b2015-12-04 10:52:59 -08001339 }
1340 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1341 portNextObjStore.entrySet()) {
1342 removeGroup(entry.getValue());
1343 }
Charles Chan59cc16d2017-02-02 16:20:42 -08001344 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1345 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001346 removeGroup(entry.getValue());
1347 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001348 }*/ //XXX revisit
1349
Saurav Dasceccf242017-08-03 18:30:35 -07001350 /**
1351 * Triggers a one time bucket verification operation on all hash groups
1352 * on this device.
1353 */
1354 public void triggerBucketCorrector() {
1355 BucketCorrector bc = new BucketCorrector();
1356 bc.run();
1357 }
1358
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001359 /**
1360 * Modifies L2IG bucket when the interface configuration is updated, especially
1361 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1362 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1363 * TrafficTreatment in turn.
1364 *
1365 * @param portNumber the port on this device that needs to be updated
1366 * @param vlanId the vlan id corresponding to this port
1367 * @param pushVlan indicates if packets should be sent out untagged or not out
1368 * from the port. If true, updated TrafficTreatment involves
1369 * pop vlan tag action. If false, updated TrafficTreatment
1370 * does not involve pop vlan tag action.
1371 */
1372 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
1373 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1374 if (pushVlan) {
1375 tBuilder.popVlan();
1376 }
1377 tBuilder.setOutput(portNumber);
1378
1379 TrafficSelector metadata =
1380 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1381
1382 int nextId = getVlanNextObjectiveId(vlanId);
1383
1384 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1385 .builder().withId(nextId)
1386 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1387 .addTreatment(tBuilder.build())
1388 .withMeta(metadata);
1389
1390 ObjectiveContext context = new DefaultObjectiveContext(
1391 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1392 portNumber, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001393 (objective, error) -> {
1394 log.warn("port {} failed to updated NextObj {} on {}: {}", portNumber, nextId, deviceId, error);
1395 srManager.invalidateNextObj(objective.id());
1396 });
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001397
1398 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001399 }
1400
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001401 /**
1402 * Adds a single port to the L2FG or removes it from the L2FG.
1403 *
1404 * @param vlanId the vlan id corresponding to this port
1405 * @param portNum the port on this device to be updated
1406 * @param nextId the next objective ID for the given vlan id
1407 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1408 */
1409 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001410 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1411 if (toPopVlan(portNum, vlanId)) {
1412 tBuilder.popVlan();
1413 }
1414 tBuilder.setOutput(portNum);
1415
1416 TrafficSelector metadata =
1417 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1418
1419 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1420 .builder().withId(nextId)
1421 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1422 .addTreatment(tBuilder.build())
1423 .withMeta(metadata);
1424
1425 ObjectiveContext context = new DefaultObjectiveContext(
1426 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1427 portNum, nextId, deviceId),
Charles Chan55b806f2018-08-23 14:30:33 -07001428 (objective, error) -> {
1429 log.warn("port {} failed to removedFrom NextObj {} on {}: {}", portNum, nextId, deviceId, error);
1430 srManager.invalidateNextObj(objective.id());
1431 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001432
1433 if (install) {
1434 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1435 } else {
1436 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1437 }
1438 }
Saurav Das1547b3f2017-05-05 17:01:08 -07001439
1440 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -07001441 * Performs bucket verification operation for all hash groups in this device.
1442 * Checks RouteHandler to ensure that routing is stable before attempting
1443 * verification. Verification involves creating a nextObjective with
1444 * operation VERIFY for existing next objectives in the store, and passing
1445 * it to the driver. It is the driver that actually performs the verification
1446 * by adding or removing buckets to match the verification next objective
1447 * created here.
Saurav Das1547b3f2017-05-05 17:01:08 -07001448 */
Saurav Dasceccf242017-08-03 18:30:35 -07001449 protected final class BucketCorrector implements Runnable {
1450 Integer nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001451
Saurav Dasceccf242017-08-03 18:30:35 -07001452 BucketCorrector() {
1453 this.nextId = null;
1454 }
1455
1456 BucketCorrector(Integer nextId) {
1457 this.nextId = nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001458 }
1459
1460 @Override
1461 public void run() {
Saurav Dasceccf242017-08-03 18:30:35 -07001462 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1463 return;
Saurav Das1547b3f2017-05-05 17:01:08 -07001464 }
Saurav Dasceccf242017-08-03 18:30:35 -07001465 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1466 if (rh == null) {
1467 return;
1468 }
1469 if (!rh.isRoutingStable()) {
1470 return;
1471 }
1472 rh.acquireRoutingLock();
1473 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001474 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07001475 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1476 .stream()
1477 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +01001478 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -08001479 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1480 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1481 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1482 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1483 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasceccf242017-08-03 18:30:35 -07001484 .map(entry -> entry.getKey())
1485 .collect(Collectors.toSet());
1486 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1487 NextNeighbors next = dsNextObjStore.get(dsKey);
1488 if (next == null) {
1489 continue;
1490 }
1491 int nid = next.nextId();
1492 if (nextId != null && nextId != nid) {
1493 continue;
1494 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001495 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasceccf242017-08-03 18:30:35 -07001496 deviceId, dsKey, next);
1497 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das09c2c4d2018-08-13 15:34:26 -07001498 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasceccf242017-08-03 18:30:35 -07001499 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1500 .withId(nid)
1501 .withType(NextObjective.Type.HASHED)
1502 .withMeta(metabuilder.build())
1503 .fromApp(appId);
1504
1505 next.dstNextHops().forEach((dstDev, nextHops) -> {
1506 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1507 nextHops.forEach(neighbor -> {
1508 MacAddress neighborMac;
1509 try {
1510 neighborMac = deviceConfig.getDeviceMac(neighbor);
1511 } catch (DeviceConfigNotFoundException e) {
1512 log.warn(e.getMessage() + " Aborting neighbor"
1513 + neighbor);
1514 return;
1515 }
1516 devicePortMap.get(neighbor).forEach(port -> {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001517 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasceccf242017-08-03 18:30:35 -07001518 + " port/label {}/{} to dst {} via {}",
1519 deviceId, nid, port, edgeLabel,
1520 dstDev, neighbor);
Saurav Dasa4020382018-02-14 14:14:54 -08001521 nextObjBuilder
1522 .addTreatment(treatmentBuilder(port,
1523 neighborMac,
1524 dsKey.destinationSet().swap(),
1525 edgeLabel));
Saurav Dasceccf242017-08-03 18:30:35 -07001526 });
1527 });
1528 });
1529
1530 NextObjective nextObjective = nextObjBuilder.verify();
1531 flowObjectiveService.next(deviceId, nextObjective);
1532 }
1533 } finally {
1534 rh.releaseRoutingLock();
1535 }
1536
1537 }
1538
1539 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
Saurav Dasa4020382018-02-14 14:14:54 -08001540 boolean swap, int edgeLabel) {
Saurav Dasceccf242017-08-03 18:30:35 -07001541 TrafficTreatment.Builder tBuilder =
1542 DefaultTrafficTreatment.builder();
1543 tBuilder.setOutput(outport)
1544 .setEthDst(dstMac)
1545 .setEthSrc(nodeMacAddr);
1546 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001547 if (swap) {
1548 // swap label case
1549 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1550 } else {
1551 // ecmp with label push case
1552 tBuilder.pushMpls()
1553 .copyTtlOut()
1554 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1555 }
Saurav Dasceccf242017-08-03 18:30:35 -07001556 }
1557 return tBuilder.build();
Saurav Das1547b3f2017-05-05 17:01:08 -07001558 }
1559 }
1560
Charles Chane0260e12018-08-22 17:56:47 -07001561 /**
1562 * Determines whether the hash group bucket should include a popVlan action.
1563 * We don't popVlan for PW.
1564 *
1565 * @param ds destination set
1566 * @return true if VLAN needs to be popped
1567 */
1568 private boolean popVlanInHashGroup(DestinationSet ds) {
1569 return (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.SWAP_NOT_BOS) &&
1570 (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.POP_NOT_BOS);
1571 }
Pier Luigi63edd932018-01-14 21:56:11 +01001572}