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