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