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