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