blob: a0e2aeb7c5780a54bb9ebcacb3aa11dc40537870 [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;
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -080027import org.onlab.packet.IGMPMembership;
alshabibeff00542015-09-23 13:22:33 -070028import org.onlab.packet.IPv4;
29import org.onlab.packet.Ip4Address;
30import org.onlab.packet.IpAddress;
31import org.onlab.packet.IpPrefix;
alshabibeff00542015-09-23 13:22:33 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
alshabib79e52872015-12-07 16:01:01 -080034import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DeviceId;
alshabib03adb492016-02-01 17:25:00 -080036import org.onosproject.net.Port;
37import org.onosproject.net.PortNumber;
alshabibfe69e9a2016-02-11 17:31:36 -080038import org.onosproject.net.config.ConfigFactory;
39import org.onosproject.net.config.NetworkConfigEvent;
40import org.onosproject.net.config.NetworkConfigListener;
alshabib79e52872015-12-07 16:01:01 -080041import org.onosproject.net.config.NetworkConfigRegistry;
alshabibfe69e9a2016-02-11 17:31:36 -080042import org.onosproject.net.config.basics.SubjectFactories;
alshabib03adb492016-02-01 17:25:00 -080043import org.onosproject.net.device.DeviceEvent;
44import org.onosproject.net.device.DeviceListener;
45import org.onosproject.net.device.DeviceService;
46import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flowobjective.DefaultFilteringObjective;
49import org.onosproject.net.flowobjective.FilteringObjective;
50import org.onosproject.net.flowobjective.FlowObjectiveService;
51import org.onosproject.net.flowobjective.Objective;
52import org.onosproject.net.flowobjective.ObjectiveContext;
53import org.onosproject.net.flowobjective.ObjectiveError;
alshabib79e52872015-12-07 16:01:01 -080054import org.onosproject.net.mcast.McastRoute;
55import org.onosproject.net.mcast.MulticastRouteService;
alshabibeff00542015-09-23 13:22:33 -070056import org.onosproject.net.packet.InboundPacket;
57import org.onosproject.net.packet.PacketContext;
alshabibeff00542015-09-23 13:22:33 -070058import org.onosproject.net.packet.PacketProcessor;
59import org.onosproject.net.packet.PacketService;
alshabib03adb492016-02-01 17:25:00 -080060import org.onosproject.olt.AccessDeviceConfig;
61import org.onosproject.olt.AccessDeviceData;
alshabibeff00542015-09-23 13:22:33 -070062import org.slf4j.Logger;
63
alshabibfe69e9a2016-02-11 17:31:36 -080064import java.util.List;
alshabib03adb492016-02-01 17:25:00 -080065import java.util.Map;
66import java.util.concurrent.ConcurrentHashMap;
67
68import static org.slf4j.LoggerFactory.getLogger;
alshabib79e52872015-12-07 16:01:01 -080069
alshabibeff00542015-09-23 13:22:33 -070070/**
71 * Internet Group Management Protocol.
72 */
73@Component(immediate = true)
alshabib79e52872015-12-07 16:01:01 -080074public class IgmpSnoop {
alshabibeff00542015-09-23 13:22:33 -070075 private final Logger log = getLogger(getClass());
76
alshabib79e52872015-12-07 16:01:01 -080077 private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4";
78
79 @Property(name = "multicastAddress",
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -080080 label = "Define the multicast base range to listen to")
alshabib79e52872015-12-07 16:01:01 -080081 private String multicastAddress = DEFAULT_MCAST_ADDR;
82
alshabibeff00542015-09-23 13:22:33 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib03adb492016-02-01 17:25:00 -080084 protected FlowObjectiveService flowObjectiveService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibeff00542015-09-23 13:22:33 -070087 protected PacketService packetService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected CoreService coreService;
91
alshabib79e52872015-12-07 16:01:01 -080092 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected NetworkConfigRegistry networkConfig;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected MulticastRouteService multicastService;
97
alshabib03adb492016-02-01 17:25:00 -080098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected DeviceService deviceService;
100
101 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
102
103 private DeviceListener deviceListener = new InternalDeviceListener();
alshabib79e52872015-12-07 16:01:01 -0800104 private IgmpPacketProcessor processor = new IgmpPacketProcessor();
alshabibeff00542015-09-23 13:22:33 -0700105 private static ApplicationId appId;
106
alshabibfe69e9a2016-02-11 17:31:36 -0800107 private InternalNetworkConfigListener configListener =
108 new InternalNetworkConfigListener();
109
110 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
111 AccessDeviceConfig.class;
112
113 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
114 new ConfigFactory<DeviceId, AccessDeviceConfig>(
115 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
116 @Override
117 public AccessDeviceConfig createConfig() {
118 return new AccessDeviceConfig();
119 }
120 };
121
122
alshabibeff00542015-09-23 13:22:33 -0700123 @Activate
124 public void activate() {
125 appId = coreService.registerApplication("org.onosproject.igmp");
126
127 packetService.addProcessor(processor, PacketProcessor.director(1));
128
alshabibfe69e9a2016-02-11 17:31:36 -0800129 networkConfig.registerConfigFactory(configFactory);
130 networkConfig.addListener(configListener);
131
alshabib03adb492016-02-01 17:25:00 -0800132 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
alshabib79e52872015-12-07 16:01:01 -0800133 subject -> {
alshabib03adb492016-02-01 17:25:00 -0800134 AccessDeviceConfig config = networkConfig.getConfig(subject,
135 AccessDeviceConfig.class);
alshabib79e52872015-12-07 16:01:01 -0800136 if (config != null) {
alshabib03adb492016-02-01 17:25:00 -0800137 AccessDeviceData data = config.getOlt();
138 oltData.put(data.deviceId(), data);
alshabibfe69e9a2016-02-11 17:31:36 -0800139
alshabib79e52872015-12-07 16:01:01 -0800140 }
141 }
142 );
alshabibeff00542015-09-23 13:22:33 -0700143
alshabibfe69e9a2016-02-11 17:31:36 -0800144 oltData.keySet().stream()
145 .flatMap(did -> deviceService.getPorts(did).stream())
146 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
147 .filter(p -> p.isEnabled())
148 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
149
alshabib03adb492016-02-01 17:25:00 -0800150 deviceService.addListener(deviceListener);
151
alshabibeff00542015-09-23 13:22:33 -0700152 log.info("Started");
153 }
154
155 @Deactivate
156 public void deactivate() {
157 packetService.removeProcessor(processor);
158 processor = null;
alshabib03adb492016-02-01 17:25:00 -0800159 deviceService.removeListener(deviceListener);
alshabibfe69e9a2016-02-11 17:31:36 -0800160 networkConfig.removeListener(configListener);
161 networkConfig.unregisterConfigFactory(configFactory);
alshabibeff00542015-09-23 13:22:33 -0700162 log.info("Stopped");
163 }
164
alshabib03adb492016-02-01 17:25:00 -0800165 private void processFilterObjective(DeviceId devId, Port port, boolean remove) {
alshabib79e52872015-12-07 16:01:01 -0800166
alshabib03adb492016-02-01 17:25:00 -0800167 //TODO migrate to packet requests when packet service uses filtering objectives
168 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
169
170 builder = remove ? builder.deny() : builder.permit();
171
172 FilteringObjective igmp = builder
173 .withKey(Criteria.matchInPort(port.number()))
174 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
175 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
176 .withMeta(DefaultTrafficTreatment.builder()
177 .setOutput(PortNumber.CONTROLLER).build())
178 .fromApp(appId)
179 .withPriority(1000)
180 .add(new ObjectiveContext() {
181 @Override
182 public void onSuccess(Objective objective) {
183 log.info("Igmp filter for {} on {} installed.",
184 devId, port);
185 }
186
187 @Override
188 public void onError(Objective objective, ObjectiveError error) {
189 log.info("Igmp filter for {} on {} failed because {}.",
190 devId, port, error);
191 }
192 });
193
194 flowObjectiveService.filter(devId, igmp);
195 }
196
197 private void processQuery(IGMP pkt, ConnectPoint location) {
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800198 // TODO is this the right thing to do for a query?
alshabib03adb492016-02-01 17:25:00 -0800199 pkt.getGroups().forEach(group -> group.getSources().forEach(src -> {
200
201 McastRoute route = new McastRoute(src,
202 group.getGaddr(),
203 McastRoute.Type.IGMP);
204 multicastService.add(route);
205 multicastService.addSink(route, location);
206
207 }));
alshabib79e52872015-12-07 16:01:01 -0800208 }
209
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800210 private void processMembership(IGMP pkt, ConnectPoint location) {
211 pkt.getGroups().forEach(group -> {
212
213 if (!(group instanceof IGMPMembership)) {
214 log.warn("Wrong group type in IGMP membership");
215 return;
216 }
217
218 IGMPMembership membership = (IGMPMembership) group;
219
220 McastRoute route = new McastRoute(IpAddress.valueOf("0.0.0.0"),
221 group.getGaddr(),
222 McastRoute.Type.IGMP);
223
224 if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE ||
225 membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) {
226
227
228 multicastService.add(route);
229 multicastService.addSink(route, location);
230
231 } else if (membership.getRecordType() == IGMPMembership.MODE_IS_EXCLUDE ||
232 membership.getRecordType() == IGMPMembership.CHANGE_TO_EXCLUDE_MODE) {
233 multicastService.removeSink(route, location);
234 // TODO remove route if all sinks are gone
235 }
236
237 });
238 }
239
alshabibeff00542015-09-23 13:22:33 -0700240 /**
241 * Packet processor responsible for handling IGMP packets.
242 */
alshabib79e52872015-12-07 16:01:01 -0800243 private class IgmpPacketProcessor implements PacketProcessor {
alshabibeff00542015-09-23 13:22:33 -0700244
245 @Override
246 public void process(PacketContext context) {
247 // Stop processing if the packet has been handled, since we
248 // can't do any more to it.
249 if (context.isHandled()) {
250 return;
251 }
252
253 InboundPacket pkt = context.inPacket();
254 Ethernet ethPkt = pkt.parsed();
255 if (ethPkt == null) {
256 return;
257 }
258
259 /*
260 * IPv6 MLD packets are handled by ICMP6. We'll only deal
261 * with IPv4.
262 */
263 if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) {
264 return;
265 }
266
267 IPv4 ip = (IPv4) ethPkt.getPayload();
268 IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress());
269 IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress());
alshabib79e52872015-12-07 16:01:01 -0800270 log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr,
271 context.inPacket().receivedFrom());
272
alshabibeff00542015-09-23 13:22:33 -0700273
274 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700275 log.debug("IGMP Picked up a non IGMP packet.");
alshabibeff00542015-09-23 13:22:33 -0700276 return;
277 }
278
alshabib79e52872015-12-07 16:01:01 -0800279 IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR);
alshabibeff00542015-09-23 13:22:33 -0700280 if (!mcast.contains(gaddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700281 log.debug("IGMP Picked up a non multicast packet.");
alshabibeff00542015-09-23 13:22:33 -0700282 return;
283 }
284
285 if (mcast.contains(saddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700286 log.debug("IGMP Picked up a packet with a multicast source address.");
alshabibeff00542015-09-23 13:22:33 -0700287 return;
288 }
alshabibeff00542015-09-23 13:22:33 -0700289
290 IGMP igmp = (IGMP) ip.getPayload();
291 switch (igmp.getIgmpType()) {
292
293 case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800294 processMembership(igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700295 break;
296
297 case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:
alshabib79e52872015-12-07 16:01:01 -0800298 processQuery(igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700299 break;
300
301 case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT:
302 case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT:
303 case IGMP.TYPE_IGMPV2_LEAVE_GROUP:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800304 log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: {}",
alshabib79e52872015-12-07 16:01:01 -0800305 igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700306 break;
alshabibeff00542015-09-23 13:22:33 -0700307 default:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800308 log.debug("Unknown IGMP message type: {}", igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700309 break;
310 }
311 }
312 }
alshabib79e52872015-12-07 16:01:01 -0800313
alshabib79e52872015-12-07 16:01:01 -0800314
alshabib03adb492016-02-01 17:25:00 -0800315 private class InternalDeviceListener implements DeviceListener {
316 @Override
317 public void event(DeviceEvent event) {
318 switch (event.type()) {
319
320 case DEVICE_ADDED:
321 case DEVICE_UPDATED:
322 case DEVICE_REMOVED:
323 case DEVICE_SUSPENDED:
324 case DEVICE_AVAILABILITY_CHANGED:
325 case PORT_STATS_UPDATED:
326 break;
327 case PORT_ADDED:
328 if (event.port().isEnabled()) {
329 processFilterObjective(event.subject().id(), event.port(), false);
330 }
331 break;
332 case PORT_UPDATED:
333 if (event.port().isEnabled()) {
334 processFilterObjective(event.subject().id(), event.port(), false);
335 } else {
336 processFilterObjective(event.subject().id(), event.port(), true);
337 }
338 break;
339 case PORT_REMOVED:
alshabibfe69e9a2016-02-11 17:31:36 -0800340 processFilterObjective(event.subject().id(), event.port(), true);
alshabib03adb492016-02-01 17:25:00 -0800341 break;
342 default:
343 log.warn("Unknown device event {}", event.type());
344 break;
345 }
alshabib03adb492016-02-01 17:25:00 -0800346 }
347
348 @Override
349 public boolean isRelevant(DeviceEvent event) {
350 return oltData.containsKey(event.subject().id());
351 }
alshabib79e52872015-12-07 16:01:01 -0800352 }
alshabibfe69e9a2016-02-11 17:31:36 -0800353
354 private class InternalNetworkConfigListener implements NetworkConfigListener {
355 @Override
356 public void event(NetworkConfigEvent event) {
357 switch (event.type()) {
358
359 case CONFIG_ADDED:
360 case CONFIG_UPDATED:
361 if (event.configClass().equals(CONFIG_CLASS)) {
362 AccessDeviceConfig config =
363 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
364 if (config != null) {
365 oltData.put(config.getOlt().deviceId(), config.getOlt());
366 provisionDefaultFlows((DeviceId) event.subject());
367 }
368 }
369 break;
370 case CONFIG_UNREGISTERED:
371 case CONFIG_REMOVED:
372 default:
373 break;
374 }
375 }
376 }
377
378 private void provisionDefaultFlows(DeviceId deviceId) {
379 List<Port> ports = deviceService.getPorts(deviceId);
380
381 ports.stream()
382 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
383 .filter(p -> p.isEnabled())
384 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
385
386 }
alshabibeff00542015-09-23 13:22:33 -0700387}