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