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