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 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 16 | package org.onosproject.igmp; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 17 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Property; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 24 | import org.onlab.packet.EthType; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IGMP; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IPv4; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.IpPrefix; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.PortNumber; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 37 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 42 | import org.onosproject.net.flow.criteria.Criteria; |
| 43 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 44 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 45 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 46 | import org.onosproject.net.flowobjective.Objective; |
| 47 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 48 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 49 | import org.onosproject.net.mcast.McastRoute; |
| 50 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 51 | import org.onosproject.net.packet.InboundPacket; |
| 52 | import org.onosproject.net.packet.PacketContext; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 53 | import org.onosproject.net.packet.PacketProcessor; |
| 54 | import org.onosproject.net.packet.PacketService; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 55 | import org.onosproject.olt.AccessDeviceConfig; |
| 56 | import org.onosproject.olt.AccessDeviceData; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 59 | import java.util.Map; |
| 60 | import java.util.concurrent.ConcurrentHashMap; |
| 61 | |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 63 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Internet Group Management Protocol. |
| 66 | */ |
| 67 | @Component(immediate = true) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 68 | public class IgmpSnoop { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 71 | private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4"; |
| 72 | |
| 73 | @Property(name = "multicastAddress", |
| 74 | label = "Define the multicast base raneg to listen to") |
| 75 | private String multicastAddress = DEFAULT_MCAST_ADDR; |
| 76 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 78 | protected FlowObjectiveService flowObjectiveService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 81 | protected PacketService packetService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected CoreService coreService; |
| 85 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected NetworkConfigRegistry networkConfig; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected MulticastRouteService multicastService; |
| 91 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected DeviceService deviceService; |
| 94 | |
| 95 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 96 | |
| 97 | private DeviceListener deviceListener = new InternalDeviceListener(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 98 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 99 | private static ApplicationId appId; |
| 100 | |
| 101 | @Activate |
| 102 | public void activate() { |
| 103 | appId = coreService.registerApplication("org.onosproject.igmp"); |
| 104 | |
| 105 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 106 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 107 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 108 | subject -> { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 109 | AccessDeviceConfig config = networkConfig.getConfig(subject, |
| 110 | AccessDeviceConfig.class); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 111 | if (config != null) { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 112 | AccessDeviceData data = config.getOlt(); |
| 113 | oltData.put(data.deviceId(), data); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | ); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 117 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 118 | deviceService.addListener(deviceListener); |
| 119 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 120 | log.info("Started"); |
| 121 | } |
| 122 | |
| 123 | @Deactivate |
| 124 | public void deactivate() { |
| 125 | packetService.removeProcessor(processor); |
| 126 | processor = null; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 127 | deviceService.removeListener(deviceListener); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 128 | log.info("Stopped"); |
| 129 | } |
| 130 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 131 | private void processFilterObjective(DeviceId devId, Port port, boolean remove) { |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 132 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 133 | //TODO migrate to packet requests when packet service uses filtering objectives |
| 134 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 135 | |
| 136 | builder = remove ? builder.deny() : builder.permit(); |
| 137 | |
| 138 | FilteringObjective igmp = builder |
| 139 | .withKey(Criteria.matchInPort(port.number())) |
| 140 | .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType())) |
| 141 | .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP)) |
| 142 | .withMeta(DefaultTrafficTreatment.builder() |
| 143 | .setOutput(PortNumber.CONTROLLER).build()) |
| 144 | .fromApp(appId) |
| 145 | .withPriority(1000) |
| 146 | .add(new ObjectiveContext() { |
| 147 | @Override |
| 148 | public void onSuccess(Objective objective) { |
| 149 | log.info("Igmp filter for {} on {} installed.", |
| 150 | devId, port); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onError(Objective objective, ObjectiveError error) { |
| 155 | log.info("Igmp filter for {} on {} failed because {}.", |
| 156 | devId, port, error); |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | flowObjectiveService.filter(devId, igmp); |
| 161 | } |
| 162 | |
| 163 | private void processQuery(IGMP pkt, ConnectPoint location) { |
| 164 | pkt.getGroups().forEach(group -> group.getSources().forEach(src -> { |
| 165 | |
| 166 | McastRoute route = new McastRoute(src, |
| 167 | group.getGaddr(), |
| 168 | McastRoute.Type.IGMP); |
| 169 | multicastService.add(route); |
| 170 | multicastService.addSink(route, location); |
| 171 | |
| 172 | })); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 173 | } |
| 174 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Packet processor responsible for handling IGMP packets. |
| 177 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 178 | private class IgmpPacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 179 | |
| 180 | @Override |
| 181 | public void process(PacketContext context) { |
| 182 | // Stop processing if the packet has been handled, since we |
| 183 | // can't do any more to it. |
| 184 | if (context.isHandled()) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | InboundPacket pkt = context.inPacket(); |
| 189 | Ethernet ethPkt = pkt.parsed(); |
| 190 | if (ethPkt == null) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * IPv6 MLD packets are handled by ICMP6. We'll only deal |
| 196 | * with IPv4. |
| 197 | */ |
| 198 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 203 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
| 204 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 205 | log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr, |
| 206 | context.inPacket().receivedFrom()); |
| 207 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 208 | |
| 209 | if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 210 | log.debug("IGMP Picked up a non IGMP packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 214 | IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 215 | if (!mcast.contains(gaddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 216 | log.debug("IGMP Picked up a non multicast packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | if (mcast.contains(saddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 221 | log.debug("IGMP Picked up a packet with a multicast source address."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 222 | return; |
| 223 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 224 | |
| 225 | IGMP igmp = (IGMP) ip.getPayload(); |
| 226 | switch (igmp.getIgmpType()) { |
| 227 | |
| 228 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
| 229 | IGMPProcessMembership.processMembership(igmp, pkt.receivedFrom()); |
| 230 | break; |
| 231 | |
| 232 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 233 | processQuery(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 234 | break; |
| 235 | |
| 236 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 237 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 238 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
| 239 | log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: " + |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 240 | igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | default: |
| 244 | log.debug("Unkown IGMP message type: " + igmp.getIgmpType()); |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 249 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 250 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 251 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 252 | private class InternalDeviceListener implements DeviceListener { |
| 253 | @Override |
| 254 | public void event(DeviceEvent event) { |
| 255 | switch (event.type()) { |
| 256 | |
| 257 | case DEVICE_ADDED: |
| 258 | case DEVICE_UPDATED: |
| 259 | case DEVICE_REMOVED: |
| 260 | case DEVICE_SUSPENDED: |
| 261 | case DEVICE_AVAILABILITY_CHANGED: |
| 262 | case PORT_STATS_UPDATED: |
| 263 | break; |
| 264 | case PORT_ADDED: |
| 265 | if (event.port().isEnabled()) { |
| 266 | processFilterObjective(event.subject().id(), event.port(), false); |
| 267 | } |
| 268 | break; |
| 269 | case PORT_UPDATED: |
| 270 | if (event.port().isEnabled()) { |
| 271 | processFilterObjective(event.subject().id(), event.port(), false); |
| 272 | } else { |
| 273 | processFilterObjective(event.subject().id(), event.port(), true); |
| 274 | } |
| 275 | break; |
| 276 | case PORT_REMOVED: |
| 277 | processFilterObjective(event.subject().id(), event.port(), false); |
| 278 | break; |
| 279 | default: |
| 280 | log.warn("Unknown device event {}", event.type()); |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public boolean isRelevant(DeviceEvent event) { |
| 288 | return oltData.containsKey(event.subject().id()); |
| 289 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 290 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 291 | } |