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