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