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