blob: 874bf38cde7bcc4276a11a418a465768fba31d6a [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,
Yi Tseng11f95b52019-05-08 14:03:19 -0700118 REQUEST_NDP_RS_RA + ":Boolean=" + REQUEST_NDP_RS_RA_DEFAULT,
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700119 USE_DHCP + ":Boolean=" + USE_DHCP_DEFAULT,
120 USE_DHCP6 + ":Boolean=" + USE_DHCP6_DEFAULT,
121 REQUEST_INTERCEPTS_ENABLED + ":Boolean=" + REQUEST_INTERCEPTS_ENABLED_DEFAULT,
122 MULTIHOMING_ENABLED + ":Boolean=" + MULTIHOMING_ENABLED_DEFAULT,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700123 })
Charles Chanff79dd92018-06-01 16:33:48 -0700124public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700125 private final Logger log = getLogger(getClass());
126
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700127 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800128 protected CoreService coreService;
129
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700130 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700131 protected HostProviderRegistry providerRegistry;
132
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700133 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800134 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700135
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700136 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700137 protected TopologyService topologyService;
138
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800140 protected HostService hostService;
141
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800143 protected DeviceService deviceService;
144
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700145 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700146 protected ComponentConfigService cfgService;
147
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700148 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700149 protected NetworkConfigRegistry registry;
150
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700151 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700152 protected InterfaceService interfaceService;
153
alshabibe1cf87d2014-10-17 09:23:50 -0700154 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800155 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700156 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800157
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800158 private ApplicationId appId;
159
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700160 /** Enable host removal on port/device down events. */
Thomas Vachuska33601602014-11-19 03:32:15 -0800161 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700162
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700163 /** Request ARP packets for neighbor discovery by the Host Location Provider; default is true. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700164 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800165
Yi Tseng11f95b52019-05-08 14:03:19 -0700166 /** Requests IPv6 NDP Neighbor Solicitation and Advertisement by the Host Location Provider; default is false. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700167 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800168
Yi Tseng11f95b52019-05-08 14:03:19 -0700169 /** Requests IPv6 NDP Router Solicitation and Advertisement by the Host Location Provider; default is false. */
170 private boolean requestIpv6NdpRsRa = false;
171
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700172 /** Use DHCP to update IP address of the host; default is false. */
Charles Chane6067892016-11-17 10:23:46 -0800173 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700174
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700175 /** Use DHCPv6 to update IP address of the host; default is false. */
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700176 private boolean useDhcp6 = false;
177
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700178 /** Enable requesting packet intercepts. */
Jonathan Hart9af322d2016-01-06 17:42:04 -0800179 private boolean requestInterceptsEnabled = true;
180
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700181 /** Allow hosts to be multihomed. */
Charles Chan35a32322017-08-14 11:42:11 -0700182 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800183
Charles Chanff79dd92018-06-01 16:33:48 -0700184 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800185
Charles Chanff79dd92018-06-01 16:33:48 -0700186 ExecutorService deviceEventHandler;
187 private ExecutorService probeEventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700188 private ExecutorService packetHandler;
Charles Chan8277b6b2017-12-03 13:48:46 -0800189
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700190 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700191 protected NetworkConfigService netcfgService;
192
193 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
194 new ConfigFactory<ConnectPoint, HostLearningConfig>(
195 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
196 HostLearningConfig.class, "hostLearning") {
197 @Override
198 public HostLearningConfig createConfig() {
199 return new HostLearningConfig();
200 }
201 };
202
alshabibe1cf87d2014-10-17 09:23:50 -0700203 /**
204 * Creates an OpenFlow host provider.
205 */
206 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800207 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700208 }
209
210 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800211 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700212 cfgService.registerProperties(getClass());
213 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700214 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
215 "device-event-handler", log));
216 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
217 "probe-event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700218 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
219 "packet-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700220 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700221 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800222 deviceService.addListener(deviceListener);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700223 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800224 modified(context);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700225 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800226
Charles M.C. Chane148de82015-05-06 12:38:21 +0800227 log.info("Started with Application ID {}", appId.id());
228 }
229
230 @Deactivate
231 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800232 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700233
234 withdrawIntercepts();
235
Charles M.C. Chane148de82015-05-06 12:38:21 +0800236 providerRegistry.unregister(this);
237 packetService.removeProcessor(processor);
238 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700239 deviceEventHandler.shutdown();
240 probeEventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700241 packetHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800242 providerService = null;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700243 registry.unregisterConfigFactory(hostLearningConfig);
244 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800245 log.info("Stopped");
246 }
247
248 @Modified
249 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800250 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800251
252 if (requestInterceptsEnabled) {
253 requestIntercepts();
254 } else {
255 withdrawIntercepts();
256 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800257 }
258
259 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700260 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800261 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700262 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800263 // Use ARP
Yi Tseng11f95b52019-05-08 14:03:19 -0700264 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
265 .matchEthType(Ethernet.TYPE_ARP);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700266 if (requestArp) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700267 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700268 } else {
Yi Tseng11f95b52019-05-08 14:03:19 -0700269 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800270 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700271
Yi Tseng11f95b52019-05-08 14:03:19 -0700272 // Use IPv6 NDP Neighbor Solicitation and Advertisement
273 selector.matchEthType(Ethernet.TYPE_IPV6)
274 .matchIPProtocol(IPv6.PROTOCOL_ICMP6);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700275 if (requestIpv6ND) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700276 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
277 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
278 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
279 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700280 } else {
Yi Tseng11f95b52019-05-08 14:03:19 -0700281 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
282 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
283 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
284 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
285 }
286
287 // Use IPv6 NDP Router Solicitation and Advertisement
288 if (requestIpv6NdpRsRa) {
289 selector.matchIcmpv6Type(ICMP6.ROUTER_SOLICITATION);
290 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
291 selector.matchIcmpv6Type(ICMP6.ROUTER_ADVERTISEMENT);
292 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
293 } else {
294 selector.matchIcmpv6Type(ICMP6.ROUTER_SOLICITATION);
295 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
296 selector.matchIcmpv6Type(ICMP6.ROUTER_ADVERTISEMENT);
297 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Charles Chane6067892016-11-17 10:23:46 -0800298 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700299 }
300
301 /**
302 * Withdraw packet intercepts.
303 */
304 private void withdrawIntercepts() {
305 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
306 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700307 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700308
309 // IPv6 Neighbor Solicitation packet.
310 selector.matchEthType(Ethernet.TYPE_IPV6);
311 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
312 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
313 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
314
315 // IPv6 Neighbor Advertisement packet.
316 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
317 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800318 }
319
320 /**
321 * Extracts properties from the component configuration context.
322 *
323 * @param context the component context
324 */
325 private void readComponentConfiguration(ComponentContext context) {
326 Dictionary<?, ?> properties = context.getProperties();
327 Boolean flag;
328
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700329 flag = Tools.isPropertyEnabled(properties, HOST_REMOVAL_ENABLED);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800330 if (flag == null) {
331 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700332 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800333 } else {
334 hostRemovalEnabled = flag;
335 log.info("Configured. Host removal on port/device down events is {}",
336 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800337 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800338
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700339 flag = Tools.isPropertyEnabled(properties, REQUEST_ARP);
Charles Chane6067892016-11-17 10:23:46 -0800340 if (flag == null) {
341 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700342 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800343 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700344 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800345 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700346 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800347 }
348
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700349 flag = Tools.isPropertyEnabled(properties, REQUEST_NDP);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800350 if (flag == null) {
351 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700352 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800353 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700354 requestIpv6ND = flag;
Yi Tseng11f95b52019-05-08 14:03:19 -0700355 log.info("Configured. Using IPv6 NDP Neighbor Solicitation and Advertisement is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700356 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800357 }
358
Yi Tseng11f95b52019-05-08 14:03:19 -0700359 flag = Tools.isPropertyEnabled(properties, REQUEST_NDP_RS_RA);
360 if (flag == null) {
361 log.info("Using IPv6 Neighbor Discovery is not configured, " +
362 "using current value of {}", requestIpv6NdpRsRa);
363 } else {
364 requestIpv6NdpRsRa = flag;
365 log.info("Configured. Using IPv6 NDP Router Solicitation and Advertisement is {}",
366 requestIpv6NdpRsRa ? "enabled" : "disabled");
367 }
368
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700369 flag = Tools.isPropertyEnabled(properties, USE_DHCP);
Charles Chane6067892016-11-17 10:23:46 -0800370 if (flag == null) {
371 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700372 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800373 } else {
374 useDhcp = flag;
375 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700376 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800377 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800378
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700379 flag = Tools.isPropertyEnabled(properties, USE_DHCP6);
380 if (flag == null) {
381 log.info("Using DHCP6 is not configured, " +
382 "using current value of {}", useDhcp6);
383 } else {
384 useDhcp6 = flag;
385 log.info("Configured. Using DHCP6 is {}",
386 useDhcp6 ? "enabled" : "disabled");
387 }
388
389 flag = Tools.isPropertyEnabled(properties, REQUEST_INTERCEPTS_ENABLED);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800390 if (flag == null) {
391 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700392 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800393 } else {
394 requestInterceptsEnabled = flag;
395 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700396 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800397 }
Charles Chan35a32322017-08-14 11:42:11 -0700398
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700399 flag = Tools.isPropertyEnabled(properties, MULTIHOMING_ENABLED);
Charles Chan35a32322017-08-14 11:42:11 -0700400 if (flag == null) {
401 log.info("Multihoming is not configured, " +
402 "using current value of {}", multihomingEnabled);
403 } else {
404 multihomingEnabled = flag;
405 log.info("Configured. Multihoming is {}",
406 multihomingEnabled ? "enabled" : "disabled");
407 }
Charles Chan47933752017-11-30 15:37:50 -0800408 }
Charles Chan35a32322017-08-14 11:42:11 -0700409
Charles Chan47933752017-11-30 15:37:50 -0800410 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700411 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800412 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700413
414 // FIXME Disabling host probing for now, because sending packets from a
415 // broadcast MAC address caused problems when two ONOS networks were
416 // interconnected. Host probing should take into account the interface
417 // configuration when determining which source address to use.
418
419 //MastershipRole role = deviceService.getRole(host.location().deviceId());
420 //if (role.equals(MastershipRole.MASTER)) {
421 // host.ipAddresses().forEach(ip -> {
422 // sendProbe(host, ip);
423 // });
424 //} else {
425 // log.info("not the master, master will probe {}");
426 //}
sdn94b00152016-08-30 02:12:32 -0700427 }
428
429 private void sendProbe(Host host, IpAddress targetIp) {
430 Ethernet probePacket = null;
431 if (targetIp.isIp4()) {
432 // IPv4: Use ARP
433 probePacket = buildArpRequest(targetIp, host);
434 } else {
435 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700436 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700437 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800438 return;
sdn94b00152016-08-30 02:12:32 -0700439 }
440
441 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
442
443 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
444 ByteBuffer.wrap(probePacket.serialize()));
445
446 packetService.emit(outboundPacket);
447 }
448
Charles Chan888e20a2017-05-01 15:44:23 -0700449 // 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 -0700450 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700451 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
452 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
453 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700454 }
455
456 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800457 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700458 * Create or update host information.
459 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800460 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700461 * @param hid host ID
462 * @param mac source Mac address
463 * @param vlan VLAN ID
464 * @param innerVlan inner VLAN ID
465 * @param outerTpid outer TPID
466 * @param hloc host location
467 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800468 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700469 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
470 VlanId innerVlan, EthType outerTpid,
471 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700472 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
473
474 if (multihomingEnabled) {
475 Host existingHost = hostService.getHost(hid);
476 if (existingHost != null) {
477 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700478
Charles Chan653e2ac2017-11-22 14:49:55 -0800479 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
480 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700481 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800482 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800483 } else {
484 // Move within the same switch
485 // Simply replace old location that is on the same device
486 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
487 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700488 }
489 }
490 }
491
Charles Chan888e20a2017-05-01 15:44:23 -0700492 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700493 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
494 innerVlan, outerTpid, false) :
495 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
496 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700497 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800498 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700499 } catch (IllegalStateException e) {
500 log.debug("Host {} suppressed", hid);
501 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800502 }
alshabibe1cf87d2014-10-17 09:23:50 -0700503
Charles Chane6067892016-11-17 10:23:46 -0800504 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700505 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800506 *
507 * @param hid host ID
508 * @param ip IP address
509 */
Charles Chan888e20a2017-05-01 15:44:23 -0700510 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800511 Host host = hostService.getHost(hid);
512 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700513 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800514 return;
515 }
516
Charles Chan888e20a2017-05-01 15:44:23 -0700517 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700518 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800519 try {
520 providerService.hostDetected(hid, desc, false);
521 } catch (IllegalStateException e) {
522 log.debug("Host {} suppressed", hid);
523 }
524 }
525
alshabibe1cf87d2014-10-17 09:23:50 -0700526 @Override
527 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700528 packetHandler.execute(() -> processPacketInternal(context));
529 }
530
531 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700532 if (context == null) {
533 return;
534 }
alshabibe1cf87d2014-10-17 09:23:50 -0700535
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700536 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800537 if (eth == null) {
538 return;
539 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700540
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800541 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700542 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
543 return;
544 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800545
alshabibe1cf87d2014-10-17 09:23:50 -0700546 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700547 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
548 VlanId innerVlan = VlanId.NONE;
549 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
550 // Set up values for double-tagged hosts
551 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
552 innerVlan = vlan;
553 vlan = outerVlan;
554 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
555 }
alshabibe1cf87d2014-10-17 09:23:50 -0700556 ConnectPoint heardOn = context.inPacket().receivedFrom();
557
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700558 // If this arrived on control port, bail out.
559 if (heardOn.port().isLogical()) {
560 return;
561 }
562
alshabibe1cf87d2014-10-17 09:23:50 -0700563 // If this is not an edge port, bail out.
564 Topology topology = topologyService.currentTopology();
565 if (topologyService.isInfrastructure(topology, heardOn)) {
566 return;
567 }
568
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700569 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700570 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700571 MacAddress destMac = eth.getDestinationMAC();
572
Charles Chanff79dd92018-06-01 16:33:48 -0700573 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700574 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700575 return;
576 }
alshabibe1cf87d2014-10-17 09:23:50 -0700577
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700578 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
579 // if learning is disabled bail out.
580 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
581 log.debug("Learning disabled for {}, abort.", heardOn);
582 return;
583 }
584
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800585 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700586 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
587 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800588 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
589 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700590 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700591
Charles Chan5d5e0622015-09-25 13:00:06 -0700592 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700593 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700594 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700595 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700596 if (useDhcp) {
597 DHCP dhcp = findDhcp(eth).orElse(null);
598 // DHCP ACK: additionally update IP of DHCP client
599 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
600 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
601 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
602 HostId hostId = HostId.hostId(hostMac, hostVlan);
603 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700604 }
Charles Chane6067892016-11-17 10:23:46 -0800605 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700606 // NeighborAdvertisement and NeighborSolicitation: possible
607 // new hosts, update both location and IP.
608 //
609 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800610 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800611 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800612 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700613 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800614
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800615 // skip extension headers
616 IPacket pkt = ipv6;
617 while (pkt.getPayload() != null &&
618 pkt.getPayload() instanceof IExtensionHeader) {
619 pkt = pkt.getPayload();
620 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800621 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700622
623 // DHCPv6 protocol
624 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
625 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700626 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700627 handleDhcp6(dhcp6, vlan);
628 return;
629 }
630
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800631 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700632 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800633 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400634 if (pkt != null) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700635 if ((pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation)) {
636 if (ip.isZero()) {
637 return;
638 }
639 // RouterSolicitation, RouterAdvertisement
640 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800641 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800642 }
Charles Chan178b0f52017-10-08 23:51:22 -0400643 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
644 // Duplicate Address Detection
645 if (ip.isZero()) {
646 return;
647 }
648 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700649 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400650
651 // Also learn from the target address of NeighborAdvertisement
652 if (pkt instanceof NeighborAdvertisement) {
653 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
654 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700655 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400656 }
657 return;
658 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800659 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800660 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800661
Yi Tsengaa417a62017-09-08 17:22:51 -0700662 // multicast, exclude DHCPv6
663 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800664 return;
665 }
666
667 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700668 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700669 }
670 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700671
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700672 /**
673 * Handles DHCPv6 packet, if message type is ACK, update IP address
674 * according to DHCPv6 payload (IA Address option).
675 *
676 * @param dhcp6 the DHCPv6 payload
677 * @param vlanId the vlan of this packet
678 */
679 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
680 // extract the relay message if exist
681 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
682 dhcp6 = dhcp6.getOptions().stream()
683 .filter(opt -> opt instanceof Dhcp6RelayOption)
684 .map(BasePacket::getPayload)
685 .map(pld -> (DHCP6) pld)
686 .findFirst()
687 .orElse(null);
688 }
689
690 if (dhcp6 == null) {
691 // Can't find dhcp payload
692 log.warn("Can't find dhcp payload from relay message");
693 return;
694 }
695
696 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
697 // Update IP address only when we received REPLY message
698 return;
699 }
700 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
701 .stream()
702 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
703 .map(opt -> (Dhcp6ClientIdOption) opt)
704 .findFirst();
705
706 if (!clientIdOption.isPresent()) {
707 // invalid DHCPv6 option
708 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
709 return;
710 }
711
712 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
713 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
714 // No any mac address found
715 log.warn("Can't find client mac from option {}", clientIdOption);
716 return;
717 }
718 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
719
720 // Extract IPv6 address from IA NA ot IA TA option
721 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
722 .stream()
723 .filter(opt -> opt instanceof Dhcp6IaNaOption)
724 .map(opt -> (Dhcp6IaNaOption) opt)
725 .findFirst();
726 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
727 .stream()
728 .filter(opt -> opt instanceof Dhcp6IaTaOption)
729 .map(opt -> (Dhcp6IaTaOption) opt)
730 .findFirst();
731 Optional<Dhcp6IaAddressOption> iaAddressOption;
732 if (iaNaOption.isPresent()) {
733 iaAddressOption = iaNaOption.get().getOptions().stream()
734 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
735 .map(opt -> (Dhcp6IaAddressOption) opt)
736 .findFirst();
737 } else if (iaTaOption.isPresent()) {
738 iaAddressOption = iaTaOption.get().getOptions().stream()
739 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
740 .map(opt -> (Dhcp6IaAddressOption) opt)
741 .findFirst();
742 } else {
743 iaAddressOption = Optional.empty();
744 }
745 if (iaAddressOption.isPresent()) {
746 Ip6Address ip = iaAddressOption.get().getIp6Address();
747 HostId hostId = HostId.hostId(clientMac, vlanId);
748 updateHostIp(hostId, ip);
749 } else {
750 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
751 }
752 }
753
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700754 private Optional<DHCP> findDhcp(Ethernet eth) {
755 IPacket pkt = eth.getPayload();
756 return Stream.of(pkt)
757 .filter(Objects::nonNull)
758 .filter(p -> p instanceof IPv4)
759 .map(IPacket::getPayload)
760 .filter(Objects::nonNull)
761 .filter(p -> p instanceof UDP)
762 .map(IPacket::getPayload)
763 .filter(Objects::nonNull)
764 .filter(p -> p instanceof DHCP)
765 .map(p -> (DHCP) p)
766 .findFirst();
767 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700768
769 private Optional<DHCP6> findDhcp6(IPacket pkt) {
770 return Stream.of(pkt)
771 .filter(Objects::nonNull)
772 .filter(p -> p instanceof UDP)
773 .map(IPacket::getPayload)
774 .filter(Objects::nonNull)
775 .filter(p -> p instanceof DHCP6)
776 .map(p -> (DHCP6) p)
777 .findFirst();
778 }
alshabibe1cf87d2014-10-17 09:23:50 -0700779 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800780
781 // Auxiliary listener to device events.
782 private class InternalDeviceListener implements DeviceListener {
783 @Override
784 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700785 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800786 }
787
788 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800789 Device device = event.subject();
790 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700791 case DEVICE_ADDED:
792 break;
793 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700794 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
795 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700796 }
797 break;
798 case DEVICE_SUSPENDED:
799 case DEVICE_UPDATED:
800 // Nothing to do?
801 break;
802 case DEVICE_REMOVED:
803 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700804 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700805 }
806 break;
807 case PORT_ADDED:
808 break;
809 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700810 if (hostRemovalEnabled && !event.port().isEnabled()) {
811 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700812 }
813 break;
814 case PORT_REMOVED:
enen923305efe2018-07-03 23:16:53 +0100815 if (hostRemovalEnabled) {
816 processPortDown(new ConnectPoint(device.id(), event.port().number()));
817 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700818 break;
819 default:
820 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800821 }
822 }
823 }
824
Charles Chan888e20a2017-05-01 15:44:23 -0700825 /**
826 * When a device goes down, update the location of affected hosts.
827 *
828 * @param deviceId the device that goes down
829 */
830 private void processDeviceDown(DeviceId deviceId) {
831 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
832 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
833 .forEach(affectedLocation ->
834 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
835 );
836 }
837
838 /**
839 * When a port goes down, update the location of affected hosts.
840 *
841 * @param connectPoint the port that goes down
842 */
843 private void processPortDown(ConnectPoint connectPoint) {
844 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
845 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
846 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800847 }
848
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700849
850 private class InternalConfigListener implements NetworkConfigListener {
851
852 @Override
853 public void event(NetworkConfigEvent event) {
854 switch (event.type()) {
855 case CONFIG_ADDED:
856 case CONFIG_UPDATED:
857 log.debug("HostLearningConfig event of type {}", event.type());
858 // if learning enabled do nothing
859 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
860 if (learningConfig.hostLearningEnabled()) {
861 return;
862 }
863
864 // if host learning is disable remove this location from existing, learnt hosts
865 ConnectPoint connectPoint = learningConfig.subject();
866 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
867 for (Host host : connectedHosts) {
868 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
869
870 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
871 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
872 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
873 }
874 }
875 break;
876 case CONFIG_REMOVED:
877 default:
878 break;
879 }
880 }
881
882 @Override
883 public boolean isRelevant(NetworkConfigEvent event) {
884 if (!event.configClass().equals(HostLearningConfig.class)) {
885 return false;
886 }
887 return true;
888 }
889 }
alshabibe1cf87d2014-10-17 09:23:50 -0700890}