David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4d08470 | 2017-08-03 22:45:58 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -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 | */ |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame] | 16 | package org.opencord.igmpproxy.impl; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 17 | |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 20 | import org.opencord.igmpproxy.IgmpLeadershipService; |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame] | 21 | import org.opencord.igmpproxy.IgmpStatisticsService; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 22 | import org.opencord.igmpproxy.impl.store.groupmember.GroupMember; |
| 23 | import org.opencord.igmpproxy.GroupMemberId; |
| 24 | import org.opencord.igmpproxy.impl.store.groupmember.GroupMemberStore; |
| 25 | import org.opencord.igmpproxy.statemachine.StateMachineService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 26 | import org.opencord.sadis.BaseInformationService; |
| 27 | import org.opencord.sadis.SadisService; |
| 28 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 34 | import org.onlab.packet.EthType; |
| 35 | import org.onlab.packet.Ethernet; |
| 36 | import org.onlab.packet.IGMP; |
| 37 | import org.onlab.packet.IGMPGroup; |
| 38 | import org.onlab.packet.IGMPMembership; |
| 39 | import org.onlab.packet.IGMPQuery; |
| 40 | import org.onlab.packet.IPv4; |
| 41 | import org.onlab.packet.Ip4Address; |
| 42 | import org.onlab.packet.IpAddress; |
| 43 | import org.onlab.packet.VlanId; |
| 44 | import org.onosproject.core.ApplicationId; |
| 45 | import org.onosproject.core.CoreService; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 46 | import org.onosproject.mastership.MastershipService; |
| 47 | import org.onosproject.net.AnnotationKeys; |
| 48 | import org.onosproject.net.ConnectPoint; |
| 49 | import org.onosproject.net.DeviceId; |
| 50 | import org.onosproject.net.Port; |
| 51 | import org.onosproject.net.PortNumber; |
| 52 | import org.onosproject.net.config.ConfigFactory; |
| 53 | import org.onosproject.net.config.NetworkConfigEvent; |
| 54 | import org.onosproject.net.config.NetworkConfigListener; |
| 55 | import org.onosproject.net.config.NetworkConfigRegistry; |
Jonathan Hart | 488e114 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 56 | import org.onosproject.net.config.basics.McastConfig; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 57 | import org.onosproject.net.config.basics.SubjectFactories; |
| 58 | import org.onosproject.net.device.DeviceEvent; |
| 59 | import org.onosproject.net.device.DeviceListener; |
| 60 | import org.onosproject.net.device.DeviceService; |
| 61 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 62 | import org.onosproject.net.flow.FlowRuleService; |
| 63 | import org.onosproject.net.flow.criteria.Criteria; |
| 64 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 65 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 66 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 67 | import org.onosproject.net.flowobjective.Objective; |
| 68 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 69 | import org.onosproject.net.flowobjective.ObjectiveError; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 70 | import org.onosproject.mcast.api.McastRoute; |
| 71 | import org.onosproject.mcast.api.MulticastRouteService; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 72 | import org.onosproject.net.packet.InboundPacket; |
| 73 | import org.onosproject.net.packet.PacketContext; |
| 74 | import org.onosproject.net.packet.PacketProcessor; |
| 75 | import org.onosproject.net.packet.PacketService; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 76 | import org.slf4j.Logger; |
| 77 | import org.slf4j.LoggerFactory; |
| 78 | |
| 79 | import java.util.ArrayList; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 80 | import java.util.Arrays; |
| 81 | import java.util.List; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 82 | import java.util.Collection; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 83 | import java.util.HashSet; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 84 | import java.util.Iterator; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 85 | import java.util.Map; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 86 | import java.util.Optional; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 87 | import java.util.Set; |
| 88 | import java.util.TimerTask; |
| 89 | import java.util.concurrent.ConcurrentHashMap; |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 90 | import java.util.concurrent.ExecutorService; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 91 | import java.util.concurrent.Executors; |
| 92 | import java.util.concurrent.ScheduledExecutorService; |
| 93 | import java.util.concurrent.TimeUnit; |
| 94 | |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 95 | import static org.onlab.packet.IGMPMembership.MODE_IS_INCLUDE; |
| 96 | import static org.onlab.packet.IGMPMembership.MODE_IS_EXCLUDE; |
| 97 | import static org.onlab.packet.IGMPMembership.CHANGE_TO_INCLUDE_MODE; |
| 98 | import static org.onlab.packet.IGMPMembership.CHANGE_TO_EXCLUDE_MODE; |
| 99 | import static org.onlab.packet.IGMPMembership.ALLOW_NEW_SOURCES; |
| 100 | import static org.onlab.packet.IGMPMembership.BLOCK_OLD_SOURCES; |
| 101 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 102 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 103 | import static org.onlab.util.Tools.groupedThreads; |
| 104 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 105 | /** |
| 106 | * Igmp process application, use proxy mode, support first join/ last leave , fast leave |
| 107 | * period query and keep alive, packet out igmp message to uplink port features. |
| 108 | */ |
| 109 | @Component(immediate = true) |
| 110 | public class IgmpManager { |
| 111 | |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 112 | private static final String APP_NAME = "org.opencord.igmpproxy"; |
| 113 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 114 | private static final Class<IgmpproxyConfig> IGMPPROXY_CONFIG_CLASS = |
| 115 | IgmpproxyConfig.class; |
| 116 | private static final Class<IgmpproxySsmTranslateConfig> IGMPPROXY_SSM_CONFIG_CLASS = |
| 117 | IgmpproxySsmTranslateConfig.class; |
| 118 | private static final Class<McastConfig> MCAST_CONFIG_CLASS = |
| 119 | McastConfig.class; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 120 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 121 | private static ApplicationId appId; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 122 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 123 | private static int unSolicitedTimeout = 3; // unit is 1 sec |
| 124 | private static int keepAliveCount = 3; |
| 125 | private static int lastQueryInterval = 2; //unit is 1 sec |
| 126 | private static int lastQueryCount = 2; |
| 127 | private static boolean fastLeave = true; |
| 128 | private static boolean withRAUplink = true; |
| 129 | private static boolean withRADownlink = false; |
| 130 | private static boolean periodicQuery = true; |
| 131 | private static short mvlan = 4000; |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 132 | private static short mvlanInner = VlanId.NONE.toShort(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 133 | private static byte igmpCos = 7; |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 134 | private static byte igmpUniCos = 7; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 135 | public static boolean connectPointMode = true; |
| 136 | public static ConnectPoint connectPoint = null; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 137 | private static ConnectPoint sourceDeviceAndPort = null; |
| 138 | private static boolean enableIgmpProvisioning = false; |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 139 | private static boolean igmpOnPodBasis = false; |
Arjun E K | b0018fd | 2020-04-07 13:26:40 +0000 | [diff] [blame] | 140 | private static boolean outgoingIgmpWithV3 = true; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 141 | |
| 142 | private static final Integer MAX_PRIORITY = 10000; |
| 143 | private static final String INSTALLED = "installed"; |
| 144 | private static final String REMOVED = "removed"; |
| 145 | private static final String INSTALLATION = "installation"; |
| 146 | private static final String REMOVAL = "removal"; |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 147 | private static final String NNI_PREFIX = "nni"; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 148 | |
ke han | 29af27b | 2017-09-08 10:29:12 +0800 | [diff] [blame] | 149 | private static boolean pimSSmInterworking = false; |
| 150 | private static final String DEFAULT_PIMSSM_HOST = "127.0.0.1"; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 151 | private final ScheduledExecutorService scheduledExecutorService = |
| 152 | Executors.newScheduledThreadPool(1); |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 153 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 154 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 155 | protected CoreService coreService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 156 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 157 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 158 | protected PacketService packetService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 159 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 160 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 161 | protected MastershipService mastershipService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 162 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 163 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 164 | protected FlowRuleService flowRuleService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 165 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 166 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 167 | protected DeviceService deviceService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 168 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 169 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 170 | protected FlowObjectiveService flowObjectiveService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 171 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 172 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 173 | protected NetworkConfigRegistry networkConfig; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 174 | |
Carmelo Cascone | bef302e | 2019-11-14 19:58:20 -0800 | [diff] [blame] | 175 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 176 | protected MulticastRouteService multicastService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 177 | |
| 178 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 179 | protected SadisService sadisService; |
| 180 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 181 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 182 | protected IgmpStatisticsService igmpStatisticsManager; |
| 183 | |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 184 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 185 | protected GroupMemberStore groupMemberStore; |
| 186 | |
| 187 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 188 | protected StateMachineService stateMachineService; |
| 189 | |
| 190 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 191 | protected IgmpLeadershipService igmpLeadershipService; |
| 192 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 193 | private IgmpPacketProcessor processor = new IgmpPacketProcessor(); |
| 194 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 195 | private ApplicationId coreAppId; |
| 196 | private Map<Ip4Address, Ip4Address> ssmTranslateTable = new ConcurrentHashMap<>(); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 197 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 198 | private InternalNetworkConfigListener configListener = |
| 199 | new InternalNetworkConfigListener(); |
| 200 | private DeviceListener deviceListener = new InternalDeviceListener(); |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 201 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 202 | private ConfigFactory<ApplicationId, IgmpproxyConfig> igmpproxyConfigFactory = |
| 203 | new ConfigFactory<ApplicationId, IgmpproxyConfig>( |
| 204 | SubjectFactories.APP_SUBJECT_FACTORY, IGMPPROXY_CONFIG_CLASS, "igmpproxy") { |
| 205 | @Override |
| 206 | public IgmpproxyConfig createConfig() { |
| 207 | return new IgmpproxyConfig(); |
| 208 | } |
| 209 | }; |
| 210 | private ConfigFactory<ApplicationId, IgmpproxySsmTranslateConfig> igmpproxySsmConfigFactory = |
| 211 | new ConfigFactory<ApplicationId, IgmpproxySsmTranslateConfig>( |
| 212 | SubjectFactories.APP_SUBJECT_FACTORY, IGMPPROXY_SSM_CONFIG_CLASS, "ssmTranslate", true) { |
| 213 | @Override |
| 214 | public IgmpproxySsmTranslateConfig createConfig() { |
| 215 | return new IgmpproxySsmTranslateConfig(); |
| 216 | } |
| 217 | }; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 218 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 219 | private int maxResp = 10; //unit is 1 sec |
| 220 | private int keepAliveInterval = 120; //unit is 1 sec |
| 221 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 222 | private ExecutorService eventExecutor; |
| 223 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 224 | public static int getUnsolicitedTimeout() { |
| 225 | return unSolicitedTimeout; |
| 226 | } |
| 227 | |
Arjun E K | b0018fd | 2020-04-07 13:26:40 +0000 | [diff] [blame] | 228 | public static boolean outgoingIgmpWithV3() { |
| 229 | return outgoingIgmpWithV3; |
| 230 | } |
| 231 | |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 232 | protected BaseInformationService<SubscriberAndDeviceInformation> subsService; |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 233 | |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 234 | private List<Byte> validMembershipModes = Arrays.asList(MODE_IS_INCLUDE, MODE_IS_EXCLUDE, CHANGE_TO_INCLUDE_MODE, |
| 235 | CHANGE_TO_EXCLUDE_MODE, ALLOW_NEW_SOURCES, BLOCK_OLD_SOURCES); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 236 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 237 | @Activate |
| 238 | protected void activate() { |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 239 | appId = coreService.registerApplication(APP_NAME); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 240 | coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME); |
| 241 | packetService.addProcessor(processor, PacketProcessor.director(4)); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 242 | IgmpSender.init(packetService, igmpLeadershipService, igmpStatisticsManager); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 243 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 244 | networkConfig.registerConfigFactory(igmpproxySsmConfigFactory); |
| 245 | networkConfig.registerConfigFactory(igmpproxyConfigFactory); |
| 246 | networkConfig.addListener(configListener); |
| 247 | |
| 248 | configListener.reconfigureNetwork(networkConfig.getConfig(appId, IGMPPROXY_CONFIG_CLASS)); |
| 249 | configListener.reconfigureSsmTable(networkConfig.getConfig(appId, IGMPPROXY_SSM_CONFIG_CLASS)); |
| 250 | |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 251 | subsService = sadisService.getSubscriberInfoService(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 252 | if (connectPointMode) { |
| 253 | provisionConnectPointFlows(); |
| 254 | } else { |
| 255 | provisionUplinkFlows(); |
| 256 | } |
| 257 | |
| 258 | McastConfig config = networkConfig.getConfig(coreAppId, MCAST_CONFIG_CLASS); |
| 259 | if (config != null) { |
| 260 | mvlan = config.egressVlan().toShort(); |
Deepa Vaddireddy | 88134a4 | 2017-07-05 12:16:03 +0530 | [diff] [blame] | 261 | IgmpSender.getInstance().setMvlan(mvlan); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 262 | mvlanInner = config.egressInnerVlan().toShort(); |
| 263 | IgmpSender.getInstance().setMvlanInner(mvlanInner); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 264 | } |
| 265 | deviceService.addListener(deviceListener); |
Sonal Kasliwal | ddc3ff2 | 2019-11-18 11:52:49 +0000 | [diff] [blame] | 266 | scheduledExecutorService.scheduleAtFixedRate(new IgmpProxyTimerTask(), 1000, 1000, TimeUnit.MILLISECONDS); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 267 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("cord/igmpproxy", |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 268 | "events-igmp-%d", log)); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 269 | log.info("Started"); |
| 270 | } |
| 271 | |
| 272 | @Deactivate |
| 273 | protected void deactivate() { |
| 274 | scheduledExecutorService.shutdown(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 275 | eventExecutor.shutdown(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 276 | |
| 277 | // de-register and null our handler |
| 278 | networkConfig.removeListener(configListener); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 279 | networkConfig.unregisterConfigFactory(igmpproxyConfigFactory); |
| 280 | networkConfig.unregisterConfigFactory(igmpproxySsmConfigFactory); |
| 281 | deviceService.removeListener(deviceListener); |
| 282 | packetService.removeProcessor(processor); |
| 283 | flowRuleService.removeFlowRulesById(appId); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 284 | log.info("Stopped"); |
| 285 | } |
| 286 | |
| 287 | protected Ip4Address getDeviceIp(DeviceId ofDeviceId) { |
| 288 | try { |
| 289 | String[] mgmtAddress = deviceService.getDevice(ofDeviceId) |
| 290 | .annotations().value(AnnotationKeys.MANAGEMENT_ADDRESS).split(":"); |
| 291 | return Ip4Address.valueOf(mgmtAddress[0]); |
| 292 | } catch (Exception ex) { |
| 293 | log.info("No valid Ipaddress for " + ofDeviceId.toString()); |
| 294 | return null; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | private void processIgmpQuery(IGMPQuery igmpQuery, ConnectPoint cp, int maxResp) { |
| 299 | |
| 300 | DeviceId deviceId = cp.deviceId(); |
| 301 | Ip4Address gAddr = igmpQuery.getGaddr().getIp4Address(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 302 | maxResp = calculateMaxResp(maxResp); |
| 303 | if (gAddr != null && !gAddr.isZero()) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 304 | stateMachineService.specialQuery(deviceId, gAddr, maxResp); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 305 | igmpStatisticsManager.getIgmpStats().increaseIgmpGrpSpecificMembershipQuery(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 306 | } else { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 307 | stateMachineService.generalQuery(deviceId, maxResp); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 308 | igmpStatisticsManager.getIgmpStats().increaseIgmpGeneralMembershipQuery(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 311 | |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 312 | private void processIgmpConnectPointQuery(IGMPQuery igmpQuery, ConnectPoint cp, int maxResp) { |
| 313 | |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 314 | Ip4Address gAddr = igmpQuery.getGaddr().getIp4Address(); |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 315 | final int maxResponseTime = calculateMaxResp(maxResp); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 316 | //The query is received on the ConnectPoint |
| 317 | // send query accordingly to the registered OLT devices. |
| 318 | if (gAddr != null && !gAddr.isZero()) { |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 319 | deviceService.getAvailableDevices().forEach(device -> { |
| 320 | Optional<SubscriberAndDeviceInformation> accessDevice = getSubscriberAndDeviceInformation(device.id()); |
| 321 | if (accessDevice.isPresent()) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 322 | stateMachineService.specialQuery(device.id(), gAddr, maxResponseTime); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 323 | igmpStatisticsManager.getIgmpStats().increaseIgmpGrpAndSrcSpecificMembershipQuery(); |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 324 | } |
| 325 | }); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 326 | igmpStatisticsManager.getIgmpStats().increaseCurrentGrpNumCounter(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 327 | } else { |
| 328 | //Don't know which group is targeted by the query |
| 329 | //So query all the members(in all the OLTs) and proxy their reports |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 330 | stateMachineService.generalQuery(maxResponseTime); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 331 | igmpStatisticsManager.getIgmpStats().increaseIgmpGeneralMembershipQuery(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | |
| 336 | private int calculateMaxResp(int maxResp) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 337 | if (maxResp >= 128) { |
| 338 | int mant = maxResp & 0xf; |
| 339 | int exp = (maxResp >> 4) & 0x7; |
| 340 | maxResp = (mant | 0x10) << (exp + 3); |
| 341 | } |
| 342 | |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 343 | return (maxResp + 5) / 10; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | private Ip4Address ssmTranslateRoute(IpAddress group) { |
| 347 | return ssmTranslateTable.get(group); |
| 348 | } |
| 349 | |
| 350 | private void processIgmpReport(IGMPMembership igmpGroup, VlanId vlan, ConnectPoint cp, byte igmpType) { |
| 351 | DeviceId deviceId = cp.deviceId(); |
| 352 | PortNumber portNumber = cp.port(); |
| 353 | |
| 354 | Ip4Address groupIp = igmpGroup.getGaddr().getIp4Address(); |
| 355 | if (!groupIp.isMulticast()) { |
| 356 | log.info(groupIp.toString() + " is not a valid group address"); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 357 | igmpStatisticsManager.getIgmpStats().increaseFailJoinReqUnknownMulticastIpCounter(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | Ip4Address srcIp = getDeviceIp(deviceId); |
| 361 | |
| 362 | byte recordType = igmpGroup.getRecordType(); |
| 363 | boolean join = false; |
| 364 | |
| 365 | ArrayList<Ip4Address> sourceList = new ArrayList<>(); |
| 366 | |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 367 | if (!validMembershipModes.contains(recordType)) { |
| 368 | igmpStatisticsManager.getIgmpStats().increaseReportsRxWithWrongModeCounter(); |
| 369 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 370 | if (igmpGroup.getSources().size() > 0) { |
| 371 | igmpGroup.getSources().forEach(source -> sourceList.add(source.getIp4Address())); |
| 372 | if (recordType == IGMPMembership.CHANGE_TO_EXCLUDE_MODE || |
| 373 | recordType == IGMPMembership.MODE_IS_EXCLUDE || |
| 374 | recordType == IGMPMembership.BLOCK_OLD_SOURCES) { |
| 375 | join = false; |
| 376 | } else if (recordType == IGMPMembership.CHANGE_TO_INCLUDE_MODE || |
| 377 | recordType == IGMPMembership.MODE_IS_INCLUDE || |
| 378 | recordType == IGMPMembership.ALLOW_NEW_SOURCES) { |
| 379 | join = true; |
| 380 | } |
| 381 | } else { |
ke han | 29af27b | 2017-09-08 10:29:12 +0800 | [diff] [blame] | 382 | IpAddress src = null; |
| 383 | if (pimSSmInterworking) { |
| 384 | src = ssmTranslateRoute(groupIp); |
| 385 | if (src == null) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 386 | log.info("no ssm translate for group {}", groupIp); |
ke han | 29af27b | 2017-09-08 10:29:12 +0800 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | } else { |
| 390 | src = IpAddress.valueOf(DEFAULT_PIMSSM_HOST); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 391 | } |
| 392 | sourceList.add(src.getIp4Address()); |
| 393 | if (recordType == IGMPMembership.CHANGE_TO_EXCLUDE_MODE || |
| 394 | recordType == IGMPMembership.MODE_IS_EXCLUDE || |
| 395 | recordType == IGMPMembership.BLOCK_OLD_SOURCES) { |
| 396 | join = true; |
| 397 | } else if (recordType == IGMPMembership.CHANGE_TO_INCLUDE_MODE || |
| 398 | recordType == IGMPMembership.MODE_IS_INCLUDE || |
| 399 | recordType == IGMPMembership.ALLOW_NEW_SOURCES) { |
| 400 | join = false; |
| 401 | } |
| 402 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 403 | GroupMemberId groupMemberKey = GroupMemberId.of(groupIp, deviceId, portNumber); |
| 404 | GroupMember groupMember = groupMemberStore.getGroupMember(groupMemberKey); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 405 | |
| 406 | if (join) { |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 407 | igmpStatisticsManager.getIgmpStats().increaseIgmpJoinReq(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 408 | if (groupMember == null) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 409 | Optional<ConnectPoint> sourceConfigured = getSource(); |
| 410 | if (!sourceConfigured.isPresent()) { |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 411 | igmpStatisticsManager.getIgmpStats().increaseIgmpFailJoinReq(); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 412 | log.warn("Unable to process IGMP Join from {} since no source " + |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 413 | "configuration is found.", deviceId); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 414 | igmpStatisticsManager.getIgmpStats().increaseFailJoinReqInsuffPermissionAccessCounter(); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 415 | return; |
| 416 | } |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 417 | |
| 418 | Optional<PortNumber> deviceUplink = getDeviceUplink(deviceId); |
| 419 | if (deviceUplink.isEmpty()) { |
| 420 | log.warn("Unable to process IGMP Join since uplink port " + |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 421 | "of the device {} is not found.", deviceId); |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | if (igmpType == IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT) { |
| 426 | groupMember = new GroupMember(groupIp, vlan, deviceId, portNumber, true); |
| 427 | } else { |
| 428 | groupMember = new GroupMember(groupIp, vlan, deviceId, portNumber, false); |
| 429 | } |
| 430 | |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 431 | HashSet<ConnectPoint> sourceConnectPoints = Sets.newHashSet(sourceConfigured.get()); |
| 432 | |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 433 | boolean isJoined = stateMachineService.join(deviceId, groupIp, srcIp, deviceUplink.get()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 434 | if (isJoined) { |
| 435 | igmpStatisticsManager.getIgmpStats().increaseIgmpSuccessJoinRejoinReq(); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 436 | igmpStatisticsManager.getIgmpStats().increaseIgmpChannelJoinCounter(); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 437 | } else { |
| 438 | igmpStatisticsManager.getIgmpStats().increaseIgmpFailJoinReq(); |
| 439 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 440 | groupMemberStore.putGroupMember(groupMember); |
| 441 | log.debug("Group member created with id: {}", groupMember.getGroupMemberId()); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 442 | groupMember.updateList(recordType, sourceList); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 443 | groupMember.getSourceList().forEach(source -> { |
| 444 | McastRoute route = new McastRoute(source, groupIp, McastRoute.Type.IGMP); |
| 445 | //add route |
| 446 | multicastService.add(route); |
| 447 | //add source to the route |
| 448 | multicastService.addSources(route, Sets.newHashSet(sourceConnectPoints)); |
| 449 | //add sink to the route |
| 450 | multicastService.addSinks(route, Sets.newHashSet(cp)); |
| 451 | }); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 452 | igmpStatisticsManager.getIgmpStats().increaseUnconfiguredGroupCounter(); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 453 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 454 | } |
| 455 | groupMember.resetAllTimers(); |
| 456 | groupMember.updateList(recordType, sourceList); |
| 457 | groupMember.setLeave(false); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 458 | //put updated member to the store |
| 459 | groupMemberStore.putGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 460 | } else { |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 461 | igmpStatisticsManager.getIgmpStats().increaseIgmpLeaveReq(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 462 | if (groupMember == null) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 463 | log.info("receive leave but no instance, group {} device: {} port:{}", |
| 464 | groupIp, deviceId, portNumber); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 465 | igmpStatisticsManager.getIgmpStats().increaseUnconfiguredGroupCounter(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 466 | return; |
| 467 | } else { |
| 468 | groupMember.setLeave(true); |
| 469 | if (fastLeave) { |
| 470 | leaveAction(groupMember); |
| 471 | } else { |
| 472 | sendQuery(groupMember); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 473 | //put modified group member object to the store |
| 474 | groupMemberStore.updateGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | private void leaveAction(GroupMember groupMember) { |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 481 | igmpStatisticsManager.getIgmpStats().increaseIgmpDisconnect(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 482 | ConnectPoint cp = new ConnectPoint(groupMember.getDeviceId(), groupMember.getPortNumber()); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 483 | stateMachineService.leave(groupMember.getDeviceId(), groupMember.getGroupIp()); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 484 | groupMember.getSourceList().forEach(source -> multicastService.removeSinks( |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 485 | new McastRoute(source, groupMember.getGroupIp(), |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 486 | McastRoute.Type.IGMP), Sets.newHashSet(cp))); |
| 487 | groupMemberStore.removeGroupMember(groupMember.getGroupMemberId()); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private void sendQuery(GroupMember groupMember) { |
| 491 | Ethernet ethpkt; |
| 492 | Ip4Address srcIp = getDeviceIp(groupMember.getDeviceId()); |
| 493 | if (groupMember.getv2()) { |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 494 | ethpkt = IgmpSender.getInstance().buildIgmpV2Query(groupMember.getGroupIp(), |
| 495 | srcIp, groupMember.getvlan().toShort()); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 496 | } else { |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 497 | ethpkt = IgmpSender.getInstance().buildIgmpV3Query(groupMember.getGroupIp(), |
| 498 | srcIp, groupMember.getvlan().toShort()); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 499 | } |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 500 | log.debug("Sending IGMP query to {}/{} for group {}: {}", |
| 501 | groupMember.getDeviceId(), groupMember.getPortNumber(), groupMember.getGroupIp(), ethpkt); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 502 | IgmpSender.getInstance().sendIgmpPacket(ethpkt, groupMember.getDeviceId(), groupMember.getPortNumber()); |
| 503 | } |
| 504 | |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 505 | /** |
| 506 | * @return connect point of the source if configured; and empty Optional otherwise. |
| 507 | */ |
| 508 | public static Optional<ConnectPoint> getSource() { |
| 509 | return sourceDeviceAndPort == null ? Optional.empty() : |
| 510 | Optional.of(sourceDeviceAndPort); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Packet processor responsible for forwarding packets along their paths. |
| 515 | */ |
| 516 | private class IgmpPacketProcessor implements PacketProcessor { |
| 517 | @Override |
| 518 | public void process(PacketContext context) { |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 519 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 520 | eventExecutor.execute(() -> { |
| 521 | try { |
| 522 | InboundPacket pkt = context.inPacket(); |
| 523 | Ethernet ethPkt = pkt.parsed(); |
| 524 | if (ethPkt == null) { |
| 525 | return; |
| 526 | } |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 527 | igmpStatisticsManager.getIgmpStats().increaseTotalMsgReceived(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 528 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 529 | if (ethPkt.getEtherType() != Ethernet.TYPE_IPV4) { |
| 530 | return; |
| 531 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 532 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 533 | IPv4 ipv4Pkt = (IPv4) ethPkt.getPayload(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 534 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 535 | if (ipv4Pkt.getProtocol() != IPv4.PROTOCOL_IGMP) { |
| 536 | return; |
| 537 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 538 | |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 539 | igmpStatisticsManager.getIgmpStats().increaseIgmpValidChecksumCounter(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 540 | short vlan = ethPkt.getVlanID(); |
| 541 | DeviceId deviceId = pkt.receivedFrom().deviceId(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 542 | |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 543 | if (!isConnectPoint(deviceId, pkt.receivedFrom().port()) && |
| 544 | !getSubscriberAndDeviceInformation(deviceId).isPresent()) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 545 | log.error("Device not registered in netcfg : {}", deviceId); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 546 | igmpStatisticsManager.getIgmpStats().increaseFailJoinReqInsuffPermissionAccessCounter(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 547 | return; |
| 548 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 549 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 550 | IGMP igmp = (IGMP) ipv4Pkt.getPayload(); |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 551 | |
| 552 | Optional<PortNumber> deviceUpLinkOpt = getDeviceUplink(deviceId); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 553 | PortNumber upLinkPort = deviceUpLinkOpt.isPresent() ? deviceUpLinkOpt.get() : null; |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 554 | switch (igmp.getIgmpType()) { |
| 555 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_QUERY: |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 556 | igmpStatisticsManager.getIgmpStats().increaseIgmpv3MembershipQuery(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 557 | //Discard Query from OLT’s non-uplink port’s |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 558 | if (!pkt.receivedFrom().port().equals(upLinkPort)) { |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 559 | if (isConnectPoint(deviceId, pkt.receivedFrom().port())) { |
| 560 | log.info("IGMP Picked up query from connectPoint"); |
| 561 | //OK to process packet |
| 562 | processIgmpConnectPointQuery((IGMPQuery) igmp.getGroups().get(0), |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 563 | pkt.receivedFrom(), |
| 564 | 0xff & igmp.getMaxRespField()); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 565 | break; |
| 566 | } else { |
| 567 | //Not OK to process packet |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 568 | log.warn("IGMP Picked up query from non-uplink port {}", upLinkPort); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 572 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 573 | processIgmpQuery((IGMPQuery) igmp.getGroups().get(0), pkt.receivedFrom(), |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 574 | 0xff & igmp.getMaxRespField()); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 575 | break; |
| 576 | case IGMP.TYPE_IGMPV1_MEMBERSHIP_REPORT: |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 577 | igmpStatisticsManager.getIgmpStats().increaseIgmpv1MembershipReport(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 578 | log.debug("IGMP version 1 message types are not currently supported."); |
| 579 | break; |
| 580 | case IGMP.TYPE_IGMPV3_MEMBERSHIP_REPORT: |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 581 | igmpStatisticsManager.getIgmpStats().increaseIgmpv3MembershipReport(); |
| 582 | processIgmpMessage(pkt, igmp, upLinkPort, vlan); |
| 583 | break; |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 584 | case IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT: |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 585 | igmpStatisticsManager.getIgmpStats().increaseIgmpv2MembershipReport(); |
| 586 | processIgmpMessage(pkt, igmp, upLinkPort, vlan); |
| 587 | break; |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 588 | case IGMP.TYPE_IGMPV2_LEAVE_GROUP: |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 589 | igmpStatisticsManager.getIgmpStats().increaseIgmpv2LeaveGroup(); |
| 590 | processIgmpMessage(pkt, igmp, upLinkPort, vlan); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 591 | break; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 592 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 593 | default: |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 594 | log.warn("Unknown IGMP message type: {}", igmp.getIgmpType()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 595 | igmpStatisticsManager.getIgmpStats().increaseInvalidIgmpMsgReceived(); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 596 | igmpStatisticsManager.getIgmpStats().increaseUnknownIgmpTypePacketsRxCounter(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 597 | break; |
| 598 | } |
| 599 | |
| 600 | } catch (Exception ex) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 601 | log.error("igmp process error : ", ex); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 602 | } |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 603 | }); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 607 | private void processIgmpMessage(InboundPacket pkt, IGMP igmp, PortNumber upLinkPort, short vlan) { |
| 608 | //Discard join/leave from OLT’s uplink port’s |
| 609 | if (pkt.receivedFrom().port().equals(upLinkPort) || |
| 610 | isConnectPoint(pkt.receivedFrom().deviceId(), pkt.receivedFrom().port())) { |
| 611 | log.info("IGMP Picked up join/leave from uplink/connectPoint port"); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | Iterator<IGMPGroup> itr = igmp.getGroups().iterator(); |
| 616 | while (itr.hasNext()) { |
| 617 | IGMPGroup group = itr.next(); |
| 618 | if (group instanceof IGMPMembership) { |
| 619 | processIgmpReport((IGMPMembership) group, VlanId.vlanId(vlan), |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 620 | pkt.receivedFrom(), igmp.getIgmpType()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 621 | } else { |
| 622 | IGMPMembership mgroup = new IGMPMembership(group.getGaddr().getIp4Address()); |
| 623 | mgroup.setRecordType(igmp.getIgmpType() == IGMP.TYPE_IGMPV2_MEMBERSHIP_REPORT ? |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 624 | IGMPMembership.MODE_IS_EXCLUDE : |
| 625 | IGMPMembership.MODE_IS_INCLUDE); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 626 | processIgmpReport(mgroup, VlanId.vlanId(vlan), |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 627 | pkt.receivedFrom(), igmp.getIgmpType()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | } |
| 632 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 633 | private class IgmpProxyTimerTask extends TimerTask { |
| 634 | public void run() { |
| 635 | try { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 636 | stateMachineService.timeOut1s(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 637 | queryMembers(); |
| 638 | } catch (Exception ex) { |
| 639 | log.warn("Igmp timer task error : {}", ex.getMessage()); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | private void queryMembers() { |
| 644 | GroupMember groupMember; |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 645 | Set<GroupMemberId> keySet = groupMemberStore.getAllGroupMemberIds(); |
| 646 | for (GroupMemberId key : keySet) { |
| 647 | groupMember = groupMemberStore.getGroupMember(key); |
| 648 | if (groupMember == null) { |
| 649 | continue; |
| 650 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 651 | DeviceId did = groupMember.getDeviceId(); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 652 | if (igmpLeadershipService.isLocalLeader(did)) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 653 | if (groupMember.isLeave()) { |
| 654 | lastQuery(groupMember); |
| 655 | } else if (periodicQuery) { |
| 656 | periodicQuery(groupMember); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | private void lastQuery(GroupMember groupMember) { |
| 663 | if (groupMember.getLastQueryInterval() < lastQueryInterval) { |
| 664 | groupMember.lastQueryInterval(true); // count times |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 665 | //put modified group member object to the store |
| 666 | groupMemberStore.updateGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 667 | } else if (groupMember.getLastQueryCount() < lastQueryCount - 1) { |
| 668 | sendQuery(groupMember); |
| 669 | groupMember.lastQueryInterval(false); // reset count number |
| 670 | groupMember.lastQueryCount(true); //count times |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 671 | //put modified group member object to the store |
| 672 | groupMemberStore.updateGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 673 | } else if (groupMember.getLastQueryCount() == lastQueryCount - 1) { |
| 674 | leaveAction(groupMember); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | private void periodicQuery(GroupMember groupMember) { |
| 679 | if (groupMember.getKeepAliveQueryInterval() < keepAliveInterval) { |
| 680 | groupMember.keepAliveInterval(true); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 681 | //put modified group member object to the store |
| 682 | groupMemberStore.updateGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 683 | } else if (groupMember.getKeepAliveQueryCount() < keepAliveCount) { |
| 684 | sendQuery(groupMember); |
| 685 | groupMember.keepAliveInterval(false); |
| 686 | groupMember.keepAliveQueryCount(true); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 687 | //put modified group member object to the store |
| 688 | groupMemberStore.updateGroupMember(groupMember); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 689 | } else if (groupMember.getKeepAliveQueryCount() == keepAliveCount) { |
| 690 | leaveAction(groupMember); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | } |
| 695 | |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 696 | public Optional<PortNumber> getDeviceUplink(DeviceId devId) { |
| 697 | Device device = deviceService.getDevice(devId); |
| 698 | if (device == null || device.serialNumber() == null) { |
| 699 | return Optional.empty(); |
Deepa Vaddireddy | bcd5235 | 2017-09-21 05:04:48 +0000 | [diff] [blame] | 700 | } |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 701 | Optional<SubscriberAndDeviceInformation> olt = getSubscriberAndDeviceInformation(device.serialNumber()); |
| 702 | if (olt.isEmpty()) { |
| 703 | return Optional.empty(); |
| 704 | } |
| 705 | PortNumber portNumber = PortNumber.portNumber(olt.get().uplinkPort()); |
| 706 | return validateUpLinkPort(device.id(), portNumber) ? |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 707 | Optional.of(portNumber) : Optional.empty(); |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 711 | * @param deviceId device id |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 712 | * @param portNumber port number |
| 713 | * @return true if the port name starts with NNI_PREFIX; false otherwise. |
| 714 | */ |
| 715 | public boolean validateUpLinkPort(DeviceId deviceId, PortNumber portNumber) { |
| 716 | Port port = deviceService.getPort(deviceId, portNumber); |
| 717 | if (port == null) { |
| 718 | //port is not discovered by ONOS; so cannot validate it. |
| 719 | return false; |
| 720 | } |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 721 | boolean isValid = port.annotations().value(AnnotationKeys.PORT_NAME) != null && |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 722 | port.annotations().value(AnnotationKeys.PORT_NAME).startsWith(NNI_PREFIX); |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 723 | if (!isValid) { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 724 | log.warn("Port cannot be validated; it is not configured as an NNI port. Device/port: {}/{}", |
| 725 | deviceId, portNumber); |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 726 | } |
| 727 | return isValid; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 728 | } |
| 729 | |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 730 | public static boolean isIgmpOnPodBasis() { |
| 731 | return igmpOnPodBasis; |
| 732 | } |
| 733 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 734 | private void processFilterObjective(DeviceId devId, PortNumber port, boolean remove) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 735 | if (!enableIgmpProvisioning) { |
| 736 | log.debug("IGMP trap rules won't be installed since enableIgmpProvisioning flag is not set"); |
| 737 | return; |
| 738 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 739 | //TODO migrate to packet requests when packet service uses filtering objectives |
| 740 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder(); |
| 741 | |
| 742 | builder = remove ? builder.deny() : builder.permit(); |
| 743 | |
| 744 | FilteringObjective igmp = builder |
| 745 | .withKey(Criteria.matchInPort(port)) |
| 746 | .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType())) |
| 747 | .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP)) |
| 748 | .withMeta(DefaultTrafficTreatment.builder().setOutput(PortNumber.CONTROLLER).build()) |
| 749 | .fromApp(appId) |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 750 | .withPriority(MAX_PRIORITY) |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 751 | .add(new ObjectiveContext() { |
| 752 | @Override |
| 753 | public void onSuccess(Objective objective) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 754 | log.info("Igmp filter for {} on {} {}.", |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 755 | devId, port, (remove) ? REMOVED : INSTALLED); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | @Override |
| 759 | public void onError(Objective objective, ObjectiveError error) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 760 | log.info("Igmp filter {} for device {} on port {} failed because of {}", |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 761 | (remove) ? INSTALLATION : REMOVAL, devId, port, |
| 762 | error); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 763 | } |
| 764 | }); |
| 765 | |
| 766 | flowObjectiveService.filter(devId, igmp); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 767 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | private boolean isConnectPoint(DeviceId device, PortNumber port) { |
Deepa Vaddireddy | 01f33b4 | 2017-07-02 13:26:53 +0530 | [diff] [blame] | 771 | if (connectPoint != null) { |
| 772 | return (connectPointMode && connectPoint.deviceId().equals(device) |
| 773 | && connectPoint.port().equals(port)); |
| 774 | } else { |
| 775 | log.info("connectPoint not configured for device {}", device); |
| 776 | return false; |
| 777 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 778 | } |
Deepa Vaddireddy | 01f33b4 | 2017-07-02 13:26:53 +0530 | [diff] [blame] | 779 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 780 | private boolean isUplink(DeviceId device, PortNumber port) { |
Esin Karaman | ce5ce51 | 2020-02-25 15:58:14 +0000 | [diff] [blame] | 781 | if (connectPointMode) { |
| 782 | return false; |
| 783 | } |
| 784 | Optional<PortNumber> upLinkPort = getDeviceUplink(device); |
| 785 | return upLinkPort.isPresent() && upLinkPort.get().equals(port); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 786 | } |
| 787 | |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 788 | /** |
| 789 | * Fetches device information associated with the device serial number from SADIS. |
| 790 | * |
| 791 | * @param serialNumber serial number of a device |
| 792 | * @return device information; an empty Optional otherwise. |
| 793 | */ |
| 794 | private Optional<SubscriberAndDeviceInformation> getSubscriberAndDeviceInformation(String serialNumber) { |
| 795 | long start = System.currentTimeMillis(); |
| 796 | try { |
| 797 | return Optional.ofNullable(sadisService.getSubscriberInfoService().get(serialNumber)); |
| 798 | } finally { |
| 799 | if (log.isDebugEnabled()) { |
| 800 | // SADIS can call remote systems to fetch device data and this calls can take a long time. |
| 801 | // This measurement is just for monitoring these kinds of situations. |
| 802 | log.debug("Device fetched from SADIS. Elapsed {} msec", System.currentTimeMillis() - start); |
| 803 | } |
| 804 | |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Fetches device information associated with the device serial number from SADIS. |
| 810 | * |
| 811 | * @param deviceId device id |
| 812 | * @return device information; an empty Optional otherwise. |
| 813 | */ |
| 814 | private Optional<SubscriberAndDeviceInformation> getSubscriberAndDeviceInformation(DeviceId deviceId) { |
| 815 | Device device = deviceService.getDevice(deviceId); |
| 816 | if (device == null || device.serialNumber() == null) { |
| 817 | return Optional.empty(); |
| 818 | } |
| 819 | return getSubscriberAndDeviceInformation(device.serialNumber()); |
| 820 | } |
| 821 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 822 | private class InternalDeviceListener implements DeviceListener { |
| 823 | @Override |
| 824 | public void event(DeviceEvent event) { |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 825 | eventExecutor.execute(() -> { |
| 826 | DeviceId devId = event.subject().id(); |
| 827 | Port p = event.port(); |
Deepa Vaddireddy | ca7b25d | 2017-09-28 13:47:18 +0000 | [diff] [blame] | 828 | |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 829 | if (!igmpLeadershipService.isLocalLeader(devId)) { |
| 830 | return; |
| 831 | } |
| 832 | if (getSubscriberAndDeviceInformation(devId).isEmpty() && |
| 833 | !(p != null && isConnectPoint(devId, p.number()))) { |
| 834 | return; |
| 835 | } |
| 836 | PortNumber port; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 837 | |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 838 | switch (event.type()) { |
| 839 | |
| 840 | case DEVICE_ADDED: |
| 841 | case DEVICE_UPDATED: |
| 842 | case DEVICE_REMOVED: |
| 843 | case DEVICE_SUSPENDED: |
| 844 | case DEVICE_AVAILABILITY_CHANGED: |
| 845 | case PORT_STATS_UPDATED: |
| 846 | break; |
| 847 | case PORT_ADDED: |
| 848 | port = p.number(); |
| 849 | if (getSubscriberAndDeviceInformation(devId).isPresent() && |
| 850 | !isUplink(devId, port) && !isConnectPoint(devId, port)) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 851 | processFilterObjective(devId, port, false); |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 852 | } else if (isUplink(devId, port)) { |
| 853 | provisionUplinkFlows(); |
| 854 | } else if (isConnectPoint(devId, port)) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 855 | provisionConnectPointFlows(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 856 | } |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 857 | onSourceStateChanged(devId, port, true); |
| 858 | break; |
| 859 | case PORT_UPDATED: |
| 860 | port = p.number(); |
| 861 | if (getSubscriberAndDeviceInformation(devId).isPresent() && |
| 862 | !isUplink(devId, port) && !isConnectPoint(devId, port)) { |
| 863 | processFilterObjective(devId, port, !event.port().isEnabled()); |
| 864 | } else if (isUplink(devId, port)) { |
| 865 | if (event.port().isEnabled()) { |
| 866 | provisionUplinkFlows(devId); |
| 867 | } else { |
| 868 | processFilterObjective(devId, port, true); |
| 869 | } |
| 870 | } else if (isConnectPoint(devId, port)) { |
| 871 | if (event.port().isEnabled()) { |
| 872 | provisionConnectPointFlows(); |
| 873 | } else { |
| 874 | unprovisionConnectPointFlows(); |
| 875 | } |
| 876 | } |
| 877 | onSourceStateChanged(devId, port, event.port().isEnabled()); |
| 878 | break; |
| 879 | case PORT_REMOVED: |
| 880 | port = p.number(); |
| 881 | processFilterObjective(devId, port, true); |
| 882 | onSourceStateChanged(devId, port, false); |
| 883 | break; |
| 884 | default: |
| 885 | log.info("Unknown device event {}", event.type()); |
| 886 | break; |
| 887 | } |
| 888 | }); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | @Override |
| 892 | public boolean isRelevant(DeviceEvent event) { |
| 893 | return true; |
| 894 | } |
| 895 | } |
| 896 | |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 897 | private Set<McastRoute> multicastRoutesOfIgmpProxy() { |
| 898 | Set<McastRoute> routes = new HashSet<>(); //using set to eliminate multiple entities |
| 899 | groupMemberStore.getAllGroupMemberIds().forEach(groupMemberId -> { |
| 900 | GroupMember groupMember = groupMemberStore.getGroupMember(groupMemberId); |
| 901 | if (groupMember != null) { |
| 902 | groupMember.getSourceList().forEach(source -> { |
| 903 | //regenerate the routes created by this application |
| 904 | routes.add(new McastRoute(source, groupMemberId.getGroupIp(), McastRoute.Type.IGMP)); |
| 905 | }); |
| 906 | } |
| 907 | }); |
| 908 | return routes; |
| 909 | } |
| 910 | |
| 911 | private void onSourceStateChanged(DeviceId deviceId, PortNumber portNumber, boolean enabled) { |
| 912 | if (!(getSource().isPresent() && |
| 913 | getSource().get().deviceId().equals(deviceId) && |
| 914 | getSource().get().port().equals(portNumber))) { |
| 915 | //connect point is not configured as the source |
| 916 | log.debug("{}/{} is not the source cp. Stopped processing it further", deviceId, portNumber); |
| 917 | return; |
| 918 | } |
| 919 | log.info("source device:port is {}. DeviceId={}, portNumber={}", |
| 920 | (enabled ? "enabled. Restoring the source" : |
| 921 | "disabled. Deleting it from multicast routes"), deviceId, portNumber); |
| 922 | |
| 923 | Set<McastRoute> routes = multicastRoutesOfIgmpProxy(); |
| 924 | routes.forEach(route -> { |
| 925 | if (enabled) { |
| 926 | //add source to the route |
| 927 | multicastService.addSources(route, Sets.newHashSet(new ConnectPoint(deviceId, portNumber))); |
| 928 | } else { |
| 929 | //remove the source from the route |
| 930 | multicastService.removeSources(route, Sets.newHashSet(new ConnectPoint(deviceId, portNumber))); |
| 931 | } |
| 932 | }); |
| 933 | } |
| 934 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 935 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 936 | |
| 937 | private void reconfigureNetwork(IgmpproxyConfig cfg) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 938 | IgmpproxyConfig newCfg = cfg == null ? new IgmpproxyConfig() : cfg; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 939 | |
| 940 | unSolicitedTimeout = newCfg.unsolicitedTimeOut(); |
| 941 | maxResp = newCfg.maxResp(); |
| 942 | keepAliveInterval = newCfg.keepAliveInterval(); |
| 943 | keepAliveCount = newCfg.keepAliveCount(); |
| 944 | lastQueryInterval = newCfg.lastQueryInterval(); |
| 945 | lastQueryCount = newCfg.lastQueryCount(); |
| 946 | withRAUplink = newCfg.withRAUplink(); |
| 947 | withRADownlink = newCfg.withRADownlink(); |
| 948 | igmpCos = newCfg.igmpCos(); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 949 | igmpUniCos = newCfg.igmpUniCos(); // defines priority bit of IGMP query message sent to UNI ports |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 950 | periodicQuery = newCfg.periodicQuery(); |
| 951 | fastLeave = newCfg.fastLeave(); |
ke han | 29af27b | 2017-09-08 10:29:12 +0800 | [diff] [blame] | 952 | pimSSmInterworking = newCfg.pimSsmInterworking(); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 953 | enableIgmpProvisioning = newCfg.enableIgmpProvisioning(); |
Esin Karaman | b38700c | 2019-09-17 13:01:25 +0000 | [diff] [blame] | 954 | igmpOnPodBasis = newCfg.igmpOnPodBasis(); |
Arjun E K | b0018fd | 2020-04-07 13:26:40 +0000 | [diff] [blame] | 955 | if (newCfg.outgoingIgmpWithV3() != null && |
| 956 | outgoingIgmpWithV3 != newCfg.outgoingIgmpWithV3()) { |
| 957 | outgoingIgmpWithV3 = newCfg.outgoingIgmpWithV3(); |
| 958 | } |
Deepa Vaddireddy | ca7b25d | 2017-09-28 13:47:18 +0000 | [diff] [blame] | 959 | |
| 960 | if (connectPointMode != newCfg.connectPointMode() || |
| 961 | connectPoint != newCfg.connectPoint()) { |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 962 | connectPointMode = newCfg.connectPointMode(); |
Deepa Vaddireddy | ca7b25d | 2017-09-28 13:47:18 +0000 | [diff] [blame] | 963 | connectPoint = newCfg.connectPoint(); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 964 | if (connectPointMode) { |
| 965 | unprovisionUplinkFlows(); |
| 966 | provisionConnectPointFlows(); |
| 967 | } else { |
| 968 | unprovisionConnectPointFlows(); |
| 969 | provisionUplinkFlows(); |
| 970 | } |
| 971 | } |
| 972 | if (connectPoint != null) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 973 | log.info("connect point : {}", connectPoint); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 974 | } |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 975 | log.info("mode: {}", connectPointMode); |
| 976 | |
| 977 | getSourceConnectPoint(newCfg); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 978 | |
| 979 | IgmpSender.getInstance().setIgmpCos(igmpCos); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 980 | IgmpSender.getInstance().setIgmpUniCos(igmpUniCos); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 981 | IgmpSender.getInstance().setMaxResp(maxResp); |
| 982 | IgmpSender.getInstance().setMvlan(mvlan); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 983 | IgmpSender.getInstance().setMvlanInner(mvlanInner); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 984 | IgmpSender.getInstance().setWithRADownlink(withRADownlink); |
| 985 | IgmpSender.getInstance().setWithRAUplink(withRAUplink); |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 986 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 987 | |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 988 | void getSourceConnectPoint(IgmpproxyConfig cfg) { |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 989 | ConnectPoint oldSourceDevPort = sourceDeviceAndPort; |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 990 | sourceDeviceAndPort = cfg.getSourceDeviceAndPort(); |
| 991 | if (sourceDeviceAndPort != null) { |
| 992 | log.debug("source parameter configured to {}", sourceDeviceAndPort); |
| 993 | } |
Esin Karaman | 09b41e5 | 2020-06-01 10:52:55 +0000 | [diff] [blame^] | 994 | if (oldSourceDevPort != null && !oldSourceDevPort.equals(sourceDeviceAndPort)) { |
| 995 | //source config has changed, remove the old source from multicast routes |
| 996 | onSourceStateChanged(oldSourceDevPort.deviceId(), oldSourceDevPort.port(), false); |
| 997 | } |
| 998 | if (sourceDeviceAndPort != null && !sourceDeviceAndPort.equals(oldSourceDevPort)) { |
| 999 | //add new source to the existing routes |
| 1000 | onSourceStateChanged(sourceDeviceAndPort.deviceId(), sourceDeviceAndPort.port(), true); |
| 1001 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | public void reconfigureSsmTable(IgmpproxySsmTranslateConfig cfg) { |
| 1005 | if (cfg == null) { |
| 1006 | return; |
| 1007 | } |
| 1008 | Collection<McastRoute> translations = cfg.getSsmTranslations(); |
| 1009 | for (McastRoute route : translations) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 1010 | ssmTranslateTable.put(route.group().getIp4Address(), route.source().get().getIp4Address()); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | @Override |
| 1015 | public void event(NetworkConfigEvent event) { |
| 1016 | switch (event.type()) { |
| 1017 | case CONFIG_ADDED: |
| 1018 | case CONFIG_UPDATED: |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 1019 | // NOTE how to know if something has changed in sadis? |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1020 | |
| 1021 | if (event.configClass().equals(IGMPPROXY_CONFIG_CLASS)) { |
| 1022 | IgmpproxyConfig config = networkConfig.getConfig(appId, IGMPPROXY_CONFIG_CLASS); |
| 1023 | if (config != null) { |
Esin Karaman | eff1039 | 2019-06-27 18:09:13 +0000 | [diff] [blame] | 1024 | log.info("igmpproxy config received. {}", config); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1025 | reconfigureNetwork(config); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (event.configClass().equals(IGMPPROXY_SSM_CONFIG_CLASS)) { |
| 1030 | IgmpproxySsmTranslateConfig config = networkConfig.getConfig(appId, IGMPPROXY_SSM_CONFIG_CLASS); |
| 1031 | if (config != null) { |
| 1032 | reconfigureSsmTable(config); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | if (event.configClass().equals(MCAST_CONFIG_CLASS)) { |
| 1037 | McastConfig config = networkConfig.getConfig(coreAppId, MCAST_CONFIG_CLASS); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 1038 | boolean vlanConfigChanged = config != null && mvlan != config.egressVlan().toShort(); |
| 1039 | boolean innerVlanConfigChanged = config != null && |
| 1040 | mvlanInner != config.egressInnerVlan().toShort(); |
| 1041 | |
| 1042 | if (vlanConfigChanged || innerVlanConfigChanged) { |
| 1043 | log.info("igmpproxy vlan config received. {}", config); |
| 1044 | //at least one of the vlan configs has changed. Call leave before setting new values |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 1045 | groupMemberStore.getAllGroupMembers().forEach(m -> leaveAction(m)); |
Esin Karaman | 586f1d6 | 2020-06-04 10:15:34 +0000 | [diff] [blame] | 1046 | if (vlanConfigChanged) { |
| 1047 | mvlan = config.egressVlan().toShort(); |
| 1048 | IgmpSender.getInstance().setMvlan(mvlan); |
| 1049 | } |
| 1050 | if (innerVlanConfigChanged) { |
| 1051 | mvlanInner = config.egressInnerVlan().toShort(); |
| 1052 | IgmpSender.getInstance().setMvlanInner(mvlanInner); |
| 1053 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | log.info("Reconfigured"); |
| 1058 | break; |
| 1059 | case CONFIG_REGISTERED: |
| 1060 | case CONFIG_UNREGISTERED: |
| 1061 | break; |
| 1062 | case CONFIG_REMOVED: |
Matteo Scandolo | 7482bbe | 2020-02-12 14:37:09 -0800 | [diff] [blame] | 1063 | // NOTE how to know if something has changed in sadis? |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1064 | default: |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1070 | private void provisionUplinkFlows(DeviceId deviceId) { |
| 1071 | if (connectPointMode) { |
| 1072 | return; |
| 1073 | } |
| 1074 | |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 1075 | Optional<PortNumber> upLink = getDeviceUplink(deviceId); |
| 1076 | if (upLink.isPresent()) { |
| 1077 | processFilterObjective(deviceId, upLink.get(), false); |
| 1078 | } |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | private void provisionUplinkFlows() { |
| 1082 | if (connectPointMode) { |
| 1083 | return; |
| 1084 | } |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 1085 | deviceService.getAvailableDevices().forEach(device -> { |
| 1086 | Optional<SubscriberAndDeviceInformation> accessDevice = getSubscriberAndDeviceInformation(device.id()); |
| 1087 | if (accessDevice.isPresent()) { |
| 1088 | provisionUplinkFlows(device.id()); |
| 1089 | } |
| 1090 | }); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1091 | } |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 1092 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1093 | private void unprovisionUplinkFlows() { |
Esin Karaman | 00e16b7 | 2020-02-21 10:32:39 +0000 | [diff] [blame] | 1094 | deviceService.getAvailableDevices().forEach(device -> { |
| 1095 | Optional<SubscriberAndDeviceInformation> accessDevices = getSubscriberAndDeviceInformation(device.id()); |
| 1096 | if (accessDevices.isPresent()) { |
| 1097 | Optional<PortNumber> upLink = getDeviceUplink(device.id()); |
| 1098 | if (upLink.isPresent()) { |
| 1099 | processFilterObjective(device.id(), upLink.get(), true); |
| 1100 | } |
| 1101 | } |
| 1102 | }); |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | private void provisionConnectPointFlows() { |
| 1106 | if ((!connectPointMode) || connectPoint == null) { |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | processFilterObjective(connectPoint.deviceId(), connectPoint.port(), false); |
| 1111 | } |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame] | 1112 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1113 | private void unprovisionConnectPointFlows() { |
| 1114 | if (connectPoint == null) { |
| 1115 | return; |
| 1116 | } |
| 1117 | processFilterObjective(connectPoint.deviceId(), connectPoint.port(), true); |
| 1118 | } |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 1119 | |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 1120 | } |