Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.host.impl; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 25 | import org.onlab.packet.ARP; |
| 26 | import org.onlab.packet.Ethernet; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 27 | import org.onlab.packet.ICMP6; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IPacket; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IPv6; |
| 30 | import org.onlab.packet.IpAddress; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 31 | import org.onlab.packet.MacAddress; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 33 | import org.onlab.packet.ipv6.IExtensionHeader; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 34 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 35 | import org.onlab.packet.ndp.NeighborSolicitation; |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 36 | import org.onlab.packet.ndp.RouterAdvertisement; |
| 37 | import org.onlab.packet.ndp.RouterSolicitation; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 38 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 39 | import org.onosproject.core.ApplicationId; |
| 40 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 42 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; |
| 45 | import org.onosproject.net.HostLocation; |
| 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.host.DefaultHostDescription; |
| 52 | import org.onosproject.net.host.HostDescription; |
| 53 | import org.onosproject.net.host.HostProvider; |
| 54 | import org.onosproject.net.host.HostProviderRegistry; |
| 55 | import org.onosproject.net.host.HostProviderService; |
| 56 | import org.onosproject.net.host.HostService; |
| 57 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 58 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketProcessor; |
| 60 | import org.onosproject.net.packet.PacketService; |
| 61 | import org.onosproject.net.provider.AbstractProvider; |
| 62 | import org.onosproject.net.provider.ProviderId; |
| 63 | import org.onosproject.net.topology.Topology; |
| 64 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 65 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 68 | import java.util.Dictionary; |
| 69 | import java.util.Set; |
| 70 | |
| 71 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Provider which uses an OpenFlow controller to detect network |
| 76 | * end-station hosts. |
| 77 | */ |
| 78 | @Component(immediate = true) |
| 79 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 80 | |
| 81 | private final Logger log = getLogger(getClass()); |
| 82 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected CoreService coreService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 87 | protected HostProviderRegistry providerRegistry; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 90 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected TopologyService topologyService; |
| 94 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected HostService hostService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected DeviceService deviceService; |
| 100 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected ComponentConfigService cfgService; |
| 103 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 104 | private HostProviderService providerService; |
| 105 | |
| 106 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 107 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 108 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 109 | private ApplicationId appId; |
| 110 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 111 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 112 | label = "Enable host removal on port/device down events") |
| 113 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 114 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 115 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
| 116 | label = "Enable using IPv6 Neighbor Discovery by the " + |
| 117 | "Host Location Provider; default is false") |
| 118 | private boolean ipv6NeighborDiscovery = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Creates an OpenFlow host provider. |
| 122 | */ |
| 123 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 124 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 128 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 129 | cfgService.registerProperties(getClass()); |
| 130 | appId = coreService.registerApplication("org.onosproject.provider.host"); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 131 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 132 | providerService = providerRegistry.register(this); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 133 | packetService.addProcessor(processor, 1); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 134 | deviceService.addListener(deviceListener); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame^] | 135 | readComponentConfiguration(context); |
| 136 | requestPackests(); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 137 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame^] | 138 | log.info("Started with Application ID {}", appId.id()); |
| 139 | } |
| 140 | |
| 141 | @Deactivate |
| 142 | public void deactivate() { |
| 143 | // TODO revoke all packet requests when deactivate |
| 144 | cfgService.unregisterProperties(getClass(), false); |
| 145 | providerRegistry.unregister(this); |
| 146 | packetService.removeProcessor(processor); |
| 147 | deviceService.removeListener(deviceListener); |
| 148 | providerService = null; |
| 149 | log.info("Stopped"); |
| 150 | } |
| 151 | |
| 152 | @Modified |
| 153 | public void modified(ComponentContext context) { |
| 154 | // TODO revoke unnecessary packet requests when config being modified |
| 155 | readComponentConfiguration(context); |
| 156 | requestPackests(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Request packet in via PacketService. |
| 161 | */ |
| 162 | private void requestPackests() { |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 163 | TrafficSelector.Builder selectorBuilder = |
| 164 | DefaultTrafficSelector.builder(); |
| 165 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 166 | packetService.requestPackets(selectorBuilder.build(), |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame^] | 167 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 168 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 169 | if (ipv6NeighborDiscovery) { |
| 170 | // IPv6 Neighbor Solicitation packet. |
| 171 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 172 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 173 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 174 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 175 | packetService.requestPackets(selectorBuilder.build(), |
| 176 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 177 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 178 | // IPv6 Neighbor Advertisement packet. |
| 179 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 180 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 181 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 182 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 183 | packetService.requestPackets(selectorBuilder.build(), |
| 184 | PacketPriority.CONTROL, appId); |
| 185 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Extracts properties from the component configuration context. |
| 190 | * |
| 191 | * @param context the component context |
| 192 | */ |
| 193 | private void readComponentConfiguration(ComponentContext context) { |
| 194 | Dictionary<?, ?> properties = context.getProperties(); |
| 195 | Boolean flag; |
| 196 | |
| 197 | flag = isPropertyEnabled(properties, "hostRemovalEnabled"); |
| 198 | if (flag == null) { |
| 199 | log.info("Host removal on port/device down events is not configured, " + |
| 200 | "using current value of {}", hostRemovalEnabled); |
| 201 | } else { |
| 202 | hostRemovalEnabled = flag; |
| 203 | log.info("Configured. Host removal on port/device down events is {}", |
| 204 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 205 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 206 | |
| 207 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 208 | if (flag == null) { |
| 209 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
| 210 | "using current value of {}", ipv6NeighborDiscovery); |
| 211 | } else { |
| 212 | ipv6NeighborDiscovery = flag; |
| 213 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
| 214 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Check property name is defined and set to true. |
| 220 | * |
| 221 | * @param properties properties to be looked up |
| 222 | * @param propertyName the name of the property to look up |
| 223 | * @return value when the propertyName is defined or return null |
| 224 | */ |
| 225 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 226 | String propertyName) { |
| 227 | Boolean value = null; |
| 228 | try { |
| 229 | String s = (String) properties.get(propertyName); |
| 230 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 231 | } catch (ClassCastException e) { |
| 232 | // No propertyName defined. |
| 233 | value = null; |
| 234 | } |
| 235 | return value; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 236 | } |
| 237 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public void triggerProbe(Host host) { |
| 240 | log.info("Triggering probe on device {}", host); |
| 241 | } |
| 242 | |
| 243 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 244 | /** |
| 245 | * Update host location only. |
| 246 | * |
| 247 | * @param hid host ID |
| 248 | * @param mac source Mac address |
| 249 | * @param vlan VLAN ID |
| 250 | * @param hloc host location |
| 251 | */ |
| 252 | private void updateLocation(HostId hid, MacAddress mac, |
| 253 | VlanId vlan, HostLocation hloc) { |
| 254 | HostDescription desc = new DefaultHostDescription(mac, vlan, hloc); |
| 255 | providerService.hostDetected(hid, desc); |
| 256 | } |
| 257 | /** |
| 258 | * Update host location and IP address. |
| 259 | * |
| 260 | * @param hid host ID |
| 261 | * @param mac source Mac address |
| 262 | * @param vlan VLAN ID |
| 263 | * @param hloc host location |
| 264 | * @param ip source IP address |
| 265 | */ |
| 266 | private void updateLocationIP(HostId hid, MacAddress mac, |
| 267 | VlanId vlan, HostLocation hloc, |
| 268 | IpAddress ip) { |
| 269 | HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ip); |
| 270 | providerService.hostDetected(hid, desc); |
| 271 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 272 | |
| 273 | @Override |
| 274 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 275 | if (context == null) { |
| 276 | return; |
| 277 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 278 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 279 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 280 | if (eth == null) { |
| 281 | return; |
| 282 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 283 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 284 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 285 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 286 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 287 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 288 | // If this arrived on control port, bail out. |
| 289 | if (heardOn.port().isLogical()) { |
| 290 | return; |
| 291 | } |
| 292 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 293 | // If this is not an edge port, bail out. |
| 294 | Topology topology = topologyService.currentTopology(); |
| 295 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 296 | return; |
| 297 | } |
| 298 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 299 | HostLocation hloc = |
| 300 | new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 301 | |
| 302 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 303 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 304 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 305 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 306 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 307 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 308 | arp.getSenderProtocolAddress()); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 309 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 310 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 311 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 312 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 313 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 314 | |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 315 | // |
| 316 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 317 | // new hosts, update both location and IP. |
| 318 | // |
| 319 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 320 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 321 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 322 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
| 323 | ipv6.getSourceAddress()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 324 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 325 | // skip extension headers |
| 326 | IPacket pkt = ipv6; |
| 327 | while (pkt.getPayload() != null && |
| 328 | pkt.getPayload() instanceof IExtensionHeader) { |
| 329 | pkt = pkt.getPayload(); |
| 330 | } |
| 331 | |
| 332 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 333 | pkt = pkt.getPayload(); |
| 334 | if (pkt != null && pkt instanceof ICMP6) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 335 | pkt = pkt.getPayload(); |
| 336 | // RouterSolicitation, RouterAdvertisement |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 337 | if (pkt != null && (pkt instanceof RouterAdvertisement || |
| 338 | pkt instanceof RouterSolicitation)) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 339 | return; |
| 340 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 341 | if (pkt != null && (pkt instanceof NeighborSolicitation || |
| 342 | pkt instanceof NeighborAdvertisement)) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 343 | // Duplicate Address Detection |
| 344 | if (ip.isZero()) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 345 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 346 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 347 | // NeighborSolicitation, NeighborAdvertisement |
| 348 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
| 349 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 350 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 351 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 352 | |
| 353 | // multicast |
| 354 | if (eth.isMulticast()) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | // normal IPv6 packets |
| 359 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 363 | |
| 364 | // Auxiliary listener to device events. |
| 365 | private class InternalDeviceListener implements DeviceListener { |
| 366 | @Override |
| 367 | public void event(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 368 | Device device = event.subject(); |
| 369 | switch (event.type()) { |
| 370 | case DEVICE_ADDED: |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 371 | break; |
| 372 | case DEVICE_AVAILABILITY_CHANGED: |
| 373 | if (hostRemovalEnabled && |
| 374 | !deviceService.isAvailable(device.id())) { |
| 375 | removeHosts(hostService.getConnectedHosts(device.id())); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 376 | } |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 377 | break; |
| 378 | case DEVICE_SUSPENDED: |
| 379 | case DEVICE_UPDATED: |
| 380 | // Nothing to do? |
| 381 | break; |
| 382 | case DEVICE_REMOVED: |
| 383 | if (hostRemovalEnabled) { |
| 384 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 385 | } |
| 386 | break; |
| 387 | case PORT_ADDED: |
| 388 | break; |
| 389 | case PORT_UPDATED: |
| 390 | if (hostRemovalEnabled) { |
| 391 | ConnectPoint point = |
| 392 | new ConnectPoint(device.id(), event.port().number()); |
| 393 | removeHosts(hostService.getConnectedHosts(point)); |
| 394 | } |
| 395 | break; |
| 396 | case PORT_REMOVED: |
| 397 | // Nothing to do? |
| 398 | break; |
| 399 | default: |
| 400 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Signals host vanish for all specified hosts. |
| 406 | private void removeHosts(Set<Host> hosts) { |
| 407 | for (Host host : hosts) { |
| 408 | providerService.hostVanished(host.id()); |
| 409 | } |
| 410 | } |
| 411 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 412 | } |