blob: 3fe934c44e320c34715edb607365234808fa68ab [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),
Saurav Das1547b3f2017-05-05 17:01:08 -0700391 (objective, error) ->
Saurav Das9df5b7c2017-08-14 16:44:43 -0700392 log.warn("addToHash failed to add port/label {} to"
393 + " NextObj {} on {}: {}", portLabels,
Saurav Dasc88d4662017-05-15 15:34:25 -0700394 nextId, deviceId, error));
Saurav Das1547b3f2017-05-05 17:01:08 -0700395 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
396 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800397 }
398
399 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700400 * Makes a call to the FlowObjective service to remove buckets from
401 * a hash group. User must ensure that all the ports & labels are meant
402 * same neighbor (ie. dstMac).
Saurav Dasceccf242017-08-03 18:30:35 -0700403 *
Pier Luigi63edd932018-01-14 21:56:11 +0100404 * @param portLabels a collection of port & label combinations to remove
Saurav Das9df5b7c2017-08-14 16:44:43 -0700405 * from the hash group identified by the nextId
Saurav Dasceccf242017-08-03 18:30:35 -0700406 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700407 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasceccf242017-08-03 18:30:35 -0700408 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700409 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
410 MacAddress dstMac, Integer nextId) {
Saurav Dasceccf242017-08-03 18:30:35 -0700411 NextObjective.Builder nextObjBuilder = DefaultNextObjective
412 .builder()
413 .withType(NextObjective.Type.HASHED) //same as original
414 .withId(nextId)
Saurav Das9df5b7c2017-08-14 16:44:43 -0700415 .fromApp(appId);
416 // Create the buckets to be removed
417 portLabels.forEach(pl -> {
418 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
419 tBuilder.setOutput(pl.port)
420 .setEthDst(dstMac)
421 .setEthSrc(nodeMacAddr);
Charles Chane0260e12018-08-22 17:56:47 -0700422 if (pl.popVlan) {
423 tBuilder.popVlan();
424 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700425 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
426 tBuilder.pushMpls()
427 .copyTtlOut()
428 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
429 }
430 nextObjBuilder.addTreatment(tBuilder.build());
431 });
432 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
433 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700434
Saurav Das9df5b7c2017-08-14 16:44:43 -0700435 ObjectiveContext context = new DefaultObjectiveContext(
436 (objective) -> log.debug("port/label {} removedFrom NextObj"
437 + " {} on {}", portLabels, nextId, deviceId),
438 (objective, error) ->
439 log.warn("port/label {} failed to removeFrom NextObj {} on "
440 + "{}: {}", portLabels, nextId, deviceId, error));
441 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasceccf242017-08-03 18:30:35 -0700442 flowObjectiveService.next(deviceId, nextObjective);
443 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700444
445 /**
446 * Checks all the hash-groups in the target-switch meant for the destination
447 * switch, and either adds or removes buckets to make the neighbor-set
448 * match the given next-hops. Typically called by the master instance of the
449 * destination switch, which may be different from the master instance of the
450 * target switch where hash-group changes are made.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800451 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700452 * @param targetSw the switch in which the hash groups will be edited
453 * @param nextHops the current next hops for the target switch to reach
454 * the dest sw
455 * @param destSw the destination switch
456 * @param revoke true if hash groups need to remove buckets from the
457 * the groups to match the current next hops
458 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800459 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700460 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
461 DeviceId destSw, boolean revoke) {
462 // temporary storage of keys to be updated
Saurav Das7bcbe702017-06-13 15:35:54 -0700463 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Dasc88d4662017-05-15 15:34:25 -0700464 new HashMap<>();
Saurav Das9df5b7c2017-08-14 16:44:43 -0700465 boolean foundNextObjective = false, success = true;
Charles Chan0b4e6182015-11-03 10:42:14 -0800466
Saurav Das7bcbe702017-06-13 15:35:54 -0700467 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Dasc88d4662017-05-15 15:34:25 -0700468 // with different neighbors than the given next-hops
Saurav Das7bcbe702017-06-13 15:35:54 -0700469 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
470 if (!dskey.deviceId().equals(targetSw) ||
471 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700472 continue;
473 }
474 foundNextObjective = true;
Saurav Das7bcbe702017-06-13 15:35:54 -0700475 NextNeighbors nhops = dsNextObjStore.get(dskey);
476 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
477 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
478 Integer nextId = nhops.nextId();
Saurav Dase0d4c872018-03-05 14:37:16 -0800479 if (currNeighbors == null || nextHops == null) {
480 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
481 + " in targetSw:{} for dstSw:{}", currNeighbors,
482 nextHops, targetSw, destSw);
483 success &= false;
484 continue;
485 }
Charles Chan0b4e6182015-11-03 10:42:14 -0800486
Saurav Dasa4020382018-02-14 14:14:54 -0800487 // some store elements may not be hashed next-objectives - ignore them
488 if (isSimpleNextObjective(dskey)) {
489 log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}"
490 + " -> dstSw:{} with current nextHops:{} to new"
491 + " nextHops: {} in nextId:{}",
492 (revoke) ? "removal" : "addition", targetSw, destSw,
493 currNeighbors, nextHops, nextId);
Saurav Dase0d4c872018-03-05 14:37:16 -0800494 if ((revoke && !nextHops.isEmpty())
495 || (!revoke && !nextHops.equals(currNeighbors))) {
Saurav Das137f27f2018-06-11 17:02:31 -0700496 log.debug("Simple next objective cannot be edited to "
Saurav Dasa4020382018-02-14 14:14:54 -0800497 + "move from {} to {}", currNeighbors, nextHops);
498 }
499 continue;
500 }
501
Saurav Dasc88d4662017-05-15 15:34:25 -0700502 Set<DeviceId> diff;
503 if (revoke) {
504 diff = Sets.difference(currNeighbors, nextHops);
505 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
506 + "hops:{} ..removing {}", targetSw, destSw, nextId,
507 currNeighbors, diff);
508 } else {
509 diff = Sets.difference(nextHops, currNeighbors);
510 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
511 + "hops:{} ..adding {}", targetSw, destSw, nextId,
512 currNeighbors, diff);
513 }
Charles Chane0260e12018-08-22 17:56:47 -0700514 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, popVlanInHashGroup(dskey.destinationSet()),
Saurav Das9df5b7c2017-08-14 16:44:43 -0700515 revoke);
516 if (suc) {
517 // to update neighbor set with changes made
Saurav Dasc88d4662017-05-15 15:34:25 -0700518 if (revoke) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700519 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700520 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700521 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700522 }
sangho834e4b02015-05-01 09:38:25 -0700523 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700524 success &= suc;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800525 }
526
Saurav Dasc88d4662017-05-15 15:34:25 -0700527 if (!foundNextObjective) {
528 log.debug("Cannot find any nextObjectives for route targetSw:{} "
529 + "-> dstSw:{}", targetSw, destSw);
530 return true; // nothing to do, return true so ECMPspg is updated
531 }
532
Saurav Das7bcbe702017-06-13 15:35:54 -0700533 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
534 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700535 NextNeighbors currentNextHops = dsNextObjStore.get(key);
536 if (currentNextHops == null) {
537 log.warn("fixHashGroups could not update global store in "
538 + "device {} .. missing nextNeighbors for key {}",
539 deviceId, key);
Saurav Dasc88d4662017-05-15 15:34:25 -0700540 continue;
541 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700542 Set<DeviceId> newNeighbors = new HashSet<>();
543 newNeighbors.addAll(tempStore.get(key));
544 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
545 ImmutableMap.copyOf(currentNextHops.dstNextHops());
546 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
547 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
548 targetSw, destSw, key, currentNextHops.nextId());
549 log.debug("Old dstNextHops: {}", oldDstNextHops);
550 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
551 // update global store
552 dsNextObjStore.put(key,
553 new NextNeighbors(currentNextHops.dstNextHops(),
554 currentNextHops.nextId()));
Saurav Dasc88d4662017-05-15 15:34:25 -0700555 }
Saurav Dasa4020382018-02-14 14:14:54 -0800556
Saurav Das9df5b7c2017-08-14 16:44:43 -0700557 // even if one fails and others succeed, return false so ECMPspg not updated
558 return success;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800559 }
560
Saurav Dasceccf242017-08-03 18:30:35 -0700561 /**
562 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
563 * that are not already in the store for the given DestinationSet. Note that
564 * this method does not remove existing next hops for the destinations in the
565 * DestinationSet.
566 *
567 * @param ds the DestinationSet for which the next hops need to be updated
568 * @param newDstNextHops a map of per-destination next hops to update the
569 * destinationSet with
570 * @return true if successful in updating all next hops
571 */
572 private boolean updateNextHops(DestinationSet ds,
Saurav Das7bcbe702017-06-13 15:35:54 -0700573 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
574 DestinationSetNextObjectiveStoreKey key =
575 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
576 NextNeighbors currNext = dsNextObjStore.get(key);
577 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
578
579 // add newDstNextHops to currDstNextHops for each dst
580 boolean success = true;
581 for (DeviceId dstSw : ds.getDestinationSwitches()) {
582 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
583 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
584 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
585 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
586 int edgeLabel = ds.getEdgeLabel(dstSw);
587 int nextId = currNext.nextId();
588
589 // new next hops should be added
590 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
Charles Chane0260e12018-08-22 17:56:47 -0700591 edgeLabel, nextId, popVlanInHashGroup(key.destinationSet()), false);
Saurav Das7bcbe702017-06-13 15:35:54 -0700592 if (suc) {
593 currNhops.addAll(newNhops);
594 currDstNextHops.put(dstSw, currNhops); // this is only a local change
595 }
596 success &= suc;
597 }
598
599 if (success) {
600 // update global store
601 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
602 currNext.nextId()));
603 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
604 dsNextObjStore.get(key));
605 }
606 return success;
607 }
608
Saurav Dasceccf242017-08-03 18:30:35 -0700609 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700610 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
611 * needs to ensure that the given neighbors are all next hops towards the
612 * same destination (represented by the given edgeLabel).
Saurav Dasceccf242017-08-03 18:30:35 -0700613 *
614 * @param neighbors set of neighbor device ids
615 * @param edgeLabel MPLS label to use in buckets
616 * @param nextId the nextObjective to change
Charles Chane0260e12018-08-22 17:56:47 -0700617 * @param popVlan this hash group bucket shuold includes a popVlan action
Saurav Dasceccf242017-08-03 18:30:35 -0700618 * @param revoke true if buckets need to be removed, false if they need to
619 * be added
620 * @return true if successful in adding or removing buckets for all ports
621 * to the neighbors
622 */
623 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Charles Chane0260e12018-08-22 17:56:47 -0700624 int nextId, boolean popVlan, boolean revoke) {
Saurav Dasceccf242017-08-03 18:30:35 -0700625 for (DeviceId neighbor : neighbors) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700626 MacAddress neighborMac;
Saurav Das7bcbe702017-06-13 15:35:54 -0700627 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700628 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das7bcbe702017-06-13 15:35:54 -0700629 } catch (DeviceConfigNotFoundException e) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700630 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
631 + " for nextId:" + nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700632 return false;
633 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700634 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
635 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700636 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Das9df5b7c2017-08-14 16:44:43 -0700637 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700638 deviceId, neighbor, nextId);
639 return false;
640 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700641 List<PortLabel> pl = Lists.newArrayList();
Charles Chane0260e12018-08-22 17:56:47 -0700642 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel, popVlan)));
Saurav Das7bcbe702017-06-13 15:35:54 -0700643 if (revoke) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700644 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
645 + "with Port/Label:{} to next object id {}",
646 deviceId, pl, nextId);
647 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700648 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700649 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
650 + "with Port/Label: {} to next object id {}",
651 deviceId, pl, nextId);
652 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700653 }
654 }
655 return true;
656 }
657
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800658 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800659 * Returns true if the destination set is meant for swap or multi-labeled
660 * packet transport, and MPLS ECMP is not supported.
661 *
662 * @param dskey the key representing the destination set
663 * @return true if destination set is meant for simple next objectives
664 */
665 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
666 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
667 && !srManager.getMplsEcmp();
668 }
669
670 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800671 * Adds or removes a port that has been configured with a vlan to a broadcast group
672 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800673 *
674 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800675 * @param vlanId the vlan id corresponding to the broadcast domain/group
676 * @param popVlan indicates if packets should be sent out untagged or not out
677 * of the port. If true, indicates an access (untagged) or native vlan
678 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800679 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800680 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800681 public void processEdgePort(PortNumber port, VlanId vlanId,
682 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800683 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800684 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800685 if (nextId == -1) {
686 if (portUp) {
687 log.debug("**Creating flooding group for first port enabled in"
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800688 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan59cc16d2017-02-02 16:20:42 -0800689 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800690 } else {
691 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800692 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800693 }
694 return;
695 }
696
697 log.info("**port{} in device {}: {} Bucket with Port {} to"
698 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
699 (portUp) ? "Adding" : "Removing",
700 port, nextId);
701 // Create the bucket to be added or removed
702 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800703 if (popVlan) {
704 tBuilder.popVlan();
705 }
Saurav Das1a129a02016-11-18 15:21:57 -0800706 tBuilder.setOutput(port);
707
Saurav Das1a129a02016-11-18 15:21:57 -0800708 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800709 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800710
711 NextObjective.Builder nextObjBuilder = DefaultNextObjective
712 .builder().withId(nextId)
713 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
714 .addTreatment(tBuilder.build())
715 .withMeta(metadata);
716
717 ObjectiveContext context = new DefaultObjectiveContext(
718 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
719 port, (portUp) ? "addedTo" : "removedFrom",
720 nextId, deviceId),
721 (objective, error) ->
722 log.warn("port {} failed to {} NextObj {} on {}: {}",
723 port, (portUp) ? "addTo" : "removeFrom",
724 nextId, deviceId, error));
725
726 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
727 : nextObjBuilder.removeFromExisting(context);
728 log.debug("edgePort processed: Submited next objective {} in device {}",
729 nextId, deviceId);
730 flowObjectiveService.next(deviceId, nextObj);
731 }
732
733 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800734 * Returns the next objective of type hashed (or simple) associated with the
735 * destination set. In addition, updates the existing next-objective if new
736 * route-paths found have resulted in the addition of new next-hops to a
737 * particular destination. If there is no existing next objective for this
738 * destination set, this method would create a next objective and return the
739 * nextId. Optionally metadata can be passed in for the creation of the next
740 * objective. If the parameter simple is true then a simple next objective
741 * is created instead of a hashed one.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800742 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700743 * @param ds destination set
744 * @param nextHops a map of per destination next hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800745 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800746 * @param simple if true, a simple next objective will be created instead of
747 * a hashed next objective
Saurav Das8a0732e2015-11-20 15:27:53 -0800748 * @return int if found or -1 if there are errors in the creation of the
Saurav Dasa4020382018-02-14 14:14:54 -0800749 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800750 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700751 public int getNextObjectiveId(DestinationSet ds,
752 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Dasa4020382018-02-14 14:14:54 -0800753 TrafficSelector meta, boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700754 NextNeighbors next = dsNextObjStore.
755 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
756 if (next == null) {
757 log.debug("getNextObjectiveId in device{}: Next objective id "
758 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700759 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
760 deviceId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700761 dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700762 .stream()
763 .filter((nsStoreEntry) ->
764 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
765 .collect(Collectors.toList()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700766
Saurav Dasa4020382018-02-14 14:14:54 -0800767 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das7bcbe702017-06-13 15:35:54 -0700768 next = dsNextObjStore.
769 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
770 if (next == null) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700771 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das7bcbe702017-06-13 15:35:54 -0700772 // failure in creating group
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700773 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700774 } else {
775 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700776 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700777 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700778 } else {
779 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700780 + "found for {}", deviceId, next.nextId(), ds);
781 // should fix hash groups too if next-hops have changed
782 if (!next.dstNextHops().equals(nextHops)) {
783 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
784 deviceId, next.nextId());
785 if (!updateNextHops(ds, nextHops)) {
786 // failure in updating group
787 return -1;
788 }
789 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700790 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700791 return next.nextId();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800792 }
793
sangho0b2b6d12015-05-20 22:16:38 -0700794 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800795 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800796 * or -1 if no such objective exists. Note that this method does NOT create
797 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800798 * created at startup from network configuration. Typically this is used
799 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700800 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800801 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700802 * @return int if found or -1
803 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800804 public int getVlanNextObjectiveId(VlanId vlanId) {
805 Integer nextId = vlanNextObjStore.
806 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700807
808 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700809 }
810
811 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800812 * Returns the next objective of type simple associated with the port on the
813 * device, given the treatment. Different treatments to the same port result
814 * in different next objectives. If no such objective exists, this method
Saurav Das961beb22017-03-29 19:09:17 -0700815 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800816 * the creation of the objective. Typically this is used for L2 and L3 forwarding
817 * to compute nodes and containers/VMs on the compute nodes directly attached
818 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800819 *
820 * @param portNum the port number for the simple next objective
821 * @param treatment the actions to apply on the packets (should include outport)
822 * @param meta optional metadata passed into the creation of the next objective
Saurav Das961beb22017-03-29 19:09:17 -0700823 * @param createIfMissing true if a next object should be created if not found
Saurav Das4ce45962015-11-24 23:21:05 -0800824 * @return int if found or created, -1 if there are errors during the
825 * creation of the next objective.
826 */
827 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700828 TrafficSelector meta, boolean createIfMissing) {
Charles Chane849c192016-01-11 18:28:54 -0800829 Integer nextId = portNextObjStore
Saurav Das76ae6812017-03-15 15:15:14 -0700830 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das961beb22017-03-29 19:09:17 -0700831 if (nextId != null) {
832 return nextId;
833 }
834 log.debug("getPortNextObjectiveId in device {}: Next objective id "
835 + "not found for port: {} .. {}", deviceId, portNum,
836 (createIfMissing) ? "creating" : "aborting");
837 if (!createIfMissing) {
838 return -1;
839 }
840 // create missing next objective
841 createGroupFromPort(portNum, treatment, meta);
842 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
843 treatment, meta));
Saurav Das4ce45962015-11-24 23:21:05 -0800844 if (nextId == null) {
Saurav Das961beb22017-03-29 19:09:17 -0700845 log.warn("getPortNextObjectiveId: unable to create next obj"
846 + "for dev:{} port:{}", deviceId, portNum);
847 return -1;
Charles Chane849c192016-01-11 18:28:54 -0800848 }
849 return nextId;
850 }
851
852 /**
sangho0b2b6d12015-05-20 22:16:38 -0700853 * Checks if the next objective ID (group) for the neighbor set exists or not.
854 *
855 * @param ns neighbor set to check
856 * @return true if it exists, false otherwise
857 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700858 public boolean hasNextObjectiveId(DestinationSet ns) {
859 NextNeighbors nextHops = dsNextObjStore.
860 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
861 if (nextHops == null) {
sangho0b2b6d12015-05-20 22:16:38 -0700862 return false;
863 }
864
865 return true;
866 }
867
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800868 private void populateNeighborMaps() {
869 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700870 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800871 if (link.type() != Link.Type.DIRECT) {
872 continue;
873 }
874 addNeighborAtPort(link.dst().deviceId(), link.src().port());
875 }
876 }
877
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700878 protected void addNeighborAtPort(DeviceId neighborId,
879 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800880 // Update DeviceToPort database
881 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
882 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800883 Set<PortNumber> ports = Collections
884 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
885 ports.add(portToNeighbor);
886 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
887 if (portnums != null) {
888 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800889 }
890
891 // Update portToDevice database
Saurav Dasa4020382018-02-14 14:14:54 -0800892 // should always update as neighbor could have changed on this port
893 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800894 if (prev != null) {
Saurav Das5a356042018-04-06 20:16:01 -0700895 log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -0700896 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800897 }
898 }
899
sangho1e575652015-05-14 00:39:53 -0700900 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700901 * Creates a NextObjective for a hash group in this device from a given
Saurav Dasa4020382018-02-14 14:14:54 -0800902 * DestinationSet. If the parameter simple is true, a simple next objective
903 * is created instead.
sangho1e575652015-05-14 00:39:53 -0700904 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700905 * @param ds the DestinationSet
906 * @param neighbors a map for each destination and its next-hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800907 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800908 * @param simple if true, a simple next objective will be created instead of
909 * a hashed next objective
sangho1e575652015-05-14 00:39:53 -0700910 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700911 public void createGroupFromDestinationSet(DestinationSet ds,
912 Map<DeviceId, Set<DeviceId>> neighbors,
913 TrafficSelector meta,
Saurav Dasa4020382018-02-14 14:14:54 -0800914 boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700915 int nextId = flowObjectiveService.allocateNextId();
Saurav Dasa4020382018-02-14 14:14:54 -0800916 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
917 : NextObjective.Type.HASHED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700918 if (neighbors == null || neighbors.isEmpty()) {
919 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
920 + "to create group in dev:{} for ds: {} with next-hops {}",
921 deviceId, ds, neighbors);
922 return;
923 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700924
925 NextObjective.Builder nextObjBuilder = DefaultNextObjective
926 .builder()
927 .withId(nextId)
928 .withType(type)
929 .fromApp(appId);
930 if (meta != null) {
931 nextObjBuilder.withMeta(meta);
932 }
933
934 // create treatment buckets for each neighbor for each dst Device
935 // except in the special case where we only want to pick a single
Saurav Dasa4020382018-02-14 14:14:54 -0800936 // neighbor/port for a simple nextObj
Saurav Das7bcbe702017-06-13 15:35:54 -0700937 boolean foundSingleNeighbor = false;
938 boolean treatmentAdded = false;
939 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
940 for (DeviceId dst : ds.getDestinationSwitches()) {
941 Set<DeviceId> nextHops = neighbors.get(dst);
942 if (nextHops == null || nextHops.isEmpty()) {
943 continue;
Pier Ventre917127a2016-10-31 16:49:19 -0700944 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700945
946 if (foundSingleNeighbor) {
947 break;
948 }
949
950 for (DeviceId neighborId : nextHops) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800951 if (devicePortMap.get(neighborId) == null) {
952 log.warn("Neighbor {} is not in the port map yet for dev:{}",
953 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700954 return;
Jon Hallcbd1b392017-01-18 20:15:44 -0800955 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700956 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800957 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700958 return;
sangho834e4b02015-05-01 09:38:25 -0700959 }
960
Saurav Das8a0732e2015-11-20 15:27:53 -0800961 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800962 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800963 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800964 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700965 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800966 return;
967 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700968 // For each port to the neighbor, we create a new treatment
Pier Ventre917127a2016-10-31 16:49:19 -0700969 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Dasa4020382018-02-14 14:14:54 -0800970 // In this case we need a SIMPLE nextObj. We randomly pick a port
971 if (simple) {
Pier Ventre917127a2016-10-31 16:49:19 -0700972 int size = devicePortMap.get(neighborId).size();
973 int index = RandomUtils.nextInt(0, size);
974 neighborPorts = Collections.singleton(
Saurav Das7bcbe702017-06-13 15:35:54 -0700975 Iterables.get(devicePortMap.get(neighborId),
976 index));
977 foundSingleNeighbor = true;
Pier Ventre917127a2016-10-31 16:49:19 -0700978 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700979
Pier Ventre917127a2016-10-31 16:49:19 -0700980 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700981 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
982 .builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700983 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
984 int edgeLabel = ds.getEdgeLabel(dst);
985 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -0800986 if (simple) {
987 // swap label case
988 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
989 } else {
990 // ecmp with label push case
991 tBuilder.pushMpls().copyTtlOut()
992 .setMpls(MplsLabel.mplsLabel(edgeLabel));
993 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700994 }
Charles Chan69b7ae62018-08-12 15:19:01 -0700995
996 // Set VLAN ID for PW transport. Otherwise pop vlan
Charles Chane0260e12018-08-22 17:56:47 -0700997 if (!popVlanInHashGroup(ds)) {
Saurav Das09c2c4d2018-08-13 15:34:26 -0700998 tBuilder.setVlanId(srManager.getPwTransportVlan());
Charles Chan69b7ae62018-08-12 15:19:01 -0700999 } else {
1000 tBuilder.popVlan();
Andreas Pantelopoulos192284c2018-05-01 14:56:05 -07001001 }
Charles Chan69b7ae62018-08-12 15:19:01 -07001002
Saurav Das8a0732e2015-11-20 15:27:53 -08001003 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001004 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das7bcbe702017-06-13 15:35:54 -07001005 treatmentAdded = true;
1006 //update store
1007 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
1008 if (existingNeighbors == null) {
1009 existingNeighbors = new HashSet<>();
1010 }
1011 existingNeighbors.add(neighborId);
1012 dstNextHops.put(dst, existingNeighbors);
1013 log.debug("creating treatment for port/label {}/{} in next:{}",
1014 sp, edgeLabel, nextId);
1015 }
1016
1017 if (foundSingleNeighbor) {
1018 break;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001019 }
1020 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001021 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001022
1023 if (!treatmentAdded) {
1024 log.warn("Could not createGroup from DestinationSet {} without any"
1025 + "next hops {}", ds, neighbors);
1026 return;
1027 }
1028 ObjectiveContext context = new DefaultObjectiveContext(
1029 (objective) ->
1030 log.debug("createGroupsFromDestinationSet installed "
1031 + "NextObj {} on {}", nextId, deviceId),
1032 (objective, error) ->
1033 log.warn("createGroupsFromDestinationSet failed to install"
1034 + " NextObj {} on {}: {}", nextId, deviceId, error)
1035 );
1036 NextObjective nextObj = nextObjBuilder.add(context);
1037 log.debug(".. createGroupsFromDestinationSet: Submitted "
1038 + "next objective {} in device {}", nextId, deviceId);
1039 flowObjectiveService.next(deviceId, nextObj);
1040 //update store
1041 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1042 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001043 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001044
Saurav Das4ce45962015-11-24 23:21:05 -08001045 /**
Saurav Das1a129a02016-11-18 15:21:57 -08001046 * Creates broadcast groups for all ports in the same subnet for
1047 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -08001048 */
Charles Chan59cc16d2017-02-02 16:20:42 -08001049 public void createGroupsFromVlanConfig() {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001050 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001051 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -08001052 });
Saurav Das1a129a02016-11-18 15:21:57 -08001053 }
Charles Chan9f676b62015-10-29 14:58:10 -07001054
Saurav Das1a129a02016-11-18 15:21:57 -08001055 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001056 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -08001057 *
Charles Chan59cc16d2017-02-02 16:20:42 -08001058 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -08001059 * @param ports list of ports in the subnet
1060 */
Charles Chan7ffd81f2017-02-08 15:52:08 -08001061 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001062 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -07001063
Charles Chan59cc16d2017-02-02 16:20:42 -08001064 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -08001065 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -08001066 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001067 return;
1068 }
Charles Chan188ebf52015-12-23 00:15:11 -08001069
Saurav Das1a129a02016-11-18 15:21:57 -08001070 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -08001071 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -07001072
Saurav Das1a129a02016-11-18 15:21:57 -08001073 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -07001074
Saurav Das1a129a02016-11-18 15:21:57 -08001075 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1076 .builder().withId(nextId)
1077 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1078 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -07001079
Saurav Das1a129a02016-11-18 15:21:57 -08001080 ports.forEach(port -> {
1081 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -08001082 if (toPopVlan(port, vlanId)) {
1083 tBuilder.popVlan();
1084 }
Saurav Das1a129a02016-11-18 15:21:57 -08001085 tBuilder.setOutput(port);
1086 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -07001087 });
Saurav Das1a129a02016-11-18 15:21:57 -08001088
Saurav Das961beb22017-03-29 19:09:17 -07001089 ObjectiveContext context = new DefaultObjectiveContext(
1090 (objective) ->
1091 log.debug("createBroadcastGroupFromVlan installed "
1092 + "NextObj {} on {}", nextId, deviceId),
1093 (objective, error) ->
1094 log.warn("createBroadcastGroupFromVlan failed to install"
1095 + " NextObj {} on {}: {}", nextId, deviceId, error)
1096 );
1097 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das1a129a02016-11-18 15:21:57 -08001098 flowObjectiveService.next(deviceId, nextObj);
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001099 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1100 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001101
Charles Chan59cc16d2017-02-02 16:20:42 -08001102 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001103 }
1104
Charles Chane849c192016-01-11 18:28:54 -08001105 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001106 * Removes a single broadcast group from a given vlan id.
1107 * The group should be empty.
1108 * @param deviceId device Id to remove the group
1109 * @param portNum port number related to the group
1110 * @param vlanId vlan id of the broadcast group to remove
1111 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1112 */
1113 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1114 VlanId vlanId, boolean popVlan) {
1115 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1116
1117 if (!vlanNextObjStore.containsKey(key)) {
1118 log.debug("Broadcast group for device {} and subnet {} does not exist",
1119 deviceId, vlanId);
1120 return;
1121 }
1122
1123 TrafficSelector metadata =
1124 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1125
1126 int nextId = vlanNextObjStore.get(key);
1127
1128 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1129 .builder().withId(nextId)
1130 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1131 .withMeta(metadata);
1132
1133 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1134 if (popVlan) {
1135 tBuilder.popVlan();
1136 }
1137 tBuilder.setOutput(portNum);
1138 nextObjBuilder.addTreatment(tBuilder.build());
1139
1140 ObjectiveContext context = new DefaultObjectiveContext(
1141 (objective) ->
1142 log.debug("removeBroadcastGroupFromVlan removed "
1143 + "NextObj {} on {}", nextId, deviceId),
1144 (objective, error) ->
1145 log.warn("removeBroadcastGroupFromVlan failed to remove "
1146 + " NextObj {} on {}: {}", nextId, deviceId, error)
1147 );
1148 NextObjective nextObj = nextObjBuilder.remove(context);
1149 flowObjectiveService.next(deviceId, nextObj);
1150 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1151 nextId, deviceId);
1152
1153 vlanNextObjStore.remove(key, nextId);
1154 }
1155
1156 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001157 * Determine if we should pop given vlan before sending packets to the given port.
1158 *
1159 * @param portNumber port number
1160 * @param vlanId vlan id
1161 * @return true if the vlan id is not contained in any vlanTagged config
1162 */
1163 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001164 return srManager.interfaceService
1165 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan7ffd81f2017-02-08 15:52:08 -08001166 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1167 }
1168
1169 /**
Saurav Das4ce45962015-11-24 23:21:05 -08001170 * Create simple next objective for a single port. The treatments can include
1171 * all outgoing actions that need to happen on the packet.
1172 *
1173 * @param portNum the outgoing port on the device
1174 * @param treatment the actions to apply on the packets (should include outport)
1175 * @param meta optional data to pass to the driver
1176 */
1177 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1178 TrafficSelector meta) {
1179 int nextId = flowObjectiveService.allocateNextId();
1180 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das76ae6812017-03-15 15:15:14 -07001181 deviceId, portNum, treatment, meta);
Saurav Das4ce45962015-11-24 23:21:05 -08001182
1183 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1184 .builder().withId(nextId)
1185 .withType(NextObjective.Type.SIMPLE)
1186 .addTreatment(treatment)
1187 .fromApp(appId)
1188 .withMeta(meta);
1189
Saurav Das961beb22017-03-29 19:09:17 -07001190 ObjectiveContext context = new DefaultObjectiveContext(
1191 (objective) ->
1192 log.debug("createGroupFromPort installed "
1193 + "NextObj {} on {}", nextId, deviceId),
1194 (objective, error) ->
1195 log.warn("createGroupFromPort failed to install"
1196 + " NextObj {} on {}: {}", nextId, deviceId, error)
1197 );
1198 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das4ce45962015-11-24 23:21:05 -08001199 flowObjectiveService.next(deviceId, nextObj);
1200 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1201 + "for port {}", nextId, deviceId, portNum);
1202
1203 portNextObjStore.put(key, nextId);
1204 }
1205
sangho1e575652015-05-14 00:39:53 -07001206 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001207 * Removes simple next objective for a single port.
1208 *
1209 * @param deviceId device id that has the port to deal with
1210 * @param portNum the outgoing port on the device
1211 * @param vlanId vlan id associated with the port
1212 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1213 */
1214 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1215 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1216 mbuilder.matchVlanId(vlanId);
1217
1218 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1219 tbuilder.immediate().setOutput(portNum);
1220 if (popVlan) {
1221 tbuilder.immediate().popVlan();
1222 }
1223
1224 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1225 tbuilder.build(), mbuilder.build(), false);
1226
1227 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1228 deviceId, portNum, tbuilder.build(), mbuilder.build());
1229 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1230 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1231 .builder().withId(portNextObjId)
1232 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1233 ObjectiveContext context = new DefaultObjectiveContext(
1234 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1235 portNextObjId, deviceId),
1236 (objective, error) ->
1237 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1238 portNextObjId, deviceId, error));
1239 NextObjective nextObjective = nextObjBuilder.remove(context);
1240 log.info("**removePortNextObjective: Submitted "
1241 + "next objective {} in device {}",
1242 portNextObjId, deviceId);
1243 flowObjectiveService.next(deviceId, nextObjective);
1244
1245 portNextObjStore.remove(key);
1246 }
1247 }
1248 /**
sangho1e575652015-05-14 00:39:53 -07001249 * Removes groups for the next objective ID given.
1250 *
1251 * @param objectiveId next objective ID to remove
1252 * @return true if succeeds, false otherwise
1253 */
1254 public boolean removeGroup(int objectiveId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001255 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1256 dsNextObjStore.entrySet()) {
1257 if (e.getValue().nextId() != objectiveId) {
1258 continue;
1259 }
Pier Luigi63edd932018-01-14 21:56:11 +01001260 // Right now it is just used in TunnelHandler
1261 // remember in future that PW transit groups could
1262 // be Indirect groups
sangho1e575652015-05-14 00:39:53 -07001263 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1264 .builder().withId(objectiveId)
1265 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -07001266 ObjectiveContext context = new DefaultObjectiveContext(
1267 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1268 objectiveId, deviceId),
1269 (objective, error) ->
1270 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
1271 objectiveId, deviceId, error));
1272 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -08001273 log.info("**removeGroup: Submited "
1274 + "next objective {} in device {}",
1275 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -07001276 flowObjectiveService.next(deviceId, nextObjective);
1277
Saurav Das7bcbe702017-06-13 15:35:54 -07001278 dsNextObjStore.remove(e.getKey());
sangho0b2b6d12015-05-20 22:16:38 -07001279 return true;
sangho1e575652015-05-14 00:39:53 -07001280 }
1281
1282 return false;
1283 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001284 /**
1285 * Remove simple next objective for a single port. The treatments can include
1286 * all outgoing actions that need to happen on the packet.
1287 *
1288 * @param portNum the outgoing port on the device
1289 * @param treatment the actions applied on the packets (should include outport)
1290 * @param meta optional data to pass to the driver
1291 */
1292 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1293 TrafficSelector meta) {
1294 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1295 deviceId, portNum, treatment, meta);
1296 Integer nextId = portNextObjStore.get(key);
1297
1298 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1299 .builder().withId(nextId)
1300 .withType(NextObjective.Type.SIMPLE)
1301 .addTreatment(treatment)
1302 .fromApp(appId)
1303 .withMeta(meta);
1304
1305 ObjectiveContext context = new DefaultObjectiveContext(
1306 (objective) ->
1307 log.info("removeGroupFromPort installed "
1308 + "NextObj {} on {}", nextId, deviceId),
1309 (objective, error) ->
1310 log.warn("removeGroupFromPort failed to install"
1311 + " NextObj {} on {}: {}", nextId, deviceId, error)
1312 );
1313 NextObjective nextObj = nextObjBuilder.remove(context);
1314 flowObjectiveService.next(deviceId, nextObj);
1315 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1316 + "for port {}", nextId, deviceId, portNum);
1317
1318 portNextObjStore.remove(key);
1319 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001320
Charles Chane849c192016-01-11 18:28:54 -08001321 /**
1322 * Removes all groups from all next objective stores.
1323 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001324 /*public void removeAllGroups() {
1325 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das423fe2b2015-12-04 10:52:59 -08001326 nsNextObjStore.entrySet()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001327 removeGroup(entry.getValue().nextId());
Saurav Das423fe2b2015-12-04 10:52:59 -08001328 }
1329 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1330 portNextObjStore.entrySet()) {
1331 removeGroup(entry.getValue());
1332 }
Charles Chan59cc16d2017-02-02 16:20:42 -08001333 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1334 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001335 removeGroup(entry.getValue());
1336 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001337 }*/ //XXX revisit
1338
Saurav Dasceccf242017-08-03 18:30:35 -07001339 /**
1340 * Triggers a one time bucket verification operation on all hash groups
1341 * on this device.
1342 */
1343 public void triggerBucketCorrector() {
1344 BucketCorrector bc = new BucketCorrector();
1345 bc.run();
1346 }
1347
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001348 /**
1349 * Modifies L2IG bucket when the interface configuration is updated, especially
1350 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1351 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1352 * TrafficTreatment in turn.
1353 *
1354 * @param portNumber the port on this device that needs to be updated
1355 * @param vlanId the vlan id corresponding to this port
1356 * @param pushVlan indicates if packets should be sent out untagged or not out
1357 * from the port. If true, updated TrafficTreatment involves
1358 * pop vlan tag action. If false, updated TrafficTreatment
1359 * does not involve pop vlan tag action.
1360 */
1361 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
1362 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1363 if (pushVlan) {
1364 tBuilder.popVlan();
1365 }
1366 tBuilder.setOutput(portNumber);
1367
1368 TrafficSelector metadata =
1369 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1370
1371 int nextId = getVlanNextObjectiveId(vlanId);
1372
1373 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1374 .builder().withId(nextId)
1375 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1376 .addTreatment(tBuilder.build())
1377 .withMeta(metadata);
1378
1379 ObjectiveContext context = new DefaultObjectiveContext(
1380 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1381 portNumber, nextId, deviceId),
1382 (objective, error) ->
1383 log.warn("port {} failed to updated NextObj {} on {}: {}",
1384 portNumber, nextId, deviceId, error));
1385
1386 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001387 }
1388
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001389 /**
1390 * Adds a single port to the L2FG or removes it from the L2FG.
1391 *
1392 * @param vlanId the vlan id corresponding to this port
1393 * @param portNum the port on this device to be updated
1394 * @param nextId the next objective ID for the given vlan id
1395 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1396 */
1397 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001398 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1399 if (toPopVlan(portNum, vlanId)) {
1400 tBuilder.popVlan();
1401 }
1402 tBuilder.setOutput(portNum);
1403
1404 TrafficSelector metadata =
1405 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1406
1407 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1408 .builder().withId(nextId)
1409 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1410 .addTreatment(tBuilder.build())
1411 .withMeta(metadata);
1412
1413 ObjectiveContext context = new DefaultObjectiveContext(
1414 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1415 portNum, nextId, deviceId),
1416 (objective, error) ->
1417 log.warn("port {} failed to removedFrom NextObj {} on {}: {}",
1418 portNum, nextId, deviceId, error));
1419
1420 if (install) {
1421 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1422 } else {
1423 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1424 }
1425 }
Saurav Das1547b3f2017-05-05 17:01:08 -07001426
1427 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -07001428 * Performs bucket verification operation for all hash groups in this device.
1429 * Checks RouteHandler to ensure that routing is stable before attempting
1430 * verification. Verification involves creating a nextObjective with
1431 * operation VERIFY for existing next objectives in the store, and passing
1432 * it to the driver. It is the driver that actually performs the verification
1433 * by adding or removing buckets to match the verification next objective
1434 * created here.
Saurav Das1547b3f2017-05-05 17:01:08 -07001435 */
Saurav Dasceccf242017-08-03 18:30:35 -07001436 protected final class BucketCorrector implements Runnable {
1437 Integer nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001438
Saurav Dasceccf242017-08-03 18:30:35 -07001439 BucketCorrector() {
1440 this.nextId = null;
1441 }
1442
1443 BucketCorrector(Integer nextId) {
1444 this.nextId = nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001445 }
1446
1447 @Override
1448 public void run() {
Saurav Dasceccf242017-08-03 18:30:35 -07001449 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1450 return;
Saurav Das1547b3f2017-05-05 17:01:08 -07001451 }
Saurav Dasceccf242017-08-03 18:30:35 -07001452 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1453 if (rh == null) {
1454 return;
1455 }
1456 if (!rh.isRoutingStable()) {
1457 return;
1458 }
1459 rh.acquireRoutingLock();
1460 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001461 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07001462 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1463 .stream()
1464 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +01001465 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -08001466 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1467 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1468 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1469 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1470 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasceccf242017-08-03 18:30:35 -07001471 .map(entry -> entry.getKey())
1472 .collect(Collectors.toSet());
1473 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1474 NextNeighbors next = dsNextObjStore.get(dsKey);
1475 if (next == null) {
1476 continue;
1477 }
1478 int nid = next.nextId();
1479 if (nextId != null && nextId != nid) {
1480 continue;
1481 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001482 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasceccf242017-08-03 18:30:35 -07001483 deviceId, dsKey, next);
1484 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das09c2c4d2018-08-13 15:34:26 -07001485 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasceccf242017-08-03 18:30:35 -07001486 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1487 .withId(nid)
1488 .withType(NextObjective.Type.HASHED)
1489 .withMeta(metabuilder.build())
1490 .fromApp(appId);
1491
1492 next.dstNextHops().forEach((dstDev, nextHops) -> {
1493 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1494 nextHops.forEach(neighbor -> {
1495 MacAddress neighborMac;
1496 try {
1497 neighborMac = deviceConfig.getDeviceMac(neighbor);
1498 } catch (DeviceConfigNotFoundException e) {
1499 log.warn(e.getMessage() + " Aborting neighbor"
1500 + neighbor);
1501 return;
1502 }
1503 devicePortMap.get(neighbor).forEach(port -> {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001504 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasceccf242017-08-03 18:30:35 -07001505 + " port/label {}/{} to dst {} via {}",
1506 deviceId, nid, port, edgeLabel,
1507 dstDev, neighbor);
Saurav Dasa4020382018-02-14 14:14:54 -08001508 nextObjBuilder
1509 .addTreatment(treatmentBuilder(port,
1510 neighborMac,
1511 dsKey.destinationSet().swap(),
1512 edgeLabel));
Saurav Dasceccf242017-08-03 18:30:35 -07001513 });
1514 });
1515 });
1516
1517 NextObjective nextObjective = nextObjBuilder.verify();
1518 flowObjectiveService.next(deviceId, nextObjective);
1519 }
1520 } finally {
1521 rh.releaseRoutingLock();
1522 }
1523
1524 }
1525
1526 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
Saurav Dasa4020382018-02-14 14:14:54 -08001527 boolean swap, int edgeLabel) {
Saurav Dasceccf242017-08-03 18:30:35 -07001528 TrafficTreatment.Builder tBuilder =
1529 DefaultTrafficTreatment.builder();
1530 tBuilder.setOutput(outport)
1531 .setEthDst(dstMac)
1532 .setEthSrc(nodeMacAddr);
1533 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001534 if (swap) {
1535 // swap label case
1536 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1537 } else {
1538 // ecmp with label push case
1539 tBuilder.pushMpls()
1540 .copyTtlOut()
1541 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1542 }
Saurav Dasceccf242017-08-03 18:30:35 -07001543 }
1544 return tBuilder.build();
Saurav Das1547b3f2017-05-05 17:01:08 -07001545 }
1546 }
1547
Charles Chane0260e12018-08-22 17:56:47 -07001548 /**
1549 * Determines whether the hash group bucket should include a popVlan action.
1550 * We don't popVlan for PW.
1551 *
1552 * @param ds destination set
1553 * @return true if VLAN needs to be popped
1554 */
1555 private boolean popVlanInHashGroup(DestinationSet ds) {
1556 return (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.SWAP_NOT_BOS) &&
1557 (ds.getTypeOfDstSet() != DestinationSet.DestinationSetType.POP_NOT_BOS);
1558 }
Pier Luigi63edd932018-01-14 21:56:11 +01001559}