blob: 71f5c1599bb3c196c218feac242738dbfe0ccd19 [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 }
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700913
Pier Ventre229fd0b2016-10-31 16:49:19 -0700914 for (PortNumber sp : neighborPorts) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700915 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment
916 .builder();
Saurav Das261c3002017-06-13 15:35:54 -0700917 tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr);
918 int edgeLabel = ds.getEdgeLabel(dst);
919 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
Charles Chanf4586112015-11-09 16:37:23 -0800920 tBuilder.pushMpls()
Saurav Das261c3002017-06-13 15:35:54 -0700921 .copyTtlOut()
922 .setMpls(MplsLabel.mplsLabel(edgeLabel));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700923 }
Saurav Das4c35fc42015-11-20 15:27:53 -0800924 tBuilder.setOutput(sp);
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700925 nextObjBuilder.addTreatment(tBuilder.build());
Saurav Das261c3002017-06-13 15:35:54 -0700926 treatmentAdded = true;
927 //update store
928 Set<DeviceId> existingNeighbors = dstNextHops.get(dst);
929 if (existingNeighbors == null) {
930 existingNeighbors = new HashSet<>();
931 }
932 existingNeighbors.add(neighborId);
933 dstNextHops.put(dst, existingNeighbors);
934 log.debug("creating treatment for port/label {}/{} in next:{}",
935 sp, edgeLabel, nextId);
936 }
937
938 if (foundSingleNeighbor) {
939 break;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700940 }
941 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700942 }
Saurav Das261c3002017-06-13 15:35:54 -0700943
944 if (!treatmentAdded) {
945 log.warn("Could not createGroup from DestinationSet {} without any"
946 + "next hops {}", ds, neighbors);
947 return;
948 }
949 ObjectiveContext context = new DefaultObjectiveContext(
950 (objective) ->
951 log.debug("createGroupsFromDestinationSet installed "
952 + "NextObj {} on {}", nextId, deviceId),
953 (objective, error) ->
954 log.warn("createGroupsFromDestinationSet failed to install"
955 + " NextObj {} on {}: {}", nextId, deviceId, error)
956 );
957 NextObjective nextObj = nextObjBuilder.add(context);
958 log.debug(".. createGroupsFromDestinationSet: Submitted "
959 + "next objective {} in device {}", nextId, deviceId);
960 flowObjectiveService.next(deviceId, nextObj);
961 //update store
962 dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds),
963 new NextNeighbors(dstNextHops, nextId));
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700964 }
965
Saurav Das2d94d312015-11-24 23:21:05 -0800966 /**
Saurav Dasf0f592d2016-11-18 15:21:57 -0800967 * Creates broadcast groups for all ports in the same subnet for
968 * all configured subnets.
Saurav Das2d94d312015-11-24 23:21:05 -0800969 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800970 public void createGroupsFromVlanConfig() {
Charles Chan90772a72017-02-08 15:52:08 -0800971 srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> {
Charles Chan10b0fb72017-02-02 16:20:42 -0800972 createBcastGroupFromVlan(vlanId, ports);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800973 });
Saurav Dasf0f592d2016-11-18 15:21:57 -0800974 }
Charles Chanc6ad7752015-10-29 14:58:10 -0700975
Saurav Dasf0f592d2016-11-18 15:21:57 -0800976 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800977 * Creates a single broadcast group from a given vlan id and list of ports.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800978 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800979 * @param vlanId vlan id
Saurav Dasf0f592d2016-11-18 15:21:57 -0800980 * @param ports list of ports in the subnet
981 */
Charles Chan90772a72017-02-08 15:52:08 -0800982 public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800983 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
Charles Chanc6ad7752015-10-29 14:58:10 -0700984
Charles Chan10b0fb72017-02-02 16:20:42 -0800985 if (vlanNextObjStore.containsKey(key)) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800986 log.debug("Broadcast group for device {} and subnet {} exists",
Charles Chan10b0fb72017-02-02 16:20:42 -0800987 deviceId, vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -0800988 return;
989 }
Charles Chande6655c2015-12-23 00:15:11 -0800990
Saurav Dasf0f592d2016-11-18 15:21:57 -0800991 TrafficSelector metadata =
Charles Chan10b0fb72017-02-02 16:20:42 -0800992 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
Charles Chan77277672015-10-20 16:24:19 -0700993
Saurav Dasf0f592d2016-11-18 15:21:57 -0800994 int nextId = flowObjectiveService.allocateNextId();
Charles Chan77277672015-10-20 16:24:19 -0700995
Saurav Dasf0f592d2016-11-18 15:21:57 -0800996 NextObjective.Builder nextObjBuilder = DefaultNextObjective
997 .builder().withId(nextId)
998 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
999 .withMeta(metadata);
Charles Chan77277672015-10-20 16:24:19 -07001000
Saurav Dasf0f592d2016-11-18 15:21:57 -08001001 ports.forEach(port -> {
1002 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
Charles Chan90772a72017-02-08 15:52:08 -08001003 if (toPopVlan(port, vlanId)) {
1004 tBuilder.popVlan();
1005 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001006 tBuilder.setOutput(port);
1007 nextObjBuilder.addTreatment(tBuilder.build());
Charles Chan77277672015-10-20 16:24:19 -07001008 });
Saurav Dasf0f592d2016-11-18 15:21:57 -08001009
Saurav Das2cb38292017-03-29 19:09:17 -07001010 ObjectiveContext context = new DefaultObjectiveContext(
1011 (objective) ->
1012 log.debug("createBroadcastGroupFromVlan installed "
1013 + "NextObj {} on {}", nextId, deviceId),
1014 (objective, error) ->
1015 log.warn("createBroadcastGroupFromVlan failed to install"
1016 + " NextObj {} on {}: {}", nextId, deviceId, error)
1017 );
1018 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001019 flowObjectiveService.next(deviceId, nextObj);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001020 log.debug("createBcastGroupFromVlan: Submitted next objective {} in device {}",
Saurav Dasf0f592d2016-11-18 15:21:57 -08001021 nextId, deviceId);
1022
Charles Chan10b0fb72017-02-02 16:20:42 -08001023 vlanNextObjStore.put(key, nextId);
Charles Chan77277672015-10-20 16:24:19 -07001024 }
1025
Charles Chanb7f75ac2016-01-11 18:28:54 -08001026 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001027 * Removes a single broadcast group from a given vlan id.
1028 * The group should be empty.
1029 * @param deviceId device Id to remove the group
1030 * @param portNum port number related to the group
1031 * @param vlanId vlan id of the broadcast group to remove
1032 * @param popVlan true if the TrafficTreatment involves pop vlan tag action
1033 */
1034 public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum,
1035 VlanId vlanId, boolean popVlan) {
1036 VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId);
1037
1038 if (!vlanNextObjStore.containsKey(key)) {
1039 log.debug("Broadcast group for device {} and subnet {} does not exist",
1040 deviceId, vlanId);
1041 return;
1042 }
1043
1044 TrafficSelector metadata =
1045 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1046
1047 int nextId = vlanNextObjStore.get(key);
1048
1049 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1050 .builder().withId(nextId)
1051 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1052 .withMeta(metadata);
1053
1054 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1055 if (popVlan) {
1056 tBuilder.popVlan();
1057 }
1058 tBuilder.setOutput(portNum);
1059 nextObjBuilder.addTreatment(tBuilder.build());
1060
1061 ObjectiveContext context = new DefaultObjectiveContext(
1062 (objective) ->
1063 log.debug("removeBroadcastGroupFromVlan removed "
1064 + "NextObj {} on {}", nextId, deviceId),
1065 (objective, error) ->
1066 log.warn("removeBroadcastGroupFromVlan failed to remove "
1067 + " NextObj {} on {}: {}", nextId, deviceId, error)
1068 );
1069 NextObjective nextObj = nextObjBuilder.remove(context);
1070 flowObjectiveService.next(deviceId, nextObj);
1071 log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}",
1072 nextId, deviceId);
1073
1074 vlanNextObjStore.remove(key, nextId);
1075 }
1076
1077 /**
Charles Chan90772a72017-02-08 15:52:08 -08001078 * Determine if we should pop given vlan before sending packets to the given port.
1079 *
1080 * @param portNumber port number
1081 * @param vlanId vlan id
1082 * @return true if the vlan id is not contained in any vlanTagged config
1083 */
1084 private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) {
Saurav Das261c3002017-06-13 15:35:54 -07001085 return srManager.interfaceService
1086 .getInterfacesByPort(new ConnectPoint(deviceId, portNumber))
Charles Chan90772a72017-02-08 15:52:08 -08001087 .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId));
1088 }
1089
1090 /**
Saurav Das2d94d312015-11-24 23:21:05 -08001091 * Create simple next objective for a single port. The treatments can include
1092 * all outgoing actions that need to happen on the packet.
1093 *
1094 * @param portNum the outgoing port on the device
1095 * @param treatment the actions to apply on the packets (should include outport)
1096 * @param meta optional data to pass to the driver
1097 */
1098 public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1099 TrafficSelector meta) {
1100 int nextId = flowObjectiveService.allocateNextId();
1101 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
Saurav Das368cf212017-03-15 15:15:14 -07001102 deviceId, portNum, treatment, meta);
Saurav Das2d94d312015-11-24 23:21:05 -08001103
1104 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1105 .builder().withId(nextId)
1106 .withType(NextObjective.Type.SIMPLE)
1107 .addTreatment(treatment)
1108 .fromApp(appId)
1109 .withMeta(meta);
1110
Saurav Das2cb38292017-03-29 19:09:17 -07001111 ObjectiveContext context = new DefaultObjectiveContext(
1112 (objective) ->
1113 log.debug("createGroupFromPort installed "
1114 + "NextObj {} on {}", nextId, deviceId),
1115 (objective, error) ->
1116 log.warn("createGroupFromPort failed to install"
1117 + " NextObj {} on {}: {}", nextId, deviceId, error)
1118 );
1119 NextObjective nextObj = nextObjBuilder.add(context);
Saurav Das2d94d312015-11-24 23:21:05 -08001120 flowObjectiveService.next(deviceId, nextObj);
1121 log.debug("createGroupFromPort: Submited next objective {} in device {} "
1122 + "for port {}", nextId, deviceId, portNum);
1123
1124 portNextObjStore.put(key, nextId);
1125 }
1126
sangho27462c62015-05-14 00:39:53 -07001127 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001128 * Removes simple next objective for a single port.
1129 *
1130 * @param deviceId device id that has the port to deal with
1131 * @param portNum the outgoing port on the device
1132 * @param vlanId vlan id associated with the port
1133 * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise
1134 */
1135 public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) {
1136 TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();
1137 mbuilder.matchVlanId(vlanId);
1138
1139 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
1140 tbuilder.immediate().setOutput(portNum);
1141 if (popVlan) {
1142 tbuilder.immediate().popVlan();
1143 }
1144
1145 int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
1146 tbuilder.build(), mbuilder.build(), false);
1147
1148 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1149 deviceId, portNum, tbuilder.build(), mbuilder.build());
1150 if (portNextObjId != -1 && portNextObjStore.containsKey(key)) {
1151 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1152 .builder().withId(portNextObjId)
1153 .withType(NextObjective.Type.SIMPLE).fromApp(appId);
1154 ObjectiveContext context = new DefaultObjectiveContext(
1155 (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}",
1156 portNextObjId, deviceId),
1157 (objective, error) ->
1158 log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}",
1159 portNextObjId, deviceId, error));
1160 NextObjective nextObjective = nextObjBuilder.remove(context);
1161 log.info("**removePortNextObjective: Submitted "
1162 + "next objective {} in device {}",
1163 portNextObjId, deviceId);
1164 flowObjectiveService.next(deviceId, nextObjective);
1165
1166 portNextObjStore.remove(key);
1167 }
1168 }
1169 /**
sangho27462c62015-05-14 00:39:53 -07001170 * Removes groups for the next objective ID given.
1171 *
1172 * @param objectiveId next objective ID to remove
1173 * @return true if succeeds, false otherwise
1174 */
1175 public boolean removeGroup(int objectiveId) {
Saurav Das261c3002017-06-13 15:35:54 -07001176 for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e :
1177 dsNextObjStore.entrySet()) {
1178 if (e.getValue().nextId() != objectiveId) {
1179 continue;
1180 }
sangho27462c62015-05-14 00:39:53 -07001181 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1182 .builder().withId(objectiveId)
1183 .withType(NextObjective.Type.HASHED).fromApp(appId);
Charles Chana4ee4f92016-04-23 14:48:16 -07001184 ObjectiveContext context = new DefaultObjectiveContext(
1185 (objective) -> log.debug("RemoveGroup removes NextObj {} on {}",
1186 objectiveId, deviceId),
1187 (objective, error) ->
1188 log.warn("RemoveGroup failed to remove NextObj {} on {}: {}",
1189 objectiveId, deviceId, error));
1190 NextObjective nextObjective = nextObjBuilder.remove(context);
Saurav Das4c35fc42015-11-20 15:27:53 -08001191 log.info("**removeGroup: Submited "
1192 + "next objective {} in device {}",
1193 objectiveId, deviceId);
sangho27462c62015-05-14 00:39:53 -07001194 flowObjectiveService.next(deviceId, nextObjective);
1195
Saurav Das261c3002017-06-13 15:35:54 -07001196 dsNextObjStore.remove(e.getKey());
sangho4a5c42a2015-05-20 22:16:38 -07001197 return true;
sangho27462c62015-05-14 00:39:53 -07001198 }
1199
1200 return false;
1201 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001202 /**
1203 * Remove simple next objective for a single port. The treatments can include
1204 * all outgoing actions that need to happen on the packet.
1205 *
1206 * @param portNum the outgoing port on the device
1207 * @param treatment the actions applied on the packets (should include outport)
1208 * @param meta optional data to pass to the driver
1209 */
1210 public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment,
1211 TrafficSelector meta) {
1212 PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey(
1213 deviceId, portNum, treatment, meta);
1214 Integer nextId = portNextObjStore.get(key);
1215
1216 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1217 .builder().withId(nextId)
1218 .withType(NextObjective.Type.SIMPLE)
1219 .addTreatment(treatment)
1220 .fromApp(appId)
1221 .withMeta(meta);
1222
1223 ObjectiveContext context = new DefaultObjectiveContext(
1224 (objective) ->
1225 log.info("removeGroupFromPort installed "
1226 + "NextObj {} on {}", nextId, deviceId),
1227 (objective, error) ->
1228 log.warn("removeGroupFromPort failed to install"
1229 + " NextObj {} on {}: {}", nextId, deviceId, error)
1230 );
1231 NextObjective nextObj = nextObjBuilder.remove(context);
1232 flowObjectiveService.next(deviceId, nextObj);
1233 log.info("removeGroupFromPort: Submitted next objective {} in device {} "
1234 + "for port {}", nextId, deviceId, portNum);
1235
1236 portNextObjStore.remove(key);
1237 }
Srikanth Vavilapalli8c83f1d2015-05-22 13:47:31 -07001238
Charles Chanb7f75ac2016-01-11 18:28:54 -08001239 /**
1240 * Removes all groups from all next objective stores.
1241 */
Saurav Das261c3002017-06-13 15:35:54 -07001242 /*public void removeAllGroups() {
1243 for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry:
Saurav Das62af8802015-12-04 10:52:59 -08001244 nsNextObjStore.entrySet()) {
Saurav Das261c3002017-06-13 15:35:54 -07001245 removeGroup(entry.getValue().nextId());
Saurav Das62af8802015-12-04 10:52:59 -08001246 }
1247 for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry:
1248 portNextObjStore.entrySet()) {
1249 removeGroup(entry.getValue());
1250 }
Charles Chan10b0fb72017-02-02 16:20:42 -08001251 for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry:
1252 vlanNextObjStore.entrySet()) {
Saurav Das62af8802015-12-04 10:52:59 -08001253 removeGroup(entry.getValue());
1254 }
Saurav Das261c3002017-06-13 15:35:54 -07001255 }*/ //XXX revisit
1256
Saurav Dasfbe74572017-08-03 18:30:35 -07001257 /**
1258 * Triggers a one time bucket verification operation on all hash groups
1259 * on this device.
1260 */
1261 public void triggerBucketCorrector() {
1262 BucketCorrector bc = new BucketCorrector();
1263 bc.run();
1264 }
1265
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001266 public void updateGroupFromVlanConfiguration(PortNumber portNumber, Collection<VlanId> vlanIds,
1267 int nextId, boolean install) {
1268 vlanIds.forEach(vlanId -> updateGroupFromVlanInternal(vlanId, portNumber, nextId, install));
1269 }
1270
1271 private void updateGroupFromVlanInternal(VlanId vlanId, PortNumber portNum, int nextId, boolean install) {
1272 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1273 if (toPopVlan(portNum, vlanId)) {
1274 tBuilder.popVlan();
1275 }
1276 tBuilder.setOutput(portNum);
1277
1278 TrafficSelector metadata =
1279 DefaultTrafficSelector.builder().matchVlanId(vlanId).build();
1280
1281 NextObjective.Builder nextObjBuilder = DefaultNextObjective
1282 .builder().withId(nextId)
1283 .withType(NextObjective.Type.BROADCAST).fromApp(appId)
1284 .addTreatment(tBuilder.build())
1285 .withMeta(metadata);
1286
1287 ObjectiveContext context = new DefaultObjectiveContext(
1288 (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}",
1289 portNum, nextId, deviceId),
1290 (objective, error) ->
1291 log.warn("port {} failed to removedFrom NextObj {} on {}: {}",
1292 portNum, nextId, deviceId, error));
1293
1294 if (install) {
1295 flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context));
1296 } else {
1297 flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context));
1298 }
1299 }
Saurav Das8a3022d2017-05-05 17:01:08 -07001300
1301 /**
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001302 * Performs bucket verification operation for all hash groups in this device.
1303 * Checks RouteHandler to ensure that routing is stable before attempting
1304 * verification. Verification involves creating a nextObjective with
1305 * operation VERIFY for existing next objectives in the store, and passing
1306 * it to the driver. It is the driver that actually performs the verification
1307 * by adding or removing buckets to match the verification next objective
1308 * created here.
Saurav Das8a3022d2017-05-05 17:01:08 -07001309 */
Saurav Dasfbe74572017-08-03 18:30:35 -07001310 protected final class BucketCorrector implements Runnable {
1311 Integer nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001312
Saurav Dasfbe74572017-08-03 18:30:35 -07001313 BucketCorrector() {
1314 this.nextId = null;
1315 }
1316
1317 BucketCorrector(Integer nextId) {
1318 this.nextId = nextId;
Saurav Das8a3022d2017-05-05 17:01:08 -07001319 }
1320
1321 @Override
1322 public void run() {
Saurav Dasfbe74572017-08-03 18:30:35 -07001323 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1324 return;
Saurav Das8a3022d2017-05-05 17:01:08 -07001325 }
Saurav Dasfbe74572017-08-03 18:30:35 -07001326 DefaultRoutingHandler rh = srManager.getRoutingHandler();
1327 if (rh == null) {
1328 return;
1329 }
1330 if (!rh.isRoutingStable()) {
1331 return;
1332 }
1333 rh.acquireRoutingLock();
1334 try {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001335 log.trace("running bucket corrector for dev: {}", deviceId);
Saurav Dasfbe74572017-08-03 18:30:35 -07001336 Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet()
1337 .stream()
1338 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
1339 .map(entry -> entry.getKey())
1340 .collect(Collectors.toSet());
1341 for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) {
1342 NextNeighbors next = dsNextObjStore.get(dsKey);
1343 if (next == null) {
1344 continue;
1345 }
1346 int nid = next.nextId();
1347 if (nextId != null && nextId != nid) {
1348 continue;
1349 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001350 log.trace("bkt-corr: dsNextObjStore for device {}: {}",
Saurav Dasfbe74572017-08-03 18:30:35 -07001351 deviceId, dsKey, next);
1352 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
1353 metabuilder.matchVlanId(INTERNAL_VLAN);
1354 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder()
1355 .withId(nid)
1356 .withType(NextObjective.Type.HASHED)
1357 .withMeta(metabuilder.build())
1358 .fromApp(appId);
1359
1360 next.dstNextHops().forEach((dstDev, nextHops) -> {
1361 int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev);
1362 nextHops.forEach(neighbor -> {
1363 MacAddress neighborMac;
1364 try {
1365 neighborMac = deviceConfig.getDeviceMac(neighbor);
1366 } catch (DeviceConfigNotFoundException e) {
1367 log.warn(e.getMessage() + " Aborting neighbor"
1368 + neighbor);
1369 return;
1370 }
1371 devicePortMap.get(neighbor).forEach(port -> {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001372 log.trace("verify in device {} nextId {}: bucket with"
Saurav Dasfbe74572017-08-03 18:30:35 -07001373 + " port/label {}/{} to dst {} via {}",
1374 deviceId, nid, port, edgeLabel,
1375 dstDev, neighbor);
1376 nextObjBuilder.addTreatment(treatmentBuilder(port,
1377 neighborMac, edgeLabel));
1378 });
1379 });
1380 });
1381
1382 NextObjective nextObjective = nextObjBuilder.verify();
1383 flowObjectiveService.next(deviceId, nextObjective);
1384 }
1385 } finally {
1386 rh.releaseRoutingLock();
1387 }
1388
1389 }
1390
1391 TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac,
1392 int edgeLabel) {
1393 TrafficTreatment.Builder tBuilder =
1394 DefaultTrafficTreatment.builder();
1395 tBuilder.setOutput(outport)
1396 .setEthDst(dstMac)
1397 .setEthSrc(nodeMacAddr);
1398 if (edgeLabel != DestinationSet.NO_EDGE_LABEL) {
1399 tBuilder.pushMpls()
1400 .copyTtlOut()
1401 .setMpls(MplsLabel.mplsLabel(edgeLabel));
1402 }
1403 return tBuilder.build();
Saurav Das8a3022d2017-05-05 17:01:08 -07001404 }
1405 }
1406
Saurav Dasfbe74572017-08-03 18:30:35 -07001407}