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