Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Iterables; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
| 21 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 22 | import org.apache.commons.lang3.RandomUtils; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.MplsLabel; |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.core.ApplicationId; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.PortNumber; |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficTreatment; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | a4ee4f9 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 39 | import org.onosproject.net.flowobjective.NextObjective; |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 41 | import org.onosproject.net.link.LinkService; |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 42 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 43 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 44 | import org.onosproject.segmentrouting.config.DeviceProperties; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 45 | import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey; |
| 46 | import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 47 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 48 | import org.onosproject.store.service.EventuallyConsistentMap; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 51 | import java.net.URI; |
| 52 | import java.util.ArrayList; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 53 | import java.util.Collection; |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 54 | import java.util.Collections; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 55 | import java.util.HashMap; |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 56 | import java.util.HashSet; |
| 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Set; |
| 60 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ScheduledExecutorService; |
| 62 | import java.util.concurrent.TimeUnit; |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 63 | import java.util.stream.Collectors; |
| 64 | |
| 65 | import static com.google.common.base.Preconditions.checkNotNull; |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 66 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 67 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 68 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 69 | import static org.slf4j.LoggerFactory.getLogger; |
| 70 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 71 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 72 | * Default ECMP group handler creation module. This component creates a set of |
| 73 | * ECMP groups for every neighbor that this device is connected to based on |
| 74 | * whether the current device is an edge device or a transit device. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 75 | */ |
| 76 | public class DefaultGroupHandler { |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 77 | protected static final Logger log = getLogger(DefaultGroupHandler.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 78 | |
| 79 | protected final DeviceId deviceId; |
| 80 | protected final ApplicationId appId; |
| 81 | protected final DeviceProperties deviceConfig; |
| 82 | protected final List<Integer> allSegmentIds; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 83 | protected int ipv4NodeSegmentId = -1; |
| 84 | protected int ipv6NodeSegmentId = -1; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 85 | protected boolean isEdgeRouter = false; |
| 86 | protected MacAddress nodeMacAddr = null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 87 | protected LinkService linkService; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 88 | protected FlowObjectiveService flowObjectiveService; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 89 | /** |
| 90 | * local store for neighbor-device-ids and the set of ports on this device |
| 91 | * that connect to the same neighbor. |
| 92 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 93 | protected ConcurrentHashMap<DeviceId, Set<PortNumber>> devicePortMap = |
| 94 | new ConcurrentHashMap<>(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 95 | /** |
| 96 | * local store for ports on this device connected to neighbor-device-id. |
| 97 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 98 | protected ConcurrentHashMap<PortNumber, DeviceId> portDeviceMap = |
| 99 | new ConcurrentHashMap<>(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 100 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 101 | // distributed store for (device+neighborset) mapped to next-id |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 102 | protected EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> |
| 103 | nsNextObjStore = null; |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 104 | // distributed store for (device+subnet-ip-prefix) mapped to next-id |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 105 | protected EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> |
| 106 | vlanNextObjStore = null; |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 107 | // distributed store for (device+port+treatment) mapped to next-id |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 108 | protected EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> |
| 109 | portNextObjStore = null; |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 110 | private SegmentRoutingManager srManager; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 111 | |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 112 | private static final long RETRY_INTERVAL_SEC = 30; |
| 113 | private ScheduledExecutorService executorService |
| 114 | = newScheduledThreadPool(1, groupedThreads("retryhashbkts", "retry-%d", log)); |
| 115 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 116 | protected KryoNamespace.Builder kryo = new KryoNamespace.Builder() |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 117 | .register(URI.class).register(HashSet.class) |
Ray Milkey | 78b21a1 | 2017-06-15 11:55:51 -0700 | [diff] [blame] | 118 | .register(PortNumber.class) |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 119 | .register(NeighborSet.class).register(PolicyGroupIdentifier.class) |
| 120 | .register(PolicyGroupParams.class) |
| 121 | .register(GroupBucketIdentifier.class) |
| 122 | .register(GroupBucketIdentifier.BucketOutputType.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 123 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 124 | protected DefaultGroupHandler(DeviceId deviceId, ApplicationId appId, |
| 125 | DeviceProperties config, |
| 126 | LinkService linkService, |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 127 | FlowObjectiveService flowObjService, |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 128 | SegmentRoutingManager srManager) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 129 | this.deviceId = checkNotNull(deviceId); |
| 130 | this.appId = checkNotNull(appId); |
| 131 | this.deviceConfig = checkNotNull(config); |
| 132 | this.linkService = checkNotNull(linkService); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 133 | this.allSegmentIds = checkNotNull(config.getAllDeviceSegmentIds()); |
| 134 | try { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 135 | this.ipv4NodeSegmentId = config.getIPv4SegmentId(deviceId); |
| 136 | this.ipv6NodeSegmentId = config.getIPv6SegmentId(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 137 | this.isEdgeRouter = config.isEdgeDevice(deviceId); |
| 138 | this.nodeMacAddr = checkNotNull(config.getDeviceMac(deviceId)); |
| 139 | } catch (DeviceConfigNotFoundException e) { |
| 140 | log.warn(e.getMessage() |
| 141 | + " Skipping value assignment in DefaultGroupHandler"); |
| 142 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 143 | this.flowObjectiveService = flowObjService; |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 144 | this.nsNextObjStore = srManager.nsNextObjStore(); |
| 145 | this.vlanNextObjStore = srManager.vlanNextObjStore(); |
| 146 | this.portNextObjStore = srManager.portNextObjStore(); |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 147 | this.srManager = srManager; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 148 | |
| 149 | populateNeighborMaps(); |
| 150 | } |
| 151 | |
| 152 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 153 | * Creates a group handler object. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 154 | * |
| 155 | * @param deviceId device identifier |
| 156 | * @param appId application identifier |
| 157 | * @param config interface to retrieve the device properties |
| 158 | * @param linkService link service object |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 159 | * @param flowObjService flow objective service object |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 160 | * @param srManager segment routing manager |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 161 | * @throws DeviceConfigNotFoundException if the device configuration is not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 162 | * @return default group handler type |
| 163 | */ |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 164 | public static DefaultGroupHandler createGroupHandler( |
| 165 | DeviceId deviceId, |
| 166 | ApplicationId appId, |
| 167 | DeviceProperties config, |
| 168 | LinkService linkService, |
| 169 | FlowObjectiveService flowObjService, |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 170 | SegmentRoutingManager srManager) |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 171 | throws DeviceConfigNotFoundException { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 172 | return new DefaultGroupHandler(deviceId, appId, config, |
| 173 | linkService, |
| 174 | flowObjService, |
| 175 | srManager); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 179 | * Updates local stores for link-src device/port to neighbor (link-dst). |
| 180 | * |
| 181 | * @param link the infrastructure link |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 182 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 183 | public void portUpForLink(Link link) { |
| 184 | if (!link.src().deviceId().equals(deviceId)) { |
| 185 | log.warn("linkUp: deviceId{} doesn't match with link src {}", |
| 186 | deviceId, link.src().deviceId()); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | log.info("* portUpForLink: Device {} linkUp at local port {} to " |
| 191 | + "neighbor {}", deviceId, link.src().port(), link.dst().deviceId()); |
| 192 | // ensure local state is updated even if linkup is aborted later on |
| 193 | addNeighborAtPort(link.dst().deviceId(), |
| 194 | link.src().port()); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Updates local stores for port that has gone down. |
| 199 | * |
| 200 | * @param port port number that has gone down |
| 201 | */ |
| 202 | public void portDown(PortNumber port) { |
| 203 | if (portDeviceMap.get(port) == null) { |
| 204 | log.warn("portDown: unknown port"); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | log.debug("Device {} portDown {} to neighbor {}", deviceId, port, |
| 209 | portDeviceMap.get(port)); |
| 210 | devicePortMap.get(portDeviceMap.get(port)).remove(port); |
| 211 | portDeviceMap.remove(port); |
| 212 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 215 | * Checks all groups in the src-device of link for neighbor sets that include |
| 216 | * the dst-device of link, and edits the hash groups according to link up |
| 217 | * or down. Should only be called by the master instance of the src-switch |
| 218 | * of link. Typically used when there are no route-path changes due to the |
| 219 | * link up or down, as the ECMPspg does not change. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 220 | * |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 221 | * @param link the infrastructure link that has gone down or come up |
| 222 | * @param linkDown true if link has gone down |
| 223 | * @param firstTime true if link has come up for the first time i.e a link |
| 224 | * not seen-before |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 225 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 226 | public void retryHash(Link link, boolean linkDown, boolean firstTime) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 227 | MacAddress dstMac; |
| 228 | try { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 229 | dstMac = deviceConfig.getDeviceMac(link.dst().deviceId()); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 230 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 231 | log.warn(e.getMessage() + " Aborting retryHash."); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 232 | return; |
| 233 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 234 | // find all the neighborSets related to link |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 235 | Set<NeighborSet> nsSet = nsNextObjStore.keySet() |
| 236 | .stream() |
| 237 | .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId))) |
| 238 | .map((nsStoreEntry) -> (nsStoreEntry.neighborSet())) |
| 239 | .filter((ns) -> (ns.getDeviceIds() |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 240 | .contains(link.dst().deviceId()))) |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 241 | .collect(Collectors.toSet()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 242 | log.debug("retryHash: nsNextObjStore contents for linkSrc {} -> linkDst {}: {}", |
| 243 | deviceId, link.dst().deviceId(), nsSet); |
| 244 | |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 245 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 246 | Integer nextId = nsNextObjStore. |
| 247 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 248 | if (nextId == null) { |
| 249 | log.warn("retryHash in device {}, but global store has no record " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 250 | + "for neighbor-set {}", deviceId, ns); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | if (!linkDown) { |
| 254 | addToHashedNextObjective(link.src().port(), dstMac, ns, |
| 255 | nextId, false); |
| 256 | if (firstTime) { |
| 257 | // some links may have come up before the next-objective was created |
| 258 | // we take this opportunity to ensure other ports to same next-hop-dst |
| 259 | // are part of the hash group (see CORD-1180). Duplicate additions |
| 260 | // to the same hash group are avoided by the driver. |
| 261 | for (PortNumber p : devicePortMap.get(link.dst().deviceId())) { |
| 262 | if (p.equals(link.src().port())) { |
| 263 | continue; |
| 264 | } |
| 265 | addToHashedNextObjective(p, dstMac, ns, nextId, false); |
| 266 | } |
| 267 | } |
| 268 | } else { |
| 269 | removeFromHashedNextObjective(link.src().port(), dstMac, ns, |
| 270 | nextId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 271 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 272 | } |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 273 | |
| 274 | // It's possible that at the time of linkup, some hash-groups have |
| 275 | // not been created yet by the instance responsible for creating them, or |
| 276 | // due to the eventually-consistent nature of the nsNextObjStore it has |
| 277 | // not synced up with this instance yet. Thus we perform this check again |
| 278 | // after a delay (see CORD-1180). Duplicate additions to the same hash group |
| 279 | // are avoided by the driver. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 280 | if (!linkDown && firstTime) { |
| 281 | executorService.schedule(new RetryHashBkts(link, dstMac), |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 282 | RETRY_INTERVAL_SEC, TimeUnit.SECONDS); |
| 283 | } |
| 284 | } |
| 285 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 286 | /** |
| 287 | * Makes a call to the FlowObjective service to add a single bucket to |
| 288 | * a hashed group. |
| 289 | * |
| 290 | * @param outport port to add to hash group |
| 291 | * @param dstMac destination mac address of next-hop |
| 292 | * @param ns neighbor set representing next-hops and destination switch |
| 293 | * @param nextId id for next-objective to which the bucket will be added |
| 294 | * @param retry indicates if this method is being called on a retry attempt |
| 295 | * at adding a bucket to the group |
| 296 | */ |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 297 | private void addToHashedNextObjective(PortNumber outport, MacAddress dstMac, |
| 298 | NeighborSet ns, Integer nextId, boolean retry) { |
| 299 | // Create the new bucket to be updated |
| 300 | TrafficTreatment.Builder tBuilder = |
| 301 | DefaultTrafficTreatment.builder(); |
| 302 | tBuilder.setOutput(outport) |
| 303 | .setEthDst(dstMac) |
| 304 | .setEthSrc(nodeMacAddr); |
| 305 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
| 306 | tBuilder.pushMpls() |
| 307 | .copyTtlOut() |
| 308 | .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel())); |
| 309 | } |
| 310 | // setup metadata to pass to nextObjective - indicate the vlan on egress |
| 311 | // if needed by the switch pipeline. Since hashed next-hops are always to |
| 312 | // other neighboring routers, there is no subnet assigned on those ports. |
| 313 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 314 | metabuilder.matchVlanId(INTERNAL_VLAN); |
| 315 | |
| 316 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder() |
| 317 | .withId(nextId) |
| 318 | .withType(NextObjective.Type.HASHED) |
| 319 | .addTreatment(tBuilder.build()) |
| 320 | .withMeta(metabuilder.build()) |
| 321 | .fromApp(appId); |
| 322 | log.info("{} in device {}: Adding Bucket with Port {} to next object id {}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 323 | (retry) ? "retry-addToHash" : "addToHash", |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 324 | deviceId, outport, nextId); |
| 325 | |
| 326 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 327 | (objective) -> log.debug("{} addedTo NextObj {} on {}", |
| 328 | (retry) ? "retry-addToHash" : "addToHash", |
| 329 | nextId, deviceId), |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 330 | (objective, error) -> |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 331 | log.warn("{} failed to addTo NextObj {} on {}: {}", |
| 332 | (retry) ? "retry-addToHash" : "addToHash", |
| 333 | nextId, deviceId, error)); |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 334 | NextObjective nextObjective = nextObjBuilder.addToExisting(context); |
| 335 | flowObjectiveService.next(deviceId, nextObjective); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 339 | * Makes a call to the FlowObjective service to remove a single bucket from |
| 340 | * a hashed group. |
| 341 | * |
| 342 | * @param port port to remove from hash group |
| 343 | * @param dstMac destination mac address of next-hop |
| 344 | * @param ns neighbor set representing next-hops and destination switch |
| 345 | * @param nextId id for next-objective from which the bucket will be removed |
| 346 | */ |
| 347 | private void removeFromHashedNextObjective(PortNumber port, MacAddress dstMac, |
| 348 | NeighborSet ns, Integer nextId) { |
| 349 | // Create the bucket to be removed |
| 350 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 351 | .builder(); |
| 352 | tBuilder.setOutput(port) |
| 353 | .setEthDst(dstMac) |
| 354 | .setEthSrc(nodeMacAddr); |
| 355 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
| 356 | tBuilder.pushMpls() |
| 357 | .copyTtlOut() |
| 358 | .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel())); |
| 359 | } |
| 360 | log.info("{} in device {}: Removing Bucket with Port {} to next object id {}", |
| 361 | "removeFromHash", deviceId, port, nextId); |
| 362 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 363 | .builder() |
| 364 | .withType(NextObjective.Type.HASHED) //same as original |
| 365 | .withId(nextId) |
| 366 | .fromApp(appId) |
| 367 | .addTreatment(tBuilder.build()); |
| 368 | ObjectiveContext context = new DefaultObjectiveContext( |
| 369 | (objective) -> log.debug("port {} removedFrom NextObj {} on {}", |
| 370 | port, nextId, deviceId), |
| 371 | (objective, error) -> |
| 372 | log.warn("port {} failed to removeFrom NextObj {} on {}: {}", |
| 373 | port, nextId, deviceId, error)); |
| 374 | NextObjective nextObjective = nextObjBuilder. |
| 375 | removeFromExisting(context); |
| 376 | |
| 377 | flowObjectiveService.next(deviceId, nextObjective); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Checks all the hash-groups in the target-switch meant for the destination |
| 382 | * switch, and either adds or removes buckets to make the neighbor-set |
| 383 | * match the given next-hops. Typically called by the master instance of the |
| 384 | * destination switch, which may be different from the master instance of the |
| 385 | * target switch where hash-group changes are made. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 386 | * |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 387 | * @param targetSw the switch in which the hash groups will be edited |
| 388 | * @param nextHops the current next hops for the target switch to reach |
| 389 | * the dest sw |
| 390 | * @param destSw the destination switch |
| 391 | * @param revoke true if hash groups need to remove buckets from the |
| 392 | * the groups to match the current next hops |
| 393 | * @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] | 394 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 395 | public boolean fixHashGroups(DeviceId targetSw, Set<DeviceId> nextHops, |
| 396 | DeviceId destSw, boolean revoke) { |
| 397 | // temporary storage of keys to be updated |
| 398 | Map<NeighborSetNextObjectiveStoreKey, Set<DeviceId>> tempStore = |
| 399 | new HashMap<>(); |
| 400 | boolean foundNextObjective = false; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 401 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 402 | // retrieve hash-groups meant for destSw, which have neighborSets |
| 403 | // with different neighbors than the given next-hops |
| 404 | for (NeighborSetNextObjectiveStoreKey nskey : nsNextObjStore.keySet()) { |
| 405 | if (!nskey.deviceId().equals(targetSw) || |
| 406 | !nskey.neighborSet().getDestinationSw().equals(destSw)) { |
| 407 | continue; |
| 408 | } |
| 409 | foundNextObjective = true; |
| 410 | Set<DeviceId> currNeighbors = nskey.neighborSet().getDeviceIds(); |
| 411 | Integer nextId = nsNextObjStore.get(nskey); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 412 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 413 | Set<DeviceId> diff; |
| 414 | if (revoke) { |
| 415 | diff = Sets.difference(currNeighbors, nextHops); |
| 416 | log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next " |
| 417 | + "hops:{} ..removing {}", targetSw, destSw, nextId, |
| 418 | currNeighbors, diff); |
| 419 | } else { |
| 420 | diff = Sets.difference(nextHops, currNeighbors); |
| 421 | log.debug("targetSw:{} -> dstSw:{} in nextId:{} has current next " |
| 422 | + "hops:{} ..adding {}", targetSw, destSw, nextId, |
| 423 | currNeighbors, diff); |
| 424 | } |
| 425 | for (DeviceId neighbor : diff) { |
| 426 | MacAddress dstMac; |
| 427 | try { |
| 428 | dstMac = deviceConfig.getDeviceMac(neighbor); |
| 429 | } catch (DeviceConfigNotFoundException e) { |
| 430 | log.warn(e.getMessage() + " Aborting fixHashGroup for nextId:" |
| 431 | + nskey); |
| 432 | return false; |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 433 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 434 | if (devicePortMap.get(neighbor) == null || |
| 435 | devicePortMap.get(neighbor).isEmpty()) { |
| 436 | log.warn("No ports found in dev:{} for neighbor:{} .. cannot " |
| 437 | + "fix hash group for nextId: {}", |
| 438 | deviceId, neighbor, nextId); |
| 439 | return false; |
| 440 | } |
| 441 | if (revoke) { |
| 442 | for (PortNumber port : devicePortMap.get(neighbor)) { |
| 443 | log.info("fixHashGroup in device {}: Removing Bucket " |
| 444 | + "with Port {} to next object id {}", |
| 445 | deviceId, port, nextId); |
| 446 | removeFromHashedNextObjective(port, dstMac, |
| 447 | nskey.neighborSet(), |
| 448 | nextId); |
| 449 | } |
| 450 | // to update neighbor set with changes made |
| 451 | tempStore.put(nskey, Sets.difference(currNeighbors, diff)); |
| 452 | } else { |
| 453 | for (PortNumber port : devicePortMap.get(neighbor)) { |
| 454 | log.info("fixHashGroup in device {}: Adding Bucket " |
| 455 | + "with Port {} to next object id {}", |
| 456 | deviceId, port, nextId); |
| 457 | addToHashedNextObjective(port, dstMac, |
| 458 | nskey.neighborSet(), |
| 459 | nextId, false); |
| 460 | } |
| 461 | // to update neighbor set with changes made |
| 462 | tempStore.put(nskey, Sets.union(currNeighbors, diff)); |
| 463 | } |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 464 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 467 | if (!foundNextObjective) { |
| 468 | log.debug("Cannot find any nextObjectives for route targetSw:{} " |
| 469 | + "-> dstSw:{}", targetSw, destSw); |
| 470 | return true; // nothing to do, return true so ECMPspg is updated |
| 471 | } |
| 472 | |
| 473 | // update the nsNextObjectiveStore with new neighborSets to nextId mappings |
| 474 | for (NeighborSetNextObjectiveStoreKey oldkey : tempStore.keySet()) { |
| 475 | Integer nextId = nsNextObjStore.get(oldkey); |
| 476 | if (nextId == null) { |
| 477 | continue; |
| 478 | } |
| 479 | Set<DeviceId> newNeighbors = tempStore.get(oldkey); |
| 480 | NeighborSet newNs = new NeighborSet(newNeighbors, |
| 481 | oldkey.neighborSet().mplsSet(), |
| 482 | oldkey.neighborSet().getEdgeLabel(), |
| 483 | oldkey.neighborSet().getDestinationSw()); |
| 484 | NeighborSetNextObjectiveStoreKey newkey = |
| 485 | new NeighborSetNextObjectiveStoreKey(deviceId, newNs); |
| 486 | log.debug("Updating nsNextObjStore: oldKey:{} -> newKey:{} :: nextId:{}", |
| 487 | oldkey, newkey, nextId); |
| 488 | synchronized (nsNextObjStore) { |
| 489 | nsNextObjStore.remove(oldkey); |
| 490 | nsNextObjStore.put(newkey, nextId); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return true; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 498 | * Adds or removes a port that has been configured with a vlan to a broadcast group |
| 499 | * 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] | 500 | * |
| 501 | * @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] | 502 | * @param vlanId the vlan id corresponding to the broadcast domain/group |
| 503 | * @param popVlan indicates if packets should be sent out untagged or not out |
| 504 | * of the port. If true, indicates an access (untagged) or native vlan |
| 505 | * configuration. If false, indicates a trunk (tagged) vlan config. |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 506 | * @param portUp true if port is enabled, false if disabled |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 507 | */ |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 508 | public void processEdgePort(PortNumber port, VlanId vlanId, |
| 509 | boolean popVlan, boolean portUp) { |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 510 | //get the next id for the subnet and edit it. |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 511 | Integer nextId = getVlanNextObjectiveId(vlanId); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 512 | if (nextId == -1) { |
| 513 | if (portUp) { |
| 514 | log.debug("**Creating flooding group for first port enabled in" |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 515 | + " subnet {} on dev {} port {}", vlanId, deviceId, port); |
| 516 | createBcastGroupFromVlan(vlanId, Collections.singleton(port)); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 517 | } else { |
| 518 | log.warn("Could not find flooding group for subnet {} on dev:{} when" |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 519 | + " removing port:{}", vlanId, deviceId, port); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 520 | } |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | log.info("**port{} in device {}: {} Bucket with Port {} to" |
| 525 | + " next-id {}", (portUp) ? "UP" : "DOWN", deviceId, |
| 526 | (portUp) ? "Adding" : "Removing", |
| 527 | port, nextId); |
| 528 | // Create the bucket to be added or removed |
| 529 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 530 | if (popVlan) { |
| 531 | tBuilder.popVlan(); |
| 532 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 533 | tBuilder.setOutput(port); |
| 534 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 535 | TrafficSelector metadata = |
Saurav Das | 3fb2827 | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 536 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 537 | |
| 538 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 539 | .builder().withId(nextId) |
| 540 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 541 | .addTreatment(tBuilder.build()) |
| 542 | .withMeta(metadata); |
| 543 | |
| 544 | ObjectiveContext context = new DefaultObjectiveContext( |
| 545 | (objective) -> log.debug("port {} successfully {} NextObj {} on {}", |
| 546 | port, (portUp) ? "addedTo" : "removedFrom", |
| 547 | nextId, deviceId), |
| 548 | (objective, error) -> |
| 549 | log.warn("port {} failed to {} NextObj {} on {}: {}", |
| 550 | port, (portUp) ? "addTo" : "removeFrom", |
| 551 | nextId, deviceId, error)); |
| 552 | |
| 553 | NextObjective nextObj = (portUp) ? nextObjBuilder.addToExisting(context) |
| 554 | : nextObjBuilder.removeFromExisting(context); |
| 555 | log.debug("edgePort processed: Submited next objective {} in device {}", |
| 556 | nextId, deviceId); |
| 557 | flowObjectiveService.next(deviceId, nextObj); |
| 558 | } |
| 559 | |
| 560 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 561 | * Returns the next objective of type hashed associated with the neighborset. |
| 562 | * If there is no next objective for this neighborset, this method |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 563 | * would create a next objective and return. Optionally metadata can be |
| 564 | * passed in for the creation of the next objective. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 565 | * |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 566 | * @param ns neighborset |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 567 | * @param meta metadata passed into the creation of a Next Objective |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 568 | * @param isBos if Bos is set |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 569 | * @return int if found or -1 if there are errors in the creation of the |
| 570 | * neighbor set. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 571 | */ |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 572 | public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta, boolean isBos) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 573 | Integer nextId = nsNextObjStore. |
| 574 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 575 | if (nextId == null) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 576 | log.trace("getNextObjectiveId in device{}: Next objective id " |
| 577 | + "not found for {} and creating", deviceId, ns); |
| 578 | log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}", |
| 579 | deviceId, |
| 580 | nsNextObjStore.entrySet() |
| 581 | .stream() |
| 582 | .filter((nsStoreEntry) -> |
| 583 | (nsStoreEntry.getKey().deviceId().equals(deviceId))) |
| 584 | .collect(Collectors.toList())); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 585 | createGroupsFromNeighborsets(Collections.singleton(ns), meta, isBos); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 586 | nextId = nsNextObjStore. |
| 587 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 588 | if (nextId == null) { |
| 589 | log.warn("getNextObjectiveId: unable to create next objective"); |
| 590 | return -1; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 591 | } else { |
| 592 | log.debug("getNextObjectiveId in device{}: Next objective id {} " |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 593 | + "created for {}", deviceId, nextId, ns); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 594 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 595 | } else { |
| 596 | log.trace("getNextObjectiveId in device{}: Next objective id {} " |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 597 | + "found for {}", deviceId, nextId, ns); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 598 | } |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 599 | return nextId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 600 | } |
| 601 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 602 | /** |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 603 | * Returns the next objective of type broadcast associated with the vlan, |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 604 | * or -1 if no such objective exists. Note that this method does NOT create |
| 605 | * 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] | 606 | * created at startup from network configuration. Typically this is used |
| 607 | * for L2 flooding within the subnet configured on the switch. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 608 | * |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 609 | * @param vlanId vlan id |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 610 | * @return int if found or -1 |
| 611 | */ |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 612 | public int getVlanNextObjectiveId(VlanId vlanId) { |
| 613 | Integer nextId = vlanNextObjStore. |
| 614 | get(new VlanNextObjectiveStoreKey(deviceId, vlanId)); |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 615 | |
| 616 | return (nextId != null) ? nextId : -1; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 620 | * Returns the next objective of type simple associated with the port on the |
| 621 | * device, given the treatment. Different treatments to the same port result |
| 622 | * in different next objectives. If no such objective exists, this method |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 623 | * 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] | 624 | * the creation of the objective. Typically this is used for L2 and L3 forwarding |
| 625 | * to compute nodes and containers/VMs on the compute nodes directly attached |
| 626 | * to the switch. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 627 | * |
| 628 | * @param portNum the port number for the simple next objective |
| 629 | * @param treatment the actions to apply on the packets (should include outport) |
| 630 | * @param meta optional metadata passed into the creation of the next objective |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 631 | * @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] | 632 | * @return int if found or created, -1 if there are errors during the |
| 633 | * creation of the next objective. |
| 634 | */ |
| 635 | public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 636 | TrafficSelector meta, boolean createIfMissing) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 637 | Integer nextId = portNextObjStore |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 638 | .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment, meta)); |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 639 | if (nextId != null) { |
| 640 | return nextId; |
| 641 | } |
| 642 | log.debug("getPortNextObjectiveId in device {}: Next objective id " |
| 643 | + "not found for port: {} .. {}", deviceId, portNum, |
| 644 | (createIfMissing) ? "creating" : "aborting"); |
| 645 | if (!createIfMissing) { |
| 646 | return -1; |
| 647 | } |
| 648 | // create missing next objective |
| 649 | createGroupFromPort(portNum, treatment, meta); |
| 650 | nextId = portNextObjStore.get(new PortNextObjectiveStoreKey(deviceId, portNum, |
| 651 | treatment, meta)); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 652 | if (nextId == null) { |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 653 | log.warn("getPortNextObjectiveId: unable to create next obj" |
| 654 | + "for dev:{} port:{}", deviceId, portNum); |
| 655 | return -1; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 656 | } |
| 657 | return nextId; |
| 658 | } |
| 659 | |
| 660 | /** |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 661 | * Checks if the next objective ID (group) for the neighbor set exists or not. |
| 662 | * |
| 663 | * @param ns neighbor set to check |
| 664 | * @return true if it exists, false otherwise |
| 665 | */ |
| 666 | public boolean hasNextObjectiveId(NeighborSet ns) { |
| 667 | Integer nextId = nsNextObjStore. |
| 668 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
| 669 | if (nextId == null) { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | return true; |
| 674 | } |
| 675 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 676 | private void populateNeighborMaps() { |
| 677 | Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 678 | for (Link link : outgoingLinks) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 679 | if (link.type() != Link.Type.DIRECT) { |
| 680 | continue; |
| 681 | } |
| 682 | addNeighborAtPort(link.dst().deviceId(), link.src().port()); |
| 683 | } |
| 684 | } |
| 685 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 686 | protected void addNeighborAtPort(DeviceId neighborId, |
| 687 | PortNumber portToNeighbor) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 688 | // Update DeviceToPort database |
| 689 | log.debug("Device {} addNeighborAtPort: neighbor {} at port {}", |
| 690 | deviceId, neighborId, portToNeighbor); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 691 | Set<PortNumber> ports = Collections |
| 692 | .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>()); |
| 693 | ports.add(portToNeighbor); |
| 694 | Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports); |
| 695 | if (portnums != null) { |
| 696 | portnums.add(portToNeighbor); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // Update portToDevice database |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 700 | DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId); |
| 701 | if (prev != null) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 702 | log.debug("Device: {} port: {} already has neighbor: {} ", |
| 703 | deviceId, portToNeighbor, prev, neighborId); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 707 | protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) { |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 708 | List<DeviceId> list = new ArrayList<>(neighbors); |
| 709 | Set<Set<DeviceId>> sets = new HashSet<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 710 | // get the number of elements in the neighbors |
| 711 | int elements = list.size(); |
| 712 | // the number of members of a power set is 2^n |
| 713 | // including the empty set |
| 714 | int powerElements = (1 << elements); |
| 715 | |
| 716 | // run a binary counter for the number of power elements |
| 717 | // NOTE: Exclude empty set |
| 718 | for (long i = 1; i < powerElements; i++) { |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 719 | Set<DeviceId> neighborSubSet = new HashSet<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 720 | for (int j = 0; j < elements; j++) { |
| 721 | if ((i >> j) % 2 == 1) { |
| 722 | neighborSubSet.add(list.get(j)); |
| 723 | } |
| 724 | } |
| 725 | sets.add(neighborSubSet); |
| 726 | } |
| 727 | return sets; |
| 728 | } |
| 729 | |
| 730 | private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 731 | int segmentId; |
| 732 | try { |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 733 | // IPv6 sid is not inserted. this part of the code is not used for now. |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 734 | segmentId = deviceConfig.getIPv4SegmentId(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 735 | } catch (DeviceConfigNotFoundException e) { |
| 736 | log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId."); |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | return segmentId == sId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 743 | protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 744 | |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 745 | List<Integer> nsSegmentIds = new ArrayList<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 746 | |
| 747 | // Always pair up with no edge label |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 748 | // If (neighbors.size() == 1) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 749 | nsSegmentIds.add(-1); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 750 | // } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 751 | |
| 752 | // Filter out SegmentIds matching with the |
| 753 | // nodes in the combo |
| 754 | for (Integer sId : allSegmentIds) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 755 | if (sId.equals(this.ipv4NodeSegmentId)) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 756 | continue; |
| 757 | } |
| 758 | boolean filterOut = false; |
| 759 | // Check if the edge label being set is of |
| 760 | // any node in the Neighbor set |
| 761 | for (DeviceId deviceId : neighbors) { |
| 762 | if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) { |
| 763 | filterOut = true; |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | if (!filterOut) { |
| 768 | nsSegmentIds.add(sId); |
| 769 | } |
| 770 | } |
| 771 | return nsSegmentIds; |
| 772 | } |
| 773 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 774 | /** |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 775 | * Creates hash groups from a set of NeighborSet given. |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 776 | * |
| 777 | * @param nsSet a set of NeighborSet |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 778 | * @param meta metadata passed into the creation of a Next Objective |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 779 | * @param isBos if BoS is set |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 780 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 781 | public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet, |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 782 | TrafficSelector meta, |
| 783 | boolean isBos) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 784 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 785 | int nextId = flowObjectiveService.allocateNextId(); |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 786 | NextObjective.Type type = NextObjective.Type.HASHED; |
| 787 | Set<DeviceId> neighbors = ns.getDeviceIds(); |
| 788 | // If Bos == False and MPLS-ECMP == false, we have |
| 789 | // to use simple group and we will pick a single neighbor. |
| 790 | if (!isBos && !srManager.getMplsEcmp()) { |
| 791 | type = NextObjective.Type.SIMPLE; |
| 792 | neighbors = Collections.singleton(ns.getFirstNeighbor()); |
| 793 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 794 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 795 | .builder() |
| 796 | .withId(nextId) |
| 797 | .withType(type) |
| 798 | .fromApp(appId); |
| 799 | // For each neighbor, we have to update the sent actions |
| 800 | for (DeviceId neighborId : neighbors) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 801 | if (devicePortMap.get(neighborId) == null) { |
| 802 | log.warn("Neighbor {} is not in the port map yet for dev:{}", |
| 803 | neighborId, deviceId); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 804 | return; |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 805 | } else if (devicePortMap.get(neighborId).isEmpty()) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 806 | log.warn("There are no ports for " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 807 | + "the Device {} in the port map yet", neighborId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 808 | return; |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 811 | MacAddress neighborMac; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 812 | try { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 813 | neighborMac = deviceConfig.getDeviceMac(neighborId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 814 | } catch (DeviceConfigNotFoundException e) { |
| 815 | log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets."); |
| 816 | return; |
| 817 | } |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 818 | // For each port, we have to create a new treatment |
| 819 | Set<PortNumber> neighborPorts = devicePortMap.get(neighborId); |
| 820 | // In this case we are using a SIMPLE group. We randomly pick a port |
| 821 | if (!isBos && !srManager.getMplsEcmp()) { |
| 822 | int size = devicePortMap.get(neighborId).size(); |
| 823 | int index = RandomUtils.nextInt(0, size); |
| 824 | neighborPorts = Collections.singleton( |
| 825 | Iterables.get(devicePortMap.get(neighborId), index) |
| 826 | ); |
| 827 | } |
| 828 | for (PortNumber sp : neighborPorts) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 829 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 830 | .builder(); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 831 | tBuilder.setEthDst(neighborMac) |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 832 | .setEthSrc(nodeMacAddr); |
| 833 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 834 | tBuilder.pushMpls() |
| 835 | .copyTtlOut() |
| 836 | .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel())); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 837 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 838 | tBuilder.setOutput(sp); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 839 | nextObjBuilder.addTreatment(tBuilder.build()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 842 | if (meta != null) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 843 | nextObjBuilder.withMeta(meta); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 844 | } |
Charles Chan | a4ee4f9 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 845 | |
| 846 | ObjectiveContext context = new DefaultObjectiveContext( |
Pier Ventre | 229fd0b | 2016-10-31 16:49:19 -0700 | [diff] [blame] | 847 | (objective) -> |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 848 | log.debug("createGroupsFromNeighborsets installed " |
| 849 | + "NextObj {} on {}", nextId, deviceId), |
Charles Chan | a4ee4f9 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 850 | (objective, error) -> |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 851 | log.warn("createGroupsFromNeighborsets failed to install" |
| 852 | + " NextObj {} on {}: {}", nextId, deviceId, error) |
| 853 | ); |
Charles Chan | a4ee4f9 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 854 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 855 | log.debug("**createGroupsFromNeighborsets: Submitted " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 856 | + "next objective {} in device {}", |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 857 | nextId, deviceId); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 858 | flowObjectiveService.next(deviceId, nextObj); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 859 | nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns), |
| 860 | nextId); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 864 | /** |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 865 | * Creates broadcast groups for all ports in the same subnet for |
| 866 | * all configured subnets. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 867 | */ |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 868 | public void createGroupsFromVlanConfig() { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 869 | srManager.getVlanPortMap(deviceId).asMap().forEach((vlanId, ports) -> { |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 870 | createBcastGroupFromVlan(vlanId, ports); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 871 | }); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 872 | } |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 873 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 874 | /** |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 875 | * 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] | 876 | * |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 877 | * @param vlanId vlan id |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 878 | * @param ports list of ports in the subnet |
| 879 | */ |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 880 | public void createBcastGroupFromVlan(VlanId vlanId, Collection<PortNumber> ports) { |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 881 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 882 | |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 883 | if (vlanNextObjStore.containsKey(key)) { |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 884 | log.debug("Broadcast group for device {} and subnet {} exists", |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 885 | deviceId, vlanId); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 886 | return; |
| 887 | } |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 888 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 889 | TrafficSelector metadata = |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 890 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 891 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 892 | int nextId = flowObjectiveService.allocateNextId(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 893 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 894 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 895 | .builder().withId(nextId) |
| 896 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 897 | .withMeta(metadata); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 898 | |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 899 | ports.forEach(port -> { |
| 900 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 901 | if (toPopVlan(port, vlanId)) { |
| 902 | tBuilder.popVlan(); |
| 903 | } |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 904 | tBuilder.setOutput(port); |
| 905 | nextObjBuilder.addTreatment(tBuilder.build()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 906 | }); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 907 | |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 908 | ObjectiveContext context = new DefaultObjectiveContext( |
| 909 | (objective) -> |
| 910 | log.debug("createBroadcastGroupFromVlan installed " |
| 911 | + "NextObj {} on {}", nextId, deviceId), |
| 912 | (objective, error) -> |
| 913 | log.warn("createBroadcastGroupFromVlan failed to install" |
| 914 | + " NextObj {} on {}: {}", nextId, deviceId, error) |
| 915 | ); |
| 916 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 917 | flowObjectiveService.next(deviceId, nextObj); |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 918 | log.debug("createBcastGroupFromVlan: Submited next objective {} in device {}", |
Saurav Das | f0f592d | 2016-11-18 15:21:57 -0800 | [diff] [blame] | 919 | nextId, deviceId); |
| 920 | |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 921 | vlanNextObjStore.put(key, nextId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 924 | /** |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 925 | * Determine if we should pop given vlan before sending packets to the given port. |
| 926 | * |
| 927 | * @param portNumber port number |
| 928 | * @param vlanId vlan id |
| 929 | * @return true if the vlan id is not contained in any vlanTagged config |
| 930 | */ |
| 931 | private boolean toPopVlan(PortNumber portNumber, VlanId vlanId) { |
| 932 | return srManager.interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, portNumber)) |
| 933 | .stream().noneMatch(intf -> intf.vlanTagged().contains(vlanId)); |
| 934 | } |
| 935 | |
| 936 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 937 | * Create simple next objective for a single port. The treatments can include |
| 938 | * all outgoing actions that need to happen on the packet. |
| 939 | * |
| 940 | * @param portNum the outgoing port on the device |
| 941 | * @param treatment the actions to apply on the packets (should include outport) |
| 942 | * @param meta optional data to pass to the driver |
| 943 | */ |
| 944 | public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment, |
| 945 | TrafficSelector meta) { |
| 946 | int nextId = flowObjectiveService.allocateNextId(); |
| 947 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 948 | deviceId, portNum, treatment, meta); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 949 | |
| 950 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 951 | .builder().withId(nextId) |
| 952 | .withType(NextObjective.Type.SIMPLE) |
| 953 | .addTreatment(treatment) |
| 954 | .fromApp(appId) |
| 955 | .withMeta(meta); |
| 956 | |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 957 | ObjectiveContext context = new DefaultObjectiveContext( |
| 958 | (objective) -> |
| 959 | log.debug("createGroupFromPort installed " |
| 960 | + "NextObj {} on {}", nextId, deviceId), |
| 961 | (objective, error) -> |
| 962 | log.warn("createGroupFromPort failed to install" |
| 963 | + " NextObj {} on {}: {}", nextId, deviceId, error) |
| 964 | ); |
| 965 | NextObjective nextObj = nextObjBuilder.add(context); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 966 | flowObjectiveService.next(deviceId, nextObj); |
| 967 | log.debug("createGroupFromPort: Submited next objective {} in device {} " |
| 968 | + "for port {}", nextId, deviceId, portNum); |
| 969 | |
| 970 | portNextObjStore.put(key, nextId); |
| 971 | } |
| 972 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 973 | /** |
| 974 | * Removes groups for the next objective ID given. |
| 975 | * |
| 976 | * @param objectiveId next objective ID to remove |
| 977 | * @return true if succeeds, false otherwise |
| 978 | */ |
| 979 | public boolean removeGroup(int objectiveId) { |
| 980 | |
| 981 | if (nsNextObjStore.containsValue(objectiveId)) { |
| 982 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 983 | .builder().withId(objectiveId) |
| 984 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Charles Chan | a4ee4f9 | 2016-04-23 14:48:16 -0700 | [diff] [blame] | 985 | ObjectiveContext context = new DefaultObjectiveContext( |
| 986 | (objective) -> log.debug("RemoveGroup removes NextObj {} on {}", |
| 987 | objectiveId, deviceId), |
| 988 | (objective, error) -> |
| 989 | log.warn("RemoveGroup failed to remove NextObj {} on {}: {}", |
| 990 | objectiveId, deviceId, error)); |
| 991 | NextObjective nextObjective = nextObjBuilder.remove(context); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 992 | log.info("**removeGroup: Submited " |
| 993 | + "next objective {} in device {}", |
| 994 | objectiveId, deviceId); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 995 | flowObjectiveService.next(deviceId, nextObjective); |
| 996 | |
| 997 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) { |
| 998 | if (entry.getValue().equals(objectiveId)) { |
| 999 | nsNextObjStore.remove(entry.getKey()); |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 1003 | return true; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | return false; |
| 1007 | } |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 1008 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 1009 | /** |
| 1010 | * Removes all groups from all next objective stores. |
| 1011 | */ |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1012 | public void removeAllGroups() { |
| 1013 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: |
| 1014 | nsNextObjStore.entrySet()) { |
| 1015 | removeGroup(entry.getValue()); |
| 1016 | } |
| 1017 | for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry: |
| 1018 | portNextObjStore.entrySet()) { |
| 1019 | removeGroup(entry.getValue()); |
| 1020 | } |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1021 | for (Map.Entry<VlanNextObjectiveStoreKey, Integer> entry: |
| 1022 | vlanNextObjStore.entrySet()) { |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1023 | removeGroup(entry.getValue()); |
| 1024 | } |
| 1025 | // should probably clean local stores port-neighbor |
| 1026 | } |
Saurav Das | 8a3022d | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 1027 | |
| 1028 | /** |
| 1029 | * RetryHashBkts is a one-time retry at populating all the buckets of a |
| 1030 | * hash group based on the given link. Should only be called by the |
| 1031 | * master instance of the src-device of the link. |
| 1032 | */ |
| 1033 | protected final class RetryHashBkts implements Runnable { |
| 1034 | Link link; |
| 1035 | MacAddress dstMac; |
| 1036 | |
| 1037 | private RetryHashBkts(Link link, MacAddress dstMac) { |
| 1038 | this.link = link; |
| 1039 | this.dstMac = dstMac; |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
| 1043 | public void run() { |
| 1044 | log.info("RETRY Hash buckets for linkup: {}", link); |
| 1045 | Set<NeighborSet> nsSet = nsNextObjStore.keySet() |
| 1046 | .stream() |
| 1047 | .filter(nsStoreEntry -> nsStoreEntry.deviceId().equals(deviceId)) |
| 1048 | .map(nsStoreEntry -> nsStoreEntry.neighborSet()) |
| 1049 | .filter(ns -> ns.getDeviceIds() |
| 1050 | .contains(link.dst().deviceId())) |
| 1051 | .collect(Collectors.toSet()); |
| 1052 | log.debug("retry-link: nsNextObjStore contents for device {}: {}", |
| 1053 | deviceId, nsSet); |
| 1054 | for (NeighborSet ns : nsSet) { |
| 1055 | Integer nextId = nsNextObjStore. |
| 1056 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
| 1057 | if (nextId != null) { |
| 1058 | addToHashedNextObjective(link.src().port(), dstMac, ns, |
| 1059 | nextId, true); |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 1066 | } |