Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1 | /* |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 17 | package org.onosproject.segmentrouting.mcast; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 18 | |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalCause; |
| 22 | import com.google.common.cache.RemovalNotification; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.collect.Lists; |
Pier Luigi | bad6d6c | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Sets; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onlab.util.KryoNamespace; |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 33 | import org.onosproject.cluster.NodeId; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Ray Milkey | 6c1f0f0 | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.basics.McastConfig; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.Path; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.flow.criteria.Criteria; |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 49 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 53 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 54 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 55 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 56 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 57 | import org.onosproject.net.mcast.McastEvent; |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 58 | import org.onosproject.net.mcast.McastRoute; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 59 | import org.onosproject.net.mcast.McastRouteInfo; |
Pier Luigi | 83f919b | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 60 | import org.onosproject.net.topology.Topology; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 61 | import org.onosproject.net.topology.TopologyService; |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 62 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 63 | import org.onosproject.segmentrouting.SegmentRoutingService; |
Pier Luigi | f7049c5 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 64 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 65 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 66 | import org.onosproject.segmentrouting.storekey.McastStoreKey; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 67 | import org.onosproject.store.serializers.KryoNamespaces; |
| 68 | import org.onosproject.store.service.ConsistentMap; |
| 69 | import org.onosproject.store.service.Serializer; |
| 70 | import org.onosproject.store.service.StorageService; |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 71 | import org.onosproject.store.service.Versioned; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 75 | import java.time.Instant; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 76 | import java.util.Collection; |
| 77 | import java.util.Collections; |
Pier Luigi | bad6d6c | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 78 | import java.util.Comparator; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 79 | import java.util.List; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 80 | import java.util.Map; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 81 | import java.util.Optional; |
| 82 | import java.util.Set; |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 83 | import java.util.concurrent.ScheduledExecutorService; |
| 84 | import java.util.concurrent.TimeUnit; |
| 85 | import java.util.concurrent.locks.Lock; |
| 86 | import java.util.concurrent.locks.ReentrantLock; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 87 | import java.util.stream.Collectors; |
| 88 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 89 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 90 | import static org.onlab.util.Tools.groupedThreads; |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 91 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
| 92 | import static org.onosproject.net.mcast.McastEvent.Type.ROUTE_REMOVED; |
| 93 | import static org.onosproject.net.mcast.McastEvent.Type.SOURCE_ADDED; |
| 94 | import static org.onosproject.net.mcast.McastEvent.Type.SOURCE_UPDATED; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 95 | import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN; |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 96 | import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS; |
| 97 | import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS; |
| 98 | import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 101 | * Handles Multicast related events. |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 102 | */ |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 103 | public class McastHandler { |
| 104 | private static final Logger log = LoggerFactory.getLogger(McastHandler.class); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 105 | private final SegmentRoutingManager srManager; |
| 106 | private final ApplicationId coreAppId; |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 107 | private final StorageService storageService; |
| 108 | private final TopologyService topologyService; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 109 | private final ConsistentMap<McastStoreKey, NextObjective> mcastNextObjStore; |
| 110 | private final KryoNamespace.Builder mcastKryo; |
| 111 | private final ConsistentMap<McastStoreKey, McastRole> mcastRoleStore; |
| 112 | |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 113 | // Wait time for the cache |
| 114 | private static final int WAIT_TIME_MS = 1000; |
| 115 | /** |
| 116 | * The mcastEventCache is implemented to avoid race condition by giving more time to the |
| 117 | * underlying subsystems to process previous calls. |
| 118 | */ |
| 119 | private Cache<McastCacheKey, McastEvent> mcastEventCache = CacheBuilder.newBuilder() |
| 120 | .expireAfterWrite(WAIT_TIME_MS, TimeUnit.MILLISECONDS) |
| 121 | .removalListener((RemovalNotification<McastCacheKey, McastEvent> notification) -> { |
| 122 | // Get group ip, sink and related event |
| 123 | IpAddress mcastIp = notification.getKey().mcastIp(); |
| 124 | ConnectPoint sink = notification.getKey().sink(); |
| 125 | McastEvent mcastEvent = notification.getValue(); |
| 126 | RemovalCause cause = notification.getCause(); |
| 127 | log.debug("mcastEventCache removal event. group={}, sink={}, mcastEvent={}, cause={}", |
| 128 | mcastIp, sink, mcastEvent, cause); |
| 129 | // If it expires or it has been replaced, we deque the event |
| 130 | switch (notification.getCause()) { |
| 131 | case REPLACED: |
| 132 | case EXPIRED: |
| 133 | dequeueMcastEvent(mcastEvent); |
| 134 | break; |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | }).build(); |
| 139 | |
| 140 | private void enqueueMcastEvent(McastEvent mcastEvent) { |
| 141 | log.debug("Enqueue mcastEvent {}", mcastEvent); |
| 142 | final McastRouteInfo mcastRouteInfo = mcastEvent.subject(); |
| 143 | // Let's create the keys of the cache |
| 144 | ImmutableSet.Builder<ConnectPoint> sinksBuilder = ImmutableSet.builder(); |
| 145 | // For this event we will have a set of sinks |
| 146 | if (mcastEvent.type() == SOURCE_ADDED || |
| 147 | mcastEvent.type() == SOURCE_UPDATED || |
| 148 | mcastEvent.type() == ROUTE_REMOVED) { |
| 149 | // Add all the sinks |
| 150 | sinksBuilder.addAll(mcastRouteInfo.sinks()); |
| 151 | } else { |
| 152 | // We have just one sink in this case |
| 153 | ConnectPoint sink = mcastRouteInfo.sink().orElse(null); |
| 154 | // It is always true, unless something of bad happened |
| 155 | // in the mcast route store |
| 156 | if (sink != null) { |
| 157 | sinksBuilder.add(sink); |
| 158 | } |
| 159 | } |
| 160 | // Push the elements in the cache |
| 161 | sinksBuilder.build().forEach(sink -> { |
| 162 | McastCacheKey cacheKey = new McastCacheKey(mcastRouteInfo.route().group(), |
| 163 | sink); |
| 164 | mcastEventCache.put(cacheKey, mcastEvent); |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | private void dequeueMcastEvent(McastEvent mcastEvent) { |
| 169 | log.debug("Dequeue mcastEvent {}", mcastEvent); |
| 170 | final McastRouteInfo mcastRouteInfo = mcastEvent.subject(); |
| 171 | // Get source, mcast group |
| 172 | ConnectPoint source = mcastRouteInfo.source().orElse(null); |
| 173 | IpAddress mcastIp = mcastRouteInfo.route().group(); |
| 174 | // According to the event type let's call the proper method |
| 175 | switch (mcastEvent.type()) { |
| 176 | case SOURCE_ADDED: |
| 177 | // Get all the sinks and process |
| 178 | Set<ConnectPoint> sinks = mcastRouteInfo.sinks(); |
| 179 | sinks.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp)); |
| 180 | break; |
| 181 | case SOURCE_UPDATED: |
| 182 | // Get old source |
| 183 | ConnectPoint oldSource = mcastEvent.prevSubject().source().orElse(null); |
| 184 | // Just the first cached element will be processed |
| 185 | processSourceUpdatedInternal(mcastIp, source, oldSource); |
| 186 | break; |
| 187 | case ROUTE_REMOVED: |
| 188 | // Process the route removed, just the first cached element will be processed |
| 189 | processRouteRemovedInternal(source, mcastIp); |
| 190 | break; |
| 191 | case SINK_ADDED: |
| 192 | // Get the only sink and process |
| 193 | ConnectPoint sink = mcastRouteInfo.sink().orElse(null); |
| 194 | processSinkAddedInternal(source, sink, mcastIp); |
| 195 | break; |
| 196 | case SINK_REMOVED: |
| 197 | sink = mcastRouteInfo.sink().orElse(null); |
| 198 | processSinkRemovedInternal(source, sink, mcastIp); |
| 199 | break; |
| 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 205 | // Mcast lock to serialize local operations |
| 206 | private final Lock mcastLock = new ReentrantLock(); |
| 207 | |
| 208 | /** |
| 209 | * Acquires the lock used when making mcast changes. |
| 210 | */ |
| 211 | private void mcastLock() { |
| 212 | mcastLock.lock(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Releases the lock used when making mcast changes. |
| 217 | */ |
| 218 | private void mcastUnlock() { |
| 219 | mcastLock.unlock(); |
| 220 | } |
| 221 | |
| 222 | // Stability threshold for Mcast. Seconds |
| 223 | private static final long MCAST_STABLITY_THRESHOLD = 5; |
| 224 | // Last change done |
| 225 | private Instant lastMcastChange = Instant.now(); |
| 226 | |
| 227 | /** |
| 228 | * Determines if mcast in the network has been stable in the last |
| 229 | * MCAST_STABLITY_THRESHOLD seconds, by comparing the current time |
| 230 | * to the last mcast change timestamp. |
| 231 | * |
| 232 | * @return true if stable |
| 233 | */ |
| 234 | private boolean isMcastStable() { |
| 235 | long last = (long) (lastMcastChange.toEpochMilli() / 1000.0); |
| 236 | long now = (long) (Instant.now().toEpochMilli() / 1000.0); |
Saurav Das | a402038 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 237 | log.trace("Mcast stable since {}s", now - last); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 238 | return (now - last) > MCAST_STABLITY_THRESHOLD; |
| 239 | } |
| 240 | |
| 241 | // Verify interval for Mcast |
| 242 | private static final long MCAST_VERIFY_INTERVAL = 30; |
| 243 | |
| 244 | // Executor for mcast bucket corrector |
| 245 | private ScheduledExecutorService executorService |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 246 | = newScheduledThreadPool(1, groupedThreads("mcastWorker", "mcastWorker-%d", log)); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 247 | |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 248 | /** |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 249 | * Constructs the McastEventHandler. |
| 250 | * |
| 251 | * @param srManager Segment Routing manager |
| 252 | */ |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 253 | public McastHandler(SegmentRoutingManager srManager) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 254 | coreAppId = srManager.coreService.getAppId(CoreService.CORE_APP_NAME); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 255 | this.srManager = srManager; |
| 256 | this.storageService = srManager.storageService; |
| 257 | this.topologyService = srManager.topologyService; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 258 | mcastKryo = new KryoNamespace.Builder() |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 259 | .register(KryoNamespaces.API) |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 260 | .register(McastStoreKey.class) |
| 261 | .register(McastRole.class); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 262 | mcastNextObjStore = storageService |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 263 | .<McastStoreKey, NextObjective>consistentMapBuilder() |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 264 | .withName("onos-mcast-nextobj-store") |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 265 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-NextObj"))) |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 266 | .build(); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 267 | mcastRoleStore = storageService |
| 268 | .<McastStoreKey, McastRole>consistentMapBuilder() |
| 269 | .withName("onos-mcast-role-store") |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 270 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-Role"))) |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 271 | .build(); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 272 | // Init the executor service and the buckets corrector |
| 273 | executorService.scheduleWithFixedDelay(new McastBucketCorrector(), 10, |
| 274 | MCAST_VERIFY_INTERVAL, |
| 275 | TimeUnit.SECONDS); |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 276 | // Schedule the clean up, this will allow the processing of the expired events |
| 277 | executorService.scheduleAtFixedRate(mcastEventCache::cleanUp, 0, |
| 278 | WAIT_TIME_MS, TimeUnit.MILLISECONDS); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Read initial multicast from mcast store. |
| 283 | */ |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 284 | public void init() { |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 285 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
| 286 | ConnectPoint source = srManager.multicastRouteService.fetchSource(mcastRoute); |
| 287 | Set<ConnectPoint> sinks = srManager.multicastRouteService.fetchSinks(mcastRoute); |
| 288 | sinks.forEach(sink -> { |
| 289 | processSinkAddedInternal(source, sink, mcastRoute.group()); |
| 290 | }); |
| 291 | }); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 295 | * Clean up when deactivating the application. |
| 296 | */ |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 297 | public void terminate() { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 298 | executorService.shutdown(); |
| 299 | } |
| 300 | |
| 301 | /** |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 302 | * Processes the SOURCE_ADDED, SOURCE_UPDATED, SINK_ADDED, |
| 303 | * SINK_REMOVED and ROUTE_REMOVED events. |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 304 | * |
| 305 | * @param event McastEvent with SOURCE_ADDED type |
| 306 | */ |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 307 | public void processMcastEvent(McastEvent event) { |
| 308 | log.info("process {}", event); |
| 309 | // Verify if it is a complete event |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 310 | McastRouteInfo mcastRouteInfo = event.subject(); |
| 311 | if (!mcastRouteInfo.isComplete()) { |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 312 | log.info("Incompleted McastRouteInfo. Abort {}", event.type()); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 313 | return; |
| 314 | } |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 315 | // Just enqueue for now |
| 316 | enqueueMcastEvent(event); |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 320 | * Process the SOURCE_UPDATED event. |
| 321 | * |
| 322 | * @param newSource the updated srouce info |
| 323 | * @param oldSource the outdated source info |
| 324 | */ |
| 325 | private void processSourceUpdatedInternal(IpAddress mcastIp, |
| 326 | ConnectPoint newSource, |
| 327 | ConnectPoint oldSource) { |
| 328 | lastMcastChange = Instant.now(); |
| 329 | mcastLock(); |
| 330 | try { |
| 331 | log.debug("Processing source updated for group {}", mcastIp); |
| 332 | |
| 333 | // Build key for the store and retrieve old data |
| 334 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, oldSource.deviceId()); |
| 335 | |
| 336 | // Verify leadership on the operation |
| 337 | if (!isLeader(oldSource)) { |
| 338 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // This device is not serving this multicast group |
| 343 | if (!mcastRoleStore.containsKey(mcastStoreKey) || |
| 344 | !mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 345 | log.warn("{} is not serving {}. Abort.", oldSource.deviceId(), mcastIp); |
| 346 | return; |
| 347 | } |
| 348 | NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value(); |
| 349 | Set<PortNumber> outputPorts = getPorts(nextObjective.next()); |
| 350 | |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 351 | // This an optimization to avoid unnecessary removal and add |
| 352 | if (!assignedVlanFromNext(nextObjective).equals(assignedVlan(newSource))) { |
| 353 | // Let's remove old flows and groups |
| 354 | removeGroupFromDevice(oldSource.deviceId(), mcastIp, assignedVlan(oldSource)); |
| 355 | // Push new flows and group |
| 356 | outputPorts.forEach(portNumber -> addPortToDevice(newSource.deviceId(), portNumber, |
| 357 | mcastIp, assignedVlan(newSource))); |
| 358 | } |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 359 | addFilterToDevice(newSource.deviceId(), newSource.port(), |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 360 | assignedVlan(newSource), mcastIp, INGRESS); |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 361 | // Setup mcast roles |
| 362 | mcastRoleStore.put(new McastStoreKey(mcastIp, newSource.deviceId()), |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 363 | INGRESS); |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 364 | } finally { |
| 365 | mcastUnlock(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 370 | * Removes the entire mcast tree related to this group. |
| 371 | * |
| 372 | * @param mcastIp multicast group IP address |
| 373 | */ |
| 374 | private void processRouteRemovedInternal(ConnectPoint source, IpAddress mcastIp) { |
| 375 | lastMcastChange = Instant.now(); |
| 376 | mcastLock(); |
| 377 | try { |
Pier Luigi | 57d4179 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 378 | log.debug("Processing route removed for group {}", mcastIp); |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 379 | |
| 380 | // Find out the ingress, transit and egress device of the affected group |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 381 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 382 | .stream().findAny().orElse(null); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 383 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 384 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 385 | |
| 386 | // Verify leadership on the operation |
| 387 | if (!isLeader(source)) { |
| 388 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 389 | return; |
| 390 | } |
| 391 | |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 392 | // If there are no egress devices, sinks could be only on the ingress |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 393 | if (!egressDevices.isEmpty()) { |
| 394 | egressDevices.forEach( |
| 395 | deviceId -> removeGroupFromDevice(deviceId, mcastIp, assignedVlan(null)) |
| 396 | ); |
| 397 | } |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 398 | // Transit could be empty if sinks are on the ingress |
| 399 | if (!transitDevices.isEmpty()) { |
| 400 | transitDevices.forEach( |
| 401 | deviceId -> removeGroupFromDevice(deviceId, mcastIp, assignedVlan(null)) |
| 402 | ); |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 403 | } |
| 404 | // Ingress device should be not null |
| 405 | if (ingressDevice != null) { |
| 406 | removeGroupFromDevice(ingressDevice, mcastIp, assignedVlan(source)); |
| 407 | } |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 408 | } finally { |
| 409 | mcastUnlock(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 414 | * Removes a path from source to sink for given multicast group. |
| 415 | * |
| 416 | * @param source connect point of the multicast source |
| 417 | * @param sink connection point of the multicast sink |
| 418 | * @param mcastIp multicast group IP address |
| 419 | */ |
| 420 | private void processSinkRemovedInternal(ConnectPoint source, ConnectPoint sink, |
| 421 | IpAddress mcastIp) { |
| 422 | lastMcastChange = Instant.now(); |
| 423 | mcastLock(); |
| 424 | try { |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 425 | // Verify leadership on the operation |
| 426 | if (!isLeader(source)) { |
| 427 | log.debug("Skip {} due to lack of leadership", mcastIp); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 428 | return; |
| 429 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 430 | |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 431 | boolean isLast = false; |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 432 | // When source and sink are on the same device |
| 433 | if (source.deviceId().equals(sink.deviceId())) { |
| 434 | // Source and sink are on even the same port. There must be something wrong. |
| 435 | if (source.port().equals(sink.port())) { |
| 436 | log.warn("Skip {} since sink {} is on the same port of source {}. Abort", |
| 437 | mcastIp, sink, source); |
| 438 | return; |
| 439 | } |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 440 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, assignedVlan(source)); |
| 441 | if (isLast) { |
| 442 | mcastRoleStore.remove(new McastStoreKey(mcastIp, sink.deviceId())); |
| 443 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 446 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 447 | // Process the egress device |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 448 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, assignedVlan(null)); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 449 | if (isLast) { |
| 450 | mcastRoleStore.remove(new McastStoreKey(mcastIp, sink.deviceId())); |
| 451 | } |
| 452 | |
| 453 | // If this is the last sink on the device, also update upstream |
| 454 | Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(), mcastIp); |
| 455 | if (mcastPath.isPresent()) { |
| 456 | List<Link> links = Lists.newArrayList(mcastPath.get().links()); |
| 457 | Collections.reverse(links); |
| 458 | for (Link link : links) { |
| 459 | if (isLast) { |
| 460 | isLast = removePortFromDevice( |
| 461 | link.src().deviceId(), |
| 462 | link.src().port(), |
| 463 | mcastIp, |
| 464 | assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null) |
| 465 | ); |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 466 | if (isLast) { |
| 467 | mcastRoleStore.remove(new McastStoreKey(mcastIp, link.src().deviceId())); |
| 468 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 469 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 472 | } finally { |
| 473 | mcastUnlock(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Establishes a path from source to sink for given multicast group. |
| 479 | * |
| 480 | * @param source connect point of the multicast source |
| 481 | * @param sink connection point of the multicast sink |
| 482 | * @param mcastIp multicast group IP address |
| 483 | */ |
| 484 | private void processSinkAddedInternal(ConnectPoint source, ConnectPoint sink, |
| 485 | IpAddress mcastIp) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 486 | lastMcastChange = Instant.now(); |
| 487 | mcastLock(); |
| 488 | try { |
| 489 | // Continue only when this instance is the master of source device |
| 490 | if (!srManager.mastershipService.isLocalMaster(source.deviceId())) { |
| 491 | log.debug("Skip {} due to lack of mastership of the source device {}", |
| 492 | mcastIp, source.deviceId()); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 495 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 496 | // Process the ingress device |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 497 | addFilterToDevice(source.deviceId(), source.port(), |
| 498 | assignedVlan(source), mcastIp, INGRESS); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 499 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 500 | // When source and sink are on the same device |
| 501 | if (source.deviceId().equals(sink.deviceId())) { |
| 502 | // Source and sink are on even the same port. There must be something wrong. |
| 503 | if (source.port().equals(sink.port())) { |
| 504 | log.warn("Skip {} since sink {} is on the same port of source {}. Abort", |
| 505 | mcastIp, sink, source); |
| 506 | return; |
| 507 | } |
| 508 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, assignedVlan(source)); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 509 | mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()), INGRESS); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 510 | return; |
| 511 | } |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 512 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 513 | // Find a path. If present, create/update groups and flows for each hop |
| 514 | Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(), mcastIp); |
| 515 | if (mcastPath.isPresent()) { |
| 516 | List<Link> links = mcastPath.get().links(); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 517 | |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 518 | // Setup mcast role for ingress |
| 519 | mcastRoleStore.put(new McastStoreKey(mcastIp, source.deviceId()), |
| 520 | INGRESS); |
| 521 | |
| 522 | // Setup properly the transit |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 523 | links.forEach(link -> { |
| 524 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
| 525 | assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null)); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 526 | addFilterToDevice(link.dst().deviceId(), link.dst().port(), |
| 527 | assignedVlan(null), mcastIp, null); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 528 | }); |
| 529 | |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 530 | // Setup mcast role for the transit |
| 531 | links.stream() |
| 532 | .filter(link -> !link.dst().deviceId().equals(sink.deviceId())) |
| 533 | .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.dst().deviceId()), |
| 534 | TRANSIT)); |
| 535 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 536 | // Process the egress device |
| 537 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, assignedVlan(null)); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 538 | // Setup mcast role for egress |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 539 | mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()), |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 540 | EGRESS); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 541 | } else { |
| 542 | log.warn("Unable to find a path from {} to {}. Abort sinkAdded", |
| 543 | source.deviceId(), sink.deviceId()); |
| 544 | } |
| 545 | } finally { |
| 546 | mcastUnlock(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
| 550 | /** |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 551 | * Processes the LINK_DOWN event. |
| 552 | * |
| 553 | * @param affectedLink Link that is going down |
| 554 | */ |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 555 | public void processLinkDown(Link affectedLink) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 556 | lastMcastChange = Instant.now(); |
| 557 | mcastLock(); |
| 558 | try { |
| 559 | // Get groups affected by the link down event |
| 560 | getAffectedGroups(affectedLink).forEach(mcastIp -> { |
| 561 | // TODO Optimize when the group editing is in place |
| 562 | log.debug("Processing link down {} for group {}", |
| 563 | affectedLink, mcastIp); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 564 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 565 | // Find out the ingress, transit and egress device of affected group |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 566 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 567 | .stream().findAny().orElse(null); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 568 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 569 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 570 | ConnectPoint source = getSource(mcastIp); |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 571 | |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 572 | // Do not proceed if ingress device or source of this group are missing |
| 573 | // If sinks are in other leafs, we have ingress, transit, egress, and source |
| 574 | // If sinks are in the same leaf, we have just ingress and source |
| 575 | if (ingressDevice == null || source == null) { |
| 576 | log.warn("Missing ingress {} or source {} for group {}", |
| 577 | ingressDevice, source, mcastIp); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 578 | return; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 579 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 580 | |
| 581 | // Continue only when this instance is the master of source device |
| 582 | if (!srManager.mastershipService.isLocalMaster(source.deviceId())) { |
| 583 | log.debug("Skip {} due to lack of mastership of the source device {}", |
| 584 | source.deviceId()); |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | // Remove entire transit |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 589 | transitDevices.forEach(transitDevice -> |
| 590 | removeGroupFromDevice(transitDevice, mcastIp, assignedVlan(null))); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 591 | |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 592 | // Remove transit-facing ports on the ingress device |
| 593 | removeIngressTransitPorts(mcastIp, ingressDevice, source); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 594 | |
| 595 | // Construct a new path for each egress device |
| 596 | egressDevices.forEach(egressDevice -> { |
| 597 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, mcastIp); |
| 598 | if (mcastPath.isPresent()) { |
| 599 | installPath(mcastIp, source, mcastPath.get()); |
| 600 | } else { |
| 601 | log.warn("Fail to recover egress device {} from link failure {}", |
| 602 | egressDevice, affectedLink); |
| 603 | removeGroupFromDevice(egressDevice, mcastIp, assignedVlan(null)); |
| 604 | } |
| 605 | }); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 606 | }); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 607 | } finally { |
| 608 | mcastUnlock(); |
| 609 | } |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 613 | * Process the DEVICE_DOWN event. |
| 614 | * |
| 615 | * @param deviceDown device going down |
| 616 | */ |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 617 | public void processDeviceDown(DeviceId deviceDown) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 618 | lastMcastChange = Instant.now(); |
| 619 | mcastLock(); |
| 620 | try { |
| 621 | // Get the mcast groups affected by the device going down |
| 622 | getAffectedGroups(deviceDown).forEach(mcastIp -> { |
| 623 | // TODO Optimize when the group editing is in place |
| 624 | log.debug("Processing device down {} for group {}", |
| 625 | deviceDown, mcastIp); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 626 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 627 | // Find out the ingress, transit and egress device of affected group |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 628 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 629 | .stream().findAny().orElse(null); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 630 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 631 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 632 | ConnectPoint source = getSource(mcastIp); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 633 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 634 | // Do not proceed if ingress device or source of this group are missing |
| 635 | // If sinks are in other leafs, we have ingress, transit, egress, and source |
| 636 | // If sinks are in the same leaf, we have just ingress and source |
| 637 | if (ingressDevice == null || source == null) { |
| 638 | log.warn("Missing ingress {} or source {} for group {}", |
| 639 | ingressDevice, source, mcastIp); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 640 | return; |
| 641 | } |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 642 | |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 643 | // Verify leadership on the operation |
| 644 | if (!isLeader(source)) { |
| 645 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 646 | return; |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 647 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 648 | |
| 649 | // If it exists, we have to remove it in any case |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 650 | if (!transitDevices.isEmpty()) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 651 | // Remove entire transit |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 652 | transitDevices.forEach(transitDevice -> |
| 653 | removeGroupFromDevice(transitDevice, mcastIp, assignedVlan(null))); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 654 | } |
| 655 | // If the ingress is down |
| 656 | if (ingressDevice.equals(deviceDown)) { |
| 657 | // Remove entire ingress |
| 658 | removeGroupFromDevice(ingressDevice, mcastIp, assignedVlan(source)); |
| 659 | // If other sinks different from the ingress exist |
| 660 | if (!egressDevices.isEmpty()) { |
| 661 | // Remove all the remaining egress |
| 662 | egressDevices.forEach( |
| 663 | egressDevice -> removeGroupFromDevice(egressDevice, mcastIp, assignedVlan(null)) |
| 664 | ); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 665 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 666 | } else { |
| 667 | // Egress or transit could be down at this point |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 668 | // Get the ingress-transit ports if they exist |
| 669 | removeIngressTransitPorts(mcastIp, ingressDevice, source); |
| 670 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 671 | // One of the egress device is down |
| 672 | if (egressDevices.contains(deviceDown)) { |
| 673 | // Remove entire device down |
| 674 | removeGroupFromDevice(deviceDown, mcastIp, assignedVlan(null)); |
| 675 | // Remove the device down from egress |
| 676 | egressDevices.remove(deviceDown); |
| 677 | // If there are no more egress and ingress does not have sinks |
| 678 | if (egressDevices.isEmpty() && !hasSinks(ingressDevice, mcastIp)) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 679 | // We have done |
| 680 | return; |
| 681 | } |
| 682 | } |
| 683 | // Construct a new path for each egress device |
| 684 | egressDevices.forEach(egressDevice -> { |
| 685 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, mcastIp); |
| 686 | // If there is a new path |
| 687 | if (mcastPath.isPresent()) { |
| 688 | // Let's install the new mcast path for this egress |
| 689 | installPath(mcastIp, source, mcastPath.get()); |
| 690 | } else { |
| 691 | // We were not able to find an alternative path for this egress |
| 692 | log.warn("Fail to recover egress device {} from device down {}", |
| 693 | egressDevice, deviceDown); |
| 694 | removeGroupFromDevice(egressDevice, mcastIp, assignedVlan(null)); |
| 695 | } |
| 696 | }); |
| 697 | } |
| 698 | }); |
| 699 | } finally { |
| 700 | mcastUnlock(); |
| 701 | } |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 705 | * Utility method to remove all the ingress transit ports. |
| 706 | * |
| 707 | * @param mcastIp the group ip |
| 708 | * @param ingressDevice the ingress device for this group |
| 709 | * @param source the source connect point |
| 710 | */ |
| 711 | private void removeIngressTransitPorts(IpAddress mcastIp, DeviceId ingressDevice, |
| 712 | ConnectPoint source) { |
| 713 | Set<PortNumber> ingressTransitPorts = ingressTransitPort(mcastIp); |
| 714 | ingressTransitPorts.forEach(ingressTransitPort -> { |
| 715 | if (ingressTransitPort != null) { |
| 716 | boolean isLast = removePortFromDevice(ingressDevice, ingressTransitPort, |
| 717 | mcastIp, assignedVlan(source)); |
| 718 | if (isLast) { |
| 719 | mcastRoleStore.remove(new McastStoreKey(mcastIp, ingressDevice)); |
| 720 | } |
| 721 | } |
| 722 | }); |
| 723 | } |
| 724 | |
| 725 | /** |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 726 | * Adds filtering objective for given device and port. |
| 727 | * |
| 728 | * @param deviceId device ID |
| 729 | * @param port ingress port number |
| 730 | * @param assignedVlan assigned VLAN ID |
| 731 | */ |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 732 | private void addFilterToDevice(DeviceId deviceId, PortNumber port, |
| 733 | VlanId assignedVlan, IpAddress mcastIp, McastRole mcastRole) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 734 | // Do nothing if the port is configured as suppressed |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 735 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 736 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 737 | .getConfig(srManager.appId(), SegmentRoutingAppConfig.class); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 738 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
| 739 | log.info("Ignore suppressed port {}", connectPoint); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
Charles Chan | 50d900c | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 743 | MacAddress routerMac; |
| 744 | try { |
| 745 | routerMac = srManager.deviceConfiguration().getDeviceMac(deviceId); |
| 746 | } catch (DeviceConfigNotFoundException dcnfe) { |
| 747 | log.warn("Fail to push filtering objective since device is not configured. Abort"); |
| 748 | return; |
| 749 | } |
| 750 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 751 | FilteringObjective.Builder filtObjBuilder = |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 752 | filterObjBuilder(port, assignedVlan, mcastIp, routerMac, mcastRole); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 753 | ObjectiveContext context = new DefaultObjectiveContext( |
| 754 | (objective) -> log.debug("Successfully add filter on {}/{}, vlan {}", |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 755 | deviceId, port.toLong(), assignedVlan), |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 756 | (objective, error) -> |
| 757 | log.warn("Failed to add filter on {}/{}, vlan {}: {}", |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 758 | deviceId, port.toLong(), assignedVlan, error)); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 759 | srManager.flowObjectiveService.filter(deviceId, filtObjBuilder.add(context)); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Adds a port to given multicast group on given device. This involves the |
| 764 | * update of L3 multicast group and multicast routing table entry. |
| 765 | * |
| 766 | * @param deviceId device ID |
| 767 | * @param port port to be added |
| 768 | * @param mcastIp multicast group |
| 769 | * @param assignedVlan assigned VLAN ID |
| 770 | */ |
| 771 | private void addPortToDevice(DeviceId deviceId, PortNumber port, |
| 772 | IpAddress mcastIp, VlanId assignedVlan) { |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 773 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 774 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 775 | NextObjective newNextObj; |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 776 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 777 | // First time someone request this mcast group via this device |
| 778 | portBuilder.add(port); |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 779 | // New nextObj |
| 780 | newNextObj = nextObjBuilder(mcastIp, assignedVlan, |
| 781 | portBuilder.build(), null).add(); |
| 782 | // Store the new port |
| 783 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 784 | } else { |
| 785 | // This device already serves some subscribers of this mcast group |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 786 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 787 | // Stop if the port is already in the nextobj |
| 788 | Set<PortNumber> existingPorts = getPorts(nextObj.next()); |
| 789 | if (existingPorts.contains(port)) { |
| 790 | log.info("NextObj for {}/{} already exists. Abort", deviceId, port); |
| 791 | return; |
| 792 | } |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 793 | // Let's add the port and reuse the previous one |
Yuta HIGUCHI | 0eb68e1 | 2018-02-09 18:05:23 -0800 | [diff] [blame] | 794 | portBuilder.addAll(existingPorts).add(port); |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 795 | // Reuse previous nextObj |
| 796 | newNextObj = nextObjBuilder(mcastIp, assignedVlan, |
| 797 | portBuilder.build(), nextObj.id()).addToExisting(); |
| 798 | // Store the final next objective and send only the difference to the driver |
| 799 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
| 800 | // Add just the new port |
| 801 | portBuilder = ImmutableSet.builder(); |
| 802 | portBuilder.add(port); |
| 803 | newNextObj = nextObjBuilder(mcastIp, assignedVlan, |
| 804 | portBuilder.build(), nextObj.id()).addToExisting(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 805 | } |
| 806 | // Create, store and apply the new nextObj and fwdObj |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 807 | ObjectiveContext context = new DefaultObjectiveContext( |
| 808 | (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}", |
| 809 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 810 | (objective, error) -> |
| 811 | log.warn("Failed to add {} on {}/{}, vlan {}: {}", |
| 812 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 813 | ForwardingObjective fwdObj = |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 814 | fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 815 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 816 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Removes a port from given multicast group on given device. |
| 821 | * This involves the update of L3 multicast group and multicast routing |
| 822 | * table entry. |
| 823 | * |
| 824 | * @param deviceId device ID |
| 825 | * @param port port to be added |
| 826 | * @param mcastIp multicast group |
| 827 | * @param assignedVlan assigned VLAN ID |
| 828 | * @return true if this is the last sink on this device |
| 829 | */ |
| 830 | private boolean removePortFromDevice(DeviceId deviceId, PortNumber port, |
| 831 | IpAddress mcastIp, VlanId assignedVlan) { |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 832 | McastStoreKey mcastStoreKey = |
| 833 | new McastStoreKey(mcastIp, deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 834 | // This device is not serving this multicast group |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 835 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 836 | log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port); |
| 837 | return false; |
| 838 | } |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 839 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 840 | |
| 841 | Set<PortNumber> existingPorts = getPorts(nextObj.next()); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 842 | // This port does not serve this multicast group |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 843 | if (!existingPorts.contains(port)) { |
| 844 | log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port); |
| 845 | return false; |
| 846 | } |
| 847 | // Copy and modify the ImmutableSet |
| 848 | existingPorts = Sets.newHashSet(existingPorts); |
| 849 | existingPorts.remove(port); |
| 850 | |
| 851 | NextObjective newNextObj; |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 852 | ObjectiveContext context; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 853 | ForwardingObjective fwdObj; |
| 854 | if (existingPorts.isEmpty()) { |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 855 | // If this is the last sink, remove flows and last bucket |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 856 | // NOTE: Rely on GroupStore garbage collection rather than explicitly |
| 857 | // remove L3MG since there might be other flows/groups refer to |
| 858 | // the same L2IG |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 859 | context = new DefaultObjectiveContext( |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 860 | (objective) -> log.debug("Successfully remove {} on {}/{}, vlan {}", |
| 861 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 862 | (objective, error) -> |
| 863 | log.warn("Failed to remove {} on {}/{}, vlan {}: {}", |
| 864 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
| 865 | fwdObj = fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
| 866 | mcastNextObjStore.remove(mcastStoreKey); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 867 | } else { |
| 868 | // If this is not the last sink, update flows and groups |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 869 | context = new DefaultObjectiveContext( |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 870 | (objective) -> log.debug("Successfully update {} on {}/{}, vlan {}", |
| 871 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 872 | (objective, error) -> |
| 873 | log.warn("Failed to update {} on {}/{}, vlan {}: {}", |
| 874 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 875 | // Here we store the next objective with the remaining port |
| 876 | newNextObj = nextObjBuilder(mcastIp, assignedVlan, |
| 877 | existingPorts, nextObj.id()).removeFromExisting(); |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 878 | fwdObj = fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 879 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 880 | } |
Pier Luigi | d1be7b1 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 881 | // Let's modify the next objective removing the bucket |
| 882 | newNextObj = nextObjBuilder(mcastIp, assignedVlan, |
| 883 | ImmutableSet.of(port), nextObj.id()).removeFromExisting(); |
| 884 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 885 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 886 | return existingPorts.isEmpty(); |
| 887 | } |
| 888 | |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 889 | /** |
| 890 | * Removes entire group on given device. |
| 891 | * |
| 892 | * @param deviceId device ID |
| 893 | * @param mcastIp multicast group to be removed |
| 894 | * @param assignedVlan assigned VLAN ID |
| 895 | */ |
| 896 | private void removeGroupFromDevice(DeviceId deviceId, IpAddress mcastIp, |
| 897 | VlanId assignedVlan) { |
| 898 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId); |
| 899 | // This device is not serving this multicast group |
| 900 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 901 | log.warn("{} is not serving {}. Abort.", deviceId, mcastIp); |
| 902 | return; |
| 903 | } |
| 904 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
| 905 | // NOTE: Rely on GroupStore garbage collection rather than explicitly |
| 906 | // remove L3MG since there might be other flows/groups refer to |
| 907 | // the same L2IG |
| 908 | ObjectiveContext context = new DefaultObjectiveContext( |
| 909 | (objective) -> log.debug("Successfully remove {} on {}, vlan {}", |
| 910 | mcastIp, deviceId, assignedVlan), |
| 911 | (objective, error) -> |
| 912 | log.warn("Failed to remove {} on {}, vlan {}: {}", |
| 913 | mcastIp, deviceId, assignedVlan, error)); |
| 914 | ForwardingObjective fwdObj = fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
| 915 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
| 916 | mcastNextObjStore.remove(mcastStoreKey); |
| 917 | mcastRoleStore.remove(mcastStoreKey); |
| 918 | } |
| 919 | |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 920 | private void installPath(IpAddress mcastIp, ConnectPoint source, Path mcastPath) { |
| 921 | // Get Links |
| 922 | List<Link> links = mcastPath.links(); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 923 | |
| 924 | // Setup new ingress mcast role |
| 925 | mcastRoleStore.put(new McastStoreKey(mcastIp, links.get(0).src().deviceId()), |
| 926 | INGRESS); |
| 927 | |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 928 | // For each link, modify the next on the source device adding the src port |
| 929 | // and a new filter objective on the destination port |
| 930 | links.forEach(link -> { |
| 931 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
| 932 | assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null)); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 933 | addFilterToDevice(link.dst().deviceId(), link.dst().port(), |
| 934 | assignedVlan(null), mcastIp, null); |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 935 | }); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 936 | |
| 937 | // Setup mcast role for the transit |
| 938 | links.stream() |
| 939 | .filter(link -> !link.src().deviceId().equals(source.deviceId())) |
| 940 | .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.src().deviceId()), |
| 941 | TRANSIT)); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 944 | /** |
| 945 | * Creates a next objective builder for multicast. |
| 946 | * |
| 947 | * @param mcastIp multicast group |
| 948 | * @param assignedVlan assigned VLAN ID |
| 949 | * @param outPorts set of output port numbers |
| 950 | * @return next objective builder |
| 951 | */ |
| 952 | private NextObjective.Builder nextObjBuilder(IpAddress mcastIp, |
Pier Luigi | 21fffd2 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 953 | VlanId assignedVlan, Set<PortNumber> outPorts, Integer nextId) { |
| 954 | // If nextId is null allocate a new one |
| 955 | if (nextId == null) { |
| 956 | nextId = srManager.flowObjectiveService.allocateNextId(); |
| 957 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 958 | |
| 959 | TrafficSelector metadata = |
| 960 | DefaultTrafficSelector.builder() |
| 961 | .matchVlanId(assignedVlan) |
| 962 | .matchIPDst(mcastIp.toIpPrefix()) |
| 963 | .build(); |
| 964 | |
| 965 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 966 | .builder().withId(nextId) |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 967 | .withType(NextObjective.Type.BROADCAST).fromApp(srManager.appId()) |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 968 | .withMeta(metadata); |
| 969 | |
| 970 | outPorts.forEach(port -> { |
| 971 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 972 | if (egressVlan().equals(VlanId.NONE)) { |
| 973 | tBuilder.popVlan(); |
| 974 | } |
| 975 | tBuilder.setOutput(port); |
| 976 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 977 | }); |
| 978 | |
| 979 | return nextObjBuilder; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Creates a forwarding objective builder for multicast. |
| 984 | * |
| 985 | * @param mcastIp multicast group |
| 986 | * @param assignedVlan assigned VLAN ID |
| 987 | * @param nextId next ID of the L3 multicast group |
| 988 | * @return forwarding objective builder |
| 989 | */ |
| 990 | private ForwardingObjective.Builder fwdObjBuilder(IpAddress mcastIp, |
| 991 | VlanId assignedVlan, int nextId) { |
| 992 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 993 | IpPrefix mcastPrefix = mcastIp.toIpPrefix(); |
| 994 | |
| 995 | if (mcastIp.isIp4()) { |
| 996 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 997 | sbuilder.matchIPDst(mcastPrefix); |
| 998 | } else { |
| 999 | sbuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 1000 | sbuilder.matchIPv6Dst(mcastPrefix); |
| 1001 | } |
| 1002 | |
| 1003 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1004 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 1005 | metabuilder.matchVlanId(assignedVlan); |
| 1006 | |
| 1007 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder(); |
| 1008 | fwdBuilder.withSelector(sbuilder.build()) |
| 1009 | .withMeta(metabuilder.build()) |
| 1010 | .nextStep(nextId) |
| 1011 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1012 | .fromApp(srManager.appId()) |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1013 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 1014 | return fwdBuilder; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * Creates a filtering objective builder for multicast. |
| 1019 | * |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1020 | * @param ingressPort ingress port of the multicast stream |
| 1021 | * @param assignedVlan assigned VLAN ID |
Charles Chan | 50d900c | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1022 | * @param routerMac router MAC. This is carried in metadata and used from some switches that |
| 1023 | * need to put unicast entry before multicast entry in TMAC table. |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1024 | * @return filtering objective builder |
| 1025 | */ |
Charles Chan | 45b69ab | 2018-03-02 15:41:41 -0800 | [diff] [blame] | 1026 | private FilteringObjective.Builder filterObjBuilder(PortNumber ingressPort, |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1027 | VlanId assignedVlan, IpAddress mcastIp, MacAddress routerMac, McastRole mcastRole) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1028 | FilteringObjective.Builder filtBuilder = DefaultFilteringObjective.builder(); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1029 | // Let's add the in port matching and the priority |
| 1030 | filtBuilder.withKey(Criteria.matchInPort(ingressPort)) |
| 1031 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 1032 | // According to the mcast role we match on the proper vlan |
| 1033 | // If the role is null we are on the transit or on the egress |
| 1034 | if (mcastRole == null) { |
| 1035 | filtBuilder.addCondition(Criteria.matchVlanId(egressVlan())); |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1036 | } else { |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1037 | filtBuilder.addCondition(Criteria.matchVlanId(ingressVlan())); |
Julia Ferguson | 65428c3 | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1038 | } |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1039 | // According to the IP type we set the proper match on the mac address |
| 1040 | if (mcastIp.isIp4()) { |
| 1041 | filtBuilder.addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, |
| 1042 | MacAddress.IPV4_MULTICAST_MASK)); |
| 1043 | } else { |
| 1044 | filtBuilder.addCondition(Criteria.matchEthDstMasked(MacAddress.IPV6_MULTICAST, |
| 1045 | MacAddress.IPV6_MULTICAST_MASK)); |
| 1046 | } |
| 1047 | // We finally build the meta treatment |
Charles Chan | 1588e7b | 2016-06-28 16:50:13 -0700 | [diff] [blame] | 1048 | TrafficTreatment tt = DefaultTrafficTreatment.builder() |
Charles Chan | 50d900c | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1049 | .pushVlan().setVlanId(assignedVlan) |
| 1050 | .setEthDst(routerMac) |
| 1051 | .build(); |
Charles Chan | 1588e7b | 2016-06-28 16:50:13 -0700 | [diff] [blame] | 1052 | filtBuilder.withMeta(tt); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1053 | // Done, we return a permit filtering objective |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1054 | return filtBuilder.permit().fromApp(srManager.appId()); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * Gets output ports information from treatments. |
| 1059 | * |
| 1060 | * @param treatments collection of traffic treatments |
| 1061 | * @return set of output port numbers |
| 1062 | */ |
| 1063 | private Set<PortNumber> getPorts(Collection<TrafficTreatment> treatments) { |
| 1064 | ImmutableSet.Builder<PortNumber> builder = ImmutableSet.builder(); |
| 1065 | treatments.forEach(treatment -> { |
| 1066 | treatment.allInstructions().stream() |
| 1067 | .filter(instr -> instr instanceof OutputInstruction) |
| 1068 | .forEach(instr -> { |
| 1069 | builder.add(((OutputInstruction) instr).port()); |
| 1070 | }); |
| 1071 | }); |
| 1072 | return builder.build(); |
| 1073 | } |
| 1074 | |
Pier Luigi | f7049c5 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1075 | // Utility method to verify is a link is a pair-link |
| 1076 | private boolean isPairLink(Link link) { |
| 1077 | // Take src id, src port, dst id and dst port |
| 1078 | final DeviceId srcId = link.src().deviceId(); |
| 1079 | final PortNumber srcPort = link.src().port(); |
| 1080 | final DeviceId dstId = link.dst().deviceId(); |
| 1081 | final PortNumber dstPort = link.dst().port(); |
| 1082 | // init as true |
| 1083 | boolean isPairLink = true; |
| 1084 | try { |
| 1085 | // If one of this condition is not true; it is not a pair link |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1086 | if (!(srManager.deviceConfiguration().isEdgeDevice(srcId) && |
| 1087 | srManager.deviceConfiguration().isEdgeDevice(dstId) && |
| 1088 | srManager.deviceConfiguration().getPairDeviceId(srcId).equals(dstId) && |
| 1089 | srManager.deviceConfiguration().getPairLocalPort(srcId).equals(srcPort) && |
| 1090 | srManager.deviceConfiguration().getPairLocalPort(dstId).equals(dstPort))) { |
Pier Luigi | f7049c5 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1091 | isPairLink = false; |
| 1092 | } |
| 1093 | } catch (DeviceConfigNotFoundException e) { |
| 1094 | // Configuration not provided |
| 1095 | log.warn("Could not check if the link {} is pairlink " |
| 1096 | + "config not yet provided", link); |
| 1097 | isPairLink = false; |
| 1098 | } |
| 1099 | return isPairLink; |
| 1100 | } |
| 1101 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1102 | /** |
| 1103 | * Gets a path from src to dst. |
| 1104 | * If a path was allocated before, returns the allocated path. |
| 1105 | * Otherwise, randomly pick one from available paths. |
| 1106 | * |
| 1107 | * @param src source device ID |
| 1108 | * @param dst destination device ID |
| 1109 | * @param mcastIp multicast group |
| 1110 | * @return an optional path from src to dst |
| 1111 | */ |
| 1112 | private Optional<Path> getPath(DeviceId src, DeviceId dst, IpAddress mcastIp) { |
Pier Luigi | 83f919b | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 1113 | // Takes a snapshot of the topology |
| 1114 | final Topology currentTopology = topologyService.currentTopology(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1115 | List<Path> allPaths = Lists.newArrayList( |
Pier Luigi | 83f919b | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 1116 | topologyService.getPaths(currentTopology, src, dst) |
| 1117 | ); |
Pier Luigi | f7049c5 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1118 | // Create list of valid paths |
| 1119 | allPaths.removeIf(path -> path.links().stream().anyMatch(this::isPairLink)); |
| 1120 | // If there are no valid paths, just exit |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1121 | log.debug("{} path(s) found from {} to {}", allPaths.size(), src, dst); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1122 | if (allPaths.isEmpty()) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1123 | return Optional.empty(); |
| 1124 | } |
| 1125 | |
Pier Luigi | bad6d6c | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1126 | // Create a map index of suitablity-to-list of paths. For example |
| 1127 | // a path in the list associated to the index 1 shares only the |
| 1128 | // first hop and it is less suitable of a path belonging to the index |
| 1129 | // 2 that shares leaf-spine. |
| 1130 | Map<Integer, List<Path>> eligiblePaths = Maps.newHashMap(); |
| 1131 | // Some init steps |
| 1132 | int nhop; |
| 1133 | McastStoreKey mcastStoreKey; |
| 1134 | Link hop; |
| 1135 | PortNumber srcPort; |
| 1136 | Set<PortNumber> existingPorts; |
| 1137 | NextObjective nextObj; |
| 1138 | // Iterate over paths looking for eligible paths |
| 1139 | for (Path path : allPaths) { |
| 1140 | // Unlikely, it will happen... |
| 1141 | if (!src.equals(path.links().get(0).src().deviceId())) { |
| 1142 | continue; |
| 1143 | } |
| 1144 | nhop = 0; |
| 1145 | // Iterate over the links |
| 1146 | while (nhop < path.links().size()) { |
| 1147 | // Get the link and verify if a next related |
| 1148 | // to the src device exist in the store |
| 1149 | hop = path.links().get(nhop); |
| 1150 | mcastStoreKey = new McastStoreKey(mcastIp, hop.src().deviceId()); |
| 1151 | // It does not exist in the store, exit |
| 1152 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1153 | break; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1154 | } |
Pier Luigi | bad6d6c | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1155 | // Get the output ports on the next |
| 1156 | nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1157 | existingPorts = getPorts(nextObj.next()); |
| 1158 | // And the src port on the link |
| 1159 | srcPort = hop.src().port(); |
| 1160 | // the src port is not used as output, exit |
| 1161 | if (!existingPorts.contains(srcPort)) { |
| 1162 | break; |
| 1163 | } |
| 1164 | nhop++; |
| 1165 | } |
| 1166 | // n_hop defines the index |
| 1167 | if (nhop > 0) { |
| 1168 | eligiblePaths.compute(nhop, (index, paths) -> { |
| 1169 | paths = paths == null ? Lists.newArrayList() : paths; |
| 1170 | paths.add(path); |
| 1171 | return paths; |
| 1172 | }); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1173 | } |
| 1174 | } |
Pier Luigi | bad6d6c | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1175 | |
| 1176 | // No suitable paths |
| 1177 | if (eligiblePaths.isEmpty()) { |
| 1178 | log.debug("No eligiblePath(s) found from {} to {}", src, dst); |
| 1179 | // Otherwise, randomly pick a path |
| 1180 | Collections.shuffle(allPaths); |
| 1181 | return allPaths.stream().findFirst(); |
| 1182 | } |
| 1183 | |
| 1184 | // Let's take the best ones |
| 1185 | Integer bestIndex = eligiblePaths.keySet() |
| 1186 | .stream() |
| 1187 | .sorted(Comparator.reverseOrder()) |
| 1188 | .findFirst().orElse(null); |
| 1189 | List<Path> bestPaths = eligiblePaths.get(bestIndex); |
| 1190 | log.debug("{} eligiblePath(s) found from {} to {}", |
| 1191 | bestPaths.size(), src, dst); |
| 1192 | // randomly pick a path on the highest index |
| 1193 | Collections.shuffle(bestPaths); |
| 1194 | return bestPaths.stream().findFirst(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /** |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1198 | * Gets device(s) of given role in given multicast group. |
| 1199 | * |
| 1200 | * @param mcastIp multicast IP |
| 1201 | * @param role multicast role |
| 1202 | * @return set of device ID or empty set if not found |
| 1203 | */ |
| 1204 | private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role) { |
| 1205 | return mcastRoleStore.entrySet().stream() |
| 1206 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) && |
| 1207 | entry.getValue().value() == role) |
| 1208 | .map(Map.Entry::getKey).map(McastStoreKey::deviceId) |
| 1209 | .collect(Collectors.toSet()); |
| 1210 | } |
| 1211 | |
| 1212 | /** |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1213 | * Gets source connect point of given multicast group. |
| 1214 | * |
| 1215 | * @param mcastIp multicast IP |
| 1216 | * @return source connect point or null if not found |
| 1217 | */ |
| 1218 | private ConnectPoint getSource(IpAddress mcastIp) { |
| 1219 | return srManager.multicastRouteService.getRoutes().stream() |
| 1220 | .filter(mcastRoute -> mcastRoute.group().equals(mcastIp)) |
| 1221 | .map(mcastRoute -> srManager.multicastRouteService.fetchSource(mcastRoute)) |
| 1222 | .findAny().orElse(null); |
| 1223 | } |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1224 | /** |
| 1225 | * Gets sinks of given multicast group. |
| 1226 | * |
| 1227 | * @param mcastIp multicast IP |
| 1228 | * @return set of sinks or empty set if not found |
| 1229 | */ |
| 1230 | private Set<ConnectPoint> getSinks(IpAddress mcastIp) { |
| 1231 | return srManager.multicastRouteService.getRoutes().stream() |
| 1232 | .filter(mcastRoute -> mcastRoute.group().equals(mcastIp)) |
| 1233 | .map(mcastRoute -> srManager.multicastRouteService.fetchSinks(mcastRoute)) |
| 1234 | .findAny().orElse(Collections.emptySet()); |
| 1235 | } |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1236 | |
| 1237 | /** |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1238 | * Gets groups which is affected by the link down event. |
| 1239 | * |
| 1240 | * @param link link going down |
| 1241 | * @return a set of multicast IpAddress |
| 1242 | */ |
| 1243 | private Set<IpAddress> getAffectedGroups(Link link) { |
| 1244 | DeviceId deviceId = link.src().deviceId(); |
| 1245 | PortNumber port = link.src().port(); |
| 1246 | return mcastNextObjStore.entrySet().stream() |
| 1247 | .filter(entry -> entry.getKey().deviceId().equals(deviceId) && |
| 1248 | getPorts(entry.getValue().value().next()).contains(port)) |
| 1249 | .map(Map.Entry::getKey).map(McastStoreKey::mcastIp) |
| 1250 | .collect(Collectors.toSet()); |
| 1251 | } |
| 1252 | |
| 1253 | /** |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1254 | * Gets groups which are affected by the device down event. |
| 1255 | * |
| 1256 | * @param deviceId device going down |
| 1257 | * @return a set of multicast IpAddress |
| 1258 | */ |
| 1259 | private Set<IpAddress> getAffectedGroups(DeviceId deviceId) { |
| 1260 | return mcastNextObjStore.entrySet().stream() |
| 1261 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
| 1262 | .map(Map.Entry::getKey).map(McastStoreKey::mcastIp) |
| 1263 | .collect(Collectors.toSet()); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1267 | * Gets ingress VLAN from McastConfig. |
| 1268 | * |
| 1269 | * @return ingress VLAN or VlanId.NONE if not configured |
| 1270 | */ |
| 1271 | private VlanId ingressVlan() { |
| 1272 | McastConfig mcastConfig = |
| 1273 | srManager.cfgService.getConfig(coreAppId, McastConfig.class); |
| 1274 | return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE; |
| 1275 | } |
| 1276 | |
| 1277 | /** |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1278 | * Gets egress VLAN from McastConfig. |
| 1279 | * |
| 1280 | * @return egress VLAN or VlanId.NONE if not configured |
| 1281 | */ |
| 1282 | private VlanId egressVlan() { |
| 1283 | McastConfig mcastConfig = |
| 1284 | srManager.cfgService.getConfig(coreAppId, McastConfig.class); |
| 1285 | return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * Gets assigned VLAN according to the value of egress VLAN. |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1290 | * If connect point is specified, try to reuse the assigned VLAN on the connect point. |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1291 | * |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1292 | * @param cp connect point; Can be null if not specified |
| 1293 | * @return assigned VLAN ID |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1294 | */ |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1295 | private VlanId assignedVlan(ConnectPoint cp) { |
| 1296 | // Use the egressVlan if it is tagged |
| 1297 | if (!egressVlan().equals(VlanId.NONE)) { |
| 1298 | return egressVlan(); |
| 1299 | } |
| 1300 | // Reuse unicast VLAN if the port has subnet configured |
| 1301 | if (cp != null) { |
Charles Chan | b4879a5 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 1302 | VlanId untaggedVlan = srManager.getInternalVlanId(cp); |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1303 | return (untaggedVlan != null) ? untaggedVlan : INTERNAL_VLAN; |
Charles Chan | 8d44986 | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 1304 | } |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 1305 | // Use DEFAULT_VLAN if none of the above matches |
| 1306 | return SegmentRoutingManager.INTERNAL_VLAN; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1307 | } |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1308 | |
| 1309 | /** |
Pier Luigi | 05514fd | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 1310 | * Gets assigned VLAN according to the value in the meta. |
| 1311 | * |
| 1312 | * @param nextObjective nextObjective to analyze |
| 1313 | * @return assigned VLAN ID |
| 1314 | */ |
| 1315 | private VlanId assignedVlanFromNext(NextObjective nextObjective) { |
| 1316 | return ((VlanIdCriterion) nextObjective.meta().getCriterion(VLAN_VID)).vlanId(); |
| 1317 | } |
| 1318 | |
| 1319 | /** |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1320 | * Gets the spine-facing port on ingress device of given multicast group. |
| 1321 | * |
| 1322 | * @param mcastIp multicast IP |
| 1323 | * @return spine-facing port on ingress device |
| 1324 | */ |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1325 | private Set<PortNumber> ingressTransitPort(IpAddress mcastIp) { |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1326 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1327 | .stream().findAny().orElse(null); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1328 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1329 | if (ingressDevice != null) { |
| 1330 | NextObjective nextObj = mcastNextObjStore |
| 1331 | .get(new McastStoreKey(mcastIp, ingressDevice)).value(); |
| 1332 | Set<PortNumber> ports = getPorts(nextObj.next()); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1333 | // Let's find out all the ingress-transit ports |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1334 | for (PortNumber port : ports) { |
| 1335 | // Spine-facing port should have no subnet and no xconnect |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1336 | if (srManager.deviceConfiguration() != null && |
| 1337 | srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() && |
Charles Chan | fc5c780 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1338 | !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) { |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1339 | portBuilder.add(port); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | } |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1343 | return portBuilder.build(); |
Charles Chan | 2199c30 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1344 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | /** |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1347 | * Verify if the given device has sinks |
| 1348 | * for the multicast group. |
| 1349 | * |
| 1350 | * @param deviceId device Id |
| 1351 | * @param mcastIp multicast IP |
| 1352 | * @return true if the device has sink for the group. |
| 1353 | * False otherwise. |
| 1354 | */ |
| 1355 | private boolean hasSinks(DeviceId deviceId, IpAddress mcastIp) { |
| 1356 | if (deviceId != null) { |
| 1357 | // Get the nextobjective |
| 1358 | Versioned<NextObjective> versionedNextObj = mcastNextObjStore.get( |
| 1359 | new McastStoreKey(mcastIp, deviceId) |
| 1360 | ); |
| 1361 | // If it exists |
| 1362 | if (versionedNextObj != null) { |
| 1363 | NextObjective nextObj = versionedNextObj.value(); |
| 1364 | // Retrieves all the output ports |
| 1365 | Set<PortNumber> ports = getPorts(nextObj.next()); |
| 1366 | // Tries to find at least one port that is not spine-facing |
| 1367 | for (PortNumber port : ports) { |
| 1368 | // Spine-facing port should have no subnet and no xconnect |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1369 | if (srManager.deviceConfiguration() != null && |
| 1370 | (!srManager.deviceConfiguration().getPortSubnets(deviceId, port).isEmpty() || |
Pier Luigi | eba73a0 | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1371 | srManager.xConnectHandler.hasXConnect(new ConnectPoint(deviceId, port)))) { |
| 1372 | return true; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1381 | * Removes filtering objective for given device and port. |
| 1382 | * |
| 1383 | * @param deviceId device ID |
| 1384 | * @param port ingress port number |
| 1385 | * @param assignedVlan assigned VLAN ID |
| 1386 | * @param mcastIp multicast IP address |
| 1387 | */ |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1388 | private void removeFilterToDevice(DeviceId deviceId, PortNumber port, |
| 1389 | VlanId assignedVlan, IpAddress mcastIp, McastRole mcastRole) { |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1390 | // Do nothing if the port is configured as suppressed |
| 1391 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 1392 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1393 | .getConfig(srManager.appId(), SegmentRoutingAppConfig.class); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1394 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
| 1395 | log.info("Ignore suppressed port {}", connectPoint); |
| 1396 | return; |
| 1397 | } |
| 1398 | |
Charles Chan | 50d900c | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1399 | MacAddress routerMac; |
| 1400 | try { |
| 1401 | routerMac = srManager.deviceConfiguration().getDeviceMac(deviceId); |
| 1402 | } catch (DeviceConfigNotFoundException dcnfe) { |
| 1403 | log.warn("Fail to push filtering objective since device is not configured. Abort"); |
| 1404 | return; |
| 1405 | } |
| 1406 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1407 | FilteringObjective.Builder filtObjBuilder = |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1408 | filterObjBuilder(port, assignedVlan, mcastIp, routerMac, mcastRole); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1409 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1410 | (objective) -> log.debug("Successfully removed filter on {}/{}, vlan {}", |
| 1411 | deviceId, port.toLong(), assignedVlan), |
| 1412 | (objective, error) -> |
| 1413 | log.warn("Failed to remove filter on {}/{}, vlan {}: {}", |
| 1414 | deviceId, port.toLong(), assignedVlan, error)); |
| 1415 | srManager.flowObjectiveService.filter(deviceId, filtObjBuilder.remove(context)); |
| 1416 | } |
| 1417 | |
| 1418 | /** |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1419 | * Updates filtering objective for given device and port. |
| 1420 | * It is called in general when the mcast config has been |
| 1421 | * changed. |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1422 | * |
| 1423 | * @param deviceId device ID |
| 1424 | * @param portNum ingress port number |
| 1425 | * @param vlanId assigned VLAN ID |
| 1426 | * @param install true to add, false to remove |
| 1427 | */ |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1428 | public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum, |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1429 | VlanId vlanId, boolean install) { |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1430 | lastMcastChange = Instant.now(); |
| 1431 | mcastLock(); |
| 1432 | try { |
| 1433 | // Iterates over the route and updates properly the filtering objective |
| 1434 | // on the source device. |
| 1435 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
| 1436 | ConnectPoint source = srManager.multicastRouteService.fetchSource(mcastRoute); |
| 1437 | if (source.deviceId().equals(deviceId) && source.port().equals(portNum)) { |
| 1438 | if (install) { |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1439 | addFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), INGRESS); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1440 | } else { |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1441 | removeFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), null); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1442 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1443 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1444 | }); |
| 1445 | } finally { |
| 1446 | mcastUnlock(); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Pier Luigi | 9930da5 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 1450 | private boolean isLeader(ConnectPoint source) { |
| 1451 | // Continue only when we have the mastership on the operation |
| 1452 | if (!srManager.mastershipService.isLocalMaster(source.deviceId())) { |
| 1453 | // When the source is available we just check the mastership |
| 1454 | if (srManager.deviceService.isAvailable(source.deviceId())) { |
| 1455 | return false; |
| 1456 | } |
| 1457 | // Fallback with Leadership service |
| 1458 | // source id is used a topic |
| 1459 | NodeId leader = srManager.leadershipService.runForLeadership( |
| 1460 | source.deviceId().toString()).leaderNodeId(); |
| 1461 | // Verify if this node is the leader |
| 1462 | if (!srManager.clusterService.getLocalNode().id().equals(leader)) { |
| 1463 | return false; |
| 1464 | } |
| 1465 | } |
| 1466 | // Done |
| 1467 | return true; |
| 1468 | } |
| 1469 | |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1470 | /** |
| 1471 | * Performs bucket verification operation for all mcast groups in the devices. |
| 1472 | * Firstly, it verifies that mcast is stable before trying verification operation. |
| 1473 | * Verification consists in creating new nexts with VERIFY operation. Actually, |
| 1474 | * the operation is totally delegated to the driver. |
| 1475 | */ |
| 1476 | private final class McastBucketCorrector implements Runnable { |
| 1477 | |
| 1478 | @Override |
| 1479 | public void run() { |
| 1480 | // Verify if the Mcast has been stable for MCAST_STABLITY_THRESHOLD |
| 1481 | if (!isMcastStable()) { |
| 1482 | return; |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1483 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1484 | // Acquires lock |
| 1485 | mcastLock(); |
| 1486 | try { |
| 1487 | // Iterates over the routes and verify the related next objectives |
| 1488 | srManager.multicastRouteService.getRoutes() |
| 1489 | .stream() |
| 1490 | .map(McastRoute::group) |
| 1491 | .forEach(mcastIp -> { |
| 1492 | log.trace("Running mcast buckets corrector for mcast group: {}", |
| 1493 | mcastIp); |
| 1494 | |
| 1495 | // For each group we get current information in the store |
| 1496 | // and issue a check of the next objectives in place |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1497 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1498 | .stream().findAny().orElse(null); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1499 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | e5bff48 | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1500 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1501 | // Get source and sinks from Mcast Route Service and warn about errors |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1502 | ConnectPoint source = getSource(mcastIp); |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1503 | Set<ConnectPoint> sinks = getSinks(mcastIp); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1504 | |
| 1505 | // Do not proceed if ingress device or source of this group are missing |
| 1506 | if (ingressDevice == null || source == null) { |
Pier Luigi | b87b8ab | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1507 | if (!sinks.isEmpty()) { |
| 1508 | log.warn("Unable to run buckets corrector. " + |
| 1509 | "Missing ingress {} or source {} for group {}", |
| 1510 | ingressDevice, source, mcastIp); |
| 1511 | } |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | // Continue only when this instance is the master of source device |
| 1516 | if (!srManager.mastershipService.isLocalMaster(source.deviceId())) { |
| 1517 | log.trace("Unable to run buckets corrector. " + |
| 1518 | "Skip {} due to lack of mastership " + |
| 1519 | "of the source device {}", |
| 1520 | mcastIp, source.deviceId()); |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | // Create the set of the devices to be processed |
| 1525 | ImmutableSet.Builder<DeviceId> devicesBuilder = ImmutableSet.builder(); |
| 1526 | devicesBuilder.add(ingressDevice); |
Pier | 37db369 | 2018-03-12 15:00:54 -0700 | [diff] [blame^] | 1527 | if (!transitDevices.isEmpty()) { |
| 1528 | devicesBuilder.addAll(transitDevices); |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1529 | } |
| 1530 | if (!egressDevices.isEmpty()) { |
| 1531 | devicesBuilder.addAll(egressDevices); |
| 1532 | } |
| 1533 | Set<DeviceId> devicesToProcess = devicesBuilder.build(); |
| 1534 | |
| 1535 | // Iterate over the devices |
| 1536 | devicesToProcess.forEach(deviceId -> { |
| 1537 | McastStoreKey currentKey = new McastStoreKey(mcastIp, deviceId); |
| 1538 | // If next exists in our store verify related next objective |
| 1539 | if (mcastNextObjStore.containsKey(currentKey)) { |
| 1540 | NextObjective currentNext = mcastNextObjStore.get(currentKey).value(); |
| 1541 | // Get current ports |
| 1542 | Set<PortNumber> currentPorts = getPorts(currentNext.next()); |
| 1543 | // Rebuild the next objective |
| 1544 | currentNext = nextObjBuilder( |
| 1545 | mcastIp, |
| 1546 | assignedVlan(deviceId.equals(source.deviceId()) ? source : null), |
| 1547 | currentPorts, |
| 1548 | currentNext.id() |
| 1549 | ).verify(); |
| 1550 | // Send to the flowobjective service |
| 1551 | srManager.flowObjectiveService.next(deviceId, currentNext); |
| 1552 | } else { |
Pier Luigi | 83f919b | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 1553 | log.warn("Unable to run buckets corrector. " + |
Pier Luigi | b72201b | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1554 | "Missing next for {} and group {}", |
| 1555 | deviceId, mcastIp); |
| 1556 | } |
| 1557 | }); |
| 1558 | |
| 1559 | }); |
| 1560 | } finally { |
| 1561 | // Finally, it releases the lock |
| 1562 | mcastUnlock(); |
| 1563 | } |
| 1564 | |
| 1565 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1566 | } |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1567 | |
| 1568 | public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) { |
| 1569 | // If mcast ip is present |
| 1570 | if (mcastIp != null) { |
| 1571 | return mcastNextObjStore.entrySet().stream() |
| 1572 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
| 1573 | .collect(Collectors.toMap(Map.Entry::getKey, |
| 1574 | entry -> entry.getValue().value().id())); |
| 1575 | } |
| 1576 | // Otherwise take all the groups |
| 1577 | return mcastNextObjStore.entrySet().stream() |
| 1578 | .collect(Collectors.toMap(Map.Entry::getKey, |
| 1579 | entry -> entry.getValue().value().id())); |
| 1580 | } |
| 1581 | |
Pier Luigi | 96fe077 | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1582 | public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) { |
Pier Luigi | b29144d | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1583 | // If mcast ip is present |
| 1584 | if (mcastIp != null) { |
| 1585 | return mcastRoleStore.entrySet().stream() |
| 1586 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
| 1587 | .collect(Collectors.toMap(Map.Entry::getKey, |
| 1588 | entry -> entry.getValue().value())); |
| 1589 | } |
| 1590 | // Otherwise take all the groups |
| 1591 | return mcastRoleStore.entrySet().stream() |
| 1592 | .collect(Collectors.toMap(Map.Entry::getKey, |
| 1593 | entry -> entry.getValue().value())); |
| 1594 | } |
| 1595 | |
| 1596 | public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) { |
| 1597 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
| 1598 | // Get the source |
| 1599 | ConnectPoint source = getSource(mcastIp); |
| 1600 | // Source cannot be null, we don't know the starting point |
| 1601 | if (source != null) { |
| 1602 | // Init steps |
| 1603 | Set<DeviceId> visited = Sets.newHashSet(); |
| 1604 | List<ConnectPoint> currentPath = Lists.newArrayList( |
| 1605 | source |
| 1606 | ); |
| 1607 | // Build recursively the mcast paths |
| 1608 | buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp); |
| 1609 | } |
| 1610 | return mcastPaths; |
| 1611 | } |
| 1612 | |
| 1613 | private void buildMcastPaths(DeviceId toVisit, Set<DeviceId> visited, |
| 1614 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths, |
| 1615 | List<ConnectPoint> currentPath, IpAddress mcastIp) { |
| 1616 | // If we have visited the node to visit |
| 1617 | // there is a loop |
| 1618 | if (visited.contains(toVisit)) { |
| 1619 | return; |
| 1620 | } |
| 1621 | // Visit next-hop |
| 1622 | visited.add(toVisit); |
| 1623 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit); |
| 1624 | // Looking for next-hops |
| 1625 | if (mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1626 | // Build egress connectpoints |
| 1627 | NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1628 | // Get Ports |
| 1629 | Set<PortNumber> outputPorts = getPorts(nextObjective.next()); |
| 1630 | // Build relative cps |
| 1631 | ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder(); |
| 1632 | outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber))); |
| 1633 | Set<ConnectPoint> egressPoints = cpBuilder.build(); |
| 1634 | // Define other variables for the next steps |
| 1635 | Set<Link> egressLinks; |
| 1636 | List<ConnectPoint> newCurrentPath; |
| 1637 | Set<DeviceId> newVisited; |
| 1638 | DeviceId newToVisit; |
| 1639 | for (ConnectPoint egressPoint : egressPoints) { |
| 1640 | egressLinks = srManager.linkService.getEgressLinks(egressPoint); |
| 1641 | // If it does not have egress links, stop |
| 1642 | if (egressLinks.isEmpty()) { |
| 1643 | // Add the connect points to the path |
| 1644 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1645 | newCurrentPath.add(0, egressPoint); |
| 1646 | // Save in the map |
| 1647 | mcastPaths.put(egressPoint, newCurrentPath); |
| 1648 | } else { |
| 1649 | newVisited = Sets.newHashSet(visited); |
| 1650 | // Iterate over the egress links for the next hops |
| 1651 | for (Link egressLink : egressLinks) { |
| 1652 | // Update to visit |
| 1653 | newToVisit = egressLink.dst().deviceId(); |
| 1654 | // Add the connect points to the path |
| 1655 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1656 | newCurrentPath.add(0, egressPoint); |
| 1657 | newCurrentPath.add(0, egressLink.dst()); |
| 1658 | // Go to the next hop |
| 1659 | buildMcastPaths(newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp); |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1666 | } |