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 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 725 | log.info("Received DHCP Packet of type {} from {}", |
| 726 | incomingPacketType, context.inPacket().receivedFrom()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 727 | |
| 728 | switch (incomingPacketType) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 729 | case DHCPDISCOVER: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 730 | Ethernet ethernetPacketDiscover = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 731 | processDhcpPacketFromClient(context, packet); |
| 732 | if (ethernetPacketDiscover != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 733 | forwardPacket(ethernetPacketDiscover, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 734 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 735 | entry = getSubscriberInfoFromClient(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 736 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 737 | break; |
| 738 | case DHCPOFFER: |
| 739 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 740 | Ethernet ethernetPacketOffer = |
| 741 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 742 | if (ethernetPacketOffer != null) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 743 | sendReply(ethernetPacketOffer, dhcpPayload, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 744 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 745 | entry = getSubscriberInfoFromServer(dhcpPayload, context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 746 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPOFFER")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 747 | break; |
| 748 | case DHCPREQUEST: |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 749 | Ethernet ethernetPacketRequest = |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 750 | processDhcpPacketFromClient(context, packet); |
| 751 | if (ethernetPacketRequest != null) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 752 | forwardPacket(ethernetPacketRequest, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 753 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 754 | entry = getSubscriberInfoFromClient(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 755 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 756 | break; |
| 757 | case DHCPACK: |
| 758 | //reply to dhcp client. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 759 | Ethernet ethernetPacketAck = |
| 760 | processDhcpPacketFromServer(context, packet); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 761 | if (ethernetPacketAck != null) { |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 762 | sendReply(ethernetPacketAck, dhcpPayload, context); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 763 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 764 | entry = getSubscriberInfoFromServer(dhcpPayload, context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 765 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPACK")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 766 | break; |
| 767 | case DHCPDECLINE: |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 768 | Ethernet ethernetPacketDecline = |
| 769 | processDhcpPacketFromClient(context, packet); |
| 770 | if (ethernetPacketDecline != null) { |
| 771 | forwardPacket(ethernetPacketDecline, context); |
| 772 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 773 | entry = getSubscriberInfoFromClient(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 774 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPDECLINE")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 775 | break; |
| 776 | case DHCPNAK: |
Arjun E K | 05ad20b | 2020-03-13 13:25:17 +0000 | [diff] [blame] | 777 | //reply to dhcp client. |
| 778 | Ethernet ethernetPacketNak = |
| 779 | processDhcpPacketFromServer(context, packet); |
| 780 | if (ethernetPacketNak != null) { |
| 781 | sendReply(ethernetPacketNak, dhcpPayload, context); |
| 782 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 783 | entry = getSubscriberInfoFromServer(dhcpPayload, context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 784 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPNACK")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 785 | break; |
| 786 | case DHCPRELEASE: |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 787 | Ethernet ethernetPacketRelease = |
| 788 | processDhcpPacketFromClient(context, packet); |
| 789 | if (ethernetPacketRelease != null) { |
| 790 | forwardPacket(ethernetPacketRelease, context); |
| 791 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 792 | entry = getSubscriberInfoFromClient(context); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 793 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("DHCPRELEASE")); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 794 | break; |
| 795 | default: |
| 796 | break; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 800 | private Ethernet processDhcpPacketFromClient(PacketContext context, |
| 801 | Ethernet ethernetPacket) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 802 | if (log.isTraceEnabled()) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 803 | log.trace("DHCP Packet received from client at {} {}", |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 804 | context.inPacket().receivedFrom(), ethernetPacket); |
| 805 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 806 | |
| 807 | MacAddress relayAgentMac = relayAgentMacAddress(context); |
| 808 | if (relayAgentMac == null) { |
| 809 | log.warn("RelayAgent MAC not found "); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 810 | return null; |
| 811 | } |
| 812 | |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 813 | Ethernet etherReply = ethernetPacket; |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 814 | |
| 815 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 816 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 817 | DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 818 | ConnectPoint inPort = context.inPacket().receivedFrom(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 819 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 820 | if (enableDhcpBroadcastReplies) { |
| 821 | // We want the reply to come back as a L2 broadcast |
| 822 | dhcpPacket.setFlags((short) 0x8000); |
| 823 | } |
| 824 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 825 | MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 826 | VlanId clientVlan = VlanId.vlanId(ethernetPacket.getVlanID()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 827 | IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 828 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 829 | SubscriberAndDeviceInformation entry = getSubscriber(context); |
| 830 | if (entry == null) { |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 831 | log.warn("Dropping packet as subscriber entry is not available"); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 832 | return null; |
| 833 | } |
| 834 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 835 | UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, entry); |
| 836 | if (uniTagInformation == null) { |
| 837 | log.warn("Missing service information for connectPoint {} / cTag {}", |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 838 | inPort, clientVlan); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 839 | return null; |
| 840 | } |
| 841 | |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 842 | DhcpAllocationInfo info = new DhcpAllocationInfo( |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 843 | inPort, dhcpPacket.getPacketType(), entry.circuitId(), |
| 844 | clientMac, clientIp, clientVlan, entry.id()); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 845 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 846 | String key = getUniqueUuidFromString(entry.id() + clientMac |
| 847 | + clientVlan); |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 848 | allocations.put(key, info); |
Jonathan Hart | c36c955 | 2018-07-31 15:07:53 -0400 | [diff] [blame] | 849 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 850 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 851 | inPort)); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 852 | if (option82) { |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 853 | DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry); |
| 854 | udpPacket.setPayload(dhcpPacketWithOption82); |
| 855 | } |
| 856 | |
| 857 | ipv4Packet.setPayload(udpPacket); |
| 858 | etherReply.setPayload(ipv4Packet); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 859 | if (modifyClientPktsSrcDstMac) { |
| 860 | etherReply.setSourceMACAddress(relayAgentMac); |
| 861 | etherReply.setDestinationMACAddress(dhcpConnectMac); |
| 862 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 863 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 864 | etherReply.setPriorityCode(ethernetPacket.getPriorityCode()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 865 | etherReply.setVlanID(uniTagInformation.getPonCTag().toShort()); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 866 | etherReply.setQinQTPID(Ethernet.TYPE_VLAN); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 867 | etherReply.setQinQVID(uniTagInformation.getPonSTag().toShort()); |
| 868 | if (uniTagInformation.getUsPonSTagPriority() != -1) { |
| 869 | etherReply.setQinQPriorityCode((byte) uniTagInformation.getUsPonSTagPriority()); |
| 870 | } |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 871 | log.info("Finished processing DHCP Packet of type {} from {} and relaying to dhcpServer", |
| 872 | dhcpPacket.getPacketType(), entry.id()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 873 | return etherReply; |
| 874 | } |
| 875 | |
| 876 | //build the DHCP offer/ack with proper client port. |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 877 | private Ethernet processDhcpPacketFromServer(PacketContext context, |
| 878 | Ethernet ethernetPacket) { |
| 879 | if (log.isTraceEnabled()) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 880 | log.trace("DHCP Packet received from server at {} {}", |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 881 | context.inPacket().receivedFrom(), ethernetPacket); |
| 882 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 883 | // get dhcp header. |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 884 | Ethernet etherReply = (Ethernet) ethernetPacket.clone(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 885 | IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); |
| 886 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 887 | DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 888 | VlanId innerVlan = VlanId.vlanId(ethernetPacket.getVlanID()); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 889 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 890 | MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 891 | ConnectPoint subsCp = getConnectPointOfClient(dstMac, context); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 892 | // If we can't find the subscriber, can't process further |
| 893 | if (subsCp == null) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 894 | log.warn("Couldn't find subscriber, service or host info for mac" |
| 895 | + " address {} .. DHCP packet won't be delivered", dstMac); |
Amit Ghosh | 2095dc6 | 2017-09-25 20:56:55 +0100 | [diff] [blame] | 896 | return null; |
| 897 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 898 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 899 | SubscriberAndDeviceInformation entry = getSubscriberInfoFromServer(dhcpPayload, context); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 900 | if (entry != null) { |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 901 | IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress()); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 902 | // store DHCPAllocationInfo |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 903 | DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp, |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 904 | dhcpPayload.getPacketType(), entry.circuitId(), dstMac, ip, |
| 905 | innerVlan, entry.id()); |
| 906 | String key = getUniqueUuidFromString(entry.id() |
| 907 | + info.macAddress() + innerVlan); |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 908 | allocations.put(key, info); |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 909 | |
| 910 | post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp)); |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 911 | } |
Thomas Lee S | 0dc9a3b | 2020-01-14 10:42:29 +0530 | [diff] [blame] | 912 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 913 | UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, entry); |
| 914 | if (uniTagInformation == null) { |
| 915 | log.warn("Missing service information for connectPoint {} / cTag {}", |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 916 | context.inPacket().receivedFrom(), context.inPacket().parsed().getVlanID()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 917 | return null; |
| 918 | } |
| 919 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 920 | updateDhcpRelayCountersStore(entry, DhcpL2RelayCounterNames.valueOf("PACKETS_FROM_SERVER")); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 921 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 922 | // we leave the srcMac from the original packet |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 923 | etherReply.setQinQVID(VlanId.NO_VID); |
| 924 | etherReply.setQinQPriorityCode((byte) 0); |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 925 | etherReply.setDestinationMACAddress(dstMac); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 926 | etherReply.setVlanID(uniTagInformation.getPonCTag().toShort()); |
| 927 | if (uniTagInformation.getUsPonCTagPriority() != -1) { |
| 928 | etherReply.setPriorityCode((byte) uniTagInformation.getUsPonCTagPriority()); |
| 929 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 930 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 931 | if (option82) { |
| 932 | udpPacket.setPayload(removeOption82(dhcpPayload)); |
| 933 | } else { |
| 934 | udpPacket.setPayload(dhcpPayload); |
| 935 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 936 | ipv4Packet.setPayload(udpPacket); |
| 937 | etherReply.setPayload(ipv4Packet); |
| 938 | |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 939 | log.info("Finished processing packet.. relaying to client"); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 940 | return etherReply; |
| 941 | } |
| 942 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 943 | /* |
| 944 | * Get ConnectPoint of the Client based on it's MAC address |
| 945 | */ |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 946 | private ConnectPoint getConnectPointOfClient(MacAddress dstMac, PacketContext context) { |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 947 | Set<Host> hosts = hostService.getHostsByMac(dstMac); |
| 948 | if (hosts == null || hosts.isEmpty()) { |
| 949 | log.warn("Cannot determine host for DHCP client: {}. Aborting " |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 950 | + "relay for DHCP Packet from server", dstMac); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 951 | return null; |
| 952 | } |
| 953 | for (Host h : hosts) { |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 954 | // if more than one (for example, multiple services with same |
| 955 | // mac-address but different service VLANs (inner/C vlans) |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 956 | // find the connect point which has an valid entry in SADIS |
| 957 | ConnectPoint cp = new ConnectPoint(h.location().deviceId(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 958 | h.location().port()); |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 959 | |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 960 | String portId = nasPortId(cp); |
| 961 | SubscriberAndDeviceInformation sub = subsService.get(portId); |
| 962 | if (sub == null) { |
| 963 | log.warn("Subscriber info not found for {}", cp); |
| 964 | return null; |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 965 | } |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 966 | // check for cvlan in subscriber's uniTagInfo list |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 967 | UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, sub); |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 968 | if (uniTagInformation != null) { |
| 969 | return cp; |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 970 | } |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 971 | } |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 972 | // no sadis config found for this connectPoint/vlan |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 973 | log.warn("Missing service information for dhcp packet received from" |
| 974 | + " {} with cTag {} .. cannot relay to client", |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 975 | context.inPacket().receivedFrom(), context.inPacket().parsed().getVlanID()); |
Jonathan Hart | b4fbc92 | 2020-04-14 12:17:44 -0700 | [diff] [blame] | 976 | |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 977 | return null; |
| 978 | } |
| 979 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 980 | // send the response to the requester host (client) |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 981 | private void sendReply(Ethernet ethPacket, DHCP dhcpPayload, PacketContext context) { |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 982 | MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress()); |
Gamze Abaka | a64b3bc | 2020-01-31 06:51:43 +0000 | [diff] [blame] | 983 | ConnectPoint subCp = getConnectPointOfClient(descMac, context); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 984 | |
| 985 | // Send packet out to requester if the host information is available |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 986 | if (subCp != null) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 987 | log.info("Sending DHCP Packet to client at {}", subCp); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 988 | TrafficTreatment t = DefaultTrafficTreatment.builder() |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 989 | .setOutput(subCp.port()).build(); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 990 | OutboundPacket o = new DefaultOutboundPacket( |
Amit Ghosh | a17354e | 2017-08-23 12:56:04 +0100 | [diff] [blame] | 991 | subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize())); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 992 | if (log.isTraceEnabled()) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 993 | log.trace("Relaying packet to DHCP client at {} {}", subCp, |
Saurav Das | 15626a0 | 2018-09-27 18:36:45 -0700 | [diff] [blame] | 994 | ethPacket); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 995 | } |
| 996 | packetService.emit(o); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 997 | } else { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 998 | log.error("Dropping DHCP Packet because can't find host for {}", descMac); |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1003 | private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) { |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1004 | log.trace("option82data {} ", entry); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1005 | |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 1006 | List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1007 | DhcpOption82 option82 = new DhcpOption82(); |
| 1008 | option82.setAgentCircuitId(entry.circuitId()); |
| 1009 | option82.setAgentRemoteId(entry.remoteId()); |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 1010 | DhcpOption option = new DhcpOption() |
Deepa Vaddireddy | 5f278d6 | 2017-08-30 05:59:39 +0530 | [diff] [blame] | 1011 | .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 1012 | .setData(option82.toByteArray()) |
| 1013 | .setLength(option82.length()); |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1014 | |
| 1015 | options.add(options.size() - 1, option); |
| 1016 | dhcpPacket.setOptions(options); |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1017 | |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1018 | return dhcpPacket; |
| 1019 | |
| 1020 | } |
| 1021 | |
| 1022 | private DHCP removeOption82(DHCP dhcpPacket) { |
Jonathan Hart | edbf642 | 2018-05-02 17:30:05 -0700 | [diff] [blame] | 1023 | List<DhcpOption> options = dhcpPacket.getOptions(); |
| 1024 | List<DhcpOption> newoptions = options.stream() |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1025 | .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue()) |
| 1026 | .collect(Collectors.toList()); |
| 1027 | |
| 1028 | return dhcpPacket.setOptions(newoptions); |
| 1029 | } |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1030 | |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1031 | /** |
| 1032 | * Listener for network config events. |
| 1033 | */ |
| 1034 | private class InternalConfigListener implements NetworkConfigListener { |
| 1035 | |
| 1036 | @Override |
| 1037 | public void event(NetworkConfigEvent event) { |
| 1038 | |
| 1039 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 1040 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 1041 | event.configClass().equals(DhcpL2RelayConfig.class)) { |
| 1042 | updateConfig(); |
| 1043 | log.info("Reconfigured"); |
| 1044 | } |
| 1045 | } |
| 1046 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1047 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1048 | /** |
| 1049 | * Handles Mastership changes for the devices which connect |
| 1050 | * to the DHCP server. |
| 1051 | */ |
| 1052 | private class InnerMastershipListener implements MastershipListener { |
| 1053 | @Override |
| 1054 | public void event(MastershipEvent event) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1055 | if (!useOltUplink) { |
| 1056 | if (dhcpServerConnectPoint.get() != null && |
| 1057 | dhcpServerConnectPoint.get().deviceId(). |
| 1058 | equals(event.subject())) { |
| 1059 | log.trace("Mastership Event recevived for {}", event.subject()); |
| 1060 | // mastership of the device for our connect point has changed |
| 1061 | // reselect |
| 1062 | selectServerConnectPoint(); |
| 1063 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | } |
Deepa vaddireddy | 0060f53 | 2017-08-04 06:46:05 +0000 | [diff] [blame] | 1067 | |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1068 | private void removeAllocations(Predicate<Map.Entry<String, Versioned<DhcpAllocationInfo>>> pred) { |
| 1069 | allocations.stream() |
| 1070 | .filter(pred) |
| 1071 | .map(Map.Entry::getKey) |
| 1072 | .collect(Collectors.toList()) |
| 1073 | .forEach(allocations::remove); |
| 1074 | } |
| 1075 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 1076 | @Override |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1077 | public void clearAllocations() { |
| 1078 | allocations.clear(); |
| 1079 | } |
| 1080 | |
| 1081 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 1082 | @Override |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1083 | public boolean removeAllocationsByConnectPoint(ConnectPoint cp) { |
| 1084 | boolean removed = false; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1085 | for (String key : allocations.keySet()) { |
| 1086 | DhcpAllocationInfo entry = allocations.asJavaMap().get(key); |
| 1087 | if (entry.location().equals(cp)) { |
| 1088 | allocations.remove(key); |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1089 | removed = true; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1092 | return removed; |
Matteo Scandolo | ab34651 | 2020-04-17 13:39:55 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 1095 | |
| 1096 | /** |
| 1097 | * Checks for mastership or falls back to leadership on deviceId. |
| 1098 | * If the node is not master and device is available |
| 1099 | * or the device is not available and the leader is different |
| 1100 | * we let master or leader else handle it |
| 1101 | * Leadership on the device topic is needed because the master can be NONE |
| 1102 | * in case the device went away, we still need to handle events |
| 1103 | * consistently |
| 1104 | */ |
| 1105 | private boolean isLocalLeader(DeviceId deviceId) { |
| 1106 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 1107 | // When the device is available we just check the mastership |
| 1108 | if (deviceService.isAvailable(deviceId)) { |
| 1109 | return false; |
| 1110 | } |
| 1111 | // Fallback with Leadership service - device id is used as topic |
| 1112 | NodeId leader = leadershipService.runForLeadership( |
| 1113 | deviceId.toString()).leaderNodeId(); |
| 1114 | // Verify if this node is the leader |
| 1115 | return clusterService.getLocalNode().id().equals(leader); |
| 1116 | } |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1120 | /** |
| 1121 | * Handles Device status change for the devices which connect |
| 1122 | * to the DHCP server. |
| 1123 | */ |
| 1124 | private class InnerDeviceListener implements DeviceListener { |
| 1125 | @Override |
| 1126 | public void event(DeviceEvent event) { |
Andrea Campanella | 6f45a1b | 2020-05-08 17:50:12 +0200 | [diff] [blame] | 1127 | final DeviceId deviceId = event.subject().id(); |
| 1128 | |
| 1129 | // Ensure only one instance handles the event |
| 1130 | if (!isLocalLeader(deviceId)) { |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1131 | return; |
| 1132 | } |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 1133 | // ignore stats |
| 1134 | if (event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) { |
| 1135 | return; |
| 1136 | } |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1137 | |
Saurav Das | bd5ce9c | 2020-09-04 18:46:45 -0700 | [diff] [blame^] | 1138 | log.debug("Device Event received for {} event {}", event.subject(), |
| 1139 | event.type()); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1140 | |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1141 | switch (event.type()) { |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1142 | case DEVICE_REMOVED: |
| 1143 | log.info("Device removed {}", event.subject().id()); |
| 1144 | removeAllocations(e -> e.getValue().value().location().deviceId().equals(deviceId)); |
| 1145 | break; |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1146 | case DEVICE_AVAILABILITY_CHANGED: |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1147 | boolean available = deviceService.isAvailable(deviceId); |
| 1148 | log.info("Device Avail Changed {} to {}", event.subject().id(), available); |
| 1149 | |
| 1150 | if (!available && deviceService.getPorts(deviceId).isEmpty()) { |
| 1151 | removeAllocations(e -> e.getValue().value().location().deviceId().equals(deviceId)); |
| 1152 | log.info("Device {} is removed from DHCP allocationmap ", deviceId); |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1153 | } |
| 1154 | break; |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1155 | case PORT_REMOVED: |
| 1156 | Port port = event.port(); |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1157 | log.info("Port {} is deleted on device {}", port, deviceId); |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 1158 | |
| 1159 | ConnectPoint cp = new ConnectPoint(deviceId, port.number()); |
| 1160 | removeAllocations(e -> e.getValue().value().location().equals(cp)); |
| 1161 | |
Thomas Lee S | 6b77ad2 | 2020-01-10 11:27:43 +0530 | [diff] [blame] | 1162 | log.info("Port {} on device {} is removed from DHCP allocationmap", event.port(), deviceId); |
| 1163 | break; |
Thomas Lee S | 9df1508 | 2019-12-23 11:31:15 +0530 | [diff] [blame] | 1164 | default: |
| 1165 | break; |
| 1166 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1167 | if (!useOltUplink) { |
| 1168 | if (dhcpServerConnectPoint.get() == null) { |
| 1169 | switch (event.type()) { |
| 1170 | case DEVICE_ADDED: |
| 1171 | case DEVICE_AVAILABILITY_CHANGED: |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1172 | // some device is available check if we can get a |
| 1173 | // connect point we can use |
| 1174 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1175 | break; |
| 1176 | default: |
| 1177 | break; |
| 1178 | } |
| 1179 | return; |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1180 | } |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1181 | if (dhcpServerConnectPoint.get().deviceId(). |
| 1182 | equals(event.subject().id())) { |
| 1183 | switch (event.type()) { |
| 1184 | case DEVICE_AVAILABILITY_CHANGED: |
| 1185 | case DEVICE_REMOVED: |
| 1186 | case DEVICE_SUSPENDED: |
| 1187 | // state of our device has changed, check if we need |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1188 | // to re-select a connectpoint |
| 1189 | addOrRemoveDhcpTrapFromServer(true); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | default: |
| 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | } else { |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1196 | switch (event.type()) { |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1197 | case PORT_ADDED: |
Saurav Das | b4e3e10 | 2018-10-02 15:31:17 -0700 | [diff] [blame] | 1198 | if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) { |
Saurav Das | b14f08a | 2019-02-22 16:34:15 -0800 | [diff] [blame] | 1199 | requestDhcpPacketsFromConnectPoint( |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1200 | new ConnectPoint(event.subject().id(), event.port().number()), |
| 1201 | Optional.empty()); |
Amit Ghosh | 83c8c89 | 2017-11-09 11:08:27 +0000 | [diff] [blame] | 1202 | } |
Amit Ghosh | 8951f04 | 2017-08-10 13:48:10 +0100 | [diff] [blame] | 1203 | break; |
| 1204 | default: |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | } |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 1210 | |
| 1211 | private class InnerDhcpL2RelayStoreDelegate implements DhcpL2RelayStoreDelegate { |
| 1212 | @Override |
| 1213 | public void notify(DhcpL2RelayEvent event) { |
| 1214 | if (event.type().equals(DhcpL2RelayEvent.Type.STATS_UPDATE)) { |
| 1215 | DhcpL2RelayEvent toPost = event; |
| 1216 | if (event.getSubscriberId() != null) { |
| 1217 | // infuse the event with the allocation info before posting |
| 1218 | DhcpAllocationInfo info = Versioned.valueOrNull(allocations.get(event.getSubscriberId())); |
| 1219 | toPost = new DhcpL2RelayEvent(event.type(), info, event.connectPoint(), |
Matteo Scandolo | 64bba8c | 2020-08-19 11:50:33 -0700 | [diff] [blame] | 1220 | event.getCountersEntry(), event.getSubscriberId()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 1221 | } |
| 1222 | post(toPost); |
| 1223 | } |
| 1224 | |
| 1225 | } |
| 1226 | } |
Amit Ghosh | 47243cb | 2017-07-26 05:08:53 +0100 | [diff] [blame] | 1227 | } |