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