blob: f6be85784967b9935dfe6ea25695707ac39038fc [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) {
Jonathan Hart9ad777f2016-02-19 12:44:36 -0800182 componentConfigService.registerProperties(getClass());
Jonathan Hart81d73102016-02-19 10:32:05 -0800183 modified(context);
184
alshabibeff00542015-09-23 13:22:33 -0700185 appId = coreService.registerApplication("org.onosproject.igmp");
186
187 packetService.addProcessor(processor, PacketProcessor.director(1));
188
alshabibfe69e9a2016-02-11 17:31:36 -0800189 networkConfig.registerConfigFactory(configFactory);
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800190 networkConfig.registerConfigFactory(ssmTranslateConfigFactory);
alshabibfe69e9a2016-02-11 17:31:36 -0800191 networkConfig.addListener(configListener);
192
alshabib03adb492016-02-01 17:25:00 -0800193 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
alshabib79e52872015-12-07 16:01:01 -0800194 subject -> {
alshabib03adb492016-02-01 17:25:00 -0800195 AccessDeviceConfig config = networkConfig.getConfig(subject,
196 AccessDeviceConfig.class);
alshabib79e52872015-12-07 16:01:01 -0800197 if (config != null) {
alshabib03adb492016-02-01 17:25:00 -0800198 AccessDeviceData data = config.getOlt();
199 oltData.put(data.deviceId(), data);
alshabibfe69e9a2016-02-11 17:31:36 -0800200
alshabib79e52872015-12-07 16:01:01 -0800201 }
202 }
203 );
alshabibeff00542015-09-23 13:22:33 -0700204
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800205 IgmpSsmTranslateConfig ssmTranslateConfig =
206 networkConfig.getConfig(appId, IgmpSsmTranslateConfig.class);
207
208 if (ssmTranslateConfig != null) {
209 Collection<McastRoute> translations = ssmTranslateConfig.getSsmTranslations();
210 for (McastRoute route : translations) {
211 ssmTranslateTable.put(route.group(), route.source());
212 }
213 }
214
alshabibfe69e9a2016-02-11 17:31:36 -0800215 oltData.keySet().stream()
216 .flatMap(did -> deviceService.getPorts(did).stream())
217 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
218 .filter(p -> p.isEnabled())
219 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
220
alshabib03adb492016-02-01 17:25:00 -0800221 deviceService.addListener(deviceListener);
222
Jonathan Hart81d73102016-02-19 10:32:05 -0800223 restartQueryTask();
alshabib42d32eb2016-02-14 19:53:42 -0800224
alshabibeff00542015-09-23 13:22:33 -0700225 log.info("Started");
226 }
227
228 @Deactivate
229 public void deactivate() {
230 packetService.removeProcessor(processor);
231 processor = null;
alshabib03adb492016-02-01 17:25:00 -0800232 deviceService.removeListener(deviceListener);
alshabibfe69e9a2016-02-11 17:31:36 -0800233 networkConfig.removeListener(configListener);
234 networkConfig.unregisterConfigFactory(configFactory);
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800235 networkConfig.unregisterConfigFactory(ssmTranslateConfigFactory);
alshabib42d32eb2016-02-14 19:53:42 -0800236 queryTask.cancel(true);
237 queryService.shutdownNow();
Jonathan Hart81d73102016-02-19 10:32:05 -0800238 componentConfigService.unregisterProperties(getClass(), false);
alshabibeff00542015-09-23 13:22:33 -0700239 log.info("Stopped");
240 }
241
Jonathan Hart81d73102016-02-19 10:32:05 -0800242 @Modified
243 protected void modified(ComponentContext context) {
244 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
245
246 String strQueryPeriod = Tools.get(properties, "queryPeriod");
247 String strResponseCode = Tools.get(properties, "maxRespCode");
248 try {
249 byte newMaxRespCode = Byte.parseByte(strResponseCode);
250 if (maxRespCode != newMaxRespCode) {
251 maxRespCode = newMaxRespCode;
252 queryPacket = buildQueryPacket();
253 }
254
255 int newQueryPeriod = Integer.parseInt(strQueryPeriod);
256 if (newQueryPeriod != queryPeriod) {
257 queryPeriod = newQueryPeriod;
258 restartQueryTask();
259 }
260
261 } catch (NumberFormatException e) {
262 log.warn("Error parsing config input", e);
263 }
264
265 log.info("queryPeriod set to {}", queryPeriod);
266 log.info("maxRespCode set to {}", maxRespCode);
267 }
268
269 private void restartQueryTask() {
270 if (queryTask != null) {
271 queryTask.cancel(true);
272 }
273 queryPacket = buildQueryPacket();
274 queryTask = queryService.scheduleWithFixedDelay(
275 SafeRecurringTask.wrap(this::querySubscribers),
276 0,
277 queryPeriod,
278 TimeUnit.SECONDS);
279 }
280
alshabib03adb492016-02-01 17:25:00 -0800281 private void processFilterObjective(DeviceId devId, Port port, boolean remove) {
alshabib79e52872015-12-07 16:01:01 -0800282
alshabib03adb492016-02-01 17:25:00 -0800283 //TODO migrate to packet requests when packet service uses filtering objectives
284 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
285
286 builder = remove ? builder.deny() : builder.permit();
287
288 FilteringObjective igmp = builder
289 .withKey(Criteria.matchInPort(port.number()))
290 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
291 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
292 .withMeta(DefaultTrafficTreatment.builder()
293 .setOutput(PortNumber.CONTROLLER).build())
294 .fromApp(appId)
alshabib1aa58142016-02-17 15:37:56 -0800295 .withPriority(10000)
alshabib03adb492016-02-01 17:25:00 -0800296 .add(new ObjectiveContext() {
297 @Override
298 public void onSuccess(Objective objective) {
299 log.info("Igmp filter for {} on {} installed.",
300 devId, port);
301 }
302
303 @Override
304 public void onError(Objective objective, ObjectiveError error) {
305 log.info("Igmp filter for {} on {} failed because {}.",
306 devId, port, error);
307 }
308 });
309
310 flowObjectiveService.filter(devId, igmp);
311 }
312
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800313 private void processMembership(IGMP pkt, ConnectPoint location) {
314 pkt.getGroups().forEach(group -> {
315
316 if (!(group instanceof IGMPMembership)) {
317 log.warn("Wrong group type in IGMP membership");
318 return;
319 }
320
321 IGMPMembership membership = (IGMPMembership) group;
322
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800323 // TODO allow pulling source from IGMP packet
Jonathan Hartdd7e3d52016-02-18 15:56:22 -0800324 IpAddress source = ssmTranslateTable.get(group.getGaddr());
325 if (source == null) {
326 log.warn("No source found in SSM translate table for {}", group.getGaddr());
327 return;
Jonathan Hart5e8689c2016-02-16 13:06:26 -0800328 }
329
330 McastRoute route = new McastRoute(source,
331 group.getGaddr(),
332 McastRoute.Type.IGMP);
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800333
334 if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE ||
335 membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) {
336
Jonathan Hartdd7e3d52016-02-18 15:56:22 -0800337 multicastService.removeSink(route, location);
338 // TODO remove route if all sinks are gone
339 } else if (membership.getRecordType() == IGMPMembership.MODE_IS_EXCLUDE ||
340 membership.getRecordType() == IGMPMembership.CHANGE_TO_EXCLUDE_MODE) {
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800341
342 multicastService.add(route);
343 multicastService.addSink(route, location);
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800344 }
345
346 });
347 }
348
alshabib42d32eb2016-02-14 19:53:42 -0800349 private ByteBuffer buildQueryPacket() {
350 IGMP igmp = new IGMP();
351 igmp.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY);
352 igmp.setMaxRespCode(maxRespCode);
353
354 IGMPQuery query = new IGMPQuery(IpAddress.valueOf("0.0.0.0"), 0);
355 igmp.addGroup(query);
356
357 IPv4 ip = new IPv4();
358 ip.setDestinationAddress(DEST_IP);
359 ip.setProtocol(IPv4.PROTOCOL_IGMP);
360 ip.setSourceAddress("192.168.1.1");
361 ip.setTtl((byte) 1);
362 ip.setPayload(igmp);
363
364 Ethernet eth = new Ethernet();
365 eth.setDestinationMACAddress(DEST_MAC);
366 eth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
367 eth.setEtherType(Ethernet.TYPE_IPV4);
368
369 eth.setPayload(ip);
370
371 return ByteBuffer.wrap(eth.serialize());
372 }
373
374 private void querySubscribers() {
375 oltData.keySet().stream()
376 .flatMap(did -> deviceService.getPorts(did).stream())
377 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
378 .filter(p -> p.isEnabled())
379 .forEach(p -> {
380 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
381 .setOutput(p.number()).build();
382 packetService.emit(new DefaultOutboundPacket((DeviceId) p.element().id(),
383 treatment, queryPacket));
384 });
385 }
386
alshabibeff00542015-09-23 13:22:33 -0700387 /**
388 * Packet processor responsible for handling IGMP packets.
389 */
alshabib79e52872015-12-07 16:01:01 -0800390 private class IgmpPacketProcessor implements PacketProcessor {
alshabibeff00542015-09-23 13:22:33 -0700391
392 @Override
393 public void process(PacketContext context) {
394 // Stop processing if the packet has been handled, since we
395 // can't do any more to it.
396 if (context.isHandled()) {
397 return;
398 }
399
400 InboundPacket pkt = context.inPacket();
401 Ethernet ethPkt = pkt.parsed();
402 if (ethPkt == null) {
403 return;
404 }
405
406 /*
407 * IPv6 MLD packets are handled by ICMP6. We'll only deal
408 * with IPv4.
409 */
410 if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) {
411 return;
412 }
413
414 IPv4 ip = (IPv4) ethPkt.getPayload();
415 IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress());
416 IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress());
alshabib79e52872015-12-07 16:01:01 -0800417 log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr,
418 context.inPacket().receivedFrom());
419
alshabibeff00542015-09-23 13:22:33 -0700420
421 if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700422 log.debug("IGMP Picked up a non IGMP packet.");
alshabibeff00542015-09-23 13:22:33 -0700423 return;
424 }
425
alshabib79e52872015-12-07 16:01:01 -0800426 IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR);
alshabibeff00542015-09-23 13:22:33 -0700427 if (!mcast.contains(gaddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700428 log.debug("IGMP Picked up a non multicast packet.");
alshabibeff00542015-09-23 13:22:33 -0700429 return;
430 }
431
432 if (mcast.contains(saddr)) {
Rusty Eddy158d5d82015-10-12 16:59:04 -0700433 log.debug("IGMP Picked up a packet with a multicast source address.");
alshabibeff00542015-09-23 13:22:33 -0700434 return;
435 }
alshabibeff00542015-09-23 13:22:33 -0700436
437 IGMP igmp = (IGMP) ip.getPayload();
438 switch (igmp.getIgmpType()) {
439
440 case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800441 processMembership(igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700442 break;
443
444 case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY:
alshabib42d32eb2016-02-14 19:53:42 -0800445 log.debug("Received a membership query {} from {}",
446 igmp, pkt.receivedFrom());
alshabibeff00542015-09-23 13:22:33 -0700447 break;
448
449 case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT:
450 case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT:
451 case IGMP.TYPE_IGMPV2_LEAVE_GROUP:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800452 log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: {}",
alshabib42d32eb2016-02-14 19:53:42 -0800453 igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700454 break;
alshabibeff00542015-09-23 13:22:33 -0700455 default:
Jonathan Hart6ccfc5a2016-02-12 19:26:02 -0800456 log.debug("Unknown IGMP message type: {}", igmp.getIgmpType());
alshabibeff00542015-09-23 13:22:33 -0700457 break;
458 }
459 }
460 }
alshabib79e52872015-12-07 16:01:01 -0800461
alshabib79e52872015-12-07 16:01:01 -0800462
alshabib03adb492016-02-01 17:25:00 -0800463 private class InternalDeviceListener implements DeviceListener {
464 @Override
465 public void event(DeviceEvent event) {
alshabib1aa58142016-02-17 15:37:56 -0800466 DeviceId devId = event.subject().id();
alshabib03adb492016-02-01 17:25:00 -0800467 switch (event.type()) {
468
469 case DEVICE_ADDED:
470 case DEVICE_UPDATED:
471 case DEVICE_REMOVED:
472 case DEVICE_SUSPENDED:
473 case DEVICE_AVAILABILITY_CHANGED:
474 case PORT_STATS_UPDATED:
475 break;
476 case PORT_ADDED:
alshabib1aa58142016-02-17 15:37:56 -0800477 if (!oltData.get(devId).uplink().equals(event.port().number()) &&
478 event.port().isEnabled()) {
alshabib03adb492016-02-01 17:25:00 -0800479 processFilterObjective(event.subject().id(), event.port(), false);
480 }
481 break;
482 case PORT_UPDATED:
alshabib1aa58142016-02-17 15:37:56 -0800483 if (oltData.get(devId).uplink().equals(event.port().number())) {
484 break;
485 }
alshabib03adb492016-02-01 17:25:00 -0800486 if (event.port().isEnabled()) {
487 processFilterObjective(event.subject().id(), event.port(), false);
488 } else {
489 processFilterObjective(event.subject().id(), event.port(), true);
490 }
491 break;
492 case PORT_REMOVED:
alshabibfe69e9a2016-02-11 17:31:36 -0800493 processFilterObjective(event.subject().id(), event.port(), true);
alshabib03adb492016-02-01 17:25:00 -0800494 break;
495 default:
496 log.warn("Unknown device event {}", event.type());
497 break;
498 }
alshabib03adb492016-02-01 17:25:00 -0800499 }
500
501 @Override
502 public boolean isRelevant(DeviceEvent event) {
503 return oltData.containsKey(event.subject().id());
504 }
alshabib79e52872015-12-07 16:01:01 -0800505 }
alshabibfe69e9a2016-02-11 17:31:36 -0800506
507 private class InternalNetworkConfigListener implements NetworkConfigListener {
508 @Override
509 public void event(NetworkConfigEvent event) {
510 switch (event.type()) {
511
512 case CONFIG_ADDED:
513 case CONFIG_UPDATED:
514 if (event.configClass().equals(CONFIG_CLASS)) {
515 AccessDeviceConfig config =
516 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
517 if (config != null) {
518 oltData.put(config.getOlt().deviceId(), config.getOlt());
519 provisionDefaultFlows((DeviceId) event.subject());
520 }
521 }
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800522 if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) {
523 IgmpSsmTranslateConfig config =
524 networkConfig.getConfig((ApplicationId) event.subject(),
525 SSM_TRANSLATE_CONFIG_CLASS);
526
527 if (config != null) {
528 ssmTranslateTable.clear();
529 config.getSsmTranslations().forEach(
530 route -> ssmTranslateTable.put(route.group(), route.source()));
531 }
532 }
alshabibfe69e9a2016-02-11 17:31:36 -0800533 break;
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800534 case CONFIG_REGISTERED:
alshabibfe69e9a2016-02-11 17:31:36 -0800535 case CONFIG_UNREGISTERED:
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800536 break;
alshabibfe69e9a2016-02-11 17:31:36 -0800537 case CONFIG_REMOVED:
Jonathan Hart3a8896b2016-02-16 13:06:26 -0800538 if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) {
539 ssmTranslateTable.clear();
540 }
alshabibfe69e9a2016-02-11 17:31:36 -0800541 default:
542 break;
543 }
544 }
545 }
546
547 private void provisionDefaultFlows(DeviceId deviceId) {
548 List<Port> ports = deviceService.getPorts(deviceId);
549
550 ports.stream()
551 .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number()))
552 .filter(p -> p.isEnabled())
553 .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false));
554
555 }
alshabibeff00542015-09-23 13:22:33 -0700556}