blob: 716178d5e1a00c983adbb4eff42975ab10327be9 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070016package org.onosproject.segmentrouting.grouphandler;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080017
Saurav Das9df5b7c2017-08-14 16:44:43 -070018import com.google.common.collect.ImmutableMap;
Pier Ventre917127a2016-10-31 16:49:19 -070019import com.google.common.collect.Iterables;
Saurav Das9df5b7c2017-08-14 16:44:43 -070020import com.google.common.collect.Lists;
Saurav Dasc88d4662017-05-15 15:34:25 -070021import com.google.common.collect.Sets;
22
Pier Ventre917127a2016-10-31 16:49:19 -070023import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080025import org.onlab.packet.MplsLabel;
Saurav Das423fe2b2015-12-04 10:52:59 -080026import org.onlab.packet.VlanId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070027import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import org.onosproject.core.ApplicationId;
Charles Chan59cc16d2017-02-02 16:20:42 -080029import org.onosproject.net.ConnectPoint;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
Saurav Das423fe2b2015-12-04 10:52:59 -080033import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das8a0732e2015-11-20 15:27:53 -080035import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan216e3c82016-04-23 14:48:16 -070038import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070039import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080042import org.onosproject.net.link.LinkService;
Saurav Dasceccf242017-08-03 18:30:35 -070043import org.onosproject.segmentrouting.DefaultRoutingHandler;
Saurav Das423fe2b2015-12-04 10:52:59 -080044import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080045import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
46import org.onosproject.segmentrouting.config.DeviceProperties;
Ruchi Sahota5d800282019-01-28 01:08:18 +000047import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Das7bcbe702017-06-13 15:35:54 -070048import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070049import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080050import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota5d800282019-01-28 01:08:18 +000051import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070052import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080053import org.slf4j.Logger;
54
Pier Ventre917127a2016-10-31 16:49:19 -070055import java.net.URI;
Charles Chan7ffd81f2017-02-08 15:52:08 -080056import java.util.Collection;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.Collections;
Saurav Dasc88d4662017-05-15 15:34:25 -070058import java.util.HashMap;
Pier Ventre917127a2016-10-31 16:49:19 -070059import java.util.HashSet;
60import java.util.List;
61import java.util.Map;
62import java.util.Set;
63import java.util.concurrent.ConcurrentHashMap;
Saurav Das1547b3f2017-05-05 17:01:08 -070064import java.util.concurrent.ScheduledExecutorService;
65import java.util.concurrent.TimeUnit;
Pier Ventre917127a2016-10-31 16:49:19 -070066import java.util.stream.Collectors;
67
68import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das1547b3f2017-05-05 17:01:08 -070069import static java.util.concurrent.Executors.newScheduledThreadPool;
70import static org.onlab.util.Tools.groupedThreads;
Pier Ventre917127a2016-10-31 16:49:19 -070071import static org.slf4j.LoggerFactory.getLogger;
72
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080073/**
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070074 * Default ECMP group handler creation module. This component creates a set of
75 * ECMP groups for every neighbor that this device is connected to based on
76 * whether the current device is an edge device or a transit device.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080077 */
78public class DefaultGroupHandler {
Ray Milkey9c9cde42018-01-12 14:22:06 -080079 private static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080080
Saurav Dasceccf242017-08-03 18:30:35 -070081 private static final long VERIFY_INTERVAL = 30; // secs
82
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080083 protected final DeviceId deviceId;
84 protected final ApplicationId appId;
85 protected final DeviceProperties deviceConfig;
86 protected final List<Integer> allSegmentIds;
Pier Ventree0ae7a32016-11-23 09:57:42 -080087 protected int ipv4NodeSegmentId = -1;
88 protected int ipv6NodeSegmentId = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -080089 protected boolean isEdgeRouter = false;
90 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080091 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070092 protected FlowObjectiveService flowObjectiveService;
Ruchi Sahota5d800282019-01-28 01:08:18 +000093 private DeviceConfiguration config;
94
Saurav Dasc88d4662017-05-15 15:34:25 -070095 /**
96 * local store for neighbor-device-ids and the set of ports on this device
97 * that connect to the same neighbor.
98 */
Saurav Das8a0732e2015-11-20 15:27:53 -080099 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
100 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700101 /**
102 * local store for ports on this device connected to neighbor-device-id.
103 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800104 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
105 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700106
Saurav Das7bcbe702017-06-13 15:35:54 -0700107 // distributed store for (device+destination-set) mapped to next-id and neighbors
108 protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
109 dsNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800110 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan59cc16d2017-02-02 16:20:42 -0800111 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
112 vlanNextObjStore = null;
Ruchi Sahota5d800282019-01-28 01:08:18 +0000113 // distributed store for (device+mac+vlan+treatment) mapped to next-id
114 protected EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
115 macVlanNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800116 // distributed store for (device+port+treatment) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -0800117 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
118 portNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800119 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800120
Saurav Das1547b3f2017-05-05 17:01:08 -0700121 private ScheduledExecutorService executorService
Saurav Dasceccf242017-08-03 18:30:35 -0700122 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das1547b3f2017-05-05 17:01:08 -0700123
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700124 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700125 .register(URI.class).register(HashSet.class)
Saurav Das7bcbe702017-06-13 15:35:54 -0700126 .register(DeviceId.class).register(PortNumber.class)
127 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700128 .register(PolicyGroupParams.class)
129 .register(GroupBucketIdentifier.class)
130 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800131
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700132 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
133 DeviceProperties config,
134 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700135 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800136 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800137 this.deviceId = checkNotNull(deviceId);
138 this.appId = checkNotNull(appId);
139 this.deviceConfig = checkNotNull(config);
140 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800141 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
142 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800143 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
144 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800145 this.isEdgeRouter = config.isEdgeDevice(deviceId);
146 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
147 } catch (DeviceConfigNotFoundException e) {
148 log.warn(e.getMessage()
149 + " Skipping value assignment in DefaultGroupHandler");
150 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700151 this.flowObjectiveService = flowObjService;
Saurav Das7bcbe702017-06-13 15:35:54 -0700152 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeye4afdb52017-04-05 09:42:04 -0700153 this.vlanNextObjStore = srManager.vlanNextObjStore();
154 this.portNextObjStore = srManager.portNextObjStore();
Ruchi Sahota5d800282019-01-28 01:08:18 +0000155 this.macVlanNextObjStore = srManager.macVlanNextObjStore();
Charles Chan188ebf52015-12-23 00:15:11 -0800156 this.srManager = srManager;
Saurav Dasceccf242017-08-03 18:30:35 -0700157 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
158 VERIFY_INTERVAL,
159 TimeUnit.SECONDS);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800160 populateNeighborMaps();
161 }
162
163 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700164 * Gracefully shuts down a groupHandler. Typically called when the handler is
165 * no longer needed.
166 */
167 public void shutdown() {
168 executorService.shutdown();
169 }
170
171 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700172 * Creates a group handler object.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800173 *
174 * @param deviceId device identifier
175 * @param appId application identifier
176 * @param config interface to retrieve the device properties
177 * @param linkService link service object
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700178 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800179 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800180 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800181 * @return default group handler type
182 */
Saurav Das4ce45962015-11-24 23:21:05 -0800183 public static DefaultGroupHandler createGroupHandler(
Saurav Dasceccf242017-08-03 18:30:35 -0700184 DeviceId deviceId,
185 ApplicationId appId,
186 DeviceProperties config,
187 LinkService linkService,
188 FlowObjectiveService flowObjService,
189 SegmentRoutingManager srManager)
190 throws DeviceConfigNotFoundException {
Saurav Dasc88d4662017-05-15 15:34:25 -0700191 return new DefaultGroupHandler(deviceId, appId, config,
192 linkService,
193 flowObjService,
194 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800195 }
196
197 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800198 * Updates local stores for link-src-device/port to neighbor (link-dst) for
199 * link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -0700200 *
201 * @param link the infrastructure link
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800202 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700203 public void portUpForLink(Link link) {
Saurav Dasceccf242017-08-03 18:30:35 -0700204 if (!link.src().deviceId().equals(deviceId)) {
205 log.warn("linkUp: deviceId{} doesn't match with link src {}",
206 deviceId, link.src().deviceId());
207 return;
208 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700209
Saurav Dasceccf242017-08-03 18:30:35 -0700210 log.info("* portUpForLink: Device {} linkUp at local port {} to "
211 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
212 // ensure local state is updated even if linkup is aborted later on
213 addNeighborAtPort(link.dst().deviceId(),
214 link.src().port());
215 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700216
Saurav Dasceccf242017-08-03 18:30:35 -0700217 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800218 * Updates local stores for link-src-device/port to neighbor (link-dst) for
219 * link that has gone down.
Saurav Dasceccf242017-08-03 18:30:35 -0700220 *
Saurav Dasa4020382018-02-14 14:14:54 -0800221 * @param link the infrastructure link
Saurav Dasceccf242017-08-03 18:30:35 -0700222 */
Saurav Dasa4020382018-02-14 14:14:54 -0800223 public void portDownForLink(Link link) {
224 PortNumber port = link.src().port();
Saurav Dasceccf242017-08-03 18:30:35 -0700225 if (portDeviceMap.get(port) == null) {
226 log.warn("portDown: unknown port");
227 return;
228 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700229
Saurav Dasceccf242017-08-03 18:30:35 -0700230 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
231 portDeviceMap.get(port));
232 devicePortMap.get(portDeviceMap.get(port)).remove(port);
233 portDeviceMap.remove(port);
234 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800235
236 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800237 * Cleans up local stores for removed neighbor device.
238 *
239 * @param neighborId the device identifier for the neighbor device
240 */
241 public void cleanUpForNeighborDown(DeviceId neighborId) {
242 Set<PortNumber> ports = devicePortMap.remove(neighborId);
243 if (ports != null) {
244 ports.forEach(p -> portDeviceMap.remove(p));
245 }
246 }
247
248 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700249 * Checks all groups in the src-device of link for neighbor sets that include
250 * the dst-device of link, and edits the hash groups according to link up
251 * or down. Should only be called by the master instance of the src-switch
252 * of link. Typically used when there are no route-path changes due to the
253 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800254 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700255 * @param link the infrastructure link that has gone down or come up
256 * @param linkDown true if link has gone down
257 * @param firstTime true if link has come up for the first time i.e a link
258 * not seen-before
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800259 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700260 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700261 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800262 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700263 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800264 } catch (DeviceConfigNotFoundException e) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700265 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800266 return;
267 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700268 // find all the destinationSets related to link
269 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700270 .stream()
Saurav Das7bcbe702017-06-13 15:35:54 -0700271 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +0100272 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -0800273 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
274 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
275 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
276 .filter(entry -> !entry.getKey().destinationSet().swap() ||
277 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Das7bcbe702017-06-13 15:35:54 -0700278 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
279 .map(entry -> entry.getKey())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700280 .collect(Collectors.toSet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700281
Saurav Das7bcbe702017-06-13 15:35:54 -0700282 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
283 deviceId, link.dst().deviceId(), dsKeySet);
284
285 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
286 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
287 if (nextHops == null) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700288 log.warn("retryHash in device {}, but global store has no record "
Saurav Das7bcbe702017-06-13 15:35:54 -0700289 + "for dsKey:{}", deviceId, dsKey);
Saurav Dasc88d4662017-05-15 15:34:25 -0700290 continue;
291 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700292 int nextId = nextHops.nextId();
293 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -0700294 if (!linkDown) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700295 List<PortLabel> pl = Lists.newArrayList();
Saurav Dasc88d4662017-05-15 15:34:25 -0700296 if (firstTime) {
297 // some links may have come up before the next-objective was created
298 // we take this opportunity to ensure other ports to same next-hop-dst
299 // are part of the hash group (see CORD-1180). Duplicate additions
300 // to the same hash group are avoided by the driver.
301 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700302 dstSet.forEach(dst -> {
303 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700304 pl.add(new PortLabel(p, edgeLabel));
Saurav Das7bcbe702017-06-13 15:35:54 -0700305 });
Saurav Dasc88d4662017-05-15 15:34:25 -0700306 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700307 addToHashedNextObjective(pl, neighborMac, nextId);
308 } else {
309 // handle only the port that came up
310 dstSet.forEach(dst -> {
311 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
312 pl.add(new PortLabel(link.src().port(), edgeLabel));
313 });
314 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700315 }
316 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700317 // linkdown
318 List<PortLabel> pl = Lists.newArrayList();
Saurav Das7bcbe702017-06-13 15:35:54 -0700319 dstSet.forEach(dst -> {
320 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700321 pl.add(new PortLabel(link.src().port(), edgeLabel));
Saurav Das7bcbe702017-06-13 15:35:54 -0700322 });
Saurav Das9df5b7c2017-08-14 16:44:43 -0700323 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700324 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800325 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700326 }
327
Saurav Dasc88d4662017-05-15 15:34:25 -0700328 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700329 * Utility class for associating output ports and the corresponding MPLS
330 * labels to push. In dual-homing, there are different labels to push
331 * corresponding to the destination switches in an edge-pair. If both
332 * destinations are reachable via the same spine, then the output-port to
333 * the spine will be associated with two labels i.e. there will be two
334 * PortLabel objects for the same port but with different labels.
335 */
336 private class PortLabel {
337 PortNumber port;
338 int edgeLabel;
339
340 PortLabel(PortNumber port, int edgeLabel) {
341 this.port = port;
342 this.edgeLabel = edgeLabel;
343 }
344
345 @Override
346 public String toString() {
347 return port.toString() + "/" + String.valueOf(edgeLabel);
348 }
349 }
350
351 /**
352 * Makes a call to the FlowObjective service to add buckets to
353 * a hashed group. User must ensure that all the ports & labels are meant
354 * same neighbor (ie. dstMac).
Saurav Dasc88d4662017-05-15 15:34:25 -0700355 *
Pier Luigi63edd932018-01-14 21:56:11 +0100356 * @param portLabels a collection of port & label combinations to add
Saurav Das9df5b7c2017-08-14 16:44:43 -0700357 * to the hash group identified by the nextId
Saurav Dasc88d4662017-05-15 15:34:25 -0700358 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700359 * @param nextId id for next-objective to which buckets will be added
Saurav Dasceccf242017-08-03 18:30:35 -0700360 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700361 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700362 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
363 MacAddress dstMac, Integer nextId) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700364 // setup metadata to pass to nextObjective - indicate the vlan on egress
365 // if needed by the switch pipeline. Since hashed next-hops are always to
366 // other neighboring routers, there is no subnet assigned on those ports.
367 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das3c82f192018-08-13 15:34:26 -0700368 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Das1547b3f2017-05-05 17:01:08 -0700369 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
370 .withId(nextId)
371 .withType(NextObjective.Type.HASHED)
Saurav Das1547b3f2017-05-05 17:01:08 -0700372 .withMeta(metabuilder.build())
373 .fromApp(appId);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700374 // Create the new buckets to be updated
375 portLabels.forEach(pl -> {
376 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
377 tBuilder.setOutput(pl.port)
378 .setEthDst(dstMac)
379 .setEthSrc(nodeMacAddr);
380 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
381 tBuilder.pushMpls()
382 .copyTtlOut()
383 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
384 }
385 nextObjBuilder.addTreatment(tBuilder.build());
386 });
387
388 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
389 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700390
391 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das9df5b7c2017-08-14 16:44:43 -0700392 (objective) -> log.debug("addToHash port/label {} addedTo "
393 + "NextObj {} on {}", portLabels, nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -0700394 (objective, error) -> {
395 log.warn("addToHash failed to add port/label {} to NextObj {} on {}: {}",
396 portLabels, nextId, deviceId, error);
397 srManager.invalidateNextObj(objective.id());
398 });
Saurav Das1547b3f2017-05-05 17:01:08 -0700399 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
400 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800401 }
402
403 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -0700404 * Makes a call to the FlowObjective service to remove buckets from
405 * a hash group. User must ensure that all the ports & labels are meant
406 * same neighbor (ie. dstMac).
Saurav Dasceccf242017-08-03 18:30:35 -0700407 *
Pier Luigi63edd932018-01-14 21:56:11 +0100408 * @param portLabels a collection of port & label combinations to remove
Saurav Das9df5b7c2017-08-14 16:44:43 -0700409 * from the hash group identified by the nextId
Saurav Dasceccf242017-08-03 18:30:35 -0700410 * @param dstMac destination mac address of next-hop
Saurav Das9df5b7c2017-08-14 16:44:43 -0700411 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasceccf242017-08-03 18:30:35 -0700412 */
Saurav Das9df5b7c2017-08-14 16:44:43 -0700413 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
414 MacAddress dstMac, Integer nextId) {
Saurav Dasceccf242017-08-03 18:30:35 -0700415 NextObjective.Builder nextObjBuilder = DefaultNextObjective
416 .builder()
417 .withType(NextObjective.Type.HASHED) //same as original
418 .withId(nextId)
Saurav Das9df5b7c2017-08-14 16:44:43 -0700419 .fromApp(appId);
420 // Create the buckets to be removed
421 portLabels.forEach(pl -> {
422 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
423 tBuilder.setOutput(pl.port)
424 .setEthDst(dstMac)
425 .setEthSrc(nodeMacAddr);
426 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 Chan1dbadbd2018-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 Dasf1027d42018-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 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700517 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId,
518 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),
594 edgeLabel, nextId, false);
595 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
620 * @param revoke true if buckets need to be removed, false if they need to
621 * be added
622 * @return true if successful in adding or removing buckets for all ports
623 * to the neighbors
624 */
625 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Saurav Das7bcbe702017-06-13 15:35:54 -0700626 int nextId, boolean revoke) {
Saurav Dasceccf242017-08-03 18:30:35 -0700627 for (DeviceId neighbor : neighbors) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700628 MacAddress neighborMac;
Saurav Das7bcbe702017-06-13 15:35:54 -0700629 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700630 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das7bcbe702017-06-13 15:35:54 -0700631 } catch (DeviceConfigNotFoundException e) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700632 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
633 + " for nextId:" + nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700634 return false;
635 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700636 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
637 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700638 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Das9df5b7c2017-08-14 16:44:43 -0700639 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700640 deviceId, neighbor, nextId);
641 return false;
642 }
Saurav Das9df5b7c2017-08-14 16:44:43 -0700643 List<PortLabel> pl = Lists.newArrayList();
644 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel)));
Saurav Das7bcbe702017-06-13 15:35:54 -0700645 if (revoke) {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700646 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
647 + "with Port/Label:{} to next object id {}",
648 deviceId, pl, nextId);
649 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700650 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -0700651 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
652 + "with Port/Label: {} to next object id {}",
653 deviceId, pl, nextId);
654 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700655 }
656 }
657 return true;
658 }
659
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800660 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800661 * Returns true if the destination set is meant for swap or multi-labeled
662 * packet transport, and MPLS ECMP is not supported.
663 *
664 * @param dskey the key representing the destination set
665 * @return true if destination set is meant for simple next objectives
666 */
667 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
668 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
669 && !srManager.getMplsEcmp();
670 }
671
672 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800673 * Adds or removes a port that has been configured with a vlan to a broadcast group
674 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800675 *
676 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800677 * @param vlanId the vlan id corresponding to the broadcast domain/group
678 * @param popVlan indicates if packets should be sent out untagged or not out
679 * of the port. If true, indicates an access (untagged) or native vlan
680 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800681 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800682 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800683 public void processEdgePort(PortNumber port, VlanId vlanId,
684 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800685 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800686 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800687 if (nextId == -1) {
688 if (portUp) {
689 log.debug("**Creating flooding group for first port enabled in"
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800690 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan59cc16d2017-02-02 16:20:42 -0800691 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800692 } else {
693 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800694 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800695 }
696 return;
697 }
698
699 log.info("**port{} in device {}: {} Bucket with Port {} to"
700 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
701 (portUp) ? "Adding" : "Removing",
702 port, nextId);
703 // Create the bucket to be added or removed
704 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800705 if (popVlan) {
706 tBuilder.popVlan();
707 }
Saurav Das1a129a02016-11-18 15:21:57 -0800708 tBuilder.setOutput(port);
709
Saurav Das1a129a02016-11-18 15:21:57 -0800710 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800711 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800712
713 NextObjective.Builder nextObjBuilder = DefaultNextObjective
714 .builder().withId(nextId)
715 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
716 .addTreatment(tBuilder.build())
717 .withMeta(metadata);
718
719 ObjectiveContext context = new DefaultObjectiveContext(
720 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
721 port, (portUp) ? "addedTo" : "removedFrom",
722 nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -0700723 (objective, error) -> {
724 log.warn("port {} failed to {} NextObj {} on {}: {}",
725 port, (portUp) ? "addTo" : "removeFrom", nextId, deviceId, error);
726 srManager.invalidateNextObj(objective.id());
727 });
Saurav Das1a129a02016-11-18 15:21:57 -0800728
729 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
730 : nextObjBuilder.removeFromExisting(context);
731 log.debug("edgePort processed: Submited next objective {} in device {}",
732 nextId, deviceId);
733 flowObjectiveService.next(deviceId, nextObj);
734 }
735
736 /**
Saurav Dasa4020382018-02-14 14:14:54 -0800737 * Returns the next objective of type hashed (or simple) associated with the
738 * destination set. In addition, updates the existing next-objective if new
739 * route-paths found have resulted in the addition of new next-hops to a
740 * particular destination. If there is no existing next objective for this
741 * destination set, this method would create a next objective and return the
742 * nextId. Optionally metadata can be passed in for the creation of the next
743 * objective. If the parameter simple is true then a simple next objective
744 * is created instead of a hashed one.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800745 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700746 * @param ds destination set
747 * @param nextHops a map of per destination next hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800748 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800749 * @param simple if true, a simple next objective will be created instead of
750 * a hashed next objective
Saurav Das8a0732e2015-11-20 15:27:53 -0800751 * @return int if found or -1 if there are errors in the creation of the
Saurav Dasa4020382018-02-14 14:14:54 -0800752 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800753 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700754 public int getNextObjectiveId(DestinationSet ds,
755 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Dasa4020382018-02-14 14:14:54 -0800756 TrafficSelector meta, boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700757 NextNeighbors next = dsNextObjStore.
758 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
759 if (next == null) {
760 log.debug("getNextObjectiveId in device{}: Next objective id "
761 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700762 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
763 deviceId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700764 dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700765 .stream()
766 .filter((nsStoreEntry) ->
767 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
768 .collect(Collectors.toList()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700769
Saurav Dasa4020382018-02-14 14:14:54 -0800770 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das7bcbe702017-06-13 15:35:54 -0700771 next = dsNextObjStore.
772 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
773 if (next == null) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700774 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das7bcbe702017-06-13 15:35:54 -0700775 // failure in creating group
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700776 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700777 } else {
778 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700779 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700780 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700781 } else {
782 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700783 + "found for {}", deviceId, next.nextId(), ds);
784 // should fix hash groups too if next-hops have changed
785 if (!next.dstNextHops().equals(nextHops)) {
786 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
787 deviceId, next.nextId());
788 if (!updateNextHops(ds, nextHops)) {
789 // failure in updating group
790 return -1;
791 }
792 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700793 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700794 return next.nextId();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800795 }
796
sangho0b2b6d12015-05-20 22:16:38 -0700797 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800798 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800799 * or -1 if no such objective exists. Note that this method does NOT create
800 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800801 * created at startup from network configuration. Typically this is used
802 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700803 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800804 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700805 * @return int if found or -1
806 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800807 public int getVlanNextObjectiveId(VlanId vlanId) {
808 Integer nextId = vlanNextObjStore.
809 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700810
811 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700812 }
813
814 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +0000815 * Returns the next objective of type simple associated with the mac/vlan on the
816 * device, given the treatment. Different treatments to the same mac/vlan result
817 * in different next objectives. If no such objective exists, this method
818 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
819 * the creation of the objective. Typically this is used for L2 and L3 forwarding
820 * to compute nodes and containers/VMs on the compute nodes directly attached
821 * to the switch.
822 *
823 * @param macAddr the mac addr for the simple next objective
824 * @param vlanId the vlan 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
827 * @param createIfMissing true if a next object should be created if not found
828 * @return int if found or created, -1 if there are errors during the
829 * creation of the next objective.
830 */
831 public int getMacVlanNextObjectiveId(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment,
832 TrafficSelector meta, boolean createIfMissing) {
833
834 Integer nextId = macVlanNextObjStore
835 .get(new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId));
836
837 if (nextId != null) {
838 return nextId;
839 }
840
841 log.debug("getMacVlanNextObjectiveId in device {}: Next objective id "
842 + "not found for host : {}/{} .. {}", deviceId, macAddr, vlanId,
843 (createIfMissing) ? "creating" : "aborting");
844
845 if (!createIfMissing) {
846 return -1;
847 }
848
849 /* create missing next objective */
850 nextId = createGroupFromMacVlan(macAddr, vlanId, treatment, meta);
851 if (nextId == null) {
852 log.warn("getMacVlanNextObjectiveId: unable to create next obj"
853 + "for dev:{} host:{}/{}", deviceId, macAddr, vlanId);
854 return -1;
855 }
856 return nextId;
857 }
858
859
860 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800861 * Returns the next objective of type simple associated with the port on the
862 * device, given the treatment. Different treatments to the same port result
863 * in different next objectives. If no such objective exists, this method
Saurav Das961beb22017-03-29 19:09:17 -0700864 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800865 * the creation of the objective. Typically this is used for L2 and L3 forwarding
866 * to compute nodes and containers/VMs on the compute nodes directly attached
867 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800868 *
869 * @param portNum the port number for the simple next objective
870 * @param treatment the actions to apply on the packets (should include outport)
871 * @param meta optional metadata passed into the creation of the next objective
Saurav Das961beb22017-03-29 19:09:17 -0700872 * @param createIfMissing true if a next object should be created if not found
Saurav Das4ce45962015-11-24 23:21:05 -0800873 * @return int if found or created, -1 if there are errors during the
874 * creation of the next objective.
875 */
876 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700877 TrafficSelector meta, boolean createIfMissing) {
Charles Chane849c192016-01-11 18:28:54 -0800878 Integer nextId = portNextObjStore
Saurav Das76ae6812017-03-15 15:15:14 -0700879 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das961beb22017-03-29 19:09:17 -0700880 if (nextId != null) {
881 return nextId;
882 }
883 log.debug("getPortNextObjectiveId in device {}: Next objective id "
884 + "not found for port: {} .. {}", deviceId, portNum,
885 (createIfMissing) ? "creating" : "aborting");
886 if (!createIfMissing) {
887 return -1;
888 }
889 // create missing next objective
890 createGroupFromPort(portNum, treatment, meta);
891 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
892 treatment, meta));
Saurav Das4ce45962015-11-24 23:21:05 -0800893 if (nextId == null) {
Saurav Das961beb22017-03-29 19:09:17 -0700894 log.warn("getPortNextObjectiveId: unable to create next obj"
895 + "for dev:{} port:{}", deviceId, portNum);
896 return -1;
Charles Chane849c192016-01-11 18:28:54 -0800897 }
898 return nextId;
899 }
900
901 /**
sangho0b2b6d12015-05-20 22:16:38 -0700902 * Checks if the next objective ID (group) for the neighbor set exists or not.
903 *
904 * @param ns neighbor set to check
905 * @return true if it exists, false otherwise
906 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700907 public boolean hasNextObjectiveId(DestinationSet ns) {
908 NextNeighbors nextHops = dsNextObjStore.
909 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
910 if (nextHops == null) {
sangho0b2b6d12015-05-20 22:16:38 -0700911 return false;
912 }
913
914 return true;
915 }
916
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800917 private void populateNeighborMaps() {
918 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700919 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800920 if (link.type() != Link.Type.DIRECT) {
921 continue;
922 }
923 addNeighborAtPort(link.dst().deviceId(), link.src().port());
924 }
925 }
926
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700927 protected void addNeighborAtPort(DeviceId neighborId,
928 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800929 // Update DeviceToPort database
930 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
931 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800932 Set<PortNumber> ports = Collections
933 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
934 ports.add(portToNeighbor);
935 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
936 if (portnums != null) {
937 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800938 }
939
940 // Update portToDevice database
Saurav Dasa4020382018-02-14 14:14:54 -0800941 // should always update as neighbor could have changed on this port
942 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800943 if (prev != null) {
Saurav Das5a356042018-04-06 20:16:01 -0700944 log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Dasc88d4662017-05-15 15:34:25 -0700945 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800946 }
947 }
948
sangho1e575652015-05-14 00:39:53 -0700949 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700950 * Creates a NextObjective for a hash group in this device from a given
Saurav Dasa4020382018-02-14 14:14:54 -0800951 * DestinationSet. If the parameter simple is true, a simple next objective
952 * is created instead.
sangho1e575652015-05-14 00:39:53 -0700953 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700954 * @param ds the DestinationSet
955 * @param neighbors a map for each destination and its next-hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800956 * @param meta metadata passed into the creation of a Next Objective
Saurav Dasa4020382018-02-14 14:14:54 -0800957 * @param simple if true, a simple next objective will be created instead of
958 * a hashed next objective
sangho1e575652015-05-14 00:39:53 -0700959 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700960 public void createGroupFromDestinationSet(DestinationSet ds,
961 Map<DeviceId, Set<DeviceId>> neighbors,
962 TrafficSelector meta,
Saurav Dasa4020382018-02-14 14:14:54 -0800963 boolean simple) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700964 int nextId = flowObjectiveService.allocateNextId();
Saurav Dasa4020382018-02-14 14:14:54 -0800965 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
966 : NextObjective.Type.HASHED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700967 if (neighbors == null || neighbors.isEmpty()) {
968 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
969 + "to create group in dev:{} for ds: {} with next-hops {}",
970 deviceId, ds, neighbors);
971 return;
972 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700973
974 NextObjective.Builder nextObjBuilder = DefaultNextObjective
975 .builder()
976 .withId(nextId)
977 .withType(type)
978 .fromApp(appId);
979 if (meta != null) {
980 nextObjBuilder.withMeta(meta);
981 }
982
983 // create treatment buckets for each neighbor for each dst Device
984 // except in the special case where we only want to pick a single
Saurav Dasa4020382018-02-14 14:14:54 -0800985 // neighbor/port for a simple nextObj
Saurav Das7bcbe702017-06-13 15:35:54 -0700986 boolean foundSingleNeighbor = false;
987 boolean treatmentAdded = false;
988 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
989 for (DeviceId dst : ds.getDestinationSwitches()) {
990 Set<DeviceId> nextHops = neighbors.get(dst);
991 if (nextHops == null || nextHops.isEmpty()) {
992 continue;
Pier Ventre917127a2016-10-31 16:49:19 -0700993 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700994
995 if (foundSingleNeighbor) {
996 break;
997 }
998
999 for (DeviceId neighborId : nextHops) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001000 if (devicePortMap.get(neighborId) == null) {
1001 log.warn("Neighbor {} is not in the port map yet for dev:{}",
1002 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -07001003 return;
Jon Hallcbd1b392017-01-18 20:15:44 -08001004 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001005 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -08001006 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001007 return;
sangho834e4b02015-05-01 09:38:25 -07001008 }
1009
Saurav Das8a0732e2015-11-20 15:27:53 -08001010 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -08001011 try {
Saurav Das8a0732e2015-11-20 15:27:53 -08001012 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -08001013 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001014 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan0b4e6182015-11-03 10:42:14 -08001015 return;
1016 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001017 // For each port to the neighbor, we create a new treatment
Pier Ventre917127a2016-10-31 16:49:19 -07001018 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Dasa4020382018-02-14 14:14:54 -08001019 // In this case we need a SIMPLE nextObj. We randomly pick a port
1020 if (simple) {
Pier Ventre917127a2016-10-31 16:49:19 -07001021 int size = devicePortMap.get(neighborId).size();
1022 int index = RandomUtils.nextInt(0, size);
1023 neighborPorts = Collections.singleton(
Saurav Das7bcbe702017-06-13 15:35:54 -07001024 Iterables.get(devicePortMap.get(neighborId),
1025 index));
1026 foundSingleNeighbor = true;
Pier Ventre917127a2016-10-31 16:49:19 -07001027 }
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001028
Pier Ventre917127a2016-10-31 16:49:19 -07001029 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001030 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
1031 .builder();
Saurav Das7bcbe702017-06-13 15:35:54 -07001032 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
1033 int edgeLabel = ds.getEdgeLabel(dst);
1034 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001035 if (simple) {
1036 // swap label case
1037 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1038 } else {
1039 // ecmp with label push case
1040 tBuilder.pushMpls().copyTtlOut()
1041 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1042 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001043 }
Andreas Pantelopoulose8d5f412018-05-01 14:56:05 -07001044 if ((ds.getTypeOfDstSet() == DestinationSet.DestinationSetType.SWAP_NOT_BOS) ||
1045 (ds.getTypeOfDstSet() == DestinationSet.DestinationSetType.POP_NOT_BOS)) {
Saurav Das3c82f192018-08-13 15:34:26 -07001046 tBuilder.setVlanId(srManager.getPwTransportVlan());
Andreas Pantelopoulose8d5f412018-05-01 14:56:05 -07001047 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001048 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001049 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das7bcbe702017-06-13 15:35:54 -07001050 treatmentAdded = true;
1051 //update store
1052 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
1053 if (existingNeighbors == null) {
1054 existingNeighbors = new HashSet<>();
1055 }
1056 existingNeighbors.add(neighborId);
1057 dstNextHops.put(dst, existingNeighbors);
1058 log.debug("creating treatment for port/label {}/{} in next:{}",
1059 sp, edgeLabel, nextId);
1060 }
1061
1062 if (foundSingleNeighbor) {
1063 break;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001064 }
1065 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001066 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001067
1068 if (!treatmentAdded) {
1069 log.warn("Could not createGroup from DestinationSet {} without any"
1070 + "next hops {}", ds, neighbors);
1071 return;
1072 }
1073 ObjectiveContext context = new DefaultObjectiveContext(
1074 (objective) ->
1075 log.debug("createGroupsFromDestinationSet installed "
1076 + "NextObj {} on {}", nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001077 (objective, error) -> {
1078 log.warn("createGroupsFromDestinationSet failed to install NextObj {} on {}: {}",
1079 nextId, deviceId, error);
1080 srManager.invalidateNextObj(objective.id());
1081 });
Saurav Das7bcbe702017-06-13 15:35:54 -07001082 NextObjective nextObj = nextObjBuilder.add(context);
1083 log.debug(".. createGroupsFromDestinationSet: Submitted "
1084 + "next objective {} in device {}", nextId, deviceId);
1085 flowObjectiveService.next(deviceId, nextObj);
1086 //update store
1087 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1088 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001089 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -07001090
Saurav Das4ce45962015-11-24 23:21:05 -08001091 /**
Saurav Das1a129a02016-11-18 15:21:57 -08001092 * Creates broadcast groups for all ports in the same subnet for
1093 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -08001094 */
Charles Chan59cc16d2017-02-02 16:20:42 -08001095 public void createGroupsFromVlanConfig() {
Charles Chan7ffd81f2017-02-08 15:52:08 -08001096 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -08001097 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -08001098 });
Saurav Das1a129a02016-11-18 15:21:57 -08001099 }
Charles Chan9f676b62015-10-29 14:58:10 -07001100
Saurav Das1a129a02016-11-18 15:21:57 -08001101 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001102 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -08001103 *
Charles Chan59cc16d2017-02-02 16:20:42 -08001104 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -08001105 * @param ports list of ports in the subnet
1106 */
Charles Chan7ffd81f2017-02-08 15:52:08 -08001107 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001108 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -07001109
Charles Chan59cc16d2017-02-02 16:20:42 -08001110 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -08001111 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -08001112 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001113 return;
1114 }
Charles Chan188ebf52015-12-23 00:15:11 -08001115
Saurav Das1a129a02016-11-18 15:21:57 -08001116 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -08001117 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -07001118
Saurav Das1a129a02016-11-18 15:21:57 -08001119 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -07001120
Saurav Das1a129a02016-11-18 15:21:57 -08001121 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1122 .builder().withId(nextId)
1123 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1124 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -07001125
Saurav Das1a129a02016-11-18 15:21:57 -08001126 ports.forEach(port -> {
1127 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -08001128 if (toPopVlan(port, vlanId)) {
1129 tBuilder.popVlan();
1130 }
Saurav Das1a129a02016-11-18 15:21:57 -08001131 tBuilder.setOutput(port);
1132 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -07001133 });
Saurav Das1a129a02016-11-18 15:21:57 -08001134
Saurav Das961beb22017-03-29 19:09:17 -07001135 ObjectiveContext context = new DefaultObjectiveContext(
1136 (objective) ->
1137 log.debug("createBroadcastGroupFromVlan installed "
1138 + "NextObj {} on {}", nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001139 (objective, error) -> {
1140 log.warn("createBroadcastGroupFromVlan failed to install NextObj {} on {}: {}",
1141 nextId, deviceId, error);
1142 srManager.invalidateNextObj(objective.id());
1143 });
Saurav Das961beb22017-03-29 19:09:17 -07001144 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das1a129a02016-11-18 15:21:57 -08001145 flowObjectiveService.next(deviceId, nextObj);
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001146 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1147 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001148
Charles Chan59cc16d2017-02-02 16:20:42 -08001149 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001150 }
1151
Charles Chane849c192016-01-11 18:28:54 -08001152 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001153 * Removes a single broadcast group from a given vlan id.
1154 * The group should be empty.
1155 * @param deviceId device Id to remove the group
1156 * @param portNum port number related to the group
1157 * @param vlanId vlan id of the broadcast group to remove
1158 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1159 */
1160 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1161 VlanId vlanId, boolean popVlan) {
1162 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1163
1164 if (!vlanNextObjStore.containsKey(key)) {
1165 log.debug("Broadcast group for device {} and subnet {} does not exist",
1166 deviceId, vlanId);
1167 return;
1168 }
1169
1170 TrafficSelector metadata =
1171 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1172
1173 int nextId = vlanNextObjStore.get(key);
1174
1175 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1176 .builder().withId(nextId)
1177 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1178 .withMeta(metadata);
1179
1180 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1181 if (popVlan) {
1182 tBuilder.popVlan();
1183 }
1184 tBuilder.setOutput(portNum);
1185 nextObjBuilder.addTreatment(tBuilder.build());
1186
1187 ObjectiveContext context = new DefaultObjectiveContext(
1188 (objective) ->
1189 log.debug("removeBroadcastGroupFromVlan removed "
1190 + "NextObj {} on {}", nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001191 (objective, error) -> {
1192 log.warn("removeBroadcastGroupFromVlan failed to remove NextObj {} on {}: {}",
1193 nextId, deviceId, error);
1194 srManager.invalidateNextObj(objective.id());
1195 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001196 NextObjective nextObj = nextObjBuilder.remove(context);
1197 flowObjectiveService.next(deviceId, nextObj);
1198 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1199 nextId, deviceId);
1200
1201 vlanNextObjStore.remove(key, nextId);
1202 }
1203
1204 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001205 * Determine if we should pop given vlan before sending packets to the given port.
1206 *
1207 * @param portNumber port number
1208 * @param vlanId vlan id
1209 * @return true if the vlan id is not contained in any vlanTagged config
1210 */
1211 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001212 return srManager.interfaceService
1213 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan7ffd81f2017-02-08 15:52:08 -08001214 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1215 }
1216
1217 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001218 * Create simple next objective for an indirect host mac/vlan. The treatments can include
1219 * all outgoing actions that need to happen on the packet.
1220 *
1221 * @param macAddr the mac address of the host
1222 * @param vlanId the vlan of the host
1223 * @param treatment the actions to apply on the packets (should include outport)
1224 * @param meta optional data to pass to the driver
1225 * @return next objective ID
1226 */
1227 public int createGroupFromMacVlan(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment,
1228 TrafficSelector meta) {
1229 int nextId = flowObjectiveService.allocateNextId();
1230 MacVlanNextObjectiveStoreKey key = new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId);
1231
1232 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1233 .builder().withId(nextId)
1234 .withType(NextObjective.Type.SIMPLE)
1235 .addTreatment(treatment)
1236 .fromApp(appId)
1237 .withMeta(meta);
1238
1239 ObjectiveContext context = new DefaultObjectiveContext(
1240 (objective) ->
1241 log.debug("createGroupFromMacVlan installed "
1242 + "NextObj {} on {}", nextId, deviceId),
1243 (objective, error) -> {
1244 log.warn("createGroupFromMacVlan failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1245 srManager.invalidateNextObj(objective.id());
1246 });
1247 NextObjective nextObj = nextObjBuilder.add(context);
1248 flowObjectiveService.next(deviceId, nextObj);
1249 log.debug("createGroupFromMacVlan: Submited next objective {} in device {} "
1250 + "for host {}/{}", nextId, deviceId, macAddr, vlanId);
1251
1252 macVlanNextObjStore.put(key, nextId);
1253 return nextId;
1254 }
1255
1256 /**
Saurav Das4ce45962015-11-24 23:21:05 -08001257 * Create simple next objective for a single port. The treatments can include
1258 * all outgoing actions that need to happen on the packet.
1259 *
1260 * @param portNum the outgoing port on the device
1261 * @param treatment the actions to apply on the packets (should include outport)
1262 * @param meta optional data to pass to the driver
1263 */
1264 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1265 TrafficSelector meta) {
1266 int nextId = flowObjectiveService.allocateNextId();
1267 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das76ae6812017-03-15 15:15:14 -07001268 deviceId, portNum, treatment, meta);
Saurav Das4ce45962015-11-24 23:21:05 -08001269
1270 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1271 .builder().withId(nextId)
1272 .withType(NextObjective.Type.SIMPLE)
1273 .addTreatment(treatment)
1274 .fromApp(appId)
1275 .withMeta(meta);
1276
Saurav Das961beb22017-03-29 19:09:17 -07001277 ObjectiveContext context = new DefaultObjectiveContext(
1278 (objective) ->
1279 log.debug("createGroupFromPort installed "
1280 + "NextObj {} on {}", nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001281 (objective, error) -> {
1282 log.warn("createGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1283 srManager.invalidateNextObj(objective.id());
1284 });
Saurav Das961beb22017-03-29 19:09:17 -07001285 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das4ce45962015-11-24 23:21:05 -08001286 flowObjectiveService.next(deviceId, nextObj);
1287 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1288 + "for port {}", nextId, deviceId, portNum);
1289
1290 portNextObjStore.put(key, nextId);
1291 }
1292
sangho1e575652015-05-14 00:39:53 -07001293 /**
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001294 * Removes simple next objective for a single port.
1295 *
1296 * @param deviceId device id that has the port to deal with
1297 * @param portNum the outgoing port on the device
1298 * @param vlanId vlan id associated with the port
1299 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1300 */
1301 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1302 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1303 mbuilder.matchVlanId(vlanId);
1304
1305 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1306 tbuilder.immediate().setOutput(portNum);
1307 if (popVlan) {
1308 tbuilder.immediate().popVlan();
1309 }
1310
1311 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1312 tbuilder.build(), mbuilder.build(), false);
1313
1314 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1315 deviceId, portNum, tbuilder.build(), mbuilder.build());
1316 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1317 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1318 .builder().withId(portNextObjId)
1319 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1320 ObjectiveContext context = new DefaultObjectiveContext(
1321 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1322 portNextObjId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001323 (objective, error) -> {
1324 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1325 portNextObjId, deviceId, error);
1326 srManager.invalidateNextObj(objective.id());
1327 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001328 NextObjective nextObjective = nextObjBuilder.remove(context);
1329 log.info("**removePortNextObjective: Submitted "
1330 + "next objective {} in device {}",
1331 portNextObjId, deviceId);
1332 flowObjectiveService.next(deviceId, nextObjective);
1333
1334 portNextObjStore.remove(key);
1335 }
1336 }
Ruchi Sahota5d800282019-01-28 01:08:18 +00001337
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001338 /**
sangho1e575652015-05-14 00:39:53 -07001339 * Removes groups for the next objective ID given.
1340 *
1341 * @param objectiveId next objective ID to remove
1342 * @return true if succeeds, false otherwise
1343 */
1344 public boolean removeGroup(int objectiveId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001345 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1346 dsNextObjStore.entrySet()) {
1347 if (e.getValue().nextId() != objectiveId) {
1348 continue;
1349 }
Pier Luigi63edd932018-01-14 21:56:11 +01001350 // Right now it is just used in TunnelHandler
1351 // remember in future that PW transit groups could
1352 // be Indirect groups
sangho1e575652015-05-14 00:39:53 -07001353 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1354 .builder().withId(objectiveId)
1355 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -07001356 ObjectiveContext context = new DefaultObjectiveContext(
1357 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1358 objectiveId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001359 (objective, error) -> {
1360 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", objectiveId, deviceId, error);
1361 srManager.invalidateNextObj(objective.id());
1362 });
Charles Chan216e3c82016-04-23 14:48:16 -07001363 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -08001364 log.info("**removeGroup: Submited "
1365 + "next objective {} in device {}",
1366 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -07001367 flowObjectiveService.next(deviceId, nextObjective);
1368
Saurav Das7bcbe702017-06-13 15:35:54 -07001369 dsNextObjStore.remove(e.getKey());
sangho0b2b6d12015-05-20 22:16:38 -07001370 return true;
sangho1e575652015-05-14 00:39:53 -07001371 }
1372
1373 return false;
1374 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001375 /**
1376 * Remove simple next objective for a single port. The treatments can include
1377 * all outgoing actions that need to happen on the packet.
1378 *
1379 * @param portNum the outgoing port on the device
1380 * @param treatment the actions applied on the packets (should include outport)
1381 * @param meta optional data to pass to the driver
1382 */
1383 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1384 TrafficSelector meta) {
1385 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1386 deviceId, portNum, treatment, meta);
1387 Integer nextId = portNextObjStore.get(key);
1388
1389 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1390 .builder().withId(nextId)
1391 .withType(NextObjective.Type.SIMPLE)
1392 .addTreatment(treatment)
1393 .fromApp(appId)
1394 .withMeta(meta);
1395
1396 ObjectiveContext context = new DefaultObjectiveContext(
1397 (objective) ->
1398 log.info("removeGroupFromPort installed "
1399 + "NextObj {} on {}", nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001400 (objective, error) -> {
1401 log.warn("removeGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error);
1402 srManager.invalidateNextObj(objective.id());
1403 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001404 );
1405 NextObjective nextObj = nextObjBuilder.remove(context);
1406 flowObjectiveService.next(deviceId, nextObj);
1407 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1408 + "for port {}", nextId, deviceId, portNum);
1409
1410 portNextObjStore.remove(key);
1411 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001412
Charles Chane849c192016-01-11 18:28:54 -08001413 /**
1414 * Removes all groups from all next objective stores.
1415 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001416 /*public void removeAllGroups() {
1417 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das423fe2b2015-12-04 10:52:59 -08001418 nsNextObjStore.entrySet()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001419 removeGroup(entry.getValue().nextId());
Saurav Das423fe2b2015-12-04 10:52:59 -08001420 }
1421 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1422 portNextObjStore.entrySet()) {
1423 removeGroup(entry.getValue());
1424 }
Charles Chan59cc16d2017-02-02 16:20:42 -08001425 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1426 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001427 removeGroup(entry.getValue());
1428 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001429 }*/ //XXX revisit
1430
Saurav Dasceccf242017-08-03 18:30:35 -07001431 /**
1432 * Triggers a one time bucket verification operation on all hash groups
1433 * on this device.
1434 */
1435 public void triggerBucketCorrector() {
1436 BucketCorrector bc = new BucketCorrector();
1437 bc.run();
1438 }
1439
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001440 /**
1441 * Modifies L2IG bucket when the interface configuration is updated, especially
1442 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1443 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1444 * TrafficTreatment in turn.
1445 *
1446 * @param portNumber the port on this device that needs to be updated
1447 * @param vlanId the vlan id corresponding to this port
1448 * @param pushVlan indicates if packets should be sent out untagged or not out
1449 * from the port. If true, updated TrafficTreatment involves
1450 * pop vlan tag action. If false, updated TrafficTreatment
1451 * does not involve pop vlan tag action.
1452 */
1453 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
1454 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1455 if (pushVlan) {
1456 tBuilder.popVlan();
1457 }
1458 tBuilder.setOutput(portNumber);
1459
1460 TrafficSelector metadata =
1461 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1462
1463 int nextId = getVlanNextObjectiveId(vlanId);
1464
1465 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1466 .builder().withId(nextId)
1467 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1468 .addTreatment(tBuilder.build())
1469 .withMeta(metadata);
1470
1471 ObjectiveContext context = new DefaultObjectiveContext(
1472 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1473 portNumber, nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001474 (objective, error) -> {
1475 log.warn("port {} failed to updated NextObj {} on {}: {}", portNumber, nextId, deviceId, error);
1476 srManager.invalidateNextObj(objective.id());
1477 });
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001478
1479 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001480 }
1481
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001482 /**
Ruchi Sahota5d800282019-01-28 01:08:18 +00001483 * Returns the next ID for the given host port from the store.
1484 *
1485 * @param hostMac mac of host for which Next ID is required.
1486 * @param hostVlanId vlan of host for which Next ID is required.
1487 * @param port port of device for which next ID is required.
1488 * @return next objective ID or -1 if an error occurred during retrieval
1489 */
1490 public int getNextIdForHostPort(MacAddress hostMac, VlanId hostVlanId, PortNumber port) {
1491
1492 MacAddress deviceMac;
1493 try {
1494 deviceMac = deviceConfig.getDeviceMac(deviceId);
1495 } catch (DeviceConfigNotFoundException e) {
1496 log.warn(e.getMessage() + " in getNextIdForHostPort");
1497 return -1;
1498 }
1499
1500 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1501 tbuilder.deferred()
1502 .setEthDst(hostMac)
1503 .setEthSrc(deviceMac)
1504 .setVlanId(hostVlanId)
1505 .setOutput(port);
1506
1507 TrafficSelector metadata =
1508 DefaultTrafficSelector.builder().matchVlanId(hostVlanId).build();
1509
1510 int nextId = getMacVlanNextObjectiveId(hostMac, hostVlanId,
1511 tbuilder.build(), metadata, true);
1512
1513 log.debug("getNextId : hostMac {}, deviceMac {}, port {}, hostVlanId {} Treatment {} Meta {} nextId {} ",
1514 hostMac, deviceMac, port, hostVlanId, tbuilder.build(), metadata, nextId);
1515
1516 return nextId;
1517 }
1518
1519 /**
1520 * Updates the next objective for the given nextId .
1521 *
1522 * @param hostMac mac of host for which Next obj is to be updated.
1523 * @param hostVlanId vlan of host for which Next obj is to be updated.
1524 * @param port port with which to update the Next Obj.
1525 * @param nextId of Next Obj which needs to be updated.
1526 */
1527 public void updateL3UcastGroupBucket(MacAddress hostMac, VlanId hostVlanId, PortNumber port, int nextId) {
1528
1529 MacAddress deviceMac;
1530 try {
1531 deviceMac = deviceConfig.getDeviceMac(deviceId);
1532 } catch (DeviceConfigNotFoundException e) {
1533 log.warn(e.getMessage() + " in updateL3UcastGroupBucket");
1534 return;
1535 }
1536
1537 TrafficSelector metadata =
1538 DefaultTrafficSelector.builder().matchVlanId(hostVlanId).build();
1539
1540 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1541 tbuilder.deferred()
1542 .setEthDst(hostMac)
1543 .setEthSrc(deviceMac)
1544 .setVlanId(hostVlanId)
1545 .setOutput(port);
1546
1547 log.debug(" update L3Ucast : deviceMac {}, port {}, host {}/{}, nextid {}, Treatment {} Meta {}",
1548 deviceMac, port, hostMac, hostVlanId, nextId, tbuilder.build(), metadata);
1549
1550 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1551 .builder().withId(nextId)
1552 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1553 .addTreatment(tbuilder.build())
1554 .withMeta(metadata);
1555
1556 ObjectiveContext context = new DefaultObjectiveContext(
1557 (objective) -> log.debug(" NextId {} successfully updated host {} vlan {} with port {}",
1558 nextId, hostMac, hostVlanId, port),
1559 (objective, error) -> {
1560 log.warn(" NextId {} failed to update host {} vlan {} with port {}, error : {}",
1561 nextId, hostMac, hostVlanId, port, error);
1562 srManager.invalidateNextObj(objective.id());
1563 });
1564
1565 NextObjective nextObj = nextObjBuilder.modify(context);
1566 flowObjectiveService.next(deviceId, nextObj);
1567
1568 }
1569
1570 /**
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09001571 * Adds a single port to the L2FG or removes it from the L2FG.
1572 *
1573 * @param vlanId the vlan id corresponding to this port
1574 * @param portNum the port on this device to be updated
1575 * @param nextId the next objective ID for the given vlan id
1576 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1577 */
1578 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001579 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1580 if (toPopVlan(portNum, vlanId)) {
1581 tBuilder.popVlan();
1582 }
1583 tBuilder.setOutput(portNum);
1584
1585 TrafficSelector metadata =
1586 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1587
1588 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1589 .builder().withId(nextId)
1590 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1591 .addTreatment(tBuilder.build())
1592 .withMeta(metadata);
1593
1594 ObjectiveContext context = new DefaultObjectiveContext(
1595 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1596 portNum, nextId, deviceId),
Charles Chan1dbadbd2018-08-23 14:30:33 -07001597 (objective, error) -> {
1598 log.warn("port {} failed to removedFrom NextObj {} on {}: {}", portNum, nextId, deviceId, error);
1599 srManager.invalidateNextObj(objective.id());
1600 });
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001601
1602 if (install) {
1603 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1604 } else {
1605 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1606 }
1607 }
Saurav Das1547b3f2017-05-05 17:01:08 -07001608
1609 /**
Saurav Das9df5b7c2017-08-14 16:44:43 -07001610 * Performs bucket verification operation for all hash groups in this device.
1611 * Checks RouteHandler to ensure that routing is stable before attempting
1612 * verification. Verification involves creating a nextObjective with
1613 * operation VERIFY for existing next objectives in the store, and passing
1614 * it to the driver. It is the driver that actually performs the verification
1615 * by adding or removing buckets to match the verification next objective
1616 * created here.
Saurav Das1547b3f2017-05-05 17:01:08 -07001617 */
Saurav Dasceccf242017-08-03 18:30:35 -07001618 protected final class BucketCorrector implements Runnable {
1619 Integer nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001620
Saurav Dasceccf242017-08-03 18:30:35 -07001621 BucketCorrector() {
1622 this.nextId = null;
1623 }
1624
1625 BucketCorrector(Integer nextId) {
1626 this.nextId = nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001627 }
1628
1629 @Override
1630 public void run() {
Saurav Dasceccf242017-08-03 18:30:35 -07001631 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1632 return;
Saurav Das1547b3f2017-05-05 17:01:08 -07001633 }
Saurav Dasceccf242017-08-03 18:30:35 -07001634 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1635 if (rh == null) {
1636 return;
1637 }
1638 if (!rh.isRoutingStable()) {
1639 return;
1640 }
1641 rh.acquireRoutingLock();
1642 try {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001643 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07001644 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1645 .stream()
1646 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigi63edd932018-01-14 21:56:11 +01001647 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Dasa4020382018-02-14 14:14:54 -08001648 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1649 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1650 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1651 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1652 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasceccf242017-08-03 18:30:35 -07001653 .map(entry -> entry.getKey())
1654 .collect(Collectors.toSet());
1655 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1656 NextNeighbors next = dsNextObjStore.get(dsKey);
1657 if (next == null) {
1658 continue;
1659 }
1660 int nid = next.nextId();
1661 if (nextId != null && nextId != nid) {
1662 continue;
1663 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001664 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasceccf242017-08-03 18:30:35 -07001665 deviceId, dsKey, next);
1666 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
Saurav Das3c82f192018-08-13 15:34:26 -07001667 metabuilder.matchVlanId(srManager.getDefaultInternalVlan());
Saurav Dasceccf242017-08-03 18:30:35 -07001668 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1669 .withId(nid)
1670 .withType(NextObjective.Type.HASHED)
1671 .withMeta(metabuilder.build())
1672 .fromApp(appId);
1673
1674 next.dstNextHops().forEach((dstDev, nextHops) -> {
1675 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1676 nextHops.forEach(neighbor -> {
1677 MacAddress neighborMac;
1678 try {
1679 neighborMac = deviceConfig.getDeviceMac(neighbor);
1680 } catch (DeviceConfigNotFoundException e) {
1681 log.warn(e.getMessage() + " Aborting neighbor"
1682 + neighbor);
1683 return;
1684 }
1685 devicePortMap.get(neighbor).forEach(port -> {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001686 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasceccf242017-08-03 18:30:35 -07001687 + " port/label {}/{} to dst {} via {}",
1688 deviceId, nid, port, edgeLabel,
1689 dstDev, neighbor);
Saurav Dasa4020382018-02-14 14:14:54 -08001690 nextObjBuilder
1691 .addTreatment(treatmentBuilder(port,
1692 neighborMac,
1693 dsKey.destinationSet().swap(),
1694 edgeLabel));
Saurav Dasceccf242017-08-03 18:30:35 -07001695 });
1696 });
1697 });
1698
1699 NextObjective nextObjective = nextObjBuilder.verify();
1700 flowObjectiveService.next(deviceId, nextObjective);
1701 }
1702 } finally {
1703 rh.releaseRoutingLock();
1704 }
1705
1706 }
1707
1708 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
Saurav Dasa4020382018-02-14 14:14:54 -08001709 boolean swap, int edgeLabel) {
Saurav Dasceccf242017-08-03 18:30:35 -07001710 TrafficTreatment.Builder tBuilder =
1711 DefaultTrafficTreatment.builder();
1712 tBuilder.setOutput(outport)
1713 .setEthDst(dstMac)
1714 .setEthSrc(nodeMacAddr);
1715 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Dasa4020382018-02-14 14:14:54 -08001716 if (swap) {
1717 // swap label case
1718 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1719 } else {
1720 // ecmp with label push case
1721 tBuilder.pushMpls()
1722 .copyTtlOut()
1723 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1724 }
Saurav Dasceccf242017-08-03 18:30:35 -07001725 }
1726 return tBuilder.build();
Saurav Das1547b3f2017-05-05 17:01:08 -07001727 }
1728 }
1729
Pier Luigi63edd932018-01-14 21:56:11 +01001730}