blob: caa7f82d2372581d3916adbb4cb91f9670c20ba2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska33601602014-11-19 03:32:15 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabibe1cf87d2014-10-17 09:23:50 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte8600eb2015-01-12 10:30:45 -080025import org.onlab.packet.ARP;
26import org.onlab.packet.Ethernet;
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080027import org.onlab.packet.ICMP6;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070028import org.onlab.packet.IPacket;
Jonathan Harte8600eb2015-01-12 10:30:45 -080029import org.onlab.packet.IPv6;
30import org.onlab.packet.IpAddress;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080031import org.onlab.packet.MacAddress;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.VlanId;
Charles M.C. Chan956cb692015-04-26 18:49:39 +080033import org.onlab.packet.ipv6.IExtensionHeader;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.ndp.NeighborAdvertisement;
35import org.onlab.packet.ndp.NeighborSolicitation;
Charles M.C. Chan441d7da2015-03-17 21:03:39 +080036import org.onlab.packet.ndp.RouterAdvertisement;
37import org.onlab.packet.ndp.RouterSolicitation;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070038import org.onosproject.cfg.ComponentConfigService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.ConnectPoint;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080042import org.onosproject.net.Device;
Brian O'Connorabafb502014-12-02 22:26:20 -080043import org.onosproject.net.Host;
44import org.onosproject.net.HostId;
45import org.onosproject.net.HostLocation;
46import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080049import org.onosproject.net.flow.DefaultTrafficSelector;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080050import org.onosproject.net.flow.TrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.host.DefaultHostDescription;
52import org.onosproject.net.host.HostDescription;
53import org.onosproject.net.host.HostProvider;
54import org.onosproject.net.host.HostProviderRegistry;
55import org.onosproject.net.host.HostProviderService;
56import org.onosproject.net.host.HostService;
57import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080058import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.packet.PacketProcessor;
60import org.onosproject.net.packet.PacketService;
61import org.onosproject.net.provider.AbstractProvider;
62import org.onosproject.net.provider.ProviderId;
63import org.onosproject.net.topology.Topology;
64import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33601602014-11-19 03:32:15 -080065import org.osgi.service.component.ComponentContext;
alshabibe1cf87d2014-10-17 09:23:50 -070066import org.slf4j.Logger;
67
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070068import java.util.Dictionary;
69import java.util.Set;
Madan Jampania3770c32015-12-11 12:07:41 -080070import java.util.concurrent.ExecutorService;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070071
72import static com.google.common.base.Strings.isNullOrEmpty;
Madan Jampania3770c32015-12-11 12:07:41 -080073import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
74import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuskaf845cf62015-03-24 10:13:09 -070075import static org.slf4j.LoggerFactory.getLogger;
76
alshabibe1cf87d2014-10-17 09:23:50 -070077/**
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -070078 * Provider which uses an OpenFlow controller to detect network end-station
79 * hosts.
alshabibe1cf87d2014-10-17 09:23:50 -070080 */
81@Component(immediate = true)
82public class HostLocationProvider extends AbstractProvider implements HostProvider {
83
84 private final Logger log = getLogger(getClass());
85
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected CoreService coreService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibe1cf87d2014-10-17 09:23:50 -070090 protected HostProviderRegistry providerRegistry;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -080093 protected PacketService packetService;
alshabibe1cf87d2014-10-17 09:23:50 -070094
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected TopologyService topologyService;
97
Thomas Vachuska33601602014-11-19 03:32:15 -080098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected HostService hostService;
100
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected DeviceService deviceService;
103
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ComponentConfigService cfgService;
106
alshabibe1cf87d2014-10-17 09:23:50 -0700107 private HostProviderService providerService;
108
109 private final InternalHostProvider processor = new InternalHostProvider();
Thomas Vachuska33601602014-11-19 03:32:15 -0800110 private final DeviceListener deviceListener = new InternalDeviceListener();
111
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800112 private ApplicationId appId;
113
Thomas Vachuska33601602014-11-19 03:32:15 -0800114 @Property(name = "hostRemovalEnabled", boolValue = true,
115 label = "Enable host removal on port/device down events")
116 private boolean hostRemovalEnabled = true;
alshabibe1cf87d2014-10-17 09:23:50 -0700117
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800118 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700119 label = "Enable using IPv6 Neighbor Discovery by the " +
120 "Host Location Provider; default is false")
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800121 private boolean ipv6NeighborDiscovery = false;
alshabibe1cf87d2014-10-17 09:23:50 -0700122
Jonathan Hart9af322d2016-01-06 17:42:04 -0800123 @Property(name = "requestInterceptsEnabled", boolValue = true,
124 label = "Enable requesting packet intercepts")
125 private boolean requestInterceptsEnabled = true;
126
Madan Jampania3770c32015-12-11 12:07:41 -0800127 protected ExecutorService eventHandler;
128
alshabibe1cf87d2014-10-17 09:23:50 -0700129 /**
130 * Creates an OpenFlow host provider.
131 */
132 public HostLocationProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800133 super(new ProviderId("of", "org.onosproject.provider.host"));
alshabibe1cf87d2014-10-17 09:23:50 -0700134 }
135
136 @Activate
Thomas Vachuska33601602014-11-19 03:32:15 -0800137 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700138 cfgService.registerProperties(getClass());
139 appId = coreService.registerApplication("org.onosproject.provider.host");
Jonathan Hart9af322d2016-01-06 17:42:04 -0800140 eventHandler = newSingleThreadScheduledExecutor(
141 groupedThreads("onos/host-loc-provider", "event-handler"));
alshabibe1cf87d2014-10-17 09:23:50 -0700142 providerService = providerRegistry.register(this);
Brian O'Connor3b783262015-07-29 17:49:24 -0700143 packetService.addProcessor(processor, PacketProcessor.advisor(1));
Thomas Vachuska33601602014-11-19 03:32:15 -0800144 deviceService.addListener(deviceListener);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800145
146 modified(context);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -0800147
Charles M.C. Chane148de82015-05-06 12:38:21 +0800148 log.info("Started with Application ID {}", appId.id());
149 }
150
151 @Deactivate
152 public void deactivate() {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800153 cfgService.unregisterProperties(getClass(), false);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700154
155 withdrawIntercepts();
156
Charles M.C. Chane148de82015-05-06 12:38:21 +0800157 providerRegistry.unregister(this);
158 packetService.removeProcessor(processor);
159 deviceService.removeListener(deviceListener);
Madan Jampania3770c32015-12-11 12:07:41 -0800160 eventHandler.shutdown();
Charles M.C. Chane148de82015-05-06 12:38:21 +0800161 providerService = null;
162 log.info("Stopped");
163 }
164
165 @Modified
166 public void modified(ComponentContext context) {
Charles M.C. Chane148de82015-05-06 12:38:21 +0800167 readComponentConfiguration(context);
Jonathan Hart9af322d2016-01-06 17:42:04 -0800168
169 if (requestInterceptsEnabled) {
170 requestIntercepts();
171 } else {
172 withdrawIntercepts();
173 }
Charles M.C. Chane148de82015-05-06 12:38:21 +0800174 }
175
176 /**
Thomas Vachuska27bee092015-06-23 19:03:10 -0700177 * Request packet intercepts.
Charles M.C. Chane148de82015-05-06 12:38:21 +0800178 */
Thomas Vachuska27bee092015-06-23 19:03:10 -0700179 private void requestIntercepts() {
180 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
181 selector.matchEthType(Ethernet.TYPE_ARP);
182 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800183
Thomas Vachuska27bee092015-06-23 19:03:10 -0700184 // IPv6 Neighbor Solicitation packet.
185 selector.matchEthType(Ethernet.TYPE_IPV6);
186 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
187 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800188 if (ipv6NeighborDiscovery) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700189 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
190 } else {
191 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800192 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700193
194 // IPv6 Neighbor Advertisement packet.
195 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
196 if (ipv6NeighborDiscovery) {
197 packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId);
198 } else {
199 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
200 }
201 }
202
203 /**
204 * Withdraw packet intercepts.
205 */
206 private void withdrawIntercepts() {
207 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
208 selector.matchEthType(Ethernet.TYPE_ARP);
Aaron Kruglikov07a923d2015-07-03 13:30:57 -0700209 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Thomas Vachuska27bee092015-06-23 19:03:10 -0700210
211 // IPv6 Neighbor Solicitation packet.
212 selector.matchEthType(Ethernet.TYPE_IPV6);
213 selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
214 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
215 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
216
217 // IPv6 Neighbor Advertisement packet.
218 selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
219 packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800220 }
221
222 /**
223 * Extracts properties from the component configuration context.
224 *
225 * @param context the component context
226 */
227 private void readComponentConfiguration(ComponentContext context) {
228 Dictionary<?, ?> properties = context.getProperties();
229 Boolean flag;
230
231 flag = isPropertyEnabled(properties, "hostRemovalEnabled");
232 if (flag == null) {
233 log.info("Host removal on port/device down events is not configured, " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700234 "using current value of {}", hostRemovalEnabled);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800235 } else {
236 hostRemovalEnabled = flag;
237 log.info("Configured. Host removal on port/device down events is {}",
238 hostRemovalEnabled ? "enabled" : "disabled");
Thomas Vachuska33601602014-11-19 03:32:15 -0800239 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800240
241 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
242 if (flag == null) {
243 log.info("Using IPv6 Neighbor Discovery is not configured, " +
Thomas Vachuska27bee092015-06-23 19:03:10 -0700244 "using current value of {}", ipv6NeighborDiscovery);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800245 } else {
246 ipv6NeighborDiscovery = flag;
247 log.info("Configured. Using IPv6 Neighbor Discovery is {}",
248 ipv6NeighborDiscovery ? "enabled" : "disabled");
249 }
Jonathan Hart9af322d2016-01-06 17:42:04 -0800250
251 flag = isPropertyEnabled(properties, "requestInterceptsEnabled");
252 if (flag == null) {
253 log.info("Request intercepts is not configured, " +
254 "using current value of {}", requestInterceptsEnabled);
255 } else {
256 requestInterceptsEnabled = flag;
257 log.info("Configured. Request intercepts is {}",
258 requestInterceptsEnabled ? "enabled" : "disabled");
259 }
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800260 }
261
262 /**
263 * Check property name is defined and set to true.
264 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700265 * @param properties properties to be looked up
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800266 * @param propertyName the name of the property to look up
267 * @return value when the propertyName is defined or return null
268 */
269 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
270 String propertyName) {
271 Boolean value = null;
272 try {
273 String s = (String) properties.get(propertyName);
274 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
275 } catch (ClassCastException e) {
276 // No propertyName defined.
277 value = null;
278 }
279 return value;
Thomas Vachuska33601602014-11-19 03:32:15 -0800280 }
281
alshabibe1cf87d2014-10-17 09:23:50 -0700282 @Override
283 public void triggerProbe(Host host) {
284 log.info("Triggering probe on device {}", host);
285 }
286
287 private class InternalHostProvider implements PacketProcessor {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800288 /**
289 * Update host location only.
290 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700291 * @param hid host ID
292 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800293 * @param vlan VLAN ID
294 * @param hloc host location
295 */
296 private void updateLocation(HostId hid, MacAddress mac,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700297 VlanId vlan, HostLocation hloc) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800298 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700299 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800300 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700301 } catch (IllegalStateException e) {
302 log.debug("Host {} suppressed", hid);
303 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800304 }
Thomas Vachuska27bee092015-06-23 19:03:10 -0700305
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800306 /**
307 * Update host location and IP address.
308 *
Thomas Vachuska27bee092015-06-23 19:03:10 -0700309 * @param hid host ID
310 * @param mac source Mac address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800311 * @param vlan VLAN ID
312 * @param hloc host location
Thomas Vachuska27bee092015-06-23 19:03:10 -0700313 * @param ip source IP address
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800314 */
315 private void updateLocationIP(HostId hid, MacAddress mac,
316 VlanId vlan, HostLocation hloc,
317 IpAddress ip) {
Thomas Vachuskae7966102015-09-09 17:33:33 -0700318 HostDescription desc = ip.isZero() || ip.isSelfAssigned() ?
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700319 new DefaultHostDescription(mac, vlan, hloc) :
320 new DefaultHostDescription(mac, vlan, hloc, ip);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700321 try {
Ray Milkeydc083442016-02-22 11:27:57 -0800322 providerService.hostDetected(hid, desc, false);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700323 } catch (IllegalStateException e) {
324 log.debug("Host {} suppressed", hid);
325 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800326 }
alshabibe1cf87d2014-10-17 09:23:50 -0700327
328 @Override
329 public void process(PacketContext context) {
alshabib4a179dc2014-10-17 17:17:01 -0700330 if (context == null) {
331 return;
332 }
alshabibe1cf87d2014-10-17 09:23:50 -0700333
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700334 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800335 if (eth == null) {
336 return;
337 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800338 MacAddress srcMac = eth.getSourceMAC();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800339
alshabibe1cf87d2014-10-17 09:23:50 -0700340 VlanId vlan = VlanId.vlanId(eth.getVlanID());
341 ConnectPoint heardOn = context.inPacket().receivedFrom();
342
Thomas Vachuskaf845cf62015-03-24 10:13:09 -0700343 // If this arrived on control port, bail out.
344 if (heardOn.port().isLogical()) {
345 return;
346 }
347
alshabibe1cf87d2014-10-17 09:23:50 -0700348 // If this is not an edge port, bail out.
349 Topology topology = topologyService.currentTopology();
350 if (topologyService.isInfrastructure(topology, heardOn)) {
351 return;
352 }
353
Thomas Vachuskaec9c7dd2015-09-03 18:30:04 -0700354 HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis());
alshabibe1cf87d2014-10-17 09:23:50 -0700355 HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
356
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800357 // ARP: possible new hosts, update both location and IP
alshabibe1cf87d2014-10-17 09:23:50 -0700358 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
359 ARP arp = (ARP) eth.getPayload();
Pavlin Radoslavovd6612f92015-02-23 13:53:32 -0800360 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET,
361 arp.getSenderProtocolAddress());
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800362 updateLocationIP(hid, srcMac, vlan, hloc, ip);
alshabibe1cf87d2014-10-17 09:23:50 -0700363
Charles Chan5d5e0622015-09-25 13:00:06 -0700364 // IPv4: update location only
alshabibe1cf87d2014-10-17 09:23:50 -0700365 } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800366 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700367
Charles Chan5d5e0622015-09-25 13:00:06 -0700368 //
369 // NeighborAdvertisement and NeighborSolicitation: possible
370 // new hosts, update both location and IP.
371 //
372 // IPv6: update location only
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800373 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800374 IPv6 ipv6 = (IPv6) eth.getPayload();
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800375 IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6,
Thomas Vachuska27bee092015-06-23 19:03:10 -0700376 ipv6.getSourceAddress());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800377
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800378 // skip extension headers
379 IPacket pkt = ipv6;
380 while (pkt.getPayload() != null &&
381 pkt.getPayload() instanceof IExtensionHeader) {
382 pkt = pkt.getPayload();
383 }
384
385 // Neighbor Discovery Protocol
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800386 pkt = pkt.getPayload();
387 if (pkt != null && pkt instanceof ICMP6) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800388 pkt = pkt.getPayload();
389 // RouterSolicitation, RouterAdvertisement
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800390 if (pkt != null && (pkt instanceof RouterAdvertisement ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700391 pkt instanceof RouterSolicitation)) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800392 return;
393 }
Charles M.C. Chan9148d2d2015-04-27 03:36:39 +0800394 if (pkt != null && (pkt instanceof NeighborSolicitation ||
Thomas Vachuska27bee092015-06-23 19:03:10 -0700395 pkt instanceof NeighborAdvertisement)) {
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800396 // Duplicate Address Detection
397 if (ip.isZero()) {
Charles M.C. Chan441d7da2015-03-17 21:03:39 +0800398 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800399 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800400 // NeighborSolicitation, NeighborAdvertisement
401 updateLocationIP(hid, srcMac, vlan, hloc, ip);
402 return;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800403 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800404 }
Charles M.C. Chan956cb692015-04-26 18:49:39 +0800405
406 // multicast
407 if (eth.isMulticast()) {
408 return;
409 }
410
411 // normal IPv6 packets
412 updateLocation(hid, srcMac, vlan, hloc);
alshabibe1cf87d2014-10-17 09:23:50 -0700413 }
414 }
415 }
Thomas Vachuska33601602014-11-19 03:32:15 -0800416
417 // Auxiliary listener to device events.
418 private class InternalDeviceListener implements DeviceListener {
419 @Override
420 public void event(DeviceEvent event) {
Madan Jampania3770c32015-12-11 12:07:41 -0800421 eventHandler.execute(() -> handleEvent(event));
422 }
423
424 private void handleEvent(DeviceEvent event) {
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800425 Device device = event.subject();
426 switch (event.type()) {
Thomas Vachuska27bee092015-06-23 19:03:10 -0700427 case DEVICE_ADDED:
428 break;
429 case DEVICE_AVAILABILITY_CHANGED:
430 if (hostRemovalEnabled &&
431 !deviceService.isAvailable(device.id())) {
432 removeHosts(hostService.getConnectedHosts(device.id()));
433 }
434 break;
435 case DEVICE_SUSPENDED:
436 case DEVICE_UPDATED:
437 // Nothing to do?
438 break;
439 case DEVICE_REMOVED:
440 if (hostRemovalEnabled) {
441 removeHosts(hostService.getConnectedHosts(device.id()));
442 }
443 break;
444 case PORT_ADDED:
445 break;
446 case PORT_UPDATED:
447 if (hostRemovalEnabled) {
448 ConnectPoint point =
449 new ConnectPoint(device.id(), event.port().number());
450 removeHosts(hostService.getConnectedHosts(point));
451 }
452 break;
453 case PORT_REMOVED:
454 // Nothing to do?
455 break;
456 default:
457 break;
Thomas Vachuska33601602014-11-19 03:32:15 -0800458 }
459 }
460 }
461
462 // Signals host vanish for all specified hosts.
463 private void removeHosts(Set<Host> hosts) {
464 for (Host host : hosts) {
465 providerService.hostVanished(host.id());
466 }
467 }
468
alshabibe1cf87d2014-10-17 09:23:50 -0700469}