Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Dictionary; |
| 21 | import java.util.Set; |
| 22 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Modified; |
| 27 | import org.apache.felix.scr.annotations.Property; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.ARP; |
| 31 | import org.onlab.packet.Ethernet; |
| 32 | import org.onlab.packet.IPacket; |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 33 | import org.onlab.packet.ICMP6; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 34 | import org.onlab.packet.IPv6; |
| 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onlab.packet.VlanId; |
| 37 | import org.onlab.packet.ndp.NeighborAdvertisement; |
| 38 | import org.onlab.packet.ndp.NeighborSolicitation; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 39 | import org.onosproject.core.ApplicationId; |
| 40 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.ConnectPoint; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 42 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Host; |
| 44 | import org.onosproject.net.HostId; |
| 45 | import org.onosproject.net.HostLocation; |
| 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.host.DefaultHostDescription; |
| 52 | import org.onosproject.net.host.HostDescription; |
| 53 | import org.onosproject.net.host.HostProvider; |
| 54 | import org.onosproject.net.host.HostProviderRegistry; |
| 55 | import org.onosproject.net.host.HostProviderService; |
| 56 | import org.onosproject.net.host.HostService; |
| 57 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 58 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.packet.PacketProcessor; |
| 60 | import org.onosproject.net.packet.PacketService; |
| 61 | import org.onosproject.net.provider.AbstractProvider; |
| 62 | import org.onosproject.net.provider.ProviderId; |
| 63 | import org.onosproject.net.topology.Topology; |
| 64 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 65 | import org.osgi.service.component.ComponentContext; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 66 | import org.slf4j.Logger; |
| 67 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Provider which uses an OpenFlow controller to detect network |
| 70 | * end-station hosts. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 74 | |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected CoreService coreService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 81 | protected HostProviderRegistry providerRegistry; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 84 | protected PacketService packetService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected TopologyService topologyService; |
| 88 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected HostService hostService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected DeviceService deviceService; |
| 94 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 95 | private HostProviderService providerService; |
| 96 | |
| 97 | private final InternalHostProvider processor = new InternalHostProvider(); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 98 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 99 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 100 | private ApplicationId appId; |
| 101 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 102 | @Property(name = "hostRemovalEnabled", boolValue = true, |
| 103 | label = "Enable host removal on port/device down events") |
| 104 | private boolean hostRemovalEnabled = true; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | /** |
| 108 | * Creates an OpenFlow host provider. |
| 109 | */ |
| 110 | public HostLocationProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 111 | super(new ProviderId("of", "org.onosproject.provider.host")); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Activate |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 115 | public void activate(ComponentContext context) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 116 | appId = |
| 117 | coreService.registerApplication("org.onosproject.provider.host"); |
| 118 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 119 | modified(context); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 120 | providerService = providerRegistry.register(this); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 121 | packetService.addProcessor(processor, 1); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 122 | deviceService.addListener(deviceListener); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 123 | |
| 124 | TrafficSelector.Builder selectorBuilder = |
| 125 | DefaultTrafficSelector.builder(); |
| 126 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 127 | packetService.requestPackets(selectorBuilder.build(), |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 128 | PacketPriority.CONTROL, appId); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 129 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 130 | // IPv6 Neighbor Solicitation packet. |
| 131 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 132 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 133 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 134 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 135 | packetService.requestPackets(selectorBuilder.build(), |
| 136 | PacketPriority.CONTROL, appId); |
| 137 | |
| 138 | // IPv6 Neighbor Advertisement packet. |
| 139 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 140 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 141 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 142 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 143 | packetService.requestPackets(selectorBuilder.build(), |
| 144 | PacketPriority.CONTROL, appId); |
| 145 | |
| 146 | log.info("Started with Application ID {}", appId.id()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Deactivate |
| 150 | public void deactivate() { |
| 151 | providerRegistry.unregister(this); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 152 | packetService.removeProcessor(processor); |
alshabib | bfc6b72 | 2014-11-29 12:53:48 -0800 | [diff] [blame] | 153 | deviceService.removeListener(deviceListener); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 154 | providerService = null; |
| 155 | log.info("Stopped"); |
| 156 | } |
| 157 | |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 158 | @Modified |
| 159 | public void modified(ComponentContext context) { |
| 160 | Dictionary properties = context.getProperties(); |
| 161 | try { |
| 162 | String flag = (String) properties.get("hostRemovalEnabled"); |
| 163 | if (flag != null) { |
| 164 | hostRemovalEnabled = flag.equals("true"); |
| 165 | } |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 166 | } catch (ClassCastException e) { |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 167 | hostRemovalEnabled = true; |
| 168 | } |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 169 | log.info("Host removal is {}", |
| 170 | hostRemovalEnabled ? "enabled" : "disabled"); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 171 | } |
| 172 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 173 | @Override |
| 174 | public void triggerProbe(Host host) { |
| 175 | log.info("Triggering probe on device {}", host); |
| 176 | } |
| 177 | |
| 178 | private class InternalHostProvider implements PacketProcessor { |
| 179 | |
| 180 | @Override |
| 181 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 182 | if (context == null) { |
| 183 | return; |
| 184 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 185 | Ethernet eth = context.inPacket().parsed(); |
| 186 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 187 | if (eth == null) { |
| 188 | return; |
| 189 | } |
| 190 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 191 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 192 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 193 | |
| 194 | // If this is not an edge port, bail out. |
| 195 | Topology topology = topologyService.currentTopology(); |
| 196 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 197 | return; |
| 198 | } |
| 199 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 200 | HostLocation hloc = |
| 201 | new HostLocation(heardOn, System.currentTimeMillis()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 202 | |
| 203 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 204 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 205 | // ARP: possible new hosts, update both location and IP |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 206 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 207 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 208 | IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, |
| 209 | arp.getSenderProtocolAddress()); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 210 | HostDescription hdescr = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 211 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 212 | hloc, ip); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 213 | providerService.hostDetected(hid, hdescr); |
| 214 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 215 | // IPv4: update location only |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 216 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 217 | HostDescription hdescr = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 218 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 219 | providerService.hostDetected(hid, hdescr); |
| 220 | |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 221 | // |
| 222 | // NeighborAdvertisement and NeighborSolicitation: possible |
| 223 | // new hosts, update both location and IP. |
| 224 | // |
| 225 | // IPv6: update location only |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 226 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) { |
| 227 | IpAddress ip = null; |
| 228 | IPv6 ipv6 = (IPv6) eth.getPayload(); |
| 229 | |
| 230 | IPacket iPkt = ipv6; |
| 231 | while (iPkt != null) { |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 232 | if (iPkt instanceof NeighborAdvertisement || |
| 233 | iPkt instanceof NeighborSolicitation) { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 234 | IpAddress sourceAddress = |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 235 | IpAddress.valueOf(IpAddress.Version.INET6, |
| 236 | ipv6.getSourceAddress()); |
| 237 | // Ignore DAD packets, in which source address is zero |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 238 | if (!sourceAddress.isZero()) { |
| 239 | ip = sourceAddress; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | iPkt = iPkt.getPayload(); |
| 244 | } |
| 245 | HostDescription hdescr = (ip == null) ? |
Pavlin Radoslavov | d6612f9 | 2015-02-23 13:53:32 -0800 | [diff] [blame^] | 246 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 247 | hloc) : |
| 248 | new DefaultHostDescription(eth.getSourceMAC(), vlan, |
| 249 | hloc, ip); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 250 | providerService.hostDetected(hid, hdescr); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 254 | |
| 255 | // Auxiliary listener to device events. |
| 256 | private class InternalDeviceListener implements DeviceListener { |
| 257 | @Override |
| 258 | public void event(DeviceEvent event) { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 259 | Device device = event.subject(); |
| 260 | switch (event.type()) { |
| 261 | case DEVICE_ADDED: |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 262 | break; |
| 263 | case DEVICE_AVAILABILITY_CHANGED: |
| 264 | if (hostRemovalEnabled && |
| 265 | !deviceService.isAvailable(device.id())) { |
| 266 | removeHosts(hostService.getConnectedHosts(device.id())); |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 267 | } |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 268 | break; |
| 269 | case DEVICE_SUSPENDED: |
| 270 | case DEVICE_UPDATED: |
| 271 | // Nothing to do? |
| 272 | break; |
| 273 | case DEVICE_REMOVED: |
| 274 | if (hostRemovalEnabled) { |
| 275 | removeHosts(hostService.getConnectedHosts(device.id())); |
| 276 | } |
| 277 | break; |
| 278 | case PORT_ADDED: |
| 279 | break; |
| 280 | case PORT_UPDATED: |
| 281 | if (hostRemovalEnabled) { |
| 282 | ConnectPoint point = |
| 283 | new ConnectPoint(device.id(), event.port().number()); |
| 284 | removeHosts(hostService.getConnectedHosts(point)); |
| 285 | } |
| 286 | break; |
| 287 | case PORT_REMOVED: |
| 288 | // Nothing to do? |
| 289 | break; |
| 290 | default: |
| 291 | break; |
Thomas Vachuska | 3360160 | 2014-11-19 03:32:15 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Signals host vanish for all specified hosts. |
| 297 | private void removeHosts(Set<Host> hosts) { |
| 298 | for (Host host : hosts) { |
| 299 | providerService.hostVanished(host.id()); |
| 300 | } |
| 301 | } |
| 302 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 303 | } |