Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.segmentrouting.mcast; |
| 18 | |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 24 | import com.google.common.hash.HashFunction; |
| 25 | import com.google.common.hash.Hashing; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.mcast.api.McastRoute; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.HostId; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 36 | import org.onosproject.net.Link; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.config.basics.McastConfig; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.flow.criteria.Criteria; |
| 44 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 45 | import org.onosproject.net.flow.instructions.Instructions; |
| 46 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 47 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 49 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 50 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 51 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 52 | import org.onosproject.net.flowobjective.NextObjective; |
| 53 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 54 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 55 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 56 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 57 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
| 58 | import org.slf4j.Logger; |
| 59 | |
| 60 | import java.util.Collection; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 61 | import java.util.List; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 62 | import java.util.Map; |
| 63 | import java.util.Set; |
| 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Utility class for Multicast Handler. |
| 70 | */ |
| 71 | class McastUtils { |
| 72 | |
| 73 | // Internal reference to the log |
| 74 | private final Logger log; |
| 75 | // Internal reference to SR Manager |
| 76 | private SegmentRoutingManager srManager; |
| 77 | // Internal reference to the app id |
| 78 | private ApplicationId coreAppId; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 79 | // Hashing function for the multicast hasher |
| 80 | private static final HashFunction HASH_FN = Hashing.md5(); |
| 81 | // Read only cache of the Mcast leader |
| 82 | private Map<IpAddress, NodeId> mcastLeaderCache; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Builds a new McastUtils object. |
| 86 | * |
| 87 | * @param srManager the SR manager |
| 88 | * @param coreAppId the core application id |
| 89 | * @param log log reference of the McastHandler |
| 90 | */ |
| 91 | McastUtils(SegmentRoutingManager srManager, ApplicationId coreAppId, Logger log) { |
| 92 | this.srManager = srManager; |
| 93 | this.coreAppId = coreAppId; |
| 94 | this.log = log; |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 95 | this.mcastLeaderCache = Maps.newConcurrentMap(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Pier | 72d0e58 | 2018-04-20 14:14:34 +0200 | [diff] [blame] | 99 | * Clean up when deactivating the application. |
| 100 | */ |
| 101 | public void terminate() { |
| 102 | mcastLeaderCache.clear(); |
| 103 | } |
| 104 | |
| 105 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 106 | * Get router mac using application config and the connect point. |
| 107 | * |
| 108 | * @param deviceId the device id |
| 109 | * @param port the port number |
| 110 | * @return the router mac if the port is configured, otherwise null |
| 111 | */ |
| 112 | private MacAddress getRouterMac(DeviceId deviceId, PortNumber port) { |
| 113 | // Do nothing if the port is configured as suppressed |
| 114 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 115 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 116 | .getConfig(srManager.appId(), SegmentRoutingAppConfig.class); |
| 117 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
| 118 | log.info("Ignore suppressed port {}", connectPoint); |
| 119 | return MacAddress.NONE; |
| 120 | } |
| 121 | // Get the router mac using the device configuration |
| 122 | MacAddress routerMac; |
| 123 | try { |
| 124 | routerMac = srManager.deviceConfiguration().getDeviceMac(deviceId); |
| 125 | } catch (DeviceConfigNotFoundException dcnfe) { |
Esin Karaman | 93b2889 | 2019-07-24 11:26:39 +0000 | [diff] [blame^] | 126 | log.warn("Failed to get device MAC since the device {} is not configured", deviceId); |
| 127 | return null; |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 128 | } |
| 129 | return routerMac; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Adds filtering objective for given device and port. |
| 134 | * |
| 135 | * @param deviceId device ID |
| 136 | * @param port ingress port number |
| 137 | * @param assignedVlan assigned VLAN ID |
| 138 | * @param mcastIp the group address |
| 139 | * @param mcastRole the role of the device |
| 140 | */ |
| 141 | void addFilterToDevice(DeviceId deviceId, PortNumber port, VlanId assignedVlan, |
| 142 | IpAddress mcastIp, McastRole mcastRole) { |
| 143 | |
| 144 | MacAddress routerMac = getRouterMac(deviceId, port); |
Esin Karaman | 93b2889 | 2019-07-24 11:26:39 +0000 | [diff] [blame^] | 145 | |
| 146 | if (MacAddress.NONE.equals(routerMac)) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | FilteringObjective.Builder filtObjBuilder = filterObjBuilder(port, assignedVlan, mcastIp, |
| 151 | routerMac, mcastRole); |
| 152 | ObjectiveContext context = new DefaultObjectiveContext( |
| 153 | (objective) -> log.debug("Successfully add filter on {}/{}, vlan {}", |
| 154 | deviceId, port.toLong(), assignedVlan), |
| 155 | (objective, error) -> |
| 156 | log.warn("Failed to add filter on {}/{}, vlan {}: {}", |
| 157 | deviceId, port.toLong(), assignedVlan, error)); |
| 158 | srManager.flowObjectiveService.filter(deviceId, filtObjBuilder.add(context)); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Removes filtering objective for given device and port. |
| 163 | * |
| 164 | * @param deviceId device ID |
| 165 | * @param port ingress port number |
| 166 | * @param assignedVlan assigned VLAN ID |
| 167 | * @param mcastIp multicast IP address |
| 168 | * @param mcastRole the multicast role of the device |
| 169 | */ |
| 170 | void removeFilterToDevice(DeviceId deviceId, PortNumber port, VlanId assignedVlan, |
| 171 | IpAddress mcastIp, McastRole mcastRole) { |
| 172 | |
| 173 | MacAddress routerMac = getRouterMac(deviceId, port); |
Esin Karaman | 93b2889 | 2019-07-24 11:26:39 +0000 | [diff] [blame^] | 174 | |
| 175 | if (MacAddress.NONE.equals(routerMac)) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
| 179 | FilteringObjective.Builder filtObjBuilder = |
| 180 | filterObjBuilder(port, assignedVlan, mcastIp, routerMac, mcastRole); |
| 181 | ObjectiveContext context = new DefaultObjectiveContext( |
| 182 | (objective) -> log.debug("Successfully removed filter on {}/{}, vlan {}", |
| 183 | deviceId, port.toLong(), assignedVlan), |
| 184 | (objective, error) -> |
| 185 | log.warn("Failed to remove filter on {}/{}, vlan {}: {}", |
| 186 | deviceId, port.toLong(), assignedVlan, error)); |
| 187 | srManager.flowObjectiveService.filter(deviceId, filtObjBuilder.remove(context)); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Gets assigned VLAN according to the value in the meta. |
| 192 | * |
| 193 | * @param nextObjective nextObjective to analyze |
| 194 | * @return assigned VLAN ID |
| 195 | */ |
| 196 | VlanId assignedVlanFromNext(NextObjective nextObjective) { |
| 197 | return ((VlanIdCriterion) nextObjective.meta().getCriterion(VLAN_VID)).vlanId(); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Gets ingress VLAN from McastConfig. |
| 202 | * |
| 203 | * @return ingress VLAN or VlanId.NONE if not configured |
| 204 | */ |
| 205 | private VlanId ingressVlan() { |
| 206 | McastConfig mcastConfig = |
| 207 | srManager.cfgService.getConfig(coreAppId, McastConfig.class); |
| 208 | return (mcastConfig != null) ? mcastConfig.ingressVlan() : VlanId.NONE; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Gets egress VLAN from McastConfig. |
| 213 | * |
| 214 | * @return egress VLAN or VlanId.NONE if not configured |
| 215 | */ |
| 216 | private VlanId egressVlan() { |
| 217 | McastConfig mcastConfig = |
| 218 | srManager.cfgService.getConfig(coreAppId, McastConfig.class); |
| 219 | return (mcastConfig != null) ? mcastConfig.egressVlan() : VlanId.NONE; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Gets assigned VLAN according to the value of egress VLAN. |
| 224 | * If connect point is specified, try to reuse the assigned VLAN on the connect point. |
| 225 | * |
| 226 | * @param cp connect point; Can be null if not specified |
| 227 | * @return assigned VLAN ID |
| 228 | */ |
| 229 | VlanId assignedVlan(ConnectPoint cp) { |
| 230 | // Use the egressVlan if it is tagged |
| 231 | if (!egressVlan().equals(VlanId.NONE)) { |
| 232 | return egressVlan(); |
| 233 | } |
| 234 | // Reuse unicast VLAN if the port has subnet configured |
| 235 | if (cp != null) { |
| 236 | VlanId untaggedVlan = srManager.getInternalVlanId(cp); |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 237 | return (untaggedVlan != null) ? untaggedVlan |
| 238 | : srManager.getDefaultInternalVlan(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 239 | } |
| 240 | // Use DEFAULT_VLAN if none of the above matches |
Saurav Das | 9bf4958 | 2018-08-13 15:34:26 -0700 | [diff] [blame] | 241 | return srManager.getDefaultInternalVlan(); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Gets source connect point of given multicast group. |
| 246 | * |
| 247 | * @param mcastIp multicast IP |
| 248 | * @return source connect point or null if not found |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 249 | * |
| 250 | * @deprecated in 1.12 ("Magpie") release. |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 251 | */ |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 252 | @Deprecated |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 253 | ConnectPoint getSource(IpAddress mcastIp) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 254 | McastRoute mcastRoute = srManager.multicastRouteService.getRoutes().stream() |
| 255 | .filter(mcastRouteInternal -> mcastRouteInternal.group().equals(mcastIp)) |
| 256 | .findFirst().orElse(null); |
| 257 | return mcastRoute == null ? null : srManager.multicastRouteService.sources(mcastRoute) |
| 258 | .stream() |
| 259 | .findFirst().orElse(null); |
| 260 | } |
| 261 | |
| 262 | /** |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 263 | * Gets sources connect points of given multicast group. |
| 264 | * |
| 265 | * @param mcastIp multicast IP |
| 266 | * @return sources connect points or empty set if not found |
| 267 | */ |
| 268 | Set<ConnectPoint> getSources(IpAddress mcastIp) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 269 | // TODO we should support different types of routes |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 270 | McastRoute mcastRoute = srManager.multicastRouteService.getRoutes().stream() |
| 271 | .filter(mcastRouteInternal -> mcastRouteInternal.group().equals(mcastIp)) |
| 272 | .findFirst().orElse(null); |
| 273 | return mcastRoute == null ? ImmutableSet.of() : |
| 274 | srManager.multicastRouteService.sources(mcastRoute); |
| 275 | } |
| 276 | |
| 277 | /** |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 278 | * Gets sinks of given multicast group. |
| 279 | * |
| 280 | * @param mcastIp multicast IP |
| 281 | * @return map of sinks or empty map if not found |
| 282 | */ |
| 283 | Map<HostId, Set<ConnectPoint>> getSinks(IpAddress mcastIp) { |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 284 | // TODO we should support different types of routes |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 285 | McastRoute mcastRoute = srManager.multicastRouteService.getRoutes().stream() |
| 286 | .filter(mcastRouteInternal -> mcastRouteInternal.group().equals(mcastIp)) |
| 287 | .findFirst().orElse(null); |
| 288 | return mcastRoute == null ? |
Pier | 71c5577 | 2018-04-17 17:25:22 +0200 | [diff] [blame] | 289 | ImmutableMap.of() : |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 290 | srManager.multicastRouteService.routeData(mcastRoute).sinks(); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Get sinks affected by this egress device. |
| 295 | * |
| 296 | * @param egressDevice the egress device |
| 297 | * @param mcastIp the mcast ip address |
| 298 | * @return the map of the sinks affected |
| 299 | */ |
| 300 | Map<HostId, Set<ConnectPoint>> getAffectedSinks(DeviceId egressDevice, |
Pier | e99511d | 2018-04-19 16:47:06 +0200 | [diff] [blame] | 301 | IpAddress mcastIp) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 302 | return getSinks(mcastIp).entrySet() |
| 303 | .stream() |
| 304 | .filter(hostIdSetEntry -> hostIdSetEntry.getValue().stream() |
| 305 | .map(ConnectPoint::deviceId) |
| 306 | .anyMatch(deviceId -> deviceId.equals(egressDevice)) |
| 307 | ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Creates a next objective builder for multicast. |
| 312 | * |
| 313 | * @param mcastIp multicast group |
| 314 | * @param assignedVlan assigned VLAN ID |
| 315 | * @param outPorts set of output port numbers |
| 316 | * @param nextId the next id |
| 317 | * @return next objective builder |
| 318 | */ |
| 319 | NextObjective.Builder nextObjBuilder(IpAddress mcastIp, VlanId assignedVlan, |
| 320 | Set<PortNumber> outPorts, Integer nextId) { |
| 321 | // If nextId is null allocate a new one |
| 322 | if (nextId == null) { |
| 323 | nextId = srManager.flowObjectiveService.allocateNextId(); |
| 324 | } |
| 325 | // Build the meta selector with the fwd objective info |
| 326 | TrafficSelector metadata = |
| 327 | DefaultTrafficSelector.builder() |
| 328 | .matchVlanId(assignedVlan) |
| 329 | .matchIPDst(mcastIp.toIpPrefix()) |
| 330 | .build(); |
| 331 | // Define the nextobjective type |
| 332 | NextObjective.Builder nextObjBuilder = DefaultNextObjective |
| 333 | .builder().withId(nextId) |
| 334 | .withType(NextObjective.Type.BROADCAST) |
| 335 | .fromApp(srManager.appId()) |
| 336 | .withMeta(metadata); |
| 337 | // Add the output ports |
| 338 | outPorts.forEach(port -> { |
| 339 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 340 | if (egressVlan().equals(VlanId.NONE)) { |
| 341 | tBuilder.popVlan(); |
| 342 | } |
| 343 | tBuilder.setOutput(port); |
| 344 | nextObjBuilder.addTreatment(tBuilder.build()); |
| 345 | }); |
| 346 | // Done return the complete builder |
| 347 | return nextObjBuilder; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Creates a forwarding objective builder for multicast. |
| 352 | * |
| 353 | * @param mcastIp multicast group |
| 354 | * @param assignedVlan assigned VLAN ID |
| 355 | * @param nextId next ID of the L3 multicast group |
| 356 | * @return forwarding objective builder |
| 357 | */ |
| 358 | ForwardingObjective.Builder fwdObjBuilder(IpAddress mcastIp, |
| 359 | VlanId assignedVlan, int nextId) { |
| 360 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 361 | // Let's the matching on the group address |
| 362 | // TODO SSM support in future |
| 363 | if (mcastIp.isIp6()) { |
| 364 | sbuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 365 | sbuilder.matchIPv6Dst(mcastIp.toIpPrefix()); |
| 366 | } else { |
| 367 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 368 | sbuilder.matchIPDst(mcastIp.toIpPrefix()); |
| 369 | } |
| 370 | // Then build the meta selector |
| 371 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 372 | metabuilder.matchVlanId(assignedVlan); |
| 373 | // Finally return the completed builder |
| 374 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder(); |
| 375 | fwdBuilder.withSelector(sbuilder.build()) |
| 376 | .withMeta(metabuilder.build()) |
| 377 | .nextStep(nextId) |
| 378 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 379 | .fromApp(srManager.appId()) |
| 380 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 381 | return fwdBuilder; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Creates a filtering objective builder for multicast. |
| 386 | * |
| 387 | * @param ingressPort ingress port of the multicast stream |
| 388 | * @param assignedVlan assigned VLAN ID |
| 389 | * @param mcastIp the group address |
| 390 | * @param routerMac router MAC. This is carried in metadata and used from some switches that |
| 391 | * need to put unicast entry before multicast entry in TMAC table. |
| 392 | * @param mcastRole the Multicast role |
| 393 | * @return filtering objective builder |
| 394 | */ |
| 395 | private FilteringObjective.Builder filterObjBuilder(PortNumber ingressPort, VlanId assignedVlan, |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame] | 396 | IpAddress mcastIp, MacAddress routerMac, McastRole mcastRole) { |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 397 | FilteringObjective.Builder filtBuilder = DefaultFilteringObjective.builder(); |
| 398 | // Let's add the in port matching and the priority |
| 399 | filtBuilder.withKey(Criteria.matchInPort(ingressPort)) |
| 400 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY); |
| 401 | // According to the mcast role we match on the proper vlan |
| 402 | // If the role is null we are on the transit or on the egress |
| 403 | if (mcastRole == null) { |
| 404 | filtBuilder.addCondition(Criteria.matchVlanId(egressVlan())); |
| 405 | } else { |
| 406 | filtBuilder.addCondition(Criteria.matchVlanId(ingressVlan())); |
| 407 | } |
| 408 | // According to the IP type we set the proper match on the mac address |
| 409 | if (mcastIp.isIp4()) { |
| 410 | filtBuilder.addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame] | 411 | MacAddress.IPV4_MULTICAST_MASK)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 412 | } else { |
| 413 | filtBuilder.addCondition(Criteria.matchEthDstMasked(MacAddress.IPV6_MULTICAST, |
Charles Chan | ba59dd6 | 2018-05-10 22:19:49 +0000 | [diff] [blame] | 414 | MacAddress.IPV6_MULTICAST_MASK)); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 415 | } |
| 416 | // We finally build the meta treatment |
Esin Karaman | 93b2889 | 2019-07-24 11:26:39 +0000 | [diff] [blame^] | 417 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 418 | tBuilder.pushVlan().setVlanId(assignedVlan); |
| 419 | |
| 420 | if (routerMac != null && !routerMac.equals(MacAddress.NONE)) { |
| 421 | tBuilder.setEthDst(routerMac); |
| 422 | } |
| 423 | |
| 424 | filtBuilder.withMeta(tBuilder.build()); |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 425 | // Done, we return a permit filtering objective |
| 426 | return filtBuilder.permit().fromApp(srManager.appId()); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Gets output ports information from treatments. |
| 431 | * |
| 432 | * @param treatments collection of traffic treatments |
| 433 | * @return set of output port numbers |
| 434 | */ |
| 435 | Set<PortNumber> getPorts(Collection<TrafficTreatment> treatments) { |
| 436 | ImmutableSet.Builder<PortNumber> builder = ImmutableSet.builder(); |
| 437 | treatments.forEach(treatment -> treatment.allInstructions().stream() |
| 438 | .filter(instr -> instr instanceof Instructions.OutputInstruction) |
| 439 | .forEach(instr -> builder.add(((Instructions.OutputInstruction) instr).port()))); |
| 440 | return builder.build(); |
| 441 | } |
Pier | db27b8d | 2018-04-17 16:29:56 +0200 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * Returns the hash of the group address. |
| 445 | * |
| 446 | * @param ipAddress the ip address |
| 447 | * @return the hash of the address |
| 448 | */ |
| 449 | private Long hasher(IpAddress ipAddress) { |
| 450 | return HASH_FN.newHasher() |
| 451 | .putBytes(ipAddress.toOctets()) |
| 452 | .hash() |
| 453 | .asLong(); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Given a multicast group define a leader for it. |
| 458 | * |
| 459 | * @param mcastIp the group address |
| 460 | * @return true if the instance is the leader of the group |
| 461 | */ |
| 462 | boolean isLeader(IpAddress mcastIp) { |
| 463 | // Get our id |
| 464 | final NodeId currentNodeId = srManager.clusterService.getLocalNode().id(); |
| 465 | // Get the leader for this group using the ip address as key |
| 466 | final NodeId leader = srManager.workPartitionService.getLeader(mcastIp, this::hasher); |
| 467 | // If there is not a leader, let's send an error |
| 468 | if (leader == null) { |
| 469 | log.error("Fail to elect a leader for {}.", mcastIp); |
| 470 | return false; |
| 471 | } |
| 472 | // Update cache and return operation result |
| 473 | mcastLeaderCache.put(mcastIp, leader); |
| 474 | return currentNodeId.equals(leader); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Given a multicast group withdraw its leader. |
| 479 | * |
| 480 | * @param mcastIp the group address |
| 481 | */ |
| 482 | void withdrawLeader(IpAddress mcastIp) { |
| 483 | // For now just update the cache |
| 484 | mcastLeaderCache.remove(mcastIp); |
| 485 | } |
| 486 | |
| 487 | Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) { |
| 488 | // If mcast ip is present |
| 489 | if (mcastIp != null) { |
| 490 | return mcastLeaderCache.entrySet().stream() |
| 491 | .filter(entry -> entry.getKey().equals(mcastIp)) |
| 492 | .collect(Collectors.toMap(Map.Entry::getKey, |
| 493 | Map.Entry::getValue)); |
| 494 | } |
| 495 | // Otherwise take all the groups |
| 496 | return ImmutableMap.copyOf(mcastLeaderCache); |
| 497 | } |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Build recursively the mcast paths. |
| 501 | * |
| 502 | * @param mcastNextObjStore mcast next obj store |
| 503 | * @param toVisit the node to visit |
| 504 | * @param visited the visited nodes |
| 505 | * @param mcastPaths the current mcast paths |
| 506 | * @param currentPath the current path |
| 507 | * @param mcastIp the group ip |
| 508 | * @param source the source |
| 509 | */ |
| 510 | void buildMcastPaths(Map<McastStoreKey, NextObjective> mcastNextObjStore, |
| 511 | DeviceId toVisit, Set<DeviceId> visited, |
| 512 | Map<ConnectPoint, List<ConnectPoint>> mcastPaths, |
| 513 | List<ConnectPoint> currentPath, IpAddress mcastIp, |
| 514 | ConnectPoint source) { |
| 515 | // If we have visited the node to visit there is a loop |
| 516 | if (visited.contains(toVisit)) { |
| 517 | return; |
| 518 | } |
| 519 | // Visit next-hop |
| 520 | visited.add(toVisit); |
| 521 | VlanId assignedVlan = assignedVlan(toVisit.equals(source.deviceId()) ? source : null); |
| 522 | McastStoreKey mcastStoreKey = new McastStoreKey(mcastIp, toVisit, assignedVlan); |
| 523 | // Looking for next-hops |
| 524 | if (mcastNextObjStore.containsKey(mcastStoreKey)) { |
| 525 | // Build egress connect points, get ports and build relative cps |
| 526 | NextObjective nextObjective = mcastNextObjStore.get(mcastStoreKey); |
| 527 | Set<PortNumber> outputPorts = getPorts(nextObjective.next()); |
| 528 | ImmutableSet.Builder<ConnectPoint> cpBuilder = ImmutableSet.builder(); |
| 529 | outputPorts.forEach(portNumber -> cpBuilder.add(new ConnectPoint(toVisit, portNumber))); |
| 530 | Set<ConnectPoint> egressPoints = cpBuilder.build(); |
| 531 | Set<Link> egressLinks; |
| 532 | List<ConnectPoint> newCurrentPath; |
| 533 | Set<DeviceId> newVisited; |
| 534 | DeviceId newToVisit; |
| 535 | for (ConnectPoint egressPoint : egressPoints) { |
| 536 | egressLinks = srManager.linkService.getEgressLinks(egressPoint); |
| 537 | // If it does not have egress links, stop |
| 538 | if (egressLinks.isEmpty()) { |
| 539 | // Add the connect points to the path |
| 540 | newCurrentPath = Lists.newArrayList(currentPath); |
| 541 | newCurrentPath.add(0, egressPoint); |
| 542 | mcastPaths.put(egressPoint, newCurrentPath); |
| 543 | } else { |
| 544 | newVisited = Sets.newHashSet(visited); |
| 545 | // Iterate over the egress links for the next hops |
| 546 | for (Link egressLink : egressLinks) { |
| 547 | newToVisit = egressLink.dst().deviceId(); |
| 548 | newCurrentPath = Lists.newArrayList(currentPath); |
| 549 | newCurrentPath.add(0, egressPoint); |
| 550 | newCurrentPath.add(0, egressLink.dst()); |
| 551 | buildMcastPaths(mcastNextObjStore, newToVisit, newVisited, mcastPaths, newCurrentPath, mcastIp, |
| 552 | source); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
Pier | 7b65716 | 2018-03-27 11:29:42 -0700 | [diff] [blame] | 558 | } |