blob: e838a373e68b18cc91336d2bcf9fee5fd6325d66 [file] [log] [blame]
Amit Ghosh47243cb2017-07-26 05:08:53 +01001/*
Deepa vaddireddy0060f532017-08-04 06:46:05 +00002 * Copyright 2017-present Open Networking Foundation
Amit Ghosh47243cb2017-07-26 05:08:53 +01003 *
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 Scandolo57af5d12019-04-29 17:11:41 -070016package org.opencord.dhcpl2relay.impl;
Amit Ghosh47243cb2017-07-26 05:08:53 +010017
Saurav Das15626a02018-09-27 18:36:45 -070018import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
19import static org.onlab.packet.MacAddress.valueOf;
20import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
21
22import java.nio.ByteBuffer;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.Dictionary;
26import java.util.List;
27import java.util.Map;
28import java.util.Optional;
29import java.util.Set;
30import java.util.concurrent.atomic.AtomicReference;
31import java.util.stream.Collectors;
32
Amit Ghosh47243cb2017-07-26 05:08:53 +010033import org.apache.felix.scr.annotations.Activate;
34import org.apache.felix.scr.annotations.Component;
35import org.apache.felix.scr.annotations.Deactivate;
36import org.apache.felix.scr.annotations.Modified;
37import org.apache.felix.scr.annotations.Property;
38import org.apache.felix.scr.annotations.Reference;
39import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hartc36c9552018-07-31 15:07:53 -040040import org.apache.felix.scr.annotations.Service;
Amit Ghosh47243cb2017-07-26 05:08:53 +010041import org.onlab.packet.DHCP;
Amit Ghosh47243cb2017-07-26 05:08:53 +010042import org.onlab.packet.DHCPPacketType;
Deepa vaddireddy0060f532017-08-04 06:46:05 +000043import org.onlab.packet.Ethernet;
Amit Ghosh47243cb2017-07-26 05:08:53 +010044import org.onlab.packet.IPv4;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053045import org.onlab.packet.IpAddress;
Deepa vaddireddy0060f532017-08-04 06:46:05 +000046import org.onlab.packet.MacAddress;
Amit Ghosh47243cb2017-07-26 05:08:53 +010047import org.onlab.packet.TpPort;
48import org.onlab.packet.UDP;
49import org.onlab.packet.VlanId;
Jonathan Hartedbf6422018-05-02 17:30:05 -070050import org.onlab.packet.dhcp.DhcpOption;
Amit Ghosh47243cb2017-07-26 05:08:53 +010051import org.onlab.util.Tools;
52import org.onosproject.cfg.ComponentConfigService;
53import org.onosproject.core.ApplicationId;
54import org.onosproject.core.CoreService;
Jonathan Hartc36c9552018-07-31 15:07:53 -040055import org.onosproject.event.AbstractListenerManager;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053056import org.onosproject.mastership.MastershipEvent;
57import org.onosproject.mastership.MastershipListener;
58import org.onosproject.mastership.MastershipService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010059import org.onosproject.net.AnnotationKeys;
60import org.onosproject.net.ConnectPoint;
Amit Ghosh83c8c892017-11-09 11:08:27 +000061import org.onosproject.net.Device;
62import org.onosproject.net.DeviceId;
Amit Ghosh47243cb2017-07-26 05:08:53 +010063import org.onosproject.net.Host;
64import org.onosproject.net.Port;
Amit Ghosh83c8c892017-11-09 11:08:27 +000065import org.onosproject.net.PortNumber;
Amit Ghosh47243cb2017-07-26 05:08:53 +010066import org.onosproject.net.config.ConfigFactory;
67import org.onosproject.net.config.NetworkConfigEvent;
68import org.onosproject.net.config.NetworkConfigListener;
69import org.onosproject.net.config.NetworkConfigRegistry;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053070import org.onosproject.net.device.DeviceEvent;
71import org.onosproject.net.device.DeviceListener;
Amit Ghosh47243cb2017-07-26 05:08:53 +010072import org.onosproject.net.device.DeviceService;
73import org.onosproject.net.flow.DefaultTrafficSelector;
74import org.onosproject.net.flow.DefaultTrafficTreatment;
75import org.onosproject.net.flow.TrafficSelector;
76import org.onosproject.net.flow.TrafficTreatment;
Saurav Dasb4e3e102018-10-02 15:31:17 -070077import org.onosproject.net.flowobjective.FlowObjectiveService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010078import org.onosproject.net.host.HostService;
79import org.onosproject.net.packet.DefaultOutboundPacket;
80import org.onosproject.net.packet.OutboundPacket;
81import org.onosproject.net.packet.PacketContext;
82import org.onosproject.net.packet.PacketPriority;
83import org.onosproject.net.packet.PacketProcessor;
84import org.onosproject.net.packet.PacketService;
Matteo Scandolo57af5d12019-04-29 17:11:41 -070085import org.opencord.dhcpl2relay.DhcpAllocationInfo;
86import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
87import org.opencord.dhcpl2relay.DhcpL2RelayListener;
88import org.opencord.dhcpl2relay.DhcpL2RelayService;
89import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
Gamze Abakac806c6c2018-12-03 12:49:46 +000090import org.opencord.sadis.BaseInformationService;
91import org.opencord.sadis.SadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010092import org.opencord.sadis.SubscriberAndDeviceInformation;
Amit Ghosh47243cb2017-07-26 05:08:53 +010093import org.osgi.service.component.ComponentContext;
94import org.slf4j.Logger;
95import org.slf4j.LoggerFactory;
96
Saurav Das15626a02018-09-27 18:36:45 -070097import com.google.common.collect.ImmutableSet;
98import com.google.common.collect.Lists;
99import com.google.common.collect.Maps;
100import com.google.common.collect.Sets;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100101
102/**
103 * DHCP Relay Agent Application Component.
104 */
Jonathan Hartc36c9552018-07-31 15:07:53 -0400105@Service
Amit Ghosh47243cb2017-07-26 05:08:53 +0100106@Component(immediate = true)
Jonathan Hartc36c9552018-07-31 15:07:53 -0400107public class DhcpL2Relay
108 extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener>
109 implements DhcpL2RelayService {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100110
111 public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay";
Saurav Dasb4e3e102018-10-02 15:31:17 -0700112 private static final String HOST_LOC_PROVIDER =
113 "org.onosproject.provider.host.impl.HostLocationProvider";
Amit Ghosh47243cb2017-07-26 05:08:53 +0100114 private final Logger log = LoggerFactory.getLogger(getClass());
115 private final InternalConfigListener cfgListener =
116 new InternalConfigListener();
117
118 private final Set<ConfigFactory> factories = ImmutableSet.of(
119 new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY,
120 DhcpL2RelayConfig.class,
121 "dhcpl2relay") {
122 @Override
123 public DhcpL2RelayConfig createConfig() {
124 return new DhcpL2RelayConfig();
125 }
126 }
127 );
128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected NetworkConfigRegistry cfgService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected CoreService coreService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected PacketService packetService;
137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected HostService hostService;
140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
142 protected ComponentConfigService componentConfigService;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abakac806c6c2018-12-03 12:49:46 +0000145 protected SadisService sadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
148 protected DeviceService deviceService;
149
Amit Ghosh8951f042017-08-10 13:48:10 +0100150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
151 protected MastershipService mastershipService;
152
Saurav Dasb4e3e102018-10-02 15:31:17 -0700153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
154 protected FlowObjectiveService flowObjectiveService;
155
Amit Ghosh47243cb2017-07-26 05:08:53 +0100156 @Property(name = "option82", boolValue = true,
157 label = "Add option 82 to relayed packets")
158 protected boolean option82 = true;
159
Amit Ghosha17354e2017-08-23 12:56:04 +0100160 @Property(name = "enableDhcpBroadcastReplies", boolValue = false,
Amit Ghosh2095dc62017-09-25 20:56:55 +0100161 label = "Ask the DHCP Server to send back replies as L2 broadcast")
Amit Ghosha17354e2017-08-23 12:56:04 +0100162 protected boolean enableDhcpBroadcastReplies = false;
163
Amit Ghosh47243cb2017-07-26 05:08:53 +0100164 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor =
165 new DhcpRelayPacketProcessor();
166
Amit Ghosh8951f042017-08-10 13:48:10 +0100167 private InnerMastershipListener changeListener = new InnerMastershipListener();
168 private InnerDeviceListener deviceListener = new InnerDeviceListener();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100169
Amit Ghosh8951f042017-08-10 13:48:10 +0100170 // connect points to the DHCP server
171 Set<ConnectPoint> dhcpConnectPoints;
172 private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100173 private MacAddress dhcpConnectMac = MacAddress.BROADCAST;
174 private ApplicationId appId;
175
Amit Ghosha17354e2017-08-23 12:56:04 +0100176 static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap();
Amit Ghosh83c8c892017-11-09 11:08:27 +0000177 private boolean modifyClientPktsSrcDstMac = false;
178 //Whether to use the uplink port of the OLTs to send/receive messages to the DHCP server
179 private boolean useOltUplink = false;
Amit Ghosha17354e2017-08-23 12:56:04 +0100180
Gamze Abakac806c6c2018-12-03 12:49:46 +0000181 private BaseInformationService<SubscriberAndDeviceInformation> subsService;
182
Amit Ghosh47243cb2017-07-26 05:08:53 +0100183 @Activate
184 protected void activate(ComponentContext context) {
185 //start the dhcp relay agent
186 appId = coreService.registerApplication(DHCP_L2RELAY_APP);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700187 // ensure that host-learning via dhcp includes IP addresses
188 componentConfigService.preSetProperty(HOST_LOC_PROVIDER,
189 "useDhcp", Boolean.TRUE.toString());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100190 componentConfigService.registerProperties(getClass());
Jonathan Hartc36c9552018-07-31 15:07:53 -0400191 eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100192
193 cfgService.addListener(cfgListener);
Amit Ghosh8951f042017-08-10 13:48:10 +0100194 mastershipService.addListener(changeListener);
195 deviceService.addListener(deviceListener);
196
Matteo Scandolof8da6572019-09-30 09:30:32 -0700197 subsService = sadisService.getSubscriberInfoService();
198
Amit Ghosh47243cb2017-07-26 05:08:53 +0100199 factories.forEach(cfgService::registerConfigFactory);
200 //update the dhcp server configuration.
201 updateConfig();
202 //add the packet services.
203 packetService.addProcessor(dhcpRelayPacketProcessor,
204 PacketProcessor.director(0));
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000205 if (context != null) {
206 modified(context);
207 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100208
Gamze Abakac806c6c2018-12-03 12:49:46 +0000209
Amit Ghosh47243cb2017-07-26 05:08:53 +0100210 log.info("DHCP-L2-RELAY Started");
211 }
212
213 @Deactivate
214 protected void deactivate() {
215 cfgService.removeListener(cfgListener);
216 factories.forEach(cfgService::unregisterConfigFactory);
217 packetService.removeProcessor(dhcpRelayPacketProcessor);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700218 cancelDhcpPktsFromServer();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100219
220 componentConfigService.unregisterProperties(getClass(), false);
Deepa Vaddireddy77a6ac72017-09-20 20:36:52 +0530221 deviceService.removeListener(deviceListener);
222 mastershipService.removeListener(changeListener);
Jonathan Hartc36c9552018-07-31 15:07:53 -0400223 eventDispatcher.removeSink(DhcpL2RelayEvent.class);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100224 log.info("DHCP-L2-RELAY Stopped");
225 }
226
227 @Modified
228 protected void modified(ComponentContext context) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000229
Amit Ghosh47243cb2017-07-26 05:08:53 +0100230 Dictionary<?, ?> properties = context.getProperties();
231
232 Boolean o = Tools.isPropertyEnabled(properties, "option82");
233 if (o != null) {
234 option82 = o;
235 }
Amit Ghosh2095dc62017-09-25 20:56:55 +0100236
237 o = Tools.isPropertyEnabled(properties, "enableDhcpBroadcastReplies");
238 if (o != null) {
239 enableDhcpBroadcastReplies = o;
240 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100241 }
242
243 /**
244 * Checks if this app has been configured.
245 *
246 * @return true if all information we need have been initialized
247 */
248 private boolean configured() {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000249 if (!useOltUplink) {
250 return dhcpServerConnectPoint.get() != null;
251 }
252 return true;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100253 }
254
Amit Ghosh8951f042017-08-10 13:48:10 +0100255 /**
256 * Selects a connect point through an available device for which it is the master.
257 */
258 private void selectServerConnectPoint() {
259 synchronized (this) {
260 dhcpServerConnectPoint.set(null);
261 if (dhcpConnectPoints != null) {
262 // find a connect point through a device for which we are master
263 for (ConnectPoint cp: dhcpConnectPoints) {
264 if (mastershipService.isLocalMaster(cp.deviceId())) {
265 if (deviceService.isAvailable(cp.deviceId())) {
266 dhcpServerConnectPoint.set(cp);
267 }
268 log.info("DHCP connectPoint selected is {}", cp);
269 break;
270 }
271 }
272 }
273
274 log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get());
275
276 if (dhcpServerConnectPoint.get() == null) {
277 log.error("Master of none, can't relay DHCP Message to server");
278 }
279 }
280 }
281
282 /**
283 * Updates the network configuration.
284 */
Amit Ghosh47243cb2017-07-26 05:08:53 +0100285 private void updateConfig() {
286 DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class);
287 if (cfg == null) {
288 log.warn("Dhcp Server info not available");
289 return;
290 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100291
292 dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint());
Amit Ghosh83c8c892017-11-09 11:08:27 +0000293 modifyClientPktsSrcDstMac = cfg.getModifySrcDstMacAddresses();
Saurav Dasb4e3e102018-10-02 15:31:17 -0700294 boolean prevUseOltUplink = useOltUplink;
Amit Ghosh83c8c892017-11-09 11:08:27 +0000295 useOltUplink = cfg.getUseOltUplinkForServerPktInOut();
Amit Ghosh8951f042017-08-10 13:48:10 +0100296
Saurav Dasb4e3e102018-10-02 15:31:17 -0700297 if (useOltUplink) {
298 for (ConnectPoint cp : getUplinkPortsOfOlts()) {
299 log.debug("requestDhcpPackets: ConnectPoint: {}", cp);
Matteo Scandolof8da6572019-09-30 09:30:32 -0700300 requestDhcpPacketsFromConnectPoint(cp, Optional.ofNullable(null));
Saurav Dasb4e3e102018-10-02 15:31:17 -0700301 }
302 // check if previous config was different and so trap flows may
Saurav Dasb14f08a2019-02-22 16:34:15 -0800303 // need to be removed from other places like AGG switches
Saurav Dasb4e3e102018-10-02 15:31:17 -0700304 if (!prevUseOltUplink) {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800305 addOrRemoveDhcpTrapFromServer(false);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700306 }
Saurav Dasb4e3e102018-10-02 15:31:17 -0700307 } else {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800308 // uplink on AGG switch
309 addOrRemoveDhcpTrapFromServer(true);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700310 }
311 }
312
313 private void cancelDhcpPktsFromServer() {
314 if (useOltUplink) {
315 for (ConnectPoint cp : getUplinkPortsOfOlts()) {
316 log.debug("cancelDhcpPackets: ConnectPoint: {}", cp);
Matteo Scandolof8da6572019-09-30 09:30:32 -0700317 cancelDhcpPacketsFromConnectPoint(cp, Optional.ofNullable(null));
Saurav Dasb4e3e102018-10-02 15:31:17 -0700318 }
319 } else {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800320 // uplink on AGG switch
321 addOrRemoveDhcpTrapFromServer(false);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000322 }
Saurav Dasb4e3e102018-10-02 15:31:17 -0700323 }
324
Saurav Dasb14f08a2019-02-22 16:34:15 -0800325 /**
326 * Used to add or remove DHCP trap flow for packets received from DHCP server.
327 * Typically used on a non OLT device, like an AGG switch. When adding, a
328 * new dhcp server connect point is selected from the configured options.
329 *
330 * @param add true if dhcp trap flow is to be added, false to remove the
331 * trap flow
332 */
333 private void addOrRemoveDhcpTrapFromServer(boolean add) {
334 if (add) {
335 selectServerConnectPoint();
336 log.debug("dhcp server connect point: " + dhcpServerConnectPoint);
337 }
338 if (dhcpServerConnectPoint.get() == null) {
339 log.warn("No dhcpServer connectPoint found, cannot {} dhcp trap flows",
340 (add) ? "install" : "remove");
341 return;
342 }
343 if (add) {
344 log.info("Adding trap to dhcp server connect point: "
345 + dhcpServerConnectPoint);
346 requestDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
347 Optional.of(PacketPriority.HIGH1));
348 } else {
349 log.info("Removing trap from dhcp server connect point: "
350 + dhcpServerConnectPoint);
351 cancelDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
352 Optional.of(PacketPriority.HIGH1));
353 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100354 }
355
356 /**
Amit Ghosh83c8c892017-11-09 11:08:27 +0000357 * Returns all the uplink ports of OLTs configured in SADIS.
358 * Only ports visible in ONOS and for which this instance is master
359 * are returned
360 */
361 private List<ConnectPoint> getUplinkPortsOfOlts() {
362 List<ConnectPoint> cps = new ArrayList<>();
363
364 // find all the olt devices and if their uplink ports are visible
365 Iterable<Device> devices = deviceService.getDevices();
366 for (Device d : devices) {
367 // check if this device is provisioned in Sadis
368
369 log.debug("getUplinkPortsOfOlts: Checking mastership of {}", d);
370 // do only for devices for which we are the master
371 if (!mastershipService.isLocalMaster(d.id())) {
372 continue;
373 }
374
375 String devSerialNo = d.serialNumber();
376 SubscriberAndDeviceInformation deviceInfo = subsService.get(devSerialNo);
377 log.debug("getUplinkPortsOfOlts: Found device: {}", deviceInfo);
378 if (deviceInfo != null) {
379 // check if the uplink port with that number is available on the device
380 PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort());
381 Port port = deviceService.getPort(d.id(), pNum);
382 log.debug("getUplinkPortsOfOlts: Found port: {}", port);
383 if (port != null) {
384 cps.add(new ConnectPoint(d.id(), pNum));
385 }
386 }
387 }
388 return cps;
389 }
390
391 /**
392 * Returns whether the passed port is the uplink port of the olt device.
393 */
394 private boolean isUplinkPortOfOlt(DeviceId dId, Port p) {
395 log.debug("isUplinkPortOfOlt: DeviceId: {} Port: {}", dId, p);
396 // do only for devices for which we are the master
397 if (!mastershipService.isLocalMaster(dId)) {
398 return false;
399 }
400
401 Device d = deviceService.getDevice(dId);
402 SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber());
403
404 if (deviceInfo != null) {
405 return (deviceInfo.uplinkPort() == p.number().toLong());
406 }
407
408 return false;
409 }
410
411 /**
412 * Returns the connectPoint which is the uplink port of the OLT.
413 */
414 private ConnectPoint getUplinkConnectPointOfOlt(DeviceId dId) {
415
416 Device d = deviceService.getDevice(dId);
417 SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber());
418 log.debug("getUplinkConnectPointOfOlt DeviceId: {} devInfo: {}", dId, deviceInfo);
419 if (deviceInfo != null) {
420 PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort());
421 Port port = deviceService.getPort(d.id(), pNum);
422 if (port != null) {
423 return new ConnectPoint(d.id(), pNum);
424 }
425 }
426
427 return null;
428 }
429
430 /**
431 * Request DHCP packet from particular connect point via PacketService.
Saurav Dasb14f08a2019-02-22 16:34:15 -0800432 * Optionally provide a priority for the trap flow. If no such priority is
433 * provided, the default priority will be used.
434 *
435 * @param cp the connect point to trap dhcp packets from
436 * @param priority of the trap flow, null to use default priority
Amit Ghosh83c8c892017-11-09 11:08:27 +0000437 */
Saurav Dasb14f08a2019-02-22 16:34:15 -0800438 private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp,
439 Optional<PacketPriority> priority) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000440 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
441 .matchEthType(Ethernet.TYPE_IPV4)
442 .matchInPort(cp.port())
443 .matchIPProtocol(IPv4.PROTOCOL_UDP)
444 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
445 packetService.requestPackets(selectorServer.build(),
Saurav Dasb14f08a2019-02-22 16:34:15 -0800446 priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
447 appId, Optional.of(cp.deviceId()));
Amit Ghosh83c8c892017-11-09 11:08:27 +0000448 }
449
450 /**
Saurav Dasb14f08a2019-02-22 16:34:15 -0800451 * Cancel DHCP packet from particular connect point via PacketService. If
452 * the request was made with a specific packet priority, then the same
453 * priority should be used in this call.
454 *
455 * @param cp the connect point for the trap flow
456 * @param priority with which the trap flow was requested; if request
457 * priority was not specified, this param should also be null
Amit Ghosh83c8c892017-11-09 11:08:27 +0000458 */
Saurav Dasb14f08a2019-02-22 16:34:15 -0800459 private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp,
460 Optional<PacketPriority> priority) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000461 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
462 .matchEthType(Ethernet.TYPE_IPV4)
463 .matchInPort(cp.port())
464 .matchIPProtocol(IPv4.PROTOCOL_UDP)
465 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
466 packetService.cancelPackets(selectorServer.build(),
Saurav Dasb14f08a2019-02-22 16:34:15 -0800467 priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
468 appId, Optional.of(cp.deviceId()));
Amit Ghosh83c8c892017-11-09 11:08:27 +0000469 }
470
Amit Ghosha17354e2017-08-23 12:56:04 +0100471 public static Map<String, DhcpAllocationInfo> allocationMap() {
472 return allocationMap;
473 }
474
Amit Ghosh47243cb2017-07-26 05:08:53 +0100475 private SubscriberAndDeviceInformation getDevice(PacketContext context) {
476 String serialNo = deviceService.getDevice(context.inPacket().
477 receivedFrom().deviceId()).serialNumber();
478
479 return subsService.get(serialNo);
480 }
481
482 private SubscriberAndDeviceInformation getDevice(ConnectPoint cp) {
483 String serialNo = deviceService.getDevice(cp.deviceId()).
484 serialNumber();
485
486 return subsService.get(serialNo);
487 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100488
489 private MacAddress relayAgentMacAddress(PacketContext context) {
490
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000491 SubscriberAndDeviceInformation device = this.getDevice(context);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100492 if (device == null) {
493 log.warn("Device not found for {}", context.inPacket().
494 receivedFrom());
495 return null;
496 }
497
498 return device.hardwareIdentifier();
499 }
500
501 private String nasPortId(PacketContext context) {
Amit Ghosh8951f042017-08-10 13:48:10 +0100502 return nasPortId(context.inPacket().receivedFrom());
503 }
504
505 private String nasPortId(ConnectPoint cp) {
506 Port p = deviceService.getPort(cp);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100507 return p.annotations().value(AnnotationKeys.PORT_NAME);
508 }
509
510 private SubscriberAndDeviceInformation getSubscriber(PacketContext context) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100511 return subsService.get(nasPortId(context));
512 }
513
514 private VlanId cTag(PacketContext context) {
515 SubscriberAndDeviceInformation sub = getSubscriber(context);
516 if (sub == null) {
517 log.warn("Subscriber info not found for {}", context.inPacket().
518 receivedFrom());
519 return VlanId.NONE;
520 }
521 return sub.cTag();
522 }
523
Amit Ghosh8951f042017-08-10 13:48:10 +0100524 private VlanId cTag(ConnectPoint cp) {
525 String portId = nasPortId(cp);
526 SubscriberAndDeviceInformation sub = subsService.get(portId);
527 if (sub == null) {
528 log.warn("Subscriber info not found for {} looking for C-TAG", cp);
529 return VlanId.NONE;
530 }
531 return sub.cTag();
532 }
533
534 private VlanId sTag(ConnectPoint cp) {
535 String portId = nasPortId(cp);
536 SubscriberAndDeviceInformation sub = subsService.get(portId);
537 if (sub == null) {
538 log.warn("Subscriber info not found for {} looking for S-TAG", cp);
539 return VlanId.NONE;
540 }
541 return sub.sTag();
542 }
543
Amit Ghosh47243cb2017-07-26 05:08:53 +0100544 private VlanId sTag(PacketContext context) {
545 SubscriberAndDeviceInformation sub = getSubscriber(context);
546 if (sub == null) {
547 log.warn("Subscriber info not found for {}", context.inPacket().
548 receivedFrom());
549 return VlanId.NONE;
550 }
551 return sub.sTag();
552 }
553
554 private class DhcpRelayPacketProcessor implements PacketProcessor {
555
556 @Override
557 public void process(PacketContext context) {
558 if (!configured()) {
559 log.warn("Missing DHCP relay config. Abort packet processing");
560 return;
561 }
562
563 // process the packet and get the payload
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530564 Ethernet packet = context.inPacket().parsed();
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000565
Amit Ghosh47243cb2017-07-26 05:08:53 +0100566 if (packet == null) {
567 log.warn("Packet is null");
568 return;
569 }
570
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530571 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100572 IPv4 ipv4Packet = (IPv4) packet.getPayload();
573
574 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
575 UDP udpPacket = (UDP) ipv4Packet.getPayload();
576 if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT ||
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000577 udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100578 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
579 //This packet is dhcp.
580 processDhcpPacket(context, packet, dhcpPayload);
581 }
582 }
583 }
584 }
585
586 //forward the packet to ConnectPoint where the DHCP server is attached.
Amit Ghosh83c8c892017-11-09 11:08:27 +0000587 private void forwardPacket(Ethernet packet, PacketContext context) {
588 ConnectPoint toSendTo = null;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100589
Amit Ghosh83c8c892017-11-09 11:08:27 +0000590 if (!useOltUplink) {
591 toSendTo = dhcpServerConnectPoint.get();
592 } else {
593 toSendTo = getUplinkConnectPointOfOlt(context.inPacket().
594 receivedFrom().deviceId());
595 }
596
597 if (toSendTo != null) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100598 TrafficTreatment t = DefaultTrafficTreatment.builder()
Amit Ghosh83c8c892017-11-09 11:08:27 +0000599 .setOutput(toSendTo.port()).build();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100600 OutboundPacket o = new DefaultOutboundPacket(
Amit Ghosh83c8c892017-11-09 11:08:27 +0000601 toSendTo.deviceId(), t,
Amit Ghosh47243cb2017-07-26 05:08:53 +0100602 ByteBuffer.wrap(packet.serialize()));
603 if (log.isTraceEnabled()) {
Saurav Das15626a02018-09-27 18:36:45 -0700604 log.trace("Relaying packet to dhcp server at {} {}",
605 toSendTo, packet);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100606 }
607 packetService.emit(o);
608 } else {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000609 log.error("No connect point to send msg to DHCP Server");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100610 }
611 }
612
Amit Ghosha17354e2017-08-23 12:56:04 +0100613 // get the type of the DHCP packet
614 private DHCPPacketType getDhcpPacketType(DHCP dhcpPayload) {
615
Jonathan Hartedbf6422018-05-02 17:30:05 -0700616 for (DhcpOption option : dhcpPayload.getOptions()) {
Amit Ghosha17354e2017-08-23 12:56:04 +0100617 if (option.getCode() == OptionCode_MessageType.getValue()) {
618 byte[] data = option.getData();
619 return DHCPPacketType.getType(data[0]);
620 }
621 }
622 return null;
623 }
624
Amit Ghosh47243cb2017-07-26 05:08:53 +0100625 //process the dhcp packet before sending to server
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530626 private void processDhcpPacket(PacketContext context, Ethernet packet,
Amit Ghosh47243cb2017-07-26 05:08:53 +0100627 DHCP dhcpPayload) {
628 if (dhcpPayload == null) {
629 log.warn("DHCP payload is null");
630 return;
631 }
632
Amit Ghosha17354e2017-08-23 12:56:04 +0100633 DHCPPacketType incomingPacketType = getDhcpPacketType(dhcpPayload);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000634
Saurav Das15626a02018-09-27 18:36:45 -0700635 log.info("Received DHCP Packet of type {} from {}",
636 incomingPacketType, context.inPacket().receivedFrom());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100637
638 switch (incomingPacketType) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000639 case DHCPDISCOVER:
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530640 Ethernet ethernetPacketDiscover =
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000641 processDhcpPacketFromClient(context, packet);
642 if (ethernetPacketDiscover != null) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000643 forwardPacket(ethernetPacketDiscover, context);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000644 }
645 break;
646 case DHCPOFFER:
647 //reply to dhcp client.
Saurav Das15626a02018-09-27 18:36:45 -0700648 Ethernet ethernetPacketOffer =
649 processDhcpPacketFromServer(context, packet);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000650 if (ethernetPacketOffer != null) {
651 sendReply(ethernetPacketOffer, dhcpPayload);
652 }
653 break;
654 case DHCPREQUEST:
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530655 Ethernet ethernetPacketRequest =
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000656 processDhcpPacketFromClient(context, packet);
657 if (ethernetPacketRequest != null) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000658 forwardPacket(ethernetPacketRequest, context);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000659 }
660 break;
661 case DHCPACK:
662 //reply to dhcp client.
Saurav Das15626a02018-09-27 18:36:45 -0700663 Ethernet ethernetPacketAck =
664 processDhcpPacketFromServer(context, packet);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000665 if (ethernetPacketAck != null) {
666 sendReply(ethernetPacketAck, dhcpPayload);
667 }
668 break;
669 default:
670 break;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100671 }
672 }
673
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530674 private Ethernet processDhcpPacketFromClient(PacketContext context,
675 Ethernet ethernetPacket) {
Saurav Das15626a02018-09-27 18:36:45 -0700676 if (log.isTraceEnabled()) {
677 log.trace("DHCP packet received from client at {} {}",
678 context.inPacket().receivedFrom(), ethernetPacket);
679 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100680
681 MacAddress relayAgentMac = relayAgentMacAddress(context);
682 if (relayAgentMac == null) {
683 log.warn("RelayAgent MAC not found ");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100684 return null;
685 }
686
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530687 Ethernet etherReply = ethernetPacket;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100688
689 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
690 UDP udpPacket = (UDP) ipv4Packet.getPayload();
691 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
692
Amit Ghosha17354e2017-08-23 12:56:04 +0100693 if (enableDhcpBroadcastReplies) {
694 // We want the reply to come back as a L2 broadcast
695 dhcpPacket.setFlags((short) 0x8000);
696 }
697
Jonathan Hartc36c9552018-07-31 15:07:53 -0400698 MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress());
699 IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100700
Jonathan Hartc36c9552018-07-31 15:07:53 -0400701 SubscriberAndDeviceInformation entry = getSubscriber(context);
702 if (entry == null) {
Saurav Das15626a02018-09-27 18:36:45 -0700703 log.warn("Dropping packet as subscriber entry is not available");
Jonathan Hartc36c9552018-07-31 15:07:53 -0400704 return null;
705 }
706
707 DhcpAllocationInfo info = new DhcpAllocationInfo(
708 context.inPacket().receivedFrom(), dhcpPacket.getPacketType(),
709 entry.nasPortId(), clientMac, clientIp);
710
711 allocationMap.put(entry.nasPortId(), info);
712
Saurav Das15626a02018-09-27 18:36:45 -0700713 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info,
714 context.inPacket().receivedFrom()));
Amit Ghosha17354e2017-08-23 12:56:04 +0100715
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000716 if (option82) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000717 DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry);
718 udpPacket.setPayload(dhcpPacketWithOption82);
719 }
720
721 ipv4Packet.setPayload(udpPacket);
722 etherReply.setPayload(ipv4Packet);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000723 if (modifyClientPktsSrcDstMac) {
724 etherReply.setSourceMACAddress(relayAgentMac);
725 etherReply.setDestinationMACAddress(dhcpConnectMac);
726 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100727
Amit Ghosh8951f042017-08-10 13:48:10 +0100728 etherReply.setPriorityCode(ethernetPacket.getPriorityCode());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100729 etherReply.setVlanID(cTag(context).toShort());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530730 etherReply.setQinQTPID(Ethernet.TYPE_VLAN);
731 etherReply.setQinQVID(sTag(context).toShort());
Saurav Das15626a02018-09-27 18:36:45 -0700732 log.info("Finished processing packet.. relaying to dhcpServer");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100733 return etherReply;
734 }
735
736 //build the DHCP offer/ack with proper client port.
Saurav Das15626a02018-09-27 18:36:45 -0700737 private Ethernet processDhcpPacketFromServer(PacketContext context,
738 Ethernet ethernetPacket) {
739 if (log.isTraceEnabled()) {
740 log.trace("DHCP packet received from server at {} {}",
741 context.inPacket().receivedFrom(), ethernetPacket);
742 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100743 // get dhcp header.
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530744 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100745 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
746 UDP udpPacket = (UDP) ipv4Packet.getPayload();
747 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
748
Amit Ghosh47243cb2017-07-26 05:08:53 +0100749 MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100750 ConnectPoint subsCp = getConnectPointOfClient(dstMac);
Amit Ghosh2095dc62017-09-25 20:56:55 +0100751 // If we can't find the subscriber, can't process further
752 if (subsCp == null) {
753 return null;
754 }
Amit Ghosha17354e2017-08-23 12:56:04 +0100755 // if it's an ACK packet store the information for display purpose
756 if (getDhcpPacketType(dhcpPayload) == DHCPPacketType.DHCPACK) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100757
Amit Ghosha17354e2017-08-23 12:56:04 +0100758 String portId = nasPortId(subsCp);
759 SubscriberAndDeviceInformation sub = subsService.get(portId);
760 if (sub != null) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700761 List<DhcpOption> options = dhcpPayload.getOptions();
762 List<DhcpOption> circuitIds = options.stream()
Amit Ghosha17354e2017-08-23 12:56:04 +0100763 .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
764 .collect(Collectors.toList());
765
766 String circuitId = "None";
767 if (circuitIds.size() == 1) {
Amit Ghosh2095dc62017-09-25 20:56:55 +0100768 byte[] array = circuitIds.get(0).getData();
769
770 try {
771 // we leave the first two bytes as they are the id and length
772 circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8");
773 } catch (Exception e) { }
Amit Ghosha17354e2017-08-23 12:56:04 +0100774 }
775
776 IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress());
777
778 //storeDHCPAllocationInfo
Jonathan Hartc36c9552018-07-31 15:07:53 -0400779 DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp,
780 dhcpPayload.getPacketType(), circuitId, dstMac, ip);
Amit Ghosha17354e2017-08-23 12:56:04 +0100781
782 allocationMap.put(sub.nasPortId(), info);
Jonathan Hartc36c9552018-07-31 15:07:53 -0400783
784 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp));
Amit Ghosha17354e2017-08-23 12:56:04 +0100785 }
786 } // end storing of info
Amit Ghosh47243cb2017-07-26 05:08:53 +0100787
788 // we leave the srcMac from the original packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530789 etherReply.setDestinationMACAddress(dstMac);
790 etherReply.setQinQVID(sTag(subsCp).toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100791 etherReply.setPriorityCode(ethernetPacket.getPriorityCode());
792 etherReply.setVlanID((cTag(subsCp).toShort()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100793
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000794 if (option82) {
795 udpPacket.setPayload(removeOption82(dhcpPayload));
796 } else {
797 udpPacket.setPayload(dhcpPayload);
798 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100799 ipv4Packet.setPayload(udpPacket);
800 etherReply.setPayload(ipv4Packet);
801
Saurav Das15626a02018-09-27 18:36:45 -0700802 log.info("Finished processing packet.. relaying to client");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100803 return etherReply;
804 }
805
Amit Ghosha17354e2017-08-23 12:56:04 +0100806 /*
807 * Get ConnectPoint of the Client based on it's MAC address
808 */
809 private ConnectPoint getConnectPointOfClient(MacAddress dstMac) {
810 Set<Host> hosts = hostService.getHostsByMac(dstMac);
811 if (hosts == null || hosts.isEmpty()) {
812 log.warn("Cannot determine host for DHCP client: {}. Aborting "
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530813 + "relay for dhcp packet from server",
Amit Ghosha17354e2017-08-23 12:56:04 +0100814 dstMac);
815 return null;
816 }
817 for (Host h : hosts) {
818 // if more than one,
819 // find the connect point which has an valid entry in SADIS
820 ConnectPoint cp = new ConnectPoint(h.location().deviceId(),
821 h.location().port());
822
823 if (sTag(cp) != VlanId.NONE) {
824 return cp;
825 }
826 }
827
828 return null;
829 }
830
Amit Ghosh47243cb2017-07-26 05:08:53 +0100831 //send the response to the requester host.
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530832 private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100833 MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100834 ConnectPoint subCp = getConnectPointOfClient(descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100835
836 // Send packet out to requester if the host information is available
Amit Ghosha17354e2017-08-23 12:56:04 +0100837 if (subCp != null) {
Saurav Das15626a02018-09-27 18:36:45 -0700838 log.info("Sending DHCP packet to client at {}", subCp);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100839 TrafficTreatment t = DefaultTrafficTreatment.builder()
Amit Ghosha17354e2017-08-23 12:56:04 +0100840 .setOutput(subCp.port()).build();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100841 OutboundPacket o = new DefaultOutboundPacket(
Amit Ghosha17354e2017-08-23 12:56:04 +0100842 subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100843 if (log.isTraceEnabled()) {
Saurav Das15626a02018-09-27 18:36:45 -0700844 log.trace("Relaying packet to dhcp client at {} {}", subCp,
845 ethPacket);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100846 }
847 packetService.emit(o);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100848 } else {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000849 log.error("Dropping DHCP packet because can't find host for {}", descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100850 }
851 }
852 }
853
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000854 private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) {
855 log.debug("option82data {} ", entry);
856
Jonathan Hartedbf6422018-05-02 17:30:05 -0700857 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000858 DhcpOption82 option82 = new DhcpOption82();
859 option82.setAgentCircuitId(entry.circuitId());
860 option82.setAgentRemoteId(entry.remoteId());
Jonathan Hartedbf6422018-05-02 17:30:05 -0700861 DhcpOption option = new DhcpOption()
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530862 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
863 .setData(option82.toByteArray())
864 .setLength(option82.length());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000865
866 options.add(options.size() - 1, option);
867 dhcpPacket.setOptions(options);
Amit Ghosh8951f042017-08-10 13:48:10 +0100868
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000869 return dhcpPacket;
870
871 }
872
873 private DHCP removeOption82(DHCP dhcpPacket) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700874 List<DhcpOption> options = dhcpPacket.getOptions();
875 List<DhcpOption> newoptions = options.stream()
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000876 .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
877 .collect(Collectors.toList());
878
879 return dhcpPacket.setOptions(newoptions);
880 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100881 /**
882 * Listener for network config events.
883 */
884 private class InternalConfigListener implements NetworkConfigListener {
885
886 @Override
887 public void event(NetworkConfigEvent event) {
888
889 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
890 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
891 event.configClass().equals(DhcpL2RelayConfig.class)) {
892 updateConfig();
893 log.info("Reconfigured");
894 }
895 }
896 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000897
Amit Ghosh8951f042017-08-10 13:48:10 +0100898 /**
899 * Handles Mastership changes for the devices which connect
900 * to the DHCP server.
901 */
902 private class InnerMastershipListener implements MastershipListener {
903 @Override
904 public void event(MastershipEvent event) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000905 if (!useOltUplink) {
906 if (dhcpServerConnectPoint.get() != null &&
907 dhcpServerConnectPoint.get().deviceId().
908 equals(event.subject())) {
909 log.trace("Mastership Event recevived for {}", event.subject());
910 // mastership of the device for our connect point has changed
911 // reselect
912 selectServerConnectPoint();
913 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100914 }
915 }
916 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000917
Amit Ghosh8951f042017-08-10 13:48:10 +0100918 /**
919 * Handles Device status change for the devices which connect
920 * to the DHCP server.
921 */
922 private class InnerDeviceListener implements DeviceListener {
923 @Override
924 public void event(DeviceEvent event) {
Saurav Das15626a02018-09-27 18:36:45 -0700925 if (log.isTraceEnabled() &&
926 !event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) {
927 log.trace("Device Event received for {} event {}",
928 event.subject(), event.type());
929 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000930 if (!useOltUplink) {
931 if (dhcpServerConnectPoint.get() == null) {
932 switch (event.type()) {
933 case DEVICE_ADDED:
934 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasb14f08a2019-02-22 16:34:15 -0800935 // some device is available check if we can get a
936 // connect point we can use
937 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000938 break;
939 default:
940 break;
941 }
942 return;
Amit Ghosh8951f042017-08-10 13:48:10 +0100943 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000944 if (dhcpServerConnectPoint.get().deviceId().
945 equals(event.subject().id())) {
946 switch (event.type()) {
947 case DEVICE_AVAILABILITY_CHANGED:
948 case DEVICE_REMOVED:
949 case DEVICE_SUSPENDED:
950 // state of our device has changed, check if we need
Saurav Dasb14f08a2019-02-22 16:34:15 -0800951 // to re-select a connectpoint
952 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000953 break;
954 default:
955 break;
956 }
957 }
958 } else {
Amit Ghosh8951f042017-08-10 13:48:10 +0100959 switch (event.type()) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000960 case PORT_ADDED:
Saurav Dasb4e3e102018-10-02 15:31:17 -0700961 if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800962 requestDhcpPacketsFromConnectPoint(
963 new ConnectPoint(event.subject().id(), event.port().number()),
964 null);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000965 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100966 break;
967 default:
968 break;
969 }
970 }
971 }
972 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100973}