Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1 | /* |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 17 | package org.onosproject.segmentrouting.mcast; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 18 | |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.cache.RemovalCause; |
| 22 | import com.google.common.cache.RemovalNotification; |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 23 | import com.google.common.collect.HashMultimap; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
| 26 | import com.google.common.collect.Lists; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 27 | import com.google.common.collect.Maps; |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 28 | import com.google.common.collect.Multimap; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 29 | import com.google.common.collect.Sets; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IpAddress; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onlab.util.KryoNamespace; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 33 | import org.onosproject.cluster.NodeId; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 36 | import org.onosproject.mcast.api.McastEvent; |
| 37 | import org.onosproject.mcast.api.McastRoute; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 38 | import org.onosproject.mcast.api.McastRouteData; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 39 | import org.onosproject.mcast.api.McastRouteUpdate; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 40 | import org.onosproject.net.HostId; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 41 | import org.onosproject.net.ConnectPoint; |
| 42 | import org.onosproject.net.DeviceId; |
| 43 | import org.onosproject.net.Link; |
| 44 | import org.onosproject.net.Path; |
| 45 | import org.onosproject.net.PortNumber; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 50 | import org.onosproject.net.topology.LinkWeigher; |
Pier Luigi | d8a1516 | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 51 | import org.onosproject.net.topology.Topology; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 52 | import org.onosproject.net.topology.TopologyService; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 53 | import org.onosproject.segmentrouting.SRLinkWeigher; |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 54 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 55 | import org.onosproject.segmentrouting.storekey.McastStoreKey; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 56 | import org.onosproject.store.serializers.KryoNamespaces; |
| 57 | import org.onosproject.store.service.ConsistentMap; |
| 58 | import org.onosproject.store.service.Serializer; |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 59 | import org.onosproject.store.service.Versioned; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
| 62 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 63 | import java.time.Instant; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 64 | import java.util.Collection; |
| 65 | import java.util.Collections; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 66 | import java.util.Comparator; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 67 | import java.util.List; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 68 | import java.util.Map; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 69 | import java.util.Map.Entry; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 70 | import java.util.Optional; |
| 71 | import java.util.Set; |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 72 | import java.util.concurrent.ScheduledExecutorService; |
| 73 | import java.util.concurrent.TimeUnit; |
| 74 | import java.util.concurrent.locks.Lock; |
| 75 | import java.util.concurrent.locks.ReentrantLock; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 76 | import java.util.stream.Collectors; |
| 77 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 78 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 79 | import static org.onlab.util.Tools.groupedThreads; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 80 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 81 | import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_ADDED; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 82 | import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_REMOVED; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 83 | import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_ADDED; |
| 84 | import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_REMOVED; |
| 85 | import static org.onosproject.mcast.api.McastEvent.Type.SINKS_ADDED; |
| 86 | import static org.onosproject.mcast.api.McastEvent.Type.SINKS_REMOVED; |
| 87 | |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 88 | import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS; |
| 89 | import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS; |
| 90 | import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 93 | * Handles Multicast related events. |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 94 | */ |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 95 | public class McastHandler { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 96 | // Logger instance |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 97 | private static final Logger log = LoggerFactory.getLogger(McastHandler.class); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 98 | // Reference to srManager and most used internal objects |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 99 | private final SegmentRoutingManager srManager; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 100 | private final TopologyService topologyService; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 101 | private final McastUtils mcastUtils; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 102 | // Internal store of the Mcast nextobjectives |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 103 | private final ConsistentMap<McastStoreKey, NextObjective> mcastNextObjStore; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 104 | // Internal store of the Mcast roles |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 105 | private final ConsistentMap<McastStoreKey, McastRole> mcastRoleStore; |
| 106 | |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 107 | // Wait time for the cache |
| 108 | private static final int WAIT_TIME_MS = 1000; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 109 | |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 110 | /** |
| 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(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 119 | HostId sink = notification.getKey().sinkHost(); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 120 | 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) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 136 | // Retrieve, currentData, prevData and the group |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 137 | final McastRouteUpdate mcastRouteUpdate = mcastEvent.subject(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 138 | final McastRouteUpdate mcastRoutePrevUpdate = mcastEvent.prevSubject(); |
| 139 | final IpAddress group = mcastRoutePrevUpdate.route().group(); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 140 | // Let's create the keys of the cache |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 141 | ImmutableSet.Builder<HostId> sinksBuilder = ImmutableSet.builder(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 142 | if (mcastEvent.type() == SOURCES_ADDED || |
| 143 | mcastEvent.type() == SOURCES_REMOVED) { |
| 144 | // FIXME To be addressed with multiple sources support |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 145 | 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); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 155 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 156 | })); |
| 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 Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 171 | } |
| 172 | // Push the elements in the cache |
| 173 | sinksBuilder.build().forEach(sink -> { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 174 | McastCacheKey cacheKey = new McastCacheKey(group, sink); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 175 | mcastEventCache.put(cacheKey, mcastEvent); |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | private void dequeueMcastEvent(McastEvent mcastEvent) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 180 | // Get new and old data |
| 181 | final McastRouteUpdate mcastUpdate = mcastEvent.subject(); |
| 182 | final McastRouteUpdate mcastPrevUpdate = mcastEvent.prevSubject(); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 183 | // Get source, mcast group |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 184 | // FIXME To be addressed with multiple sources support |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 185 | final ConnectPoint source = mcastPrevUpdate.sources() |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 186 | .stream() |
| 187 | .findFirst() |
| 188 | .orElse(null); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 189 | IpAddress mcastIp = mcastPrevUpdate.route().group(); |
| 190 | // Get all the previous sinks |
| 191 | Set<ConnectPoint> prevSinks = mcastPrevUpdate.sinks() |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 192 | .values() |
| 193 | .stream() |
| 194 | .flatMap(Collection::stream) |
| 195 | .collect(Collectors.toSet()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 196 | // According to the event type let's call the proper method |
| 197 | switch (mcastEvent.type()) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 198 | 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 Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 206 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 207 | case SOURCES_REMOVED: |
| 208 | // FIXME To be addressed with multiple sources support |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 209 | // Get old source |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 210 | //ConnectPoint oldSource = mcastEvent.prevSubject().source().orElse(null); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 211 | // Just the first cached element will be processed |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 212 | //processSourceUpdatedInternal(mcastIp, source, oldSource); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 213 | break; |
| 214 | case ROUTE_REMOVED: |
| 215 | // Process the route removed, just the first cached element will be processed |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 216 | processRouteRemovedInternal(source, mcastIp); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 217 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 218 | case SINKS_ADDED: |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 219 | // FIXME To be addressed with multiple sources support |
| 220 | processSinksAddedInternal(source, mcastIp, |
| 221 | mcastUpdate.sinks(), prevSinks); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 222 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 223 | case SINKS_REMOVED: |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 224 | // FIXME To be addressed with multiple sources support |
| 225 | processSinksRemovedInternal(source, mcastIp, |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 226 | mcastUpdate.sinks(), mcastPrevUpdate.sinks()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 227 | break; |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 233 | // 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 Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 265 | log.trace("Mcast stable since {}s", now - last); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 266 | 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 Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 274 | = newScheduledThreadPool(1, groupedThreads("mcastWorker", "mcastWorker-%d", log)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 275 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 276 | /** |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 277 | * Constructs the McastEventHandler. |
| 278 | * |
| 279 | * @param srManager Segment Routing manager |
| 280 | */ |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 281 | public McastHandler(SegmentRoutingManager srManager) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 282 | ApplicationId coreAppId = srManager.coreService.getAppId(CoreService.CORE_APP_NAME); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 283 | this.srManager = srManager; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 284 | this.topologyService = srManager.topologyService; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 285 | KryoNamespace.Builder mcastKryo = new KryoNamespace.Builder() |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 286 | .register(KryoNamespaces.API) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 287 | .register(McastStoreKey.class) |
| 288 | .register(McastRole.class); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 289 | mcastNextObjStore = srManager.storageService |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 290 | .<McastStoreKey, NextObjective>consistentMapBuilder() |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 291 | .withName("onos-mcast-nextobj-store") |
Charles Chan | 4922a17 | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 292 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-NextObj"))) |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 293 | .build(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 294 | mcastRoleStore = srManager.storageService |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 295 | .<McastStoreKey, McastRole>consistentMapBuilder() |
| 296 | .withName("onos-mcast-role-store") |
Charles Chan | 4922a17 | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 297 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-Role"))) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 298 | .build(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 299 | // Let's create McastUtils object |
| 300 | mcastUtils = new McastUtils(srManager, coreAppId, log); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 301 | // Init the executor service and the buckets corrector |
| 302 | executorService.scheduleWithFixedDelay(new McastBucketCorrector(), 10, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 303 | MCAST_VERIFY_INTERVAL, TimeUnit.SECONDS); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 304 | // 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 Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Read initial multicast from mcast store. |
| 311 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 312 | public void init() { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 313 | lastMcastChange = Instant.now(); |
| 314 | mcastLock(); |
| 315 | try { |
| 316 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 317 | // 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 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 322 | // 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 Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 351 | * Clean up when deactivating the application. |
| 352 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 353 | public void terminate() { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 354 | executorService.shutdown(); |
| 355 | } |
| 356 | |
| 357 | /** |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 358 | * Processes the SOURCE_ADDED, SOURCE_UPDATED, SINK_ADDED, |
| 359 | * SINK_REMOVED and ROUTE_REMOVED events. |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 360 | * |
| 361 | * @param event McastEvent with SOURCE_ADDED type |
| 362 | */ |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 363 | public void processMcastEvent(McastEvent event) { |
| 364 | log.info("process {}", event); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 365 | // 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 Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 375 | |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 376 | /** |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 377 | * Process the ROUTE_ADDED event. |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 378 | * |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 379 | * @param mcastIp the group address |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 380 | */ |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 381 | private void processRouteAddedInternal(IpAddress mcastIp) { |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 382 | lastMcastChange = Instant.now(); |
| 383 | mcastLock(); |
| 384 | try { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 385 | log.debug("Processing route added for group {}", mcastIp); |
| 386 | // Just elect a new leader |
| 387 | mcastUtils.isLeader(mcastIp); |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 388 | } finally { |
| 389 | mcastUnlock(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 394 | * 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 Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 402 | log.debug("Processing route removed for group {}", mcastIp); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 403 | // 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 Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 409 | |
| 410 | // Find out the ingress, transit and egress device of the affected group |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 411 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 412 | .stream().findAny().orElse(null); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 413 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 414 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 415 | |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 416 | // If there are no egress devices, sinks could be only on the ingress |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 417 | if (!egressDevices.isEmpty()) { |
| 418 | egressDevices.forEach( |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 419 | deviceId -> removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null)) |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 420 | ); |
| 421 | } |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 422 | // Transit could be empty if sinks are on the ingress |
| 423 | if (!transitDevices.isEmpty()) { |
| 424 | transitDevices.forEach( |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 425 | deviceId -> removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null)) |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 426 | ); |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 427 | } |
| 428 | // Ingress device should be not null |
| 429 | if (ingressDevice != null) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 430 | removeGroupFromDevice(ingressDevice, mcastIp, mcastUtils.assignedVlan(source)); |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 431 | } |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 432 | } finally { |
| 433 | mcastUnlock(); |
| 434 | } |
| 435 | } |
| 436 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 437 | |
| 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 |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 444 | * @param prevSinks the previous sinks |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 445 | */ |
| 446 | private void processSinksRemovedInternal(ConnectPoint source, IpAddress mcastIp, |
| 447 | Map<HostId, Set<ConnectPoint>> newSinks, |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 448 | Map<HostId, Set<ConnectPoint>> prevSinks) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 449 | lastMcastChange = Instant.now(); |
| 450 | mcastLock(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 451 | try { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 452 | // Verify leadership on the operation |
| 453 | if (!mcastUtils.isLeader(mcastIp)) { |
| 454 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 455 | return; |
| 456 | } |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 457 | // Remove the previous ones |
| 458 | Set<ConnectPoint> sinksToBeRemoved = processSinksToBeRemoved(mcastIp, prevSinks, |
| 459 | newSinks); |
| 460 | sinksToBeRemoved.forEach(sink -> processSinkRemovedInternal(source, sink, mcastIp)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 461 | // Recover the dual-homed sinks |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 462 | Set<ConnectPoint> sinksToBeRecovered = processSinksToBeRecovered(mcastIp, newSinks, |
| 463 | prevSinks); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 464 | sinksToBeRecovered.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, null)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 465 | } finally { |
| 466 | mcastUnlock(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 470 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 471 | * 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, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 478 | IpAddress mcastIp) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 479 | lastMcastChange = Instant.now(); |
| 480 | mcastLock(); |
| 481 | try { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 482 | boolean isLast; |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 483 | // 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 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 491 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source)); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 492 | if (isLast) { |
| 493 | mcastRoleStore.remove(new McastStoreKey(mcastIp, sink.deviceId())); |
| 494 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 495 | return; |
| 496 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 497 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 498 | // Process the egress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 499 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 500 | 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 |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 505 | Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(), |
| 506 | mcastIp, null); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 507 | 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, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 516 | mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? |
| 517 | source : null) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 518 | ); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 519 | if (isLast) { |
| 520 | mcastRoleStore.remove(new McastStoreKey(mcastIp, link.src().deviceId())); |
| 521 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 522 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 525 | } finally { |
| 526 | mcastUnlock(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 530 | |
| 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 { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 545 | // Verify leadership on the operation |
| 546 | if (!mcastUtils.isLeader(mcastIp)) { |
| 547 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 548 | return; |
| 549 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 550 | // 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 Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 560 | /** |
| 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, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 568 | IpAddress mcastIp, List<Path> allPaths) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 569 | lastMcastChange = Instant.now(); |
| 570 | mcastLock(); |
| 571 | try { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 572 | // Process the ingress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 573 | mcastUtils.addFilterToDevice(source.deviceId(), source.port(), |
| 574 | mcastUtils.assignedVlan(source), mcastIp, INGRESS); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 575 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 576 | // 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 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 584 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source)); |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 585 | mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()), INGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 586 | return; |
| 587 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 588 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 589 | // Find a path. If present, create/update groups and flows for each hop |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 590 | Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(), |
| 591 | mcastIp, allPaths); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 592 | if (mcastPath.isPresent()) { |
| 593 | List<Link> links = mcastPath.get().links(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 594 | |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 595 | // Setup mcast role for ingress |
| 596 | mcastRoleStore.put(new McastStoreKey(mcastIp, source.deviceId()), |
| 597 | INGRESS); |
| 598 | |
| 599 | // Setup properly the transit |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 600 | links.forEach(link -> { |
| 601 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 602 | 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 606 | }); |
| 607 | |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 608 | // 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 614 | // Process the egress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 615 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null)); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 616 | // Setup mcast role for egress |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 617 | mcastRoleStore.put(new McastStoreKey(mcastIp, sink.deviceId()), |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 618 | EGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 619 | } else { |
| 620 | log.warn("Unable to find a path from {} to {}. Abort sinkAdded", |
| 621 | source.deviceId(), sink.deviceId()); |
| 622 | } |
| 623 | } finally { |
| 624 | mcastUnlock(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 629 | * Processes the LINK_DOWN event. |
| 630 | * |
| 631 | * @param affectedLink Link that is going down |
| 632 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 633 | public void processLinkDown(Link affectedLink) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 634 | 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); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 642 | // Verify leadership on the operation |
| 643 | if (!mcastUtils.isLeader(mcastIp)) { |
| 644 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 645 | return; |
| 646 | } |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 647 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 648 | // Find out the ingress, transit and egress device of affected group |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 649 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 650 | .stream().findAny().orElse(null); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 651 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 652 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 653 | ConnectPoint source = mcastUtils.getSource(mcastIp); |
Charles Chan | a8f9dee | 2016-05-16 18:44:13 -0700 | [diff] [blame] | 654 | |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 655 | // 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 661 | return; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 662 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 663 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 664 | // Remove entire transit |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 665 | transitDevices.forEach(transitDevice -> |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 666 | removeGroupFromDevice(transitDevice, mcastIp, |
| 667 | mcastUtils.assignedVlan(null))); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 668 | |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 669 | // Remove transit-facing ports on the ingress device |
| 670 | removeIngressTransitPorts(mcastIp, ingressDevice, source); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 671 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 672 | // TODO create a shared procedure with DEVICE_DOWN |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 673 | // Compute mcast tree for the the egress devices |
| 674 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, egressDevices); |
| 675 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 676 | // We have to verify, if there are egresses without paths |
| 677 | Set<DeviceId> notRecovered = Sets.newHashSet(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 678 | mcastTree.forEach((egressDevice, paths) -> { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 679 | // Let's check if there is at least a path |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 680 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, |
| 681 | mcastIp, paths); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 682 | // 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 686 | log.warn("Fail to recover egress device {} from link failure {}", |
| 687 | egressDevice, affectedLink); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 688 | removeGroupFromDevice(egressDevice, mcastIp, |
| 689 | mcastUtils.assignedVlan(null)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 690 | } |
| 691 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 692 | |
| 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 Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 710 | }); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 711 | } finally { |
| 712 | mcastUnlock(); |
| 713 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /** |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 717 | * Process the DEVICE_DOWN event. |
| 718 | * |
| 719 | * @param deviceDown device going down |
| 720 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 721 | public void processDeviceDown(DeviceId deviceDown) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 722 | 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); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 730 | // Verify leadership on the operation |
| 731 | if (!mcastUtils.isLeader(mcastIp)) { |
| 732 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 733 | return; |
| 734 | } |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 735 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 736 | // Find out the ingress, transit and egress device of affected group |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 737 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 738 | .stream().findAny().orElse(null); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 739 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 740 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 741 | ConnectPoint source = mcastUtils.getSource(mcastIp); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 742 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 743 | // 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 Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 749 | return; |
| 750 | } |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 751 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 752 | // If it exists, we have to remove it in any case |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 753 | if (!transitDevices.isEmpty()) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 754 | // Remove entire transit |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 755 | transitDevices.forEach(transitDevice -> |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 756 | removeGroupFromDevice(transitDevice, mcastIp, |
| 757 | mcastUtils.assignedVlan(null))); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 758 | } |
| 759 | // If the ingress is down |
| 760 | if (ingressDevice.equals(deviceDown)) { |
| 761 | // Remove entire ingress |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 762 | removeGroupFromDevice(ingressDevice, mcastIp, mcastUtils.assignedVlan(source)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 763 | // If other sinks different from the ingress exist |
| 764 | if (!egressDevices.isEmpty()) { |
| 765 | // Remove all the remaining egress |
| 766 | egressDevices.forEach( |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 767 | egressDevice -> removeGroupFromDevice(egressDevice, mcastIp, |
| 768 | mcastUtils.assignedVlan(null)) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 769 | ); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 770 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 771 | } else { |
| 772 | // Egress or transit could be down at this point |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 773 | // Get the ingress-transit ports if they exist |
| 774 | removeIngressTransitPorts(mcastIp, ingressDevice, source); |
| 775 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 776 | // One of the egress device is down |
| 777 | if (egressDevices.contains(deviceDown)) { |
| 778 | // Remove entire device down |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 779 | removeGroupFromDevice(deviceDown, mcastIp, mcastUtils.assignedVlan(null)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 780 | // 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 784 | // We have done |
| 785 | return; |
| 786 | } |
| 787 | } |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 788 | |
| 789 | // Compute mcast tree for the the egress devices |
| 790 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, egressDevices); |
| 791 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 792 | // We have to verify, if there are egresses without paths |
| 793 | Set<DeviceId> notRecovered = Sets.newHashSet(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 794 | mcastTree.forEach((egressDevice, paths) -> { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 795 | // Let's check if there is at least a path |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 796 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 797 | mcastIp, paths); |
| 798 | // No paths, we have to try with alternative location |
| 799 | if (!mcastPath.isPresent()) { |
| 800 | notRecovered.add(egressDevice); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 801 | // 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); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 804 | removeGroupFromDevice(egressDevice, mcastIp, mcastUtils.assignedVlan(null)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 805 | } |
| 806 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 807 | |
| 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 Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 825 | } |
| 826 | }); |
| 827 | } finally { |
| 828 | mcastUnlock(); |
| 829 | } |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 833 | * 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 | /** |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 920 | * 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 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 952 | * Process new locations and return the set of sinks to be added |
| 953 | * in the context of the recovery. |
| 954 | * |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 955 | * @param newSinks the remaining sinks |
| 956 | * @param prevSinks the previous sinks |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 957 | * @return the set of the sinks to be processed |
| 958 | */ |
| 959 | private Set<ConnectPoint> processSinksToBeRecovered(IpAddress mcastIp, |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 960 | Map<HostId, Set<ConnectPoint>> newSinks, |
| 961 | Map<HostId, Set<ConnectPoint>> prevSinks) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 962 | // 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(); |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 965 | newSinks.forEach((hostId, connectPoints) -> { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 966 | // 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 | } |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 972 | // If previously it had two locations, we need to recover it |
| 973 | // Filter out if the remaining location is already served |
| 974 | if (prevSinks.containsKey(hostId) && prevSinks.get(hostId).size() == 2) { |
| 975 | sinksToBeProcessed.add(connectPoints.stream() |
| 976 | .filter(connectPoint -> !isSink(mcastIp, connectPoint)) |
| 977 | .findFirst().orElseGet(null)); |
| 978 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 979 | }); |
| 980 | return sinksToBeProcessed; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Process all the sinks related to a mcast group and return |
| 985 | * the ones to be processed. |
| 986 | * |
| 987 | * @param source the source connect point |
| 988 | * @param mcastIp the group address |
| 989 | * @param sinks the sinks to be evaluated |
| 990 | * @return the set of the sinks to be processed |
| 991 | */ |
| 992 | private Set<ConnectPoint> processSinksToBeAdded(ConnectPoint source, IpAddress mcastIp, |
| 993 | Map<HostId, Set<ConnectPoint>> sinks) { |
| 994 | // Iterate over the sinks in order to build the set |
| 995 | // of the connect points to be served by this group |
| 996 | final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet(); |
| 997 | sinks.forEach(((hostId, connectPoints) -> { |
| 998 | // If it has more than 2 locations |
| 999 | if (connectPoints.size() > 2 || connectPoints.size() == 0) { |
| 1000 | log.debug("Skip {} since sink {} has {} locations", |
| 1001 | mcastIp, hostId, connectPoints.size()); |
| 1002 | return; |
| 1003 | } |
| 1004 | // If it has one location, just use it |
| 1005 | if (connectPoints.size() == 1) { |
| 1006 | sinksToBeProcessed.add(connectPoints.stream() |
| 1007 | .findFirst().orElseGet(null)); |
| 1008 | return; |
| 1009 | } |
| 1010 | // We prefer to reuse existing flows |
| 1011 | ConnectPoint sinkToBeProcessed = connectPoints.stream() |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1012 | .filter(connectPoint -> isSink(mcastIp, connectPoint)) |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1013 | .findFirst().orElse(null); |
| 1014 | if (sinkToBeProcessed != null) { |
| 1015 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1016 | return; |
| 1017 | } |
| 1018 | // Otherwise we prefer to reuse existing egresses |
| 1019 | Set<DeviceId> egresses = getDevice(mcastIp, EGRESS); |
| 1020 | sinkToBeProcessed = connectPoints.stream() |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1021 | .filter(connectPoint -> egresses.contains(connectPoint.deviceId())) |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1022 | .findFirst().orElse(null); |
| 1023 | if (sinkToBeProcessed != null) { |
| 1024 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1025 | return; |
| 1026 | } |
| 1027 | // Otherwise we prefer a location co-located with the source (if it exists) |
| 1028 | sinkToBeProcessed = connectPoints.stream() |
| 1029 | .filter(connectPoint -> connectPoint.deviceId().equals(source.deviceId())) |
| 1030 | .findFirst().orElse(null); |
| 1031 | if (sinkToBeProcessed != null) { |
| 1032 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1033 | return; |
| 1034 | } |
| 1035 | // Finally, we randomly pick a new location |
| 1036 | sinksToBeProcessed.add(connectPoints.stream() |
| 1037 | .findFirst().orElseGet(null)); |
| 1038 | })); |
| 1039 | // We have done, return the set |
| 1040 | return sinksToBeProcessed; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1044 | * Utility method to remove all the ingress transit ports. |
| 1045 | * |
| 1046 | * @param mcastIp the group ip |
| 1047 | * @param ingressDevice the ingress device for this group |
| 1048 | * @param source the source connect point |
| 1049 | */ |
| 1050 | private void removeIngressTransitPorts(IpAddress mcastIp, DeviceId ingressDevice, |
| 1051 | ConnectPoint source) { |
| 1052 | Set<PortNumber> ingressTransitPorts = ingressTransitPort(mcastIp); |
| 1053 | ingressTransitPorts.forEach(ingressTransitPort -> { |
| 1054 | if (ingressTransitPort != null) { |
| 1055 | boolean isLast = removePortFromDevice(ingressDevice, ingressTransitPort, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1056 | mcastIp, mcastUtils.assignedVlan(source)); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1057 | if (isLast) { |
| 1058 | mcastRoleStore.remove(new McastStoreKey(mcastIp, ingressDevice)); |
| 1059 | } |
| 1060 | } |
| 1061 | }); |
| 1062 | } |
| 1063 | |
| 1064 | /** |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1065 | * Adds a port to given multicast group on given device. This involves the |
| 1066 | * update of L3 multicast group and multicast routing table entry. |
| 1067 | * |
| 1068 | * @param deviceId device ID |
| 1069 | * @param port port to be added |
| 1070 | * @param mcastIp multicast group |
| 1071 | * @param assignedVlan assigned VLAN ID |
| 1072 | */ |
| 1073 | private void addPortToDevice(DeviceId deviceId, PortNumber port, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1074 | IpAddress mcastIp, VlanId assignedVlan) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1075 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1076 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1077 | NextObjective newNextObj; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1078 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1079 | // First time someone request this mcast group via this device |
| 1080 | portBuilder.add(port); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1081 | // New nextObj |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1082 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1083 | portBuilder.build(), null).add(); |
| 1084 | // Store the new port |
| 1085 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1086 | } else { |
| 1087 | // This device already serves some subscribers of this mcast group |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1088 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1089 | // Stop if the port is already in the nextobj |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1090 | Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next()); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1091 | if (existingPorts.contains(port)) { |
| 1092 | log.info("NextObj for {}/{} already exists. Abort", deviceId, port); |
| 1093 | return; |
| 1094 | } |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1095 | // Let's add the port and reuse the previous one |
Yuta HIGUCHI | bef07b5 | 2018-02-09 18:05:23 -0800 | [diff] [blame] | 1096 | portBuilder.addAll(existingPorts).add(port); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1097 | // Reuse previous nextObj |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1098 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1099 | portBuilder.build(), nextObj.id()).addToExisting(); |
| 1100 | // Store the final next objective and send only the difference to the driver |
| 1101 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
| 1102 | // Add just the new port |
| 1103 | portBuilder = ImmutableSet.builder(); |
| 1104 | portBuilder.add(port); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1105 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1106 | portBuilder.build(), nextObj.id()).addToExisting(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1107 | } |
| 1108 | // Create, store and apply the new nextObj and fwdObj |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1109 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1110 | (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}", |
| 1111 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 1112 | (objective, error) -> |
| 1113 | log.warn("Failed to add {} on {}/{}, vlan {}: {}", |
| 1114 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1115 | ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, |
| 1116 | newNextObj.id()).add(context); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1117 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 1118 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * Removes a port from given multicast group on given device. |
| 1123 | * This involves the update of L3 multicast group and multicast routing |
| 1124 | * table entry. |
| 1125 | * |
| 1126 | * @param deviceId device ID |
| 1127 | * @param port port to be added |
| 1128 | * @param mcastIp multicast group |
| 1129 | * @param assignedVlan assigned VLAN ID |
| 1130 | * @return true if this is the last sink on this device |
| 1131 | */ |
| 1132 | private boolean removePortFromDevice(DeviceId deviceId, PortNumber port, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1133 | IpAddress mcastIp, VlanId assignedVlan) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1134 | McastStoreKey mcastStoreKey = |
| 1135 | new McastStoreKey(mcastIp, deviceId); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1136 | // This device is not serving this multicast group |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1137 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1138 | log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port); |
| 1139 | return false; |
| 1140 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1141 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1142 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1143 | Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1144 | // This port does not serve this multicast group |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1145 | if (!existingPorts.contains(port)) { |
| 1146 | log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port); |
| 1147 | return false; |
| 1148 | } |
| 1149 | // Copy and modify the ImmutableSet |
| 1150 | existingPorts = Sets.newHashSet(existingPorts); |
| 1151 | existingPorts.remove(port); |
| 1152 | |
| 1153 | NextObjective newNextObj; |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1154 | ObjectiveContext context; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1155 | ForwardingObjective fwdObj; |
| 1156 | if (existingPorts.isEmpty()) { |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1157 | // If this is the last sink, remove flows and last bucket |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1158 | // NOTE: Rely on GroupStore garbage collection rather than explicitly |
| 1159 | // remove L3MG since there might be other flows/groups refer to |
| 1160 | // the same L2IG |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1161 | context = new DefaultObjectiveContext( |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1162 | (objective) -> log.debug("Successfully remove {} on {}/{}, vlan {}", |
| 1163 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 1164 | (objective, error) -> |
| 1165 | log.warn("Failed to remove {} on {}/{}, vlan {}: {}", |
| 1166 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1167 | fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1168 | mcastNextObjStore.remove(mcastStoreKey); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1169 | } else { |
| 1170 | // If this is not the last sink, update flows and groups |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1171 | context = new DefaultObjectiveContext( |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1172 | (objective) -> log.debug("Successfully update {} on {}/{}, vlan {}", |
| 1173 | mcastIp, deviceId, port.toLong(), assignedVlan), |
| 1174 | (objective, error) -> |
| 1175 | log.warn("Failed to update {} on {}/{}, vlan {}: {}", |
| 1176 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1177 | // Here we store the next objective with the remaining port |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1178 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1179 | existingPorts, nextObj.id()).removeFromExisting(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1180 | fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1181 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1182 | } |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1183 | // Let's modify the next objective removing the bucket |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1184 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1185 | ImmutableSet.of(port), nextObj.id()).removeFromExisting(); |
| 1186 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 1187 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1188 | return existingPorts.isEmpty(); |
| 1189 | } |
| 1190 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1191 | /** |
| 1192 | * Removes entire group on given device. |
| 1193 | * |
| 1194 | * @param deviceId device ID |
| 1195 | * @param mcastIp multicast group to be removed |
| 1196 | * @param assignedVlan assigned VLAN ID |
| 1197 | */ |
| 1198 | private void removeGroupFromDevice(DeviceId deviceId, IpAddress mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1199 | VlanId assignedVlan) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1200 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId); |
| 1201 | // This device is not serving this multicast group |
| 1202 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1203 | log.warn("{} is not serving {}. Abort.", deviceId, mcastIp); |
| 1204 | return; |
| 1205 | } |
| 1206 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1207 | // NOTE: Rely on GroupStore garbage collection rather than explicitly |
| 1208 | // remove L3MG since there might be other flows/groups refer to |
| 1209 | // the same L2IG |
| 1210 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1211 | (objective) -> log.debug("Successfully remove {} on {}, vlan {}", |
| 1212 | mcastIp, deviceId, assignedVlan), |
| 1213 | (objective, error) -> |
| 1214 | log.warn("Failed to remove {} on {}, vlan {}: {}", |
| 1215 | mcastIp, deviceId, assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1216 | ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1217 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
| 1218 | mcastNextObjStore.remove(mcastStoreKey); |
| 1219 | mcastRoleStore.remove(mcastStoreKey); |
| 1220 | } |
| 1221 | |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1222 | private void installPath(IpAddress mcastIp, ConnectPoint source, Path mcastPath) { |
| 1223 | // Get Links |
| 1224 | List<Link> links = mcastPath.links(); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1225 | |
| 1226 | // Setup new ingress mcast role |
| 1227 | mcastRoleStore.put(new McastStoreKey(mcastIp, links.get(0).src().deviceId()), |
| 1228 | INGRESS); |
| 1229 | |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1230 | // For each link, modify the next on the source device adding the src port |
| 1231 | // and a new filter objective on the destination port |
| 1232 | links.forEach(link -> { |
| 1233 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1234 | mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null)); |
| 1235 | mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(), |
| 1236 | mcastUtils.assignedVlan(null), mcastIp, null); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1237 | }); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1238 | |
| 1239 | // Setup mcast role for the transit |
| 1240 | links.stream() |
| 1241 | .filter(link -> !link.src().deviceId().equals(source.deviceId())) |
| 1242 | .forEach(link -> mcastRoleStore.put(new McastStoreKey(mcastIp, link.src().deviceId()), |
| 1243 | TRANSIT)); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1246 | /** |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1247 | * Go through all the paths, looking for shared links to be used |
| 1248 | * in the final path computation. |
| 1249 | * |
| 1250 | * @param egresses egress devices |
| 1251 | * @param availablePaths all the available paths towards the egress |
| 1252 | * @return shared links between egress devices |
| 1253 | */ |
| 1254 | private Set<Link> exploreMcastTree(Set<DeviceId> egresses, |
| 1255 | Map<DeviceId, List<Path>> availablePaths) { |
| 1256 | // Length of the shortest path |
| 1257 | int minLength = Integer.MAX_VALUE; |
| 1258 | int length; |
| 1259 | // Current paths |
| 1260 | List<Path> currentPaths; |
| 1261 | // Verify the source can still reach all the egresses |
| 1262 | for (DeviceId egress : egresses) { |
| 1263 | // From the source we cannot reach all the sinks |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1264 | // just continue and let's figure out after |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1265 | currentPaths = availablePaths.get(egress); |
| 1266 | if (currentPaths.isEmpty()) { |
| 1267 | continue; |
| 1268 | } |
| 1269 | // Get the length of the first one available, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1270 | // update the min length |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1271 | length = currentPaths.get(0).links().size(); |
| 1272 | if (length < minLength) { |
| 1273 | minLength = length; |
| 1274 | } |
Pier Luigi | 51ee7c0 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1275 | } |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1276 | // If there are no paths |
| 1277 | if (minLength == Integer.MAX_VALUE) { |
| 1278 | return Collections.emptySet(); |
| 1279 | } |
| 1280 | // Iterate looking for shared links |
| 1281 | int index = 0; |
| 1282 | // Define the sets for the intersection |
| 1283 | Set<Link> sharedLinks = Sets.newHashSet(); |
| 1284 | Set<Link> currentSharedLinks; |
| 1285 | Set<Link> currentLinks; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1286 | DeviceId egressToRemove = null; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1287 | // Let's find out the shared links |
| 1288 | while (index < minLength) { |
| 1289 | // Initialize the intersection with the paths related to the first egress |
| 1290 | currentPaths = availablePaths.get( |
| 1291 | egresses.stream() |
| 1292 | .findFirst() |
| 1293 | .orElse(null) |
| 1294 | ); |
| 1295 | currentSharedLinks = Sets.newHashSet(); |
| 1296 | // Iterate over the paths and take the "index" links |
| 1297 | for (Path path : currentPaths) { |
| 1298 | currentSharedLinks.add(path.links().get(index)); |
| 1299 | } |
| 1300 | // Iterate over the remaining egress |
| 1301 | for (DeviceId egress : egresses) { |
| 1302 | // Iterate over the paths and take the "index" links |
| 1303 | currentLinks = Sets.newHashSet(); |
| 1304 | for (Path path : availablePaths.get(egress)) { |
| 1305 | currentLinks.add(path.links().get(index)); |
| 1306 | } |
| 1307 | // Do intersection |
| 1308 | currentSharedLinks = Sets.intersection(currentSharedLinks, currentLinks); |
| 1309 | // If there are no shared paths exit and record the device to remove |
| 1310 | // we have to retry with a subset of sinks |
| 1311 | if (currentSharedLinks.isEmpty()) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1312 | egressToRemove = egress; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1313 | index = minLength; |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | sharedLinks.addAll(currentSharedLinks); |
| 1318 | index++; |
| 1319 | } |
| 1320 | // If the shared links is empty and there are egress |
| 1321 | // let's retry another time with less sinks, we can |
| 1322 | // still build optimal subtrees |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1323 | if (sharedLinks.isEmpty() && egresses.size() > 1 && egressToRemove != null) { |
| 1324 | egresses.remove(egressToRemove); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1325 | sharedLinks = exploreMcastTree(egresses, availablePaths); |
| 1326 | } |
| 1327 | return sharedLinks; |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Build Mcast tree having as root the given source and as leaves the given egress points. |
| 1332 | * |
| 1333 | * @param source source of the tree |
| 1334 | * @param sinks leaves of the tree |
| 1335 | * @return the computed Mcast tree |
| 1336 | */ |
| 1337 | private Map<ConnectPoint, List<Path>> computeSinkMcastTree(DeviceId source, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1338 | Set<ConnectPoint> sinks) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1339 | // Get the egress devices, remove source from the egress if present |
| 1340 | Set<DeviceId> egresses = sinks.stream() |
| 1341 | .map(ConnectPoint::deviceId) |
| 1342 | .filter(deviceId -> !deviceId.equals(source)) |
| 1343 | .collect(Collectors.toSet()); |
| 1344 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(source, egresses); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1345 | // Build final tree and return it as it is |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1346 | final Map<ConnectPoint, List<Path>> finalTree = Maps.newHashMap(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1347 | // We need to put back the source if it was originally present |
| 1348 | sinks.forEach(sink -> { |
| 1349 | List<Path> sinkPaths = mcastTree.get(sink.deviceId()); |
| 1350 | finalTree.put(sink, sinkPaths != null ? sinkPaths : ImmutableList.of()); |
| 1351 | }); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1352 | return finalTree; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * Build Mcast tree having as root the given source and as leaves the given egress. |
| 1357 | * |
| 1358 | * @param source source of the tree |
| 1359 | * @param egresses leaves of the tree |
| 1360 | * @return the computed Mcast tree |
| 1361 | */ |
| 1362 | private Map<DeviceId, List<Path>> computeMcastTree(DeviceId source, |
| 1363 | Set<DeviceId> egresses) { |
| 1364 | // Pre-compute all the paths |
| 1365 | Map<DeviceId, List<Path>> availablePaths = Maps.newHashMap(); |
| 1366 | // No links to enforce |
| 1367 | egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress, |
| 1368 | Collections.emptySet()))); |
| 1369 | // Explore the topology looking for shared links amongst the egresses |
| 1370 | Set<Link> linksToEnforce = exploreMcastTree(Sets.newHashSet(egresses), availablePaths); |
| 1371 | // Remove all the paths from the previous computation |
| 1372 | availablePaths.clear(); |
| 1373 | // Build the final paths enforcing the shared links between egress devices |
| 1374 | egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress, |
| 1375 | linksToEnforce))); |
| 1376 | return availablePaths; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Gets path from src to dst computed using the custom link weigher. |
| 1381 | * |
| 1382 | * @param src source device ID |
| 1383 | * @param dst destination device ID |
| 1384 | * @return list of paths from src to dst |
| 1385 | */ |
| 1386 | private List<Path> getPaths(DeviceId src, DeviceId dst, Set<Link> linksToEnforce) { |
| 1387 | // Takes a snapshot of the topology |
| 1388 | final Topology currentTopology = topologyService.currentTopology(); |
| 1389 | // Build a specific link weigher for this path computation |
| 1390 | final LinkWeigher linkWeigher = new SRLinkWeigher(srManager, src, linksToEnforce); |
| 1391 | // We will use our custom link weigher for our path |
| 1392 | // computations and build the list of valid paths |
| 1393 | List<Path> allPaths = Lists.newArrayList( |
| 1394 | topologyService.getPaths(currentTopology, src, dst, linkWeigher) |
| 1395 | ); |
| 1396 | // If there are no valid paths, just exit |
| 1397 | log.debug("{} path(s) found from {} to {}", allPaths.size(), src, dst); |
| 1398 | return allPaths; |
Pier Luigi | 51ee7c0 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1401 | /** |
| 1402 | * Gets a path from src to dst. |
| 1403 | * If a path was allocated before, returns the allocated path. |
| 1404 | * Otherwise, randomly pick one from available paths. |
| 1405 | * |
| 1406 | * @param src source device ID |
| 1407 | * @param dst destination device ID |
| 1408 | * @param mcastIp multicast group |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1409 | * @param allPaths paths list |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1410 | * @return an optional path from src to dst |
| 1411 | */ |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1412 | private Optional<Path> getPath(DeviceId src, DeviceId dst, |
| 1413 | IpAddress mcastIp, List<Path> allPaths) { |
| 1414 | // Firstly we get all the valid paths, if the supplied are null |
| 1415 | if (allPaths == null) { |
| 1416 | allPaths = getPaths(src, dst, Collections.emptySet()); |
| 1417 | } |
| 1418 | |
| 1419 | // If there are no paths just exit |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1420 | if (allPaths.isEmpty()) { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1421 | return Optional.empty(); |
| 1422 | } |
| 1423 | |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1424 | // Create a map index of suitablity-to-list of paths. For example |
| 1425 | // a path in the list associated to the index 1 shares only the |
| 1426 | // first hop and it is less suitable of a path belonging to the index |
| 1427 | // 2 that shares leaf-spine. |
| 1428 | Map<Integer, List<Path>> eligiblePaths = Maps.newHashMap(); |
| 1429 | // Some init steps |
| 1430 | int nhop; |
| 1431 | McastStoreKey mcastStoreKey; |
| 1432 | Link hop; |
| 1433 | PortNumber srcPort; |
| 1434 | Set<PortNumber> existingPorts; |
| 1435 | NextObjective nextObj; |
| 1436 | // Iterate over paths looking for eligible paths |
| 1437 | for (Path path : allPaths) { |
| 1438 | // Unlikely, it will happen... |
| 1439 | if (!src.equals(path.links().get(0).src().deviceId())) { |
| 1440 | continue; |
| 1441 | } |
| 1442 | nhop = 0; |
| 1443 | // Iterate over the links |
| 1444 | while (nhop < path.links().size()) { |
| 1445 | // Get the link and verify if a next related |
| 1446 | // to the src device exist in the store |
| 1447 | hop = path.links().get(nhop); |
| 1448 | mcastStoreKey = new McastStoreKey(mcastIp, hop.src().deviceId()); |
| 1449 | // It does not exist in the store, exit |
| 1450 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1451 | break; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1452 | } |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1453 | // Get the output ports on the next |
| 1454 | nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1455 | existingPorts = mcastUtils.getPorts(nextObj.next()); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1456 | // And the src port on the link |
| 1457 | srcPort = hop.src().port(); |
| 1458 | // the src port is not used as output, exit |
| 1459 | if (!existingPorts.contains(srcPort)) { |
| 1460 | break; |
| 1461 | } |
| 1462 | nhop++; |
| 1463 | } |
| 1464 | // n_hop defines the index |
| 1465 | if (nhop > 0) { |
| 1466 | eligiblePaths.compute(nhop, (index, paths) -> { |
| 1467 | paths = paths == null ? Lists.newArrayList() : paths; |
| 1468 | paths.add(path); |
| 1469 | return paths; |
| 1470 | }); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1471 | } |
| 1472 | } |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1473 | |
| 1474 | // No suitable paths |
| 1475 | if (eligiblePaths.isEmpty()) { |
| 1476 | log.debug("No eligiblePath(s) found from {} to {}", src, dst); |
| 1477 | // Otherwise, randomly pick a path |
| 1478 | Collections.shuffle(allPaths); |
| 1479 | return allPaths.stream().findFirst(); |
| 1480 | } |
| 1481 | |
| 1482 | // Let's take the best ones |
| 1483 | Integer bestIndex = eligiblePaths.keySet() |
| 1484 | .stream() |
| 1485 | .sorted(Comparator.reverseOrder()) |
| 1486 | .findFirst().orElse(null); |
| 1487 | List<Path> bestPaths = eligiblePaths.get(bestIndex); |
| 1488 | log.debug("{} eligiblePath(s) found from {} to {}", |
| 1489 | bestPaths.size(), src, dst); |
| 1490 | // randomly pick a path on the highest index |
| 1491 | Collections.shuffle(bestPaths); |
| 1492 | return bestPaths.stream().findFirst(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1496 | * Gets device(s) of given role in given multicast group. |
| 1497 | * |
| 1498 | * @param mcastIp multicast IP |
| 1499 | * @param role multicast role |
| 1500 | * @return set of device ID or empty set if not found |
| 1501 | */ |
| 1502 | private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role) { |
| 1503 | return mcastRoleStore.entrySet().stream() |
| 1504 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) && |
| 1505 | entry.getValue().value() == role) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1506 | .map(Entry::getKey).map(McastStoreKey::deviceId) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1507 | .collect(Collectors.toSet()); |
| 1508 | } |
| 1509 | |
| 1510 | /** |
| 1511 | * Gets groups which is affected by the link down event. |
| 1512 | * |
| 1513 | * @param link link going down |
| 1514 | * @return a set of multicast IpAddress |
| 1515 | */ |
| 1516 | private Set<IpAddress> getAffectedGroups(Link link) { |
| 1517 | DeviceId deviceId = link.src().deviceId(); |
| 1518 | PortNumber port = link.src().port(); |
| 1519 | return mcastNextObjStore.entrySet().stream() |
| 1520 | .filter(entry -> entry.getKey().deviceId().equals(deviceId) && |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1521 | mcastUtils.getPorts(entry.getValue().value().next()).contains(port)) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1522 | .map(Entry::getKey).map(McastStoreKey::mcastIp) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1523 | .collect(Collectors.toSet()); |
| 1524 | } |
| 1525 | |
| 1526 | /** |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1527 | * Gets groups which are affected by the device down event. |
| 1528 | * |
| 1529 | * @param deviceId device going down |
| 1530 | * @return a set of multicast IpAddress |
| 1531 | */ |
| 1532 | private Set<IpAddress> getAffectedGroups(DeviceId deviceId) { |
| 1533 | return mcastNextObjStore.entrySet().stream() |
| 1534 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1535 | .map(Entry::getKey).map(McastStoreKey::mcastIp) |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1536 | .collect(Collectors.toSet()); |
| 1537 | } |
| 1538 | |
| 1539 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1540 | * Gets the spine-facing port on ingress device of given multicast group. |
| 1541 | * |
| 1542 | * @param mcastIp multicast IP |
| 1543 | * @return spine-facing port on ingress device |
| 1544 | */ |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1545 | private Set<PortNumber> ingressTransitPort(IpAddress mcastIp) { |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1546 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1547 | .stream().findAny().orElse(null); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1548 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1549 | if (ingressDevice != null) { |
| 1550 | NextObjective nextObj = mcastNextObjStore |
| 1551 | .get(new McastStoreKey(mcastIp, ingressDevice)).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1552 | Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next()); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1553 | // Let's find out all the ingress-transit ports |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1554 | for (PortNumber port : ports) { |
| 1555 | // Spine-facing port should have no subnet and no xconnect |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1556 | if (srManager.deviceConfiguration() != null && |
| 1557 | srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() && |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1558 | !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) { |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1559 | portBuilder.add(port); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | } |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1563 | return portBuilder.build(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1564 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1565 | |
| 1566 | /** |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1567 | * Verify if the given device has sinks |
| 1568 | * for the multicast group. |
| 1569 | * |
| 1570 | * @param deviceId device Id |
| 1571 | * @param mcastIp multicast IP |
| 1572 | * @return true if the device has sink for the group. |
| 1573 | * False otherwise. |
| 1574 | */ |
| 1575 | private boolean hasSinks(DeviceId deviceId, IpAddress mcastIp) { |
| 1576 | if (deviceId != null) { |
| 1577 | // Get the nextobjective |
| 1578 | Versioned<NextObjective> versionedNextObj = mcastNextObjStore.get( |
| 1579 | new McastStoreKey(mcastIp, deviceId) |
| 1580 | ); |
| 1581 | // If it exists |
| 1582 | if (versionedNextObj != null) { |
| 1583 | NextObjective nextObj = versionedNextObj.value(); |
| 1584 | // Retrieves all the output ports |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1585 | Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next()); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1586 | // Tries to find at least one port that is not spine-facing |
| 1587 | for (PortNumber port : ports) { |
| 1588 | // Spine-facing port should have no subnet and no xconnect |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1589 | if (srManager.deviceConfiguration() != null && |
| 1590 | (!srManager.deviceConfiguration().getPortSubnets(deviceId, port).isEmpty() || |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1591 | srManager.xConnectHandler.hasXConnect(new ConnectPoint(deviceId, port)))) { |
| 1592 | return true; |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | return false; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1601 | * Verify if a given connect point is sink for this group. |
| 1602 | * |
| 1603 | * @param mcastIp group address |
| 1604 | * @param connectPoint connect point to be verified |
| 1605 | * @return true if the connect point is sink of the group |
| 1606 | */ |
| 1607 | private boolean isSink(IpAddress mcastIp, ConnectPoint connectPoint) { |
| 1608 | // Let's check if we are already serving that location |
| 1609 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, connectPoint.deviceId()); |
| 1610 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1611 | return false; |
| 1612 | } |
| 1613 | // Get next and check with the port |
| 1614 | NextObjective mcastNext = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1615 | return mcastUtils.getPorts(mcastNext.next()).contains(connectPoint.port()); |
| 1616 | } |
| 1617 | |
| 1618 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1619 | * Updates filtering objective for given device and port. |
| 1620 | * It is called in general when the mcast config has been |
| 1621 | * changed. |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1622 | * |
| 1623 | * @param deviceId device ID |
| 1624 | * @param portNum ingress port number |
| 1625 | * @param vlanId assigned VLAN ID |
| 1626 | * @param install true to add, false to remove |
| 1627 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1628 | public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum, |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1629 | VlanId vlanId, boolean install) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1630 | lastMcastChange = Instant.now(); |
| 1631 | mcastLock(); |
| 1632 | try { |
| 1633 | // Iterates over the route and updates properly the filtering objective |
| 1634 | // on the source device. |
| 1635 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1636 | log.debug("Update filter for {}", mcastRoute.group()); |
| 1637 | // Verify leadership on the operation |
| 1638 | if (!mcastUtils.isLeader(mcastRoute.group())) { |
| 1639 | log.debug("Skip {} due to lack of leadership", mcastRoute.group()); |
| 1640 | return; |
| 1641 | } |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1642 | // FIXME To be addressed with multiple sources support |
| 1643 | ConnectPoint source = srManager.multicastRouteService.sources(mcastRoute) |
| 1644 | .stream() |
| 1645 | .findFirst().orElse(null); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1646 | if (source.deviceId().equals(deviceId) && source.port().equals(portNum)) { |
| 1647 | if (install) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1648 | mcastUtils.addFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), INGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1649 | } else { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1650 | mcastUtils.removeFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), null); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1651 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1652 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1653 | }); |
| 1654 | } finally { |
| 1655 | mcastUnlock(); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * Performs bucket verification operation for all mcast groups in the devices. |
| 1661 | * Firstly, it verifies that mcast is stable before trying verification operation. |
| 1662 | * Verification consists in creating new nexts with VERIFY operation. Actually, |
| 1663 | * the operation is totally delegated to the driver. |
| 1664 | */ |
| 1665 | private final class McastBucketCorrector implements Runnable { |
| 1666 | |
| 1667 | @Override |
| 1668 | public void run() { |
| 1669 | // Verify if the Mcast has been stable for MCAST_STABLITY_THRESHOLD |
| 1670 | if (!isMcastStable()) { |
| 1671 | return; |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1672 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1673 | // Acquires lock |
| 1674 | mcastLock(); |
| 1675 | try { |
| 1676 | // Iterates over the routes and verify the related next objectives |
| 1677 | srManager.multicastRouteService.getRoutes() |
| 1678 | .stream() |
| 1679 | .map(McastRoute::group) |
| 1680 | .forEach(mcastIp -> { |
| 1681 | log.trace("Running mcast buckets corrector for mcast group: {}", |
| 1682 | mcastIp); |
| 1683 | |
| 1684 | // For each group we get current information in the store |
| 1685 | // and issue a check of the next objectives in place |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1686 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1687 | .stream().findAny().orElse(null); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1688 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT); |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 1689 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1690 | // Get source and sinks from Mcast Route Service and warn about errors |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1691 | ConnectPoint source = mcastUtils.getSource(mcastIp); |
| 1692 | Set<ConnectPoint> sinks = mcastUtils.getSinks(mcastIp).values().stream() |
| 1693 | .flatMap(Collection::stream) |
| 1694 | .collect(Collectors.toSet()); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1695 | |
| 1696 | // Do not proceed if ingress device or source of this group are missing |
| 1697 | if (ingressDevice == null || source == null) { |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1698 | if (!sinks.isEmpty()) { |
| 1699 | log.warn("Unable to run buckets corrector. " + |
| 1700 | "Missing ingress {} or source {} for group {}", |
| 1701 | ingressDevice, source, mcastIp); |
| 1702 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1703 | return; |
| 1704 | } |
| 1705 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1706 | // Continue only when this instance is the leader of the group |
| 1707 | if (!mcastUtils.isLeader(mcastIp)) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1708 | log.trace("Unable to run buckets corrector. " + |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1709 | "Skip {} due to lack of leadership", mcastIp); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1710 | return; |
| 1711 | } |
| 1712 | |
| 1713 | // Create the set of the devices to be processed |
| 1714 | ImmutableSet.Builder<DeviceId> devicesBuilder = ImmutableSet.builder(); |
| 1715 | devicesBuilder.add(ingressDevice); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1716 | if (!transitDevices.isEmpty()) { |
| 1717 | devicesBuilder.addAll(transitDevices); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1718 | } |
| 1719 | if (!egressDevices.isEmpty()) { |
| 1720 | devicesBuilder.addAll(egressDevices); |
| 1721 | } |
| 1722 | Set<DeviceId> devicesToProcess = devicesBuilder.build(); |
| 1723 | |
| 1724 | // Iterate over the devices |
| 1725 | devicesToProcess.forEach(deviceId -> { |
| 1726 | McastStoreKey currentKey = new McastStoreKey(mcastIp, deviceId); |
| 1727 | // If next exists in our store verify related next objective |
| 1728 | if (mcastNextObjStore.containsKey(currentKey)) { |
| 1729 | NextObjective currentNext = mcastNextObjStore.get(currentKey).value(); |
| 1730 | // Get current ports |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1731 | Set<PortNumber> currentPorts = mcastUtils.getPorts(currentNext.next()); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1732 | // Rebuild the next objective |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1733 | currentNext = mcastUtils.nextObjBuilder( |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1734 | mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1735 | mcastUtils.assignedVlan(deviceId.equals(source.deviceId()) ? |
| 1736 | source : null), |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1737 | currentPorts, |
| 1738 | currentNext.id() |
| 1739 | ).verify(); |
| 1740 | // Send to the flowobjective service |
| 1741 | srManager.flowObjectiveService.next(deviceId, currentNext); |
| 1742 | } else { |
Pier Luigi | d8a1516 | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 1743 | log.warn("Unable to run buckets corrector. " + |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1744 | "Missing next for {} and group {}", |
| 1745 | deviceId, mcastIp); |
| 1746 | } |
| 1747 | }); |
| 1748 | |
| 1749 | }); |
| 1750 | } finally { |
| 1751 | // Finally, it releases the lock |
| 1752 | mcastUnlock(); |
| 1753 | } |
| 1754 | |
| 1755 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1756 | } |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1757 | |
| 1758 | public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) { |
| 1759 | // If mcast ip is present |
| 1760 | if (mcastIp != null) { |
| 1761 | return mcastNextObjStore.entrySet().stream() |
| 1762 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1763 | .collect(Collectors.toMap(Entry::getKey, |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1764 | entry -> entry.getValue().value().id())); |
| 1765 | } |
| 1766 | // Otherwise take all the groups |
| 1767 | return mcastNextObjStore.entrySet().stream() |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1768 | .collect(Collectors.toMap(Entry::getKey, |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1769 | entry -> entry.getValue().value().id())); |
| 1770 | } |
| 1771 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 1772 | /** |
| 1773 | * Returns the associated roles to the mcast groups or to the single |
| 1774 | * group if mcastIp is present. |
| 1775 | * |
| 1776 | * @param mcastIp the group ip |
| 1777 | * @return the mapping mcastIp-device to mcast role |
| 1778 | * |
| 1779 | * @deprecated in 1.12 ("Magpie") release. |
| 1780 | */ |
| 1781 | @Deprecated |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1782 | public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) { |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1783 | // If mcast ip is present |
| 1784 | if (mcastIp != null) { |
| 1785 | return mcastRoleStore.entrySet().stream() |
| 1786 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1787 | .collect(Collectors.toMap(Entry::getKey, |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1788 | entry -> entry.getValue().value())); |
| 1789 | } |
| 1790 | // Otherwise take all the groups |
| 1791 | return mcastRoleStore.entrySet().stream() |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1792 | .collect(Collectors.toMap(Entry::getKey, |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1793 | entry -> entry.getValue().value())); |
| 1794 | } |
| 1795 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 1796 | /** |
| 1797 | * Returns the associated paths to the mcast group. |
| 1798 | * |
| 1799 | * @param mcastIp the group ip |
| 1800 | * @return the mapping egress point to mcast path |
| 1801 | * |
| 1802 | * @deprecated in 1.12 ("Magpie") release. |
| 1803 | */ |
| 1804 | @Deprecated |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1805 | public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) { |
| 1806 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
| 1807 | // Get the source |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1808 | ConnectPoint source = mcastUtils.getSource(mcastIp); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1809 | // Source cannot be null, we don't know the starting point |
| 1810 | if (source != null) { |
| 1811 | // Init steps |
| 1812 | Set<DeviceId> visited = Sets.newHashSet(); |
| 1813 | List<ConnectPoint> currentPath = Lists.newArrayList( |
| 1814 | source |
| 1815 | ); |
| 1816 | // Build recursively the mcast paths |
| 1817 | buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp); |
| 1818 | } |
| 1819 | return mcastPaths; |
| 1820 | } |
| 1821 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame^] | 1822 | /** |
| 1823 | * Returns the associated trees to the mcast group. |
| 1824 | * |
| 1825 | * @param mcastIp the group ip |
| 1826 | * @param sourcecp the source connect point |
| 1827 | * @return the mapping egress point to mcast path |
| 1828 | */ |
| 1829 | public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp, |
| 1830 | ConnectPoint sourcecp) { |
| 1831 | Multimap<ConnectPoint, List<ConnectPoint>> mcastTrees = HashMultimap.create(); |
| 1832 | // Get the sources |
| 1833 | Set<ConnectPoint> sources = mcastUtils.getSources(mcastIp); |
| 1834 | |
| 1835 | // If we are providing the source, let's filter out |
| 1836 | if (sourcecp != null) { |
| 1837 | sources = sources.stream() |
| 1838 | .filter(source -> source.equals(sourcecp)) |
| 1839 | .collect(Collectors.toSet()); |
| 1840 | } |
| 1841 | |
| 1842 | // Source cannot be null, we don't know the starting point |
| 1843 | if (!sources.isEmpty()) { |
| 1844 | sources.forEach(source -> { |
| 1845 | // Init steps |
| 1846 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
| 1847 | Set<DeviceId> visited = Sets.newHashSet(); |
| 1848 | List<ConnectPoint> currentPath = Lists.newArrayList(source); |
| 1849 | // Build recursively the mcast paths |
| 1850 | buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp); |
| 1851 | mcastPaths.forEach(mcastTrees::put); |
| 1852 | }); |
| 1853 | } |
| 1854 | return mcastTrees; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * Build recursively the mcast paths. |
| 1859 | * |
| 1860 | * @param toVisit the node to visit |
| 1861 | * @param visited the visited nodes |
| 1862 | * @param mcastPaths the current mcast paths |
| 1863 | * @param currentPath the current path |
| 1864 | * @param mcastIp the group ip |
| 1865 | */ |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1866 | private void buildMcastPaths(DeviceId toVisit, Set<DeviceId> visited, |
| 1867 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths, |
| 1868 | List<ConnectPoint> currentPath, IpAddress mcastIp) { |
| 1869 | // If we have visited the node to visit |
| 1870 | // there is a loop |
| 1871 | if (visited.contains(toVisit)) { |
| 1872 | return; |
| 1873 | } |
| 1874 | // Visit next-hop |
| 1875 | visited.add(toVisit); |
| 1876 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit); |
| 1877 | // Looking for next-hops |
| 1878 | if (mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1879 | // Build egress connectpoints |
| 1880 | NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1881 | // Get Ports |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1882 | Set<PortNumber> outputPorts = mcastUtils.getPorts(nextObjective.next()); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1883 | // Build relative cps |
| 1884 | ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder(); |
| 1885 | outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber))); |
| 1886 | Set<ConnectPoint> egressPoints = cpBuilder.build(); |
| 1887 | // Define other variables for the next steps |
| 1888 | Set<Link> egressLinks; |
| 1889 | List<ConnectPoint> newCurrentPath; |
| 1890 | Set<DeviceId> newVisited; |
| 1891 | DeviceId newToVisit; |
| 1892 | for (ConnectPoint egressPoint : egressPoints) { |
| 1893 | egressLinks = srManager.linkService.getEgressLinks(egressPoint); |
| 1894 | // If it does not have egress links, stop |
| 1895 | if (egressLinks.isEmpty()) { |
| 1896 | // Add the connect points to the path |
| 1897 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1898 | newCurrentPath.add(0, egressPoint); |
| 1899 | // Save in the map |
| 1900 | mcastPaths.put(egressPoint, newCurrentPath); |
| 1901 | } else { |
| 1902 | newVisited = Sets.newHashSet(visited); |
| 1903 | // Iterate over the egress links for the next hops |
| 1904 | for (Link egressLink : egressLinks) { |
| 1905 | // Update to visit |
| 1906 | newToVisit = egressLink.dst().deviceId(); |
| 1907 | // Add the connect points to the path |
| 1908 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1909 | newCurrentPath.add(0, egressPoint); |
| 1910 | newCurrentPath.add(0, egressLink.dst()); |
| 1911 | // Go to the next hop |
| 1912 | buildMcastPaths(newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp); |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1919 | /** |
| 1920 | * Return the leaders of the mcast groups. |
| 1921 | * |
| 1922 | * @param mcastIp the group ip |
| 1923 | * @return the mapping group-node |
| 1924 | */ |
| 1925 | public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) { |
| 1926 | return mcastUtils.getMcastLeaders(mcastIp); |
| 1927 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1928 | } |