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