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