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