blob: 90c58b7e8ba2ee1668e0b7518db7e436c6f5d140 [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
18import java.nio.ByteBuffer;
19import java.util.Collection;
Yi Tseng51f1be92017-09-01 17:24:57 -070020import java.util.Collections;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070021import java.util.Dictionary;
Yi Tseng51f1be92017-09-01 17:24:57 -070022import java.util.List;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070023import java.util.Objects;
24import java.util.Optional;
25import java.util.Set;
Yi Tseng525ff402017-10-23 19:39:39 -070026import java.util.stream.Collectors;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070027import java.util.stream.Stream;
28
Yi Tseng51f1be92017-09-01 17:24:57 -070029import com.google.common.collect.ImmutableList;
Ruchi Sahota196a9ca2019-03-01 16:56:07 +000030import com.google.common.collect.ImmutableSet;
Yi Tseng525ff402017-10-23 19:39:39 -070031import com.google.common.collect.Streams;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070032import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Component;
34import org.apache.felix.scr.annotations.Deactivate;
35import org.apache.felix.scr.annotations.Modified;
36import org.apache.felix.scr.annotations.Property;
37import org.apache.felix.scr.annotations.Reference;
38import org.apache.felix.scr.annotations.ReferenceCardinality;
39import org.apache.felix.scr.annotations.Service;
40import org.onlab.packet.ARP;
41import org.onlab.packet.DHCP;
42import org.onlab.packet.DHCP6;
43import org.onlab.packet.IPacket;
44import org.onlab.packet.IPv6;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070045import org.onlab.packet.Ethernet;
46import org.onlab.packet.IPv4;
Yi Tseng4f2a0462017-08-31 11:21:00 -070047import org.onlab.packet.Ip4Address;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070048import org.onlab.packet.MacAddress;
Kalhee Kimba366062017-11-07 16:32:09 +000049import org.onlab.packet.IpPrefix;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070050import org.onlab.packet.UDP;
51import org.onlab.packet.VlanId;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070052import org.onlab.util.Tools;
53import org.onosproject.cfg.ComponentConfigService;
54import org.onosproject.core.ApplicationId;
55import org.onosproject.core.CoreService;
Yi Tseng51301292017-07-28 13:02:59 -070056import org.onosproject.dhcprelay.api.DhcpHandler;
57import org.onosproject.dhcprelay.api.DhcpRelayService;
Yi Tseng919b2df2017-09-07 16:22:51 -070058import org.onosproject.dhcprelay.api.DhcpServerInfo;
Yi Tseng483ac6f2017-08-02 15:03:31 -070059import org.onosproject.dhcprelay.config.DefaultDhcpRelayConfig;
Taras Lemkin371ac5e2018-03-26 14:52:58 +000060import org.onosproject.dhcprelay.config.DhcpServerConfig;
Kalhee Kimba366062017-11-07 16:32:09 +000061import org.onosproject.dhcprelay.config.EnableDhcpFpmConfig;
Taras Lemkin371ac5e2018-03-26 14:52:58 +000062import org.onosproject.dhcprelay.config.IndirectDhcpRelayConfig;
63import org.onosproject.dhcprelay.config.IgnoreDhcpConfig;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070064import org.onosproject.dhcprelay.store.DhcpRecord;
65import org.onosproject.dhcprelay.store.DhcpRelayStore;
Kalhee Kimba366062017-11-07 16:32:09 +000066import org.onosproject.dhcprelay.store.DhcpFpmPrefixStore;
67import org.onosproject.routing.fpm.api.FpmRecord;
Yi Tseng127ffe52017-09-12 15:55:17 -070068import org.onosproject.net.Device;
Yi Tseng4f2a0462017-08-31 11:21:00 -070069import org.onosproject.net.Host;
Yi Tseng483ac6f2017-08-02 15:03:31 -070070import org.onosproject.net.config.Config;
Yi Tseng127ffe52017-09-12 15:55:17 -070071import org.onosproject.net.device.DeviceEvent;
72import org.onosproject.net.device.DeviceListener;
73import org.onosproject.net.device.DeviceService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070074import org.onosproject.net.intf.Interface;
75import org.onosproject.net.intf.InterfaceService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070076import org.onosproject.net.ConnectPoint;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070077import org.onosproject.net.HostId;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070078import org.onosproject.net.config.ConfigFactory;
79import org.onosproject.net.config.NetworkConfigEvent;
80import org.onosproject.net.config.NetworkConfigListener;
81import org.onosproject.net.config.NetworkConfigRegistry;
82import org.onosproject.net.flow.DefaultTrafficSelector;
83import org.onosproject.net.flow.DefaultTrafficTreatment;
84import org.onosproject.net.flow.TrafficSelector;
85import org.onosproject.net.flow.TrafficTreatment;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070086import org.onosproject.net.host.HostService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070087import org.onosproject.net.packet.DefaultOutboundPacket;
88import org.onosproject.net.packet.OutboundPacket;
89import org.onosproject.net.packet.PacketContext;
90import org.onosproject.net.packet.PacketPriority;
91import org.onosproject.net.packet.PacketProcessor;
92import org.onosproject.net.packet.PacketService;
Kalhee Kimba366062017-11-07 16:32:09 +000093
Yi Tseng7a38f9a2017-06-09 14:36:40 -070094import org.osgi.service.component.ComponentContext;
95import org.slf4j.Logger;
96import org.slf4j.LoggerFactory;
Kalhee Kime5bb7092017-11-29 19:03:02 +000097import java.util.concurrent.Executors;
Ruchi Sahota196a9ca2019-03-01 16:56:07 +000098import java.util.concurrent.ExecutorService;
Kalhee Kime5bb7092017-11-29 19:03:02 +000099import java.util.concurrent.ScheduledExecutorService;
100import java.util.concurrent.TimeUnit;
101import static org.onlab.util.Tools.groupedThreads;
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000102import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700103import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
Yi Tseng127ffe52017-09-12 15:55:17 -0700104
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700105/**
106 * DHCP Relay Agent Application Component.
107 */
108@Component(immediate = true)
109@Service
110public class DhcpRelayManager implements DhcpRelayService {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700111 public static final String DHCP_RELAY_APP = "org.onosproject.dhcprelay";
Yi Tseng06799d62017-09-01 16:02:56 -0700112 public static final String ROUTE_STORE_IMPL =
113 "org.onosproject.routeservice.store.RouteStoreImpl";
Yi Tseng525ff402017-10-23 19:39:39 -0700114
Yi Tseng51f1be92017-09-01 17:24:57 -0700115 private static final TrafficSelector ARP_SELECTOR = DefaultTrafficSelector.builder()
116 .matchEthType(Ethernet.TYPE_ARP)
117 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700118 private final Logger log = LoggerFactory.getLogger(getClass());
119 private final InternalConfigListener cfgListener = new InternalConfigListener();
120
121 private final Set<ConfigFactory> factories = ImmutableSet.of(
Yi Tseng483ac6f2017-08-02 15:03:31 -0700122 new ConfigFactory<ApplicationId, DefaultDhcpRelayConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000123 DefaultDhcpRelayConfig.class,
124 DefaultDhcpRelayConfig.KEY,
125 true) {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700126 @Override
Yi Tseng483ac6f2017-08-02 15:03:31 -0700127 public DefaultDhcpRelayConfig createConfig() {
128 return new DefaultDhcpRelayConfig();
129 }
130 },
131 new ConfigFactory<ApplicationId, IndirectDhcpRelayConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000132 IndirectDhcpRelayConfig.class,
133 IndirectDhcpRelayConfig.KEY,
134 true) {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700135 @Override
136 public IndirectDhcpRelayConfig createConfig() {
137 return new IndirectDhcpRelayConfig();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700138 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700139 },
140 new ConfigFactory<ApplicationId, IgnoreDhcpConfig>(APP_SUBJECT_FACTORY,
Kalhee Kimba366062017-11-07 16:32:09 +0000141 IgnoreDhcpConfig.class,
142 IgnoreDhcpConfig.KEY,
143 true) {
Yi Tseng51f1be92017-09-01 17:24:57 -0700144 @Override
145 public IgnoreDhcpConfig createConfig() {
146 return new IgnoreDhcpConfig();
147 }
Kalhee Kimba366062017-11-07 16:32:09 +0000148 },
149 new ConfigFactory<ApplicationId, EnableDhcpFpmConfig>(APP_SUBJECT_FACTORY,
150 EnableDhcpFpmConfig.class,
151 EnableDhcpFpmConfig.KEY,
152 false) {
153 @Override
154 public EnableDhcpFpmConfig createConfig() {
155 return new EnableDhcpFpmConfig();
156 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700157 }
158 );
159
Kalhee Kime5bb7092017-11-29 19:03:02 +0000160
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
162 protected NetworkConfigRegistry cfgService;
163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
165 protected CoreService coreService;
166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
168 protected PacketService packetService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
171 protected HostService hostService;
172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700174 protected InterfaceService interfaceService;
175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
177 protected DhcpRelayStore dhcpRelayStore;
178
179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
180 protected ComponentConfigService compCfgService;
181
Yi Tseng51f1be92017-09-01 17:24:57 -0700182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng127ffe52017-09-12 15:55:17 -0700183 protected DeviceService deviceService;
184
Kalhee Kimba366062017-11-07 16:32:09 +0000185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
186 protected DhcpFpmPrefixStore dhcpFpmPrefixStore;
187
Yi Tseng51301292017-07-28 13:02:59 -0700188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY,
Yi Tseng127ffe52017-09-12 15:55:17 -0700189 target = "(version=4)")
Yi Tseng51301292017-07-28 13:02:59 -0700190 protected DhcpHandler v4Handler;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY,
Yi Tseng127ffe52017-09-12 15:55:17 -0700193 target = "(version=6)")
Yi Tseng51301292017-07-28 13:02:59 -0700194 protected DhcpHandler v6Handler;
195
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700196 @Property(name = "arpEnabled", boolValue = true,
Yi Tseng127ffe52017-09-12 15:55:17 -0700197 label = "Enable Address resolution protocol")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700198 protected boolean arpEnabled = true;
199
Kalhee Kime5bb7092017-11-29 19:03:02 +0000200 @Property(name = "dhcpPollInterval", intValue = 24 * 3600,
201 label = "dhcp relay poll interval")
202 protected int dhcpPollInterval = 24 * 3600;
203
Kalhee Kimba366062017-11-07 16:32:09 +0000204 @Property(name = "dhcpFpmEnabled", boolValue = false,
205 label = "Enable DhcpRelay Fpm")
206 protected boolean dhcpFpmEnabled = false;
207
Kalhee Kime5bb7092017-11-29 19:03:02 +0000208 private ScheduledExecutorService timerExecutor;
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000209 protected ExecutorService devEventExecutor;
Kalhee Kime5bb7092017-11-29 19:03:02 +0000210
Yi Tseng127ffe52017-09-12 15:55:17 -0700211 protected DeviceListener deviceListener = new InternalDeviceListener();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700212 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700213 private ApplicationId appId;
214
Kalhee Kime5bb7092017-11-29 19:03:02 +0000215 /**
216 * One second timer.
217 */
218 class Dhcp6Timer implements Runnable {
219 @Override
220 public void run() {
221 v6Handler.timeTick();
222 }
223 };
Kalhee Kimba366062017-11-07 16:32:09 +0000224
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700225 @Activate
226 protected void activate(ComponentContext context) {
227 //start the dhcp relay agent
228 appId = coreService.registerApplication(DHCP_RELAY_APP);
229
230 cfgService.addListener(cfgListener);
231 factories.forEach(cfgService::registerConfigFactory);
232 //update the dhcp server configuration.
233 updateConfig();
Yi Tseng51301292017-07-28 13:02:59 -0700234
235 //add the packet processor
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700236 packetService.addProcessor(dhcpRelayPacketProcessor, PacketProcessor.director(0));
Yi Tseng51301292017-07-28 13:02:59 -0700237
Kalhee Kime5bb7092017-11-29 19:03:02 +0000238 timerExecutor = Executors.newScheduledThreadPool(1,
239 groupedThreads("dhcpRelay",
240 "config-reloader-%d", log));
241 timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(),
242 0,
243 dhcpPollInterval,
244 TimeUnit.SECONDS);
245
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000246 devEventExecutor = newSingleThreadScheduledExecutor(
247 groupedThreads("onos/dhcprelay-dev-events", "events-%d", log));
248
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700249 modified(context);
250
Yi Tseng06799d62017-09-01 16:02:56 -0700251 // Enable distribute route store
252 compCfgService.preSetProperty(ROUTE_STORE_IMPL,
Kalhee Kimba366062017-11-07 16:32:09 +0000253 "distributed", Boolean.TRUE.toString());
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700254 compCfgService.registerProperties(getClass());
Yi Tseng127ffe52017-09-12 15:55:17 -0700255
256 deviceService.addListener(deviceListener);
Kalhee Kime5bb7092017-11-29 19:03:02 +0000257
258
259
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700260 log.info("DHCP-RELAY Started");
261 }
262
263 @Deactivate
264 protected void deactivate() {
265 cfgService.removeListener(cfgListener);
266 factories.forEach(cfgService::unregisterConfigFactory);
267 packetService.removeProcessor(dhcpRelayPacketProcessor);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700268 cancelArpPackets();
Yi Tsengdfa8ee22017-08-07 12:45:01 -0700269 compCfgService.unregisterProperties(getClass(), false);
Yi Tseng127ffe52017-09-12 15:55:17 -0700270 deviceService.removeListener(deviceListener);
Kalhee Kime5bb7092017-11-29 19:03:02 +0000271 timerExecutor.shutdown();
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000272 devEventExecutor.shutdownNow();
273 devEventExecutor = null;
Kalhee Kime5bb7092017-11-29 19:03:02 +0000274
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700275 log.info("DHCP-RELAY Stopped");
276 }
277
278 @Modified
279 protected void modified(ComponentContext context) {
280 Dictionary<?, ?> properties = context.getProperties();
281 Boolean flag;
282
283 flag = Tools.isPropertyEnabled(properties, "arpEnabled");
284 if (flag != null) {
285 arpEnabled = flag;
286 log.info("Address resolution protocol is {}",
Kalhee Kimba366062017-11-07 16:32:09 +0000287 arpEnabled ? "enabled" : "disabled");
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700288 }
289
290 if (arpEnabled) {
291 requestArpPackets();
292 } else {
293 cancelArpPackets();
294 }
Kalhee Kimba366062017-11-07 16:32:09 +0000295
Kalhee Kime5bb7092017-11-29 19:03:02 +0000296 int intervalVal = Tools.getIntegerProperty(properties, "dhcpPollInterval");
297 log.info("DhcpRelay poll interval new {} old {}", intervalVal, dhcpPollInterval);
298 if (intervalVal != dhcpPollInterval) {
299 timerExecutor.shutdown();
300 dhcpPollInterval = intervalVal;
301 timerExecutor = Executors.newScheduledThreadPool(1,
302 groupedThreads("dhcpRelay",
303 "config-reloader-%d", log));
304 timerExecutor.scheduleAtFixedRate(new Dhcp6Timer(),
305 0,
306 dhcpPollInterval > 1 ? dhcpPollInterval : 1,
307 TimeUnit.SECONDS);
308 v6Handler.setDhcp6PollInterval(dhcpPollInterval);
309 }
310
Kalhee Kimba366062017-11-07 16:32:09 +0000311 flag = Tools.isPropertyEnabled(properties, "dhcpFpmEnabled");
312 if (flag != null) {
313 boolean oldValue = dhcpFpmEnabled;
314 dhcpFpmEnabled = flag;
315 log.info("DhcpRelay FPM is {}",
316 dhcpFpmEnabled ? "enabled" : "disabled");
317
318 if (dhcpFpmEnabled && !oldValue) {
319 log.info("Dhcp Fpm is enabled.");
320 processDhcpFpmRoutes(true);
321 }
322 if (!dhcpFpmEnabled && oldValue) {
323 log.info("Dhcp Fpm is disabled.");
324 processDhcpFpmRoutes(false);
325 }
326 v6Handler.setDhcpFpmEnabled(dhcpFpmEnabled);
327 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700328 }
329
Yi Tseng525ff402017-10-23 19:39:39 -0700330 private static List<TrafficSelector> buildClientDhcpSelectors() {
331 return Streams.concat(Dhcp4HandlerImpl.DHCP_SELECTORS.stream(),
332 Dhcp6HandlerImpl.DHCP_SELECTORS.stream())
333 .collect(Collectors.toList());
334 }
335
Yi Tseng483ac6f2017-08-02 15:03:31 -0700336 /**
337 * Updates DHCP relay app configuration.
338 */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700339 private void updateConfig() {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700340 DefaultDhcpRelayConfig defaultConfig =
341 cfgService.getConfig(appId, DefaultDhcpRelayConfig.class);
342 IndirectDhcpRelayConfig indirectConfig =
343 cfgService.getConfig(appId, IndirectDhcpRelayConfig.class);
Yi Tseng51f1be92017-09-01 17:24:57 -0700344 IgnoreDhcpConfig ignoreDhcpConfig =
345 cfgService.getConfig(appId, IgnoreDhcpConfig.class);
Yi Tseng483ac6f2017-08-02 15:03:31 -0700346
347 if (defaultConfig != null) {
348 updateConfig(defaultConfig);
349 }
Yi Tseng483ac6f2017-08-02 15:03:31 -0700350 if (indirectConfig != null) {
351 updateConfig(indirectConfig);
352 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700353 if (ignoreDhcpConfig != null) {
354 updateConfig(ignoreDhcpConfig);
355 }
Yi Tseng483ac6f2017-08-02 15:03:31 -0700356 }
357
358 /**
359 * Updates DHCP relay app configuration with given configuration.
360 *
361 * @param config the configuration ot update
362 */
Yi Tseng51f1be92017-09-01 17:24:57 -0700363 protected void updateConfig(Config config) {
Yi Tseng483ac6f2017-08-02 15:03:31 -0700364 if (config instanceof IndirectDhcpRelayConfig) {
365 IndirectDhcpRelayConfig indirectConfig = (IndirectDhcpRelayConfig) config;
366 v4Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs());
367 v6Handler.setIndirectDhcpServerConfigs(indirectConfig.dhcpServerConfigs());
Yi Tseng3df7f9d2017-08-17 13:08:31 -0700368 } else if (config instanceof DefaultDhcpRelayConfig) {
369 DefaultDhcpRelayConfig defaultConfig = (DefaultDhcpRelayConfig) config;
370 v4Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs());
371 v6Handler.setDefaultDhcpServerConfigs(defaultConfig.dhcpServerConfigs());
Yi Tseng483ac6f2017-08-02 15:03:31 -0700372 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700373 if (config instanceof IgnoreDhcpConfig) {
Yi Tseng525ff402017-10-23 19:39:39 -0700374 v4Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config);
375 v6Handler.updateIgnoreVlanConfig((IgnoreDhcpConfig) config);
Yi Tseng51f1be92017-09-01 17:24:57 -0700376 }
377 }
378
379 protected void removeConfig(Config config) {
380 if (config instanceof IndirectDhcpRelayConfig) {
381 v4Handler.setIndirectDhcpServerConfigs(Collections.emptyList());
382 v6Handler.setIndirectDhcpServerConfigs(Collections.emptyList());
383 } else if (config instanceof DefaultDhcpRelayConfig) {
384 v4Handler.setDefaultDhcpServerConfigs(Collections.emptyList());
385 v6Handler.setDefaultDhcpServerConfigs(Collections.emptyList());
386 }
387 if (config instanceof IgnoreDhcpConfig) {
Yi Tseng525ff402017-10-23 19:39:39 -0700388 v4Handler.updateIgnoreVlanConfig(null);
389 v6Handler.updateIgnoreVlanConfig(null);
Yi Tseng51f1be92017-09-01 17:24:57 -0700390 }
391 }
392
Kalhee Kimba366062017-11-07 16:32:09 +0000393 private void processDhcpFpmRoutes(Boolean add) {
394 // needs to restore/remove fpm
395 }
396
397 public boolean isDhcpFpmEnabled() {
398 return dhcpFpmEnabled;
399 }
400
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700401 /**
402 * Request ARP packet in via PacketService.
403 */
404 private void requestArpPackets() {
Yi Tseng51f1be92017-09-01 17:24:57 -0700405 packetService.requestPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700406 }
407
408 /**
409 * Cancel requested ARP packets in via packet service.
410 */
411 private void cancelArpPackets() {
Yi Tseng51f1be92017-09-01 17:24:57 -0700412 packetService.cancelPackets(ARP_SELECTOR, PacketPriority.CONTROL, appId);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700413 }
414
415 @Override
416 public Optional<DhcpRecord> getDhcpRecord(HostId hostId) {
417 return dhcpRelayStore.getDhcpRecord(hostId);
418 }
419
420 @Override
421 public Collection<DhcpRecord> getDhcpRecords() {
422 return dhcpRelayStore.getDhcpRecords();
423 }
Kalhee Kime5bb7092017-11-29 19:03:02 +0000424 @Override
425 public void updateDhcpRecord(HostId hostId, DhcpRecord dhcpRecord) {
426 dhcpRelayStore.updateDhcpRecord(hostId, dhcpRecord);
427 }
Yi Tseng13a41a12017-07-26 13:45:01 -0700428 @Override
429 public Optional<MacAddress> getDhcpServerMacAddress() {
Yi Tseng4f2a0462017-08-31 11:21:00 -0700430 // TODO: depreated it
431 DefaultDhcpRelayConfig config = cfgService.getConfig(appId, DefaultDhcpRelayConfig.class);
432 DhcpServerConfig serverConfig = config.dhcpServerConfigs().get(0);
433 Ip4Address serverip = serverConfig.getDhcpServerIp4().get();
434 return hostService.getHostsByIp(serverip)
435 .stream()
436 .map(Host::mac)
437 .findFirst();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700438 }
439
Yi Tseng919b2df2017-09-07 16:22:51 -0700440 @Override
441 public List<DhcpServerInfo> getDefaultDhcpServerInfoList() {
442 return ImmutableList.<DhcpServerInfo>builder()
443 .addAll(v4Handler.getDefaultDhcpServerInfoList())
444 .addAll(v6Handler.getDefaultDhcpServerInfoList())
445 .build();
446 }
447
448 @Override
449 public List<DhcpServerInfo> getIndirectDhcpServerInfoList() {
450 return ImmutableList.<DhcpServerInfo>builder()
451 .addAll(v4Handler.getIndirectDhcpServerInfoList())
452 .addAll(v6Handler.getIndirectDhcpServerInfoList())
453 .build();
454 }
455
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700456 /**
457 * Gets DHCP data from a packet.
458 *
459 * @param packet the packet
460 * @return the DHCP data; empty if it is not a DHCP packet
461 */
462 private Optional<DHCP> findDhcp(Ethernet packet) {
463 return Stream.of(packet)
464 .filter(Objects::nonNull)
465 .map(Ethernet::getPayload)
466 .filter(p -> p instanceof IPv4)
467 .map(IPacket::getPayload)
468 .filter(Objects::nonNull)
469 .filter(p -> p instanceof UDP)
470 .map(IPacket::getPayload)
471 .filter(Objects::nonNull)
472 .filter(p -> p instanceof DHCP)
473 .map(p -> (DHCP) p)
474 .findFirst();
475 }
476
477 /**
478 * Gets DHCPv6 data from a packet.
479 *
480 * @param packet the packet
481 * @return the DHCPv6 data; empty if it is not a DHCPv6 packet
482 */
483 private Optional<DHCP6> findDhcp6(Ethernet packet) {
484 return Stream.of(packet)
485 .filter(Objects::nonNull)
486 .map(Ethernet::getPayload)
487 .filter(p -> p instanceof IPv6)
488 .map(IPacket::getPayload)
489 .filter(Objects::nonNull)
490 .filter(p -> p instanceof UDP)
491 .map(IPacket::getPayload)
492 .filter(Objects::nonNull)
493 .filter(p -> p instanceof DHCP6)
494 .map(p -> (DHCP6) p)
495 .findFirst();
496 }
497
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700498
499 private class DhcpRelayPacketProcessor implements PacketProcessor {
500
501 @Override
502 public void process(PacketContext context) {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700503 // process the packet and get the payload
504 Ethernet packet = context.inPacket().parsed();
505 if (packet == null) {
506 return;
507 }
508
509 findDhcp(packet).ifPresent(dhcpPayload -> {
Yi Tseng51301292017-07-28 13:02:59 -0700510 v4Handler.processDhcpPacket(context, dhcpPayload);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700511 });
512
513 findDhcp6(packet).ifPresent(dhcp6Payload -> {
Yi Tseng51301292017-07-28 13:02:59 -0700514 v6Handler.processDhcpPacket(context, dhcp6Payload);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700515 });
516
517 if (packet.getEtherType() == Ethernet.TYPE_ARP && arpEnabled) {
518 ARP arpPacket = (ARP) packet.getPayload();
519 VlanId vlanId = VlanId.vlanId(packet.getVlanID());
520 Set<Interface> interfaces = interfaceService.
521 getInterfacesByPort(context.inPacket().receivedFrom());
522 //ignore the packets if dhcp server interface is not configured on onos.
523 if (interfaces.isEmpty()) {
524 log.warn("server virtual interface not configured");
525 return;
526 }
527 if ((arpPacket.getOpCode() != ARP.OP_REQUEST)) {
528 // handle request only
529 return;
530 }
531 MacAddress interfaceMac = interfaces.stream()
532 .filter(iface -> iface.vlan().equals(vlanId))
533 .map(Interface::mac)
534 .filter(mac -> !mac.equals(MacAddress.NONE))
535 .findFirst()
Yi Tseng51301292017-07-28 13:02:59 -0700536 .orElse(MacAddress.ONOS);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700537 if (interfaceMac == null) {
538 // can't find interface mac address
539 return;
540 }
541 processArpPacket(context, packet, interfaceMac);
542 }
543 }
544
545 /**
546 * Processes the ARP Payload and initiates a reply to the client.
547 *
548 * @param context the packet context
549 * @param packet the ethernet payload
550 * @param replyMac mac address to be replied
551 */
552 private void processArpPacket(PacketContext context, Ethernet packet, MacAddress replyMac) {
553 ARP arpPacket = (ARP) packet.getPayload();
Ray Milkeyf0c47612017-09-28 11:29:38 -0700554 ARP arpReply = arpPacket.duplicate();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700555 arpReply.setOpCode(ARP.OP_REPLY);
556
557 arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
558 arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
559 arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
560 arpReply.setSenderHardwareAddress(replyMac.toBytes());
561
562 // Ethernet Frame.
563 Ethernet ethReply = new Ethernet();
564 ethReply.setSourceMACAddress(replyMac.toBytes());
565 ethReply.setDestinationMACAddress(packet.getSourceMAC());
566 ethReply.setEtherType(Ethernet.TYPE_ARP);
567 ethReply.setVlanID(packet.getVlanID());
568 ethReply.setPayload(arpReply);
569
570 ConnectPoint targetPort = context.inPacket().receivedFrom();
571 TrafficTreatment t = DefaultTrafficTreatment.builder()
572 .setOutput(targetPort.port()).build();
573 OutboundPacket o = new DefaultOutboundPacket(
574 targetPort.deviceId(), t, ByteBuffer.wrap(ethReply.serialize()));
575 if (log.isTraceEnabled()) {
576 log.trace("Relaying ARP packet {} to {}", packet, targetPort);
577 }
578 packetService.emit(o);
579 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700580 }
581
582 /**
583 * Listener for network config events.
584 */
585 private class InternalConfigListener implements NetworkConfigListener {
586 @Override
587 public void event(NetworkConfigEvent event) {
Yi Tseng51f1be92017-09-01 17:24:57 -0700588 switch (event.type()) {
589 case CONFIG_UPDATED:
590 case CONFIG_ADDED:
591 event.config().ifPresent(config -> {
592 updateConfig(config);
593 log.info("{} updated", config.getClass().getSimpleName());
594 });
595 break;
596 case CONFIG_REMOVED:
597 event.prevConfig().ifPresent(config -> {
598 removeConfig(config);
599 log.info("{} removed", config.getClass().getSimpleName());
600 });
601 break;
Charles Chan6be77d82018-04-21 00:44:29 -0700602 case CONFIG_REGISTERED:
603 case CONFIG_UNREGISTERED:
604 break;
Yi Tseng51f1be92017-09-01 17:24:57 -0700605 default:
606 log.warn("Unsupported event type {}", event.type());
607 break;
Yi Tseng483ac6f2017-08-02 15:03:31 -0700608 }
Charles Chan285cb772018-01-03 16:26:32 -0800609 }
Yi Tseng51f1be92017-09-01 17:24:57 -0700610
Charles Chan285cb772018-01-03 16:26:32 -0800611 @Override
612 public boolean isRelevant(NetworkConfigEvent event) {
613 if (event.configClass().equals(DefaultDhcpRelayConfig.class) ||
614 event.configClass().equals(IndirectDhcpRelayConfig.class) ||
615 event.configClass().equals(IgnoreDhcpConfig.class)) {
616 return true;
617 }
618 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
619 return false;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700620 }
621 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700622
623 private class InternalDeviceListener implements DeviceListener {
624
625 @Override
626 public void event(DeviceEvent event) {
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000627 if (devEventExecutor != null) {
Yi Tseng127ffe52017-09-12 15:55:17 -0700628 Device device = event.subject();
629 switch (event.type()) {
630 case DEVICE_ADDED:
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000631 devEventExecutor.execute(this::updateIgnoreVlanConfigs);
Yi Tseng127ffe52017-09-12 15:55:17 -0700632 break;
633 case DEVICE_AVAILABILITY_CHANGED:
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000634 devEventExecutor.execute(() -> deviceAvailabilityChanged(device));
635 break;
Yi Tseng127ffe52017-09-12 15:55:17 -0700636 default:
637 break;
638 }
Ruchi Sahota196a9ca2019-03-01 16:56:07 +0000639 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700640 }
641
642 private void deviceAvailabilityChanged(Device device) {
643 if (deviceService.isAvailable(device.id())) {
Yi Tseng525ff402017-10-23 19:39:39 -0700644 updateIgnoreVlanConfigs();
Saurav Dasfa9004b2017-12-13 16:19:35 -0800645 } else {
646 removeIgnoreVlanState();
Yi Tseng127ffe52017-09-12 15:55:17 -0700647 }
648 }
649
Yi Tseng525ff402017-10-23 19:39:39 -0700650 private void updateIgnoreVlanConfigs() {
Yi Tseng127ffe52017-09-12 15:55:17 -0700651 IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class);
Yi Tseng525ff402017-10-23 19:39:39 -0700652 v4Handler.updateIgnoreVlanConfig(config);
653 v6Handler.updateIgnoreVlanConfig(config);
Yi Tseng127ffe52017-09-12 15:55:17 -0700654 }
Saurav Dasfa9004b2017-12-13 16:19:35 -0800655
656 private void removeIgnoreVlanState() {
657 IgnoreDhcpConfig config = cfgService.getConfig(appId, IgnoreDhcpConfig.class);
658 v4Handler.removeIgnoreVlanState(config);
659 v6Handler.removeIgnoreVlanState(config);
660 }
Yi Tseng127ffe52017-09-12 15:55:17 -0700661 }
Kalhee Kimba366062017-11-07 16:32:09 +0000662
663
664
665 public Optional<FpmRecord> getFpmRecord(IpPrefix prefix) {
666 return dhcpFpmPrefixStore.getFpmRecord(prefix);
667 }
668
669 public Collection<FpmRecord> getFpmRecords() {
670 return dhcpFpmPrefixStore.getFpmRecords();
671 }
672
673 @Override
674 public void addFpmRecord(IpPrefix prefix, FpmRecord fpmRecord) {
675 dhcpFpmPrefixStore.addFpmRecord(prefix, fpmRecord);
676 }
677
678 @Override
679 public Optional<FpmRecord> removeFpmRecord(IpPrefix prefix) {
680 return dhcpFpmPrefixStore.removeFpmRecord(prefix);
681 }
Kalhee Kime5bb7092017-11-29 19:03:02 +0000682
683
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700684}