blob: 089f173b1aaa8c06e48ea8b3afc5fa64af1f7540 [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;
pierf543ff82019-06-25 14:09:57 +020045import org.onlab.util.PredictableExecutor;
Jian Lid9b5f552016-03-11 18:15:31 -080046import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070047import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080048import org.onosproject.core.ApplicationId;
49import org.onosproject.core.CoreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050import org.onosproject.net.ConnectPoint;
51import org.onosproject.net.Device;
52import org.onosproject.net.DeviceId;
53import org.onosproject.net.Host;
54import org.onosproject.net.HostId;
55import org.onosproject.net.HostLocation;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
58import org.onosproject.net.config.NetworkConfigListener;
59import org.onosproject.net.config.NetworkConfigRegistry;
60import org.onosproject.net.config.NetworkConfigService;
61import org.onosproject.net.config.basics.BasicHostConfig;
62import org.onosproject.net.config.basics.HostLearningConfig;
63import org.onosproject.net.config.basics.SubjectFactories;
Brian O'Connorabafb502014-12-02 22:26:20 -080064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080067import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070068import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080069import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070070import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080071import org.onosproject.net.host.DefaultHostDescription;
72import org.onosproject.net.host.HostDescription;
73import org.onosproject.net.host.HostProvider;
74import org.onosproject.net.host.HostProviderRegistry;
75import org.onosproject.net.host.HostProviderService;
76import org.onosproject.net.host.HostService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077import org.onosproject.net.intf.InterfaceService;
sdn94b00152016-08-30 02:12:32 -070078import org.onosproject.net.packet.DefaultOutboundPacket;
79import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080080import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080081import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080082import org.onosproject.net.packet.PacketProcessor;
83import org.onosproject.net.packet.PacketService;
84import org.onosproject.net.provider.AbstractProvider;
85import org.onosproject.net.provider.ProviderId;
86import org.onosproject.net.topology.Topology;
87import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080088import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089import org.osgi.service.component.annotations.Activate;
90import org.osgi.service.component.annotations.Component;
91import org.osgi.service.component.annotations.Deactivate;
92import org.osgi.service.component.annotations.Modified;
93import org.osgi.service.component.annotations.Reference;
94import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibe1cf87d2014-10-17 09:23:50 -070095import org.slf4j.Logger;
96
sdn94b00152016-08-30 02:12:32 -070097import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070098import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070099import java.util.Objects;
100import java.util.Optional;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700101import java.util.Set;
Madan Jampania3770c32015-12-11 12:07:41 -0800102import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700103import java.util.stream.Stream;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700104
Madan Jampania3770c32015-12-11 12:07:41 -0800105import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
106import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700107import static org.onosproject.provider.host.impl.OsgiPropertyConstants.*;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700108import static org.slf4j.LoggerFactory.getLogger;
109
alshabibe1cf87d2014-10-17 09:23:50 -0700110/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700111 * Provider which uses an OpenFlow controller to detect network end-station
112 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700113 */
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700114@Component(immediate = true, service = HostProvider.class,
115 property = {
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700116 HOST_REMOVAL_ENABLED + ":Boolean=" + HOST_REMOVAL_ENABLED_DEFAULT,
117 REQUEST_ARP + ":Boolean=" + REQUEST_ARP_DEFAULT,
118 REQUEST_NDP + ":Boolean=" + REQUEST_NDP_DEFAULT,
Yi Tseng11f95b52019-05-08 14:03:19 -0700119 REQUEST_NDP_RS_RA + ":Boolean=" + REQUEST_NDP_RS_RA_DEFAULT,
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700120 USE_DHCP + ":Boolean=" + USE_DHCP_DEFAULT,
121 USE_DHCP6 + ":Boolean=" + USE_DHCP6_DEFAULT,
122 REQUEST_INTERCEPTS_ENABLED + ":Boolean=" + REQUEST_INTERCEPTS_ENABLED_DEFAULT,
123 MULTIHOMING_ENABLED + ":Boolean=" + MULTIHOMING_ENABLED_DEFAULT,
Thomas Vachuska4167c3f2018-10-16 07:16:31 -0700124 })
Charles Chanff79dd92018-06-01 16:33:48 -0700125public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700126 private final Logger log = getLogger(getClass());
127
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800129 protected CoreService coreService;
130
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700132 protected HostProviderRegistry providerRegistry;
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800135 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700136
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700138 protected TopologyService topologyService;
139
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800141 protected HostService hostService;
142
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700143 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800144 protected DeviceService deviceService;
145
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700147 protected ComponentConfigService cfgService;
148
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700149 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700150 protected NetworkConfigRegistry registry;
151
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700152 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700153 protected InterfaceService interfaceService;
154
alshabibe1cf87d2014-10-17 09:23:50 -0700155 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800156 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700157 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800158
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800159 private ApplicationId appId;
160
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700161 /** Enable host removal on port/device down events. */
Thomas Vachuska33601602014-11-19 03:32:15 -0800162 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700163
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700164 /** Request ARP packets for neighbor discovery by the Host Location Provider; default is true. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700165 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800166
Yi Tseng11f95b52019-05-08 14:03:19 -0700167 /** Requests IPv6 NDP Neighbor Solicitation and Advertisement by the Host Location Provider; default is false. */
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700168 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800169
Yi Tseng11f95b52019-05-08 14:03:19 -0700170 /** Requests IPv6 NDP Router Solicitation and Advertisement by the Host Location Provider; default is false. */
171 private boolean requestIpv6NdpRsRa = false;
172
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700173 /** Use DHCP to update IP address of the host; default is false. */
Charles Chane6067892016-11-17 10:23:46 -0800174 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700175
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700176 /** Use DHCPv6 to update IP address of the host; default is false. */
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700177 private boolean useDhcp6 = false;
178
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700179 /** Enable requesting packet intercepts. */
Jonathan Hart9af322d2016-01-06 17:42:04 -0800180 private boolean requestInterceptsEnabled = true;
181
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700182 /** Allow hosts to be multihomed. */
Charles Chan35a32322017-08-14 11:42:11 -0700183 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800184
Charles Chanff79dd92018-06-01 16:33:48 -0700185 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800186
Charles Chanff79dd92018-06-01 16:33:48 -0700187 ExecutorService deviceEventHandler;
188 private ExecutorService probeEventHandler;
pierf543ff82019-06-25 14:09:57 +0200189 // Packet workers - 0 will leverage available processors
190 private static final int DEFAULT_THREADS = 0;
191 private PredictableExecutor packetWorkers;
Charles Chan8277b6b2017-12-03 13:48:46 -0800192
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700193 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700194 protected NetworkConfigService netcfgService;
195
196 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
197 new ConfigFactory<ConnectPoint, HostLearningConfig>(
198 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
199 HostLearningConfig.class, "hostLearning") {
200 @Override
201 public HostLearningConfig createConfig() {
202 return new HostLearningConfig();
203 }
204 };
205
alshabibe1cf87d2014-10-17 09:23:50 -0700206 /**
207 * Creates an OpenFlow host provider.
208 */
209 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800210 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700211 }
212
213 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800214 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700215 cfgService.registerProperties(getClass());
216 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700217 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
218 "device-event-handler", log));
219 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
220 "probe-event-handler", log));
pierf543ff82019-06-25 14:09:57 +0200221 packetWorkers = new PredictableExecutor(DEFAULT_THREADS, groupedThreads("onos/host-loc-provider",
222 "packet-worker-%d", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700223 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700224 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800225 deviceService.addListener(deviceListener);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700226 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800227 modified(context);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700228 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800229
Charles M.C. Chane148de82015-05-06 12:38:21 +0800230 log.info("Started with Application ID {}", appId.id());
231 }
232
233 @Deactivate
234 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800235 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700236
237 withdrawIntercepts();
238
Charles M.C. Chane148de82015-05-06 12:38:21 +0800239 providerRegistry.unregister(this);
240 packetService.removeProcessor(processor);
241 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700242 deviceEventHandler.shutdown();
243 probeEventHandler.shutdown();
pierf543ff82019-06-25 14:09:57 +0200244 packetWorkers.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800245 providerService = null;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700246 registry.unregisterConfigFactory(hostLearningConfig);
247 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800248 log.info("Stopped");
249 }
250
251 @Modified
252 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800253 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800254
255 if (requestInterceptsEnabled) {
256 requestIntercepts();
257 } else {
258 withdrawIntercepts();
259 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800260 }
261
262 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700263 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800264 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700265 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800266 // Use ARP
Yi Tseng11f95b52019-05-08 14:03:19 -0700267 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
268 .matchEthType(Ethernet.TYPE_ARP);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700269 if (requestArp) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700270 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700271 } else {
Yi Tseng11f95b52019-05-08 14:03:19 -0700272 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800273 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700274
Yi Tseng11f95b52019-05-08 14:03:19 -0700275 // Use IPv6 NDP Neighbor Solicitation and Advertisement
276 selector.matchEthType(Ethernet.TYPE_IPV6)
277 .matchIPProtocol(IPv6.PROTOCOL_ICMP6);
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700278 if (requestIpv6ND) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700279 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
280 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
281 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
282 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700283 } else {
Yi Tseng11f95b52019-05-08 14:03:19 -0700284 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
285 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
286 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
287 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
288 }
289
290 // Use IPv6 NDP Router Solicitation and Advertisement
291 if (requestIpv6NdpRsRa) {
292 selector.matchIcmpv6Type(ICMP6.ROUTER_SOLICITATION);
293 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
294 selector.matchIcmpv6Type(ICMP6.ROUTER_ADVERTISEMENT);
295 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
296 } else {
297 selector.matchIcmpv6Type(ICMP6.ROUTER_SOLICITATION);
298 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
299 selector.matchIcmpv6Type(ICMP6.ROUTER_ADVERTISEMENT);
300 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Charles Chane6067892016-11-17 10:23:46 -0800301 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700302 }
303
304 /**
305 * Withdraw packet intercepts.
306 */
307 private void withdrawIntercepts() {
308 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
309 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700310 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700311
312 // IPv6 Neighbor Solicitation packet.
313 selector.matchEthType(Ethernet.TYPE_IPV6);
314 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
315 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
316 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
317
318 // IPv6 Neighbor Advertisement packet.
319 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
320 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800321 }
322
323 /**
324 * Extracts properties from the component configuration context.
325 *
326 * @param context the component context
327 */
328 private void readComponentConfiguration(ComponentContext context) {
329 Dictionary<?, ?> properties = context.getProperties();
330 Boolean flag;
331
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700332 flag = Tools.isPropertyEnabled(properties, HOST_REMOVAL_ENABLED);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800333 if (flag == null) {
334 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700335 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800336 } else {
337 hostRemovalEnabled = flag;
338 log.info("Configured. Host removal on port/device down events is {}",
339 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800340 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800341
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700342 flag = Tools.isPropertyEnabled(properties, REQUEST_ARP);
Charles Chane6067892016-11-17 10:23:46 -0800343 if (flag == null) {
344 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700345 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800346 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700347 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800348 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700349 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800350 }
351
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700352 flag = Tools.isPropertyEnabled(properties, REQUEST_NDP);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800353 if (flag == null) {
354 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700355 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800356 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700357 requestIpv6ND = flag;
Yi Tseng11f95b52019-05-08 14:03:19 -0700358 log.info("Configured. Using IPv6 NDP Neighbor Solicitation and Advertisement is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700359 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800360 }
361
Yi Tseng11f95b52019-05-08 14:03:19 -0700362 flag = Tools.isPropertyEnabled(properties, REQUEST_NDP_RS_RA);
363 if (flag == null) {
364 log.info("Using IPv6 Neighbor Discovery is not configured, " +
365 "using current value of {}", requestIpv6NdpRsRa);
366 } else {
367 requestIpv6NdpRsRa = flag;
368 log.info("Configured. Using IPv6 NDP Router Solicitation and Advertisement is {}",
369 requestIpv6NdpRsRa ? "enabled" : "disabled");
370 }
371
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700372 flag = Tools.isPropertyEnabled(properties, USE_DHCP);
Charles Chane6067892016-11-17 10:23:46 -0800373 if (flag == null) {
374 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700375 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800376 } else {
377 useDhcp = flag;
378 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700379 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800380 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800381
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700382 flag = Tools.isPropertyEnabled(properties, USE_DHCP6);
383 if (flag == null) {
384 log.info("Using DHCP6 is not configured, " +
385 "using current value of {}", useDhcp6);
386 } else {
387 useDhcp6 = flag;
388 log.info("Configured. Using DHCP6 is {}",
389 useDhcp6 ? "enabled" : "disabled");
390 }
391
392 flag = Tools.isPropertyEnabled(properties, REQUEST_INTERCEPTS_ENABLED);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800393 if (flag == null) {
394 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700395 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800396 } else {
397 requestInterceptsEnabled = flag;
398 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700399 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800400 }
Charles Chan35a32322017-08-14 11:42:11 -0700401
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700402 flag = Tools.isPropertyEnabled(properties, MULTIHOMING_ENABLED);
Charles Chan35a32322017-08-14 11:42:11 -0700403 if (flag == null) {
404 log.info("Multihoming is not configured, " +
405 "using current value of {}", multihomingEnabled);
406 } else {
407 multihomingEnabled = flag;
408 log.info("Configured. Multihoming is {}",
409 multihomingEnabled ? "enabled" : "disabled");
410 }
Charles Chan47933752017-11-30 15:37:50 -0800411 }
Charles Chan35a32322017-08-14 11:42:11 -0700412
Charles Chan47933752017-11-30 15:37:50 -0800413 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700414 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800415 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700416
417 // FIXME Disabling host probing for now, because sending packets from a
418 // broadcast MAC address caused problems when two ONOS networks were
419 // interconnected. Host probing should take into account the interface
420 // configuration when determining which source address to use.
421
422 //MastershipRole role = deviceService.getRole(host.location().deviceId());
423 //if (role.equals(MastershipRole.MASTER)) {
424 // host.ipAddresses().forEach(ip -> {
425 // sendProbe(host, ip);
426 // });
427 //} else {
428 // log.info("not the master, master will probe {}");
429 //}
sdn94b00152016-08-30 02:12:32 -0700430 }
431
432 private void sendProbe(Host host, IpAddress targetIp) {
433 Ethernet probePacket = null;
434 if (targetIp.isIp4()) {
435 // IPv4: Use ARP
436 probePacket = buildArpRequest(targetIp, host);
437 } else {
438 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700439 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700440 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800441 return;
sdn94b00152016-08-30 02:12:32 -0700442 }
443
444 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
445
446 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
447 ByteBuffer.wrap(probePacket.serialize()));
448
449 packetService.emit(outboundPacket);
450 }
451
Charles Chan888e20a2017-05-01 15:44:23 -0700452 // 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 -0700453 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700454 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
455 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
456 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700457 }
458
459 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800460 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700461 * Create or update host information.
462 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800463 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700464 * @param hid host ID
465 * @param mac source Mac address
466 * @param vlan VLAN ID
467 * @param innerVlan inner VLAN ID
468 * @param outerTpid outer TPID
469 * @param hloc host location
470 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800471 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700472 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
473 VlanId innerVlan, EthType outerTpid,
474 HostLocation hloc, IpAddress ip) {
Andrea Campanella76da0292020-09-23 11:42:21 +0200475 log.debug("Creating Host {} based on Location {}", hid, hloc);
Charles Chan35a32322017-08-14 11:42:11 -0700476 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
477
478 if (multihomingEnabled) {
479 Host existingHost = hostService.getHost(hid);
480 if (existingHost != null) {
481 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700482
Charles Chan653e2ac2017-11-22 14:49:55 -0800483 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
484 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700485 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800486 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800487 } else {
488 // Move within the same switch
489 // Simply replace old location that is on the same device
490 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
491 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700492 }
493 }
494 }
495
Charles Chan888e20a2017-05-01 15:44:23 -0700496 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700497 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
498 innerVlan, outerTpid, false) :
499 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
500 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700501 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800502 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700503 } catch (IllegalStateException e) {
504 log.debug("Host {} suppressed", hid);
505 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800506 }
alshabibe1cf87d2014-10-17 09:23:50 -0700507
Charles Chane6067892016-11-17 10:23:46 -0800508 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700509 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800510 *
511 * @param hid host ID
512 * @param ip IP address
513 */
Charles Chan888e20a2017-05-01 15:44:23 -0700514 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800515 Host host = hostService.getHost(hid);
516 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700517 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800518 return;
519 }
520
Charles Chan888e20a2017-05-01 15:44:23 -0700521 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700522 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800523 try {
524 providerService.hostDetected(hid, desc, false);
525 } catch (IllegalStateException e) {
526 log.debug("Host {} suppressed", hid);
527 }
528 }
529
alshabibe1cf87d2014-10-17 09:23:50 -0700530 @Override
531 public void process(PacketContext context) {
pierf543ff82019-06-25 14:09:57 +0200532 // Verify valid context
alshabib4a179dc2014-10-17 17:17:01 -0700533 if (context == null) {
534 return;
535 }
pierf543ff82019-06-25 14:09:57 +0200536 // Verify valid Ethernet packet
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700537 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800538 if (eth == null) {
539 return;
540 }
pierf543ff82019-06-25 14:09:57 +0200541 // Dispatch to a worker thread
542 HostId hostId = HostId.hostId(eth.getSourceMAC(), VlanId.vlanId(eth.getVlanID()));
543 packetWorkers.execute(() -> processPacketInternal(context), hostId.hashCode());
544 }
545
546 private void processPacketInternal(PacketContext context) {
547 Ethernet eth = context.inPacket().parsed();
Jonathan Hartf353e402016-09-23 09:27:36 -0700548
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800549 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700550 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
551 return;
552 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800553
alshabibe1cf87d2014-10-17 09:23:50 -0700554 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700555 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
556 VlanId innerVlan = VlanId.NONE;
557 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
558 // Set up values for double-tagged hosts
559 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
560 innerVlan = vlan;
561 vlan = outerVlan;
562 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
563 }
alshabibe1cf87d2014-10-17 09:23:50 -0700564 ConnectPoint heardOn = context.inPacket().receivedFrom();
565
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700566 // If this arrived on control port, bail out.
567 if (heardOn.port().isLogical()) {
568 return;
569 }
570
alshabibe1cf87d2014-10-17 09:23:50 -0700571 // If this is not an edge port, bail out.
572 Topology topology = topologyService.currentTopology();
573 if (topologyService.isInfrastructure(topology, heardOn)) {
574 return;
575 }
576
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700577 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700578 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700579 MacAddress destMac = eth.getDestinationMAC();
580
Charles Chanff79dd92018-06-01 16:33:48 -0700581 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700582 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700583 return;
584 }
alshabibe1cf87d2014-10-17 09:23:50 -0700585
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700586 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
587 // if learning is disabled bail out.
588 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
589 log.debug("Learning disabled for {}, abort.", heardOn);
590 return;
591 }
592
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800593 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700594 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
595 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800596 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
597 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700598 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700599
Charles Chan5d5e0622015-09-25 13:00:06 -0700600 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700601 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700602 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700603 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700604 if (useDhcp) {
605 DHCP dhcp = findDhcp(eth).orElse(null);
606 // DHCP ACK: additionally update IP of DHCP client
607 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
608 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
609 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
610 HostId hostId = HostId.hostId(hostMac, hostVlan);
611 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700612 }
Charles Chane6067892016-11-17 10:23:46 -0800613 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700614 // NeighborAdvertisement and NeighborSolicitation: possible
615 // new hosts, update both location and IP.
616 //
617 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800618 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800619 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800620 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700621 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800622
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800623 // skip extension headers
624 IPacket pkt = ipv6;
625 while (pkt.getPayload() != null &&
626 pkt.getPayload() instanceof IExtensionHeader) {
627 pkt = pkt.getPayload();
628 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800629 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700630
631 // DHCPv6 protocol
632 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
633 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700634 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700635 handleDhcp6(dhcp6, vlan);
636 return;
637 }
638
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800639 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700640 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800641 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400642 if (pkt != null) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700643 if ((pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation)) {
644 if (ip.isZero()) {
645 return;
646 }
647 // RouterSolicitation, RouterAdvertisement
648 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800649 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800650 }
Charles Chan178b0f52017-10-08 23:51:22 -0400651 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
652 // Duplicate Address Detection
653 if (ip.isZero()) {
654 return;
655 }
656 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700657 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400658
659 // Also learn from the target address of NeighborAdvertisement
660 if (pkt instanceof NeighborAdvertisement) {
661 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
662 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700663 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400664 }
665 return;
666 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800667 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800668 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800669
Yi Tsengaa417a62017-09-08 17:22:51 -0700670 // multicast, exclude DHCPv6
671 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800672 return;
673 }
674
675 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700676 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700677 }
678 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700679
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700680 /**
681 * Handles DHCPv6 packet, if message type is ACK, update IP address
682 * according to DHCPv6 payload (IA Address option).
683 *
684 * @param dhcp6 the DHCPv6 payload
685 * @param vlanId the vlan of this packet
686 */
687 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
688 // extract the relay message if exist
689 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
690 dhcp6 = dhcp6.getOptions().stream()
691 .filter(opt -> opt instanceof Dhcp6RelayOption)
692 .map(BasePacket::getPayload)
693 .map(pld -> (DHCP6) pld)
694 .findFirst()
695 .orElse(null);
696 }
697
698 if (dhcp6 == null) {
699 // Can't find dhcp payload
700 log.warn("Can't find dhcp payload from relay message");
701 return;
702 }
703
704 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
705 // Update IP address only when we received REPLY message
706 return;
707 }
708 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
709 .stream()
710 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
711 .map(opt -> (Dhcp6ClientIdOption) opt)
712 .findFirst();
713
714 if (!clientIdOption.isPresent()) {
715 // invalid DHCPv6 option
716 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
717 return;
718 }
719
720 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
721 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
722 // No any mac address found
723 log.warn("Can't find client mac from option {}", clientIdOption);
724 return;
725 }
726 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
727
728 // Extract IPv6 address from IA NA ot IA TA option
729 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
730 .stream()
731 .filter(opt -> opt instanceof Dhcp6IaNaOption)
732 .map(opt -> (Dhcp6IaNaOption) opt)
733 .findFirst();
734 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
735 .stream()
736 .filter(opt -> opt instanceof Dhcp6IaTaOption)
737 .map(opt -> (Dhcp6IaTaOption) opt)
738 .findFirst();
739 Optional<Dhcp6IaAddressOption> iaAddressOption;
740 if (iaNaOption.isPresent()) {
741 iaAddressOption = iaNaOption.get().getOptions().stream()
742 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
743 .map(opt -> (Dhcp6IaAddressOption) opt)
744 .findFirst();
745 } else if (iaTaOption.isPresent()) {
746 iaAddressOption = iaTaOption.get().getOptions().stream()
747 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
748 .map(opt -> (Dhcp6IaAddressOption) opt)
749 .findFirst();
750 } else {
751 iaAddressOption = Optional.empty();
752 }
753 if (iaAddressOption.isPresent()) {
754 Ip6Address ip = iaAddressOption.get().getIp6Address();
755 HostId hostId = HostId.hostId(clientMac, vlanId);
756 updateHostIp(hostId, ip);
757 } else {
758 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
759 }
760 }
761
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700762 private Optional<DHCP> findDhcp(Ethernet eth) {
763 IPacket pkt = eth.getPayload();
764 return Stream.of(pkt)
765 .filter(Objects::nonNull)
766 .filter(p -> p instanceof IPv4)
767 .map(IPacket::getPayload)
768 .filter(Objects::nonNull)
769 .filter(p -> p instanceof UDP)
770 .map(IPacket::getPayload)
771 .filter(Objects::nonNull)
772 .filter(p -> p instanceof DHCP)
773 .map(p -> (DHCP) p)
774 .findFirst();
775 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700776
777 private Optional<DHCP6> findDhcp6(IPacket pkt) {
778 return Stream.of(pkt)
779 .filter(Objects::nonNull)
780 .filter(p -> p instanceof UDP)
781 .map(IPacket::getPayload)
782 .filter(Objects::nonNull)
783 .filter(p -> p instanceof DHCP6)
784 .map(p -> (DHCP6) p)
785 .findFirst();
786 }
alshabibe1cf87d2014-10-17 09:23:50 -0700787 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800788
789 // Auxiliary listener to device events.
790 private class InternalDeviceListener implements DeviceListener {
791 @Override
792 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700793 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800794 }
795
796 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800797 Device device = event.subject();
798 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700799 case DEVICE_ADDED:
800 break;
801 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700802 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
803 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700804 }
805 break;
806 case DEVICE_SUSPENDED:
807 case DEVICE_UPDATED:
808 // Nothing to do?
809 break;
810 case DEVICE_REMOVED:
811 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700812 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700813 }
814 break;
815 case PORT_ADDED:
816 break;
817 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700818 if (hostRemovalEnabled && !event.port().isEnabled()) {
819 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700820 }
821 break;
822 case PORT_REMOVED:
enen923305efe2018-07-03 23:16:53 +0100823 if (hostRemovalEnabled) {
824 processPortDown(new ConnectPoint(device.id(), event.port().number()));
825 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700826 break;
827 default:
828 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800829 }
830 }
831 }
832
Charles Chan888e20a2017-05-01 15:44:23 -0700833 /**
834 * When a device goes down, update the location of affected hosts.
835 *
836 * @param deviceId the device that goes down
837 */
838 private void processDeviceDown(DeviceId deviceId) {
839 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
840 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
841 .forEach(affectedLocation ->
842 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
843 );
844 }
845
846 /**
847 * When a port goes down, update the location of affected hosts.
848 *
849 * @param connectPoint the port that goes down
850 */
851 private void processPortDown(ConnectPoint connectPoint) {
852 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
853 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
854 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800855 }
856
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700857
858 private class InternalConfigListener implements NetworkConfigListener {
859
860 @Override
861 public void event(NetworkConfigEvent event) {
862 switch (event.type()) {
863 case CONFIG_ADDED:
864 case CONFIG_UPDATED:
865 log.debug("HostLearningConfig event of type {}", event.type());
866 // if learning enabled do nothing
867 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
868 if (learningConfig.hostLearningEnabled()) {
869 return;
870 }
871
872 // if host learning is disable remove this location from existing, learnt hosts
873 ConnectPoint connectPoint = learningConfig.subject();
874 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
875 for (Host host : connectedHosts) {
876 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
877
878 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
879 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
880 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
881 }
882 }
883 break;
884 case CONFIG_REMOVED:
885 default:
886 break;
887 }
888 }
889
890 @Override
891 public boolean isRelevant(NetworkConfigEvent event) {
892 if (!event.configClass().equals(HostLearningConfig.class)) {
893 return false;
894 }
895 return true;
896 }
897 }
alshabibe1cf87d2014-10-17 09:23:50 -0700898}