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