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