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