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