blob: 1e0e6fd66b1f00c631aa46cec54a0b670057e1d0 [file] [log] [blame]
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting.grouphandler;
17
Saurav Dasfe0b05e2017-08-14 16:44:43 -070018import com.google.common.collect.ImmutableMap;
Pier Ventre229fd0b2016-10-31 16:49:19 -070019import com.google.common.collect.Iterables;
Saurav Dasfe0b05e2017-08-14 16:44:43 -070020import com.google.common.collect.Lists;
Saurav Das62ae6792017-05-15 15:34:25 -070021import com.google.common.collect.Sets;
22
Pier Ventre229fd0b2016-10-31 16:49:19 -070023import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070024import org.onlab.packet.MacAddress;
25import org.onlab.packet.MplsLabel;
Saurav Das62af8802015-12-04 10:52:59 -080026import org.onlab.packet.VlanId;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070027import org.onlab.util.KryoNamespace;
28import org.onosproject.core.ApplicationId;
Charles Chan10b0fb72017-02-02 16:20:42 -080029import org.onosproject.net.ConnectPoint;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
Saurav Das62af8802015-12-04 10:52:59 -080033import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070034import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das4c35fc42015-11-20 15:27:53 -080035import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070036import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chana4ee4f92016-04-23 14:48:16 -070038import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070039import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070042import org.onosproject.net.link.LinkService;
Saurav Dasfbe74572017-08-03 18:30:35 -070043import org.onosproject.segmentrouting.DefaultRoutingHandler;
Saurav Das62af8802015-12-04 10:52:59 -080044import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan319d1a22015-11-03 10:42:14 -080045import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
46import org.onosproject.segmentrouting.config.DeviceProperties;
Saurav Das261c3002017-06-13 15:35:54 -070047import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070048import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080049import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070050import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070051import org.slf4j.Logger;
52
Pier Ventre229fd0b2016-10-31 16:49:19 -070053import java.net.URI;
Charles Chan90772a72017-02-08 15:52:08 -080054import java.util.Collection;
Pier Ventre229fd0b2016-10-31 16:49:19 -070055import java.util.Collections;
Saurav Das62ae6792017-05-15 15:34:25 -070056import java.util.HashMap;
Pier Ventre229fd0b2016-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 Das8a3022d2017-05-05 17:01:08 -070062import java.util.concurrent.ScheduledExecutorService;
63import java.util.concurrent.TimeUnit;
Pier Ventre229fd0b2016-10-31 16:49:19 -070064import java.util.stream.Collectors;
65
66import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das8a3022d2017-05-05 17:01:08 -070067import static java.util.concurrent.Executors.newScheduledThreadPool;
68import static org.onlab.util.Tools.groupedThreads;
Charles Chan10b0fb72017-02-02 16:20:42 -080069import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
Pier Ventre229fd0b2016-10-31 16:49:19 -070070import static org.slf4j.LoggerFactory.getLogger;
71
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070072/**
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070073 * Default ECMP group handler creation module. This component creates a set of
74 * ECMP groups for every neighbor that this device is connected to based on
75 * whether the current device is an edge device or a transit device.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070076 */
77public class DefaultGroupHandler {
Ray Milkey5247fa92018-01-12 14:22:06 -080078 private static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070079
Saurav Dasfbe74572017-08-03 18:30:35 -070080 private static final long VERIFY_INTERVAL = 30; // secs
81
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070082 protected final DeviceId deviceId;
83 protected final ApplicationId appId;
84 protected final DeviceProperties deviceConfig;
85 protected final List<Integer> allSegmentIds;
Pier Ventreadb4ae62016-11-23 09:57:42 -080086 protected int ipv4NodeSegmentId = -1;
87 protected int ipv6NodeSegmentId = -1;
Charles Chan319d1a22015-11-03 10:42:14 -080088 protected boolean isEdgeRouter = false;
89 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070090 protected LinkService linkService;
Srikanth Vavilapalli64505482015-04-21 13:04:13 -070091 protected FlowObjectiveService flowObjectiveService;
Saurav Das62ae6792017-05-15 15:34:25 -070092 /**
93 * local store for neighbor-device-ids and the set of ports on this device
94 * that connect to the same neighbor.
95 */
Saurav Das4c35fc42015-11-20 15:27:53 -080096 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
97 new ConcurrentHashMap<>();
Saurav Das62ae6792017-05-15 15:34:25 -070098 /**
99 * local store for ports on this device connected to neighbor-device-id.
100 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800101 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
102 new ConcurrentHashMap<>();
Saurav Das62ae6792017-05-15 15:34:25 -0700103
Saurav Das261c3002017-06-13 15:35:54 -0700104 // distributed store for (device+destination-set) mapped to next-id and neighbors
105 protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
106 dsNextObjStore = null;
Saurav Dasf0f592d2016-11-18 15:21:57 -0800107 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan10b0fb72017-02-02 16:20:42 -0800108 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
109 vlanNextObjStore = null;
Saurav Dasf0f592d2016-11-18 15:21:57 -0800110 // distributed store for (device+port+treatment) mapped to next-id
Charles Chanb7f75ac2016-01-11 18:28:54 -0800111 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
112 portNextObjStore = null;
Charles Chande6655c2015-12-23 00:15:11 -0800113 private SegmentRoutingManager srManager;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700114
Saurav Das8a3022d2017-05-05 17:01:08 -0700115 private ScheduledExecutorService executorService
Saurav Dasfbe74572017-08-03 18:30:35 -0700116 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das8a3022d2017-05-05 17:01:08 -0700117
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700118 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700119 .register(URI.class).register(HashSet.class)
Saurav Das261c3002017-06-13 15:35:54 -0700120 .register(DeviceId.class).register(PortNumber.class)
121 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700122 .register(PolicyGroupParams.class)
123 .register(GroupBucketIdentifier.class)
124 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700125
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700126 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
127 DeviceProperties config,
128 LinkService linkService,
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700129 FlowObjectiveService flowObjService,
Charles Chande6655c2015-12-23 00:15:11 -0800130 SegmentRoutingManager srManager) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700131 this.deviceId = checkNotNull(deviceId);
132 this.appId = checkNotNull(appId);
133 this.deviceConfig = checkNotNull(config);
134 this.linkService = checkNotNull(linkService);
Charles Chan319d1a22015-11-03 10:42:14 -0800135 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
136 try {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800137 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
138 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan319d1a22015-11-03 10:42:14 -0800139 this.isEdgeRouter = config.isEdgeDevice(deviceId);
140 this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId));
141 } catch (DeviceConfigNotFoundException e) {
142 log.warn(e.getMessage()
143 + " Skipping value assignment in DefaultGroupHandler");
144 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700145 this.flowObjectiveService = flowObjService;
Saurav Das261c3002017-06-13 15:35:54 -0700146 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeyb85de082017-04-05 09:42:04 -0700147 this.vlanNextObjStore = srManager.vlanNextObjStore();
148 this.portNextObjStore = srManager.portNextObjStore();
Charles Chande6655c2015-12-23 00:15:11 -0800149 this.srManager = srManager;
Saurav Dasfbe74572017-08-03 18:30:35 -0700150 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
151 VERIFY_INTERVAL,
152 TimeUnit.SECONDS);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700153 populateNeighborMaps();
154 }
155
156 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700157 * Gracefully shuts down a groupHandler. Typically called when the handler is
158 * no longer needed.
159 */
160 public void shutdown() {
161 executorService.shutdown();
162 }
163
164 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700165 * Creates a group handler object.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700166 *
167 * @param deviceId device identifier
168 * @param appId application identifier
169 * @param config interface to retrieve the device properties
170 * @param linkService link service object
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700171 * @param flowObjService flow objective service object
Charles Chanb7f75ac2016-01-11 18:28:54 -0800172 * @param srManager segment routing manager
Charles Chan319d1a22015-11-03 10:42:14 -0800173 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700174 * @return default group handler type
175 */
Saurav Das2d94d312015-11-24 23:21:05 -0800176 public static DefaultGroupHandler createGroupHandler(
Saurav Dasfbe74572017-08-03 18:30:35 -0700177 DeviceId deviceId,
178 ApplicationId appId,
179 DeviceProperties config,
180 LinkService linkService,
181 FlowObjectiveService flowObjService,
182 SegmentRoutingManager srManager)
183 throws DeviceConfigNotFoundException {
Saurav Das62ae6792017-05-15 15:34:25 -0700184 return new DefaultGroupHandler(deviceId, appId, config,
185 linkService,
186 flowObjService,
187 srManager);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700188 }
189
190 /**
Saurav Das97241862018-02-14 14:14:54 -0800191 * Updates local stores for link-src-device/port to neighbor (link-dst) for
192 * link that has come up.
Saurav Das62ae6792017-05-15 15:34:25 -0700193 *
194 * @param link the infrastructure link
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700195 */
Saurav Das62ae6792017-05-15 15:34:25 -0700196 public void portUpForLink(Link link) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700197 if (!link.src().deviceId().equals(deviceId)) {
198 log.warn("linkUp: deviceId{} doesn't match with link src {}",
199 deviceId, link.src().deviceId());
200 return;
201 }
Saurav Das62ae6792017-05-15 15:34:25 -0700202
Saurav Dasfbe74572017-08-03 18:30:35 -0700203 log.info("* portUpForLink: Device {} linkUp at local port {} to "
204 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
205 // ensure local state is updated even if linkup is aborted later on
206 addNeighborAtPort(link.dst().deviceId(),
207 link.src().port());
208 }
Saurav Das62ae6792017-05-15 15:34:25 -0700209
Saurav Dasfbe74572017-08-03 18:30:35 -0700210 /**
Saurav Das97241862018-02-14 14:14:54 -0800211 * Updates local stores for link-src-device/port to neighbor (link-dst) for
212 * link that has gone down.
Saurav Dasfbe74572017-08-03 18:30:35 -0700213 *
Saurav Das97241862018-02-14 14:14:54 -0800214 * @param link the infrastructure link
Saurav Dasfbe74572017-08-03 18:30:35 -0700215 */
Saurav Das97241862018-02-14 14:14:54 -0800216 public void portDownForLink(Link link) {
217 PortNumber port = link.src().port();
Saurav Dasfbe74572017-08-03 18:30:35 -0700218 if (portDeviceMap.get(port) == null) {
219 log.warn("portDown: unknown port");
220 return;
221 }
Saurav Das62ae6792017-05-15 15:34:25 -0700222
Saurav Dasfbe74572017-08-03 18:30:35 -0700223 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
224 portDeviceMap.get(port));
225 devicePortMap.get(portDeviceMap.get(port)).remove(port);
226 portDeviceMap.remove(port);
227 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700228
229 /**
Saurav Das97241862018-02-14 14:14:54 -0800230 * Cleans up local stores for removed neighbor device.
231 *
232 * @param neighborId the device identifier for the neighbor device
233 */
234 public void cleanUpForNeighborDown(DeviceId neighborId) {
235 Set<PortNumber> ports = devicePortMap.remove(neighborId);
236 if (ports != null) {
237 ports.forEach(p -> portDeviceMap.remove(p));
238 }
239 }
240
241 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700242 * Checks all groups in the src-device of link for neighbor sets that include
243 * the dst-device of link, and edits the hash groups according to link up
244 * or down. Should only be called by the master instance of the src-switch
245 * of link. Typically used when there are no route-path changes due to the
246 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700247 *
Saurav Das62ae6792017-05-15 15:34:25 -0700248 * @param link the infrastructure link that has gone down or come up
249 * @param linkDown true if link has gone down
250 * @param firstTime true if link has come up for the first time i.e a link
251 * not seen-before
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700252 */
Saurav Das62ae6792017-05-15 15:34:25 -0700253 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700254 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -0800255 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700256 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan319d1a22015-11-03 10:42:14 -0800257 } catch (DeviceConfigNotFoundException e) {
Saurav Das62ae6792017-05-15 15:34:25 -0700258 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan319d1a22015-11-03 10:42:14 -0800259 return;
260 }
Saurav Das261c3002017-06-13 15:35:54 -0700261 // find all the destinationSets related to link
262 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700263 .stream()
Saurav Das261c3002017-06-13 15:35:54 -0700264 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigiecb09f42018-01-14 21:56:11 +0100265 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Das97241862018-02-14 14:14:54 -0800266 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
267 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
268 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
269 .filter(entry -> !entry.getKey().destinationSet().swap() ||
270 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Das261c3002017-06-13 15:35:54 -0700271 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
272 .map(entry -> entry.getKey())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700273 .collect(Collectors.toSet());
Saurav Das62ae6792017-05-15 15:34:25 -0700274
Saurav Das261c3002017-06-13 15:35:54 -0700275 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
276 deviceId, link.dst().deviceId(), dsKeySet);
277
278 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
279 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
280 if (nextHops == null) {
Saurav Das62ae6792017-05-15 15:34:25 -0700281 log.warn("retryHash in device {}, but global store has no record "
Saurav Das261c3002017-06-13 15:35:54 -0700282 + "for dsKey:{}", deviceId, dsKey);
Saurav Das62ae6792017-05-15 15:34:25 -0700283 continue;
284 }
Saurav Das261c3002017-06-13 15:35:54 -0700285 int nextId = nextHops.nextId();
286 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -0700287 if (!linkDown) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700288 List<PortLabel> pl = Lists.newArrayList();
Saurav Das62ae6792017-05-15 15:34:25 -0700289 if (firstTime) {
290 // some links may have come up before the next-objective was created
291 // we take this opportunity to ensure other ports to same next-hop-dst
292 // are part of the hash group (see CORD-1180). Duplicate additions
293 // to the same hash group are avoided by the driver.
294 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -0700295 dstSet.forEach(dst -> {
296 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700297 pl.add(new PortLabel(p, edgeLabel));
Saurav Das261c3002017-06-13 15:35:54 -0700298 });
Saurav Das62ae6792017-05-15 15:34:25 -0700299 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700300 addToHashedNextObjective(pl, neighborMac, nextId);
301 } else {
302 // handle only the port that came up
303 dstSet.forEach(dst -> {
304 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
305 pl.add(new PortLabel(link.src().port(), edgeLabel));
306 });
307 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das62ae6792017-05-15 15:34:25 -0700308 }
309 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700310 // linkdown
311 List<PortLabel> pl = Lists.newArrayList();
Saurav Das261c3002017-06-13 15:35:54 -0700312 dstSet.forEach(dst -> {
313 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700314 pl.add(new PortLabel(link.src().port(), edgeLabel));
Saurav Das261c3002017-06-13 15:35:54 -0700315 });
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700316 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700317 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700318 }
Saurav Das8a3022d2017-05-05 17:01:08 -0700319 }
320
Saurav Das62ae6792017-05-15 15:34:25 -0700321 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700322 * Utility class for associating output ports and the corresponding MPLS
323 * labels to push. In dual-homing, there are different labels to push
324 * corresponding to the destination switches in an edge-pair. If both
325 * destinations are reachable via the same spine, then the output-port to
326 * the spine will be associated with two labels i.e. there will be two
327 * PortLabel objects for the same port but with different labels.
328 */
329 private class PortLabel {
330 PortNumber port;
331 int edgeLabel;
332
333 PortLabel(PortNumber port, int edgeLabel) {
334 this.port = port;
335 this.edgeLabel = edgeLabel;
336 }
337
338 @Override
339 public String toString() {
340 return port.toString() + "/" + String.valueOf(edgeLabel);
341 }
342 }
343
344 /**
345 * Makes a call to the FlowObjective service to add buckets to
346 * a hashed group. User must ensure that all the ports & labels are meant
347 * same neighbor (ie. dstMac).
Saurav Das62ae6792017-05-15 15:34:25 -0700348 *
Pier Luigiecb09f42018-01-14 21:56:11 +0100349 * @param portLabels a collection of port & label combinations to add
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700350 * to the hash group identified by the nextId
Saurav Das62ae6792017-05-15 15:34:25 -0700351 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700352 * @param nextId id for next-objective to which buckets will be added
Saurav Dasfbe74572017-08-03 18:30:35 -0700353 *
Saurav Das62ae6792017-05-15 15:34:25 -0700354 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700355 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
356 MacAddress dstMac, Integer nextId) {
Saurav Das8a3022d2017-05-05 17:01:08 -0700357 // setup metadata to pass to nextObjective - indicate the vlan on egress
358 // if needed by the switch pipeline. Since hashed next-hops are always to
359 // other neighboring routers, there is no subnet assigned on those ports.
360 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
361 metabuilder.matchVlanId(INTERNAL_VLAN);
Saurav Das8a3022d2017-05-05 17:01:08 -0700362 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
363 .withId(nextId)
364 .withType(NextObjective.Type.HASHED)
Saurav Das8a3022d2017-05-05 17:01:08 -0700365 .withMeta(metabuilder.build())
366 .fromApp(appId);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700367 // Create the new buckets to be updated
368 portLabels.forEach(pl -> {
369 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
370 tBuilder.setOutput(pl.port)
371 .setEthDst(dstMac)
372 .setEthSrc(nodeMacAddr);
373 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
374 tBuilder.pushMpls()
375 .copyTtlOut()
376 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
377 }
378 nextObjBuilder.addTreatment(tBuilder.build());
379 });
380
381 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
382 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das8a3022d2017-05-05 17:01:08 -0700383
384 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700385 (objective) -> log.debug("addToHash port/label {} addedTo "
386 + "NextObj {} on {}", portLabels, nextId, deviceId),
Saurav Das8a3022d2017-05-05 17:01:08 -0700387 (objective, error) ->
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700388 log.warn("addToHash failed to add port/label {} to"
389 + " NextObj {} on {}: {}", portLabels,
Saurav Das62ae6792017-05-15 15:34:25 -0700390 nextId, deviceId, error));
Saurav Das8a3022d2017-05-05 17:01:08 -0700391 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
392 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700393 }
394
395 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700396 * Makes a call to the FlowObjective service to remove buckets from
397 * a hash group. User must ensure that all the ports & labels are meant
398 * same neighbor (ie. dstMac).
Saurav Dasfbe74572017-08-03 18:30:35 -0700399 *
Pier Luigiecb09f42018-01-14 21:56:11 +0100400 * @param portLabels a collection of port & label combinations to remove
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700401 * from the hash group identified by the nextId
Saurav Dasfbe74572017-08-03 18:30:35 -0700402 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700403 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasfbe74572017-08-03 18:30:35 -0700404 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700405 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
406 MacAddress dstMac, Integer nextId) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700407 NextObjective.Builder nextObjBuilder = DefaultNextObjective
408 .builder()
409 .withType(NextObjective.Type.HASHED) //same as original
410 .withId(nextId)
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700411 .fromApp(appId);
412 // Create the buckets to be removed
413 portLabels.forEach(pl -> {
414 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
415 tBuilder.setOutput(pl.port)
416 .setEthDst(dstMac)
417 .setEthSrc(nodeMacAddr);
418 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
419 tBuilder.pushMpls()
420 .copyTtlOut()
421 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
422 }
423 nextObjBuilder.addTreatment(tBuilder.build());
424 });
425 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
426 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Das62ae6792017-05-15 15:34:25 -0700427
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700428 ObjectiveContext context = new DefaultObjectiveContext(
429 (objective) -> log.debug("port/label {} removedFrom NextObj"
430 + " {} on {}", portLabels, nextId, deviceId),
431 (objective, error) ->
432 log.warn("port/label {} failed to removeFrom NextObj {} on "
433 + "{}: {}", portLabels, nextId, deviceId, error));
434 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasfbe74572017-08-03 18:30:35 -0700435 flowObjectiveService.next(deviceId, nextObjective);
436 }
Saurav Das62ae6792017-05-15 15:34:25 -0700437
438 /**
439 * Checks all the hash-groups in the target-switch meant for the destination
440 * switch, and either adds or removes buckets to make the neighbor-set
441 * match the given next-hops. Typically called by the master instance of the
442 * destination switch, which may be different from the master instance of the
443 * target switch where hash-group changes are made.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700444 *
Saurav Das62ae6792017-05-15 15:34:25 -0700445 * @param targetSw the switch in which the hash groups will be edited
446 * @param nextHops the current next hops for the target switch to reach
447 * the dest sw
448 * @param destSw the destination switch
449 * @param revoke true if hash groups need to remove buckets from the
450 * the groups to match the current next hops
451 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700452 */
Saurav Das62ae6792017-05-15 15:34:25 -0700453 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
454 DeviceId destSw, boolean revoke) {
455 // temporary storage of keys to be updated
Saurav Das261c3002017-06-13 15:35:54 -0700456 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Das62ae6792017-05-15 15:34:25 -0700457 new HashMap<>();
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700458 boolean foundNextObjective = false, success = true;
Charles Chan319d1a22015-11-03 10:42:14 -0800459
Saurav Das261c3002017-06-13 15:35:54 -0700460 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Das62ae6792017-05-15 15:34:25 -0700461 // with different neighbors than the given next-hops
Saurav Das261c3002017-06-13 15:35:54 -0700462 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
463 if (!dskey.deviceId().equals(targetSw) ||
464 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Das62ae6792017-05-15 15:34:25 -0700465 continue;
466 }
467 foundNextObjective = true;
Saurav Das261c3002017-06-13 15:35:54 -0700468 NextNeighbors nhops = dsNextObjStore.get(dskey);
469 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
470 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
471 Integer nextId = nhops.nextId();
Charles Chan319d1a22015-11-03 10:42:14 -0800472
Saurav Das97241862018-02-14 14:14:54 -0800473 // some store elements may not be hashed next-objectives - ignore them
474 if (isSimpleNextObjective(dskey)) {
475 log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}"
476 + " -> dstSw:{} with current nextHops:{} to new"
477 + " nextHops: {} in nextId:{}",
478 (revoke) ? "removal" : "addition", targetSw, destSw,
479 currNeighbors, nextHops, nextId);
480 if ((revoke && !nextHops.isEmpty())
481 || (!revoke && !nextHops.equals(currNeighbors))) {
482 log.warn("Simple next objective cannot be edited to "
483 + "move from {} to {}", currNeighbors, nextHops);
484 }
485 continue;
486 }
487
Saurav Das50375612017-12-13 16:19:35 -0800488 if (currNeighbors == null || nextHops == null) {
489 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
490 + " in targetSw:{} for dstSw:{}", currNeighbors, nextHops,
491 targetSw, destSw);
492 success &= false;
493 continue;
494 }
495
Saurav Das62ae6792017-05-15 15:34:25 -0700496 Set<DeviceId> diff;
497 if (revoke) {
498 diff = Sets.difference(currNeighbors, nextHops);
499 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
500 + "hops:{} ..removing {}", targetSw, destSw, nextId,
501 currNeighbors, diff);
502 } else {
503 diff = Sets.difference(nextHops, currNeighbors);
504 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
505 + "hops:{} ..adding {}", targetSw, destSw, nextId,
506 currNeighbors, diff);
507 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700508 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId,
509 revoke);
510 if (suc) {
511 // to update neighbor set with changes made
Saurav Das62ae6792017-05-15 15:34:25 -0700512 if (revoke) {
Saurav Das261c3002017-06-13 15:35:54 -0700513 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700514 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700515 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700516 }
sangho2165d222015-05-01 09:38:25 -0700517 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700518 success &= suc;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700519 }
520
Saurav Das62ae6792017-05-15 15:34:25 -0700521 if (!foundNextObjective) {
522 log.debug("Cannot find any nextObjectives for route targetSw:{} "
523 + "-> dstSw:{}", targetSw, destSw);
524 return true; // nothing to do, return true so ECMPspg is updated
525 }
526
Saurav Das261c3002017-06-13 15:35:54 -0700527 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
528 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700529 NextNeighbors currentNextHops = dsNextObjStore.get(key);
530 if (currentNextHops == null) {
531 log.warn("fixHashGroups could not update global store in "
532 + "device {} .. missing nextNeighbors for key {}",
533 deviceId, key);
Saurav Das62ae6792017-05-15 15:34:25 -0700534 continue;
535 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700536 Set<DeviceId> newNeighbors = new HashSet<>();
537 newNeighbors.addAll(tempStore.get(key));
538 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
539 ImmutableMap.copyOf(currentNextHops.dstNextHops());
540 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
541 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
542 targetSw, destSw, key, currentNextHops.nextId());
543 log.debug("Old dstNextHops: {}", oldDstNextHops);
544 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
545 // update global store
546 dsNextObjStore.put(key,
547 new NextNeighbors(currentNextHops.dstNextHops(),
548 currentNextHops.nextId()));
Saurav Das62ae6792017-05-15 15:34:25 -0700549 }
Saurav Das97241862018-02-14 14:14:54 -0800550
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700551 // even if one fails and others succeed, return false so ECMPspg not updated
552 return success;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700553 }
554
Saurav Dasfbe74572017-08-03 18:30:35 -0700555 /**
556 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
557 * that are not already in the store for the given DestinationSet. Note that
558 * this method does not remove existing next hops for the destinations in the
559 * DestinationSet.
560 *
561 * @param ds the DestinationSet for which the next hops need to be updated
562 * @param newDstNextHops a map of per-destination next hops to update the
563 * destinationSet with
564 * @return true if successful in updating all next hops
565 */
566 private boolean updateNextHops(DestinationSet ds,
Saurav Das261c3002017-06-13 15:35:54 -0700567 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
568 DestinationSetNextObjectiveStoreKey key =
569 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
570 NextNeighbors currNext = dsNextObjStore.get(key);
571 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
572
573 // add newDstNextHops to currDstNextHops for each dst
574 boolean success = true;
575 for (DeviceId dstSw : ds.getDestinationSwitches()) {
576 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
577 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
578 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
579 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
580 int edgeLabel = ds.getEdgeLabel(dstSw);
581 int nextId = currNext.nextId();
582
583 // new next hops should be added
584 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
585 edgeLabel, nextId, false);
586 if (suc) {
587 currNhops.addAll(newNhops);
588 currDstNextHops.put(dstSw, currNhops); // this is only a local change
589 }
590 success &= suc;
591 }
592
593 if (success) {
594 // update global store
595 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
596 currNext.nextId()));
597 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
598 dsNextObjStore.get(key));
599 }
600 return success;
601 }
602
Saurav Dasfbe74572017-08-03 18:30:35 -0700603 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700604 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
605 * needs to ensure that the given neighbors are all next hops towards the
606 * same destination (represented by the given edgeLabel).
Saurav Dasfbe74572017-08-03 18:30:35 -0700607 *
608 * @param neighbors set of neighbor device ids
609 * @param edgeLabel MPLS label to use in buckets
610 * @param nextId the nextObjective to change
611 * @param revoke true if buckets need to be removed, false if they need to
612 * be added
613 * @return true if successful in adding or removing buckets for all ports
614 * to the neighbors
615 */
616 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Saurav Das261c3002017-06-13 15:35:54 -0700617 int nextId, boolean revoke) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700618 for (DeviceId neighbor : neighbors) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700619 MacAddress neighborMac;
Saurav Das261c3002017-06-13 15:35:54 -0700620 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700621 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das261c3002017-06-13 15:35:54 -0700622 } catch (DeviceConfigNotFoundException e) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700623 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
624 + " for nextId:" + nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700625 return false;
626 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700627 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
628 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das261c3002017-06-13 15:35:54 -0700629 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700630 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das261c3002017-06-13 15:35:54 -0700631 deviceId, neighbor, nextId);
632 return false;
633 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700634 List<PortLabel> pl = Lists.newArrayList();
635 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel)));
Saurav Das261c3002017-06-13 15:35:54 -0700636 if (revoke) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700637 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
638 + "with Port/Label:{} to next object id {}",
639 deviceId, pl, nextId);
640 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700641 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700642 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
643 + "with Port/Label: {} to next object id {}",
644 deviceId, pl, nextId);
645 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700646 }
647 }
648 return true;
649 }
650
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700651 /**
Saurav Das97241862018-02-14 14:14:54 -0800652 * Returns true if the destination set is meant for swap or multi-labeled
653 * packet transport, and MPLS ECMP is not supported.
654 *
655 * @param dskey the key representing the destination set
656 * @return true if destination set is meant for simple next objectives
657 */
658 boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) {
659 return (dskey.destinationSet().notBos() || dskey.destinationSet().swap())
660 && !srManager.getMplsEcmp();
661 }
662
663 /**
Saurav Das3fb28272017-03-04 16:08:47 -0800664 * Adds or removes a port that has been configured with a vlan to a broadcast group
665 * for bridging. Should only be called by the master instance for this device.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800666 *
667 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Das3fb28272017-03-04 16:08:47 -0800668 * @param vlanId the vlan id corresponding to the broadcast domain/group
669 * @param popVlan indicates if packets should be sent out untagged or not out
670 * of the port. If true, indicates an access (untagged) or native vlan
671 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800672 * @param portUp true if port is enabled, false if disabled
Saurav Dasf0f592d2016-11-18 15:21:57 -0800673 */
Saurav Das3fb28272017-03-04 16:08:47 -0800674 public void processEdgePort(PortNumber port, VlanId vlanId,
675 boolean popVlan, boolean portUp) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800676 //get the next id for the subnet and edit it.
Charles Chan10b0fb72017-02-02 16:20:42 -0800677 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800678 if (nextId == -1) {
679 if (portUp) {
680 log.debug("**Creating flooding group for first port enabled in"
Saurav Das2b6a00f2017-12-05 15:00:23 -0800681 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan10b0fb72017-02-02 16:20:42 -0800682 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Dasf0f592d2016-11-18 15:21:57 -0800683 } else {
684 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan10b0fb72017-02-02 16:20:42 -0800685 + " removing port:{}", vlanId, deviceId, port);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800686 }
687 return;
688 }
689
690 log.info("**port{} in device {}: {} Bucket with Port {} to"
691 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
692 (portUp) ? "Adding" : "Removing",
693 port, nextId);
694 // Create the bucket to be added or removed
695 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Das3fb28272017-03-04 16:08:47 -0800696 if (popVlan) {
697 tBuilder.popVlan();
698 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800699 tBuilder.setOutput(port);
700
Saurav Dasf0f592d2016-11-18 15:21:57 -0800701 TrafficSelector metadata =
Saurav Das3fb28272017-03-04 16:08:47 -0800702 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Dasf0f592d2016-11-18 15:21:57 -0800703
704 NextObjective.Builder nextObjBuilder = DefaultNextObjective
705 .builder().withId(nextId)
706 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
707 .addTreatment(tBuilder.build())
708 .withMeta(metadata);
709
710 ObjectiveContext context = new DefaultObjectiveContext(
711 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
712 port, (portUp) ? "addedTo" : "removedFrom",
713 nextId, deviceId),
714 (objective, error) ->
715 log.warn("port {} failed to {} NextObj {} on {}: {}",
716 port, (portUp) ? "addTo" : "removeFrom",
717 nextId, deviceId, error));
718
719 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
720 : nextObjBuilder.removeFromExisting(context);
721 log.debug("edgePort processed: Submited next objective {} in device {}",
722 nextId, deviceId);
723 flowObjectiveService.next(deviceId, nextObj);
724 }
725
726 /**
Saurav Das97241862018-02-14 14:14:54 -0800727 * Returns the next objective of type hashed (or simple) associated with the
728 * destination set. In addition, updates the existing next-objective if new
729 * route-paths found have resulted in the addition of new next-hops to a
730 * particular destination. If there is no existing next objective for this
731 * destination set, this method would create a next objective and return the
732 * nextId. Optionally metadata can be passed in for the creation of the next
733 * objective. If the parameter simple is true then a simple next objective
734 * is created instead of a hashed one.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700735 *
Saurav Das261c3002017-06-13 15:35:54 -0700736 * @param ds destination set
737 * @param nextHops a map of per destination next hops
Saurav Das4c35fc42015-11-20 15:27:53 -0800738 * @param meta metadata passed into the creation of a Next Objective
Saurav Das97241862018-02-14 14:14:54 -0800739 * @param simple if true, a simple next objective will be created instead of
740 * a hashed next objective
Saurav Das4c35fc42015-11-20 15:27:53 -0800741 * @return int if found or -1 if there are errors in the creation of the
Saurav Das97241862018-02-14 14:14:54 -0800742 * neighbor set.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700743 */
Saurav Das261c3002017-06-13 15:35:54 -0700744 public int getNextObjectiveId(DestinationSet ds,
745 Map<DeviceId, Set<DeviceId>> nextHops,
Saurav Das97241862018-02-14 14:14:54 -0800746 TrafficSelector meta, boolean simple) {
Saurav Das261c3002017-06-13 15:35:54 -0700747 NextNeighbors next = dsNextObjStore.
748 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
749 if (next == null) {
750 log.debug("getNextObjectiveId in device{}: Next objective id "
751 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700752 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
753 deviceId,
Saurav Das261c3002017-06-13 15:35:54 -0700754 dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700755 .stream()
756 .filter((nsStoreEntry) ->
757 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
758 .collect(Collectors.toList()));
Saurav Das261c3002017-06-13 15:35:54 -0700759
Saurav Das97241862018-02-14 14:14:54 -0800760 createGroupFromDestinationSet(ds, nextHops, meta, simple);
Saurav Das261c3002017-06-13 15:35:54 -0700761 next = dsNextObjStore.
762 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
763 if (next == null) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700764 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das261c3002017-06-13 15:35:54 -0700765 // failure in creating group
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700766 return -1;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700767 } else {
768 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700769 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700770 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700771 } else {
772 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700773 + "found for {}", deviceId, next.nextId(), ds);
774 // should fix hash groups too if next-hops have changed
775 if (!next.dstNextHops().equals(nextHops)) {
776 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
777 deviceId, next.nextId());
778 if (!updateNextHops(ds, nextHops)) {
779 // failure in updating group
780 return -1;
781 }
782 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700783 }
Saurav Das261c3002017-06-13 15:35:54 -0700784 return next.nextId();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700785 }
786
sangho4a5c42a2015-05-20 22:16:38 -0700787 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800788 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das2d94d312015-11-24 23:21:05 -0800789 * or -1 if no such objective exists. Note that this method does NOT create
790 * the next objective as a side-effect. It is expected that is objective is
Saurav Dasf0f592d2016-11-18 15:21:57 -0800791 * created at startup from network configuration. Typically this is used
792 * for L2 flooding within the subnet configured on the switch.
Charles Chan77277672015-10-20 16:24:19 -0700793 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800794 * @param vlanId vlan id
Charles Chan77277672015-10-20 16:24:19 -0700795 * @return int if found or -1
796 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800797 public int getVlanNextObjectiveId(VlanId vlanId) {
798 Integer nextId = vlanNextObjStore.
799 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chanc6ad7752015-10-29 14:58:10 -0700800
801 return (nextId != null) ? nextId : -1;
Charles Chan77277672015-10-20 16:24:19 -0700802 }
803
804 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800805 * Returns the next objective of type simple associated with the port on the
806 * device, given the treatment. Different treatments to the same port result
807 * in different next objectives. If no such objective exists, this method
Saurav Das2cb38292017-03-29 19:09:17 -0700808 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Dasf0f592d2016-11-18 15:21:57 -0800809 * the creation of the objective. Typically this is used for L2 and L3 forwarding
810 * to compute nodes and containers/VMs on the compute nodes directly attached
811 * to the switch.
Saurav Das2d94d312015-11-24 23:21:05 -0800812 *
813 * @param portNum the port number for the simple next objective
814 * @param treatment the actions to apply on the packets (should include outport)
815 * @param meta optional metadata passed into the creation of the next objective
Saurav Das2cb38292017-03-29 19:09:17 -0700816 * @param createIfMissing true if a next object should be created if not found
Saurav Das2d94d312015-11-24 23:21:05 -0800817 * @return int if found or created, -1 if there are errors during the
818 * creation of the next objective.
819 */
820 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700821 TrafficSelector meta, boolean createIfMissing) {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800822 Integer nextId = portNextObjStore
Saurav Das368cf212017-03-15 15:15:14 -0700823 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das2cb38292017-03-29 19:09:17 -0700824 if (nextId != null) {
825 return nextId;
826 }
827 log.debug("getPortNextObjectiveId in device {}: Next objective id "
828 + "not found for port: {} .. {}", deviceId, portNum,
829 (createIfMissing) ? "creating" : "aborting");
830 if (!createIfMissing) {
831 return -1;
832 }
833 // create missing next objective
834 createGroupFromPort(portNum, treatment, meta);
835 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
836 treatment, meta));
Saurav Das2d94d312015-11-24 23:21:05 -0800837 if (nextId == null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700838 log.warn("getPortNextObjectiveId: unable to create next obj"
839 + "for dev:{} port:{}", deviceId, portNum);
840 return -1;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800841 }
842 return nextId;
843 }
844
845 /**
sangho4a5c42a2015-05-20 22:16:38 -0700846 * Checks if the next objective ID (group) for the neighbor set exists or not.
847 *
848 * @param ns neighbor set to check
849 * @return true if it exists, false otherwise
850 */
Saurav Das261c3002017-06-13 15:35:54 -0700851 public boolean hasNextObjectiveId(DestinationSet ns) {
852 NextNeighbors nextHops = dsNextObjStore.
853 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
854 if (nextHops == null) {
sangho4a5c42a2015-05-20 22:16:38 -0700855 return false;
856 }
857
858 return true;
859 }
860
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700861 private void populateNeighborMaps() {
862 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700863 for (Link link : outgoingLinks) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700864 if (link.type() != Link.Type.DIRECT) {
865 continue;
866 }
867 addNeighborAtPort(link.dst().deviceId(), link.src().port());
868 }
869 }
870
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700871 protected void addNeighborAtPort(DeviceId neighborId,
872 PortNumber portToNeighbor) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700873 // Update DeviceToPort database
874 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
875 deviceId, neighborId, portToNeighbor);
Saurav Das4c35fc42015-11-20 15:27:53 -0800876 Set<PortNumber> ports = Collections
877 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
878 ports.add(portToNeighbor);
879 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
880 if (portnums != null) {
881 portnums.add(portToNeighbor);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700882 }
883
884 // Update portToDevice database
Saurav Das97241862018-02-14 14:14:54 -0800885 // should always update as neighbor could have changed on this port
886 DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId);
Saurav Das4c35fc42015-11-20 15:27:53 -0800887 if (prev != null) {
Saurav Das97241862018-02-14 14:14:54 -0800888 log.debug("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ",
Saurav Das62ae6792017-05-15 15:34:25 -0700889 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700890 }
891 }
892
sangho27462c62015-05-14 00:39:53 -0700893 /**
Saurav Das261c3002017-06-13 15:35:54 -0700894 * Creates a NextObjective for a hash group in this device from a given
Saurav Das97241862018-02-14 14:14:54 -0800895 * DestinationSet. If the parameter simple is true, a simple next objective
896 * is created instead.
sangho27462c62015-05-14 00:39:53 -0700897 *
Saurav Das261c3002017-06-13 15:35:54 -0700898 * @param ds the DestinationSet
899 * @param neighbors a map for each destination and its next-hops
Saurav Das4c35fc42015-11-20 15:27:53 -0800900 * @param meta metadata passed into the creation of a Next Objective
Saurav Das97241862018-02-14 14:14:54 -0800901 * @param simple if true, a simple next objective will be created instead of
902 * a hashed next objective
sangho27462c62015-05-14 00:39:53 -0700903 */
Saurav Das261c3002017-06-13 15:35:54 -0700904 public void createGroupFromDestinationSet(DestinationSet ds,
905 Map<DeviceId, Set<DeviceId>> neighbors,
906 TrafficSelector meta,
Saurav Das97241862018-02-14 14:14:54 -0800907 boolean simple) {
Saurav Das261c3002017-06-13 15:35:54 -0700908 int nextId = flowObjectiveService.allocateNextId();
Saurav Das97241862018-02-14 14:14:54 -0800909 NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE
910 : NextObjective.Type.HASHED;
Saurav Das261c3002017-06-13 15:35:54 -0700911 if (neighbors == null || neighbors.isEmpty()) {
912 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
913 + "to create group in dev:{} for ds: {} with next-hops {}",
914 deviceId, ds, neighbors);
915 return;
916 }
Saurav Das261c3002017-06-13 15:35:54 -0700917
918 NextObjective.Builder nextObjBuilder = DefaultNextObjective
919 .builder()
920 .withId(nextId)
921 .withType(type)
922 .fromApp(appId);
923 if (meta != null) {
924 nextObjBuilder.withMeta(meta);
925 }
926
927 // create treatment buckets for each neighbor for each dst Device
928 // except in the special case where we only want to pick a single
Saurav Das97241862018-02-14 14:14:54 -0800929 // neighbor/port for a simple nextObj
Saurav Das261c3002017-06-13 15:35:54 -0700930 boolean foundSingleNeighbor = false;
931 boolean treatmentAdded = false;
932 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
933 for (DeviceId dst : ds.getDestinationSwitches()) {
934 Set<DeviceId> nextHops = neighbors.get(dst);
935 if (nextHops == null || nextHops.isEmpty()) {
936 continue;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700937 }
Saurav Das261c3002017-06-13 15:35:54 -0700938
939 if (foundSingleNeighbor) {
940 break;
941 }
942
943 for (DeviceId neighborId : nextHops) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800944 if (devicePortMap.get(neighborId) == null) {
945 log.warn("Neighbor {} is not in the port map yet for dev:{}",
946 neighborId, deviceId);
sangho2165d222015-05-01 09:38:25 -0700947 return;
Jon Hall31d84782017-01-18 20:15:44 -0800948 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700949 log.warn("There are no ports for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800950 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700951 return;
sangho2165d222015-05-01 09:38:25 -0700952 }
953
Saurav Das4c35fc42015-11-20 15:27:53 -0800954 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -0800955 try {
Saurav Das4c35fc42015-11-20 15:27:53 -0800956 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan319d1a22015-11-03 10:42:14 -0800957 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700958 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan319d1a22015-11-03 10:42:14 -0800959 return;
960 }
Saurav Das261c3002017-06-13 15:35:54 -0700961 // For each port to the neighbor, we create a new treatment
Pier Ventre229fd0b2016-10-31 16:49:19 -0700962 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
Saurav Das97241862018-02-14 14:14:54 -0800963 // In this case we need a SIMPLE nextObj. We randomly pick a port
964 if (simple) {
Pier Ventre229fd0b2016-10-31 16:49:19 -0700965 int size = devicePortMap.get(neighborId).size();
966 int index = RandomUtils.nextInt(0, size);
967 neighborPorts = Collections.singleton(
Saurav Das261c3002017-06-13 15:35:54 -0700968 Iterables.get(devicePortMap.get(neighborId),
969 index));
970 foundSingleNeighbor = true;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700971 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700972
Pier Ventre229fd0b2016-10-31 16:49:19 -0700973 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700974 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
975 .builder();
Saurav Das261c3002017-06-13 15:35:54 -0700976 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
977 int edgeLabel = ds.getEdgeLabel(dst);
978 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Das97241862018-02-14 14:14:54 -0800979 if (simple) {
980 // swap label case
981 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
982 } else {
983 // ecmp with label push case
984 tBuilder.pushMpls().copyTtlOut()
985 .setMpls(MplsLabel.mplsLabel(edgeLabel));
986 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700987 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800988 tBuilder.setOutput(sp);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700989 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das261c3002017-06-13 15:35:54 -0700990 treatmentAdded = true;
991 //update store
992 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
993 if (existingNeighbors == null) {
994 existingNeighbors = new HashSet<>();
995 }
996 existingNeighbors.add(neighborId);
997 dstNextHops.put(dst, existingNeighbors);
998 log.debug("creating treatment for port/label {}/{} in next:{}",
999 sp, edgeLabel, nextId);
1000 }
1001
1002 if (foundSingleNeighbor) {
1003 break;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001004 }
1005 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001006 }
Saurav Das261c3002017-06-13 15:35:54 -07001007
1008 if (!treatmentAdded) {
1009 log.warn("Could not createGroup from DestinationSet {} without any"
1010 + "next hops {}", ds, neighbors);
1011 return;
1012 }
1013 ObjectiveContext context = new DefaultObjectiveContext(
1014 (objective) ->
1015 log.debug("createGroupsFromDestinationSet installed "
1016 + "NextObj {} on {}", nextId, deviceId),
1017 (objective, error) ->
1018 log.warn("createGroupsFromDestinationSet failed to install"
1019 + " NextObj {} on {}: {}", nextId, deviceId, error)
1020 );
1021 NextObjective nextObj = nextObjBuilder.add(context);
1022 log.debug(".. createGroupsFromDestinationSet: Submitted "
1023 + "next objective {} in device {}", nextId, deviceId);
1024 flowObjectiveService.next(deviceId, nextObj);
1025 //update store
1026 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
1027 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001028 }
1029
Saurav Das2d94d312015-11-24 23:21:05 -08001030 /**
Saurav Dasf0f592d2016-11-18 15:21:57 -08001031 * Creates broadcast groups for all ports in the same subnet for
1032 * all configured subnets.
Saurav Das2d94d312015-11-24 23:21:05 -08001033 */
Charles Chan10b0fb72017-02-02 16:20:42 -08001034 public void createGroupsFromVlanConfig() {
Charles Chan90772a72017-02-08 15:52:08 -08001035 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001036 createBcastGroupFromVlan(vlanId, ports);
Pier Ventreb6a7f342016-11-26 21:05:22 -08001037 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001038 }
Charles Chanc6ad7752015-10-29 14:58:10 -07001039
Saurav Dasf0f592d2016-11-18 15:21:57 -08001040 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001041 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Dasf0f592d2016-11-18 15:21:57 -08001042 *
Charles Chan10b0fb72017-02-02 16:20:42 -08001043 * @param vlanId vlan id
Saurav Dasf0f592d2016-11-18 15:21:57 -08001044 * @param ports list of ports in the subnet
1045 */
Charles Chan90772a72017-02-08 15:52:08 -08001046 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001047 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chanc6ad7752015-10-29 14:58:10 -07001048
Charles Chan10b0fb72017-02-02 16:20:42 -08001049 if (vlanNextObjStore.containsKey(key)) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001050 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan10b0fb72017-02-02 16:20:42 -08001051 deviceId, vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001052 return;
1053 }
Charles Chande6655c2015-12-23 00:15:11 -08001054
Saurav Dasf0f592d2016-11-18 15:21:57 -08001055 TrafficSelector metadata =
Charles Chan10b0fb72017-02-02 16:20:42 -08001056 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chan77277672015-10-20 16:24:19 -07001057
Saurav Dasf0f592d2016-11-18 15:21:57 -08001058 int nextId = flowObjectiveService.allocateNextId();
Charles Chan77277672015-10-20 16:24:19 -07001059
Saurav Dasf0f592d2016-11-18 15:21:57 -08001060 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1061 .builder().withId(nextId)
1062 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1063 .withMeta(metadata);
Charles Chan77277672015-10-20 16:24:19 -07001064
Saurav Dasf0f592d2016-11-18 15:21:57 -08001065 ports.forEach(port -> {
1066 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan90772a72017-02-08 15:52:08 -08001067 if (toPopVlan(port, vlanId)) {
1068 tBuilder.popVlan();
1069 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001070 tBuilder.setOutput(port);
1071 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chan77277672015-10-20 16:24:19 -07001072 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001073
Saurav Das2cb38292017-03-29 19:09:17 -07001074 ObjectiveContext context = new DefaultObjectiveContext(
1075 (objective) ->
1076 log.debug("createBroadcastGroupFromVlan installed "
1077 + "NextObj {} on {}", nextId, deviceId),
1078 (objective, error) ->
1079 log.warn("createBroadcastGroupFromVlan failed to install"
1080 + " NextObj {} on {}: {}", nextId, deviceId, error)
1081 );
1082 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001083 flowObjectiveService.next(deviceId, nextObj);
Saurav Das2b6a00f2017-12-05 15:00:23 -08001084 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1085 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001086
Charles Chan10b0fb72017-02-02 16:20:42 -08001087 vlanNextObjStore.put(key, nextId);
Charles Chan77277672015-10-20 16:24:19 -07001088 }
1089
Charles Chanb7f75ac2016-01-11 18:28:54 -08001090 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001091 * Removes a single broadcast group from a given vlan id.
1092 * The group should be empty.
1093 * @param deviceId device Id to remove the group
1094 * @param portNum port number related to the group
1095 * @param vlanId vlan id of the broadcast group to remove
1096 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1097 */
1098 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1099 VlanId vlanId, boolean popVlan) {
1100 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1101
1102 if (!vlanNextObjStore.containsKey(key)) {
1103 log.debug("Broadcast group for device {} and subnet {} does not exist",
1104 deviceId, vlanId);
1105 return;
1106 }
1107
1108 TrafficSelector metadata =
1109 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1110
1111 int nextId = vlanNextObjStore.get(key);
1112
1113 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1114 .builder().withId(nextId)
1115 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1116 .withMeta(metadata);
1117
1118 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1119 if (popVlan) {
1120 tBuilder.popVlan();
1121 }
1122 tBuilder.setOutput(portNum);
1123 nextObjBuilder.addTreatment(tBuilder.build());
1124
1125 ObjectiveContext context = new DefaultObjectiveContext(
1126 (objective) ->
1127 log.debug("removeBroadcastGroupFromVlan removed "
1128 + "NextObj {} on {}", nextId, deviceId),
1129 (objective, error) ->
1130 log.warn("removeBroadcastGroupFromVlan failed to remove "
1131 + " NextObj {} on {}: {}", nextId, deviceId, error)
1132 );
1133 NextObjective nextObj = nextObjBuilder.remove(context);
1134 flowObjectiveService.next(deviceId, nextObj);
1135 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1136 nextId, deviceId);
1137
1138 vlanNextObjStore.remove(key, nextId);
1139 }
1140
1141 /**
Charles Chan90772a72017-02-08 15:52:08 -08001142 * Determine if we should pop given vlan before sending packets to the given port.
1143 *
1144 * @param portNumber port number
1145 * @param vlanId vlan id
1146 * @return true if the vlan id is not contained in any vlanTagged config
1147 */
1148 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das261c3002017-06-13 15:35:54 -07001149 return srManager.interfaceService
1150 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan90772a72017-02-08 15:52:08 -08001151 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1152 }
1153
1154 /**
Saurav Das2d94d312015-11-24 23:21:05 -08001155 * Create simple next objective for a single port. The treatments can include
1156 * all outgoing actions that need to happen on the packet.
1157 *
1158 * @param portNum the outgoing port on the device
1159 * @param treatment the actions to apply on the packets (should include outport)
1160 * @param meta optional data to pass to the driver
1161 */
1162 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1163 TrafficSelector meta) {
1164 int nextId = flowObjectiveService.allocateNextId();
1165 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das368cf212017-03-15 15:15:14 -07001166 deviceId, portNum, treatment, meta);
Saurav Das2d94d312015-11-24 23:21:05 -08001167
1168 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1169 .builder().withId(nextId)
1170 .withType(NextObjective.Type.SIMPLE)
1171 .addTreatment(treatment)
1172 .fromApp(appId)
1173 .withMeta(meta);
1174
Saurav Das2cb38292017-03-29 19:09:17 -07001175 ObjectiveContext context = new DefaultObjectiveContext(
1176 (objective) ->
1177 log.debug("createGroupFromPort installed "
1178 + "NextObj {} on {}", nextId, deviceId),
1179 (objective, error) ->
1180 log.warn("createGroupFromPort failed to install"
1181 + " NextObj {} on {}: {}", nextId, deviceId, error)
1182 );
1183 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das2d94d312015-11-24 23:21:05 -08001184 flowObjectiveService.next(deviceId, nextObj);
1185 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1186 + "for port {}", nextId, deviceId, portNum);
1187
1188 portNextObjStore.put(key, nextId);
1189 }
1190
sangho27462c62015-05-14 00:39:53 -07001191 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001192 * Removes simple next objective for a single port.
1193 *
1194 * @param deviceId device id that has the port to deal with
1195 * @param portNum the outgoing port on the device
1196 * @param vlanId vlan id associated with the port
1197 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1198 */
1199 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1200 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1201 mbuilder.matchVlanId(vlanId);
1202
1203 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1204 tbuilder.immediate().setOutput(portNum);
1205 if (popVlan) {
1206 tbuilder.immediate().popVlan();
1207 }
1208
1209 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1210 tbuilder.build(), mbuilder.build(), false);
1211
1212 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1213 deviceId, portNum, tbuilder.build(), mbuilder.build());
1214 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1215 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1216 .builder().withId(portNextObjId)
1217 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1218 ObjectiveContext context = new DefaultObjectiveContext(
1219 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1220 portNextObjId, deviceId),
1221 (objective, error) ->
1222 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1223 portNextObjId, deviceId, error));
1224 NextObjective nextObjective = nextObjBuilder.remove(context);
1225 log.info("**removePortNextObjective: Submitted "
1226 + "next objective {} in device {}",
1227 portNextObjId, deviceId);
1228 flowObjectiveService.next(deviceId, nextObjective);
1229
1230 portNextObjStore.remove(key);
1231 }
1232 }
1233 /**
sangho27462c62015-05-14 00:39:53 -07001234 * Removes groups for the next objective ID given.
1235 *
1236 * @param objectiveId next objective ID to remove
1237 * @return true if succeeds, false otherwise
1238 */
1239 public boolean removeGroup(int objectiveId) {
Saurav Das261c3002017-06-13 15:35:54 -07001240 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1241 dsNextObjStore.entrySet()) {
1242 if (e.getValue().nextId() != objectiveId) {
1243 continue;
1244 }
Pier Luigiecb09f42018-01-14 21:56:11 +01001245 // Right now it is just used in TunnelHandler
1246 // remember in future that PW transit groups could
1247 // be Indirect groups
sangho27462c62015-05-14 00:39:53 -07001248 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1249 .builder().withId(objectiveId)
1250 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chana4ee4f92016-04-23 14:48:16 -07001251 ObjectiveContext context = new DefaultObjectiveContext(
1252 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1253 objectiveId, deviceId),
1254 (objective, error) ->
1255 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
1256 objectiveId, deviceId, error));
1257 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das4c35fc42015-11-20 15:27:53 -08001258 log.info("**removeGroup: Submited "
1259 + "next objective {} in device {}",
1260 objectiveId, deviceId);
sangho27462c62015-05-14 00:39:53 -07001261 flowObjectiveService.next(deviceId, nextObjective);
1262
Saurav Das261c3002017-06-13 15:35:54 -07001263 dsNextObjStore.remove(e.getKey());
sangho4a5c42a2015-05-20 22:16:38 -07001264 return true;
sangho27462c62015-05-14 00:39:53 -07001265 }
1266
1267 return false;
1268 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001269 /**
1270 * Remove simple next objective for a single port. The treatments can include
1271 * all outgoing actions that need to happen on the packet.
1272 *
1273 * @param portNum the outgoing port on the device
1274 * @param treatment the actions applied on the packets (should include outport)
1275 * @param meta optional data to pass to the driver
1276 */
1277 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1278 TrafficSelector meta) {
1279 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1280 deviceId, portNum, treatment, meta);
1281 Integer nextId = portNextObjStore.get(key);
1282
1283 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1284 .builder().withId(nextId)
1285 .withType(NextObjective.Type.SIMPLE)
1286 .addTreatment(treatment)
1287 .fromApp(appId)
1288 .withMeta(meta);
1289
1290 ObjectiveContext context = new DefaultObjectiveContext(
1291 (objective) ->
1292 log.info("removeGroupFromPort installed "
1293 + "NextObj {} on {}", nextId, deviceId),
1294 (objective, error) ->
1295 log.warn("removeGroupFromPort failed to install"
1296 + " NextObj {} on {}: {}", nextId, deviceId, error)
1297 );
1298 NextObjective nextObj = nextObjBuilder.remove(context);
1299 flowObjectiveService.next(deviceId, nextObj);
1300 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1301 + "for port {}", nextId, deviceId, portNum);
1302
1303 portNextObjStore.remove(key);
1304 }
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001305
Charles Chanb7f75ac2016-01-11 18:28:54 -08001306 /**
1307 * Removes all groups from all next objective stores.
1308 */
Saurav Das261c3002017-06-13 15:35:54 -07001309 /*public void removeAllGroups() {
1310 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das62af8802015-12-04 10:52:59 -08001311 nsNextObjStore.entrySet()) {
Saurav Das261c3002017-06-13 15:35:54 -07001312 removeGroup(entry.getValue().nextId());
Saurav Das62af8802015-12-04 10:52:59 -08001313 }
1314 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1315 portNextObjStore.entrySet()) {
1316 removeGroup(entry.getValue());
1317 }
Charles Chan10b0fb72017-02-02 16:20:42 -08001318 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1319 vlanNextObjStore.entrySet()) {
Saurav Das62af8802015-12-04 10:52:59 -08001320 removeGroup(entry.getValue());
1321 }
Saurav Das261c3002017-06-13 15:35:54 -07001322 }*/ //XXX revisit
1323
Saurav Dasfbe74572017-08-03 18:30:35 -07001324 /**
1325 * Triggers a one time bucket verification operation on all hash groups
1326 * on this device.
1327 */
1328 public void triggerBucketCorrector() {
1329 BucketCorrector bc = new BucketCorrector();
1330 bc.run();
1331 }
1332
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001333 /**
1334 * Modifies L2IG bucket when the interface configuration is updated, especially
1335 * when the interface has same VLAN ID but the VLAN type is changed (e.g., from
1336 * vlan-tagged [10] to vlan-untagged 10), which requires changes on
1337 * TrafficTreatment in turn.
1338 *
1339 * @param portNumber the port on this device that needs to be updated
1340 * @param vlanId the vlan id corresponding to this port
1341 * @param pushVlan indicates if packets should be sent out untagged or not out
1342 * from the port. If true, updated TrafficTreatment involves
1343 * pop vlan tag action. If false, updated TrafficTreatment
1344 * does not involve pop vlan tag action.
1345 */
1346 public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) {
1347 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1348 if (pushVlan) {
1349 tBuilder.popVlan();
1350 }
1351 tBuilder.setOutput(portNumber);
1352
1353 TrafficSelector metadata =
1354 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1355
1356 int nextId = getVlanNextObjectiveId(vlanId);
1357
1358 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1359 .builder().withId(nextId)
1360 .withType(NextObjective.Type.SIMPLE).fromApp(appId)
1361 .addTreatment(tBuilder.build())
1362 .withMeta(metadata);
1363
1364 ObjectiveContext context = new DefaultObjectiveContext(
1365 (objective) -> log.debug("port {} successfully updated NextObj {} on {}",
1366 portNumber, nextId, deviceId),
1367 (objective, error) ->
1368 log.warn("port {} failed to updated NextObj {} on {}: {}",
1369 portNumber, nextId, deviceId, error));
1370
1371 flowObjectiveService.next(deviceId, nextObjBuilder.modify(context));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001372 }
1373
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001374 /**
1375 * Adds a single port to the L2FG or removes it from the L2FG.
1376 *
1377 * @param vlanId the vlan id corresponding to this port
1378 * @param portNum the port on this device to be updated
1379 * @param nextId the next objective ID for the given vlan id
1380 * @param install if true, adds the port to L2FG. If false, removes it from L2FG.
1381 */
1382 public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001383 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1384 if (toPopVlan(portNum, vlanId)) {
1385 tBuilder.popVlan();
1386 }
1387 tBuilder.setOutput(portNum);
1388
1389 TrafficSelector metadata =
1390 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1391
1392 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1393 .builder().withId(nextId)
1394 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1395 .addTreatment(tBuilder.build())
1396 .withMeta(metadata);
1397
1398 ObjectiveContext context = new DefaultObjectiveContext(
1399 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1400 portNum, nextId, deviceId),
1401 (objective, error) ->
1402 log.warn("port {} failed to removedFrom NextObj {} on {}: {}",
1403 portNum, nextId, deviceId, error));
1404
1405 if (install) {
1406 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1407 } else {
1408 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1409 }
1410 }
Saurav Das8a3022d2017-05-05 17:01:08 -07001411
1412 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001413 * Performs bucket verification operation for all hash groups in this device.
1414 * Checks RouteHandler to ensure that routing is stable before attempting
1415 * verification. Verification involves creating a nextObjective with
1416 * operation VERIFY for existing next objectives in the store, and passing
1417 * it to the driver. It is the driver that actually performs the verification
1418 * by adding or removing buckets to match the verification next objective
1419 * created here.
Saurav Das8a3022d2017-05-05 17:01:08 -07001420 */
Saurav Dasfbe74572017-08-03 18:30:35 -07001421 protected final class BucketCorrector implements Runnable {
1422 Integer nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001423
Saurav Dasfbe74572017-08-03 18:30:35 -07001424 BucketCorrector() {
1425 this.nextId = null;
1426 }
1427
1428 BucketCorrector(Integer nextId) {
1429 this.nextId = nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001430 }
1431
1432 @Override
1433 public void run() {
Saurav Dasfbe74572017-08-03 18:30:35 -07001434 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1435 return;
Saurav Das8a3022d2017-05-05 17:01:08 -07001436 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001437 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1438 if (rh == null) {
1439 return;
1440 }
1441 if (!rh.isRoutingStable()) {
1442 return;
1443 }
1444 rh.acquireRoutingLock();
1445 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001446 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasfbe74572017-08-03 18:30:35 -07001447 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1448 .stream()
1449 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier Luigiecb09f42018-01-14 21:56:11 +01001450 // Filter out PW transit groups or include them if MPLS ECMP is supported
Saurav Das97241862018-02-14 14:14:54 -08001451 .filter(entry -> !entry.getKey().destinationSet().notBos() ||
1452 (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp()))
1453 // Filter out simple SWAP groups or include them if MPLS ECMP is supported
1454 .filter(entry -> !entry.getKey().destinationSet().swap() ||
1455 (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp()))
Saurav Dasfbe74572017-08-03 18:30:35 -07001456 .map(entry -> entry.getKey())
1457 .collect(Collectors.toSet());
1458 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1459 NextNeighbors next = dsNextObjStore.get(dsKey);
1460 if (next == null) {
1461 continue;
1462 }
1463 int nid = next.nextId();
1464 if (nextId != null && nextId != nid) {
1465 continue;
1466 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001467 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasfbe74572017-08-03 18:30:35 -07001468 deviceId, dsKey, next);
1469 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
1470 metabuilder.matchVlanId(INTERNAL_VLAN);
1471 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1472 .withId(nid)
1473 .withType(NextObjective.Type.HASHED)
1474 .withMeta(metabuilder.build())
1475 .fromApp(appId);
1476
1477 next.dstNextHops().forEach((dstDev, nextHops) -> {
1478 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1479 nextHops.forEach(neighbor -> {
1480 MacAddress neighborMac;
1481 try {
1482 neighborMac = deviceConfig.getDeviceMac(neighbor);
1483 } catch (DeviceConfigNotFoundException e) {
1484 log.warn(e.getMessage() + " Aborting neighbor"
1485 + neighbor);
1486 return;
1487 }
1488 devicePortMap.get(neighbor).forEach(port -> {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001489 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasfbe74572017-08-03 18:30:35 -07001490 + " port/label {}/{} to dst {} via {}",
1491 deviceId, nid, port, edgeLabel,
1492 dstDev, neighbor);
Saurav Das97241862018-02-14 14:14:54 -08001493 nextObjBuilder
1494 .addTreatment(treatmentBuilder(port,
1495 neighborMac,
1496 dsKey.destinationSet().swap(),
1497 edgeLabel));
Saurav Dasfbe74572017-08-03 18:30:35 -07001498 });
1499 });
1500 });
1501
1502 NextObjective nextObjective = nextObjBuilder.verify();
1503 flowObjectiveService.next(deviceId, nextObjective);
1504 }
1505 } finally {
1506 rh.releaseRoutingLock();
1507 }
1508
1509 }
1510
1511 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
Saurav Das97241862018-02-14 14:14:54 -08001512 boolean swap, int edgeLabel) {
Saurav Dasfbe74572017-08-03 18:30:35 -07001513 TrafficTreatment.Builder tBuilder =
1514 DefaultTrafficTreatment.builder();
1515 tBuilder.setOutput(outport)
1516 .setEthDst(dstMac)
1517 .setEthSrc(nodeMacAddr);
1518 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Saurav Das97241862018-02-14 14:14:54 -08001519 if (swap) {
1520 // swap label case
1521 tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel));
1522 } else {
1523 // ecmp with label push case
1524 tBuilder.pushMpls()
1525 .copyTtlOut()
1526 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1527 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001528 }
1529 return tBuilder.build();
Saurav Das8a3022d2017-05-05 17:01:08 -07001530 }
1531 }
1532
Pier Luigiecb09f42018-01-14 21:56:11 +01001533}