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