blob: a6b9bb0b440a21bc51cf694938a15bbce25e6b2d [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) {
Charles Chan35a32322017-08-14 11:42:11 -0700475 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
476
477 if (multihomingEnabled) {
478 Host existingHost = hostService.getHost(hid);
479 if (existingHost != null) {
480 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700481
Charles Chan653e2ac2017-11-22 14:49:55 -0800482 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
483 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700484 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800485 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800486 } else {
487 // Move within the same switch
488 // Simply replace old location that is on the same device
489 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
490 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700491 }
492 }
493 }
494
Charles Chan888e20a2017-05-01 15:44:23 -0700495 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700496 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
497 innerVlan, outerTpid, false) :
498 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
499 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700500 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800501 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700502 } catch (IllegalStateException e) {
503 log.debug("Host {} suppressed", hid);
504 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800505 }
alshabibe1cf87d2014-10-17 09:23:50 -0700506
Charles Chane6067892016-11-17 10:23:46 -0800507 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700508 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800509 *
510 * @param hid host ID
511 * @param ip IP address
512 */
Charles Chan888e20a2017-05-01 15:44:23 -0700513 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800514 Host host = hostService.getHost(hid);
515 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700516 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800517 return;
518 }
519
Charles Chan888e20a2017-05-01 15:44:23 -0700520 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700521 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800522 try {
523 providerService.hostDetected(hid, desc, false);
524 } catch (IllegalStateException e) {
525 log.debug("Host {} suppressed", hid);
526 }
527 }
528
alshabibe1cf87d2014-10-17 09:23:50 -0700529 @Override
530 public void process(PacketContext context) {
pierf543ff82019-06-25 14:09:57 +0200531 // Verify valid context
alshabib4a179dc2014-10-17 17:17:01 -0700532 if (context == null) {
533 return;
534 }
pierf543ff82019-06-25 14:09:57 +0200535 // Verify valid Ethernet packet
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 }
pierf543ff82019-06-25 14:09:57 +0200540 // Dispatch to a worker thread
541 HostId hostId = HostId.hostId(eth.getSourceMAC(), VlanId.vlanId(eth.getVlanID()));
542 packetWorkers.execute(() -> processPacketInternal(context), hostId.hashCode());
543 }
544
545 private void processPacketInternal(PacketContext context) {
546 Ethernet eth = context.inPacket().parsed();
Jonathan Hartf353e402016-09-23 09:27:36 -0700547
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800548 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700549 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
550 return;
551 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800552
alshabibe1cf87d2014-10-17 09:23:50 -0700553 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700554 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
555 VlanId innerVlan = VlanId.NONE;
556 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
557 // Set up values for double-tagged hosts
558 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
559 innerVlan = vlan;
560 vlan = outerVlan;
561 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
562 }
alshabibe1cf87d2014-10-17 09:23:50 -0700563 ConnectPoint heardOn = context.inPacket().receivedFrom();
564
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700565 // If this arrived on control port, bail out.
566 if (heardOn.port().isLogical()) {
567 return;
568 }
569
alshabibe1cf87d2014-10-17 09:23:50 -0700570 // If this is not an edge port, bail out.
571 Topology topology = topologyService.currentTopology();
572 if (topologyService.isInfrastructure(topology, heardOn)) {
573 return;
574 }
575
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700576 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700577 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700578 MacAddress destMac = eth.getDestinationMAC();
579
Charles Chanff79dd92018-06-01 16:33:48 -0700580 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700581 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700582 return;
583 }
alshabibe1cf87d2014-10-17 09:23:50 -0700584
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700585 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
586 // if learning is disabled bail out.
587 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
588 log.debug("Learning disabled for {}, abort.", heardOn);
589 return;
590 }
591
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800592 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700593 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
594 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800595 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
596 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700597 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700598
Charles Chan5d5e0622015-09-25 13:00:06 -0700599 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700600 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700601 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700602 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700603 if (useDhcp) {
604 DHCP dhcp = findDhcp(eth).orElse(null);
605 // DHCP ACK: additionally update IP of DHCP client
606 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
607 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
608 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
609 HostId hostId = HostId.hostId(hostMac, hostVlan);
610 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700611 }
Charles Chane6067892016-11-17 10:23:46 -0800612 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700613 // NeighborAdvertisement and NeighborSolicitation: possible
614 // new hosts, update both location and IP.
615 //
616 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800617 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800618 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800619 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700620 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800621
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800622 // skip extension headers
623 IPacket pkt = ipv6;
624 while (pkt.getPayload() != null &&
625 pkt.getPayload() instanceof IExtensionHeader) {
626 pkt = pkt.getPayload();
627 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800628 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700629
630 // DHCPv6 protocol
631 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
632 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700633 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700634 handleDhcp6(dhcp6, vlan);
635 return;
636 }
637
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800638 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700639 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800640 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400641 if (pkt != null) {
Yi Tseng11f95b52019-05-08 14:03:19 -0700642 if ((pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation)) {
643 if (ip.isZero()) {
644 return;
645 }
646 // RouterSolicitation, RouterAdvertisement
647 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800648 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800649 }
Charles Chan178b0f52017-10-08 23:51:22 -0400650 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
651 // Duplicate Address Detection
652 if (ip.isZero()) {
653 return;
654 }
655 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700656 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400657
658 // Also learn from the target address of NeighborAdvertisement
659 if (pkt instanceof NeighborAdvertisement) {
660 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
661 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700662 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400663 }
664 return;
665 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800666 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800667 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800668
Yi Tsengaa417a62017-09-08 17:22:51 -0700669 // multicast, exclude DHCPv6
670 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800671 return;
672 }
673
674 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700675 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700676 }
677 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700678
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700679 /**
680 * Handles DHCPv6 packet, if message type is ACK, update IP address
681 * according to DHCPv6 payload (IA Address option).
682 *
683 * @param dhcp6 the DHCPv6 payload
684 * @param vlanId the vlan of this packet
685 */
686 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
687 // extract the relay message if exist
688 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
689 dhcp6 = dhcp6.getOptions().stream()
690 .filter(opt -> opt instanceof Dhcp6RelayOption)
691 .map(BasePacket::getPayload)
692 .map(pld -> (DHCP6) pld)
693 .findFirst()
694 .orElse(null);
695 }
696
697 if (dhcp6 == null) {
698 // Can't find dhcp payload
699 log.warn("Can't find dhcp payload from relay message");
700 return;
701 }
702
703 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
704 // Update IP address only when we received REPLY message
705 return;
706 }
707 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
708 .stream()
709 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
710 .map(opt -> (Dhcp6ClientIdOption) opt)
711 .findFirst();
712
713 if (!clientIdOption.isPresent()) {
714 // invalid DHCPv6 option
715 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
716 return;
717 }
718
719 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
720 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
721 // No any mac address found
722 log.warn("Can't find client mac from option {}", clientIdOption);
723 return;
724 }
725 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
726
727 // Extract IPv6 address from IA NA ot IA TA option
728 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
729 .stream()
730 .filter(opt -> opt instanceof Dhcp6IaNaOption)
731 .map(opt -> (Dhcp6IaNaOption) opt)
732 .findFirst();
733 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
734 .stream()
735 .filter(opt -> opt instanceof Dhcp6IaTaOption)
736 .map(opt -> (Dhcp6IaTaOption) opt)
737 .findFirst();
738 Optional<Dhcp6IaAddressOption> iaAddressOption;
739 if (iaNaOption.isPresent()) {
740 iaAddressOption = iaNaOption.get().getOptions().stream()
741 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
742 .map(opt -> (Dhcp6IaAddressOption) opt)
743 .findFirst();
744 } else if (iaTaOption.isPresent()) {
745 iaAddressOption = iaTaOption.get().getOptions().stream()
746 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
747 .map(opt -> (Dhcp6IaAddressOption) opt)
748 .findFirst();
749 } else {
750 iaAddressOption = Optional.empty();
751 }
752 if (iaAddressOption.isPresent()) {
753 Ip6Address ip = iaAddressOption.get().getIp6Address();
754 HostId hostId = HostId.hostId(clientMac, vlanId);
755 updateHostIp(hostId, ip);
756 } else {
757 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
758 }
759 }
760
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700761 private Optional<DHCP> findDhcp(Ethernet eth) {
762 IPacket pkt = eth.getPayload();
763 return Stream.of(pkt)
764 .filter(Objects::nonNull)
765 .filter(p -> p instanceof IPv4)
766 .map(IPacket::getPayload)
767 .filter(Objects::nonNull)
768 .filter(p -> p instanceof UDP)
769 .map(IPacket::getPayload)
770 .filter(Objects::nonNull)
771 .filter(p -> p instanceof DHCP)
772 .map(p -> (DHCP) p)
773 .findFirst();
774 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700775
776 private Optional<DHCP6> findDhcp6(IPacket pkt) {
777 return Stream.of(pkt)
778 .filter(Objects::nonNull)
779 .filter(p -> p instanceof UDP)
780 .map(IPacket::getPayload)
781 .filter(Objects::nonNull)
782 .filter(p -> p instanceof DHCP6)
783 .map(p -> (DHCP6) p)
784 .findFirst();
785 }
alshabibe1cf87d2014-10-17 09:23:50 -0700786 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800787
788 // Auxiliary listener to device events.
789 private class InternalDeviceListener implements DeviceListener {
790 @Override
791 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700792 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800793 }
794
795 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800796 Device device = event.subject();
797 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700798 case DEVICE_ADDED:
799 break;
800 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700801 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
802 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700803 }
804 break;
805 case DEVICE_SUSPENDED:
806 case DEVICE_UPDATED:
807 // Nothing to do?
808 break;
809 case DEVICE_REMOVED:
810 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700811 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700812 }
813 break;
814 case PORT_ADDED:
815 break;
816 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700817 if (hostRemovalEnabled && !event.port().isEnabled()) {
818 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700819 }
820 break;
821 case PORT_REMOVED:
enen923305efe2018-07-03 23:16:53 +0100822 if (hostRemovalEnabled) {
823 processPortDown(new ConnectPoint(device.id(), event.port().number()));
824 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700825 break;
826 default:
827 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800828 }
829 }
830 }
831
Charles Chan888e20a2017-05-01 15:44:23 -0700832 /**
833 * When a device goes down, update the location of affected hosts.
834 *
835 * @param deviceId the device that goes down
836 */
837 private void processDeviceDown(DeviceId deviceId) {
838 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
839 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
840 .forEach(affectedLocation ->
841 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
842 );
843 }
844
845 /**
846 * When a port goes down, update the location of affected hosts.
847 *
848 * @param connectPoint the port that goes down
849 */
850 private void processPortDown(ConnectPoint connectPoint) {
851 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
852 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
853 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800854 }
855
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700856
857 private class InternalConfigListener implements NetworkConfigListener {
858
859 @Override
860 public void event(NetworkConfigEvent event) {
861 switch (event.type()) {
862 case CONFIG_ADDED:
863 case CONFIG_UPDATED:
864 log.debug("HostLearningConfig event of type {}", event.type());
865 // if learning enabled do nothing
866 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
867 if (learningConfig.hostLearningEnabled()) {
868 return;
869 }
870
871 // if host learning is disable remove this location from existing, learnt hosts
872 ConnectPoint connectPoint = learningConfig.subject();
873 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
874 for (Host host : connectedHosts) {
875 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
876
877 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
878 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
879 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
880 }
881 }
882 break;
883 case CONFIG_REMOVED:
884 default:
885 break;
886 }
887 }
888
889 @Override
890 public boolean isRelevant(NetworkConfigEvent event) {
891 if (!event.configClass().equals(HostLearningConfig.class)) {
892 return false;
893 }
894 return true;
895 }
896 }
alshabibe1cf87d2014-10-17 09:23:50 -0700897}