blob: 7917561eabb23d963ee33dc403c16a3507d8060c [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 */
16package org.opencord.dhcpl2relay;
17
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;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053085import org.opencord.dhcpl2relay.packet.DhcpOption82;
Gamze Abakac806c6c2018-12-03 12:49:46 +000086import org.opencord.sadis.BaseInformationService;
87import org.opencord.sadis.SadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010088import org.opencord.sadis.SubscriberAndDeviceInformation;
Amit Ghosh47243cb2017-07-26 05:08:53 +010089import org.osgi.service.component.ComponentContext;
90import org.slf4j.Logger;
91import org.slf4j.LoggerFactory;
92
Saurav Das15626a02018-09-27 18:36:45 -070093import com.google.common.collect.ImmutableSet;
94import com.google.common.collect.Lists;
95import com.google.common.collect.Maps;
96import com.google.common.collect.Sets;
Amit Ghosh47243cb2017-07-26 05:08:53 +010097
98/**
99 * DHCP Relay Agent Application Component.
100 */
Jonathan Hartc36c9552018-07-31 15:07:53 -0400101@Service
Amit Ghosh47243cb2017-07-26 05:08:53 +0100102@Component(immediate = true)
Jonathan Hartc36c9552018-07-31 15:07:53 -0400103public class DhcpL2Relay
104 extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener>
105 implements DhcpL2RelayService {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100106
107 public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay";
Saurav Dasb4e3e102018-10-02 15:31:17 -0700108 private static final String HOST_LOC_PROVIDER =
109 "org.onosproject.provider.host.impl.HostLocationProvider";
Amit Ghosh47243cb2017-07-26 05:08:53 +0100110 private final Logger log = LoggerFactory.getLogger(getClass());
111 private final InternalConfigListener cfgListener =
112 new InternalConfigListener();
113
114 private final Set<ConfigFactory> factories = ImmutableSet.of(
115 new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY,
116 DhcpL2RelayConfig.class,
117 "dhcpl2relay") {
118 @Override
119 public DhcpL2RelayConfig createConfig() {
120 return new DhcpL2RelayConfig();
121 }
122 }
123 );
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected NetworkConfigRegistry cfgService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected CoreService coreService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected PacketService packetService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected HostService hostService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected ComponentConfigService componentConfigService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Gamze Abakac806c6c2018-12-03 12:49:46 +0000141 protected SadisService sadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected DeviceService deviceService;
145
Amit Ghosh8951f042017-08-10 13:48:10 +0100146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected MastershipService mastershipService;
148
Saurav Dasb4e3e102018-10-02 15:31:17 -0700149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
150 protected FlowObjectiveService flowObjectiveService;
151
Amit Ghosh47243cb2017-07-26 05:08:53 +0100152 @Property(name = "option82", boolValue = true,
153 label = "Add option 82 to relayed packets")
154 protected boolean option82 = true;
155
Amit Ghosha17354e2017-08-23 12:56:04 +0100156 @Property(name = "enableDhcpBroadcastReplies", boolValue = false,
Amit Ghosh2095dc62017-09-25 20:56:55 +0100157 label = "Ask the DHCP Server to send back replies as L2 broadcast")
Amit Ghosha17354e2017-08-23 12:56:04 +0100158 protected boolean enableDhcpBroadcastReplies = false;
159
Amit Ghosh47243cb2017-07-26 05:08:53 +0100160 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor =
161 new DhcpRelayPacketProcessor();
162
Amit Ghosh8951f042017-08-10 13:48:10 +0100163 private InnerMastershipListener changeListener = new InnerMastershipListener();
164 private InnerDeviceListener deviceListener = new InnerDeviceListener();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100165
Amit Ghosh8951f042017-08-10 13:48:10 +0100166 // connect points to the DHCP server
167 Set<ConnectPoint> dhcpConnectPoints;
168 private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100169 private MacAddress dhcpConnectMac = MacAddress.BROADCAST;
170 private ApplicationId appId;
171
Amit Ghosha17354e2017-08-23 12:56:04 +0100172 static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap();
Amit Ghosh83c8c892017-11-09 11:08:27 +0000173 private boolean modifyClientPktsSrcDstMac = false;
174 //Whether to use the uplink port of the OLTs to send/receive messages to the DHCP server
175 private boolean useOltUplink = false;
Amit Ghosha17354e2017-08-23 12:56:04 +0100176
Gamze Abakac806c6c2018-12-03 12:49:46 +0000177 private BaseInformationService<SubscriberAndDeviceInformation> subsService;
178
Amit Ghosh47243cb2017-07-26 05:08:53 +0100179 @Activate
180 protected void activate(ComponentContext context) {
181 //start the dhcp relay agent
182 appId = coreService.registerApplication(DHCP_L2RELAY_APP);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700183 // ensure that host-learning via dhcp includes IP addresses
184 componentConfigService.preSetProperty(HOST_LOC_PROVIDER,
185 "useDhcp", Boolean.TRUE.toString());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100186 componentConfigService.registerProperties(getClass());
Jonathan Hartc36c9552018-07-31 15:07:53 -0400187 eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100188
189 cfgService.addListener(cfgListener);
Amit Ghosh8951f042017-08-10 13:48:10 +0100190 mastershipService.addListener(changeListener);
191 deviceService.addListener(deviceListener);
192
Amit Ghosh47243cb2017-07-26 05:08:53 +0100193 factories.forEach(cfgService::registerConfigFactory);
194 //update the dhcp server configuration.
195 updateConfig();
196 //add the packet services.
197 packetService.addProcessor(dhcpRelayPacketProcessor,
198 PacketProcessor.director(0));
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000199 if (context != null) {
200 modified(context);
201 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100202
Gamze Abakac806c6c2018-12-03 12:49:46 +0000203 subsService = sadisService.getSubscriberInfoService();
204
Amit Ghosh47243cb2017-07-26 05:08:53 +0100205 log.info("DHCP-L2-RELAY Started");
206 }
207
208 @Deactivate
209 protected void deactivate() {
210 cfgService.removeListener(cfgListener);
211 factories.forEach(cfgService::unregisterConfigFactory);
212 packetService.removeProcessor(dhcpRelayPacketProcessor);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700213 cancelDhcpPktsFromServer();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100214
215 componentConfigService.unregisterProperties(getClass(), false);
Deepa Vaddireddy77a6ac72017-09-20 20:36:52 +0530216 deviceService.removeListener(deviceListener);
217 mastershipService.removeListener(changeListener);
Jonathan Hartc36c9552018-07-31 15:07:53 -0400218 eventDispatcher.removeSink(DhcpL2RelayEvent.class);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100219 log.info("DHCP-L2-RELAY Stopped");
220 }
221
222 @Modified
223 protected void modified(ComponentContext context) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000224
Amit Ghosh47243cb2017-07-26 05:08:53 +0100225 Dictionary<?, ?> properties = context.getProperties();
226
227 Boolean o = Tools.isPropertyEnabled(properties, "option82");
228 if (o != null) {
229 option82 = o;
230 }
Amit Ghosh2095dc62017-09-25 20:56:55 +0100231
232 o = Tools.isPropertyEnabled(properties, "enableDhcpBroadcastReplies");
233 if (o != null) {
234 enableDhcpBroadcastReplies = o;
235 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100236 }
237
238 /**
239 * Checks if this app has been configured.
240 *
241 * @return true if all information we need have been initialized
242 */
243 private boolean configured() {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000244 if (!useOltUplink) {
245 return dhcpServerConnectPoint.get() != null;
246 }
247 return true;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100248 }
249
Amit Ghosh8951f042017-08-10 13:48:10 +0100250 /**
251 * Selects a connect point through an available device for which it is the master.
252 */
253 private void selectServerConnectPoint() {
254 synchronized (this) {
255 dhcpServerConnectPoint.set(null);
256 if (dhcpConnectPoints != null) {
257 // find a connect point through a device for which we are master
258 for (ConnectPoint cp: dhcpConnectPoints) {
259 if (mastershipService.isLocalMaster(cp.deviceId())) {
260 if (deviceService.isAvailable(cp.deviceId())) {
261 dhcpServerConnectPoint.set(cp);
262 }
263 log.info("DHCP connectPoint selected is {}", cp);
264 break;
265 }
266 }
267 }
268
269 log.info("DHCP Server connectPoint is {}", dhcpServerConnectPoint.get());
270
271 if (dhcpServerConnectPoint.get() == null) {
272 log.error("Master of none, can't relay DHCP Message to server");
273 }
274 }
275 }
276
277 /**
278 * Updates the network configuration.
279 */
Amit Ghosh47243cb2017-07-26 05:08:53 +0100280 private void updateConfig() {
281 DhcpL2RelayConfig cfg = cfgService.getConfig(appId, DhcpL2RelayConfig.class);
282 if (cfg == null) {
283 log.warn("Dhcp Server info not available");
284 return;
285 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100286
287 dhcpConnectPoints = Sets.newConcurrentHashSet(cfg.getDhcpServerConnectPoint());
Amit Ghosh83c8c892017-11-09 11:08:27 +0000288 modifyClientPktsSrcDstMac = cfg.getModifySrcDstMacAddresses();
Saurav Dasb4e3e102018-10-02 15:31:17 -0700289 boolean prevUseOltUplink = useOltUplink;
Amit Ghosh83c8c892017-11-09 11:08:27 +0000290 useOltUplink = cfg.getUseOltUplinkForServerPktInOut();
Amit Ghosh8951f042017-08-10 13:48:10 +0100291
Saurav Dasb4e3e102018-10-02 15:31:17 -0700292 if (useOltUplink) {
293 for (ConnectPoint cp : getUplinkPortsOfOlts()) {
294 log.debug("requestDhcpPackets: ConnectPoint: {}", cp);
Saurav Dasb14f08a2019-02-22 16:34:15 -0800295 requestDhcpPacketsFromConnectPoint(cp, null);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700296 }
297 // check if previous config was different and so trap flows may
Saurav Dasb14f08a2019-02-22 16:34:15 -0800298 // need to be removed from other places like AGG switches
Saurav Dasb4e3e102018-10-02 15:31:17 -0700299 if (!prevUseOltUplink) {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800300 addOrRemoveDhcpTrapFromServer(false);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700301 }
Saurav Dasb4e3e102018-10-02 15:31:17 -0700302 } else {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800303 // uplink on AGG switch
304 addOrRemoveDhcpTrapFromServer(true);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700305 }
306 }
307
308 private void cancelDhcpPktsFromServer() {
309 if (useOltUplink) {
310 for (ConnectPoint cp : getUplinkPortsOfOlts()) {
311 log.debug("cancelDhcpPackets: ConnectPoint: {}", cp);
Saurav Dasb14f08a2019-02-22 16:34:15 -0800312 cancelDhcpPacketsFromConnectPoint(cp, null);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700313 }
314 } else {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800315 // uplink on AGG switch
316 addOrRemoveDhcpTrapFromServer(false);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000317 }
Saurav Dasb4e3e102018-10-02 15:31:17 -0700318 }
319
Saurav Dasb14f08a2019-02-22 16:34:15 -0800320 /**
321 * Used to add or remove DHCP trap flow for packets received from DHCP server.
322 * Typically used on a non OLT device, like an AGG switch. When adding, a
323 * new dhcp server connect point is selected from the configured options.
324 *
325 * @param add true if dhcp trap flow is to be added, false to remove the
326 * trap flow
327 */
328 private void addOrRemoveDhcpTrapFromServer(boolean add) {
329 if (add) {
330 selectServerConnectPoint();
331 log.debug("dhcp server connect point: " + dhcpServerConnectPoint);
332 }
333 if (dhcpServerConnectPoint.get() == null) {
334 log.warn("No dhcpServer connectPoint found, cannot {} dhcp trap flows",
335 (add) ? "install" : "remove");
336 return;
337 }
338 if (add) {
339 log.info("Adding trap to dhcp server connect point: "
340 + dhcpServerConnectPoint);
341 requestDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
342 Optional.of(PacketPriority.HIGH1));
343 } else {
344 log.info("Removing trap from dhcp server connect point: "
345 + dhcpServerConnectPoint);
346 cancelDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
347 Optional.of(PacketPriority.HIGH1));
348 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100349 }
350
351 /**
Amit Ghosh83c8c892017-11-09 11:08:27 +0000352 * Returns all the uplink ports of OLTs configured in SADIS.
353 * Only ports visible in ONOS and for which this instance is master
354 * are returned
355 */
356 private List<ConnectPoint> getUplinkPortsOfOlts() {
357 List<ConnectPoint> cps = new ArrayList<>();
358
359 // find all the olt devices and if their uplink ports are visible
360 Iterable<Device> devices = deviceService.getDevices();
361 for (Device d : devices) {
362 // check if this device is provisioned in Sadis
363
364 log.debug("getUplinkPortsOfOlts: Checking mastership of {}", d);
365 // do only for devices for which we are the master
366 if (!mastershipService.isLocalMaster(d.id())) {
367 continue;
368 }
369
370 String devSerialNo = d.serialNumber();
371 SubscriberAndDeviceInformation deviceInfo = subsService.get(devSerialNo);
372 log.debug("getUplinkPortsOfOlts: Found device: {}", deviceInfo);
373 if (deviceInfo != null) {
374 // check if the uplink port with that number is available on the device
375 PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort());
376 Port port = deviceService.getPort(d.id(), pNum);
377 log.debug("getUplinkPortsOfOlts: Found port: {}", port);
378 if (port != null) {
379 cps.add(new ConnectPoint(d.id(), pNum));
380 }
381 }
382 }
383 return cps;
384 }
385
386 /**
387 * Returns whether the passed port is the uplink port of the olt device.
388 */
389 private boolean isUplinkPortOfOlt(DeviceId dId, Port p) {
390 log.debug("isUplinkPortOfOlt: DeviceId: {} Port: {}", dId, p);
391 // do only for devices for which we are the master
392 if (!mastershipService.isLocalMaster(dId)) {
393 return false;
394 }
395
396 Device d = deviceService.getDevice(dId);
397 SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber());
398
399 if (deviceInfo != null) {
400 return (deviceInfo.uplinkPort() == p.number().toLong());
401 }
402
403 return false;
404 }
405
406 /**
407 * Returns the connectPoint which is the uplink port of the OLT.
408 */
409 private ConnectPoint getUplinkConnectPointOfOlt(DeviceId dId) {
410
411 Device d = deviceService.getDevice(dId);
412 SubscriberAndDeviceInformation deviceInfo = subsService.get(d.serialNumber());
413 log.debug("getUplinkConnectPointOfOlt DeviceId: {} devInfo: {}", dId, deviceInfo);
414 if (deviceInfo != null) {
415 PortNumber pNum = PortNumber.portNumber(deviceInfo.uplinkPort());
416 Port port = deviceService.getPort(d.id(), pNum);
417 if (port != null) {
418 return new ConnectPoint(d.id(), pNum);
419 }
420 }
421
422 return null;
423 }
424
425 /**
426 * Request DHCP packet from particular connect point via PacketService.
Saurav Dasb14f08a2019-02-22 16:34:15 -0800427 * Optionally provide a priority for the trap flow. If no such priority is
428 * provided, the default priority will be used.
429 *
430 * @param cp the connect point to trap dhcp packets from
431 * @param priority of the trap flow, null to use default priority
Amit Ghosh83c8c892017-11-09 11:08:27 +0000432 */
Saurav Dasb14f08a2019-02-22 16:34:15 -0800433 private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp,
434 Optional<PacketPriority> priority) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000435 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
436 .matchEthType(Ethernet.TYPE_IPV4)
437 .matchInPort(cp.port())
438 .matchIPProtocol(IPv4.PROTOCOL_UDP)
439 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
440 packetService.requestPackets(selectorServer.build(),
Saurav Dasb14f08a2019-02-22 16:34:15 -0800441 priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
442 appId, Optional.of(cp.deviceId()));
Amit Ghosh83c8c892017-11-09 11:08:27 +0000443 }
444
445 /**
Saurav Dasb14f08a2019-02-22 16:34:15 -0800446 * Cancel DHCP packet from particular connect point via PacketService. If
447 * the request was made with a specific packet priority, then the same
448 * priority should be used in this call.
449 *
450 * @param cp the connect point for the trap flow
451 * @param priority with which the trap flow was requested; if request
452 * priority was not specified, this param should also be null
Amit Ghosh83c8c892017-11-09 11:08:27 +0000453 */
Saurav Dasb14f08a2019-02-22 16:34:15 -0800454 private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp,
455 Optional<PacketPriority> priority) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000456 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
457 .matchEthType(Ethernet.TYPE_IPV4)
458 .matchInPort(cp.port())
459 .matchIPProtocol(IPv4.PROTOCOL_UDP)
460 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
461 packetService.cancelPackets(selectorServer.build(),
Saurav Dasb14f08a2019-02-22 16:34:15 -0800462 priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
463 appId, Optional.of(cp.deviceId()));
Amit Ghosh83c8c892017-11-09 11:08:27 +0000464 }
465
Amit Ghosha17354e2017-08-23 12:56:04 +0100466 public static Map<String, DhcpAllocationInfo> allocationMap() {
467 return allocationMap;
468 }
469
Amit Ghosh47243cb2017-07-26 05:08:53 +0100470 private SubscriberAndDeviceInformation getDevice(PacketContext context) {
471 String serialNo = deviceService.getDevice(context.inPacket().
472 receivedFrom().deviceId()).serialNumber();
473
474 return subsService.get(serialNo);
475 }
476
477 private SubscriberAndDeviceInformation getDevice(ConnectPoint cp) {
478 String serialNo = deviceService.getDevice(cp.deviceId()).
479 serialNumber();
480
481 return subsService.get(serialNo);
482 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100483
484 private MacAddress relayAgentMacAddress(PacketContext context) {
485
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000486 SubscriberAndDeviceInformation device = this.getDevice(context);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100487 if (device == null) {
488 log.warn("Device not found for {}", context.inPacket().
489 receivedFrom());
490 return null;
491 }
492
493 return device.hardwareIdentifier();
494 }
495
496 private String nasPortId(PacketContext context) {
Amit Ghosh8951f042017-08-10 13:48:10 +0100497 return nasPortId(context.inPacket().receivedFrom());
498 }
499
500 private String nasPortId(ConnectPoint cp) {
501 Port p = deviceService.getPort(cp);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100502 return p.annotations().value(AnnotationKeys.PORT_NAME);
503 }
504
505 private SubscriberAndDeviceInformation getSubscriber(PacketContext context) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100506 return subsService.get(nasPortId(context));
507 }
508
509 private VlanId cTag(PacketContext context) {
510 SubscriberAndDeviceInformation sub = getSubscriber(context);
511 if (sub == null) {
512 log.warn("Subscriber info not found for {}", context.inPacket().
513 receivedFrom());
514 return VlanId.NONE;
515 }
516 return sub.cTag();
517 }
518
Amit Ghosh8951f042017-08-10 13:48:10 +0100519 private VlanId cTag(ConnectPoint cp) {
520 String portId = nasPortId(cp);
521 SubscriberAndDeviceInformation sub = subsService.get(portId);
522 if (sub == null) {
523 log.warn("Subscriber info not found for {} looking for C-TAG", cp);
524 return VlanId.NONE;
525 }
526 return sub.cTag();
527 }
528
529 private VlanId sTag(ConnectPoint cp) {
530 String portId = nasPortId(cp);
531 SubscriberAndDeviceInformation sub = subsService.get(portId);
532 if (sub == null) {
533 log.warn("Subscriber info not found for {} looking for S-TAG", cp);
534 return VlanId.NONE;
535 }
536 return sub.sTag();
537 }
538
Amit Ghosh47243cb2017-07-26 05:08:53 +0100539 private VlanId sTag(PacketContext context) {
540 SubscriberAndDeviceInformation sub = getSubscriber(context);
541 if (sub == null) {
542 log.warn("Subscriber info not found for {}", context.inPacket().
543 receivedFrom());
544 return VlanId.NONE;
545 }
546 return sub.sTag();
547 }
548
549 private class DhcpRelayPacketProcessor implements PacketProcessor {
550
551 @Override
552 public void process(PacketContext context) {
553 if (!configured()) {
554 log.warn("Missing DHCP relay config. Abort packet processing");
555 return;
556 }
557
558 // process the packet and get the payload
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530559 Ethernet packet = context.inPacket().parsed();
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000560
Amit Ghosh47243cb2017-07-26 05:08:53 +0100561 if (packet == null) {
562 log.warn("Packet is null");
563 return;
564 }
565
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530566 if (packet.getEtherType() == Ethernet.TYPE_IPV4) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100567 IPv4 ipv4Packet = (IPv4) packet.getPayload();
568
569 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
570 UDP udpPacket = (UDP) ipv4Packet.getPayload();
571 if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT ||
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000572 udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100573 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
574 //This packet is dhcp.
575 processDhcpPacket(context, packet, dhcpPayload);
576 }
577 }
578 }
579 }
580
581 //forward the packet to ConnectPoint where the DHCP server is attached.
Amit Ghosh83c8c892017-11-09 11:08:27 +0000582 private void forwardPacket(Ethernet packet, PacketContext context) {
583 ConnectPoint toSendTo = null;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100584
Amit Ghosh83c8c892017-11-09 11:08:27 +0000585 if (!useOltUplink) {
586 toSendTo = dhcpServerConnectPoint.get();
587 } else {
588 toSendTo = getUplinkConnectPointOfOlt(context.inPacket().
589 receivedFrom().deviceId());
590 }
591
592 if (toSendTo != null) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100593 TrafficTreatment t = DefaultTrafficTreatment.builder()
Amit Ghosh83c8c892017-11-09 11:08:27 +0000594 .setOutput(toSendTo.port()).build();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100595 OutboundPacket o = new DefaultOutboundPacket(
Amit Ghosh83c8c892017-11-09 11:08:27 +0000596 toSendTo.deviceId(), t,
Amit Ghosh47243cb2017-07-26 05:08:53 +0100597 ByteBuffer.wrap(packet.serialize()));
598 if (log.isTraceEnabled()) {
Saurav Das15626a02018-09-27 18:36:45 -0700599 log.trace("Relaying packet to dhcp server at {} {}",
600 toSendTo, packet);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100601 }
602 packetService.emit(o);
603 } else {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000604 log.error("No connect point to send msg to DHCP Server");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100605 }
606 }
607
Amit Ghosha17354e2017-08-23 12:56:04 +0100608 // get the type of the DHCP packet
609 private DHCPPacketType getDhcpPacketType(DHCP dhcpPayload) {
610
Jonathan Hartedbf6422018-05-02 17:30:05 -0700611 for (DhcpOption option : dhcpPayload.getOptions()) {
Amit Ghosha17354e2017-08-23 12:56:04 +0100612 if (option.getCode() == OptionCode_MessageType.getValue()) {
613 byte[] data = option.getData();
614 return DHCPPacketType.getType(data[0]);
615 }
616 }
617 return null;
618 }
619
Amit Ghosh47243cb2017-07-26 05:08:53 +0100620 //process the dhcp packet before sending to server
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530621 private void processDhcpPacket(PacketContext context, Ethernet packet,
Amit Ghosh47243cb2017-07-26 05:08:53 +0100622 DHCP dhcpPayload) {
623 if (dhcpPayload == null) {
624 log.warn("DHCP payload is null");
625 return;
626 }
627
Amit Ghosha17354e2017-08-23 12:56:04 +0100628 DHCPPacketType incomingPacketType = getDhcpPacketType(dhcpPayload);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000629
Saurav Das15626a02018-09-27 18:36:45 -0700630 log.info("Received DHCP Packet of type {} from {}",
631 incomingPacketType, context.inPacket().receivedFrom());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100632
633 switch (incomingPacketType) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000634 case DHCPDISCOVER:
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530635 Ethernet ethernetPacketDiscover =
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000636 processDhcpPacketFromClient(context, packet);
637 if (ethernetPacketDiscover != null) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000638 forwardPacket(ethernetPacketDiscover, context);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000639 }
640 break;
641 case DHCPOFFER:
642 //reply to dhcp client.
Saurav Das15626a02018-09-27 18:36:45 -0700643 Ethernet ethernetPacketOffer =
644 processDhcpPacketFromServer(context, packet);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000645 if (ethernetPacketOffer != null) {
646 sendReply(ethernetPacketOffer, dhcpPayload);
647 }
648 break;
649 case DHCPREQUEST:
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530650 Ethernet ethernetPacketRequest =
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000651 processDhcpPacketFromClient(context, packet);
652 if (ethernetPacketRequest != null) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000653 forwardPacket(ethernetPacketRequest, context);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000654 }
655 break;
656 case DHCPACK:
657 //reply to dhcp client.
Saurav Das15626a02018-09-27 18:36:45 -0700658 Ethernet ethernetPacketAck =
659 processDhcpPacketFromServer(context, packet);
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000660 if (ethernetPacketAck != null) {
661 sendReply(ethernetPacketAck, dhcpPayload);
662 }
663 break;
664 default:
665 break;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100666 }
667 }
668
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530669 private Ethernet processDhcpPacketFromClient(PacketContext context,
670 Ethernet ethernetPacket) {
Saurav Das15626a02018-09-27 18:36:45 -0700671 if (log.isTraceEnabled()) {
672 log.trace("DHCP packet received from client at {} {}",
673 context.inPacket().receivedFrom(), ethernetPacket);
674 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100675
676 MacAddress relayAgentMac = relayAgentMacAddress(context);
677 if (relayAgentMac == null) {
678 log.warn("RelayAgent MAC not found ");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100679 return null;
680 }
681
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530682 Ethernet etherReply = ethernetPacket;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100683
684 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
685 UDP udpPacket = (UDP) ipv4Packet.getPayload();
686 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
687
Amit Ghosha17354e2017-08-23 12:56:04 +0100688 if (enableDhcpBroadcastReplies) {
689 // We want the reply to come back as a L2 broadcast
690 dhcpPacket.setFlags((short) 0x8000);
691 }
692
Jonathan Hartc36c9552018-07-31 15:07:53 -0400693 MacAddress clientMac = MacAddress.valueOf(dhcpPacket.getClientHardwareAddress());
694 IpAddress clientIp = IpAddress.valueOf(dhcpPacket.getClientIPAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100695
Jonathan Hartc36c9552018-07-31 15:07:53 -0400696 SubscriberAndDeviceInformation entry = getSubscriber(context);
697 if (entry == null) {
Saurav Das15626a02018-09-27 18:36:45 -0700698 log.warn("Dropping packet as subscriber entry is not available");
Jonathan Hartc36c9552018-07-31 15:07:53 -0400699 return null;
700 }
701
702 DhcpAllocationInfo info = new DhcpAllocationInfo(
703 context.inPacket().receivedFrom(), dhcpPacket.getPacketType(),
704 entry.nasPortId(), clientMac, clientIp);
705
706 allocationMap.put(entry.nasPortId(), info);
707
Saurav Das15626a02018-09-27 18:36:45 -0700708 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info,
709 context.inPacket().receivedFrom()));
Amit Ghosha17354e2017-08-23 12:56:04 +0100710
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000711 if (option82) {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000712 DHCP dhcpPacketWithOption82 = addOption82(dhcpPacket, entry);
713 udpPacket.setPayload(dhcpPacketWithOption82);
714 }
715
716 ipv4Packet.setPayload(udpPacket);
717 etherReply.setPayload(ipv4Packet);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000718 if (modifyClientPktsSrcDstMac) {
719 etherReply.setSourceMACAddress(relayAgentMac);
720 etherReply.setDestinationMACAddress(dhcpConnectMac);
721 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100722
Amit Ghosh8951f042017-08-10 13:48:10 +0100723 etherReply.setPriorityCode(ethernetPacket.getPriorityCode());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100724 etherReply.setVlanID(cTag(context).toShort());
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530725 etherReply.setQinQTPID(Ethernet.TYPE_VLAN);
726 etherReply.setQinQVID(sTag(context).toShort());
Saurav Das15626a02018-09-27 18:36:45 -0700727 log.info("Finished processing packet.. relaying to dhcpServer");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100728 return etherReply;
729 }
730
731 //build the DHCP offer/ack with proper client port.
Saurav Das15626a02018-09-27 18:36:45 -0700732 private Ethernet processDhcpPacketFromServer(PacketContext context,
733 Ethernet ethernetPacket) {
734 if (log.isTraceEnabled()) {
735 log.trace("DHCP packet received from server at {} {}",
736 context.inPacket().receivedFrom(), ethernetPacket);
737 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100738 // get dhcp header.
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530739 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100740 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
741 UDP udpPacket = (UDP) ipv4Packet.getPayload();
742 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
743
Amit Ghosh47243cb2017-07-26 05:08:53 +0100744 MacAddress dstMac = valueOf(dhcpPayload.getClientHardwareAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100745 ConnectPoint subsCp = getConnectPointOfClient(dstMac);
Amit Ghosh2095dc62017-09-25 20:56:55 +0100746 // If we can't find the subscriber, can't process further
747 if (subsCp == null) {
748 return null;
749 }
Amit Ghosha17354e2017-08-23 12:56:04 +0100750 // if it's an ACK packet store the information for display purpose
751 if (getDhcpPacketType(dhcpPayload) == DHCPPacketType.DHCPACK) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100752
Amit Ghosha17354e2017-08-23 12:56:04 +0100753 String portId = nasPortId(subsCp);
754 SubscriberAndDeviceInformation sub = subsService.get(portId);
755 if (sub != null) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700756 List<DhcpOption> options = dhcpPayload.getOptions();
757 List<DhcpOption> circuitIds = options.stream()
Amit Ghosha17354e2017-08-23 12:56:04 +0100758 .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
759 .collect(Collectors.toList());
760
761 String circuitId = "None";
762 if (circuitIds.size() == 1) {
Amit Ghosh2095dc62017-09-25 20:56:55 +0100763 byte[] array = circuitIds.get(0).getData();
764
765 try {
766 // we leave the first two bytes as they are the id and length
767 circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8");
768 } catch (Exception e) { }
Amit Ghosha17354e2017-08-23 12:56:04 +0100769 }
770
771 IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress());
772
773 //storeDHCPAllocationInfo
Jonathan Hartc36c9552018-07-31 15:07:53 -0400774 DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp,
775 dhcpPayload.getPacketType(), circuitId, dstMac, ip);
Amit Ghosha17354e2017-08-23 12:56:04 +0100776
777 allocationMap.put(sub.nasPortId(), info);
Jonathan Hartc36c9552018-07-31 15:07:53 -0400778
779 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp));
Amit Ghosha17354e2017-08-23 12:56:04 +0100780 }
781 } // end storing of info
Amit Ghosh47243cb2017-07-26 05:08:53 +0100782
783 // we leave the srcMac from the original packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530784 etherReply.setDestinationMACAddress(dstMac);
785 etherReply.setQinQVID(sTag(subsCp).toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100786 etherReply.setPriorityCode(ethernetPacket.getPriorityCode());
787 etherReply.setVlanID((cTag(subsCp).toShort()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100788
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000789 if (option82) {
790 udpPacket.setPayload(removeOption82(dhcpPayload));
791 } else {
792 udpPacket.setPayload(dhcpPayload);
793 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100794 ipv4Packet.setPayload(udpPacket);
795 etherReply.setPayload(ipv4Packet);
796
Saurav Das15626a02018-09-27 18:36:45 -0700797 log.info("Finished processing packet.. relaying to client");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100798 return etherReply;
799 }
800
Amit Ghosha17354e2017-08-23 12:56:04 +0100801 /*
802 * Get ConnectPoint of the Client based on it's MAC address
803 */
804 private ConnectPoint getConnectPointOfClient(MacAddress dstMac) {
805 Set<Host> hosts = hostService.getHostsByMac(dstMac);
806 if (hosts == null || hosts.isEmpty()) {
807 log.warn("Cannot determine host for DHCP client: {}. Aborting "
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530808 + "relay for dhcp packet from server",
Amit Ghosha17354e2017-08-23 12:56:04 +0100809 dstMac);
810 return null;
811 }
812 for (Host h : hosts) {
813 // if more than one,
814 // find the connect point which has an valid entry in SADIS
815 ConnectPoint cp = new ConnectPoint(h.location().deviceId(),
816 h.location().port());
817
818 if (sTag(cp) != VlanId.NONE) {
819 return cp;
820 }
821 }
822
823 return null;
824 }
825
Amit Ghosh47243cb2017-07-26 05:08:53 +0100826 //send the response to the requester host.
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530827 private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100828 MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100829 ConnectPoint subCp = getConnectPointOfClient(descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100830
831 // Send packet out to requester if the host information is available
Amit Ghosha17354e2017-08-23 12:56:04 +0100832 if (subCp != null) {
Saurav Das15626a02018-09-27 18:36:45 -0700833 log.info("Sending DHCP packet to client at {}", subCp);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100834 TrafficTreatment t = DefaultTrafficTreatment.builder()
Amit Ghosha17354e2017-08-23 12:56:04 +0100835 .setOutput(subCp.port()).build();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100836 OutboundPacket o = new DefaultOutboundPacket(
Amit Ghosha17354e2017-08-23 12:56:04 +0100837 subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100838 if (log.isTraceEnabled()) {
Saurav Das15626a02018-09-27 18:36:45 -0700839 log.trace("Relaying packet to dhcp client at {} {}", subCp,
840 ethPacket);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100841 }
842 packetService.emit(o);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100843 } else {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000844 log.error("Dropping DHCP packet because can't find host for {}", descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100845 }
846 }
847 }
848
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000849 private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) {
850 log.debug("option82data {} ", entry);
851
Jonathan Hartedbf6422018-05-02 17:30:05 -0700852 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000853 DhcpOption82 option82 = new DhcpOption82();
854 option82.setAgentCircuitId(entry.circuitId());
855 option82.setAgentRemoteId(entry.remoteId());
Jonathan Hartedbf6422018-05-02 17:30:05 -0700856 DhcpOption option = new DhcpOption()
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530857 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
858 .setData(option82.toByteArray())
859 .setLength(option82.length());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000860
861 options.add(options.size() - 1, option);
862 dhcpPacket.setOptions(options);
Amit Ghosh8951f042017-08-10 13:48:10 +0100863
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000864 return dhcpPacket;
865
866 }
867
868 private DHCP removeOption82(DHCP dhcpPacket) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700869 List<DhcpOption> options = dhcpPacket.getOptions();
870 List<DhcpOption> newoptions = options.stream()
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000871 .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
872 .collect(Collectors.toList());
873
874 return dhcpPacket.setOptions(newoptions);
875 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100876 /**
877 * Listener for network config events.
878 */
879 private class InternalConfigListener implements NetworkConfigListener {
880
881 @Override
882 public void event(NetworkConfigEvent event) {
883
884 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
885 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
886 event.configClass().equals(DhcpL2RelayConfig.class)) {
887 updateConfig();
888 log.info("Reconfigured");
889 }
890 }
891 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000892
Amit Ghosh8951f042017-08-10 13:48:10 +0100893 /**
894 * Handles Mastership changes for the devices which connect
895 * to the DHCP server.
896 */
897 private class InnerMastershipListener implements MastershipListener {
898 @Override
899 public void event(MastershipEvent event) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000900 if (!useOltUplink) {
901 if (dhcpServerConnectPoint.get() != null &&
902 dhcpServerConnectPoint.get().deviceId().
903 equals(event.subject())) {
904 log.trace("Mastership Event recevived for {}", event.subject());
905 // mastership of the device for our connect point has changed
906 // reselect
907 selectServerConnectPoint();
908 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100909 }
910 }
911 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000912
Amit Ghosh8951f042017-08-10 13:48:10 +0100913 /**
914 * Handles Device status change for the devices which connect
915 * to the DHCP server.
916 */
917 private class InnerDeviceListener implements DeviceListener {
918 @Override
919 public void event(DeviceEvent event) {
Saurav Das15626a02018-09-27 18:36:45 -0700920 if (log.isTraceEnabled() &&
921 !event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) {
922 log.trace("Device Event received for {} event {}",
923 event.subject(), event.type());
924 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000925 if (!useOltUplink) {
926 if (dhcpServerConnectPoint.get() == null) {
927 switch (event.type()) {
928 case DEVICE_ADDED:
929 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasb14f08a2019-02-22 16:34:15 -0800930 // some device is available check if we can get a
931 // connect point we can use
932 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000933 break;
934 default:
935 break;
936 }
937 return;
Amit Ghosh8951f042017-08-10 13:48:10 +0100938 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000939 if (dhcpServerConnectPoint.get().deviceId().
940 equals(event.subject().id())) {
941 switch (event.type()) {
942 case DEVICE_AVAILABILITY_CHANGED:
943 case DEVICE_REMOVED:
944 case DEVICE_SUSPENDED:
945 // state of our device has changed, check if we need
Saurav Dasb14f08a2019-02-22 16:34:15 -0800946 // to re-select a connectpoint
947 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000948 break;
949 default:
950 break;
951 }
952 }
953 } else {
Amit Ghosh8951f042017-08-10 13:48:10 +0100954 switch (event.type()) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000955 case PORT_ADDED:
Saurav Dasb4e3e102018-10-02 15:31:17 -0700956 if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800957 requestDhcpPacketsFromConnectPoint(
958 new ConnectPoint(event.subject().id(), event.port().number()),
959 null);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000960 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100961 break;
962 default:
963 break;
964 }
965 }
966 }
967 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100968}