blob: c0af0bf178b43224fbdddb7a9e0ee114dcab38de [file] [log] [blame]
alshabibeff00542015-09-23 13:22:33 -07001/*
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 */
alshabib79e52872015-12-07 16:01:01 -080016package org.onosproject.igmp;
alshabibeff00542015-09-23 13:22:33 -070017
alshabibeff00542015-09-23 13:22:33 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
alshabib79e52872015-12-07 16:01:01 -080021import org.apache.felix.scr.annotations.Property;
alshabibeff00542015-09-23 13:22:33 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib03adb492016-02-01 17:25:00 -080024import org.onlab.packet.EthType;
alshabibeff00542015-09-23 13:22:33 -070025import org.onlab.packet.Ethernet;
alshabib03adb492016-02-01 17:25:00 -080026import org.onlab.packet.IGMP;
alshabibeff00542015-09-23 13:22:33 -070027import org.onlab.packet.IPv4;
28import org.onlab.packet.Ip4Address;
29import org.onlab.packet.IpAddress;
30import org.onlab.packet.IpPrefix;
alshabibeff00542015-09-23 13:22:33 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
alshabib79e52872015-12-07 16:01:01 -080033import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
alshabib03adb492016-02-01 17:25:00 -080035import org.onosproject.net.Port;
36import org.onosproject.net.PortNumber;
alshabib79e52872015-12-07 16:01:01 -080037import org.onosproject.net.config.NetworkConfigRegistry;
alshabib03adb492016-02-01 17:25:00 -080038import org.onosproject.net.device.DeviceEvent;
39import org.onosproject.net.device.DeviceListener;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.flow.DefaultTrafficTreatment;
42import org.onosproject.net.flow.criteria.Criteria;
43import org.onosproject.net.flowobjective.DefaultFilteringObjective;
44import org.onosproject.net.flowobjective.FilteringObjective;
45import org.onosproject.net.flowobjective.FlowObjectiveService;
46import org.onosproject.net.flowobjective.Objective;
47import org.onosproject.net.flowobjective.ObjectiveContext;
48import org.onosproject.net.flowobjective.ObjectiveError;
alshabib79e52872015-12-07 16:01:01 -080049import org.onosproject.net.mcast.McastRoute;
50import org.onosproject.net.mcast.MulticastRouteService;
alshabibeff00542015-09-23 13:22:33 -070051import org.onosproject.net.packet.InboundPacket;
52import org.onosproject.net.packet.PacketContext;
alshabibeff00542015-09-23 13:22:33 -070053import org.onosproject.net.packet.PacketProcessor;
54import org.onosproject.net.packet.PacketService;
alshabib03adb492016-02-01 17:25:00 -080055import org.onosproject.olt.AccessDeviceConfig;
56import org.onosproject.olt.AccessDeviceData;
alshabibeff00542015-09-23 13:22:33 -070057import org.slf4j.Logger;
58
alshabib03adb492016-02-01 17:25:00 -080059import java.util.Map;
60import java.util.concurrent.ConcurrentHashMap;
61
62import static org.slf4j.LoggerFactory.getLogger;
alshabib79e52872015-12-07 16:01:01 -080063
alshabibeff00542015-09-23 13:22:33 -070064/**
65 * Internet Group Management Protocol.
66 */
67@Component(immediate = true)
alshabib79e52872015-12-07 16:01:01 -080068public class IgmpSnoop {
alshabibeff00542015-09-23 13:22:33 -070069 private final Logger log = getLogger(getClass());
70
alshabib79e52872015-12-07 16:01:01 -080071 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
alshabibeff00542015-09-23 13:22:33 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib03adb492016-02-01 17:25:00 -080078 protected FlowObjectiveService flowObjectiveService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibeff00542015-09-23 13:22:33 -070081 protected PacketService packetService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
alshabib79e52872015-12-07 16:01:01 -080086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected NetworkConfigRegistry networkConfig;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected MulticastRouteService multicastService;
91
alshabib03adb492016-02-01 17:25:00 -080092 @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();
alshabib79e52872015-12-07 16:01:01 -080098 private IgmpPacketProcessor processor = new IgmpPacketProcessor();
alshabibeff00542015-09-23 13:22:33 -070099 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
alshabib03adb492016-02-01 17:25:00 -0800107 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
alshabib79e52872015-12-07 16:01:01 -0800108 subject -> {
alshabib03adb492016-02-01 17:25:00 -0800109 AccessDeviceConfig config = networkConfig.getConfig(subject,
110 AccessDeviceConfig.class);
alshabib79e52872015-12-07 16:01:01 -0800111 if (config != null) {
alshabib03adb492016-02-01 17:25:00 -0800112 AccessDeviceData data = config.getOlt();
113 oltData.put(data.deviceId(), data);
alshabib79e52872015-12-07 16:01:01 -0800114 }
115 }
116 );
alshabibeff00542015-09-23 13:22:33 -0700117
alshabib03adb492016-02-01 17:25:00 -0800118 deviceService.addListener(deviceListener);
119
alshabibeff00542015-09-23 13:22:33 -0700120 log.info("Started");
121 }
122
123 @Deactivate
124 public void deactivate() {
125 packetService.removeProcessor(processor);
126 processor = null;
alshabib03adb492016-02-01 17:25:00 -0800127 deviceService.removeListener(deviceListener);
alshabibeff00542015-09-23 13:22:33 -0700128 log.info("Stopped");
129 }
130
alshabib03adb492016-02-01 17:25:00 -0800131 private void processFilterObjective(DeviceId devId, Port port, boolean remove) {
alshabib79e52872015-12-07 16:01:01 -0800132
alshabib03adb492016-02-01 17:25:00 -0800133 //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 }));
alshabib79e52872015-12-07 16:01:01 -0800173 }
174
alshabibeff00542015-09-23 13:22:33 -0700175 /**
176 * Packet processor responsible for handling IGMP packets.
177 */
alshabib79e52872015-12-07 16:01:01 -0800178 private class IgmpPacketProcessor implements PacketProcessor {
alshabibeff00542015-09-23 13:22:33 -0700179
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());
alshabib79e52872015-12-07 16:01:01 -0800205 log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr,
206 context.inPacket().receivedFrom());
207
alshabibeff00542015-09-23 13:22:33 -0700208
209 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700210 log.debug("IGMP Picked up a non IGMP packet.");
alshabibeff00542015-09-23 13:22:33 -0700211 return;
212 }
213
alshabib79e52872015-12-07 16:01:01 -0800214 IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR);
alshabibeff00542015-09-23 13:22:33 -0700215 if (!mcast.contains(gaddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700216 log.debug("IGMP Picked up a non multicast packet.");
alshabibeff00542015-09-23 13:22:33 -0700217 return;
218 }
219
220 if (mcast.contains(saddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700221 log.debug("IGMP Picked up a packet with a multicast source address.");
alshabibeff00542015-09-23 13:22:33 -0700222 return;
223 }
alshabibeff00542015-09-23 13:22:33 -0700224
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:
alshabib79e52872015-12-07 16:01:01 -0800233 processQuery(igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700234 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: " +
alshabib79e52872015-12-07 16:01:01 -0800240 igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700241 break;
242
243 default:
244 log.debug("Unkown IGMP message type: " + igmp.getIgmpType());
245 break;
246 }
247 }
248 }
alshabib79e52872015-12-07 16:01:01 -0800249
alshabib79e52872015-12-07 16:01:01 -0800250
alshabib79e52872015-12-07 16:01:01 -0800251
alshabib03adb492016-02-01 17:25:00 -0800252 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 }
alshabib79e52872015-12-07 16:01:01 -0800290 }
alshabibeff00542015-09-23 13:22:33 -0700291}