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