Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1 | /* |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [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 | */ |
Matteo Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 16 | package org.opencord.dhcpl2relay.impl; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 17 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 22 | import org.onlab.packet.DHCP; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 24 | import org.onlab.packet.IPv4; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 27 | import org.onlab.packet.TpPort; |
| 28 | import org.onlab.packet.UDP; |
| 29 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 30 | import org.onlab.packet.dhcp.DhcpOption; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 31 | import org.onlab.util.Tools; |
| 32 | import org.onosproject.cfg.ComponentConfigService; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 35 | import org.onosproject.event.AbstractListenerManager; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 36 | import org.onosproject.mastership.MastershipEvent; |
| 37 | import org.onosproject.mastership.MastershipListener; |
| 38 | import org.onosproject.mastership.MastershipService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 39 | import org.onosproject.net.AnnotationKeys; |
| 40 | import org.onosproject.net.ConnectPoint; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 41 | import org.onosproject.net.Device; |
| 42 | import org.onosproject.net.DeviceId; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.Port; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 45 | import org.onosproject.net.PortNumber; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 46 | import org.onosproject.net.config.ConfigFactory; |
| 47 | import org.onosproject.net.config.NetworkConfigEvent; |
| 48 | import org.onosproject.net.config.NetworkConfigListener; |
| 49 | import org.onosproject.net.config.NetworkConfigRegistry; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 50 | import org.onosproject.net.device.DeviceEvent; |
| 51 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceService; |
| 53 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 54 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 55 | import org.onosproject.net.flow.TrafficSelector; |
| 56 | import org.onosproject.net.flow.TrafficTreatment; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 57 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 58 | import org.onosproject.net.host.HostService; |
| 59 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 60 | import org.onosproject.net.packet.OutboundPacket; |
| 61 | import org.onosproject.net.packet.PacketContext; |
| 62 | import org.onosproject.net.packet.PacketPriority; |
| 63 | import org.onosproject.net.packet.PacketProcessor; |
| 64 | import org.onosproject.net.packet.PacketService; |
Matteo Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 65 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 66 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 67 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 68 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 69 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82; |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 70 | import org.opencord.sadis.BaseInformationService; |
| 71 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 72 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 73 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 74 | import org.osgi.service.component.annotations.Activate; |
| 75 | import org.osgi.service.component.annotations.Component; |
| 76 | import org.osgi.service.component.annotations.Deactivate; |
| 77 | import org.osgi.service.component.annotations.Modified; |
| 78 | import org.osgi.service.component.annotations.Reference; |
| 79 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 80 | import org.slf4j.Logger; |
| 81 | import org.slf4j.LoggerFactory; |
| 82 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 83 | import java.nio.ByteBuffer; |
| 84 | import java.util.ArrayList; |
| 85 | import java.util.Arrays; |
| 86 | import java.util.Dictionary; |
| 87 | import java.util.List; |
| 88 | import java.util.Map; |
| 89 | import java.util.Optional; |
| 90 | import java.util.Set; |
| 91 | import java.util.concurrent.atomic.AtomicReference; |
| 92 | import java.util.stream.Collectors; |
| 93 | |
| 94 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 95 | import static org.onlab.packet.MacAddress.valueOf; |
| 96 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 97 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES; |
| 98 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT; |
| 99 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82; |
| 100 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82_DEFAULT; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * DHCP Relay Agent Application Component. |
| 104 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 105 | @Component(immediate = true, |
| 106 | property = { |
| 107 | OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT, |
| 108 | ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT, |
| 109 | }) |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 110 | public class DhcpL2Relay |
| 111 | extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener> |
| 112 | implements DhcpL2RelayService { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 113 | |
| 114 | public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay"; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 115 | private static final String HOST_LOC_PROVIDER = |
| 116 | "org.onosproject.provider.host.impl.HostLocationProvider"; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 117 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 118 | private final InternalConfigListener cfgListener = |
| 119 | new InternalConfigListener(); |
| 120 | |
| 121 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 122 | new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY, |
| 123 | DhcpL2RelayConfig.class, |
| 124 | "dhcpl2relay") { |
| 125 | @Override |
| 126 | public DhcpL2RelayConfig createConfig() { |
| 127 | return new DhcpL2RelayConfig(); |
| 128 | } |
| 129 | } |
| 130 | ); |
| 131 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 133 | protected NetworkConfigRegistry cfgService; |
| 134 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 136 | protected CoreService coreService; |
| 137 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 139 | protected PacketService packetService; |
| 140 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 142 | protected HostService hostService; |
| 143 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 145 | protected ComponentConfigService componentConfigService; |
| 146 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 148 | protected SadisService sadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 149 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 151 | protected DeviceService deviceService; |
| 152 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 154 | protected MastershipService mastershipService; |
| 155 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 157 | protected FlowObjectiveService flowObjectiveService; |
| 158 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 159 | /** Add option 82 to relayed packets */ |
| 160 | protected boolean option82 = OPTION_82_DEFAULT; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 161 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 162 | /** Ask the DHCP Server to send back replies as L2 broadcast */ |
| 163 | protected boolean enableDhcpBroadcastReplies = ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 164 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 165 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = |
| 166 | new DhcpRelayPacketProcessor(); |
| 167 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 168 | private InnerMastershipListener changeListener = new InnerMastershipListener(); |
| 169 | private InnerDeviceListener deviceListener = new InnerDeviceListener(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 170 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 171 | // connect points to the DHCP server |
| 172 | Set<ConnectPoint> dhcpConnectPoints; |
| 173 | private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 174 | private MacAddress dhcpConnectMac = MacAddress.BROADCAST; |
| 175 | private ApplicationId appId; |
| 176 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 177 | static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap(); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 178 | private boolean modifyClientPktsSrcDstMac = false; |
| 179 | //Whether to use the uplink port of the OLTs to send/receive messages to the DHCP server |
| 180 | private boolean useOltUplink = false; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 181 | |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 182 | private BaseInformationService<SubscriberAndDeviceInformation> subsService; |
| 183 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 184 | @Activate |
| 185 | protected void activate(ComponentContext context) { |
| 186 | //start the dhcp relay agent |
| 187 | appId = coreService.registerApplication(DHCP_L2RELAY_APP); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 188 | // ensure that host-learning via dhcp includes IP addresses |
| 189 | componentConfigService.preSetProperty(HOST_LOC_PROVIDER, |
| 190 | "useDhcp", Boolean.TRUE.toString()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 191 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 192 | eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 193 | |
| 194 | cfgService.addListener(cfgListener); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 195 | mastershipService.addListener(changeListener); |
| 196 | deviceService.addListener(deviceListener); |
| 197 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 198 | factories.forEach(cfgService::registerConfigFactory); |
| 199 | //update the dhcp server configuration. |
| 200 | updateConfig(); |
| 201 | //add the packet services. |
| 202 | packetService.addProcessor(dhcpRelayPacketProcessor, |
| 203 | PacketProcessor.director(0)); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 204 | if (context != null) { |
| 205 | modified(context); |
| 206 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 207 | |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 208 | subsService = sadisService.getSubscriberInfoService(); |
| 209 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 210 | log.info("DHCP-L2-RELAY Started"); |
| 211 | } |
| 212 | |
| 213 | @Deactivate |
| 214 | protected void deactivate() { |
| 215 | cfgService.removeListener(cfgListener); |
| 216 | factories.forEach(cfgService::unregisterConfigFactory); |
| 217 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 218 | cancelDhcpPktsFromServer(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 219 | |
| 220 | componentConfigService.unregisterProperties(getClass(), false); |
Deepa Vaddireddy | 77a6ac7 | 2017-09-20 20:36:52 +0530 | [diff] [blame] | 221 | deviceService.removeListener(deviceListener); |
| 222 | mastershipService.removeListener(changeListener); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 223 | eventDispatcher.removeSink(DhcpL2RelayEvent.class); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 224 | log.info("DHCP-L2-RELAY Stopped"); |
| 225 | } |
| 226 | |
| 227 | @Modified |
| 228 | protected void modified(ComponentContext context) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 229 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 230 | Dictionary<?, ?> properties = context.getProperties(); |
| 231 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 232 | Boolean o = Tools.isPropertyEnabled(properties, OPTION_82); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 233 | if (o != null) { |
| 234 | option82 = o; |
| 235 | } |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 236 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 237 | o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_BROADCAST_REPLIES); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 238 | if (o != null) { |
| 239 | enableDhcpBroadcastReplies = o; |
| 240 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Checks if this app has been configured. |
| 245 | * |
| 246 | * @return true if all information we need have been initialized |
| 247 | */ |
| 248 | private boolean configured() { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 249 | if (!useOltUplink) { |
| 250 | return dhcpServerConnectPoint.get() != null; |
| 251 | } |
| 252 | return true; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 255 | /** |
| 256 | * Selects a connect point through an available device for which it is the master. |
| 257 | */ |
| 258 | private void selectServerConnectPoint() { |
| 259 | synchronized (this) { |
| 260 | dhcpServerConnectPoint.set(null); |
| 261 | if (dhcpConnectPoints != null) { |
| 262 | // find a connect point through a device for which we are master |
| 263 | for (ConnectPoint cp: dhcpConnectPoints) { |
| 264 | if (mastershipService.isLocalMaster(cp.deviceId())) { |
| 265 | if (deviceService.isAvailable(cp.deviceId())) { |
| 266 | dhcpServerConnectPoint.set(cp); |
| 267 | } |
| 268 | log.info("DHCP connectPoint selected is {}", cp); |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get()); |
| 275 | |
| 276 | if (dhcpServerConnectPoint.get() == null) { |
| 277 | log.error("Master of none, can't relay DHCP Message to server"); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Updates the network configuration. |
| 284 | */ |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 285 | private void updateConfig() { |
| 286 | DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class); |
| 287 | if (cfg == null) { |
| 288 | log.warn("Dhcp Server info not available"); |
| 289 | return; |
| 290 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 291 | |
| 292 | dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 293 | modifyClientPktsSrcDstMac = cfg.getModifySrcDstMacAddresses(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 294 | boolean prevUseOltUplink = useOltUplink; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 295 | useOltUplink = cfg.getUseOltUplinkForServerPktInOut(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 296 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 297 | if (useOltUplink) { |
| 298 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 299 | log.debug("requestDhcpPackets: ConnectPoint: {}", cp); |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 300 | requestDhcpPacketsFromConnectPoint(cp, null); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 301 | } |
| 302 | // check if previous config was different and so trap flows may |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 303 | // need to be removed from other places like AGG switches |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 304 | if (!prevUseOltUplink) { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 305 | addOrRemoveDhcpTrapFromServer(false); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 306 | } |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 307 | } else { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 308 | // uplink on AGG switch |
| 309 | addOrRemoveDhcpTrapFromServer(true); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | private void cancelDhcpPktsFromServer() { |
| 314 | if (useOltUplink) { |
| 315 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 316 | log.debug("cancelDhcpPackets: ConnectPoint: {}", cp); |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 317 | cancelDhcpPacketsFromConnectPoint(cp, null); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 318 | } |
| 319 | } else { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 320 | // uplink on AGG switch |
| 321 | addOrRemoveDhcpTrapFromServer(false); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 322 | } |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Used to add or remove DHCP trap flow for packets received from DHCP server. |
| 327 | * Typically used on a non OLT device, like an AGG switch. When adding, a |
| 328 | * new dhcp server connect point is selected from the configured options. |
| 329 | * |
| 330 | * @param add true if dhcp trap flow is to be added, false to remove the |
| 331 | * trap flow |
| 332 | */ |
| 333 | private void addOrRemoveDhcpTrapFromServer(boolean add) { |
| 334 | if (add) { |
| 335 | selectServerConnectPoint(); |
| 336 | log.debug("dhcp server connect point: " + dhcpServerConnectPoint); |
| 337 | } |
| 338 | if (dhcpServerConnectPoint.get() == null) { |
| 339 | log.warn("No dhcpServer connectPoint found, cannot {} dhcp trap flows", |
| 340 | (add) ? "install" : "remove"); |
| 341 | return; |
| 342 | } |
| 343 | if (add) { |
| 344 | log.info("Adding trap to dhcp server connect point: " |
| 345 | + dhcpServerConnectPoint); |
| 346 | requestDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(), |
| 347 | Optional.of(PacketPriority.HIGH1)); |
| 348 | } else { |
| 349 | log.info("Removing trap from dhcp server connect point: " |
| 350 | + dhcpServerConnectPoint); |
| 351 | cancelDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(), |
| 352 | Optional.of(PacketPriority.HIGH1)); |
| 353 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 357 | * Returns all the uplink ports of OLTs configured in SADIS. |
| 358 | * Only ports visible in ONOS and for which this instance is master |
| 359 | * are returned |
| 360 | */ |
| 361 | private List<ConnectPoint> getUplinkPortsOfOlts() { |
| 362 | List<ConnectPoint> cps = new ArrayList<>(); |
| 363 | |
| 364 | // find all the olt devices and if their uplink ports are visible |
| 365 | Iterable<Device> devices = deviceService.getDevices(); |
| 366 | for (Device d : devices) { |
| 367 | // check if this device is provisioned in Sadis |
| 368 | |
| 369 | log.debug("getUplinkPortsOfOlts: Checking mastership of {}", d); |
| 370 | // do only for devices for which we are the master |
| 371 | if (!mastershipService.isLocalMaster(d.id())) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | String devSerialNo = d.serialNumber(); |
| 376 | SubscriberAndDeviceInformation deviceInfo = subsService.get(devSerialNo); |
| 377 | log.debug("getUplinkPortsOfOlts: Found device: {}", deviceInfo); |
| 378 | if (deviceInfo != null) { |
| 379 | // check if the uplink port with that number is available on the device |
| 380 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 381 | Port port = deviceService.getPort(d.id(), pNum); |
| 382 | log.debug("getUplinkPortsOfOlts: Found port: {}", port); |
| 383 | if (port != null) { |
| 384 | cps.add(new ConnectPoint(d.id(), pNum)); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return cps; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Returns whether the passed port is the uplink port of the olt device. |
| 393 | */ |
| 394 | private boolean isUplinkPortOfOlt(DeviceId dId, Port p) { |
| 395 | log.debug("isUplinkPortOfOlt: DeviceId: {} Port: {}", dId, p); |
| 396 | // do only for devices for which we are the master |
| 397 | if (!mastershipService.isLocalMaster(dId)) { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | Device d = deviceService.getDevice(dId); |
| 402 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 403 | |
| 404 | if (deviceInfo != null) { |
| 405 | return (deviceInfo.uplinkPort() == p.number().toLong()); |
| 406 | } |
| 407 | |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Returns the connectPoint which is the uplink port of the OLT. |
| 413 | */ |
| 414 | private ConnectPoint getUplinkConnectPointOfOlt(DeviceId dId) { |
| 415 | |
| 416 | Device d = deviceService.getDevice(dId); |
| 417 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 418 | log.debug("getUplinkConnectPointOfOlt DeviceId: {} devInfo: {}", dId, deviceInfo); |
| 419 | if (deviceInfo != null) { |
| 420 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 421 | Port port = deviceService.getPort(d.id(), pNum); |
| 422 | if (port != null) { |
| 423 | return new ConnectPoint(d.id(), pNum); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return null; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Request DHCP packet from particular connect point via PacketService. |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 432 | * Optionally provide a priority for the trap flow. If no such priority is |
| 433 | * provided, the default priority will be used. |
| 434 | * |
| 435 | * @param cp the connect point to trap dhcp packets from |
| 436 | * @param priority of the trap flow, null to use default priority |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 437 | */ |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 438 | private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp, |
| 439 | Optional<PacketPriority> priority) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 440 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 441 | .matchEthType(Ethernet.TYPE_IPV4) |
| 442 | .matchInPort(cp.port()) |
| 443 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 444 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 445 | packetService.requestPackets(selectorServer.build(), |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 446 | priority.isPresent() ? priority.get() : PacketPriority.CONTROL, |
| 447 | appId, Optional.of(cp.deviceId())); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 451 | * Cancel DHCP packet from particular connect point via PacketService. If |
| 452 | * the request was made with a specific packet priority, then the same |
| 453 | * priority should be used in this call. |
| 454 | * |
| 455 | * @param cp the connect point for the trap flow |
| 456 | * @param priority with which the trap flow was requested; if request |
| 457 | * priority was not specified, this param should also be null |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 458 | */ |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 459 | private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp, |
| 460 | Optional<PacketPriority> priority) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 461 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 462 | .matchEthType(Ethernet.TYPE_IPV4) |
| 463 | .matchInPort(cp.port()) |
| 464 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 465 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 466 | packetService.cancelPackets(selectorServer.build(), |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 467 | priority.isPresent() ? priority.get() : PacketPriority.CONTROL, |
| 468 | appId, Optional.of(cp.deviceId())); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 471 | public static Map<String, DhcpAllocationInfo> allocationMap() { |
| 472 | return allocationMap; |
| 473 | } |
| 474 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 475 | private SubscriberAndDeviceInformation getDevice(PacketContext context) { |
| 476 | String serialNo = deviceService.getDevice(context.inPacket(). |
| 477 | receivedFrom().deviceId()).serialNumber(); |
| 478 | |
| 479 | return subsService.get(serialNo); |
| 480 | } |
| 481 | |
| 482 | private SubscriberAndDeviceInformation getDevice(ConnectPoint cp) { |
| 483 | String serialNo = deviceService.getDevice(cp.deviceId()). |
| 484 | serialNumber(); |
| 485 | |
| 486 | return subsService.get(serialNo); |
| 487 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 488 | |
| 489 | private MacAddress relayAgentMacAddress(PacketContext context) { |
| 490 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 491 | SubscriberAndDeviceInformation device = this.getDevice(context); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 492 | if (device == null) { |
| 493 | log.warn("Device not found for {}", context.inPacket(). |
| 494 | receivedFrom()); |
| 495 | return null; |
| 496 | } |
| 497 | |
| 498 | return device.hardwareIdentifier(); |
| 499 | } |
| 500 | |
| 501 | private String nasPortId(PacketContext context) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 502 | return nasPortId(context.inPacket().receivedFrom()); |
| 503 | } |
| 504 | |
| 505 | private String nasPortId(ConnectPoint cp) { |
| 506 | Port p = deviceService.getPort(cp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 507 | return p.annotations().value(AnnotationKeys.PORT_NAME); |
| 508 | } |
| 509 | |
| 510 | private SubscriberAndDeviceInformation getSubscriber(PacketContext context) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 511 | return subsService.get(nasPortId(context)); |
| 512 | } |
| 513 | |
| 514 | private VlanId cTag(PacketContext context) { |
| 515 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 516 | if (sub == null) { |
| 517 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 518 | receivedFrom()); |
| 519 | return VlanId.NONE; |
| 520 | } |
| 521 | return sub.cTag(); |
| 522 | } |
| 523 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 524 | private VlanId cTag(ConnectPoint cp) { |
| 525 | String portId = nasPortId(cp); |
| 526 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 527 | if (sub == null) { |
| 528 | log.warn("Subscriber info not found for {} looking for C-TAG", cp); |
| 529 | return VlanId.NONE; |
| 530 | } |
| 531 | return sub.cTag(); |
| 532 | } |
| 533 | |
| 534 | private VlanId sTag(ConnectPoint cp) { |
| 535 | String portId = nasPortId(cp); |
| 536 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 537 | if (sub == null) { |
| 538 | log.warn("Subscriber info not found for {} looking for S-TAG", cp); |
| 539 | return VlanId.NONE; |
| 540 | } |
| 541 | return sub.sTag(); |
| 542 | } |
| 543 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 544 | private VlanId sTag(PacketContext context) { |
| 545 | SubscriberAndDeviceInformation sub = getSubscriber(context); |
| 546 | if (sub == null) { |
| 547 | log.warn("Subscriber info not found for {}", context.inPacket(). |
| 548 | receivedFrom()); |
| 549 | return VlanId.NONE; |
| 550 | } |
| 551 | return sub.sTag(); |
| 552 | } |
| 553 | |
| 554 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
| 555 | |
| 556 | @Override |
| 557 | public void process(PacketContext context) { |
| 558 | if (!configured()) { |
| 559 | log.warn("Missing DHCP relay config. Abort packet processing"); |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | // process the packet and get the payload |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 564 | Ethernet packet = context.inPacket().parsed(); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 565 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 566 | if (packet == null) { |
| 567 | log.warn("Packet is null"); |
| 568 | return; |
| 569 | } |
| 570 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 571 | if (packet.getEtherType() == Ethernet.TYPE_IPV4) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 572 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 573 | |
| 574 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 575 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 576 | if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT || |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 577 | udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 578 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 579 | //This packet is dhcp. |
| 580 | processDhcpPacket(context, packet, dhcpPayload); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | //forward the packet to ConnectPoint where the DHCP server is attached. |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 587 | private void forwardPacket(Ethernet packet, PacketContext context) { |
| 588 | ConnectPoint toSendTo = null; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 589 | |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 590 | if (!useOltUplink) { |
| 591 | toSendTo = dhcpServerConnectPoint.get(); |
| 592 | } else { |
| 593 | toSendTo = getUplinkConnectPointOfOlt(context.inPacket(). |
| 594 | receivedFrom().deviceId()); |
| 595 | } |
| 596 | |
| 597 | if (toSendTo != null) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 598 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 599 | .setOutput(toSendTo.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 600 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 601 | toSendTo.deviceId(), t, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 602 | ByteBuffer.wrap(packet.serialize())); |
| 603 | if (log.isTraceEnabled()) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 604 | log.trace("Relaying packet to dhcp server at {} {}", |
| 605 | toSendTo, packet); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 606 | } |
| 607 | packetService.emit(o); |
| 608 | } else { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 609 | log.error("No connect point to send msg to DHCP Server"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 613 | // get the type of the DHCP packet |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 614 | private DHCP.MsgType getDhcpPacketType(DHCP dhcpPayload) { |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 615 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 616 | for (DhcpOption option : dhcpPayload.getOptions()) { |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 617 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 618 | byte[] data = option.getData(); |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 619 | return DHCP.MsgType.getType(data[0]); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | return null; |
| 623 | } |
| 624 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 625 | //process the dhcp packet before sending to server |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 626 | private void processDhcpPacket(PacketContext context, Ethernet packet, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 627 | DHCP dhcpPayload) { |
| 628 | if (dhcpPayload == null) { |
| 629 | log.warn("DHCP payload is null"); |
| 630 | return; |
| 631 | } |
| 632 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 633 | DHCP.MsgType incomingPacketType = getDhcpPacketType(dhcpPayload); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 634 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 635 | log.info("Received DHCP Packet of type {} from {}", |
| 636 | incomingPacketType, context.inPacket().receivedFrom()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 637 | |
| 638 | switch (incomingPacketType) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 639 | case DHCPDISCOVER: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 640 | Ethernet ethernetPacketDiscover = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 641 | processDhcpPacketFromClient(context, packet); |
| 642 | if (ethernetPacketDiscover != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 643 | forwardPacket(ethernetPacketDiscover, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 644 | } |
| 645 | break; |
| 646 | case DHCPOFFER: |
| 647 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 648 | Ethernet ethernetPacketOffer = |
| 649 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 650 | if (ethernetPacketOffer != null) { |
| 651 | sendReply(ethernetPacketOffer, dhcpPayload); |
| 652 | } |
| 653 | break; |
| 654 | case DHCPREQUEST: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 655 | Ethernet ethernetPacketRequest = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 656 | processDhcpPacketFromClient(context, packet); |
| 657 | if (ethernetPacketRequest != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 658 | forwardPacket(ethernetPacketRequest, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 659 | } |
| 660 | break; |
| 661 | case DHCPACK: |
| 662 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 663 | Ethernet ethernetPacketAck = |
| 664 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 665 | if (ethernetPacketAck != null) { |
| 666 | sendReply(ethernetPacketAck, dhcpPayload); |
| 667 | } |
| 668 | break; |
| 669 | default: |
| 670 | break; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 674 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
| 675 | Ethernet ethernetPacket) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 676 | if (log.isTraceEnabled()) { |
| 677 | log.trace("DHCP packet received from client at {} {}", |
| 678 | context.inPacket().receivedFrom(), ethernetPacket); |
| 679 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 680 | |
| 681 | MacAddress relayAgentMac = relayAgentMacAddress(context); |
| 682 | if (relayAgentMac == null) { |
| 683 | log.warn("RelayAgent MAC not found "); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 684 | return null; |
| 685 | } |
| 686 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 687 | Ethernet etherReply = ethernetPacket; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 688 | |
| 689 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 690 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 691 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
| 692 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 693 | if (enableDhcpBroadcastReplies) { |
| 694 | // We want the reply to come back as a L2 broadcast |
| 695 | dhcpPacket.setFlags((short) 0x8000); |
| 696 | } |
| 697 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 698 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
| 699 | IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 700 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 701 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
| 702 | if (entry == null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 703 | log.warn("Dropping packet as subscriber entry is not available"); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 704 | return null; |
| 705 | } |
| 706 | |
| 707 | DhcpAllocationInfo info = new DhcpAllocationInfo( |
| 708 | context.inPacket().receivedFrom(), dhcpPacket.getPacketType(), |
| 709 | entry.nasPortId(), clientMac, clientIp); |
| 710 | |
| 711 | allocationMap.put(entry.nasPortId(), info); |
| 712 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 713 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, |
| 714 | context.inPacket().receivedFrom())); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 715 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 716 | if (option82) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 717 | DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry); |
| 718 | udpPacket.setPayload(dhcpPacketWithOption82); |
| 719 | } |
| 720 | |
| 721 | ipv4Packet.setPayload(udpPacket); |
| 722 | etherReply.setPayload(ipv4Packet); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 723 | if (modifyClientPktsSrcDstMac) { |
| 724 | etherReply.setSourceMACAddress(relayAgentMac); |
| 725 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
| 726 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 727 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 728 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 729 | etherReply.setVlanID(cTag(context).toShort()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 730 | etherReply.setQinQTPID(Ethernet.TYPE_VLAN); |
| 731 | etherReply.setQinQVID(sTag(context).toShort()); |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 732 | log.info("Finished processing packet.. relaying to dhcpServer"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 733 | return etherReply; |
| 734 | } |
| 735 | |
| 736 | //build the DHCP offer/ack with proper client port. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 737 | private Ethernet processDhcpPacketFromServer(PacketContext context, |
| 738 | Ethernet ethernetPacket) { |
| 739 | if (log.isTraceEnabled()) { |
| 740 | log.trace("DHCP packet received from server at {} {}", |
| 741 | context.inPacket().receivedFrom(), ethernetPacket); |
| 742 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 743 | // get dhcp header. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 744 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 745 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 746 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 747 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
| 748 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 749 | MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 750 | ConnectPoint subsCp = getConnectPointOfClient(dstMac); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 751 | // If we can't find the subscriber, can't process further |
| 752 | if (subsCp == null) { |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 753 | log.warn("Couldn't find connection point for mac address {} DHCPOFFERs won't be delivered", dstMac); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 754 | return null; |
| 755 | } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 756 | // if it's an ACK packet store the information for display purpose |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame^] | 757 | if (getDhcpPacketType(dhcpPayload) == DHCP.MsgType.DHCPACK) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 758 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 759 | String portId = nasPortId(subsCp); |
| 760 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 761 | if (sub != null) { |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 762 | List<DhcpOption> options = dhcpPayload.getOptions(); |
| 763 | List<DhcpOption> circuitIds = options.stream() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 764 | .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 765 | .collect(Collectors.toList()); |
| 766 | |
| 767 | String circuitId = "None"; |
| 768 | if (circuitIds.size() == 1) { |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 769 | byte[] array = circuitIds.get(0).getData(); |
| 770 | |
| 771 | try { |
| 772 | // we leave the first two bytes as they are the id and length |
| 773 | circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8"); |
| 774 | } catch (Exception e) { } |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress()); |
| 778 | |
| 779 | //storeDHCPAllocationInfo |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 780 | DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp, |
| 781 | dhcpPayload.getPacketType(), circuitId, dstMac, ip); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 782 | |
| 783 | allocationMap.put(sub.nasPortId(), info); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 784 | |
| 785 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp)); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 786 | } |
| 787 | } // end storing of info |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 788 | |
| 789 | // we leave the srcMac from the original packet |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 790 | etherReply.setDestinationMACAddress(dstMac); |
| 791 | etherReply.setQinQVID(sTag(subsCp).toShort()); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 792 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
| 793 | etherReply.setVlanID((cTag(subsCp).toShort())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 794 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 795 | if (option82) { |
| 796 | udpPacket.setPayload(removeOption82(dhcpPayload)); |
| 797 | } else { |
| 798 | udpPacket.setPayload(dhcpPayload); |
| 799 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 800 | ipv4Packet.setPayload(udpPacket); |
| 801 | etherReply.setPayload(ipv4Packet); |
| 802 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 803 | log.info("Finished processing packet.. relaying to client"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 804 | return etherReply; |
| 805 | } |
| 806 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 807 | /* |
| 808 | * Get ConnectPoint of the Client based on it's MAC address |
| 809 | */ |
| 810 | private ConnectPoint getConnectPointOfClient(MacAddress dstMac) { |
| 811 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 812 | if (hosts == null || hosts.isEmpty()) { |
| 813 | log.warn("Cannot determine host for DHCP client: {}. Aborting " |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 814 | + "relay for dhcp packet from server", |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 815 | dstMac); |
| 816 | return null; |
| 817 | } |
| 818 | for (Host h : hosts) { |
| 819 | // if more than one, |
| 820 | // find the connect point which has an valid entry in SADIS |
| 821 | ConnectPoint cp = new ConnectPoint(h.location().deviceId(), |
| 822 | h.location().port()); |
| 823 | |
| 824 | if (sTag(cp) != VlanId.NONE) { |
| 825 | return cp; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return null; |
| 830 | } |
| 831 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 832 | //send the response to the requester host. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 833 | private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 834 | MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 835 | ConnectPoint subCp = getConnectPointOfClient(descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 836 | |
| 837 | // Send packet out to requester if the host information is available |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 838 | if (subCp != null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 839 | log.info("Sending DHCP packet to client at {}", subCp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 840 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 841 | .setOutput(subCp.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 842 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 843 | subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 844 | if (log.isTraceEnabled()) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 845 | log.trace("Relaying packet to dhcp client at {} {}", subCp, |
| 846 | ethPacket); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 847 | } |
| 848 | packetService.emit(o); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 849 | } else { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 850 | log.error("Dropping DHCP packet because can't find host for {}", descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 855 | private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) { |
| 856 | log.debug("option82data {} ", entry); |
| 857 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 858 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 859 | DhcpOption82 option82 = new DhcpOption82(); |
| 860 | option82.setAgentCircuitId(entry.circuitId()); |
| 861 | option82.setAgentRemoteId(entry.remoteId()); |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 862 | DhcpOption option = new DhcpOption() |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 863 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 864 | .setData(option82.toByteArray()) |
| 865 | .setLength(option82.length()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 866 | |
| 867 | options.add(options.size() - 1, option); |
| 868 | dhcpPacket.setOptions(options); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 869 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 870 | return dhcpPacket; |
| 871 | |
| 872 | } |
| 873 | |
| 874 | private DHCP removeOption82(DHCP dhcpPacket) { |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 875 | List<DhcpOption> options = dhcpPacket.getOptions(); |
| 876 | List<DhcpOption> newoptions = options.stream() |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 877 | .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 878 | .collect(Collectors.toList()); |
| 879 | |
| 880 | return dhcpPacket.setOptions(newoptions); |
| 881 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 882 | /** |
| 883 | * Listener for network config events. |
| 884 | */ |
| 885 | private class InternalConfigListener implements NetworkConfigListener { |
| 886 | |
| 887 | @Override |
| 888 | public void event(NetworkConfigEvent event) { |
| 889 | |
| 890 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 891 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 892 | event.configClass().equals(DhcpL2RelayConfig.class)) { |
| 893 | updateConfig(); |
| 894 | log.info("Reconfigured"); |
| 895 | } |
| 896 | } |
| 897 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 898 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 899 | /** |
| 900 | * Handles Mastership changes for the devices which connect |
| 901 | * to the DHCP server. |
| 902 | */ |
| 903 | private class InnerMastershipListener implements MastershipListener { |
| 904 | @Override |
| 905 | public void event(MastershipEvent event) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 906 | if (!useOltUplink) { |
| 907 | if (dhcpServerConnectPoint.get() != null && |
| 908 | dhcpServerConnectPoint.get().deviceId(). |
| 909 | equals(event.subject())) { |
| 910 | log.trace("Mastership Event recevived for {}", event.subject()); |
| 911 | // mastership of the device for our connect point has changed |
| 912 | // reselect |
| 913 | selectServerConnectPoint(); |
| 914 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 918 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 919 | /** |
| 920 | * Handles Device status change for the devices which connect |
| 921 | * to the DHCP server. |
| 922 | */ |
| 923 | private class InnerDeviceListener implements DeviceListener { |
| 924 | @Override |
| 925 | public void event(DeviceEvent event) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 926 | if (log.isTraceEnabled() && |
| 927 | !event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) { |
| 928 | log.trace("Device Event received for {} event {}", |
| 929 | event.subject(), event.type()); |
| 930 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 931 | if (!useOltUplink) { |
| 932 | if (dhcpServerConnectPoint.get() == null) { |
| 933 | switch (event.type()) { |
| 934 | case DEVICE_ADDED: |
| 935 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 936 | // some device is available check if we can get a |
| 937 | // connect point we can use |
| 938 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 939 | break; |
| 940 | default: |
| 941 | break; |
| 942 | } |
| 943 | return; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 944 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 945 | if (dhcpServerConnectPoint.get().deviceId(). |
| 946 | equals(event.subject().id())) { |
| 947 | switch (event.type()) { |
| 948 | case DEVICE_AVAILABILITY_CHANGED: |
| 949 | case DEVICE_REMOVED: |
| 950 | case DEVICE_SUSPENDED: |
| 951 | // state of our device has changed, check if we need |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 952 | // to re-select a connectpoint |
| 953 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 954 | break; |
| 955 | default: |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 | } else { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 960 | switch (event.type()) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 961 | case PORT_ADDED: |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 962 | if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 963 | requestDhcpPacketsFromConnectPoint( |
| 964 | new ConnectPoint(event.subject().id(), event.port().number()), |
| 965 | null); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 966 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 967 | break; |
| 968 | default: |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 974 | } |