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 | |
| 291 | MacAddress dstMac; |
| 292 | try { |
| 293 | dstMac = deviceConfig.getDeviceMac(portDeviceMap.get(port)); |
| 294 | } catch (DeviceConfigNotFoundException e) { |
| 295 | log.warn(e.getMessage() + " Aborting portDown."); |
| 296 | return; |
| 297 | } |
| 298 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 299 | log.debug("Device {} portDown {} to neighbor {}", deviceId, port, |
| 300 | portDeviceMap.get(port)); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 301 | /*Set<NeighborSet> nsSet = computeImpactedNeighborsetForPortEvent(portDeviceMap |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 302 | .get(port), |
| 303 | devicePortMap |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 304 | .keySet());*/ |
| 305 | Set<NeighborSet> nsSet = nsNextObjStore.keySet() |
| 306 | .stream() |
| 307 | .filter((nsStoreEntry) -> (nsStoreEntry.deviceId().equals(deviceId))) |
| 308 | .map((nsStoreEntry) -> (nsStoreEntry.neighborSet())) |
| 309 | .filter((ns) -> (ns.getDeviceIds() |
| 310 | .contains(portDeviceMap.get(port)))) |
| 311 | .collect(Collectors.toSet()); |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 312 | log.debug("portDown: nsNextObjStore contents for device {}:{}", |
| 313 | deviceId, nsSet); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 314 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 315 | Integer nextId = nsNextObjStore. |
| 316 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 317 | if (nextId != null && isMaster) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 318 | log.info("**portDown in device {}: Removing Bucket " |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 319 | + "with Port {} to next object id {}", |
| 320 | deviceId, |
| 321 | port, |
| 322 | nextId); |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 323 | // Create the bucket to be removed |
| 324 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 325 | .builder(); |
| 326 | tBuilder.setOutput(port) |
| 327 | .setEthDst(dstMac) |
| 328 | .setEthSrc(nodeMacAddr); |
| 329 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
| 330 | tBuilder.pushMpls() |
| 331 | .copyTtlOut() |
| 332 | .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel())); |
| 333 | } |
| 334 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 335 | .builder() |
| 336 | .withType(NextObjective.Type.HASHED) //same as original |
| 337 | .withId(nextId) |
| 338 | .fromApp(appId) |
| 339 | .addTreatment(tBuilder.build()); |
| 340 | NextObjective nextObjective = nextObjBuilder. |
| 341 | removeFromExisting(new SRNextObjectiveContext(deviceId)); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 342 | |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 343 | flowObjectiveService.next(deviceId, nextObjective); |
| 344 | |
| 345 | // the removal of a bucket may actually change the neighborset |
| 346 | // update the global store |
| 347 | /* |
| 348 | Set<DeviceId> neighbors = new HashSet<DeviceId>(ns.getDeviceIds()); |
| 349 | boolean removed = neighbors.remove(portDeviceMap.get(port)); |
| 350 | if (removed) { |
| 351 | NeighborSet nsnew = new NeighborSet(neighbors, ns.getEdgeLabel()); |
| 352 | nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, nsnew), |
| 353 | nextId); |
| 354 | nsNextObjStore.remove(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
| 355 | }*/ |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | devicePortMap.get(portDeviceMap.get(port)).remove(port); |
| 361 | portDeviceMap.remove(port); |
| 362 | } |
| 363 | |
| 364 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 365 | * Returns the next objective of type hashed associated with the neighborset. |
| 366 | * If there is no next objective for this neighborset, this method |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 367 | * would create a next objective and return. Optionally metadata can be |
| 368 | * passed in for the creation of the next objective. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 369 | * |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 370 | * @param ns neighborset |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 371 | * @param meta metadata passed into the creation of a Next Objective |
| 372 | * @return int if found or -1 if there are errors in the creation of the |
| 373 | * neighbor set. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 374 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 375 | public int getNextObjectiveId(NeighborSet ns, TrafficSelector meta) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 376 | Integer nextId = nsNextObjStore. |
| 377 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 378 | if (nextId == null) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 379 | log.trace("getNextObjectiveId in device{}: Next objective id " |
| 380 | + "not found for {} and creating", deviceId, ns); |
| 381 | log.trace("getNextObjectiveId: nsNextObjStore contents for device {}: {}", |
| 382 | deviceId, |
| 383 | nsNextObjStore.entrySet() |
| 384 | .stream() |
| 385 | .filter((nsStoreEntry) -> |
| 386 | (nsStoreEntry.getKey().deviceId().equals(deviceId))) |
| 387 | .collect(Collectors.toList())); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 388 | createGroupsFromNeighborsets(Collections.singleton(ns), meta); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 389 | nextId = nsNextObjStore. |
| 390 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 391 | if (nextId == null) { |
| 392 | log.warn("getNextObjectiveId: unable to create next objective"); |
| 393 | return -1; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 394 | } else { |
| 395 | log.debug("getNextObjectiveId in device{}: Next objective id {} " |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 396 | + "created for {}", deviceId, nextId, ns); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 397 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 398 | } else { |
| 399 | log.trace("getNextObjectiveId in device{}: Next objective id {} " |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 400 | + "found for {}", deviceId, nextId, ns); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 401 | } |
Sho SHIMIZU | d179335 | 2015-09-11 14:24:50 -0700 | [diff] [blame] | 402 | return nextId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 403 | } |
| 404 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 405 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 406 | * Returns the next objective of type broadcast associated with the subnet, |
| 407 | * or -1 if no such objective exists. Note that this method does NOT create |
| 408 | * the next objective as a side-effect. It is expected that is objective is |
| 409 | * created at startup from network configuration. |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 410 | * |
| 411 | * @param prefix subnet information |
| 412 | * @return int if found or -1 |
| 413 | */ |
| 414 | public int getSubnetNextObjectiveId(IpPrefix prefix) { |
| 415 | Integer nextId = subnetNextObjStore. |
| 416 | get(new SubnetNextObjectiveStoreKey(deviceId, prefix)); |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 417 | |
| 418 | return (nextId != null) ? nextId : -1; |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 422 | * Returns the next objective of type simple associated with the port on the |
| 423 | * device, given the treatment. Different treatments to the same port result |
| 424 | * in different next objectives. If no such objective exists, this method |
| 425 | * creates one and returns the id. Optionally metadata can be passed in for |
| 426 | * the creation of the objective. |
| 427 | * |
| 428 | * @param portNum the port number for the simple next objective |
| 429 | * @param treatment the actions to apply on the packets (should include outport) |
| 430 | * @param meta optional metadata passed into the creation of the next objective |
| 431 | * @return int if found or created, -1 if there are errors during the |
| 432 | * creation of the next objective. |
| 433 | */ |
| 434 | public int getPortNextObjectiveId(PortNumber portNum, TrafficTreatment treatment, |
| 435 | TrafficSelector meta) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 436 | Integer nextId = portNextObjStore |
| 437 | .get(new PortNextObjectiveStoreKey(deviceId, portNum, treatment)); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 438 | if (nextId == null) { |
| 439 | log.trace("getPortNextObjectiveId in device{}: Next objective id " |
| 440 | + "not found for {} and {} creating", deviceId, portNum); |
| 441 | createGroupFromPort(portNum, treatment, meta); |
| 442 | nextId = portNextObjStore.get( |
| 443 | new PortNextObjectiveStoreKey(deviceId, portNum, treatment)); |
| 444 | if (nextId == null) { |
| 445 | log.warn("getPortNextObjectiveId: unable to create next obj" |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 446 | + "for dev:{} port:{}", deviceId, portNum); |
| 447 | return -1; |
| 448 | } |
| 449 | } |
| 450 | return nextId; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Returns the next objective ID of type broadcast associated with the VLAN |
| 455 | * cross-connection. |
| 456 | * |
| 457 | * @param vlanId VLAN ID for the cross-connection |
| 458 | * @return int if found or created, -1 if there are errors during the |
| 459 | * creation of the next objective |
| 460 | */ |
| 461 | public int getXConnectNextObjectiveId(VlanId vlanId) { |
| 462 | Integer nextId = xConnectNextObjStore |
| 463 | .get(new XConnectNextObjectiveStoreKey(deviceId, vlanId)); |
| 464 | if (nextId == null) { |
| 465 | log.trace("getXConnectNextObjectiveId: Next objective id " |
| 466 | + "not found for device {} and vlan {}. Creating", deviceId, vlanId); |
| 467 | createGroupsForXConnect(deviceId); |
| 468 | nextId = xConnectNextObjStore.get( |
| 469 | new XConnectNextObjectiveStoreKey(deviceId, vlanId)); |
| 470 | if (nextId == null) { |
| 471 | log.warn("getXConnectNextObjectiveId: Next objective id " |
| 472 | + "not found for device {} and vlan {}.", deviceId, vlanId); |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | } |
| 476 | return nextId; |
| 477 | } |
| 478 | |
| 479 | /** |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 480 | * Checks if the next objective ID (group) for the neighbor set exists or not. |
| 481 | * |
| 482 | * @param ns neighbor set to check |
| 483 | * @return true if it exists, false otherwise |
| 484 | */ |
| 485 | public boolean hasNextObjectiveId(NeighborSet ns) { |
| 486 | Integer nextId = nsNextObjStore. |
| 487 | get(new NeighborSetNextObjectiveStoreKey(deviceId, ns)); |
| 488 | if (nextId == null) { |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | return true; |
| 493 | } |
| 494 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 495 | // Empty implementation |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 496 | protected void newNeighbor(Link newLink) { |
| 497 | } |
| 498 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 499 | // Empty implementation |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 500 | protected void newPortToExistingNeighbor(Link newLink) { |
| 501 | } |
| 502 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 503 | // Empty implementation |
| 504 | protected Set<NeighborSet> |
| 505 | computeImpactedNeighborsetForPortEvent(DeviceId impactedNeighbor, |
| 506 | Set<DeviceId> updatedNeighbors) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 507 | return null; |
| 508 | } |
| 509 | |
| 510 | private void populateNeighborMaps() { |
| 511 | Set<Link> outgoingLinks = linkService.getDeviceEgressLinks(deviceId); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 512 | for (Link link : outgoingLinks) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 513 | if (link.type() != Link.Type.DIRECT) { |
| 514 | continue; |
| 515 | } |
| 516 | addNeighborAtPort(link.dst().deviceId(), link.src().port()); |
| 517 | } |
| 518 | } |
| 519 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 520 | protected void addNeighborAtPort(DeviceId neighborId, |
| 521 | PortNumber portToNeighbor) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 522 | // Update DeviceToPort database |
| 523 | log.debug("Device {} addNeighborAtPort: neighbor {} at port {}", |
| 524 | deviceId, neighborId, portToNeighbor); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 525 | Set<PortNumber> ports = Collections |
| 526 | .newSetFromMap(new ConcurrentHashMap<PortNumber, Boolean>()); |
| 527 | ports.add(portToNeighbor); |
| 528 | Set<PortNumber> portnums = devicePortMap.putIfAbsent(neighborId, ports); |
| 529 | if (portnums != null) { |
| 530 | portnums.add(portToNeighbor); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Update portToDevice database |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 534 | DeviceId prev = portDeviceMap.putIfAbsent(portToNeighbor, neighborId); |
| 535 | if (prev != null) { |
| 536 | log.warn("Device: {} port: {} has neighbor: {}. NOT updating " |
| 537 | + "to neighbor: {}", deviceId, portToNeighbor, prev, neighborId); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 541 | protected Set<Set<DeviceId>> getPowerSetOfNeighbors(Set<DeviceId> neighbors) { |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 542 | List<DeviceId> list = new ArrayList<>(neighbors); |
| 543 | Set<Set<DeviceId>> sets = new HashSet<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 544 | // get the number of elements in the neighbors |
| 545 | int elements = list.size(); |
| 546 | // the number of members of a power set is 2^n |
| 547 | // including the empty set |
| 548 | int powerElements = (1 << elements); |
| 549 | |
| 550 | // run a binary counter for the number of power elements |
| 551 | // NOTE: Exclude empty set |
| 552 | for (long i = 1; i < powerElements; i++) { |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 553 | Set<DeviceId> neighborSubSet = new HashSet<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 554 | for (int j = 0; j < elements; j++) { |
| 555 | if ((i >> j) % 2 == 1) { |
| 556 | neighborSubSet.add(list.get(j)); |
| 557 | } |
| 558 | } |
| 559 | sets.add(neighborSubSet); |
| 560 | } |
| 561 | return sets; |
| 562 | } |
| 563 | |
| 564 | private boolean isSegmentIdSameAsNodeSegmentId(DeviceId deviceId, int sId) { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 565 | int segmentId; |
| 566 | try { |
| 567 | segmentId = deviceConfig.getSegmentId(deviceId); |
| 568 | } catch (DeviceConfigNotFoundException e) { |
| 569 | log.warn(e.getMessage() + " Aborting isSegmentIdSameAsNodeSegmentId."); |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | return segmentId == sId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 576 | protected List<Integer> getSegmentIdsTobePairedWithNeighborSet(Set<DeviceId> neighbors) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 577 | |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 578 | List<Integer> nsSegmentIds = new ArrayList<>(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 579 | |
| 580 | // Always pair up with no edge label |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 581 | // If (neighbors.size() == 1) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 582 | nsSegmentIds.add(-1); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 583 | // } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 584 | |
| 585 | // Filter out SegmentIds matching with the |
| 586 | // nodes in the combo |
| 587 | for (Integer sId : allSegmentIds) { |
| 588 | if (sId.equals(nodeSegmentId)) { |
| 589 | continue; |
| 590 | } |
| 591 | boolean filterOut = false; |
| 592 | // Check if the edge label being set is of |
| 593 | // any node in the Neighbor set |
| 594 | for (DeviceId deviceId : neighbors) { |
| 595 | if (isSegmentIdSameAsNodeSegmentId(deviceId, sId)) { |
| 596 | filterOut = true; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | if (!filterOut) { |
| 601 | nsSegmentIds.add(sId); |
| 602 | } |
| 603 | } |
| 604 | return nsSegmentIds; |
| 605 | } |
| 606 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 607 | /** |
| 608 | * Creates Groups from a set of NeighborSet given. |
| 609 | * |
| 610 | * @param nsSet a set of NeighborSet |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 611 | * @param meta metadata passed into the creation of a Next Objective |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 612 | */ |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 613 | public void createGroupsFromNeighborsets(Set<NeighborSet> nsSet, |
| 614 | TrafficSelector meta) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 615 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 616 | int nextId = flowObjectiveService.allocateNextId(); |
| 617 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 618 | .builder().withId(nextId) |
| 619 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 620 | for (DeviceId neighborId : ns.getDeviceIds()) { |
| 621 | if (devicePortMap.get(neighborId) == null) { |
| 622 | log.warn("Neighbor {} is not in the port map yet for dev:{}", |
| 623 | neighborId, deviceId); |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 624 | return; |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 625 | } else if (devicePortMap.get(neighborId).size() == 0) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 626 | log.warn("There are no ports for " |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 627 | + "the Device {} in the port map yet", neighborId); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 628 | return; |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 631 | MacAddress neighborMac; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 632 | try { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 633 | neighborMac = deviceConfig.getDeviceMac(neighborId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 634 | } catch (DeviceConfigNotFoundException e) { |
| 635 | log.warn(e.getMessage() + " Aborting createGroupsFromNeighborsets."); |
| 636 | return; |
| 637 | } |
| 638 | |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 639 | for (PortNumber sp : devicePortMap.get(neighborId)) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 640 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment |
| 641 | .builder(); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 642 | tBuilder.setEthDst(neighborMac) |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 643 | .setEthSrc(nodeMacAddr); |
| 644 | if (ns.getEdgeLabel() != NeighborSet.NO_EDGE_LABEL) { |
Charles Chan | f458611 | 2015-11-09 16:37:23 -0800 | [diff] [blame] | 645 | tBuilder.pushMpls() |
| 646 | .copyTtlOut() |
| 647 | .setMpls(MplsLabel.mplsLabel(ns.getEdgeLabel())); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 648 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 649 | tBuilder.setOutput(sp); |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 650 | nextObjBuilder.addTreatment(tBuilder.build()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 653 | if (meta != null) { |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 654 | nextObjBuilder.withMeta(meta); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 655 | } |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 656 | NextObjective nextObj = nextObjBuilder. |
| 657 | add(new SRNextObjectiveContext(deviceId)); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 658 | log.info("**createGroupsFromNeighborsets: Submited " |
| 659 | + "next objective {} in device {}", |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 660 | nextId, deviceId); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 661 | flowObjectiveService.next(deviceId, nextObj); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 662 | nsNextObjStore.put(new NeighborSetNextObjectiveStoreKey(deviceId, ns), |
| 663 | nextId); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 667 | /** |
| 668 | * Creates broadcast groups for all ports in the same configured subnet. |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 669 | */ |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 670 | public void createGroupsFromSubnetConfig() { |
| 671 | Map<Ip4Prefix, List<PortNumber>> subnetPortMap = |
| 672 | this.deviceConfig.getSubnetPortsMap(this.deviceId); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 673 | // Construct a broadcast group for each subnet |
| 674 | subnetPortMap.forEach((subnet, ports) -> { |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 675 | SubnetNextObjectiveStoreKey key = |
| 676 | new SubnetNextObjectiveStoreKey(deviceId, subnet); |
| 677 | |
| 678 | if (subnetNextObjStore.containsKey(key)) { |
| 679 | log.debug("Broadcast group for device {} and subnet {} exists", |
| 680 | deviceId, subnet); |
| 681 | return; |
| 682 | } |
| 683 | |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 684 | VlanId assignedVlanId = |
| 685 | srManager.getSubnetAssignedVlanId(this.deviceId, subnet); |
| 686 | TrafficSelector metadata = |
| 687 | DefaultTrafficSelector.builder().matchVlanId(assignedVlanId).build(); |
| 688 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 689 | int nextId = flowObjectiveService.allocateNextId(); |
| 690 | |
| 691 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 692 | .builder().withId(nextId) |
Charles Chan | de6655c | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 693 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 694 | .withMeta(metadata); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 695 | |
| 696 | ports.forEach(port -> { |
| 697 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
Saurav Das | 74b4922 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 698 | tBuilder.popVlan(); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 699 | tBuilder.setOutput(port); |
| 700 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 701 | }); |
| 702 | |
| 703 | NextObjective nextObj = nextObjBuilder.add(); |
| 704 | flowObjectiveService.next(deviceId, nextObj); |
| 705 | log.debug("createGroupFromSubnetConfig: Submited " |
| 706 | + "next objective {} in device {}", |
| 707 | nextId, deviceId); |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 708 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 709 | subnetNextObjStore.put(key, nextId); |
| 710 | }); |
| 711 | } |
| 712 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 713 | /** |
| 714 | * Creates broadcast groups for VLAN cross-connect ports. |
| 715 | * |
| 716 | * @param deviceId the DPID of the switch |
| 717 | */ |
| 718 | public void createGroupsForXConnect(DeviceId deviceId) { |
| 719 | Map<VlanId, List<ConnectPoint>> xConnectsForDevice = deviceConfig.getXConnects(); |
| 720 | |
| 721 | xConnectsForDevice.forEach((vlanId, connectPoints) -> { |
| 722 | // Only proceed the xConnect for given device |
| 723 | for (ConnectPoint connectPoint : connectPoints) { |
| 724 | if (!connectPoint.deviceId().equals(deviceId)) { |
| 725 | return; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // Check if the next obj is already in the store |
| 730 | XConnectNextObjectiveStoreKey key = |
| 731 | new XConnectNextObjectiveStoreKey(deviceId, vlanId); |
| 732 | if (xConnectNextObjStore.containsKey(key)) { |
| 733 | log.debug("Cross-connect Broadcast group for device {} and vlanId {} exists", |
| 734 | deviceId, vlanId); |
| 735 | return; |
| 736 | } |
| 737 | |
| 738 | TrafficSelector metadata = |
| 739 | DefaultTrafficSelector.builder().matchVlanId(vlanId).build(); |
| 740 | int nextId = flowObjectiveService.allocateNextId(); |
| 741 | |
| 742 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 743 | .builder().withId(nextId) |
| 744 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 745 | .withMeta(metadata); |
| 746 | |
| 747 | connectPoints.forEach(connectPoint -> { |
| 748 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 749 | tBuilder.setOutput(connectPoint.port()); |
| 750 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 751 | }); |
| 752 | |
| 753 | NextObjective nextObj = nextObjBuilder.add(); |
| 754 | flowObjectiveService.next(deviceId, nextObj); |
| 755 | log.debug("createGroupsForXConnect: Submited next objective {} in device {}", |
| 756 | nextId, deviceId); |
| 757 | xConnectNextObjStore.put(key, nextId); |
| 758 | }); |
| 759 | } |
| 760 | |
Saurav Das | 2d94d31 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 761 | |
| 762 | /** |
| 763 | * Create simple next objective for a single port. The treatments can include |
| 764 | * all outgoing actions that need to happen on the packet. |
| 765 | * |
| 766 | * @param portNum the outgoing port on the device |
| 767 | * @param treatment the actions to apply on the packets (should include outport) |
| 768 | * @param meta optional data to pass to the driver |
| 769 | */ |
| 770 | public void createGroupFromPort(PortNumber portNum, TrafficTreatment treatment, |
| 771 | TrafficSelector meta) { |
| 772 | int nextId = flowObjectiveService.allocateNextId(); |
| 773 | PortNextObjectiveStoreKey key = new PortNextObjectiveStoreKey( |
| 774 | deviceId, portNum, treatment); |
| 775 | |
| 776 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 777 | .builder().withId(nextId) |
| 778 | .withType(NextObjective.Type.SIMPLE) |
| 779 | .addTreatment(treatment) |
| 780 | .fromApp(appId) |
| 781 | .withMeta(meta); |
| 782 | |
| 783 | NextObjective nextObj = nextObjBuilder.add(); |
| 784 | flowObjectiveService.next(deviceId, nextObj); |
| 785 | log.debug("createGroupFromPort: Submited next objective {} in device {} " |
| 786 | + "for port {}", nextId, deviceId, portNum); |
| 787 | |
| 788 | portNextObjStore.put(key, nextId); |
| 789 | } |
| 790 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 791 | /** |
| 792 | * Removes groups for the next objective ID given. |
| 793 | * |
| 794 | * @param objectiveId next objective ID to remove |
| 795 | * @return true if succeeds, false otherwise |
| 796 | */ |
| 797 | public boolean removeGroup(int objectiveId) { |
| 798 | |
| 799 | if (nsNextObjStore.containsValue(objectiveId)) { |
| 800 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 801 | .builder().withId(objectiveId) |
| 802 | .withType(NextObjective.Type.HASHED).fromApp(appId); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 803 | NextObjective nextObjective = nextObjBuilder. |
| 804 | remove(new SRNextObjectiveContext(deviceId)); |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 805 | log.info("**removeGroup: Submited " |
| 806 | + "next objective {} in device {}", |
| 807 | objectiveId, deviceId); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 808 | flowObjectiveService.next(deviceId, nextObjective); |
| 809 | |
| 810 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: nsNextObjStore.entrySet()) { |
| 811 | if (entry.getValue().equals(objectiveId)) { |
| 812 | nsNextObjStore.remove(entry.getKey()); |
| 813 | break; |
| 814 | } |
| 815 | } |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 816 | return true; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | return false; |
| 820 | } |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 821 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 822 | /** |
| 823 | * Removes all groups from all next objective stores. |
| 824 | */ |
Saurav Das | 62af880 | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 825 | public void removeAllGroups() { |
| 826 | for (Map.Entry<NeighborSetNextObjectiveStoreKey, Integer> entry: |
| 827 | nsNextObjStore.entrySet()) { |
| 828 | removeGroup(entry.getValue()); |
| 829 | } |
| 830 | for (Map.Entry<PortNextObjectiveStoreKey, Integer> entry: |
| 831 | portNextObjStore.entrySet()) { |
| 832 | removeGroup(entry.getValue()); |
| 833 | } |
| 834 | for (Map.Entry<SubnetNextObjectiveStoreKey, Integer> entry: |
| 835 | subnetNextObjStore.entrySet()) { |
| 836 | removeGroup(entry.getValue()); |
| 837 | } |
| 838 | // should probably clean local stores port-neighbor |
| 839 | } |
| 840 | |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 841 | protected static class SRNextObjectiveContext implements ObjectiveContext { |
| 842 | final DeviceId deviceId; |
| 843 | |
| 844 | SRNextObjectiveContext(DeviceId deviceId) { |
| 845 | this.deviceId = deviceId; |
| 846 | } |
| 847 | @Override |
| 848 | public void onSuccess(Objective objective) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 849 | log.info("Next objective {} operation successful in device {}", |
| 850 | objective.id(), deviceId); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | @Override |
| 854 | public void onError(Objective objective, ObjectiveError error) { |
| 855 | log.warn("Next objective {} operation failed with error: {} in device {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 856 | objective.id(), error, deviceId); |
Srikanth Vavilapalli | 8c83f1d | 2015-05-22 13:47:31 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 859 | } |