blob: bb14e166401c01519b6d4ab70e657bc910e7c38f [file] [log] [blame]
Yi Tseng7a38f9a2017-06-09 14:36:40 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yi Tseng7a38f9a2017-06-09 14:36:40 -07003 *
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.onosproject.dhcprelay;
17
Yi Tseng51f1be92017-09-01 17:24:57 -070018import com.google.common.collect.ImmutableList;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import com.google.common.collect.ImmutableSet;
Yi Tseng525ff402017-10-23 19:39:39 -070020import com.google.common.collect.Streams;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070021import org.onlab.packet.ARP;
22import org.onlab.packet.DHCP;
23import org.onlab.packet.DHCP6;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070024import org.onlab.packet.Ethernet;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.onlab.packet.IPacket;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070026import org.onlab.packet.IPv4;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.onlab.packet.IPv6;
Yi Tseng4f2a0462017-08-31 11:21:00 -070028import org.onlab.packet.Ip4Address;
Kalhee Kimba366062017-11-07 16:32:09 +000029import org.onlab.packet.IpPrefix;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.onlab.packet.MacAddress;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070031import org.onlab.packet.UDP;
32import org.onlab.packet.VlanId;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070033import org.onlab.util.Tools;
34import org.onosproject.cfg.ComponentConfigService;
35import org.onosproject.core.ApplicationId;
36import org.onosproject.core.CoreService;
Yi Tseng51301292017-07-28 13:02:59 -070037import org.onosproject.dhcprelay.api.DhcpHandler;
38import org.onosproject.dhcprelay.api.DhcpRelayService;
Yi Tseng919b2df2017-09-07 16:22:51 -070039import org.onosproject.dhcprelay.api.DhcpServerInfo;
Yi Tseng483ac6f2017-08-02 15:03:31 -070040import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig;
Taras Lemkin96a0d342018-03-26 14:52:58 +000041import org.onosproject.dhcprelay.config.DhcpServerConfig;
Kalhee Kimba366062017-11-07 16:32:09 +000042import org.onosproject.dhcprelay.config.EnableDhcpFpmConfig;
Taras Lemkin96a0d342018-03-26 14:52:58 +000043import org.onosproject.dhcprelay.config.IgnoreDhcpConfig;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig;
45import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070046import org.onosproject.dhcprelay.store.DhcpRecord;
47import org.onosproject.dhcprelay.store.DhcpRelayStore;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048import org.onosproject.net.ConnectPoint;
Yi Tseng127ffe52017-09-12 15:55:17 -070049import org.onosproject.net.Device;
Yi Tseng4f2a0462017-08-31 11:21:00 -070050import org.onosproject.net.Host;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070051import org.onosproject.net.HostId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052import org.onosproject.net.config.Config;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
55import org.onosproject.net.config.NetworkConfigListener;
56import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
59import org.onosproject.net.device.DeviceService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070060import org.onosproject.net.flow.DefaultTrafficSelector;
61import org.onosproject.net.flow.DefaultTrafficTreatment;
62import org.onosproject.net.flow.TrafficSelector;
63import org.onosproject.net.flow.TrafficTreatment;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070064import org.onosproject.net.host.HostService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065import org.onosproject.net.intf.Interface;
66import org.onosproject.net.intf.InterfaceService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070067import org.onosproject.net.packet.DefaultOutboundPacket;
68import org.onosproject.net.packet.OutboundPacket;
69import org.onosproject.net.packet.PacketContext;
70import org.onosproject.net.packet.PacketPriority;
71import org.onosproject.net.packet.PacketProcessor;
72import org.onosproject.net.packet.PacketService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073import org.onosproject.routing.fpm.api.FpmRecord;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070074import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070075import org.osgi.service.component.annotations.Activate;
76import org.osgi.service.component.annotations.Component;
77import org.osgi.service.component.annotations.Deactivate;
78import org.osgi.service.component.annotations.Modified;
79import org.osgi.service.component.annotations.Reference;
80import org.osgi.service.component.annotations.ReferenceCardinality;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070081import org.slf4j.Logger;
82import org.slf4j.LoggerFactory;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083
84import java.nio.ByteBuffer;
85import java.util.Collection;
86import java.util.Collections;
87import java.util.Dictionary;
88import java.util.List;
89import java.util.Objects;
90import java.util.Optional;
91import java.util.Set;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000092import java.util.concurrent.Executors;
Ruchi Sahotae4934e12019-03-01 16:56:07 +000093import java.util.concurrent.ExecutorService;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000094import java.util.concurrent.ScheduledExecutorService;
95import java.util.concurrent.TimeUnit;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070096import java.util.stream.Collectors;
97import java.util.stream.Stream;
98
Kalhee Kimd94ceea2017-11-29 19:03:02 +000099import static org.onlab.util.Tools.groupedThreads;
Ray Milkey687c00c2018-10-31 10:18:41 -0700100import static org.onosproject.dhcprelay.OsgiPropertyConstants.ARP_ENABLED;
101import static org.onosproject.dhcprelay.OsgiPropertyConstants.ARP_ENABLED_DEFAULT;
102import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_FPM_ENABLED;
103import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_FPM_ENABLED_DEFAULT;
104import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_POLL_INTERVAL;
105import static org.onosproject.dhcprelay.OsgiPropertyConstants.DHCP_POLL_INTERVAL_DEFAULT;
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000106import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700107import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
Yi Tseng127ffe52017-09-12 15:55:17 -0700108
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700109/**
110 * DHCP Relay Agent Application Component.
111 */
Ray Milkey687c00c2018-10-31 10:18:41 -0700112@Component(
113 immediate = true,
114 service = DhcpRelayService.class,
115 property = {
116 ARP_ENABLED + ":Boolean=" + ARP_ENABLED_DEFAULT,
117 DHCP_POLL_INTERVAL + ":Integer=" + DHCP_POLL_INTERVAL_DEFAULT,
118 DHCP_FPM_ENABLED + ":Boolean=" + DHCP_FPM_ENABLED_DEFAULT
119 }
120)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700121public class DhcpRelayManager implements DhcpRelayService {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700122 public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay";
Yi Tseng06799d62017-09-01 16:02:56 -0700123 public static final String ROUTE_STORE_IMPL =
124 "org.onosproject.routeservice.store.RouteStoreImpl";
Yi Tseng525ff402017-10-23 19:39:39 -0700125
Yi Tseng51f1be92017-09-01 17:24:57 -0700126 private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder()
127 .matchEthType(Ethernet.TYPE_ARP)
128 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700129 private final Logger log = LoggerFactory.getLogger(getClass());
130 private final InternalConfigListener cfgListener = new InternalConfigListener();
131
132 private final Set<ConfigFactory> factories = ImmutableSet.of(
Yi Tseng483ac6f2017-08-02 15:03:31 -0700133 new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000134 DefaultDhcpRelayConfig.class,
135 DefaultDhcpRelayConfig.KEY,
136 true) {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700137 @Override
Yi Tseng483ac6f2017-08-02 15:03:31 -0700138 public DefaultDhcpRelayConfig createConfig() {
139 return new DefaultDhcpRelayConfig();
140 }
141 },
142 new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000143 IndirectDhcpRelayConfig.class,
144 IndirectDhcpRelayConfig.KEY,
145 true) {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700146 @Override
147 public IndirectDhcpRelayConfig createConfig() {
148 return new IndirectDhcpRelayConfig();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700149 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700150 },
151 new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000152 IgnoreDhcpConfig.class,
153 IgnoreDhcpConfig.KEY,
154 true) {
Yi Tseng51f1be92017-09-01 17:24:57 -0700155 @Override
156 public IgnoreDhcpConfig createConfig() {
157 return new IgnoreDhcpConfig();
158 }
Kalhee Kimba366062017-11-07 16:32:09 +0000159 },
160 new ConfigFactory<ApplicationId, EnableDhcpFpmConfig>(APP_SUBJECT_FACTORY,
161 EnableDhcpFpmConfig.class,
162 EnableDhcpFpmConfig.KEY,
163 false) {
164 @Override
165 public EnableDhcpFpmConfig createConfig() {
166 return new EnableDhcpFpmConfig();
167 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700168 }
169 );
170
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000171
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700172 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700173 protected NetworkConfigRegistry cfgService;
174
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700175 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700176 protected CoreService coreService;
177
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700178 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700179 protected PacketService packetService;
180
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700181 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700182 protected HostService hostService;
183
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700184 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700185 protected InterfaceService interfaceService;
186
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700187 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700188 protected DhcpRelayStore dhcpRelayStore;
189
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700190 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700191 protected ComponentConfigService compCfgService;
192
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700193 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng127ffe52017-09-12 15:55:17 -0700194 protected DeviceService deviceService;
195
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700196 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalhee Kimba366062017-11-07 16:32:09 +0000197 protected DhcpFpmPrefixStore dhcpFpmPrefixStore;
198
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY,
Yi Tseng127ffe52017-09-12 15:55:17 -0700200 target = "(version=4)")
Yi Tseng51301292017-07-28 13:02:59 -0700201 protected DhcpHandler v4Handler;
202
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700203 @Reference(cardinality = ReferenceCardinality.MANDATORY,
Yi Tseng127ffe52017-09-12 15:55:17 -0700204 target = "(version=6)")
Yi Tseng51301292017-07-28 13:02:59 -0700205 protected DhcpHandler v6Handler;
206
Ray Milkey687c00c2018-10-31 10:18:41 -0700207 /** Enable Address resolution protocol. */
208 protected boolean arpEnabled = ARP_ENABLED_DEFAULT;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700209
Ray Milkey687c00c2018-10-31 10:18:41 -0700210 /** dhcp relay poll interval. */
211 protected int dhcpPollInterval = DHCP_POLL_INTERVAL_DEFAULT;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000212
Ray Milkey687c00c2018-10-31 10:18:41 -0700213 /** Enable DhcpRelay Fpm. */
214 protected boolean dhcpFpmEnabled = DHCP_FPM_ENABLED_DEFAULT;
Kalhee Kimba366062017-11-07 16:32:09 +0000215
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000216 private ScheduledExecutorService timerExecutor;
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000217 protected ExecutorService devEventExecutor;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000218
Yi Tseng127ffe52017-09-12 15:55:17 -0700219 protected DeviceListener deviceListener = new InternalDeviceListener();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700220 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700221 private ApplicationId appId;
222
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000223 /**
224 * One second timer.
225 */
226 class Dhcp6Timer implements Runnable {
227 @Override
228 public void run() {
229 v6Handler.timeTick();
230 }
231 };
Kalhee Kimba366062017-11-07 16:32:09 +0000232
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700233 @Activate
234 protected void activate(ComponentContext context) {
235 //start the dhcp relay agent
236 appId = coreService.registerApplication(DHCP_RELAY_APP);
237
238 cfgService.addListener(cfgListener);
239 factories.forEach(cfgService::registerConfigFactory);
240 //update the dhcp server configuration.
241 updateConfig();
Yi Tseng51301292017-07-28 13:02:59 -0700242
243 //add the packet processor
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700244 packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0));
Yi Tseng51301292017-07-28 13:02:59 -0700245
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000246 timerExecutor = Executors.newScheduledThreadPool(1,
247 groupedThreads("dhcpRelay",
248 "config-reloader-%d", log));
249 timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(),
250 0,
251 dhcpPollInterval,
252 TimeUnit.SECONDS);
253
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000254 devEventExecutor = newSingleThreadScheduledExecutor(
255 groupedThreads("onos/dhcprelay-dev-events", "events-%d", log));
256
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700257 modified(context);
258
Yi Tseng06799d62017-09-01 16:02:56 -0700259 // Enable distribute route store
260 compCfgService.preSetProperty(ROUTE_STORE_IMPL,
Kalhee Kimba366062017-11-07 16:32:09 +0000261 "distributed", Boolean.TRUE.toString());
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700262 compCfgService.registerProperties(getClass());
Yi Tseng127ffe52017-09-12 15:55:17 -0700263
264 deviceService.addListener(deviceListener);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000265
266
267
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700268 log.info("DHCP-RELAY Started");
269 }
270
271 @Deactivate
272 protected void deactivate() {
273 cfgService.removeListener(cfgListener);
274 factories.forEach(cfgService::unregisterConfigFactory);
275 packetService.removeProcessor(dhcpRelayPacketProcessor);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700276 cancelArpPackets();
Yi Tsengdfa8ee22017-08-07 12:45:01 -0700277 compCfgService.unregisterProperties(getClass(), false);
Yi Tseng127ffe52017-09-12 15:55:17 -0700278 deviceService.removeListener(deviceListener);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000279 timerExecutor.shutdown();
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000280 devEventExecutor.shutdownNow();
281 devEventExecutor = null;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000282
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700283 log.info("DHCP-RELAY Stopped");
284 }
285
286 @Modified
287 protected void modified(ComponentContext context) {
288 Dictionary<?, ?> properties = context.getProperties();
289 Boolean flag;
290
Ray Milkey687c00c2018-10-31 10:18:41 -0700291 flag = Tools.isPropertyEnabled(properties, ARP_ENABLED);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700292 if (flag != null) {
293 arpEnabled = flag;
294 log.info("Address resolution protocol is {}",
Kalhee Kimba366062017-11-07 16:32:09 +0000295 arpEnabled ? "enabled" : "disabled");
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700296 }
297
298 if (arpEnabled) {
299 requestArpPackets();
300 } else {
301 cancelArpPackets();
302 }
Kalhee Kimba366062017-11-07 16:32:09 +0000303
Ray Milkey687c00c2018-10-31 10:18:41 -0700304 int intervalVal = Tools.getIntegerProperty(properties, DHCP_POLL_INTERVAL);
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000305 log.info("DhcpRelay poll interval new {} old {}", intervalVal, dhcpPollInterval);
306 if (intervalVal != dhcpPollInterval) {
307 timerExecutor.shutdown();
308 dhcpPollInterval = intervalVal;
309 timerExecutor = Executors.newScheduledThreadPool(1,
310 groupedThreads("dhcpRelay",
311 "config-reloader-%d", log));
312 timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(),
313 0,
314 dhcpPollInterval > 1 ? dhcpPollInterval : 1,
315 TimeUnit.SECONDS);
316 v6Handler.setDhcp6PollInterval(dhcpPollInterval);
317 }
318
Ray Milkey687c00c2018-10-31 10:18:41 -0700319 flag = Tools.isPropertyEnabled(properties, DHCP_FPM_ENABLED);
Kalhee Kimba366062017-11-07 16:32:09 +0000320 if (flag != null) {
321 boolean oldValue = dhcpFpmEnabled;
322 dhcpFpmEnabled = flag;
323 log.info("DhcpRelay FPM is {}",
324 dhcpFpmEnabled ? "enabled" : "disabled");
325
326 if (dhcpFpmEnabled && !oldValue) {
327 log.info("Dhcp Fpm is enabled.");
328 processDhcpFpmRoutes(true);
329 }
330 if (!dhcpFpmEnabled && oldValue) {
331 log.info("Dhcp Fpm is disabled.");
332 processDhcpFpmRoutes(false);
333 }
334 v6Handler.setDhcpFpmEnabled(dhcpFpmEnabled);
335 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700336 }
337
Yi Tseng525ff402017-10-23 19:39:39 -0700338 private static List<TrafficSelector> buildClientDhcpSelectors() {
339 return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(),
340 Dhcp6HandlerImpl.DHCP_SELECTORS.stream())
341 .collect(Collectors.toList());
342 }
343
Yi Tseng483ac6f2017-08-02 15:03:31 -0700344 /**
345 * Updates DHCP relay app configuration.
346 */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700347 private void updateConfig() {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700348 DefaultDhcpRelayConfig defaultConfig =
349 cfgService.getConfig(appId, DefaultDhcpRelayConfig.class);
350 IndirectDhcpRelayConfig indirectConfig =
351 cfgService.getConfig(appId, IndirectDhcpRelayConfig.class);
Yi Tseng51f1be92017-09-01 17:24:57 -0700352 IgnoreDhcpConfig ignoreDhcpConfig =
353 cfgService.getConfig(appId, IgnoreDhcpConfig.class);
Yi Tseng483ac6f2017-08-02 15:03:31 -0700354
355 if (defaultConfig != null) {
356 updateConfig(defaultConfig);
357 }
Yi Tseng483ac6f2017-08-02 15:03:31 -0700358 if (indirectConfig != null) {
359 updateConfig(indirectConfig);
360 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700361 if (ignoreDhcpConfig != null) {
362 updateConfig(ignoreDhcpConfig);
363 }
Yi Tseng483ac6f2017-08-02 15:03:31 -0700364 }
365
366 /**
367 * Updates DHCP relay app configuration with given configuration.
368 *
369 * @param config the configuration ot update
370 */
Yi Tseng51f1be92017-09-01 17:24:57 -0700371 protected void updateConfig(Config config) {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700372 if (config instanceof IndirectDhcpRelayConfig) {
373 IndirectDhcpRelayConfig indirectConfig = (IndirectDhcpRelayConfig) config;
374 v4Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs());
375 v6Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs());
Yi Tseng3df7f9d2017-08-17 13:08:31 -0700376 } else if (config instanceof DefaultDhcpRelayConfig) {
377 DefaultDhcpRelayConfig defaultConfig = (DefaultDhcpRelayConfig) config;
378 v4Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs());
379 v6Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs());
Yi Tseng483ac6f2017-08-02 15:03:31 -0700380 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700381 if (config instanceof IgnoreDhcpConfig) {
Yi Tseng525ff402017-10-23 19:39:39 -0700382 v4Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config);
383 v6Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config);
Yi Tseng51f1be92017-09-01 17:24:57 -0700384 }
385 }
386
387 protected void removeConfig(Config config) {
388 if (config instanceof IndirectDhcpRelayConfig) {
389 v4Handler.setIndirectDhcpServerConfigs(Collections.emptyList());
390 v6Handler.setIndirectDhcpServerConfigs(Collections.emptyList());
391 } else if (config instanceof DefaultDhcpRelayConfig) {
392 v4Handler.setDefaultDhcpServerConfigs(Collections.emptyList());
393 v6Handler.setDefaultDhcpServerConfigs(Collections.emptyList());
394 }
395 if (config instanceof IgnoreDhcpConfig) {
Yi Tseng525ff402017-10-23 19:39:39 -0700396 v4Handler.updateIgnoreVlanConfig(null);
397 v6Handler.updateIgnoreVlanConfig(null);
Yi Tseng51f1be92017-09-01 17:24:57 -0700398 }
399 }
400
Kalhee Kimba366062017-11-07 16:32:09 +0000401 private void processDhcpFpmRoutes(Boolean add) {
402 // needs to restore/remove fpm
403 }
404
405 public boolean isDhcpFpmEnabled() {
406 return dhcpFpmEnabled;
407 }
408
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700409 /**
410 * Request ARP packet in via PacketService.
411 */
412 private void requestArpPackets() {
Yi Tseng51f1be92017-09-01 17:24:57 -0700413 packetService.requestPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700414 }
415
416 /**
417 * Cancel requested ARP packets in via packet service.
418 */
419 private void cancelArpPackets() {
Yi Tseng51f1be92017-09-01 17:24:57 -0700420 packetService.cancelPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700421 }
422
423 @Override
424 public Optional<DhcpRecord> getDhcpRecord(HostId hostId) {
425 return dhcpRelayStore.getDhcpRecord(hostId);
426 }
427
428 @Override
429 public Collection<DhcpRecord> getDhcpRecords() {
430 return dhcpRelayStore.getDhcpRecords();
431 }
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000432 @Override
433 public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) {
434 dhcpRelayStore.updateDhcpRecord(hostId, dhcpRecord);
435 }
Yi Tseng13a41a12017-07-26 13:45:01 -0700436 @Override
437 public Optional<MacAddress> getDhcpServerMacAddress() {
Yi Tseng4f2a0462017-08-31 11:21:00 -0700438 // TODO: depreated it
439 DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class);
440 DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0);
441 Ip4Address serverip = serverConfig.getDhcpServerIp4().get();
442 return hostService.getHostsByIp(serverip)
443 .stream()
444 .map(Host::mac)
445 .findFirst();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700446 }
447
Yi Tseng919b2df2017-09-07 16:22:51 -0700448 @Override
449 public List<DhcpServerInfo> getDefaultDhcpServerInfoList() {
450 return ImmutableList.<DhcpServerInfo>builder()
451 .addAll(v4Handler.getDefaultDhcpServerInfoList())
452 .addAll(v6Handler.getDefaultDhcpServerInfoList())
453 .build();
454 }
455
456 @Override
457 public List<DhcpServerInfo> getIndirectDhcpServerInfoList() {
458 return ImmutableList.<DhcpServerInfo>builder()
459 .addAll(v4Handler.getIndirectDhcpServerInfoList())
460 .addAll(v6Handler.getIndirectDhcpServerInfoList())
461 .build();
462 }
463
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700464 /**
465 * Gets DHCP data from a packet.
466 *
467 * @param packet the packet
468 * @return the DHCP data; empty if it is not a DHCP packet
469 */
470 private Optional<DHCP> findDhcp(Ethernet packet) {
471 return Stream.of(packet)
472 .filter(Objects::nonNull)
473 .map(Ethernet::getPayload)
474 .filter(p -> p instanceof IPv4)
475 .map(IPacket::getPayload)
476 .filter(Objects::nonNull)
477 .filter(p -> p instanceof UDP)
478 .map(IPacket::getPayload)
479 .filter(Objects::nonNull)
480 .filter(p -> p instanceof DHCP)
481 .map(p -> (DHCP) p)
482 .findFirst();
483 }
484
485 /**
486 * Gets DHCPv6 data from a packet.
487 *
488 * @param packet the packet
489 * @return the DHCPv6 data; empty if it is not a DHCPv6 packet
490 */
491 private Optional<DHCP6> findDhcp6(Ethernet packet) {
492 return Stream.of(packet)
493 .filter(Objects::nonNull)
494 .map(Ethernet::getPayload)
495 .filter(p -> p instanceof IPv6)
496 .map(IPacket::getPayload)
497 .filter(Objects::nonNull)
498 .filter(p -> p instanceof UDP)
499 .map(IPacket::getPayload)
500 .filter(Objects::nonNull)
501 .filter(p -> p instanceof DHCP6)
502 .map(p -> (DHCP6) p)
503 .findFirst();
504 }
505
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700506
507 private class DhcpRelayPacketProcessor implements PacketProcessor {
508
509 @Override
510 public void process(PacketContext context) {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700511 // process the packet and get the payload
512 Ethernet packet = context.inPacket().parsed();
513 if (packet == null) {
514 return;
515 }
516
517 findDhcp(packet).ifPresent(dhcpPayload -> {
Yi Tseng51301292017-07-28 13:02:59 -0700518 v4Handler.processDhcpPacket(context, dhcpPayload);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700519 });
520
521 findDhcp6(packet).ifPresent(dhcp6Payload -> {
Yi Tseng51301292017-07-28 13:02:59 -0700522 v6Handler.processDhcpPacket(context, dhcp6Payload);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700523 });
524
525 if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) {
526 ARP arpPacket = (ARP) packet.getPayload();
527 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
528 Set<Interface> interfaces = interfaceService.
529 getInterfacesByPort(context.inPacket().receivedFrom());
530 //ignore the packets if dhcp server interface is not configured on onos.
531 if (interfaces.isEmpty()) {
532 log.warn("server virtual interface not configured");
533 return;
534 }
535 if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) {
536 // handle request only
537 return;
538 }
539 MacAddress interfaceMac = interfaces.stream()
540 .filter(iface -> iface.vlan().equals(vlanId))
541 .map(Interface::mac)
542 .filter(mac -> !mac.equals(MacAddress.NONE))
543 .findFirst()
Yi Tseng51301292017-07-28 13:02:59 -0700544 .orElse(MacAddress.ONOS);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700545 if (interfaceMac == null) {
546 // can't find interface mac address
547 return;
548 }
549 processArpPacket(context, packet, interfaceMac);
550 }
551 }
552
553 /**
554 * Processes the ARP Payload and initiates a reply to the client.
555 *
556 * @param context the packet context
557 * @param packet the ethernet payload
558 * @param replyMac mac address to be replied
559 */
560 private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) {
561 ARP arpPacket = (ARP) packet.getPayload();
Ray Milkeyf0c47612017-09-28 11:29:38 -0700562 ARP arpReply = arpPacket.duplicate();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700563 arpReply.setOpCode(ARP.OP_REPLY);
564
565 arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
566 arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
567 arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
568 arpReply.setSenderHardwareAddress(replyMac.toBytes());
569
570 // Ethernet Frame.
571 Ethernet ethReply = new Ethernet();
572 ethReply.setSourceMACAddress(replyMac.toBytes());
573 ethReply.setDestinationMACAddress(packet.getSourceMAC());
574 ethReply.setEtherType(Ethernet.TYPE_ARP);
575 ethReply.setVlanID(packet.getVlanID());
576 ethReply.setPayload(arpReply);
577
578 ConnectPoint targetPort = context.inPacket().receivedFrom();
579 TrafficTreatment t = DefaultTrafficTreatment.builder()
580 .setOutput(targetPort.port()).build();
581 OutboundPacket o = new DefaultOutboundPacket(
582 targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize()));
583 if (log.isTraceEnabled()) {
584 log.trace("Relaying ARP packet {} to {}", packet, targetPort);
585 }
586 packetService.emit(o);
587 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700588 }
589
590 /**
591 * Listener for network config events.
592 */
593 private class InternalConfigListener implements NetworkConfigListener {
594 @Override
595 public void event(NetworkConfigEvent event) {
Yi Tseng51f1be92017-09-01 17:24:57 -0700596 switch (event.type()) {
597 case CONFIG_UPDATED:
598 case CONFIG_ADDED:
599 event.config().ifPresent(config -> {
600 updateConfig(config);
601 log.info("{} updated", config.getClass().getSimpleName());
602 });
603 break;
604 case CONFIG_REMOVED:
605 event.prevConfig().ifPresent(config -> {
606 removeConfig(config);
607 log.info("{} removed", config.getClass().getSimpleName());
608 });
609 break;
Charles Chan10b2fee2018-04-21 00:44:29 -0700610 case CONFIG_REGISTERED:
611 case CONFIG_UNREGISTERED:
612 break;
Yi Tseng51f1be92017-09-01 17:24:57 -0700613 default:
614 log.warn("Unsupported event type {}", event.type());
615 break;
Yi Tseng483ac6f2017-08-02 15:03:31 -0700616 }
Charles Chan50443e82018-01-03 16:26:32 -0800617 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700618
Charles Chan50443e82018-01-03 16:26:32 -0800619 @Override
620 public boolean isRelevant(NetworkConfigEvent event) {
621 if (event.configClass().equals(DefaultDhcpRelayConfig.class) ||
622 event.configClass().equals(IndirectDhcpRelayConfig.class) ||
623 event.configClass().equals(IgnoreDhcpConfig.class)) {
624 return true;
625 }
626 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
627 return false;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700628 }
629 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700630
631 private class InternalDeviceListener implements DeviceListener {
632
633 @Override
634 public void event(DeviceEvent event) {
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000635 if (devEventExecutor != null) {
Yi Tseng127ffe52017-09-12 15:55:17 -0700636 Device device = event.subject();
637 switch (event.type()) {
638 case DEVICE_ADDED:
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000639 devEventExecutor.execute(this::updateIgnoreVlanConfigs);
Yi Tseng127ffe52017-09-12 15:55:17 -0700640 break;
641 case DEVICE_AVAILABILITY_CHANGED:
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000642 devEventExecutor.execute(() -> deviceAvailabilityChanged(device));
Ray Milkeyd6a67c32018-02-02 10:30:35 -0800643 break;
Yi Tseng127ffe52017-09-12 15:55:17 -0700644 default:
645 break;
646 }
Ruchi Sahotae4934e12019-03-01 16:56:07 +0000647 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700648 }
649
650 private void deviceAvailabilityChanged(Device device) {
651 if (deviceService.isAvailable(device.id())) {
Yi Tseng525ff402017-10-23 19:39:39 -0700652 updateIgnoreVlanConfigs();
Saurav Dasb805f1a2017-12-13 16:19:35 -0800653 } else {
654 removeIgnoreVlanState();
Yi Tseng127ffe52017-09-12 15:55:17 -0700655 }
656 }
657
Yi Tseng525ff402017-10-23 19:39:39 -0700658 private void updateIgnoreVlanConfigs() {
Yi Tseng127ffe52017-09-12 15:55:17 -0700659 IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class);
Yi Tseng525ff402017-10-23 19:39:39 -0700660 v4Handler.updateIgnoreVlanConfig(config);
661 v6Handler.updateIgnoreVlanConfig(config);
Yi Tseng127ffe52017-09-12 15:55:17 -0700662 }
Saurav Dasb805f1a2017-12-13 16:19:35 -0800663
664 private void removeIgnoreVlanState() {
665 IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class);
666 v4Handler.removeIgnoreVlanState(config);
667 v6Handler.removeIgnoreVlanState(config);
668 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700669 }
Kalhee Kimba366062017-11-07 16:32:09 +0000670
671
672
673 public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) {
674 return dhcpFpmPrefixStore.getFpmRecord(prefix);
675 }
676
677 public Collection<FpmRecord> getFpmRecords() {
678 return dhcpFpmPrefixStore.getFpmRecords();
679 }
680
681 @Override
682 public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) {
683 dhcpFpmPrefixStore.addFpmRecord(prefix, fpmRecord);
684 }
685
686 @Override
687 public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) {
688 return dhcpFpmPrefixStore.removeFpmRecord(prefix);
689 }
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000690
691
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700692}