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