Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.grouphandler; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 17 | |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Iterables; |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
| 22 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 23 | import org.apache.commons.lang3.RandomUtils; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
sangho | 32a5932 | 2015-02-17 12:07:41 -0800 | [diff] [blame] | 25 | import org.onlab.packet.MplsLabel; |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 27 | import org.onlab.util.KryoNamespace; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.PortNumber; |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 40 | import org.onosproject.net.flowobjective.NextObjective; |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 42 | import org.onosproject.net.link.LinkService; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 43 | import org.onosproject.segmentrouting.DefaultRoutingHandler; |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 44 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 45 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 46 | import org.onosproject.segmentrouting.config.DeviceProperties; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 47 | import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 48 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 49 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 50 | import org.onosproject.store.service.EventuallyConsistentMap; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 53 | import java.net.URI; |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 54 | import java.util.Collection; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 55 | import java.util.Collections; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 56 | import java.util.HashMap; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 57 | import java.util.HashSet; |
| 58 | import java.util.List; |
| 59 | import java.util.Map; |
| 60 | import java.util.Set; |
| 61 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 62 | import java.util.concurrent.ScheduledExecutorService; |
| 63 | import java.util.concurrent.TimeUnit; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static com.google.common.base.Preconditions.checkNotNull; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 67 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 68 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 69 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 70 | import static org.slf4j.LoggerFactory.getLogger; |
| 71 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 72 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 73 | * 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 76 | */ |
| 77 | public class DefaultGroupHandler { |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 78 | protected static final Logger log = getLogger(DefaultGroupHandler.class); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 79 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 80 | private static final long VERIFY_INTERVAL = 30; // secs |
| 81 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 82 | protected final DeviceId deviceId; |
| 83 | protected final ApplicationId appId; |
| 84 | protected final DeviceProperties deviceConfig; |
| 85 | protected final List<Integer> allSegmentIds; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 86 | protected int ipv4NodeSegmentId = -1; |
| 87 | protected int ipv6NodeSegmentId = -1; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 88 | protected boolean isEdgeRouter = false; |
| 89 | protected MacAddress nodeMacAddr = null; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 90 | protected LinkService linkService; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 91 | protected FlowObjectiveService flowObjectiveService; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 92 | /** |
| 93 | * local store for neighbor-device-ids and the set of ports on this device |
| 94 | * that connect to the same neighbor. |
| 95 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 96 | protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap = |
| 97 | new ConcurrentHashMap<>(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 98 | /** |
| 99 | * local store for ports on this device connected to neighbor-device-id. |
| 100 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 101 | protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap = |
| 102 | new ConcurrentHashMap<>(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 103 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 104 | // distributed store for (device+destination-set) mapped to next-id and neighbors |
| 105 | protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> |
| 106 | dsNextObjStore = null; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 107 | // distributed store for (device+subnet-ip-prefix) mapped to next-id |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 108 | protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> |
| 109 | vlanNextObjStore = null; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 110 | // distributed store for (device+port+treatment) mapped to next-id |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 111 | protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
| 112 | portNextObjStore = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 113 | private SegmentRoutingManager srManager; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 114 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 115 | private ScheduledExecutorService executorService |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 116 | = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log)); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 117 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 118 | protected KryoNamespace.Builder kryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 119 | .register(URI.class).register(HashSet.class) |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 120 | .register(DeviceId.class).register(PortNumber.class) |
| 121 | .register(DestinationSet.class).register(PolicyGroupIdentifier.class) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 122 | .register(PolicyGroupParams.class) |
| 123 | .register(GroupBucketIdentifier.class) |
| 124 | .register(GroupBucketIdentifier.BucketOutputType.class); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 125 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 126 | protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId, |
| 127 | DeviceProperties config, |
| 128 | LinkService linkService, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 129 | FlowObjectiveService flowObjService, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 130 | SegmentRoutingManager srManager) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 131 | this.deviceId = checkNotNull(deviceId); |
| 132 | this.appId = checkNotNull(appId); |
| 133 | this.deviceConfig = checkNotNull(config); |
| 134 | this.linkService = checkNotNull(linkService); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 135 | this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds()); |
| 136 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 137 | this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId); |
| 138 | this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 139 | 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 Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 145 | this.flowObjectiveService = flowObjService; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 146 | this.dsNextObjStore = srManager.dsNextObjStore(); |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 147 | this.vlanNextObjStore = srManager.vlanNextObjStore(); |
| 148 | this.portNextObjStore = srManager.portNextObjStore(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 149 | this.srManager = srManager; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 150 | executorService.scheduleWithFixedDelay(new BucketCorrector(), 10, |
| 151 | VERIFY_INTERVAL, |
| 152 | TimeUnit.SECONDS); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 153 | populateNeighborMaps(); |
| 154 | } |
| 155 | |
| 156 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 157 | * 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 165 | * Creates a group handler object. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 166 | * |
| 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 Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 171 | * @param flowObjService flow objective service object |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 172 | * @param srManager segment routing manager |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 173 | * @throws DeviceConfigNotFoundException if the device configuration is not found |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 174 | * @return default group handler type |
| 175 | */ |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 176 | public static DefaultGroupHandler createGroupHandler( |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 177 | DeviceId deviceId, |
| 178 | ApplicationId appId, |
| 179 | DeviceProperties config, |
| 180 | LinkService linkService, |
| 181 | FlowObjectiveService flowObjService, |
| 182 | SegmentRoutingManager srManager) |
| 183 | throws DeviceConfigNotFoundException { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 184 | return new DefaultGroupHandler(deviceId, appId, config, |
| 185 | linkService, |
| 186 | flowObjService, |
| 187 | srManager); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 191 | * Updates local stores for link-src device/port to neighbor (link-dst). |
| 192 | * |
| 193 | * @param link the infrastructure link |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 194 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 195 | public void portUpForLink(Link link) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 196 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 201 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 202 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 208 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 209 | /** |
| 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 219 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 220 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 225 | |
| 226 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 227 | * 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 232 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 233 | * @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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 237 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 238 | public void retryHash(Link link, boolean linkDown, boolean firstTime) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 239 | MacAddress neighborMac; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 240 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 241 | neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId()); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 242 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 243 | log.warn(e.getMessage() + " Aborting retryHash."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 244 | return; |
| 245 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 246 | // find all the destinationSets related to link |
| 247 | Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet() |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 248 | .stream() |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 249 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 250 | .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId())) |
| 251 | .map(entry -> entry.getKey()) |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 252 | .collect(Collectors.toSet()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 253 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 254 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 260 | log.warn("retryHash in device {}, but global store has no record " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 261 | + "for dsKey:{}", deviceId, dsKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 262 | continue; |
| 263 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 264 | int nextId = nextHops.nextId(); |
| 265 | Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 266 | if (!linkDown) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 267 | List<PortLabel> pl = Lists.newArrayList(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 268 | 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 274 | dstSet.forEach(dst -> { |
| 275 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 276 | pl.add(new PortLabel(p, edgeLabel)); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 277 | }); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 278 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 279 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 287 | } |
| 288 | } else { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 289 | // linkdown |
| 290 | List<PortLabel> pl = Lists.newArrayList(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 291 | dstSet.forEach(dst -> { |
| 292 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 293 | pl.add(new PortLabel(link.src().port(), edgeLabel)); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 294 | }); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 295 | removeFromHashedNextObjective(pl, neighborMac, nextId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 296 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 297 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 300 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 301 | * 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 327 | * |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 328 | * @param portLables a collection of port & label combinations to add |
| 329 | * to the hash group identified by the nextId |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 330 | * @param dstMac destination mac address of next-hop |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 331 | * @param nextId id for next-objective to which buckets will be added |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 332 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 333 | */ |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 334 | private void addToHashedNextObjective(Collection<PortLabel> portLabels, |
| 335 | MacAddress dstMac, Integer nextId) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 336 | // 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 Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 341 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 342 | .withId(nextId) |
| 343 | .withType(NextObjective.Type.HASHED) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 344 | .withMeta(metabuilder.build()) |
| 345 | .fromApp(appId); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 346 | // 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 Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 362 | |
| 363 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 364 | (objective) -> log.debug("addToHash port/label {} addedTo " |
| 365 | + "NextObj {} on {}", portLabels, nextId, deviceId), |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 366 | (objective, error) -> |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 367 | log.warn("addToHash failed to add port/label {} to" |
| 368 | + " NextObj {} on {}: {}", portLabels, |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 369 | nextId, deviceId, error)); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 370 | NextObjective nextObjective = nextObjBuilder.addToExisting(context); |
| 371 | flowObjectiveService.next(deviceId, nextObjective); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 375 | * 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 Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 378 | * |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 379 | * @param portLables a collection of port & label combinations to remove |
| 380 | * from the hash group identified by the nextId |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 381 | * @param dstMac destination mac address of next-hop |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 382 | * @param nextId id for next-objective from which buckets will be removed |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 383 | */ |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 384 | private void removeFromHashedNextObjective(Collection<PortLabel> portLabels, |
| 385 | MacAddress dstMac, Integer nextId) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 386 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 387 | .builder() |
| 388 | .withType(NextObjective.Type.HASHED) //same as original |
| 389 | .withId(nextId) |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 390 | .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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 406 | |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 407 | 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 Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 414 | flowObjectiveService.next(deviceId, nextObjective); |
| 415 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 416 | |
| 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 423 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 424 | * @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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 431 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 432 | public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops, |
| 433 | DeviceId destSw, boolean revoke) { |
| 434 | // temporary storage of keys to be updated |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 435 | Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore = |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 436 | new HashMap<>(); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 437 | boolean foundNextObjective = false, success = true; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 438 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 439 | // retrieve hash-groups meant for destSw, which have destinationSets |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 440 | // with different neighbors than the given next-hops |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 441 | for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) { |
| 442 | if (!dskey.deviceId().equals(targetSw) || |
| 443 | !dskey.destinationSet().getDestinationSwitches().contains(destSw)) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | foundNextObjective = true; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 447 | 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 Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 451 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 452 | 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 Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 464 | boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, |
| 465 | revoke); |
| 466 | if (suc) { |
| 467 | // to update neighbor set with changes made |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 468 | if (revoke) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 469 | tempStore.put(dskey, Sets.difference(currNeighbors, diff)); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 470 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 471 | tempStore.put(dskey, Sets.union(currNeighbors, diff)); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 472 | } |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 473 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 474 | success &= suc; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 477 | 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 483 | // update the dsNextObjectiveStore with new destinationSet to nextId mappings |
| 484 | for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 485 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 490 | continue; |
| 491 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 492 | 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 Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 505 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 506 | // even if one fails and others succeed, return false so ECMPspg not updated |
| 507 | return success; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 510 | /** |
| 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 522 | 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 Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 558 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 559 | * 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 Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 562 | * |
| 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 572 | int nextId, boolean revoke) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 573 | for (DeviceId neighbor : neighbors) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 574 | MacAddress neighborMac; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 575 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 576 | neighborMac = deviceConfig.getDeviceMac(neighbor); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 577 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 578 | log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop" |
| 579 | + " for nextId:" + nextId); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 580 | return false; |
| 581 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 582 | Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor); |
| 583 | if (portsToNeighbor == null || portsToNeighbor.isEmpty()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 584 | log.warn("No ports found in dev:{} for neighbor:{} .. cannot " |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 585 | + "updateAllPortsToNextHop for nextId: {}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 586 | deviceId, neighbor, nextId); |
| 587 | return false; |
| 588 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 589 | List<PortLabel> pl = Lists.newArrayList(); |
| 590 | portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel))); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 591 | if (revoke) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 592 | 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 596 | } else { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 597 | 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | return true; |
| 604 | } |
| 605 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 606 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 607 | * 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 Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 609 | * |
| 610 | * @param port the port on this device that needs to be added/removed to a bcast group |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 611 | * @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 Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 615 | * @param portUp true if port is enabled, false if disabled |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 616 | */ |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 617 | public void processEdgePort(PortNumber port, VlanId vlanId, |
| 618 | boolean popVlan, boolean portUp) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 619 | //get the next id for the subnet and edit it. |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 620 | Integer nextId = getVlanNextObjectiveId(vlanId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 621 | if (nextId == -1) { |
| 622 | if (portUp) { |
| 623 | log.debug("**Creating flooding group for first port enabled in" |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 624 | + " subnet {} on dev {} port {}", vlanId, deviceId, port); |
| 625 | createBcastGroupFromVlan(vlanId, Collections.singleton(port)); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 626 | } else { |
| 627 | log.warn("Could not find flooding group for subnet {} on dev:{} when" |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 628 | + " removing port:{}", vlanId, deviceId, port); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 629 | } |
| 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 Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 639 | if (popVlan) { |
| 640 | tBuilder.popVlan(); |
| 641 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 642 | tBuilder.setOutput(port); |
| 643 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 644 | TrafficSelector metadata = |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 645 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 646 | |
| 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 670 | * 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 Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 675 | * passed in for the creation of the next objective. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 676 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 677 | * @param ds destination set |
| 678 | * @param nextHops a map of per destination next hops |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 679 | * @param meta metadata passed into the creation of a Next Objective |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 680 | * @param isBos if Bos is set |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 681 | * @return int if found or -1 if there are errors in the creation of the |
| 682 | * neighbor set. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 683 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 684 | 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 Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 692 | log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}", |
| 693 | deviceId, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 694 | dsNextObjStore.entrySet() |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 695 | .stream() |
| 696 | .filter((nsStoreEntry) -> |
| 697 | (nsStoreEntry.getKey().deviceId().equals(deviceId))) |
| 698 | .collect(Collectors.toList())); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 699 | |
| 700 | createGroupFromDestinationSet(ds, nextHops, meta, isBos); |
| 701 | next = dsNextObjStore. |
| 702 | get(new DestinationSetNextObjectiveStoreKey(deviceId, ds)); |
| 703 | if (next == null) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 704 | log.warn("getNextObjectiveId: unable to create next objective"); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 705 | // failure in creating group |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 706 | return -1; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 707 | } else { |
| 708 | log.debug("getNextObjectiveId in device{}: Next objective id {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 709 | + "created for {}", deviceId, next.nextId(), ds); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 710 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 711 | } else { |
| 712 | log.trace("getNextObjectiveId in device{}: Next objective id {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 713 | + "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 Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 723 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 724 | return next.nextId(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 725 | } |
| 726 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 727 | /** |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 728 | * Returns the next objective of type broadcast associated with the vlan, |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 729 | * 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 Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 731 | * created at startup from network configuration. Typically this is used |
| 732 | * for L2 flooding within the subnet configured on the switch. |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 733 | * |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 734 | * @param vlanId vlan id |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 735 | * @return int if found or -1 |
| 736 | */ |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 737 | public int getVlanNextObjectiveId(VlanId vlanId) { |
| 738 | Integer nextId = vlanNextObjStore. |
| 739 | get(new VlanNextObjectiveStoreKey(deviceId, vlanId)); |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 740 | |
| 741 | return (nextId != null) ? nextId : -1; |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 745 | * 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 Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 748 | * creates one (if requested) and returns the id. Optionally metadata can be passed in for |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 749 | * 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 Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 752 | * |
| 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 Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 756 | * @param createIfMissing true if a next object should be created if not found |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 757 | * @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 Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 761 | TrafficSelector meta, boolean createIfMissing) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 762 | Integer nextId = portNextObjStore |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 763 | .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta)); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 764 | 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 Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 777 | if (nextId == null) { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 778 | log.warn("getPortNextObjectiveId: unable to create next obj" |
| 779 | + "for dev:{} port:{}", deviceId, portNum); |
| 780 | return -1; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 781 | } |
| 782 | return nextId; |
| 783 | } |
| 784 | |
| 785 | /** |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 786 | * 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 791 | public boolean hasNextObjectiveId(DestinationSet ns) { |
| 792 | NextNeighbors nextHops = dsNextObjStore. |
| 793 | get(new DestinationSetNextObjectiveStoreKey(deviceId, ns)); |
| 794 | if (nextHops == null) { |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 795 | return false; |
| 796 | } |
| 797 | |
| 798 | return true; |
| 799 | } |
| 800 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 801 | private void populateNeighborMaps() { |
| 802 | Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 803 | for (Link link : outgoingLinks) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 804 | if (link.type() != Link.Type.DIRECT) { |
| 805 | continue; |
| 806 | } |
| 807 | addNeighborAtPort(link.dst().deviceId(), link.src().port()); |
| 808 | } |
| 809 | } |
| 810 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 811 | protected void addNeighborAtPort(DeviceId neighborId, |
| 812 | PortNumber portToNeighbor) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 813 | // Update DeviceToPort database |
| 814 | log.debug("Device {} addNeighborAtPort: neighbor {} at port {}", |
| 815 | deviceId, neighborId, portToNeighbor); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 816 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // Update portToDevice database |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 825 | DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId); |
| 826 | if (prev != null) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 827 | log.debug("Device: {} port: {} already has neighbor: {} ", |
| 828 | deviceId, portToNeighbor, prev, neighborId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 832 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 833 | * Creates a NextObjective for a hash group in this device from a given |
| 834 | * DestinationSet. |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 835 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 836 | * @param ds the DestinationSet |
| 837 | * @param neighbors a map for each destination and its next-hops |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 838 | * @param meta metadata passed into the creation of a Next Objective |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 839 | * @param isBos if BoS is set |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 840 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 841 | 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 Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 878 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 879 | |
| 880 | if (foundSingleNeighbor) { |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | for (DeviceId neighborId : nextHops) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 885 | if (devicePortMap.get(neighborId) == null) { |
| 886 | log.warn("Neighbor {} is not in the port map yet for dev:{}", |
| 887 | neighborId, deviceId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 888 | return; |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 889 | } else if (devicePortMap.get(neighborId).isEmpty()) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 890 | log.warn("There are no ports for " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 891 | + "the Device {} in the port map yet", neighborId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 892 | return; |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 895 | MacAddress neighborMac; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 896 | try { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 897 | neighborMac = deviceConfig.getDeviceMac(neighborId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 898 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 899 | log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 900 | return; |
| 901 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 902 | // For each port to the neighbor, we create a new treatment |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 903 | 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 Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 909 | Iterables.get(devicePortMap.get(neighborId), |
| 910 | index)); |
| 911 | foundSingleNeighbor = true; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 912 | } |
| 913 | for (PortNumber sp : neighborPorts) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 914 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 915 | .builder(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 916 | tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr); |
| 917 | int edgeLabel = ds.getEdgeLabel(dst); |
| 918 | if (edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
Charles Chan | 68aa62d | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 919 | tBuilder.pushMpls() |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 920 | .copyTtlOut() |
| 921 | .setMpls(MplsLabel.mplsLabel(edgeLabel)); |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 922 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 923 | tBuilder.setOutput(sp); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 924 | nextObjBuilder.addTreatment(tBuilder.build()); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 925 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 939 | } |
| 940 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 941 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 942 | |
| 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 963 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 964 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 965 | /** |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 966 | * Creates broadcast groups for all ports in the same subnet for |
| 967 | * all configured subnets. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 968 | */ |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 969 | public void createGroupsFromVlanConfig() { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 970 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 971 | createBcastGroupFromVlan(vlanId, ports); |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 972 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 973 | } |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 974 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 975 | /** |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 976 | * Creates a single broadcast group from a given vlan id and list of ports. |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 977 | * |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 978 | * @param vlanId vlan id |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 979 | * @param ports list of ports in the subnet |
| 980 | */ |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 981 | public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) { |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 982 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 983 | |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 984 | if (vlanNextObjStore.containsKey(key)) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 985 | log.debug("Broadcast group for device {} and subnet {} exists", |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 986 | deviceId, vlanId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 987 | return; |
| 988 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 989 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 990 | TrafficSelector metadata = |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 991 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 992 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 993 | int nextId = flowObjectiveService.allocateNextId(); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 994 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 995 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 996 | .builder().withId(nextId) |
| 997 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 998 | .withMeta(metadata); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 999 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1000 | ports.forEach(port -> { |
| 1001 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1002 | if (toPopVlan(port, vlanId)) { |
| 1003 | tBuilder.popVlan(); |
| 1004 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1005 | tBuilder.setOutput(port); |
| 1006 | nextObjBuilder.addTreatment(tBuilder.build()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1007 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1008 | |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1009 | 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 Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1018 | flowObjectiveService.next(deviceId, nextObj); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1019 | log.debug("createBcastGroupFromVlan: Submited next objective {} in device {}", |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1020 | nextId, deviceId); |
| 1021 | |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1022 | vlanNextObjStore.put(key, nextId); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1025 | /** |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1026 | * Determine if we should pop given vlan before sending packets to the given port. |
| 1027 | * |
| 1028 | * @param portNumber port number |
| 1029 | * @param vlanId vlan id |
| 1030 | * @return true if the vlan id is not contained in any vlanTagged config |
| 1031 | */ |
| 1032 | private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1033 | return srManager.interfaceService |
| 1034 | .getInterfacesByPort(new ConnectPoint(deviceId, portNumber)) |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1035 | .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId)); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1039 | * Create simple next objective for a single port. The treatments can include |
| 1040 | * all outgoing actions that need to happen on the packet. |
| 1041 | * |
| 1042 | * @param portNum the outgoing port on the device |
| 1043 | * @param treatment the actions to apply on the packets (should include outport) |
| 1044 | * @param meta optional data to pass to the driver |
| 1045 | */ |
| 1046 | public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment, |
| 1047 | TrafficSelector meta) { |
| 1048 | int nextId = flowObjectiveService.allocateNextId(); |
| 1049 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 1050 | deviceId, portNum, treatment, meta); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1051 | |
| 1052 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1053 | .builder().withId(nextId) |
| 1054 | .withType(NextObjective.Type.SIMPLE) |
| 1055 | .addTreatment(treatment) |
| 1056 | .fromApp(appId) |
| 1057 | .withMeta(meta); |
| 1058 | |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1059 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1060 | (objective) -> |
| 1061 | log.debug("createGroupFromPort installed " |
| 1062 | + "NextObj {} on {}", nextId, deviceId), |
| 1063 | (objective, error) -> |
| 1064 | log.warn("createGroupFromPort failed to install" |
| 1065 | + " NextObj {} on {}: {}", nextId, deviceId, error) |
| 1066 | ); |
| 1067 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1068 | flowObjectiveService.next(deviceId, nextObj); |
| 1069 | log.debug("createGroupFromPort: Submited next objective {} in device {} " |
| 1070 | + "for port {}", nextId, deviceId, portNum); |
| 1071 | |
| 1072 | portNextObjStore.put(key, nextId); |
| 1073 | } |
| 1074 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1075 | /** |
| 1076 | * Removes groups for the next objective ID given. |
| 1077 | * |
| 1078 | * @param objectiveId next objective ID to remove |
| 1079 | * @return true if succeeds, false otherwise |
| 1080 | */ |
| 1081 | public boolean removeGroup(int objectiveId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1082 | for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e : |
| 1083 | dsNextObjStore.entrySet()) { |
| 1084 | if (e.getValue().nextId() != objectiveId) { |
| 1085 | continue; |
| 1086 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1087 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1088 | .builder().withId(objectiveId) |
| 1089 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 1090 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1091 | (objective) -> log.debug("RemoveGroup removes NextObj {} on {}", |
| 1092 | objectiveId, deviceId), |
| 1093 | (objective, error) -> |
| 1094 | log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", |
| 1095 | objectiveId, deviceId, error)); |
| 1096 | NextObjective nextObjective = nextObjBuilder.remove(context); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1097 | log.info("**removeGroup: Submited " |
| 1098 | + "next objective {} in device {}", |
| 1099 | objectiveId, deviceId); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1100 | flowObjectiveService.next(deviceId, nextObjective); |
| 1101 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1102 | dsNextObjStore.remove(e.getKey()); |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 1103 | return true; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | return false; |
| 1107 | } |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1108 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1109 | /** |
| 1110 | * Removes all groups from all next objective stores. |
| 1111 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1112 | /*public void removeAllGroups() { |
| 1113 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry: |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1114 | nsNextObjStore.entrySet()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1115 | removeGroup(entry.getValue().nextId()); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1116 | } |
| 1117 | for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry: |
| 1118 | portNextObjStore.entrySet()) { |
| 1119 | removeGroup(entry.getValue()); |
| 1120 | } |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1121 | for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry: |
| 1122 | vlanNextObjStore.entrySet()) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1123 | removeGroup(entry.getValue()); |
| 1124 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1125 | }*/ //XXX revisit |
| 1126 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1127 | /** |
| 1128 | * Triggers a one time bucket verification operation on all hash groups |
| 1129 | * on this device. |
| 1130 | */ |
| 1131 | public void triggerBucketCorrector() { |
| 1132 | BucketCorrector bc = new BucketCorrector(); |
| 1133 | bc.run(); |
| 1134 | } |
| 1135 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1136 | |
| 1137 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1138 | * Performs bucket verification operation for all hash groups in this device. |
| 1139 | * Checks RouteHandler to ensure that routing is stable before attempting |
| 1140 | * verification. Verification involves creating a nextObjective with |
| 1141 | * operation VERIFY for existing next objectives in the store, and passing |
| 1142 | * it to the driver. It is the driver that actually performs the verification |
| 1143 | * by adding or removing buckets to match the verification next objective |
| 1144 | * created here. |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1145 | */ |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1146 | protected final class BucketCorrector implements Runnable { |
| 1147 | Integer nextId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1148 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1149 | BucketCorrector() { |
| 1150 | this.nextId = null; |
| 1151 | } |
| 1152 | |
| 1153 | BucketCorrector(Integer nextId) { |
| 1154 | this.nextId = nextId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | @Override |
| 1158 | public void run() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1159 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1160 | return; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1161 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1162 | DefaultRoutingHandler rh = srManager.getRoutingHandler(); |
| 1163 | if (rh == null) { |
| 1164 | return; |
| 1165 | } |
| 1166 | if (!rh.isRoutingStable()) { |
| 1167 | return; |
| 1168 | } |
| 1169 | rh.acquireRoutingLock(); |
| 1170 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1171 | log.trace("running bucket corrector for dev: {}", deviceId); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1172 | Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet() |
| 1173 | .stream() |
| 1174 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 1175 | .map(entry -> entry.getKey()) |
| 1176 | .collect(Collectors.toSet()); |
| 1177 | for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) { |
| 1178 | NextNeighbors next = dsNextObjStore.get(dsKey); |
| 1179 | if (next == null) { |
| 1180 | continue; |
| 1181 | } |
| 1182 | int nid = next.nextId(); |
| 1183 | if (nextId != null && nextId != nid) { |
| 1184 | continue; |
| 1185 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1186 | log.trace("bkt-corr: dsNextObjStore for device {}: {}", |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1187 | deviceId, dsKey, next); |
| 1188 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 1189 | metabuilder.matchVlanId(INTERNAL_VLAN); |
| 1190 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 1191 | .withId(nid) |
| 1192 | .withType(NextObjective.Type.HASHED) |
| 1193 | .withMeta(metabuilder.build()) |
| 1194 | .fromApp(appId); |
| 1195 | |
| 1196 | next.dstNextHops().forEach((dstDev, nextHops) -> { |
| 1197 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev); |
| 1198 | nextHops.forEach(neighbor -> { |
| 1199 | MacAddress neighborMac; |
| 1200 | try { |
| 1201 | neighborMac = deviceConfig.getDeviceMac(neighbor); |
| 1202 | } catch (DeviceConfigNotFoundException e) { |
| 1203 | log.warn(e.getMessage() + " Aborting neighbor" |
| 1204 | + neighbor); |
| 1205 | return; |
| 1206 | } |
| 1207 | devicePortMap.get(neighbor).forEach(port -> { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1208 | log.trace("verify in device {} nextId {}: bucket with" |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1209 | + " port/label {}/{} to dst {} via {}", |
| 1210 | deviceId, nid, port, edgeLabel, |
| 1211 | dstDev, neighbor); |
| 1212 | nextObjBuilder.addTreatment(treatmentBuilder(port, |
| 1213 | neighborMac, edgeLabel)); |
| 1214 | }); |
| 1215 | }); |
| 1216 | }); |
| 1217 | |
| 1218 | NextObjective nextObjective = nextObjBuilder.verify(); |
| 1219 | flowObjectiveService.next(deviceId, nextObjective); |
| 1220 | } |
| 1221 | } finally { |
| 1222 | rh.releaseRoutingLock(); |
| 1223 | } |
| 1224 | |
| 1225 | } |
| 1226 | |
| 1227 | TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac, |
| 1228 | int edgeLabel) { |
| 1229 | TrafficTreatment.Builder tBuilder = |
| 1230 | DefaultTrafficTreatment.builder(); |
| 1231 | tBuilder.setOutput(outport) |
| 1232 | .setEthDst(dstMac) |
| 1233 | .setEthSrc(nodeMacAddr); |
| 1234 | if (edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
| 1235 | tBuilder.pushMpls() |
| 1236 | .copyTtlOut() |
| 1237 | .setMpls(MplsLabel.mplsLabel(edgeLabel)); |
| 1238 | } |
| 1239 | return tBuilder.build(); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1243 | } |