blob: 50d0969de5dc8b9b6e3521fbdd548246f4f155a1 [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) {
Pier665b0fc2018-04-19 15:53:20 +0200975 ConnectPoint sinkToBeProcessed = connectPoints.stream()
976 .filter(connectPoint -> !isSink(mcastIp, connectPoint))
977 .findFirst().orElse(null);
978 if (sinkToBeProcessed != null) {
979 sinksToBeProcessed.add(sinkToBeProcessed);
980 }
Pier28164682018-04-17 15:50:43 +0200981 }
Pier7b657162018-03-27 11:29:42 -0700982 });
983 return sinksToBeProcessed;
984 }
985
986 /**
987 * Process all the sinks related to a mcast group and return
988 * the ones to be processed.
989 *
990 * @param source the source connect point
991 * @param mcastIp the group address
992 * @param sinks the sinks to be evaluated
993 * @return the set of the sinks to be processed
994 */
995 private Set<ConnectPoint> processSinksToBeAdded(ConnectPoint source, IpAddress mcastIp,
996 Map<HostId, Set<ConnectPoint>> sinks) {
997 // Iterate over the sinks in order to build the set
998 // of the connect points to be served by this group
999 final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet();
1000 sinks.forEach(((hostId, connectPoints) -> {
1001 // If it has more than 2 locations
1002 if (connectPoints.size() > 2 || connectPoints.size() == 0) {
1003 log.debug("Skip {} since sink {} has {} locations",
1004 mcastIp, hostId, connectPoints.size());
1005 return;
1006 }
1007 // If it has one location, just use it
1008 if (connectPoints.size() == 1) {
1009 sinksToBeProcessed.add(connectPoints.stream()
Pier665b0fc2018-04-19 15:53:20 +02001010 .findFirst().orElse(null));
Pier7b657162018-03-27 11:29:42 -07001011 return;
1012 }
1013 // We prefer to reuse existing flows
1014 ConnectPoint sinkToBeProcessed = connectPoints.stream()
Pier28164682018-04-17 15:50:43 +02001015 .filter(connectPoint -> isSink(mcastIp, connectPoint))
Pier7b657162018-03-27 11:29:42 -07001016 .findFirst().orElse(null);
1017 if (sinkToBeProcessed != null) {
1018 sinksToBeProcessed.add(sinkToBeProcessed);
1019 return;
1020 }
1021 // Otherwise we prefer to reuse existing egresses
1022 Set<DeviceId> egresses = getDevice(mcastIp, EGRESS);
1023 sinkToBeProcessed = connectPoints.stream()
Pier28164682018-04-17 15:50:43 +02001024 .filter(connectPoint -> egresses.contains(connectPoint.deviceId()))
Pier7b657162018-03-27 11:29:42 -07001025 .findFirst().orElse(null);
1026 if (sinkToBeProcessed != null) {
1027 sinksToBeProcessed.add(sinkToBeProcessed);
1028 return;
1029 }
1030 // Otherwise we prefer a location co-located with the source (if it exists)
1031 sinkToBeProcessed = connectPoints.stream()
1032 .filter(connectPoint -> connectPoint.deviceId().equals(source.deviceId()))
1033 .findFirst().orElse(null);
1034 if (sinkToBeProcessed != null) {
1035 sinksToBeProcessed.add(sinkToBeProcessed);
1036 return;
1037 }
1038 // Finally, we randomly pick a new location
1039 sinksToBeProcessed.add(connectPoints.stream()
Pier665b0fc2018-04-19 15:53:20 +02001040 .findFirst().orElse(null));
Pier7b657162018-03-27 11:29:42 -07001041 }));
1042 // We have done, return the set
1043 return sinksToBeProcessed;
1044 }
1045
1046 /**
Pier1a7e0c02018-03-12 15:00:54 -07001047 * Utility method to remove all the ingress transit ports.
1048 *
1049 * @param mcastIp the group ip
1050 * @param ingressDevice the ingress device for this group
1051 * @param source the source connect point
1052 */
1053 private void removeIngressTransitPorts(IpAddress mcastIp, DeviceId ingressDevice,
1054 ConnectPoint source) {
1055 Set<PortNumber> ingressTransitPorts = ingressTransitPort(mcastIp);
1056 ingressTransitPorts.forEach(ingressTransitPort -> {
1057 if (ingressTransitPort != null) {
1058 boolean isLast = removePortFromDevice(ingressDevice, ingressTransitPort,
Pier7b657162018-03-27 11:29:42 -07001059 mcastIp, mcastUtils.assignedVlan(source));
Pier1a7e0c02018-03-12 15:00:54 -07001060 if (isLast) {
1061 mcastRoleStore.remove(new McastStoreKey(mcastIp, ingressDevice));
1062 }
1063 }
1064 });
1065 }
1066
1067 /**
Charles Chanc91c8782016-03-30 17:54:24 -07001068 * Adds a port to given multicast group on given device. This involves the
1069 * update of L3 multicast group and multicast routing table entry.
1070 *
1071 * @param deviceId device ID
1072 * @param port port to be added
1073 * @param mcastIp multicast group
1074 * @param assignedVlan assigned VLAN ID
1075 */
1076 private void addPortToDevice(DeviceId deviceId, PortNumber port,
Pier7b657162018-03-27 11:29:42 -07001077 IpAddress mcastIp, VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001078 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId);
Charles Chanc91c8782016-03-30 17:54:24 -07001079 ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder();
Pier Luigi4f0dd212018-01-19 10:24:53 +01001080 NextObjective newNextObj;
Charles Chan72779502016-04-23 17:36:10 -07001081 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
Charles Chanc91c8782016-03-30 17:54:24 -07001082 // First time someone request this mcast group via this device
1083 portBuilder.add(port);
Pier Luigi4f0dd212018-01-19 10:24:53 +01001084 // New nextObj
Pier7b657162018-03-27 11:29:42 -07001085 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001086 portBuilder.build(), null).add();
1087 // Store the new port
1088 mcastNextObjStore.put(mcastStoreKey, newNextObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001089 } else {
1090 // This device already serves some subscribers of this mcast group
Charles Chan72779502016-04-23 17:36:10 -07001091 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Charles Chanc91c8782016-03-30 17:54:24 -07001092 // Stop if the port is already in the nextobj
Pier7b657162018-03-27 11:29:42 -07001093 Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next());
Charles Chanc91c8782016-03-30 17:54:24 -07001094 if (existingPorts.contains(port)) {
1095 log.info("NextObj for {}/{} already exists. Abort", deviceId, port);
1096 return;
1097 }
Pier Luigi4f0dd212018-01-19 10:24:53 +01001098 // Let's add the port and reuse the previous one
Yuta HIGUCHIbef07b52018-02-09 18:05:23 -08001099 portBuilder.addAll(existingPorts).add(port);
Pier Luigi4f0dd212018-01-19 10:24:53 +01001100 // Reuse previous nextObj
Pier7b657162018-03-27 11:29:42 -07001101 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001102 portBuilder.build(), nextObj.id()).addToExisting();
1103 // Store the final next objective and send only the difference to the driver
1104 mcastNextObjStore.put(mcastStoreKey, newNextObj);
1105 // Add just the new port
1106 portBuilder = ImmutableSet.builder();
1107 portBuilder.add(port);
Pier7b657162018-03-27 11:29:42 -07001108 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi4f0dd212018-01-19 10:24:53 +01001109 portBuilder.build(), nextObj.id()).addToExisting();
Charles Chanc91c8782016-03-30 17:54:24 -07001110 }
1111 // Create, store and apply the new nextObj and fwdObj
Charles Chan72779502016-04-23 17:36:10 -07001112 ObjectiveContext context = new DefaultObjectiveContext(
1113 (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}",
1114 mcastIp, deviceId, port.toLong(), assignedVlan),
1115 (objective, error) ->
1116 log.warn("Failed to add {} on {}/{}, vlan {}: {}",
1117 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001118 ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan,
1119 newNextObj.id()).add(context);
Charles Chanc91c8782016-03-30 17:54:24 -07001120 srManager.flowObjectiveService.next(deviceId, newNextObj);
1121 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001122 }
1123
1124 /**
1125 * Removes a port from given multicast group on given device.
1126 * This involves the update of L3 multicast group and multicast routing
1127 * table entry.
1128 *
1129 * @param deviceId device ID
1130 * @param port port to be added
1131 * @param mcastIp multicast group
1132 * @param assignedVlan assigned VLAN ID
1133 * @return true if this is the last sink on this device
1134 */
1135 private boolean removePortFromDevice(DeviceId deviceId, PortNumber port,
Pier7b657162018-03-27 11:29:42 -07001136 IpAddress mcastIp, VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001137 McastStoreKey mcastStoreKey =
1138 new McastStoreKey(mcastIp, deviceId);
Charles Chanc91c8782016-03-30 17:54:24 -07001139 // This device is not serving this multicast group
Charles Chan72779502016-04-23 17:36:10 -07001140 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
Charles Chanc91c8782016-03-30 17:54:24 -07001141 log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port);
1142 return false;
1143 }
Charles Chan72779502016-04-23 17:36:10 -07001144 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Charles Chanc91c8782016-03-30 17:54:24 -07001145
Pier7b657162018-03-27 11:29:42 -07001146 Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next());
Charles Chan72779502016-04-23 17:36:10 -07001147 // This port does not serve this multicast group
Charles Chanc91c8782016-03-30 17:54:24 -07001148 if (!existingPorts.contains(port)) {
1149 log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port);
1150 return false;
1151 }
1152 // Copy and modify the ImmutableSet
1153 existingPorts = Sets.newHashSet(existingPorts);
1154 existingPorts.remove(port);
1155
1156 NextObjective newNextObj;
Pier Luigi8cd46de2018-01-19 10:24:53 +01001157 ObjectiveContext context;
Charles Chanc91c8782016-03-30 17:54:24 -07001158 ForwardingObjective fwdObj;
1159 if (existingPorts.isEmpty()) {
Pier Luigi8cd46de2018-01-19 10:24:53 +01001160 // If this is the last sink, remove flows and last bucket
Charles Chanc91c8782016-03-30 17:54:24 -07001161 // NOTE: Rely on GroupStore garbage collection rather than explicitly
1162 // remove L3MG since there might be other flows/groups refer to
1163 // the same L2IG
Pier Luigi8cd46de2018-01-19 10:24:53 +01001164 context = new DefaultObjectiveContext(
Charles Chan72779502016-04-23 17:36:10 -07001165 (objective) -> log.debug("Successfully remove {} on {}/{}, vlan {}",
1166 mcastIp, deviceId, port.toLong(), assignedVlan),
1167 (objective, error) ->
1168 log.warn("Failed to remove {} on {}/{}, vlan {}: {}",
1169 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001170 fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context);
Charles Chan72779502016-04-23 17:36:10 -07001171 mcastNextObjStore.remove(mcastStoreKey);
Charles Chanc91c8782016-03-30 17:54:24 -07001172 } else {
1173 // If this is not the last sink, update flows and groups
Pier Luigi8cd46de2018-01-19 10:24:53 +01001174 context = new DefaultObjectiveContext(
Charles Chan72779502016-04-23 17:36:10 -07001175 (objective) -> log.debug("Successfully update {} on {}/{}, vlan {}",
1176 mcastIp, deviceId, port.toLong(), assignedVlan),
1177 (objective, error) ->
1178 log.warn("Failed to update {} on {}/{}, vlan {}: {}",
1179 mcastIp, deviceId, port.toLong(), assignedVlan, error));
Pier Luigi8cd46de2018-01-19 10:24:53 +01001180 // Here we store the next objective with the remaining port
Pier7b657162018-03-27 11:29:42 -07001181 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi8cd46de2018-01-19 10:24:53 +01001182 existingPorts, nextObj.id()).removeFromExisting();
Pier7b657162018-03-27 11:29:42 -07001183 fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context);
Charles Chan72779502016-04-23 17:36:10 -07001184 mcastNextObjStore.put(mcastStoreKey, newNextObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001185 }
Pier Luigi8cd46de2018-01-19 10:24:53 +01001186 // Let's modify the next objective removing the bucket
Pier7b657162018-03-27 11:29:42 -07001187 newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan,
Pier Luigi8cd46de2018-01-19 10:24:53 +01001188 ImmutableSet.of(port), nextObj.id()).removeFromExisting();
1189 srManager.flowObjectiveService.next(deviceId, newNextObj);
1190 srManager.flowObjectiveService.forward(deviceId, fwdObj);
Charles Chanc91c8782016-03-30 17:54:24 -07001191 return existingPorts.isEmpty();
1192 }
1193
Charles Chan72779502016-04-23 17:36:10 -07001194 /**
1195 * Removes entire group on given device.
1196 *
1197 * @param deviceId device ID
1198 * @param mcastIp multicast group to be removed
1199 * @param assignedVlan assigned VLAN ID
1200 */
1201 private void removeGroupFromDevice(DeviceId deviceId, IpAddress mcastIp,
Pier7b657162018-03-27 11:29:42 -07001202 VlanId assignedVlan) {
Charles Chan72779502016-04-23 17:36:10 -07001203 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId);
1204 // This device is not serving this multicast group
1205 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1206 log.warn("{} is not serving {}. Abort.", deviceId, mcastIp);
1207 return;
1208 }
1209 NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value();
1210 // NOTE: Rely on GroupStore garbage collection rather than explicitly
1211 // remove L3MG since there might be other flows/groups refer to
1212 // the same L2IG
1213 ObjectiveContext context = new DefaultObjectiveContext(
1214 (objective) -> log.debug("Successfully remove {} on {}, vlan {}",
1215 mcastIp, deviceId, assignedVlan),
1216 (objective, error) ->
1217 log.warn("Failed to remove {} on {}, vlan {}: {}",
1218 mcastIp, deviceId, assignedVlan, error));
Pier7b657162018-03-27 11:29:42 -07001219 ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context);
Charles Chan72779502016-04-23 17:36:10 -07001220 srManager.flowObjectiveService.forward(deviceId, fwdObj);
1221 mcastNextObjStore.remove(mcastStoreKey);
1222 mcastRoleStore.remove(mcastStoreKey);
1223 }
1224
Pier Luigi580fd8a2018-01-16 10:47:50 +01001225 private void installPath(IpAddress mcastIp, ConnectPoint source, Path mcastPath) {
1226 // Get Links
1227 List<Link> links = mcastPath.links();
Pier1a7e0c02018-03-12 15:00:54 -07001228
1229 // Setup new ingress mcast role
1230 mcastRoleStore.put(new McastStoreKey(mcastIp, links.get(0).src().deviceId()),
1231 INGRESS);
1232
Pier Luigi580fd8a2018-01-16 10:47:50 +01001233 // For each link, modify the next on the source device adding the src port
1234 // and a new filter objective on the destination port
1235 links.forEach(link -> {
1236 addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp,
Pier7b657162018-03-27 11:29:42 -07001237 mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null));
1238 mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(),
1239 mcastUtils.assignedVlan(null), mcastIp, null);
Pier Luigi580fd8a2018-01-16 10:47:50 +01001240 });
Pier1a7e0c02018-03-12 15:00:54 -07001241
1242 // Setup mcast role for the transit
1243 links.stream()
1244 .filter(link -> !link.src().deviceId().equals(source.deviceId()))
1245 .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.src().deviceId()),
1246 TRANSIT));
Charles Chan72779502016-04-23 17:36:10 -07001247 }
1248
Charles Chanc91c8782016-03-30 17:54:24 -07001249 /**
Pier1f87aca2018-03-14 16:47:32 -07001250 * Go through all the paths, looking for shared links to be used
1251 * in the final path computation.
1252 *
1253 * @param egresses egress devices
1254 * @param availablePaths all the available paths towards the egress
1255 * @return shared links between egress devices
1256 */
1257 private Set<Link> exploreMcastTree(Set<DeviceId> egresses,
1258 Map<DeviceId, List<Path>> availablePaths) {
1259 // Length of the shortest path
1260 int minLength = Integer.MAX_VALUE;
1261 int length;
1262 // Current paths
1263 List<Path> currentPaths;
1264 // Verify the source can still reach all the egresses
1265 for (DeviceId egress : egresses) {
1266 // From the source we cannot reach all the sinks
Pier7b657162018-03-27 11:29:42 -07001267 // just continue and let's figure out after
Pier1f87aca2018-03-14 16:47:32 -07001268 currentPaths = availablePaths.get(egress);
1269 if (currentPaths.isEmpty()) {
1270 continue;
1271 }
1272 // Get the length of the first one available,
Pier7b657162018-03-27 11:29:42 -07001273 // update the min length
Pier1f87aca2018-03-14 16:47:32 -07001274 length = currentPaths.get(0).links().size();
1275 if (length < minLength) {
1276 minLength = length;
1277 }
Pier Luigi51ee7c02018-02-23 19:57:40 +01001278 }
Pier1f87aca2018-03-14 16:47:32 -07001279 // If there are no paths
1280 if (minLength == Integer.MAX_VALUE) {
1281 return Collections.emptySet();
1282 }
1283 // Iterate looking for shared links
1284 int index = 0;
1285 // Define the sets for the intersection
1286 Set<Link> sharedLinks = Sets.newHashSet();
1287 Set<Link> currentSharedLinks;
1288 Set<Link> currentLinks;
Pier7b657162018-03-27 11:29:42 -07001289 DeviceId egressToRemove = null;
Pier1f87aca2018-03-14 16:47:32 -07001290 // Let's find out the shared links
1291 while (index < minLength) {
1292 // Initialize the intersection with the paths related to the first egress
1293 currentPaths = availablePaths.get(
1294 egresses.stream()
1295 .findFirst()
1296 .orElse(null)
1297 );
1298 currentSharedLinks = Sets.newHashSet();
1299 // Iterate over the paths and take the "index" links
1300 for (Path path : currentPaths) {
1301 currentSharedLinks.add(path.links().get(index));
1302 }
1303 // Iterate over the remaining egress
1304 for (DeviceId egress : egresses) {
1305 // Iterate over the paths and take the "index" links
1306 currentLinks = Sets.newHashSet();
1307 for (Path path : availablePaths.get(egress)) {
1308 currentLinks.add(path.links().get(index));
1309 }
1310 // Do intersection
1311 currentSharedLinks = Sets.intersection(currentSharedLinks, currentLinks);
1312 // If there are no shared paths exit and record the device to remove
1313 // we have to retry with a subset of sinks
1314 if (currentSharedLinks.isEmpty()) {
Pier7b657162018-03-27 11:29:42 -07001315 egressToRemove = egress;
Pier1f87aca2018-03-14 16:47:32 -07001316 index = minLength;
1317 break;
1318 }
1319 }
1320 sharedLinks.addAll(currentSharedLinks);
1321 index++;
1322 }
1323 // If the shared links is empty and there are egress
1324 // let's retry another time with less sinks, we can
1325 // still build optimal subtrees
Pier7b657162018-03-27 11:29:42 -07001326 if (sharedLinks.isEmpty() && egresses.size() > 1 && egressToRemove != null) {
1327 egresses.remove(egressToRemove);
Pier1f87aca2018-03-14 16:47:32 -07001328 sharedLinks = exploreMcastTree(egresses, availablePaths);
1329 }
1330 return sharedLinks;
1331 }
1332
1333 /**
1334 * Build Mcast tree having as root the given source and as leaves the given egress points.
1335 *
1336 * @param source source of the tree
1337 * @param sinks leaves of the tree
1338 * @return the computed Mcast tree
1339 */
1340 private Map<ConnectPoint, List<Path>> computeSinkMcastTree(DeviceId source,
Pier7b657162018-03-27 11:29:42 -07001341 Set<ConnectPoint> sinks) {
Pier1f87aca2018-03-14 16:47:32 -07001342 // Get the egress devices, remove source from the egress if present
1343 Set<DeviceId> egresses = sinks.stream()
1344 .map(ConnectPoint::deviceId)
1345 .filter(deviceId -> !deviceId.equals(source))
1346 .collect(Collectors.toSet());
1347 Map<DeviceId, List<Path>> mcastTree = computeMcastTree(source, egresses);
Pier7b657162018-03-27 11:29:42 -07001348 // Build final tree and return it as it is
Pier1f87aca2018-03-14 16:47:32 -07001349 final Map<ConnectPoint, List<Path>> finalTree = Maps.newHashMap();
Pier7b657162018-03-27 11:29:42 -07001350 // We need to put back the source if it was originally present
1351 sinks.forEach(sink -> {
1352 List<Path> sinkPaths = mcastTree.get(sink.deviceId());
1353 finalTree.put(sink, sinkPaths != null ? sinkPaths : ImmutableList.of());
1354 });
Pier1f87aca2018-03-14 16:47:32 -07001355 return finalTree;
1356 }
1357
1358 /**
1359 * Build Mcast tree having as root the given source and as leaves the given egress.
1360 *
1361 * @param source source of the tree
1362 * @param egresses leaves of the tree
1363 * @return the computed Mcast tree
1364 */
1365 private Map<DeviceId, List<Path>> computeMcastTree(DeviceId source,
1366 Set<DeviceId> egresses) {
1367 // Pre-compute all the paths
1368 Map<DeviceId, List<Path>> availablePaths = Maps.newHashMap();
1369 // No links to enforce
1370 egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress,
1371 Collections.emptySet())));
1372 // Explore the topology looking for shared links amongst the egresses
1373 Set<Link> linksToEnforce = exploreMcastTree(Sets.newHashSet(egresses), availablePaths);
1374 // Remove all the paths from the previous computation
1375 availablePaths.clear();
1376 // Build the final paths enforcing the shared links between egress devices
1377 egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress,
1378 linksToEnforce)));
1379 return availablePaths;
1380 }
1381
1382 /**
1383 * Gets path from src to dst computed using the custom link weigher.
1384 *
1385 * @param src source device ID
1386 * @param dst destination device ID
1387 * @return list of paths from src to dst
1388 */
1389 private List<Path> getPaths(DeviceId src, DeviceId dst, Set<Link> linksToEnforce) {
1390 // Takes a snapshot of the topology
1391 final Topology currentTopology = topologyService.currentTopology();
1392 // Build a specific link weigher for this path computation
1393 final LinkWeigher linkWeigher = new SRLinkWeigher(srManager, src, linksToEnforce);
1394 // We will use our custom link weigher for our path
1395 // computations and build the list of valid paths
1396 List<Path> allPaths = Lists.newArrayList(
1397 topologyService.getPaths(currentTopology, src, dst, linkWeigher)
1398 );
1399 // If there are no valid paths, just exit
1400 log.debug("{} path(s) found from {} to {}", allPaths.size(), src, dst);
1401 return allPaths;
Pier Luigi51ee7c02018-02-23 19:57:40 +01001402 }
1403
Charles Chanc91c8782016-03-30 17:54:24 -07001404 /**
1405 * Gets a path from src to dst.
1406 * If a path was allocated before, returns the allocated path.
1407 * Otherwise, randomly pick one from available paths.
1408 *
1409 * @param src source device ID
1410 * @param dst destination device ID
1411 * @param mcastIp multicast group
Pier1f87aca2018-03-14 16:47:32 -07001412 * @param allPaths paths list
Charles Chanc91c8782016-03-30 17:54:24 -07001413 * @return an optional path from src to dst
1414 */
Pier1f87aca2018-03-14 16:47:32 -07001415 private Optional<Path> getPath(DeviceId src, DeviceId dst,
1416 IpAddress mcastIp, List<Path> allPaths) {
1417 // Firstly we get all the valid paths, if the supplied are null
1418 if (allPaths == null) {
1419 allPaths = getPaths(src, dst, Collections.emptySet());
1420 }
1421
1422 // If there are no paths just exit
Charles Chanc91c8782016-03-30 17:54:24 -07001423 if (allPaths.isEmpty()) {
Charles Chanc91c8782016-03-30 17:54:24 -07001424 return Optional.empty();
1425 }
1426
Pier Luigi91573e12018-01-23 16:06:38 +01001427 // Create a map index of suitablity-to-list of paths. For example
1428 // a path in the list associated to the index 1 shares only the
1429 // first hop and it is less suitable of a path belonging to the index
1430 // 2 that shares leaf-spine.
1431 Map<Integer, List<Path>> eligiblePaths = Maps.newHashMap();
1432 // Some init steps
1433 int nhop;
1434 McastStoreKey mcastStoreKey;
1435 Link hop;
1436 PortNumber srcPort;
1437 Set<PortNumber> existingPorts;
1438 NextObjective nextObj;
1439 // Iterate over paths looking for eligible paths
1440 for (Path path : allPaths) {
1441 // Unlikely, it will happen...
1442 if (!src.equals(path.links().get(0).src().deviceId())) {
1443 continue;
1444 }
1445 nhop = 0;
1446 // Iterate over the links
1447 while (nhop < path.links().size()) {
1448 // Get the link and verify if a next related
1449 // to the src device exist in the store
1450 hop = path.links().get(nhop);
1451 mcastStoreKey = new McastStoreKey(mcastIp, hop.src().deviceId());
1452 // It does not exist in the store, exit
1453 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1454 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001455 }
Pier Luigi91573e12018-01-23 16:06:38 +01001456 // Get the output ports on the next
1457 nextObj = mcastNextObjStore.get(mcastStoreKey).value();
Pier7b657162018-03-27 11:29:42 -07001458 existingPorts = mcastUtils.getPorts(nextObj.next());
Pier Luigi91573e12018-01-23 16:06:38 +01001459 // And the src port on the link
1460 srcPort = hop.src().port();
1461 // the src port is not used as output, exit
1462 if (!existingPorts.contains(srcPort)) {
1463 break;
1464 }
1465 nhop++;
1466 }
1467 // n_hop defines the index
1468 if (nhop > 0) {
1469 eligiblePaths.compute(nhop, (index, paths) -> {
1470 paths = paths == null ? Lists.newArrayList() : paths;
1471 paths.add(path);
1472 return paths;
1473 });
Charles Chanc91c8782016-03-30 17:54:24 -07001474 }
1475 }
Pier Luigi91573e12018-01-23 16:06:38 +01001476
1477 // No suitable paths
1478 if (eligiblePaths.isEmpty()) {
1479 log.debug("No eligiblePath(s) found from {} to {}", src, dst);
1480 // Otherwise, randomly pick a path
1481 Collections.shuffle(allPaths);
1482 return allPaths.stream().findFirst();
1483 }
1484
1485 // Let's take the best ones
1486 Integer bestIndex = eligiblePaths.keySet()
1487 .stream()
1488 .sorted(Comparator.reverseOrder())
1489 .findFirst().orElse(null);
1490 List<Path> bestPaths = eligiblePaths.get(bestIndex);
1491 log.debug("{} eligiblePath(s) found from {} to {}",
1492 bestPaths.size(), src, dst);
1493 // randomly pick a path on the highest index
1494 Collections.shuffle(bestPaths);
1495 return bestPaths.stream().findFirst();
Charles Chanc91c8782016-03-30 17:54:24 -07001496 }
1497
1498 /**
Charles Chan72779502016-04-23 17:36:10 -07001499 * Gets device(s) of given role in given multicast group.
1500 *
1501 * @param mcastIp multicast IP
1502 * @param role multicast role
1503 * @return set of device ID or empty set if not found
1504 */
1505 private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role) {
1506 return mcastRoleStore.entrySet().stream()
1507 .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) &&
1508 entry.getValue().value() == role)
Pier1f87aca2018-03-14 16:47:32 -07001509 .map(Entry::getKey).map(McastStoreKey::deviceId)
Charles Chan72779502016-04-23 17:36:10 -07001510 .collect(Collectors.toSet());
1511 }
1512
1513 /**
1514 * Gets groups which is affected by the link down event.
1515 *
1516 * @param link link going down
1517 * @return a set of multicast IpAddress
1518 */
1519 private Set<IpAddress> getAffectedGroups(Link link) {
1520 DeviceId deviceId = link.src().deviceId();
1521 PortNumber port = link.src().port();
1522 return mcastNextObjStore.entrySet().stream()
1523 .filter(entry -> entry.getKey().deviceId().equals(deviceId) &&
Pier7b657162018-03-27 11:29:42 -07001524 mcastUtils.getPorts(entry.getValue().value().next()).contains(port))
Pier1f87aca2018-03-14 16:47:32 -07001525 .map(Entry::getKey).map(McastStoreKey::mcastIp)
Charles Chan72779502016-04-23 17:36:10 -07001526 .collect(Collectors.toSet());
1527 }
1528
1529 /**
Pier Luigi580fd8a2018-01-16 10:47:50 +01001530 * Gets groups which are affected by the device down event.
1531 *
1532 * @param deviceId device going down
1533 * @return a set of multicast IpAddress
1534 */
1535 private Set<IpAddress> getAffectedGroups(DeviceId deviceId) {
1536 return mcastNextObjStore.entrySet().stream()
1537 .filter(entry -> entry.getKey().deviceId().equals(deviceId))
Pier1f87aca2018-03-14 16:47:32 -07001538 .map(Entry::getKey).map(McastStoreKey::mcastIp)
Pier Luigi580fd8a2018-01-16 10:47:50 +01001539 .collect(Collectors.toSet());
1540 }
1541
1542 /**
Charles Chan72779502016-04-23 17:36:10 -07001543 * Gets the spine-facing port on ingress device of given multicast group.
1544 *
1545 * @param mcastIp multicast IP
1546 * @return spine-facing port on ingress device
1547 */
Pier1a7e0c02018-03-12 15:00:54 -07001548 private Set<PortNumber> ingressTransitPort(IpAddress mcastIp) {
Pier979e61a2018-03-07 11:42:50 +01001549 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Charles Chan72779502016-04-23 17:36:10 -07001550 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -07001551 ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder();
Charles Chan72779502016-04-23 17:36:10 -07001552 if (ingressDevice != null) {
1553 NextObjective nextObj = mcastNextObjStore
1554 .get(new McastStoreKey(mcastIp, ingressDevice)).value();
Pier7b657162018-03-27 11:29:42 -07001555 Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next());
Pier1a7e0c02018-03-12 15:00:54 -07001556 // Let's find out all the ingress-transit ports
Charles Chan72779502016-04-23 17:36:10 -07001557 for (PortNumber port : ports) {
1558 // Spine-facing port should have no subnet and no xconnect
Pier Luigi69f774d2018-02-28 12:10:50 +01001559 if (srManager.deviceConfiguration() != null &&
1560 srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() &&
Charles Chan82f19972016-05-17 13:13:55 -07001561 !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) {
Pier1a7e0c02018-03-12 15:00:54 -07001562 portBuilder.add(port);
Charles Chan72779502016-04-23 17:36:10 -07001563 }
1564 }
1565 }
Pier1a7e0c02018-03-12 15:00:54 -07001566 return portBuilder.build();
Charles Chan72779502016-04-23 17:36:10 -07001567 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001568
1569 /**
Pier Luigi580fd8a2018-01-16 10:47:50 +01001570 * Verify if the given device has sinks
1571 * for the multicast group.
1572 *
1573 * @param deviceId device Id
1574 * @param mcastIp multicast IP
1575 * @return true if the device has sink for the group.
1576 * False otherwise.
1577 */
1578 private boolean hasSinks(DeviceId deviceId, IpAddress mcastIp) {
1579 if (deviceId != null) {
1580 // Get the nextobjective
1581 Versioned<NextObjective> versionedNextObj = mcastNextObjStore.get(
1582 new McastStoreKey(mcastIp, deviceId)
1583 );
1584 // If it exists
1585 if (versionedNextObj != null) {
1586 NextObjective nextObj = versionedNextObj.value();
1587 // Retrieves all the output ports
Pier7b657162018-03-27 11:29:42 -07001588 Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next());
Pier Luigi580fd8a2018-01-16 10:47:50 +01001589 // Tries to find at least one port that is not spine-facing
1590 for (PortNumber port : ports) {
1591 // Spine-facing port should have no subnet and no xconnect
Pier Luigi69f774d2018-02-28 12:10:50 +01001592 if (srManager.deviceConfiguration() != null &&
1593 (!srManager.deviceConfiguration().getPortSubnets(deviceId, port).isEmpty() ||
Pier Luigi580fd8a2018-01-16 10:47:50 +01001594 srManager.xConnectHandler.hasXConnect(new ConnectPoint(deviceId, port)))) {
1595 return true;
1596 }
1597 }
1598 }
1599 }
1600 return false;
1601 }
1602
1603 /**
Pier28164682018-04-17 15:50:43 +02001604 * Verify if a given connect point is sink for this group.
1605 *
1606 * @param mcastIp group address
1607 * @param connectPoint connect point to be verified
1608 * @return true if the connect point is sink of the group
1609 */
1610 private boolean isSink(IpAddress mcastIp, ConnectPoint connectPoint) {
1611 // Let's check if we are already serving that location
1612 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, connectPoint.deviceId());
1613 if (!mcastNextObjStore.containsKey(mcastStoreKey)) {
1614 return false;
1615 }
1616 // Get next and check with the port
1617 NextObjective mcastNext = mcastNextObjStore.get(mcastStoreKey).value();
1618 return mcastUtils.getPorts(mcastNext.next()).contains(connectPoint.port());
1619 }
1620
1621 /**
Pier Luigi35dab3f2018-01-25 16:16:02 +01001622 * Updates filtering objective for given device and port.
1623 * It is called in general when the mcast config has been
1624 * changed.
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001625 *
1626 * @param deviceId device ID
1627 * @param portNum ingress port number
1628 * @param vlanId assigned VLAN ID
1629 * @param install true to add, false to remove
1630 */
Pier Luigi69f774d2018-02-28 12:10:50 +01001631 public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum,
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001632 VlanId vlanId, boolean install) {
Pier Luigi35dab3f2018-01-25 16:16:02 +01001633 lastMcastChange = Instant.now();
1634 mcastLock();
1635 try {
1636 // Iterates over the route and updates properly the filtering objective
1637 // on the source device.
1638 srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> {
Pierdb27b8d2018-04-17 16:29:56 +02001639 log.debug("Update filter for {}", mcastRoute.group());
1640 // Verify leadership on the operation
1641 if (!mcastUtils.isLeader(mcastRoute.group())) {
1642 log.debug("Skip {} due to lack of leadership", mcastRoute.group());
1643 return;
1644 }
Pier1f87aca2018-03-14 16:47:32 -07001645 // FIXME To be addressed with multiple sources support
1646 ConnectPoint source = srManager.multicastRouteService.sources(mcastRoute)
1647 .stream()
1648 .findFirst().orElse(null);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001649 if (source.deviceId().equals(deviceId) && source.port().equals(portNum)) {
1650 if (install) {
Pier7b657162018-03-27 11:29:42 -07001651 mcastUtils.addFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), INGRESS);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001652 } else {
Pier7b657162018-03-27 11:29:42 -07001653 mcastUtils.removeFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), null);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001654 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001655 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001656 });
1657 } finally {
1658 mcastUnlock();
1659 }
1660 }
1661
1662 /**
1663 * Performs bucket verification operation for all mcast groups in the devices.
1664 * Firstly, it verifies that mcast is stable before trying verification operation.
1665 * Verification consists in creating new nexts with VERIFY operation. Actually,
1666 * the operation is totally delegated to the driver.
1667 */
1668 private final class McastBucketCorrector implements Runnable {
1669
1670 @Override
1671 public void run() {
1672 // Verify if the Mcast has been stable for MCAST_STABLITY_THRESHOLD
1673 if (!isMcastStable()) {
1674 return;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001675 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001676 // Acquires lock
1677 mcastLock();
1678 try {
1679 // Iterates over the routes and verify the related next objectives
1680 srManager.multicastRouteService.getRoutes()
1681 .stream()
1682 .map(McastRoute::group)
1683 .forEach(mcastIp -> {
1684 log.trace("Running mcast buckets corrector for mcast group: {}",
1685 mcastIp);
1686
1687 // For each group we get current information in the store
1688 // and issue a check of the next objectives in place
Pier979e61a2018-03-07 11:42:50 +01001689 DeviceId ingressDevice = getDevice(mcastIp, INGRESS)
Pier Luigi35dab3f2018-01-25 16:16:02 +01001690 .stream().findAny().orElse(null);
Pier1a7e0c02018-03-12 15:00:54 -07001691 Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT);
Pier979e61a2018-03-07 11:42:50 +01001692 Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS);
Pier Luigi92e69be2018-03-02 12:53:37 +01001693 // Get source and sinks from Mcast Route Service and warn about errors
Pier7b657162018-03-27 11:29:42 -07001694 ConnectPoint source = mcastUtils.getSource(mcastIp);
1695 Set<ConnectPoint> sinks = mcastUtils.getSinks(mcastIp).values().stream()
1696 .flatMap(Collection::stream)
1697 .collect(Collectors.toSet());
Pier Luigi35dab3f2018-01-25 16:16:02 +01001698
1699 // Do not proceed if ingress device or source of this group are missing
1700 if (ingressDevice == null || source == null) {
Pier Luigi92e69be2018-03-02 12:53:37 +01001701 if (!sinks.isEmpty()) {
1702 log.warn("Unable to run buckets corrector. " +
1703 "Missing ingress {} or source {} for group {}",
1704 ingressDevice, source, mcastIp);
1705 }
Pier Luigi35dab3f2018-01-25 16:16:02 +01001706 return;
1707 }
1708
Pierdb27b8d2018-04-17 16:29:56 +02001709 // Continue only when this instance is the leader of the group
1710 if (!mcastUtils.isLeader(mcastIp)) {
Pier Luigi35dab3f2018-01-25 16:16:02 +01001711 log.trace("Unable to run buckets corrector. " +
Pierdb27b8d2018-04-17 16:29:56 +02001712 "Skip {} due to lack of leadership", mcastIp);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001713 return;
1714 }
1715
1716 // Create the set of the devices to be processed
1717 ImmutableSet.Builder<DeviceId> devicesBuilder = ImmutableSet.builder();
1718 devicesBuilder.add(ingressDevice);
Pier1a7e0c02018-03-12 15:00:54 -07001719 if (!transitDevices.isEmpty()) {
1720 devicesBuilder.addAll(transitDevices);
Pier Luigi35dab3f2018-01-25 16:16:02 +01001721 }
1722 if (!egressDevices.isEmpty()) {
1723 devicesBuilder.addAll(egressDevices);
1724 }
1725 Set<DeviceId> devicesToProcess = devicesBuilder.build();
1726
1727 // Iterate over the devices
1728 devicesToProcess.forEach(deviceId -> {
1729 McastStoreKey currentKey = new McastStoreKey(mcastIp, deviceId);
1730 // If next exists in our store verify related next objective
1731 if (mcastNextObjStore.containsKey(currentKey)) {
1732 NextObjective currentNext = mcastNextObjStore.get(currentKey).value();
1733 // Get current ports
Pier7b657162018-03-27 11:29:42 -07001734 Set<PortNumber> currentPorts = mcastUtils.getPorts(currentNext.next());
Pier Luigi35dab3f2018-01-25 16:16:02 +01001735 // Rebuild the next objective
Pier7b657162018-03-27 11:29:42 -07001736 currentNext = mcastUtils.nextObjBuilder(
Pier Luigi35dab3f2018-01-25 16:16:02 +01001737 mcastIp,
Pier7b657162018-03-27 11:29:42 -07001738 mcastUtils.assignedVlan(deviceId.equals(source.deviceId()) ?
1739 source : null),
Pier Luigi35dab3f2018-01-25 16:16:02 +01001740 currentPorts,
1741 currentNext.id()
1742 ).verify();
1743 // Send to the flowobjective service
1744 srManager.flowObjectiveService.next(deviceId, currentNext);
1745 } else {
Pier Luigid8a15162018-02-15 16:33:08 +01001746 log.warn("Unable to run buckets corrector. " +
Pier Luigi35dab3f2018-01-25 16:16:02 +01001747 "Missing next for {} and group {}",
1748 deviceId, mcastIp);
1749 }
1750 });
1751
1752 });
1753 } finally {
1754 // Finally, it releases the lock
1755 mcastUnlock();
1756 }
1757
1758 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001759 }
Pier Luigi0f9635b2018-01-15 18:06:43 +01001760
1761 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
1762 // If mcast ip is present
1763 if (mcastIp != null) {
1764 return mcastNextObjStore.entrySet().stream()
1765 .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp()))
Pier1f87aca2018-03-14 16:47:32 -07001766 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001767 entry -> entry.getValue().value().id()));
1768 }
1769 // Otherwise take all the groups
1770 return mcastNextObjStore.entrySet().stream()
Pier1f87aca2018-03-14 16:47:32 -07001771 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001772 entry -> entry.getValue().value().id()));
1773 }
1774
Pier71c55772018-04-17 17:25:22 +02001775 /**
1776 * Returns the associated roles to the mcast groups or to the single
1777 * group if mcastIp is present.
1778 *
1779 * @param mcastIp the group ip
1780 * @return the mapping mcastIp-device to mcast role
1781 *
1782 * @deprecated in 1.12 ("Magpie") release.
1783 */
1784 @Deprecated
Pier Luigi69f774d2018-02-28 12:10:50 +01001785 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001786 // If mcast ip is present
1787 if (mcastIp != null) {
1788 return mcastRoleStore.entrySet().stream()
1789 .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp()))
Pier1f87aca2018-03-14 16:47:32 -07001790 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001791 entry -> entry.getValue().value()));
1792 }
1793 // Otherwise take all the groups
1794 return mcastRoleStore.entrySet().stream()
Pier1f87aca2018-03-14 16:47:32 -07001795 .collect(Collectors.toMap(Entry::getKey,
Pier Luigi0f9635b2018-01-15 18:06:43 +01001796 entry -> entry.getValue().value()));
1797 }
1798
Pier71c55772018-04-17 17:25:22 +02001799 /**
1800 * Returns the associated paths to the mcast group.
1801 *
1802 * @param mcastIp the group ip
1803 * @return the mapping egress point to mcast path
1804 *
1805 * @deprecated in 1.12 ("Magpie") release.
1806 */
1807 @Deprecated
Pier Luigi0f9635b2018-01-15 18:06:43 +01001808 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1809 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap();
1810 // Get the source
Pier7b657162018-03-27 11:29:42 -07001811 ConnectPoint source = mcastUtils.getSource(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001812 // Source cannot be null, we don't know the starting point
1813 if (source != null) {
1814 // Init steps
1815 Set<DeviceId> visited = Sets.newHashSet();
1816 List<ConnectPoint> currentPath = Lists.newArrayList(
1817 source
1818 );
1819 // Build recursively the mcast paths
1820 buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp);
1821 }
1822 return mcastPaths;
1823 }
1824
Pier71c55772018-04-17 17:25:22 +02001825 /**
1826 * Returns the associated trees to the mcast group.
1827 *
1828 * @param mcastIp the group ip
1829 * @param sourcecp the source connect point
1830 * @return the mapping egress point to mcast path
1831 */
1832 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1833 ConnectPoint sourcecp) {
1834 Multimap<ConnectPoint, List<ConnectPoint>> mcastTrees = HashMultimap.create();
1835 // Get the sources
1836 Set<ConnectPoint> sources = mcastUtils.getSources(mcastIp);
1837
1838 // If we are providing the source, let's filter out
1839 if (sourcecp != null) {
1840 sources = sources.stream()
1841 .filter(source -> source.equals(sourcecp))
1842 .collect(Collectors.toSet());
1843 }
1844
1845 // Source cannot be null, we don't know the starting point
1846 if (!sources.isEmpty()) {
1847 sources.forEach(source -> {
1848 // Init steps
1849 Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap();
1850 Set<DeviceId> visited = Sets.newHashSet();
1851 List<ConnectPoint> currentPath = Lists.newArrayList(source);
1852 // Build recursively the mcast paths
1853 buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp);
1854 mcastPaths.forEach(mcastTrees::put);
1855 });
1856 }
1857 return mcastTrees;
1858 }
1859
1860 /**
1861 * Build recursively the mcast paths.
1862 *
1863 * @param toVisit the node to visit
1864 * @param visited the visited nodes
1865 * @param mcastPaths the current mcast paths
1866 * @param currentPath the current path
1867 * @param mcastIp the group ip
1868 */
Pier Luigi0f9635b2018-01-15 18:06:43 +01001869 private void buildMcastPaths(DeviceId toVisit, Set<DeviceId> visited,
1870 Map<ConnectPoint, List<ConnectPoint>> mcastPaths,
1871 List<ConnectPoint> currentPath, IpAddress mcastIp) {
1872 // If we have visited the node to visit
1873 // there is a loop
1874 if (visited.contains(toVisit)) {
1875 return;
1876 }
1877 // Visit next-hop
1878 visited.add(toVisit);
1879 McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit);
1880 // Looking for next-hops
1881 if (mcastNextObjStore.containsKey(mcastStoreKey)) {
1882 // Build egress connectpoints
1883 NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value();
1884 // Get Ports
Pier7b657162018-03-27 11:29:42 -07001885 Set<PortNumber> outputPorts = mcastUtils.getPorts(nextObjective.next());
Pier Luigi0f9635b2018-01-15 18:06:43 +01001886 // Build relative cps
1887 ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder();
1888 outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber)));
1889 Set<ConnectPoint> egressPoints = cpBuilder.build();
1890 // Define other variables for the next steps
1891 Set<Link> egressLinks;
1892 List<ConnectPoint> newCurrentPath;
1893 Set<DeviceId> newVisited;
1894 DeviceId newToVisit;
1895 for (ConnectPoint egressPoint : egressPoints) {
1896 egressLinks = srManager.linkService.getEgressLinks(egressPoint);
1897 // If it does not have egress links, stop
1898 if (egressLinks.isEmpty()) {
1899 // Add the connect points to the path
1900 newCurrentPath = Lists.newArrayList(currentPath);
1901 newCurrentPath.add(0, egressPoint);
1902 // Save in the map
1903 mcastPaths.put(egressPoint, newCurrentPath);
1904 } else {
1905 newVisited = Sets.newHashSet(visited);
1906 // Iterate over the egress links for the next hops
1907 for (Link egressLink : egressLinks) {
1908 // Update to visit
1909 newToVisit = egressLink.dst().deviceId();
1910 // Add the connect points to the path
1911 newCurrentPath = Lists.newArrayList(currentPath);
1912 newCurrentPath.add(0, egressPoint);
1913 newCurrentPath.add(0, egressLink.dst());
1914 // Go to the next hop
1915 buildMcastPaths(newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp);
1916 }
1917 }
1918 }
1919 }
1920 }
1921
Pierdb27b8d2018-04-17 16:29:56 +02001922 /**
1923 * Return the leaders of the mcast groups.
1924 *
1925 * @param mcastIp the group ip
1926 * @return the mapping group-node
1927 */
1928 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1929 return mcastUtils.getMcastLeaders(mcastIp);
1930 }
Charles Chanc91c8782016-03-30 17:54:24 -07001931}