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 | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 20 | import com.google.common.cache.Cache; |
| 21 | import com.google.common.cache.CacheBuilder; |
| 22 | import com.google.common.cache.RemovalCause; |
| 23 | import com.google.common.cache.RemovalNotification; |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 24 | import com.google.common.collect.HashMultimap; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableSet; |
| 27 | import com.google.common.collect.Lists; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 28 | import com.google.common.collect.Maps; |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 29 | import com.google.common.collect.Multimap; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 30 | import com.google.common.collect.Sets; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
| 33 | import org.onlab.util.KryoNamespace; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 34 | import org.onosproject.cluster.NodeId; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 35 | import org.onosproject.core.ApplicationId; |
| 36 | import org.onosproject.core.CoreService; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 37 | import org.onosproject.mcast.api.McastEvent; |
| 38 | import org.onosproject.mcast.api.McastRoute; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 39 | import org.onosproject.mcast.api.McastRouteData; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 40 | import org.onosproject.mcast.api.McastRouteUpdate; |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 41 | import org.onosproject.net.HostId; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.Link; |
| 45 | import org.onosproject.net.Path; |
| 46 | import org.onosproject.net.PortNumber; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 49 | import org.onosproject.net.flowobjective.NextObjective; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.ObjectiveContext; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 51 | import org.onosproject.net.topology.LinkWeigher; |
Pier Luigi | d8a1516 | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 52 | import org.onosproject.net.topology.Topology; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyService; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 54 | import org.onosproject.segmentrouting.SRLinkWeigher; |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 55 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
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; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 62 | import java.time.Instant; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 63 | import java.util.Collection; |
| 64 | import java.util.Collections; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 65 | import java.util.Comparator; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 66 | import java.util.List; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 67 | import java.util.Map; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 68 | import java.util.Map.Entry; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 69 | import java.util.Optional; |
| 70 | import java.util.Set; |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 71 | import java.util.concurrent.ScheduledExecutorService; |
| 72 | import java.util.concurrent.TimeUnit; |
| 73 | import java.util.concurrent.locks.Lock; |
| 74 | import java.util.concurrent.locks.ReentrantLock; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 75 | import java.util.stream.Collectors; |
| 76 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 77 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 78 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 79 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 80 | import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_ADDED; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 81 | import static org.onosproject.mcast.api.McastEvent.Type.ROUTE_REMOVED; |
Andrea Campanella | ef30d7a | 2018-04-27 14:44:15 +0200 | [diff] [blame] | 82 | import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_ADDED; |
| 83 | import static org.onosproject.mcast.api.McastEvent.Type.SOURCES_REMOVED; |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 84 | import static org.onosproject.mcast.api.McastEvent.Type.SINKS_ADDED; |
| 85 | import static org.onosproject.mcast.api.McastEvent.Type.SINKS_REMOVED; |
| 86 | |
Pier | 979e61a | 2018-03-07 11:42:50 +0100 | [diff] [blame] | 87 | import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS; |
| 88 | import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS; |
| 89 | import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 92 | * Handles Multicast related events. |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 93 | */ |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 94 | public class McastHandler { |
| 95 | private static final Logger log = LoggerFactory.getLogger(McastHandler.class); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 96 | private final SegmentRoutingManager srManager; |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 97 | private final TopologyService topologyService; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 98 | private final McastUtils mcastUtils; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 99 | private final ConsistentMap<McastStoreKey, NextObjective> mcastNextObjStore; |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 100 | private final ConsistentMap<McastRoleStoreKey, McastRole> mcastRoleStore; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 101 | |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 102 | // Wait time for the cache |
| 103 | private static final int WAIT_TIME_MS = 1000; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 104 | |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 105 | //The mcastEventCache is implemented to avoid race condition by giving more time |
| 106 | // to the underlying subsystems to process previous calls. |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 107 | private Cache<McastCacheKey, McastEvent> mcastEventCache = CacheBuilder.newBuilder() |
| 108 | .expireAfterWrite(WAIT_TIME_MS, TimeUnit.MILLISECONDS) |
| 109 | .removalListener((RemovalNotification<McastCacheKey, McastEvent> notification) -> { |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 110 | IpAddress mcastIp = notification.getKey().mcastIp(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 111 | HostId sink = notification.getKey().sinkHost(); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 112 | McastEvent mcastEvent = notification.getValue(); |
| 113 | RemovalCause cause = notification.getCause(); |
| 114 | log.debug("mcastEventCache removal event. group={}, sink={}, mcastEvent={}, cause={}", |
| 115 | mcastIp, sink, mcastEvent, cause); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 116 | // If it expires or it has been replaced, we deque the event - no when evicted |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 117 | switch (notification.getCause()) { |
| 118 | case REPLACED: |
| 119 | case EXPIRED: |
| 120 | dequeueMcastEvent(mcastEvent); |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | }).build(); |
| 126 | |
| 127 | private void enqueueMcastEvent(McastEvent mcastEvent) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 128 | final McastRouteUpdate mcastRouteUpdate = mcastEvent.subject(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 129 | final McastRouteUpdate mcastRoutePrevUpdate = mcastEvent.prevSubject(); |
| 130 | final IpAddress group = mcastRoutePrevUpdate.route().group(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 131 | ImmutableSet.Builder<HostId> sinksBuilder = ImmutableSet.builder(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 132 | if (mcastEvent.type() == SOURCES_ADDED || |
| 133 | mcastEvent.type() == SOURCES_REMOVED) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 134 | // Current subject and prev just differ for the source connect points |
| 135 | sinksBuilder.addAll(mcastRouteUpdate.sinks().keySet()); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 136 | } else if (mcastEvent.type() == SINKS_ADDED) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 137 | mcastRouteUpdate.sinks().forEach(((hostId, connectPoints) -> { |
| 138 | // Get the previous locations and verify if there are changes |
| 139 | Set<ConnectPoint> prevConnectPoints = mcastRoutePrevUpdate.sinks().get(hostId); |
| 140 | Set<ConnectPoint> changes = Sets.difference(connectPoints, prevConnectPoints != null ? |
| 141 | prevConnectPoints : Collections.emptySet()); |
| 142 | if (!changes.isEmpty()) { |
| 143 | sinksBuilder.add(hostId); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 144 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 145 | })); |
| 146 | } else if (mcastEvent.type() == SINKS_REMOVED) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 147 | mcastRoutePrevUpdate.sinks().forEach(((hostId, connectPoints) -> { |
| 148 | // Get the current locations and verify if there are changes |
| 149 | Set<ConnectPoint> currentConnectPoints = mcastRouteUpdate.sinks().get(hostId); |
| 150 | Set<ConnectPoint> changes = Sets.difference(connectPoints, currentConnectPoints != null ? |
| 151 | currentConnectPoints : Collections.emptySet()); |
| 152 | if (!changes.isEmpty()) { |
| 153 | sinksBuilder.add(hostId); |
| 154 | } |
| 155 | })); |
| 156 | } else if (mcastEvent.type() == ROUTE_REMOVED) { |
| 157 | // Current subject is null, just take the previous host ids |
| 158 | sinksBuilder.addAll(mcastRoutePrevUpdate.sinks().keySet()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 159 | } |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 160 | sinksBuilder.build().forEach(sink -> { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 161 | McastCacheKey cacheKey = new McastCacheKey(group, sink); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 162 | mcastEventCache.put(cacheKey, mcastEvent); |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | private void dequeueMcastEvent(McastEvent mcastEvent) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 167 | final McastRouteUpdate mcastUpdate = mcastEvent.subject(); |
| 168 | final McastRouteUpdate mcastPrevUpdate = mcastEvent.prevSubject(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 169 | IpAddress mcastIp = mcastPrevUpdate.route().group(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 170 | Set<ConnectPoint> prevSinks = mcastPrevUpdate.sinks() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 171 | .values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); |
| 172 | Set<ConnectPoint> prevSources = mcastPrevUpdate.sources() |
| 173 | .values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); |
| 174 | Set<ConnectPoint> sources; |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 175 | switch (mcastEvent.type()) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 176 | case SOURCES_ADDED: |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 177 | sources = mcastUpdate.sources() |
| 178 | .values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); |
| 179 | Set<ConnectPoint> sourcesToBeAdded = Sets.difference(sources, prevSources); |
| 180 | processSourcesAddedInternal(sourcesToBeAdded, mcastIp, mcastUpdate.sinks()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 181 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 182 | case SOURCES_REMOVED: |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 183 | sources = mcastUpdate.sources() |
| 184 | .values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); |
| 185 | Set<ConnectPoint> sourcesToBeRemoved = Sets.difference(prevSources, sources); |
| 186 | processSourcesRemovedInternal(sourcesToBeRemoved, sources, mcastIp, mcastUpdate.sinks()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 187 | break; |
| 188 | case ROUTE_REMOVED: |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 189 | processRouteRemovedInternal(prevSources, mcastIp); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 190 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 191 | case SINKS_ADDED: |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 192 | processSinksAddedInternal(prevSources, mcastIp, mcastUpdate.sinks(), prevSinks); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 193 | break; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 194 | case SINKS_REMOVED: |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 195 | processSinksRemovedInternal(prevSources, mcastIp, mcastUpdate.sinks(), mcastPrevUpdate.sinks()); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 196 | break; |
| 197 | default: |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 202 | // Mcast lock to serialize local operations |
| 203 | private final Lock mcastLock = new ReentrantLock(); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 204 | private void mcastLock() { |
| 205 | mcastLock.lock(); |
| 206 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 207 | private void mcastUnlock() { |
| 208 | mcastLock.unlock(); |
| 209 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 210 | // Stability threshold for Mcast. Seconds |
| 211 | private static final long MCAST_STABLITY_THRESHOLD = 5; |
| 212 | // Last change done |
| 213 | private Instant lastMcastChange = Instant.now(); |
| 214 | |
| 215 | /** |
| 216 | * Determines if mcast in the network has been stable in the last |
| 217 | * MCAST_STABLITY_THRESHOLD seconds, by comparing the current time |
| 218 | * to the last mcast change timestamp. |
| 219 | * |
| 220 | * @return true if stable |
| 221 | */ |
| 222 | private boolean isMcastStable() { |
| 223 | long last = (long) (lastMcastChange.toEpochMilli() / 1000.0); |
| 224 | long now = (long) (Instant.now().toEpochMilli() / 1000.0); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 225 | log.trace("Mcast stable since {}s", now - last); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 226 | return (now - last) > MCAST_STABLITY_THRESHOLD; |
| 227 | } |
| 228 | |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 229 | // Verify interval for Mcast bucket corrector |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 230 | private static final long MCAST_VERIFY_INTERVAL = 30; |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 231 | // Executor for mcast bucket corrector and for cache |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 232 | private ScheduledExecutorService executorService |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 233 | = newScheduledThreadPool(1, groupedThreads("mcastWorker", "mcastWorker-%d", log)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 234 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 235 | /** |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 236 | * Constructs the McastEventHandler. |
| 237 | * |
| 238 | * @param srManager Segment Routing manager |
| 239 | */ |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 240 | public McastHandler(SegmentRoutingManager srManager) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 241 | ApplicationId coreAppId = srManager.coreService.getAppId(CoreService.CORE_APP_NAME); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 242 | this.srManager = srManager; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 243 | this.topologyService = srManager.topologyService; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 244 | KryoNamespace.Builder mcastKryo = new KryoNamespace.Builder() |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 245 | .register(KryoNamespaces.API) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 246 | .register(new McastStoreKeySerializer(), McastStoreKey.class); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 247 | mcastNextObjStore = srManager.storageService |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 248 | .<McastStoreKey, NextObjective>consistentMapBuilder() |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 249 | .withName("onos-mcast-nextobj-store") |
Charles Chan | 4922a17 | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 250 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-NextObj"))) |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 251 | .build(); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 252 | mcastKryo = new KryoNamespace.Builder() |
| 253 | .register(KryoNamespaces.API) |
| 254 | .register(new McastRoleStoreKeySerializer(), McastRoleStoreKey.class) |
| 255 | .register(McastRole.class); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 256 | mcastRoleStore = srManager.storageService |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 257 | .<McastRoleStoreKey, McastRole>consistentMapBuilder() |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 258 | .withName("onos-mcast-role-store") |
Charles Chan | 4922a17 | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 259 | .withSerializer(Serializer.using(mcastKryo.build("McastHandler-Role"))) |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 260 | .build(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 261 | mcastUtils = new McastUtils(srManager, coreAppId, log); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 262 | // Init the executor service, the buckets corrector and schedule the clean up |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 263 | executorService.scheduleWithFixedDelay(new McastBucketCorrector(), 10, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 264 | MCAST_VERIFY_INTERVAL, TimeUnit.SECONDS); |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 265 | executorService.scheduleAtFixedRate(mcastEventCache::cleanUp, 0, |
| 266 | WAIT_TIME_MS, TimeUnit.MILLISECONDS); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 270 | * Read initial multicast configuration from mcast store. |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 271 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 272 | public void init() { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 273 | lastMcastChange = Instant.now(); |
| 274 | mcastLock(); |
| 275 | try { |
| 276 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 277 | log.debug("Init group {}", mcastRoute.group()); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 278 | if (!mcastUtils.isLeader(mcastRoute.group())) { |
| 279 | log.debug("Skip {} due to lack of leadership", mcastRoute.group()); |
| 280 | return; |
| 281 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 282 | McastRouteData mcastRouteData = srManager.multicastRouteService.routeData(mcastRoute); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 283 | // For each source process the mcast tree |
| 284 | srManager.multicastRouteService.sources(mcastRoute).forEach(source -> { |
| 285 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
| 286 | Set<DeviceId> visited = Sets.newHashSet(); |
| 287 | List<ConnectPoint> currentPath = Lists.newArrayList(source); |
| 288 | buildMcastPaths(source.deviceId(), visited, mcastPaths, |
| 289 | currentPath, mcastRoute.group(), source); |
| 290 | // Get all the sinks and process them |
| 291 | Set<ConnectPoint> sinks = processSinksToBeAdded(source, mcastRoute.group(), |
| 292 | mcastRouteData.sinks()); |
| 293 | // Filter out all the working sinks, we do not want to move them |
| 294 | // TODO we need a better way to distinguish flows coming from different sources |
| 295 | sinks = sinks.stream() |
| 296 | .filter(sink -> !mcastPaths.containsKey(sink) || |
| 297 | !isSinkForSource(mcastRoute.group(), sink, source)) |
| 298 | .collect(Collectors.toSet()); |
| 299 | if (sinks.isEmpty()) { |
| 300 | log.debug("Skip {} for source {} nothing to do", mcastRoute.group(), source); |
| 301 | return; |
| 302 | } |
| 303 | Map<ConnectPoint, List<Path>> mcasTree = computeSinkMcastTree(source.deviceId(), sinks); |
| 304 | mcasTree.forEach((sink, paths) -> processSinkAddedInternal(source, sink, |
| 305 | mcastRoute.group(), paths)); |
| 306 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 307 | }); |
| 308 | } finally { |
| 309 | mcastUnlock(); |
| 310 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 314 | * Clean up when deactivating the application. |
| 315 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 316 | public void terminate() { |
Pier | 72d0e58 | 2018-04-20 14:14:34 +0200 | [diff] [blame] | 317 | mcastEventCache.invalidateAll(); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 318 | executorService.shutdown(); |
Pier | 72d0e58 | 2018-04-20 14:14:34 +0200 | [diff] [blame] | 319 | mcastNextObjStore.destroy(); |
| 320 | mcastRoleStore.destroy(); |
| 321 | mcastUtils.terminate(); |
| 322 | log.info("Terminated"); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /** |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 326 | * Processes the SOURCE_ADDED, SOURCE_UPDATED, SINK_ADDED, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 327 | * SINK_REMOVED, ROUTE_ADDED and ROUTE_REMOVED events. |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 328 | * |
| 329 | * @param event McastEvent with SOURCE_ADDED type |
| 330 | */ |
Pier Luigi | d29ca7c | 2018-02-28 17:24:03 +0100 | [diff] [blame] | 331 | public void processMcastEvent(McastEvent event) { |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 332 | log.info("process {}", event); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 333 | // If it is a route added, we do not enqueue |
| 334 | if (event.type() == ROUTE_ADDED) { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 335 | processRouteAddedInternal(event.subject().route().group()); |
| 336 | } else { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 337 | enqueueMcastEvent(event); |
| 338 | } |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 342 | * Process the SOURCES_ADDED event. |
| 343 | * |
| 344 | * @param sources the sources connect point |
| 345 | * @param mcastIp the group address |
| 346 | * @param sinks the sinks connect points |
| 347 | */ |
| 348 | private void processSourcesAddedInternal(Set<ConnectPoint> sources, IpAddress mcastIp, |
| 349 | Map<HostId, Set<ConnectPoint>> sinks) { |
| 350 | lastMcastChange = Instant.now(); |
| 351 | mcastLock(); |
| 352 | try { |
| 353 | log.debug("Processing sources added {} for group {}", sources, mcastIp); |
| 354 | if (!mcastUtils.isLeader(mcastIp)) { |
| 355 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 356 | return; |
| 357 | } |
| 358 | sources.forEach(source -> { |
| 359 | Set<ConnectPoint> sinksToBeAdded = processSinksToBeAdded(source, mcastIp, sinks); |
| 360 | Map<ConnectPoint, List<Path>> mcasTree = computeSinkMcastTree(source.deviceId(), sinksToBeAdded); |
| 361 | mcasTree.forEach((sink, paths) -> processSinkAddedInternal(source, sink, mcastIp, paths)); |
| 362 | }); |
| 363 | } finally { |
| 364 | mcastUnlock(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Process the SOURCES_REMOVED event. |
| 370 | * |
| 371 | * @param sourcesToBeRemoved the source connect points to be removed |
| 372 | * @param remainingSources the remainig source connect points |
| 373 | * @param mcastIp the group address |
| 374 | * @param sinks the sinks connect points |
| 375 | */ |
| 376 | private void processSourcesRemovedInternal(Set<ConnectPoint> sourcesToBeRemoved, |
| 377 | Set<ConnectPoint> remainingSources, |
| 378 | IpAddress mcastIp, |
| 379 | Map<HostId, Set<ConnectPoint>> sinks) { |
| 380 | lastMcastChange = Instant.now(); |
| 381 | mcastLock(); |
| 382 | try { |
| 383 | log.debug("Processing sources removed {} for group {}", sourcesToBeRemoved, mcastIp); |
| 384 | if (!mcastUtils.isLeader(mcastIp)) { |
| 385 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 386 | return; |
| 387 | } |
| 388 | if (remainingSources.isEmpty()) { |
| 389 | processRouteRemovedInternal(sourcesToBeRemoved, mcastIp); |
| 390 | return; |
| 391 | } |
| 392 | // Skip offline devices |
| 393 | Set<ConnectPoint> candidateSources = sourcesToBeRemoved.stream() |
| 394 | .filter(source -> srManager.deviceService.isAvailable(source.deviceId())) |
| 395 | .collect(Collectors.toSet()); |
| 396 | if (candidateSources.isEmpty()) { |
| 397 | log.debug("Skip {} due to empty sources to be removed", mcastIp); |
| 398 | return; |
| 399 | } |
| 400 | Set<Link> remainingLinks = Sets.newHashSet(); |
| 401 | Map<ConnectPoint, Set<Link>> candidateLinks = Maps.newHashMap(); |
| 402 | Map<ConnectPoint, Set<ConnectPoint>> candidateSinks = Maps.newHashMap(); |
| 403 | Set<ConnectPoint> totalSources = Sets.newHashSet(candidateSources); |
| 404 | totalSources.addAll(remainingSources); |
| 405 | // Calculate all the links used by the sources |
| 406 | totalSources.forEach(source -> { |
| 407 | Set<ConnectPoint> currentSinks = sinks.values() |
| 408 | .stream().flatMap(Collection::stream) |
| 409 | .filter(sink -> isSinkForSource(mcastIp, sink, source)) |
| 410 | .collect(Collectors.toSet()); |
| 411 | candidateSinks.put(source, currentSinks); |
| 412 | currentSinks.forEach(currentSink -> { |
| 413 | Optional<Path> currentPath = getPath(source.deviceId(), currentSink.deviceId(), |
| 414 | mcastIp, null, source); |
| 415 | if (currentPath.isPresent()) { |
| 416 | if (!candidateSources.contains(source)) { |
| 417 | remainingLinks.addAll(currentPath.get().links()); |
| 418 | } else { |
| 419 | candidateLinks.put(source, Sets.newHashSet(currentPath.get().links())); |
| 420 | } |
| 421 | } |
| 422 | }); |
| 423 | }); |
| 424 | // Clean transit links |
| 425 | candidateLinks.forEach((source, currentCandidateLinks) -> { |
| 426 | Set<Link> linksToBeRemoved = Sets.difference(currentCandidateLinks, remainingLinks) |
| 427 | .immutableCopy(); |
| 428 | if (!linksToBeRemoved.isEmpty()) { |
| 429 | currentCandidateLinks.forEach(link -> { |
| 430 | DeviceId srcLink = link.src().deviceId(); |
| 431 | // Remove ports only on links to be removed |
| 432 | if (linksToBeRemoved.contains(link)) { |
| 433 | removePortFromDevice(link.src().deviceId(), link.src().port(), mcastIp, |
| 434 | mcastUtils.assignedVlan(srcLink.equals(source.deviceId()) ? |
| 435 | source : null)); |
| 436 | } |
| 437 | // Remove role on the candidate links |
| 438 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, srcLink, source)); |
| 439 | }); |
| 440 | } |
| 441 | }); |
| 442 | // Clean ingress and egress |
| 443 | candidateSources.forEach(source -> { |
| 444 | Set<ConnectPoint> currentSinks = candidateSinks.get(source); |
| 445 | currentSinks.forEach(currentSink -> { |
| 446 | VlanId assignedVlan = mcastUtils.assignedVlan(source.deviceId().equals(currentSink.deviceId()) ? |
| 447 | source : null); |
| 448 | // Sinks co-located with the source |
| 449 | if (source.deviceId().equals(currentSink.deviceId())) { |
| 450 | if (source.port().equals(currentSink.port())) { |
| 451 | log.warn("Skip {} since sink {} is on the same port of source {}. Abort", |
| 452 | mcastIp, currentSink, source); |
| 453 | return; |
| 454 | } |
| 455 | // We need to check against the other sources and if it is |
| 456 | // necessary remove the port from the device - no overlap |
| 457 | Set<VlanId> otherVlans = remainingSources.stream() |
| 458 | // Only sources co-located and having this sink |
| 459 | .filter(remainingSource -> remainingSource.deviceId() |
| 460 | .equals(source.deviceId()) && candidateSinks.get(remainingSource) |
| 461 | .contains(currentSink)) |
| 462 | .map(remainingSource -> mcastUtils.assignedVlan( |
| 463 | remainingSource.deviceId().equals(currentSink.deviceId()) ? |
| 464 | remainingSource : null)).collect(Collectors.toSet()); |
| 465 | if (!otherVlans.contains(assignedVlan)) { |
| 466 | removePortFromDevice(currentSink.deviceId(), currentSink.port(), |
| 467 | mcastIp, assignedVlan); |
| 468 | } |
| 469 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, currentSink.deviceId(), |
| 470 | source)); |
| 471 | return; |
| 472 | } |
| 473 | Set<VlanId> otherVlans = remainingSources.stream() |
| 474 | .filter(remainingSource -> candidateSinks.get(remainingSource) |
| 475 | .contains(currentSink)) |
| 476 | .map(remainingSource -> mcastUtils.assignedVlan( |
| 477 | remainingSource.deviceId().equals(currentSink.deviceId()) ? |
| 478 | remainingSource : null)).collect(Collectors.toSet()); |
| 479 | // Sinks on other leaves |
| 480 | if (!otherVlans.contains(assignedVlan)) { |
| 481 | removePortFromDevice(currentSink.deviceId(), currentSink.port(), |
| 482 | mcastIp, assignedVlan); |
| 483 | } |
| 484 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, currentSink.deviceId(), |
| 485 | source)); |
| 486 | }); |
| 487 | }); |
| 488 | } finally { |
| 489 | mcastUnlock(); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 494 | * Process the ROUTE_ADDED event. |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 495 | * |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 496 | * @param mcastIp the group address |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 497 | */ |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 498 | private void processRouteAddedInternal(IpAddress mcastIp) { |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 499 | lastMcastChange = Instant.now(); |
| 500 | mcastLock(); |
| 501 | try { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 502 | log.debug("Processing route added for group {}", mcastIp); |
| 503 | // Just elect a new leader |
| 504 | mcastUtils.isLeader(mcastIp); |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 505 | } finally { |
| 506 | mcastUnlock(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 511 | * Removes the entire mcast tree related to this group. |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 512 | * @param sources the source connect points |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 513 | * @param mcastIp multicast group IP address |
| 514 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 515 | private void processRouteRemovedInternal(Set<ConnectPoint> sources, IpAddress mcastIp) { |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 516 | lastMcastChange = Instant.now(); |
| 517 | mcastLock(); |
| 518 | try { |
Pier Luigi | e80d6b4 | 2018-02-26 12:31:38 +0100 | [diff] [blame] | 519 | log.debug("Processing route removed for group {}", mcastIp); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 520 | if (!mcastUtils.isLeader(mcastIp)) { |
| 521 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 522 | mcastUtils.withdrawLeader(mcastIp); |
| 523 | return; |
| 524 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 525 | sources.forEach(source -> { |
| 526 | // Find out the ingress, transit and egress device of the affected group |
| 527 | DeviceId ingressDevice = getDevice(mcastIp, INGRESS, source) |
| 528 | .stream().findFirst().orElse(null); |
| 529 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT, source); |
| 530 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS, source); |
| 531 | // If there are no egress and transit devices, sinks could be only on the ingress |
| 532 | if (!egressDevices.isEmpty()) { |
| 533 | egressDevices.forEach(deviceId -> { |
| 534 | removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null)); |
| 535 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, deviceId, source)); |
| 536 | }); |
| 537 | } |
| 538 | if (!transitDevices.isEmpty()) { |
| 539 | transitDevices.forEach(deviceId -> { |
| 540 | removeGroupFromDevice(deviceId, mcastIp, mcastUtils.assignedVlan(null)); |
| 541 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, deviceId, source)); |
| 542 | }); |
| 543 | } |
| 544 | if (ingressDevice != null) { |
| 545 | removeGroupFromDevice(ingressDevice, mcastIp, mcastUtils.assignedVlan(source)); |
| 546 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, ingressDevice, source)); |
| 547 | } |
| 548 | }); |
| 549 | // Finally, withdraw the leadership |
| 550 | mcastUtils.withdrawLeader(mcastIp); |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 551 | } finally { |
| 552 | mcastUnlock(); |
| 553 | } |
| 554 | } |
| 555 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 556 | /** |
| 557 | * Process sinks to be removed. |
| 558 | * |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 559 | * @param sources the source connect points |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 560 | * @param mcastIp the ip address of the group |
| 561 | * @param newSinks the new sinks to be processed |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 562 | * @param prevSinks the previous sinks |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 563 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 564 | private void processSinksRemovedInternal(Set<ConnectPoint> sources, IpAddress mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 565 | Map<HostId, Set<ConnectPoint>> newSinks, |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 566 | Map<HostId, Set<ConnectPoint>> prevSinks) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 567 | lastMcastChange = Instant.now(); |
| 568 | mcastLock(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 569 | try { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 570 | if (!mcastUtils.isLeader(mcastIp)) { |
| 571 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 572 | return; |
| 573 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 574 | log.debug("Processing sinks removed for group {} and for sources {}", |
| 575 | mcastIp, sources); |
| 576 | Map<ConnectPoint, Map<ConnectPoint, Optional<Path>>> treesToBeRemoved = Maps.newHashMap(); |
| 577 | Map<ConnectPoint, Set<ConnectPoint>> treesToBeAdded = Maps.newHashMap(); |
| 578 | sources.forEach(source -> { |
| 579 | // Save the path associated to the sinks to be removed |
| 580 | Set<ConnectPoint> sinksToBeRemoved = processSinksToBeRemoved(mcastIp, prevSinks, |
| 581 | newSinks, source); |
| 582 | Map<ConnectPoint, Optional<Path>> treeToBeRemoved = Maps.newHashMap(); |
| 583 | sinksToBeRemoved.forEach(sink -> treeToBeRemoved.put(sink, getPath(source.deviceId(), |
| 584 | sink.deviceId(), mcastIp, |
| 585 | null, source))); |
| 586 | treesToBeRemoved.put(source, treeToBeRemoved); |
| 587 | // Recover the dual-homed sinks |
| 588 | Set<ConnectPoint> sinksToBeRecovered = processSinksToBeRecovered(mcastIp, newSinks, |
| 589 | prevSinks, source); |
| 590 | treesToBeAdded.put(source, sinksToBeRecovered); |
| 591 | }); |
| 592 | // Remove the sinks taking into account the multiple sources and the original paths |
| 593 | treesToBeRemoved.forEach((source, tree) -> |
| 594 | tree.forEach((sink, path) -> processSinkRemovedInternal(source, sink, mcastIp, path))); |
| 595 | // Add new sinks according to the recovery procedure |
| 596 | treesToBeAdded.forEach((source, sinks) -> |
| 597 | sinks.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, null))); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 598 | } finally { |
| 599 | mcastUnlock(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Pier Luigi | 6786b92 | 2018-02-02 16:19:11 +0100 | [diff] [blame] | 603 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 604 | * Removes a path from source to sink for given multicast group. |
| 605 | * |
| 606 | * @param source connect point of the multicast source |
| 607 | * @param sink connection point of the multicast sink |
| 608 | * @param mcastIp multicast group IP address |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 609 | * @param mcastPath path associated to the sink |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 610 | */ |
| 611 | private void processSinkRemovedInternal(ConnectPoint source, ConnectPoint sink, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 612 | IpAddress mcastIp, Optional<Path> mcastPath) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 613 | lastMcastChange = Instant.now(); |
| 614 | mcastLock(); |
| 615 | try { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 616 | log.debug("Processing sink removed {} for group {} and for source {}", sink, mcastIp, source); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 617 | boolean isLast; |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 618 | // When source and sink are on the same device |
| 619 | if (source.deviceId().equals(sink.deviceId())) { |
| 620 | // Source and sink are on even the same port. There must be something wrong. |
| 621 | if (source.port().equals(sink.port())) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 622 | log.warn("Skip {} since sink {} is on the same port of source {}. Abort", mcastIp, sink, source); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 623 | return; |
| 624 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 625 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source)); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 626 | if (isLast) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 627 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, sink.deviceId(), source)); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 628 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 629 | return; |
| 630 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 631 | // Process the egress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 632 | isLast = removePortFromDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 633 | if (isLast) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 634 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, sink.deviceId(), source)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 635 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 636 | // If this is the last sink on the device, also update upstream |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 637 | if (mcastPath.isPresent()) { |
| 638 | List<Link> links = Lists.newArrayList(mcastPath.get().links()); |
| 639 | Collections.reverse(links); |
| 640 | for (Link link : links) { |
| 641 | if (isLast) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 642 | isLast = removePortFromDevice(link.src().deviceId(), link.src().port(), mcastIp, |
| 643 | mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null)); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 644 | if (isLast) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 645 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, link.src().deviceId(), source)); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 646 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 647 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 650 | } finally { |
| 651 | mcastUnlock(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * Process sinks to be added. |
| 658 | * |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 659 | * @param sources the source connect points |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 660 | * @param mcastIp the group IP |
| 661 | * @param newSinks the new sinks to be processed |
| 662 | * @param allPrevSinks all previous sinks |
| 663 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 664 | private void processSinksAddedInternal(Set<ConnectPoint> sources, IpAddress mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 665 | Map<HostId, Set<ConnectPoint>> newSinks, |
| 666 | Set<ConnectPoint> allPrevSinks) { |
| 667 | lastMcastChange = Instant.now(); |
| 668 | mcastLock(); |
| 669 | try { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 670 | if (!mcastUtils.isLeader(mcastIp)) { |
| 671 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 672 | return; |
| 673 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 674 | log.debug("Processing sinks added for group {} and for sources {}", mcastIp, sources); |
| 675 | sources.forEach(source -> { |
| 676 | Set<ConnectPoint> sinksToBeAdded = processSinksToBeAdded(source, mcastIp, newSinks); |
| 677 | sinksToBeAdded = Sets.difference(sinksToBeAdded, allPrevSinks); |
| 678 | sinksToBeAdded.forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, null)); |
| 679 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 680 | } finally { |
| 681 | mcastUnlock(); |
| 682 | } |
| 683 | } |
| 684 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 685 | /** |
| 686 | * Establishes a path from source to sink for given multicast group. |
| 687 | * |
| 688 | * @param source connect point of the multicast source |
| 689 | * @param sink connection point of the multicast sink |
| 690 | * @param mcastIp multicast group IP address |
| 691 | */ |
| 692 | private void processSinkAddedInternal(ConnectPoint source, ConnectPoint sink, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 693 | IpAddress mcastIp, List<Path> allPaths) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 694 | lastMcastChange = Instant.now(); |
| 695 | mcastLock(); |
| 696 | try { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 697 | log.debug("Processing sink added {} for group {} and for source {}", sink, mcastIp, source); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 698 | // Process the ingress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 699 | mcastUtils.addFilterToDevice(source.deviceId(), source.port(), |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 700 | mcastUtils.assignedVlan(source), mcastIp, INGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 701 | if (source.deviceId().equals(sink.deviceId())) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 702 | if (source.port().equals(sink.port())) { |
| 703 | log.warn("Skip {} since sink {} is on the same port of source {}. Abort", |
| 704 | mcastIp, sink, source); |
| 705 | return; |
| 706 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 707 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(source)); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 708 | mcastRoleStore.put(new McastRoleStoreKey(mcastIp, sink.deviceId(), source), INGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 709 | return; |
| 710 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 711 | // Find a path. If present, create/update groups and flows for each hop |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 712 | Optional<Path> mcastPath = getPath(source.deviceId(), sink.deviceId(), mcastIp, allPaths, source); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 713 | if (mcastPath.isPresent()) { |
| 714 | List<Link> links = mcastPath.get().links(); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 715 | // Setup mcast role for ingress |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 716 | mcastRoleStore.put(new McastRoleStoreKey(mcastIp, source.deviceId(), source), INGRESS); |
| 717 | // Setup properly the transit forwarding |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 718 | links.forEach(link -> { |
| 719 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 720 | mcastUtils.assignedVlan(link.src().deviceId() |
| 721 | .equals(source.deviceId()) ? source : null)); |
| 722 | mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(), |
| 723 | mcastUtils.assignedVlan(null), mcastIp, null); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 724 | }); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 725 | // Setup mcast role for the transit |
| 726 | links.stream() |
| 727 | .filter(link -> !link.dst().deviceId().equals(sink.deviceId())) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 728 | .forEach(link -> mcastRoleStore.put(new McastRoleStoreKey(mcastIp, link.dst().deviceId(), |
| 729 | source), TRANSIT)); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 730 | // Process the egress device |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 731 | addPortToDevice(sink.deviceId(), sink.port(), mcastIp, mcastUtils.assignedVlan(null)); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 732 | // Setup mcast role for egress |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 733 | mcastRoleStore.put(new McastRoleStoreKey(mcastIp, sink.deviceId(), source), EGRESS); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 734 | } else { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 735 | log.warn("Unable to find a path from {} to {}. Abort sinkAdded", source.deviceId(), sink.deviceId()); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 736 | } |
| 737 | } finally { |
| 738 | mcastUnlock(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
| 742 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 743 | * Processes the LINK_DOWN event. |
| 744 | * |
| 745 | * @param affectedLink Link that is going down |
| 746 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 747 | public void processLinkDown(Link affectedLink) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 748 | lastMcastChange = Instant.now(); |
| 749 | mcastLock(); |
| 750 | try { |
| 751 | // Get groups affected by the link down event |
| 752 | getAffectedGroups(affectedLink).forEach(mcastIp -> { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 753 | log.debug("Processing link down {} for group {}", affectedLink, mcastIp); |
| 754 | recoverFailure(mcastIp, affectedLink); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 755 | }); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 756 | } finally { |
| 757 | mcastUnlock(); |
| 758 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 762 | * Process the DEVICE_DOWN event. |
| 763 | * |
| 764 | * @param deviceDown device going down |
| 765 | */ |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 766 | public void processDeviceDown(DeviceId deviceDown) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 767 | lastMcastChange = Instant.now(); |
| 768 | mcastLock(); |
| 769 | try { |
| 770 | // Get the mcast groups affected by the device going down |
| 771 | getAffectedGroups(deviceDown).forEach(mcastIp -> { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 772 | log.debug("Processing device down {} for group {}", deviceDown, mcastIp); |
| 773 | recoverFailure(mcastIp, deviceDown); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 774 | }); |
| 775 | } finally { |
| 776 | mcastUnlock(); |
| 777 | } |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 781 | * General failure recovery procedure. |
| 782 | * |
| 783 | * @param mcastIp the group to recover |
| 784 | * @param failedElement the failed element |
| 785 | */ |
| 786 | private void recoverFailure(IpAddress mcastIp, Object failedElement) { |
| 787 | // TODO Optimize when the group editing is in place |
| 788 | if (!mcastUtils.isLeader(mcastIp)) { |
| 789 | log.debug("Skip {} due to lack of leadership", mcastIp); |
| 790 | return; |
| 791 | } |
| 792 | // Do not proceed if the sources of this group are missing |
| 793 | Set<ConnectPoint> sources = getSources(mcastIp); |
| 794 | if (sources.isEmpty()) { |
| 795 | log.warn("Missing sources for group {}", mcastIp); |
| 796 | return; |
| 797 | } |
| 798 | // Find out the ingress devices of the affected group |
| 799 | // If sinks are in other leafs, we have ingress, transit, egress, and source |
| 800 | // If sinks are in the same leaf, we have just ingress and source |
| 801 | Set<DeviceId> ingressDevices = getDevice(mcastIp, INGRESS); |
| 802 | if (ingressDevices.isEmpty()) { |
| 803 | log.warn("Missing ingress devices for group {}", ingressDevices, mcastIp); |
| 804 | return; |
| 805 | } |
| 806 | // For each tree, delete ingress-transit part |
| 807 | sources.forEach(source -> { |
| 808 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT, source); |
| 809 | transitDevices.forEach(transitDevice -> { |
| 810 | removeGroupFromDevice(transitDevice, mcastIp, mcastUtils.assignedVlan(null)); |
| 811 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, transitDevice, source)); |
| 812 | }); |
| 813 | }); |
| 814 | removeIngressTransitPorts(mcastIp, ingressDevices, sources); |
| 815 | // TODO Evaluate the possibility of building optimize trees between sources |
| 816 | Map<DeviceId, Set<ConnectPoint>> notRecovered = Maps.newHashMap(); |
| 817 | sources.forEach(source -> { |
| 818 | Set<DeviceId> notRecoveredInternal = Sets.newHashSet(); |
| 819 | DeviceId ingressDevice = ingressDevices.stream() |
| 820 | .filter(deviceId -> deviceId.equals(source.deviceId())).findFirst().orElse(null); |
| 821 | // Clean also the ingress |
| 822 | if (failedElement instanceof DeviceId && ingressDevice.equals(failedElement)) { |
| 823 | removeGroupFromDevice((DeviceId) failedElement, mcastIp, mcastUtils.assignedVlan(source)); |
| 824 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, (DeviceId) failedElement, source)); |
| 825 | } |
| 826 | if (ingressDevice == null) { |
| 827 | log.warn("Skip failure recovery - " + |
| 828 | "Missing ingress for source {} and group {}", source, mcastIp); |
| 829 | return; |
| 830 | } |
| 831 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS, source); |
| 832 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, egressDevices); |
| 833 | // We have to verify, if there are egresses without paths |
| 834 | mcastTree.forEach((egressDevice, paths) -> { |
| 835 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, |
| 836 | mcastIp, paths, source); |
| 837 | // No paths, we have to try with alternative location |
| 838 | if (!mcastPath.isPresent()) { |
| 839 | notRecovered.compute(egressDevice, (deviceId, listSources) -> { |
| 840 | listSources = listSources == null ? Sets.newHashSet() : listSources; |
| 841 | listSources.add(source); |
| 842 | return listSources; |
| 843 | }); |
| 844 | notRecoveredInternal.add(egressDevice); |
| 845 | } |
| 846 | }); |
| 847 | // Fast path, we can recover all the locations |
| 848 | if (notRecoveredInternal.isEmpty()) { |
| 849 | mcastTree.forEach((egressDevice, paths) -> { |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 850 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, |
| 851 | mcastIp, paths, source); |
| 852 | if (mcastPath.isPresent()) { |
| 853 | installPath(mcastIp, source, mcastPath.get()); |
| 854 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 855 | }); |
| 856 | } else { |
| 857 | // Let's try to recover using alternative locations |
| 858 | recoverSinks(egressDevices, notRecoveredInternal, mcastIp, |
| 859 | ingressDevice, source); |
| 860 | } |
| 861 | }); |
| 862 | // Finally remove the egresses not recovered |
| 863 | notRecovered.forEach((egressDevice, listSources) -> { |
| 864 | Set<ConnectPoint> currentSources = getSources(mcastIp, egressDevice, EGRESS); |
| 865 | if (Objects.equal(currentSources, listSources)) { |
| 866 | log.warn("Fail to recover egress device {} from {} failure {}", |
| 867 | egressDevice, failedElement instanceof Link ? "Link" : "Device", failedElement); |
| 868 | removeGroupFromDevice(egressDevice, mcastIp, mcastUtils.assignedVlan(null)); |
| 869 | } |
| 870 | listSources.forEach(source -> mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, egressDevice, source))); |
| 871 | }); |
| 872 | } |
| 873 | |
| 874 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 875 | * Try to recover sinks using alternate locations. |
| 876 | * |
| 877 | * @param egressDevices the original egress devices |
| 878 | * @param notRecovered the devices not recovered |
| 879 | * @param mcastIp the group address |
| 880 | * @param ingressDevice the ingress device |
| 881 | * @param source the source connect point |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 882 | */ |
| 883 | private void recoverSinks(Set<DeviceId> egressDevices, Set<DeviceId> notRecovered, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 884 | IpAddress mcastIp, DeviceId ingressDevice, ConnectPoint source) { |
| 885 | log.debug("Processing recover sinks for group {} and for source {}", |
| 886 | mcastIp, source); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 887 | Set<DeviceId> recovered = Sets.difference(egressDevices, notRecovered); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 888 | Set<ConnectPoint> totalAffectedSinks = Sets.newHashSet(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 889 | Set<ConnectPoint> totalSinks = Sets.newHashSet(); |
| 890 | // Let's compute all the affected sinks and all the sinks |
| 891 | notRecovered.forEach(deviceId -> { |
| 892 | totalAffectedSinks.addAll( |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 893 | mcastUtils.getAffectedSinks(deviceId, mcastIp).values().stream() |
| 894 | .flatMap(Collection::stream) |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 895 | .filter(connectPoint -> connectPoint.deviceId().equals(deviceId)) |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 896 | .collect(Collectors.toSet()) |
| 897 | ); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 898 | totalSinks.addAll( |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 899 | mcastUtils.getAffectedSinks(deviceId, mcastIp).values().stream() |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 900 | .flatMap(Collection::stream).collect(Collectors.toSet()) |
| 901 | ); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 902 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 903 | Set<ConnectPoint> sinksToBeAdded = Sets.difference(totalSinks, totalAffectedSinks); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 904 | Set<DeviceId> newEgressDevices = sinksToBeAdded.stream() |
| 905 | .map(ConnectPoint::deviceId).collect(Collectors.toSet()); |
| 906 | newEgressDevices.addAll(recovered); |
| 907 | Set<DeviceId> copyNewEgressDevices = ImmutableSet.copyOf(newEgressDevices); |
| 908 | newEgressDevices = newEgressDevices.stream() |
| 909 | .filter(deviceId -> !deviceId.equals(ingressDevice)).collect(Collectors.toSet()); |
| 910 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(ingressDevice, newEgressDevices); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 911 | // if the source was originally in the new locations, add new sinks |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 912 | if (copyNewEgressDevices.contains(ingressDevice)) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 913 | sinksToBeAdded.stream() |
| 914 | .filter(connectPoint -> connectPoint.deviceId().equals(ingressDevice)) |
| 915 | .forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, ImmutableList.of())); |
| 916 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 917 | // Construct a new path for each egress device |
| 918 | mcastTree.forEach((egressDevice, paths) -> { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 919 | Optional<Path> mcastPath = getPath(ingressDevice, egressDevice, mcastIp, paths, source); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 920 | if (mcastPath.isPresent()) { |
| 921 | // Using recovery procedure |
| 922 | if (recovered.contains(egressDevice)) { |
| 923 | installPath(mcastIp, source, mcastPath.get()); |
| 924 | } else { |
| 925 | // otherwise we need to threat as new sink |
| 926 | sinksToBeAdded.stream() |
| 927 | .filter(connectPoint -> connectPoint.deviceId().equals(egressDevice)) |
| 928 | .forEach(sink -> processSinkAddedInternal(source, sink, mcastIp, paths)); |
| 929 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 930 | } |
| 931 | }); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /** |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 935 | * Process all the sinks related to a mcast group and return |
| 936 | * the ones to be removed. |
| 937 | * |
| 938 | * @param mcastIp the group address |
| 939 | * @param prevsinks the previous sinks to be evaluated |
| 940 | * @param newSinks the new sinks to be evaluted |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 941 | * @param source the source connect point |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 942 | * @return the set of the sinks to be removed |
| 943 | */ |
| 944 | private Set<ConnectPoint> processSinksToBeRemoved(IpAddress mcastIp, |
| 945 | Map<HostId, Set<ConnectPoint>> prevsinks, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 946 | Map<HostId, Set<ConnectPoint>> newSinks, |
| 947 | ConnectPoint source) { |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 948 | final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet(); |
| 949 | prevsinks.forEach(((hostId, connectPoints) -> { |
| 950 | // We have to check with the existing flows |
| 951 | ConnectPoint sinkToBeProcessed = connectPoints.stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 952 | .filter(connectPoint -> isSinkForSource(mcastIp, connectPoint, source)) |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 953 | .findFirst().orElse(null); |
| 954 | if (sinkToBeProcessed != null) { |
| 955 | // If the host has been removed or location has been removed |
| 956 | if (!newSinks.containsKey(hostId) || |
| 957 | !newSinks.get(hostId).contains(sinkToBeProcessed)) { |
| 958 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 959 | } |
| 960 | } |
| 961 | })); |
| 962 | // We have done, return the set |
| 963 | return sinksToBeProcessed; |
| 964 | } |
| 965 | |
| 966 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 967 | * Process new locations and return the set of sinks to be added |
| 968 | * in the context of the recovery. |
| 969 | * |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 970 | * @param newSinks the remaining sinks |
| 971 | * @param prevSinks the previous sinks |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 972 | * @param source the source connect point |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 973 | * @return the set of the sinks to be processed |
| 974 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 975 | private Set<ConnectPoint> processSinksToBeRecovered(IpAddress mcastIp, |
| 976 | Map<HostId, Set<ConnectPoint>> newSinks, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 977 | Map<HostId, Set<ConnectPoint>> prevSinks, |
| 978 | ConnectPoint source) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 979 | final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet(); |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 980 | newSinks.forEach((hostId, connectPoints) -> { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 981 | // If it has more than 1 locations |
| 982 | if (connectPoints.size() > 1 || connectPoints.size() == 0) { |
| 983 | log.debug("Skip {} since sink {} has {} locations", |
| 984 | mcastIp, hostId, connectPoints.size()); |
| 985 | return; |
| 986 | } |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 987 | // If previously it had two locations, we need to recover it |
| 988 | // Filter out if the remaining location is already served |
| 989 | if (prevSinks.containsKey(hostId) && prevSinks.get(hostId).size() == 2) { |
Pier | 665b0fc | 2018-04-19 15:53:20 +0200 | [diff] [blame] | 990 | ConnectPoint sinkToBeProcessed = connectPoints.stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 991 | .filter(connectPoint -> !isSinkForSource(mcastIp, connectPoint, source)) |
Pier | 665b0fc | 2018-04-19 15:53:20 +0200 | [diff] [blame] | 992 | .findFirst().orElse(null); |
| 993 | if (sinkToBeProcessed != null) { |
| 994 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 995 | } |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 996 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 997 | }); |
| 998 | return sinksToBeProcessed; |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Process all the sinks related to a mcast group and return |
| 1003 | * the ones to be processed. |
| 1004 | * |
| 1005 | * @param source the source connect point |
| 1006 | * @param mcastIp the group address |
| 1007 | * @param sinks the sinks to be evaluated |
| 1008 | * @return the set of the sinks to be processed |
| 1009 | */ |
| 1010 | private Set<ConnectPoint> processSinksToBeAdded(ConnectPoint source, IpAddress mcastIp, |
| 1011 | Map<HostId, Set<ConnectPoint>> sinks) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1012 | final Set<ConnectPoint> sinksToBeProcessed = Sets.newHashSet(); |
| 1013 | sinks.forEach(((hostId, connectPoints) -> { |
| 1014 | // If it has more than 2 locations |
| 1015 | if (connectPoints.size() > 2 || connectPoints.size() == 0) { |
| 1016 | log.debug("Skip {} since sink {} has {} locations", |
| 1017 | mcastIp, hostId, connectPoints.size()); |
| 1018 | return; |
| 1019 | } |
| 1020 | // If it has one location, just use it |
| 1021 | if (connectPoints.size() == 1) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1022 | sinksToBeProcessed.add(connectPoints.stream().findFirst().orElse(null)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | // We prefer to reuse existing flows |
| 1026 | ConnectPoint sinkToBeProcessed = connectPoints.stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1027 | .filter(connectPoint -> { |
| 1028 | if (!isSinkForGroup(mcastIp, connectPoint, source)) { |
| 1029 | return false; |
| 1030 | } |
| 1031 | if (!isSinkReachable(mcastIp, connectPoint, source)) { |
| 1032 | return false; |
| 1033 | } |
| 1034 | ConnectPoint other = connectPoints.stream() |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1035 | .filter(remaining -> !remaining.equals(connectPoint)) |
| 1036 | .findFirst().orElse(null); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1037 | // We are already serving the sink |
| 1038 | return !isSinkForSource(mcastIp, other, source); |
| 1039 | }).findFirst().orElse(null); |
| 1040 | |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1041 | if (sinkToBeProcessed != null) { |
| 1042 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1043 | return; |
| 1044 | } |
| 1045 | // Otherwise we prefer to reuse existing egresses |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1046 | Set<DeviceId> egresses = getDevice(mcastIp, EGRESS, source); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1047 | sinkToBeProcessed = connectPoints.stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1048 | .filter(connectPoint -> { |
| 1049 | if (!egresses.contains(connectPoint.deviceId())) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | if (!isSinkReachable(mcastIp, connectPoint, source)) { |
| 1053 | return false; |
| 1054 | } |
| 1055 | ConnectPoint other = connectPoints.stream() |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1056 | .filter(remaining -> !remaining.equals(connectPoint)) |
| 1057 | .findFirst().orElse(null); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1058 | return !isSinkForSource(mcastIp, other, source); |
| 1059 | }).findFirst().orElse(null); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1060 | if (sinkToBeProcessed != null) { |
| 1061 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1062 | return; |
| 1063 | } |
| 1064 | // Otherwise we prefer a location co-located with the source (if it exists) |
| 1065 | sinkToBeProcessed = connectPoints.stream() |
| 1066 | .filter(connectPoint -> connectPoint.deviceId().equals(source.deviceId())) |
| 1067 | .findFirst().orElse(null); |
| 1068 | if (sinkToBeProcessed != null) { |
| 1069 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1070 | return; |
| 1071 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1072 | // Finally, we randomly pick a new location if it is reachable |
| 1073 | sinkToBeProcessed = connectPoints.stream() |
| 1074 | .filter(connectPoint -> { |
| 1075 | if (!isSinkReachable(mcastIp, connectPoint, source)) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | ConnectPoint other = connectPoints.stream() |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1079 | .filter(remaining -> !remaining.equals(connectPoint)) |
| 1080 | .findFirst().orElse(null); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1081 | return !isSinkForSource(mcastIp, other, source); |
| 1082 | }).findFirst().orElse(null); |
| 1083 | if (sinkToBeProcessed != null) { |
| 1084 | sinksToBeProcessed.add(sinkToBeProcessed); |
| 1085 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1086 | })); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1087 | return sinksToBeProcessed; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1091 | * Utility method to remove all the ingress transit ports. |
| 1092 | * |
| 1093 | * @param mcastIp the group ip |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1094 | * @param ingressDevices the ingress devices |
| 1095 | * @param sources the source connect points |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1096 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1097 | private void removeIngressTransitPorts(IpAddress mcastIp, Set<DeviceId> ingressDevices, |
| 1098 | Set<ConnectPoint> sources) { |
| 1099 | Map<ConnectPoint, Set<PortNumber>> ingressTransitPorts = Maps.newHashMap(); |
| 1100 | sources.forEach(source -> { |
| 1101 | DeviceId ingressDevice = ingressDevices.stream() |
| 1102 | .filter(deviceId -> deviceId.equals(source.deviceId())) |
| 1103 | .findFirst().orElse(null); |
| 1104 | if (ingressDevice == null) { |
| 1105 | log.warn("Skip removeIngressTransitPorts - " + |
| 1106 | "Missing ingress for source {} and group {}", |
| 1107 | source, mcastIp); |
| 1108 | return; |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1109 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1110 | ingressTransitPorts.put(source, ingressTransitPort(mcastIp, ingressDevice, source)); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1111 | }); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1112 | ingressTransitPorts.forEach((source, ports) -> ports.forEach(ingressTransitPort -> { |
| 1113 | DeviceId ingressDevice = ingressDevices.stream() |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1114 | .filter(deviceId -> deviceId.equals(source.deviceId())) |
| 1115 | .findFirst().orElse(null); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1116 | boolean isLast = removePortFromDevice(ingressDevice, ingressTransitPort, |
| 1117 | mcastIp, mcastUtils.assignedVlan(source)); |
| 1118 | if (isLast) { |
| 1119 | mcastRoleStore.remove(new McastRoleStoreKey(mcastIp, ingressDevice, source)); |
| 1120 | } |
| 1121 | })); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /** |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1125 | * Adds a port to given multicast group on given device. This involves the |
| 1126 | * update of L3 multicast group and multicast routing table entry. |
| 1127 | * |
| 1128 | * @param deviceId device ID |
| 1129 | * @param port port to be added |
| 1130 | * @param mcastIp multicast group |
| 1131 | * @param assignedVlan assigned VLAN ID |
| 1132 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1133 | private void addPortToDevice(DeviceId deviceId, PortNumber port, |
| 1134 | IpAddress mcastIp, VlanId assignedVlan) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1135 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId, assignedVlan); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1136 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1137 | NextObjective newNextObj; |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1138 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1139 | // First time someone request this mcast group via this device |
| 1140 | portBuilder.add(port); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1141 | // New nextObj |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1142 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
| 1143 | portBuilder.build(), null).add(); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1144 | // Store the new port |
| 1145 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1146 | } else { |
| 1147 | // This device already serves some subscribers of this mcast group |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1148 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1149 | // Stop if the port is already in the nextobj |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1150 | Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next()); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1151 | if (existingPorts.contains(port)) { |
| 1152 | log.info("NextObj for {}/{} already exists. Abort", deviceId, port); |
| 1153 | return; |
| 1154 | } |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1155 | // Let's add the port and reuse the previous one |
Yuta HIGUCHI | bef07b5 | 2018-02-09 18:05:23 -0800 | [diff] [blame] | 1156 | portBuilder.addAll(existingPorts).add(port); |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1157 | // Reuse previous nextObj |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1158 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1159 | portBuilder.build(), nextObj.id()).addToExisting(); |
| 1160 | // Store the final next objective and send only the difference to the driver |
| 1161 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
| 1162 | // Add just the new port |
| 1163 | portBuilder = ImmutableSet.builder(); |
| 1164 | portBuilder.add(port); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1165 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 4f0dd21 | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1166 | portBuilder.build(), nextObj.id()).addToExisting(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1167 | } |
| 1168 | // Create, store and apply the new nextObj and fwdObj |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1169 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1170 | (objective) -> log.debug("Successfully add {} on {}/{}, vlan {}", |
| 1171 | mcastIp, deviceId, port.toLong(), assignedVlan), |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1172 | (objective, error) -> log.warn("Failed to add {} on {}/{}, vlan {}: {}", |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1173 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1174 | ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, |
| 1175 | newNextObj.id()).add(context); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1176 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 1177 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Removes a port from given multicast group on given device. |
| 1182 | * This involves the update of L3 multicast group and multicast routing |
| 1183 | * table entry. |
| 1184 | * |
| 1185 | * @param deviceId device ID |
| 1186 | * @param port port to be added |
| 1187 | * @param mcastIp multicast group |
| 1188 | * @param assignedVlan assigned VLAN ID |
| 1189 | * @return true if this is the last sink on this device |
| 1190 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1191 | private boolean removePortFromDevice(DeviceId deviceId, PortNumber port, |
| 1192 | IpAddress mcastIp, VlanId assignedVlan) { |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1193 | McastStoreKey mcastStoreKey = |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1194 | new McastStoreKey(mcastIp, deviceId, assignedVlan); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1195 | // This device is not serving this multicast group |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1196 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1197 | return true; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1198 | } |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1199 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1200 | Set<PortNumber> existingPorts = mcastUtils.getPorts(nextObj.next()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1201 | // This port does not serve this multicast group |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1202 | if (!existingPorts.contains(port)) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1203 | if (!existingPorts.isEmpty()) { |
| 1204 | log.warn("{} is not serving {} on port {}. Abort.", deviceId, mcastIp, port); |
| 1205 | return false; |
| 1206 | } |
| 1207 | return true; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1208 | } |
| 1209 | // Copy and modify the ImmutableSet |
| 1210 | existingPorts = Sets.newHashSet(existingPorts); |
| 1211 | existingPorts.remove(port); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1212 | NextObjective newNextObj; |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1213 | ObjectiveContext context; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1214 | ForwardingObjective fwdObj; |
| 1215 | if (existingPorts.isEmpty()) { |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1216 | context = new DefaultObjectiveContext( |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1217 | (objective) -> log.debug("Successfully remove {} on {}/{}, vlan {}", |
| 1218 | mcastIp, deviceId, port.toLong(), assignedVlan), |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1219 | (objective, error) -> log.warn("Failed to remove {} on {}/{}, vlan {}: {}", |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1220 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1221 | fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1222 | mcastNextObjStore.remove(mcastStoreKey); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1223 | } else { |
| 1224 | // If this is not the last sink, update flows and groups |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1225 | context = new DefaultObjectiveContext( |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1226 | (objective) -> log.debug("Successfully update {} on {}/{}, vlan {}", |
| 1227 | mcastIp, deviceId, port.toLong(), assignedVlan), |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1228 | (objective, error) -> log.warn("Failed to update {} on {}/{}, vlan {}: {}", |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1229 | mcastIp, deviceId, port.toLong(), assignedVlan, error)); |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1230 | // Here we store the next objective with the remaining port |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1231 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1232 | existingPorts, nextObj.id()).removeFromExisting(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1233 | fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, newNextObj.id()).add(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1234 | mcastNextObjStore.put(mcastStoreKey, newNextObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1235 | } |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1236 | // Let's modify the next objective removing the bucket |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1237 | newNextObj = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
Pier Luigi | 8cd46de | 2018-01-19 10:24:53 +0100 | [diff] [blame] | 1238 | ImmutableSet.of(port), nextObj.id()).removeFromExisting(); |
| 1239 | srManager.flowObjectiveService.next(deviceId, newNextObj); |
| 1240 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1241 | return existingPorts.isEmpty(); |
| 1242 | } |
| 1243 | |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1244 | /** |
| 1245 | * Removes entire group on given device. |
| 1246 | * |
| 1247 | * @param deviceId device ID |
| 1248 | * @param mcastIp multicast group to be removed |
| 1249 | * @param assignedVlan assigned VLAN ID |
| 1250 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1251 | private void removeGroupFromDevice(DeviceId deviceId, IpAddress mcastIp, |
| 1252 | VlanId assignedVlan) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1253 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, deviceId, assignedVlan); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1254 | // This device is not serving this multicast group |
| 1255 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1256 | log.warn("{} is not serving {}. Abort.", deviceId, mcastIp); |
| 1257 | return; |
| 1258 | } |
| 1259 | NextObjective nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1260 | ObjectiveContext context = new DefaultObjectiveContext( |
| 1261 | (objective) -> log.debug("Successfully remove {} on {}, vlan {}", |
| 1262 | mcastIp, deviceId, assignedVlan), |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1263 | (objective, error) -> log.warn("Failed to remove {} on {}, vlan {}: {}", |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1264 | mcastIp, deviceId, assignedVlan, error)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1265 | ForwardingObjective fwdObj = mcastUtils.fwdObjBuilder(mcastIp, assignedVlan, nextObj.id()).remove(context); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1266 | srManager.flowObjectiveService.forward(deviceId, fwdObj); |
| 1267 | mcastNextObjStore.remove(mcastStoreKey); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1270 | private void installPath(IpAddress mcastIp, ConnectPoint source, Path mcastPath) { |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1271 | List<Link> links = mcastPath.links(); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1272 | // Setup new ingress mcast role |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1273 | mcastRoleStore.put(new McastRoleStoreKey(mcastIp, links.get(0).src().deviceId(), source), |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1274 | INGRESS); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1275 | // For each link, modify the next on the source device adding the src port |
| 1276 | // and a new filter objective on the destination port |
| 1277 | links.forEach(link -> { |
| 1278 | addPortToDevice(link.src().deviceId(), link.src().port(), mcastIp, |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1279 | mcastUtils.assignedVlan(link.src().deviceId().equals(source.deviceId()) ? source : null)); |
| 1280 | mcastUtils.addFilterToDevice(link.dst().deviceId(), link.dst().port(), |
| 1281 | mcastUtils.assignedVlan(null), mcastIp, null); |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1282 | }); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1283 | // Setup mcast role for the transit |
| 1284 | links.stream() |
| 1285 | .filter(link -> !link.src().deviceId().equals(source.deviceId())) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1286 | .forEach(link -> mcastRoleStore.put(new McastRoleStoreKey(mcastIp, link.src().deviceId(), source), |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1287 | TRANSIT)); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1290 | /** |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1291 | * Go through all the paths, looking for shared links to be used |
| 1292 | * in the final path computation. |
| 1293 | * |
| 1294 | * @param egresses egress devices |
| 1295 | * @param availablePaths all the available paths towards the egress |
| 1296 | * @return shared links between egress devices |
| 1297 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1298 | private Set<Link> exploreMcastTree(Set<DeviceId> egresses, |
| 1299 | Map<DeviceId, List<Path>> availablePaths) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1300 | int minLength = Integer.MAX_VALUE; |
| 1301 | int length; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1302 | List<Path> currentPaths; |
| 1303 | // Verify the source can still reach all the egresses |
| 1304 | for (DeviceId egress : egresses) { |
| 1305 | // From the source we cannot reach all the sinks |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1306 | // just continue and let's figure out after |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1307 | currentPaths = availablePaths.get(egress); |
| 1308 | if (currentPaths.isEmpty()) { |
| 1309 | continue; |
| 1310 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1311 | // Get the length of the first one available, update the min length |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1312 | length = currentPaths.get(0).links().size(); |
| 1313 | if (length < minLength) { |
| 1314 | minLength = length; |
| 1315 | } |
Pier Luigi | 51ee7c0 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1316 | } |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1317 | // If there are no paths |
| 1318 | if (minLength == Integer.MAX_VALUE) { |
| 1319 | return Collections.emptySet(); |
| 1320 | } |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1321 | int index = 0; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1322 | Set<Link> sharedLinks = Sets.newHashSet(); |
| 1323 | Set<Link> currentSharedLinks; |
| 1324 | Set<Link> currentLinks; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1325 | DeviceId egressToRemove = null; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1326 | // Let's find out the shared links |
| 1327 | while (index < minLength) { |
| 1328 | // Initialize the intersection with the paths related to the first egress |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1329 | currentPaths = availablePaths.get(egresses.stream().findFirst().orElse(null)); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1330 | currentSharedLinks = Sets.newHashSet(); |
| 1331 | // Iterate over the paths and take the "index" links |
| 1332 | for (Path path : currentPaths) { |
| 1333 | currentSharedLinks.add(path.links().get(index)); |
| 1334 | } |
| 1335 | // Iterate over the remaining egress |
| 1336 | for (DeviceId egress : egresses) { |
| 1337 | // Iterate over the paths and take the "index" links |
| 1338 | currentLinks = Sets.newHashSet(); |
| 1339 | for (Path path : availablePaths.get(egress)) { |
| 1340 | currentLinks.add(path.links().get(index)); |
| 1341 | } |
| 1342 | // Do intersection |
| 1343 | currentSharedLinks = Sets.intersection(currentSharedLinks, currentLinks); |
| 1344 | // If there are no shared paths exit and record the device to remove |
| 1345 | // we have to retry with a subset of sinks |
| 1346 | if (currentSharedLinks.isEmpty()) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1347 | egressToRemove = egress; |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1348 | index = minLength; |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | sharedLinks.addAll(currentSharedLinks); |
| 1353 | index++; |
| 1354 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1355 | // If the shared links is empty and there are egress let's retry another time with less sinks, |
| 1356 | // we can still build optimal subtrees |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1357 | if (sharedLinks.isEmpty() && egresses.size() > 1 && egressToRemove != null) { |
| 1358 | egresses.remove(egressToRemove); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1359 | sharedLinks = exploreMcastTree(egresses, availablePaths); |
| 1360 | } |
| 1361 | return sharedLinks; |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Build Mcast tree having as root the given source and as leaves the given egress points. |
| 1366 | * |
| 1367 | * @param source source of the tree |
| 1368 | * @param sinks leaves of the tree |
| 1369 | * @return the computed Mcast tree |
| 1370 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1371 | private Map<ConnectPoint, List<Path>> computeSinkMcastTree(DeviceId source, |
| 1372 | Set<ConnectPoint> sinks) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1373 | // Get the egress devices, remove source from the egress if present |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1374 | Set<DeviceId> egresses = sinks.stream().map(ConnectPoint::deviceId) |
| 1375 | .filter(deviceId -> !deviceId.equals(source)).collect(Collectors.toSet()); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1376 | Map<DeviceId, List<Path>> mcastTree = computeMcastTree(source, egresses); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1377 | final Map<ConnectPoint, List<Path>> finalTree = Maps.newHashMap(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1378 | // We need to put back the source if it was originally present |
| 1379 | sinks.forEach(sink -> { |
| 1380 | List<Path> sinkPaths = mcastTree.get(sink.deviceId()); |
| 1381 | finalTree.put(sink, sinkPaths != null ? sinkPaths : ImmutableList.of()); |
| 1382 | }); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1383 | return finalTree; |
| 1384 | } |
| 1385 | |
| 1386 | /** |
| 1387 | * Build Mcast tree having as root the given source and as leaves the given egress. |
| 1388 | * |
| 1389 | * @param source source of the tree |
| 1390 | * @param egresses leaves of the tree |
| 1391 | * @return the computed Mcast tree |
| 1392 | */ |
| 1393 | private Map<DeviceId, List<Path>> computeMcastTree(DeviceId source, |
| 1394 | Set<DeviceId> egresses) { |
| 1395 | // Pre-compute all the paths |
| 1396 | Map<DeviceId, List<Path>> availablePaths = Maps.newHashMap(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1397 | egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress, |
| 1398 | Collections.emptySet()))); |
| 1399 | // Explore the topology looking for shared links amongst the egresses |
| 1400 | Set<Link> linksToEnforce = exploreMcastTree(Sets.newHashSet(egresses), availablePaths); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1401 | // Build the final paths enforcing the shared links between egress devices |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1402 | availablePaths.clear(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1403 | egresses.forEach(egress -> availablePaths.put(egress, getPaths(source, egress, |
| 1404 | linksToEnforce))); |
| 1405 | return availablePaths; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * Gets path from src to dst computed using the custom link weigher. |
| 1410 | * |
| 1411 | * @param src source device ID |
| 1412 | * @param dst destination device ID |
| 1413 | * @return list of paths from src to dst |
| 1414 | */ |
| 1415 | private List<Path> getPaths(DeviceId src, DeviceId dst, Set<Link> linksToEnforce) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1416 | final Topology currentTopology = topologyService.currentTopology(); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1417 | final LinkWeigher linkWeigher = new SRLinkWeigher(srManager, src, linksToEnforce); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1418 | List<Path> allPaths = Lists.newArrayList(topologyService.getPaths(currentTopology, src, dst, linkWeigher)); |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1419 | log.debug("{} path(s) found from {} to {}", allPaths.size(), src, dst); |
| 1420 | return allPaths; |
Pier Luigi | 51ee7c0 | 2018-02-23 19:57:40 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1423 | /** |
| 1424 | * Gets a path from src to dst. |
| 1425 | * If a path was allocated before, returns the allocated path. |
| 1426 | * Otherwise, randomly pick one from available paths. |
| 1427 | * |
| 1428 | * @param src source device ID |
| 1429 | * @param dst destination device ID |
| 1430 | * @param mcastIp multicast group |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1431 | * @param allPaths paths list |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1432 | * @return an optional path from src to dst |
| 1433 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1434 | private Optional<Path> getPath(DeviceId src, DeviceId dst, IpAddress mcastIp, |
| 1435 | List<Path> allPaths, ConnectPoint source) { |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1436 | if (allPaths == null) { |
| 1437 | allPaths = getPaths(src, dst, Collections.emptySet()); |
| 1438 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1439 | if (allPaths.isEmpty()) { |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1440 | return Optional.empty(); |
| 1441 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1442 | // Create a map index of suitability-to-list of paths. For example |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1443 | // a path in the list associated to the index 1 shares only the |
| 1444 | // first hop and it is less suitable of a path belonging to the index |
| 1445 | // 2 that shares leaf-spine. |
| 1446 | Map<Integer, List<Path>> eligiblePaths = Maps.newHashMap(); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1447 | int nhop; |
| 1448 | McastStoreKey mcastStoreKey; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1449 | PortNumber srcPort; |
| 1450 | Set<PortNumber> existingPorts; |
| 1451 | NextObjective nextObj; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1452 | for (Path path : allPaths) { |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1453 | if (!src.equals(path.links().get(0).src().deviceId())) { |
| 1454 | continue; |
| 1455 | } |
| 1456 | nhop = 0; |
| 1457 | // Iterate over the links |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1458 | for (Link hop : path.links()) { |
| 1459 | VlanId assignedVlan = mcastUtils.assignedVlan(hop.src().deviceId().equals(src) ? |
| 1460 | source : null); |
| 1461 | mcastStoreKey = new McastStoreKey(mcastIp, hop.src().deviceId(), assignedVlan); |
| 1462 | // It does not exist in the store, go to the next link |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1463 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1464 | continue; |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1465 | } |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1466 | nextObj = mcastNextObjStore.get(mcastStoreKey).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1467 | existingPorts = mcastUtils.getPorts(nextObj.next()); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1468 | srcPort = hop.src().port(); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1469 | // the src port is not used as output, go to the next link |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1470 | if (!existingPorts.contains(srcPort)) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1471 | continue; |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1472 | } |
| 1473 | nhop++; |
| 1474 | } |
| 1475 | // n_hop defines the index |
| 1476 | if (nhop > 0) { |
| 1477 | eligiblePaths.compute(nhop, (index, paths) -> { |
| 1478 | paths = paths == null ? Lists.newArrayList() : paths; |
| 1479 | paths.add(path); |
| 1480 | return paths; |
| 1481 | }); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1484 | if (eligiblePaths.isEmpty()) { |
| 1485 | log.debug("No eligiblePath(s) found from {} to {}", src, dst); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1486 | Collections.shuffle(allPaths); |
| 1487 | return allPaths.stream().findFirst(); |
| 1488 | } |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1489 | // Let's take the best ones |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1490 | Integer bestIndex = eligiblePaths.keySet().stream() |
| 1491 | .sorted(Comparator.reverseOrder()).findFirst().orElse(null); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1492 | List<Path> bestPaths = eligiblePaths.get(bestIndex); |
| 1493 | log.debug("{} eligiblePath(s) found from {} to {}", |
| 1494 | bestPaths.size(), src, dst); |
Pier Luigi | 91573e1 | 2018-01-23 16:06:38 +0100 | [diff] [blame] | 1495 | Collections.shuffle(bestPaths); |
| 1496 | return bestPaths.stream().findFirst(); |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1500 | * Gets device(s) of given role and of given source in given multicast tree. |
| 1501 | * |
| 1502 | * @param mcastIp multicast IP |
| 1503 | * @param role multicast role |
| 1504 | * @param source source connect point |
| 1505 | * @return set of device ID or empty set if not found |
| 1506 | */ |
| 1507 | private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role, ConnectPoint source) { |
| 1508 | return mcastRoleStore.entrySet().stream() |
| 1509 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) && |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1510 | entry.getKey().source().equals(source) && |
| 1511 | entry.getValue().value() == role) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1512 | .map(Entry::getKey).map(McastRoleStoreKey::deviceId).collect(Collectors.toSet()); |
| 1513 | } |
| 1514 | |
| 1515 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1516 | * Gets device(s) of given role in given multicast group. |
| 1517 | * |
| 1518 | * @param mcastIp multicast IP |
| 1519 | * @param role multicast role |
| 1520 | * @return set of device ID or empty set if not found |
| 1521 | */ |
| 1522 | private Set<DeviceId> getDevice(IpAddress mcastIp, McastRole role) { |
| 1523 | return mcastRoleStore.entrySet().stream() |
| 1524 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) && |
| 1525 | entry.getValue().value() == role) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1526 | .map(Entry::getKey).map(McastRoleStoreKey::deviceId).collect(Collectors.toSet()); |
| 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * Gets source(s) of given role, given device in given multicast group. |
| 1531 | * |
| 1532 | * @param mcastIp multicast IP |
| 1533 | * @param deviceId device id |
| 1534 | * @param role multicast role |
| 1535 | * @return set of device ID or empty set if not found |
| 1536 | */ |
| 1537 | private Set<ConnectPoint> getSources(IpAddress mcastIp, DeviceId deviceId, McastRole role) { |
| 1538 | return mcastRoleStore.entrySet().stream() |
| 1539 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp) && |
| 1540 | entry.getKey().deviceId().equals(deviceId) && entry.getValue().value() == role) |
| 1541 | .map(Entry::getKey).map(McastRoleStoreKey::source).collect(Collectors.toSet()); |
| 1542 | } |
| 1543 | |
| 1544 | /** |
| 1545 | * Gets source(s) of given multicast group. |
| 1546 | * |
| 1547 | * @param mcastIp multicast IP |
| 1548 | * @return set of device ID or empty set if not found |
| 1549 | */ |
| 1550 | private Set<ConnectPoint> getSources(IpAddress mcastIp) { |
| 1551 | return mcastRoleStore.entrySet().stream() |
| 1552 | .filter(entry -> entry.getKey().mcastIp().equals(mcastIp)) |
| 1553 | .map(Entry::getKey).map(McastRoleStoreKey::source).collect(Collectors.toSet()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * Gets groups which is affected by the link down event. |
| 1558 | * |
| 1559 | * @param link link going down |
| 1560 | * @return a set of multicast IpAddress |
| 1561 | */ |
| 1562 | private Set<IpAddress> getAffectedGroups(Link link) { |
| 1563 | DeviceId deviceId = link.src().deviceId(); |
| 1564 | PortNumber port = link.src().port(); |
| 1565 | return mcastNextObjStore.entrySet().stream() |
| 1566 | .filter(entry -> entry.getKey().deviceId().equals(deviceId) && |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1567 | mcastUtils.getPorts(entry.getValue().value().next()).contains(port)) |
| 1568 | .map(Entry::getKey).map(McastStoreKey::mcastIp).collect(Collectors.toSet()); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /** |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1572 | * Gets groups which are affected by the device down event. |
| 1573 | * |
| 1574 | * @param deviceId device going down |
| 1575 | * @return a set of multicast IpAddress |
| 1576 | */ |
| 1577 | private Set<IpAddress> getAffectedGroups(DeviceId deviceId) { |
| 1578 | return mcastNextObjStore.entrySet().stream() |
| 1579 | .filter(entry -> entry.getKey().deviceId().equals(deviceId)) |
Pier | 1f87aca | 2018-03-14 16:47:32 -0700 | [diff] [blame] | 1580 | .map(Entry::getKey).map(McastStoreKey::mcastIp) |
Pier Luigi | 580fd8a | 2018-01-16 10:47:50 +0100 | [diff] [blame] | 1581 | .collect(Collectors.toSet()); |
| 1582 | } |
| 1583 | |
| 1584 | /** |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1585 | * Gets the spine-facing port on ingress device of given multicast group. |
| 1586 | * |
| 1587 | * @param mcastIp multicast IP |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1588 | * @param ingressDevice the ingress device |
| 1589 | * @param source the source connect point |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1590 | * @return spine-facing port on ingress device |
| 1591 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1592 | private Set<PortNumber> ingressTransitPort(IpAddress mcastIp, DeviceId ingressDevice, |
| 1593 | ConnectPoint source) { |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1594 | ImmutableSet.Builder<PortNumber> portBuilder = ImmutableSet.builder(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1595 | if (ingressDevice != null) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1596 | NextObjective nextObj = mcastNextObjStore.get(new McastStoreKey(mcastIp, ingressDevice, |
| 1597 | mcastUtils.assignedVlan(source))).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1598 | Set<PortNumber> ports = mcastUtils.getPorts(nextObj.next()); |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1599 | // Let's find out all the ingress-transit ports |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1600 | for (PortNumber port : ports) { |
| 1601 | // Spine-facing port should have no subnet and no xconnect |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1602 | if (srManager.deviceConfiguration() != null && |
| 1603 | srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() && |
Charles Chan | 82f1997 | 2016-05-17 13:13:55 -0700 | [diff] [blame] | 1604 | !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) { |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1605 | portBuilder.add(port); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | } |
Pier | 1a7e0c0 | 2018-03-12 15:00:54 -0700 | [diff] [blame] | 1609 | return portBuilder.build(); |
Charles Chan | 7277950 | 2016-04-23 17:36:10 -0700 | [diff] [blame] | 1610 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | /** |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1613 | * Verify if a given connect point is sink for this group. |
| 1614 | * |
| 1615 | * @param mcastIp group address |
| 1616 | * @param connectPoint connect point to be verified |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1617 | * @param source source connect point |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1618 | * @return true if the connect point is sink of the group |
| 1619 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1620 | private boolean isSinkForGroup(IpAddress mcastIp, ConnectPoint connectPoint, |
| 1621 | ConnectPoint source) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1622 | VlanId assignedVlan = mcastUtils.assignedVlan(connectPoint.deviceId().equals(source.deviceId()) ? |
| 1623 | source : null); |
| 1624 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, connectPoint.deviceId(), assignedVlan); |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1625 | if (!mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 1626 | return false; |
| 1627 | } |
Pier | 2816468 | 2018-04-17 15:50:43 +0200 | [diff] [blame] | 1628 | NextObjective mcastNext = mcastNextObjStore.get(mcastStoreKey).value(); |
| 1629 | return mcastUtils.getPorts(mcastNext.next()).contains(connectPoint.port()); |
| 1630 | } |
| 1631 | |
| 1632 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1633 | * Verify if a given connect point is sink for this group and for this source. |
| 1634 | * |
| 1635 | * @param mcastIp group address |
| 1636 | * @param connectPoint connect point to be verified |
| 1637 | * @param source source connect point |
| 1638 | * @return true if the connect point is sink of the group |
| 1639 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1640 | private boolean isSinkForSource(IpAddress mcastIp, ConnectPoint connectPoint, |
| 1641 | ConnectPoint source) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1642 | boolean isSink = isSinkForGroup(mcastIp, connectPoint, source); |
| 1643 | DeviceId device; |
| 1644 | if (connectPoint.deviceId().equals(source.deviceId())) { |
| 1645 | device = getDevice(mcastIp, INGRESS, source).stream() |
| 1646 | .filter(deviceId -> deviceId.equals(connectPoint.deviceId())) |
| 1647 | .findFirst().orElse(null); |
| 1648 | } else { |
| 1649 | device = getDevice(mcastIp, EGRESS, source).stream() |
| 1650 | .filter(deviceId -> deviceId.equals(connectPoint.deviceId())) |
| 1651 | .findFirst().orElse(null); |
| 1652 | } |
| 1653 | return isSink && device != null; |
| 1654 | } |
| 1655 | |
| 1656 | /** |
| 1657 | * Verify if a sink is reachable from this source. |
| 1658 | * |
| 1659 | * @param mcastIp group address |
| 1660 | * @param sink connect point to be verified |
| 1661 | * @param source source connect point |
| 1662 | * @return true if the connect point is reachable from the source |
| 1663 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1664 | private boolean isSinkReachable(IpAddress mcastIp, ConnectPoint sink, |
| 1665 | ConnectPoint source) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1666 | return sink.deviceId().equals(source.deviceId()) || |
| 1667 | getPath(source.deviceId(), sink.deviceId(), mcastIp, null, source).isPresent(); |
| 1668 | } |
| 1669 | |
| 1670 | /** |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1671 | * Updates filtering objective for given device and port. |
| 1672 | * It is called in general when the mcast config has been |
| 1673 | * changed. |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1674 | * |
| 1675 | * @param deviceId device ID |
| 1676 | * @param portNum ingress port number |
| 1677 | * @param vlanId assigned VLAN ID |
| 1678 | * @param install true to add, false to remove |
| 1679 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1680 | public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum, |
| 1681 | VlanId vlanId, boolean install) { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1682 | lastMcastChange = Instant.now(); |
| 1683 | mcastLock(); |
| 1684 | try { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1685 | // Iterates over the route and updates properly the filtering objective on the source device. |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1686 | srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> { |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1687 | log.debug("Update filter for {}", mcastRoute.group()); |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1688 | if (!mcastUtils.isLeader(mcastRoute.group())) { |
| 1689 | log.debug("Skip {} due to lack of leadership", mcastRoute.group()); |
| 1690 | return; |
| 1691 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1692 | // Get the sources and for each one update properly the filtering objectives |
| 1693 | Set<ConnectPoint> sources = srManager.multicastRouteService.sources(mcastRoute); |
| 1694 | sources.forEach(source -> { |
| 1695 | if (source.deviceId().equals(deviceId) && source.port().equals(portNum)) { |
| 1696 | if (install) { |
| 1697 | mcastUtils.addFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), INGRESS); |
| 1698 | } else { |
| 1699 | mcastUtils.removeFilterToDevice(deviceId, portNum, vlanId, mcastRoute.group(), null); |
| 1700 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1701 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1702 | }); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1703 | }); |
| 1704 | } finally { |
| 1705 | mcastUnlock(); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * Performs bucket verification operation for all mcast groups in the devices. |
| 1711 | * Firstly, it verifies that mcast is stable before trying verification operation. |
| 1712 | * Verification consists in creating new nexts with VERIFY operation. Actually, |
| 1713 | * the operation is totally delegated to the driver. |
| 1714 | */ |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1715 | private final class McastBucketCorrector implements Runnable { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1716 | |
| 1717 | @Override |
| 1718 | public void run() { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1719 | if (!isMcastStable()) { |
| 1720 | return; |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1721 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1722 | mcastLock(); |
| 1723 | try { |
| 1724 | // Iterates over the routes and verify the related next objectives |
| 1725 | srManager.multicastRouteService.getRoutes() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1726 | .stream().map(McastRoute::group) |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1727 | .forEach(mcastIp -> { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1728 | log.trace("Running mcast buckets corrector for mcast group: {}", mcastIp); |
| 1729 | // Verify leadership on the operation |
| 1730 | if (!mcastUtils.isLeader(mcastIp)) { |
| 1731 | log.trace("Skip {} due to lack of leadership", mcastIp); |
| 1732 | return; |
| 1733 | } |
| 1734 | // Get sources and sinks from Mcast Route Service and warn about errors |
| 1735 | Set<ConnectPoint> sources = mcastUtils.getSources(mcastIp); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1736 | Set<ConnectPoint> sinks = mcastUtils.getSinks(mcastIp).values().stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1737 | .flatMap(Collection::stream).collect(Collectors.toSet()); |
| 1738 | // Do not proceed if sources of this group are missing |
| 1739 | if (sources.isEmpty()) { |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1740 | if (!sinks.isEmpty()) { |
| 1741 | log.warn("Unable to run buckets corrector. " + |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1742 | "Missing source {} for group {}", sources, mcastIp); |
Pier Luigi | 92e69be | 2018-03-02 12:53:37 +0100 | [diff] [blame] | 1743 | } |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1744 | return; |
| 1745 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1746 | sources.forEach(source -> { |
| 1747 | // For each group we get current information in the store |
| 1748 | // and issue a check of the next objectives in place |
| 1749 | Set<DeviceId> ingressDevices = getDevice(mcastIp, INGRESS, source); |
| 1750 | Set<DeviceId> transitDevices = getDevice(mcastIp, TRANSIT, source); |
| 1751 | Set<DeviceId> egressDevices = getDevice(mcastIp, EGRESS, source); |
| 1752 | // Do not proceed if ingress devices are missing |
| 1753 | if (ingressDevices.isEmpty()) { |
| 1754 | if (!sinks.isEmpty()) { |
| 1755 | log.warn("Unable to run buckets corrector. " + |
| 1756 | "Missing ingress {} for source {} and for group {}", |
| 1757 | ingressDevices, source, mcastIp); |
| 1758 | } |
| 1759 | return; |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1760 | } |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1761 | // Create the set of the devices to be processed |
| 1762 | ImmutableSet.Builder<DeviceId> devicesBuilder = ImmutableSet.builder(); |
| 1763 | if (!ingressDevices.isEmpty()) { |
| 1764 | devicesBuilder.addAll(ingressDevices); |
| 1765 | } |
| 1766 | if (!transitDevices.isEmpty()) { |
| 1767 | devicesBuilder.addAll(transitDevices); |
| 1768 | } |
| 1769 | if (!egressDevices.isEmpty()) { |
| 1770 | devicesBuilder.addAll(egressDevices); |
| 1771 | } |
| 1772 | Set<DeviceId> devicesToProcess = devicesBuilder.build(); |
| 1773 | devicesToProcess.forEach(deviceId -> { |
| 1774 | VlanId assignedVlan = mcastUtils.assignedVlan(deviceId.equals(source.deviceId()) ? |
| 1775 | source : null); |
| 1776 | McastStoreKey currentKey = new McastStoreKey(mcastIp, deviceId, assignedVlan); |
| 1777 | if (mcastNextObjStore.containsKey(currentKey)) { |
| 1778 | NextObjective currentNext = mcastNextObjStore.get(currentKey).value(); |
| 1779 | // Rebuild the next objective using assigned vlan |
| 1780 | currentNext = mcastUtils.nextObjBuilder(mcastIp, assignedVlan, |
| 1781 | mcastUtils.getPorts(currentNext.next()), currentNext.id()).verify(); |
| 1782 | // Send to the flowobjective service |
| 1783 | srManager.flowObjectiveService.next(deviceId, currentNext); |
| 1784 | } else { |
| 1785 | log.warn("Unable to run buckets corrector. " + |
| 1786 | "Missing next for {}, for source {} and for group {}", |
| 1787 | deviceId, source, mcastIp); |
| 1788 | } |
| 1789 | }); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1790 | }); |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1791 | }); |
| 1792 | } finally { |
Pier Luigi | 35dab3f | 2018-01-25 16:16:02 +0100 | [diff] [blame] | 1793 | mcastUnlock(); |
| 1794 | } |
| 1795 | |
| 1796 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 1797 | } |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1798 | |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1799 | /** |
| 1800 | * Returns the associated next ids to the mcast groups or to the single |
| 1801 | * group if mcastIp is present. |
| 1802 | * |
| 1803 | * @param mcastIp the group ip |
| 1804 | * @return the mapping mcastIp-device to next id |
| 1805 | */ |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1806 | public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) { |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1807 | if (mcastIp != null) { |
| 1808 | return mcastNextObjStore.entrySet().stream() |
| 1809 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1810 | .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().value().id())); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1811 | } |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1812 | return mcastNextObjStore.entrySet().stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1813 | .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().value().id())); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1816 | /** |
| 1817 | * Returns the associated roles to the mcast groups or to the single |
| 1818 | * group if mcastIp is present. |
| 1819 | * |
| 1820 | * @param mcastIp the group ip |
| 1821 | * @return the mapping mcastIp-device to mcast role |
| 1822 | * |
| 1823 | * @deprecated in 1.12 ("Magpie") release. |
| 1824 | */ |
| 1825 | @Deprecated |
Pier Luigi | 69f774d | 2018-02-28 12:10:50 +0100 | [diff] [blame] | 1826 | public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) { |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1827 | if (mcastIp != null) { |
| 1828 | return mcastRoleStore.entrySet().stream() |
| 1829 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1830 | .collect(Collectors.toMap(entry -> new McastStoreKey(entry.getKey().mcastIp(), |
| 1831 | entry.getKey().deviceId(), null), entry -> entry.getValue().value())); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1832 | } |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1833 | return mcastRoleStore.entrySet().stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1834 | .collect(Collectors.toMap(entry -> new McastStoreKey(entry.getKey().mcastIp(), |
| 1835 | entry.getKey().deviceId(), null), entry -> entry.getValue().value())); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1838 | /** |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1839 | * Returns the associated roles to the mcast groups. |
| 1840 | * |
| 1841 | * @param mcastIp the group ip |
| 1842 | * @param sourcecp the source connect point |
| 1843 | * @return the mapping mcastIp-device to mcast role |
| 1844 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1845 | public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, |
| 1846 | ConnectPoint sourcecp) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1847 | if (mcastIp != null) { |
| 1848 | Map<McastRoleStoreKey, McastRole> roles = mcastRoleStore.entrySet().stream() |
| 1849 | .filter(mcastEntry -> mcastIp.equals(mcastEntry.getKey().mcastIp())) |
| 1850 | .collect(Collectors.toMap(entry -> new McastRoleStoreKey(entry.getKey().mcastIp(), |
| 1851 | entry.getKey().deviceId(), entry.getKey().source()), entry -> entry.getValue().value())); |
| 1852 | if (sourcecp != null) { |
| 1853 | roles = roles.entrySet().stream() |
| 1854 | .filter(mcastEntry -> sourcecp.equals(mcastEntry.getKey().source())) |
| 1855 | .collect(Collectors.toMap(entry -> new McastRoleStoreKey(entry.getKey().mcastIp(), |
| 1856 | entry.getKey().deviceId(), entry.getKey().source()), Entry::getValue)); |
| 1857 | } |
| 1858 | return roles; |
| 1859 | } |
| 1860 | return mcastRoleStore.entrySet().stream() |
| 1861 | .collect(Collectors.toMap(entry -> new McastRoleStoreKey(entry.getKey().mcastIp(), |
| 1862 | entry.getKey().deviceId(), entry.getKey().source()), entry -> entry.getValue().value())); |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | /** |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1867 | * Returns the associated paths to the mcast group. |
| 1868 | * |
| 1869 | * @param mcastIp the group ip |
| 1870 | * @return the mapping egress point to mcast path |
| 1871 | * |
| 1872 | * @deprecated in 1.12 ("Magpie") release. |
| 1873 | */ |
| 1874 | @Deprecated |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1875 | public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) { |
| 1876 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1877 | ConnectPoint source = mcastUtils.getSource(mcastIp); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1878 | if (source != null) { |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1879 | Set<DeviceId> visited = Sets.newHashSet(); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1880 | List<ConnectPoint> currentPath = Lists.newArrayList(source); |
| 1881 | buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp, source); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1882 | } |
| 1883 | return mcastPaths; |
| 1884 | } |
| 1885 | |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1886 | /** |
| 1887 | * Returns the associated trees to the mcast group. |
| 1888 | * |
| 1889 | * @param mcastIp the group ip |
| 1890 | * @param sourcecp the source connect point |
| 1891 | * @return the mapping egress point to mcast path |
| 1892 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1893 | public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp, |
| 1894 | ConnectPoint sourcecp) { |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1895 | Multimap<ConnectPoint, List<ConnectPoint>> mcastTrees = HashMultimap.create(); |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1896 | Set<ConnectPoint> sources = mcastUtils.getSources(mcastIp); |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1897 | if (sourcecp != null) { |
| 1898 | sources = sources.stream() |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1899 | .filter(source -> source.equals(sourcecp)).collect(Collectors.toSet()); |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1900 | } |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1901 | if (!sources.isEmpty()) { |
| 1902 | sources.forEach(source -> { |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1903 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths = Maps.newHashMap(); |
| 1904 | Set<DeviceId> visited = Sets.newHashSet(); |
| 1905 | List<ConnectPoint> currentPath = Lists.newArrayList(source); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1906 | buildMcastPaths(source.deviceId(), visited, mcastPaths, currentPath, mcastIp, source); |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1907 | mcastPaths.forEach(mcastTrees::put); |
| 1908 | }); |
| 1909 | } |
| 1910 | return mcastTrees; |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * Build recursively the mcast paths. |
| 1915 | * |
| 1916 | * @param toVisit the node to visit |
| 1917 | * @param visited the visited nodes |
| 1918 | * @param mcastPaths the current mcast paths |
| 1919 | * @param currentPath the current path |
| 1920 | * @param mcastIp the group ip |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1921 | * @param source the source |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 1922 | */ |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame^] | 1923 | private void buildMcastPaths(DeviceId toVisit, Set<DeviceId> visited, |
| 1924 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths, |
| 1925 | List<ConnectPoint> currentPath, IpAddress mcastIp, |
| 1926 | ConnectPoint source) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1927 | // If we have visited the node to visit there is a loop |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1928 | if (visited.contains(toVisit)) { |
| 1929 | return; |
| 1930 | } |
| 1931 | // Visit next-hop |
| 1932 | visited.add(toVisit); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1933 | VlanId assignedVlan = mcastUtils.assignedVlan(toVisit.equals(source.deviceId()) ? source : null); |
| 1934 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit, assignedVlan); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1935 | // Looking for next-hops |
| 1936 | if (mcastNextObjStore.containsKey(mcastStoreKey)) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1937 | // Build egress connect points, get ports and build relative cps |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1938 | NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey).value(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1939 | Set<PortNumber> outputPorts = mcastUtils.getPorts(nextObjective.next()); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1940 | ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder(); |
| 1941 | outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber))); |
| 1942 | Set<ConnectPoint> egressPoints = cpBuilder.build(); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1943 | Set<Link> egressLinks; |
| 1944 | List<ConnectPoint> newCurrentPath; |
| 1945 | Set<DeviceId> newVisited; |
| 1946 | DeviceId newToVisit; |
| 1947 | for (ConnectPoint egressPoint : egressPoints) { |
| 1948 | egressLinks = srManager.linkService.getEgressLinks(egressPoint); |
| 1949 | // If it does not have egress links, stop |
| 1950 | if (egressLinks.isEmpty()) { |
| 1951 | // Add the connect points to the path |
| 1952 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1953 | newCurrentPath.add(0, egressPoint); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1954 | mcastPaths.put(egressPoint, newCurrentPath); |
| 1955 | } else { |
| 1956 | newVisited = Sets.newHashSet(visited); |
| 1957 | // Iterate over the egress links for the next hops |
| 1958 | for (Link egressLink : egressLinks) { |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1959 | newToVisit = egressLink.dst().deviceId(); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1960 | newCurrentPath = Lists.newArrayList(currentPath); |
| 1961 | newCurrentPath.add(0, egressPoint); |
| 1962 | newCurrentPath.add(0, egressLink.dst()); |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 1963 | buildMcastPaths(newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp, source); |
Pier Luigi | 0f9635b | 2018-01-15 18:06:43 +0100 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 1970 | /** |
| 1971 | * Return the leaders of the mcast groups. |
| 1972 | * |
| 1973 | * @param mcastIp the group ip |
| 1974 | * @return the mapping group-node |
| 1975 | */ |
| 1976 | public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) { |
| 1977 | return mcastUtils.getMcastLeaders(mcastIp); |
| 1978 | } |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1979 | } |