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