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