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