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