alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.host.impl; |
| 2 | |
Jonathan Hart | 32cc1c0 | 2014-10-18 01:38:11 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 5 | import org.apache.felix.scr.annotations.Activate; |
| 6 | import org.apache.felix.scr.annotations.Component; |
| 7 | import org.apache.felix.scr.annotations.Deactivate; |
| 8 | import org.apache.felix.scr.annotations.Reference; |
| 9 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 10 | import org.onlab.onos.net.ConnectPoint; |
| 11 | import org.onlab.onos.net.Host; |
| 12 | import org.onlab.onos.net.HostId; |
| 13 | import org.onlab.onos.net.HostLocation; |
| 14 | import org.onlab.onos.net.host.DefaultHostDescription; |
| 15 | import org.onlab.onos.net.host.HostDescription; |
| 16 | import org.onlab.onos.net.host.HostProvider; |
| 17 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 18 | import org.onlab.onos.net.host.HostProviderService; |
| 19 | import org.onlab.onos.net.packet.PacketContext; |
| 20 | import org.onlab.onos.net.packet.PacketProcessor; |
| 21 | import org.onlab.onos.net.packet.PacketService; |
| 22 | import org.onlab.onos.net.provider.AbstractProvider; |
| 23 | import org.onlab.onos.net.provider.ProviderId; |
| 24 | import org.onlab.onos.net.topology.Topology; |
| 25 | import org.onlab.onos.net.topology.TopologyService; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 26 | import org.onlab.packet.ARP; |
| 27 | import org.onlab.packet.Ethernet; |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.slf4j.Logger; |
| 31 | |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Provider which uses an OpenFlow controller to detect network |
| 34 | * end-station hosts. |
| 35 | */ |
| 36 | @Component(immediate = true) |
| 37 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected HostProviderRegistry providerRegistry; |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected PacketService pktService; |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected TopologyService topologyService; |
| 49 | |
| 50 | private HostProviderService providerService; |
| 51 | |
| 52 | private final InternalHostProvider processor = new InternalHostProvider(); |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Creates an OpenFlow host provider. |
| 57 | */ |
| 58 | public HostLocationProvider() { |
| 59 | super(new ProviderId("of", "org.onlab.onos.provider.host")); |
| 60 | } |
| 61 | |
| 62 | @Activate |
| 63 | public void activate() { |
| 64 | providerService = providerRegistry.register(this); |
| 65 | pktService.addProcessor(processor, 1); |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | providerRegistry.unregister(this); |
| 72 | pktService.removeProcessor(processor); |
| 73 | providerService = null; |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void triggerProbe(Host host) { |
| 79 | log.info("Triggering probe on device {}", host); |
| 80 | } |
| 81 | |
| 82 | private class InternalHostProvider implements PacketProcessor { |
| 83 | |
| 84 | @Override |
| 85 | public void process(PacketContext context) { |
alshabib | 4a179dc | 2014-10-17 17:17:01 -0700 | [diff] [blame] | 86 | if (context == null) { |
| 87 | return; |
| 88 | } |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 89 | Ethernet eth = context.inPacket().parsed(); |
| 90 | |
| 91 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 92 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 93 | |
| 94 | // If this is not an edge port, bail out. |
| 95 | Topology topology = topologyService.currentTopology(); |
| 96 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
| 101 | |
| 102 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 103 | |
| 104 | // Potentially a new or moved host |
| 105 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 106 | ARP arp = (ARP) eth.getPayload(); |
Jonathan Hart | 32cc1c0 | 2014-10-18 01:38:11 -0700 | [diff] [blame] | 107 | IpPrefix ip = IpPrefix.valueOf(arp.getSenderProtocolAddress(), |
| 108 | IpPrefix.MAX_INET_MASK); |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 109 | HostDescription hdescr = |
| 110 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip); |
| 111 | providerService.hostDetected(hid, hdescr); |
| 112 | |
| 113 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
| 114 | //Do not learn new ip from ip packet. |
| 115 | HostDescription hdescr = |
| 116 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc); |
| 117 | providerService.hostDetected(hid, hdescr); |
| 118 | |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |