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