blob: 19bd2211644dc811e13cb811a16c06feaab39880 [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;
Jonathan Hart81d73102016-02-19 10:32:05 -080021import org.apache.felix.scr.annotations.Modified;
alshabib79e52872015-12-07 16:01:01 -080022import org.apache.felix.scr.annotations.Property;
alshabibeff00542015-09-23 13:22:33 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib03adb492016-02-01 17:25:00 -080025import org.onlab.packet.EthType;
alshabibeff00542015-09-23 13:22:33 -070026import org.onlab.packet.Ethernet;
alshabib03adb492016-02-01 17:25:00 -080027import org.onlab.packet.IGMP;
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -080028import org.onlab.packet.IGMPMembership;
alshabib42d32eb2016-02-14 19:53:42 -080029import org.onlab.packet.IGMPQuery;
alshabibeff00542015-09-23 13:22:33 -070030import org.onlab.packet.IPv4;
31import org.onlab.packet.Ip4Address;
32import org.onlab.packet.IpAddress;
33import org.onlab.packet.IpPrefix;
alshabib42d32eb2016-02-14 19:53:42 -080034import org.onlab.util.SafeRecurringTask;
Jonathan Hart81d73102016-02-19 10:32:05 -080035import org.onlab.util.Tools;
36import org.onosproject.cfg.ComponentConfigService;
alshabibeff00542015-09-23 13:22:33 -070037import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
alshabib79e52872015-12-07 16:01:01 -080039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.DeviceId;
alshabib03adb492016-02-01 17:25:00 -080041import org.onosproject.net.Port;
42import org.onosproject.net.PortNumber;
alshabibfe69e9a2016-02-11 17:31:36 -080043import org.onosproject.net.config.ConfigFactory;
44import org.onosproject.net.config.NetworkConfigEvent;
45import org.onosproject.net.config.NetworkConfigListener;
alshabib79e52872015-12-07 16:01:01 -080046import org.onosproject.net.config.NetworkConfigRegistry;
alshabibfe69e9a2016-02-11 17:31:36 -080047import org.onosproject.net.config.basics.SubjectFactories;
alshabib03adb492016-02-01 17:25:00 -080048import org.onosproject.net.device.DeviceEvent;
49import org.onosproject.net.device.DeviceListener;
50import org.onosproject.net.device.DeviceService;
51import org.onosproject.net.flow.DefaultTrafficTreatment;
alshabib42d32eb2016-02-14 19:53:42 -080052import org.onosproject.net.flow.TrafficTreatment;
alshabib03adb492016-02-01 17:25:00 -080053import org.onosproject.net.flow.criteria.Criteria;
54import org.onosproject.net.flowobjective.DefaultFilteringObjective;
55import org.onosproject.net.flowobjective.FilteringObjective;
56import org.onosproject.net.flowobjective.FlowObjectiveService;
57import org.onosproject.net.flowobjective.Objective;
58import org.onosproject.net.flowobjective.ObjectiveContext;
59import org.onosproject.net.flowobjective.ObjectiveError;
alshabib79e52872015-12-07 16:01:01 -080060import org.onosproject.net.mcast.McastRoute;
61import org.onosproject.net.mcast.MulticastRouteService;
alshabib42d32eb2016-02-14 19:53:42 -080062import org.onosproject.net.packet.DefaultOutboundPacket;
alshabibeff00542015-09-23 13:22:33 -070063import org.onosproject.net.packet.InboundPacket;
64import org.onosproject.net.packet.PacketContext;
alshabibeff00542015-09-23 13:22:33 -070065import org.onosproject.net.packet.PacketProcessor;
66import org.onosproject.net.packet.PacketService;
alshabib03adb492016-02-01 17:25:00 -080067import org.onosproject.olt.AccessDeviceConfig;
68import org.onosproject.olt.AccessDeviceData;
Jonathan Hart81d73102016-02-19 10:32:05 -080069import org.osgi.service.component.ComponentContext;
alshabibeff00542015-09-23 13:22:33 -070070import org.slf4j.Logger;
71
alshabib42d32eb2016-02-14 19:53:42 -080072import java.nio.ByteBuffer;
Jonathan Hart5e8689c2016-02-16 13:06:26 -080073import java.util.Collection;
Jonathan Hart81d73102016-02-19 10:32:05 -080074import java.util.Dictionary;
alshabibfe69e9a2016-02-11 17:31:36 -080075import java.util.List;
alshabib03adb492016-02-01 17:25:00 -080076import java.util.Map;
Jonathan Hart81d73102016-02-19 10:32:05 -080077import java.util.Properties;
alshabib03adb492016-02-01 17:25:00 -080078import java.util.concurrent.ConcurrentHashMap;
alshabib42d32eb2016-02-14 19:53:42 -080079import java.util.concurrent.Executors;
80import java.util.concurrent.ScheduledExecutorService;
81import java.util.concurrent.ScheduledFuture;
82import java.util.concurrent.TimeUnit;
alshabib03adb492016-02-01 17:25:00 -080083
alshabib42d32eb2016-02-14 19:53:42 -080084import static org.onlab.util.Tools.groupedThreads;
alshabib03adb492016-02-01 17:25:00 -080085import static org.slf4j.LoggerFactory.getLogger;
alshabib79e52872015-12-07 16:01:01 -080086
alshabibeff00542015-09-23 13:22:33 -070087/**
88 * Internet Group Management Protocol.
89 */
90@Component(immediate = true)
alshabib79e52872015-12-07 16:01:01 -080091public class IgmpSnoop {
alshabib42d32eb2016-02-14 19:53:42 -080092
alshabibeff00542015-09-23 13:22:33 -070093 private final Logger log = getLogger(getClass());
94
alshabib42d32eb2016-02-14 19:53:42 -080095 private static final String DEST_MAC = "01:00:5E:00:00:01";
96 private static final String DEST_IP = "224.0.0.1";
97
98 private static final int DEFAULT_QUERY_PERIOD_SECS = 60;
Jonathan Hart81d73102016-02-19 10:32:05 -080099 private static final byte DEFAULT_IGMP_RESP_CODE = 100;
alshabib79e52872015-12-07 16:01:01 -0800100 private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4";
101
102 @Property(name = "multicastAddress",
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800103 label = "Define the multicast base range to listen to")
alshabib79e52872015-12-07 16:01:01 -0800104 private String multicastAddress = DEFAULT_MCAST_ADDR;
105
alshabib42d32eb2016-02-14 19:53:42 -0800106 @Property(name = "queryPeriod", intValue = DEFAULT_QUERY_PERIOD_SECS,
107 label = "Delay in seconds between successive query runs")
108 private int queryPeriod = DEFAULT_QUERY_PERIOD_SECS;
109
110 @Property(name = "maxRespCode", byteValue = DEFAULT_IGMP_RESP_CODE,
111 label = "Maximum time allowed before sending a responding report")
112 private byte maxRespCode = DEFAULT_IGMP_RESP_CODE;
113
alshabibeff00542015-09-23 13:22:33 -0700114 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib03adb492016-02-01 17:25:00 -0800115 protected FlowObjectiveService flowObjectiveService;
116
117 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibeff00542015-09-23 13:22:33 -0700118 protected PacketService packetService;
119
120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected CoreService coreService;
122
alshabib79e52872015-12-07 16:01:01 -0800123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
124 protected NetworkConfigRegistry networkConfig;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart81d73102016-02-19 10:32:05 -0800127 protected ComponentConfigService componentConfigService;
128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib79e52872015-12-07 16:01:01 -0800130 protected MulticastRouteService multicastService;
131
alshabib03adb492016-02-01 17:25:00 -0800132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected DeviceService deviceService;
134
alshabib42d32eb2016-02-14 19:53:42 -0800135
136 private ScheduledFuture<?> queryTask;
137 private final ScheduledExecutorService queryService =
138 Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/igmp-query",
139 "membership-query"));
140
alshabib03adb492016-02-01 17:25:00 -0800141 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
142
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800143 private Map<IpAddress, IpAddress> ssmTranslateTable = new ConcurrentHashMap<>();
144
alshabib03adb492016-02-01 17:25:00 -0800145 private DeviceListener deviceListener = new InternalDeviceListener();
alshabib79e52872015-12-07 16:01:01 -0800146 private IgmpPacketProcessor processor = new IgmpPacketProcessor();
alshabibeff00542015-09-23 13:22:33 -0700147 private static ApplicationId appId;
148
alshabibfe69e9a2016-02-11 17:31:36 -0800149 private InternalNetworkConfigListener configListener =
150 new InternalNetworkConfigListener();
151
152 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
153 AccessDeviceConfig.class;
154
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800155 private static final Class<IgmpSsmTranslateConfig> SSM_TRANSLATE_CONFIG_CLASS =
156 IgmpSsmTranslateConfig.class;
157
alshabibfe69e9a2016-02-11 17:31:36 -0800158 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
159 new ConfigFactory<DeviceId, AccessDeviceConfig>(
160 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
161 @Override
162 public AccessDeviceConfig createConfig() {
163 return new AccessDeviceConfig();
164 }
165 };
166
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800167 private ConfigFactory<ApplicationId, IgmpSsmTranslateConfig> ssmTranslateConfigFactory =
168 new ConfigFactory<ApplicationId, IgmpSsmTranslateConfig>(
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800169 SubjectFactories.APP_SUBJECT_FACTORY, SSM_TRANSLATE_CONFIG_CLASS, "ssmTranslate", true) {
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800170 @Override
171 public IgmpSsmTranslateConfig createConfig() {
172 return new IgmpSsmTranslateConfig();
173 }
174 };
175
alshabibfe69e9a2016-02-11 17:31:36 -0800176
alshabib42d32eb2016-02-14 19:53:42 -0800177 private ByteBuffer queryPacket;
178
179
alshabibeff00542015-09-23 13:22:33 -0700180 @Activate
Jonathan Hart81d73102016-02-19 10:32:05 -0800181 public void activate(ComponentContext context) {
182 modified(context);
183
alshabibeff00542015-09-23 13:22:33 -0700184 appId = coreService.registerApplication("org.onosproject.igmp");
185
Jonathan Hart81d73102016-02-19 10:32:05 -0800186 componentConfigService.registerProperties(getClass());
187
alshabibeff00542015-09-23 13:22:33 -0700188 packetService.addProcessor(processor, PacketProcessor.director(1));
189
alshabibfe69e9a2016-02-11 17:31:36 -0800190 networkConfig.registerConfigFactory(configFactory);
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800191 networkConfig.registerConfigFactory(ssmTranslateConfigFactory);
alshabibfe69e9a2016-02-11 17:31:36 -0800192 networkConfig.addListener(configListener);
193
alshabib03adb492016-02-01 17:25:00 -0800194 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
alshabib79e52872015-12-07 16:01:01 -0800195 subject -> {
alshabib03adb492016-02-01 17:25:00 -0800196 AccessDeviceConfig config = networkConfig.getConfig(subject,
197 AccessDeviceConfig.class);
alshabib79e52872015-12-07 16:01:01 -0800198 if (config != null) {
alshabib03adb492016-02-01 17:25:00 -0800199 AccessDeviceData data = config.getOlt();
200 oltData.put(data.deviceId(), data);
alshabibfe69e9a2016-02-11 17:31:36 -0800201
alshabib79e52872015-12-07 16:01:01 -0800202 }
203 }
204 );
alshabibeff00542015-09-23 13:22:33 -0700205
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800206 IgmpSsmTranslateConfig ssmTranslateConfig =
207 networkConfig.getConfig(appId, IgmpSsmTranslateConfig.class);
208
209 if (ssmTranslateConfig != null) {
210 Collection<McastRoute> translations = ssmTranslateConfig.getSsmTranslations();
211 for (McastRoute route : translations) {
212 ssmTranslateTable.put(route.group(), route.source());
213 }
214 }
215
alshabibfe69e9a2016-02-11 17:31:36 -0800216 oltData.keySet().stream()
217 .flatMap(did -> deviceService.getPorts(did).stream())
218 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
219 .filter(p -> p.isEnabled())
220 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
221
alshabib03adb492016-02-01 17:25:00 -0800222 deviceService.addListener(deviceListener);
223
Jonathan Hart81d73102016-02-19 10:32:05 -0800224 restartQueryTask();
alshabib42d32eb2016-02-14 19:53:42 -0800225
alshabibeff00542015-09-23 13:22:33 -0700226 log.info("Started");
227 }
228
229 @Deactivate
230 public void deactivate() {
231 packetService.removeProcessor(processor);
232 processor = null;
alshabib03adb492016-02-01 17:25:00 -0800233 deviceService.removeListener(deviceListener);
alshabibfe69e9a2016-02-11 17:31:36 -0800234 networkConfig.removeListener(configListener);
235 networkConfig.unregisterConfigFactory(configFactory);
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800236 networkConfig.unregisterConfigFactory(ssmTranslateConfigFactory);
alshabib42d32eb2016-02-14 19:53:42 -0800237 queryTask.cancel(true);
238 queryService.shutdownNow();
Jonathan Hart81d73102016-02-19 10:32:05 -0800239 componentConfigService.unregisterProperties(getClass(), false);
alshabibeff00542015-09-23 13:22:33 -0700240 log.info("Stopped");
241 }
242
Jonathan Hart81d73102016-02-19 10:32:05 -0800243 @Modified
244 protected void modified(ComponentContext context) {
245 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
246
247 String strQueryPeriod = Tools.get(properties, "queryPeriod");
248 String strResponseCode = Tools.get(properties, "maxRespCode");
249 try {
250 byte newMaxRespCode = Byte.parseByte(strResponseCode);
251 if (maxRespCode != newMaxRespCode) {
252 maxRespCode = newMaxRespCode;
253 queryPacket = buildQueryPacket();
254 }
255
256 int newQueryPeriod = Integer.parseInt(strQueryPeriod);
257 if (newQueryPeriod != queryPeriod) {
258 queryPeriod = newQueryPeriod;
259 restartQueryTask();
260 }
261
262 } catch (NumberFormatException e) {
263 log.warn("Error parsing config input", e);
264 }
265
266 log.info("queryPeriod set to {}", queryPeriod);
267 log.info("maxRespCode set to {}", maxRespCode);
268 }
269
270 private void restartQueryTask() {
271 if (queryTask != null) {
272 queryTask.cancel(true);
273 }
274 queryPacket = buildQueryPacket();
275 queryTask = queryService.scheduleWithFixedDelay(
276 SafeRecurringTask.wrap(this::querySubscribers),
277 0,
278 queryPeriod,
279 TimeUnit.SECONDS);
280 }
281
alshabib03adb492016-02-01 17:25:00 -0800282 private void processFilterObjective(DeviceId devId, Port port, boolean remove) {
alshabib79e52872015-12-07 16:01:01 -0800283
alshabib03adb492016-02-01 17:25:00 -0800284 //TODO migrate to packet requests when packet service uses filtering objectives
285 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
286
287 builder = remove ? builder.deny() : builder.permit();
288
289 FilteringObjective igmp = builder
290 .withKey(Criteria.matchInPort(port.number()))
291 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
292 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
293 .withMeta(DefaultTrafficTreatment.builder()
294 .setOutput(PortNumber.CONTROLLER).build())
295 .fromApp(appId)
alshabib1aa58142016-02-17 15:37:56 -0800296 .withPriority(10000)
alshabib03adb492016-02-01 17:25:00 -0800297 .add(new ObjectiveContext() {
298 @Override
299 public void onSuccess(Objective objective) {
300 log.info("Igmp filter for {} on {} installed.",
301 devId, port);
302 }
303
304 @Override
305 public void onError(Objective objective, ObjectiveError error) {
306 log.info("Igmp filter for {} on {} failed because {}.",
307 devId, port, error);
308 }
309 });
310
311 flowObjectiveService.filter(devId, igmp);
312 }
313
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800314 private void processMembership(IGMP pkt, ConnectPoint location) {
315 pkt.getGroups().forEach(group -> {
316
317 if (!(group instanceof IGMPMembership)) {
318 log.warn("Wrong group type in IGMP membership");
319 return;
320 }
321
322 IGMPMembership membership = (IGMPMembership) group;
323
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800324 // TODO allow pulling source from IGMP packet
Jonathan Hartdd7e3d52016-02-18 15:56:22 -0800325 IpAddress source = ssmTranslateTable.get(group.getGaddr());
326 if (source == null) {
327 log.warn("No source found in SSM translate table for {}", group.getGaddr());
328 return;
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800329 }
330
331 McastRoute route = new McastRoute(source,
332 group.getGaddr(),
333 McastRoute.Type.IGMP);
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800334
335 if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE ||
336 membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) {
337
Jonathan Hartdd7e3d52016-02-18 15:56:22 -0800338 multicastService.removeSink(route, location);
339 // TODO remove route if all sinks are gone
340 } else if (membership.getRecordType() == IGMPMembership.MODE_IS_EXCLUDE ||
341 membership.getRecordType() == IGMPMembership.CHANGE_TO_EXCLUDE_MODE) {
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800342
343 multicastService.add(route);
344 multicastService.addSink(route, location);
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800345 }
346
347 });
348 }
349
alshabib42d32eb2016-02-14 19:53:42 -0800350 private ByteBuffer buildQueryPacket() {
351 IGMP igmp = new IGMP();
352 igmp.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY);
353 igmp.setMaxRespCode(maxRespCode);
354
355 IGMPQuery query = new IGMPQuery(IpAddress.valueOf("0.0.0.0"), 0);
356 igmp.addGroup(query);
357
358 IPv4 ip = new IPv4();
359 ip.setDestinationAddress(DEST_IP);
360 ip.setProtocol(IPv4.PROTOCOL_IGMP);
361 ip.setSourceAddress("192.168.1.1");
362 ip.setTtl((byte) 1);
363 ip.setPayload(igmp);
364
365 Ethernet eth = new Ethernet();
366 eth.setDestinationMACAddress(DEST_MAC);
367 eth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
368 eth.setEtherType(Ethernet.TYPE_IPV4);
369
370 eth.setPayload(ip);
371
372 return ByteBuffer.wrap(eth.serialize());
373 }
374
375 private void querySubscribers() {
376 oltData.keySet().stream()
377 .flatMap(did -> deviceService.getPorts(did).stream())
378 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
379 .filter(p -> p.isEnabled())
380 .forEach(p -> {
381 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
382 .setOutput(p.number()).build();
383 packetService.emit(new DefaultOutboundPacket((DeviceId) p.element().id(),
384 treatment, queryPacket));
385 });
386 }
387
alshabibeff00542015-09-23 13:22:33 -0700388 /**
389 * Packet processor responsible for handling IGMP packets.
390 */
alshabib79e52872015-12-07 16:01:01 -0800391 private class IgmpPacketProcessor implements PacketProcessor {
alshabibeff00542015-09-23 13:22:33 -0700392
393 @Override
394 public void process(PacketContext context) {
395 // Stop processing if the packet has been handled, since we
396 // can't do any more to it.
397 if (context.isHandled()) {
398 return;
399 }
400
401 InboundPacket pkt = context.inPacket();
402 Ethernet ethPkt = pkt.parsed();
403 if (ethPkt == null) {
404 return;
405 }
406
407 /*
408 * IPv6 MLD packets are handled by ICMP6. We'll only deal
409 * with IPv4.
410 */
411 if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) {
412 return;
413 }
414
415 IPv4 ip = (IPv4) ethPkt.getPayload();
416 IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress());
417 IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress());
alshabib79e52872015-12-07 16:01:01 -0800418 log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr,
419 context.inPacket().receivedFrom());
420
alshabibeff00542015-09-23 13:22:33 -0700421
422 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700423 log.debug("IGMP Picked up a non IGMP packet.");
alshabibeff00542015-09-23 13:22:33 -0700424 return;
425 }
426
alshabib79e52872015-12-07 16:01:01 -0800427 IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR);
alshabibeff00542015-09-23 13:22:33 -0700428 if (!mcast.contains(gaddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700429 log.debug("IGMP Picked up a non multicast packet.");
alshabibeff00542015-09-23 13:22:33 -0700430 return;
431 }
432
433 if (mcast.contains(saddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700434 log.debug("IGMP Picked up a packet with a multicast source address.");
alshabibeff00542015-09-23 13:22:33 -0700435 return;
436 }
alshabibeff00542015-09-23 13:22:33 -0700437
438 IGMP igmp = (IGMP) ip.getPayload();
439 switch (igmp.getIgmpType()) {
440
441 case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800442 processMembership(igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700443 break;
444
445 case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:
alshabib42d32eb2016-02-14 19:53:42 -0800446 log.debug("Received a membership query {} from {}",
447 igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700448 break;
449
450 case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT:
451 case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT:
452 case IGMP.TYPE_IGMPV2_LEAVE_GROUP:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800453 log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: {}",
alshabib42d32eb2016-02-14 19:53:42 -0800454 igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700455 break;
alshabibeff00542015-09-23 13:22:33 -0700456 default:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800457 log.debug("Unknown IGMP message type: {}", igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700458 break;
459 }
460 }
461 }
alshabib79e52872015-12-07 16:01:01 -0800462
alshabib79e52872015-12-07 16:01:01 -0800463
alshabib03adb492016-02-01 17:25:00 -0800464 private class InternalDeviceListener implements DeviceListener {
465 @Override
466 public void event(DeviceEvent event) {
alshabib1aa58142016-02-17 15:37:56 -0800467 DeviceId devId = event.subject().id();
alshabib03adb492016-02-01 17:25:00 -0800468 switch (event.type()) {
469
470 case DEVICE_ADDED:
471 case DEVICE_UPDATED:
472 case DEVICE_REMOVED:
473 case DEVICE_SUSPENDED:
474 case DEVICE_AVAILABILITY_CHANGED:
475 case PORT_STATS_UPDATED:
476 break;
477 case PORT_ADDED:
alshabib1aa58142016-02-17 15:37:56 -0800478 if (!oltData.get(devId).uplink().equals(event.port().number()) &&
479 event.port().isEnabled()) {
alshabib03adb492016-02-01 17:25:00 -0800480 processFilterObjective(event.subject().id(), event.port(), false);
481 }
482 break;
483 case PORT_UPDATED:
alshabib1aa58142016-02-17 15:37:56 -0800484 if (oltData.get(devId).uplink().equals(event.port().number())) {
485 break;
486 }
alshabib03adb492016-02-01 17:25:00 -0800487 if (event.port().isEnabled()) {
488 processFilterObjective(event.subject().id(), event.port(), false);
489 } else {
490 processFilterObjective(event.subject().id(), event.port(), true);
491 }
492 break;
493 case PORT_REMOVED:
alshabibfe69e9a2016-02-11 17:31:36 -0800494 processFilterObjective(event.subject().id(), event.port(), true);
alshabib03adb492016-02-01 17:25:00 -0800495 break;
496 default:
497 log.warn("Unknown device event {}", event.type());
498 break;
499 }
alshabib03adb492016-02-01 17:25:00 -0800500 }
501
502 @Override
503 public boolean isRelevant(DeviceEvent event) {
504 return oltData.containsKey(event.subject().id());
505 }
alshabib79e52872015-12-07 16:01:01 -0800506 }
alshabibfe69e9a2016-02-11 17:31:36 -0800507
508 private class InternalNetworkConfigListener implements NetworkConfigListener {
509 @Override
510 public void event(NetworkConfigEvent event) {
511 switch (event.type()) {
512
513 case CONFIG_ADDED:
514 case CONFIG_UPDATED:
515 if (event.configClass().equals(CONFIG_CLASS)) {
516 AccessDeviceConfig config =
517 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
518 if (config != null) {
519 oltData.put(config.getOlt().deviceId(), config.getOlt());
520 provisionDefaultFlows((DeviceId) event.subject());
521 }
522 }
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800523 if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) {
524 IgmpSsmTranslateConfig config =
525 networkConfig.getConfig((ApplicationId) event.subject(),
526 SSM_TRANSLATE_CONFIG_CLASS);
527
528 if (config != null) {
529 ssmTranslateTable.clear();
530 config.getSsmTranslations().forEach(
531 route -> ssmTranslateTable.put(route.group(), route.source()));
532 }
533 }
alshabibfe69e9a2016-02-11 17:31:36 -0800534 break;
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800535 case CONFIG_REGISTERED:
alshabibfe69e9a2016-02-11 17:31:36 -0800536 case CONFIG_UNREGISTERED:
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800537 break;
alshabibfe69e9a2016-02-11 17:31:36 -0800538 case CONFIG_REMOVED:
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800539 if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) {
540 ssmTranslateTable.clear();
541 }
alshabibfe69e9a2016-02-11 17:31:36 -0800542 default:
543 break;
544 }
545 }
546 }
547
548 private void provisionDefaultFlows(DeviceId deviceId) {
549 List<Port> ports = deviceService.getPorts(deviceId);
550
551 ports.stream()
552 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
553 .filter(p -> p.isEnabled())
554 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
555
556 }
alshabibeff00542015-09-23 13:22:33 -0700557}