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