Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 38 | import org.onlab.util.Tools; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 39 | import org.onosproject.cfg.ComponentConfigService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 40 | import org.onosproject.core.ApplicationId; |
| 41 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.Host; |
| 45 | import org.onosproject.net.HostId; |
| 46 | import org.onosproject.net.HostLocation; |
| 47 | import org.onosproject.net.device.DeviceEvent; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.host.DefaultHostDescription; |
| 53 | import org.onosproject.net.host.HostDescription; |
| 54 | import org.onosproject.net.host.HostProvider; |
| 55 | import org.onosproject.net.host.HostProviderRegistry; |
| 56 | import org.onosproject.net.host.HostProviderService; |
| 57 | import org.onosproject.net.host.HostService; |
| 58 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 60 | import org.onosproject.net.packet.PacketProcessor; |
| 61 | import org.onosproject.net.packet.PacketService; |
| 62 | import org.onosproject.net.provider.AbstractProvider; |
| 63 | import org.onosproject.net.provider.ProviderId; |
| 64 | import org.onosproject.net.topology.Topology; |
| 65 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 66 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 69 | import java.util.Dictionary; |
| 70 | import java.util.Set; |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 71 | import java.util.concurrent.ExecutorService; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 72 | |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 73 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 74 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 75 | import static org.slf4j.LoggerFactory.getLogger; |
| 76 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 77 | /** |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 78 | * Provider which uses an OpenFlow controller to detect network end-station |
| 79 | * hosts. |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 80 | */ |
| 81 | @Component(immediate = true) |
| 82 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 83 | |
| 84 | private final Logger log = getLogger(getClass()); |
| 85 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected CoreService coreService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 90 | protected HostProviderRegistry providerRegistry; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 93 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected TopologyService topologyService; |
| 97 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected HostService hostService; |
| 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected DeviceService deviceService; |
| 103 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 105 | protected ComponentConfigService cfgService; |
| 106 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 107 | private HostProviderService providerService; |
| 108 | |
| 109 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 110 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 111 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 112 | private ApplicationId appId; |
| 113 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 114 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 115 | label = "Enable host removal on port/device down events") |
| 116 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 117 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 118 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 119 | label = "Enable using IPv6 Neighbor Discovery by the " + |
| 120 | "Host Location Provider; default is false") |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 121 | private boolean ipv6NeighborDiscovery = false; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 122 | |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 123 | @Property(name = "requestInterceptsEnabled", boolValue = true, |
| 124 | label = "Enable requesting packet intercepts") |
| 125 | private boolean requestInterceptsEnabled = true; |
| 126 | |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 127 | protected ExecutorService eventHandler; |
| 128 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Creates an OpenFlow host provider. |
| 131 | */ |
| 132 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 133 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 137 | public void activate(ComponentContext context) { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 138 | cfgService.registerProperties(getClass()); |
| 139 | appId = coreService.registerApplication("org.onosproject.provider.host"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 140 | eventHandler = newSingleThreadScheduledExecutor( |
HIGUCHI Yuta | d9e0105 | 2016-04-14 09:31:42 -0700 | [diff] [blame] | 141 | groupedThreads("onos/host-loc-provider", "event-handler", log)); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 142 | providerService = providerRegistry.register(this); |
Brian O'Connor | 3b78326 | 2015-07-29 17:49:24 -0700 | [diff] [blame] | 143 | packetService.addProcessor(processor, PacketProcessor.advisor(1)); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 144 | deviceService.addListener(deviceListener); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 145 | |
| 146 | modified(context); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 147 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 148 | log.info("Started with Application ID {}", appId.id()); |
| 149 | } |
| 150 | |
| 151 | @Deactivate |
| 152 | public void deactivate() { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 153 | cfgService.unregisterProperties(getClass(), false); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 154 | |
| 155 | withdrawIntercepts(); |
| 156 | |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 157 | providerRegistry.unregister(this); |
| 158 | packetService.removeProcessor(processor); |
| 159 | deviceService.removeListener(deviceListener); |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 160 | eventHandler.shutdown(); |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 161 | providerService = null; |
| 162 | log.info("Stopped"); |
| 163 | } |
| 164 | |
| 165 | @Modified |
| 166 | public void modified(ComponentContext context) { |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 167 | readComponentConfiguration(context); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 168 | |
| 169 | if (requestInterceptsEnabled) { |
| 170 | requestIntercepts(); |
| 171 | } else { |
| 172 | withdrawIntercepts(); |
| 173 | } |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 177 | * Request packet intercepts. |
Charles M.C. Chan | e148de8 | 2015-05-06 12:38:21 +0800 | [diff] [blame] | 178 | */ |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 179 | private void requestIntercepts() { |
| 180 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 181 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 182 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 183 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 184 | // IPv6 Neighbor Solicitation packet. |
| 185 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 186 | selector.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 187 | selector.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 188 | if (ipv6NeighborDiscovery) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 189 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, appId); |
| 190 | } else { |
| 191 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 192 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 193 | |
| 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 Kruglikov | 07a923d | 2015-07-03 13:30:57 -0700 | [diff] [blame] | 209 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, appId); |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 210 | |
| 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 Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 220 | } |
| 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 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 231 | flag = Tools.isPropertyEnabled(properties, "hostRemovalEnabled"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 232 | if (flag == null) { |
| 233 | log.info("Host removal on port/device down events is not configured, " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 234 | "using current value of {}", hostRemovalEnabled); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 235 | } else { |
| 236 | hostRemovalEnabled = flag; |
| 237 | log.info("Configured. Host removal on port/device down events is {}", |
| 238 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 239 | } |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 240 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 241 | flag = Tools.isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 242 | if (flag == null) { |
| 243 | log.info("Using IPv6 Neighbor Discovery is not configured, " + |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 244 | "using current value of {}", ipv6NeighborDiscovery); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 245 | } else { |
| 246 | ipv6NeighborDiscovery = flag; |
| 247 | log.info("Configured. Using IPv6 Neighbor Discovery is {}", |
| 248 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 249 | } |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 250 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 251 | flag = Tools.isPropertyEnabled(properties, "requestInterceptsEnabled"); |
Jonathan Hart | 9af322d | 2016-01-06 17:42:04 -0800 | [diff] [blame] | 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 Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame] | 260 | } |
| 261 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 262 | @Override |
| 263 | public void triggerProbe(Host host) { |
| 264 | log.info("Triggering probe on device {}", host); |
| 265 | } |
| 266 | |
| 267 | private class InternalHostProvider implements PacketProcessor { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 268 | /** |
| 269 | * Update host location only. |
| 270 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 271 | * @param hid host ID |
| 272 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 273 | * @param vlan VLAN ID |
| 274 | * @param hloc host location |
| 275 | */ |
| 276 | private void updateLocation(HostId hid, MacAddress mac, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 277 | VlanId vlan, HostLocation hloc) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 278 | HostDescription desc = new DefaultHostDescription(mac, vlan, hloc); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 279 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 280 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 281 | } catch (IllegalStateException e) { |
| 282 | log.debug("Host {} suppressed", hid); |
| 283 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 284 | } |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 285 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 286 | /** |
| 287 | * Update host location and IP address. |
| 288 | * |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 289 | * @param hid host ID |
| 290 | * @param mac source Mac address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 291 | * @param vlan VLAN ID |
| 292 | * @param hloc host location |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 293 | * @param ip source IP address |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 294 | */ |
| 295 | private void updateLocationIP(HostId hid, MacAddress mac, |
| 296 | VlanId vlan, HostLocation hloc, |
| 297 | IpAddress ip) { |
Thomas Vachuska | e796610 | 2015-09-09 17:33:33 -0700 | [diff] [blame] | 298 | HostDescription desc = ip.isZero() || ip.isSelfAssigned() ? |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 299 | new DefaultHostDescription(mac, vlan, hloc) : |
| 300 | new DefaultHostDescription(mac, vlan, hloc, ip); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 301 | try { |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 302 | providerService.hostDetected(hid, desc, false); |
Sahil Lele | 3a0cdd5 | 2015-07-21 14:16:31 -0700 | [diff] [blame] | 303 | } catch (IllegalStateException e) { |
| 304 | log.debug("Host {} suppressed", hid); |
| 305 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 306 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 307 | |
| 308 | @Override |
| 309 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 310 | if (context == null) { |
| 311 | return; |
| 312 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 313 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 314 | Ethernet eth = context.inPacket().parsed(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 315 | if (eth == null) { |
| 316 | return; |
| 317 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 318 | MacAddress srcMac = eth.getSourceMAC(); |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 319 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 320 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 321 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 322 | |
Thomas Vachuska | f845cf6 | 2015-03-24 10:13:09 -0700 | [diff] [blame] | 323 | // If this arrived on control port, bail out. |
| 324 | if (heardOn.port().isLogical()) { |
| 325 | return; |
| 326 | } |
| 327 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 328 | // If this is not an edge port, bail out. |
| 329 | Topology topology = topologyService.currentTopology(); |
| 330 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 331 | return; |
| 332 | } |
| 333 | |
Thomas Vachuska | ec9c7dd | 2015-09-03 18:30:04 -0700 | [diff] [blame] | 334 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 335 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 336 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 337 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 338 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 339 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame] | 340 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 341 | arp.getSenderProtocolAddress()); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 342 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 343 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 344 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 345 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 346 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 347 | |
Charles Chan | 5d5e062 | 2015-09-25 13:00:06 -0700 | [diff] [blame] | 348 | // |
| 349 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 350 | // new hosts, update both location and IP. |
| 351 | // |
| 352 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 353 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 354 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 355 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET6, |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 356 | ipv6.getSourceAddress()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 357 | |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 358 | // skip extension headers |
| 359 | IPacket pkt = ipv6; |
| 360 | while (pkt.getPayload() != null && |
| 361 | pkt.getPayload() instanceof IExtensionHeader) { |
| 362 | pkt = pkt.getPayload(); |
| 363 | } |
| 364 | |
| 365 | // Neighbor Discovery Protocol |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 366 | pkt = pkt.getPayload(); |
| 367 | if (pkt != null && pkt instanceof ICMP6) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 368 | pkt = pkt.getPayload(); |
| 369 | // RouterSolicitation, RouterAdvertisement |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 370 | if (pkt != null && (pkt instanceof RouterAdvertisement || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 371 | pkt instanceof RouterSolicitation)) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 372 | return; |
| 373 | } |
Charles M.C. Chan | 9148d2d | 2015-04-27 03:36:39 +0800 | [diff] [blame] | 374 | if (pkt != null && (pkt instanceof NeighborSolicitation || |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 375 | pkt instanceof NeighborAdvertisement)) { |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 376 | // Duplicate Address Detection |
| 377 | if (ip.isZero()) { |
Charles M.C. Chan | 441d7da | 2015-03-17 21:03:39 +0800 | [diff] [blame] | 378 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 379 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 380 | // NeighborSolicitation, NeighborAdvertisement |
| 381 | updateLocationIP(hid, srcMac, vlan, hloc, ip); |
| 382 | return; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 383 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 384 | } |
Charles M.C. Chan | 956cb69 | 2015-04-26 18:49:39 +0800 | [diff] [blame] | 385 | |
| 386 | // multicast |
| 387 | if (eth.isMulticast()) { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | // normal IPv6 packets |
| 392 | updateLocation(hid, srcMac, vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 396 | |
| 397 | // Auxiliary listener to device events. |
| 398 | private class InternalDeviceListener implements DeviceListener { |
| 399 | @Override |
| 400 | public void event(DeviceEvent event) { |
Madan Jampani | a3770c3 | 2015-12-11 12:07:41 -0800 | [diff] [blame] | 401 | eventHandler.execute(() -> handleEvent(event)); |
| 402 | } |
| 403 | |
| 404 | private void handleEvent(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 405 | Device device = event.subject(); |
| 406 | switch (event.type()) { |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 407 | case DEVICE_ADDED: |
| 408 | break; |
| 409 | case DEVICE_AVAILABILITY_CHANGED: |
| 410 | if (hostRemovalEnabled && |
| 411 | !deviceService.isAvailable(device.id())) { |
| 412 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 413 | } |
| 414 | break; |
| 415 | case DEVICE_SUSPENDED: |
| 416 | case DEVICE_UPDATED: |
| 417 | // Nothing to do? |
| 418 | break; |
| 419 | case DEVICE_REMOVED: |
| 420 | if (hostRemovalEnabled) { |
| 421 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 422 | } |
| 423 | break; |
| 424 | case PORT_ADDED: |
| 425 | break; |
| 426 | case PORT_UPDATED: |
| 427 | if (hostRemovalEnabled) { |
| 428 | ConnectPoint point = |
| 429 | new ConnectPoint(device.id(), event.port().number()); |
| 430 | removeHosts(hostService.getConnectedHosts(point)); |
| 431 | } |
| 432 | break; |
| 433 | case PORT_REMOVED: |
| 434 | // Nothing to do? |
| 435 | break; |
| 436 | default: |
| 437 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // Signals host vanish for all specified hosts. |
| 443 | private void removeHosts(Set<Host> hosts) { |
| 444 | for (Host host : hosts) { |
Charles Chan | 8b0fdaa | 2016-02-23 18:35:27 -0800 | [diff] [blame] | 445 | if (host.providerId().equals(HostLocationProvider.this.id())) { |
| 446 | providerService.hostVanished(host.id()); |
| 447 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 451 | } |