blob: 82de81b69817c71b0e42685c59780457876522fb [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
Carmelo Casconede1e6e32019-07-15 19:39:08 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Amit Ghosh47243cb2017-07-26 05:08:53 +010022import org.onlab.packet.DHCP;
Deepa vaddireddy0060f532017-08-04 06:46:05 +000023import org.onlab.packet.Ethernet;
Amit Ghosh47243cb2017-07-26 05:08:53 +010024import org.onlab.packet.IPv4;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053025import org.onlab.packet.IpAddress;
Deepa vaddireddy0060f532017-08-04 06:46:05 +000026import org.onlab.packet.MacAddress;
Amit Ghosh47243cb2017-07-26 05:08:53 +010027import org.onlab.packet.TpPort;
28import org.onlab.packet.UDP;
29import org.onlab.packet.VlanId;
Jonathan Hartedbf6422018-05-02 17:30:05 -070030import org.onlab.packet.dhcp.DhcpOption;
Amit Ghosh47243cb2017-07-26 05:08:53 +010031import org.onlab.util.Tools;
32import org.onosproject.cfg.ComponentConfigService;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonathan Hartc36c9552018-07-31 15:07:53 -040035import org.onosproject.event.AbstractListenerManager;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053036import org.onosproject.mastership.MastershipEvent;
37import org.onosproject.mastership.MastershipListener;
38import org.onosproject.mastership.MastershipService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010039import org.onosproject.net.AnnotationKeys;
40import org.onosproject.net.ConnectPoint;
Amit Ghosh83c8c892017-11-09 11:08:27 +000041import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
Amit Ghosh47243cb2017-07-26 05:08:53 +010043import org.onosproject.net.Host;
44import org.onosproject.net.Port;
Amit Ghosh83c8c892017-11-09 11:08:27 +000045import org.onosproject.net.PortNumber;
Amit Ghosh47243cb2017-07-26 05:08:53 +010046import org.onosproject.net.config.ConfigFactory;
47import org.onosproject.net.config.NetworkConfigEvent;
48import org.onosproject.net.config.NetworkConfigListener;
49import org.onosproject.net.config.NetworkConfigRegistry;
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +053050import org.onosproject.net.device.DeviceEvent;
51import org.onosproject.net.device.DeviceListener;
Amit Ghosh47243cb2017-07-26 05:08:53 +010052import org.onosproject.net.device.DeviceService;
53import org.onosproject.net.flow.DefaultTrafficSelector;
54import org.onosproject.net.flow.DefaultTrafficTreatment;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
Saurav Dasb4e3e102018-10-02 15:31:17 -070057import org.onosproject.net.flowobjective.FlowObjectiveService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010058import org.onosproject.net.host.HostService;
59import org.onosproject.net.packet.DefaultOutboundPacket;
60import org.onosproject.net.packet.OutboundPacket;
61import org.onosproject.net.packet.PacketContext;
62import org.onosproject.net.packet.PacketPriority;
63import org.onosproject.net.packet.PacketProcessor;
64import org.onosproject.net.packet.PacketService;
Matteo Scandolo57af5d12019-04-29 17:11:41 -070065import org.opencord.dhcpl2relay.DhcpAllocationInfo;
66import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
67import org.opencord.dhcpl2relay.DhcpL2RelayListener;
68import org.opencord.dhcpl2relay.DhcpL2RelayService;
69import org.opencord.dhcpl2relay.impl.packet.DhcpOption82;
Gamze Abakac806c6c2018-12-03 12:49:46 +000070import org.opencord.sadis.BaseInformationService;
71import org.opencord.sadis.SadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +010072import org.opencord.sadis.SubscriberAndDeviceInformation;
Amit Ghosh47243cb2017-07-26 05:08:53 +010073import org.osgi.service.component.ComponentContext;
Carmelo Casconede1e6e32019-07-15 19:39:08 -070074import org.osgi.service.component.annotations.Activate;
75import org.osgi.service.component.annotations.Component;
76import org.osgi.service.component.annotations.Deactivate;
77import org.osgi.service.component.annotations.Modified;
78import org.osgi.service.component.annotations.Reference;
79import org.osgi.service.component.annotations.ReferenceCardinality;
Amit Ghosh47243cb2017-07-26 05:08:53 +010080import org.slf4j.Logger;
81import org.slf4j.LoggerFactory;
82
Carmelo Casconede1e6e32019-07-15 19:39:08 -070083import java.nio.ByteBuffer;
84import java.util.ArrayList;
85import java.util.Arrays;
86import java.util.Dictionary;
87import java.util.List;
88import java.util.Map;
89import java.util.Optional;
90import java.util.Set;
91import java.util.concurrent.atomic.AtomicReference;
92import java.util.stream.Collectors;
93
94import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
95import static org.onlab.packet.MacAddress.valueOf;
96import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
97import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES;
98import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT;
99import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82;
100import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82_DEFAULT;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100101
102/**
103 * DHCP Relay Agent Application Component.
104 */
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700105@Component(immediate = true,
106property = {
107 OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT,
108 ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT,
109})
Jonathan Hartc36c9552018-07-31 15:07:53 -0400110public class DhcpL2Relay
111 extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener>
112 implements DhcpL2RelayService {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100113
114 public static final String DHCP_L2RELAY_APP = "org.opencord.dhcpl2relay";
Saurav Dasb4e3e102018-10-02 15:31:17 -0700115 private static final String HOST_LOC_PROVIDER =
116 "org.onosproject.provider.host.impl.HostLocationProvider";
Amit Ghosh47243cb2017-07-26 05:08:53 +0100117 private final Logger log = LoggerFactory.getLogger(getClass());
118 private final InternalConfigListener cfgListener =
119 new InternalConfigListener();
120
121 private final Set<ConfigFactory> factories = ImmutableSet.of(
122 new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY,
123 DhcpL2RelayConfig.class,
124 "dhcpl2relay") {
125 @Override
126 public DhcpL2RelayConfig createConfig() {
127 return new DhcpL2RelayConfig();
128 }
129 }
130 );
131
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100133 protected NetworkConfigRegistry cfgService;
134
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100136 protected CoreService coreService;
137
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100139 protected PacketService packetService;
140
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100142 protected HostService hostService;
143
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100145 protected ComponentConfigService componentConfigService;
146
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700147 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Gamze Abakac806c6c2018-12-03 12:49:46 +0000148 protected SadisService sadisService;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100149
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh47243cb2017-07-26 05:08:53 +0100151 protected DeviceService deviceService;
152
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700153 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Amit Ghosh8951f042017-08-10 13:48:10 +0100154 protected MastershipService mastershipService;
155
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700156 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dasb4e3e102018-10-02 15:31:17 -0700157 protected FlowObjectiveService flowObjectiveService;
158
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700159 /** Add option 82 to relayed packets */
160 protected boolean option82 = OPTION_82_DEFAULT;
Amit Ghosh47243cb2017-07-26 05:08:53 +0100161
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700162 /** Ask the DHCP Server to send back replies as L2 broadcast */
163 protected boolean enableDhcpBroadcastReplies = ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT;
Amit Ghosha17354e2017-08-23 12:56:04 +0100164
Amit Ghosh47243cb2017-07-26 05:08:53 +0100165 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor =
166 new DhcpRelayPacketProcessor();
167
Amit Ghosh8951f042017-08-10 13:48:10 +0100168 private InnerMastershipListener changeListener = new InnerMastershipListener();
169 private InnerDeviceListener deviceListener = new InnerDeviceListener();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100170
Amit Ghosh8951f042017-08-10 13:48:10 +0100171 // connect points to the DHCP server
172 Set<ConnectPoint> dhcpConnectPoints;
173 private AtomicReference<ConnectPoint> dhcpServerConnectPoint = new AtomicReference<>();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100174 private MacAddress dhcpConnectMac = MacAddress.BROADCAST;
175 private ApplicationId appId;
176
Amit Ghosha17354e2017-08-23 12:56:04 +0100177 static Map<String, DhcpAllocationInfo> allocationMap = Maps.newConcurrentMap();
Amit Ghosh83c8c892017-11-09 11:08:27 +0000178 private boolean modifyClientPktsSrcDstMac = false;
179 //Whether to use the uplink port of the OLTs to send/receive messages to the DHCP server
180 private boolean useOltUplink = false;
Amit Ghosha17354e2017-08-23 12:56:04 +0100181
Gamze Abakac806c6c2018-12-03 12:49:46 +0000182 private BaseInformationService<SubscriberAndDeviceInformation> subsService;
183
Amit Ghosh47243cb2017-07-26 05:08:53 +0100184 @Activate
185 protected void activate(ComponentContext context) {
186 //start the dhcp relay agent
187 appId = coreService.registerApplication(DHCP_L2RELAY_APP);
Saurav Dasb4e3e102018-10-02 15:31:17 -0700188 // ensure that host-learning via dhcp includes IP addresses
189 componentConfigService.preSetProperty(HOST_LOC_PROVIDER,
190 "useDhcp", Boolean.TRUE.toString());
Amit Ghosh47243cb2017-07-26 05:08:53 +0100191 componentConfigService.registerProperties(getClass());
Jonathan Hartc36c9552018-07-31 15:07:53 -0400192 eventDispatcher.addSink(DhcpL2RelayEvent.class, listenerRegistry);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100193
194 cfgService.addListener(cfgListener);
Amit Ghosh8951f042017-08-10 13:48:10 +0100195 mastershipService.addListener(changeListener);
196 deviceService.addListener(deviceListener);
197
Amit Ghosh47243cb2017-07-26 05:08:53 +0100198 factories.forEach(cfgService::registerConfigFactory);
199 //update the dhcp server configuration.
200 updateConfig();
201 //add the packet services.
202 packetService.addProcessor(dhcpRelayPacketProcessor,
203 PacketProcessor.director(0));
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000204 if (context != null) {
205 modified(context);
206 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100207
Gamze Abakac806c6c2018-12-03 12:49:46 +0000208 subsService = sadisService.getSubscriberInfoService();
209
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
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700232 Boolean o = Tools.isPropertyEnabled(properties, OPTION_82);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100233 if (o != null) {
234 option82 = o;
235 }
Amit Ghosh2095dc62017-09-25 20:56:55 +0100236
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700237 o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_BROADCAST_REPLIES);
Amit Ghosh2095dc62017-09-25 20:56:55 +0100238 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);
Saurav Dasb14f08a2019-02-22 16:34:15 -0800300 requestDhcpPacketsFromConnectPoint(cp, 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);
Saurav Dasb14f08a2019-02-22 16:34:15 -0800317 cancelDhcpPacketsFromConnectPoint(cp, 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
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700614 private DHCP.MsgType getDhcpPacketType(DHCP dhcpPayload) {
Amit Ghosha17354e2017-08-23 12:56:04 +0100615
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();
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700619 return DHCP.MsgType.getType(data[0]);
Amit Ghosha17354e2017-08-23 12:56:04 +0100620 }
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
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700633 DHCP.MsgType 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) {
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700753 log.warn("Couldn't find connection point for mac address {} DHCPOFFERs won't be delivered", dstMac);
Amit Ghosh2095dc62017-09-25 20:56:55 +0100754 return null;
755 }
Amit Ghosha17354e2017-08-23 12:56:04 +0100756 // if it's an ACK packet store the information for display purpose
Carmelo Casconede1e6e32019-07-15 19:39:08 -0700757 if (getDhcpPacketType(dhcpPayload) == DHCP.MsgType.DHCPACK) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100758
Amit Ghosha17354e2017-08-23 12:56:04 +0100759 String portId = nasPortId(subsCp);
760 SubscriberAndDeviceInformation sub = subsService.get(portId);
761 if (sub != null) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700762 List<DhcpOption> options = dhcpPayload.getOptions();
763 List<DhcpOption> circuitIds = options.stream()
Amit Ghosha17354e2017-08-23 12:56:04 +0100764 .filter(option -> option.getCode() == DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
765 .collect(Collectors.toList());
766
767 String circuitId = "None";
768 if (circuitIds.size() == 1) {
Amit Ghosh2095dc62017-09-25 20:56:55 +0100769 byte[] array = circuitIds.get(0).getData();
770
771 try {
772 // we leave the first two bytes as they are the id and length
773 circuitId = new String(Arrays.copyOfRange(array, 2, array.length), "UTF-8");
774 } catch (Exception e) { }
Amit Ghosha17354e2017-08-23 12:56:04 +0100775 }
776
777 IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress());
778
779 //storeDHCPAllocationInfo
Jonathan Hartc36c9552018-07-31 15:07:53 -0400780 DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp,
781 dhcpPayload.getPacketType(), circuitId, dstMac, ip);
Amit Ghosha17354e2017-08-23 12:56:04 +0100782
783 allocationMap.put(sub.nasPortId(), info);
Jonathan Hartc36c9552018-07-31 15:07:53 -0400784
785 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp));
Amit Ghosha17354e2017-08-23 12:56:04 +0100786 }
787 } // end storing of info
Amit Ghosh47243cb2017-07-26 05:08:53 +0100788
789 // we leave the srcMac from the original packet
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530790 etherReply.setDestinationMACAddress(dstMac);
791 etherReply.setQinQVID(sTag(subsCp).toShort());
Amit Ghosh8951f042017-08-10 13:48:10 +0100792 etherReply.setPriorityCode(ethernetPacket.getPriorityCode());
793 etherReply.setVlanID((cTag(subsCp).toShort()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100794
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000795 if (option82) {
796 udpPacket.setPayload(removeOption82(dhcpPayload));
797 } else {
798 udpPacket.setPayload(dhcpPayload);
799 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100800 ipv4Packet.setPayload(udpPacket);
801 etherReply.setPayload(ipv4Packet);
802
Saurav Das15626a02018-09-27 18:36:45 -0700803 log.info("Finished processing packet.. relaying to client");
Amit Ghosh47243cb2017-07-26 05:08:53 +0100804 return etherReply;
805 }
806
Amit Ghosha17354e2017-08-23 12:56:04 +0100807 /*
808 * Get ConnectPoint of the Client based on it's MAC address
809 */
810 private ConnectPoint getConnectPointOfClient(MacAddress dstMac) {
811 Set<Host> hosts = hostService.getHostsByMac(dstMac);
812 if (hosts == null || hosts.isEmpty()) {
813 log.warn("Cannot determine host for DHCP client: {}. Aborting "
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530814 + "relay for dhcp packet from server",
Amit Ghosha17354e2017-08-23 12:56:04 +0100815 dstMac);
816 return null;
817 }
818 for (Host h : hosts) {
819 // if more than one,
820 // find the connect point which has an valid entry in SADIS
821 ConnectPoint cp = new ConnectPoint(h.location().deviceId(),
822 h.location().port());
823
824 if (sTag(cp) != VlanId.NONE) {
825 return cp;
826 }
827 }
828
829 return null;
830 }
831
Amit Ghosh47243cb2017-07-26 05:08:53 +0100832 //send the response to the requester host.
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530833 private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) {
Amit Ghosh47243cb2017-07-26 05:08:53 +0100834 MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress());
Amit Ghosha17354e2017-08-23 12:56:04 +0100835 ConnectPoint subCp = getConnectPointOfClient(descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100836
837 // Send packet out to requester if the host information is available
Amit Ghosha17354e2017-08-23 12:56:04 +0100838 if (subCp != null) {
Saurav Das15626a02018-09-27 18:36:45 -0700839 log.info("Sending DHCP packet to client at {}", subCp);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100840 TrafficTreatment t = DefaultTrafficTreatment.builder()
Amit Ghosha17354e2017-08-23 12:56:04 +0100841 .setOutput(subCp.port()).build();
Amit Ghosh47243cb2017-07-26 05:08:53 +0100842 OutboundPacket o = new DefaultOutboundPacket(
Amit Ghosha17354e2017-08-23 12:56:04 +0100843 subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize()));
Amit Ghosh47243cb2017-07-26 05:08:53 +0100844 if (log.isTraceEnabled()) {
Saurav Das15626a02018-09-27 18:36:45 -0700845 log.trace("Relaying packet to dhcp client at {} {}", subCp,
846 ethPacket);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100847 }
848 packetService.emit(o);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100849 } else {
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000850 log.error("Dropping DHCP packet because can't find host for {}", descMac);
Amit Ghosh47243cb2017-07-26 05:08:53 +0100851 }
852 }
853 }
854
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000855 private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) {
856 log.debug("option82data {} ", entry);
857
Jonathan Hartedbf6422018-05-02 17:30:05 -0700858 List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000859 DhcpOption82 option82 = new DhcpOption82();
860 option82.setAgentCircuitId(entry.circuitId());
861 option82.setAgentRemoteId(entry.remoteId());
Jonathan Hartedbf6422018-05-02 17:30:05 -0700862 DhcpOption option = new DhcpOption()
Deepa Vaddireddy5f278d62017-08-30 05:59:39 +0530863 .setCode(DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
864 .setData(option82.toByteArray())
865 .setLength(option82.length());
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000866
867 options.add(options.size() - 1, option);
868 dhcpPacket.setOptions(options);
Amit Ghosh8951f042017-08-10 13:48:10 +0100869
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000870 return dhcpPacket;
871
872 }
873
874 private DHCP removeOption82(DHCP dhcpPacket) {
Jonathan Hartedbf6422018-05-02 17:30:05 -0700875 List<DhcpOption> options = dhcpPacket.getOptions();
876 List<DhcpOption> newoptions = options.stream()
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000877 .filter(option -> option.getCode() != DHCP.DHCPOptionCode.OptionCode_CircuitID.getValue())
878 .collect(Collectors.toList());
879
880 return dhcpPacket.setOptions(newoptions);
881 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100882 /**
883 * Listener for network config events.
884 */
885 private class InternalConfigListener implements NetworkConfigListener {
886
887 @Override
888 public void event(NetworkConfigEvent event) {
889
890 if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
891 event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
892 event.configClass().equals(DhcpL2RelayConfig.class)) {
893 updateConfig();
894 log.info("Reconfigured");
895 }
896 }
897 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000898
Amit Ghosh8951f042017-08-10 13:48:10 +0100899 /**
900 * Handles Mastership changes for the devices which connect
901 * to the DHCP server.
902 */
903 private class InnerMastershipListener implements MastershipListener {
904 @Override
905 public void event(MastershipEvent event) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000906 if (!useOltUplink) {
907 if (dhcpServerConnectPoint.get() != null &&
908 dhcpServerConnectPoint.get().deviceId().
909 equals(event.subject())) {
910 log.trace("Mastership Event recevived for {}", event.subject());
911 // mastership of the device for our connect point has changed
912 // reselect
913 selectServerConnectPoint();
914 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100915 }
916 }
917 }
Deepa vaddireddy0060f532017-08-04 06:46:05 +0000918
Amit Ghosh8951f042017-08-10 13:48:10 +0100919 /**
920 * Handles Device status change for the devices which connect
921 * to the DHCP server.
922 */
923 private class InnerDeviceListener implements DeviceListener {
924 @Override
925 public void event(DeviceEvent event) {
Saurav Das15626a02018-09-27 18:36:45 -0700926 if (log.isTraceEnabled() &&
927 !event.type().equals(DeviceEvent.Type.PORT_STATS_UPDATED)) {
928 log.trace("Device Event received for {} event {}",
929 event.subject(), event.type());
930 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000931 if (!useOltUplink) {
932 if (dhcpServerConnectPoint.get() == null) {
933 switch (event.type()) {
934 case DEVICE_ADDED:
935 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasb14f08a2019-02-22 16:34:15 -0800936 // some device is available check if we can get a
937 // connect point we can use
938 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000939 break;
940 default:
941 break;
942 }
943 return;
Amit Ghosh8951f042017-08-10 13:48:10 +0100944 }
Amit Ghosh83c8c892017-11-09 11:08:27 +0000945 if (dhcpServerConnectPoint.get().deviceId().
946 equals(event.subject().id())) {
947 switch (event.type()) {
948 case DEVICE_AVAILABILITY_CHANGED:
949 case DEVICE_REMOVED:
950 case DEVICE_SUSPENDED:
951 // state of our device has changed, check if we need
Saurav Dasb14f08a2019-02-22 16:34:15 -0800952 // to re-select a connectpoint
953 addOrRemoveDhcpTrapFromServer(true);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000954 break;
955 default:
956 break;
957 }
958 }
959 } else {
Amit Ghosh8951f042017-08-10 13:48:10 +0100960 switch (event.type()) {
Amit Ghosh83c8c892017-11-09 11:08:27 +0000961 case PORT_ADDED:
Saurav Dasb4e3e102018-10-02 15:31:17 -0700962 if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) {
Saurav Dasb14f08a2019-02-22 16:34:15 -0800963 requestDhcpPacketsFromConnectPoint(
964 new ConnectPoint(event.subject().id(), event.port().number()),
965 null);
Amit Ghosh83c8c892017-11-09 11:08:27 +0000966 }
Amit Ghosh8951f042017-08-10 13:48:10 +0100967 break;
968 default:
969 break;
970 }
971 }
972 }
973 }
Amit Ghosh47243cb2017-07-26 05:08:53 +0100974}