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 | |
Charles Chan | 264b4b7 | 2018-10-19 16:32:07 -0700 | [diff] [blame] | 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 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()) { |
Sudhir Kumar Maurya | 85e9313 | 2019-05-02 03:03:59 -0400 | [diff] [blame] | 350 | case HOST_ADDED: |
| 351 | case HOST_REMOVED: |
| 352 | case HOST_UPDATED: |
| 353 | log.trace("Unhandled host event type: {} received. Ignoring.", event.type()); |
| 354 | break; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 355 | case HOST_MOVED: |
| 356 | log.trace("Processing host event {}", event); |
| 357 | |
| 358 | Host host = event.subject(); |
| 359 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
| 360 | Set<HostLocation> newLocations = host.locations(); |
| 361 | |
| 362 | // Dual-home host port failure |
| 363 | // For each old location, in failed and paired devices update L2 vlan groups |
| 364 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
| 365 | |
| 366 | Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(prevLocation.deviceId()); |
| 367 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(prevLocation.deviceId()); |
| 368 | |
| 369 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 370 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) && |
| 371 | hasXconnect(new ConnectPoint(prevLocation.deviceId(), prevLocation.port()))) { |
| 372 | |
| 373 | List<VlanId> xconnectVlans = getXconnectVlans(prevLocation.deviceId(), |
| 374 | prevLocation.port()); |
| 375 | xconnectVlans.forEach(xconnectVlan -> { |
| 376 | // Add single-home host into L2 multicast group at paired device side. |
| 377 | // Also append ACL rule to forward traffic from paired port to L2 multicast group. |
| 378 | newLocations.stream() |
| 379 | .filter(location -> location.deviceId().equals(pairDeviceId.get())) |
| 380 | .forEach(location -> populateL2Multicast(location.deviceId(), |
| 381 | srService.getPairLocalPort( |
| 382 | location.deviceId()).get(), |
| 383 | xconnectVlan, |
| 384 | Collections.singletonList( |
| 385 | location.port()))); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 386 | // Ensure pair-port attached to xconnect vlan flooding group |
| 387 | // at dual home failed device. |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 388 | updateL2Flooding(prevLocation.deviceId(), pairLocalPort.get(), xconnectVlan, true); |
| 389 | }); |
| 390 | } |
| 391 | }); |
| 392 | |
| 393 | // Dual-home host port restoration |
| 394 | // For each new location, reverse xconnect loop prevention groups. |
| 395 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
| 396 | final Optional<DeviceId> pairDeviceId = srService.getPairDeviceId(newLocation.deviceId()); |
| 397 | Optional<PortNumber> pairLocalPort = srService.getPairLocalPort(newLocation.deviceId()); |
| 398 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 399 | hasXconnect((new ConnectPoint(newLocation.deviceId(), newLocation.port())))) { |
| 400 | |
| 401 | List<VlanId> xconnectVlans = getXconnectVlans(newLocation.deviceId(), |
| 402 | newLocation.port()); |
| 403 | xconnectVlans.forEach(xconnectVlan -> { |
| 404 | // Remove recovered dual homed port from vlan L2 multicast group |
| 405 | prevLocations.stream() |
| 406 | .filter(prevLocation -> prevLocation.deviceId().equals(pairDeviceId.get())) |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 407 | .forEach(prevLocation -> revokeL2Multicast( |
| 408 | prevLocation.deviceId(), |
| 409 | xconnectVlan, |
| 410 | Collections.singletonList(newLocation.port())) |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 411 | ); |
| 412 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 413 | // Remove pair-port from vlan's flooding group at dual home |
| 414 | // restored device, if needed. |
| 415 | if (!hasAccessPortInMulticastGroup(new VlanNextObjectiveStoreKey( |
| 416 | newLocation.deviceId(), xconnectVlan), pairLocalPort.get())) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 417 | updateL2Flooding(newLocation.deviceId(), |
| 418 | pairLocalPort.get(), |
| 419 | xconnectVlan, |
| 420 | false); |
| 421 | |
| 422 | // Clean L2 multicast group at pair-device; also update store. |
| 423 | cleanupL2MulticastRule(pairDeviceId.get(), |
| 424 | srService.getPairLocalPort(pairDeviceId.get()).get(), |
| 425 | xconnectVlan, |
| 426 | false); |
| 427 | } |
| 428 | }); |
| 429 | } |
| 430 | }); |
| 431 | break; |
| 432 | |
| 433 | default: |
| 434 | log.warn("Unsupported host event type: {} received. Ignoring.", event.type()); |
| 435 | break; |
| 436 | } |
| 437 | }); |
| 438 | } |
| 439 | } |
| 440 | |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 441 | private void init(DeviceId deviceId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 442 | getXconnects().stream() |
| 443 | .filter(desc -> desc.key().deviceId().equals(deviceId)) |
| 444 | .forEach(desc -> populateXConnect(desc.key(), desc.ports())); |
| 445 | } |
| 446 | |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 447 | private void cleanup(DeviceId deviceId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 448 | xconnectNextObjStore.entrySet().stream() |
| 449 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 450 | .forEach(entry -> xconnectNextObjStore.remove(entry.getKey())); |
| 451 | log.debug("{} is removed from xConnectNextObjStore", deviceId); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Populates XConnect groups and flows for given key. |
| 456 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 457 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 458 | * @param ports a set of ports to be cross-connected |
| 459 | */ |
| 460 | private void populateXConnect(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 461 | if (!isLocalLeader(key.deviceId())) { |
| 462 | log.debug("Abort populating XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 466 | populateFilter(key, ports); |
| 467 | populateFwd(key, populateNext(key, ports)); |
| 468 | populateAcl(key); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Populates filtering objectives for given XConnect. |
| 473 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 474 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 475 | * @param ports XConnect ports |
| 476 | */ |
| 477 | private void populateFilter(XconnectKey key, Set<PortNumber> ports) { |
| 478 | ports.forEach(port -> { |
| 479 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 480 | ObjectiveContext context = new DefaultObjectiveContext( |
| 481 | (objective) -> log.debug("XConnect FilterObj for {} on port {} populated", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 482 | key, port), |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 483 | (objective, error) -> |
| 484 | log.warn("Failed to populate XConnect FilterObj for {} on port {}: {}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 485 | key, port, error)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 486 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.add(context)); |
| 487 | }); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Populates next objectives for given XConnect. |
| 492 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 493 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 494 | * @param ports XConnect ports |
| 495 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 496 | private int populateNext(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 497 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 498 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 499 | log.debug("NextObj for {} found, id={}", key, nextId); |
| 500 | return nextId; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 501 | } else { |
| 502 | NextObjective.Builder nextObjBuilder = nextObjBuilder(key, ports); |
| 503 | ObjectiveContext nextContext = new DefaultObjectiveContext( |
| 504 | // To serialize this with kryo |
| 505 | (Serializable & Consumer<Objective>) (objective) -> |
| 506 | log.debug("XConnect NextObj for {} added", key), |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 507 | (Serializable & BiConsumer<Objective, ObjectiveError>) (objective, error) -> { |
| 508 | log.warn("Failed to add XConnect NextObj for {}: {}", key, error); |
| 509 | srService.invalidateNextObj(objective.id()); |
| 510 | }); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 511 | NextObjective nextObj = nextObjBuilder.add(nextContext); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 512 | flowObjectiveService.next(key.deviceId(), nextObj); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 513 | xconnectNextObjStore.put(key, nextObj.id()); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 514 | 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] | 515 | return nextObj.id(); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 516 | } |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Populates bridging forwarding objectives for given XConnect. |
| 521 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 522 | * @param key XConnect store key |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 523 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 524 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 525 | private void populateFwd(XconnectKey key, int nextId) { |
| 526 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 527 | ObjectiveContext fwdContext = new DefaultObjectiveContext( |
| 528 | (objective) -> log.debug("XConnect FwdObj for {} populated", key), |
| 529 | (objective, error) -> |
| 530 | log.warn("Failed to populate XConnect FwdObj for {}: {}", key, error)); |
| 531 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.add(fwdContext)); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Populates ACL forwarding objectives for given XConnect. |
| 536 | * |
| 537 | * @param key XConnect store key |
| 538 | */ |
| 539 | private void populateAcl(XconnectKey key) { |
| 540 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 541 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 542 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 543 | (objective, error) -> |
| 544 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 545 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.add(aclContext)); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Revokes XConnect groups and flows for given key. |
| 550 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 551 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 552 | * @param ports XConnect ports |
| 553 | */ |
| 554 | private void revokeXConnect(XconnectKey key, Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 555 | if (!isLocalLeader(key.deviceId())) { |
| 556 | log.debug("Abort revoking XConnect {}: {}", key, ERROR_NOT_LEADER); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 560 | revokeFilter(key, ports); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 561 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 562 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 563 | revokeFwd(key, nextId, null); |
| 564 | revokeNext(key, ports, nextId, null); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 565 | } else { |
| 566 | log.warn("NextObj for {} does not exist in the store.", key); |
| 567 | } |
| 568 | revokeAcl(key); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Revokes filtering objectives for given XConnect. |
| 573 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 574 | * @param key XConnect store key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 575 | * @param ports XConnect ports |
| 576 | */ |
| 577 | private void revokeFilter(XconnectKey key, Set<PortNumber> ports) { |
| 578 | ports.forEach(port -> { |
| 579 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(key, port); |
| 580 | ObjectiveContext context = new DefaultObjectiveContext( |
| 581 | (objective) -> log.debug("XConnect FilterObj for {} on port {} revoked", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 582 | key, port), |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 583 | (objective, error) -> |
| 584 | log.warn("Failed to revoke XConnect FilterObj for {} on port {}: {}", |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 585 | key, port, error)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 586 | flowObjectiveService.filter(key.deviceId(), filtObjBuilder.remove(context)); |
| 587 | }); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Revokes next objectives for given XConnect. |
| 592 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 593 | * @param key XConnect store key |
| 594 | * @param ports ports in the XConnect |
| 595 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 596 | * @param nextFuture completable future for this next objective operation |
| 597 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 598 | private void revokeNext(XconnectKey key, Set<PortNumber> ports, int nextId, |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 599 | CompletableFuture<ObjectiveError> nextFuture) { |
| 600 | ObjectiveContext context = new ObjectiveContext() { |
| 601 | @Override |
| 602 | public void onSuccess(Objective objective) { |
| 603 | log.debug("Previous NextObj for {} removed", key); |
| 604 | if (nextFuture != null) { |
| 605 | nextFuture.complete(null); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | public void onError(Objective objective, ObjectiveError error) { |
| 611 | log.warn("Failed to remove previous NextObj for {}: {}", key, error); |
| 612 | if (nextFuture != null) { |
| 613 | nextFuture.complete(error); |
| 614 | } |
Charles Chan | 1dbadbd | 2018-08-23 14:30:33 -0700 | [diff] [blame] | 615 | srService.invalidateNextObj(objective.id()); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 616 | } |
| 617 | }; |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 618 | flowObjectiveService.next(key.deviceId(), nextObjBuilder(key, ports, nextId).remove(context)); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 619 | xconnectNextObjStore.remove(key); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Revokes bridging forwarding objectives for given XConnect. |
| 624 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 625 | * @param key XConnect store key |
| 626 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 627 | * @param fwdFuture completable future for this forwarding objective operation |
| 628 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 629 | private void revokeFwd(XconnectKey key, int nextId, CompletableFuture<ObjectiveError> fwdFuture) { |
| 630 | ForwardingObjective.Builder fwdObjBuilder = fwdObjBuilder(key, nextId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 631 | ObjectiveContext context = new ObjectiveContext() { |
| 632 | @Override |
| 633 | public void onSuccess(Objective objective) { |
| 634 | log.debug("Previous FwdObj for {} removed", key); |
| 635 | if (fwdFuture != null) { |
| 636 | fwdFuture.complete(null); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | @Override |
| 641 | public void onError(Objective objective, ObjectiveError error) { |
| 642 | log.warn("Failed to remove previous FwdObj for {}: {}", key, error); |
| 643 | if (fwdFuture != null) { |
| 644 | fwdFuture.complete(error); |
| 645 | } |
| 646 | } |
| 647 | }; |
| 648 | flowObjectiveService.forward(key.deviceId(), fwdObjBuilder.remove(context)); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Revokes ACL forwarding objectives for given XConnect. |
| 653 | * |
| 654 | * @param key XConnect store key |
| 655 | */ |
| 656 | private void revokeAcl(XconnectKey key) { |
| 657 | ForwardingObjective.Builder aclObjBuilder = aclObjBuilder(key.vlanId()); |
| 658 | ObjectiveContext aclContext = new DefaultObjectiveContext( |
| 659 | (objective) -> log.debug("XConnect AclObj for {} populated", key), |
| 660 | (objective, error) -> |
| 661 | log.warn("Failed to populate XConnect AclObj for {}: {}", key, error)); |
| 662 | flowObjectiveService.forward(key.deviceId(), aclObjBuilder.remove(aclContext)); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Updates XConnect groups and flows for given key. |
| 667 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 668 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 669 | * @param prevPorts previous XConnect ports |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 670 | * @param ports new XConnect ports |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 671 | */ |
| 672 | private void updateXConnect(XconnectKey key, Set<PortNumber> prevPorts, |
| 673 | Set<PortNumber> ports) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 674 | if (!isLocalLeader(key.deviceId())) { |
| 675 | log.debug("Abort updating XConnect {}: {}", key, ERROR_NOT_LEADER); |
| 676 | return; |
| 677 | } |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 678 | // NOTE: ACL flow doesn't include port information. No need to update it. |
| 679 | // Pair port is built-in and thus not going to change. No need to update it. |
| 680 | |
| 681 | // remove old filter |
| 682 | prevPorts.stream().filter(port -> !ports.contains(port)).forEach(port -> |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 683 | revokeFilter(key, |
| 684 | ImmutableSet.of(port))); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 685 | // install new filter |
| 686 | ports.stream().filter(port -> !prevPorts.contains(port)).forEach(port -> |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 687 | populateFilter(key, |
| 688 | ImmutableSet.of(port))); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 689 | |
| 690 | CompletableFuture<ObjectiveError> fwdFuture = new CompletableFuture<>(); |
| 691 | CompletableFuture<ObjectiveError> nextFuture = new CompletableFuture<>(); |
| 692 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 693 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
| 694 | if (nextId != -1) { |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 695 | revokeFwd(key, nextId, fwdFuture); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 696 | |
| 697 | fwdFuture.thenAcceptAsync(fwdStatus -> { |
| 698 | if (fwdStatus == null) { |
| 699 | log.debug("Fwd removed. Now remove group {}", key); |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 700 | revokeNext(key, prevPorts, nextId, nextFuture); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 701 | } |
| 702 | }); |
| 703 | |
| 704 | nextFuture.thenAcceptAsync(nextStatus -> { |
| 705 | if (nextStatus == null) { |
| 706 | log.debug("Installing new group and flow for {}", key); |
| 707 | populateFwd(key, populateNext(key, ports)); |
| 708 | } |
| 709 | }); |
| 710 | } else { |
| 711 | log.warn("NextObj for {} does not exist in the store.", key); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /** |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 716 | * Creates a next objective builder for XConnect with given nextId. |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 717 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 718 | * @param key XConnect key |
| 719 | * @param ports set of XConnect ports |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 720 | * @param nextId next objective id |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 721 | * @return next objective builder |
| 722 | */ |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 723 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports, int nextId) { |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 724 | TrafficSelector metadata = |
| 725 | DefaultTrafficSelector.builder().matchVlanId(key.vlanId()).build(); |
| 726 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 727 | .builder().withId(nextId) |
| 728 | .withType(NextObjective.Type.BROADCAST).fromApp(appId) |
| 729 | .withMeta(metadata); |
| 730 | ports.forEach(port -> { |
| 731 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 732 | tBuilder.setOutput(port); |
| 733 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 734 | }); |
| 735 | return nextObjBuilder; |
| 736 | } |
| 737 | |
| 738 | /** |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 739 | * Creates a next objective builder for XConnect. |
| 740 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 741 | * @param key XConnect key |
Charles Chan | 263e7d9 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 742 | * @param ports set of XConnect ports |
| 743 | * @return next objective builder |
| 744 | */ |
| 745 | private NextObjective.Builder nextObjBuilder(XconnectKey key, Set<PortNumber> ports) { |
| 746 | int nextId = flowObjectiveService.allocateNextId(); |
| 747 | return nextObjBuilder(key, ports, nextId); |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /** |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 752 | * Creates a bridging forwarding objective builder for XConnect. |
| 753 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 754 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 755 | * @param nextId next ID of the broadcast group for this XConnect key |
| 756 | * @return forwarding objective builder |
| 757 | */ |
| 758 | private ForwardingObjective.Builder fwdObjBuilder(XconnectKey key, int nextId) { |
| 759 | /* |
| 760 | * Driver should treat objectives with MacAddress.NONE and !VlanId.NONE |
| 761 | * as the VLAN cross-connect broadcast rules |
| 762 | */ |
| 763 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 764 | sbuilder.matchVlanId(key.vlanId()); |
| 765 | sbuilder.matchEthDst(MacAddress.NONE); |
| 766 | |
| 767 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 768 | fob.withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 769 | .withSelector(sbuilder.build()) |
| 770 | .nextStep(nextId) |
| 771 | .withPriority(XCONNECT_PRIORITY) |
| 772 | .fromApp(appId) |
| 773 | .makePermanent(); |
| 774 | return fob; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * Creates an ACL forwarding objective builder for XConnect. |
| 779 | * |
| 780 | * @param vlanId cross connect VLAN id |
| 781 | * @return forwarding objective builder |
| 782 | */ |
| 783 | private ForwardingObjective.Builder aclObjBuilder(VlanId vlanId) { |
| 784 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 785 | sbuilder.matchVlanId(vlanId); |
| 786 | |
| 787 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 788 | |
| 789 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder(); |
| 790 | fob.withFlag(ForwardingObjective.Flag.VERSATILE) |
| 791 | .withSelector(sbuilder.build()) |
| 792 | .withTreatment(tbuilder.build()) |
| 793 | .withPriority(XCONNECT_ACL_PRIORITY) |
| 794 | .fromApp(appId) |
| 795 | .makePermanent(); |
| 796 | return fob; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Creates a filtering objective builder for XConnect. |
| 801 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 802 | * @param key XConnect key |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 803 | * @param port XConnect ports |
| 804 | * @return next objective builder |
| 805 | */ |
| 806 | private FilteringObjective.Builder filterObjBuilder(XconnectKey key, PortNumber port) { |
| 807 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 808 | fob.withKey(Criteria.matchInPort(port)) |
| 809 | .addCondition(Criteria.matchVlanId(key.vlanId())) |
| 810 | .addCondition(Criteria.matchEthDst(MacAddress.NONE)) |
| 811 | .withPriority(XCONNECT_PRIORITY); |
| 812 | return fob.permit().fromApp(appId); |
| 813 | } |
| 814 | |
| 815 | /** |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 816 | * 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] | 817 | * |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 818 | * @param deviceId Device ID |
| 819 | * @param port Port details |
| 820 | * @param vlanId VLAN ID |
| 821 | * @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] | 822 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 823 | private void updateL2Flooding(DeviceId deviceId, PortNumber port, VlanId vlanId, boolean install) { |
| 824 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 825 | // Ensure leadership on device |
| 826 | if (!isLocalLeader(deviceId)) { |
| 827 | log.debug("Abort updating L2Flood {}: {}", key, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 828 | return; |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 829 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 830 | |
| 831 | // Locate L2 flooding group details for given xconnect vlan |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 832 | int nextId = Versioned.valueOrElse(xconnectNextObjStore.get(key), -1); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 833 | if (nextId == -1) { |
| 834 | log.debug("XConnect vlan {} broadcast group for device {} doesn't exists. " + |
| 835 | "Aborting pair group linking.", vlanId, deviceId); |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | // Add pairing-port group to flooding group |
| 840 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 841 | // treatment.popVlan(); |
| 842 | treatment.setOutput(port); |
| 843 | ObjectiveContext context = new DefaultObjectiveContext( |
| 844 | (objective) -> |
| 845 | log.debug("Pair port added/removed to vlan {} next objective {} on {}", |
| 846 | vlanId, nextId, deviceId), |
| 847 | (objective, error) -> |
| 848 | log.warn("Failed adding/removing pair port to vlan {} next objective {} on {}." + |
| 849 | "Error : {}", vlanId, nextId, deviceId, error) |
| 850 | ); |
| 851 | NextObjective.Builder vlanNextObjectiveBuilder = DefaultNextObjective.builder() |
| 852 | .withId(nextId) |
| 853 | .withType(NextObjective.Type.BROADCAST) |
| 854 | .fromApp(srService.appId()) |
| 855 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 856 | .addTreatment(treatment.build()); |
| 857 | if (install) { |
| 858 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.addToExisting(context)); |
| 859 | } else { |
| 860 | flowObjectiveService.next(deviceId, vlanNextObjectiveBuilder.removeFromExisting(context)); |
| 861 | } |
| 862 | log.debug("Submitted next objective {} for vlan: {} in device {}", |
| 863 | nextId, vlanId, deviceId); |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 864 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 865 | |
| 866 | /** |
| 867 | * Populate L2 multicast rule on given deviceId that matches given mac, given vlan and |
| 868 | * output to given port's L2 mulitcast group. |
| 869 | * |
| 870 | * @param deviceId Device ID |
| 871 | * @param pairPort Pair port number |
| 872 | * @param vlanId VLAN ID |
| 873 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 874 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 875 | private void populateL2Multicast(DeviceId deviceId, PortNumber pairPort, |
| 876 | VlanId vlanId, List<PortNumber> accessPorts) { |
| 877 | // Ensure enough rights to program pair device |
| 878 | if (!srService.shouldProgram(deviceId)) { |
| 879 | log.debug("Abort populate L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
| 880 | return; |
| 881 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 882 | |
| 883 | boolean multicastGroupExists = true; |
| 884 | int vlanMulticastNextId; |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 885 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 886 | |
| 887 | // Step 1 : Populate single homed access ports into vlan's L2 multicast group |
| 888 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 889 | .builder() |
| 890 | .withType(NextObjective.Type.BROADCAST) |
| 891 | .fromApp(srService.appId()) |
| 892 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId) |
| 893 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 894 | vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 895 | if (vlanMulticastNextId == -1) { |
| 896 | // Vlan's L2 multicast group doesn't exist; create it, update store and add pair port as sub-group |
| 897 | multicastGroupExists = false; |
| 898 | vlanMulticastNextId = flowObjectiveService.allocateNextId(); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 899 | addMulticastGroupNextObjectiveId(key, vlanMulticastNextId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 900 | vlanMulticastNextObjBuilder.addTreatment( |
psneha | 9118a5a | 2019-07-01 05:34:23 -0400 | [diff] [blame^] | 901 | DefaultTrafficTreatment.builder().setOutput(pairPort).build() |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 902 | ); |
| 903 | } |
| 904 | vlanMulticastNextObjBuilder.withId(vlanMulticastNextId); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 905 | int nextId = vlanMulticastNextId; |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 906 | accessPorts.forEach(p -> { |
| 907 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 908 | // Do vlan popup action based on interface configuration |
| 909 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 910 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 911 | egressAction.popVlan(); |
| 912 | } |
| 913 | egressAction.setOutput(p); |
| 914 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 915 | addMulticastGroupPort(key, p); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 916 | }); |
| 917 | ObjectiveContext context = new DefaultObjectiveContext( |
| 918 | (objective) -> |
| 919 | log.debug("L2 multicast group installed/updated. " |
| 920 | + "NextObject Id {} on {} for subnet {} ", |
| 921 | nextId, deviceId, vlanId), |
| 922 | (objective, error) -> |
| 923 | log.warn("L2 multicast group failed to install/update. " |
| 924 | + " NextObject Id {} on {} for subnet {} : {}", |
| 925 | nextId, deviceId, vlanId, error) |
| 926 | ); |
| 927 | if (!multicastGroupExists) { |
| 928 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.add(context)); |
| 929 | |
| 930 | // Step 2 : Populate ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 931 | TrafficSelector.Builder multicastSelector = DefaultTrafficSelector.builder(); |
| 932 | multicastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 933 | multicastSelector.matchInPort(pairPort); |
| 934 | multicastSelector.matchVlanId(vlanId); |
| 935 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 936 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 937 | .nextStep(vlanMulticastNextId) |
| 938 | .withSelector(multicastSelector.build()) |
| 939 | .withPriority(100) |
| 940 | .fromApp(srService.appId()) |
| 941 | .makePermanent(); |
| 942 | context = new DefaultObjectiveContext( |
| 943 | (objective) -> log.debug("L2 multicasting versatile rule for device {}, port/vlan {}/{} populated", |
| 944 | deviceId, |
| 945 | pairPort, |
| 946 | vlanId), |
| 947 | (objective, error) -> log.warn("Failed to populate L2 multicasting versatile rule for device {}, " + |
| 948 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 949 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.add(context)); |
| 950 | } else { |
| 951 | // L2_MULTICAST & BROADCAST are similar structure in subgroups; so going with BROADCAST type. |
| 952 | vlanMulticastNextObjBuilder.withType(NextObjective.Type.BROADCAST); |
| 953 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.addToExisting(context)); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Removes access ports from VLAN L2 multicast group on given deviceId. |
| 959 | * |
| 960 | * @param deviceId Device ID |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 961 | * @param vlanId VLAN ID |
| 962 | * @param accessPorts List of access ports to be added into L2 multicast group |
| 963 | */ |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 964 | private void revokeL2Multicast(DeviceId deviceId, VlanId vlanId, List<PortNumber> accessPorts) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 965 | // Ensure enough rights to program pair device |
| 966 | if (!srService.shouldProgram(deviceId)) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 967 | log.debug("Abort revoke L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 968 | return; |
| 969 | } |
| 970 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 971 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 972 | |
| 973 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 974 | if (vlanMulticastNextId == -1) { |
| 975 | return; |
| 976 | } |
| 977 | NextObjective.Builder vlanMulticastNextObjBuilder = DefaultNextObjective |
| 978 | .builder() |
| 979 | .withType(NextObjective.Type.BROADCAST) |
| 980 | .fromApp(srService.appId()) |
| 981 | .withMeta(DefaultTrafficSelector.builder().matchVlanId(vlanId).build()) |
| 982 | .withId(vlanMulticastNextId); |
| 983 | accessPorts.forEach(p -> { |
| 984 | TrafficTreatment.Builder egressAction = DefaultTrafficTreatment.builder(); |
| 985 | // Do vlan popup action based on interface configuration |
| 986 | if (interfaceService.getInterfacesByPort(new ConnectPoint(deviceId, p)) |
| 987 | .stream().noneMatch(i -> i.vlanTagged().contains(vlanId))) { |
| 988 | egressAction.popVlan(); |
| 989 | } |
| 990 | egressAction.setOutput(p); |
| 991 | vlanMulticastNextObjBuilder.addTreatment(egressAction.build()); |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 992 | removeMulticastGroupPort(key, p); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 993 | }); |
| 994 | ObjectiveContext context = new DefaultObjectiveContext( |
| 995 | (objective) -> |
| 996 | log.debug("L2 multicast group installed/updated. " |
| 997 | + "NextObject Id {} on {} for subnet {} ", |
| 998 | vlanMulticastNextId, deviceId, vlanId), |
| 999 | (objective, error) -> |
| 1000 | log.warn("L2 multicast group failed to install/update. " |
| 1001 | + " NextObject Id {} on {} for subnet {} : {}", |
| 1002 | vlanMulticastNextId, deviceId, vlanId, error) |
| 1003 | ); |
| 1004 | flowObjectiveService.next(deviceId, vlanMulticastNextObjBuilder.removeFromExisting(context)); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Cleans up VLAN L2 multicast group on given deviceId. ACL rules for the group will also be deleted. |
| 1009 | * Normally multicast group is not removed if it contains access ports; which can be forced |
| 1010 | * by "force" flag |
| 1011 | * |
| 1012 | * @param deviceId Device ID |
| 1013 | * @param pairPort Pair port number |
| 1014 | * @param vlanId VLAN ID |
| 1015 | * @param force Forceful removal |
| 1016 | */ |
| 1017 | private void cleanupL2MulticastRule(DeviceId deviceId, PortNumber pairPort, VlanId vlanId, boolean force) { |
| 1018 | |
| 1019 | // Ensure enough rights to program pair device |
| 1020 | if (!srService.shouldProgram(deviceId)) { |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1021 | log.debug("Abort cleanup L2Multicast {}-{}: {}", deviceId, vlanId, ERROR_NOT_LEADER); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1022 | return; |
| 1023 | } |
| 1024 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1025 | VlanNextObjectiveStoreKey key = new VlanNextObjectiveStoreKey(deviceId, vlanId); |
| 1026 | |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1027 | // Ensure L2 multicast group doesn't contain access ports |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1028 | if (hasAccessPortInMulticastGroup(key, pairPort) && !force) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | // Load L2 multicast group details |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1033 | int vlanMulticastNextId = getMulticastGroupNextObjectiveId(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1034 | if (vlanMulticastNextId == -1) { |
| 1035 | return; |
| 1036 | } |
| 1037 | |
| 1038 | // Step 1 : Clear ACL rule; selector = vlan + pair-port, output = vlan L2 multicast group |
| 1039 | TrafficSelector.Builder l2MulticastSelector = DefaultTrafficSelector.builder(); |
| 1040 | l2MulticastSelector.matchEthType(Ethernet.TYPE_VLAN); |
| 1041 | l2MulticastSelector.matchInPort(pairPort); |
| 1042 | l2MulticastSelector.matchVlanId(vlanId); |
| 1043 | ForwardingObjective.Builder vlanMulticastForwardingObj = DefaultForwardingObjective.builder() |
| 1044 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 1045 | .nextStep(vlanMulticastNextId) |
| 1046 | .withSelector(l2MulticastSelector.build()) |
| 1047 | .withPriority(100) |
| 1048 | .fromApp(srService.appId()) |
| 1049 | .makePermanent(); |
| 1050 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1051 | (objective) -> log.debug("L2 multicasting rule for device {}, port/vlan {}/{} deleted", deviceId, |
| 1052 | pairPort, vlanId), |
| 1053 | (objective, error) -> log.warn("Failed to delete L2 multicasting rule for device {}, " + |
| 1054 | "ports/vlan {}/{}: {}", deviceId, pairPort, vlanId, error)); |
| 1055 | flowObjectiveService.forward(deviceId, vlanMulticastForwardingObj.remove(context)); |
| 1056 | |
| 1057 | // Step 2 : Clear L2 multicast group associated with vlan |
| 1058 | NextObjective.Builder l2MulticastGroupBuilder = DefaultNextObjective |
| 1059 | .builder() |
| 1060 | .withId(vlanMulticastNextId) |
| 1061 | .withType(NextObjective.Type.BROADCAST) |
| 1062 | .fromApp(srService.appId()) |
| 1063 | .withMeta(DefaultTrafficSelector.builder() |
| 1064 | .matchVlanId(vlanId) |
| 1065 | .matchEthDst(MacAddress.IPV4_MULTICAST).build()) |
| 1066 | .addTreatment(DefaultTrafficTreatment.builder().popVlan().setOutput(pairPort).build()); |
| 1067 | context = new DefaultObjectiveContext( |
| 1068 | (objective) -> |
| 1069 | log.debug("L2 multicast group with NextObject Id {} deleted on {} for subnet {} ", |
| 1070 | vlanMulticastNextId, deviceId, vlanId), |
| 1071 | (objective, error) -> |
| 1072 | log.warn("L2 multicast group with NextObject Id {} failed to delete on {} for subnet {} : {}", |
| 1073 | vlanMulticastNextId, deviceId, vlanId, error) |
| 1074 | ); |
| 1075 | flowObjectiveService.next(deviceId, l2MulticastGroupBuilder.remove(context)); |
| 1076 | |
| 1077 | // Finally clear store. |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1078 | removeMulticastGroup(key); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1079 | } |
| 1080 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1081 | private int getMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key) { |
| 1082 | return Versioned.valueOrElse(xconnectMulticastNextStore.get(key), -1); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1083 | } |
| 1084 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1085 | private void addMulticastGroupNextObjectiveId(VlanNextObjectiveStoreKey key, int nextId) { |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1086 | if (nextId == -1) { |
| 1087 | return; |
| 1088 | } |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1089 | xconnectMulticastNextStore.put(key, nextId); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1090 | } |
| 1091 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1092 | private void addMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1093 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1094 | if (ports == null) { |
| 1095 | ports = Lists.newArrayList(); |
| 1096 | } |
| 1097 | ports.add(port); |
| 1098 | return ports; |
| 1099 | }); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1100 | } |
| 1101 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1102 | private void removeMulticastGroupPort(VlanNextObjectiveStoreKey groupKey, PortNumber port) { |
| 1103 | xconnectMulticastPortsStore.compute(groupKey, (key, ports) -> { |
| 1104 | if (ports != null && !ports.isEmpty()) { |
| 1105 | ports.remove(port); |
| 1106 | } |
| 1107 | return ports; |
| 1108 | }); |
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 void removeMulticastGroup(VlanNextObjectiveStoreKey groupKey) { |
| 1112 | xconnectMulticastPortsStore.remove(groupKey); |
| 1113 | xconnectMulticastNextStore.remove(groupKey); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1114 | } |
| 1115 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1116 | private boolean hasAccessPortInMulticastGroup(VlanNextObjectiveStoreKey groupKey, PortNumber pairPort) { |
| 1117 | List<PortNumber> ports = Versioned.valueOrElse(xconnectMulticastPortsStore.get(groupKey), ImmutableList.of()); |
jayakumarthazhath | 46945b6 | 2018-10-01 00:51:54 +0530 | [diff] [blame] | 1118 | return ports.stream().anyMatch(p -> !p.equals(pairPort)); |
| 1119 | } |
| 1120 | |
pier | 6c48dba | 2018-11-27 11:23:50 -0800 | [diff] [blame] | 1121 | // Custom-built function, when the device is not available we need a fallback mechanism |
| 1122 | private boolean isLocalLeader(DeviceId deviceId) { |
| 1123 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1124 | // When the device is available we just check the mastership |
| 1125 | if (deviceService.isAvailable(deviceId)) { |
| 1126 | return false; |
| 1127 | } |
| 1128 | // Fallback with Leadership service - device id is used as topic |
| 1129 | NodeId leader = leadershipService.runForLeadership( |
| 1130 | deviceId.toString()).leaderNodeId(); |
| 1131 | // Verify if this node is the leader |
| 1132 | return clusterService.getLocalNode().id().equals(leader); |
| 1133 | } |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
Charles Chan | 4e87b3e | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1137 | } |