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