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; |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 47 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 48 | import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 49 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 50 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 51 | import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 52 | import org.onosproject.store.service.EventuallyConsistentMap; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 55 | import java.net.URI; |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 56 | import java.util.Collection; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 57 | import java.util.Collections; |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 58 | import java.util.HashMap; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 59 | import java.util.HashSet; |
| 60 | import java.util.List; |
| 61 | import java.util.Map; |
| 62 | import java.util.Set; |
| 63 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 64 | import java.util.concurrent.ScheduledExecutorService; |
| 65 | import java.util.concurrent.TimeUnit; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 66 | import java.util.stream.Collectors; |
| 67 | |
| 68 | import static com.google.common.base.Preconditions.checkNotNull; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 69 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 70 | import static org.onlab.util.Tools.groupedThreads; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 71 | import static org.slf4j.LoggerFactory.getLogger; |
| 72 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 73 | /** |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 74 | * Default ECMP group handler creation module. This component creates a set of |
| 75 | * ECMP groups for every neighbor that this device is connected to based on |
| 76 | * whether the current device is an edge device or a transit device. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 77 | */ |
| 78 | public class DefaultGroupHandler { |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 79 | private static final Logger log = getLogger(DefaultGroupHandler.class); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 80 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 81 | private static final long VERIFY_INTERVAL = 30; // secs |
| 82 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 83 | protected final DeviceId deviceId; |
| 84 | protected final ApplicationId appId; |
| 85 | protected final DeviceProperties deviceConfig; |
| 86 | protected final List<Integer> allSegmentIds; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 87 | protected int ipv4NodeSegmentId = -1; |
| 88 | protected int ipv6NodeSegmentId = -1; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 89 | protected boolean isEdgeRouter = false; |
| 90 | protected MacAddress nodeMacAddr = null; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 91 | protected LinkService linkService; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 92 | protected FlowObjectiveService flowObjectiveService; |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 93 | private DeviceConfiguration config; |
| 94 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 95 | /** |
| 96 | * local store for neighbor-device-ids and the set of ports on this device |
| 97 | * that connect to the same neighbor. |
| 98 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 99 | protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap = |
| 100 | new ConcurrentHashMap<>(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 101 | /** |
| 102 | * local store for ports on this device connected to neighbor-device-id. |
| 103 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 104 | protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap = |
| 105 | new ConcurrentHashMap<>(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 106 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 107 | // distributed store for (device+destination-set) mapped to next-id and neighbors |
| 108 | protected EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> |
| 109 | dsNextObjStore = null; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 110 | // distributed store for (device+subnet-ip-prefix) mapped to next-id |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 111 | protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> |
| 112 | vlanNextObjStore = null; |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 113 | // distributed store for (device+mac+vlan+treatment) mapped to next-id |
| 114 | protected EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> |
| 115 | macVlanNextObjStore = null; |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 116 | // distributed store for (device+port+treatment) mapped to next-id |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 117 | protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
| 118 | portNextObjStore = null; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 119 | private SegmentRoutingManager srManager; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 120 | |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 121 | private ScheduledExecutorService executorService |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 122 | = newScheduledThreadPool(1, groupedThreads("bktCorrector", "bktC-%d", log)); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 123 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 124 | protected KryoNamespace.Builder kryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 125 | .register(URI.class).register(HashSet.class) |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 126 | .register(DeviceId.class).register(PortNumber.class) |
| 127 | .register(DestinationSet.class).register(PolicyGroupIdentifier.class) |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 128 | .register(PolicyGroupParams.class) |
| 129 | .register(GroupBucketIdentifier.class) |
| 130 | .register(GroupBucketIdentifier.BucketOutputType.class); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 131 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 132 | protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId, |
| 133 | DeviceProperties config, |
| 134 | LinkService linkService, |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 135 | FlowObjectiveService flowObjService, |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 136 | SegmentRoutingManager srManager) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 137 | this.deviceId = checkNotNull(deviceId); |
| 138 | this.appId = checkNotNull(appId); |
| 139 | this.deviceConfig = checkNotNull(config); |
| 140 | this.linkService = checkNotNull(linkService); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 141 | this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds()); |
| 142 | try { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 143 | this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId); |
| 144 | this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 145 | this.isEdgeRouter = config.isEdgeDevice(deviceId); |
| 146 | this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId)); |
| 147 | } catch (DeviceConfigNotFoundException e) { |
| 148 | log.warn(e.getMessage() |
| 149 | + " Skipping value assignment in DefaultGroupHandler"); |
| 150 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 151 | this.flowObjectiveService = flowObjService; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 152 | this.dsNextObjStore = srManager.dsNextObjStore(); |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 153 | this.vlanNextObjStore = srManager.vlanNextObjStore(); |
| 154 | this.portNextObjStore = srManager.portNextObjStore(); |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 155 | this.macVlanNextObjStore = srManager.macVlanNextObjStore(); |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 156 | this.srManager = srManager; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 157 | executorService.scheduleWithFixedDelay(new BucketCorrector(), 10, |
| 158 | VERIFY_INTERVAL, |
| 159 | TimeUnit.SECONDS); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 160 | populateNeighborMaps(); |
| 161 | } |
| 162 | |
| 163 | /** |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 164 | * Gracefully shuts down a groupHandler. Typically called when the handler is |
| 165 | * no longer needed. |
| 166 | */ |
| 167 | public void shutdown() { |
| 168 | executorService.shutdown(); |
| 169 | } |
| 170 | |
| 171 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 172 | * Creates a group handler object. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 173 | * |
| 174 | * @param deviceId device identifier |
| 175 | * @param appId application identifier |
| 176 | * @param config interface to retrieve the device properties |
| 177 | * @param linkService link service object |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 178 | * @param flowObjService flow objective service object |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 179 | * @param srManager segment routing manager |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 180 | * @throws DeviceConfigNotFoundException if the device configuration is not found |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 181 | * @return default group handler type |
| 182 | */ |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 183 | public static DefaultGroupHandler createGroupHandler( |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 184 | DeviceId deviceId, |
| 185 | ApplicationId appId, |
| 186 | DeviceProperties config, |
| 187 | LinkService linkService, |
| 188 | FlowObjectiveService flowObjService, |
| 189 | SegmentRoutingManager srManager) |
| 190 | throws DeviceConfigNotFoundException { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 191 | return new DefaultGroupHandler(deviceId, appId, config, |
| 192 | linkService, |
| 193 | flowObjService, |
| 194 | srManager); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 198 | * Updates local stores for link-src-device/port to neighbor (link-dst) for |
| 199 | * link that has come up. |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 200 | * |
| 201 | * @param link the infrastructure link |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 202 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 203 | public void portUpForLink(Link link) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 204 | if (!link.src().deviceId().equals(deviceId)) { |
| 205 | log.warn("linkUp: deviceId{} doesn't match with link src {}", |
| 206 | deviceId, link.src().deviceId()); |
| 207 | return; |
| 208 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 209 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 210 | log.info("* portUpForLink: Device {} linkUp at local port {} to " |
| 211 | + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId()); |
| 212 | // ensure local state is updated even if linkup is aborted later on |
| 213 | addNeighborAtPort(link.dst().deviceId(), |
| 214 | link.src().port()); |
| 215 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 216 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 217 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 218 | * Updates local stores for link-src-device/port to neighbor (link-dst) for |
| 219 | * link that has gone down. |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 220 | * |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 221 | * @param link the infrastructure link |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 222 | */ |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 223 | public void portDownForLink(Link link) { |
| 224 | PortNumber port = link.src().port(); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 225 | if (portDeviceMap.get(port) == null) { |
| 226 | log.warn("portDown: unknown port"); |
| 227 | return; |
| 228 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 229 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 230 | log.debug("Device {} portDown {} to neighbor {}", deviceId, port, |
| 231 | portDeviceMap.get(port)); |
| 232 | devicePortMap.get(portDeviceMap.get(port)).remove(port); |
| 233 | portDeviceMap.remove(port); |
| 234 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 235 | |
| 236 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 237 | * Cleans up local stores for removed neighbor device. |
| 238 | * |
| 239 | * @param neighborId the device identifier for the neighbor device |
| 240 | */ |
| 241 | public void cleanUpForNeighborDown(DeviceId neighborId) { |
| 242 | Set<PortNumber> ports = devicePortMap.remove(neighborId); |
| 243 | if (ports != null) { |
| 244 | ports.forEach(p -> portDeviceMap.remove(p)); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 249 | * Checks all groups in the src-device of link for neighbor sets that include |
| 250 | * the dst-device of link, and edits the hash groups according to link up |
| 251 | * or down. Should only be called by the master instance of the src-switch |
| 252 | * of link. Typically used when there are no route-path changes due to the |
| 253 | * link up or down, as the ECMPspg does not change. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 254 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 255 | * @param link the infrastructure link that has gone down or come up |
| 256 | * @param linkDown true if link has gone down |
| 257 | * @param firstTime true if link has come up for the first time i.e a link |
| 258 | * not seen-before |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 259 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 260 | public void retryHash(Link link, boolean linkDown, boolean firstTime) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 261 | MacAddress neighborMac; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 262 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 263 | neighborMac = deviceConfig.getDeviceMac(link.dst().deviceId()); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 264 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 265 | log.warn(e.getMessage() + " Aborting retryHash."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 266 | return; |
| 267 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 268 | // find all the destinationSets related to link |
| 269 | Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet() |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 270 | .stream() |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 271 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 272 | // Filter out PW transit groups or include them if MPLS ECMP is supported |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 273 | .filter(entry -> !entry.getKey().destinationSet().notBos() || |
| 274 | (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp())) |
| 275 | // Filter out simple SWAP groups or include them if MPLS ECMP is supported |
| 276 | .filter(entry -> !entry.getKey().destinationSet().swap() || |
| 277 | (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp())) |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 278 | .filter(entry -> entry.getValue().containsNextHop(link.dst().deviceId())) |
| 279 | .map(entry -> entry.getKey()) |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 280 | .collect(Collectors.toSet()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 281 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 282 | log.debug("retryHash: dsNextObjStore contents for linkSrc {} -> linkDst {}: {}", |
| 283 | deviceId, link.dst().deviceId(), dsKeySet); |
| 284 | |
| 285 | for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) { |
| 286 | NextNeighbors nextHops = dsNextObjStore.get(dsKey); |
| 287 | if (nextHops == null) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 288 | log.warn("retryHash in device {}, but global store has no record " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 289 | + "for dsKey:{}", deviceId, dsKey); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 290 | continue; |
| 291 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 292 | int nextId = nextHops.nextId(); |
| 293 | Set<DeviceId> dstSet = nextHops.getDstForNextHop(link.dst().deviceId()); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 294 | if (!linkDown) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 295 | List<PortLabel> pl = Lists.newArrayList(); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 296 | if (firstTime) { |
| 297 | // some links may have come up before the next-objective was created |
| 298 | // we take this opportunity to ensure other ports to same next-hop-dst |
| 299 | // are part of the hash group (see CORD-1180). Duplicate additions |
| 300 | // to the same hash group are avoided by the driver. |
| 301 | for (PortNumber p : devicePortMap.get(link.dst().deviceId())) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 302 | dstSet.forEach(dst -> { |
| 303 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 304 | pl.add(new PortLabel(p, edgeLabel)); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 305 | }); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 306 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 307 | addToHashedNextObjective(pl, neighborMac, nextId); |
| 308 | } else { |
| 309 | // handle only the port that came up |
| 310 | dstSet.forEach(dst -> { |
| 311 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst); |
| 312 | pl.add(new PortLabel(link.src().port(), edgeLabel)); |
| 313 | }); |
| 314 | addToHashedNextObjective(pl, neighborMac, nextId); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 315 | } |
| 316 | } else { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 317 | // linkdown |
| 318 | List<PortLabel> pl = Lists.newArrayList(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 319 | dstSet.forEach(dst -> { |
| 320 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dst); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 321 | pl.add(new PortLabel(link.src().port(), edgeLabel)); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 322 | }); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 323 | removeFromHashedNextObjective(pl, neighborMac, nextId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 324 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 325 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 328 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 329 | * Utility class for associating output ports and the corresponding MPLS |
| 330 | * labels to push. In dual-homing, there are different labels to push |
| 331 | * corresponding to the destination switches in an edge-pair. If both |
| 332 | * destinations are reachable via the same spine, then the output-port to |
| 333 | * the spine will be associated with two labels i.e. there will be two |
| 334 | * PortLabel objects for the same port but with different labels. |
| 335 | */ |
| 336 | private class PortLabel { |
| 337 | PortNumber port; |
| 338 | int edgeLabel; |
| 339 | |
| 340 | PortLabel(PortNumber port, int edgeLabel) { |
| 341 | this.port = port; |
| 342 | this.edgeLabel = edgeLabel; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public String toString() { |
| 347 | return port.toString() + "/" + String.valueOf(edgeLabel); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Makes a call to the FlowObjective service to add buckets to |
| 353 | * a hashed group. User must ensure that all the ports & labels are meant |
| 354 | * same neighbor (ie. dstMac). |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 355 | * |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 356 | * @param portLabels a collection of port & label combinations to add |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 357 | * to the hash group identified by the nextId |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 358 | * @param dstMac destination mac address of next-hop |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 359 | * @param nextId id for next-objective to which buckets will be added |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 360 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 361 | */ |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 362 | private void addToHashedNextObjective(Collection<PortLabel> portLabels, |
| 363 | MacAddress dstMac, Integer nextId) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 364 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 365 | // if needed by the switch pipeline. Since hashed next-hops are always to |
| 366 | // other neighboring routers, there is no subnet assigned on those ports. |
| 367 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
Saurav Das | 3c82f19 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 368 | metabuilder.matchVlanId(srManager.getDefaultInternalVlan()); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 369 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 370 | .withId(nextId) |
| 371 | .withType(NextObjective.Type.HASHED) |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 372 | .withMeta(metabuilder.build()) |
| 373 | .fromApp(appId); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 374 | // Create the new buckets to be updated |
| 375 | portLabels.forEach(pl -> { |
| 376 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 377 | tBuilder.setOutput(pl.port) |
| 378 | .setEthDst(dstMac) |
| 379 | .setEthSrc(nodeMacAddr); |
| 380 | if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
| 381 | tBuilder.pushMpls() |
| 382 | .copyTtlOut() |
| 383 | .setMpls(MplsLabel.mplsLabel(pl.edgeLabel)); |
| 384 | } |
| 385 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 386 | }); |
| 387 | |
| 388 | log.debug("addToHash in device {}: Adding Bucket with port/label {} " |
| 389 | + "to nextId {}", deviceId, portLabels, nextId); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 390 | |
| 391 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 392 | (objective) -> log.debug("addToHash port/label {} addedTo " |
| 393 | + "NextObj {} on {}", portLabels, nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 394 | (objective, error) -> { |
| 395 | log.warn("addToHash failed to add port/label {} to NextObj {} on {}: {}", |
| 396 | portLabels, nextId, deviceId, error); |
| 397 | srManager.invalidateNextObj(objective.id()); |
| 398 | }); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 399 | NextObjective nextObjective = nextObjBuilder.addToExisting(context); |
| 400 | flowObjectiveService.next(deviceId, nextObjective); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 404 | * Makes a call to the FlowObjective service to remove buckets from |
| 405 | * a hash group. User must ensure that all the ports & labels are meant |
| 406 | * same neighbor (ie. dstMac). |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 407 | * |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 408 | * @param portLabels a collection of port & label combinations to remove |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 409 | * from the hash group identified by the nextId |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 410 | * @param dstMac destination mac address of next-hop |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 411 | * @param nextId id for next-objective from which buckets will be removed |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 412 | */ |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 413 | private void removeFromHashedNextObjective(Collection<PortLabel> portLabels, |
| 414 | MacAddress dstMac, Integer nextId) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 415 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 416 | .builder() |
| 417 | .withType(NextObjective.Type.HASHED) //same as original |
| 418 | .withId(nextId) |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 419 | .fromApp(appId); |
| 420 | // Create the buckets to be removed |
| 421 | portLabels.forEach(pl -> { |
| 422 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 423 | tBuilder.setOutput(pl.port) |
| 424 | .setEthDst(dstMac) |
| 425 | .setEthSrc(nodeMacAddr); |
| 426 | if (pl.edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
| 427 | tBuilder.pushMpls() |
| 428 | .copyTtlOut() |
| 429 | .setMpls(MplsLabel.mplsLabel(pl.edgeLabel)); |
| 430 | } |
| 431 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 432 | }); |
| 433 | log.debug("removeFromHash in device {}: Removing Bucket with port/label" |
| 434 | + " {} from nextId {}", deviceId, portLabels, nextId); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 435 | |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 436 | ObjectiveContext context = new DefaultObjectiveContext( |
| 437 | (objective) -> log.debug("port/label {} removedFrom NextObj" |
| 438 | + " {} on {}", portLabels, nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 439 | (objective, error) -> { |
| 440 | log.warn("port/label {} failed to removeFrom NextObj {} on {}: {}", |
| 441 | portLabels, nextId, deviceId, error); |
| 442 | srManager.invalidateNextObj(objective.id()); |
| 443 | }); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 444 | NextObjective nextObjective = nextObjBuilder.removeFromExisting(context); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 445 | flowObjectiveService.next(deviceId, nextObjective); |
| 446 | } |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 447 | |
| 448 | /** |
| 449 | * Checks all the hash-groups in the target-switch meant for the destination |
| 450 | * switch, and either adds or removes buckets to make the neighbor-set |
| 451 | * match the given next-hops. Typically called by the master instance of the |
| 452 | * destination switch, which may be different from the master instance of the |
| 453 | * target switch where hash-group changes are made. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 454 | * |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 455 | * @param targetSw the switch in which the hash groups will be edited |
| 456 | * @param nextHops the current next hops for the target switch to reach |
| 457 | * the dest sw |
| 458 | * @param destSw the destination switch |
| 459 | * @param revoke true if hash groups need to remove buckets from the |
| 460 | * the groups to match the current next hops |
| 461 | * @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] | 462 | */ |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 463 | public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops, |
| 464 | DeviceId destSw, boolean revoke) { |
| 465 | // temporary storage of keys to be updated |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 466 | Map<DestinationSetNextObjectiveStoreKey, Set<DeviceId>> tempStore = |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 467 | new HashMap<>(); |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 468 | boolean foundNextObjective = false, success = true; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 469 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 470 | // retrieve hash-groups meant for destSw, which have destinationSets |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 471 | // with different neighbors than the given next-hops |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 472 | for (DestinationSetNextObjectiveStoreKey dskey : dsNextObjStore.keySet()) { |
| 473 | if (!dskey.deviceId().equals(targetSw) || |
| 474 | !dskey.destinationSet().getDestinationSwitches().contains(destSw)) { |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 475 | continue; |
| 476 | } |
| 477 | foundNextObjective = true; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 478 | NextNeighbors nhops = dsNextObjStore.get(dskey); |
| 479 | Set<DeviceId> currNeighbors = nhops.nextHops(destSw); |
| 480 | int edgeLabel = dskey.destinationSet().getEdgeLabel(destSw); |
| 481 | Integer nextId = nhops.nextId(); |
Saurav Das | e0d4c87 | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 482 | if (currNeighbors == null || nextHops == null) { |
| 483 | log.warn("fixing hash groups but found currNeighbors:{} or nextHops:{}" |
| 484 | + " in targetSw:{} for dstSw:{}", currNeighbors, |
| 485 | nextHops, targetSw, destSw); |
| 486 | success &= false; |
| 487 | continue; |
| 488 | } |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 489 | |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 490 | // some store elements may not be hashed next-objectives - ignore them |
| 491 | if (isSimpleNextObjective(dskey)) { |
| 492 | log.debug("Ignoring {} of SIMPLE nextObj for targetSw:{}" |
| 493 | + " -> dstSw:{} with current nextHops:{} to new" |
| 494 | + " nextHops: {} in nextId:{}", |
| 495 | (revoke) ? "removal" : "addition", targetSw, destSw, |
| 496 | currNeighbors, nextHops, nextId); |
Saurav Das | e0d4c87 | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 497 | if ((revoke && !nextHops.isEmpty()) |
| 498 | || (!revoke && !nextHops.equals(currNeighbors))) { |
Saurav Das | f1027d4 | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 499 | log.debug("Simple next objective cannot be edited to " |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 500 | + "move from {} to {}", currNeighbors, nextHops); |
| 501 | } |
| 502 | continue; |
| 503 | } |
| 504 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 505 | Set<DeviceId> diff; |
| 506 | if (revoke) { |
| 507 | diff = Sets.difference(currNeighbors, nextHops); |
| 508 | log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next " |
| 509 | + "hops:{} ..removing {}", targetSw, destSw, nextId, |
| 510 | currNeighbors, diff); |
| 511 | } else { |
| 512 | diff = Sets.difference(nextHops, currNeighbors); |
| 513 | log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next " |
| 514 | + "hops:{} ..adding {}", targetSw, destSw, nextId, |
| 515 | currNeighbors, diff); |
| 516 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 517 | boolean suc = updateAllPortsToNextHop(diff, edgeLabel, nextId, |
| 518 | revoke); |
| 519 | if (suc) { |
| 520 | // to update neighbor set with changes made |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 521 | if (revoke) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 522 | tempStore.put(dskey, Sets.difference(currNeighbors, diff)); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 523 | } else { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 524 | tempStore.put(dskey, Sets.union(currNeighbors, diff)); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 525 | } |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 526 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 527 | success &= suc; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 530 | if (!foundNextObjective) { |
| 531 | log.debug("Cannot find any nextObjectives for route targetSw:{} " |
| 532 | + "-> dstSw:{}", targetSw, destSw); |
pier | 1c2ca73 | 2019-04-25 18:51:51 +0200 | [diff] [blame^] | 533 | return false; // nothing to do, return false so re-route will be performed |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 536 | // update the dsNextObjectiveStore with new destinationSet to nextId mappings |
| 537 | for (DestinationSetNextObjectiveStoreKey key : tempStore.keySet()) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 538 | NextNeighbors currentNextHops = dsNextObjStore.get(key); |
| 539 | if (currentNextHops == null) { |
| 540 | log.warn("fixHashGroups could not update global store in " |
| 541 | + "device {} .. missing nextNeighbors for key {}", |
| 542 | deviceId, key); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 543 | continue; |
| 544 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 545 | Set<DeviceId> newNeighbors = new HashSet<>(); |
| 546 | newNeighbors.addAll(tempStore.get(key)); |
| 547 | Map<DeviceId, Set<DeviceId>> oldDstNextHops = |
| 548 | ImmutableMap.copyOf(currentNextHops.dstNextHops()); |
| 549 | currentNextHops.dstNextHops().put(destSw, newNeighbors); //local change |
| 550 | log.debug("Updating nsNextObjStore target:{} -> dst:{} in key:{} nextId:{}", |
| 551 | targetSw, destSw, key, currentNextHops.nextId()); |
| 552 | log.debug("Old dstNextHops: {}", oldDstNextHops); |
| 553 | log.debug("New dstNextHops: {}", currentNextHops.dstNextHops()); |
| 554 | // update global store |
| 555 | dsNextObjStore.put(key, |
| 556 | new NextNeighbors(currentNextHops.dstNextHops(), |
| 557 | currentNextHops.nextId())); |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 558 | } |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 559 | |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 560 | // even if one fails and others succeed, return false so ECMPspg not updated |
| 561 | return success; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 564 | /** |
| 565 | * Updates the DestinationSetNextObjectiveStore with any per-destination nexthops |
| 566 | * that are not already in the store for the given DestinationSet. Note that |
| 567 | * this method does not remove existing next hops for the destinations in the |
| 568 | * DestinationSet. |
| 569 | * |
| 570 | * @param ds the DestinationSet for which the next hops need to be updated |
| 571 | * @param newDstNextHops a map of per-destination next hops to update the |
| 572 | * destinationSet with |
| 573 | * @return true if successful in updating all next hops |
| 574 | */ |
| 575 | private boolean updateNextHops(DestinationSet ds, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 576 | Map<DeviceId, Set<DeviceId>> newDstNextHops) { |
| 577 | DestinationSetNextObjectiveStoreKey key = |
| 578 | new DestinationSetNextObjectiveStoreKey(deviceId, ds); |
| 579 | NextNeighbors currNext = dsNextObjStore.get(key); |
| 580 | Map<DeviceId, Set<DeviceId>> currDstNextHops = currNext.dstNextHops(); |
| 581 | |
| 582 | // add newDstNextHops to currDstNextHops for each dst |
| 583 | boolean success = true; |
| 584 | for (DeviceId dstSw : ds.getDestinationSwitches()) { |
| 585 | Set<DeviceId> currNhops = currDstNextHops.get(dstSw); |
| 586 | Set<DeviceId> newNhops = newDstNextHops.get(dstSw); |
| 587 | currNhops = (currNhops == null) ? Sets.newHashSet() : currNhops; |
| 588 | newNhops = (newNhops == null) ? Sets.newHashSet() : newNhops; |
| 589 | int edgeLabel = ds.getEdgeLabel(dstSw); |
| 590 | int nextId = currNext.nextId(); |
| 591 | |
| 592 | // new next hops should be added |
| 593 | boolean suc = updateAllPortsToNextHop(Sets.difference(newNhops, currNhops), |
| 594 | edgeLabel, nextId, false); |
| 595 | if (suc) { |
| 596 | currNhops.addAll(newNhops); |
| 597 | currDstNextHops.put(dstSw, currNhops); // this is only a local change |
| 598 | } |
| 599 | success &= suc; |
| 600 | } |
| 601 | |
| 602 | if (success) { |
| 603 | // update global store |
| 604 | dsNextObjStore.put(key, new NextNeighbors(currDstNextHops, |
| 605 | currNext.nextId())); |
| 606 | log.debug("Updated device:{} ds:{} new next-hops: {}", deviceId, ds, |
| 607 | dsNextObjStore.get(key)); |
| 608 | } |
| 609 | return success; |
| 610 | } |
| 611 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 612 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 613 | * Adds or removes buckets for all ports to a set of neighbor devices. Caller |
| 614 | * needs to ensure that the given neighbors are all next hops towards the |
| 615 | * same destination (represented by the given edgeLabel). |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 616 | * |
| 617 | * @param neighbors set of neighbor device ids |
| 618 | * @param edgeLabel MPLS label to use in buckets |
| 619 | * @param nextId the nextObjective to change |
| 620 | * @param revoke true if buckets need to be removed, false if they need to |
| 621 | * be added |
| 622 | * @return true if successful in adding or removing buckets for all ports |
| 623 | * to the neighbors |
| 624 | */ |
| 625 | private boolean updateAllPortsToNextHop(Set<DeviceId> neighbors, int edgeLabel, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 626 | int nextId, boolean revoke) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 627 | for (DeviceId neighbor : neighbors) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 628 | MacAddress neighborMac; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 629 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 630 | neighborMac = deviceConfig.getDeviceMac(neighbor); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 631 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 632 | log.warn(e.getMessage() + " Aborting updateAllPortsToNextHop" |
| 633 | + " for nextId:" + nextId); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 634 | return false; |
| 635 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 636 | Collection<PortNumber> portsToNeighbor = devicePortMap.get(neighbor); |
| 637 | if (portsToNeighbor == null || portsToNeighbor.isEmpty()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 638 | log.warn("No ports found in dev:{} for neighbor:{} .. cannot " |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 639 | + "updateAllPortsToNextHop for nextId: {}", |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 640 | deviceId, neighbor, nextId); |
| 641 | return false; |
| 642 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 643 | List<PortLabel> pl = Lists.newArrayList(); |
| 644 | portsToNeighbor.forEach(p -> pl.add(new PortLabel(p, edgeLabel))); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 645 | if (revoke) { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 646 | log.debug("updateAllPortsToNextHops in device {}: Removing Bucket(s) " |
| 647 | + "with Port/Label:{} to next object id {}", |
| 648 | deviceId, pl, nextId); |
| 649 | removeFromHashedNextObjective(pl, neighborMac, nextId); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 650 | } else { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 651 | log.debug("fixHashGroup in device {}: Adding Bucket(s) " |
| 652 | + "with Port/Label: {} to next object id {}", |
| 653 | deviceId, pl, nextId); |
| 654 | addToHashedNextObjective(pl, neighborMac, nextId); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | return true; |
| 658 | } |
| 659 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 660 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 661 | * Returns true if the destination set is meant for swap or multi-labeled |
| 662 | * packet transport, and MPLS ECMP is not supported. |
| 663 | * |
| 664 | * @param dskey the key representing the destination set |
| 665 | * @return true if destination set is meant for simple next objectives |
| 666 | */ |
| 667 | boolean isSimpleNextObjective(DestinationSetNextObjectiveStoreKey dskey) { |
| 668 | return (dskey.destinationSet().notBos() || dskey.destinationSet().swap()) |
| 669 | && !srManager.getMplsEcmp(); |
| 670 | } |
| 671 | |
| 672 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 673 | * Adds or removes a port that has been configured with a vlan to a broadcast group |
| 674 | * 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] | 675 | * |
| 676 | * @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] | 677 | * @param vlanId the vlan id corresponding to the broadcast domain/group |
| 678 | * @param popVlan indicates if packets should be sent out untagged or not out |
| 679 | * of the port. If true, indicates an access (untagged) or native vlan |
| 680 | * configuration. If false, indicates a trunk (tagged) vlan config. |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 681 | * @param portUp true if port is enabled, false if disabled |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 682 | */ |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 683 | public void processEdgePort(PortNumber port, VlanId vlanId, |
| 684 | boolean popVlan, boolean portUp) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 685 | //get the next id for the subnet and edit it. |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 686 | Integer nextId = getVlanNextObjectiveId(vlanId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 687 | if (nextId == -1) { |
| 688 | if (portUp) { |
| 689 | log.debug("**Creating flooding group for first port enabled in" |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 690 | + " vlan {} on dev {} port {}", vlanId, deviceId, port); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 691 | createBcastGroupFromVlan(vlanId, Collections.singleton(port)); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 692 | } else { |
| 693 | log.warn("Could not find flooding group for subnet {} on dev:{} when" |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 694 | + " removing port:{}", vlanId, deviceId, port); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 695 | } |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | log.info("**port{} in device {}: {} Bucket with Port {} to" |
| 700 | + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId, |
| 701 | (portUp) ? "Adding" : "Removing", |
| 702 | port, nextId); |
| 703 | // Create the bucket to be added or removed |
| 704 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 705 | if (popVlan) { |
| 706 | tBuilder.popVlan(); |
| 707 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 708 | tBuilder.setOutput(port); |
| 709 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 710 | TrafficSelector metadata = |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 711 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 712 | |
| 713 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 714 | .builder().withId(nextId) |
| 715 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 716 | .addTreatment(tBuilder.build()) |
| 717 | .withMeta(metadata); |
| 718 | |
| 719 | ObjectiveContext context = new DefaultObjectiveContext( |
| 720 | (objective) -> log.debug("port {} successfully {} NextObj {} on {}", |
| 721 | port, (portUp) ? "addedTo" : "removedFrom", |
| 722 | nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 723 | (objective, error) -> { |
| 724 | log.warn("port {} failed to {} NextObj {} on {}: {}", |
| 725 | port, (portUp) ? "addTo" : "removeFrom", nextId, deviceId, error); |
| 726 | srManager.invalidateNextObj(objective.id()); |
| 727 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 728 | |
| 729 | NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context) |
| 730 | : nextObjBuilder.removeFromExisting(context); |
| 731 | log.debug("edgePort processed: Submited next objective {} in device {}", |
| 732 | nextId, deviceId); |
| 733 | flowObjectiveService.next(deviceId, nextObj); |
| 734 | } |
| 735 | |
| 736 | /** |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 737 | * Returns the next objective of type hashed (or simple) associated with the |
| 738 | * destination set. In addition, updates the existing next-objective if new |
| 739 | * route-paths found have resulted in the addition of new next-hops to a |
| 740 | * particular destination. If there is no existing next objective for this |
| 741 | * destination set, this method would create a next objective and return the |
| 742 | * nextId. Optionally metadata can be passed in for the creation of the next |
| 743 | * objective. If the parameter simple is true then a simple next objective |
| 744 | * is created instead of a hashed one. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 745 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 746 | * @param ds destination set |
| 747 | * @param nextHops a map of per destination next hops |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 748 | * @param meta metadata passed into the creation of a Next Objective |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 749 | * @param simple if true, a simple next objective will be created instead of |
| 750 | * a hashed next objective |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 751 | * @return int if found or -1 if there are errors in the creation of the |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 752 | * neighbor set. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 753 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 754 | public int getNextObjectiveId(DestinationSet ds, |
| 755 | Map<DeviceId, Set<DeviceId>> nextHops, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 756 | TrafficSelector meta, boolean simple) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 757 | NextNeighbors next = dsNextObjStore. |
| 758 | get(new DestinationSetNextObjectiveStoreKey(deviceId, ds)); |
| 759 | if (next == null) { |
| 760 | log.debug("getNextObjectiveId in device{}: Next objective id " |
| 761 | + "not found for {} ... creating", deviceId, ds); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 762 | log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}", |
| 763 | deviceId, |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 764 | dsNextObjStore.entrySet() |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 765 | .stream() |
| 766 | .filter((nsStoreEntry) -> |
| 767 | (nsStoreEntry.getKey().deviceId().equals(deviceId))) |
| 768 | .collect(Collectors.toList())); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 769 | |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 770 | createGroupFromDestinationSet(ds, nextHops, meta, simple); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 771 | next = dsNextObjStore. |
| 772 | get(new DestinationSetNextObjectiveStoreKey(deviceId, ds)); |
| 773 | if (next == null) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 774 | log.warn("getNextObjectiveId: unable to create next objective"); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 775 | // failure in creating group |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 776 | return -1; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 777 | } else { |
| 778 | log.debug("getNextObjectiveId in device{}: Next objective id {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 779 | + "created for {}", deviceId, next.nextId(), ds); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 780 | } |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 781 | } else { |
| 782 | log.trace("getNextObjectiveId in device{}: Next objective id {} " |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 783 | + "found for {}", deviceId, next.nextId(), ds); |
| 784 | // should fix hash groups too if next-hops have changed |
| 785 | if (!next.dstNextHops().equals(nextHops)) { |
| 786 | log.debug("Nexthops have changed for dev:{} nextId:{} ..updating", |
| 787 | deviceId, next.nextId()); |
| 788 | if (!updateNextHops(ds, nextHops)) { |
| 789 | // failure in updating group |
| 790 | return -1; |
| 791 | } |
| 792 | } |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 793 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 794 | return next.nextId(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 795 | } |
| 796 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 797 | /** |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 798 | * Returns the next objective of type broadcast associated with the vlan, |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 799 | * or -1 if no such objective exists. Note that this method does NOT create |
| 800 | * 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] | 801 | * created at startup from network configuration. Typically this is used |
| 802 | * for L2 flooding within the subnet configured on the switch. |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 803 | * |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 804 | * @param vlanId vlan id |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 805 | * @return int if found or -1 |
| 806 | */ |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 807 | public int getVlanNextObjectiveId(VlanId vlanId) { |
| 808 | Integer nextId = vlanNextObjStore. |
| 809 | get(new VlanNextObjectiveStoreKey(deviceId, vlanId)); |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 810 | |
| 811 | return (nextId != null) ? nextId : -1; |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 815 | * Returns the next objective of type simple associated with the mac/vlan on the |
| 816 | * device, given the treatment. Different treatments to the same mac/vlan result |
| 817 | * in different next objectives. If no such objective exists, this method |
| 818 | * creates one (if requested) and returns the id. Optionally metadata can be passed in for |
| 819 | * the creation of the objective. Typically this is used for L2 and L3 forwarding |
| 820 | * to compute nodes and containers/VMs on the compute nodes directly attached |
| 821 | * to the switch. |
| 822 | * |
| 823 | * @param macAddr the mac addr for the simple next objective |
| 824 | * @param vlanId the vlan for the simple next objective |
Ruchi Sahota | b555e59 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 825 | * @param port port with which to create the Next Obj. |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 826 | * @param createIfMissing true if a next object should be created if not found |
| 827 | * @return int if found or created, -1 if there are errors during the |
| 828 | * creation of the next objective. |
| 829 | */ |
Ruchi Sahota | b555e59 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 830 | public int getMacVlanNextObjectiveId(MacAddress macAddr, VlanId vlanId, PortNumber port, |
| 831 | boolean createIfMissing) { |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 832 | |
| 833 | Integer nextId = macVlanNextObjStore |
| 834 | .get(new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId)); |
| 835 | |
| 836 | if (nextId != null) { |
| 837 | return nextId; |
| 838 | } |
| 839 | |
| 840 | log.debug("getMacVlanNextObjectiveId in device {}: Next objective id " |
| 841 | + "not found for host : {}/{} .. {}", deviceId, macAddr, vlanId, |
| 842 | (createIfMissing) ? "creating" : "aborting"); |
| 843 | |
| 844 | if (!createIfMissing) { |
| 845 | return -1; |
| 846 | } |
| 847 | |
Ruchi Sahota | b555e59 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 848 | MacAddress deviceMac; |
| 849 | try { |
| 850 | deviceMac = deviceConfig.getDeviceMac(deviceId); |
| 851 | } catch (DeviceConfigNotFoundException e) { |
| 852 | log.warn(e.getMessage() + " in getMacVlanNextObjectiveId"); |
| 853 | return -1; |
| 854 | } |
| 855 | |
| 856 | // since we are creating now, port cannot be null |
| 857 | if (port == null) { |
| 858 | log.debug("getMacVlanNextObjectiveId : port information cannot be null " |
| 859 | + "for device {}, host {}/{}", deviceId, macAddr, vlanId); |
| 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | TrafficSelector.Builder meta = DefaultTrafficSelector.builder(); |
| 864 | |
| 865 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 866 | treatment.deferred() |
| 867 | .setEthDst(macAddr) |
| 868 | .setEthSrc(deviceMac) |
| 869 | .setOutput(port); |
| 870 | |
| 871 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 872 | VlanId untaggedVlan = srManager.interfaceService.getUntaggedVlanId(connectPoint); |
| 873 | Set<VlanId> taggedVlans = srManager.interfaceService.getTaggedVlanId(connectPoint); |
| 874 | VlanId nativeVlan = srManager.interfaceService.getNativeVlanId(connectPoint); |
| 875 | |
| 876 | // Adjust the meta according to VLAN configuration |
| 877 | if (taggedVlans.contains(vlanId)) { |
| 878 | treatment.setVlanId(vlanId); |
| 879 | } else if (vlanId.equals(VlanId.NONE)) { |
| 880 | if (untaggedVlan != null) { |
| 881 | meta.matchVlanId(untaggedVlan); |
| 882 | } else if (nativeVlan != null) { |
| 883 | meta.matchVlanId(nativeVlan); |
| 884 | } else { |
| 885 | log.warn("Untagged nexthop {}/{} is not allowed on {} without untagged or native vlan", |
| 886 | macAddr, vlanId, connectPoint); |
| 887 | return -1; |
| 888 | } |
| 889 | } else { |
| 890 | log.warn("Tagged nexthop {}/{} is not allowed on {} without VLAN listed" |
| 891 | + " in tagged vlan", macAddr, vlanId, connectPoint); |
| 892 | return -1; |
| 893 | } |
| 894 | |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 895 | /* create missing next objective */ |
Ruchi Sahota | b555e59 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 896 | nextId = createGroupFromMacVlan(macAddr, vlanId, treatment.build(), meta.build()); |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 897 | if (nextId == null) { |
| 898 | log.warn("getMacVlanNextObjectiveId: unable to create next obj" |
| 899 | + "for dev:{} host:{}/{}", deviceId, macAddr, vlanId); |
| 900 | return -1; |
| 901 | } |
| 902 | return nextId; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | /** |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 907 | * Returns the next objective of type simple associated with the port on the |
| 908 | * device, given the treatment. Different treatments to the same port result |
| 909 | * in different next objectives. If no such objective exists, this method |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 910 | * 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] | 911 | * the creation of the objective. Typically this is used for L2 and L3 forwarding |
| 912 | * to compute nodes and containers/VMs on the compute nodes directly attached |
| 913 | * to the switch. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 914 | * |
| 915 | * @param portNum the port number for the simple next objective |
| 916 | * @param treatment the actions to apply on the packets (should include outport) |
| 917 | * @param meta optional metadata passed into the creation of the next objective |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 918 | * @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] | 919 | * @return int if found or created, -1 if there are errors during the |
| 920 | * creation of the next objective. |
| 921 | */ |
| 922 | public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment, |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 923 | TrafficSelector meta, boolean createIfMissing) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 924 | Integer nextId = portNextObjStore |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 925 | .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta)); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 926 | if (nextId != null) { |
| 927 | return nextId; |
| 928 | } |
| 929 | log.debug("getPortNextObjectiveId in device {}: Next objective id " |
| 930 | + "not found for port: {} .. {}", deviceId, portNum, |
| 931 | (createIfMissing) ? "creating" : "aborting"); |
| 932 | if (!createIfMissing) { |
| 933 | return -1; |
| 934 | } |
| 935 | // create missing next objective |
| 936 | createGroupFromPort(portNum, treatment, meta); |
| 937 | nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum, |
| 938 | treatment, meta)); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 939 | if (nextId == null) { |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 940 | log.warn("getPortNextObjectiveId: unable to create next obj" |
| 941 | + "for dev:{} port:{}", deviceId, portNum); |
| 942 | return -1; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 943 | } |
| 944 | return nextId; |
| 945 | } |
| 946 | |
| 947 | /** |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 948 | * Checks if the next objective ID (group) for the neighbor set exists or not. |
| 949 | * |
| 950 | * @param ns neighbor set to check |
| 951 | * @return true if it exists, false otherwise |
| 952 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 953 | public boolean hasNextObjectiveId(DestinationSet ns) { |
| 954 | NextNeighbors nextHops = dsNextObjStore. |
| 955 | get(new DestinationSetNextObjectiveStoreKey(deviceId, ns)); |
| 956 | if (nextHops == null) { |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 957 | return false; |
| 958 | } |
| 959 | |
| 960 | return true; |
| 961 | } |
| 962 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 963 | private void populateNeighborMaps() { |
| 964 | Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 965 | for (Link link : outgoingLinks) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 966 | if (link.type() != Link.Type.DIRECT) { |
| 967 | continue; |
| 968 | } |
| 969 | addNeighborAtPort(link.dst().deviceId(), link.src().port()); |
| 970 | } |
| 971 | } |
| 972 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 973 | protected void addNeighborAtPort(DeviceId neighborId, |
| 974 | PortNumber portToNeighbor) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 975 | // Update DeviceToPort database |
| 976 | log.debug("Device {} addNeighborAtPort: neighbor {} at port {}", |
| 977 | deviceId, neighborId, portToNeighbor); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 978 | Set<PortNumber> ports = Collections |
| 979 | .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>()); |
| 980 | ports.add(portToNeighbor); |
| 981 | Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports); |
| 982 | if (portnums != null) { |
| 983 | portnums.add(portToNeighbor); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | // Update portToDevice database |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 987 | // should always update as neighbor could have changed on this port |
| 988 | DeviceId prev = portDeviceMap.put(portToNeighbor, neighborId); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 989 | if (prev != null) { |
Saurav Das | 5a35604 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 990 | log.warn("Device/port: {}/{} previous neighbor: {}, current neighbor: {} ", |
Saurav Das | c88d466 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 991 | deviceId, portToNeighbor, prev, neighborId); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 995 | /** |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 996 | * Creates a NextObjective for a hash group in this device from a given |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 997 | * DestinationSet. If the parameter simple is true, a simple next objective |
| 998 | * is created instead. |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 999 | * |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1000 | * @param ds the DestinationSet |
| 1001 | * @param neighbors a map for each destination and its next-hops |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1002 | * @param meta metadata passed into the creation of a Next Objective |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1003 | * @param simple if true, a simple next objective will be created instead of |
| 1004 | * a hashed next objective |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1005 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1006 | public void createGroupFromDestinationSet(DestinationSet ds, |
| 1007 | Map<DeviceId, Set<DeviceId>> neighbors, |
| 1008 | TrafficSelector meta, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1009 | boolean simple) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1010 | int nextId = flowObjectiveService.allocateNextId(); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1011 | NextObjective.Type type = (simple) ? NextObjective.Type.SIMPLE |
| 1012 | : NextObjective.Type.HASHED; |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1013 | if (neighbors == null || neighbors.isEmpty()) { |
| 1014 | log.warn("createGroupsFromDestinationSet: needs at least one neighbor" |
| 1015 | + "to create group in dev:{} for ds: {} with next-hops {}", |
| 1016 | deviceId, ds, neighbors); |
| 1017 | return; |
| 1018 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1019 | |
| 1020 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1021 | .builder() |
| 1022 | .withId(nextId) |
| 1023 | .withType(type) |
| 1024 | .fromApp(appId); |
| 1025 | if (meta != null) { |
| 1026 | nextObjBuilder.withMeta(meta); |
| 1027 | } |
| 1028 | |
| 1029 | // create treatment buckets for each neighbor for each dst Device |
| 1030 | // except in the special case where we only want to pick a single |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1031 | // neighbor/port for a simple nextObj |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1032 | boolean foundSingleNeighbor = false; |
| 1033 | boolean treatmentAdded = false; |
| 1034 | Map<DeviceId, Set<DeviceId>> dstNextHops = new ConcurrentHashMap<>(); |
| 1035 | for (DeviceId dst : ds.getDestinationSwitches()) { |
| 1036 | Set<DeviceId> nextHops = neighbors.get(dst); |
| 1037 | if (nextHops == null || nextHops.isEmpty()) { |
| 1038 | continue; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1039 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1040 | |
| 1041 | if (foundSingleNeighbor) { |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | for (DeviceId neighborId : nextHops) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1046 | if (devicePortMap.get(neighborId) == null) { |
| 1047 | log.warn("Neighbor {} is not in the port map yet for dev:{}", |
| 1048 | neighborId, deviceId); |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 1049 | return; |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 1050 | } else if (devicePortMap.get(neighborId).isEmpty()) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1051 | log.warn("There are no ports for " |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1052 | + "the Device {} in the port map yet", neighborId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 1053 | return; |
sangho | 834e4b0 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1056 | MacAddress neighborMac; |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1057 | try { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1058 | neighborMac = deviceConfig.getDeviceMac(neighborId); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1059 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1060 | log.warn(e.getMessage() + " Aborting createGroupsFromDestinationset."); |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1061 | return; |
| 1062 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1063 | // For each port to the neighbor, we create a new treatment |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1064 | Set<PortNumber> neighborPorts = devicePortMap.get(neighborId); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1065 | // In this case we need a SIMPLE nextObj. We randomly pick a port |
| 1066 | if (simple) { |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1067 | int size = devicePortMap.get(neighborId).size(); |
| 1068 | int index = RandomUtils.nextInt(0, size); |
| 1069 | neighborPorts = Collections.singleton( |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1070 | Iterables.get(devicePortMap.get(neighborId), |
| 1071 | index)); |
| 1072 | foundSingleNeighbor = true; |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1073 | } |
Andreas Pantelopoulos | 27532cd | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1074 | |
Pier Ventre | 917127a | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 1075 | for (PortNumber sp : neighborPorts) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1076 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 1077 | .builder(); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1078 | tBuilder.setEthDst(neighborMac).setEthSrc(nodeMacAddr); |
| 1079 | int edgeLabel = ds.getEdgeLabel(dst); |
| 1080 | if (edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1081 | if (simple) { |
| 1082 | // swap label case |
| 1083 | tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel)); |
| 1084 | } else { |
| 1085 | // ecmp with label push case |
| 1086 | tBuilder.pushMpls().copyTtlOut() |
| 1087 | .setMpls(MplsLabel.mplsLabel(edgeLabel)); |
| 1088 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1089 | } |
Andreas Pantelopoulos | e8d5f41 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 1090 | if ((ds.getTypeOfDstSet() == DestinationSet.DestinationSetType.SWAP_NOT_BOS) || |
| 1091 | (ds.getTypeOfDstSet() == DestinationSet.DestinationSetType.POP_NOT_BOS)) { |
Saurav Das | 3c82f19 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1092 | tBuilder.setVlanId(srManager.getPwTransportVlan()); |
Andreas Pantelopoulos | e8d5f41 | 2018-05-01 14:56:05 -0700 | [diff] [blame] | 1093 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1094 | tBuilder.setOutput(sp); |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 1095 | nextObjBuilder.addTreatment(tBuilder.build()); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1096 | treatmentAdded = true; |
| 1097 | //update store |
| 1098 | Set<DeviceId> existingNeighbors = dstNextHops.get(dst); |
| 1099 | if (existingNeighbors == null) { |
| 1100 | existingNeighbors = new HashSet<>(); |
| 1101 | } |
| 1102 | existingNeighbors.add(neighborId); |
| 1103 | dstNextHops.put(dst, existingNeighbors); |
| 1104 | log.debug("creating treatment for port/label {}/{} in next:{}", |
| 1105 | sp, edgeLabel, nextId); |
| 1106 | } |
| 1107 | |
| 1108 | if (foundSingleNeighbor) { |
| 1109 | break; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1112 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1113 | |
| 1114 | if (!treatmentAdded) { |
| 1115 | log.warn("Could not createGroup from DestinationSet {} without any" |
| 1116 | + "next hops {}", ds, neighbors); |
| 1117 | return; |
| 1118 | } |
| 1119 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1120 | (objective) -> |
| 1121 | log.debug("createGroupsFromDestinationSet installed " |
| 1122 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1123 | (objective, error) -> { |
| 1124 | log.warn("createGroupsFromDestinationSet failed to install NextObj {} on {}: {}", |
| 1125 | nextId, deviceId, error); |
| 1126 | srManager.invalidateNextObj(objective.id()); |
| 1127 | }); |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1128 | NextObjective nextObj = nextObjBuilder.add(context); |
| 1129 | log.debug(".. createGroupsFromDestinationSet: Submitted " |
| 1130 | + "next objective {} in device {}", nextId, deviceId); |
| 1131 | flowObjectiveService.next(deviceId, nextObj); |
| 1132 | //update store |
| 1133 | dsNextObjStore.put(new DestinationSetNextObjectiveStoreKey(deviceId, ds), |
| 1134 | new NextNeighbors(dstNextHops, nextId)); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1135 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 1136 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1137 | /** |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1138 | * Creates broadcast groups for all ports in the same subnet for |
| 1139 | * all configured subnets. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1140 | */ |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1141 | public void createGroupsFromVlanConfig() { |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1142 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1143 | createBcastGroupFromVlan(vlanId, ports); |
Pier Ventre | 10bd8d1 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 1144 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1145 | } |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 1146 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1147 | /** |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1148 | * 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] | 1149 | * |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1150 | * @param vlanId vlan id |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1151 | * @param ports list of ports in the subnet |
| 1152 | */ |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1153 | public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) { |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1154 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 1155 | |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1156 | if (vlanNextObjStore.containsKey(key)) { |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1157 | log.debug("Broadcast group for device {} and subnet {} exists", |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1158 | deviceId, vlanId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1159 | return; |
| 1160 | } |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1161 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1162 | TrafficSelector metadata = |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1163 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1164 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1165 | int nextId = flowObjectiveService.allocateNextId(); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1166 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1167 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1168 | .builder().withId(nextId) |
| 1169 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 1170 | .withMeta(metadata); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1171 | |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1172 | ports.forEach(port -> { |
| 1173 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1174 | if (toPopVlan(port, vlanId)) { |
| 1175 | tBuilder.popVlan(); |
| 1176 | } |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1177 | tBuilder.setOutput(port); |
| 1178 | nextObjBuilder.addTreatment(tBuilder.build()); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1179 | }); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1180 | |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1181 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1182 | (objective) -> |
| 1183 | log.debug("createBroadcastGroupFromVlan installed " |
| 1184 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1185 | (objective, error) -> { |
| 1186 | log.warn("createBroadcastGroupFromVlan failed to install NextObj {} on {}: {}", |
| 1187 | nextId, deviceId, error); |
| 1188 | srManager.invalidateNextObj(objective.id()); |
| 1189 | }); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1190 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1191 | flowObjectiveService.next(deviceId, nextObj); |
Saurav Das | f14d9ef | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 1192 | log.debug("createBcastGroupFromVlan: Submitted next objective {} " |
| 1193 | + "for vlan: {} in device {}", nextId, vlanId, deviceId); |
Saurav Das | 1a129a0 | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 1194 | |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1195 | vlanNextObjStore.put(key, nextId); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1198 | /** |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1199 | * Removes a single broadcast group from a given vlan id. |
| 1200 | * The group should be empty. |
| 1201 | * @param deviceId device Id to remove the group |
| 1202 | * @param portNum port number related to the group |
| 1203 | * @param vlanId vlan id of the broadcast group to remove |
| 1204 | * @param popVlan true if the TrafficTreatment involves pop vlan tag action |
| 1205 | */ |
| 1206 | public void removeBcastGroupFromVlan(DeviceId deviceId, PortNumber portNum, |
| 1207 | VlanId vlanId, boolean popVlan) { |
| 1208 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 1209 | |
| 1210 | if (!vlanNextObjStore.containsKey(key)) { |
| 1211 | log.debug("Broadcast group for device {} and subnet {} does not exist", |
| 1212 | deviceId, vlanId); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | TrafficSelector metadata = |
| 1217 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
| 1218 | |
| 1219 | int nextId = vlanNextObjStore.get(key); |
| 1220 | |
| 1221 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1222 | .builder().withId(nextId) |
| 1223 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 1224 | .withMeta(metadata); |
| 1225 | |
| 1226 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1227 | if (popVlan) { |
| 1228 | tBuilder.popVlan(); |
| 1229 | } |
| 1230 | tBuilder.setOutput(portNum); |
| 1231 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 1232 | |
| 1233 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1234 | (objective) -> |
| 1235 | log.debug("removeBroadcastGroupFromVlan removed " |
| 1236 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1237 | (objective, error) -> { |
| 1238 | log.warn("removeBroadcastGroupFromVlan failed to remove NextObj {} on {}: {}", |
| 1239 | nextId, deviceId, error); |
| 1240 | srManager.invalidateNextObj(objective.id()); |
| 1241 | }); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1242 | NextObjective nextObj = nextObjBuilder.remove(context); |
| 1243 | flowObjectiveService.next(deviceId, nextObj); |
| 1244 | log.debug("removeBcastGroupFromVlan: Submited next objective {} in device {}", |
| 1245 | nextId, deviceId); |
| 1246 | |
| 1247 | vlanNextObjStore.remove(key, nextId); |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1251 | * Determine if we should pop given vlan before sending packets to the given port. |
| 1252 | * |
| 1253 | * @param portNumber port number |
| 1254 | * @param vlanId vlan id |
| 1255 | * @return true if the vlan id is not contained in any vlanTagged config |
| 1256 | */ |
| 1257 | private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1258 | return srManager.interfaceService |
| 1259 | .getInterfacesByPort(new ConnectPoint(deviceId, portNumber)) |
Charles Chan | 7ffd81f | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 1260 | .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId)); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1264 | * Create simple next objective for an indirect host mac/vlan. The treatments can include |
| 1265 | * all outgoing actions that need to happen on the packet. |
| 1266 | * |
| 1267 | * @param macAddr the mac address of the host |
| 1268 | * @param vlanId the vlan of the host |
| 1269 | * @param treatment the actions to apply on the packets (should include outport) |
| 1270 | * @param meta optional data to pass to the driver |
| 1271 | * @return next objective ID |
| 1272 | */ |
| 1273 | public int createGroupFromMacVlan(MacAddress macAddr, VlanId vlanId, TrafficTreatment treatment, |
| 1274 | TrafficSelector meta) { |
| 1275 | int nextId = flowObjectiveService.allocateNextId(); |
| 1276 | MacVlanNextObjectiveStoreKey key = new MacVlanNextObjectiveStoreKey(deviceId, macAddr, vlanId); |
| 1277 | |
| 1278 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1279 | .builder().withId(nextId) |
| 1280 | .withType(NextObjective.Type.SIMPLE) |
| 1281 | .addTreatment(treatment) |
| 1282 | .fromApp(appId) |
| 1283 | .withMeta(meta); |
| 1284 | |
| 1285 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1286 | (objective) -> |
| 1287 | log.debug("createGroupFromMacVlan installed " |
| 1288 | + "NextObj {} on {}", nextId, deviceId), |
| 1289 | (objective, error) -> { |
| 1290 | log.warn("createGroupFromMacVlan failed to install NextObj {} on {}: {}", nextId, deviceId, error); |
| 1291 | srManager.invalidateNextObj(objective.id()); |
| 1292 | }); |
| 1293 | NextObjective nextObj = nextObjBuilder.add(context); |
| 1294 | flowObjectiveService.next(deviceId, nextObj); |
| 1295 | log.debug("createGroupFromMacVlan: Submited next objective {} in device {} " |
| 1296 | + "for host {}/{}", nextId, deviceId, macAddr, vlanId); |
| 1297 | |
| 1298 | macVlanNextObjStore.put(key, nextId); |
| 1299 | return nextId; |
| 1300 | } |
| 1301 | |
| 1302 | /** |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1303 | * Create simple next objective for a single port. The treatments can include |
| 1304 | * all outgoing actions that need to happen on the packet. |
| 1305 | * |
| 1306 | * @param portNum the outgoing port on the device |
| 1307 | * @param treatment the actions to apply on the packets (should include outport) |
| 1308 | * @param meta optional data to pass to the driver |
| 1309 | */ |
| 1310 | public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment, |
| 1311 | TrafficSelector meta) { |
| 1312 | int nextId = flowObjectiveService.allocateNextId(); |
| 1313 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
Saurav Das | 76ae681 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 1314 | deviceId, portNum, treatment, meta); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1315 | |
| 1316 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1317 | .builder().withId(nextId) |
| 1318 | .withType(NextObjective.Type.SIMPLE) |
| 1319 | .addTreatment(treatment) |
| 1320 | .fromApp(appId) |
| 1321 | .withMeta(meta); |
| 1322 | |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1323 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1324 | (objective) -> |
| 1325 | log.debug("createGroupFromPort installed " |
| 1326 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1327 | (objective, error) -> { |
| 1328 | log.warn("createGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error); |
| 1329 | srManager.invalidateNextObj(objective.id()); |
| 1330 | }); |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 1331 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1332 | flowObjectiveService.next(deviceId, nextObj); |
| 1333 | log.debug("createGroupFromPort: Submited next objective {} in device {} " |
| 1334 | + "for port {}", nextId, deviceId, portNum); |
| 1335 | |
| 1336 | portNextObjStore.put(key, nextId); |
| 1337 | } |
| 1338 | |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1339 | /** |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1340 | * Removes simple next objective for a single port. |
| 1341 | * |
| 1342 | * @param deviceId device id that has the port to deal with |
| 1343 | * @param portNum the outgoing port on the device |
| 1344 | * @param vlanId vlan id associated with the port |
| 1345 | * @param popVlan true if POP_VLAN action is applied on the packets, false otherwise |
| 1346 | */ |
| 1347 | public void removePortNextObjective(DeviceId deviceId, PortNumber portNum, VlanId vlanId, boolean popVlan) { |
| 1348 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 1349 | mbuilder.matchVlanId(vlanId); |
| 1350 | |
| 1351 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1352 | tbuilder.immediate().setOutput(portNum); |
| 1353 | if (popVlan) { |
| 1354 | tbuilder.immediate().popVlan(); |
| 1355 | } |
| 1356 | |
| 1357 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum, |
| 1358 | tbuilder.build(), mbuilder.build(), false); |
| 1359 | |
| 1360 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
| 1361 | deviceId, portNum, tbuilder.build(), mbuilder.build()); |
| 1362 | if (portNextObjId != -1 && portNextObjStore.containsKey(key)) { |
| 1363 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1364 | .builder().withId(portNextObjId) |
| 1365 | .withType(NextObjective.Type.SIMPLE).fromApp(appId); |
| 1366 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1367 | (objective) -> log.debug("removePortNextObjective removes NextObj {} on {}", |
| 1368 | portNextObjId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1369 | (objective, error) -> { |
| 1370 | log.warn("removePortNextObjective failed to remove NextObj {} on {}: {}", |
| 1371 | portNextObjId, deviceId, error); |
| 1372 | srManager.invalidateNextObj(objective.id()); |
| 1373 | }); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1374 | NextObjective nextObjective = nextObjBuilder.remove(context); |
| 1375 | log.info("**removePortNextObjective: Submitted " |
| 1376 | + "next objective {} in device {}", |
| 1377 | portNextObjId, deviceId); |
| 1378 | flowObjectiveService.next(deviceId, nextObjective); |
| 1379 | |
| 1380 | portNextObjStore.remove(key); |
| 1381 | } |
| 1382 | } |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1383 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1384 | /** |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1385 | * Removes groups for the next objective ID given. |
| 1386 | * |
| 1387 | * @param objectiveId next objective ID to remove |
| 1388 | * @return true if succeeds, false otherwise |
| 1389 | */ |
| 1390 | public boolean removeGroup(int objectiveId) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1391 | for (Map.Entry<DestinationSetNextObjectiveStoreKey, NextNeighbors> e : |
| 1392 | dsNextObjStore.entrySet()) { |
| 1393 | if (e.getValue().nextId() != objectiveId) { |
| 1394 | continue; |
| 1395 | } |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 1396 | // Right now it is just used in TunnelHandler |
| 1397 | // remember in future that PW transit groups could |
| 1398 | // be Indirect groups |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1399 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1400 | .builder().withId(objectiveId) |
| 1401 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 1402 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1403 | (objective) -> log.debug("RemoveGroup removes NextObj {} on {}", |
| 1404 | objectiveId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1405 | (objective, error) -> { |
| 1406 | log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", objectiveId, deviceId, error); |
| 1407 | srManager.invalidateNextObj(objective.id()); |
| 1408 | }); |
Charles Chan | 216e3c8 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 1409 | NextObjective nextObjective = nextObjBuilder.remove(context); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1410 | log.info("**removeGroup: Submited " |
| 1411 | + "next objective {} in device {}", |
| 1412 | objectiveId, deviceId); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1413 | flowObjectiveService.next(deviceId, nextObjective); |
| 1414 | |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1415 | dsNextObjStore.remove(e.getKey()); |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 1416 | return true; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | return false; |
| 1420 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1421 | /** |
| 1422 | * Remove simple next objective for a single port. The treatments can include |
| 1423 | * all outgoing actions that need to happen on the packet. |
| 1424 | * |
| 1425 | * @param portNum the outgoing port on the device |
| 1426 | * @param treatment the actions applied on the packets (should include outport) |
| 1427 | * @param meta optional data to pass to the driver |
| 1428 | */ |
| 1429 | public void removeGroupFromPort(PortNumber portNum, TrafficTreatment treatment, |
| 1430 | TrafficSelector meta) { |
| 1431 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
| 1432 | deviceId, portNum, treatment, meta); |
| 1433 | Integer nextId = portNextObjStore.get(key); |
| 1434 | |
| 1435 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1436 | .builder().withId(nextId) |
| 1437 | .withType(NextObjective.Type.SIMPLE) |
| 1438 | .addTreatment(treatment) |
| 1439 | .fromApp(appId) |
| 1440 | .withMeta(meta); |
| 1441 | |
| 1442 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1443 | (objective) -> |
| 1444 | log.info("removeGroupFromPort installed " |
| 1445 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1446 | (objective, error) -> { |
| 1447 | log.warn("removeGroupFromPort failed to install NextObj {} on {}: {}", nextId, deviceId, error); |
| 1448 | srManager.invalidateNextObj(objective.id()); |
| 1449 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1450 | ); |
| 1451 | NextObjective nextObj = nextObjBuilder.remove(context); |
| 1452 | flowObjectiveService.next(deviceId, nextObj); |
| 1453 | log.info("removeGroupFromPort: Submitted next objective {} in device {} " |
| 1454 | + "for port {}", nextId, deviceId, portNum); |
| 1455 | |
| 1456 | portNextObjStore.remove(key); |
| 1457 | } |
Srikanth Vavilapalli | f3a8bc0 | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1458 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1459 | /** |
| 1460 | * Removes all groups from all next objective stores. |
| 1461 | */ |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1462 | /*public void removeAllGroups() { |
| 1463 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, NextNeighbors> entry: |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1464 | nsNextObjStore.entrySet()) { |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1465 | removeGroup(entry.getValue().nextId()); |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1466 | } |
| 1467 | for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry: |
| 1468 | portNextObjStore.entrySet()) { |
| 1469 | removeGroup(entry.getValue()); |
| 1470 | } |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1471 | for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry: |
| 1472 | vlanNextObjStore.entrySet()) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1473 | removeGroup(entry.getValue()); |
| 1474 | } |
Saurav Das | 7bcbe70 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1475 | }*/ //XXX revisit |
| 1476 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1477 | /** |
| 1478 | * Triggers a one time bucket verification operation on all hash groups |
| 1479 | * on this device. |
| 1480 | */ |
| 1481 | public void triggerBucketCorrector() { |
| 1482 | BucketCorrector bc = new BucketCorrector(); |
| 1483 | bc.run(); |
| 1484 | } |
| 1485 | |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1486 | /** |
| 1487 | * Modifies L2IG bucket when the interface configuration is updated, especially |
| 1488 | * when the interface has same VLAN ID but the VLAN type is changed (e.g., from |
| 1489 | * vlan-tagged [10] to vlan-untagged 10), which requires changes on |
| 1490 | * TrafficTreatment in turn. |
| 1491 | * |
| 1492 | * @param portNumber the port on this device that needs to be updated |
| 1493 | * @param vlanId the vlan id corresponding to this port |
| 1494 | * @param pushVlan indicates if packets should be sent out untagged or not out |
| 1495 | * from the port. If true, updated TrafficTreatment involves |
| 1496 | * pop vlan tag action. If false, updated TrafficTreatment |
| 1497 | * does not involve pop vlan tag action. |
| 1498 | */ |
| 1499 | public void updateL2InterfaceGroupBucket(PortNumber portNumber, VlanId vlanId, boolean pushVlan) { |
| 1500 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1501 | if (pushVlan) { |
| 1502 | tBuilder.popVlan(); |
| 1503 | } |
| 1504 | tBuilder.setOutput(portNumber); |
| 1505 | |
| 1506 | TrafficSelector metadata = |
| 1507 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
| 1508 | |
| 1509 | int nextId = getVlanNextObjectiveId(vlanId); |
| 1510 | |
| 1511 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1512 | .builder().withId(nextId) |
| 1513 | .withType(NextObjective.Type.SIMPLE).fromApp(appId) |
| 1514 | .addTreatment(tBuilder.build()) |
| 1515 | .withMeta(metadata); |
| 1516 | |
| 1517 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1518 | (objective) -> log.debug("port {} successfully updated NextObj {} on {}", |
| 1519 | portNumber, nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1520 | (objective, error) -> { |
| 1521 | log.warn("port {} failed to updated NextObj {} on {}: {}", portNumber, nextId, deviceId, error); |
| 1522 | srManager.invalidateNextObj(objective.id()); |
| 1523 | }); |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1524 | |
| 1525 | flowObjectiveService.next(deviceId, nextObjBuilder.modify(context)); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1528 | /** |
Ruchi Sahota | 5d80028 | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1529 | * Updates the next objective for the given nextId . |
| 1530 | * |
| 1531 | * @param hostMac mac of host for which Next obj is to be updated. |
| 1532 | * @param hostVlanId vlan of host for which Next obj is to be updated. |
| 1533 | * @param port port with which to update the Next Obj. |
| 1534 | * @param nextId of Next Obj which needs to be updated. |
| 1535 | */ |
| 1536 | public void updateL3UcastGroupBucket(MacAddress hostMac, VlanId hostVlanId, PortNumber port, int nextId) { |
| 1537 | |
| 1538 | MacAddress deviceMac; |
| 1539 | try { |
| 1540 | deviceMac = deviceConfig.getDeviceMac(deviceId); |
| 1541 | } catch (DeviceConfigNotFoundException e) { |
| 1542 | log.warn(e.getMessage() + " in updateL3UcastGroupBucket"); |
| 1543 | return; |
| 1544 | } |
| 1545 | |
| 1546 | TrafficSelector metadata = |
| 1547 | DefaultTrafficSelector.builder().matchVlanId(hostVlanId).build(); |
| 1548 | |
| 1549 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 1550 | tbuilder.deferred() |
| 1551 | .setEthDst(hostMac) |
| 1552 | .setEthSrc(deviceMac) |
| 1553 | .setVlanId(hostVlanId) |
| 1554 | .setOutput(port); |
| 1555 | |
| 1556 | log.debug(" update L3Ucast : deviceMac {}, port {}, host {}/{}, nextid {}, Treatment {} Meta {}", |
| 1557 | deviceMac, port, hostMac, hostVlanId, nextId, tbuilder.build(), metadata); |
| 1558 | |
| 1559 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1560 | .builder().withId(nextId) |
| 1561 | .withType(NextObjective.Type.SIMPLE).fromApp(appId) |
| 1562 | .addTreatment(tbuilder.build()) |
| 1563 | .withMeta(metadata); |
| 1564 | |
| 1565 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1566 | (objective) -> log.debug(" NextId {} successfully updated host {} vlan {} with port {}", |
| 1567 | nextId, hostMac, hostVlanId, port), |
| 1568 | (objective, error) -> { |
| 1569 | log.warn(" NextId {} failed to update host {} vlan {} with port {}, error : {}", |
| 1570 | nextId, hostMac, hostVlanId, port, error); |
| 1571 | srManager.invalidateNextObj(objective.id()); |
| 1572 | }); |
| 1573 | |
| 1574 | NextObjective nextObj = nextObjBuilder.modify(context); |
| 1575 | flowObjectiveService.next(deviceId, nextObj); |
| 1576 | |
| 1577 | } |
| 1578 | |
| 1579 | /** |
Jonghwan Hyun | f810a7a | 2018-02-12 16:43:45 +0900 | [diff] [blame] | 1580 | * Adds a single port to the L2FG or removes it from the L2FG. |
| 1581 | * |
| 1582 | * @param vlanId the vlan id corresponding to this port |
| 1583 | * @param portNum the port on this device to be updated |
| 1584 | * @param nextId the next objective ID for the given vlan id |
| 1585 | * @param install if true, adds the port to L2FG. If false, removes it from L2FG. |
| 1586 | */ |
| 1587 | public void updateGroupFromVlanConfiguration(VlanId vlanId, PortNumber portNum, int nextId, boolean install) { |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1588 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1589 | if (toPopVlan(portNum, vlanId)) { |
| 1590 | tBuilder.popVlan(); |
| 1591 | } |
| 1592 | tBuilder.setOutput(portNum); |
| 1593 | |
| 1594 | TrafficSelector metadata = |
| 1595 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
| 1596 | |
| 1597 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 1598 | .builder().withId(nextId) |
| 1599 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 1600 | .addTreatment(tBuilder.build()) |
| 1601 | .withMeta(metadata); |
| 1602 | |
| 1603 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1604 | (objective) -> log.debug("port {} successfully removedFrom NextObj {} on {}", |
| 1605 | portNum, nextId, deviceId), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 1606 | (objective, error) -> { |
| 1607 | log.warn("port {} failed to removedFrom NextObj {} on {}: {}", portNum, nextId, deviceId, error); |
| 1608 | srManager.invalidateNextObj(objective.id()); |
| 1609 | }); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1610 | |
| 1611 | if (install) { |
| 1612 | flowObjectiveService.next(deviceId, nextObjBuilder.addToExisting(context)); |
| 1613 | } else { |
| 1614 | flowObjectiveService.next(deviceId, nextObjBuilder.removeFromExisting(context)); |
| 1615 | } |
| 1616 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1617 | |
| 1618 | /** |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1619 | * Performs bucket verification operation for all hash groups in this device. |
| 1620 | * Checks RouteHandler to ensure that routing is stable before attempting |
| 1621 | * verification. Verification involves creating a nextObjective with |
| 1622 | * operation VERIFY for existing next objectives in the store, and passing |
| 1623 | * it to the driver. It is the driver that actually performs the verification |
| 1624 | * by adding or removing buckets to match the verification next objective |
| 1625 | * created here. |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1626 | */ |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1627 | protected final class BucketCorrector implements Runnable { |
| 1628 | Integer nextId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1629 | |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1630 | BucketCorrector() { |
| 1631 | this.nextId = null; |
| 1632 | } |
| 1633 | |
| 1634 | BucketCorrector(Integer nextId) { |
| 1635 | this.nextId = nextId; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | @Override |
| 1639 | public void run() { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1640 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1641 | return; |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1642 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1643 | DefaultRoutingHandler rh = srManager.getRoutingHandler(); |
| 1644 | if (rh == null) { |
| 1645 | return; |
| 1646 | } |
| 1647 | if (!rh.isRoutingStable()) { |
| 1648 | return; |
| 1649 | } |
| 1650 | rh.acquireRoutingLock(); |
| 1651 | try { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1652 | log.trace("running bucket corrector for dev: {}", deviceId); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1653 | Set<DestinationSetNextObjectiveStoreKey> dsKeySet = dsNextObjStore.entrySet() |
| 1654 | .stream() |
| 1655 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 1656 | // Filter out PW transit groups or include them if MPLS ECMP is supported |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1657 | .filter(entry -> !entry.getKey().destinationSet().notBos() || |
| 1658 | (entry.getKey().destinationSet().notBos() && srManager.getMplsEcmp())) |
| 1659 | // Filter out simple SWAP groups or include them if MPLS ECMP is supported |
| 1660 | .filter(entry -> !entry.getKey().destinationSet().swap() || |
| 1661 | (entry.getKey().destinationSet().swap() && srManager.getMplsEcmp())) |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1662 | .map(entry -> entry.getKey()) |
| 1663 | .collect(Collectors.toSet()); |
| 1664 | for (DestinationSetNextObjectiveStoreKey dsKey : dsKeySet) { |
| 1665 | NextNeighbors next = dsNextObjStore.get(dsKey); |
| 1666 | if (next == null) { |
| 1667 | continue; |
| 1668 | } |
| 1669 | int nid = next.nextId(); |
| 1670 | if (nextId != null && nextId != nid) { |
| 1671 | continue; |
| 1672 | } |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1673 | log.trace("bkt-corr: dsNextObjStore for device {}: {}", |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1674 | deviceId, dsKey, next); |
| 1675 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
Saurav Das | 3c82f19 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 1676 | metabuilder.matchVlanId(srManager.getDefaultInternalVlan()); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1677 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 1678 | .withId(nid) |
| 1679 | .withType(NextObjective.Type.HASHED) |
| 1680 | .withMeta(metabuilder.build()) |
| 1681 | .fromApp(appId); |
| 1682 | |
| 1683 | next.dstNextHops().forEach((dstDev, nextHops) -> { |
| 1684 | int edgeLabel = dsKey.destinationSet().getEdgeLabel(dstDev); |
| 1685 | nextHops.forEach(neighbor -> { |
| 1686 | MacAddress neighborMac; |
| 1687 | try { |
| 1688 | neighborMac = deviceConfig.getDeviceMac(neighbor); |
| 1689 | } catch (DeviceConfigNotFoundException e) { |
| 1690 | log.warn(e.getMessage() + " Aborting neighbor" |
| 1691 | + neighbor); |
| 1692 | return; |
| 1693 | } |
| 1694 | devicePortMap.get(neighbor).forEach(port -> { |
Saurav Das | 9df5b7c | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1695 | log.trace("verify in device {} nextId {}: bucket with" |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1696 | + " port/label {}/{} to dst {} via {}", |
| 1697 | deviceId, nid, port, edgeLabel, |
| 1698 | dstDev, neighbor); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1699 | nextObjBuilder |
| 1700 | .addTreatment(treatmentBuilder(port, |
| 1701 | neighborMac, |
| 1702 | dsKey.destinationSet().swap(), |
| 1703 | edgeLabel)); |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1704 | }); |
| 1705 | }); |
| 1706 | }); |
| 1707 | |
| 1708 | NextObjective nextObjective = nextObjBuilder.verify(); |
| 1709 | flowObjectiveService.next(deviceId, nextObjective); |
| 1710 | } |
| 1711 | } finally { |
| 1712 | rh.releaseRoutingLock(); |
| 1713 | } |
| 1714 | |
| 1715 | } |
| 1716 | |
| 1717 | TrafficTreatment treatmentBuilder(PortNumber outport, MacAddress dstMac, |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1718 | boolean swap, int edgeLabel) { |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1719 | TrafficTreatment.Builder tBuilder = |
| 1720 | DefaultTrafficTreatment.builder(); |
| 1721 | tBuilder.setOutput(outport) |
| 1722 | .setEthDst(dstMac) |
| 1723 | .setEthSrc(nodeMacAddr); |
| 1724 | if (edgeLabel != DestinationSet.NO_EDGE_LABEL) { |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1725 | if (swap) { |
| 1726 | // swap label case |
| 1727 | tBuilder.setMpls(MplsLabel.mplsLabel(edgeLabel)); |
| 1728 | } else { |
| 1729 | // ecmp with label push case |
| 1730 | tBuilder.pushMpls() |
| 1731 | .copyTtlOut() |
| 1732 | .setMpls(MplsLabel.mplsLabel(edgeLabel)); |
| 1733 | } |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 1734 | } |
| 1735 | return tBuilder.build(); |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
Pier Luigi | 63edd93 | 2018-01-14 21:56:11 +0100 | [diff] [blame] | 1739 | } |