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