blob: 848e2cab65a9c1c144bba27f843149642e18bb4d [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 {
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -070078 protected 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 Das62ae6792017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700194 */
Saurav Das62ae6792017-05-15 15:34:25 -0700195 public void portUpForLink(Link link) {
Saurav Dasfbe74572017-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 Das62ae6792017-05-15 15:34:25 -0700201
Saurav Dasfbe74572017-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 Das62ae6792017-05-15 15:34:25 -0700208
Saurav Dasfbe74572017-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 Das62ae6792017-05-15 15:34:25 -0700219
Saurav Dasfbe74572017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700225
226 /**
Saurav Das62ae6792017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700232 *
Saurav Das62ae6792017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700237 */
Saurav Das62ae6792017-05-15 15:34:25 -0700238 public void retryHash(Link link, boolean linkDown, boolean firstTime) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700239 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -0800240 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700241 neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId());
Charles Chan319d1a22015-11-03 10:42:14 -0800242 } catch (DeviceConfigNotFoundException e) {
Saurav Das62ae6792017-05-15 15:34:25 -0700243 log.warn(e.getMessage() + " Aborting retryHash.");
Charles Chan319d1a22015-11-03 10:42:14 -0800244 return;
245 }
Saurav Das261c3002017-06-13 15:35:54 -0700246 // find all the destinationSets related to link
247 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700248 .stream()
Saurav Das261c3002017-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 Vavilapalli7cd16712015-05-04 09:48:09 -0700252 .collect(Collectors.toSet());
Saurav Das62ae6792017-05-15 15:34:25 -0700253
Saurav Das261c3002017-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 Das62ae6792017-05-15 15:34:25 -0700260 log.warn("retryHash in device {}, but global store has no record "
Saurav Das261c3002017-06-13 15:35:54 -0700261 + "for dsKey:{}", deviceId, dsKey);
Saurav Das62ae6792017-05-15 15:34:25 -0700262 continue;
263 }
Saurav Das261c3002017-06-13 15:35:54 -0700264 int nextId = nextHops.nextId();
265 Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -0700266 if (!linkDown) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700267 List<PortLabel> pl = Lists.newArrayList();
Saurav Das62ae6792017-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 Das261c3002017-06-13 15:35:54 -0700274 dstSet.forEach(dst -> {
275 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700276 pl.add(new PortLabel(p, edgeLabel));
Saurav Das261c3002017-06-13 15:35:54 -0700277 });
Saurav Das62ae6792017-05-15 15:34:25 -0700278 }
Saurav Dasfe0b05e2017-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 Das62ae6792017-05-15 15:34:25 -0700287 }
288 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700289 // linkdown
290 List<PortLabel> pl = Lists.newArrayList();
Saurav Das261c3002017-06-13 15:35:54 -0700291 dstSet.forEach(dst -> {
292 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst);
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700293 pl.add(new PortLabel(link.src().port(), edgeLabel));
Saurav Das261c3002017-06-13 15:35:54 -0700294 });
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700295 removeFromHashedNextObjective(pl, neighborMac, nextId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700296 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700297 }
Saurav Das8a3022d2017-05-05 17:01:08 -0700298 }
299
Saurav Das62ae6792017-05-15 15:34:25 -0700300 /**
Saurav Dasfe0b05e2017-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 Das62ae6792017-05-15 15:34:25 -0700327 *
Saurav Dasfe0b05e2017-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 Das62ae6792017-05-15 15:34:25 -0700330 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700331 * @param nextId id for next-objective to which buckets will be added
Saurav Dasfbe74572017-08-03 18:30:35 -0700332 *
Saurav Das62ae6792017-05-15 15:34:25 -0700333 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700334 private void addToHashedNextObjective(Collection<PortLabel> portLabels,
335 MacAddress dstMac, Integer nextId) {
Saurav Das8a3022d2017-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 Das8a3022d2017-05-05 17:01:08 -0700341 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
342 .withId(nextId)
343 .withType(NextObjective.Type.HASHED)
Saurav Das8a3022d2017-05-05 17:01:08 -0700344 .withMeta(metabuilder.build())
345 .fromApp(appId);
Saurav Dasfe0b05e2017-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 Das8a3022d2017-05-05 17:01:08 -0700362
363 ObjectiveContext context = new DefaultObjectiveContext(
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700364 (objective) -> log.debug("addToHash port/label {} addedTo "
365 + "NextObj {} on {}", portLabels, nextId, deviceId),
Saurav Das8a3022d2017-05-05 17:01:08 -0700366 (objective, error) ->
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700367 log.warn("addToHash failed to add port/label {} to"
368 + " NextObj {} on {}: {}", portLabels,
Saurav Das62ae6792017-05-15 15:34:25 -0700369 nextId, deviceId, error));
Saurav Das8a3022d2017-05-05 17:01:08 -0700370 NextObjective nextObjective = nextObjBuilder.addToExisting(context);
371 flowObjectiveService.next(deviceId, nextObjective);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700372 }
373
374 /**
Saurav Dasfe0b05e2017-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 Dasfbe74572017-08-03 18:30:35 -0700378 *
Saurav Dasfe0b05e2017-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 Dasfbe74572017-08-03 18:30:35 -0700381 * @param dstMac destination mac address of next-hop
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700382 * @param nextId id for next-objective from which buckets will be removed
Saurav Dasfbe74572017-08-03 18:30:35 -0700383 */
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700384 private void removeFromHashedNextObjective(Collection<PortLabel> portLabels,
385 MacAddress dstMac, Integer nextId) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700386 NextObjective.Builder nextObjBuilder = DefaultNextObjective
387 .builder()
388 .withType(NextObjective.Type.HASHED) //same as original
389 .withId(nextId)
Saurav Dasfe0b05e2017-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 Das62ae6792017-05-15 15:34:25 -0700406
Saurav Dasfe0b05e2017-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 Dasfbe74572017-08-03 18:30:35 -0700414 flowObjectiveService.next(deviceId, nextObjective);
415 }
Saurav Das62ae6792017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700423 *
Saurav Das62ae6792017-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 Vavilapalli37a461b2015-04-07 15:12:32 -0700431 */
Saurav Das62ae6792017-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 Das261c3002017-06-13 15:35:54 -0700435 Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore =
Saurav Das62ae6792017-05-15 15:34:25 -0700436 new HashMap<>();
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700437 boolean foundNextObjective = false, success = true;
Charles Chan319d1a22015-11-03 10:42:14 -0800438
Saurav Das261c3002017-06-13 15:35:54 -0700439 // retrieve hash-groups meant for destSw, which have destinationSets
Saurav Das62ae6792017-05-15 15:34:25 -0700440 // with different neighbors than the given next-hops
Saurav Das261c3002017-06-13 15:35:54 -0700441 for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) {
442 if (!dskey.deviceId().equals(targetSw) ||
443 !dskey.destinationSet().getDestinationSwitches().contains(destSw)) {
Saurav Das62ae6792017-05-15 15:34:25 -0700444 continue;
445 }
446 foundNextObjective = true;
Saurav Das261c3002017-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 Chan319d1a22015-11-03 10:42:14 -0800451
Saurav Das62ae6792017-05-15 15:34:25 -0700452 Set<DeviceId> diff;
453 if (revoke) {
454 diff = Sets.difference(currNeighbors, nextHops);
455 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
456 + "hops:{} ..removing {}", targetSw, destSw, nextId,
457 currNeighbors, diff);
458 } else {
459 diff = Sets.difference(nextHops, currNeighbors);
460 log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next "
461 + "hops:{} ..adding {}", targetSw, destSw, nextId,
462 currNeighbors, diff);
463 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700464 boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId,
465 revoke);
466 if (suc) {
467 // to update neighbor set with changes made
Saurav Das62ae6792017-05-15 15:34:25 -0700468 if (revoke) {
Saurav Das261c3002017-06-13 15:35:54 -0700469 tempStore.put(dskey, Sets.difference(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700470 } else {
Saurav Das261c3002017-06-13 15:35:54 -0700471 tempStore.put(dskey, Sets.union(currNeighbors, diff));
Saurav Das62ae6792017-05-15 15:34:25 -0700472 }
sangho2165d222015-05-01 09:38:25 -0700473 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700474 success &= suc;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700475 }
476
Saurav Das62ae6792017-05-15 15:34:25 -0700477 if (!foundNextObjective) {
478 log.debug("Cannot find any nextObjectives for route targetSw:{} "
479 + "-> dstSw:{}", targetSw, destSw);
480 return true; // nothing to do, return true so ECMPspg is updated
481 }
482
Saurav Das261c3002017-06-13 15:35:54 -0700483 // update the dsNextObjectiveStore with new destinationSet to nextId mappings
484 for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700485 NextNeighbors currentNextHops = dsNextObjStore.get(key);
486 if (currentNextHops == null) {
487 log.warn("fixHashGroups could not update global store in "
488 + "device {} .. missing nextNeighbors for key {}",
489 deviceId, key);
Saurav Das62ae6792017-05-15 15:34:25 -0700490 continue;
491 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700492 Set<DeviceId> newNeighbors = new HashSet<>();
493 newNeighbors.addAll(tempStore.get(key));
494 Map<DeviceId, Set<DeviceId>> oldDstNextHops =
495 ImmutableMap.copyOf(currentNextHops.dstNextHops());
496 currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change
497 log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}",
498 targetSw, destSw, key, currentNextHops.nextId());
499 log.debug("Old dstNextHops: {}", oldDstNextHops);
500 log.debug("New dstNextHops: {}", currentNextHops.dstNextHops());
501 // update global store
502 dsNextObjStore.put(key,
503 new NextNeighbors(currentNextHops.dstNextHops(),
504 currentNextHops.nextId()));
Saurav Das62ae6792017-05-15 15:34:25 -0700505 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700506 // even if one fails and others succeed, return false so ECMPspg not updated
507 return success;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700508 }
509
Saurav Dasfbe74572017-08-03 18:30:35 -0700510 /**
511 * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops
512 * that are not already in the store for the given DestinationSet. Note that
513 * this method does not remove existing next hops for the destinations in the
514 * DestinationSet.
515 *
516 * @param ds the DestinationSet for which the next hops need to be updated
517 * @param newDstNextHops a map of per-destination next hops to update the
518 * destinationSet with
519 * @return true if successful in updating all next hops
520 */
521 private boolean updateNextHops(DestinationSet ds,
Saurav Das261c3002017-06-13 15:35:54 -0700522 Map<DeviceId, Set<DeviceId>> newDstNextHops) {
523 DestinationSetNextObjectiveStoreKey key =
524 new DestinationSetNextObjectiveStoreKey(deviceId, ds);
525 NextNeighbors currNext = dsNextObjStore.get(key);
526 Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops();
527
528 // add newDstNextHops to currDstNextHops for each dst
529 boolean success = true;
530 for (DeviceId dstSw : ds.getDestinationSwitches()) {
531 Set<DeviceId> currNhops = currDstNextHops.get(dstSw);
532 Set<DeviceId> newNhops = newDstNextHops.get(dstSw);
533 currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops;
534 newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops;
535 int edgeLabel = ds.getEdgeLabel(dstSw);
536 int nextId = currNext.nextId();
537
538 // new next hops should be added
539 boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops),
540 edgeLabel, nextId, false);
541 if (suc) {
542 currNhops.addAll(newNhops);
543 currDstNextHops.put(dstSw, currNhops); // this is only a local change
544 }
545 success &= suc;
546 }
547
548 if (success) {
549 // update global store
550 dsNextObjStore.put(key, new NextNeighbors(currDstNextHops,
551 currNext.nextId()));
552 log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds,
553 dsNextObjStore.get(key));
554 }
555 return success;
556 }
557
Saurav Dasfbe74572017-08-03 18:30:35 -0700558 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700559 * Adds or removes buckets for all ports to a set of neighbor devices. Caller
560 * needs to ensure that the given neighbors are all next hops towards the
561 * same destination (represented by the given edgeLabel).
Saurav Dasfbe74572017-08-03 18:30:35 -0700562 *
563 * @param neighbors set of neighbor device ids
564 * @param edgeLabel MPLS label to use in buckets
565 * @param nextId the nextObjective to change
566 * @param revoke true if buckets need to be removed, false if they need to
567 * be added
568 * @return true if successful in adding or removing buckets for all ports
569 * to the neighbors
570 */
571 private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel,
Saurav Das261c3002017-06-13 15:35:54 -0700572 int nextId, boolean revoke) {
Saurav Dasfbe74572017-08-03 18:30:35 -0700573 for (DeviceId neighbor : neighbors) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700574 MacAddress neighborMac;
Saurav Das261c3002017-06-13 15:35:54 -0700575 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700576 neighborMac = deviceConfig.getDeviceMac(neighbor);
Saurav Das261c3002017-06-13 15:35:54 -0700577 } catch (DeviceConfigNotFoundException e) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700578 log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop"
579 + " for nextId:" + nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700580 return false;
581 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700582 Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor);
583 if (portsToNeighbor == null || portsToNeighbor.isEmpty()) {
Saurav Das261c3002017-06-13 15:35:54 -0700584 log.warn("No ports found in dev:{} for neighbor:{} .. cannot "
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700585 + "updateAllPortsToNextHop for nextId: {}",
Saurav Das261c3002017-06-13 15:35:54 -0700586 deviceId, neighbor, nextId);
587 return false;
588 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700589 List<PortLabel> pl = Lists.newArrayList();
590 portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel)));
Saurav Das261c3002017-06-13 15:35:54 -0700591 if (revoke) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700592 log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) "
593 + "with Port/Label:{} to next object id {}",
594 deviceId, pl, nextId);
595 removeFromHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700596 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -0700597 log.debug("fixHashGroup in device {}: Adding Bucket(s) "
598 + "with Port/Label: {} to next object id {}",
599 deviceId, pl, nextId);
600 addToHashedNextObjective(pl, neighborMac, nextId);
Saurav Das261c3002017-06-13 15:35:54 -0700601 }
602 }
603 return true;
604 }
605
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700606 /**
Saurav Das3fb28272017-03-04 16:08:47 -0800607 * Adds or removes a port that has been configured with a vlan to a broadcast group
608 * for bridging. Should only be called by the master instance for this device.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800609 *
610 * @param port the port on this device that needs to be added/removed to a bcast group
Saurav Das3fb28272017-03-04 16:08:47 -0800611 * @param vlanId the vlan id corresponding to the broadcast domain/group
612 * @param popVlan indicates if packets should be sent out untagged or not out
613 * of the port. If true, indicates an access (untagged) or native vlan
614 * configuration. If false, indicates a trunk (tagged) vlan config.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800615 * @param portUp true if port is enabled, false if disabled
Saurav Dasf0f592d2016-11-18 15:21:57 -0800616 */
Saurav Das3fb28272017-03-04 16:08:47 -0800617 public void processEdgePort(PortNumber port, VlanId vlanId,
618 boolean popVlan, boolean portUp) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800619 //get the next id for the subnet and edit it.
Charles Chan10b0fb72017-02-02 16:20:42 -0800620 Integer nextId = getVlanNextObjectiveId(vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800621 if (nextId == -1) {
622 if (portUp) {
623 log.debug("**Creating flooding group for first port enabled in"
Charles Chan10b0fb72017-02-02 16:20:42 -0800624 + " subnet {} on dev {} port {}", vlanId, deviceId, port);
625 createBcastGroupFromVlan(vlanId, Collections.singleton(port));
Saurav Dasf0f592d2016-11-18 15:21:57 -0800626 } else {
627 log.warn("Could not find flooding group for subnet {} on dev:{} when"
Charles Chan10b0fb72017-02-02 16:20:42 -0800628 + " removing port:{}", vlanId, deviceId, port);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800629 }
630 return;
631 }
632
633 log.info("**port{} in device {}: {} Bucket with Port {} to"
634 + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId,
635 (portUp) ? "Adding" : "Removing",
636 port, nextId);
637 // Create the bucket to be added or removed
638 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Saurav Das3fb28272017-03-04 16:08:47 -0800639 if (popVlan) {
640 tBuilder.popVlan();
641 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800642 tBuilder.setOutput(port);
643
Saurav Dasf0f592d2016-11-18 15:21:57 -0800644 TrafficSelector metadata =
Saurav Das3fb28272017-03-04 16:08:47 -0800645 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Saurav Dasf0f592d2016-11-18 15:21:57 -0800646
647 NextObjective.Builder nextObjBuilder = DefaultNextObjective
648 .builder().withId(nextId)
649 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
650 .addTreatment(tBuilder.build())
651 .withMeta(metadata);
652
653 ObjectiveContext context = new DefaultObjectiveContext(
654 (objective) -> log.debug("port {} successfully {} NextObj {} on {}",
655 port, (portUp) ? "addedTo" : "removedFrom",
656 nextId, deviceId),
657 (objective, error) ->
658 log.warn("port {} failed to {} NextObj {} on {}: {}",
659 port, (portUp) ? "addTo" : "removeFrom",
660 nextId, deviceId, error));
661
662 NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context)
663 : nextObjBuilder.removeFromExisting(context);
664 log.debug("edgePort processed: Submited next objective {} in device {}",
665 nextId, deviceId);
666 flowObjectiveService.next(deviceId, nextObj);
667 }
668
669 /**
Saurav Das261c3002017-06-13 15:35:54 -0700670 * Returns the next objective of type hashed associated with the destination set.
671 * In addition, updates the existing next-objective if new route-route paths found
672 * have resulted in the addition of new next-hops to a particular destination.
673 * If there is no existing next objective for this destination set, this method
674 * would create a next objective and return the nextId. Optionally metadata can be
Saurav Das4c35fc42015-11-20 15:27:53 -0800675 * passed in for the creation of the next objective.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700676 *
Saurav Das261c3002017-06-13 15:35:54 -0700677 * @param ds destination set
678 * @param nextHops a map of per destination next hops
Saurav Das4c35fc42015-11-20 15:27:53 -0800679 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre229fd0b2016-10-31 16:49:19 -0700680 * @param isBos if Bos is set
Saurav Das4c35fc42015-11-20 15:27:53 -0800681 * @return int if found or -1 if there are errors in the creation of the
682 * neighbor set.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700683 */
Saurav Das261c3002017-06-13 15:35:54 -0700684 public int getNextObjectiveId(DestinationSet ds,
685 Map<DeviceId, Set<DeviceId>> nextHops,
686 TrafficSelector meta, boolean isBos) {
687 NextNeighbors next = dsNextObjStore.
688 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
689 if (next == null) {
690 log.debug("getNextObjectiveId in device{}: Next objective id "
691 + "not found for {} ... creating", deviceId, ds);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700692 log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}",
693 deviceId,
Saurav Das261c3002017-06-13 15:35:54 -0700694 dsNextObjStore.entrySet()
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700695 .stream()
696 .filter((nsStoreEntry) ->
697 (nsStoreEntry.getKey().deviceId().equals(deviceId)))
698 .collect(Collectors.toList()));
Saurav Das261c3002017-06-13 15:35:54 -0700699
700 createGroupFromDestinationSet(ds, nextHops, meta, isBos);
701 next = dsNextObjStore.
702 get(new DestinationSetNextObjectiveStoreKey(deviceId, ds));
703 if (next == null) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700704 log.warn("getNextObjectiveId: unable to create next objective");
Saurav Das261c3002017-06-13 15:35:54 -0700705 // failure in creating group
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700706 return -1;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700707 } else {
708 log.debug("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700709 + "created for {}", deviceId, next.nextId(), ds);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700710 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700711 } else {
712 log.trace("getNextObjectiveId in device{}: Next objective id {} "
Saurav Das261c3002017-06-13 15:35:54 -0700713 + "found for {}", deviceId, next.nextId(), ds);
714 // should fix hash groups too if next-hops have changed
715 if (!next.dstNextHops().equals(nextHops)) {
716 log.debug("Nexthops have changed for dev:{} nextId:{} ..updating",
717 deviceId, next.nextId());
718 if (!updateNextHops(ds, nextHops)) {
719 // failure in updating group
720 return -1;
721 }
722 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700723 }
Saurav Das261c3002017-06-13 15:35:54 -0700724 return next.nextId();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700725 }
726
sangho4a5c42a2015-05-20 22:16:38 -0700727 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800728 * Returns the next objective of type broadcast associated with the vlan,
Saurav Das2d94d312015-11-24 23:21:05 -0800729 * or -1 if no such objective exists. Note that this method does NOT create
730 * the next objective as a side-effect. It is expected that is objective is
Saurav Dasf0f592d2016-11-18 15:21:57 -0800731 * created at startup from network configuration. Typically this is used
732 * for L2 flooding within the subnet configured on the switch.
Charles Chan77277672015-10-20 16:24:19 -0700733 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800734 * @param vlanId vlan id
Charles Chan77277672015-10-20 16:24:19 -0700735 * @return int if found or -1
736 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800737 public int getVlanNextObjectiveId(VlanId vlanId) {
738 Integer nextId = vlanNextObjStore.
739 get(new VlanNextObjectiveStoreKey(deviceId, vlanId));
Charles Chanc6ad7752015-10-29 14:58:10 -0700740
741 return (nextId != null) ? nextId : -1;
Charles Chan77277672015-10-20 16:24:19 -0700742 }
743
744 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800745 * Returns the next objective of type simple associated with the port on the
746 * device, given the treatment. Different treatments to the same port result
747 * in different next objectives. If no such objective exists, this method
Saurav Das2cb38292017-03-29 19:09:17 -0700748 * creates one (if requested) and returns the id. Optionally metadata can be passed in for
Saurav Dasf0f592d2016-11-18 15:21:57 -0800749 * the creation of the objective. Typically this is used for L2 and L3 forwarding
750 * to compute nodes and containers/VMs on the compute nodes directly attached
751 * to the switch.
Saurav Das2d94d312015-11-24 23:21:05 -0800752 *
753 * @param portNum the port number for the simple next objective
754 * @param treatment the actions to apply on the packets (should include outport)
755 * @param meta optional metadata passed into the creation of the next objective
Saurav Das2cb38292017-03-29 19:09:17 -0700756 * @param createIfMissing true if a next object should be created if not found
Saurav Das2d94d312015-11-24 23:21:05 -0800757 * @return int if found or created, -1 if there are errors during the
758 * creation of the next objective.
759 */
760 public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700761 TrafficSelector meta, boolean createIfMissing) {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800762 Integer nextId = portNextObjStore
Saurav Das368cf212017-03-15 15:15:14 -0700763 .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta));
Saurav Das2cb38292017-03-29 19:09:17 -0700764 if (nextId != null) {
765 return nextId;
766 }
767 log.debug("getPortNextObjectiveId in device {}: Next objective id "
768 + "not found for port: {} .. {}", deviceId, portNum,
769 (createIfMissing) ? "creating" : "aborting");
770 if (!createIfMissing) {
771 return -1;
772 }
773 // create missing next objective
774 createGroupFromPort(portNum, treatment, meta);
775 nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum,
776 treatment, meta));
Saurav Das2d94d312015-11-24 23:21:05 -0800777 if (nextId == null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700778 log.warn("getPortNextObjectiveId: unable to create next obj"
779 + "for dev:{} port:{}", deviceId, portNum);
780 return -1;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800781 }
782 return nextId;
783 }
784
785 /**
sangho4a5c42a2015-05-20 22:16:38 -0700786 * Checks if the next objective ID (group) for the neighbor set exists or not.
787 *
788 * @param ns neighbor set to check
789 * @return true if it exists, false otherwise
790 */
Saurav Das261c3002017-06-13 15:35:54 -0700791 public boolean hasNextObjectiveId(DestinationSet ns) {
792 NextNeighbors nextHops = dsNextObjStore.
793 get(new DestinationSetNextObjectiveStoreKey(deviceId, ns));
794 if (nextHops == null) {
sangho4a5c42a2015-05-20 22:16:38 -0700795 return false;
796 }
797
798 return true;
799 }
800
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700801 private void populateNeighborMaps() {
802 Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700803 for (Link link : outgoingLinks) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700804 if (link.type() != Link.Type.DIRECT) {
805 continue;
806 }
807 addNeighborAtPort(link.dst().deviceId(), link.src().port());
808 }
809 }
810
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700811 protected void addNeighborAtPort(DeviceId neighborId,
812 PortNumber portToNeighbor) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700813 // Update DeviceToPort database
814 log.debug("Device {} addNeighborAtPort: neighbor {} at port {}",
815 deviceId, neighborId, portToNeighbor);
Saurav Das4c35fc42015-11-20 15:27:53 -0800816 Set<PortNumber> ports = Collections
817 .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>());
818 ports.add(portToNeighbor);
819 Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports);
820 if (portnums != null) {
821 portnums.add(portToNeighbor);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700822 }
823
824 // Update portToDevice database
Saurav Das4c35fc42015-11-20 15:27:53 -0800825 DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId);
826 if (prev != null) {
Saurav Das62ae6792017-05-15 15:34:25 -0700827 log.debug("Device: {} port: {} already has neighbor: {} ",
828 deviceId, portToNeighbor, prev, neighborId);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700829 }
830 }
831
sangho27462c62015-05-14 00:39:53 -0700832 /**
Saurav Das261c3002017-06-13 15:35:54 -0700833 * Creates a NextObjective for a hash group in this device from a given
834 * DestinationSet.
sangho27462c62015-05-14 00:39:53 -0700835 *
Saurav Das261c3002017-06-13 15:35:54 -0700836 * @param ds the DestinationSet
837 * @param neighbors a map for each destination and its next-hops
Saurav Das4c35fc42015-11-20 15:27:53 -0800838 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre229fd0b2016-10-31 16:49:19 -0700839 * @param isBos if BoS is set
sangho27462c62015-05-14 00:39:53 -0700840 */
Saurav Das261c3002017-06-13 15:35:54 -0700841 public void createGroupFromDestinationSet(DestinationSet ds,
842 Map<DeviceId, Set<DeviceId>> neighbors,
843 TrafficSelector meta,
844 boolean isBos) {
845 int nextId = flowObjectiveService.allocateNextId();
846 NextObjective.Type type = NextObjective.Type.HASHED;
847 if (neighbors == null || neighbors.isEmpty()) {
848 log.warn("createGroupsFromDestinationSet: needs at least one neighbor"
849 + "to create group in dev:{} for ds: {} with next-hops {}",
850 deviceId, ds, neighbors);
851 return;
852 }
853 // If Bos == False and MPLS-ECMP == false, we have
854 // to use simple group and we will pick a single neighbor for a single dest.
855 if (!isBos && !srManager.getMplsEcmp()) {
856 type = NextObjective.Type.SIMPLE;
857 }
858
859 NextObjective.Builder nextObjBuilder = DefaultNextObjective
860 .builder()
861 .withId(nextId)
862 .withType(type)
863 .fromApp(appId);
864 if (meta != null) {
865 nextObjBuilder.withMeta(meta);
866 }
867
868 // create treatment buckets for each neighbor for each dst Device
869 // except in the special case where we only want to pick a single
870 // neighbor for a simple group
871 boolean foundSingleNeighbor = false;
872 boolean treatmentAdded = false;
873 Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>();
874 for (DeviceId dst : ds.getDestinationSwitches()) {
875 Set<DeviceId> nextHops = neighbors.get(dst);
876 if (nextHops == null || nextHops.isEmpty()) {
877 continue;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700878 }
Saurav Das261c3002017-06-13 15:35:54 -0700879
880 if (foundSingleNeighbor) {
881 break;
882 }
883
884 for (DeviceId neighborId : nextHops) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800885 if (devicePortMap.get(neighborId) == null) {
886 log.warn("Neighbor {} is not in the port map yet for dev:{}",
887 neighborId, deviceId);
sangho2165d222015-05-01 09:38:25 -0700888 return;
Jon Hall31d84782017-01-18 20:15:44 -0800889 } else if (devicePortMap.get(neighborId).isEmpty()) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700890 log.warn("There are no ports for "
Saurav Das4c35fc42015-11-20 15:27:53 -0800891 + "the Device {} in the port map yet", neighborId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700892 return;
sangho2165d222015-05-01 09:38:25 -0700893 }
894
Saurav Das4c35fc42015-11-20 15:27:53 -0800895 MacAddress neighborMac;
Charles Chan319d1a22015-11-03 10:42:14 -0800896 try {
Saurav Das4c35fc42015-11-20 15:27:53 -0800897 neighborMac = deviceConfig.getDeviceMac(neighborId);
Charles Chan319d1a22015-11-03 10:42:14 -0800898 } catch (DeviceConfigNotFoundException e) {
Saurav Das261c3002017-06-13 15:35:54 -0700899 log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset.");
Charles Chan319d1a22015-11-03 10:42:14 -0800900 return;
901 }
Saurav Das261c3002017-06-13 15:35:54 -0700902 // For each port to the neighbor, we create a new treatment
Pier Ventre229fd0b2016-10-31 16:49:19 -0700903 Set<PortNumber> neighborPorts = devicePortMap.get(neighborId);
904 // In this case we are using a SIMPLE group. We randomly pick a port
905 if (!isBos && !srManager.getMplsEcmp()) {
906 int size = devicePortMap.get(neighborId).size();
907 int index = RandomUtils.nextInt(0, size);
908 neighborPorts = Collections.singleton(
Saurav Das261c3002017-06-13 15:35:54 -0700909 Iterables.get(devicePortMap.get(neighborId),
910 index));
911 foundSingleNeighbor = true;
Pier Ventre229fd0b2016-10-31 16:49:19 -0700912 }
913 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700914 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
915 .builder();
Saurav Das261c3002017-06-13 15:35:54 -0700916 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
917 int edgeLabel = ds.getEdgeLabel(dst);
918 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Charles Chanf4586112015-11-09 16:37:23 -0800919 tBuilder.pushMpls()
Saurav Das261c3002017-06-13 15:35:54 -0700920 .copyTtlOut()
921 .setMpls(MplsLabel.mplsLabel(edgeLabel));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700922 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800923 tBuilder.setOutput(sp);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700924 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das261c3002017-06-13 15:35:54 -0700925 treatmentAdded = true;
926 //update store
927 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
928 if (existingNeighbors == null) {
929 existingNeighbors = new HashSet<>();
930 }
931 existingNeighbors.add(neighborId);
932 dstNextHops.put(dst, existingNeighbors);
933 log.debug("creating treatment for port/label {}/{} in next:{}",
934 sp, edgeLabel, nextId);
935 }
936
937 if (foundSingleNeighbor) {
938 break;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700939 }
940 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700941 }
Saurav Das261c3002017-06-13 15:35:54 -0700942
943 if (!treatmentAdded) {
944 log.warn("Could not createGroup from DestinationSet {} without any"
945 + "next hops {}", ds, neighbors);
946 return;
947 }
948 ObjectiveContext context = new DefaultObjectiveContext(
949 (objective) ->
950 log.debug("createGroupsFromDestinationSet installed "
951 + "NextObj {} on {}", nextId, deviceId),
952 (objective, error) ->
953 log.warn("createGroupsFromDestinationSet failed to install"
954 + " NextObj {} on {}: {}", nextId, deviceId, error)
955 );
956 NextObjective nextObj = nextObjBuilder.add(context);
957 log.debug(".. createGroupsFromDestinationSet: Submitted "
958 + "next objective {} in device {}", nextId, deviceId);
959 flowObjectiveService.next(deviceId, nextObj);
960 //update store
961 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
962 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700963 }
964
Saurav Das2d94d312015-11-24 23:21:05 -0800965 /**
Saurav Dasf0f592d2016-11-18 15:21:57 -0800966 * Creates broadcast groups for all ports in the same subnet for
967 * all configured subnets.
Saurav Das2d94d312015-11-24 23:21:05 -0800968 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800969 public void createGroupsFromVlanConfig() {
Charles Chan90772a72017-02-08 15:52:08 -0800970 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -0800971 createBcastGroupFromVlan(vlanId, ports);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800972 });
Saurav Dasf0f592d2016-11-18 15:21:57 -0800973 }
Charles Chanc6ad7752015-10-29 14:58:10 -0700974
Saurav Dasf0f592d2016-11-18 15:21:57 -0800975 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800976 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800977 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800978 * @param vlanId vlan id
Saurav Dasf0f592d2016-11-18 15:21:57 -0800979 * @param ports list of ports in the subnet
980 */
Charles Chan90772a72017-02-08 15:52:08 -0800981 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800982 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chanc6ad7752015-10-29 14:58:10 -0700983
Charles Chan10b0fb72017-02-02 16:20:42 -0800984 if (vlanNextObjStore.containsKey(key)) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800985 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan10b0fb72017-02-02 16:20:42 -0800986 deviceId, vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800987 return;
988 }
Charles Chande6655c2015-12-23 00:15:11 -0800989
Saurav Dasf0f592d2016-11-18 15:21:57 -0800990 TrafficSelector metadata =
Charles Chan10b0fb72017-02-02 16:20:42 -0800991 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chan77277672015-10-20 16:24:19 -0700992
Saurav Dasf0f592d2016-11-18 15:21:57 -0800993 int nextId = flowObjectiveService.allocateNextId();
Charles Chan77277672015-10-20 16:24:19 -0700994
Saurav Dasf0f592d2016-11-18 15:21:57 -0800995 NextObjective.Builder nextObjBuilder = DefaultNextObjective
996 .builder().withId(nextId)
997 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
998 .withMeta(metadata);
Charles Chan77277672015-10-20 16:24:19 -0700999
Saurav Dasf0f592d2016-11-18 15:21:57 -08001000 ports.forEach(port -> {
1001 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan90772a72017-02-08 15:52:08 -08001002 if (toPopVlan(port, vlanId)) {
1003 tBuilder.popVlan();
1004 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001005 tBuilder.setOutput(port);
1006 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chan77277672015-10-20 16:24:19 -07001007 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001008
Saurav Das2cb38292017-03-29 19:09:17 -07001009 ObjectiveContext context = new DefaultObjectiveContext(
1010 (objective) ->
1011 log.debug("createBroadcastGroupFromVlan installed "
1012 + "NextObj {} on {}", nextId, deviceId),
1013 (objective, error) ->
1014 log.warn("createBroadcastGroupFromVlan failed to install"
1015 + " NextObj {} on {}: {}", nextId, deviceId, error)
1016 );
1017 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001018 flowObjectiveService.next(deviceId, nextObj);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001019 log.debug("createBcastGroupFromVlan: Submitted next objective {} in device {}",
Saurav Dasf0f592d2016-11-18 15:21:57 -08001020 nextId, deviceId);
1021
Charles Chan10b0fb72017-02-02 16:20:42 -08001022 vlanNextObjStore.put(key, nextId);
Charles Chan77277672015-10-20 16:24:19 -07001023 }
1024
Charles Chanb7f75ac2016-01-11 18:28:54 -08001025 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001026 * Removes a single broadcast group from a given vlan id.
1027 * The group should be empty.
1028 * @param deviceId device Id to remove the group
1029 * @param portNum port number related to the group
1030 * @param vlanId vlan id of the broadcast group to remove
1031 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1032 */
1033 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1034 VlanId vlanId, boolean popVlan) {
1035 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1036
1037 if (!vlanNextObjStore.containsKey(key)) {
1038 log.debug("Broadcast group for device {} and subnet {} does not exist",
1039 deviceId, vlanId);
1040 return;
1041 }
1042
1043 TrafficSelector metadata =
1044 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1045
1046 int nextId = vlanNextObjStore.get(key);
1047
1048 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1049 .builder().withId(nextId)
1050 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1051 .withMeta(metadata);
1052
1053 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1054 if (popVlan) {
1055 tBuilder.popVlan();
1056 }
1057 tBuilder.setOutput(portNum);
1058 nextObjBuilder.addTreatment(tBuilder.build());
1059
1060 ObjectiveContext context = new DefaultObjectiveContext(
1061 (objective) ->
1062 log.debug("removeBroadcastGroupFromVlan removed "
1063 + "NextObj {} on {}", nextId, deviceId),
1064 (objective, error) ->
1065 log.warn("removeBroadcastGroupFromVlan failed to remove "
1066 + " NextObj {} on {}: {}", nextId, deviceId, error)
1067 );
1068 NextObjective nextObj = nextObjBuilder.remove(context);
1069 flowObjectiveService.next(deviceId, nextObj);
1070 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1071 nextId, deviceId);
1072
1073 vlanNextObjStore.remove(key, nextId);
1074 }
1075
1076 /**
Charles Chan90772a72017-02-08 15:52:08 -08001077 * Determine if we should pop given vlan before sending packets to the given port.
1078 *
1079 * @param portNumber port number
1080 * @param vlanId vlan id
1081 * @return true if the vlan id is not contained in any vlanTagged config
1082 */
1083 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das261c3002017-06-13 15:35:54 -07001084 return srManager.interfaceService
1085 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan90772a72017-02-08 15:52:08 -08001086 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1087 }
1088
1089 /**
Saurav Das2d94d312015-11-24 23:21:05 -08001090 * Create simple next objective for a single port. The treatments can include
1091 * all outgoing actions that need to happen on the packet.
1092 *
1093 * @param portNum the outgoing port on the device
1094 * @param treatment the actions to apply on the packets (should include outport)
1095 * @param meta optional data to pass to the driver
1096 */
1097 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1098 TrafficSelector meta) {
1099 int nextId = flowObjectiveService.allocateNextId();
1100 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das368cf212017-03-15 15:15:14 -07001101 deviceId, portNum, treatment, meta);
Saurav Das2d94d312015-11-24 23:21:05 -08001102
1103 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1104 .builder().withId(nextId)
1105 .withType(NextObjective.Type.SIMPLE)
1106 .addTreatment(treatment)
1107 .fromApp(appId)
1108 .withMeta(meta);
1109
Saurav Das2cb38292017-03-29 19:09:17 -07001110 ObjectiveContext context = new DefaultObjectiveContext(
1111 (objective) ->
1112 log.debug("createGroupFromPort installed "
1113 + "NextObj {} on {}", nextId, deviceId),
1114 (objective, error) ->
1115 log.warn("createGroupFromPort failed to install"
1116 + " NextObj {} on {}: {}", nextId, deviceId, error)
1117 );
1118 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das2d94d312015-11-24 23:21:05 -08001119 flowObjectiveService.next(deviceId, nextObj);
1120 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1121 + "for port {}", nextId, deviceId, portNum);
1122
1123 portNextObjStore.put(key, nextId);
1124 }
1125
sangho27462c62015-05-14 00:39:53 -07001126 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001127 * Removes simple next objective for a single port.
1128 *
1129 * @param deviceId device id that has the port to deal with
1130 * @param portNum the outgoing port on the device
1131 * @param vlanId vlan id associated with the port
1132 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1133 */
1134 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1135 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1136 mbuilder.matchVlanId(vlanId);
1137
1138 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1139 tbuilder.immediate().setOutput(portNum);
1140 if (popVlan) {
1141 tbuilder.immediate().popVlan();
1142 }
1143
1144 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1145 tbuilder.build(), mbuilder.build(), false);
1146
1147 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1148 deviceId, portNum, tbuilder.build(), mbuilder.build());
1149 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1150 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1151 .builder().withId(portNextObjId)
1152 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1153 ObjectiveContext context = new DefaultObjectiveContext(
1154 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1155 portNextObjId, deviceId),
1156 (objective, error) ->
1157 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1158 portNextObjId, deviceId, error));
1159 NextObjective nextObjective = nextObjBuilder.remove(context);
1160 log.info("**removePortNextObjective: Submitted "
1161 + "next objective {} in device {}",
1162 portNextObjId, deviceId);
1163 flowObjectiveService.next(deviceId, nextObjective);
1164
1165 portNextObjStore.remove(key);
1166 }
1167 }
1168 /**
sangho27462c62015-05-14 00:39:53 -07001169 * Removes groups for the next objective ID given.
1170 *
1171 * @param objectiveId next objective ID to remove
1172 * @return true if succeeds, false otherwise
1173 */
1174 public boolean removeGroup(int objectiveId) {
Saurav Das261c3002017-06-13 15:35:54 -07001175 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1176 dsNextObjStore.entrySet()) {
1177 if (e.getValue().nextId() != objectiveId) {
1178 continue;
1179 }
sangho27462c62015-05-14 00:39:53 -07001180 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1181 .builder().withId(objectiveId)
1182 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chana4ee4f92016-04-23 14:48:16 -07001183 ObjectiveContext context = new DefaultObjectiveContext(
1184 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1185 objectiveId, deviceId),
1186 (objective, error) ->
1187 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
1188 objectiveId, deviceId, error));
1189 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das4c35fc42015-11-20 15:27:53 -08001190 log.info("**removeGroup: Submited "
1191 + "next objective {} in device {}",
1192 objectiveId, deviceId);
sangho27462c62015-05-14 00:39:53 -07001193 flowObjectiveService.next(deviceId, nextObjective);
1194
Saurav Das261c3002017-06-13 15:35:54 -07001195 dsNextObjStore.remove(e.getKey());
sangho4a5c42a2015-05-20 22:16:38 -07001196 return true;
sangho27462c62015-05-14 00:39:53 -07001197 }
1198
1199 return false;
1200 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001201 /**
1202 * Remove simple next objective for a single port. The treatments can include
1203 * all outgoing actions that need to happen on the packet.
1204 *
1205 * @param portNum the outgoing port on the device
1206 * @param treatment the actions applied on the packets (should include outport)
1207 * @param meta optional data to pass to the driver
1208 */
1209 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1210 TrafficSelector meta) {
1211 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1212 deviceId, portNum, treatment, meta);
1213 Integer nextId = portNextObjStore.get(key);
1214
1215 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1216 .builder().withId(nextId)
1217 .withType(NextObjective.Type.SIMPLE)
1218 .addTreatment(treatment)
1219 .fromApp(appId)
1220 .withMeta(meta);
1221
1222 ObjectiveContext context = new DefaultObjectiveContext(
1223 (objective) ->
1224 log.info("removeGroupFromPort installed "
1225 + "NextObj {} on {}", nextId, deviceId),
1226 (objective, error) ->
1227 log.warn("removeGroupFromPort failed to install"
1228 + " NextObj {} on {}: {}", nextId, deviceId, error)
1229 );
1230 NextObjective nextObj = nextObjBuilder.remove(context);
1231 flowObjectiveService.next(deviceId, nextObj);
1232 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1233 + "for port {}", nextId, deviceId, portNum);
1234
1235 portNextObjStore.remove(key);
1236 }
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001237
Charles Chanb7f75ac2016-01-11 18:28:54 -08001238 /**
1239 * Removes all groups from all next objective stores.
1240 */
Saurav Das261c3002017-06-13 15:35:54 -07001241 /*public void removeAllGroups() {
1242 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das62af8802015-12-04 10:52:59 -08001243 nsNextObjStore.entrySet()) {
Saurav Das261c3002017-06-13 15:35:54 -07001244 removeGroup(entry.getValue().nextId());
Saurav Das62af8802015-12-04 10:52:59 -08001245 }
1246 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1247 portNextObjStore.entrySet()) {
1248 removeGroup(entry.getValue());
1249 }
Charles Chan10b0fb72017-02-02 16:20:42 -08001250 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1251 vlanNextObjStore.entrySet()) {
Saurav Das62af8802015-12-04 10:52:59 -08001252 removeGroup(entry.getValue());
1253 }
Saurav Das261c3002017-06-13 15:35:54 -07001254 }*/ //XXX revisit
1255
Saurav Dasfbe74572017-08-03 18:30:35 -07001256 /**
1257 * Triggers a one time bucket verification operation on all hash groups
1258 * on this device.
1259 */
1260 public void triggerBucketCorrector() {
1261 BucketCorrector bc = new BucketCorrector();
1262 bc.run();
1263 }
1264
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001265 public void updateGroupFromVlanConfiguration(PortNumber portNumber, Collection<VlanId> vlanIds,
1266 int nextId, boolean install) {
1267 vlanIds.forEach(vlanId -> updateGroupFromVlanInternal(vlanId, portNumber, nextId, install));
1268 }
1269
1270 private void updateGroupFromVlanInternal(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
1271 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1272 if (toPopVlan(portNum, vlanId)) {
1273 tBuilder.popVlan();
1274 }
1275 tBuilder.setOutput(portNum);
1276
1277 TrafficSelector metadata =
1278 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1279
1280 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1281 .builder().withId(nextId)
1282 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1283 .addTreatment(tBuilder.build())
1284 .withMeta(metadata);
1285
1286 ObjectiveContext context = new DefaultObjectiveContext(
1287 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1288 portNum, nextId, deviceId),
1289 (objective, error) ->
1290 log.warn("port {} failed to removedFrom NextObj {} on {}: {}",
1291 portNum, nextId, deviceId, error));
1292
1293 if (install) {
1294 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1295 } else {
1296 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1297 }
1298 }
Saurav Das8a3022d2017-05-05 17:01:08 -07001299
1300 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001301 * Performs bucket verification operation for all hash groups in this device.
1302 * Checks RouteHandler to ensure that routing is stable before attempting
1303 * verification. Verification involves creating a nextObjective with
1304 * operation VERIFY for existing next objectives in the store, and passing
1305 * it to the driver. It is the driver that actually performs the verification
1306 * by adding or removing buckets to match the verification next objective
1307 * created here.
Saurav Das8a3022d2017-05-05 17:01:08 -07001308 */
Saurav Dasfbe74572017-08-03 18:30:35 -07001309 protected final class BucketCorrector implements Runnable {
1310 Integer nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001311
Saurav Dasfbe74572017-08-03 18:30:35 -07001312 BucketCorrector() {
1313 this.nextId = null;
1314 }
1315
1316 BucketCorrector(Integer nextId) {
1317 this.nextId = nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001318 }
1319
1320 @Override
1321 public void run() {
Saurav Dasfbe74572017-08-03 18:30:35 -07001322 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1323 return;
Saurav Das8a3022d2017-05-05 17:01:08 -07001324 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001325 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1326 if (rh == null) {
1327 return;
1328 }
1329 if (!rh.isRoutingStable()) {
1330 return;
1331 }
1332 rh.acquireRoutingLock();
1333 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001334 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasfbe74572017-08-03 18:30:35 -07001335 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1336 .stream()
1337 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
1338 .map(entry -> entry.getKey())
1339 .collect(Collectors.toSet());
1340 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1341 NextNeighbors next = dsNextObjStore.get(dsKey);
1342 if (next == null) {
1343 continue;
1344 }
1345 int nid = next.nextId();
1346 if (nextId != null && nextId != nid) {
1347 continue;
1348 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001349 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasfbe74572017-08-03 18:30:35 -07001350 deviceId, dsKey, next);
1351 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
1352 metabuilder.matchVlanId(INTERNAL_VLAN);
1353 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1354 .withId(nid)
1355 .withType(NextObjective.Type.HASHED)
1356 .withMeta(metabuilder.build())
1357 .fromApp(appId);
1358
1359 next.dstNextHops().forEach((dstDev, nextHops) -> {
1360 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1361 nextHops.forEach(neighbor -> {
1362 MacAddress neighborMac;
1363 try {
1364 neighborMac = deviceConfig.getDeviceMac(neighbor);
1365 } catch (DeviceConfigNotFoundException e) {
1366 log.warn(e.getMessage() + " Aborting neighbor"
1367 + neighbor);
1368 return;
1369 }
1370 devicePortMap.get(neighbor).forEach(port -> {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001371 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasfbe74572017-08-03 18:30:35 -07001372 + " port/label {}/{} to dst {} via {}",
1373 deviceId, nid, port, edgeLabel,
1374 dstDev, neighbor);
1375 nextObjBuilder.addTreatment(treatmentBuilder(port,
1376 neighborMac, edgeLabel));
1377 });
1378 });
1379 });
1380
1381 NextObjective nextObjective = nextObjBuilder.verify();
1382 flowObjectiveService.next(deviceId, nextObjective);
1383 }
1384 } finally {
1385 rh.releaseRoutingLock();
1386 }
1387
1388 }
1389
1390 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
1391 int edgeLabel) {
1392 TrafficTreatment.Builder tBuilder =
1393 DefaultTrafficTreatment.builder();
1394 tBuilder.setOutput(outport)
1395 .setEthDst(dstMac)
1396 .setEthSrc(nodeMacAddr);
1397 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
1398 tBuilder.pushMpls()
1399 .copyTtlOut()
1400 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1401 }
1402 return tBuilder.build();
Saurav Das8a3022d2017-05-05 17:01:08 -07001403 }
1404 }
1405
Saurav Dasfbe74572017-08-03 18:30:35 -07001406}