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 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 18 | import static java.util.concurrent.Executors.newFixedThreadPool; |
| 19 | import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; |
| 20 | import static org.onlab.packet.MacAddress.valueOf; |
| 21 | import static org.onlab.util.Tools.groupedThreads; |
| 22 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 23 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES; |
| 24 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT; |
| 25 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82; |
| 26 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82_DEFAULT; |
| 27 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.PACKET_PROCESSOR_THREADS; |
| 28 | import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.PACKET_PROCESSOR_THREADS_DEFAULT; |
| 29 | |
| 30 | import java.io.ByteArrayOutputStream; |
| 31 | import java.nio.ByteBuffer; |
| 32 | import java.time.Instant; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Dictionary; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | import java.util.UUID; |
| 40 | import java.util.concurrent.ExecutorService; |
| 41 | import java.util.concurrent.Executors; |
| 42 | import java.util.concurrent.ScheduledExecutorService; |
| 43 | import java.util.concurrent.ScheduledFuture; |
| 44 | import java.util.concurrent.atomic.AtomicReference; |
| 45 | import java.util.function.Predicate; |
| 46 | import java.util.stream.Collectors; |
| 47 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 48 | import org.apache.commons.io.HexDump; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 49 | import org.onlab.packet.DHCP; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 50 | import org.onlab.packet.Ethernet; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 51 | import org.onlab.packet.IPv4; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 52 | import org.onlab.packet.IpAddress; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 53 | import org.onlab.packet.MacAddress; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 54 | import org.onlab.packet.TpPort; |
| 55 | import org.onlab.packet.UDP; |
| 56 | import org.onlab.packet.VlanId; |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 57 | import org.onlab.packet.dhcp.DhcpOption; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 58 | import org.onlab.packet.dhcp.DhcpRelayAgentOption; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 59 | import org.onlab.util.KryoNamespace; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 60 | import org.onlab.util.Tools; |
| 61 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 62 | import org.onosproject.cluster.ClusterService; |
| 63 | import org.onosproject.cluster.LeadershipService; |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 64 | import org.onosproject.cluster.NodeId; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 65 | import org.onosproject.core.ApplicationId; |
| 66 | import org.onosproject.core.CoreService; |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 67 | import org.onosproject.event.AbstractListenerManager; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 68 | import org.onosproject.mastership.MastershipEvent; |
| 69 | import org.onosproject.mastership.MastershipListener; |
| 70 | import org.onosproject.mastership.MastershipService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 71 | import org.onosproject.net.AnnotationKeys; |
| 72 | import org.onosproject.net.ConnectPoint; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 73 | import org.onosproject.net.Device; |
| 74 | import org.onosproject.net.DeviceId; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 75 | import org.onosproject.net.Host; |
| 76 | import org.onosproject.net.Port; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 77 | import org.onosproject.net.PortNumber; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 78 | import org.onosproject.net.config.ConfigFactory; |
| 79 | import org.onosproject.net.config.NetworkConfigEvent; |
| 80 | import org.onosproject.net.config.NetworkConfigListener; |
| 81 | import org.onosproject.net.config.NetworkConfigRegistry; |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 82 | import org.onosproject.net.device.DeviceEvent; |
| 83 | import org.onosproject.net.device.DeviceListener; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 84 | import org.onosproject.net.device.DeviceService; |
| 85 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 86 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 87 | import org.onosproject.net.flow.TrafficSelector; |
| 88 | import org.onosproject.net.flow.TrafficTreatment; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 89 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 90 | import org.onosproject.net.host.HostService; |
| 91 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 92 | import org.onosproject.net.packet.OutboundPacket; |
| 93 | import org.onosproject.net.packet.PacketContext; |
| 94 | import org.onosproject.net.packet.PacketPriority; |
| 95 | import org.onosproject.net.packet.PacketProcessor; |
| 96 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 97 | import org.onosproject.store.serializers.KryoNamespaces; |
| 98 | import org.onosproject.store.service.ConsistentMap; |
| 99 | import org.onosproject.store.service.Serializer; |
| 100 | import org.onosproject.store.service.StorageService; |
| 101 | import org.onosproject.store.service.Versioned; |
Matteo Scandolo | 57af5d1 | 2019-04-29 17:11:41 -0700 | [diff] [blame] | 102 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 103 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 104 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 105 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 106 | import org.opencord.dhcpl2relay.DhcpL2RelayStoreDelegate; |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 107 | import org.opencord.dhcpl2relay.impl.packet.DhcpOption82Data; |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 108 | import org.opencord.sadis.BaseInformationService; |
| 109 | import org.opencord.sadis.SadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 110 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 111 | import org.opencord.sadis.UniTagInformation; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 112 | import org.osgi.service.component.ComponentContext; |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 113 | import org.osgi.service.component.annotations.Activate; |
| 114 | import org.osgi.service.component.annotations.Component; |
| 115 | import org.osgi.service.component.annotations.Deactivate; |
| 116 | import org.osgi.service.component.annotations.Modified; |
| 117 | import org.osgi.service.component.annotations.Reference; |
| 118 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 119 | import org.slf4j.Logger; |
| 120 | import org.slf4j.LoggerFactory; |
| 121 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 122 | import com.google.common.base.Strings; |
| 123 | import com.google.common.collect.ImmutableMap; |
| 124 | import com.google.common.collect.ImmutableSet; |
| 125 | import com.google.common.collect.Lists; |
| 126 | import com.google.common.collect.Sets; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * DHCP Relay Agent Application Component. |
| 130 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 131 | @Component(immediate = true, |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 132 | property = { |
| 133 | OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT, |
| 134 | ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT, |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 135 | PACKET_PROCESSOR_THREADS + ":Integer=" + PACKET_PROCESSOR_THREADS_DEFAULT, |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 136 | }) |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 137 | public class DhcpL2Relay |
| 138 | extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener> |
| 139 | implements DhcpL2RelayService { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 140 | |
| 141 | public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay"; |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 142 | private static final String HOST_LOC_PROVIDER = |
| 143 | "org.onosproject.provider.host.impl.HostLocationProvider"; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 144 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 145 | private final InternalConfigListener cfgListener = |
| 146 | new InternalConfigListener(); |
| 147 | |
| 148 | private final Set<ConfigFactory> factories = ImmutableSet.of( |
| 149 | new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY, |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 150 | DhcpL2RelayConfig.class, |
| 151 | "dhcpl2relay") { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 152 | @Override |
| 153 | public DhcpL2RelayConfig createConfig() { |
| 154 | return new DhcpL2RelayConfig(); |
| 155 | } |
| 156 | } |
| 157 | ); |
| 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 NetworkConfigRegistry cfgService; |
| 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 CoreService coreService; |
| 164 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 165 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 166 | protected PacketService packetService; |
| 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 HostService hostService; |
| 170 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 172 | protected ComponentConfigService componentConfigService; |
| 173 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 174 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 175 | protected SadisService sadisService; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 176 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 177 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 178 | protected DeviceService deviceService; |
| 179 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 181 | protected MastershipService mastershipService; |
| 182 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 183 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 184 | protected StorageService storageService; |
| 185 | |
| 186 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 187 | protected FlowObjectiveService flowObjectiveService; |
| 188 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 189 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 190 | protected DhcpL2RelayCountersStore dhcpL2RelayCounters; |
| 191 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 193 | protected LeadershipService leadershipService; |
| 194 | |
| 195 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 196 | protected ClusterService clusterService; |
| 197 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 198 | // OSGi Properties |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Add option 82 to relayed packets. |
| 201 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 202 | protected boolean option82 = OPTION_82_DEFAULT; |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Ask the DHCP Server to send back replies as L2 broadcast. |
| 205 | */ |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 206 | protected boolean enableDhcpBroadcastReplies = ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 207 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Number of threads used to process the packet. |
| 210 | */ |
| 211 | protected int packetProcessorThreads = PACKET_PROCESSOR_THREADS_DEFAULT; |
| 212 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 213 | ScheduledFuture<?> refreshTask; |
| 214 | ScheduledExecutorService refreshService = Executors.newSingleThreadScheduledExecutor(); |
| 215 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 216 | private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = |
| 217 | new DhcpRelayPacketProcessor(); |
| 218 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 219 | private InnerMastershipListener changeListener = new InnerMastershipListener(); |
| 220 | private InnerDeviceListener deviceListener = new InnerDeviceListener(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 221 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 222 | // connect points to the DHCP server |
| 223 | Set<ConnectPoint> dhcpConnectPoints; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 224 | protected AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 225 | private MacAddress dhcpConnectMac = MacAddress.BROADCAST; |
| 226 | private ApplicationId appId; |
| 227 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 228 | private ConsistentMap<String, DhcpAllocationInfo> allocations; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 229 | protected boolean modifyClientPktsSrcDstMac = false; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 230 | //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] | 231 | protected boolean useOltUplink = false; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 232 | |
Gamze Abaka | c806c6c | 2018-12-03 12:49:46 +0000 | [diff] [blame] | 233 | private BaseInformationService<SubscriberAndDeviceInformation> subsService; |
| 234 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 235 | private DhcpL2RelayStoreDelegate delegate = new InnerDhcpL2RelayStoreDelegate(); |
| 236 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 237 | protected ExecutorService packetProcessorExecutor; |
| 238 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 239 | @Activate |
| 240 | protected void activate(ComponentContext context) { |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 241 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 242 | //start the dhcp relay agent |
| 243 | appId = coreService.registerApplication(DHCP_L2RELAY_APP); |
| 244 | componentConfigService.registerProperties(getClass()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 245 | eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 246 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 247 | KryoNamespace serializer = KryoNamespace.newBuilder() |
| 248 | .register(KryoNamespaces.API) |
| 249 | .register(Instant.class) |
| 250 | .register(DHCP.MsgType.class) |
| 251 | .register(DhcpAllocationInfo.class) |
| 252 | .build(); |
| 253 | |
| 254 | allocations = storageService.<String, DhcpAllocationInfo>consistentMapBuilder() |
| 255 | .withName("dhcpl2relay-allocations") |
| 256 | .withSerializer(Serializer.using(serializer)) |
| 257 | .withApplicationId(appId) |
| 258 | .build(); |
| 259 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 260 | dhcpL2RelayCounters.setDelegate(delegate); |
| 261 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 262 | cfgService.addListener(cfgListener); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 263 | mastershipService.addListener(changeListener); |
| 264 | deviceService.addListener(deviceListener); |
| 265 | |
Matteo Scandolo | 45e5a27 | 2019-09-30 09:30:32 -0700 | [diff] [blame] | 266 | subsService = sadisService.getSubscriberInfoService(); |
| 267 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 268 | factories.forEach(cfgService::registerConfigFactory); |
| 269 | //update the dhcp server configuration. |
| 270 | updateConfig(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 271 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 272 | if (context != null) { |
| 273 | modified(context); |
| 274 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 275 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 276 | //add the packet services. |
| 277 | packetService.addProcessor(dhcpRelayPacketProcessor, |
| 278 | PacketProcessor.director(0)); |
| 279 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 280 | log.info("DHCP-L2-RELAY Started"); |
| 281 | } |
| 282 | |
| 283 | @Deactivate |
| 284 | protected void deactivate() { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 285 | if (refreshTask != null) { |
| 286 | refreshTask.cancel(true); |
| 287 | } |
| 288 | if (refreshService != null) { |
| 289 | refreshService.shutdownNow(); |
| 290 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 291 | dhcpL2RelayCounters.unsetDelegate(delegate); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 292 | cfgService.removeListener(cfgListener); |
| 293 | factories.forEach(cfgService::unregisterConfigFactory); |
| 294 | packetService.removeProcessor(dhcpRelayPacketProcessor); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 295 | cancelDhcpPktsFromServer(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 296 | |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 297 | packetProcessorExecutor.shutdown(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 298 | componentConfigService.unregisterProperties(getClass(), false); |
Deepa Vaddireddy | 77a6ac7 | 2017-09-20 20:36:52 +0530 | [diff] [blame] | 299 | deviceService.removeListener(deviceListener); |
| 300 | mastershipService.removeListener(changeListener); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 301 | eventDispatcher.removeSink(DhcpL2RelayEvent.class); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 302 | log.info("DHCP-L2-RELAY Stopped"); |
| 303 | } |
| 304 | |
| 305 | @Modified |
| 306 | protected void modified(ComponentContext context) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 307 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 308 | Dictionary<?, ?> properties = context.getProperties(); |
| 309 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 310 | Boolean o = Tools.isPropertyEnabled(properties, OPTION_82); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 311 | if (o != null) { |
| 312 | option82 = o; |
| 313 | } |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 314 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 315 | o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_BROADCAST_REPLIES); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 316 | if (o != null) { |
| 317 | enableDhcpBroadcastReplies = o; |
| 318 | } |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 319 | |
| 320 | String s = Tools.get(properties, PACKET_PROCESSOR_THREADS); |
| 321 | int oldpacketProcessorThreads = packetProcessorThreads; |
| 322 | packetProcessorThreads = Strings.isNullOrEmpty(s) ? oldpacketProcessorThreads |
| 323 | : Integer.parseInt(s.trim()); |
| 324 | if (packetProcessorExecutor == null || oldpacketProcessorThreads != packetProcessorThreads) { |
| 325 | if (packetProcessorExecutor != null) { |
| 326 | packetProcessorExecutor.shutdown(); |
| 327 | } |
| 328 | packetProcessorExecutor = newFixedThreadPool(packetProcessorThreads, |
| 329 | groupedThreads("onos/dhcp", |
| 330 | "dhcp-packet-%d", log)); |
| 331 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 332 | } |
| 333 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 334 | @Override |
| 335 | public Map<String, DhcpAllocationInfo> getAllocationInfo() { |
| 336 | return ImmutableMap.copyOf(allocations.asJavaMap()); |
| 337 | } |
| 338 | |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 339 | /** |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 340 | * Generates a unique UUID from a string. |
| 341 | * |
| 342 | * @return true if all information we need have been initialized |
| 343 | */ |
| 344 | private static String getUniqueUuidFromString(String value) { |
| 345 | return UUID.nameUUIDFromBytes(value.getBytes()).toString(); |
| 346 | } |
| 347 | |
| 348 | /** |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 349 | * Checks if this app has been configured. |
| 350 | * |
| 351 | * @return true if all information we need have been initialized |
| 352 | */ |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 353 | protected boolean configured() { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 354 | if (!useOltUplink) { |
| 355 | return dhcpServerConnectPoint.get() != null; |
| 356 | } |
| 357 | return true; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 360 | /** |
| 361 | * Selects a connect point through an available device for which it is the master. |
| 362 | */ |
| 363 | private void selectServerConnectPoint() { |
| 364 | synchronized (this) { |
| 365 | dhcpServerConnectPoint.set(null); |
| 366 | if (dhcpConnectPoints != null) { |
| 367 | // find a connect point through a device for which we are master |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 368 | for (ConnectPoint cp : dhcpConnectPoints) { |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 369 | if (isLocalLeader(cp.deviceId())) { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 370 | if (deviceService.isAvailable(cp.deviceId())) { |
| 371 | dhcpServerConnectPoint.set(cp); |
| 372 | } |
| 373 | log.info("DHCP connectPoint selected is {}", cp); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get()); |
| 380 | |
| 381 | if (dhcpServerConnectPoint.get() == null) { |
| 382 | log.error("Master of none, can't relay DHCP Message to server"); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Updates the network configuration. |
| 389 | */ |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 390 | private void updateConfig() { |
| 391 | DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class); |
| 392 | if (cfg == null) { |
| 393 | log.warn("Dhcp Server info not available"); |
| 394 | return; |
| 395 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 396 | |
| 397 | dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 398 | modifyClientPktsSrcDstMac = cfg.getModifySrcDstMacAddresses(); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 399 | boolean prevUseOltUplink = useOltUplink; |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 400 | useOltUplink = cfg.getUseOltUplinkForServerPktInOut(); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 401 | |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 402 | if (useOltUplink) { |
| 403 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 404 | log.debug("requestDhcpPackets: ConnectPoint: {}", cp); |
Matteo Scandolo | 45e5a27 | 2019-09-30 09:30:32 -0700 | [diff] [blame] | 405 | requestDhcpPacketsFromConnectPoint(cp, Optional.ofNullable(null)); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 406 | } |
| 407 | // check if previous config was different and so trap flows may |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 408 | // need to be removed from other places like AGG switches |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 409 | if (!prevUseOltUplink) { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 410 | addOrRemoveDhcpTrapFromServer(false); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 411 | } |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 412 | } else { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 413 | // uplink on AGG switch |
| 414 | addOrRemoveDhcpTrapFromServer(true); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | private void cancelDhcpPktsFromServer() { |
| 419 | if (useOltUplink) { |
| 420 | for (ConnectPoint cp : getUplinkPortsOfOlts()) { |
| 421 | log.debug("cancelDhcpPackets: ConnectPoint: {}", cp); |
Matteo Scandolo | 45e5a27 | 2019-09-30 09:30:32 -0700 | [diff] [blame] | 422 | cancelDhcpPacketsFromConnectPoint(cp, Optional.ofNullable(null)); |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 423 | } |
| 424 | } else { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 425 | // uplink on AGG switch |
| 426 | addOrRemoveDhcpTrapFromServer(false); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 427 | } |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 430 | /** |
| 431 | * Used to add or remove DHCP trap flow for packets received from DHCP server. |
| 432 | * Typically used on a non OLT device, like an AGG switch. When adding, a |
| 433 | * new dhcp server connect point is selected from the configured options. |
| 434 | * |
| 435 | * @param add true if dhcp trap flow is to be added, false to remove the |
| 436 | * trap flow |
| 437 | */ |
| 438 | private void addOrRemoveDhcpTrapFromServer(boolean add) { |
| 439 | if (add) { |
| 440 | selectServerConnectPoint(); |
| 441 | log.debug("dhcp server connect point: " + dhcpServerConnectPoint); |
| 442 | } |
| 443 | if (dhcpServerConnectPoint.get() == null) { |
| 444 | log.warn("No dhcpServer connectPoint found, cannot {} dhcp trap flows", |
| 445 | (add) ? "install" : "remove"); |
| 446 | return; |
| 447 | } |
| 448 | if (add) { |
| 449 | log.info("Adding trap to dhcp server connect point: " |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 450 | + dhcpServerConnectPoint); |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 451 | requestDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(), |
| 452 | Optional.of(PacketPriority.HIGH1)); |
| 453 | } else { |
| 454 | log.info("Removing trap from dhcp server connect point: " |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 455 | + dhcpServerConnectPoint); |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 456 | cancelDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(), |
| 457 | Optional.of(PacketPriority.HIGH1)); |
| 458 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 462 | * Returns all the uplink ports of OLTs configured in SADIS. |
| 463 | * Only ports visible in ONOS and for which this instance is master |
| 464 | * are returned |
| 465 | */ |
| 466 | private List<ConnectPoint> getUplinkPortsOfOlts() { |
| 467 | List<ConnectPoint> cps = new ArrayList<>(); |
| 468 | |
| 469 | // find all the olt devices and if their uplink ports are visible |
| 470 | Iterable<Device> devices = deviceService.getDevices(); |
| 471 | for (Device d : devices) { |
| 472 | // check if this device is provisioned in Sadis |
| 473 | |
| 474 | log.debug("getUplinkPortsOfOlts: Checking mastership of {}", d); |
| 475 | // do only for devices for which we are the master |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 476 | if (!isLocalLeader(d.id())) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | |
| 480 | String devSerialNo = d.serialNumber(); |
| 481 | SubscriberAndDeviceInformation deviceInfo = subsService.get(devSerialNo); |
| 482 | log.debug("getUplinkPortsOfOlts: Found device: {}", deviceInfo); |
| 483 | if (deviceInfo != null) { |
| 484 | // check if the uplink port with that number is available on the device |
| 485 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 486 | Port port = deviceService.getPort(d.id(), pNum); |
| 487 | log.debug("getUplinkPortsOfOlts: Found port: {}", port); |
| 488 | if (port != null) { |
| 489 | cps.add(new ConnectPoint(d.id(), pNum)); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | return cps; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Returns whether the passed port is the uplink port of the olt device. |
| 498 | */ |
| 499 | private boolean isUplinkPortOfOlt(DeviceId dId, Port p) { |
| 500 | log.debug("isUplinkPortOfOlt: DeviceId: {} Port: {}", dId, p); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 501 | |
| 502 | Device d = deviceService.getDevice(dId); |
| 503 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 504 | |
| 505 | if (deviceInfo != null) { |
| 506 | return (deviceInfo.uplinkPort() == p.number().toLong()); |
| 507 | } |
| 508 | |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Returns the connectPoint which is the uplink port of the OLT. |
| 514 | */ |
| 515 | private ConnectPoint getUplinkConnectPointOfOlt(DeviceId dId) { |
| 516 | |
| 517 | Device d = deviceService.getDevice(dId); |
| 518 | SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber()); |
| 519 | log.debug("getUplinkConnectPointOfOlt DeviceId: {} devInfo: {}", dId, deviceInfo); |
| 520 | if (deviceInfo != null) { |
| 521 | PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort()); |
| 522 | Port port = deviceService.getPort(d.id(), pNum); |
| 523 | if (port != null) { |
| 524 | return new ConnectPoint(d.id(), pNum); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return null; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Request DHCP packet from particular connect point via PacketService. |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 533 | * Optionally provide a priority for the trap flow. If no such priority is |
| 534 | * provided, the default priority will be used. |
| 535 | * |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 536 | * @param cp the connect point to trap dhcp packets from |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 537 | * @param priority of the trap flow, null to use default priority |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 538 | */ |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 539 | private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp, |
| 540 | Optional<PacketPriority> priority) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 541 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 542 | .matchEthType(Ethernet.TYPE_IPV4) |
| 543 | .matchInPort(cp.port()) |
| 544 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 545 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 546 | packetService.requestPackets(selectorServer.build(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 547 | priority.isPresent() ? priority.get() : PacketPriority.CONTROL, |
| 548 | appId, Optional.of(cp.deviceId())); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /** |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 552 | * Cancel DHCP packet from particular connect point via PacketService. If |
| 553 | * the request was made with a specific packet priority, then the same |
| 554 | * priority should be used in this call. |
| 555 | * |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 556 | * @param cp the connect point for the trap flow |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 557 | * @param priority with which the trap flow was requested; if request |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 558 | * priority was not specified, this param should also be null |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 559 | */ |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 560 | private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp, |
| 561 | Optional<PacketPriority> priority) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 562 | TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() |
| 563 | .matchEthType(Ethernet.TYPE_IPV4) |
| 564 | .matchInPort(cp.port()) |
| 565 | .matchIPProtocol(IPv4.PROTOCOL_UDP) |
| 566 | .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT)); |
| 567 | packetService.cancelPackets(selectorServer.build(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 568 | priority.isPresent() ? priority.get() : PacketPriority.CONTROL, |
| 569 | appId, Optional.of(cp.deviceId())); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 572 | /** |
| 573 | * Main packet-processing engine for dhcp l2 relay agent. |
| 574 | */ |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 575 | private class DhcpRelayPacketProcessor implements PacketProcessor { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 576 | private static final String VLAN_KEYWORD = ":vlan"; |
| 577 | private static final String PCP_KEYWORD = ":pcp"; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 578 | |
| 579 | @Override |
| 580 | public void process(PacketContext context) { |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 581 | packetProcessorExecutor.execute(() -> { |
| 582 | processInternal(context); |
| 583 | }); |
| 584 | } |
| 585 | |
| 586 | private void processInternal(PacketContext context) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 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(); |
Matteo Scandolo | a8b6eea | 2018-11-27 13:47:58 -0800 | [diff] [blame] | 608 | if (log.isTraceEnabled()) { |
| 609 | log.trace("Processing packet with type {} from MAC {}", |
| 610 | getDhcpPacketType(dhcpPayload), |
| 611 | MacAddress.valueOf(dhcpPayload.getClientHardwareAddress())); |
| 612 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 613 | //This packet is dhcp. |
| 614 | processDhcpPacket(context, packet, dhcpPayload); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 620 | // process the dhcp packet before relaying to server or client |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 621 | private void processDhcpPacket(PacketContext context, Ethernet packet, |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 622 | DHCP dhcpPayload) { |
| 623 | if (dhcpPayload == null) { |
| 624 | log.warn("DHCP payload is null"); |
| 625 | return; |
| 626 | } |
| 627 | |
Carmelo Cascone | de1e6e3 | 2019-07-15 19:39:08 -0700 | [diff] [blame] | 628 | DHCP.MsgType incomingPacketType = getDhcpPacketType(dhcpPayload); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 629 | if (incomingPacketType == null) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 630 | log.warn("DHCP Packet type not found. Dump of ethernet pkt in hex format for troubleshooting."); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 631 | byte[] array = packet.serialize(); |
| 632 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 633 | try { |
| 634 | HexDump.dump(array, 0, buf, 0); |
| 635 | log.trace(buf.toString()); |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 636 | } catch (Exception e) { |
| 637 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 638 | return; |
| 639 | } |
| 640 | |
| 641 | SubscriberAndDeviceInformation entry = null; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 642 | |
Matteo Scandolo | eb5a0dc | 2020-09-15 14:54:28 -0700 | [diff] [blame] | 643 | MacAddress clientMacAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()); |
| 644 | |
| 645 | log.info("Received DHCP Packet of type {} from {} with Client MacAddress {} and vlan {}", |
| 646 | incomingPacketType, context.inPacket().receivedFrom(), |
| 647 | clientMacAddress, packet.getVlanID()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 648 | |
| 649 | switch (incomingPacketType) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 650 | case DHCPDISCOVER: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 651 | Ethernet ethernetPacketDiscover = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 652 | processDhcpPacketFromClient(context, packet); |
| 653 | if (ethernetPacketDiscover != null) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 654 | relayPacketToServer(ethernetPacketDiscover, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 655 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 656 | entry = getSubscriber(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 657 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 658 | break; |
| 659 | case DHCPOFFER: |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 660 | RelayToClientInfo r2cDataOffer = |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 661 | processDhcpPacketFromServer(context, packet); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 662 | if (r2cDataOffer != null) { |
| 663 | relayPacketToClient(r2cDataOffer, clientMacAddress); |
| 664 | entry = getSubscriber(r2cDataOffer.cp); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 665 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 666 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPOFFER")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 667 | break; |
| 668 | case DHCPREQUEST: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 669 | Ethernet ethernetPacketRequest = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 670 | processDhcpPacketFromClient(context, packet); |
| 671 | if (ethernetPacketRequest != null) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 672 | relayPacketToServer(ethernetPacketRequest, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 673 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 674 | entry = getSubscriber(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 675 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 676 | break; |
| 677 | case DHCPACK: |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 678 | RelayToClientInfo r2cDataAck = |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 679 | processDhcpPacketFromServer(context, packet); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 680 | if (r2cDataAck != null) { |
| 681 | relayPacketToClient(r2cDataAck, clientMacAddress); |
| 682 | entry = getSubscriber(r2cDataAck.cp); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 683 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 684 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPACK")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 685 | break; |
| 686 | case DHCPDECLINE: |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 687 | Ethernet ethernetPacketDecline = |
| 688 | processDhcpPacketFromClient(context, packet); |
| 689 | if (ethernetPacketDecline != null) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 690 | relayPacketToServer(ethernetPacketDecline, context); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 691 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 692 | entry = getSubscriber(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 693 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPDECLINE")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 694 | break; |
| 695 | case DHCPNAK: |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 696 | RelayToClientInfo r2cDataNack = |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 697 | processDhcpPacketFromServer(context, packet); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 698 | if (r2cDataNack != null) { |
| 699 | relayPacketToClient(r2cDataNack, clientMacAddress); |
| 700 | entry = getSubscriber(r2cDataNack.cp); |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 701 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 702 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPNACK")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 703 | break; |
| 704 | case DHCPRELEASE: |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 705 | Ethernet ethernetPacketRelease = |
| 706 | processDhcpPacketFromClient(context, packet); |
| 707 | if (ethernetPacketRelease != null) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 708 | relayPacketToServer(ethernetPacketRelease, context); |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 709 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 710 | entry = getSubscriber(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 711 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPRELEASE")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 712 | break; |
| 713 | default: |
| 714 | break; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 718 | /** |
| 719 | * Processes dhcp packets from clients. |
| 720 | * |
| 721 | * @param context the packet context |
| 722 | * @param ethernetPacket the dhcp packet from client |
| 723 | * @return the packet to relay to the server |
| 724 | */ |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 725 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
| 726 | Ethernet ethernetPacket) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 727 | if (log.isTraceEnabled()) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 728 | log.trace("DHCP Packet received from client at {} {}", |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 729 | context.inPacket().receivedFrom(), ethernetPacket); |
| 730 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 731 | |
| 732 | MacAddress relayAgentMac = relayAgentMacAddress(context); |
| 733 | if (relayAgentMac == null) { |
| 734 | log.warn("RelayAgent MAC not found "); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 735 | return null; |
| 736 | } |
| 737 | |
pier | 9e0efbe | 2020-10-28 20:33:56 +0100 | [diff] [blame] | 738 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 739 | |
| 740 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 741 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 742 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 743 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 744 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 745 | if (enableDhcpBroadcastReplies) { |
| 746 | // We want the reply to come back as a L2 broadcast |
| 747 | dhcpPacket.setFlags((short) 0x8000); |
| 748 | } |
| 749 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 750 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 751 | VlanId clientVlan = VlanId.vlanId(ethernetPacket.getVlanID()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 752 | IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 753 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 754 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
| 755 | if (entry == null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 756 | log.warn("Dropping packet as subscriber entry is not available"); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 757 | return null; |
| 758 | } |
| 759 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 760 | UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, entry); |
| 761 | if (uniTagInformation == null) { |
| 762 | log.warn("Missing service information for connectPoint {} / cTag {}", |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 763 | inPort, clientVlan); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 764 | return null; |
| 765 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 766 | DhcpOption82Data d82 = null; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 767 | if (option82) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 768 | DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry, |
| 769 | inPort, clientVlan, |
| 770 | uniTagInformation |
| 771 | .getDsPonCTagPriority()); |
| 772 | byte[] d82b = dhcpPacketWithOption82 |
| 773 | .getOption(DHCP.DHCPOptionCode.OptionCode_CircuitID) |
| 774 | .getData(); |
| 775 | d82 = new DhcpOption82Data(d82b); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 776 | udpPacket.setPayload(dhcpPacketWithOption82); |
| 777 | } |
| 778 | |
| 779 | ipv4Packet.setPayload(udpPacket); |
| 780 | etherReply.setPayload(ipv4Packet); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 781 | if (modifyClientPktsSrcDstMac) { |
| 782 | etherReply.setSourceMACAddress(relayAgentMac); |
| 783 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
| 784 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 785 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 786 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 787 | etherReply.setVlanID(uniTagInformation.getPonCTag().toShort()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 788 | etherReply.setQinQTPID(Ethernet.TYPE_VLAN); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 789 | etherReply.setQinQVID(uniTagInformation.getPonSTag().toShort()); |
| 790 | if (uniTagInformation.getUsPonSTagPriority() != -1) { |
| 791 | etherReply.setQinQPriorityCode((byte) uniTagInformation.getUsPonSTagPriority()); |
| 792 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 793 | if (uniTagInformation.getUsPonCTagPriority() != -1) { |
| 794 | etherReply.setPriorityCode((byte) uniTagInformation |
| 795 | .getUsPonCTagPriority()); |
| 796 | } |
| 797 | |
| 798 | DhcpAllocationInfo info = new DhcpAllocationInfo(inPort, |
| 799 | dhcpPacket.getPacketType(), |
| 800 | (d82 == null) |
| 801 | ? entry.circuitId() |
| 802 | : d82.getAgentCircuitId(), |
| 803 | clientMac, clientIp, |
| 804 | clientVlan, entry.id()); |
| 805 | String key = getUniqueUuidFromString(entry.id() + clientMac |
| 806 | + clientVlan); |
| 807 | allocations.put(key, info); |
| 808 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, inPort)); |
| 809 | if (log.isTraceEnabled()) { |
Andrea Campanella | d79a679 | 2020-10-26 14:44:46 +0100 | [diff] [blame] | 810 | log.trace("Finished processing DHCP Packet of type {} with MAC {} from {} " |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 811 | + "... relaying to dhcpServer", |
Andrea Campanella | d79a679 | 2020-10-26 14:44:46 +0100 | [diff] [blame] | 812 | dhcpPacket.getPacketType(), clientMac, entry.id()); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 813 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 814 | return etherReply; |
| 815 | } |
| 816 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 817 | /** |
| 818 | * Processes dhcp packets from the server. |
| 819 | * |
| 820 | * @param context the packet context |
| 821 | * @param ethernetPacket the dhcp packet |
| 822 | * @return returns information necessary for relaying packet to client |
| 823 | */ |
| 824 | private RelayToClientInfo processDhcpPacketFromServer(PacketContext context, |
| 825 | Ethernet ethernetPacket) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 826 | if (log.isTraceEnabled()) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 827 | log.trace("DHCP Packet received from server at {} {}", |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 828 | context.inPacket().receivedFrom(), ethernetPacket); |
| 829 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 830 | // get dhcp header. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 831 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 832 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 833 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 834 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 835 | VlanId innerVlan = VlanId.vlanId(ethernetPacket.getVlanID()); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 836 | MacAddress dstMac = valueOf(dhcpPacket.getClientHardwareAddress()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 837 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 838 | // we leave the srcMac from the original packet. |
| 839 | // TODO remove S-VLAN |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 840 | etherReply.setQinQVID(VlanId.NO_VID); |
| 841 | etherReply.setQinQPriorityCode((byte) 0); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 842 | etherReply.setDestinationMACAddress(dstMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 843 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 844 | // TODO deserialization of dhcp option82 leaves 'data' field null |
| 845 | // As a result we need to retrieve suboption data |
| 846 | RelayToClientInfo r2cData = null; |
| 847 | boolean usedOption82 = false; |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 848 | if (option82) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 849 | // retrieve connectPoint and vlan from option82, if it is in expected format |
| 850 | DhcpOption opt = dhcpPacket |
| 851 | .getOption(DHCP.DHCPOptionCode.OptionCode_CircuitID); |
| 852 | if (opt != null && opt instanceof DhcpRelayAgentOption) { |
| 853 | DhcpRelayAgentOption d82 = (DhcpRelayAgentOption) opt; |
| 854 | DhcpOption d82ckt = d82.getSubOption(DhcpOption82Data.CIRCUIT_ID_CODE); |
| 855 | if (d82ckt.getData() != null) { |
| 856 | r2cData = decodeCircuitId(new String(d82ckt.getData())); |
| 857 | } |
| 858 | } |
| 859 | if (r2cData != null) { |
| 860 | usedOption82 = true; |
| 861 | etherReply.setVlanID(r2cData.cvid.toShort()); |
| 862 | if (r2cData.pcp != -1) { |
| 863 | etherReply.setPriorityCode((byte) r2cData.pcp); |
| 864 | } |
| 865 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 866 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 867 | // always remove option82 if present |
| 868 | DHCP remDhcpPacket = removeOption82(dhcpPacket); |
| 869 | udpPacket.setPayload(remDhcpPacket); |
| 870 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 871 | ipv4Packet.setPayload(udpPacket); |
| 872 | etherReply.setPayload(ipv4Packet); |
| 873 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 874 | if (!usedOption82) { |
| 875 | // option 82 data not present or not used, we need to |
| 876 | // lookup host store with client dstmac and vlan from context |
| 877 | r2cData = new RelayToClientInfo(); |
| 878 | r2cData.cp = getConnectPointOfClient(dstMac, context); |
| 879 | if (r2cData.cp == null) { |
| 880 | log.warn("Couldn't find subscriber, service or host info for mac" |
| 881 | + " address {} and vlan {} .. DHCP packet can't be" |
| 882 | + " delivered to client", dstMac, innerVlan); |
| 883 | return null; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | // always need the subscriber entry |
| 888 | SubscriberAndDeviceInformation entry = getSubscriber(r2cData.cp); |
| 889 | if (entry == null) { |
| 890 | log.warn("Couldn't find subscriber info for cp {}.. DHCP packet" |
| 891 | + " can't be delivered to client mac {} and vlan {}", |
| 892 | r2cData.cp, dstMac, innerVlan); |
| 893 | return null; |
| 894 | } |
| 895 | |
| 896 | if (!usedOption82) { |
| 897 | UniTagInformation uniTagInformation = |
| 898 | getUnitagInformationFromPacketContext(context, entry); |
| 899 | if (uniTagInformation == null) { |
| 900 | log.warn("Missing service information for connectPoint {} " |
| 901 | + " cTag {} .. DHCP packet can't be delivered to client", |
| 902 | r2cData.cp, innerVlan); |
| 903 | return null; |
| 904 | } |
| 905 | r2cData.cvid = uniTagInformation.getPonCTag(); |
| 906 | r2cData.pcp = uniTagInformation.getDsPonCTagPriority(); |
| 907 | r2cData.cktId = entry.circuitId(); |
| 908 | etherReply.setVlanID(r2cData.cvid.toShort()); |
| 909 | if (r2cData.pcp != -1) { |
| 910 | etherReply.setPriorityCode((byte) r2cData.pcp); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | // update stats and events |
| 915 | IpAddress ip = IpAddress.valueOf(dhcpPacket.getYourIPAddress()); |
| 916 | DhcpAllocationInfo info = |
| 917 | new DhcpAllocationInfo(r2cData.cp, dhcpPacket.getPacketType(), |
| 918 | r2cData.cktId, dstMac, ip, innerVlan, |
| 919 | entry.id()); |
| 920 | String key = getUniqueUuidFromString(entry.id() + info.macAddress() |
| 921 | + innerVlan); |
| 922 | allocations.put(key, info); |
| 923 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, |
| 924 | r2cData.cp)); |
| 925 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames |
| 926 | .valueOf("PACKETS_FROM_SERVER")); |
| 927 | if (log.isTraceEnabled()) { |
| 928 | log.trace("Finished processing packet.. relaying to client at {}", |
| 929 | r2cData.cp); |
| 930 | } |
| 931 | r2cData.ethernetPkt = etherReply; |
| 932 | return r2cData; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 935 | // forward the packet to ConnectPoint where the DHCP server is attached. |
| 936 | private void relayPacketToServer(Ethernet packet, PacketContext context) { |
Andrea Campanella | d79a679 | 2020-10-26 14:44:46 +0100 | [diff] [blame] | 937 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 938 | if (log.isTraceEnabled()) { |
| 939 | IPv4 ipv4Packet = (IPv4) packet.getPayload(); |
| 940 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 941 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
Andrea Campanella | d79a679 | 2020-10-26 14:44:46 +0100 | [diff] [blame] | 942 | log.trace("Emitting packet to server: packet {}, with MAC {} from {}", |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 943 | getDhcpPacketType(dhcpPayload), |
Andrea Campanella | d79a679 | 2020-10-26 14:44:46 +0100 | [diff] [blame] | 944 | MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()), |
| 945 | entry.id()); |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 946 | } |
| 947 | ConnectPoint toSendTo = null; |
| 948 | if (!useOltUplink) { |
| 949 | toSendTo = dhcpServerConnectPoint.get(); |
| 950 | } else { |
| 951 | toSendTo = getUplinkConnectPointOfOlt(context.inPacket().receivedFrom() |
| 952 | .deviceId()); |
| 953 | } |
| 954 | |
| 955 | if (toSendTo != null) { |
| 956 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 957 | .setOutput(toSendTo.port()).build(); |
| 958 | OutboundPacket o = new DefaultOutboundPacket(toSendTo |
| 959 | .deviceId(), t, ByteBuffer.wrap(packet.serialize())); |
| 960 | if (log.isTraceEnabled()) { |
| 961 | log.trace("Relaying packet to dhcp server at {} {}", toSendTo, |
| 962 | packet); |
| 963 | } |
| 964 | packetService.emit(o); |
| 965 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 966 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames |
| 967 | .valueOf("PACKETS_TO_SERVER")); |
| 968 | } else { |
| 969 | log.error("No connect point to send msg to DHCP Server"); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // send the response to the requester host (client) |
| 974 | private void relayPacketToClient(RelayToClientInfo r2cData, |
| 975 | MacAddress dstMac) { |
| 976 | ConnectPoint subCp = r2cData.cp; |
| 977 | Ethernet ethPacket = r2cData.ethernetPkt; |
| 978 | // Send packet out to requester if the host information is available |
| 979 | if (subCp != null) { |
| 980 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
| 981 | .setOutput(subCp.port()).build(); |
| 982 | OutboundPacket o = new DefaultOutboundPacket(subCp.deviceId(), |
| 983 | t, ByteBuffer.wrap(ethPacket.serialize())); |
| 984 | if (log.isTraceEnabled()) { |
| 985 | log.trace("Relaying packet to DHCP client at {} with " |
| 986 | + "MacAddress {}, {} given {}", subCp, dstMac, |
| 987 | ethPacket, r2cData); |
| 988 | } |
| 989 | packetService.emit(o); |
| 990 | } else { |
| 991 | log.error("Dropping DHCP Packet because unknown connectPoint for {}", |
| 992 | dstMac); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Option 82 includes circuitId and remoteId data configured by an |
| 998 | * operator in sadis for a subscriber, and can be a string in any form |
| 999 | * relevant to the operator's dhcp-server. When circuitId is configured |
| 1000 | * in sadis, the relay agent adds the option, but does not use the |
| 1001 | * information for forwarding packets back to client. |
| 1002 | * <p> |
| 1003 | * If circuitId is not configured in sadis, this relay-agent adds |
| 1004 | * circuitId information in the form |
| 1005 | * "{@literal<}connectPoint>:vlan{@literal<}clientVlanId>:pcp{@literal<}downstreamPcp>" |
| 1006 | * for example, "of:0000000000000001/32:vlan200:pcp7". When the packet |
| 1007 | * is received back from the server with circuitId in this form, this |
| 1008 | * relay agent will use this information to forward packets to the |
| 1009 | * client. |
| 1010 | * |
| 1011 | * @param dhcpPacket the DHCP packet to transform |
| 1012 | * @param entry sadis information for the subscriber |
| 1013 | * @param cp the connectPoint to set if sadis entry has no circuitId |
| 1014 | * @param clientVlan the vlan to set if sadis entry has no circuitId |
| 1015 | * @param downstreamPbits the pbits to set if sadis entry has no |
| 1016 | * circuitId |
| 1017 | * @return the modified dhcp packet with option82 added |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1018 | */ |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1019 | private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry, |
| 1020 | ConnectPoint cp, VlanId clientVlan, |
| 1021 | int downstreamPbits) { |
| 1022 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
| 1023 | DhcpOption82Data option82 = new DhcpOption82Data(); |
| 1024 | if (entry.circuitId() == null || entry.circuitId().isBlank()) { |
| 1025 | option82.setAgentCircuitId(cp + VLAN_KEYWORD + clientVlan |
| 1026 | + PCP_KEYWORD |
| 1027 | + downstreamPbits); |
| 1028 | } else { |
| 1029 | option82.setAgentCircuitId(entry.circuitId()); |
| 1030 | } |
| 1031 | option82.setAgentRemoteId(entry.remoteId()); |
| 1032 | if (log.isTraceEnabled()) { |
| 1033 | log.trace("adding option82 {} ", option82); |
| 1034 | } |
| 1035 | DhcpOption option = new DhcpOption() |
| 1036 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 1037 | .setData(option82.toByteArray()) |
| 1038 | .setLength(option82.length()); |
| 1039 | |
| 1040 | options.add(options.size() - 1, option); |
| 1041 | dhcpPacket.setOptions(options); |
| 1042 | |
| 1043 | return dhcpPacket; |
| 1044 | } |
| 1045 | |
| 1046 | private DHCP removeOption82(DHCP dhcpPacket) { |
| 1047 | List<DhcpOption> options = dhcpPacket.getOptions(); |
| 1048 | List<DhcpOption> newoptions = options.stream() |
| 1049 | .filter(option -> option |
| 1050 | .getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID |
| 1051 | .getValue()) |
| 1052 | .collect(Collectors.toList()); |
| 1053 | |
| 1054 | return dhcpPacket.setOptions(newoptions); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * Returns the circuit Id values decoded from the option 82 data. Decoding |
| 1059 | * is performed if and only if the circuit id format is in the form |
| 1060 | * "{@literal<}connectPoint>:vlan{@literal<}clientVlanId>:pcp{@literal<}downstreamPcp>" |
| 1061 | * |
| 1062 | * @param cktId the circuitId string from option 82 data |
| 1063 | * @return decoded circuit id data if it is in the expected format or |
| 1064 | * null |
| 1065 | */ |
| 1066 | private RelayToClientInfo decodeCircuitId(String cktId) { |
| 1067 | if (cktId.contains(VLAN_KEYWORD) && cktId.contains(PCP_KEYWORD)) { |
| 1068 | ConnectPoint cp = ConnectPoint |
| 1069 | .fromString(cktId |
| 1070 | .substring(0, cktId.indexOf(VLAN_KEYWORD))); |
| 1071 | VlanId cvid = VlanId |
| 1072 | .vlanId(cktId.substring( |
| 1073 | cktId.indexOf(VLAN_KEYWORD) |
| 1074 | + VLAN_KEYWORD.length(), |
| 1075 | cktId.indexOf(PCP_KEYWORD))); |
| 1076 | int pcp = Integer |
| 1077 | .valueOf(cktId.substring(cktId.indexOf(PCP_KEYWORD) |
| 1078 | + PCP_KEYWORD.length())) |
| 1079 | .intValue(); |
| 1080 | log.debug("retrieved from option82-> cp={} cvlan={} down-pcp={}" |
| 1081 | + " for relaying to client ", cp, cvid, pcp); |
| 1082 | return new RelayToClientInfo(cp, cvid, pcp, cktId); |
| 1083 | } else { |
| 1084 | log.debug("Option 82 circuitId {} is operator defined and will " |
| 1085 | + "not be used for forwarding", cktId); |
| 1086 | return null; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | private class RelayToClientInfo { |
| 1091 | Ethernet ethernetPkt; |
| 1092 | ConnectPoint cp; |
| 1093 | VlanId cvid; |
| 1094 | int pcp; |
| 1095 | String cktId; |
| 1096 | |
| 1097 | public RelayToClientInfo(ConnectPoint cp, VlanId cvid, int pcp, |
| 1098 | String cktId) { |
| 1099 | this.cp = cp; |
| 1100 | this.cvid = cvid; |
| 1101 | this.pcp = pcp; |
| 1102 | this.cktId = cktId; |
| 1103 | } |
| 1104 | |
| 1105 | public RelayToClientInfo() { |
| 1106 | } |
| 1107 | |
| 1108 | @Override |
| 1109 | public String toString() { |
| 1110 | return "RelayToClientInfo: {connectPoint=" + cp + " clientVlan=" |
| 1111 | + cvid + " clientPcp=" + pcp + " circuitId=" + cktId + "}"; |
| 1112 | } |
| 1113 | |
| 1114 | } |
| 1115 | |
| 1116 | // get the type of the DHCP packet |
| 1117 | private DHCP.MsgType getDhcpPacketType(DHCP dhcpPayload) { |
| 1118 | for (DhcpOption option : dhcpPayload.getOptions()) { |
| 1119 | if (option.getCode() == OptionCode_MessageType.getValue()) { |
| 1120 | byte[] data = option.getData(); |
| 1121 | return DHCP.MsgType.getType(data[0]); |
| 1122 | } |
| 1123 | } |
| 1124 | return null; |
| 1125 | } |
| 1126 | |
| 1127 | private void updateDhcpRelayCountersStore(SubscriberAndDeviceInformation entry, |
| 1128 | DhcpL2RelayCounterNames counterType) { |
| 1129 | // Update global counter stats |
| 1130 | dhcpL2RelayCounters.incrementCounter(DhcpL2RelayEvent.GLOBAL_COUNTER, |
| 1131 | counterType); |
| 1132 | if (entry == null) { |
| 1133 | log.warn("Counter not updated as subscriber info not found."); |
| 1134 | } else { |
| 1135 | // Update subscriber counter stats |
| 1136 | dhcpL2RelayCounters.incrementCounter(entry.id(), counterType); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * Get subscriber information based on subscriber's connectPoint. |
| 1142 | * |
| 1143 | * @param subsCp the subscriber's connectPoint |
| 1144 | * @return subscriber sadis info or null if not found |
| 1145 | */ |
| 1146 | private SubscriberAndDeviceInformation getSubscriber(ConnectPoint subsCp) { |
| 1147 | if (subsCp != null) { |
| 1148 | String portName = getPortName(subsCp); |
| 1149 | return subsService.get(portName); |
| 1150 | } |
| 1151 | return null; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * Returns sadis info for subscriber based on incoming packet context. |
| 1156 | * The packet context must refer to a packet coming from a subscriber |
| 1157 | * port. |
| 1158 | * |
| 1159 | * @param context incoming packet context from subscriber port (UNI) |
| 1160 | * @return sadis info for the subscriber or null |
| 1161 | */ |
| 1162 | private SubscriberAndDeviceInformation getSubscriber(PacketContext context) { |
| 1163 | String portName = getPortName(context.inPacket().receivedFrom()); |
| 1164 | return subsService.get(portName); |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * Returns ConnectPoint of the Client based on MAC address and C-VLAN. |
| 1169 | * Verifies that returned connect point has service defined in sadis. |
| 1170 | * |
| 1171 | * @param dstMac client dstMac |
| 1172 | * @param context context for incoming packet, parsed for C-vlan id |
| 1173 | * @return connect point information for client or null if connect point |
| 1174 | * not found or service cannot be verified for client info |
| 1175 | */ |
| 1176 | private ConnectPoint getConnectPointOfClient(MacAddress dstMac, |
| 1177 | PacketContext context) { |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1178 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 1179 | if (hosts == null || hosts.isEmpty()) { |
| 1180 | log.warn("Cannot determine host for DHCP client: {}. Aborting " |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1181 | + "relay for DHCP Packet from server", dstMac); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1182 | return null; |
| 1183 | } |
| 1184 | for (Host h : hosts) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1185 | // if more than one (for example, multiple services with same |
| 1186 | // mac-address but different service VLANs (inner/C vlans) |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1187 | // find the connect point which has an valid entry in SADIS |
| 1188 | ConnectPoint cp = new ConnectPoint(h.location().deviceId(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1189 | h.location().port()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1190 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1191 | SubscriberAndDeviceInformation sub = getSubscriber(cp); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 1192 | if (sub == null) { |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1193 | log.warn("Subscriber info not found for {} for host {}", cp, h); |
| 1194 | continue; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1195 | } |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1196 | // check for cvlan in subscriber's uniTagInfo list |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1197 | UniTagInformation uniTagInformation = |
| 1198 | getUnitagInformationFromPacketContext(context, sub); |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 1199 | if (uniTagInformation != null) { |
| 1200 | return cp; |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 1201 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 1202 | } |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 1203 | // no sadis config found for this connectPoint/vlan |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1204 | log.warn("Missing service information for dhcp packet received from" |
| 1205 | + " {} with cTag {} .. cannot relay to client", |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1206 | context.inPacket().receivedFrom(), |
| 1207 | context.inPacket().parsed().getVlanID()); |
| 1208 | return null; |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Returns the port-name for the given connectPoint port. |
| 1213 | * |
| 1214 | * @param cp the given connect point |
| 1215 | * @return the port-name for the connect point port |
| 1216 | */ |
| 1217 | private String getPortName(ConnectPoint cp) { |
| 1218 | Port p = deviceService.getPort(cp); |
| 1219 | return p.annotations().value(AnnotationKeys.PORT_NAME); |
| 1220 | } |
| 1221 | |
| 1222 | /** |
| 1223 | * Return's uniTagInformation (service information) if incoming packet's |
| 1224 | * client VLAN id matches the subscriber's service info, and dhcp is |
| 1225 | * required for this service. |
| 1226 | * |
| 1227 | * @param context |
| 1228 | * @param sub |
| 1229 | * @return |
| 1230 | */ |
| 1231 | private UniTagInformation getUnitagInformationFromPacketContext(PacketContext context, |
| 1232 | SubscriberAndDeviceInformation sub) { |
| 1233 | // If the ctag is defined in the tagList and dhcp is required, |
| 1234 | // return the service info |
| 1235 | List<UniTagInformation> tagList = sub.uniTagList(); |
| 1236 | for (UniTagInformation uniServiceInformation : tagList) { |
| 1237 | if (uniServiceInformation.getPonCTag().toShort() == context.inPacket() |
| 1238 | .parsed().getVlanID()) { |
| 1239 | if (uniServiceInformation.getIsDhcpRequired()) { |
| 1240 | return uniServiceInformation; |
| 1241 | } |
| 1242 | } |
| 1243 | } |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 1244 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 1245 | return null; |
| 1246 | } |
| 1247 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1248 | |
| 1249 | private MacAddress relayAgentMacAddress(PacketContext context) { |
| 1250 | SubscriberAndDeviceInformation device = this.getDevice(context); |
| 1251 | if (device == null) { |
| 1252 | log.warn("Device not found for {}", context.inPacket().receivedFrom()); |
| 1253 | return null; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1254 | } |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1255 | return device.hardwareIdentifier(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1256 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1257 | |
Saurav Das | 45861d4 | 2020-10-07 00:03:23 -0700 | [diff] [blame] | 1258 | /** |
| 1259 | * Returns sadis information for device from which packet was received. |
| 1260 | * |
| 1261 | * @param context the packet context |
| 1262 | * @return sadis information for device |
| 1263 | */ |
| 1264 | private SubscriberAndDeviceInformation getDevice(PacketContext context) { |
| 1265 | String serialNo = deviceService |
| 1266 | .getDevice(context.inPacket().receivedFrom().deviceId()) |
| 1267 | .serialNumber(); |
| 1268 | return subsService.get(serialNo); |
| 1269 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1270 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1271 | } |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1272 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1273 | /** |
| 1274 | * Listener for network config events. |
| 1275 | */ |
| 1276 | private class InternalConfigListener implements NetworkConfigListener { |
| 1277 | |
| 1278 | @Override |
| 1279 | public void event(NetworkConfigEvent event) { |
| 1280 | |
| 1281 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 1282 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 1283 | event.configClass().equals(DhcpL2RelayConfig.class)) { |
| 1284 | updateConfig(); |
| 1285 | log.info("Reconfigured"); |
| 1286 | } |
| 1287 | } |
| 1288 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1289 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1290 | /** |
| 1291 | * Handles Mastership changes for the devices which connect |
| 1292 | * to the DHCP server. |
| 1293 | */ |
| 1294 | private class InnerMastershipListener implements MastershipListener { |
| 1295 | @Override |
| 1296 | public void event(MastershipEvent event) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1297 | if (!useOltUplink) { |
| 1298 | if (dhcpServerConnectPoint.get() != null && |
| 1299 | dhcpServerConnectPoint.get().deviceId(). |
| 1300 | equals(event.subject())) { |
| 1301 | log.trace("Mastership Event recevived for {}", event.subject()); |
| 1302 | // mastership of the device for our connect point has changed |
| 1303 | // reselect |
| 1304 | selectServerConnectPoint(); |
| 1305 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1309 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1310 | private void removeAllocations(Predicate<Map.Entry<String, Versioned<DhcpAllocationInfo>>> pred) { |
| 1311 | allocations.stream() |
| 1312 | .filter(pred) |
| 1313 | .map(Map.Entry::getKey) |
| 1314 | .collect(Collectors.toList()) |
| 1315 | .forEach(allocations::remove); |
| 1316 | } |
| 1317 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1318 | @Override |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1319 | public void clearAllocations() { |
| 1320 | allocations.clear(); |
| 1321 | } |
| 1322 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1323 | @Override |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1324 | public boolean removeAllocationsByConnectPoint(ConnectPoint cp) { |
| 1325 | boolean removed = false; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1326 | for (String key : allocations.keySet()) { |
| 1327 | DhcpAllocationInfo entry = allocations.asJavaMap().get(key); |
| 1328 | if (entry.location().equals(cp)) { |
| 1329 | allocations.remove(key); |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1330 | removed = true; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1331 | } |
| 1332 | } |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1333 | return removed; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 1336 | /** |
| 1337 | * Checks for mastership or falls back to leadership on deviceId. |
| 1338 | * If the node is not master and device is available |
| 1339 | * or the device is not available and the leader is different |
| 1340 | * we let master or leader else handle it |
| 1341 | * Leadership on the device topic is needed because the master can be NONE |
| 1342 | * in case the device went away, we still need to handle events |
| 1343 | * consistently |
| 1344 | */ |
| 1345 | private boolean isLocalLeader(DeviceId deviceId) { |
| 1346 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1347 | // When the device is available we just check the mastership |
| 1348 | if (deviceService.isAvailable(deviceId)) { |
| 1349 | return false; |
| 1350 | } |
| 1351 | // Fallback with Leadership service - device id is used as topic |
| 1352 | NodeId leader = leadershipService.runForLeadership( |
| 1353 | deviceId.toString()).leaderNodeId(); |
| 1354 | // Verify if this node is the leader |
| 1355 | return clusterService.getLocalNode().id().equals(leader); |
| 1356 | } |
| 1357 | return true; |
| 1358 | } |
| 1359 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1360 | /** |
| 1361 | * Handles Device status change for the devices which connect |
| 1362 | * to the DHCP server. |
| 1363 | */ |
| 1364 | private class InnerDeviceListener implements DeviceListener { |
| 1365 | @Override |
| 1366 | public void event(DeviceEvent event) { |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 1367 | final DeviceId deviceId = event.subject().id(); |
| 1368 | |
| 1369 | // Ensure only one instance handles the event |
| 1370 | if (!isLocalLeader(deviceId)) { |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1371 | return; |
| 1372 | } |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1373 | // ignore stats |
| 1374 | if (event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) { |
| 1375 | return; |
| 1376 | } |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1377 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame] | 1378 | log.debug("Device Event received for {} event {}", event.subject(), |
| 1379 | event.type()); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1380 | |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1381 | switch (event.type()) { |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1382 | case DEVICE_REMOVED: |
| 1383 | log.info("Device removed {}", event.subject().id()); |
| 1384 | removeAllocations(e -> e.getValue().value().location().deviceId().equals(deviceId)); |
| 1385 | break; |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1386 | case DEVICE_AVAILABILITY_CHANGED: |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1387 | boolean available = deviceService.isAvailable(deviceId); |
| 1388 | log.info("Device Avail Changed {} to {}", event.subject().id(), available); |
| 1389 | |
| 1390 | if (!available && deviceService.getPorts(deviceId).isEmpty()) { |
| 1391 | removeAllocations(e -> e.getValue().value().location().deviceId().equals(deviceId)); |
| 1392 | log.info("Device {} is removed from DHCP allocationmap ", deviceId); |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1393 | } |
| 1394 | break; |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1395 | case PORT_REMOVED: |
| 1396 | Port port = event.port(); |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1397 | log.info("Port {} is deleted on device {}", port, deviceId); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1398 | |
| 1399 | ConnectPoint cp = new ConnectPoint(deviceId, port.number()); |
| 1400 | removeAllocations(e -> e.getValue().value().location().equals(cp)); |
| 1401 | |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1402 | log.info("Port {} on device {} is removed from DHCP allocationmap", event.port(), deviceId); |
| 1403 | break; |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1404 | default: |
| 1405 | break; |
| 1406 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1407 | if (!useOltUplink) { |
| 1408 | if (dhcpServerConnectPoint.get() == null) { |
| 1409 | switch (event.type()) { |
| 1410 | case DEVICE_ADDED: |
| 1411 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1412 | // some device is available check if we can get a |
| 1413 | // connect point we can use |
| 1414 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1415 | break; |
| 1416 | default: |
| 1417 | break; |
| 1418 | } |
| 1419 | return; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1420 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1421 | if (dhcpServerConnectPoint.get().deviceId(). |
| 1422 | equals(event.subject().id())) { |
| 1423 | switch (event.type()) { |
| 1424 | case DEVICE_AVAILABILITY_CHANGED: |
| 1425 | case DEVICE_REMOVED: |
| 1426 | case DEVICE_SUSPENDED: |
| 1427 | // state of our device has changed, check if we need |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1428 | // to re-select a connectpoint |
| 1429 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1430 | break; |
| 1431 | default: |
| 1432 | break; |
| 1433 | } |
| 1434 | } |
| 1435 | } else { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1436 | switch (event.type()) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1437 | case PORT_ADDED: |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 1438 | if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1439 | requestDhcpPacketsFromConnectPoint( |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1440 | new ConnectPoint(event.subject().id(), event.port().number()), |
| 1441 | Optional.empty()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1442 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1443 | break; |
| 1444 | default: |
| 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 1450 | |
| 1451 | private class InnerDhcpL2RelayStoreDelegate implements DhcpL2RelayStoreDelegate { |
| 1452 | @Override |
| 1453 | public void notify(DhcpL2RelayEvent event) { |
| 1454 | if (event.type().equals(DhcpL2RelayEvent.Type.STATS_UPDATE)) { |
| 1455 | DhcpL2RelayEvent toPost = event; |
| 1456 | if (event.getSubscriberId() != null) { |
| 1457 | // infuse the event with the allocation info before posting |
| 1458 | DhcpAllocationInfo info = Versioned.valueOrNull(allocations.get(event.getSubscriberId())); |
| 1459 | toPost = new DhcpL2RelayEvent(event.type(), info, event.connectPoint(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1460 | event.getCountersEntry(), event.getSubscriberId()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 1461 | } |
| 1462 | post(toPost); |
| 1463 | } |
| 1464 | |
| 1465 | } |
| 1466 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1467 | } |