blob: 6d8495d6609c5c65d954d05b21fd70b9e8249a18 [file] [log] [blame]
Charles Chanc91c8782016-03-30 17:54:24 -07001/*
Pier Luigi69f774d2018-02-28 12:10:50 +01002 * Copyright 2018-present Open Networking Foundation
Charles Chanc91c8782016-03-30 17:54:24 -07003 *
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 Luigi69f774d2018-02-28 12:10:50 +010017package org.onosproject.segmentrouting.mcast;
Charles Chanc91c8782016-03-30 17:54:24 -070018
Pier Luigid29ca7c2018-02-28 17:24:03 +010019import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.cache.RemovalCause;
22import com.google.common.cache.RemovalNotification;
Pier71c55772018-04-17 17:25:22 +020023import com.google.common.collect.HashMultimap;
Pier7b657162018-03-27 11:29:42 -070024import com.google.common.collect.ImmutableList;
Charles Chanc91c8782016-03-30 17:54:24 -070025import com.google.common.collect.ImmutableSet;
26import com.google.common.collect.Lists;
Pier Luigi91573e12018-01-23 16:06:38 +010027import com.google.common.collect.Maps;
Pier71c55772018-04-17 17:25:22 +020028import com.google.common.collect.Multimap;
Charles Chanc91c8782016-03-30 17:54:24 -070029import com.google.common.collect.Sets;
Charles Chanc91c8782016-03-30 17:54:24 -070030import org.onlab.packet.IpAddress;
Charles Chanc91c8782016-03-30 17:54:24 -070031import org.onlab.packet.VlanId;
32import org.onlab.util.KryoNamespace;
Pierdb27b8d2018-04-17 16:29:56 +020033import org.onosproject.cluster.NodeId;
Charles Chanc91c8782016-03-30 17:54:24 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Pier1f87aca2018-03-14 16:47:32 -070036import org.onosproject.mcast.api.McastEvent;
37import org.onosproject.mcast.api.McastRoute;
Pier7b657162018-03-27 11:29:42 -070038import org.onosproject.mcast.api.McastRouteData;
Pier1f87aca2018-03-14 16:47:32 -070039import org.onosproject.mcast.api.McastRouteUpdate;
Pier7b657162018-03-27 11:29:42 -070040import org.onosproject.net.HostId;
Charles Chanc91c8782016-03-30 17:54:24 -070041import org.onosproject.net.ConnectPoint;
42import org.onosproject.net.DeviceId;
43import org.onosproject.net.Link;
44import org.onosproject.net.Path;
45import org.onosproject.net.PortNumber;
Charles Chan72779502016-04-23 17:36:10 -070046import org.onosproject.net.flowobjective.DefaultObjectiveContext;
Charles Chanc91c8782016-03-30 17:54:24 -070047import org.onosproject.net.flowobjective.ForwardingObjective;
48import org.onosproject.net.flowobjective.NextObjective;
Charles Chan72779502016-04-23 17:36:10 -070049import org.onosproject.net.flowobjective.ObjectiveContext;
Pier1f87aca2018-03-14 16:47:32 -070050import org.onosproject.net.topology.LinkWeigher;
Pier Luigid8a15162018-02-15 16:33:08 +010051import org.onosproject.net.topology.Topology;
Charles Chanc91c8782016-03-30 17:54:24 -070052import org.onosproject.net.topology.TopologyService;
Pier1f87aca2018-03-14 16:47:32 -070053import org.onosproject.segmentrouting.SRLinkWeigher;
Pier Luigi69f774d2018-02-28 12:10:50 +010054import org.onosproject.segmentrouting.SegmentRoutingManager;
Charles Chan72779502016-04-23 17:36:10 -070055import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chanc91c8782016-03-30 17:54:24 -070056import org.onosproject.store.serializers.KryoNamespaces;
57import org.onosproject.store.service.ConsistentMap;
58import org.onosproject.store.service.Serializer;
Pier Luigi580fd8a2018-01-16 10:47:50 +010059import org.onosproject.store.service.Versioned;
Charles Chanc91c8782016-03-30 17:54:24 -070060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
62
Pier Luigi35dab3f2018-01-25 16:16:02 +010063import java.time.Instant;
Charles Chanc91c8782016-03-30 17:54:24 -070064import java.util.Collection;
65import java.util.Collections;
Pier Luigi91573e12018-01-23 16:06:38 +010066import java.util.Comparator;
Charles Chanc91c8782016-03-30 17:54:24 -070067import java.util.List;
Charles Chan72779502016-04-23 17:36:10 -070068import java.util.Map;
Pier1f87aca2018-03-14 16:47:32 -070069import java.util.Map.Entry;
Charles Chanc91c8782016-03-30 17:54:24 -070070import java.util.Optional;
71import java.util.Set;
Pier Luigi35dab3f2018-01-25 16:16:02 +010072import java.util.concurrent.ScheduledExecutorService;
73import java.util.concurrent.TimeUnit;
74import java.util.concurrent.locks.Lock;
75import java.util.concurrent.locks.ReentrantLock;
Charles Chan72779502016-04-23 17:36:10 -070076import java.util.stream.Collectors;
77
Pier Luigi35dab3f2018-01-25 16:16:02 +010078import static java.util.concurrent.Executors.newScheduledThreadPool;
79import static org.onlab.util.Tools.groupedThreads;
Pier1f87aca2018-03-14 16:47:32 -070080
Pierdb27b8d2018-04-17 16:29:56 +020081import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_ADDED;
Pier7b657162018-03-27 11:29:42 -070082import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_REMOVED;
Pierdb27b8d2018-04-17 16:29:56 +020083import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_ADDED;
84import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_REMOVED;
85import static org.onosproject.mcast.api.McastEvent.Type.SINKS_ADDED;
86import static org.onosproject.mcast.api.McastEvent.Type.SINKS_REMOVED;
87
Pier979e61a2018-03-07 11:42:50 +010088import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS;
89import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS;
90import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT;
Charles Chanc91c8782016-03-30 17:54:24 -070091
92/**
Pier Luigi69f774d2018-02-28 12:10:50 +010093 * Handles Multicast related events.
Charles Chanc91c8782016-03-30 17:54:24 -070094 */
Charles Chan1eaf4802016-04-18 13:44:03 -070095public class McastHandler {
Pier7b657162018-03-27 11:29:42 -070096 // Logger instance
Charles Chan1eaf4802016-04-18 13:44:03 -070097 private static final Logger log = LoggerFactory.getLogger(McastHandler.class);
Pier7b657162018-03-27 11:29:42 -070098 // Reference to srManager and most used internal objects
Charles Chanc91c8782016-03-30 17:54:24 -070099 private final SegmentRoutingManager srManager;
Charles Chan82f19972016-05-17 13:13:55 -0700100 private final TopologyService topologyService;
Pierdb27b8d2018-04-17 16:29:56 +0200101 private final McastUtils mcastUtils;
Pier7b657162018-03-27 11:29:42 -0700102 // Internal store of the Mcast nextobjectives
Charles Chan72779502016-04-23 17:36:10 -0700103 private final ConsistentMap<McastStoreKey, NextObjective> mcastNextObjStore;
Pier7b657162018-03-27 11:29:42 -0700104 // Internal store of the Mcast roles
Charles Chan72779502016-04-23 17:36:10 -0700105 private final ConsistentMap<McastStoreKey, McastRole> mcastRoleStore;
106
Pier Luigid29ca7c2018-02-28 17:24:03 +0100107 // Wait time for the cache
108 private static final int WAIT_TIME_MS = 1000;
Pier7b657162018-03-27 11:29:42 -0700109
Pier Luigid29ca7c2018-02-28 17:24:03 +0100110 /**
111 * The mcastEventCache is implemented to avoid race condition by giving more time to the
112 * underlying subsystems to process previous calls.
113 */
114 private Cache<McastCacheKey, McastEvent> mcastEventCache = CacheBuilder.newBuilder()
115 .expireAfterWrite(WAIT_TIME_MS, TimeUnit.MILLISECONDS)
116 .removalListener((RemovalNotification<McastCacheKey, McastEvent> notification) -> {
117 // Get group ip, sink and related event
118 IpAddress mcastIp = notification.getKey().mcastIp();
Pier7b657162018-03-27 11:29:42 -0700119 HostId sink = notification.getKey().sinkHost();
Pier Luigid29ca7c2018-02-28 17:24:03 +0100120 McastEvent mcastEvent = notification.getValue();
121 RemovalCause cause = notification.getCause();
122 log.debug("mcastEventCache removal event. group={}, sink={}, mcastEvent={}, cause={}",
123 mcastIp, sink, mcastEvent, cause);
124 // If it expires or it has been replaced, we deque the event
125 switch (notification.getCause()) {
126 case REPLACED:
127 case EXPIRED:
128 dequeueMcastEvent(mcastEvent);
129 break;
130 default:
131 break;
132 }
133 }).build();
134
135 private void enqueueMcastEvent(McastEvent mcastEvent) {
Pier7b657162018-03-27 11:29:42 -0700136 // Retrieve, currentData, prevData and the group
Pier1f87aca2018-03-14 16:47:32 -0700137 final McastRouteUpdate mcastRouteUpdate = mcastEvent.subject();
Pier7b657162018-03-27 11:29:42 -0700138 final McastRouteUpdate mcastRoutePrevUpdate = mcastEvent.prevSubject();
139 final IpAddress group = mcastRoutePrevUpdate.route().group();
Pier Luigid29ca7c2018-02-28 17:24:03 +0100140 // Let's create the keys of the cache
Pier7b657162018-03-27 11:29:42 -0700141 ImmutableSet.Builder<HostId> sinksBuilder = ImmutableSet.builder();
Pier1f87aca2018-03-14 16:47:32 -0700142 if (mcastEvent.type() == SOURCES_ADDED ||
143 mcastEvent.type() == SOURCES_REMOVED) {
144 // FIXME To be addressed with multiple sources support
Pier7b657162018-03-27 11:29:42 -0700145 sinksBuilder.addAll(Collections.emptySet());
146 } else if (mcastEvent.type() == SINKS_ADDED) {
147 // We need to process the host id one by one
148 mcastRouteUpdate.sinks().forEach(((hostId, connectPoints) -> {
149 // Get the previous locations and verify if there are changes
150 Set<ConnectPoint> prevConnectPoints = mcastRoutePrevUpdate.sinks().get(hostId);
151 Set<ConnectPoint> changes = Sets.difference(connectPoints, prevConnectPoints != null ?
152 prevConnectPoints : Collections.emptySet());
153 if (!changes.isEmpty()) {
154 sinksBuilder.add(hostId);
Pier1f87aca2018-03-14 16:47:32 -0700155 }
Pier7b657162018-03-27 11:29:42 -0700156 }));
157 } else if (mcastEvent.type() == SINKS_REMOVED) {
158 // We need to process the host id one by one
159 mcastRoutePrevUpdate.sinks().forEach(((hostId, connectPoints) -> {
160 // Get the current locations and verify if there are changes
161 Set<ConnectPoint> currentConnectPoints = mcastRouteUpdate.sinks().get(hostId);
162 Set<ConnectPoint> changes = Sets.difference(connectPoints, currentConnectPoints != null ?
163 currentConnectPoints : Collections.emptySet());
164 if (!changes.isEmpty()) {
165 sinksBuilder.add(hostId);
166 }
167 }));
168 } else if (mcastEvent.type() == ROUTE_REMOVED) {
169 // Current subject is null, just take the previous host ids
170 sinksBuilder.addAll(mcastRoutePrevUpdate.sinks().keySet());
Pier Luigid29ca7c2018-02-28 17:24:03 +0100171 }
172 // Push the elements in the cache
173 sinksBuilder.build().forEach(sink -> {
Pier1f87aca2018-03-14 16:47:32 -0700174 McastCacheKey cacheKey = new McastCacheKey(group, sink);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100175 mcastEventCache.put(cacheKey, mcastEvent);
176 });
177 }
178
179 private void dequeueMcastEvent(McastEvent mcastEvent) {
Pier7b657162018-03-27 11:29:42 -0700180 // Get new and old data
181 final McastRouteUpdate mcastUpdate = mcastEvent.subject();
182 final McastRouteUpdate mcastPrevUpdate = mcastEvent.prevSubject();
Pier Luigid29ca7c2018-02-28 17:24:03 +0100183 // Get source, mcast group
Pier1f87aca2018-03-14 16:47:32 -0700184 // FIXME To be addressed with multiple sources support
Pier7b657162018-03-27 11:29:42 -0700185 final ConnectPoint source = mcastPrevUpdate.sources()
Pier1f87aca2018-03-14 16:47:32 -0700186 .stream()
187 .findFirst()
188 .orElse(null);
Pier7b657162018-03-27 11:29:42 -0700189 IpAddress mcastIp = mcastPrevUpdate.route().group();
190 // Get all the previous sinks
191 Set<ConnectPoint> prevSinks = mcastPrevUpdate.sinks()
Pier1f87aca2018-03-14 16:47:32 -0700192 .values()
193 .stream()
194 .flatMap(Collection::stream)
195 .collect(Collectors.toSet());
Pier Luigid29ca7c2018-02-28 17:24:03 +0100196 // According to the event type let's call the proper method
197 switch (mcastEvent.type()) {
Pier1f87aca2018-03-14 16:47:32 -0700198 case SOURCES_ADDED:
199 // FIXME To be addressed with multiple sources support
200 // Get all the sinks
201 //Set<ConnectPoint> sinks = mcastRouteInfo.sinks();
202 // Compute the Mcast tree
203 //Map<ConnectPoint, List<Path>> mcasTree = computeSinkMcastTree(source.deviceId(), sinks);
204 // Process the given sinks using the pre-computed paths
205 //mcasTree.forEach((sink, paths) -> processSinkAddedInternal(source, sink, mcastIp, paths));
Pier Luigid29ca7c2018-02-28 17:24:03 +0100206 break;
Pier1f87aca2018-03-14 16:47:32 -0700207 case SOURCES_REMOVED:
208 // FIXME To be addressed with multiple sources support
Pier Luigid29ca7c2018-02-28 17:24:03 +0100209 // Get old source
Pier1f87aca2018-03-14 16:47:32 -0700210 //ConnectPoint oldSource = mcastEvent.prevSubject().source().orElse(null);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100211 // Just the first cached element will be processed
Pier1f87aca2018-03-14 16:47:32 -0700212 //processSourceUpdatedInternal(mcastIp, source, oldSource);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100213 break;
214 case ROUTE_REMOVED:
215 // Process the route removed, just the first cached element will be processed
Pier7b657162018-03-27 11:29:42 -0700216 processRouteRemovedInternal(source, mcastIp);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100217 break;
Pier1f87aca2018-03-14 16:47:32 -0700218 case SINKS_ADDED:
Pier7b657162018-03-27 11:29:42 -0700219 // FIXME To be addressed with multiple sources support
220 processSinksAddedInternal(source, mcastIp,
221 mcastUpdate.sinks(), prevSinks);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100222 break;
Pier1f87aca2018-03-14 16:47:32 -0700223 case SINKS_REMOVED:
Pier7b657162018-03-27 11:29:42 -0700224 // FIXME To be addressed with multiple sources support
225 processSinksRemovedInternal(source, mcastIp,
Pier28164682018-04-17 15:50:43 +0200226 mcastUpdate.sinks(), mcastPrevUpdate.sinks());
Pier Luigid29ca7c2018-02-28 17:24:03 +0100227 break;
228 default:
229 break;
230 }
231 }
232
Pier Luigi35dab3f2018-01-25 16:16:02 +0100233 // Mcast lock to serialize local operations
234 private final Lock mcastLock = new ReentrantLock();
235
236 /**
237 * Acquires the lock used when making mcast changes.
238 */
239 private void mcastLock() {
240 mcastLock.lock();
241 }
242
243 /**
244 * Releases the lock used when making mcast changes.
245 */
246 private void mcastUnlock() {
247 mcastLock.unlock();
248 }
249
250 // Stability threshold for Mcast. Seconds
251 private static final long MCAST_STABLITY_THRESHOLD = 5;
252 // Last change done
253 private Instant lastMcastChange = Instant.now();
254
255 /**
256 * Determines if mcast in the network has been stable in the last
257 * MCAST_STABLITY_THRESHOLD seconds, by comparing the current time
258 * to the last mcast change timestamp.
259 *
260 * @return true if stable
261 */
262 private boolean isMcastStable() {
263 long last = (long) (lastMcastChange.toEpochMilli() / 1000.0);
264 long now = (long) (Instant.now().toEpochMilli() / 1000.0);
Saurav Das97241862018-02-14 14:14:54 -0800265 log.trace("Mcast stable since {}s", now - last);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100266 return (now - last) > MCAST_STABLITY_THRESHOLD;
267 }
268
269 // Verify interval for Mcast
270 private static final long MCAST_VERIFY_INTERVAL = 30;
271
272 // Executor for mcast bucket corrector
273 private ScheduledExecutorService executorService
Pier Luigid29ca7c2018-02-28 17:24:03 +0100274 = newScheduledThreadPool(1, groupedThreads("mcastWorker", "mcastWorker-%d", log));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100275
Charles Chan72779502016-04-23 17:36:10 -0700276 /**
Charles Chanc91c8782016-03-30 17:54:24 -0700277 * Constructs the McastEventHandler.
278 *
279 * @param srManager Segment Routing manager
280 */
Charles Chan1eaf4802016-04-18 13:44:03 -0700281 public McastHandler(SegmentRoutingManager srManager) {
Pier7b657162018-03-27 11:29:42 -0700282 ApplicationId coreAppId = srManager.coreService.getAppId(CoreService.CORE_APP_NAME);
Charles Chanc91c8782016-03-30 17:54:24 -0700283 this.srManager = srManager;
Charles Chanc91c8782016-03-30 17:54:24 -0700284 this.topologyService = srManager.topologyService;
Pier7b657162018-03-27 11:29:42 -0700285 KryoNamespace.Builder mcastKryo = new KryoNamespace.Builder()
Charles Chanc91c8782016-03-30 17:54:24 -0700286 .register(KryoNamespaces.API)
Charles Chan72779502016-04-23 17:36:10 -0700287 .register(McastStoreKey.class)
288 .register(McastRole.class);
Pier7b657162018-03-27 11:29:42 -0700289 mcastNextObjStore = srManager.storageService
Charles Chan72779502016-04-23 17:36:10 -0700290 .<McastStoreKey, NextObjective>consistentMapBuilder()
Charles Chanc91c8782016-03-30 17:54:24 -0700291 .withName("onos-mcast-nextobj-store")
Charles Chan4922a172016-05-23 16:45:45 -0700292 .withSerializer(Serializer.using(mcastKryo.build("McastHandler-NextObj")))
Charles Chanc91c8782016-03-30 17:54:24 -0700293 .build();
Pier7b657162018-03-27 11:29:42 -0700294 mcastRoleStore = srManager.storageService
Charles Chan72779502016-04-23 17:36:10 -0700295 .<McastStoreKey, McastRole>consistentMapBuilder()
296 .withName("onos-mcast-role-store")
Charles Chan4922a172016-05-23 16:45:45 -0700297 .withSerializer(Serializer.using(mcastKryo.build("McastHandler-Role")))
Charles Chan72779502016-04-23 17:36:10 -0700298 .build();
Pier7b657162018-03-27 11:29:42 -0700299 // Let's create McastUtils object
300 mcastUtils = new McastUtils(srManager, coreAppId, log);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100301 // Init the executor service and the buckets corrector
302 executorService.scheduleWithFixedDelay(new McastBucketCorrector(), 10,
Pier7b657162018-03-27 11:29:42 -0700303 MCAST_VERIFY_INTERVAL, TimeUnit.SECONDS);
Pier Luigid29ca7c2018-02-28 17:24:03 +0100304 // Schedule the clean up, this will allow the processing of the expired events
305 executorService.scheduleAtFixedRate(mcastEventCache::cleanUp, 0,
306 WAIT_TIME_MS, TimeUnit.MILLISECONDS);
Charles Chan72779502016-04-23 17:36:10 -0700307 }
308
309 /**
310 * Read initial multicast from mcast store.
311 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100312 public void init() {
Pier7b657162018-03-27 11:29:42 -0700313 lastMcastChange = Instant.now();
314 mcastLock();
315 try {
316 srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> {
Pierdb27b8d2018-04-17 16:29:56 +0200317 // Verify leadership on the operation
318 if (!mcastUtils.isLeader(mcastRoute.group())) {
319 log.debug("Skip {} due to lack of leadership", mcastRoute.group());
320 return;
321 }
Pier7b657162018-03-27 11:29:42 -0700322 // FIXME To be addressed with multiple sources support
323 ConnectPoint source = srManager.multicastRouteService.sources(mcastRoute)
324 .stream()
325 .findFirst()
326 .orElse(null);
327 // Get all the sinks and process them
328 McastRouteData mcastRouteData = srManager.multicastRouteService.routeData(mcastRoute);
329 Set<ConnectPoint> sinks = processSinksToBeAdded(source, mcastRoute.group(), mcastRouteData.sinks());
330 // Filter out all the working sinks, we do not want to move them
331 sinks = sinks.stream()
332 .filter(sink -> {
333 McastStoreKey mcastKey = new McastStoreKey(mcastRoute.group(), sink.deviceId());
334 Versioned<NextObjective> verMcastNext = mcastNextObjStore.get(mcastKey);
335 return verMcastNext == null ||
336 !mcastUtils.getPorts(verMcastNext.value().next()).contains(sink.port());
337 })
338 .collect(Collectors.toSet());
339 // Compute the Mcast tree
340 Map<ConnectPoint, List<Path>> mcasTree = computeSinkMcastTree(source.deviceId(), sinks);
341 // Process the given sinks using the pre-computed paths
342 mcasTree.forEach((sink, paths) -> processSinkAddedInternal(source, sink,
343 mcastRoute.group(), paths));
344 });
345 } finally {
346 mcastUnlock();
347 }
Charles Chanc91c8782016-03-30 17:54:24 -0700348 }
349
350 /**
Pier Luigi35dab3f2018-01-25 16:16:02 +0100351 * Clean up when deactivating the application.
352 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100353 public void terminate() {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100354 executorService.shutdown();
355 }
356
357 /**
Pier Luigid29ca7c2018-02-28 17:24:03 +0100358 * Processes the SOURCE_ADDED, SOURCE_UPDATED, SINK_ADDED,
359 * SINK_REMOVED and ROUTE_REMOVED events.
Charles Chanc91c8782016-03-30 17:54:24 -0700360 *
361 * @param event McastEvent with SOURCE_ADDED type
362 */
Pier Luigid29ca7c2018-02-28 17:24:03 +0100363 public void processMcastEvent(McastEvent event) {
364 log.info("process {}", event);
Pierdb27b8d2018-04-17 16:29:56 +0200365 // If it is a route added, we do not enqueue
366 if (event.type() == ROUTE_ADDED) {
367 // We need just to elect a leader
368 processRouteAddedInternal(event.subject().route().group());
369 } else {
370 // Just enqueue for now
371 enqueueMcastEvent(event);
372 }
Pier Luigi6786b922018-02-02 16:19:11 +0100373 }
374
Pierdb27b8d2018-04-17 16:29:56 +0200375
Pier Luigi6786b922018-02-02 16:19:11 +0100376 /**
Pierdb27b8d2018-04-17 16:29:56 +0200377 * Process the ROUTE_ADDED event.
Pier Luigie80d6b42018-02-26 12:31:38 +0100378 *
Pierdb27b8d2018-04-17 16:29:56 +0200379 * @param mcastIp the group address
Pier Luigie80d6b42018-02-26 12:31:38 +0100380 */
Pierdb27b8d2018-04-17 16:29:56 +0200381 private void processRouteAddedInternal(IpAddress mcastIp) {
Pier Luigie80d6b42018-02-26 12:31:38 +0100382 lastMcastChange = Instant.now();
383 mcastLock();
384 try {
Pierdb27b8d2018-04-17 16:29:56 +0200385 log.debug("Processing route added for group {}", mcastIp);
386 // Just elect a new leader
387 mcastUtils.isLeader(mcastIp);
Pier Luigie80d6b42018-02-26 12:31:38 +0100388 } finally {
389 mcastUnlock();
390 }
391 }
392
393 /**
Pier Luigi6786b922018-02-02 16:19:11 +0100394 * Removes the entire mcast tree related to this group.
395 *
396 * @param mcastIp multicast group IP address
397 */
398 private void processRouteRemovedInternal(ConnectPoint source, IpAddress mcastIp) {
399 lastMcastChange = Instant.now();
400 mcastLock();
401 try {
Pier Luigie80d6b42018-02-26 12:31:38 +0100402 log.debug("Processing route removed for group {}", mcastIp);
Pierdb27b8d2018-04-17 16:29:56 +0200403 // Verify leadership on the operation
404 if (!mcastUtils.isLeader(mcastIp)) {
405 log.debug("Skip {} due to lack of leadership", mcastIp);
406 mcastUtils.withdrawLeader(mcastIp);
407 return;
408 }
Pier Luigi6786b922018-02-02 16:19:11 +0100409
410 // Find out the ingress, transit and egress device of the affected group
Pier979e61a2018-03-07 11:42:50 +0100411 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Pier Luigi6786b922018-02-02 16:19:11 +0100412 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -0700413 Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT);
Pier979e61a2018-03-07 11:42:50 +0100414 Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS);
Pier Luigi6786b922018-02-02 16:19:11 +0100415
Pier1a7e0c02018-03-12 15:00:54 -0700416 // If there are no egress devices, sinks could be only on the ingress
Pier Luigi6786b922018-02-02 16:19:11 +0100417 if (!egressDevices.isEmpty()) {
418 egressDevices.forEach(
Pier7b657162018-03-27 11:29:42 -0700419 deviceId -> removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null))
Pier Luigi6786b922018-02-02 16:19:11 +0100420 );
421 }
Pier1a7e0c02018-03-12 15:00:54 -0700422 // Transit could be empty if sinks are on the ingress
423 if (!transitDevices.isEmpty()) {
424 transitDevices.forEach(
Pier7b657162018-03-27 11:29:42 -0700425 deviceId -> removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null))
Pier1a7e0c02018-03-12 15:00:54 -0700426 );
Pier Luigi6786b922018-02-02 16:19:11 +0100427 }
428 // Ingress device should be not null
429 if (ingressDevice != null) {
Pier7b657162018-03-27 11:29:42 -0700430 removeGroupFromDevice(ingressDevice, mcastIp, mcastUtils.assignedVlan(source));
Pier Luigi6786b922018-02-02 16:19:11 +0100431 }
Pier Luigi6786b922018-02-02 16:19:11 +0100432 } finally {
433 mcastUnlock();
434 }
435 }
436
Pier7b657162018-03-27 11:29:42 -0700437
438 /**
439 * Process sinks to be removed.
440 *
441 * @param source the source connect point
442 * @param mcastIp the ip address of the group
443 * @param newSinks the new sinks to be processed
Pier28164682018-04-17 15:50:43 +0200444 * @param prevSinks the previous sinks
Pier7b657162018-03-27 11:29:42 -0700445 */
446 private void processSinksRemovedInternal(ConnectPoint source, IpAddress mcastIp,
447 Map<HostId, Set<ConnectPoint>> newSinks,
Pier28164682018-04-17 15:50:43 +0200448 Map<HostId, Set<ConnectPoint>> prevSinks) {
Pier7b657162018-03-27 11:29:42 -0700449 lastMcastChange = Instant.now();
450 mcastLock();
Pier7b657162018-03-27 11:29:42 -0700451 try {
Pierdb27b8d2018-04-17 16:29:56 +0200452 // Verify leadership on the operation
453 if (!mcastUtils.isLeader(mcastIp)) {
454 log.debug("Skip {} due to lack of leadership", mcastIp);
455 return;
456 }
Pier28164682018-04-17 15:50:43 +0200457 // Remove the previous ones
458 Set<ConnectPoint> sinksToBeRemoved = processSinksToBeRemoved(mcastIp, prevSinks,
459 newSinks);
460 sinksToBeRemoved.forEach(sink -> processSinkRemovedInternal(source, sink, mcastIp));
Pier7b657162018-03-27 11:29:42 -0700461 // Recover the dual-homed sinks
Pier28164682018-04-17 15:50:43 +0200462 Set<ConnectPoint> sinksToBeRecovered = processSinksToBeRecovered(mcastIp, newSinks,
463 prevSinks);
Pier7b657162018-03-27 11:29:42 -0700464 sinksToBeRecovered.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, null));
Pier7b657162018-03-27 11:29:42 -0700465 } finally {
466 mcastUnlock();
Pier7b657162018-03-27 11:29:42 -0700467 }
468 }
469
Pier Luigi6786b922018-02-02 16:19:11 +0100470 /**
Pier Luigi35dab3f2018-01-25 16:16:02 +0100471 * Removes a path from source to sink for given multicast group.
472 *
473 * @param source connect point of the multicast source
474 * @param sink connection point of the multicast sink
475 * @param mcastIp multicast group IP address
476 */
477 private void processSinkRemovedInternal(ConnectPoint source, ConnectPoint sink,
Pier7b657162018-03-27 11:29:42 -0700478 IpAddress mcastIp) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100479 lastMcastChange = Instant.now();
480 mcastLock();
481 try {
Pier7b657162018-03-27 11:29:42 -0700482 boolean isLast;
Pier Luigi35dab3f2018-01-25 16:16:02 +0100483 // When source and sink are on the same device
484 if (source.deviceId().equals(sink.deviceId())) {
485 // Source and sink are on even the same port. There must be something wrong.
486 if (source.port().equals(sink.port())) {
487 log.warn("Skip {} since sink {} is on the same port of source {}. Abort",
488 mcastIp, sink, source);
489 return;
490 }
Pier7b657162018-03-27 11:29:42 -0700491 isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source));
Pier Luigi92e69be2018-03-02 12:53:37 +0100492 if (isLast) {
493 mcastRoleStore.remove(new McastStoreKey(mcastIp, sink.deviceId()));
494 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100495 return;
496 }
Charles Chanc91c8782016-03-30 17:54:24 -0700497
Pier Luigi35dab3f2018-01-25 16:16:02 +0100498 // Process the egress device
Pier7b657162018-03-27 11:29:42 -0700499 isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100500 if (isLast) {
501 mcastRoleStore.remove(new McastStoreKey(mcastIp, sink.deviceId()));
502 }
503
504 // If this is the last sink on the device, also update upstream
Pier1f87aca2018-03-14 16:47:32 -0700505 Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(),
506 mcastIp, null);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100507 if (mcastPath.isPresent()) {
508 List<Link> links = Lists.newArrayList(mcastPath.get().links());
509 Collections.reverse(links);
510 for (Link link : links) {
511 if (isLast) {
512 isLast = removePortFromDevice(
513 link.src().deviceId(),
514 link.src().port(),
515 mcastIp,
Pier7b657162018-03-27 11:29:42 -0700516 mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ?
517 source : null)
Pier Luigi35dab3f2018-01-25 16:16:02 +0100518 );
Pier Luigi92e69be2018-03-02 12:53:37 +0100519 if (isLast) {
520 mcastRoleStore.remove(new McastStoreKey(mcastIp, link.src().deviceId()));
521 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100522 }
Charles Chanc91c8782016-03-30 17:54:24 -0700523 }
524 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100525 } finally {
526 mcastUnlock();
Charles Chanc91c8782016-03-30 17:54:24 -0700527 }
528 }
529
Pier7b657162018-03-27 11:29:42 -0700530
531 /**
532 * Process sinks to be added.
533 *
534 * @param source the source connect point
535 * @param mcastIp the group IP
536 * @param newSinks the new sinks to be processed
537 * @param allPrevSinks all previous sinks
538 */
539 private void processSinksAddedInternal(ConnectPoint source, IpAddress mcastIp,
540 Map<HostId, Set<ConnectPoint>> newSinks,
541 Set<ConnectPoint> allPrevSinks) {
542 lastMcastChange = Instant.now();
543 mcastLock();
544 try {
Pierdb27b8d2018-04-17 16:29:56 +0200545 // Verify leadership on the operation
546 if (!mcastUtils.isLeader(mcastIp)) {
547 log.debug("Skip {} due to lack of leadership", mcastIp);
548 return;
549 }
Pier7b657162018-03-27 11:29:42 -0700550 // Get the only sinks to be processed (new ones)
551 Set<ConnectPoint> sinksToBeAdded = processSinksToBeAdded(source, mcastIp, newSinks);
552 // Install new sinks
553 sinksToBeAdded = Sets.difference(sinksToBeAdded, allPrevSinks);
554 sinksToBeAdded.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, null));
555 } finally {
556 mcastUnlock();
557 }
558 }
559
Charles Chanc91c8782016-03-30 17:54:24 -0700560 /**
561 * Establishes a path from source to sink for given multicast group.
562 *
563 * @param source connect point of the multicast source
564 * @param sink connection point of the multicast sink
565 * @param mcastIp multicast group IP address
566 */
567 private void processSinkAddedInternal(ConnectPoint source, ConnectPoint sink,
Pier7b657162018-03-27 11:29:42 -0700568 IpAddress mcastIp, List<Path> allPaths) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100569 lastMcastChange = Instant.now();
570 mcastLock();
571 try {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100572 // Process the ingress device
Pier7b657162018-03-27 11:29:42 -0700573 mcastUtils.addFilterToDevice(source.deviceId(), source.port(),
574 mcastUtils.assignedVlan(source), mcastIp, INGRESS);
Charles Chan72779502016-04-23 17:36:10 -0700575
Pier Luigi35dab3f2018-01-25 16:16:02 +0100576 // When source and sink are on the same device
577 if (source.deviceId().equals(sink.deviceId())) {
578 // Source and sink are on even the same port. There must be something wrong.
579 if (source.port().equals(sink.port())) {
580 log.warn("Skip {} since sink {} is on the same port of source {}. Abort",
581 mcastIp, sink, source);
582 return;
583 }
Pier7b657162018-03-27 11:29:42 -0700584 addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source));
Pier979e61a2018-03-07 11:42:50 +0100585 mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()), INGRESS);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100586 return;
587 }
Charles Chan72779502016-04-23 17:36:10 -0700588
Pier Luigi35dab3f2018-01-25 16:16:02 +0100589 // Find a path. If present, create/update groups and flows for each hop
Pier1f87aca2018-03-14 16:47:32 -0700590 Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(),
591 mcastIp, allPaths);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100592 if (mcastPath.isPresent()) {
593 List<Link> links = mcastPath.get().links();
Charles Chan72779502016-04-23 17:36:10 -0700594
Pier1a7e0c02018-03-12 15:00:54 -0700595 // Setup mcast role for ingress
596 mcastRoleStore.put(new McastStoreKey(mcastIp, source.deviceId()),
597 INGRESS);
598
599 // Setup properly the transit
Pier Luigi35dab3f2018-01-25 16:16:02 +0100600 links.forEach(link -> {
601 addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp,
Pier7b657162018-03-27 11:29:42 -0700602 mcastUtils.assignedVlan(link.src().deviceId()
603 .equals(source.deviceId()) ? source : null));
604 mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(),
605 mcastUtils.assignedVlan(null), mcastIp, null);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100606 });
607
Pier1a7e0c02018-03-12 15:00:54 -0700608 // Setup mcast role for the transit
609 links.stream()
610 .filter(link -> !link.dst().deviceId().equals(sink.deviceId()))
611 .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.dst().deviceId()),
612 TRANSIT));
613
Pier Luigi35dab3f2018-01-25 16:16:02 +0100614 // Process the egress device
Pier7b657162018-03-27 11:29:42 -0700615 addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null));
Pier1a7e0c02018-03-12 15:00:54 -0700616 // Setup mcast role for egress
Pier Luigi35dab3f2018-01-25 16:16:02 +0100617 mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()),
Pier979e61a2018-03-07 11:42:50 +0100618 EGRESS);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100619 } else {
620 log.warn("Unable to find a path from {} to {}. Abort sinkAdded",
621 source.deviceId(), sink.deviceId());
622 }
623 } finally {
624 mcastUnlock();
Charles Chanc91c8782016-03-30 17:54:24 -0700625 }
626 }
627
628 /**
Charles Chan72779502016-04-23 17:36:10 -0700629 * Processes the LINK_DOWN event.
630 *
631 * @param affectedLink Link that is going down
632 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100633 public void processLinkDown(Link affectedLink) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100634 lastMcastChange = Instant.now();
635 mcastLock();
636 try {
637 // Get groups affected by the link down event
638 getAffectedGroups(affectedLink).forEach(mcastIp -> {
639 // TODO Optimize when the group editing is in place
640 log.debug("Processing link down {} for group {}",
641 affectedLink, mcastIp);
Pierdb27b8d2018-04-17 16:29:56 +0200642 // Verify leadership on the operation
643 if (!mcastUtils.isLeader(mcastIp)) {
644 log.debug("Skip {} due to lack of leadership", mcastIp);
645 return;
646 }
Pier Luigi580fd8a2018-01-16 10:47:50 +0100647
Pier Luigi35dab3f2018-01-25 16:16:02 +0100648 // Find out the ingress, transit and egress device of affected group
Pier979e61a2018-03-07 11:42:50 +0100649 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Pier Luigi35dab3f2018-01-25 16:16:02 +0100650 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -0700651 Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT);
Pier979e61a2018-03-07 11:42:50 +0100652 Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS);
Pier7b657162018-03-27 11:29:42 -0700653 ConnectPoint source = mcastUtils.getSource(mcastIp);
Charles Chana8f9dee2016-05-16 18:44:13 -0700654
Pier1a7e0c02018-03-12 15:00:54 -0700655 // Do not proceed if ingress device or source of this group are missing
656 // If sinks are in other leafs, we have ingress, transit, egress, and source
657 // If sinks are in the same leaf, we have just ingress and source
658 if (ingressDevice == null || source == null) {
659 log.warn("Missing ingress {} or source {} for group {}",
660 ingressDevice, source, mcastIp);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100661 return;
Charles Chan72779502016-04-23 17:36:10 -0700662 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100663
Pier Luigi35dab3f2018-01-25 16:16:02 +0100664 // Remove entire transit
Pier1a7e0c02018-03-12 15:00:54 -0700665 transitDevices.forEach(transitDevice ->
Pier7b657162018-03-27 11:29:42 -0700666 removeGroupFromDevice(transitDevice, mcastIp,
667 mcastUtils.assignedVlan(null)));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100668
Pier1a7e0c02018-03-12 15:00:54 -0700669 // Remove transit-facing ports on the ingress device
670 removeIngressTransitPorts(mcastIp, ingressDevice, source);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100671
Pier7b657162018-03-27 11:29:42 -0700672 // TODO create a shared procedure with DEVICE_DOWN
Pier1f87aca2018-03-14 16:47:32 -0700673 // Compute mcast tree for the the egress devices
674 Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, egressDevices);
675
Pier7b657162018-03-27 11:29:42 -0700676 // We have to verify, if there are egresses without paths
677 Set<DeviceId> notRecovered = Sets.newHashSet();
Pier1f87aca2018-03-14 16:47:32 -0700678 mcastTree.forEach((egressDevice, paths) -> {
Pier7b657162018-03-27 11:29:42 -0700679 // Let's check if there is at least a path
Pier1f87aca2018-03-14 16:47:32 -0700680 Optional<Path> mcastPath = getPath(ingressDevice, egressDevice,
681 mcastIp, paths);
Pier7b657162018-03-27 11:29:42 -0700682 // No paths, we have to try with alternative location
683 if (!mcastPath.isPresent()) {
684 notRecovered.add(egressDevice);
685 // We were not able to find an alternative path for this egress
Pier Luigi35dab3f2018-01-25 16:16:02 +0100686 log.warn("Fail to recover egress device {} from link failure {}",
687 egressDevice, affectedLink);
Pier7b657162018-03-27 11:29:42 -0700688 removeGroupFromDevice(egressDevice, mcastIp,
689 mcastUtils.assignedVlan(null));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100690 }
691 });
Pier7b657162018-03-27 11:29:42 -0700692
693 // Fast path, we can recover all the locations
694 if (notRecovered.isEmpty()) {
695 // Construct a new path for each egress device
696 mcastTree.forEach((egressDevice, paths) -> {
697 // We try to enforce the sinks path on the mcast tree
698 Optional<Path> mcastPath = getPath(ingressDevice, egressDevice,
699 mcastIp, paths);
700 // If a path is present, let's install it
701 if (mcastPath.isPresent()) {
702 installPath(mcastIp, source, mcastPath.get());
703 }
704 });
705 } else {
706 // Let's try to recover using alternate
707 recoverSinks(egressDevices, notRecovered, mcastIp,
708 ingressDevice, source, true);
709 }
Charles Chan72779502016-04-23 17:36:10 -0700710 });
Pier Luigi35dab3f2018-01-25 16:16:02 +0100711 } finally {
712 mcastUnlock();
713 }
Charles Chan72779502016-04-23 17:36:10 -0700714 }
715
716 /**
Pier Luigi580fd8a2018-01-16 10:47:50 +0100717 * Process the DEVICE_DOWN event.
718 *
719 * @param deviceDown device going down
720 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100721 public void processDeviceDown(DeviceId deviceDown) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100722 lastMcastChange = Instant.now();
723 mcastLock();
724 try {
725 // Get the mcast groups affected by the device going down
726 getAffectedGroups(deviceDown).forEach(mcastIp -> {
727 // TODO Optimize when the group editing is in place
728 log.debug("Processing device down {} for group {}",
729 deviceDown, mcastIp);
Pierdb27b8d2018-04-17 16:29:56 +0200730 // Verify leadership on the operation
731 if (!mcastUtils.isLeader(mcastIp)) {
732 log.debug("Skip {} due to lack of leadership", mcastIp);
733 return;
734 }
Pier Luigi580fd8a2018-01-16 10:47:50 +0100735
Pier Luigi35dab3f2018-01-25 16:16:02 +0100736 // Find out the ingress, transit and egress device of affected group
Pier979e61a2018-03-07 11:42:50 +0100737 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Pier Luigi35dab3f2018-01-25 16:16:02 +0100738 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -0700739 Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT);
Pier979e61a2018-03-07 11:42:50 +0100740 Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS);
Pier7b657162018-03-27 11:29:42 -0700741 ConnectPoint source = mcastUtils.getSource(mcastIp);
Pier Luigi580fd8a2018-01-16 10:47:50 +0100742
Pier Luigi35dab3f2018-01-25 16:16:02 +0100743 // Do not proceed if ingress device or source of this group are missing
744 // If sinks are in other leafs, we have ingress, transit, egress, and source
745 // If sinks are in the same leaf, we have just ingress and source
746 if (ingressDevice == null || source == null) {
747 log.warn("Missing ingress {} or source {} for group {}",
748 ingressDevice, source, mcastIp);
Pier Luigi580fd8a2018-01-16 10:47:50 +0100749 return;
750 }
Pier Luigi580fd8a2018-01-16 10:47:50 +0100751
Pier Luigi35dab3f2018-01-25 16:16:02 +0100752 // If it exists, we have to remove it in any case
Pier1a7e0c02018-03-12 15:00:54 -0700753 if (!transitDevices.isEmpty()) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100754 // Remove entire transit
Pier1a7e0c02018-03-12 15:00:54 -0700755 transitDevices.forEach(transitDevice ->
Pier7b657162018-03-27 11:29:42 -0700756 removeGroupFromDevice(transitDevice, mcastIp,
757 mcastUtils.assignedVlan(null)));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100758 }
759 // If the ingress is down
760 if (ingressDevice.equals(deviceDown)) {
761 // Remove entire ingress
Pier7b657162018-03-27 11:29:42 -0700762 removeGroupFromDevice(ingressDevice, mcastIp, mcastUtils.assignedVlan(source));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100763 // If other sinks different from the ingress exist
764 if (!egressDevices.isEmpty()) {
765 // Remove all the remaining egress
766 egressDevices.forEach(
Pier7b657162018-03-27 11:29:42 -0700767 egressDevice -> removeGroupFromDevice(egressDevice, mcastIp,
768 mcastUtils.assignedVlan(null))
Pier Luigi35dab3f2018-01-25 16:16:02 +0100769 );
Pier Luigi580fd8a2018-01-16 10:47:50 +0100770 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100771 } else {
772 // Egress or transit could be down at this point
Pier1a7e0c02018-03-12 15:00:54 -0700773 // Get the ingress-transit ports if they exist
774 removeIngressTransitPorts(mcastIp, ingressDevice, source);
775
Pier Luigi35dab3f2018-01-25 16:16:02 +0100776 // One of the egress device is down
777 if (egressDevices.contains(deviceDown)) {
778 // Remove entire device down
Pier7b657162018-03-27 11:29:42 -0700779 removeGroupFromDevice(deviceDown, mcastIp, mcastUtils.assignedVlan(null));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100780 // Remove the device down from egress
781 egressDevices.remove(deviceDown);
782 // If there are no more egress and ingress does not have sinks
783 if (egressDevices.isEmpty() && !hasSinks(ingressDevice, mcastIp)) {
Pier Luigi35dab3f2018-01-25 16:16:02 +0100784 // We have done
785 return;
786 }
787 }
Pier1f87aca2018-03-14 16:47:32 -0700788
789 // Compute mcast tree for the the egress devices
790 Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, egressDevices);
791
Pier7b657162018-03-27 11:29:42 -0700792 // We have to verify, if there are egresses without paths
793 Set<DeviceId> notRecovered = Sets.newHashSet();
Pier1f87aca2018-03-14 16:47:32 -0700794 mcastTree.forEach((egressDevice, paths) -> {
Pier7b657162018-03-27 11:29:42 -0700795 // Let's check if there is at least a path
Pier1f87aca2018-03-14 16:47:32 -0700796 Optional<Path> mcastPath = getPath(ingressDevice, egressDevice,
Pier7b657162018-03-27 11:29:42 -0700797 mcastIp, paths);
798 // No paths, we have to try with alternative location
799 if (!mcastPath.isPresent()) {
800 notRecovered.add(egressDevice);
Pier Luigi35dab3f2018-01-25 16:16:02 +0100801 // We were not able to find an alternative path for this egress
802 log.warn("Fail to recover egress device {} from device down {}",
803 egressDevice, deviceDown);
Pier7b657162018-03-27 11:29:42 -0700804 removeGroupFromDevice(egressDevice, mcastIp, mcastUtils.assignedVlan(null));
Pier Luigi35dab3f2018-01-25 16:16:02 +0100805 }
806 });
Pier7b657162018-03-27 11:29:42 -0700807
808 // Fast path, we can recover all the locations
809 if (notRecovered.isEmpty()) {
810 // Construct a new path for each egress device
811 mcastTree.forEach((egressDevice, paths) -> {
812 // We try to enforce the sinks path on the mcast tree
813 Optional<Path> mcastPath = getPath(ingressDevice, egressDevice,
814 mcastIp, paths);
815 // If a path is present, let's install it
816 if (mcastPath.isPresent()) {
817 installPath(mcastIp, source, mcastPath.get());
818 }
819 });
820 } else {
821 // Let's try to recover using alternate
822 recoverSinks(egressDevices, notRecovered, mcastIp,
823 ingressDevice, source, false);
824 }
Pier Luigi35dab3f2018-01-25 16:16:02 +0100825 }
826 });
827 } finally {
828 mcastUnlock();
829 }
Pier Luigi580fd8a2018-01-16 10:47:50 +0100830 }
831
832 /**
Pier7b657162018-03-27 11:29:42 -0700833 * Try to recover sinks using alternate locations.
834 *
835 * @param egressDevices the original egress devices
836 * @param notRecovered the devices not recovered
837 * @param mcastIp the group address
838 * @param ingressDevice the ingress device
839 * @param source the source connect point
840 * @param isLinkFailure true if it is a link failure, otherwise false
841 */
842 private void recoverSinks(Set<DeviceId> egressDevices, Set<DeviceId> notRecovered,
843 IpAddress mcastIp, DeviceId ingressDevice, ConnectPoint source,
844 boolean isLinkFailure) {
845 // Recovered devices
846 Set<DeviceId> recovered = Sets.difference(egressDevices, notRecovered);
847 // Total affected sinks
848 Set<ConnectPoint> totalAffectedSinks = Sets.newHashSet();
849 // Total sinks
850 Set<ConnectPoint> totalSinks = Sets.newHashSet();
851 // Let's compute all the affected sinks and all the sinks
852 notRecovered.forEach(deviceId -> {
853 totalAffectedSinks.addAll(
854 mcastUtils.getAffectedSinks(deviceId, mcastIp)
855 .values()
856 .stream()
857 .flatMap(Collection::stream)
858 .filter(connectPoint -> connectPoint.deviceId().equals(deviceId))
859 .collect(Collectors.toSet())
860 );
861 totalSinks.addAll(
862 mcastUtils.getAffectedSinks(deviceId, mcastIp)
863 .values()
864 .stream()
865 .flatMap(Collection::stream)
866 .collect(Collectors.toSet())
867 );
868 });
869
870 // Sinks to be added
871 Set<ConnectPoint> sinksToBeAdded = Sets.difference(totalSinks, totalAffectedSinks);
872 // New egress devices, filtering out the source
873 Set<DeviceId> newEgressDevice = sinksToBeAdded.stream()
874 .map(ConnectPoint::deviceId)
875 .collect(Collectors.toSet());
876 // Let's add the devices recovered from the previous round
877 newEgressDevice.addAll(recovered);
878 // Let's do a copy of the new egresses and filter out the source
879 Set<DeviceId> copyNewEgressDevice = ImmutableSet.copyOf(newEgressDevice);
880 newEgressDevice = newEgressDevice.stream()
881 .filter(deviceId -> !deviceId.equals(ingressDevice))
882 .collect(Collectors.toSet());
883
884 // Re-compute mcast tree for the the egress devices
885 Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, newEgressDevice);
886 // if the source was originally in the new locations, add new sinks
887 if (copyNewEgressDevice.contains(ingressDevice)) {
888 sinksToBeAdded.stream()
889 .filter(connectPoint -> connectPoint.deviceId().equals(ingressDevice))
890 .forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, ImmutableList.of()));
891 }
892
893 // Construct a new path for each egress device
894 mcastTree.forEach((egressDevice, paths) -> {
895 // We try to enforce the sinks path on the mcast tree
896 Optional<Path> mcastPath = getPath(ingressDevice, egressDevice,
897 mcastIp, paths);
898 // If a path is present, let's install it
899 if (mcastPath.isPresent()) {
900 // Using recovery procedure
901 if (recovered.contains(egressDevice)) {
902 installPath(mcastIp, source, mcastPath.get());
903 } else {
904 // otherwise we need to threat as new sink
905 sinksToBeAdded.stream()
906 .filter(connectPoint -> connectPoint.deviceId().equals(egressDevice))
907 .forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, paths));
908 }
909 } else {
910 // We were not able to find an alternative path for this egress
911 log.warn("Fail to recover egress device {} from {} failure",
912 egressDevice, isLinkFailure ? "Link" : "Device");
913 removeGroupFromDevice(egressDevice, mcastIp, mcastUtils.assignedVlan(null));
914 }
915 });
916
917 }
918
919 /**
Pier28164682018-04-17 15:50:43 +0200920 * Process all the sinks related to a mcast group and return
921 * the ones to be removed.
922 *
923 * @param mcastIp the group address
924 * @param prevsinks the previous sinks to be evaluated
925 * @param newSinks the new sinks to be evaluted
926 * @return the set of the sinks to be removed
927 */
928 private Set<ConnectPoint> processSinksToBeRemoved(IpAddress mcastIp,
929 Map<HostId, Set<ConnectPoint>> prevsinks,
930 Map<HostId, Set<ConnectPoint>> newSinks) {
931 // Iterate over the sinks in order to build the set
932 // of the connect points to be removed from this group
933 final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet();
934 prevsinks.forEach(((hostId, connectPoints) -> {
935 // We have to check with the existing flows
936 ConnectPoint sinkToBeProcessed = connectPoints.stream()
937 .filter(connectPoint -> isSink(mcastIp, connectPoint))
938 .findFirst().orElse(null);
939 if (sinkToBeProcessed != null) {
940 // If the host has been removed or location has been removed
941 if (!newSinks.containsKey(hostId) ||
942 !newSinks.get(hostId).contains(sinkToBeProcessed)) {
943 sinksToBeProcessed.add(sinkToBeProcessed);
944 }
945 }
946 }));
947 // We have done, return the set
948 return sinksToBeProcessed;
949 }
950
951 /**
Pier7b657162018-03-27 11:29:42 -0700952 * Process new locations and return the set of sinks to be added
953 * in the context of the recovery.
954 *
Pier28164682018-04-17 15:50:43 +0200955 * @param newSinks the remaining sinks
956 * @param prevSinks the previous sinks
Pier7b657162018-03-27 11:29:42 -0700957 * @return the set of the sinks to be processed
958 */
959 private Set<ConnectPoint> processSinksToBeRecovered(IpAddress mcastIp,
Pier28164682018-04-17 15:50:43 +0200960 Map<HostId, Set<ConnectPoint>> newSinks,
961 Map<HostId, Set<ConnectPoint>> prevSinks) {
Pier7b657162018-03-27 11:29:42 -0700962 // Iterate over the sinks in order to build the set
963 // of the connect points to be served by this group
964 final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet();
Pier28164682018-04-17 15:50:43 +0200965 newSinks.forEach((hostId, connectPoints) -> {
Pier7b657162018-03-27 11:29:42 -0700966 // If it has more than 1 locations
967 if (connectPoints.size() > 1 || connectPoints.size() == 0) {
968 log.debug("Skip {} since sink {} has {} locations",
969 mcastIp, hostId, connectPoints.size());
970 return;
971 }
Pier28164682018-04-17 15:50:43 +0200972 // If previously it had two locations, we need to recover it
973 // Filter out if the remaining location is already served
974 if (prevSinks.containsKey(hostId) && prevSinks.get(hostId).size() == 2) {
975 sinksToBeProcessed.add(connectPoints.stream()
976 .filter(connectPoint -> !isSink(mcastIp, connectPoint))
977 .findFirst().orElseGet(null));
978 }
Pier7b657162018-03-27 11:29:42 -0700979 });
980 return sinksToBeProcessed;
981 }
982
983 /**
984 * Process all the sinks related to a mcast group and return
985 * the ones to be processed.
986 *
987 * @param source the source connect point
988 * @param mcastIp the group address
989 * @param sinks the sinks to be evaluated
990 * @return the set of the sinks to be processed
991 */
992 private Set<ConnectPoint> processSinksToBeAdded(ConnectPoint source, IpAddress mcastIp,
993 Map<HostId, Set<ConnectPoint>> sinks) {
994 // Iterate over the sinks in order to build the set
995 // of the connect points to be served by this group
996 final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet();
997 sinks.forEach(((hostId, connectPoints) -> {
998 // If it has more than 2 locations
999 if (connectPoints.size() > 2 || connectPoints.size() == 0) {
1000 log.debug("Skip {} since sink {} has {} locations",
1001 mcastIp, hostId, connectPoints.size());
1002 return;
1003 }
1004 // If it has one location, just use it
1005 if (connectPoints.size() == 1) {
1006 sinksToBeProcessed.add(connectPoints.stream()
1007 .findFirst().orElseGet(null));
1008 return;
1009 }
1010 // We prefer to reuse existing flows
1011 ConnectPoint sinkToBeProcessed = connectPoints.stream()
Pier28164682018-04-17 15:50:43 +02001012 .filter(connectPoint -> isSink(mcastIp, connectPoint))
Pier7b657162018-03-27 11:29:42 -07001013 .findFirst().orElse(null);
1014 if (sinkToBeProcessed != null) {
1015 sinksToBeProcessed.add(sinkToBeProcessed);
1016 return;
1017 }
1018 // Otherwise we prefer to reuse existing egresses
1019 Set<DeviceId> egresses = getDevice(mcastIp, EGRESS);
1020 sinkToBeProcessed = connectPoints.stream()
Pier28164682018-04-17 15:50:43 +02001021 .filter(connectPoint -> egresses.contains(connectPoint.deviceId()))
Pier7b657162018-03-27 11:29:42 -07001022 .findFirst().orElse(null);
1023 if (sinkToBeProcessed != null) {
1024 sinksToBeProcessed.add(sinkToBeProcessed);
1025 return;
1026 }
1027 // Otherwise we prefer a location co-located with the source (if it exists)
1028 sinkToBeProcessed = connectPoints.stream()
1029 .filter(connectPoint -> connectPoint.deviceId().equals(source.deviceId()))
1030 .findFirst().orElse(null);
1031 if (sinkToBeProcessed != null) {
1032 sinksToBeProcessed.add(sinkToBeProcessed);
1033 return;
1034 }
1035 // Finally, we randomly pick a new location
1036 sinksToBeProcessed.add(connectPoints.stream()
1037 .findFirst().orElseGet(null));
1038 }));
1039 // We have done, return the set
1040 return sinksToBeProcessed;
1041 }
1042
1043 /**
Pier1a7e0c02018-03-12 15:00:54 -07001044 * Utility method to remove all the ingress transit ports.
1045 *
1046 * @param mcastIp the group ip
1047 * @param ingressDevice the ingress device for this group
1048 * @param source the source connect point
1049 */
1050 private void removeIngressTransitPorts(IpAddress mcastIp, DeviceId ingressDevice,
1051 ConnectPoint source) {
1052 Set<PortNumber> ingressTransitPorts = ingressTransitPort(mcastIp);
1053 ingressTransitPorts.forEach(ingressTransitPort -> {
1054 if (ingressTransitPort != null) {
1055 boolean isLast = removePortFromDevice(ingressDevice, ingressTransitPort,
Pier7b657162018-03-27 11:29:42 -07001056 mcastIp, mcastUtils.assignedVlan(source));
Pier1a7e0c02018-03-12 15:00:54 -07001057 if (isLast) {
1058 mcastRoleStore.remove(new McastStoreKey(mcastIp, ingressDevice));
1059 }
1060 }
1061 });
1062 }
1063
1064 /**
Charles Chanc91c8782016-03-30 17:54:24 -07001065 * Adds a port to given multicast group on given device. This involves the
1066 * update of L3 multicast group and multicast routing table entry.
1067 *
1068 * @param deviceId device ID
1069 * @param port port to be added
1070 * @param mcastIp multicast group
1071 * @param assignedVlan assigned VLAN ID
1072 */
1073 private void addPortToDevice(DeviceId deviceId, PortNumber port,
Pier7b657162018-03-27 11:29:42 -07001074 IpAddress mcastIp, VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001075 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId);
Charles Chanc91c8782016-03-30 17:54:24 -07001076 ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder();
Pier Luigi4f0dd212018-01-19 10:24:53 +01001077 NextObjective newNextObj;
Charles Chan72779502016-04-23 17:36:10 -07001078 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
Charles Chanc91c8782016-03-30 17:54:24 -07001079 // First time someone request this mcast group via this device
1080 portBuilder.add(port);
Pier Luigi4f0dd212018-01-19 10:24:53 +01001081 // New nextObj
Pier7b657162018-03-27 11:29:42 -07001082 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001083 portBuilder.build(), null).add();
1084 // Store the new port
1085 mcastNextObjStore.put(mcastStoreKey, newNextObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001086 } else {
1087 // This device already serves some subscribers of this mcast group
Charles Chan72779502016-04-23 17:36:10 -07001088 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Charles Chanc91c8782016-03-30 17:54:24 -07001089 // Stop if the port is already in the nextobj
Pier7b657162018-03-27 11:29:42 -07001090 Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next());
Charles Chanc91c8782016-03-30 17:54:24 -07001091 if (existingPorts.contains(port)) {
1092 log.info("NextObj for {}/{} already exists. Abort", deviceId, port);
1093 return;
1094 }
Pier Luigi4f0dd212018-01-19 10:24:53 +01001095 // Let's add the port and reuse the previous one
Yuta HIGUCHIbef07b52018-02-09 18:05:23 -08001096 portBuilder.addAll(existingPorts).add(port);
Pier Luigi4f0dd212018-01-19 10:24:53 +01001097 // Reuse previous nextObj
Pier7b657162018-03-27 11:29:42 -07001098 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001099 portBuilder.build(), nextObj.id()).addToExisting();
1100 // Store the final next objective and send only the difference to the driver
1101 mcastNextObjStore.put(mcastStoreKey, newNextObj);
1102 // Add just the new port
1103 portBuilder = ImmutableSet.builder();
1104 portBuilder.add(port);
Pier7b657162018-03-27 11:29:42 -07001105 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001106 portBuilder.build(), nextObj.id()).addToExisting();
Charles Chanc91c8782016-03-30 17:54:24 -07001107 }
1108 // Create, store and apply the new nextObj and fwdObj
Charles Chan72779502016-04-23 17:36:10 -07001109 ObjectiveContext context = new DefaultObjectiveContext(
1110 (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}",
1111 mcastIp, deviceId, port.toLong(), assignedVlan),
1112 (objective, error) ->
1113 log.warn("Failed to add {} on {}/{}, vlan {}: {}",
1114 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001115 ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan,
1116 newNextObj.id()).add(context);
Charles Chanc91c8782016-03-30 17:54:24 -07001117 srManager.flowObjectiveService.next(deviceId, newNextObj);
1118 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001119 }
1120
1121 /**
1122 * Removes a port from given multicast group on given device.
1123 * This involves the update of L3 multicast group and multicast routing
1124 * table entry.
1125 *
1126 * @param deviceId device ID
1127 * @param port port to be added
1128 * @param mcastIp multicast group
1129 * @param assignedVlan assigned VLAN ID
1130 * @return true if this is the last sink on this device
1131 */
1132 private boolean removePortFromDevice(DeviceId deviceId, PortNumber port,
Pier7b657162018-03-27 11:29:42 -07001133 IpAddress mcastIp, VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001134 McastStoreKey mcastStoreKey =
1135 new McastStoreKey(mcastIp, deviceId);
Charles Chanc91c8782016-03-30 17:54:24 -07001136 // This device is not serving this multicast group
Charles Chan72779502016-04-23 17:36:10 -07001137 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
Charles Chanc91c8782016-03-30 17:54:24 -07001138 log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port);
1139 return false;
1140 }
Charles Chan72779502016-04-23 17:36:10 -07001141 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Charles Chanc91c8782016-03-30 17:54:24 -07001142
Pier7b657162018-03-27 11:29:42 -07001143 Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next());
Charles Chan72779502016-04-23 17:36:10 -07001144 // This port does not serve this multicast group
Charles Chanc91c8782016-03-30 17:54:24 -07001145 if (!existingPorts.contains(port)) {
1146 log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port);
1147 return false;
1148 }
1149 // Copy and modify the ImmutableSet
1150 existingPorts = Sets.newHashSet(existingPorts);
1151 existingPorts.remove(port);
1152
1153 NextObjective newNextObj;
Pier Luigi8cd46de2018-01-19 10:24:53 +01001154 ObjectiveContext context;
Charles Chanc91c8782016-03-30 17:54:24 -07001155 ForwardingObjective fwdObj;
1156 if (existingPorts.isEmpty()) {
Pier Luigi8cd46de2018-01-19 10:24:53 +01001157 // If this is the last sink, remove flows and last bucket
Charles Chanc91c8782016-03-30 17:54:24 -07001158 // NOTE: Rely on GroupStore garbage collection rather than explicitly
1159 // remove L3MG since there might be other flows/groups refer to
1160 // the same L2IG
Pier Luigi8cd46de2018-01-19 10:24:53 +01001161 context = new DefaultObjectiveContext(
Charles Chan72779502016-04-23 17:36:10 -07001162 (objective) -> log.debug("Successfully remove {} on {}/{}, vlan {}",
1163 mcastIp, deviceId, port.toLong(), assignedVlan),
1164 (objective, error) ->
1165 log.warn("Failed to remove {} on {}/{}, vlan {}: {}",
1166 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001167 fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context);
Charles Chan72779502016-04-23 17:36:10 -07001168 mcastNextObjStore.remove(mcastStoreKey);
Charles Chanc91c8782016-03-30 17:54:24 -07001169 } else {
1170 // If this is not the last sink, update flows and groups
Pier Luigi8cd46de2018-01-19 10:24:53 +01001171 context = new DefaultObjectiveContext(
Charles Chan72779502016-04-23 17:36:10 -07001172 (objective) -> log.debug("Successfully update {} on {}/{}, vlan {}",
1173 mcastIp, deviceId, port.toLong(), assignedVlan),
1174 (objective, error) ->
1175 log.warn("Failed to update {} on {}/{}, vlan {}: {}",
1176 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier Luigi8cd46de2018-01-19 10:24:53 +01001177 // Here we store the next objective with the remaining port
Pier7b657162018-03-27 11:29:42 -07001178 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi8cd46de2018-01-19 10:24:53 +01001179 existingPorts, nextObj.id()).removeFromExisting();
Pier7b657162018-03-27 11:29:42 -07001180 fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context);
Charles Chan72779502016-04-23 17:36:10 -07001181 mcastNextObjStore.put(mcastStoreKey, newNextObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001182 }
Pier Luigi8cd46de2018-01-19 10:24:53 +01001183 // Let's modify the next objective removing the bucket
Pier7b657162018-03-27 11:29:42 -07001184 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi8cd46de2018-01-19 10:24:53 +01001185 ImmutableSet.of(port), nextObj.id()).removeFromExisting();
1186 srManager.flowObjectiveService.next(deviceId, newNextObj);
1187 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001188 return existingPorts.isEmpty();
1189 }
1190
Charles Chan72779502016-04-23 17:36:10 -07001191 /**
1192 * Removes entire group on given device.
1193 *
1194 * @param deviceId device ID
1195 * @param mcastIp multicast group to be removed
1196 * @param assignedVlan assigned VLAN ID
1197 */
1198 private void removeGroupFromDevice(DeviceId deviceId, IpAddress mcastIp,
Pier7b657162018-03-27 11:29:42 -07001199 VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001200 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId);
1201 // This device is not serving this multicast group
1202 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1203 log.warn("{} is not serving {}. Abort.", deviceId, mcastIp);
1204 return;
1205 }
1206 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
1207 // NOTE: Rely on GroupStore garbage collection rather than explicitly
1208 // remove L3MG since there might be other flows/groups refer to
1209 // the same L2IG
1210 ObjectiveContext context = new DefaultObjectiveContext(
1211 (objective) -> log.debug("Successfully remove {} on {}, vlan {}",
1212 mcastIp, deviceId, assignedVlan),
1213 (objective, error) ->
1214 log.warn("Failed to remove {} on {}, vlan {}: {}",
1215 mcastIp, deviceId, assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001216 ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context);
Charles Chan72779502016-04-23 17:36:10 -07001217 srManager.flowObjectiveService.forward(deviceId, fwdObj);
1218 mcastNextObjStore.remove(mcastStoreKey);
1219 mcastRoleStore.remove(mcastStoreKey);
1220 }
1221
Pier Luigi580fd8a2018-01-16 10:47:50 +01001222 private void installPath(IpAddress mcastIp, ConnectPoint source, Path mcastPath) {
1223 // Get Links
1224 List<Link> links = mcastPath.links();
Pier1a7e0c02018-03-12 15:00:54 -07001225
1226 // Setup new ingress mcast role
1227 mcastRoleStore.put(new McastStoreKey(mcastIp, links.get(0).src().deviceId()),
1228 INGRESS);
1229
Pier Luigi580fd8a2018-01-16 10:47:50 +01001230 // For each link, modify the next on the source device adding the src port
1231 // and a new filter objective on the destination port
1232 links.forEach(link -> {
1233 addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp,
Pier7b657162018-03-27 11:29:42 -07001234 mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null));
1235 mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(),
1236 mcastUtils.assignedVlan(null), mcastIp, null);
Pier Luigi580fd8a2018-01-16 10:47:50 +01001237 });
Pier1a7e0c02018-03-12 15:00:54 -07001238
1239 // Setup mcast role for the transit
1240 links.stream()
1241 .filter(link -> !link.src().deviceId().equals(source.deviceId()))
1242 .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.src().deviceId()),
1243 TRANSIT));
Charles Chan72779502016-04-23 17:36:10 -07001244 }
1245
Charles Chanc91c8782016-03-30 17:54:24 -07001246 /**
Pier1f87aca2018-03-14 16:47:32 -07001247 * Go through all the paths, looking for shared links to be used
1248 * in the final path computation.
1249 *
1250 * @param egresses egress devices
1251 * @param availablePaths all the available paths towards the egress
1252 * @return shared links between egress devices
1253 */
1254 private Set<Link> exploreMcastTree(Set<DeviceId> egresses,
1255 Map<DeviceId, List<Path>> availablePaths) {
1256 // Length of the shortest path
1257 int minLength = Integer.MAX_VALUE;
1258 int length;
1259 // Current paths
1260 List<Path> currentPaths;
1261 // Verify the source can still reach all the egresses
1262 for (DeviceId egress : egresses) {
1263 // From the source we cannot reach all the sinks
Pier7b657162018-03-27 11:29:42 -07001264 // just continue and let's figure out after
Pier1f87aca2018-03-14 16:47:32 -07001265 currentPaths = availablePaths.get(egress);
1266 if (currentPaths.isEmpty()) {
1267 continue;
1268 }
1269 // Get the length of the first one available,
Pier7b657162018-03-27 11:29:42 -07001270 // update the min length
Pier1f87aca2018-03-14 16:47:32 -07001271 length = currentPaths.get(0).links().size();
1272 if (length < minLength) {
1273 minLength = length;
1274 }
Pier Luigi51ee7c02018-02-23 19:57:40 +01001275 }
Pier1f87aca2018-03-14 16:47:32 -07001276 // If there are no paths
1277 if (minLength == Integer.MAX_VALUE) {
1278 return Collections.emptySet();
1279 }
1280 // Iterate looking for shared links
1281 int index = 0;
1282 // Define the sets for the intersection
1283 Set<Link> sharedLinks = Sets.newHashSet();
1284 Set<Link> currentSharedLinks;
1285 Set<Link> currentLinks;
Pier7b657162018-03-27 11:29:42 -07001286 DeviceId egressToRemove = null;
Pier1f87aca2018-03-14 16:47:32 -07001287 // Let's find out the shared links
1288 while (index < minLength) {
1289 // Initialize the intersection with the paths related to the first egress
1290 currentPaths = availablePaths.get(
1291 egresses.stream()
1292 .findFirst()
1293 .orElse(null)
1294 );
1295 currentSharedLinks = Sets.newHashSet();
1296 // Iterate over the paths and take the "index" links
1297 for (Path path : currentPaths) {
1298 currentSharedLinks.add(path.links().get(index));
1299 }
1300 // Iterate over the remaining egress
1301 for (DeviceId egress : egresses) {
1302 // Iterate over the paths and take the "index" links
1303 currentLinks = Sets.newHashSet();
1304 for (Path path : availablePaths.get(egress)) {
1305 currentLinks.add(path.links().get(index));
1306 }
1307 // Do intersection
1308 currentSharedLinks = Sets.intersection(currentSharedLinks, currentLinks);
1309 // If there are no shared paths exit and record the device to remove
1310 // we have to retry with a subset of sinks
1311 if (currentSharedLinks.isEmpty()) {
Pier7b657162018-03-27 11:29:42 -07001312 egressToRemove = egress;
Pier1f87aca2018-03-14 16:47:32 -07001313 index = minLength;
1314 break;
1315 }
1316 }
1317 sharedLinks.addAll(currentSharedLinks);
1318 index++;
1319 }
1320 // If the shared links is empty and there are egress
1321 // let's retry another time with less sinks, we can
1322 // still build optimal subtrees
Pier7b657162018-03-27 11:29:42 -07001323 if (sharedLinks.isEmpty() && egresses.size() > 1 && egressToRemove != null) {
1324 egresses.remove(egressToRemove);
Pier1f87aca2018-03-14 16:47:32 -07001325 sharedLinks = exploreMcastTree(egresses, availablePaths);
1326 }
1327 return sharedLinks;
1328 }
1329
1330 /**
1331 * Build Mcast tree having as root the given source and as leaves the given egress points.
1332 *
1333 * @param source source of the tree
1334 * @param sinks leaves of the tree
1335 * @return the computed Mcast tree
1336 */
1337 private Map<ConnectPoint, List<Path>> computeSinkMcastTree(DeviceId source,
Pier7b657162018-03-27 11:29:42 -07001338 Set<ConnectPoint> sinks) {
Pier1f87aca2018-03-14 16:47:32 -07001339 // Get the egress devices, remove source from the egress if present
1340 Set<DeviceId> egresses = sinks.stream()
1341 .map(ConnectPoint::deviceId)
1342 .filter(deviceId -> !deviceId.equals(source))
1343 .collect(Collectors.toSet());
1344 Map<DeviceId, List<Path>> mcastTree = computeMcastTree(source, egresses);
Pier7b657162018-03-27 11:29:42 -07001345 // Build final tree and return it as it is
Pier1f87aca2018-03-14 16:47:32 -07001346 final Map<ConnectPoint, List<Path>> finalTree = Maps.newHashMap();
Pier7b657162018-03-27 11:29:42 -07001347 // We need to put back the source if it was originally present
1348 sinks.forEach(sink -> {
1349 List<Path> sinkPaths = mcastTree.get(sink.deviceId());
1350 finalTree.put(sink, sinkPaths != null ? sinkPaths : ImmutableList.of());
1351 });
Pier1f87aca2018-03-14 16:47:32 -07001352 return finalTree;
1353 }
1354
1355 /**
1356 * Build Mcast tree having as root the given source and as leaves the given egress.
1357 *
1358 * @param source source of the tree
1359 * @param egresses leaves of the tree
1360 * @return the computed Mcast tree
1361 */
1362 private Map<DeviceId, List<Path>> computeMcastTree(DeviceId source,
1363 Set<DeviceId> egresses) {
1364 // Pre-compute all the paths
1365 Map<DeviceId, List<Path>> availablePaths = Maps.newHashMap();
1366 // No links to enforce
1367 egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress,
1368 Collections.emptySet())));
1369 // Explore the topology looking for shared links amongst the egresses
1370 Set<Link> linksToEnforce = exploreMcastTree(Sets.newHashSet(egresses), availablePaths);
1371 // Remove all the paths from the previous computation
1372 availablePaths.clear();
1373 // Build the final paths enforcing the shared links between egress devices
1374 egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress,
1375 linksToEnforce)));
1376 return availablePaths;
1377 }
1378
1379 /**
1380 * Gets path from src to dst computed using the custom link weigher.
1381 *
1382 * @param src source device ID
1383 * @param dst destination device ID
1384 * @return list of paths from src to dst
1385 */
1386 private List<Path> getPaths(DeviceId src, DeviceId dst, Set<Link> linksToEnforce) {
1387 // Takes a snapshot of the topology
1388 final Topology currentTopology = topologyService.currentTopology();
1389 // Build a specific link weigher for this path computation
1390 final LinkWeigher linkWeigher = new SRLinkWeigher(srManager, src, linksToEnforce);
1391 // We will use our custom link weigher for our path
1392 // computations and build the list of valid paths
1393 List<Path> allPaths = Lists.newArrayList(
1394 topologyService.getPaths(currentTopology, src, dst, linkWeigher)
1395 );
1396 // If there are no valid paths, just exit
1397 log.debug("{} path(s) found from {} to {}", allPaths.size(), src, dst);
1398 return allPaths;
Pier Luigi51ee7c02018-02-23 19:57:40 +01001399 }
1400
Charles Chanc91c8782016-03-30 17:54:24 -07001401 /**
1402 * Gets a path from src to dst.
1403 * If a path was allocated before, returns the allocated path.
1404 * Otherwise, randomly pick one from available paths.
1405 *
1406 * @param src source device ID
1407 * @param dst destination device ID
1408 * @param mcastIp multicast group
Pier1f87aca2018-03-14 16:47:32 -07001409 * @param allPaths paths list
Charles Chanc91c8782016-03-30 17:54:24 -07001410 * @return an optional path from src to dst
1411 */
Pier1f87aca2018-03-14 16:47:32 -07001412 private Optional<Path> getPath(DeviceId src, DeviceId dst,
1413 IpAddress mcastIp, List<Path> allPaths) {
1414 // Firstly we get all the valid paths, if the supplied are null
1415 if (allPaths == null) {
1416 allPaths = getPaths(src, dst, Collections.emptySet());
1417 }
1418
1419 // If there are no paths just exit
Charles Chanc91c8782016-03-30 17:54:24 -07001420 if (allPaths.isEmpty()) {
Charles Chanc91c8782016-03-30 17:54:24 -07001421 return Optional.empty();
1422 }
1423
Pier Luigi91573e12018-01-23 16:06:38 +01001424 // Create a map index of suitablity-to-list of paths. For example
1425 // a path in the list associated to the index 1 shares only the
1426 // first hop and it is less suitable of a path belonging to the index
1427 // 2 that shares leaf-spine.
1428 Map<Integer, List<Path>> eligiblePaths = Maps.newHashMap();
1429 // Some init steps
1430 int nhop;
1431 McastStoreKey mcastStoreKey;
1432 Link hop;
1433 PortNumber srcPort;
1434 Set<PortNumber> existingPorts;
1435 NextObjective nextObj;
1436 // Iterate over paths looking for eligible paths
1437 for (Path path : allPaths) {
1438 // Unlikely, it will happen...
1439 if (!src.equals(path.links().get(0).src().deviceId())) {
1440 continue;
1441 }
1442 nhop = 0;
1443 // Iterate over the links
1444 while (nhop < path.links().size()) {
1445 // Get the link and verify if a next related
1446 // to the src device exist in the store
1447 hop = path.links().get(nhop);
1448 mcastStoreKey = new McastStoreKey(mcastIp, hop.src().deviceId());
1449 // It does not exist in the store, exit
1450 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1451 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001452 }
Pier Luigi91573e12018-01-23 16:06:38 +01001453 // Get the output ports on the next
1454 nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Pier7b657162018-03-27 11:29:42 -07001455 existingPorts = mcastUtils.getPorts(nextObj.next());
Pier Luigi91573e12018-01-23 16:06:38 +01001456 // And the src port on the link
1457 srcPort = hop.src().port();
1458 // the src port is not used as output, exit
1459 if (!existingPorts.contains(srcPort)) {
1460 break;
1461 }
1462 nhop++;
1463 }
1464 // n_hop defines the index
1465 if (nhop > 0) {
1466 eligiblePaths.compute(nhop, (index, paths) -> {
1467 paths = paths == null ? Lists.newArrayList() : paths;
1468 paths.add(path);
1469 return paths;
1470 });
Charles Chanc91c8782016-03-30 17:54:24 -07001471 }
1472 }
Pier Luigi91573e12018-01-23 16:06:38 +01001473
1474 // No suitable paths
1475 if (eligiblePaths.isEmpty()) {
1476 log.debug("No eligiblePath(s) found from {} to {}", src, dst);
1477 // Otherwise, randomly pick a path
1478 Collections.shuffle(allPaths);
1479 return allPaths.stream().findFirst();
1480 }
1481
1482 // Let's take the best ones
1483 Integer bestIndex = eligiblePaths.keySet()
1484 .stream()
1485 .sorted(Comparator.reverseOrder())
1486 .findFirst().orElse(null);
1487 List<Path> bestPaths = eligiblePaths.get(bestIndex);
1488 log.debug("{} eligiblePath(s) found from {} to {}",
1489 bestPaths.size(), src, dst);
1490 // randomly pick a path on the highest index
1491 Collections.shuffle(bestPaths);
1492 return bestPaths.stream().findFirst();
Charles Chanc91c8782016-03-30 17:54:24 -07001493 }
1494
1495 /**
Charles Chan72779502016-04-23 17:36:10 -07001496 * Gets device(s) of given role in given multicast group.
1497 *
1498 * @param mcastIp multicast IP
1499 * @param role multicast role
1500 * @return set of device ID or empty set if not found
1501 */
1502 private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role) {
1503 return mcastRoleStore.entrySet().stream()
1504 .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) &&
1505 entry.getValue().value() == role)
Pier1f87aca2018-03-14 16:47:32 -07001506 .map(Entry::getKey).map(McastStoreKey::deviceId)
Charles Chan72779502016-04-23 17:36:10 -07001507 .collect(Collectors.toSet());
1508 }
1509
1510 /**
1511 * Gets groups which is affected by the link down event.
1512 *
1513 * @param link link going down
1514 * @return a set of multicast IpAddress
1515 */
1516 private Set<IpAddress> getAffectedGroups(Link link) {
1517 DeviceId deviceId = link.src().deviceId();
1518 PortNumber port = link.src().port();
1519 return mcastNextObjStore.entrySet().stream()
1520 .filter(entry -> entry.getKey().deviceId().equals(deviceId) &&
Pier7b657162018-03-27 11:29:42 -07001521 mcastUtils.getPorts(entry.getValue().value().next()).contains(port))
Pier1f87aca2018-03-14 16:47:32 -07001522 .map(Entry::getKey).map(McastStoreKey::mcastIp)
Charles Chan72779502016-04-23 17:36:10 -07001523 .collect(Collectors.toSet());
1524 }
1525
1526 /**
Pier Luigi580fd8a2018-01-16 10:47:50 +01001527 * Gets groups which are affected by the device down event.
1528 *
1529 * @param deviceId device going down
1530 * @return a set of multicast IpAddress
1531 */
1532 private Set<IpAddress> getAffectedGroups(DeviceId deviceId) {
1533 return mcastNextObjStore.entrySet().stream()
1534 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier1f87aca2018-03-14 16:47:32 -07001535 .map(Entry::getKey).map(McastStoreKey::mcastIp)
Pier Luigi580fd8a2018-01-16 10:47:50 +01001536 .collect(Collectors.toSet());
1537 }
1538
1539 /**
Charles Chan72779502016-04-23 17:36:10 -07001540 * Gets the spine-facing port on ingress device of given multicast group.
1541 *
1542 * @param mcastIp multicast IP
1543 * @return spine-facing port on ingress device
1544 */
Pier1a7e0c02018-03-12 15:00:54 -07001545 private Set<PortNumber> ingressTransitPort(IpAddress mcastIp) {
Pier979e61a2018-03-07 11:42:50 +01001546 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Charles Chan72779502016-04-23 17:36:10 -07001547 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -07001548 ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder();
Charles Chan72779502016-04-23 17:36:10 -07001549 if (ingressDevice != null) {
1550 NextObjective nextObj = mcastNextObjStore
1551 .get(new McastStoreKey(mcastIp, ingressDevice)).value();
Pier7b657162018-03-27 11:29:42 -07001552 Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next());
Pier1a7e0c02018-03-12 15:00:54 -07001553 // Let's find out all the ingress-transit ports
Charles Chan72779502016-04-23 17:36:10 -07001554 for (PortNumber port : ports) {
1555 // Spine-facing port should have no subnet and no xconnect
Pier Luigi69f774d2018-02-28 12:10:50 +01001556 if (srManager.deviceConfiguration() != null &&
1557 srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() &&
Charles Chan82f19972016-05-17 13:13:55 -07001558 !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) {
Pier1a7e0c02018-03-12 15:00:54 -07001559 portBuilder.add(port);
Charles Chan72779502016-04-23 17:36:10 -07001560 }
1561 }
1562 }
Pier1a7e0c02018-03-12 15:00:54 -07001563 return portBuilder.build();
Charles Chan72779502016-04-23 17:36:10 -07001564 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001565
1566 /**
Pier Luigi580fd8a2018-01-16 10:47:50 +01001567 * Verify if the given device has sinks
1568 * for the multicast group.
1569 *
1570 * @param deviceId device Id
1571 * @param mcastIp multicast IP
1572 * @return true if the device has sink for the group.
1573 * False otherwise.
1574 */
1575 private boolean hasSinks(DeviceId deviceId, IpAddress mcastIp) {
1576 if (deviceId != null) {
1577 // Get the nextobjective
1578 Versioned<NextObjective> versionedNextObj = mcastNextObjStore.get(
1579 new McastStoreKey(mcastIp, deviceId)
1580 );
1581 // If it exists
1582 if (versionedNextObj != null) {
1583 NextObjective nextObj = versionedNextObj.value();
1584 // Retrieves all the output ports
Pier7b657162018-03-27 11:29:42 -07001585 Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next());
Pier Luigi580fd8a2018-01-16 10:47:50 +01001586 // Tries to find at least one port that is not spine-facing
1587 for (PortNumber port : ports) {
1588 // Spine-facing port should have no subnet and no xconnect
Pier Luigi69f774d2018-02-28 12:10:50 +01001589 if (srManager.deviceConfiguration() != null &&
1590 (!srManager.deviceConfiguration().getPortSubnets(deviceId, port).isEmpty() ||
Pier Luigi580fd8a2018-01-16 10:47:50 +01001591 srManager.xConnectHandler.hasXConnect(new ConnectPoint(deviceId, port)))) {
1592 return true;
1593 }
1594 }
1595 }
1596 }
1597 return false;
1598 }
1599
1600 /**
Pier28164682018-04-17 15:50:43 +02001601 * Verify if a given connect point is sink for this group.
1602 *
1603 * @param mcastIp group address
1604 * @param connectPoint connect point to be verified
1605 * @return true if the connect point is sink of the group
1606 */
1607 private boolean isSink(IpAddress mcastIp, ConnectPoint connectPoint) {
1608 // Let's check if we are already serving that location
1609 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, connectPoint.deviceId());
1610 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1611 return false;
1612 }
1613 // Get next and check with the port
1614 NextObjective mcastNext = mcastNextObjStore.get(mcastStoreKey).value();
1615 return mcastUtils.getPorts(mcastNext.next()).contains(connectPoint.port());
1616 }
1617
1618 /**
Pier Luigi35dab3f2018-01-25 16:16:02 +01001619 * Updates filtering objective for given device and port.
1620 * It is called in general when the mcast config has been
1621 * changed.
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001622 *
1623 * @param deviceId device ID
1624 * @param portNum ingress port number
1625 * @param vlanId assigned VLAN ID
1626 * @param install true to add, false to remove
1627 */
Pier Luigi69f774d2018-02-28 12:10:50 +01001628 public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum,
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001629 VlanId vlanId, boolean install) {
Pier Luigi35dab3f2018-01-25 16:16:02 +01001630 lastMcastChange = Instant.now();
1631 mcastLock();
1632 try {
1633 // Iterates over the route and updates properly the filtering objective
1634 // on the source device.
1635 srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> {
Pierdb27b8d2018-04-17 16:29:56 +02001636 log.debug("Update filter for {}", mcastRoute.group());
1637 // Verify leadership on the operation
1638 if (!mcastUtils.isLeader(mcastRoute.group())) {
1639 log.debug("Skip {} due to lack of leadership", mcastRoute.group());
1640 return;
1641 }
Pier1f87aca2018-03-14 16:47:32 -07001642 // FIXME To be addressed with multiple sources support
1643 ConnectPoint source = srManager.multicastRouteService.sources(mcastRoute)
1644 .stream()
1645 .findFirst().orElse(null);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001646 if (source.deviceId().equals(deviceId) && source.port().equals(portNum)) {
1647 if (install) {
Pier7b657162018-03-27 11:29:42 -07001648 mcastUtils.addFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), INGRESS);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001649 } else {
Pier7b657162018-03-27 11:29:42 -07001650 mcastUtils.removeFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), null);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001651 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001652 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001653 });
1654 } finally {
1655 mcastUnlock();
1656 }
1657 }
1658
1659 /**
1660 * Performs bucket verification operation for all mcast groups in the devices.
1661 * Firstly, it verifies that mcast is stable before trying verification operation.
1662 * Verification consists in creating new nexts with VERIFY operation. Actually,
1663 * the operation is totally delegated to the driver.
1664 */
1665 private final class McastBucketCorrector implements Runnable {
1666
1667 @Override
1668 public void run() {
1669 // Verify if the Mcast has been stable for MCAST_STABLITY_THRESHOLD
1670 if (!isMcastStable()) {
1671 return;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001672 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001673 // Acquires lock
1674 mcastLock();
1675 try {
1676 // Iterates over the routes and verify the related next objectives
1677 srManager.multicastRouteService.getRoutes()
1678 .stream()
1679 .map(McastRoute::group)
1680 .forEach(mcastIp -> {
1681 log.trace("Running mcast buckets corrector for mcast group: {}",
1682 mcastIp);
1683
1684 // For each group we get current information in the store
1685 // and issue a check of the next objectives in place
Pier979e61a2018-03-07 11:42:50 +01001686 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Pier Luigi35dab3f2018-01-25 16:16:02 +01001687 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -07001688 Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT);
Pier979e61a2018-03-07 11:42:50 +01001689 Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS);
Pier Luigi92e69be2018-03-02 12:53:37 +01001690 // Get source and sinks from Mcast Route Service and warn about errors
Pier7b657162018-03-27 11:29:42 -07001691 ConnectPoint source = mcastUtils.getSource(mcastIp);
1692 Set<ConnectPoint> sinks = mcastUtils.getSinks(mcastIp).values().stream()
1693 .flatMap(Collection::stream)
1694 .collect(Collectors.toSet());
Pier Luigi35dab3f2018-01-25 16:16:02 +01001695
1696 // Do not proceed if ingress device or source of this group are missing
1697 if (ingressDevice == null || source == null) {
Pier Luigi92e69be2018-03-02 12:53:37 +01001698 if (!sinks.isEmpty()) {
1699 log.warn("Unable to run buckets corrector. " +
1700 "Missing ingress {} or source {} for group {}",
1701 ingressDevice, source, mcastIp);
1702 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001703 return;
1704 }
1705
Pierdb27b8d2018-04-17 16:29:56 +02001706 // Continue only when this instance is the leader of the group
1707 if (!mcastUtils.isLeader(mcastIp)) {
Pier Luigi35dab3f2018-01-25 16:16:02 +01001708 log.trace("Unable to run buckets corrector. " +
Pierdb27b8d2018-04-17 16:29:56 +02001709 "Skip {} due to lack of leadership", mcastIp);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001710 return;
1711 }
1712
1713 // Create the set of the devices to be processed
1714 ImmutableSet.Builder<DeviceId> devicesBuilder = ImmutableSet.builder();
1715 devicesBuilder.add(ingressDevice);
Pier1a7e0c02018-03-12 15:00:54 -07001716 if (!transitDevices.isEmpty()) {
1717 devicesBuilder.addAll(transitDevices);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001718 }
1719 if (!egressDevices.isEmpty()) {
1720 devicesBuilder.addAll(egressDevices);
1721 }
1722 Set<DeviceId> devicesToProcess = devicesBuilder.build();
1723
1724 // Iterate over the devices
1725 devicesToProcess.forEach(deviceId -> {
1726 McastStoreKey currentKey = new McastStoreKey(mcastIp, deviceId);
1727 // If next exists in our store verify related next objective
1728 if (mcastNextObjStore.containsKey(currentKey)) {
1729 NextObjective currentNext = mcastNextObjStore.get(currentKey).value();
1730 // Get current ports
Pier7b657162018-03-27 11:29:42 -07001731 Set<PortNumber> currentPorts = mcastUtils.getPorts(currentNext.next());
Pier Luigi35dab3f2018-01-25 16:16:02 +01001732 // Rebuild the next objective
Pier7b657162018-03-27 11:29:42 -07001733 currentNext = mcastUtils.nextObjBuilder(
Pier Luigi35dab3f2018-01-25 16:16:02 +01001734 mcastIp,
Pier7b657162018-03-27 11:29:42 -07001735 mcastUtils.assignedVlan(deviceId.equals(source.deviceId()) ?
1736 source : null),
Pier Luigi35dab3f2018-01-25 16:16:02 +01001737 currentPorts,
1738 currentNext.id()
1739 ).verify();
1740 // Send to the flowobjective service
1741 srManager.flowObjectiveService.next(deviceId, currentNext);
1742 } else {
Pier Luigid8a15162018-02-15 16:33:08 +01001743 log.warn("Unable to run buckets corrector. " +
Pier Luigi35dab3f2018-01-25 16:16:02 +01001744 "Missing next for {} and group {}",
1745 deviceId, mcastIp);
1746 }
1747 });
1748
1749 });
1750 } finally {
1751 // Finally, it releases the lock
1752 mcastUnlock();
1753 }
1754
1755 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001756 }
Pier Luigi0f9635b2018-01-15 18:06:43 +01001757
1758 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
1759 // If mcast ip is present
1760 if (mcastIp != null) {
1761 return mcastNextObjStore.entrySet().stream()
1762 .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp()))
Pier1f87aca2018-03-14 16:47:32 -07001763 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001764 entry -> entry.getValue().value().id()));
1765 }
1766 // Otherwise take all the groups
1767 return mcastNextObjStore.entrySet().stream()
Pier1f87aca2018-03-14 16:47:32 -07001768 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001769 entry -> entry.getValue().value().id()));
1770 }
1771
Pier71c55772018-04-17 17:25:22 +02001772 /**
1773 * Returns the associated roles to the mcast groups or to the single
1774 * group if mcastIp is present.
1775 *
1776 * @param mcastIp the group ip
1777 * @return the mapping mcastIp-device to mcast role
1778 *
1779 * @deprecated in 1.12 ("Magpie") release.
1780 */
1781 @Deprecated
Pier Luigi69f774d2018-02-28 12:10:50 +01001782 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001783 // If mcast ip is present
1784 if (mcastIp != null) {
1785 return mcastRoleStore.entrySet().stream()
1786 .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp()))
Pier1f87aca2018-03-14 16:47:32 -07001787 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001788 entry -> entry.getValue().value()));
1789 }
1790 // Otherwise take all the groups
1791 return mcastRoleStore.entrySet().stream()
Pier1f87aca2018-03-14 16:47:32 -07001792 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001793 entry -> entry.getValue().value()));
1794 }
1795
Pier71c55772018-04-17 17:25:22 +02001796 /**
1797 * Returns the associated paths to the mcast group.
1798 *
1799 * @param mcastIp the group ip
1800 * @return the mapping egress point to mcast path
1801 *
1802 * @deprecated in 1.12 ("Magpie") release.
1803 */
1804 @Deprecated
Pier Luigi0f9635b2018-01-15 18:06:43 +01001805 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1806 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap();
1807 // Get the source
Pier7b657162018-03-27 11:29:42 -07001808 ConnectPoint source = mcastUtils.getSource(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001809 // Source cannot be null, we don't know the starting point
1810 if (source != null) {
1811 // Init steps
1812 Set<DeviceId> visited = Sets.newHashSet();
1813 List<ConnectPoint> currentPath = Lists.newArrayList(
1814 source
1815 );
1816 // Build recursively the mcast paths
1817 buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp);
1818 }
1819 return mcastPaths;
1820 }
1821
Pier71c55772018-04-17 17:25:22 +02001822 /**
1823 * Returns the associated trees to the mcast group.
1824 *
1825 * @param mcastIp the group ip
1826 * @param sourcecp the source connect point
1827 * @return the mapping egress point to mcast path
1828 */
1829 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1830 ConnectPoint sourcecp) {
1831 Multimap<ConnectPoint, List<ConnectPoint>> mcastTrees = HashMultimap.create();
1832 // Get the sources
1833 Set<ConnectPoint> sources = mcastUtils.getSources(mcastIp);
1834
1835 // If we are providing the source, let's filter out
1836 if (sourcecp != null) {
1837 sources = sources.stream()
1838 .filter(source -> source.equals(sourcecp))
1839 .collect(Collectors.toSet());
1840 }
1841
1842 // Source cannot be null, we don't know the starting point
1843 if (!sources.isEmpty()) {
1844 sources.forEach(source -> {
1845 // Init steps
1846 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap();
1847 Set<DeviceId> visited = Sets.newHashSet();
1848 List<ConnectPoint> currentPath = Lists.newArrayList(source);
1849 // Build recursively the mcast paths
1850 buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp);
1851 mcastPaths.forEach(mcastTrees::put);
1852 });
1853 }
1854 return mcastTrees;
1855 }
1856
1857 /**
1858 * Build recursively the mcast paths.
1859 *
1860 * @param toVisit the node to visit
1861 * @param visited the visited nodes
1862 * @param mcastPaths the current mcast paths
1863 * @param currentPath the current path
1864 * @param mcastIp the group ip
1865 */
Pier Luigi0f9635b2018-01-15 18:06:43 +01001866 private void buildMcastPaths(DeviceId toVisit, Set<DeviceId> visited,
1867 Map<ConnectPoint, List<ConnectPoint>> mcastPaths,
1868 List<ConnectPoint> currentPath, IpAddress mcastIp) {
1869 // If we have visited the node to visit
1870 // there is a loop
1871 if (visited.contains(toVisit)) {
1872 return;
1873 }
1874 // Visit next-hop
1875 visited.add(toVisit);
1876 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit);
1877 // Looking for next-hops
1878 if (mcastNextObjStore.containsKey(mcastStoreKey)) {
1879 // Build egress connectpoints
1880 NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value();
1881 // Get Ports
Pier7b657162018-03-27 11:29:42 -07001882 Set<PortNumber> outputPorts = mcastUtils.getPorts(nextObjective.next());
Pier Luigi0f9635b2018-01-15 18:06:43 +01001883 // Build relative cps
1884 ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder();
1885 outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber)));
1886 Set<ConnectPoint> egressPoints = cpBuilder.build();
1887 // Define other variables for the next steps
1888 Set<Link> egressLinks;
1889 List<ConnectPoint> newCurrentPath;
1890 Set<DeviceId> newVisited;
1891 DeviceId newToVisit;
1892 for (ConnectPoint egressPoint : egressPoints) {
1893 egressLinks = srManager.linkService.getEgressLinks(egressPoint);
1894 // If it does not have egress links, stop
1895 if (egressLinks.isEmpty()) {
1896 // Add the connect points to the path
1897 newCurrentPath = Lists.newArrayList(currentPath);
1898 newCurrentPath.add(0, egressPoint);
1899 // Save in the map
1900 mcastPaths.put(egressPoint, newCurrentPath);
1901 } else {
1902 newVisited = Sets.newHashSet(visited);
1903 // Iterate over the egress links for the next hops
1904 for (Link egressLink : egressLinks) {
1905 // Update to visit
1906 newToVisit = egressLink.dst().deviceId();
1907 // Add the connect points to the path
1908 newCurrentPath = Lists.newArrayList(currentPath);
1909 newCurrentPath.add(0, egressPoint);
1910 newCurrentPath.add(0, egressLink.dst());
1911 // Go to the next hop
1912 buildMcastPaths(newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp);
1913 }
1914 }
1915 }
1916 }
1917 }
1918
Pierdb27b8d2018-04-17 16:29:56 +02001919 /**
1920 * Return the leaders of the mcast groups.
1921 *
1922 * @param mcastIp the group ip
1923 * @return the mapping group-node
1924 */
1925 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1926 return mcastUtils.getMcastLeaders(mcastIp);
1927 }
Charles Chanc91c8782016-03-30 17:54:24 -07001928}