blob: 434cefcead7f24c628e30b72deac6752f9b0fc56 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.host.impl;
alshabibe1cf87d2014-10-17 09:23:50 -070017
Charles Chan35a32322017-08-14 11:42:11 -070018import com.google.common.collect.Sets;
Jonathan Harte8600eb2015-01-12 10:30:45 -080019import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070020import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080021import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070022import org.onlab.packet.DHCP6;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070023import org.onlab.packet.EthType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080024import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080025import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070026import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080027import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080028import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070029import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070030import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080031import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080032import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080033import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070035import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
36import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
37import org.onlab.packet.dhcp.Dhcp6IaNaOption;
38import org.onlab.packet.dhcp.Dhcp6IaTaOption;
39import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080040import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080041import org.onlab.packet.ndp.NeighborAdvertisement;
42import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080043import org.onlab.packet.ndp.RouterAdvertisement;
44import org.onlab.packet.ndp.RouterSolicitation;
Jian Lid9b5f552016-03-11 18:15:31 -080045import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070046import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080047import org.onosproject.core.ApplicationId;
48import org.onosproject.core.CoreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049import org.onosproject.net.ConnectPoint;
50import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
52import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
54import org.onosproject.net.HostLocation;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -070055import org.onosproject.net.config.ConfigFactory;
56import org.onosproject.net.config.NetworkConfigEvent;
57import org.onosproject.net.config.NetworkConfigListener;
58import org.onosproject.net.config.NetworkConfigRegistry;
59import org.onosproject.net.config.NetworkConfigService;
60import org.onosproject.net.config.basics.BasicHostConfig;
61import org.onosproject.net.config.basics.HostLearningConfig;
62import org.onosproject.net.config.basics.SubjectFactories;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.device.DeviceEvent;
64import org.onosproject.net.device.DeviceListener;
65import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080066import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070067import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080068import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070069import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.host.DefaultHostDescription;
71import org.onosproject.net.host.HostDescription;
72import org.onosproject.net.host.HostProvider;
73import org.onosproject.net.host.HostProviderRegistry;
74import org.onosproject.net.host.HostProviderService;
75import org.onosproject.net.host.HostService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076import org.onosproject.net.intf.InterfaceService;
sdn94b00152016-08-30 02:12:32 -070077import org.onosproject.net.packet.DefaultOutboundPacket;
78import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080080import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080081import org.onosproject.net.packet.PacketProcessor;
82import org.onosproject.net.packet.PacketService;
83import org.onosproject.net.provider.AbstractProvider;
84import org.onosproject.net.provider.ProviderId;
85import org.onosproject.net.topology.Topology;
86import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080087import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088import org.osgi.service.component.annotations.Activate;
89import org.osgi.service.component.annotations.Component;
90import org.osgi.service.component.annotations.Deactivate;
91import org.osgi.service.component.annotations.Modified;
92import org.osgi.service.component.annotations.Reference;
93import org.osgi.service.component.annotations.ReferenceCardinality;
alshabibe1cf87d2014-10-17 09:23:50 -070094import org.slf4j.Logger;
95
sdn94b00152016-08-30 02:12:32 -070096import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070097import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -070098import java.util.Objects;
99import java.util.Optional;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100import java.util.Set;
Madan Jampania3770c32015-12-11 12:07:41 -0800101import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700102import java.util.stream.Stream;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700103
Madan Jampania3770c32015-12-11 12:07:41 -0800104import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
105import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700106import static org.slf4j.LoggerFactory.getLogger;
107
alshabibe1cf87d2014-10-17 09:23:50 -0700108/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700109 * Provider which uses an OpenFlow controller to detect network end-station
110 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700111 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112@Component(immediate = true, service = HostProvider.class)
Charles Chanff79dd92018-06-01 16:33:48 -0700113public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700114 private final Logger log = getLogger(getClass());
115
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800117 protected CoreService coreService;
118
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700120 protected HostProviderRegistry providerRegistry;
121
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800123 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
alshabibe1cf87d2014-10-17 09:23:50 -0700126 protected TopologyService topologyService;
127
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800129 protected HostService hostService;
130
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska33601602014-11-19 03:32:15 -0800132 protected DeviceService deviceService;
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700135 protected ComponentConfigService cfgService;
136
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700138 protected NetworkConfigRegistry registry;
139
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700141 protected InterfaceService interfaceService;
142
alshabibe1cf87d2014-10-17 09:23:50 -0700143 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800144 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700145 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800146
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800147 private ApplicationId appId;
148
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700149 //@Property(name = "hostRemovalEnabled", boolValue = true,
150 // label = "Enable host removal on port/device down events")
Thomas Vachuska33601602014-11-19 03:32:15 -0800151 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700152
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700153 //@Property(name = "requestArp", boolValue = true,
154 // label = "Request ARP packets for neighbor discovery by the " +
155 // "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700156 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800157
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700158 //@Property(name = "requestIpv6ND", boolValue = false,
159 // label = "Requests IPv6 Neighbor Discovery by the " +
160 // "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700161 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800162
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700163 //@Property(name = "useDhcp", boolValue = false,
164 // label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800165 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700166
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700167 //@Property(name = "useDhcp6", boolValue = false,
168 // label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700169 private boolean useDhcp6 = false;
170
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700171 //@Property(name = "requestInterceptsEnabled", boolValue = true,
172 // label = "Enable requesting packet intercepts")
Jonathan Hart9af322d2016-01-06 17:42:04 -0800173 private boolean requestInterceptsEnabled = true;
174
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700175 //@Property(name = "multihomingEnabled", boolValue = false,
176 // label = "Allow hosts to be multihomed")
Charles Chan35a32322017-08-14 11:42:11 -0700177 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800178
Charles Chanff79dd92018-06-01 16:33:48 -0700179 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800180
Charles Chanff79dd92018-06-01 16:33:48 -0700181 ExecutorService deviceEventHandler;
182 private ExecutorService probeEventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700183 private ExecutorService packetHandler;
Charles Chan8277b6b2017-12-03 13:48:46 -0800184
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700185 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700186 protected NetworkConfigService netcfgService;
187
188 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
189 new ConfigFactory<ConnectPoint, HostLearningConfig>(
190 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
191 HostLearningConfig.class, "hostLearning") {
192 @Override
193 public HostLearningConfig createConfig() {
194 return new HostLearningConfig();
195 }
196 };
197
alshabibe1cf87d2014-10-17 09:23:50 -0700198 /**
199 * Creates an OpenFlow host provider.
200 */
201 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800202 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700203 }
204
205 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800206 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700207 cfgService.registerProperties(getClass());
208 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700209 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
210 "device-event-handler", log));
211 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
212 "probe-event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700213 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
214 "packet-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700215 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700216 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800217 deviceService.addListener(deviceListener);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700218 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800219 modified(context);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700220 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800221
Charles M.C. Chane148de82015-05-06 12:38:21 +0800222 log.info("Started with Application ID {}", appId.id());
223 }
224
225 @Deactivate
226 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800227 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700228
229 withdrawIntercepts();
230
Charles M.C. Chane148de82015-05-06 12:38:21 +0800231 providerRegistry.unregister(this);
232 packetService.removeProcessor(processor);
233 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700234 deviceEventHandler.shutdown();
235 probeEventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700236 packetHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800237 providerService = null;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700238 registry.unregisterConfigFactory(hostLearningConfig);
239 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800240 log.info("Stopped");
241 }
242
243 @Modified
244 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800245 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800246
247 if (requestInterceptsEnabled) {
248 requestIntercepts();
249 } else {
250 withdrawIntercepts();
251 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800252 }
253
254 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700255 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800256 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700257 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800258 // Use ARP
259 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
260 .matchEthType(Ethernet.TYPE_ARP)
261 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700262 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800263 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700264 } else {
Charles Chane6067892016-11-17 10:23:46 -0800265 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800266 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700267
Charles Chane6067892016-11-17 10:23:46 -0800268 // Use IPv6 Neighbor Discovery
269 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
270 .matchEthType(Ethernet.TYPE_IPV6)
271 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
272 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
273 .build();
274 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
275 .matchEthType(Ethernet.TYPE_IPV6)
276 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
277 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
278 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700279 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800280 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
281 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700282 } else {
Charles Chane6067892016-11-17 10:23:46 -0800283 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
284 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
285 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700286 }
287
288 /**
289 * Withdraw packet intercepts.
290 */
291 private void withdrawIntercepts() {
292 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
293 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700294 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700295
296 // IPv6 Neighbor Solicitation packet.
297 selector.matchEthType(Ethernet.TYPE_IPV6);
298 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
299 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
300 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
301
302 // IPv6 Neighbor Advertisement packet.
303 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
304 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800305 }
306
307 /**
308 * Extracts properties from the component configuration context.
309 *
310 * @param context the component context
311 */
312 private void readComponentConfiguration(ComponentContext context) {
313 Dictionary<?, ?> properties = context.getProperties();
314 Boolean flag;
315
Jian Lid9b5f552016-03-11 18:15:31 -0800316 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800317 if (flag == null) {
318 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700319 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800320 } else {
321 hostRemovalEnabled = flag;
322 log.info("Configured. Host removal on port/device down events is {}",
323 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800324 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800325
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700326 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800327 if (flag == null) {
328 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700329 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800330 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700331 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800332 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700333 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800334 }
335
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700336 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800337 if (flag == null) {
338 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700339 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800340 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700341 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800342 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700343 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800344 }
345
346 flag = Tools.isPropertyEnabled(properties, "useDhcp");
347 if (flag == null) {
348 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700349 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800350 } else {
351 useDhcp = flag;
352 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700353 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800354 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800355
Jian Lid9b5f552016-03-11 18:15:31 -0800356 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800357 if (flag == null) {
358 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700359 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800360 } else {
361 requestInterceptsEnabled = flag;
362 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700363 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800364 }
Charles Chan35a32322017-08-14 11:42:11 -0700365
366 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
367 if (flag == null) {
368 log.info("Multihoming is not configured, " +
369 "using current value of {}", multihomingEnabled);
370 } else {
371 multihomingEnabled = flag;
372 log.info("Configured. Multihoming is {}",
373 multihomingEnabled ? "enabled" : "disabled");
374 }
Charles Chan47933752017-11-30 15:37:50 -0800375 }
Charles Chan35a32322017-08-14 11:42:11 -0700376
Charles Chan47933752017-11-30 15:37:50 -0800377 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700378 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800379 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700380
381 // FIXME Disabling host probing for now, because sending packets from a
382 // broadcast MAC address caused problems when two ONOS networks were
383 // interconnected. Host probing should take into account the interface
384 // configuration when determining which source address to use.
385
386 //MastershipRole role = deviceService.getRole(host.location().deviceId());
387 //if (role.equals(MastershipRole.MASTER)) {
388 // host.ipAddresses().forEach(ip -> {
389 // sendProbe(host, ip);
390 // });
391 //} else {
392 // log.info("not the master, master will probe {}");
393 //}
sdn94b00152016-08-30 02:12:32 -0700394 }
395
396 private void sendProbe(Host host, IpAddress targetIp) {
397 Ethernet probePacket = null;
398 if (targetIp.isIp4()) {
399 // IPv4: Use ARP
400 probePacket = buildArpRequest(targetIp, host);
401 } else {
402 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700403 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700404 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800405 return;
sdn94b00152016-08-30 02:12:32 -0700406 }
407
408 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
409
410 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
411 ByteBuffer.wrap(probePacket.serialize()));
412
413 packetService.emit(outboundPacket);
414 }
415
Charles Chan888e20a2017-05-01 15:44:23 -0700416 // 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 -0700417 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700418 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
419 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
420 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700421 }
422
423 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800424 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700425 * Create or update host information.
426 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800427 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700428 * @param hid host ID
429 * @param mac source Mac address
430 * @param vlan VLAN ID
431 * @param innerVlan inner VLAN ID
432 * @param outerTpid outer TPID
433 * @param hloc host location
434 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800435 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700436 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
437 VlanId innerVlan, EthType outerTpid,
438 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700439 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
440
441 if (multihomingEnabled) {
442 Host existingHost = hostService.getHost(hid);
443 if (existingHost != null) {
444 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700445
Charles Chan653e2ac2017-11-22 14:49:55 -0800446 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
447 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700448 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800449 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800450 } else {
451 // Move within the same switch
452 // Simply replace old location that is on the same device
453 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
454 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700455 }
456 }
457 }
458
Charles Chan888e20a2017-05-01 15:44:23 -0700459 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700460 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
461 innerVlan, outerTpid, false) :
462 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
463 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700464 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800465 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700466 } catch (IllegalStateException e) {
467 log.debug("Host {} suppressed", hid);
468 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800469 }
alshabibe1cf87d2014-10-17 09:23:50 -0700470
Charles Chane6067892016-11-17 10:23:46 -0800471 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700472 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800473 *
474 * @param hid host ID
475 * @param ip IP address
476 */
Charles Chan888e20a2017-05-01 15:44:23 -0700477 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800478 Host host = hostService.getHost(hid);
479 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700480 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800481 return;
482 }
483
Charles Chan888e20a2017-05-01 15:44:23 -0700484 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700485 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800486 try {
487 providerService.hostDetected(hid, desc, false);
488 } catch (IllegalStateException e) {
489 log.debug("Host {} suppressed", hid);
490 }
491 }
492
alshabibe1cf87d2014-10-17 09:23:50 -0700493 @Override
494 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700495 packetHandler.execute(() -> processPacketInternal(context));
496 }
497
498 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700499 if (context == null) {
500 return;
501 }
alshabibe1cf87d2014-10-17 09:23:50 -0700502
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700503 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800504 if (eth == null) {
505 return;
506 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700507
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800508 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700509 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
510 return;
511 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800512
alshabibe1cf87d2014-10-17 09:23:50 -0700513 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700514 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
515 VlanId innerVlan = VlanId.NONE;
516 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
517 // Set up values for double-tagged hosts
518 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
519 innerVlan = vlan;
520 vlan = outerVlan;
521 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
522 }
alshabibe1cf87d2014-10-17 09:23:50 -0700523 ConnectPoint heardOn = context.inPacket().receivedFrom();
524
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700525 // If this arrived on control port, bail out.
526 if (heardOn.port().isLogical()) {
527 return;
528 }
529
alshabibe1cf87d2014-10-17 09:23:50 -0700530 // If this is not an edge port, bail out.
531 Topology topology = topologyService.currentTopology();
532 if (topologyService.isInfrastructure(topology, heardOn)) {
533 return;
534 }
535
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700536 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700537 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700538 MacAddress destMac = eth.getDestinationMAC();
539
Charles Chanff79dd92018-06-01 16:33:48 -0700540 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700541 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700542 return;
543 }
alshabibe1cf87d2014-10-17 09:23:50 -0700544
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700545 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
546 // if learning is disabled bail out.
547 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
548 log.debug("Learning disabled for {}, abort.", heardOn);
549 return;
550 }
551
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800552 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700553 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
554 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800555 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
556 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700557 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700558
Charles Chan5d5e0622015-09-25 13:00:06 -0700559 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700560 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700561 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700562 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700563 if (useDhcp) {
564 DHCP dhcp = findDhcp(eth).orElse(null);
565 // DHCP ACK: additionally update IP of DHCP client
566 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
567 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
568 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
569 HostId hostId = HostId.hostId(hostMac, hostVlan);
570 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700571 }
Charles Chane6067892016-11-17 10:23:46 -0800572 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700573 // NeighborAdvertisement and NeighborSolicitation: possible
574 // new hosts, update both location and IP.
575 //
576 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800577 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800578 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800579 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700580 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800581
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800582 // skip extension headers
583 IPacket pkt = ipv6;
584 while (pkt.getPayload() != null &&
585 pkt.getPayload() instanceof IExtensionHeader) {
586 pkt = pkt.getPayload();
587 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800588 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700589
590 // DHCPv6 protocol
591 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
592 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700593 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700594 handleDhcp6(dhcp6, vlan);
595 return;
596 }
597
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800598 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700599 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800600 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400601 if (pkt != null) {
602 // RouterSolicitation, RouterAdvertisement
603 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800604 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800605 }
Charles Chan178b0f52017-10-08 23:51:22 -0400606 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
607 // Duplicate Address Detection
608 if (ip.isZero()) {
609 return;
610 }
611 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700612 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400613
614 // Also learn from the target address of NeighborAdvertisement
615 if (pkt instanceof NeighborAdvertisement) {
616 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
617 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700618 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400619 }
620 return;
621 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800622 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800623 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800624
Yi Tsengaa417a62017-09-08 17:22:51 -0700625 // multicast, exclude DHCPv6
626 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800627 return;
628 }
629
630 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700631 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700632 }
633 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700634
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700635 /**
636 * Handles DHCPv6 packet, if message type is ACK, update IP address
637 * according to DHCPv6 payload (IA Address option).
638 *
639 * @param dhcp6 the DHCPv6 payload
640 * @param vlanId the vlan of this packet
641 */
642 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
643 // extract the relay message if exist
644 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
645 dhcp6 = dhcp6.getOptions().stream()
646 .filter(opt -> opt instanceof Dhcp6RelayOption)
647 .map(BasePacket::getPayload)
648 .map(pld -> (DHCP6) pld)
649 .findFirst()
650 .orElse(null);
651 }
652
653 if (dhcp6 == null) {
654 // Can't find dhcp payload
655 log.warn("Can't find dhcp payload from relay message");
656 return;
657 }
658
659 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
660 // Update IP address only when we received REPLY message
661 return;
662 }
663 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
664 .stream()
665 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
666 .map(opt -> (Dhcp6ClientIdOption) opt)
667 .findFirst();
668
669 if (!clientIdOption.isPresent()) {
670 // invalid DHCPv6 option
671 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
672 return;
673 }
674
675 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
676 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
677 // No any mac address found
678 log.warn("Can't find client mac from option {}", clientIdOption);
679 return;
680 }
681 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
682
683 // Extract IPv6 address from IA NA ot IA TA option
684 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
685 .stream()
686 .filter(opt -> opt instanceof Dhcp6IaNaOption)
687 .map(opt -> (Dhcp6IaNaOption) opt)
688 .findFirst();
689 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
690 .stream()
691 .filter(opt -> opt instanceof Dhcp6IaTaOption)
692 .map(opt -> (Dhcp6IaTaOption) opt)
693 .findFirst();
694 Optional<Dhcp6IaAddressOption> iaAddressOption;
695 if (iaNaOption.isPresent()) {
696 iaAddressOption = iaNaOption.get().getOptions().stream()
697 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
698 .map(opt -> (Dhcp6IaAddressOption) opt)
699 .findFirst();
700 } else if (iaTaOption.isPresent()) {
701 iaAddressOption = iaTaOption.get().getOptions().stream()
702 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
703 .map(opt -> (Dhcp6IaAddressOption) opt)
704 .findFirst();
705 } else {
706 iaAddressOption = Optional.empty();
707 }
708 if (iaAddressOption.isPresent()) {
709 Ip6Address ip = iaAddressOption.get().getIp6Address();
710 HostId hostId = HostId.hostId(clientMac, vlanId);
711 updateHostIp(hostId, ip);
712 } else {
713 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
714 }
715 }
716
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700717 private Optional<DHCP> findDhcp(Ethernet eth) {
718 IPacket pkt = eth.getPayload();
719 return Stream.of(pkt)
720 .filter(Objects::nonNull)
721 .filter(p -> p instanceof IPv4)
722 .map(IPacket::getPayload)
723 .filter(Objects::nonNull)
724 .filter(p -> p instanceof UDP)
725 .map(IPacket::getPayload)
726 .filter(Objects::nonNull)
727 .filter(p -> p instanceof DHCP)
728 .map(p -> (DHCP) p)
729 .findFirst();
730 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700731
732 private Optional<DHCP6> findDhcp6(IPacket pkt) {
733 return Stream.of(pkt)
734 .filter(Objects::nonNull)
735 .filter(p -> p instanceof UDP)
736 .map(IPacket::getPayload)
737 .filter(Objects::nonNull)
738 .filter(p -> p instanceof DHCP6)
739 .map(p -> (DHCP6) p)
740 .findFirst();
741 }
alshabibe1cf87d2014-10-17 09:23:50 -0700742 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800743
744 // Auxiliary listener to device events.
745 private class InternalDeviceListener implements DeviceListener {
746 @Override
747 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700748 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800749 }
750
751 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800752 Device device = event.subject();
753 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700754 case DEVICE_ADDED:
755 break;
756 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700757 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
758 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700759 }
760 break;
761 case DEVICE_SUSPENDED:
762 case DEVICE_UPDATED:
763 // Nothing to do?
764 break;
765 case DEVICE_REMOVED:
766 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700767 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700768 }
769 break;
770 case PORT_ADDED:
771 break;
772 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700773 if (hostRemovalEnabled && !event.port().isEnabled()) {
774 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700775 }
776 break;
777 case PORT_REMOVED:
enen923305efe2018-07-03 23:16:53 +0100778 if (hostRemovalEnabled) {
779 processPortDown(new ConnectPoint(device.id(), event.port().number()));
780 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700781 break;
782 default:
783 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800784 }
785 }
786 }
787
Charles Chan888e20a2017-05-01 15:44:23 -0700788 /**
789 * When a device goes down, update the location of affected hosts.
790 *
791 * @param deviceId the device that goes down
792 */
793 private void processDeviceDown(DeviceId deviceId) {
794 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
795 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
796 .forEach(affectedLocation ->
797 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
798 );
799 }
800
801 /**
802 * When a port goes down, update the location of affected hosts.
803 *
804 * @param connectPoint the port that goes down
805 */
806 private void processPortDown(ConnectPoint connectPoint) {
807 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
808 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
809 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800810 }
811
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700812
813 private class InternalConfigListener implements NetworkConfigListener {
814
815 @Override
816 public void event(NetworkConfigEvent event) {
817 switch (event.type()) {
818 case CONFIG_ADDED:
819 case CONFIG_UPDATED:
820 log.debug("HostLearningConfig event of type {}", event.type());
821 // if learning enabled do nothing
822 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
823 if (learningConfig.hostLearningEnabled()) {
824 return;
825 }
826
827 // if host learning is disable remove this location from existing, learnt hosts
828 ConnectPoint connectPoint = learningConfig.subject();
829 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
830 for (Host host : connectedHosts) {
831 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
832
833 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
834 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
835 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
836 }
837 }
838 break;
839 case CONFIG_REMOVED:
840 default:
841 break;
842 }
843 }
844
845 @Override
846 public boolean isRelevant(NetworkConfigEvent event) {
847 if (!event.configClass().equals(HostLearningConfig.class)) {
848 return false;
849 }
850 return true;
851 }
852 }
alshabibe1cf87d2014-10-17 09:23:50 -0700853}