blob: edc5ef5c213fbd3b718c607d9814ffc26dacdae9 [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;
Jian Lid9b5f552016-03-11 18:15:31 -080053import org.onlab.util.Tools;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070054import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080055import org.onosproject.core.ApplicationId;
56import org.onosproject.core.CoreService;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
59import org.onosproject.net.config.NetworkConfigListener;
60import org.onosproject.net.config.NetworkConfigRegistry;
61import org.onosproject.net.config.NetworkConfigService;
62import org.onosproject.net.config.basics.BasicHostConfig;
63import org.onosproject.net.config.basics.HostLearningConfig;
64import org.onosproject.net.config.basics.SubjectFactories;
Ray Milkeyfacf2862017-08-03 11:58:29 -070065import org.onosproject.net.intf.InterfaceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080066import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080067import org.onosproject.net.Device;
Charles Chan888e20a2017-05-01 15:44:23 -070068import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080069import org.onosproject.net.Host;
70import org.onosproject.net.HostId;
71import org.onosproject.net.HostLocation;
72import org.onosproject.net.device.DeviceEvent;
73import org.onosproject.net.device.DeviceListener;
74import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080075import org.onosproject.net.flow.DefaultTrafficSelector;
sdn94b00152016-08-30 02:12:32 -070076import org.onosproject.net.flow.DefaultTrafficTreatment;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080077import org.onosproject.net.flow.TrafficSelector;
sdn94b00152016-08-30 02:12:32 -070078import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080079import org.onosproject.net.host.DefaultHostDescription;
80import org.onosproject.net.host.HostDescription;
81import org.onosproject.net.host.HostProvider;
82import org.onosproject.net.host.HostProviderRegistry;
83import org.onosproject.net.host.HostProviderService;
84import org.onosproject.net.host.HostService;
sdn94b00152016-08-30 02:12:32 -070085import org.onosproject.net.packet.DefaultOutboundPacket;
86import org.onosproject.net.packet.OutboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080087import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080088import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080089import org.onosproject.net.packet.PacketProcessor;
90import org.onosproject.net.packet.PacketService;
91import org.onosproject.net.provider.AbstractProvider;
92import org.onosproject.net.provider.ProviderId;
93import org.onosproject.net.topology.Topology;
94import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080095import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070096import org.slf4j.Logger;
97
sdn94b00152016-08-30 02:12:32 -070098import java.nio.ByteBuffer;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070099import java.util.Dictionary;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700100import java.util.Objects;
101import java.util.Optional;
Madan Jampania3770c32015-12-11 12:07:41 -0800102import java.util.concurrent.ExecutorService;
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700103import java.util.stream.Stream;
Charles Chan35a32322017-08-14 11:42:11 -0700104import java.util.Set;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700105
Madan Jampania3770c32015-12-11 12:07:41 -0800106import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
107import static org.onlab.util.Tools.groupedThreads;
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 */
114@Component(immediate = true)
Charles Chan47933752017-11-30 15:37:50 -0800115@Service
Charles Chanff79dd92018-06-01 16:33:48 -0700116public class HostLocationProvider extends AbstractProvider implements HostProvider {
alshabibe1cf87d2014-10-17 09:23:50 -0700117 private final Logger log = getLogger(getClass());
118
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
120 protected CoreService coreService;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -0700123 protected HostProviderRegistry providerRegistry;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800126 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -0700127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected TopologyService topologyService;
130
Thomas Vachuska33601602014-11-19 03:32:15 -0800131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected HostService hostService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected DeviceService deviceService;
136
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected ComponentConfigService cfgService;
139
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700141 protected NetworkConfigRegistry registry;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700144 protected InterfaceService interfaceService;
145
alshabibe1cf87d2014-10-17 09:23:50 -0700146 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800147 private final DeviceListener deviceListener = new InternalDeviceListener();
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700148 private final InternalConfigListener cfgListener = new InternalConfigListener();
Thomas Vachuska33601602014-11-19 03:32:15 -0800149
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800150 private ApplicationId appId;
151
Thomas Vachuska33601602014-11-19 03:32:15 -0800152 @Property(name = "hostRemovalEnabled", boolValue = true,
153 label = "Enable host removal on port/device down events")
154 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700155
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700156 @Property(name = "requestArp", boolValue = true,
157 label = "Request ARP packets for neighbor discovery by the " +
Charles Chane6067892016-11-17 10:23:46 -0800158 "Host Location Provider; default is true")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700159 private boolean requestArp = true;
Charles Chane6067892016-11-17 10:23:46 -0800160
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700161 @Property(name = "requestIpv6ND", boolValue = false,
162 label = "Requests IPv6 Neighbor Discovery by the " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700163 "Host Location Provider; default is false")
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700164 private boolean requestIpv6ND = false;
Charles Chane6067892016-11-17 10:23:46 -0800165
166 @Property(name = "useDhcp", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700167 label = "Use DHCP to update IP address of the host; default is false")
Charles Chane6067892016-11-17 10:23:46 -0800168 private boolean useDhcp = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700169
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700170 @Property(name = "useDhcp6", boolValue = false,
Yi Tsengaa417a62017-09-08 17:22:51 -0700171 label = "Use DHCPv6 to update IP address of the host; default is false")
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700172 private boolean useDhcp6 = false;
173
Jonathan Hart9af322d2016-01-06 17:42:04 -0800174 @Property(name = "requestInterceptsEnabled", boolValue = true,
175 label = "Enable requesting packet intercepts")
176 private boolean requestInterceptsEnabled = true;
177
Charles Chan35a32322017-08-14 11:42:11 -0700178 @Property(name = "multihomingEnabled", boolValue = false,
179 label = "Allow hosts to be multihomed")
180 private boolean multihomingEnabled = false;
Madan Jampania3770c32015-12-11 12:07:41 -0800181
Charles Chanff79dd92018-06-01 16:33:48 -0700182 private HostProviderService providerService;
Charles Chan47933752017-11-30 15:37:50 -0800183
Charles Chanff79dd92018-06-01 16:33:48 -0700184 ExecutorService deviceEventHandler;
185 private ExecutorService probeEventHandler;
Charles Chan07f15f22018-05-08 21:35:50 -0700186 private ExecutorService packetHandler;
Charles Chan8277b6b2017-12-03 13:48:46 -0800187
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
189 protected NetworkConfigService netcfgService;
190
191 private ConfigFactory<ConnectPoint, HostLearningConfig> hostLearningConfig =
192 new ConfigFactory<ConnectPoint, HostLearningConfig>(
193 SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY,
194 HostLearningConfig.class, "hostLearning") {
195 @Override
196 public HostLearningConfig createConfig() {
197 return new HostLearningConfig();
198 }
199 };
200
alshabibe1cf87d2014-10-17 09:23:50 -0700201 /**
202 * Creates an OpenFlow host provider.
203 */
204 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800205 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700206 }
207
208 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800209 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700210 cfgService.registerProperties(getClass());
211 appId = coreService.registerApplication("org.onosproject.provider.host");
Charles Chanff79dd92018-06-01 16:33:48 -0700212 deviceEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
213 "device-event-handler", log));
214 probeEventHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
215 "probe-event-handler", log));
Charles Chan07f15f22018-05-08 21:35:50 -0700216 packetHandler = newSingleThreadScheduledExecutor(groupedThreads("onos/host-loc-provider",
217 "packet-handler", log));
alshabibe1cf87d2014-10-17 09:23:50 -0700218 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700219 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800220 deviceService.addListener(deviceListener);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700221 registry.registerConfigFactory(hostLearningConfig);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800222 modified(context);
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700223 netcfgService.addListener(cfgListener);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800224
Charles M.C. Chane148de82015-05-06 12:38:21 +0800225 log.info("Started with Application ID {}", appId.id());
226 }
227
228 @Deactivate
229 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800230 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700231
232 withdrawIntercepts();
233
Charles M.C. Chane148de82015-05-06 12:38:21 +0800234 providerRegistry.unregister(this);
235 packetService.removeProcessor(processor);
236 deviceService.removeListener(deviceListener);
Charles Chanff79dd92018-06-01 16:33:48 -0700237 deviceEventHandler.shutdown();
238 probeEventHandler.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700239 packetHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800240 providerService = null;
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700241 registry.unregisterConfigFactory(hostLearningConfig);
242 netcfgService.removeListener(cfgListener);
Charles M.C. Chane148de82015-05-06 12:38:21 +0800243 log.info("Stopped");
244 }
245
246 @Modified
247 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800248 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800249
250 if (requestInterceptsEnabled) {
251 requestIntercepts();
252 } else {
253 withdrawIntercepts();
254 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800255 }
256
257 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700258 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800259 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700260 private void requestIntercepts() {
Charles Chane6067892016-11-17 10:23:46 -0800261 // Use ARP
262 TrafficSelector arpSelector = DefaultTrafficSelector.builder()
263 .matchEthType(Ethernet.TYPE_ARP)
264 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700265 if (requestArp) {
Charles Chane6067892016-11-17 10:23:46 -0800266 packetService.requestPackets(arpSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700267 } else {
Charles Chane6067892016-11-17 10:23:46 -0800268 packetService.cancelPackets(arpSelector, PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800269 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700270
Charles Chane6067892016-11-17 10:23:46 -0800271 // Use IPv6 Neighbor Discovery
272 TrafficSelector ipv6NsSelector = DefaultTrafficSelector.builder()
273 .matchEthType(Ethernet.TYPE_IPV6)
274 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
275 .matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION)
276 .build();
277 TrafficSelector ipv6NaSelector = DefaultTrafficSelector.builder()
278 .matchEthType(Ethernet.TYPE_IPV6)
279 .matchIPProtocol(IPv6.PROTOCOL_ICMP6)
280 .matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT)
281 .build();
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700282 if (requestIpv6ND) {
Charles Chane6067892016-11-17 10:23:46 -0800283 packetService.requestPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
284 packetService.requestPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700285 } else {
Charles Chane6067892016-11-17 10:23:46 -0800286 packetService.cancelPackets(ipv6NsSelector, PacketPriority.CONTROL, appId);
287 packetService.cancelPackets(ipv6NaSelector, PacketPriority.CONTROL, appId);
288 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700289 }
290
291 /**
292 * Withdraw packet intercepts.
293 */
294 private void withdrawIntercepts() {
295 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
296 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700297 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700298
299 // IPv6 Neighbor Solicitation packet.
300 selector.matchEthType(Ethernet.TYPE_IPV6);
301 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
302 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
303 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
304
305 // IPv6 Neighbor Advertisement packet.
306 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
307 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800308 }
309
310 /**
311 * Extracts properties from the component configuration context.
312 *
313 * @param context the component context
314 */
315 private void readComponentConfiguration(ComponentContext context) {
316 Dictionary<?, ?> properties = context.getProperties();
317 Boolean flag;
318
Jian Lid9b5f552016-03-11 18:15:31 -0800319 flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800320 if (flag == null) {
321 log.info("Host removal on port/device down events is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700322 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800323 } else {
324 hostRemovalEnabled = flag;
325 log.info("Configured. Host removal on port/device down events is {}",
326 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800327 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800328
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700329 flag = Tools.isPropertyEnabled(properties, "requestArp");
Charles Chane6067892016-11-17 10:23:46 -0800330 if (flag == null) {
331 log.info("Using ARP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700332 "using current value of {}", requestArp);
Charles Chane6067892016-11-17 10:23:46 -0800333 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700334 requestArp = flag;
Charles Chane6067892016-11-17 10:23:46 -0800335 log.info("Configured. Using ARP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700336 requestArp ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800337 }
338
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700339 flag = Tools.isPropertyEnabled(properties, "requestIpv6ND");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800340 if (flag == null) {
341 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700342 "using current value of {}", requestIpv6ND);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800343 } else {
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700344 requestIpv6ND = flag;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800345 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700346 requestIpv6ND ? "enabled" : "disabled");
Charles Chane6067892016-11-17 10:23:46 -0800347 }
348
349 flag = Tools.isPropertyEnabled(properties, "useDhcp");
350 if (flag == null) {
351 log.info("Using DHCP is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700352 "using current value of {}", useDhcp);
Charles Chane6067892016-11-17 10:23:46 -0800353 } else {
354 useDhcp = flag;
355 log.info("Configured. Using DHCP is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700356 useDhcp ? "enabled" : "disabled");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800357 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800358
Jian Lid9b5f552016-03-11 18:15:31 -0800359 flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800360 if (flag == null) {
361 log.info("Request intercepts is not configured, " +
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700362 "using current value of {}", requestInterceptsEnabled);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800363 } else {
364 requestInterceptsEnabled = flag;
365 log.info("Configured. Request intercepts is {}",
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700366 requestInterceptsEnabled ? "enabled" : "disabled");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800367 }
Charles Chan35a32322017-08-14 11:42:11 -0700368
369 flag = Tools.isPropertyEnabled(properties, "multihomingEnabled");
370 if (flag == null) {
371 log.info("Multihoming is not configured, " +
372 "using current value of {}", multihomingEnabled);
373 } else {
374 multihomingEnabled = flag;
375 log.info("Configured. Multihoming is {}",
376 multihomingEnabled ? "enabled" : "disabled");
377 }
Charles Chan47933752017-11-30 15:37:50 -0800378 }
Charles Chan35a32322017-08-14 11:42:11 -0700379
Charles Chan47933752017-11-30 15:37:50 -0800380 @Override
alshabibe1cf87d2014-10-17 09:23:50 -0700381 public void triggerProbe(Host host) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800382 //log.info("Triggering probe on device {} ", host);
Jonathan Hartf353e402016-09-23 09:27:36 -0700383
384 // FIXME Disabling host probing for now, because sending packets from a
385 // broadcast MAC address caused problems when two ONOS networks were
386 // interconnected. Host probing should take into account the interface
387 // configuration when determining which source address to use.
388
389 //MastershipRole role = deviceService.getRole(host.location().deviceId());
390 //if (role.equals(MastershipRole.MASTER)) {
391 // host.ipAddresses().forEach(ip -> {
392 // sendProbe(host, ip);
393 // });
394 //} else {
395 // log.info("not the master, master will probe {}");
396 //}
sdn94b00152016-08-30 02:12:32 -0700397 }
398
399 private void sendProbe(Host host, IpAddress targetIp) {
400 Ethernet probePacket = null;
401 if (targetIp.isIp4()) {
402 // IPv4: Use ARP
403 probePacket = buildArpRequest(targetIp, host);
404 } else {
405 // IPv6: Use Neighbor Discovery
Jonathan Hartf353e402016-09-23 09:27:36 -0700406 //TODO need to implement ndp probe
sdn94b00152016-08-30 02:12:32 -0700407 log.info("Triggering probe on device {} ", host);
Ray Milkey74e59132018-01-17 15:24:52 -0800408 return;
sdn94b00152016-08-30 02:12:32 -0700409 }
410
411 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(host.location().port()).build();
412
413 OutboundPacket outboundPacket = new DefaultOutboundPacket(host.location().deviceId(), treatment,
414 ByteBuffer.wrap(probePacket.serialize()));
415
416 packetService.emit(outboundPacket);
417 }
418
Charles Chan888e20a2017-05-01 15:44:23 -0700419 // 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 -0700420 private Ethernet buildArpRequest(IpAddress targetIp, Host host) {
Charles Chan35a32322017-08-14 11:42:11 -0700421 return ARP.buildArpRequest(MacAddress.BROADCAST.toBytes(), Ip4Address.ZERO.toOctets(),
422 MacAddress.BROADCAST.toBytes(), targetIp.toOctets(),
423 MacAddress.BROADCAST.toBytes(), VlanId.NONE.toShort());
alshabibe1cf87d2014-10-17 09:23:50 -0700424 }
425
426 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800427 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700428 * Create or update host information.
429 * Will not update IP if IP is null, all zero or self-assigned.
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800430 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700431 * @param hid host ID
432 * @param mac source Mac address
433 * @param vlan VLAN ID
434 * @param innerVlan inner VLAN ID
435 * @param outerTpid outer TPID
436 * @param hloc host location
437 * @param ip source IP address or null if not updating
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800438 */
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700439 private void createOrUpdateHost(HostId hid, MacAddress mac, VlanId vlan,
440 VlanId innerVlan, EthType outerTpid,
441 HostLocation hloc, IpAddress ip) {
Charles Chan35a32322017-08-14 11:42:11 -0700442 Set<HostLocation> newLocations = Sets.newHashSet(hloc);
443
444 if (multihomingEnabled) {
445 Host existingHost = hostService.getHost(hid);
446 if (existingHost != null) {
447 Set<HostLocation> prevLocations = existingHost.locations();
Charles Chan35a32322017-08-14 11:42:11 -0700448
Charles Chan653e2ac2017-11-22 14:49:55 -0800449 if (prevLocations.stream().noneMatch(loc -> loc.deviceId().equals(hloc.deviceId()))) {
450 // New location is on a device that we haven't seen before
Charles Chanff79dd92018-06-01 16:33:48 -0700451 // Could be a dual-home host.
Charles Chan653e2ac2017-11-22 14:49:55 -0800452 newLocations.addAll(prevLocations);
Charles Chan653e2ac2017-11-22 14:49:55 -0800453 } else {
454 // Move within the same switch
455 // Simply replace old location that is on the same device
456 prevLocations.stream().filter(loc -> !loc.deviceId().equals(hloc.deviceId()))
457 .forEach(newLocations::add);
Charles Chan35a32322017-08-14 11:42:11 -0700458 }
459 }
460 }
461
Charles Chan888e20a2017-05-01 15:44:23 -0700462 HostDescription desc = ip == null || ip.isZero() || ip.isSelfAssigned() ?
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700463 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(),
464 innerVlan, outerTpid, false) :
465 new DefaultHostDescription(mac, vlan, newLocations, Sets.newHashSet(ip),
466 innerVlan, outerTpid, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700467 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800468 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700469 } catch (IllegalStateException e) {
470 log.debug("Host {} suppressed", hid);
471 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800472 }
alshabibe1cf87d2014-10-17 09:23:50 -0700473
Charles Chane6067892016-11-17 10:23:46 -0800474 /**
Charles Chan888e20a2017-05-01 15:44:23 -0700475 * Updates IP address for an existing host.
Charles Chane6067892016-11-17 10:23:46 -0800476 *
477 * @param hid host ID
478 * @param ip IP address
479 */
Charles Chan888e20a2017-05-01 15:44:23 -0700480 private void updateHostIp(HostId hid, IpAddress ip) {
Charles Chane6067892016-11-17 10:23:46 -0800481 Host host = hostService.getHost(hid);
482 if (host == null) {
Charles Chan35a32322017-08-14 11:42:11 -0700483 log.warn("Fail to update IP for {}. Host does not exist", hid);
Charles Chane6067892016-11-17 10:23:46 -0800484 return;
485 }
486
Charles Chan888e20a2017-05-01 15:44:23 -0700487 HostDescription desc = new DefaultHostDescription(hid.mac(), hid.vlanId(),
Charles Chan35a32322017-08-14 11:42:11 -0700488 host.locations(), Sets.newHashSet(ip), false);
Charles Chane6067892016-11-17 10:23:46 -0800489 try {
490 providerService.hostDetected(hid, desc, false);
491 } catch (IllegalStateException e) {
492 log.debug("Host {} suppressed", hid);
493 }
494 }
495
alshabibe1cf87d2014-10-17 09:23:50 -0700496 @Override
497 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -0700498 packetHandler.execute(() -> processPacketInternal(context));
499 }
500
501 private void processPacketInternal(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700502 if (context == null) {
503 return;
504 }
alshabibe1cf87d2014-10-17 09:23:50 -0700505
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 }
Jonathan Hartf353e402016-09-23 09:27:36 -0700510
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800511 MacAddress srcMac = eth.getSourceMAC();
Jonathan Hartf353e402016-09-23 09:27:36 -0700512 if (srcMac.isBroadcast() || srcMac.isMulticast()) {
513 return;
514 }
Jonathan Harte8600eb2015-01-12 10:30:45 -0800515
alshabibe1cf87d2014-10-17 09:23:50 -0700516 VlanId vlan = VlanId.vlanId(eth.getVlanID());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700517 VlanId outerVlan = VlanId.vlanId(eth.getQinQVID());
518 VlanId innerVlan = VlanId.NONE;
519 EthType outerTpid = EthType.EtherType.UNKNOWN.ethType();
520 // Set up values for double-tagged hosts
521 if (outerVlan.toShort() != Ethernet.VLAN_UNTAGGED) {
522 innerVlan = vlan;
523 vlan = outerVlan;
524 outerTpid = EthType.EtherType.lookup(eth.getQinQTPID()).ethType();
525 }
alshabibe1cf87d2014-10-17 09:23:50 -0700526 ConnectPoint heardOn = context.inPacket().receivedFrom();
527
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700528 // If this arrived on control port, bail out.
529 if (heardOn.port().isLogical()) {
530 return;
531 }
532
alshabibe1cf87d2014-10-17 09:23:50 -0700533 // If this is not an edge port, bail out.
534 Topology topology = topologyService.currentTopology();
535 if (topologyService.isInfrastructure(topology, heardOn)) {
536 return;
537 }
538
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700539 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700540 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
Charles Chan35a32322017-08-14 11:42:11 -0700541 MacAddress destMac = eth.getDestinationMAC();
542
Charles Chanff79dd92018-06-01 16:33:48 -0700543 // Ignore location probes
Charles Chan35a32322017-08-14 11:42:11 -0700544 if (multihomingEnabled && destMac.isOnos() && !MacAddress.NONE.equals(destMac)) {
Charles Chan35a32322017-08-14 11:42:11 -0700545 return;
546 }
alshabibe1cf87d2014-10-17 09:23:50 -0700547
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700548 HostLearningConfig cfg = netcfgService.getConfig(heardOn, HostLearningConfig.class);
549 // if learning is disabled bail out.
550 if ((cfg != null) && (!cfg.hostLearningEnabled())) {
551 log.debug("Learning disabled for {}, abort.", heardOn);
552 return;
553 }
554
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800555 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700556 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
557 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800558 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
559 arp.getSenderProtocolAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700560 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700561
Charles Chan5d5e0622015-09-25 13:00:06 -0700562 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700563 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Yi Tsengaa417a62017-09-08 17:22:51 -0700564 // Update host location
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700565 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengaa417a62017-09-08 17:22:51 -0700566 if (useDhcp) {
567 DHCP dhcp = findDhcp(eth).orElse(null);
568 // DHCP ACK: additionally update IP of DHCP client
569 if (dhcp != null && dhcp.getPacketType().equals(DHCP.MsgType.DHCPACK)) {
570 MacAddress hostMac = MacAddress.valueOf(dhcp.getClientHardwareAddress());
571 VlanId hostVlan = VlanId.vlanId(eth.getVlanID());
572 HostId hostId = HostId.hostId(hostMac, hostVlan);
573 updateHostIp(hostId, IpAddress.valueOf(dhcp.getYourIPAddress()));
Yi Tsengcfa637a2017-07-26 17:24:10 -0700574 }
Charles Chane6067892016-11-17 10:23:46 -0800575 }
Charles Chan5d5e0622015-09-25 13:00:06 -0700576 // NeighborAdvertisement and NeighborSolicitation: possible
577 // new hosts, update both location and IP.
578 //
579 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800580 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800581 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800582 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700583 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800584
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800585 // skip extension headers
586 IPacket pkt = ipv6;
587 while (pkt.getPayload() != null &&
588 pkt.getPayload() instanceof IExtensionHeader) {
589 pkt = pkt.getPayload();
590 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800591 pkt = pkt.getPayload();
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700592
593 // DHCPv6 protocol
594 DHCP6 dhcp6 = findDhcp6(pkt).orElse(null);
595 if (dhcp6 != null && useDhcp6) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700596 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700597 handleDhcp6(dhcp6, vlan);
598 return;
599 }
600
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800601 if (pkt != null && pkt instanceof ICMP6) {
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700602 // Neighbor Discovery Protocol
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800603 pkt = pkt.getPayload();
Charles Chan178b0f52017-10-08 23:51:22 -0400604 if (pkt != null) {
605 // RouterSolicitation, RouterAdvertisement
606 if (pkt instanceof RouterAdvertisement || pkt instanceof RouterSolicitation) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800607 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800608 }
Charles Chan178b0f52017-10-08 23:51:22 -0400609 if (pkt instanceof NeighborSolicitation || pkt instanceof NeighborAdvertisement) {
610 // Duplicate Address Detection
611 if (ip.isZero()) {
612 return;
613 }
614 // NeighborSolicitation, NeighborAdvertisement
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700615 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, ip);
Charles Chan178b0f52017-10-08 23:51:22 -0400616
617 // Also learn from the target address of NeighborAdvertisement
618 if (pkt instanceof NeighborAdvertisement) {
619 NeighborAdvertisement na = (NeighborAdvertisement) pkt;
620 Ip6Address targetAddr = Ip6Address.valueOf(na.getTargetAddress());
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700621 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, targetAddr);
Charles Chan178b0f52017-10-08 23:51:22 -0400622 }
623 return;
624 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800625 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800626 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800627
Yi Tsengaa417a62017-09-08 17:22:51 -0700628 // multicast, exclude DHCPv6
629 if (eth.isMulticast() && dhcp6 == null) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800630 return;
631 }
632
633 // normal IPv6 packets
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700634 createOrUpdateHost(hid, srcMac, vlan, innerVlan, outerTpid, hloc, null);
alshabibe1cf87d2014-10-17 09:23:50 -0700635 }
636 }
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700637
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700638 /**
639 * Handles DHCPv6 packet, if message type is ACK, update IP address
640 * according to DHCPv6 payload (IA Address option).
641 *
642 * @param dhcp6 the DHCPv6 payload
643 * @param vlanId the vlan of this packet
644 */
645 private void handleDhcp6(DHCP6 dhcp6, VlanId vlanId) {
646 // extract the relay message if exist
647 while (dhcp6 != null && DHCP6.RELAY_MSG_TYPES.contains(dhcp6.getMsgType())) {
648 dhcp6 = dhcp6.getOptions().stream()
649 .filter(opt -> opt instanceof Dhcp6RelayOption)
650 .map(BasePacket::getPayload)
651 .map(pld -> (DHCP6) pld)
652 .findFirst()
653 .orElse(null);
654 }
655
656 if (dhcp6 == null) {
657 // Can't find dhcp payload
658 log.warn("Can't find dhcp payload from relay message");
659 return;
660 }
661
662 if (dhcp6.getMsgType() != DHCP6.MsgType.REPLY.value()) {
663 // Update IP address only when we received REPLY message
664 return;
665 }
666 Optional<Dhcp6ClientIdOption> clientIdOption = dhcp6.getOptions()
667 .stream()
668 .filter(opt -> opt instanceof Dhcp6ClientIdOption)
669 .map(opt -> (Dhcp6ClientIdOption) opt)
670 .findFirst();
671
672 if (!clientIdOption.isPresent()) {
673 // invalid DHCPv6 option
674 log.warn("Can't find client ID from DHCPv6 {}", dhcp6);
675 return;
676 }
677
678 byte[] linkLayerAddr = clientIdOption.get().getDuid().getLinkLayerAddress();
679 if (linkLayerAddr == null || linkLayerAddr.length != 6) {
680 // No any mac address found
681 log.warn("Can't find client mac from option {}", clientIdOption);
682 return;
683 }
684 MacAddress clientMac = MacAddress.valueOf(linkLayerAddr);
685
686 // Extract IPv6 address from IA NA ot IA TA option
687 Optional<Dhcp6IaNaOption> iaNaOption = dhcp6.getOptions()
688 .stream()
689 .filter(opt -> opt instanceof Dhcp6IaNaOption)
690 .map(opt -> (Dhcp6IaNaOption) opt)
691 .findFirst();
692 Optional<Dhcp6IaTaOption> iaTaOption = dhcp6.getOptions()
693 .stream()
694 .filter(opt -> opt instanceof Dhcp6IaTaOption)
695 .map(opt -> (Dhcp6IaTaOption) opt)
696 .findFirst();
697 Optional<Dhcp6IaAddressOption> iaAddressOption;
698 if (iaNaOption.isPresent()) {
699 iaAddressOption = iaNaOption.get().getOptions().stream()
700 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
701 .map(opt -> (Dhcp6IaAddressOption) opt)
702 .findFirst();
703 } else if (iaTaOption.isPresent()) {
704 iaAddressOption = iaTaOption.get().getOptions().stream()
705 .filter(opt -> opt instanceof Dhcp6IaAddressOption)
706 .map(opt -> (Dhcp6IaAddressOption) opt)
707 .findFirst();
708 } else {
709 iaAddressOption = Optional.empty();
710 }
711 if (iaAddressOption.isPresent()) {
712 Ip6Address ip = iaAddressOption.get().getIp6Address();
713 HostId hostId = HostId.hostId(clientMac, vlanId);
714 updateHostIp(hostId, ip);
715 } else {
716 log.warn("Can't find IPv6 address from DHCPv6 {}", dhcp6);
717 }
718 }
719
Yi Tseng7a38f9a2017-06-09 14:36:40 -0700720 private Optional<DHCP> findDhcp(Ethernet eth) {
721 IPacket pkt = eth.getPayload();
722 return Stream.of(pkt)
723 .filter(Objects::nonNull)
724 .filter(p -> p instanceof IPv4)
725 .map(IPacket::getPayload)
726 .filter(Objects::nonNull)
727 .filter(p -> p instanceof UDP)
728 .map(IPacket::getPayload)
729 .filter(Objects::nonNull)
730 .filter(p -> p instanceof DHCP)
731 .map(p -> (DHCP) p)
732 .findFirst();
733 }
Yi Tsengfcf5dce2017-07-26 14:30:41 -0700734
735 private Optional<DHCP6> findDhcp6(IPacket pkt) {
736 return Stream.of(pkt)
737 .filter(Objects::nonNull)
738 .filter(p -> p instanceof UDP)
739 .map(IPacket::getPayload)
740 .filter(Objects::nonNull)
741 .filter(p -> p instanceof DHCP6)
742 .map(p -> (DHCP6) p)
743 .findFirst();
744 }
alshabibe1cf87d2014-10-17 09:23:50 -0700745 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800746
747 // Auxiliary listener to device events.
748 private class InternalDeviceListener implements DeviceListener {
749 @Override
750 public void event(DeviceEvent event) {
Charles Chanff79dd92018-06-01 16:33:48 -0700751 deviceEventHandler.execute(() -> handleEvent(event));
Madan Jampania3770c32015-12-11 12:07:41 -0800752 }
753
754 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800755 Device device = event.subject();
756 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700757 case DEVICE_ADDED:
758 break;
759 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan888e20a2017-05-01 15:44:23 -0700760 if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) {
761 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700762 }
763 break;
764 case DEVICE_SUSPENDED:
765 case DEVICE_UPDATED:
766 // Nothing to do?
767 break;
768 case DEVICE_REMOVED:
769 if (hostRemovalEnabled) {
Charles Chan888e20a2017-05-01 15:44:23 -0700770 processDeviceDown(device.id());
Thomas Vachuska27bee092015-06-23 19:03:10 -0700771 }
772 break;
773 case PORT_ADDED:
774 break;
775 case PORT_UPDATED:
Charles Chan888e20a2017-05-01 15:44:23 -0700776 if (hostRemovalEnabled && !event.port().isEnabled()) {
777 processPortDown(new ConnectPoint(device.id(), event.port().number()));
Thomas Vachuska27bee092015-06-23 19:03:10 -0700778 }
779 break;
780 case PORT_REMOVED:
781 // Nothing to do?
782 break;
783 default:
784 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800785 }
786 }
787 }
788
Charles Chan888e20a2017-05-01 15:44:23 -0700789 /**
790 * When a device goes down, update the location of affected hosts.
791 *
792 * @param deviceId the device that goes down
793 */
794 private void processDeviceDown(DeviceId deviceId) {
795 hostService.getConnectedHosts(deviceId).forEach(affectedHost -> affectedHost.locations().stream()
796 .filter(hostLocation -> hostLocation.deviceId().equals(deviceId))
797 .forEach(affectedLocation ->
798 providerService.removeLocationFromHost(affectedHost.id(), affectedLocation))
799 );
800 }
801
802 /**
803 * When a port goes down, update the location of affected hosts.
804 *
805 * @param connectPoint the port that goes down
806 */
807 private void processPortDown(ConnectPoint connectPoint) {
808 hostService.getConnectedHosts(connectPoint).forEach(affectedHost ->
809 providerService.removeLocationFromHost(affectedHost.id(), new HostLocation(connectPoint, 0L))
810 );
Thomas Vachuska33601602014-11-19 03:32:15 -0800811 }
812
Andreas Pantelopoulos19416412018-06-12 13:19:52 -0700813
814 private class InternalConfigListener implements NetworkConfigListener {
815
816 @Override
817 public void event(NetworkConfigEvent event) {
818 switch (event.type()) {
819 case CONFIG_ADDED:
820 case CONFIG_UPDATED:
821 log.debug("HostLearningConfig event of type {}", event.type());
822 // if learning enabled do nothing
823 HostLearningConfig learningConfig = (HostLearningConfig) event.config().get();
824 if (learningConfig.hostLearningEnabled()) {
825 return;
826 }
827
828 // if host learning is disable remove this location from existing, learnt hosts
829 ConnectPoint connectPoint = learningConfig.subject();
830 Set<Host> connectedHosts = hostService.getConnectedHosts(connectPoint);
831 for (Host host : connectedHosts) {
832 BasicHostConfig hostConfig = netcfgService.getConfig(host.id(), BasicHostConfig.class);
833
834 if ((hostConfig == null) || (!hostConfig.locations().contains(connectPoint))) {
835 // timestamp shoud not matter for comparing HostLocation and ConnectPoint
836 providerService.removeLocationFromHost(host.id(), new HostLocation(connectPoint, 1));
837 }
838 }
839 break;
840 case CONFIG_REMOVED:
841 default:
842 break;
843 }
844 }
845
846 @Override
847 public boolean isRelevant(NetworkConfigEvent event) {
848 if (!event.configClass().equals(HostLearningConfig.class)) {
849 return false;
850 }
851 return true;
852 }
853 }
alshabibe1cf87d2014-10-17 09:23:50 -0700854}