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; |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IGMPMembership; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IGMPQuery; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IPv4; |
| 30 | import org.onlab.packet.Ip4Address; |
| 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.IpPrefix; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 33 | import org.onlab.util.SafeRecurringTask; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DeviceId; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.PortNumber; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 40 | import org.onosproject.net.config.ConfigFactory; |
| 41 | import org.onosproject.net.config.NetworkConfigEvent; |
| 42 | import org.onosproject.net.config.NetworkConfigListener; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 43 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 44 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceEvent; |
| 46 | import org.onosproject.net.device.DeviceListener; |
| 47 | import org.onosproject.net.device.DeviceService; |
| 48 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.criteria.Criteria; |
| 51 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 52 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 53 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 54 | import org.onosproject.net.flowobjective.Objective; |
| 55 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 56 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 57 | import org.onosproject.net.mcast.McastRoute; |
| 58 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.DefaultOutboundPacket; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 60 | import org.onosproject.net.packet.InboundPacket; |
| 61 | import org.onosproject.net.packet.PacketContext; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 62 | import org.onosproject.net.packet.PacketProcessor; |
| 63 | import org.onosproject.net.packet.PacketService; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 64 | import org.onosproject.olt.AccessDeviceConfig; |
| 65 | import org.onosproject.olt.AccessDeviceData; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 68 | import java.nio.ByteBuffer; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 69 | import java.util.List; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 70 | import java.util.Map; |
| 71 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 72 | import java.util.concurrent.Executors; |
| 73 | import java.util.concurrent.ScheduledExecutorService; |
| 74 | import java.util.concurrent.ScheduledFuture; |
| 75 | import java.util.concurrent.TimeUnit; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 76 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 77 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 78 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 79 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Internet Group Management Protocol. |
| 82 | */ |
| 83 | @Component(immediate = true) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 84 | public class IgmpSnoop { |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 85 | |
| 86 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 87 | private final Logger log = getLogger(getClass()); |
| 88 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 89 | private static final String DEST_MAC = "01:00:5E:00:00:01"; |
| 90 | private static final String DEST_IP = "224.0.0.1"; |
| 91 | |
| 92 | private static final int DEFAULT_QUERY_PERIOD_SECS = 60; |
| 93 | private static final byte DEFAULT_IGMP_RESP_CODE = 0; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 94 | private static final String DEFAULT_MCAST_ADDR = "224.0.0.0/4"; |
| 95 | |
| 96 | @Property(name = "multicastAddress", |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 97 | label = "Define the multicast base range to listen to") |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 98 | private String multicastAddress = DEFAULT_MCAST_ADDR; |
| 99 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 100 | @Property(name = "queryPeriod", intValue = DEFAULT_QUERY_PERIOD_SECS, |
| 101 | label = "Delay in seconds between successive query runs") |
| 102 | private int queryPeriod = DEFAULT_QUERY_PERIOD_SECS; |
| 103 | |
| 104 | @Property(name = "maxRespCode", byteValue = DEFAULT_IGMP_RESP_CODE, |
| 105 | label = "Maximum time allowed before sending a responding report") |
| 106 | private byte maxRespCode = DEFAULT_IGMP_RESP_CODE; |
| 107 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 109 | protected FlowObjectiveService flowObjectiveService; |
| 110 | |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 112 | protected PacketService packetService; |
| 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected CoreService coreService; |
| 116 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected NetworkConfigRegistry networkConfig; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected MulticastRouteService multicastService; |
| 122 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected DeviceService deviceService; |
| 125 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 126 | |
| 127 | private ScheduledFuture<?> queryTask; |
| 128 | private final ScheduledExecutorService queryService = |
| 129 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/igmp-query", |
| 130 | "membership-query")); |
| 131 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 132 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 133 | |
| 134 | private DeviceListener deviceListener = new InternalDeviceListener(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 135 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 136 | private static ApplicationId appId; |
| 137 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 138 | private InternalNetworkConfigListener configListener = |
| 139 | new InternalNetworkConfigListener(); |
| 140 | |
| 141 | private static final Class<AccessDeviceConfig> CONFIG_CLASS = |
| 142 | AccessDeviceConfig.class; |
| 143 | |
| 144 | private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory = |
| 145 | new ConfigFactory<DeviceId, AccessDeviceConfig>( |
| 146 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") { |
| 147 | @Override |
| 148 | public AccessDeviceConfig createConfig() { |
| 149 | return new AccessDeviceConfig(); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 154 | private ByteBuffer queryPacket; |
| 155 | |
| 156 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 157 | @Activate |
| 158 | public void activate() { |
| 159 | appId = coreService.registerApplication("org.onosproject.igmp"); |
| 160 | |
| 161 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 162 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 163 | networkConfig.registerConfigFactory(configFactory); |
| 164 | networkConfig.addListener(configListener); |
| 165 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 166 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 167 | subject -> { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 168 | AccessDeviceConfig config = networkConfig.getConfig(subject, |
| 169 | AccessDeviceConfig.class); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 170 | if (config != null) { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 171 | AccessDeviceData data = config.getOlt(); |
| 172 | oltData.put(data.deviceId(), data); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 173 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | ); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 177 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 178 | oltData.keySet().stream() |
| 179 | .flatMap(did -> deviceService.getPorts(did).stream()) |
| 180 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 181 | .filter(p -> p.isEnabled()) |
| 182 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
| 183 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 184 | deviceService.addListener(deviceListener); |
| 185 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 186 | queryPacket = buildQueryPacket(); |
| 187 | |
| 188 | queryTask = queryService.scheduleWithFixedDelay( |
| 189 | SafeRecurringTask.wrap(this::querySubscribers), |
| 190 | 0, |
| 191 | queryPeriod, |
| 192 | TimeUnit.SECONDS); |
| 193 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 194 | log.info("Started"); |
| 195 | } |
| 196 | |
| 197 | @Deactivate |
| 198 | public void deactivate() { |
| 199 | packetService.removeProcessor(processor); |
| 200 | processor = null; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 201 | deviceService.removeListener(deviceListener); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 202 | networkConfig.removeListener(configListener); |
| 203 | networkConfig.unregisterConfigFactory(configFactory); |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 204 | queryTask.cancel(true); |
| 205 | queryService.shutdownNow(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 206 | log.info("Stopped"); |
| 207 | } |
| 208 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 209 | private void processFilterObjective(DeviceId devId, Port port, boolean remove) { |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 210 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 211 | //TODO migrate to packet requests when packet service uses filtering objectives |
| 212 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 213 | |
| 214 | builder = remove ? builder.deny() : builder.permit(); |
| 215 | |
| 216 | FilteringObjective igmp = builder |
| 217 | .withKey(Criteria.matchInPort(port.number())) |
| 218 | .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType())) |
| 219 | .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP)) |
| 220 | .withMeta(DefaultTrafficTreatment.builder() |
| 221 | .setOutput(PortNumber.CONTROLLER).build()) |
| 222 | .fromApp(appId) |
| 223 | .withPriority(1000) |
| 224 | .add(new ObjectiveContext() { |
| 225 | @Override |
| 226 | public void onSuccess(Objective objective) { |
| 227 | log.info("Igmp filter for {} on {} installed.", |
| 228 | devId, port); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void onError(Objective objective, ObjectiveError error) { |
| 233 | log.info("Igmp filter for {} on {} failed because {}.", |
| 234 | devId, port, error); |
| 235 | } |
| 236 | }); |
| 237 | |
| 238 | flowObjectiveService.filter(devId, igmp); |
| 239 | } |
| 240 | |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 241 | private void processMembership(IGMP pkt, ConnectPoint location) { |
| 242 | pkt.getGroups().forEach(group -> { |
| 243 | |
| 244 | if (!(group instanceof IGMPMembership)) { |
| 245 | log.warn("Wrong group type in IGMP membership"); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | IGMPMembership membership = (IGMPMembership) group; |
| 250 | |
| 251 | McastRoute route = new McastRoute(IpAddress.valueOf("0.0.0.0"), |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 252 | group.getGaddr(), |
| 253 | McastRoute.Type.IGMP); |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 254 | |
| 255 | if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE || |
| 256 | membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) { |
| 257 | |
| 258 | |
| 259 | multicastService.add(route); |
| 260 | multicastService.addSink(route, location); |
| 261 | |
| 262 | } else if (membership.getRecordType() == IGMPMembership.MODE_IS_EXCLUDE || |
| 263 | membership.getRecordType() == IGMPMembership.CHANGE_TO_EXCLUDE_MODE) { |
| 264 | multicastService.removeSink(route, location); |
| 265 | // TODO remove route if all sinks are gone |
| 266 | } |
| 267 | |
| 268 | }); |
| 269 | } |
| 270 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 271 | private ByteBuffer buildQueryPacket() { |
| 272 | IGMP igmp = new IGMP(); |
| 273 | igmp.setIgmpType(IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY); |
| 274 | igmp.setMaxRespCode(maxRespCode); |
| 275 | |
| 276 | IGMPQuery query = new IGMPQuery(IpAddress.valueOf("0.0.0.0"), 0); |
| 277 | igmp.addGroup(query); |
| 278 | |
| 279 | IPv4 ip = new IPv4(); |
| 280 | ip.setDestinationAddress(DEST_IP); |
| 281 | ip.setProtocol(IPv4.PROTOCOL_IGMP); |
| 282 | ip.setSourceAddress("192.168.1.1"); |
| 283 | ip.setTtl((byte) 1); |
| 284 | ip.setPayload(igmp); |
| 285 | |
| 286 | Ethernet eth = new Ethernet(); |
| 287 | eth.setDestinationMACAddress(DEST_MAC); |
| 288 | eth.setSourceMACAddress("DE:AD:BE:EF:BA:11"); |
| 289 | eth.setEtherType(Ethernet.TYPE_IPV4); |
| 290 | |
| 291 | eth.setPayload(ip); |
| 292 | |
| 293 | return ByteBuffer.wrap(eth.serialize()); |
| 294 | } |
| 295 | |
| 296 | private void querySubscribers() { |
| 297 | oltData.keySet().stream() |
| 298 | .flatMap(did -> deviceService.getPorts(did).stream()) |
| 299 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 300 | .filter(p -> p.isEnabled()) |
| 301 | .forEach(p -> { |
| 302 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 303 | .setOutput(p.number()).build(); |
| 304 | packetService.emit(new DefaultOutboundPacket((DeviceId) p.element().id(), |
| 305 | treatment, queryPacket)); |
| 306 | }); |
| 307 | } |
| 308 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Packet processor responsible for handling IGMP packets. |
| 311 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 312 | private class IgmpPacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 313 | |
| 314 | @Override |
| 315 | public void process(PacketContext context) { |
| 316 | // Stop processing if the packet has been handled, since we |
| 317 | // can't do any more to it. |
| 318 | if (context.isHandled()) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | InboundPacket pkt = context.inPacket(); |
| 323 | Ethernet ethPkt = pkt.parsed(); |
| 324 | if (ethPkt == null) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * IPv6 MLD packets are handled by ICMP6. We'll only deal |
| 330 | * with IPv4. |
| 331 | */ |
| 332 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | IPv4 ip = (IPv4) ethPkt.getPayload(); |
| 337 | IpAddress gaddr = IpAddress.valueOf(ip.getDestinationAddress()); |
| 338 | IpAddress saddr = Ip4Address.valueOf(ip.getSourceAddress()); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 339 | log.debug("Packet ({}, {}) -> ingress port: {}", saddr, gaddr, |
| 340 | context.inPacket().receivedFrom()); |
| 341 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 342 | |
| 343 | if (ip.getProtocol() != IPv4.PROTOCOL_IGMP) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 344 | log.debug("IGMP Picked up a non IGMP packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 345 | return; |
| 346 | } |
| 347 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 348 | IpPrefix mcast = IpPrefix.valueOf(DEFAULT_MCAST_ADDR); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 349 | if (!mcast.contains(gaddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 350 | log.debug("IGMP Picked up a non multicast packet."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (mcast.contains(saddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 355 | log.debug("IGMP Picked up a packet with a multicast source address."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 356 | return; |
| 357 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 358 | |
| 359 | IGMP igmp = (IGMP) ip.getPayload(); |
| 360 | switch (igmp.getIgmpType()) { |
| 361 | |
| 362 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 363 | processMembership(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 364 | break; |
| 365 | |
| 366 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 367 | log.debug("Received a membership query {} from {}", |
| 368 | igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 372 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 373 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 374 | log.debug("IGMP version 1 & 2 message types are not currently supported. Message type: {}", |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 375 | igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 376 | break; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 377 | default: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 378 | log.debug("Unknown IGMP message type: {}", igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 383 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 384 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 385 | private class InternalDeviceListener implements DeviceListener { |
| 386 | @Override |
| 387 | public void event(DeviceEvent event) { |
| 388 | switch (event.type()) { |
| 389 | |
| 390 | case DEVICE_ADDED: |
| 391 | case DEVICE_UPDATED: |
| 392 | case DEVICE_REMOVED: |
| 393 | case DEVICE_SUSPENDED: |
| 394 | case DEVICE_AVAILABILITY_CHANGED: |
| 395 | case PORT_STATS_UPDATED: |
| 396 | break; |
| 397 | case PORT_ADDED: |
| 398 | if (event.port().isEnabled()) { |
| 399 | processFilterObjective(event.subject().id(), event.port(), false); |
| 400 | } |
| 401 | break; |
| 402 | case PORT_UPDATED: |
| 403 | if (event.port().isEnabled()) { |
| 404 | processFilterObjective(event.subject().id(), event.port(), false); |
| 405 | } else { |
| 406 | processFilterObjective(event.subject().id(), event.port(), true); |
| 407 | } |
| 408 | break; |
| 409 | case PORT_REMOVED: |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 410 | processFilterObjective(event.subject().id(), event.port(), true); |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 411 | break; |
| 412 | default: |
| 413 | log.warn("Unknown device event {}", event.type()); |
| 414 | break; |
| 415 | } |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | @Override |
| 419 | public boolean isRelevant(DeviceEvent event) { |
| 420 | return oltData.containsKey(event.subject().id()); |
| 421 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 422 | } |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 423 | |
| 424 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 425 | @Override |
| 426 | public void event(NetworkConfigEvent event) { |
| 427 | switch (event.type()) { |
| 428 | |
| 429 | case CONFIG_ADDED: |
| 430 | case CONFIG_UPDATED: |
| 431 | if (event.configClass().equals(CONFIG_CLASS)) { |
| 432 | AccessDeviceConfig config = |
| 433 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 434 | if (config != null) { |
| 435 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
| 436 | provisionDefaultFlows((DeviceId) event.subject()); |
| 437 | } |
| 438 | } |
| 439 | break; |
| 440 | case CONFIG_UNREGISTERED: |
| 441 | case CONFIG_REMOVED: |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 449 | List<Port> ports = deviceService.getPorts(deviceId); |
| 450 | |
| 451 | ports.stream() |
| 452 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 453 | .filter(p -> p.isEnabled()) |
| 454 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
| 455 | |
| 456 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 457 | } |