blob: 2e4b412efa364eb046475327d12d1024f1d2faef [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabibe1cf87d2014-10-17 09:23:50 -070017
Charles Chan35a32322017-08-14 11:42:11 -070018import com.google.common.collect.Sets;
Jonathan Harte8600eb2015-01-12 10:30:45 -080019import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070020import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080021import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070022import org.onlab.packet.DHCP6;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070023import org.onlab.packet.EthType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080024import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080025import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070026import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080027import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080028import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070029import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070030import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080031import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080032import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080033import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070035import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
36import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
37import org.onlab.packet.dhcp.Dhcp6IaNaOption;
38import org.onlab.packet.dhcp.Dhcp6IaTaOption;
39import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080040import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080041import org.onlab.packet.ndp.NeighborAdvertisement;
42import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080043import org.onlab.packet.ndp.RouterAdvertisement;
44import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080045import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070046import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080047import org.onosproject.core.ApplicationId;
48import org.onosproject.core.CoreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049import org.onosproject.net.ConnectPoint;
50import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
52import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
54import org.onosproject.net.HostLocation;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -070055import org.onosproject.net.config.ConfigFactory;
56import org.onosproject.net.config.NetworkConfigEvent;
57import org.onosproject.net.config.NetworkConfigListener;
58import org.onosproject.net.config.NetworkConfigRegistry;
59import org.onosproject.net.config.NetworkConfigService;
60import org.onosproject.net.config.basics.BasicHostConfig;
61import org.onosproject.net.config.basics.HostLearningConfig;
62import org.onosproject.net.config.basics.SubjectFactories;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.device.DeviceEvent;
64import org.onosproject.net.device.DeviceListener;
65import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080066import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070067import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080068import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070069import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.host.DefaultHostDescription;
71import org.onosproject.net.host.HostDescription;
72import org.onosproject.net.host.HostProvider;
73import org.onosproject.net.host.HostProviderRegistry;
74import org.onosproject.net.host.HostProviderService;
75import org.onosproject.net.host.HostService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076import org.onosproject.net.intf.InterfaceService;
sdn94b00152016-08-30 02:12:32 -070077import org.onosproject.net.packet.DefaultOutboundPacket;
78import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080080import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.net.packet.PacketProcessor;
82import org.onosproject.net.packet.PacketService;
83import org.onosproject.net.provider.AbstractProvider;
84import org.onosproject.net.provider.ProviderId;
85import org.onosproject.net.topology.Topology;
86import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080087import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088import org.osgi.service.component.annotations.Activate;
89import org.osgi.service.component.annotations.Component;
90import org.osgi.service.component.annotations.Deactivate;
91import org.osgi.service.component.annotations.Modified;
92import org.osgi.service.component.annotations.Reference;
93import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibe1cf87d2014-10-17 09:23:50 -070094import org.slf4j.Logger;
95
sdn94b00152016-08-30 02:12:32 -070096import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070097import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070098import java.util.Objects;
99import java.util.Optional;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100import java.util.Set;
Madan Jampania3770c32015-12-11 12:07:41 -0800101import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700102import java.util.stream.Stream;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700103
Madan Jampania3770c32015-12-11 12:07:41 -0800104import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
105import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700106import static org.onosproject.provider.host.impl.OsgiPropertyConstants.*;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700107import static org.slf4j.LoggerFactory.getLogger;
108
alshabibe1cf87d2014-10-17 09:23:50 -0700109/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700110 * Provider which uses an OpenFlow controller to detect network end-station
111 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700112 */
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700113@Component(immediate = true, service = HostProvider.class,
114 property = {
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700115 HOST_REMOVAL_ENABLED + ":Boolean=" + HOST_REMOVAL_ENABLED_DEFAULT,
116 REQUEST_ARP + ":Boolean=" + REQUEST_ARP_DEFAULT,
117 REQUEST_NDP + ":Boolean=" + REQUEST_NDP_DEFAULT,
118 USE_DHCP + ":Boolean=" + USE_DHCP_DEFAULT,
119 USE_DHCP6 + ":Boolean=" + USE_DHCP6_DEFAULT,
120 REQUEST_INTERCEPTS_ENABLED + ":Boolean=" + REQUEST_INTERCEPTS_ENABLED_DEFAULT,
121 MULTIHOMING_ENABLED + ":Boolean=" + MULTIHOMING_ENABLED_DEFAULT,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700122 })
Charles Chanff79dd92018-06-01 16:33:48 -0700123public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700124 private final Logger log = getLogger(getClass());
125
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700126 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800127 protected CoreService coreService;
128
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700129 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700130 protected HostProviderRegistry providerRegistry;
131
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700132 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800133 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700134
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700135 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700136 protected TopologyService topologyService;
137
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800139 protected HostService hostService;
140
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800142 protected DeviceService deviceService;
143
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700144 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700145 protected ComponentConfigService cfgService;
146
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700147 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700148 protected NetworkConfigRegistry registry;
149
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700150 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700151 protected InterfaceService interfaceService;
152
alshabibe1cf87d2014-10-17 09:23:50 -0700153 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800154 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700155 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800156
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800157 private ApplicationId appId;
158
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700159 /** Enable host removal on port/device down events. */
Thomas Vachuska33601602014-11-19 03:32:15 -0800160 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700161
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700162 /** Request ARP packets for neighbor discovery by the Host Location Provider; default is true. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700163 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800164
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700165 /** Requests IPv6 Neighbor Discovery by the Host Location Provider; default is false. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700166 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800167
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700168 /** Use DHCP to update IP address of the host; default is false. */
Charles Chane6067892016-11-17 10:23:46 -0800169 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700170
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700171 /** Use DHCPv6 to update IP address of the host; default is false. */
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700172 private boolean useDhcp6 = false;
173
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700174 /** Enable requesting packet intercepts. */
Jonathan Hart9af322d2016-01-06 17:42:04 -0800175 private boolean requestInterceptsEnabled = true;
176
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700177 /** Allow hosts to be multihomed. */
Charles Chan35a32322017-08-14 11:42:11 -0700178 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800179
Charles Chanff79dd92018-06-01 16:33:48 -0700180 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800181
Charles Chanff79dd92018-06-01 16:33:48 -0700182 ExecutorService deviceEventHandler;
183 private ExecutorService probeEventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700184 private ExecutorService packetHandler;
Charles Chan8277b6b2017-12-03 13:48:46 -0800185
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700186 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700187 protected NetworkConfigService netcfgService;
188
189 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
190 new ConfigFactory<ConnectPoint, HostLearningConfig>(
191 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
192 HostLearningConfig.class, "hostLearning") {
193 @Override
194 public HostLearningConfig createConfig() {
195 return new HostLearningConfig();
196 }
197 };
198
alshabibe1cf87d2014-10-17 09:23:50 -0700199 /**
200 * Creates an OpenFlow host provider.
201 */
202 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800203 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700204 }
205
206 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800207 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700208 cfgService.registerProperties(getClass());
209 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700210 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
211 "device-event-handler", log));
212 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
213 "probe-event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700214 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
215 "packet-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700216 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700217 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800218 deviceService.addListener(deviceListener);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700219 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800220 modified(context);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700221 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800222
Charles M.C. Chane148de82015-05-06 12:38:21 +0800223 log.info("Started with Application ID {}", appId.id());
224 }
225
226 @Deactivate
227 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800228 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700229
230 withdrawIntercepts();
231
Charles M.C. Chane148de82015-05-06 12:38:21 +0800232 providerRegistry.unregister(this);
233 packetService.removeProcessor(processor);
234 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700235 deviceEventHandler.shutdown();
236 probeEventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700237 packetHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800238 providerService = null;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700239 registry.unregisterConfigFactory(hostLearningConfig);
240 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800241 log.info("Stopped");
242 }
243
244 @Modified
245 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800246 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800247
248 if (requestInterceptsEnabled) {
249 requestIntercepts();
250 } else {
251 withdrawIntercepts();
252 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800253 }
254
255 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700256 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800257 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700258 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800259 // Use ARP
260 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
261 .matchEthType(Ethernet.TYPE_ARP)
262 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700263 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800264 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700265 } else {
Charles Chane6067892016-11-17 10:23:46 -0800266 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800267 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700268
Charles Chane6067892016-11-17 10:23:46 -0800269 // Use IPv6 Neighbor Discovery
270 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
271 .matchEthType(Ethernet.TYPE_IPV6)
272 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
273 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
274 .build();
275 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
276 .matchEthType(Ethernet.TYPE_IPV6)
277 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
278 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
279 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700280 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800281 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
282 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700283 } else {
Charles Chane6067892016-11-17 10:23:46 -0800284 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
285 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
286 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700287 }
288
289 /**
290 * Withdraw packet intercepts.
291 */
292 private void withdrawIntercepts() {
293 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
294 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700295 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700296
297 // IPv6 Neighbor Solicitation packet.
298 selector.matchEthType(Ethernet.TYPE_IPV6);
299 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
300 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
301 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
302
303 // IPv6 Neighbor Advertisement packet.
304 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
305 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800306 }
307
308 /**
309 * Extracts properties from the component configuration context.
310 *
311 * @param context the component context
312 */
313 private void readComponentConfiguration(ComponentContext context) {
314 Dictionary<?, ?> properties = context.getProperties();
315 Boolean flag;
316
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700317 flag = Tools.isPropertyEnabled(properties, HOST_REMOVAL_ENABLED);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800318 if (flag == null) {
319 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700320 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800321 } else {
322 hostRemovalEnabled = flag;
323 log.info("Configured. Host removal on port/device down events is {}",
324 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800325 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800326
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700327 flag = Tools.isPropertyEnabled(properties, REQUEST_ARP);
Charles Chane6067892016-11-17 10:23:46 -0800328 if (flag == null) {
329 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700330 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800331 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700332 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800333 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700334 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800335 }
336
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700337 flag = Tools.isPropertyEnabled(properties, REQUEST_NDP);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800338 if (flag == null) {
339 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700340 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800341 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700342 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800343 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700344 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800345 }
346
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700347 flag = Tools.isPropertyEnabled(properties, USE_DHCP);
Charles Chane6067892016-11-17 10:23:46 -0800348 if (flag == null) {
349 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700350 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800351 } else {
352 useDhcp = flag;
353 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700354 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800355 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800356
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700357 flag = Tools.isPropertyEnabled(properties, USE_DHCP6);
358 if (flag == null) {
359 log.info("Using DHCP6 is not configured, " +
360 "using current value of {}", useDhcp6);
361 } else {
362 useDhcp6 = flag;
363 log.info("Configured. Using DHCP6 is {}",
364 useDhcp6 ? "enabled" : "disabled");
365 }
366
367 flag = Tools.isPropertyEnabled(properties, REQUEST_INTERCEPTS_ENABLED);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800368 if (flag == null) {
369 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700370 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800371 } else {
372 requestInterceptsEnabled = flag;
373 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700374 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800375 }
Charles Chan35a32322017-08-14 11:42:11 -0700376
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700377 flag = Tools.isPropertyEnabled(properties, MULTIHOMING_ENABLED);
Charles Chan35a32322017-08-14 11:42:11 -0700378 if (flag == null) {
379 log.info("Multihoming is not configured, " +
380 "using current value of {}", multihomingEnabled);
381 } else {
382 multihomingEnabled = flag;
383 log.info("Configured. Multihoming is {}",
384 multihomingEnabled ? "enabled" : "disabled");
385 }
Charles Chan47933752017-11-30 15:37:50 -0800386 }
Charles Chan35a32322017-08-14 11:42:11 -0700387
Charles Chan47933752017-11-30 15:37:50 -0800388 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700389 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800390 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700391
392 // FIXME Disabling host probing for now, because sending packets from a
393 // broadcast MAC address caused problems when two ONOS networks were
394 // interconnected. Host probing should take into account the interface
395 // configuration when determining which source address to use.
396
397 //MastershipRole role = deviceService.getRole(host.location().deviceId());
398 //if (role.equals(MastershipRole.MASTER)) {
399 // host.ipAddresses().forEach(ip -> {
400 // sendProbe(host, ip);
401 // });
402 //} else {
403 // log.info("not the master, master will probe {}");
404 //}
sdn94b00152016-08-30 02:12:32 -0700405 }
406
407 private void sendProbe(Host host, IpAddress targetIp) {
408 Ethernet probePacket = null;
409 if (targetIp.isIp4()) {
410 // IPv4: Use ARP
411 probePacket = buildArpRequest(targetIp, host);
412 } else {
413 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700414 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700415 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800416 return;
sdn94b00152016-08-30 02:12:32 -0700417 }
418
419 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
420
421 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
422 ByteBuffer.wrap(probePacket.serialize()));
423
424 packetService.emit(outboundPacket);
425 }
426
Charles Chan888e20a2017-05-01 15:44:23 -0700427 // This method is using source ip as 0.0.0.0 , to receive the reply even from the sub net hosts.
sdn94b00152016-08-30 02:12:32 -0700428 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700429 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
430 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
431 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700432 }
433
434 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800435 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700436 * Create or update host information.
437 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800438 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700439 * @param hid host ID
440 * @param mac source Mac address
441 * @param vlan VLAN ID
442 * @param innerVlan inner VLAN ID
443 * @param outerTpid outer TPID
444 * @param hloc host location
445 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800446 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700447 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
448 VlanId innerVlan, EthType outerTpid,
449 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700450 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
451
452 if (multihomingEnabled) {
453 Host existingHost = hostService.getHost(hid);
454 if (existingHost != null) {
455 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700456
Charles Chan653e2ac2017-11-22 14:49:55 -0800457 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
458 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700459 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800460 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800461 } else {
462 // Move within the same switch
463 // Simply replace old location that is on the same device
464 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
465 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700466 }
467 }
468 }
469
Charles Chan888e20a2017-05-01 15:44:23 -0700470 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700471 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
472 innerVlan, outerTpid, false) :
473 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
474 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700475 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800476 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700477 } catch (IllegalStateException e) {
478 log.debug("Host {} suppressed", hid);
479 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800480 }
alshabibe1cf87d2014-10-17 09:23:50 -0700481
Charles Chane6067892016-11-17 10:23:46 -0800482 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700483 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800484 *
485 * @param hid host ID
486 * @param ip IP address
487 */
Charles Chan888e20a2017-05-01 15:44:23 -0700488 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800489 Host host = hostService.getHost(hid);
490 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700491 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800492 return;
493 }
494
Charles Chan888e20a2017-05-01 15:44:23 -0700495 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700496 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800497 try {
498 providerService.hostDetected(hid, desc, false);
499 } catch (IllegalStateException e) {
500 log.debug("Host {} suppressed", hid);
501 }
502 }
503
alshabibe1cf87d2014-10-17 09:23:50 -0700504 @Override
505 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700506 packetHandler.execute(() -> processPacketInternal(context));
507 }
508
509 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700510 if (context == null) {
511 return;
512 }
alshabibe1cf87d2014-10-17 09:23:50 -0700513
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700514 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800515 if (eth == null) {
516 return;
517 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700518
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800519 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700520 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
521 return;
522 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800523
alshabibe1cf87d2014-10-17 09:23:50 -0700524 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700525 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
526 VlanId innerVlan = VlanId.NONE;
527 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
528 // Set up values for double-tagged hosts
529 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
530 innerVlan = vlan;
531 vlan = outerVlan;
532 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
533 }
alshabibe1cf87d2014-10-17 09:23:50 -0700534 ConnectPoint heardOn = context.inPacket().receivedFrom();
535
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700536 // If this arrived on control port, bail out.
537 if (heardOn.port().isLogical()) {
538 return;
539 }
540
alshabibe1cf87d2014-10-17 09:23:50 -0700541 // If this is not an edge port, bail out.
542 Topology topology = topologyService.currentTopology();
543 if (topologyService.isInfrastructure(topology, heardOn)) {
544 return;
545 }
546
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700547 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700548 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700549 MacAddress destMac = eth.getDestinationMAC();
550
Charles Chanff79dd92018-06-01 16:33:48 -0700551 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700552 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700553 return;
554 }
alshabibe1cf87d2014-10-17 09:23:50 -0700555
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700556 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
557 // if learning is disabled bail out.
558 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
559 log.debug("Learning disabled for {}, abort.", heardOn);
560 return;
561 }
562
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800563 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700564 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
565 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800566 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
567 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700568 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700569
Charles Chan5d5e0622015-09-25 13:00:06 -0700570 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700571 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700572 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700573 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700574 if (useDhcp) {
575 DHCP dhcp = findDhcp(eth).orElse(null);
576 // DHCP ACK: additionally update IP of DHCP client
577 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
578 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
579 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
580 HostId hostId = HostId.hostId(hostMac, hostVlan);
581 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700582 }
Charles Chane6067892016-11-17 10:23:46 -0800583 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700584 // NeighborAdvertisement and NeighborSolicitation: possible
585 // new hosts, update both location and IP.
586 //
587 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800588 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800589 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800590 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700591 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800592
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800593 // skip extension headers
594 IPacket pkt = ipv6;
595 while (pkt.getPayload() != null &&
596 pkt.getPayload() instanceof IExtensionHeader) {
597 pkt = pkt.getPayload();
598 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800599 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700600
601 // DHCPv6 protocol
602 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
603 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700604 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700605 handleDhcp6(dhcp6, vlan);
606 return;
607 }
608
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800609 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700610 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800611 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400612 if (pkt != null) {
613 // RouterSolicitation, RouterAdvertisement
614 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800615 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800616 }
Charles Chan178b0f52017-10-08 23:51:22 -0400617 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
618 // Duplicate Address Detection
619 if (ip.isZero()) {
620 return;
621 }
622 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700623 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400624
625 // Also learn from the target address of NeighborAdvertisement
626 if (pkt instanceof NeighborAdvertisement) {
627 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
628 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700629 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400630 }
631 return;
632 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800633 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800634 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800635
Yi Tsengaa417a62017-09-08 17:22:51 -0700636 // multicast, exclude DHCPv6
637 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800638 return;
639 }
640
641 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700642 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700643 }
644 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700645
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700646 /**
647 * Handles DHCPv6 packet, if message type is ACK, update IP address
648 * according to DHCPv6 payload (IA Address option).
649 *
650 * @param dhcp6 the DHCPv6 payload
651 * @param vlanId the vlan of this packet
652 */
653 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
654 // extract the relay message if exist
655 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
656 dhcp6 = dhcp6.getOptions().stream()
657 .filter(opt -> opt instanceof Dhcp6RelayOption)
658 .map(BasePacket::getPayload)
659 .map(pld -> (DHCP6) pld)
660 .findFirst()
661 .orElse(null);
662 }
663
664 if (dhcp6 == null) {
665 // Can't find dhcp payload
666 log.warn("Can't find dhcp payload from relay message");
667 return;
668 }
669
670 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
671 // Update IP address only when we received REPLY message
672 return;
673 }
674 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
675 .stream()
676 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
677 .map(opt -> (Dhcp6ClientIdOption) opt)
678 .findFirst();
679
680 if (!clientIdOption.isPresent()) {
681 // invalid DHCPv6 option
682 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
683 return;
684 }
685
686 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
687 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
688 // No any mac address found
689 log.warn("Can't find client mac from option {}", clientIdOption);
690 return;
691 }
692 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
693
694 // Extract IPv6 address from IA NA ot IA TA option
695 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
696 .stream()
697 .filter(opt -> opt instanceof Dhcp6IaNaOption)
698 .map(opt -> (Dhcp6IaNaOption) opt)
699 .findFirst();
700 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
701 .stream()
702 .filter(opt -> opt instanceof Dhcp6IaTaOption)
703 .map(opt -> (Dhcp6IaTaOption) opt)
704 .findFirst();
705 Optional<Dhcp6IaAddressOption> iaAddressOption;
706 if (iaNaOption.isPresent()) {
707 iaAddressOption = iaNaOption.get().getOptions().stream()
708 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
709 .map(opt -> (Dhcp6IaAddressOption) opt)
710 .findFirst();
711 } else if (iaTaOption.isPresent()) {
712 iaAddressOption = iaTaOption.get().getOptions().stream()
713 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
714 .map(opt -> (Dhcp6IaAddressOption) opt)
715 .findFirst();
716 } else {
717 iaAddressOption = Optional.empty();
718 }
719 if (iaAddressOption.isPresent()) {
720 Ip6Address ip = iaAddressOption.get().getIp6Address();
721 HostId hostId = HostId.hostId(clientMac, vlanId);
722 updateHostIp(hostId, ip);
723 } else {
724 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
725 }
726 }
727
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700728 private Optional<DHCP> findDhcp(Ethernet eth) {
729 IPacket pkt = eth.getPayload();
730 return Stream.of(pkt)
731 .filter(Objects::nonNull)
732 .filter(p -> p instanceof IPv4)
733 .map(IPacket::getPayload)
734 .filter(Objects::nonNull)
735 .filter(p -> p instanceof UDP)
736 .map(IPacket::getPayload)
737 .filter(Objects::nonNull)
738 .filter(p -> p instanceof DHCP)
739 .map(p -> (DHCP) p)
740 .findFirst();
741 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700742
743 private Optional<DHCP6> findDhcp6(IPacket pkt) {
744 return Stream.of(pkt)
745 .filter(Objects::nonNull)
746 .filter(p -> p instanceof UDP)
747 .map(IPacket::getPayload)
748 .filter(Objects::nonNull)
749 .filter(p -> p instanceof DHCP6)
750 .map(p -> (DHCP6) p)
751 .findFirst();
752 }
alshabibe1cf87d2014-10-17 09:23:50 -0700753 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800754
755 // Auxiliary listener to device events.
756 private class InternalDeviceListener implements DeviceListener {
757 @Override
758 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700759 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800760 }
761
762 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800763 Device device = event.subject();
764 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700765 case DEVICE_ADDED:
766 break;
767 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700768 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
769 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700770 }
771 break;
772 case DEVICE_SUSPENDED:
773 case DEVICE_UPDATED:
774 // Nothing to do?
775 break;
776 case DEVICE_REMOVED:
777 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700778 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700779 }
780 break;
781 case PORT_ADDED:
782 break;
783 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700784 if (hostRemovalEnabled && !event.port().isEnabled()) {
785 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700786 }
787 break;
788 case PORT_REMOVED:
enen923305efe2018-07-03 23:16:53 +0100789 if (hostRemovalEnabled) {
790 processPortDown(new ConnectPoint(device.id(), event.port().number()));
791 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700792 break;
793 default:
794 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800795 }
796 }
797 }
798
Charles Chan888e20a2017-05-01 15:44:23 -0700799 /**
800 * When a device goes down, update the location of affected hosts.
801 *
802 * @param deviceId the device that goes down
803 */
804 private void processDeviceDown(DeviceId deviceId) {
805 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
806 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
807 .forEach(affectedLocation ->
808 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
809 );
810 }
811
812 /**
813 * When a port goes down, update the location of affected hosts.
814 *
815 * @param connectPoint the port that goes down
816 */
817 private void processPortDown(ConnectPoint connectPoint) {
818 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
819 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
820 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800821 }
822
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700823
824 private class InternalConfigListener implements NetworkConfigListener {
825
826 @Override
827 public void event(NetworkConfigEvent event) {
828 switch (event.type()) {
829 case CONFIG_ADDED:
830 case CONFIG_UPDATED:
831 log.debug("HostLearningConfig event of type {}", event.type());
832 // if learning enabled do nothing
833 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
834 if (learningConfig.hostLearningEnabled()) {
835 return;
836 }
837
838 // if host learning is disable remove this location from existing, learnt hosts
839 ConnectPoint connectPoint = learningConfig.subject();
840 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
841 for (Host host : connectedHosts) {
842 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
843
844 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
845 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
846 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
847 }
848 }
849 break;
850 case CONFIG_REMOVED:
851 default:
852 break;
853 }
854 }
855
856 @Override
857 public boolean isRelevant(NetworkConfigEvent event) {
858 if (!event.configClass().equals(HostLearningConfig.class)) {
859 return false;
860 }
861 return true;
862 }
863 }
alshabibe1cf87d2014-10-17 09:23:50 -0700864}