Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.xconnect.impl; |
| 17 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 21 | import com.google.common.collect.Lists; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 29 | import org.onlab.packet.Ethernet; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onlab.util.KryoNamespace; |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 33 | import org.onosproject.cluster.ClusterService; |
| 34 | import org.onosproject.cluster.LeadershipService; |
| 35 | import org.onosproject.cluster.NodeId; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 36 | import org.onosproject.codec.CodecService; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
| 39 | import org.onosproject.mastership.MastershipService; |
| 40 | import org.onosproject.net.ConnectPoint; |
| 41 | import org.onosproject.net.DeviceId; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 42 | import org.onosproject.net.Host; |
| 43 | import org.onosproject.net.HostLocation; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 44 | import org.onosproject.net.PortNumber; |
| 45 | import org.onosproject.net.config.NetworkConfigService; |
| 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
| 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 50 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 51 | import org.onosproject.net.flow.TrafficSelector; |
| 52 | import org.onosproject.net.flow.TrafficTreatment; |
| 53 | import org.onosproject.net.flow.criteria.Criteria; |
| 54 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 55 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 57 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 58 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 59 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 60 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 61 | import org.onosproject.net.flowobjective.NextObjective; |
| 62 | import org.onosproject.net.flowobjective.Objective; |
| 63 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 64 | import org.onosproject.net.flowobjective.ObjectiveError; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 65 | import org.onosproject.net.host.HostEvent; |
| 66 | import org.onosproject.net.host.HostListener; |
| 67 | import org.onosproject.net.host.HostService; |
| 68 | import org.onosproject.net.intf.InterfaceService; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 69 | import org.onosproject.segmentrouting.SegmentRoutingService; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 70 | import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 71 | import org.onosproject.segmentrouting.xconnect.api.XconnectCodec; |
| 72 | import org.onosproject.segmentrouting.xconnect.api.XconnectDesc; |
| 73 | import org.onosproject.segmentrouting.xconnect.api.XconnectKey; |
| 74 | import org.onosproject.segmentrouting.xconnect.api.XconnectService; |
| 75 | import org.onosproject.store.serializers.KryoNamespaces; |
| 76 | import org.onosproject.store.service.ConsistentMap; |
| 77 | import org.onosproject.store.service.MapEvent; |
| 78 | import org.onosproject.store.service.MapEventListener; |
| 79 | import org.onosproject.store.service.Serializer; |
| 80 | import org.onosproject.store.service.StorageService; |
| 81 | import org.onosproject.store.service.Versioned; |
| 82 | import org.slf4j.Logger; |
| 83 | import org.slf4j.LoggerFactory; |
| 84 | |
| 85 | import java.io.Serializable; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 86 | import java.util.Collections; |
| 87 | import java.util.List; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 88 | import java.util.Optional; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 89 | import java.util.Set; |
| 90 | import java.util.concurrent.CompletableFuture; |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 91 | import java.util.concurrent.ExecutorService; |
| 92 | import java.util.concurrent.Executors; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 93 | import java.util.function.BiConsumer; |
| 94 | import java.util.function.Consumer; |
| 95 | import java.util.stream.Collectors; |
| 96 | |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 97 | import static org.onlab.util.Tools.groupedThreads; |
| 98 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 99 | @Service |
| 100 | @Component(immediate = true) |
| 101 | public class XconnectManager implements XconnectService { |
| 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | private CoreService coreService; |
| 104 | |
| 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | private CodecService codecService; |
| 107 | |
| 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | private StorageService storageService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | public NetworkConfigService netCfgService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | public DeviceService deviceService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | public FlowObjectiveService flowObjectiveService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 121 | private LeadershipService leadershipService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | private ClusterService clusterService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 127 | public MastershipService mastershipService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) |
| 130 | public SegmentRoutingService srService; |
| 131 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | public InterfaceService interfaceService; |
| 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 136 | HostService hostService; |
| 137 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 138 | private static final String APP_NAME = "org.onosproject.xconnect"; |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 139 | private static final String ERROR_NOT_LEADER = "Not leader controller"; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 140 | |
| 141 | private static Logger log = LoggerFactory.getLogger(XconnectManager.class); |
| 142 | |
| 143 | private ApplicationId appId; |
| 144 | private ConsistentMap<XconnectKey, Set<PortNumber>> xconnectStore; |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 145 | private ConsistentMap<XconnectKey, Integer> xconnectNextObjStore; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 146 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 147 | private ConsistentMap<VlanNextObjectiveStoreKey, Integer> xconnectMulticastNextStore; |
| 148 | private ConsistentMap<VlanNextObjectiveStoreKey, List<PortNumber>> xconnectMulticastPortsStore; |
| 149 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 150 | private final MapEventListener<XconnectKey, Set<PortNumber>> xconnectListener = new XconnectMapListener(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 151 | private ExecutorService xConnectExecutor; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 152 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 153 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 154 | private ExecutorService deviceEventExecutor; |
| 155 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 156 | private final HostListener hostListener = new InternalHostListener(); |
| 157 | private ExecutorService hostEventExecutor; |
| 158 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 159 | @Activate |
| 160 | void activate() { |
| 161 | appId = coreService.registerApplication(APP_NAME); |
| 162 | codecService.registerCodec(XconnectDesc.class, new XconnectCodec()); |
| 163 | |
| 164 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 165 | .register(KryoNamespaces.API) |
Charles Chan | 397398c | 2018-07-23 12:53:16 -0700 | [diff] [blame] | 166 | .register(XconnectManager.class) |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 167 | .register(XconnectKey.class) |
| 168 | .register(VlanNextObjectiveStoreKey.class); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 169 | |
| 170 | xconnectStore = storageService.<XconnectKey, Set<PortNumber>>consistentMapBuilder() |
| 171 | .withName("onos-sr-xconnect") |
| 172 | .withRelaxedReadConsistency() |
| 173 | .withSerializer(Serializer.using(serializer.build())) |
| 174 | .build(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 175 | xConnectExecutor = Executors.newSingleThreadScheduledExecutor( |
| 176 | groupedThreads("sr-xconnect-event", "%d", log)); |
| 177 | xconnectStore.addListener(xconnectListener, xConnectExecutor); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 178 | |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 179 | xconnectNextObjStore = storageService.<XconnectKey, Integer>consistentMapBuilder() |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 180 | .withName("onos-sr-xconnect-next") |
| 181 | .withRelaxedReadConsistency() |
| 182 | .withSerializer(Serializer.using(serializer.build())) |
| 183 | .build(); |
| 184 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 185 | xconnectMulticastNextStore = storageService.<VlanNextObjectiveStoreKey, Integer>consistentMapBuilder() |
| 186 | .withName("onos-sr-xconnect-l2multicast-next") |
| 187 | .withSerializer(Serializer.using(serializer.build())) |
| 188 | .build(); |
| 189 | xconnectMulticastPortsStore = storageService.<VlanNextObjectiveStoreKey, List<PortNumber>>consistentMapBuilder() |
| 190 | .withName("onos-sr-xconnect-l2multicast-ports") |
| 191 | .withSerializer(Serializer.using(serializer.build())) |
| 192 | .build(); |
| 193 | |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 194 | deviceEventExecutor = Executors.newSingleThreadScheduledExecutor( |
| 195 | groupedThreads("sr-xconnect-device-event", "%d", log)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 196 | deviceService.addListener(deviceListener); |
| 197 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 198 | hostEventExecutor = Executors.newSingleThreadExecutor( |
| 199 | groupedThreads("sr-xconnect-host-event", "%d", log)); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 200 | hostService.addListener(hostListener); |
| 201 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 202 | log.info("Started"); |
| 203 | } |
| 204 | |
| 205 | @Deactivate |
| 206 | void deactivate() { |
| 207 | xconnectStore.removeListener(xconnectListener); |
| 208 | deviceService.removeListener(deviceListener); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 209 | hostService.removeListener(hostListener); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 210 | codecService.unregisterCodec(XconnectDesc.class); |
| 211 | |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 212 | deviceEventExecutor.shutdown(); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 213 | hostEventExecutor.shutdown(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 214 | xConnectExecutor.shutdown(); |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 215 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 216 | log.info("Stopped"); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void addOrUpdateXconnect(DeviceId deviceId, VlanId vlanId, Set<PortNumber> ports) { |
| 221 | log.info("Adding or updating xconnect. deviceId={}, vlanId={}, ports={}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 222 | deviceId, vlanId, ports); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 223 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 224 | xconnectStore.put(key, ports); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void removeXonnect(DeviceId deviceId, VlanId vlanId) { |
| 229 | log.info("Removing xconnect. deviceId={}, vlanId={}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 230 | deviceId, vlanId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 231 | final XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 232 | xconnectStore.remove(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 233 | |
| 234 | // Cleanup multicasting support, if any. |
| 235 | srService.getPairDeviceId(deviceId).ifPresent(pairDeviceId -> { |
| 236 | cleanupL2MulticastRule(pairDeviceId, srService.getPairLocalPort(pairDeviceId).get(), vlanId, true); |
| 237 | }); |
| 238 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Set<XconnectDesc> getXconnects() { |
| 243 | return xconnectStore.asJavaMap().entrySet().stream() |
| 244 | .map(e -> new XconnectDesc(e.getKey(), e.getValue())) |
| 245 | .collect(Collectors.toSet()); |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public boolean hasXconnect(ConnectPoint cp) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 250 | return getXconnects().stream().anyMatch(desc -> desc.key().deviceId().equals(cp.deviceId()) |
| 251 | && desc.ports().contains(cp.port()) |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 252 | ); |
| 253 | } |
| 254 | |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 255 | @Override |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 256 | public List<VlanId> getXconnectVlans(DeviceId deviceId, PortNumber port) { |
| 257 | return getXconnects().stream() |
| 258 | .filter(desc -> desc.key().deviceId().equals(deviceId) && desc.ports().contains(port)) |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 259 | .map(desc -> desc.key().vlanId()) |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 260 | .collect(Collectors.toList()); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public boolean isXconnectVlan(DeviceId deviceId, VlanId vlanId) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 265 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 266 | return Versioned.valueOrNull(xconnectStore.get(key)) != null; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 270 | public ImmutableMap<XconnectKey, Integer> getNext() { |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 271 | if (xconnectNextObjStore != null) { |
| 272 | return ImmutableMap.copyOf(xconnectNextObjStore.asJavaMap()); |
| 273 | } else { |
| 274 | return ImmutableMap.of(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | @Override |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 279 | public int getNextId(DeviceId deviceId, VlanId vlanId) { |
| 280 | return Versioned.valueOrElse(xconnectNextObjStore.get(new XconnectKey(deviceId, vlanId)), -1); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 284 | public void removeNextId(int nextId) { |
| 285 | xconnectNextObjStore.entrySet().forEach(e -> { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 286 | if (e.getValue().value() == nextId) { |
Charles Chan | 1fa010c | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 287 | xconnectNextObjStore.remove(e.getKey()); |
| 288 | } |
| 289 | }); |
| 290 | } |
| 291 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 292 | private class XconnectMapListener implements MapEventListener<XconnectKey, Set<PortNumber>> { |
| 293 | @Override |
| 294 | public void event(MapEvent<XconnectKey, Set<PortNumber>> event) { |
| 295 | XconnectKey key = event.key(); |
| 296 | Versioned<Set<PortNumber>> ports = event.newValue(); |
| 297 | Versioned<Set<PortNumber>> oldPorts = event.oldValue(); |
| 298 | |
| 299 | switch (event.type()) { |
| 300 | case INSERT: |
| 301 | populateXConnect(key, ports.value()); |
| 302 | break; |
| 303 | case UPDATE: |
| 304 | updateXConnect(key, oldPorts.value(), ports.value()); |
| 305 | break; |
| 306 | case REMOVE: |
| 307 | revokeXConnect(key, oldPorts.value()); |
| 308 | break; |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private class InternalDeviceListener implements DeviceListener { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 316 | // Offload the execution to an executor and then process the event |
| 317 | // if this instance is the leader of the device |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 318 | @Override |
| 319 | public void event(DeviceEvent event) { |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 320 | deviceEventExecutor.execute(() -> { |
| 321 | DeviceId deviceId = event.subject().id(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 322 | // Just skip if we are not the leader |
| 323 | if (!isLocalLeader(deviceId)) { |
| 324 | log.debug("Not the leader of {}. Skip event {}", deviceId, event); |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 327 | // Populate or revoke according to the device availability |
| 328 | if (deviceService.isAvailable(deviceId)) { |
| 329 | init(deviceId); |
| 330 | } else { |
| 331 | cleanup(deviceId); |
Charles Chan | 1eec625 | 2018-08-07 12:48:36 -0700 | [diff] [blame] | 332 | } |
| 333 | }); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 334 | } |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 335 | // We want to manage only a subset of events and if we are the leader |
| 336 | @Override |
| 337 | public boolean isRelevant(DeviceEvent event) { |
| 338 | return event.type() == DeviceEvent.Type.DEVICE_ADDED || |
| 339 | event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED || |
| 340 | event.type() == DeviceEvent.Type.DEVICE_UPDATED; |
| 341 | } |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 342 | } |
| 343 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 344 | private class InternalHostListener implements HostListener { |
| 345 | @Override |
| 346 | public void event(HostEvent event) { |
| 347 | hostEventExecutor.execute(() -> { |
| 348 | |
| 349 | switch (event.type()) { |
| 350 | case HOST_MOVED: |
| 351 | log.trace("Processing host event {}", event); |
| 352 | |
| 353 | Host host = event.subject(); |
| 354 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
| 355 | Set<HostLocation> newLocations = host.locations(); |
| 356 | |
| 357 | // Dual-home host port failure |
| 358 | // For each old location, in failed and paired devices update L2 vlan groups |
| 359 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
| 360 | |
| 361 | Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(prevLocation.deviceId()); |
| 362 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(prevLocation.deviceId()); |
| 363 | |
| 364 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 365 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) && |
| 366 | hasXconnect(new ConnectPoint(prevLocation.deviceId(), prevLocation.port()))) { |
| 367 | |
| 368 | List<VlanId> xconnectVlans = getXconnectVlans(prevLocation.deviceId(), |
| 369 | prevLocation.port()); |
| 370 | xconnectVlans.forEach(xconnectVlan -> { |
| 371 | // Add single-home host into L2 multicast group at paired device side. |
| 372 | // Also append ACL rule to forward traffic from paired port to L2 multicast group. |
| 373 | newLocations.stream() |
| 374 | .filter(location -> location.deviceId().equals(pairDeviceId.get())) |
| 375 | .forEach(location -> populateL2Multicast(location.deviceId(), |
| 376 | srService.getPairLocalPort( |
| 377 | location.deviceId()).get(), |
| 378 | xconnectVlan, |
| 379 | Collections.singletonList( |
| 380 | location.port()))); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 381 | // Ensure pair-port attached to xconnect vlan flooding group |
| 382 | // at dual home failed device. |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 383 | updateL2Flooding(prevLocation.deviceId(), pairLocalPort.get(), xconnectVlan, true); |
| 384 | }); |
| 385 | } |
| 386 | }); |
| 387 | |
| 388 | // Dual-home host port restoration |
| 389 | // For each new location, reverse xconnect loop prevention groups. |
| 390 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
| 391 | final Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(newLocation.deviceId()); |
| 392 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(newLocation.deviceId()); |
| 393 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 394 | hasXconnect((new ConnectPoint(newLocation.deviceId(), newLocation.port())))) { |
| 395 | |
| 396 | List<VlanId> xconnectVlans = getXconnectVlans(newLocation.deviceId(), |
| 397 | newLocation.port()); |
| 398 | xconnectVlans.forEach(xconnectVlan -> { |
| 399 | // Remove recovered dual homed port from vlan L2 multicast group |
| 400 | prevLocations.stream() |
| 401 | .filter(prevLocation -> prevLocation.deviceId().equals(pairDeviceId.get())) |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 402 | .forEach(prevLocation -> revokeL2Multicast( |
| 403 | prevLocation.deviceId(), |
| 404 | xconnectVlan, |
| 405 | Collections.singletonList(newLocation.port())) |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 406 | ); |
| 407 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 408 | // Remove pair-port from vlan's flooding group at dual home |
| 409 | // restored device, if needed. |
| 410 | if (!hasAccessPortInMulticastGroup(new VlanNextObjectiveStoreKey( |
| 411 | newLocation.deviceId(), xconnectVlan), pairLocalPort.get())) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 412 | updateL2Flooding(newLocation.deviceId(), |
| 413 | pairLocalPort.get(), |
| 414 | xconnectVlan, |
| 415 | false); |
| 416 | |
| 417 | // Clean L2 multicast group at pair-device; also update store. |
| 418 | cleanupL2MulticastRule(pairDeviceId.get(), |
| 419 | srService.getPairLocalPort(pairDeviceId.get()).get(), |
| 420 | xconnectVlan, |
| 421 | false); |
| 422 | } |
| 423 | }); |
| 424 | } |
| 425 | }); |
| 426 | break; |
| 427 | |
| 428 | default: |
| 429 | log.warn("Unsupported host event type: {} received. Ignoring.", event.type()); |
| 430 | break; |
| 431 | } |
| 432 | }); |
| 433 | } |
| 434 | } |
| 435 | |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 436 | private void init(DeviceId deviceId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 437 | getXconnects().stream() |
| 438 | .filter(desc -> desc.key().deviceId().equals(deviceId)) |
| 439 | .forEach(desc -> populateXConnect(desc.key(), desc.ports())); |
| 440 | } |
| 441 | |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 442 | private void cleanup(DeviceId deviceId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 443 | xconnectNextObjStore.entrySet().stream() |
| 444 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 445 | .forEach(entry -> xconnectNextObjStore.remove(entry.getKey())); |
| 446 | log.debug("{} is removed from xConnectNextObjStore", deviceId); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Populates XConnect groups and flows for given key. |
| 451 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 452 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 453 | * @param ports a set of ports to be cross-connected |
| 454 | */ |
| 455 | private void populateXConnect(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 456 | if (!isLocalLeader(key.deviceId())) { |
| 457 | log.debug("Abort populating XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 458 | return; |
| 459 | } |
| 460 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 461 | populateFilter(key, ports); |
| 462 | populateFwd(key, populateNext(key, ports)); |
| 463 | populateAcl(key); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Populates filtering objectives for given XConnect. |
| 468 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 469 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 470 | * @param ports XConnect ports |
| 471 | */ |
| 472 | private void populateFilter(XconnectKey key, Set<PortNumber> ports) { |
| 473 | ports.forEach(port -> { |
| 474 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 475 | ObjectiveContext context = new DefaultObjectiveContext( |
| 476 | (objective) -> log.debug("XConnect FilterObj for {} on port {} populated", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 477 | key, port), |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 478 | (objective, error) -> |
| 479 | log.warn("Failed to populate XConnect FilterObj for {} on port {}: {}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 480 | key, port, error)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 481 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.add(context)); |
| 482 | }); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Populates next objectives for given XConnect. |
| 487 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 488 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 489 | * @param ports XConnect ports |
| 490 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 491 | private int populateNext(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 492 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 493 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 494 | log.debug("NextObj for {} found, id={}", key, nextId); |
| 495 | return nextId; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 496 | } else { |
| 497 | NextObjective.Builder nextObjBuilder = nextObjBuilder(key, ports); |
| 498 | ObjectiveContext nextContext = new DefaultObjectiveContext( |
| 499 | // To serialize this with kryo |
| 500 | (Serializable & Consumer<Objective>) (objective) -> |
| 501 | log.debug("XConnect NextObj for {} added", key), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 502 | (Serializable & BiConsumer<Objective, ObjectiveError>) (objective, error) -> { |
| 503 | log.warn("Failed to add XConnect NextObj for {}: {}", key, error); |
| 504 | srService.invalidateNextObj(objective.id()); |
| 505 | }); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 506 | NextObjective nextObj = nextObjBuilder.add(nextContext); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 507 | flowObjectiveService.next(key.deviceId(), nextObj); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 508 | xconnectNextObjStore.put(key, nextObj.id()); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 509 | log.debug("NextObj for {} not found. Creating new NextObj with id={}", key, nextObj.id()); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 510 | return nextObj.id(); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 511 | } |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Populates bridging forwarding objectives for given XConnect. |
| 516 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 517 | * @param key XConnect store key |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 518 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 519 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 520 | private void populateFwd(XconnectKey key, int nextId) { |
| 521 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 522 | ObjectiveContext fwdContext = new DefaultObjectiveContext( |
| 523 | (objective) -> log.debug("XConnect FwdObj for {} populated", key), |
| 524 | (objective, error) -> |
| 525 | log.warn("Failed to populate XConnect FwdObj for {}: {}", key, error)); |
| 526 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.add(fwdContext)); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Populates ACL forwarding objectives for given XConnect. |
| 531 | * |
| 532 | * @param key XConnect store key |
| 533 | */ |
| 534 | private void populateAcl(XconnectKey key) { |
| 535 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 536 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 537 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 538 | (objective, error) -> |
| 539 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 540 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.add(aclContext)); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Revokes XConnect groups and flows for given key. |
| 545 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 546 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 547 | * @param ports XConnect ports |
| 548 | */ |
| 549 | private void revokeXConnect(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 550 | if (!isLocalLeader(key.deviceId())) { |
| 551 | log.debug("Abort revoking XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 555 | revokeFilter(key, ports); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 556 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 557 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 558 | revokeFwd(key, nextId, null); |
| 559 | revokeNext(key, ports, nextId, null); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 560 | } else { |
| 561 | log.warn("NextObj for {} does not exist in the store.", key); |
| 562 | } |
| 563 | revokeAcl(key); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * Revokes filtering objectives for given XConnect. |
| 568 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 569 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 570 | * @param ports XConnect ports |
| 571 | */ |
| 572 | private void revokeFilter(XconnectKey key, Set<PortNumber> ports) { |
| 573 | ports.forEach(port -> { |
| 574 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 575 | ObjectiveContext context = new DefaultObjectiveContext( |
| 576 | (objective) -> log.debug("XConnect FilterObj for {} on port {} revoked", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 577 | key, port), |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 578 | (objective, error) -> |
| 579 | log.warn("Failed to revoke XConnect FilterObj for {} on port {}: {}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 580 | key, port, error)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 581 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.remove(context)); |
| 582 | }); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Revokes next objectives for given XConnect. |
| 587 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 588 | * @param key XConnect store key |
| 589 | * @param ports ports in the XConnect |
| 590 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 591 | * @param nextFuture completable future for this next objective operation |
| 592 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 593 | private void revokeNext(XconnectKey key, Set<PortNumber> ports, int nextId, |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 594 | CompletableFuture<ObjectiveError> nextFuture) { |
| 595 | ObjectiveContext context = new ObjectiveContext() { |
| 596 | @Override |
| 597 | public void onSuccess(Objective objective) { |
| 598 | log.debug("Previous NextObj for {} removed", key); |
| 599 | if (nextFuture != null) { |
| 600 | nextFuture.complete(null); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void onError(Objective objective, ObjectiveError error) { |
| 606 | log.warn("Failed to remove previous NextObj for {}: {}", key, error); |
| 607 | if (nextFuture != null) { |
| 608 | nextFuture.complete(error); |
| 609 | } |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 610 | srService.invalidateNextObj(objective.id()); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 611 | } |
| 612 | }; |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 613 | flowObjectiveService.next(key.deviceId(), nextObjBuilder(key, ports, nextId).remove(context)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 614 | xconnectNextObjStore.remove(key); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Revokes bridging forwarding objectives for given XConnect. |
| 619 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 620 | * @param key XConnect store key |
| 621 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 622 | * @param fwdFuture completable future for this forwarding objective operation |
| 623 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 624 | private void revokeFwd(XconnectKey key, int nextId, CompletableFuture<ObjectiveError> fwdFuture) { |
| 625 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 626 | ObjectiveContext context = new ObjectiveContext() { |
| 627 | @Override |
| 628 | public void onSuccess(Objective objective) { |
| 629 | log.debug("Previous FwdObj for {} removed", key); |
| 630 | if (fwdFuture != null) { |
| 631 | fwdFuture.complete(null); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | @Override |
| 636 | public void onError(Objective objective, ObjectiveError error) { |
| 637 | log.warn("Failed to remove previous FwdObj for {}: {}", key, error); |
| 638 | if (fwdFuture != null) { |
| 639 | fwdFuture.complete(error); |
| 640 | } |
| 641 | } |
| 642 | }; |
| 643 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.remove(context)); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Revokes ACL forwarding objectives for given XConnect. |
| 648 | * |
| 649 | * @param key XConnect store key |
| 650 | */ |
| 651 | private void revokeAcl(XconnectKey key) { |
| 652 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 653 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 654 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 655 | (objective, error) -> |
| 656 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 657 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.remove(aclContext)); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Updates XConnect groups and flows for given key. |
| 662 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 663 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 664 | * @param prevPorts previous XConnect ports |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 665 | * @param ports new XConnect ports |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 666 | */ |
| 667 | private void updateXConnect(XconnectKey key, Set<PortNumber> prevPorts, |
| 668 | Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 669 | if (!isLocalLeader(key.deviceId())) { |
| 670 | log.debug("Abort updating XConnect {}: {}", key, ERROR_NOT_LEADER); |
| 671 | return; |
| 672 | } |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 673 | // NOTE: ACL flow doesn't include port information. No need to update it. |
| 674 | // Pair port is built-in and thus not going to change. No need to update it. |
| 675 | |
| 676 | // remove old filter |
| 677 | prevPorts.stream().filter(port -> !ports.contains(port)).forEach(port -> |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 678 | revokeFilter(key, |
| 679 | ImmutableSet.of(port))); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 680 | // install new filter |
| 681 | ports.stream().filter(port -> !prevPorts.contains(port)).forEach(port -> |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 682 | populateFilter(key, |
| 683 | ImmutableSet.of(port))); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 684 | |
| 685 | CompletableFuture<ObjectiveError> fwdFuture = new CompletableFuture<>(); |
| 686 | CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>(); |
| 687 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 688 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 689 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 690 | revokeFwd(key, nextId, fwdFuture); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 691 | |
| 692 | fwdFuture.thenAcceptAsync(fwdStatus -> { |
| 693 | if (fwdStatus == null) { |
| 694 | log.debug("Fwd removed. Now remove group {}", key); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 695 | revokeNext(key, prevPorts, nextId, nextFuture); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 696 | } |
| 697 | }); |
| 698 | |
| 699 | nextFuture.thenAcceptAsync(nextStatus -> { |
| 700 | if (nextStatus == null) { |
| 701 | log.debug("Installing new group and flow for {}", key); |
| 702 | populateFwd(key, populateNext(key, ports)); |
| 703 | } |
| 704 | }); |
| 705 | } else { |
| 706 | log.warn("NextObj for {} does not exist in the store.", key); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /** |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 711 | * Creates a next objective builder for XConnect with given nextId. |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 712 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 713 | * @param key XConnect key |
| 714 | * @param ports set of XConnect ports |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 715 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 716 | * @return next objective builder |
| 717 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 718 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports, int nextId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 719 | TrafficSelector metadata = |
| 720 | DefaultTrafficSelector.builder().matchVlanId(key.vlanId()).build(); |
| 721 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 722 | .builder().withId(nextId) |
| 723 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 724 | .withMeta(metadata); |
| 725 | ports.forEach(port -> { |
| 726 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 727 | tBuilder.setOutput(port); |
| 728 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 729 | }); |
| 730 | return nextObjBuilder; |
| 731 | } |
| 732 | |
| 733 | /** |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 734 | * Creates a next objective builder for XConnect. |
| 735 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 736 | * @param key XConnect key |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 737 | * @param ports set of XConnect ports |
| 738 | * @return next objective builder |
| 739 | */ |
| 740 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports) { |
| 741 | int nextId = flowObjectiveService.allocateNextId(); |
| 742 | return nextObjBuilder(key, ports, nextId); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /** |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 747 | * Creates a bridging forwarding objective builder for XConnect. |
| 748 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 749 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 750 | * @param nextId next ID of the broadcast group for this XConnect key |
| 751 | * @return forwarding objective builder |
| 752 | */ |
| 753 | private ForwardingObjective.Builder fwdObjBuilder(XconnectKey key, int nextId) { |
| 754 | /* |
| 755 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 756 | * as the VLAN cross-connect broadcast rules |
| 757 | */ |
| 758 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 759 | sbuilder.matchVlanId(key.vlanId()); |
| 760 | sbuilder.matchEthDst(MacAddress.NONE); |
| 761 | |
| 762 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 763 | fob.withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 764 | .withSelector(sbuilder.build()) |
| 765 | .nextStep(nextId) |
| 766 | .withPriority(XCONNECT_PRIORITY) |
| 767 | .fromApp(appId) |
| 768 | .makePermanent(); |
| 769 | return fob; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Creates an ACL forwarding objective builder for XConnect. |
| 774 | * |
| 775 | * @param vlanId cross connect VLAN id |
| 776 | * @return forwarding objective builder |
| 777 | */ |
| 778 | private ForwardingObjective.Builder aclObjBuilder(VlanId vlanId) { |
| 779 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 780 | sbuilder.matchVlanId(vlanId); |
| 781 | |
| 782 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 783 | |
| 784 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 785 | fob.withFlag(ForwardingObjective.Flag.VERSATILE) |
| 786 | .withSelector(sbuilder.build()) |
| 787 | .withTreatment(tbuilder.build()) |
| 788 | .withPriority(XCONNECT_ACL_PRIORITY) |
| 789 | .fromApp(appId) |
| 790 | .makePermanent(); |
| 791 | return fob; |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Creates a filtering objective builder for XConnect. |
| 796 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 797 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 798 | * @param port XConnect ports |
| 799 | * @return next objective builder |
| 800 | */ |
| 801 | private FilteringObjective.Builder filterObjBuilder(XconnectKey key, PortNumber port) { |
| 802 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 803 | fob.withKey(Criteria.matchInPort(port)) |
| 804 | .addCondition(Criteria.matchVlanId(key.vlanId())) |
| 805 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
| 806 | .withPriority(XCONNECT_PRIORITY); |
| 807 | return fob.permit().fromApp(appId); |
| 808 | } |
| 809 | |
| 810 | /** |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 811 | * Updates L2 flooding groups; add pair link into L2 flooding group of given xconnect vlan. |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 812 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 813 | * @param deviceId Device ID |
| 814 | * @param port Port details |
| 815 | * @param vlanId VLAN ID |
| 816 | * @param install Whether to add or revoke pair link addition to flooding group |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 817 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 818 | private void updateL2Flooding(DeviceId deviceId, PortNumber port, VlanId vlanId, boolean install) { |
| 819 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 820 | // Ensure leadership on device |
| 821 | if (!isLocalLeader(deviceId)) { |
| 822 | log.debug("Abort updating L2Flood {}: {}", key, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 823 | return; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 824 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 825 | |
| 826 | // Locate L2 flooding group details for given xconnect vlan |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 827 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 828 | if (nextId == -1) { |
| 829 | log.debug("XConnect vlan {} broadcast group for device {} doesn't exists. " + |
| 830 | "Aborting pair group linking.", vlanId, deviceId); |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | // Add pairing-port group to flooding group |
| 835 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 836 | // treatment.popVlan(); |
| 837 | treatment.setOutput(port); |
| 838 | ObjectiveContext context = new DefaultObjectiveContext( |
| 839 | (objective) -> |
| 840 | log.debug("Pair port added/removed to vlan {} next objective {} on {}", |
| 841 | vlanId, nextId, deviceId), |
| 842 | (objective, error) -> |
| 843 | log.warn("Failed adding/removing pair port to vlan {} next objective {} on {}." + |
| 844 | "Error : {}", vlanId, nextId, deviceId, error) |
| 845 | ); |
| 846 | NextObjective.Builder vlanNextObjectiveBuilder = DefaultNextObjective.builder() |
| 847 | .withId(nextId) |
| 848 | .withType(NextObjective.Type.BROADCAST) |
| 849 | .fromApp(srService.appId()) |
| 850 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 851 | .addTreatment(treatment.build()); |
| 852 | if (install) { |
| 853 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.addToExisting(context)); |
| 854 | } else { |
| 855 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.removeFromExisting(context)); |
| 856 | } |
| 857 | log.debug("Submitted next objective {} for vlan: {} in device {}", |
| 858 | nextId, vlanId, deviceId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 859 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 860 | |
| 861 | /** |
| 862 | * Populate L2 multicast rule on given deviceId that matches given mac, given vlan and |
| 863 | * output to given port's L2 mulitcast group. |
| 864 | * |
| 865 | * @param deviceId Device ID |
| 866 | * @param pairPort Pair port number |
| 867 | * @param vlanId VLAN ID |
| 868 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 869 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 870 | private void populateL2Multicast(DeviceId deviceId, PortNumber pairPort, |
| 871 | VlanId vlanId, List<PortNumber> accessPorts) { |
| 872 | // Ensure enough rights to program pair device |
| 873 | if (!srService.shouldProgram(deviceId)) { |
| 874 | log.debug("Abort populate L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
| 875 | return; |
| 876 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 877 | |
| 878 | boolean multicastGroupExists = true; |
| 879 | int vlanMulticastNextId; |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 880 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 881 | |
| 882 | // Step 1 : Populate single homed access ports into vlan's L2 multicast group |
| 883 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 884 | .builder() |
| 885 | .withType(NextObjective.Type.BROADCAST) |
| 886 | .fromApp(srService.appId()) |
| 887 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId) |
| 888 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 889 | vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 890 | if (vlanMulticastNextId == -1) { |
| 891 | // Vlan's L2 multicast group doesn't exist; create it, update store and add pair port as sub-group |
| 892 | multicastGroupExists = false; |
| 893 | vlanMulticastNextId = flowObjectiveService.allocateNextId(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 894 | addMulticastGroupNextObjectiveId(key, vlanMulticastNextId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 895 | vlanMulticastNextObjBuilder.addTreatment( |
| 896 | DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build() |
| 897 | ); |
| 898 | } |
| 899 | vlanMulticastNextObjBuilder.withId(vlanMulticastNextId); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 900 | int nextId = vlanMulticastNextId; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 901 | accessPorts.forEach(p -> { |
| 902 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 903 | // Do vlan popup action based on interface configuration |
| 904 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 905 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 906 | egressAction.popVlan(); |
| 907 | } |
| 908 | egressAction.setOutput(p); |
| 909 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 910 | addMulticastGroupPort(key, p); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 911 | }); |
| 912 | ObjectiveContext context = new DefaultObjectiveContext( |
| 913 | (objective) -> |
| 914 | log.debug("L2 multicast group installed/updated. " |
| 915 | + "NextObject Id {} on {} for subnet {} ", |
| 916 | nextId, deviceId, vlanId), |
| 917 | (objective, error) -> |
| 918 | log.warn("L2 multicast group failed to install/update. " |
| 919 | + " NextObject Id {} on {} for subnet {} : {}", |
| 920 | nextId, deviceId, vlanId, error) |
| 921 | ); |
| 922 | if (!multicastGroupExists) { |
| 923 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.add(context)); |
| 924 | |
| 925 | // Step 2 : Populate ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 926 | TrafficSelector.Builder multicastSelector = DefaultTrafficSelector.builder(); |
| 927 | multicastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 928 | multicastSelector.matchInPort(pairPort); |
| 929 | multicastSelector.matchVlanId(vlanId); |
| 930 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 931 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 932 | .nextStep(vlanMulticastNextId) |
| 933 | .withSelector(multicastSelector.build()) |
| 934 | .withPriority(100) |
| 935 | .fromApp(srService.appId()) |
| 936 | .makePermanent(); |
| 937 | context = new DefaultObjectiveContext( |
| 938 | (objective) -> log.debug("L2 multicasting versatile rule for device {}, port/vlan {}/{} populated", |
| 939 | deviceId, |
| 940 | pairPort, |
| 941 | vlanId), |
| 942 | (objective, error) -> log.warn("Failed to populate L2 multicasting versatile rule for device {}, " + |
| 943 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 944 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.add(context)); |
| 945 | } else { |
| 946 | // L2_MULTICAST & BROADCAST are similar structure in subgroups; so going with BROADCAST type. |
| 947 | vlanMulticastNextObjBuilder.withType(NextObjective.Type.BROADCAST); |
| 948 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.addToExisting(context)); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Removes access ports from VLAN L2 multicast group on given deviceId. |
| 954 | * |
| 955 | * @param deviceId Device ID |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 956 | * @param vlanId VLAN ID |
| 957 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 958 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 959 | private void revokeL2Multicast(DeviceId deviceId, VlanId vlanId, List<PortNumber> accessPorts) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 960 | // Ensure enough rights to program pair device |
| 961 | if (!srService.shouldProgram(deviceId)) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 962 | log.debug("Abort revoke L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 963 | return; |
| 964 | } |
| 965 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 966 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 967 | |
| 968 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 969 | if (vlanMulticastNextId == -1) { |
| 970 | return; |
| 971 | } |
| 972 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 973 | .builder() |
| 974 | .withType(NextObjective.Type.BROADCAST) |
| 975 | .fromApp(srService.appId()) |
| 976 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 977 | .withId(vlanMulticastNextId); |
| 978 | accessPorts.forEach(p -> { |
| 979 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 980 | // Do vlan popup action based on interface configuration |
| 981 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 982 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 983 | egressAction.popVlan(); |
| 984 | } |
| 985 | egressAction.setOutput(p); |
| 986 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 987 | removeMulticastGroupPort(key, p); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 988 | }); |
| 989 | ObjectiveContext context = new DefaultObjectiveContext( |
| 990 | (objective) -> |
| 991 | log.debug("L2 multicast group installed/updated. " |
| 992 | + "NextObject Id {} on {} for subnet {} ", |
| 993 | vlanMulticastNextId, deviceId, vlanId), |
| 994 | (objective, error) -> |
| 995 | log.warn("L2 multicast group failed to install/update. " |
| 996 | + " NextObject Id {} on {} for subnet {} : {}", |
| 997 | vlanMulticastNextId, deviceId, vlanId, error) |
| 998 | ); |
| 999 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.removeFromExisting(context)); |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Cleans up VLAN L2 multicast group on given deviceId. ACL rules for the group will also be deleted. |
| 1004 | * Normally multicast group is not removed if it contains access ports; which can be forced |
| 1005 | * by "force" flag |
| 1006 | * |
| 1007 | * @param deviceId Device ID |
| 1008 | * @param pairPort Pair port number |
| 1009 | * @param vlanId VLAN ID |
| 1010 | * @param force Forceful removal |
| 1011 | */ |
| 1012 | private void cleanupL2MulticastRule(DeviceId deviceId, PortNumber pairPort, VlanId vlanId, boolean force) { |
| 1013 | |
| 1014 | // Ensure enough rights to program pair device |
| 1015 | if (!srService.shouldProgram(deviceId)) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1016 | log.debug("Abort cleanup L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1020 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 1021 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1022 | // Ensure L2 multicast group doesn't contain access ports |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1023 | if (hasAccessPortInMulticastGroup(key, pairPort) && !force) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | // Load L2 multicast group details |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1028 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1029 | if (vlanMulticastNextId == -1) { |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | // Step 1 : Clear ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 1034 | TrafficSelector.Builder l2MulticastSelector = DefaultTrafficSelector.builder(); |
| 1035 | l2MulticastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 1036 | l2MulticastSelector.matchInPort(pairPort); |
| 1037 | l2MulticastSelector.matchVlanId(vlanId); |
| 1038 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 1039 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1040 | .nextStep(vlanMulticastNextId) |
| 1041 | .withSelector(l2MulticastSelector.build()) |
| 1042 | .withPriority(100) |
| 1043 | .fromApp(srService.appId()) |
| 1044 | .makePermanent(); |
| 1045 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1046 | (objective) -> log.debug("L2 multicasting rule for device {}, port/vlan {}/{} deleted", deviceId, |
| 1047 | pairPort, vlanId), |
| 1048 | (objective, error) -> log.warn("Failed to delete L2 multicasting rule for device {}, " + |
| 1049 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 1050 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.remove(context)); |
| 1051 | |
| 1052 | // Step 2 : Clear L2 multicast group associated with vlan |
| 1053 | NextObjective.Builder l2MulticastGroupBuilder = DefaultNextObjective |
| 1054 | .builder() |
| 1055 | .withId(vlanMulticastNextId) |
| 1056 | .withType(NextObjective.Type.BROADCAST) |
| 1057 | .fromApp(srService.appId()) |
| 1058 | .withMeta(DefaultTrafficSelector.builder() |
| 1059 | .matchVlanId(vlanId) |
| 1060 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()) |
| 1061 | .addTreatment(DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build()); |
| 1062 | context = new DefaultObjectiveContext( |
| 1063 | (objective) -> |
| 1064 | log.debug("L2 multicast group with NextObject Id {} deleted on {} for subnet {} ", |
| 1065 | vlanMulticastNextId, deviceId, vlanId), |
| 1066 | (objective, error) -> |
| 1067 | log.warn("L2 multicast group with NextObject Id {} failed to delete on {} for subnet {} : {}", |
| 1068 | vlanMulticastNextId, deviceId, vlanId, error) |
| 1069 | ); |
| 1070 | flowObjectiveService.next(deviceId, l2MulticastGroupBuilder.remove(context)); |
| 1071 | |
| 1072 | // Finally clear store. |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1073 | removeMulticastGroup(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1074 | } |
| 1075 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1076 | private int getMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key) { |
| 1077 | return Versioned.valueOrElse(xconnectMulticastNextStore.get(key), -1); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1078 | } |
| 1079 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1080 | private void addMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key, int nextId) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1081 | if (nextId == -1) { |
| 1082 | return; |
| 1083 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1084 | xconnectMulticastNextStore.put(key, nextId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1085 | } |
| 1086 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1087 | private void addMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1088 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1089 | if (ports == null) { |
| 1090 | ports = Lists.newArrayList(); |
| 1091 | } |
| 1092 | ports.add(port); |
| 1093 | return ports; |
| 1094 | }); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1095 | } |
| 1096 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1097 | private void removeMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1098 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1099 | if (ports != null && !ports.isEmpty()) { |
| 1100 | ports.remove(port); |
| 1101 | } |
| 1102 | return ports; |
| 1103 | }); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1104 | } |
| 1105 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1106 | private void removeMulticastGroup(VlanNextObjectiveStoreKey groupKey) { |
| 1107 | xconnectMulticastPortsStore.remove(groupKey); |
| 1108 | xconnectMulticastNextStore.remove(groupKey); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1109 | } |
| 1110 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1111 | private boolean hasAccessPortInMulticastGroup(VlanNextObjectiveStoreKey groupKey, PortNumber pairPort) { |
| 1112 | List<PortNumber> ports = Versioned.valueOrElse(xconnectMulticastPortsStore.get(groupKey), ImmutableList.of()); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1113 | return ports.stream().anyMatch(p -> !p.equals(pairPort)); |
| 1114 | } |
| 1115 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame^] | 1116 | // Custom-built function, when the device is not available we need a fallback mechanism |
| 1117 | private boolean isLocalLeader(DeviceId deviceId) { |
| 1118 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1119 | // When the device is available we just check the mastership |
| 1120 | if (deviceService.isAvailable(deviceId)) { |
| 1121 | return false; |
| 1122 | } |
| 1123 | // Fallback with Leadership service - device id is used as topic |
| 1124 | NodeId leader = leadershipService.runForLeadership( |
| 1125 | deviceId.toString()).leaderNodeId(); |
| 1126 | // Verify if this node is the leader |
| 1127 | return clusterService.getLocalNode().id().equals(leader); |
| 1128 | } |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1132 | } |