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