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 | /** |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 75 | * Provider which uses an OpenFlow controller to detect network end-station |
| 76 | * hosts. |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 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, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 116 | label = "Enable using IPv6 Neighbor Discovery by the " + |
| 117 | "Host Location Provider; default is false") |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 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); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 133 | packetService.addProcessor(processor, PacketProcessor.advisor(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); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 136 | requestIntercepts(); |
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() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 143 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 144 | |
| 145 | withdrawIntercepts(); |
| 146 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 147 | providerRegistry.unregister(this); |
| 148 | packetService.removeProcessor(processor); |
| 149 | deviceService.removeListener(deviceListener); |
| 150 | providerService = null; |
| 151 | log.info("Stopped"); |
| 152 | } |
| 153 | |
| 154 | @Modified |
| 155 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 156 | readComponentConfiguration(context); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 157 | requestIntercepts(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 161 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 162 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 163 | private void requestIntercepts() { |
| 164 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 165 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 166 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 167 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 168 | // IPv6 Neighbor Solicitation packet. |
| 169 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 170 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 171 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 172 | if (ipv6NeighborDiscovery) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 173 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 174 | } else { |
| 175 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 176 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 177 | |
| 178 | // IPv6 Neighbor Advertisement packet. |
| 179 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 180 | if (ipv6NeighborDiscovery) { |
| 181 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 182 | } else { |
| 183 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Withdraw packet intercepts. |
| 189 | */ |
| 190 | private void withdrawIntercepts() { |
| 191 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 192 | selector.matchEthType(Ethernet.TYPE_ARP); |
Aaron Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 193 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 194 | |
| 195 | // IPv6 Neighbor Solicitation packet. |
| 196 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 197 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 198 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 199 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 200 | |
| 201 | // IPv6 Neighbor Advertisement packet. |
| 202 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 203 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Extracts properties from the component configuration context. |
| 208 | * |
| 209 | * @param context the component context |
| 210 | */ |
| 211 | private void readComponentConfiguration(ComponentContext context) { |
| 212 | Dictionary<?, ?> properties = context.getProperties(); |
| 213 | Boolean flag; |
| 214 | |
| 215 | flag = isPropertyEnabled(properties, "hostRemovalEnabled"); |
| 216 | if (flag == null) { |
| 217 | log.info("Host removal on port/device down events is not configured, " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 218 | "using current value of {}", hostRemovalEnabled); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 219 | } else { |
| 220 | hostRemovalEnabled = flag; |
| 221 | log.info("Configured. Host removal on port/device down events is {}", |
| 222 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 223 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 224 | |
| 225 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 226 | if (flag == null) { |
| 227 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 228 | "using current value of {}", ipv6NeighborDiscovery); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 229 | } else { |
| 230 | ipv6NeighborDiscovery = flag; |
| 231 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
| 232 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Check property name is defined and set to true. |
| 238 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 239 | * @param properties properties to be looked up |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 240 | * @param propertyName the name of the property to look up |
| 241 | * @return value when the propertyName is defined or return null |
| 242 | */ |
| 243 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 244 | String propertyName) { |
| 245 | Boolean value = null; |
| 246 | try { |
| 247 | String s = (String) properties.get(propertyName); |
| 248 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 249 | } catch (ClassCastException e) { |
| 250 | // No propertyName defined. |
| 251 | value = null; |
| 252 | } |
| 253 | return value; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 254 | } |
| 255 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 256 | @Override |
| 257 | public void triggerProbe(Host host) { |
| 258 | log.info("Triggering probe on device {}", host); |
| 259 | } |
| 260 | |
| 261 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 262 | /** |
| 263 | * Update host location only. |
| 264 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 265 | * @param hid host ID |
| 266 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 267 | * @param vlan VLAN ID |
| 268 | * @param hloc host location |
| 269 | */ |
| 270 | private void updateLocation(HostId hid, MacAddress mac, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 271 | VlanId vlan, HostLocation hloc) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 272 | HostDescription desc = new DefaultHostDescription(mac, vlan, hloc); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 273 | try { |
| 274 | providerService.hostDetected(hid, desc); |
| 275 | } catch (IllegalStateException e) { |
| 276 | log.debug("Host {} suppressed", hid); |
| 277 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 278 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 279 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 280 | /** |
| 281 | * Update host location and IP address. |
| 282 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 283 | * @param hid host ID |
| 284 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 285 | * @param vlan VLAN ID |
| 286 | * @param hloc host location |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 287 | * @param ip source IP address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 288 | */ |
| 289 | private void updateLocationIP(HostId hid, MacAddress mac, |
| 290 | VlanId vlan, HostLocation hloc, |
| 291 | IpAddress ip) { |
Thomas Vachuska | e796610 | 2015-09-09 17:33:33 -0700 | [diff] [blame] | 292 | HostDescription desc = ip.isZero() || ip.isSelfAssigned() ? |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 293 | new DefaultHostDescription(mac, vlan, hloc) : |
| 294 | new DefaultHostDescription(mac, vlan, hloc, ip); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 295 | try { |
| 296 | providerService.hostDetected(hid, desc); |
| 297 | } catch (IllegalStateException e) { |
| 298 | log.debug("Host {} suppressed", hid); |
| 299 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 300 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 301 | |
| 302 | @Override |
| 303 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 304 | if (context == null) { |
| 305 | return; |
| 306 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 307 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 308 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 309 | if (eth == null) { |
| 310 | return; |
| 311 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 312 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 313 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 314 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 315 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 316 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 317 | // If this arrived on control port, bail out. |
| 318 | if (heardOn.port().isLogical()) { |
| 319 | return; |
| 320 | } |
| 321 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 322 | // If this is not an edge port, bail out. |
| 323 | Topology topology = topologyService.currentTopology(); |
| 324 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 325 | return; |
| 326 | } |
| 327 | |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 328 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 329 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 330 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 331 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 332 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 333 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 334 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 335 | arp.getSenderProtocolAddress()); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 336 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 337 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame^] | 338 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 339 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 340 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 341 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame^] | 342 | // |
| 343 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 344 | // new hosts, update both location and IP. |
| 345 | // |
| 346 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 347 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 348 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 349 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 350 | ipv6.getSourceAddress()); |
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 | // skip extension headers |
| 353 | IPacket pkt = ipv6; |
| 354 | while (pkt.getPayload() != null && |
| 355 | pkt.getPayload() instanceof IExtensionHeader) { |
| 356 | pkt = pkt.getPayload(); |
| 357 | } |
| 358 | |
| 359 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 360 | pkt = pkt.getPayload(); |
| 361 | if (pkt != null && pkt instanceof ICMP6) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 362 | pkt = pkt.getPayload(); |
| 363 | // RouterSolicitation, RouterAdvertisement |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 364 | if (pkt != null && (pkt instanceof RouterAdvertisement || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 365 | pkt instanceof RouterSolicitation)) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 366 | return; |
| 367 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 368 | if (pkt != null && (pkt instanceof NeighborSolicitation || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 369 | pkt instanceof NeighborAdvertisement)) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 370 | // Duplicate Address Detection |
| 371 | if (ip.isZero()) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 372 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 373 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 374 | // NeighborSolicitation, NeighborAdvertisement |
| 375 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
| 376 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 377 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 378 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 379 | |
| 380 | // multicast |
| 381 | if (eth.isMulticast()) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // normal IPv6 packets |
| 386 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 390 | |
| 391 | // Auxiliary listener to device events. |
| 392 | private class InternalDeviceListener implements DeviceListener { |
| 393 | @Override |
| 394 | public void event(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 395 | Device device = event.subject(); |
| 396 | switch (event.type()) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 397 | case DEVICE_ADDED: |
| 398 | break; |
| 399 | case DEVICE_AVAILABILITY_CHANGED: |
| 400 | if (hostRemovalEnabled && |
| 401 | !deviceService.isAvailable(device.id())) { |
| 402 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 403 | } |
| 404 | break; |
| 405 | case DEVICE_SUSPENDED: |
| 406 | case DEVICE_UPDATED: |
| 407 | // Nothing to do? |
| 408 | break; |
| 409 | case DEVICE_REMOVED: |
| 410 | if (hostRemovalEnabled) { |
| 411 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 412 | } |
| 413 | break; |
| 414 | case PORT_ADDED: |
| 415 | break; |
| 416 | case PORT_UPDATED: |
| 417 | if (hostRemovalEnabled) { |
| 418 | ConnectPoint point = |
| 419 | new ConnectPoint(device.id(), event.port().number()); |
| 420 | removeHosts(hostService.getConnectedHosts(point)); |
| 421 | } |
| 422 | break; |
| 423 | case PORT_REMOVED: |
| 424 | // Nothing to do? |
| 425 | break; |
| 426 | default: |
| 427 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Signals host vanish for all specified hosts. |
| 433 | private void removeHosts(Set<Host> hosts) { |
| 434 | for (Host host : hosts) { |
| 435 | providerService.hostVanished(host.id()); |
| 436 | } |
| 437 | } |
| 438 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 439 | } |