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