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