alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.Ip4Address; |
| 31 | import org.onlab.packet.IPv4; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 39 | import org.onosproject.net.mcast.MulticastRouteService; |
| 40 | import org.onosproject.net.mcast.McastRoute; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 41 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 42 | import org.onosproject.net.packet.InboundPacket; |
| 43 | import org.onosproject.net.packet.OutboundPacket; |
| 44 | import org.onosproject.net.packet.PacketContext; |
| 45 | import org.onosproject.net.packet.PacketPriority; |
| 46 | import org.onosproject.net.packet.PacketProcessor; |
| 47 | import org.onosproject.net.packet.PacketService; |
| 48 | import org.slf4j.Logger; |
| 49 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 50 | import java.util.ArrayList; |
| 51 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 52 | /** |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 53 | * The multicast forwarding component. This component is responsible for |
| 54 | * handling live multicast traffic by modifying multicast state and forwarding |
| 55 | * packets that do not yet have state installed. |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | public class McastForwarding { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | private final IpPrefix mcast = IpPrefix.valueOf("224.0.0.0/4"); |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected PacketService packetService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected CoreService coreService; |
| 68 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected MulticastRouteService mcastRouteManager; |
| 71 | |
| 72 | |
| 73 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 74 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 75 | private static ApplicationId appId; |
| 76 | |
| 77 | /** |
| 78 | * Active MulticastForwardingIntent. |
| 79 | */ |
| 80 | @Activate |
| 81 | public void activate() { |
| 82 | appId = coreService.registerApplication("org.onosproject.mfwd"); |
| 83 | |
| 84 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 85 | |
| 86 | // Build a traffic selector for all multicast traffic |
| 87 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 88 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 89 | selector.matchIPDst(mcast); |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 90 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 91 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 92 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Deactivate Multicast Forwarding Intent. |
| 98 | */ |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
| 101 | packetService.removeProcessor(processor); |
| 102 | processor = null; |
| 103 | log.info("Stopped"); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the application ID, used by the McastIntentManager. |
| 108 | * |
| 109 | * @return the application ID |
| 110 | */ |
| 111 | public static ApplicationId getAppId() { |
| 112 | return appId; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Packet processor responsible for forwarding packets along their paths. |
| 117 | */ |
| 118 | private class ReactivePacketProcessor implements PacketProcessor { |
| 119 | |
| 120 | /** |
| 121 | * Process incoming packets. |
| 122 | * |
| 123 | * @param context packet processing context |
| 124 | */ |
| 125 | @Override |
| 126 | public void process(PacketContext context) { |
| 127 | // Stop processing if the packet has been handled, since we |
| 128 | // can't do any more to it. |
| 129 | if (context.isHandled()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | InboundPacket pkt = context.inPacket(); |
| 134 | Ethernet ethPkt = pkt.parsed(); |
| 135 | |
| 136 | if (ethPkt == null) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4 && |
| 141 | ethPkt.getEtherType() != Ethernet.TYPE_IPV6) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) { |
| 146 | // Ignore ipv6 at the moment. |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 151 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 152 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 153 | |
| 154 | log.debug("Packet ({}, {}) has been punted\n" + |
| 155 | "\tingress port: {}\n", |
| 156 | saddr.toString(), |
| 157 | gaddr.toString(), |
| 158 | context.inPacket().receivedFrom().toString()); |
| 159 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 160 | // Don't allow PIM/IGMP packets to be handled here. |
| 161 | byte proto = ip.getProtocol(); |
| 162 | if (proto == IPv4.PROTOCOL_PIM || proto == IPv4.PROTOCOL_IGMP) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | IpPrefix spfx = IpPrefix.valueOf(saddr, 32); |
| 167 | IpPrefix gpfx = IpPrefix.valueOf(gaddr, 32); |
| 168 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 169 | // TODO do we want to add a type for Mcast? |
| 170 | McastRoute mRoute = new McastRoute(spfx, gpfx, McastRoute.Type.STATIC); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 171 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 172 | ConnectPoint ingress = mcastRouteManager.fetchSource(mRoute); |
| 173 | |
| 174 | // An ingress port already exists. Log error. |
| 175 | if (ingress != null) { |
| 176 | log.error(McastForwarding.class.getSimpleName() + " received packet which already has a route."); |
| 177 | return; |
| 178 | } else { |
| 179 | //add ingress port |
| 180 | mcastRouteManager.addSource(mRoute, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 183 | ArrayList<ConnectPoint> egressList = (ArrayList<ConnectPoint>) mcastRouteManager.fetchSinks(mRoute); |
| 184 | //If there are no egress ports set return, otherwise forward the packets to their expected port. |
| 185 | if (egressList.size() == 0) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 189 | // Send the pack out each of the egress devices & port |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 190 | forwardPacketToDst(context, egressList); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Forward the packet to it's multicast destinations. |
| 196 | * |
| 197 | * @param context The packet context |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 198 | * @param egressList The list of egress ports which the multicast packet is intended for. |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 199 | */ |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 200 | private void forwardPacketToDst(PacketContext context, ArrayList<ConnectPoint> egressList) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 201 | |
| 202 | // Send the pack out each of the respective egress ports |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 203 | for (ConnectPoint egress : egressList) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 204 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 205 | .setOutput(egress.port()).build(); |
| 206 | |
| 207 | OutboundPacket packet = new DefaultOutboundPacket( |
| 208 | egress.deviceId(), |
| 209 | treatment, |
| 210 | context.inPacket().unparsed()); |
| 211 | |
| 212 | packetService.emit(packet); |
| 213 | } |
| 214 | } |
Julian Lawrence | fa790f6 | 2016-01-07 17:18:30 -0800 | [diff] [blame^] | 215 | |
| 216 | public static McastRoute createStaticRoute(String source, String group) { |
| 217 | checkNotNull(source, "Must provide a source"); |
| 218 | checkNotNull(group, "Must provide a group"); |
| 219 | IpPrefix ipSource = IpPrefix.valueOf(source); |
| 220 | IpPrefix ipGroup = IpPrefix.valueOf(group); |
| 221 | return createStaticcreateRoute(ipSource, ipGroup); |
| 222 | } |
| 223 | |
| 224 | public static McastRoute createStaticcreateRoute(IpPrefix source, IpPrefix group) { |
| 225 | checkNotNull(source, "Must provide a source"); |
| 226 | checkNotNull(group, "Must provide a group"); |
| 227 | McastRoute.Type type = McastRoute.Type.STATIC; |
| 228 | return new McastRoute(source, group, type); |
| 229 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 230 | } |