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