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