blob: 2c4fb5e9f58e0eed913dca7d8d3afc65c66d6f97 [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 Das041bb782017-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 Das041bb782017-08-14 16:44:43 -070020import com.google.common.collect.Lists;
Saurav Dasc88d4662017-05-15 15:34:25 -070021import com.google.common.collect.Sets;
22
Pier Ventre917127a2016-10-31 16:49:19 -070023import org.apache.commons.lang3.RandomUtils;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import org.onlab.packet.MacAddress;
sangho32a59322015-02-17 12:07:41 -080025import org.onlab.packet.MplsLabel;
Saurav Das423fe2b2015-12-04 10:52:59 -080026import org.onlab.packet.VlanId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070027import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080028import org.onosproject.core.ApplicationId;
Charles Chan59cc16d2017-02-02 16:20:42 -080029import org.onosproject.net.ConnectPoint;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
Saurav Das423fe2b2015-12-04 10:52:59 -080033import org.onosproject.net.flow.DefaultTrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080034import org.onosproject.net.flow.DefaultTrafficTreatment;
Saurav Das8a0732e2015-11-20 15:27:53 -080035import org.onosproject.net.flow.TrafficSelector;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.net.flow.TrafficTreatment;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070037import org.onosproject.net.flowobjective.DefaultNextObjective;
Charles Chan216e3c82016-04-23 14:48:16 -070038import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070039import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.NextObjective;
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070041import org.onosproject.net.flowobjective.ObjectiveContext;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080042import org.onosproject.net.link.LinkService;
Saurav Dasceccf242017-08-03 18:30:35 -070043import org.onosproject.segmentrouting.DefaultRoutingHandler;
Saurav Das423fe2b2015-12-04 10:52:59 -080044import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan0b4e6182015-11-03 10:42:14 -080045import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
46import org.onosproject.segmentrouting.config.DeviceProperties;
Saurav Das7bcbe702017-06-13 15:35:54 -070047import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070048import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080049import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070050import org.onosproject.store.service.EventuallyConsistentMap;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080051import org.slf4j.Logger;
52
Pier Ventre917127a2016-10-31 16:49:19 -070053import java.net.URI;
Charles Chan7ffd81f2017-02-08 15:52:08 -080054import java.util.Collection;
Pier Ventre917127a2016-10-31 16:49:19 -070055import java.util.Collections;
Saurav Dasc88d4662017-05-15 15:34:25 -070056import java.util.HashMap;
Pier Ventre917127a2016-10-31 16:49:19 -070057import java.util.HashSet;
58import java.util.List;
59import java.util.Map;
60import java.util.Set;
61import java.util.concurrent.ConcurrentHashMap;
Saurav Das1547b3f2017-05-05 17:01:08 -070062import java.util.concurrent.ScheduledExecutorService;
63import java.util.concurrent.TimeUnit;
Pier Ventre917127a2016-10-31 16:49:19 -070064import java.util.stream.Collectors;
65
66import static com.google.common.base.Preconditions.checkNotNull;
Saurav Das1547b3f2017-05-05 17:01:08 -070067import static java.util.concurrent.Executors.newScheduledThreadPool;
68import static org.onlab.util.Tools.groupedThreads;
Charles Chan59cc16d2017-02-02 16:20:42 -080069import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
Pier Ventre917127a2016-10-31 16:49:19 -070070import static org.slf4j.LoggerFactory.getLogger;
71
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080072/**
Srikanth Vavilapallif5b234a2015-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 Vavilapallied12ae52015-02-09 14:43:19 -080076 */
77public class DefaultGroupHandler {
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -070078 protected static final Logger log = getLogger(DefaultGroupHandler.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080079
Saurav Dasceccf242017-08-03 18:30:35 -070080 private static final long VERIFY_INTERVAL = 30; // secs
81
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080082 protected final DeviceId deviceId;
83 protected final ApplicationId appId;
84 protected final DeviceProperties deviceConfig;
85 protected final List<Integer> allSegmentIds;
Pier Ventree0ae7a32016-11-23 09:57:42 -080086 protected int ipv4NodeSegmentId = -1;
87 protected int ipv6NodeSegmentId = -1;
Charles Chan0b4e6182015-11-03 10:42:14 -080088 protected boolean isEdgeRouter = false;
89 protected MacAddress nodeMacAddr = null;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080090 protected LinkService linkService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070091 protected FlowObjectiveService flowObjectiveService;
Saurav Dasc88d4662017-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 Das8a0732e2015-11-20 15:27:53 -080096 protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap =
97 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -070098 /**
99 * local store for ports on this device connected to neighbor-device-id.
100 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800101 protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap =
102 new ConcurrentHashMap<>();
Saurav Dasc88d4662017-05-15 15:34:25 -0700103
Saurav Das7bcbe702017-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 Das1a129a02016-11-18 15:21:57 -0800107 // distributed store for (device+subnet-ip-prefix) mapped to next-id
Charles Chan59cc16d2017-02-02 16:20:42 -0800108 protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
109 vlanNextObjStore = null;
Saurav Das1a129a02016-11-18 15:21:57 -0800110 // distributed store for (device+port+treatment) mapped to next-id
Charles Chane849c192016-01-11 18:28:54 -0800111 protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
112 portNextObjStore = null;
Charles Chan188ebf52015-12-23 00:15:11 -0800113 private SegmentRoutingManager srManager;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800114
Saurav Das1547b3f2017-05-05 17:01:08 -0700115 private ScheduledExecutorService executorService
Saurav Dasceccf242017-08-03 18:30:35 -0700116 = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log));
Saurav Das1547b3f2017-05-05 17:01:08 -0700117
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700118 protected KryoNamespace.Builder kryo = new KryoNamespace.Builder()
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700119 .register(URI.class).register(HashSet.class)
Saurav Das7bcbe702017-06-13 15:35:54 -0700120 .register(DeviceId.class).register(PortNumber.class)
121 .register(DestinationSet.class).register(PolicyGroupIdentifier.class)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700122 .register(PolicyGroupParams.class)
123 .register(GroupBucketIdentifier.class)
124 .register(GroupBucketIdentifier.BucketOutputType.class);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800125
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700126 protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId,
127 DeviceProperties config,
128 LinkService linkService,
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700129 FlowObjectiveService flowObjService,
Charles Chan188ebf52015-12-23 00:15:11 -0800130 SegmentRoutingManager srManager) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800131 this.deviceId = checkNotNull(deviceId);
132 this.appId = checkNotNull(appId);
133 this.deviceConfig = checkNotNull(config);
134 this.linkService = checkNotNull(linkService);
Charles Chan0b4e6182015-11-03 10:42:14 -0800135 this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds());
136 try {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800137 this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId);
138 this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId);
Charles Chan0b4e6182015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700145 this.flowObjectiveService = flowObjService;
Saurav Das7bcbe702017-06-13 15:35:54 -0700146 this.dsNextObjStore = srManager.dsNextObjStore();
Ray Milkeye4afdb52017-04-05 09:42:04 -0700147 this.vlanNextObjStore = srManager.vlanNextObjStore();
148 this.portNextObjStore = srManager.portNextObjStore();
Charles Chan188ebf52015-12-23 00:15:11 -0800149 this.srManager = srManager;
Saurav Dasceccf242017-08-03 18:30:35 -0700150 executorService.scheduleWithFixedDelay(new BucketCorrector(), 10,
151 VERIFY_INTERVAL,
152 TimeUnit.SECONDS);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800153 populateNeighborMaps();
154 }
155
156 /**
Saurav Dasceccf242017-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 Dasc88d4662017-05-15 15:34:25 -0700165 * Creates a group handler object.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800166 *
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 Vavilapallif5b234a2015-04-21 13:04:13 -0700171 * @param flowObjService flow objective service object
Charles Chane849c192016-01-11 18:28:54 -0800172 * @param srManager segment routing manager
Charles Chan0b4e6182015-11-03 10:42:14 -0800173 * @throws DeviceConfigNotFoundException if the device configuration is not found
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800174 * @return default group handler type
175 */
Saurav Das4ce45962015-11-24 23:21:05 -0800176 public static DefaultGroupHandler createGroupHandler(
Saurav Dasceccf242017-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 Dasc88d4662017-05-15 15:34:25 -0700184 return new DefaultGroupHandler(deviceId, appId, config,
185 linkService,
186 flowObjService,
187 srManager);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800188 }
189
190 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700191 * Updates local stores for link-src device/port to neighbor (link-dst).
192 *
193 * @param link the infrastructure link
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800194 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700195 public void portUpForLink(Link link) {
Saurav Dasceccf242017-08-03 18:30:35 -0700196 if (!link.src().deviceId().equals(deviceId)) {
197 log.warn("linkUp: deviceId{} doesn't match with link src {}",
198 deviceId, link.src().deviceId());
199 return;
200 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700201
Saurav Dasceccf242017-08-03 18:30:35 -0700202 log.info("* portUpForLink: Device {} linkUp at local port {} to "
203 + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId());
204 // ensure local state is updated even if linkup is aborted later on
205 addNeighborAtPort(link.dst().deviceId(),
206 link.src().port());
207 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700208
Saurav Dasceccf242017-08-03 18:30:35 -0700209 /**
210 * Updates local stores for port that has gone down.
211 *
212 * @param port port number that has gone down
213 */
214 public void portDown(PortNumber port) {
215 if (portDeviceMap.get(port) == null) {
216 log.warn("portDown: unknown port");
217 return;
218 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700219
Saurav Dasceccf242017-08-03 18:30:35 -0700220 log.debug("Device {} portDown {} to neighbor {}", deviceId, port,
221 portDeviceMap.get(port));
222 devicePortMap.get(portDeviceMap.get(port)).remove(port);
223 portDeviceMap.remove(port);
224 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800225
226 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700227 * Checks all groups in the src-device of link for neighbor sets that include
228 * the dst-device of link, and edits the hash groups according to link up
229 * or down. Should only be called by the master instance of the src-switch
230 * of link. Typically used when there are no route-path changes due to the
231 * link up or down, as the ECMPspg does not change.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800232 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700233 * @param link the infrastructure link that has gone down or come up
234 * @param linkDown true if link has gone down
235 * @param firstTime true if link has come up for the first time i.e a link
236 * not seen-before
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800237 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700238 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Das041bb782017-08-14 16:44:43 -0700239 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800240 try {
Saurav Das041bb782017-08-14 16:44:43 -0700241 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan0b4e6182015-11-03 10:42:14 -0800242 } catch (DeviceConfigNotFoundException e) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700243 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800244 return;
245 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700246 // find all the destinationSets related to link
247 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700248 .stream()
Saurav Das7bcbe702017-06-13 15:35:54 -0700249 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
250 .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId()))
251 .map(entry -> entry.getKey())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700252 .collect(Collectors.toSet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700253
Saurav Das7bcbe702017-06-13 15:35:54 -0700254 log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}",
255 deviceId, link.dst().deviceId(), dsKeySet);
256
257 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
258 NextNeighbors nextHops = dsNextObjStore.get(dsKey);
259 if (nextHops == null) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700260 log.warn("retryHash in device {}, but global store has no record "
Saurav Das7bcbe702017-06-13 15:35:54 -0700261 + "for dsKey:{}", deviceId, dsKey);
Saurav Dasc88d4662017-05-15 15:34:25 -0700262 continue;
263 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700264 int nextId = nextHops.nextId();
265 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -0700266 if (!linkDown) {
Saurav Das041bb782017-08-14 16:44:43 -0700267 List<PortLabel> pl = Lists.newArrayList();
Saurav Dasc88d4662017-05-15 15:34:25 -0700268 if (firstTime) {
269 // some links may have come up before the next-objective was created
270 // we take this opportunity to ensure other ports to same next-hop-dst
271 // are part of the hash group (see CORD-1180). Duplicate additions
272 // to the same hash group are avoided by the driver.
273 for (PortNumber p : devicePortMap.get(link.dst().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700274 dstSet.forEach(dst -> {
275 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Das041bb782017-08-14 16:44:43 -0700276 pl.add(new PortLabel(p, edgeLabel));
Saurav Das7bcbe702017-06-13 15:35:54 -0700277 });
Saurav Dasc88d4662017-05-15 15:34:25 -0700278 }
Saurav Das041bb782017-08-14 16:44:43 -0700279 addToHashedNextObjective(pl, neighborMac, nextId);
280 } else {
281 // handle only the port that came up
282 dstSet.forEach(dst -> {
283 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
284 pl.add(new PortLabel(link.src().port(), edgeLabel));
285 });
286 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700287 }
288 } else {
Saurav Das041bb782017-08-14 16:44:43 -0700289 // linkdown
290 List<PortLabel> pl = Lists.newArrayList();
Saurav Das7bcbe702017-06-13 15:35:54 -0700291 dstSet.forEach(dst -> {
292 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Das041bb782017-08-14 16:44:43 -0700293 pl.add(new PortLabel(link.src().port(), edgeLabel));
Saurav Das7bcbe702017-06-13 15:35:54 -0700294 });
Saurav Das041bb782017-08-14 16:44:43 -0700295 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700296 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800297 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700298 }
299
Saurav Dasc88d4662017-05-15 15:34:25 -0700300 /**
Saurav Das041bb782017-08-14 16:44:43 -0700301 * Utility class for associating output ports and the corresponding MPLS
302 * labels to push. In dual-homing, there are different labels to push
303 * corresponding to the destination switches in an edge-pair. If both
304 * destinations are reachable via the same spine, then the output-port to
305 * the spine will be associated with two labels i.e. there will be two
306 * PortLabel objects for the same port but with different labels.
307 */
308 private class PortLabel {
309 PortNumber port;
310 int edgeLabel;
311
312 PortLabel(PortNumber port, int edgeLabel) {
313 this.port = port;
314 this.edgeLabel = edgeLabel;
315 }
316
317 @Override
318 public String toString() {
319 return port.toString() + "/" + String.valueOf(edgeLabel);
320 }
321 }
322
323 /**
324 * Makes a call to the FlowObjective service to add buckets to
325 * a hashed group. User must ensure that all the ports & labels are meant
326 * same neighbor (ie. dstMac).
Saurav Dasc88d4662017-05-15 15:34:25 -0700327 *
Saurav Das041bb782017-08-14 16:44:43 -0700328 * @param portLables a collection of port & label combinations to add
329 * to the hash group identified by the nextId
Saurav Dasc88d4662017-05-15 15:34:25 -0700330 * @param dstMac destination mac address of next-hop
Saurav Das041bb782017-08-14 16:44:43 -0700331 * @param nextId id for next-objective to which buckets will be added
Saurav Dasceccf242017-08-03 18:30:35 -0700332 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700333 */
Saurav Das041bb782017-08-14 16:44:43 -0700334 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
335 MacAddress dstMac, Integer nextId) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700336 // setup metadata to pass to nextObjective - indicate the vlan on egress
337 // if needed by the switch pipeline. Since hashed next-hops are always to
338 // other neighboring routers, there is no subnet assigned on those ports.
339 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
340 metabuilder.matchVlanId(INTERNAL_VLAN);
Saurav Das1547b3f2017-05-05 17:01:08 -0700341 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
342 .withId(nextId)
343 .withType(NextObjective.Type.HASHED)
Saurav Das1547b3f2017-05-05 17:01:08 -0700344 .withMeta(metabuilder.build())
345 .fromApp(appId);
Saurav Das041bb782017-08-14 16:44:43 -0700346 // Create the new buckets to be updated
347 portLabels.forEach(pl -> {
348 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
349 tBuilder.setOutput(pl.port)
350 .setEthDst(dstMac)
351 .setEthSrc(nodeMacAddr);
352 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
353 tBuilder.pushMpls()
354 .copyTtlOut()
355 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
356 }
357 nextObjBuilder.addTreatment(tBuilder.build());
358 });
359
360 log.debug("addToHash in device {}: Adding Bucket with port/label {} "
361 + "to nextId {}", deviceId, portLabels, nextId);
Saurav Das1547b3f2017-05-05 17:01:08 -0700362
363 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Das041bb782017-08-14 16:44:43 -0700364 (objective) -> log.debug("addToHash port/label {} addedTo "
365 + "NextObj {} on {}", portLabels, nextId, deviceId),
Saurav Das1547b3f2017-05-05 17:01:08 -0700366 (objective, error) ->
Saurav Das041bb782017-08-14 16:44:43 -0700367 log.warn("addToHash failed to add port/label {} to"
368 + " NextObj {} on {}: {}", portLabels,
Saurav Dasc88d4662017-05-15 15:34:25 -0700369 nextId, deviceId, error));
Saurav Das1547b3f2017-05-05 17:01:08 -0700370 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
371 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800372 }
373
374 /**
Saurav Das041bb782017-08-14 16:44:43 -0700375 * Makes a call to the FlowObjective service to remove buckets from
376 * a hash group. User must ensure that all the ports & labels are meant
377 * same neighbor (ie. dstMac).
Saurav Dasceccf242017-08-03 18:30:35 -0700378 *
Saurav Das041bb782017-08-14 16:44:43 -0700379 * @param portLables a collection of port & label combinations to remove
380 * from the hash group identified by the nextId
Saurav Dasceccf242017-08-03 18:30:35 -0700381 * @param dstMac destination mac address of next-hop
Saurav Das041bb782017-08-14 16:44:43 -0700382 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasceccf242017-08-03 18:30:35 -0700383 */
Saurav Das041bb782017-08-14 16:44:43 -0700384 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
385 MacAddress dstMac, Integer nextId) {
Saurav Dasceccf242017-08-03 18:30:35 -0700386 NextObjective.Builder nextObjBuilder = DefaultNextObjective
387 .builder()
388 .withType(NextObjective.Type.HASHED) //same as original
389 .withId(nextId)
Saurav Das041bb782017-08-14 16:44:43 -0700390 .fromApp(appId);
391 // Create the buckets to be removed
392 portLabels.forEach(pl -> {
393 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
394 tBuilder.setOutput(pl.port)
395 .setEthDst(dstMac)
396 .setEthSrc(nodeMacAddr);
397 if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) {
398 tBuilder.pushMpls()
399 .copyTtlOut()
400 .setMpls(MplsLabel.mplsLabel(pl.edgeLabel));
401 }
402 nextObjBuilder.addTreatment(tBuilder.build());
403 });
404 log.debug("removeFromHash in device {}: Removing Bucket with port/label"
405 + " {} from nextId {}", deviceId, portLabels, nextId);
Saurav Dasc88d4662017-05-15 15:34:25 -0700406
Saurav Das041bb782017-08-14 16:44:43 -0700407 ObjectiveContext context = new DefaultObjectiveContext(
408 (objective) -> log.debug("port/label {} removedFrom NextObj"
409 + " {} on {}", portLabels, nextId, deviceId),
410 (objective, error) ->
411 log.warn("port/label {} failed to removeFrom NextObj {} on "
412 + "{}: {}", portLabels, nextId, deviceId, error));
413 NextObjective nextObjective = nextObjBuilder.removeFromExisting(context);
Saurav Dasceccf242017-08-03 18:30:35 -0700414 flowObjectiveService.next(deviceId, nextObjective);
415 }
Saurav Dasc88d4662017-05-15 15:34:25 -0700416
417 /**
418 * Checks all the hash-groups in the target-switch meant for the destination
419 * switch, and either adds or removes buckets to make the neighbor-set
420 * match the given next-hops. Typically called by the master instance of the
421 * destination switch, which may be different from the master instance of the
422 * target switch where hash-group changes are made.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800423 *
Saurav Dasc88d4662017-05-15 15:34:25 -0700424 * @param targetSw the switch in which the hash groups will be edited
425 * @param nextHops the current next hops for the target switch to reach
426 * the dest sw
427 * @param destSw the destination switch
428 * @param revoke true if hash groups need to remove buckets from the
429 * the groups to match the current next hops
430 * @return true if calls are made to edit buckets, or if no edits are required
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800431 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700432 public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops,
433 DeviceId destSw, boolean revoke) {
434 // temporary storage of keys to be updated
Saurav Das7bcbe702017-06-13 15:35:54 -0700435 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Dasc88d4662017-05-15 15:34:25 -0700436 new HashMap<>();
Saurav Das041bb782017-08-14 16:44:43 -0700437 boolean foundNextObjective = false, success = true;
Charles Chan0b4e6182015-11-03 10:42:14 -0800438
Saurav Das7bcbe702017-06-13 15:35:54 -0700439 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Dasc88d4662017-05-15 15:34:25 -0700440 // with different neighbors than the given next-hops
Saurav Das7bcbe702017-06-13 15:35:54 -0700441 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
442 if (!dskey.deviceId().equals(targetSw) ||
443 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700444 continue;
445 }
446 foundNextObjective = true;
Saurav Das7bcbe702017-06-13 15:35:54 -0700447 NextNeighbors nhops = dsNextObjStore.get(dskey);
448 Set<DeviceId> currNeighbors = nhops.nextHops(destSw);
449 int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw);
450 Integer nextId = nhops.nextId();
Charles Chan0b4e6182015-11-03 10:42:14 -0800451
Saurav Dasba7eb1a2017-12-13 16:19:35 -0800452 if (currNeighbors == null || nextHops == null) {
453 log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}"
454 + " in targetSw:{} for dstSw:{}", currNeighbors, nextHops,
455 targetSw, destSw);
456 success &= false;
457 continue;
458 }
459
Saurav Dasc88d4662017-05-15 15:34:25 -0700460 Set<DeviceId> diff;
461 if (revoke) {
462 diff = Sets.difference(currNeighbors, nextHops);
463 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
464 + "hops:{} ..removing {}", targetSw, destSw, nextId,
465 currNeighbors, diff);
466 } else {
467 diff = Sets.difference(nextHops, currNeighbors);
468 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
469 + "hops:{} ..adding {}", targetSw, destSw, nextId,
470 currNeighbors, diff);
471 }
Saurav Das041bb782017-08-14 16:44:43 -0700472 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId,
473 revoke);
474 if (suc) {
475 // to update neighbor set with changes made
Saurav Dasc88d4662017-05-15 15:34:25 -0700476 if (revoke) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700477 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700478 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -0700479 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Dasc88d4662017-05-15 15:34:25 -0700480 }
sangho834e4b02015-05-01 09:38:25 -0700481 }
Saurav Das041bb782017-08-14 16:44:43 -0700482 success &= suc;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800483 }
484
Saurav Dasc88d4662017-05-15 15:34:25 -0700485 if (!foundNextObjective) {
486 log.debug("Cannot find any nextObjectives for route targetSw:{} "
487 + "-> dstSw:{}", targetSw, destSw);
488 return true; // nothing to do, return true so ECMPspg is updated
489 }
490
Saurav Das7bcbe702017-06-13 15:35:54 -0700491 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
492 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Das041bb782017-08-14 16:44:43 -0700493 NextNeighbors currentNextHops = dsNextObjStore.get(key);
494 if (currentNextHops == null) {
495 log.warn("fixHashGroups could not update global store in "
496 + "device {} .. missing nextNeighbors for key {}",
497 deviceId, key);
Saurav Dasc88d4662017-05-15 15:34:25 -0700498 continue;
499 }
Saurav Das041bb782017-08-14 16:44:43 -0700500 Set<DeviceId> newNeighbors = new HashSet<>();
501 newNeighbors.addAll(tempStore.get(key));
502 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
503 ImmutableMap.copyOf(currentNextHops.dstNextHops());
504 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
505 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
506 targetSw, destSw, key, currentNextHops.nextId());
507 log.debug("Old dstNextHops: {}", oldDstNextHops);
508 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
509 // update global store
510 dsNextObjStore.put(key,
511 new NextNeighbors(currentNextHops.dstNextHops(),
512 currentNextHops.nextId()));
Saurav Dasc88d4662017-05-15 15:34:25 -0700513 }
Saurav Das041bb782017-08-14 16:44:43 -0700514 // even if one fails and others succeed, return false so ECMPspg not updated
515 return success;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800516 }
517
Saurav Dasceccf242017-08-03 18:30:35 -0700518 /**
519 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
520 * that are not already in the store for the given DestinationSet. Note that
521 * this method does not remove existing next hops for the destinations in the
522 * DestinationSet.
523 *
524 * @param ds the DestinationSet for which the next hops need to be updated
525 * @param newDstNextHops a map of per-destination next hops to update the
526 * destinationSet with
527 * @return true if successful in updating all next hops
528 */
529 private boolean updateNextHops(DestinationSet ds,
Saurav Das7bcbe702017-06-13 15:35:54 -0700530 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
531 DestinationSetNextObjectiveStoreKey key =
532 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
533 NextNeighbors currNext = dsNextObjStore.get(key);
534 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
535
536 // add newDstNextHops to currDstNextHops for each dst
537 boolean success = true;
538 for (DeviceId dstSw : ds.getDestinationSwitches()) {
539 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
540 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
541 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
542 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
543 int edgeLabel = ds.getEdgeLabel(dstSw);
544 int nextId = currNext.nextId();
545
546 // new next hops should be added
547 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
548 edgeLabel, nextId, false);
549 if (suc) {
550 currNhops.addAll(newNhops);
551 currDstNextHops.put(dstSw, currNhops); // this is only a local change
552 }
553 success &= suc;
554 }
555
556 if (success) {
557 // update global store
558 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
559 currNext.nextId()));
560 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
561 dsNextObjStore.get(key));
562 }
563 return success;
564 }
565
Saurav Dasceccf242017-08-03 18:30:35 -0700566 /**
Saurav Das041bb782017-08-14 16:44:43 -0700567 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
568 * needs to ensure that the given neighbors are all next hops towards the
569 * same destination (represented by the given edgeLabel).
Saurav Dasceccf242017-08-03 18:30:35 -0700570 *
571 * @param neighbors set of neighbor device ids
572 * @param edgeLabel MPLS label to use in buckets
573 * @param nextId the nextObjective to change
574 * @param revoke true if buckets need to be removed, false if they need to
575 * be added
576 * @return true if successful in adding or removing buckets for all ports
577 * to the neighbors
578 */
579 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Saurav Das7bcbe702017-06-13 15:35:54 -0700580 int nextId, boolean revoke) {
Saurav Dasceccf242017-08-03 18:30:35 -0700581 for (DeviceId neighbor : neighbors) {
Saurav Das041bb782017-08-14 16:44:43 -0700582 MacAddress neighborMac;
Saurav Das7bcbe702017-06-13 15:35:54 -0700583 try {
Saurav Das041bb782017-08-14 16:44:43 -0700584 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das7bcbe702017-06-13 15:35:54 -0700585 } catch (DeviceConfigNotFoundException e) {
Saurav Das041bb782017-08-14 16:44:43 -0700586 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
587 + " for nextId:" + nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700588 return false;
589 }
Saurav Das041bb782017-08-14 16:44:43 -0700590 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
591 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700592 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Das041bb782017-08-14 16:44:43 -0700593 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das7bcbe702017-06-13 15:35:54 -0700594 deviceId, neighbor, nextId);
595 return false;
596 }
Saurav Das041bb782017-08-14 16:44:43 -0700597 List<PortLabel> pl = Lists.newArrayList();
598 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel)));
Saurav Das7bcbe702017-06-13 15:35:54 -0700599 if (revoke) {
Saurav Das041bb782017-08-14 16:44:43 -0700600 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
601 + "with Port/Label:{} to next object id {}",
602 deviceId, pl, nextId);
603 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700604 } else {
Saurav Das041bb782017-08-14 16:44:43 -0700605 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
606 + "with Port/Label: {} to next object id {}",
607 deviceId, pl, nextId);
608 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das7bcbe702017-06-13 15:35:54 -0700609 }
610 }
611 return true;
612 }
613
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800614 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800615 * Adds or removes a port that has been configured with a vlan to a broadcast group
616 * for bridging. Should only be called by the master instance for this device.
Saurav Das1a129a02016-11-18 15:21:57 -0800617 *
618 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800619 * @param vlanId the vlan id corresponding to the broadcast domain/group
620 * @param popVlan indicates if packets should be sent out untagged or not out
621 * of the port. If true, indicates an access (untagged) or native vlan
622 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Das1a129a02016-11-18 15:21:57 -0800623 * @param portUp true if port is enabled, false if disabled
Saurav Das1a129a02016-11-18 15:21:57 -0800624 */
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800625 public void processEdgePort(PortNumber port, VlanId vlanId,
626 boolean popVlan, boolean portUp) {
Saurav Das1a129a02016-11-18 15:21:57 -0800627 //get the next id for the subnet and edit it.
Charles Chan59cc16d2017-02-02 16:20:42 -0800628 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800629 if (nextId == -1) {
630 if (portUp) {
631 log.debug("**Creating flooding group for first port enabled in"
Saurav Das9aa28bd2017-12-05 15:00:23 -0800632 + " vlan {} on dev {} port {}", vlanId, deviceId, port);
Charles Chan59cc16d2017-02-02 16:20:42 -0800633 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Das1a129a02016-11-18 15:21:57 -0800634 } else {
635 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan59cc16d2017-02-02 16:20:42 -0800636 + " removing port:{}", vlanId, deviceId, port);
Saurav Das1a129a02016-11-18 15:21:57 -0800637 }
638 return;
639 }
640
641 log.info("**port{} in device {}: {} Bucket with Port {} to"
642 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
643 (portUp) ? "Adding" : "Removing",
644 port, nextId);
645 // Create the bucket to be added or removed
646 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800647 if (popVlan) {
648 tBuilder.popVlan();
649 }
Saurav Das1a129a02016-11-18 15:21:57 -0800650 tBuilder.setOutput(port);
651
Saurav Das1a129a02016-11-18 15:21:57 -0800652 TrafficSelector metadata =
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800653 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Das1a129a02016-11-18 15:21:57 -0800654
655 NextObjective.Builder nextObjBuilder = DefaultNextObjective
656 .builder().withId(nextId)
657 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
658 .addTreatment(tBuilder.build())
659 .withMeta(metadata);
660
661 ObjectiveContext context = new DefaultObjectiveContext(
662 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
663 port, (portUp) ? "addedTo" : "removedFrom",
664 nextId, deviceId),
665 (objective, error) ->
666 log.warn("port {} failed to {} NextObj {} on {}: {}",
667 port, (portUp) ? "addTo" : "removeFrom",
668 nextId, deviceId, error));
669
670 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
671 : nextObjBuilder.removeFromExisting(context);
672 log.debug("edgePort processed: Submited next objective {} in device {}",
673 nextId, deviceId);
674 flowObjectiveService.next(deviceId, nextObj);
675 }
676
677 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700678 * Returns the next objective of type hashed associated with the destination set.
679 * In addition, updates the existing next-objective if new route-route paths found
680 * have resulted in the addition of new next-hops to a particular destination.
681 * If there is no existing next objective for this destination set, this method
682 * would create a next objective and return the nextId. Optionally metadata can be
Saurav Das8a0732e2015-11-20 15:27:53 -0800683 * passed in for the creation of the next objective.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800684 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700685 * @param ds destination set
686 * @param nextHops a map of per destination next hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800687 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700688 * @param isBos if Bos is set
Saurav Das8a0732e2015-11-20 15:27:53 -0800689 * @return int if found or -1 if there are errors in the creation of the
690 * neighbor set.
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800691 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700692 public int getNextObjectiveId(DestinationSet ds,
693 Map<DeviceId, Set<DeviceId>> nextHops,
694 TrafficSelector meta, boolean isBos) {
695 NextNeighbors next = dsNextObjStore.
696 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
697 if (next == null) {
698 log.debug("getNextObjectiveId in device{}: Next objective id "
699 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700700 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
701 deviceId,
Saurav Das7bcbe702017-06-13 15:35:54 -0700702 dsNextObjStore.entrySet()
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700703 .stream()
704 .filter((nsStoreEntry) ->
705 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
706 .collect(Collectors.toList()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700707
708 createGroupFromDestinationSet(ds, nextHops, meta, isBos);
709 next = dsNextObjStore.
710 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
711 if (next == null) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700712 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das7bcbe702017-06-13 15:35:54 -0700713 // failure in creating group
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700714 return -1;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700715 } else {
716 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700717 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700718 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700719 } else {
720 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das7bcbe702017-06-13 15:35:54 -0700721 + "found for {}", deviceId, next.nextId(), ds);
722 // should fix hash groups too if next-hops have changed
723 if (!next.dstNextHops().equals(nextHops)) {
724 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
725 deviceId, next.nextId());
726 if (!updateNextHops(ds, nextHops)) {
727 // failure in updating group
728 return -1;
729 }
730 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700731 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700732 return next.nextId();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800733 }
734
sangho0b2b6d12015-05-20 22:16:38 -0700735 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800736 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das4ce45962015-11-24 23:21:05 -0800737 * or -1 if no such objective exists. Note that this method does NOT create
738 * the next objective as a side-effect. It is expected that is objective is
Saurav Das1a129a02016-11-18 15:21:57 -0800739 * created at startup from network configuration. Typically this is used
740 * for L2 flooding within the subnet configured on the switch.
Charles Chanc42e84e2015-10-20 16:24:19 -0700741 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800742 * @param vlanId vlan id
Charles Chanc42e84e2015-10-20 16:24:19 -0700743 * @return int if found or -1
744 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800745 public int getVlanNextObjectiveId(VlanId vlanId) {
746 Integer nextId = vlanNextObjStore.
747 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chan9f676b62015-10-29 14:58:10 -0700748
749 return (nextId != null) ? nextId : -1;
Charles Chanc42e84e2015-10-20 16:24:19 -0700750 }
751
752 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800753 * Returns the next objective of type simple associated with the port on the
754 * device, given the treatment. Different treatments to the same port result
755 * in different next objectives. If no such objective exists, this method
Saurav Das961beb22017-03-29 19:09:17 -0700756 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Das1a129a02016-11-18 15:21:57 -0800757 * the creation of the objective. Typically this is used for L2 and L3 forwarding
758 * to compute nodes and containers/VMs on the compute nodes directly attached
759 * to the switch.
Saurav Das4ce45962015-11-24 23:21:05 -0800760 *
761 * @param portNum the port number for the simple next objective
762 * @param treatment the actions to apply on the packets (should include outport)
763 * @param meta optional metadata passed into the creation of the next objective
Saurav Das961beb22017-03-29 19:09:17 -0700764 * @param createIfMissing true if a next object should be created if not found
Saurav Das4ce45962015-11-24 23:21:05 -0800765 * @return int if found or created, -1 if there are errors during the
766 * creation of the next objective.
767 */
768 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700769 TrafficSelector meta, boolean createIfMissing) {
Charles Chane849c192016-01-11 18:28:54 -0800770 Integer nextId = portNextObjStore
Saurav Das76ae6812017-03-15 15:15:14 -0700771 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das961beb22017-03-29 19:09:17 -0700772 if (nextId != null) {
773 return nextId;
774 }
775 log.debug("getPortNextObjectiveId in device {}: Next objective id "
776 + "not found for port: {} .. {}", deviceId, portNum,
777 (createIfMissing) ? "creating" : "aborting");
778 if (!createIfMissing) {
779 return -1;
780 }
781 // create missing next objective
782 createGroupFromPort(portNum, treatment, meta);
783 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
784 treatment, meta));
Saurav Das4ce45962015-11-24 23:21:05 -0800785 if (nextId == null) {
Saurav Das961beb22017-03-29 19:09:17 -0700786 log.warn("getPortNextObjectiveId: unable to create next obj"
787 + "for dev:{} port:{}", deviceId, portNum);
788 return -1;
Charles Chane849c192016-01-11 18:28:54 -0800789 }
790 return nextId;
791 }
792
793 /**
sangho0b2b6d12015-05-20 22:16:38 -0700794 * Checks if the next objective ID (group) for the neighbor set exists or not.
795 *
796 * @param ns neighbor set to check
797 * @return true if it exists, false otherwise
798 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700799 public boolean hasNextObjectiveId(DestinationSet ns) {
800 NextNeighbors nextHops = dsNextObjStore.
801 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
802 if (nextHops == null) {
sangho0b2b6d12015-05-20 22:16:38 -0700803 return false;
804 }
805
806 return true;
807 }
808
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800809 private void populateNeighborMaps() {
810 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700811 for (Link link : outgoingLinks) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800812 if (link.type() != Link.Type.DIRECT) {
813 continue;
814 }
815 addNeighborAtPort(link.dst().deviceId(), link.src().port());
816 }
817 }
818
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700819 protected void addNeighborAtPort(DeviceId neighborId,
820 PortNumber portToNeighbor) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800821 // Update DeviceToPort database
822 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
823 deviceId, neighborId, portToNeighbor);
Saurav Das8a0732e2015-11-20 15:27:53 -0800824 Set<PortNumber> ports = Collections
825 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
826 ports.add(portToNeighbor);
827 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
828 if (portnums != null) {
829 portnums.add(portToNeighbor);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800830 }
831
832 // Update portToDevice database
Saurav Das8a0732e2015-11-20 15:27:53 -0800833 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
834 if (prev != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700835 log.debug("Device: {} port: {} already has neighbor: {} ",
836 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800837 }
838 }
839
sangho1e575652015-05-14 00:39:53 -0700840 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700841 * Creates a NextObjective for a hash group in this device from a given
842 * DestinationSet.
sangho1e575652015-05-14 00:39:53 -0700843 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700844 * @param ds the DestinationSet
845 * @param neighbors a map for each destination and its next-hops
Saurav Das8a0732e2015-11-20 15:27:53 -0800846 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700847 * @param isBos if BoS is set
sangho1e575652015-05-14 00:39:53 -0700848 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700849 public void createGroupFromDestinationSet(DestinationSet ds,
850 Map<DeviceId, Set<DeviceId>> neighbors,
851 TrafficSelector meta,
852 boolean isBos) {
853 int nextId = flowObjectiveService.allocateNextId();
854 NextObjective.Type type = NextObjective.Type.HASHED;
855 if (neighbors == null || neighbors.isEmpty()) {
856 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
857 + "to create group in dev:{} for ds: {} with next-hops {}",
858 deviceId, ds, neighbors);
859 return;
860 }
861 // If Bos == False and MPLS-ECMP == false, we have
862 // to use simple group and we will pick a single neighbor for a single dest.
863 if (!isBos && !srManager.getMplsEcmp()) {
864 type = NextObjective.Type.SIMPLE;
865 }
866
867 NextObjective.Builder nextObjBuilder = DefaultNextObjective
868 .builder()
869 .withId(nextId)
870 .withType(type)
871 .fromApp(appId);
872 if (meta != null) {
873 nextObjBuilder.withMeta(meta);
874 }
875
876 // create treatment buckets for each neighbor for each dst Device
877 // except in the special case where we only want to pick a single
878 // neighbor for a simple group
879 boolean foundSingleNeighbor = false;
880 boolean treatmentAdded = false;
881 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
882 for (DeviceId dst : ds.getDestinationSwitches()) {
883 Set<DeviceId> nextHops = neighbors.get(dst);
884 if (nextHops == null || nextHops.isEmpty()) {
885 continue;
Pier Ventre917127a2016-10-31 16:49:19 -0700886 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700887
888 if (foundSingleNeighbor) {
889 break;
890 }
891
892 for (DeviceId neighborId : nextHops) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800893 if (devicePortMap.get(neighborId) == null) {
894 log.warn("Neighbor {} is not in the port map yet for dev:{}",
895 neighborId, deviceId);
sangho834e4b02015-05-01 09:38:25 -0700896 return;
Jon Hallcbd1b392017-01-18 20:15:44 -0800897 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700898 log.warn("There are no ports for "
Saurav Das8a0732e2015-11-20 15:27:53 -0800899 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700900 return;
sangho834e4b02015-05-01 09:38:25 -0700901 }
902
Saurav Das8a0732e2015-11-20 15:27:53 -0800903 MacAddress neighborMac;
Charles Chan0b4e6182015-11-03 10:42:14 -0800904 try {
Saurav Das8a0732e2015-11-20 15:27:53 -0800905 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan0b4e6182015-11-03 10:42:14 -0800906 } catch (DeviceConfigNotFoundException e) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700907 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan0b4e6182015-11-03 10:42:14 -0800908 return;
909 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700910 // For each port to the neighbor, we create a new treatment
Pier Ventre917127a2016-10-31 16:49:19 -0700911 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
912 // In this case we are using a SIMPLE group. We randomly pick a port
913 if (!isBos && !srManager.getMplsEcmp()) {
914 int size = devicePortMap.get(neighborId).size();
915 int index = RandomUtils.nextInt(0, size);
916 neighborPorts = Collections.singleton(
Saurav Das7bcbe702017-06-13 15:35:54 -0700917 Iterables.get(devicePortMap.get(neighborId),
918 index));
919 foundSingleNeighbor = true;
Pier Ventre917127a2016-10-31 16:49:19 -0700920 }
921 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700922 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
923 .builder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700924 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
925 int edgeLabel = ds.getEdgeLabel(dst);
926 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Charles Chan68aa62d2015-11-09 16:37:23 -0800927 tBuilder.pushMpls()
Saurav Das7bcbe702017-06-13 15:35:54 -0700928 .copyTtlOut()
929 .setMpls(MplsLabel.mplsLabel(edgeLabel));
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700930 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800931 tBuilder.setOutput(sp);
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700932 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das7bcbe702017-06-13 15:35:54 -0700933 treatmentAdded = true;
934 //update store
935 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
936 if (existingNeighbors == null) {
937 existingNeighbors = new HashSet<>();
938 }
939 existingNeighbors.add(neighborId);
940 dstNextHops.put(dst, existingNeighbors);
941 log.debug("creating treatment for port/label {}/{} in next:{}",
942 sp, edgeLabel, nextId);
943 }
944
945 if (foundSingleNeighbor) {
946 break;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800947 }
948 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800949 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700950
951 if (!treatmentAdded) {
952 log.warn("Could not createGroup from DestinationSet {} without any"
953 + "next hops {}", ds, neighbors);
954 return;
955 }
956 ObjectiveContext context = new DefaultObjectiveContext(
957 (objective) ->
958 log.debug("createGroupsFromDestinationSet installed "
959 + "NextObj {} on {}", nextId, deviceId),
960 (objective, error) ->
961 log.warn("createGroupsFromDestinationSet failed to install"
962 + " NextObj {} on {}: {}", nextId, deviceId, error)
963 );
964 NextObjective nextObj = nextObjBuilder.add(context);
965 log.debug(".. createGroupsFromDestinationSet: Submitted "
966 + "next objective {} in device {}", nextId, deviceId);
967 flowObjectiveService.next(deviceId, nextObj);
968 //update store
969 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
970 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800971 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700972
Saurav Das4ce45962015-11-24 23:21:05 -0800973 /**
Saurav Das1a129a02016-11-18 15:21:57 -0800974 * Creates broadcast groups for all ports in the same subnet for
975 * all configured subnets.
Saurav Das4ce45962015-11-24 23:21:05 -0800976 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800977 public void createGroupsFromVlanConfig() {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800978 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800979 createBcastGroupFromVlan(vlanId, ports);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800980 });
Saurav Das1a129a02016-11-18 15:21:57 -0800981 }
Charles Chan9f676b62015-10-29 14:58:10 -0700982
Saurav Das1a129a02016-11-18 15:21:57 -0800983 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800984 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Das1a129a02016-11-18 15:21:57 -0800985 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800986 * @param vlanId vlan id
Saurav Das1a129a02016-11-18 15:21:57 -0800987 * @param ports list of ports in the subnet
988 */
Charles Chan7ffd81f2017-02-08 15:52:08 -0800989 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800990 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chan9f676b62015-10-29 14:58:10 -0700991
Charles Chan59cc16d2017-02-02 16:20:42 -0800992 if (vlanNextObjStore.containsKey(key)) {
Saurav Das1a129a02016-11-18 15:21:57 -0800993 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan59cc16d2017-02-02 16:20:42 -0800994 deviceId, vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -0800995 return;
996 }
Charles Chan188ebf52015-12-23 00:15:11 -0800997
Saurav Das1a129a02016-11-18 15:21:57 -0800998 TrafficSelector metadata =
Charles Chan59cc16d2017-02-02 16:20:42 -0800999 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chanc42e84e2015-10-20 16:24:19 -07001000
Saurav Das1a129a02016-11-18 15:21:57 -08001001 int nextId = flowObjectiveService.allocateNextId();
Charles Chanc42e84e2015-10-20 16:24:19 -07001002
Saurav Das1a129a02016-11-18 15:21:57 -08001003 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1004 .builder().withId(nextId)
1005 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1006 .withMeta(metadata);
Charles Chanc42e84e2015-10-20 16:24:19 -07001007
Saurav Das1a129a02016-11-18 15:21:57 -08001008 ports.forEach(port -> {
1009 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan7ffd81f2017-02-08 15:52:08 -08001010 if (toPopVlan(port, vlanId)) {
1011 tBuilder.popVlan();
1012 }
Saurav Das1a129a02016-11-18 15:21:57 -08001013 tBuilder.setOutput(port);
1014 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chanc42e84e2015-10-20 16:24:19 -07001015 });
Saurav Das1a129a02016-11-18 15:21:57 -08001016
Saurav Das961beb22017-03-29 19:09:17 -07001017 ObjectiveContext context = new DefaultObjectiveContext(
1018 (objective) ->
1019 log.debug("createBroadcastGroupFromVlan installed "
1020 + "NextObj {} on {}", nextId, deviceId),
1021 (objective, error) ->
1022 log.warn("createBroadcastGroupFromVlan failed to install"
1023 + " NextObj {} on {}: {}", nextId, deviceId, error)
1024 );
1025 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das1a129a02016-11-18 15:21:57 -08001026 flowObjectiveService.next(deviceId, nextObj);
Saurav Das9aa28bd2017-12-05 15:00:23 -08001027 log.debug("createBcastGroupFromVlan: Submitted next objective {} "
1028 + "for vlan: {} in device {}", nextId, vlanId, deviceId);
Saurav Das1a129a02016-11-18 15:21:57 -08001029
Charles Chan59cc16d2017-02-02 16:20:42 -08001030 vlanNextObjStore.put(key, nextId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001031 }
1032
Charles Chane849c192016-01-11 18:28:54 -08001033 /**
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001034 * Removes a single broadcast group from a given vlan id.
1035 * The group should be empty.
1036 * @param deviceId device Id to remove the group
1037 * @param portNum port number related to the group
1038 * @param vlanId vlan id of the broadcast group to remove
1039 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1040 */
1041 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1042 VlanId vlanId, boolean popVlan) {
1043 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1044
1045 if (!vlanNextObjStore.containsKey(key)) {
1046 log.debug("Broadcast group for device {} and subnet {} does not exist",
1047 deviceId, vlanId);
1048 return;
1049 }
1050
1051 TrafficSelector metadata =
1052 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1053
1054 int nextId = vlanNextObjStore.get(key);
1055
1056 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1057 .builder().withId(nextId)
1058 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1059 .withMeta(metadata);
1060
1061 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1062 if (popVlan) {
1063 tBuilder.popVlan();
1064 }
1065 tBuilder.setOutput(portNum);
1066 nextObjBuilder.addTreatment(tBuilder.build());
1067
1068 ObjectiveContext context = new DefaultObjectiveContext(
1069 (objective) ->
1070 log.debug("removeBroadcastGroupFromVlan removed "
1071 + "NextObj {} on {}", nextId, deviceId),
1072 (objective, error) ->
1073 log.warn("removeBroadcastGroupFromVlan failed to remove "
1074 + " NextObj {} on {}: {}", nextId, deviceId, error)
1075 );
1076 NextObjective nextObj = nextObjBuilder.remove(context);
1077 flowObjectiveService.next(deviceId, nextObj);
1078 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1079 nextId, deviceId);
1080
1081 vlanNextObjStore.remove(key, nextId);
1082 }
1083
1084 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001085 * Determine if we should pop given vlan before sending packets to the given port.
1086 *
1087 * @param portNumber port number
1088 * @param vlanId vlan id
1089 * @return true if the vlan id is not contained in any vlanTagged config
1090 */
1091 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001092 return srManager.interfaceService
1093 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan7ffd81f2017-02-08 15:52:08 -08001094 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1095 }
1096
1097 /**
Saurav Das4ce45962015-11-24 23:21:05 -08001098 * Create simple next objective for a single port. The treatments can include
1099 * all outgoing actions that need to happen on the packet.
1100 *
1101 * @param portNum the outgoing port on the device
1102 * @param treatment the actions to apply on the packets (should include outport)
1103 * @param meta optional data to pass to the driver
1104 */
1105 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1106 TrafficSelector meta) {
1107 int nextId = flowObjectiveService.allocateNextId();
1108 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das76ae6812017-03-15 15:15:14 -07001109 deviceId, portNum, treatment, meta);
Saurav Das4ce45962015-11-24 23:21:05 -08001110
1111 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1112 .builder().withId(nextId)
1113 .withType(NextObjective.Type.SIMPLE)
1114 .addTreatment(treatment)
1115 .fromApp(appId)
1116 .withMeta(meta);
1117
Saurav Das961beb22017-03-29 19:09:17 -07001118 ObjectiveContext context = new DefaultObjectiveContext(
1119 (objective) ->
1120 log.debug("createGroupFromPort installed "
1121 + "NextObj {} on {}", nextId, deviceId),
1122 (objective, error) ->
1123 log.warn("createGroupFromPort failed to install"
1124 + " NextObj {} on {}: {}", nextId, deviceId, error)
1125 );
1126 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das4ce45962015-11-24 23:21:05 -08001127 flowObjectiveService.next(deviceId, nextObj);
1128 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1129 + "for port {}", nextId, deviceId, portNum);
1130
1131 portNextObjStore.put(key, nextId);
1132 }
1133
sangho1e575652015-05-14 00:39:53 -07001134 /**
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001135 * Removes simple next objective for a single port.
1136 *
1137 * @param deviceId device id that has the port to deal with
1138 * @param portNum the outgoing port on the device
1139 * @param vlanId vlan id associated with the port
1140 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1141 */
1142 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1143 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1144 mbuilder.matchVlanId(vlanId);
1145
1146 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1147 tbuilder.immediate().setOutput(portNum);
1148 if (popVlan) {
1149 tbuilder.immediate().popVlan();
1150 }
1151
1152 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1153 tbuilder.build(), mbuilder.build(), false);
1154
1155 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1156 deviceId, portNum, tbuilder.build(), mbuilder.build());
1157 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1158 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1159 .builder().withId(portNextObjId)
1160 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1161 ObjectiveContext context = new DefaultObjectiveContext(
1162 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1163 portNextObjId, deviceId),
1164 (objective, error) ->
1165 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1166 portNextObjId, deviceId, error));
1167 NextObjective nextObjective = nextObjBuilder.remove(context);
1168 log.info("**removePortNextObjective: Submitted "
1169 + "next objective {} in device {}",
1170 portNextObjId, deviceId);
1171 flowObjectiveService.next(deviceId, nextObjective);
1172
1173 portNextObjStore.remove(key);
1174 }
1175 }
1176 /**
sangho1e575652015-05-14 00:39:53 -07001177 * Removes groups for the next objective ID given.
1178 *
1179 * @param objectiveId next objective ID to remove
1180 * @return true if succeeds, false otherwise
1181 */
1182 public boolean removeGroup(int objectiveId) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001183 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1184 dsNextObjStore.entrySet()) {
1185 if (e.getValue().nextId() != objectiveId) {
1186 continue;
1187 }
sangho1e575652015-05-14 00:39:53 -07001188 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1189 .builder().withId(objectiveId)
1190 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chan216e3c82016-04-23 14:48:16 -07001191 ObjectiveContext context = new DefaultObjectiveContext(
1192 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1193 objectiveId, deviceId),
1194 (objective, error) ->
1195 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
1196 objectiveId, deviceId, error));
1197 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das8a0732e2015-11-20 15:27:53 -08001198 log.info("**removeGroup: Submited "
1199 + "next objective {} in device {}",
1200 objectiveId, deviceId);
sangho1e575652015-05-14 00:39:53 -07001201 flowObjectiveService.next(deviceId, nextObjective);
1202
Saurav Das7bcbe702017-06-13 15:35:54 -07001203 dsNextObjStore.remove(e.getKey());
sangho0b2b6d12015-05-20 22:16:38 -07001204 return true;
sangho1e575652015-05-14 00:39:53 -07001205 }
1206
1207 return false;
1208 }
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001209 /**
1210 * Remove simple next objective for a single port. The treatments can include
1211 * all outgoing actions that need to happen on the packet.
1212 *
1213 * @param portNum the outgoing port on the device
1214 * @param treatment the actions applied on the packets (should include outport)
1215 * @param meta optional data to pass to the driver
1216 */
1217 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1218 TrafficSelector meta) {
1219 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1220 deviceId, portNum, treatment, meta);
1221 Integer nextId = portNextObjStore.get(key);
1222
1223 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1224 .builder().withId(nextId)
1225 .withType(NextObjective.Type.SIMPLE)
1226 .addTreatment(treatment)
1227 .fromApp(appId)
1228 .withMeta(meta);
1229
1230 ObjectiveContext context = new DefaultObjectiveContext(
1231 (objective) ->
1232 log.info("removeGroupFromPort installed "
1233 + "NextObj {} on {}", nextId, deviceId),
1234 (objective, error) ->
1235 log.warn("removeGroupFromPort failed to install"
1236 + " NextObj {} on {}: {}", nextId, deviceId, error)
1237 );
1238 NextObjective nextObj = nextObjBuilder.remove(context);
1239 flowObjectiveService.next(deviceId, nextObj);
1240 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1241 + "for port {}", nextId, deviceId, portNum);
1242
1243 portNextObjStore.remove(key);
1244 }
Srikanth Vavilapallif3a8bc02015-05-22 13:47:31 -07001245
Charles Chane849c192016-01-11 18:28:54 -08001246 /**
1247 * Removes all groups from all next objective stores.
1248 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001249 /*public void removeAllGroups() {
1250 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das423fe2b2015-12-04 10:52:59 -08001251 nsNextObjStore.entrySet()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001252 removeGroup(entry.getValue().nextId());
Saurav Das423fe2b2015-12-04 10:52:59 -08001253 }
1254 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1255 portNextObjStore.entrySet()) {
1256 removeGroup(entry.getValue());
1257 }
Charles Chan59cc16d2017-02-02 16:20:42 -08001258 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1259 vlanNextObjStore.entrySet()) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001260 removeGroup(entry.getValue());
1261 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001262 }*/ //XXX revisit
1263
Saurav Dasceccf242017-08-03 18:30:35 -07001264 /**
1265 * Triggers a one time bucket verification operation on all hash groups
1266 * on this device.
1267 */
1268 public void triggerBucketCorrector() {
1269 BucketCorrector bc = new BucketCorrector();
1270 bc.run();
1271 }
1272
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001273 public void updateGroupFromVlanConfiguration(PortNumber portNumber, Collection<VlanId> vlanIds,
1274 int nextId, boolean install) {
1275 vlanIds.forEach(vlanId -> updateGroupFromVlanInternal(vlanId, portNumber, nextId, install));
1276 }
1277
1278 private void updateGroupFromVlanInternal(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
1279 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1280 if (toPopVlan(portNum, vlanId)) {
1281 tBuilder.popVlan();
1282 }
1283 tBuilder.setOutput(portNum);
1284
1285 TrafficSelector metadata =
1286 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1287
1288 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1289 .builder().withId(nextId)
1290 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1291 .addTreatment(tBuilder.build())
1292 .withMeta(metadata);
1293
1294 ObjectiveContext context = new DefaultObjectiveContext(
1295 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1296 portNum, nextId, deviceId),
1297 (objective, error) ->
1298 log.warn("port {} failed to removedFrom NextObj {} on {}: {}",
1299 portNum, nextId, deviceId, error));
1300
1301 if (install) {
1302 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1303 } else {
1304 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1305 }
1306 }
Saurav Das1547b3f2017-05-05 17:01:08 -07001307
1308 /**
Saurav Das041bb782017-08-14 16:44:43 -07001309 * Performs bucket verification operation for all hash groups in this device.
1310 * Checks RouteHandler to ensure that routing is stable before attempting
1311 * verification. Verification involves creating a nextObjective with
1312 * operation VERIFY for existing next objectives in the store, and passing
1313 * it to the driver. It is the driver that actually performs the verification
1314 * by adding or removing buckets to match the verification next objective
1315 * created here.
Saurav Das1547b3f2017-05-05 17:01:08 -07001316 */
Saurav Dasceccf242017-08-03 18:30:35 -07001317 protected final class BucketCorrector implements Runnable {
1318 Integer nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001319
Saurav Dasceccf242017-08-03 18:30:35 -07001320 BucketCorrector() {
1321 this.nextId = null;
1322 }
1323
1324 BucketCorrector(Integer nextId) {
1325 this.nextId = nextId;
Saurav Das1547b3f2017-05-05 17:01:08 -07001326 }
1327
1328 @Override
1329 public void run() {
Saurav Dasceccf242017-08-03 18:30:35 -07001330 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1331 return;
Saurav Das1547b3f2017-05-05 17:01:08 -07001332 }
Saurav Dasceccf242017-08-03 18:30:35 -07001333 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1334 if (rh == null) {
1335 return;
1336 }
1337 if (!rh.isRoutingStable()) {
1338 return;
1339 }
1340 rh.acquireRoutingLock();
1341 try {
Saurav Das041bb782017-08-14 16:44:43 -07001342 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasceccf242017-08-03 18:30:35 -07001343 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1344 .stream()
1345 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
1346 .map(entry -> entry.getKey())
1347 .collect(Collectors.toSet());
1348 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1349 NextNeighbors next = dsNextObjStore.get(dsKey);
1350 if (next == null) {
1351 continue;
1352 }
1353 int nid = next.nextId();
1354 if (nextId != null && nextId != nid) {
1355 continue;
1356 }
Saurav Das041bb782017-08-14 16:44:43 -07001357 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasceccf242017-08-03 18:30:35 -07001358 deviceId, dsKey, next);
1359 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
1360 metabuilder.matchVlanId(INTERNAL_VLAN);
1361 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1362 .withId(nid)
1363 .withType(NextObjective.Type.HASHED)
1364 .withMeta(metabuilder.build())
1365 .fromApp(appId);
1366
1367 next.dstNextHops().forEach((dstDev, nextHops) -> {
1368 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1369 nextHops.forEach(neighbor -> {
1370 MacAddress neighborMac;
1371 try {
1372 neighborMac = deviceConfig.getDeviceMac(neighbor);
1373 } catch (DeviceConfigNotFoundException e) {
1374 log.warn(e.getMessage() + " Aborting neighbor"
1375 + neighbor);
1376 return;
1377 }
1378 devicePortMap.get(neighbor).forEach(port -> {
Saurav Das041bb782017-08-14 16:44:43 -07001379 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasceccf242017-08-03 18:30:35 -07001380 + " port/label {}/{} to dst {} via {}",
1381 deviceId, nid, port, edgeLabel,
1382 dstDev, neighbor);
1383 nextObjBuilder.addTreatment(treatmentBuilder(port,
1384 neighborMac, edgeLabel));
1385 });
1386 });
1387 });
1388
1389 NextObjective nextObjective = nextObjBuilder.verify();
1390 flowObjectiveService.next(deviceId, nextObjective);
1391 }
1392 } finally {
1393 rh.releaseRoutingLock();
1394 }
1395
1396 }
1397
1398 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
1399 int edgeLabel) {
1400 TrafficTreatment.Builder tBuilder =
1401 DefaultTrafficTreatment.builder();
1402 tBuilder.setOutput(outport)
1403 .setEthDst(dstMac)
1404 .setEthSrc(nodeMacAddr);
1405 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
1406 tBuilder.pushMpls()
1407 .copyTtlOut()
1408 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1409 }
1410 return tBuilder.build();
Saurav Das1547b3f2017-05-05 17:01:08 -07001411 }
1412 }
1413
Saurav Dasceccf242017-08-03 18:30:35 -07001414}