blob: 1d9da60793d3a06020182d72fc88dc04a5f41eaa [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;
alshabibe1cf87d2014-10-17 09:23:50 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Charles Chan47933752017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Service;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.ARP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070028import org.onlab.packet.BasePacket;
Charles Chane6067892016-11-17 10:23:46 -080029import org.onlab.packet.DHCP;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070030import org.onlab.packet.DHCP6;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070031import org.onlab.packet.EthType;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080033import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070034import org.onlab.packet.IPacket;
Charles Chane6067892016-11-17 10:23:46 -080035import org.onlab.packet.IPv4;
Jonathan Harte8600eb2015-01-12 10:30:45 -080036import org.onlab.packet.IPv6;
Charles Chan35a32322017-08-14 11:42:11 -070037import org.onlab.packet.Ip4Address;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070038import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080039import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080040import org.onlab.packet.MacAddress;
Charles Chane6067892016-11-17 10:23:46 -080041import org.onlab.packet.UDP;
Jonathan Harte8600eb2015-01-12 10:30:45 -080042import org.onlab.packet.VlanId;
Yi Tsengfcf5dce2017-07-26 14:30:41 -070043import org.onlab.packet.dhcp.Dhcp6ClientIdOption;
44import org.onlab.packet.dhcp.Dhcp6IaAddressOption;
45import org.onlab.packet.dhcp.Dhcp6IaNaOption;
46import org.onlab.packet.dhcp.Dhcp6IaTaOption;
47import org.onlab.packet.dhcp.Dhcp6RelayOption;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080048import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080049import org.onlab.packet.ndp.NeighborAdvertisement;
50import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080051import org.onlab.packet.ndp.RouterAdvertisement;
52import org.onlab.packet.ndp.RouterSolicitation;
piera36d7f12019-06-25 14:09:57 +020053import org.onlab.util.PredictableExecutor;
Jian Lid9b5f552016-03-11 18:15:31 -080054import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070055import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080056import org.onosproject.core.ApplicationId;
57import org.onosproject.core.CoreService;
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -070058import org.onosproject.net.config.ConfigFactory;
59import org.onosproject.net.config.NetworkConfigEvent;
60import org.onosproject.net.config.NetworkConfigListener;
61import org.onosproject.net.config.NetworkConfigRegistry;
62import org.onosproject.net.config.NetworkConfigService;
63import org.onosproject.net.config.basics.BasicHostConfig;
64import org.onosproject.net.config.basics.HostLearningConfig;
65import org.onosproject.net.config.basics.SubjectFactories;
Ray Milkeyfacf2862017-08-03 11:58:29 -070066import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080067import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080068import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070069import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080070import org.onosproject.net.Host;
71import org.onosproject.net.HostId;
72import org.onosproject.net.HostLocation;
73import org.onosproject.net.device.DeviceEvent;
74import org.onosproject.net.device.DeviceListener;
75import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080076import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070077import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080078import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070079import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080080import org.onosproject.net.host.DefaultHostDescription;
81import org.onosproject.net.host.HostDescription;
82import org.onosproject.net.host.HostProvider;
83import org.onosproject.net.host.HostProviderRegistry;
84import org.onosproject.net.host.HostProviderService;
85import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070086import org.onosproject.net.packet.DefaultOutboundPacket;
87import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080088import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080089import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080090import org.onosproject.net.packet.PacketProcessor;
91import org.onosproject.net.packet.PacketService;
92import org.onosproject.net.provider.AbstractProvider;
93import org.onosproject.net.provider.ProviderId;
94import org.onosproject.net.topology.Topology;
95import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080096import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070097import org.slf4j.Logger;
98
sdn94b00152016-08-30 02:12:32 -070099import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700100import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700101import java.util.Objects;
102import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -0800103import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700104import java.util.stream.Stream;
Charles Chan35a32322017-08-14 11:42:11 -0700105import java.util.Set;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700106
Madan Jampania3770c32015-12-11 12:07:41 -0800107import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
108import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700109import static org.slf4j.LoggerFactory.getLogger;
110
alshabibe1cf87d2014-10-17 09:23:50 -0700111/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700112 * Provider which uses an OpenFlow controller to detect network end-station
113 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -0700114 */
115@Component(immediate = true)
Charles Chan47933752017-11-30 15:37:50 -0800116@Service
Charles Chand1d321b2018-06-01 16:33:48 -0700117public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700118 private final Logger log = getLogger(getClass());
119
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800120 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
121 protected CoreService coreService;
122
123 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700124 protected HostProviderRegistry providerRegistry;
125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800127 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
130 protected TopologyService topologyService;
131
Thomas Vachuska33601602014-11-19 03:32:15 -0800132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
133 protected HostService hostService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
136 protected DeviceService deviceService;
137
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
139 protected ComponentConfigService cfgService;
140
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700142 protected NetworkConfigRegistry registry;
143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700145 protected InterfaceService interfaceService;
146
alshabibe1cf87d2014-10-17 09:23:50 -0700147 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800148 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700149 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800150
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800151 private ApplicationId appId;
152
Thomas Vachuska33601602014-11-19 03:32:15 -0800153 @Property(name = "hostRemovalEnabled", boolValue = true,
154 label = "Enable host removal on port/device down events")
155 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700156
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700157 @Property(name = "requestArp", boolValue = true,
158 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800159 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700160 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800161
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700162 @Property(name = "requestIpv6ND", boolValue = false,
163 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700164 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700165 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800166
167 @Property(name = "useDhcp", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700168 label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800169 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700170
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700171 @Property(name = "useDhcp6", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700172 label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700173 private boolean useDhcp6 = false;
174
Jonathan Hart9af322d2016-01-06 17:42:04 -0800175 @Property(name = "requestInterceptsEnabled", boolValue = true,
176 label = "Enable requesting packet intercepts")
177 private boolean requestInterceptsEnabled = true;
178
Charles Chan35a32322017-08-14 11:42:11 -0700179 @Property(name = "multihomingEnabled", boolValue = false,
180 label = "Allow hosts to be multihomed")
181 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800182
Charles Chand1d321b2018-06-01 16:33:48 -0700183 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800184
Charles Chand1d321b2018-06-01 16:33:48 -0700185 ExecutorService deviceEventHandler;
186 private ExecutorService probeEventHandler;
piera36d7f12019-06-25 14:09:57 +0200187 // Packet workers - 0 will leverage available processors
188 private static final int DEFAULT_THREADS = 0;
189 private PredictableExecutor packetWorkers;
Charles Chan8277b6b2017-12-03 13:48:46 -0800190
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
192 protected NetworkConfigService netcfgService;
193
194 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
195 new ConfigFactory<ConnectPoint, HostLearningConfig>(
196 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
197 HostLearningConfig.class, "hostLearning") {
198 @Override
199 public HostLearningConfig createConfig() {
200 return new HostLearningConfig();
201 }
202 };
203
alshabibe1cf87d2014-10-17 09:23:50 -0700204 /**
205 * Creates an OpenFlow host provider.
206 */
207 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800208 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700209 }
210
211 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800212 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700213 cfgService.registerProperties(getClass());
214 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chand1d321b2018-06-01 16:33:48 -0700215 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
216 "device-event-handler", log));
217 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
218 "probe-event-handler", log));
piera36d7f12019-06-25 14:09:57 +0200219 packetWorkers = new PredictableExecutor(DEFAULT_THREADS, groupedThreads("onos/host-loc-provider",
220 "packet-worker-%d", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700221 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700222 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800223 deviceService.addListener(deviceListener);
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700224 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800225 modified(context);
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700226 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800227
Charles M.C. Chane148de82015-05-06 12:38:21 +0800228 log.info("Started with Application ID {}", appId.id());
229 }
230
231 @Deactivate
232 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800233 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700234
235 withdrawIntercepts();
236
Charles M.C. Chane148de82015-05-06 12:38:21 +0800237 providerRegistry.unregister(this);
238 packetService.removeProcessor(processor);
239 deviceService.removeListener(deviceListener);
Charles Chand1d321b2018-06-01 16:33:48 -0700240 deviceEventHandler.shutdown();
241 probeEventHandler.shutdown();
piera36d7f12019-06-25 14:09:57 +0200242 packetWorkers.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800243 providerService = null;
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700244 registry.unregisterConfigFactory(hostLearningConfig);
245 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800246 log.info("Stopped");
247 }
248
249 @Modified
250 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800251 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800252
253 if (requestInterceptsEnabled) {
254 requestIntercepts();
255 } else {
256 withdrawIntercepts();
257 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800258 }
259
260 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700261 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800262 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700263 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800264 // Use ARP
265 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
266 .matchEthType(Ethernet.TYPE_ARP)
267 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700268 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800269 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700270 } else {
Charles Chane6067892016-11-17 10:23:46 -0800271 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800272 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700273
Charles Chane6067892016-11-17 10:23:46 -0800274 // Use IPv6 Neighbor Discovery
275 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
276 .matchEthType(Ethernet.TYPE_IPV6)
277 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
278 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
279 .build();
280 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
281 .matchEthType(Ethernet.TYPE_IPV6)
282 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
283 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
284 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700285 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800286 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
287 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700288 } else {
Charles Chane6067892016-11-17 10:23:46 -0800289 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
290 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
291 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700292 }
293
294 /**
295 * Withdraw packet intercepts.
296 */
297 private void withdrawIntercepts() {
298 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
299 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700300 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700301
302 // IPv6 Neighbor Solicitation packet.
303 selector.matchEthType(Ethernet.TYPE_IPV6);
304 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
305 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
306 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
307
308 // IPv6 Neighbor Advertisement packet.
309 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
310 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800311 }
312
313 /**
314 * Extracts properties from the component configuration context.
315 *
316 * @param context the component context
317 */
318 private void readComponentConfiguration(ComponentContext context) {
319 Dictionary<?, ?> properties = context.getProperties();
320 Boolean flag;
321
Jian Lid9b5f552016-03-11 18:15:31 -0800322 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800323 if (flag == null) {
324 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700325 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800326 } else {
327 hostRemovalEnabled = flag;
328 log.info("Configured. Host removal on port/device down events is {}",
329 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800330 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800331
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700332 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800333 if (flag == null) {
334 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700335 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800336 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700337 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800338 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700339 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800340 }
341
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700342 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800343 if (flag == null) {
344 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700345 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800346 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700347 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800348 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700349 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800350 }
351
352 flag = Tools.isPropertyEnabled(properties, "useDhcp");
353 if (flag == null) {
354 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700355 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800356 } else {
357 useDhcp = flag;
358 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700359 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800360 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800361
Jian Lid9b5f552016-03-11 18:15:31 -0800362 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800363 if (flag == null) {
364 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700365 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800366 } else {
367 requestInterceptsEnabled = flag;
368 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700369 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800370 }
Charles Chan35a32322017-08-14 11:42:11 -0700371
372 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
373 if (flag == null) {
374 log.info("Multihoming is not configured, " +
375 "using current value of {}", multihomingEnabled);
376 } else {
377 multihomingEnabled = flag;
378 log.info("Configured. Multihoming is {}",
379 multihomingEnabled ? "enabled" : "disabled");
380 }
Charles Chan47933752017-11-30 15:37:50 -0800381 }
Charles Chan35a32322017-08-14 11:42:11 -0700382
Charles Chan47933752017-11-30 15:37:50 -0800383 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700384 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800385 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700386
387 // FIXME Disabling host probing for now, because sending packets from a
388 // broadcast MAC address caused problems when two ONOS networks were
389 // interconnected. Host probing should take into account the interface
390 // configuration when determining which source address to use.
391
392 //MastershipRole role = deviceService.getRole(host.location().deviceId());
393 //if (role.equals(MastershipRole.MASTER)) {
394 // host.ipAddresses().forEach(ip -> {
395 // sendProbe(host, ip);
396 // });
397 //} else {
398 // log.info("not the master, master will probe {}");
399 //}
sdn94b00152016-08-30 02:12:32 -0700400 }
401
402 private void sendProbe(Host host, IpAddress targetIp) {
403 Ethernet probePacket = null;
404 if (targetIp.isIp4()) {
405 // IPv4: Use ARP
406 probePacket = buildArpRequest(targetIp, host);
407 } else {
408 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700409 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700410 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800411 return;
sdn94b00152016-08-30 02:12:32 -0700412 }
413
414 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
415
416 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
417 ByteBuffer.wrap(probePacket.serialize()));
418
419 packetService.emit(outboundPacket);
420 }
421
Charles Chan888e20a2017-05-01 15:44:23 -0700422 // 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 -0700423 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700424 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
425 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
426 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700427 }
428
429 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800430 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700431 * Create or update host information.
432 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800433 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700434 * @param hid host ID
435 * @param mac source Mac address
436 * @param vlan VLAN ID
437 * @param innerVlan inner VLAN ID
438 * @param outerTpid outer TPID
439 * @param hloc host location
440 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800441 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700442 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
443 VlanId innerVlan, EthType outerTpid,
444 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700445 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
446
447 if (multihomingEnabled) {
448 Host existingHost = hostService.getHost(hid);
449 if (existingHost != null) {
450 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700451
Charles Chan653e2ac2017-11-22 14:49:55 -0800452 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
453 // New location is on a device that we haven't seen before
Charles Chand1d321b2018-06-01 16:33:48 -0700454 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800455 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800456 } else {
457 // Move within the same switch
458 // Simply replace old location that is on the same device
459 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
460 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700461 }
462 }
463 }
464
Charles Chan888e20a2017-05-01 15:44:23 -0700465 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700466 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
467 innerVlan, outerTpid, false) :
468 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
469 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700470 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800471 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700472 } catch (IllegalStateException e) {
473 log.debug("Host {} suppressed", hid);
474 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800475 }
alshabibe1cf87d2014-10-17 09:23:50 -0700476
Charles Chane6067892016-11-17 10:23:46 -0800477 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700478 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800479 *
480 * @param hid host ID
481 * @param ip IP address
482 */
Charles Chan888e20a2017-05-01 15:44:23 -0700483 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800484 Host host = hostService.getHost(hid);
485 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700486 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800487 return;
488 }
489
Charles Chan888e20a2017-05-01 15:44:23 -0700490 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700491 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800492 try {
493 providerService.hostDetected(hid, desc, false);
494 } catch (IllegalStateException e) {
495 log.debug("Host {} suppressed", hid);
496 }
497 }
498
alshabibe1cf87d2014-10-17 09:23:50 -0700499 @Override
500 public void process(PacketContext context) {
piera36d7f12019-06-25 14:09:57 +0200501 // Verify valid context
alshabib4a179dc2014-10-17 17:17:01 -0700502 if (context == null) {
503 return;
504 }
piera36d7f12019-06-25 14:09:57 +0200505 // Verify valid Ethernet packet
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700506 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800507 if (eth == null) {
508 return;
509 }
piera36d7f12019-06-25 14:09:57 +0200510 // Dispatch to a worker thread
511 HostId hostId = HostId.hostId(eth.getSourceMAC(), VlanId.vlanId(eth.getVlanID()));
512 packetWorkers.execute(() -> processPacketInternal(context), hostId.hashCode());
513 }
514
515 private void processPacketInternal(PacketContext context) {
516 Ethernet eth = context.inPacket().parsed();
Jonathan Hartf353e402016-09-23 09:27:36 -0700517
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800518 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700519 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
520 return;
521 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800522
alshabibe1cf87d2014-10-17 09:23:50 -0700523 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700524 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
525 VlanId innerVlan = VlanId.NONE;
526 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
527 // Set up values for double-tagged hosts
528 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
529 innerVlan = vlan;
530 vlan = outerVlan;
531 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
532 }
alshabibe1cf87d2014-10-17 09:23:50 -0700533 ConnectPoint heardOn = context.inPacket().receivedFrom();
534
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700535 // If this arrived on control port, bail out.
536 if (heardOn.port().isLogical()) {
537 return;
538 }
539
alshabibe1cf87d2014-10-17 09:23:50 -0700540 // If this is not an edge port, bail out.
541 Topology topology = topologyService.currentTopology();
542 if (topologyService.isInfrastructure(topology, heardOn)) {
543 return;
544 }
545
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700546 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700547 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700548 MacAddress destMac = eth.getDestinationMAC();
549
Charles Chand1d321b2018-06-01 16:33:48 -0700550 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700551 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700552 return;
553 }
alshabibe1cf87d2014-10-17 09:23:50 -0700554
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700555 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
556 // if learning is disabled bail out.
557 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
558 log.debug("Learning disabled for {}, abort.", heardOn);
559 return;
560 }
561
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800562 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700563 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
564 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800565 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
566 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700567 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700568
Charles Chan5d5e0622015-09-25 13:00:06 -0700569 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700570 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700571 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700572 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700573 if (useDhcp) {
574 DHCP dhcp = findDhcp(eth).orElse(null);
575 // DHCP ACK: additionally update IP of DHCP client
576 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
577 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
578 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
579 HostId hostId = HostId.hostId(hostMac, hostVlan);
580 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700581 }
Charles Chane6067892016-11-17 10:23:46 -0800582 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700583 // NeighborAdvertisement and NeighborSolicitation: possible
584 // new hosts, update both location and IP.
585 //
586 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800587 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800588 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800589 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700590 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800591
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800592 // skip extension headers
593 IPacket pkt = ipv6;
594 while (pkt.getPayload() != null &&
595 pkt.getPayload() instanceof IExtensionHeader) {
596 pkt = pkt.getPayload();
597 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800598 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700599
600 // DHCPv6 protocol
601 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
602 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700603 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700604 handleDhcp6(dhcp6, vlan);
605 return;
606 }
607
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800608 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700609 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800610 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400611 if (pkt != null) {
612 // RouterSolicitation, RouterAdvertisement
613 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800614 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800615 }
Charles Chan178b0f52017-10-08 23:51:22 -0400616 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
617 // Duplicate Address Detection
618 if (ip.isZero()) {
619 return;
620 }
621 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700622 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400623
624 // Also learn from the target address of NeighborAdvertisement
625 if (pkt instanceof NeighborAdvertisement) {
626 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
627 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700628 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400629 }
630 return;
631 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800632 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800633 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800634
Yi Tsengaa417a62017-09-08 17:22:51 -0700635 // multicast, exclude DHCPv6
636 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800637 return;
638 }
639
640 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700641 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700642 }
643 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700644
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700645 /**
646 * Handles DHCPv6 packet, if message type is ACK, update IP address
647 * according to DHCPv6 payload (IA Address option).
648 *
649 * @param dhcp6 the DHCPv6 payload
650 * @param vlanId the vlan of this packet
651 */
652 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
653 // extract the relay message if exist
654 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
655 dhcp6 = dhcp6.getOptions().stream()
656 .filter(opt -> opt instanceof Dhcp6RelayOption)
657 .map(BasePacket::getPayload)
658 .map(pld -> (DHCP6) pld)
659 .findFirst()
660 .orElse(null);
661 }
662
663 if (dhcp6 == null) {
664 // Can't find dhcp payload
665 log.warn("Can't find dhcp payload from relay message");
666 return;
667 }
668
669 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
670 // Update IP address only when we received REPLY message
671 return;
672 }
673 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
674 .stream()
675 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
676 .map(opt -> (Dhcp6ClientIdOption) opt)
677 .findFirst();
678
679 if (!clientIdOption.isPresent()) {
680 // invalid DHCPv6 option
681 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
682 return;
683 }
684
685 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
686 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
687 // No any mac address found
688 log.warn("Can't find client mac from option {}", clientIdOption);
689 return;
690 }
691 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
692
693 // Extract IPv6 address from IA NA ot IA TA option
694 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
695 .stream()
696 .filter(opt -> opt instanceof Dhcp6IaNaOption)
697 .map(opt -> (Dhcp6IaNaOption) opt)
698 .findFirst();
699 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
700 .stream()
701 .filter(opt -> opt instanceof Dhcp6IaTaOption)
702 .map(opt -> (Dhcp6IaTaOption) opt)
703 .findFirst();
704 Optional<Dhcp6IaAddressOption> iaAddressOption;
705 if (iaNaOption.isPresent()) {
706 iaAddressOption = iaNaOption.get().getOptions().stream()
707 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
708 .map(opt -> (Dhcp6IaAddressOption) opt)
709 .findFirst();
710 } else if (iaTaOption.isPresent()) {
711 iaAddressOption = iaTaOption.get().getOptions().stream()
712 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
713 .map(opt -> (Dhcp6IaAddressOption) opt)
714 .findFirst();
715 } else {
716 iaAddressOption = Optional.empty();
717 }
718 if (iaAddressOption.isPresent()) {
719 Ip6Address ip = iaAddressOption.get().getIp6Address();
720 HostId hostId = HostId.hostId(clientMac, vlanId);
721 updateHostIp(hostId, ip);
722 } else {
723 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
724 }
725 }
726
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700727 private Optional<DHCP> findDhcp(Ethernet eth) {
728 IPacket pkt = eth.getPayload();
729 return Stream.of(pkt)
730 .filter(Objects::nonNull)
731 .filter(p -> p instanceof IPv4)
732 .map(IPacket::getPayload)
733 .filter(Objects::nonNull)
734 .filter(p -> p instanceof UDP)
735 .map(IPacket::getPayload)
736 .filter(Objects::nonNull)
737 .filter(p -> p instanceof DHCP)
738 .map(p -> (DHCP) p)
739 .findFirst();
740 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700741
742 private Optional<DHCP6> findDhcp6(IPacket pkt) {
743 return Stream.of(pkt)
744 .filter(Objects::nonNull)
745 .filter(p -> p instanceof UDP)
746 .map(IPacket::getPayload)
747 .filter(Objects::nonNull)
748 .filter(p -> p instanceof DHCP6)
749 .map(p -> (DHCP6) p)
750 .findFirst();
751 }
alshabibe1cf87d2014-10-17 09:23:50 -0700752 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800753
754 // Auxiliary listener to device events.
755 private class InternalDeviceListener implements DeviceListener {
756 @Override
757 public void event(DeviceEvent event) {
Charles Chand1d321b2018-06-01 16:33:48 -0700758 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800759 }
760
761 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800762 Device device = event.subject();
763 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700764 case DEVICE_ADDED:
765 break;
766 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700767 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
768 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700769 }
770 break;
771 case DEVICE_SUSPENDED:
772 case DEVICE_UPDATED:
773 // Nothing to do?
774 break;
775 case DEVICE_REMOVED:
776 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700777 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700778 }
779 break;
780 case PORT_ADDED:
781 break;
782 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700783 if (hostRemovalEnabled && !event.port().isEnabled()) {
784 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700785 }
786 break;
787 case PORT_REMOVED:
788 // Nothing to do?
789 break;
790 default:
791 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800792 }
793 }
794 }
795
Charles Chan888e20a2017-05-01 15:44:23 -0700796 /**
797 * When a device goes down, update the location of affected hosts.
798 *
799 * @param deviceId the device that goes down
800 */
801 private void processDeviceDown(DeviceId deviceId) {
802 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
803 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
804 .forEach(affectedLocation ->
805 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
806 );
807 }
808
809 /**
810 * When a port goes down, update the location of affected hosts.
811 *
812 * @param connectPoint the port that goes down
813 */
814 private void processPortDown(ConnectPoint connectPoint) {
815 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
816 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
817 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800818 }
819
Andreas Pantelopoulos0903f592018-06-12 13:19:52 -0700820
821 private class InternalConfigListener implements NetworkConfigListener {
822
823 @Override
824 public void event(NetworkConfigEvent event) {
825 switch (event.type()) {
826 case CONFIG_ADDED:
827 case CONFIG_UPDATED:
828 log.debug("HostLearningConfig event of type {}", event.type());
829 // if learning enabled do nothing
830 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
831 if (learningConfig.hostLearningEnabled()) {
832 return;
833 }
834
835 // if host learning is disable remove this location from existing, learnt hosts
836 ConnectPoint connectPoint = learningConfig.subject();
837 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
838 for (Host host : connectedHosts) {
839 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
840
841 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
842 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
843 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
844 }
845 }
846 break;
847 case CONFIG_REMOVED:
848 default:
849 break;
850 }
851 }
852
853 @Override
854 public boolean isRelevant(NetworkConfigEvent event) {
855 if (!event.configClass().equals(HostLearningConfig.class)) {
856 return false;
857 }
858 return true;
859 }
860 }
alshabibe1cf87d2014-10-17 09:23:50 -0700861}