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