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