alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.mfwd.impl; |
| 17 | |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.FilteredConnectPoint; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IPv4; |
Thomas Vachuska | 61ab5e0 | 2016-01-11 13:55:15 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ip4Address; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentService; |
| 38 | import org.onosproject.net.intent.Key; |
| 39 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 40 | import org.onosproject.net.mcast.McastRoute; |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 41 | import org.onosproject.net.mcast.McastListener; |
| 42 | import org.onosproject.net.mcast.McastEvent; |
Thomas Vachuska | 61ab5e0 | 2016-01-11 13:55:15 -0800 | [diff] [blame] | 43 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 44 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 45 | import org.onosproject.net.packet.InboundPacket; |
| 46 | import org.onosproject.net.packet.OutboundPacket; |
| 47 | import org.onosproject.net.packet.PacketContext; |
| 48 | import org.onosproject.net.packet.PacketPriority; |
| 49 | import org.onosproject.net.packet.PacketProcessor; |
| 50 | import org.onosproject.net.packet.PacketService; |
| 51 | import org.slf4j.Logger; |
| 52 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 53 | import java.util.ArrayList; |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
| 55 | import java.util.HashSet; |
| 56 | import java.util.Map; |
| 57 | import java.util.Set; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 58 | |
Thomas Vachuska | 61ab5e0 | 2016-01-11 13:55:15 -0800 | [diff] [blame] | 59 | import static com.google.common.base.Preconditions.checkNotNull; |
| 60 | import static org.slf4j.LoggerFactory.getLogger; |
| 61 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 62 | /** |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 63 | * The multicast forwarding component. This component is responsible for |
| 64 | * handling live multicast traffic by modifying multicast state and forwarding |
| 65 | * packets that do not yet have state installed. |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 66 | */ |
| 67 | @Component(immediate = true) |
| 68 | public class McastForwarding { |
| 69 | |
| 70 | private final Logger log = getLogger(getClass()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 73 | protected PacketService packetService; |
| 74 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 76 | protected CoreService coreService; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 79 | private IntentService intentService; |
| 80 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 82 | protected MulticastRouteService mcastRouteManager; |
| 83 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 84 | protected McastIntentManager mcastIntentManager; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 85 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 86 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 87 | private static ApplicationId appId; |
| 88 | |
| 89 | /** |
| 90 | * Active MulticastForwardingIntent. |
| 91 | */ |
| 92 | @Activate |
| 93 | public void activate() { |
| 94 | appId = coreService.registerApplication("org.onosproject.mfwd"); |
| 95 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 96 | mcastIntentManager = new McastIntentManager(); |
| 97 | mcastRouteManager.addListener(mcastIntentManager); |
| 98 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 99 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 100 | |
| 101 | // Build a traffic selector for all multicast traffic |
| 102 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 103 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 104 | selector.matchIPDst(IpPrefix.IPV4_MULTICAST_PREFIX); |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 105 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 106 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 107 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Deactivate Multicast Forwarding Intent. |
| 113 | */ |
| 114 | @Deactivate |
| 115 | public void deactivate() { |
| 116 | packetService.removeProcessor(processor); |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 117 | mcastRouteManager.removeListener(mcastIntentManager); |
| 118 | mcastIntentManager.withdrawAllIntents(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 119 | processor = null; |
| 120 | log.info("Stopped"); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get the application ID, used by the McastIntentManager. |
| 125 | * |
| 126 | * @return the application ID |
| 127 | */ |
| 128 | public static ApplicationId getAppId() { |
| 129 | return appId; |
| 130 | } |
| 131 | |
| 132 | /** |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 133 | * Forward the packet to it's multicast destinations. |
| 134 | * |
| 135 | * @param context The packet context |
| 136 | * @param egressList The list of egress ports which the multicast packet is intended for. |
| 137 | */ |
| 138 | private void forwardPacketToDst(PacketContext context, ArrayList<ConnectPoint> egressList) { |
| 139 | |
| 140 | // Send the pack out each of the respective egress ports |
| 141 | for (ConnectPoint egress : egressList) { |
| 142 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 143 | .setOutput(egress.port()).build(); |
| 144 | |
| 145 | OutboundPacket packet = new DefaultOutboundPacket( |
| 146 | egress.deviceId(), |
| 147 | treatment, |
| 148 | context.inPacket().unparsed()); |
| 149 | |
| 150 | packetService.emit(packet); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public static McastRoute createStaticRoute(String source, String group) { |
| 155 | checkNotNull(source, "Must provide a source"); |
| 156 | checkNotNull(group, "Must provide a group"); |
| 157 | IpAddress ipSource = IpAddress.valueOf(source); |
| 158 | IpAddress ipGroup = IpAddress.valueOf(group); |
| 159 | return createStaticcreateRoute(ipSource, ipGroup); |
| 160 | } |
| 161 | |
| 162 | public static McastRoute createStaticcreateRoute(IpAddress source, IpAddress group) { |
| 163 | checkNotNull(source, "Must provide a source"); |
| 164 | checkNotNull(group, "Must provide a group"); |
| 165 | McastRoute.Type type = McastRoute.Type.STATIC; |
| 166 | return new McastRoute(source, group, type); |
| 167 | } |
| 168 | |
| 169 | /** |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 170 | * Packet processor responsible for forwarding packets along their paths. |
| 171 | */ |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 172 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 173 | private class ReactivePacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Process incoming packets. |
| 176 | * |
| 177 | * @param context packet processing context |
| 178 | */ |
| 179 | @Override |
| 180 | public void process(PacketContext context) { |
| 181 | // Stop processing if the packet has been handled, since we |
| 182 | // can't do any more to it. |
| 183 | if (context.isHandled()) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | InboundPacket pkt = context.inPacket(); |
| 188 | Ethernet ethPkt = pkt.parsed(); |
| 189 | |
| 190 | if (ethPkt == null) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4 && |
| 195 | ethPkt.getEtherType() != Ethernet.TYPE_IPV6) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 200 | // Ignore ipv6 at the moment. |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 205 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 206 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 207 | |
| 208 | log.debug("Packet ({}, {}) has been punted\n" + |
| 209 | "\tingress port: {}\n", |
| 210 | saddr.toString(), |
| 211 | gaddr.toString(), |
| 212 | context.inPacket().receivedFrom().toString()); |
| 213 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 214 | // Don't allow PIM/IGMP packets to be handled here. |
| 215 | byte proto = ip.getProtocol(); |
| 216 | if (proto == IPv4.PROTOCOL_PIM || proto == IPv4.PROTOCOL_IGMP) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | IpPrefix spfx = IpPrefix.valueOf(saddr, 32); |
| 221 | IpPrefix gpfx = IpPrefix.valueOf(gaddr, 32); |
| 222 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 223 | // TODO do we want to add a type for Mcast? |
Thomas Vachuska | 61ab5e0 | 2016-01-11 13:55:15 -0800 | [diff] [blame] | 224 | McastRoute mRoute = new McastRoute(saddr, gaddr, McastRoute.Type.STATIC); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 225 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 226 | ConnectPoint ingress = mcastRouteManager.fetchSource(mRoute); |
| 227 | |
| 228 | // An ingress port already exists. Log error. |
| 229 | if (ingress != null) { |
| 230 | log.error(McastForwarding.class.getSimpleName() + " received packet which already has a route."); |
| 231 | return; |
| 232 | } else { |
| 233 | //add ingress port |
| 234 | mcastRouteManager.addSource(mRoute, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 237 | ArrayList<ConnectPoint> egressList = (ArrayList<ConnectPoint>) mcastRouteManager.fetchSinks(mRoute); |
| 238 | //If there are no egress ports set return, otherwise forward the packets to their expected port. |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 239 | if (egressList.isEmpty()) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 243 | // Send the pack out each of the egress devices & port |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 244 | forwardPacketToDst(context, egressList); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 245 | } |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 246 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 249 | private class McastIntentManager implements McastListener { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 250 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 251 | private Map<McastRoute, Key> intentHashMap; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 252 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 253 | public McastIntentManager() { |
| 254 | intentHashMap = new HashMap<>(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 255 | } |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 256 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 257 | @Override |
| 258 | public void event(McastEvent event) { |
| 259 | McastRoute route = event.subject().route(); |
| 260 | if (intentHashMap.containsKey(route)) { |
| 261 | withdrawIntent(intentHashMap.get(route)); |
| 262 | } |
| 263 | Key routeKey = setIntent(route); |
| 264 | intentHashMap.put(route, routeKey); |
| 265 | } |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 266 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 267 | private Key setIntent(McastRoute route) { |
| 268 | |
| 269 | ConnectPoint ingressPoint = mcastRouteManager.fetchSource(route); |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 270 | Set<FilteredConnectPoint> filteredEgressPoints = new HashSet<>(); |
| 271 | mcastRouteManager.fetchSinks(route).iterator() |
| 272 | .forEachRemaining(point -> filteredEgressPoints.add(new FilteredConnectPoint(point))); |
| 273 | |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 274 | |
| 275 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 276 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 277 | |
| 278 | if (ingressPoint == null) { |
| 279 | log.warn("Can't set intent without an ingress or egress connect points"); |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | selector.matchEthType(Ethernet.TYPE_IPV4) |
| 284 | .matchIPDst(route.group().toIpPrefix()) |
| 285 | .matchIPSrc(route.source().toIpPrefix()); |
| 286 | |
| 287 | SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder() |
| 288 | .appId(appId) |
| 289 | .selector(selector.build()) |
| 290 | .treatment(treatment) |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 291 | .filteredIngressPoint(new FilteredConnectPoint(ingressPoint)); |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 292 | |
| 293 | // allowing intent to be pushed without egress points means we can drop packets. |
Ray Milkey | 4f7e363 | 2019-02-19 15:35:20 -0800 | [diff] [blame] | 294 | if (!filteredEgressPoints.isEmpty()) { |
| 295 | builder.filteredEgressPoints(filteredEgressPoints); |
Julian Lawrence | 5173188 | 2016-01-19 17:24:15 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | SinglePointToMultiPointIntent intent = builder.build(); |
| 299 | intentService.submit(intent); |
| 300 | |
| 301 | return intent.key(); |
| 302 | } |
| 303 | |
| 304 | public void withdrawAllIntents() { |
| 305 | for (Map.Entry<McastRoute, Key> entry : intentHashMap.entrySet()) { |
| 306 | withdrawIntent(entry.getValue()); |
| 307 | } |
| 308 | intentHashMap.clear(); |
| 309 | } |
| 310 | |
| 311 | public void withdrawIntent(Key key) { |
| 312 | if (key == null) { |
| 313 | // nothing to withdraw |
| 314 | return; |
| 315 | } |
| 316 | Intent intent = intentService.getIntent(key); |
| 317 | intentService.withdraw(intent); |
| 318 | } |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame] | 319 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 320 | } |