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