alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 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; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Property; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 25 | import org.onlab.packet.EthType; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IGMP; |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IGMPMembership; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IGMPQuery; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IPv4; |
| 31 | import org.onlab.packet.Ip4Address; |
| 32 | import org.onlab.packet.IpAddress; |
| 33 | import org.onlab.packet.IpPrefix; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 34 | import org.onlab.util.SafeRecurringTask; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 35 | import org.onlab.util.Tools; |
| 36 | import org.onosproject.cfg.ComponentConfigService; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.DeviceId; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
| 42 | import org.onosproject.net.PortNumber; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigEvent; |
| 45 | import org.onosproject.net.config.NetworkConfigListener; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 46 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 47 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceListener; |
| 50 | import org.onosproject.net.device.DeviceService; |
| 51 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 52 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.criteria.Criteria; |
| 54 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 55 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 56 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 57 | import org.onosproject.net.flowobjective.Objective; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 59 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 60 | import org.onosproject.net.mcast.McastRoute; |
| 61 | import org.onosproject.net.mcast.MulticastRouteService; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 62 | import org.onosproject.net.packet.DefaultOutboundPacket; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 63 | import org.onosproject.net.packet.InboundPacket; |
| 64 | import org.onosproject.net.packet.PacketContext; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 65 | import org.onosproject.net.packet.PacketProcessor; |
| 66 | import org.onosproject.net.packet.PacketService; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 67 | import org.onosproject.olt.AccessDeviceConfig; |
| 68 | import org.onosproject.olt.AccessDeviceData; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 69 | import org.osgi.service.component.ComponentContext; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 70 | import org.slf4j.Logger; |
| 71 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 72 | import java.nio.ByteBuffer; |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 73 | import java.util.Collection; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 74 | import java.util.Dictionary; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 75 | import java.util.List; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 76 | import java.util.Map; |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 77 | import java.util.Properties; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 78 | import java.util.concurrent.ConcurrentHashMap; |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 79 | import java.util.concurrent.Executors; |
| 80 | import java.util.concurrent.ScheduledExecutorService; |
| 81 | import java.util.concurrent.ScheduledFuture; |
| 82 | import java.util.concurrent.TimeUnit; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 83 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 84 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 85 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 86 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Internet Group Management Protocol. |
| 89 | */ |
| 90 | @Component(immediate = true) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 91 | public class IgmpSnoop { |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 92 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 93 | private final Logger log = getLogger(getClass()); |
| 94 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 95 | 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 Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 99 | private static final byte DEFAULT_IGMP_RESP_CODE = 100; |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 100 | |
| 101 | @Property(name = "multicastAddress", |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 102 | label = "Define the multicast base range to listen to") |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 103 | private String multicastAddress = IpPrefix.IPV4_MULTICAST_PREFIX.toString(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 104 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 105 | @Property(name = "queryPeriod", intValue = DEFAULT_QUERY_PERIOD_SECS, |
| 106 | label = "Delay in seconds between successive query runs") |
| 107 | private int queryPeriod = DEFAULT_QUERY_PERIOD_SECS; |
| 108 | |
| 109 | @Property(name = "maxRespCode", byteValue = DEFAULT_IGMP_RESP_CODE, |
| 110 | label = "Maximum time allowed before sending a responding report") |
| 111 | private byte maxRespCode = DEFAULT_IGMP_RESP_CODE; |
| 112 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 114 | protected FlowObjectiveService flowObjectiveService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 117 | protected PacketService packetService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected CoreService coreService; |
| 121 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected NetworkConfigRegistry networkConfig; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 126 | protected ComponentConfigService componentConfigService; |
| 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 129 | protected MulticastRouteService multicastService; |
| 130 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 132 | protected DeviceService deviceService; |
| 133 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 134 | |
| 135 | private ScheduledFuture<?> queryTask; |
| 136 | private final ScheduledExecutorService queryService = |
| 137 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/igmp-query", |
| 138 | "membership-query")); |
| 139 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 140 | private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>(); |
| 141 | |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 142 | private Map<IpAddress, IpAddress> ssmTranslateTable = new ConcurrentHashMap<>(); |
| 143 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 144 | private DeviceListener deviceListener = new InternalDeviceListener(); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 145 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 146 | private static ApplicationId appId; |
| 147 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 148 | private InternalNetworkConfigListener configListener = |
| 149 | new InternalNetworkConfigListener(); |
| 150 | |
| 151 | private static final Class<AccessDeviceConfig> CONFIG_CLASS = |
| 152 | AccessDeviceConfig.class; |
| 153 | |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 154 | private static final Class<IgmpSsmTranslateConfig> SSM_TRANSLATE_CONFIG_CLASS = |
| 155 | IgmpSsmTranslateConfig.class; |
| 156 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 157 | private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory = |
| 158 | new ConfigFactory<DeviceId, AccessDeviceConfig>( |
| 159 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") { |
| 160 | @Override |
| 161 | public AccessDeviceConfig createConfig() { |
| 162 | return new AccessDeviceConfig(); |
| 163 | } |
| 164 | }; |
| 165 | |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 166 | private ConfigFactory<ApplicationId, IgmpSsmTranslateConfig> ssmTranslateConfigFactory = |
| 167 | new ConfigFactory<ApplicationId, IgmpSsmTranslateConfig>( |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 168 | SubjectFactories.APP_SUBJECT_FACTORY, SSM_TRANSLATE_CONFIG_CLASS, "ssmTranslate", true) { |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 169 | @Override |
| 170 | public IgmpSsmTranslateConfig createConfig() { |
| 171 | return new IgmpSsmTranslateConfig(); |
| 172 | } |
| 173 | }; |
| 174 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 175 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 176 | private ByteBuffer queryPacket; |
| 177 | |
| 178 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 179 | @Activate |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 180 | public void activate(ComponentContext context) { |
Jonathan Hart | 9ad777f | 2016-02-19 12:44:36 -0800 | [diff] [blame] | 181 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 182 | modified(context); |
| 183 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 184 | appId = coreService.registerApplication("org.onosproject.igmp"); |
| 185 | |
| 186 | packetService.addProcessor(processor, PacketProcessor.director(1)); |
| 187 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 188 | networkConfig.registerConfigFactory(configFactory); |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 189 | networkConfig.registerConfigFactory(ssmTranslateConfigFactory); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 190 | networkConfig.addListener(configListener); |
| 191 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 192 | networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach( |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 193 | subject -> { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 194 | AccessDeviceConfig config = networkConfig.getConfig(subject, |
| 195 | AccessDeviceConfig.class); |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 196 | if (config != null) { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 197 | AccessDeviceData data = config.getOlt(); |
| 198 | oltData.put(data.deviceId(), data); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 199 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | ); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 203 | |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 204 | IgmpSsmTranslateConfig ssmTranslateConfig = |
| 205 | networkConfig.getConfig(appId, IgmpSsmTranslateConfig.class); |
| 206 | |
| 207 | if (ssmTranslateConfig != null) { |
| 208 | Collection<McastRoute> translations = ssmTranslateConfig.getSsmTranslations(); |
| 209 | for (McastRoute route : translations) { |
| 210 | ssmTranslateTable.put(route.group(), route.source()); |
| 211 | } |
| 212 | } |
| 213 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 214 | oltData.keySet().stream() |
| 215 | .flatMap(did -> deviceService.getPorts(did).stream()) |
| 216 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 217 | .filter(p -> p.isEnabled()) |
| 218 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
| 219 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 220 | deviceService.addListener(deviceListener); |
| 221 | |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 222 | restartQueryTask(); |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 223 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 224 | log.info("Started"); |
| 225 | } |
| 226 | |
| 227 | @Deactivate |
| 228 | public void deactivate() { |
| 229 | packetService.removeProcessor(processor); |
| 230 | processor = null; |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 231 | deviceService.removeListener(deviceListener); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 232 | networkConfig.removeListener(configListener); |
| 233 | networkConfig.unregisterConfigFactory(configFactory); |
Jonathan Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 234 | networkConfig.unregisterConfigFactory(ssmTranslateConfigFactory); |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 235 | queryTask.cancel(true); |
| 236 | queryService.shutdownNow(); |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 237 | componentConfigService.unregisterProperties(getClass(), false); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 238 | log.info("Stopped"); |
| 239 | } |
| 240 | |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 241 | @Modified |
| 242 | protected void modified(ComponentContext context) { |
| 243 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
| 244 | |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 245 | // TODO read multicastAddress from config |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 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 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 281 | private void processFilterObjective(DeviceId devId, Port port, boolean remove) { |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 282 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 283 | //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) |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 295 | .withPriority(10000) |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 296 | .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 Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 313 | 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 Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 323 | // TODO allow pulling source from IGMP packet |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 324 | 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 Hart | 5e8689c | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | McastRoute route = new McastRoute(source, |
| 331 | group.getGaddr(), |
| 332 | McastRoute.Type.IGMP); |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 333 | |
| 334 | if (membership.getRecordType() == IGMPMembership.MODE_IS_INCLUDE || |
| 335 | membership.getRecordType() == IGMPMembership.CHANGE_TO_INCLUDE_MODE) { |
| 336 | |
Jonathan Hart | dd7e3d5 | 2016-02-18 15:56:22 -0800 | [diff] [blame] | 337 | 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 Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 341 | |
| 342 | multicastService.add(route); |
| 343 | multicastService.addSink(route, location); |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | }); |
| 347 | } |
| 348 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 349 | 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 | |
alshabib | 10d68ae | 2016-02-22 16:05:24 -0500 | [diff] [blame] | 371 | eth.setPad(true); |
| 372 | |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 373 | return ByteBuffer.wrap(eth.serialize()); |
| 374 | } |
| 375 | |
| 376 | private void querySubscribers() { |
| 377 | oltData.keySet().stream() |
| 378 | .flatMap(did -> deviceService.getPorts(did).stream()) |
| 379 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 380 | .filter(p -> p.isEnabled()) |
| 381 | .forEach(p -> { |
| 382 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 383 | .setOutput(p.number()).build(); |
| 384 | packetService.emit(new DefaultOutboundPacket((DeviceId) p.element().id(), |
| 385 | treatment, queryPacket)); |
| 386 | }); |
| 387 | } |
| 388 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Packet processor responsible for handling IGMP packets. |
| 391 | */ |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 392 | private class IgmpPacketProcessor implements PacketProcessor { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 393 | |
| 394 | @Override |
| 395 | public void process(PacketContext context) { |
| 396 | // Stop processing if the packet has been handled, since we |
| 397 | // can't do any more to it. |
| 398 | if (context.isHandled()) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | InboundPacket pkt = context.inPacket(); |
| 403 | Ethernet ethPkt = pkt.parsed(); |
| 404 | if (ethPkt == null) { |
| 405 | return; |
| 406 | } |
| 407 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 408 | |
| 409 | // IPv6 MLD packets are handled by ICMP6. We'll only deal with IPv4. |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 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()); |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 417 | log.trace("Packet ({}, {}) -> ingress port: {}", saddr, gaddr, |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 418 | context.inPacket().receivedFrom()); |
| 419 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 420 | |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 421 | if (ip.getProtocol() != IPv4.PROTOCOL_IGMP || |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 422 | !IpPrefix.IPV4_MULTICAST_PREFIX.contains(gaddr)) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 426 | if (IpPrefix.IPV4_MULTICAST_PREFIX.contains(saddr)) { |
Rusty Eddy | 158d5d8 | 2015-10-12 16:59:04 -0700 | [diff] [blame] | 427 | log.debug("IGMP Picked up a packet with a multicast source address."); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 428 | return; |
| 429 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 430 | |
| 431 | IGMP igmp = (IGMP) ip.getPayload(); |
| 432 | switch (igmp.getIgmpType()) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 433 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 434 | processMembership(igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 435 | break; |
| 436 | |
| 437 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
alshabib | 42d32eb | 2016-02-14 19:53:42 -0800 | [diff] [blame] | 438 | log.debug("Received a membership query {} from {}", |
| 439 | igmp, pkt.receivedFrom()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 440 | break; |
| 441 | |
| 442 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
| 443 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
| 444 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
Jonathan Hart | 6ccfc5a | 2016-02-12 19:26:02 -0800 | [diff] [blame] | 445 | 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] | 446 | igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 447 | break; |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 448 | default: |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 449 | log.warn("Unknown IGMP message type: {}", igmp.getIgmpType()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 454 | |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 455 | |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 456 | private class InternalDeviceListener implements DeviceListener { |
| 457 | @Override |
| 458 | public void event(DeviceEvent event) { |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 459 | DeviceId devId = event.subject().id(); |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 460 | switch (event.type()) { |
| 461 | |
| 462 | case DEVICE_ADDED: |
| 463 | case DEVICE_UPDATED: |
| 464 | case DEVICE_REMOVED: |
| 465 | case DEVICE_SUSPENDED: |
| 466 | case DEVICE_AVAILABILITY_CHANGED: |
| 467 | case PORT_STATS_UPDATED: |
| 468 | break; |
| 469 | case PORT_ADDED: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 470 | if (!oltData.get(devId).uplink().equals(event.port().number()) && |
| 471 | event.port().isEnabled()) { |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 472 | processFilterObjective(event.subject().id(), event.port(), false); |
| 473 | } |
| 474 | break; |
| 475 | case PORT_UPDATED: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 476 | if (oltData.get(devId).uplink().equals(event.port().number())) { |
| 477 | break; |
| 478 | } |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 479 | if (event.port().isEnabled()) { |
| 480 | processFilterObjective(event.subject().id(), event.port(), false); |
| 481 | } else { |
| 482 | processFilterObjective(event.subject().id(), event.port(), true); |
| 483 | } |
| 484 | break; |
| 485 | case PORT_REMOVED: |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 486 | processFilterObjective(event.subject().id(), event.port(), true); |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 487 | break; |
| 488 | default: |
| 489 | log.warn("Unknown device event {}", event.type()); |
| 490 | break; |
| 491 | } |
alshabib | 03adb49 | 2016-02-01 17:25:00 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | @Override |
| 495 | public boolean isRelevant(DeviceEvent event) { |
| 496 | return oltData.containsKey(event.subject().id()); |
| 497 | } |
alshabib | 79e5287 | 2015-12-07 16:01:01 -0800 | [diff] [blame] | 498 | } |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 499 | |
| 500 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 501 | @Override |
| 502 | public void event(NetworkConfigEvent event) { |
| 503 | switch (event.type()) { |
| 504 | |
| 505 | case CONFIG_ADDED: |
| 506 | case CONFIG_UPDATED: |
| 507 | if (event.configClass().equals(CONFIG_CLASS)) { |
| 508 | AccessDeviceConfig config = |
| 509 | networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS); |
| 510 | if (config != null) { |
| 511 | oltData.put(config.getOlt().deviceId(), config.getOlt()); |
| 512 | provisionDefaultFlows((DeviceId) event.subject()); |
| 513 | } |
| 514 | } |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 515 | if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) { |
| 516 | IgmpSsmTranslateConfig config = |
| 517 | networkConfig.getConfig((ApplicationId) event.subject(), |
| 518 | SSM_TRANSLATE_CONFIG_CLASS); |
| 519 | |
| 520 | if (config != null) { |
| 521 | ssmTranslateTable.clear(); |
| 522 | config.getSsmTranslations().forEach( |
| 523 | route -> ssmTranslateTable.put(route.group(), route.source())); |
| 524 | } |
| 525 | } |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 526 | break; |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 527 | case CONFIG_REGISTERED: |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 528 | case CONFIG_UNREGISTERED: |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 529 | break; |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 530 | case CONFIG_REMOVED: |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 531 | if (event.configClass().equals(SSM_TRANSLATE_CONFIG_CLASS)) { |
| 532 | ssmTranslateTable.clear(); |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 533 | } else if (event.configClass().equals(CONFIG_CLASS)) { |
| 534 | oltData.remove(event.subject()); |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 535 | } |
alshabib | f544c01 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 536 | |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 537 | default: |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | private void provisionDefaultFlows(DeviceId deviceId) { |
| 544 | List<Port> ports = deviceService.getPorts(deviceId); |
| 545 | |
| 546 | ports.stream() |
| 547 | .filter(p -> !oltData.get(p.element().id()).uplink().equals(p.number())) |
| 548 | .filter(p -> p.isEnabled()) |
| 549 | .forEach(p -> processFilterObjective((DeviceId) p.element().id(), p, false)); |
alshabib | fe69e9a | 2016-02-11 17:31:36 -0800 | [diff] [blame] | 550 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 551 | } |