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