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.of.host.impl; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import static org.onosproject.net.DeviceId.deviceId; |
| 19 | import static org.onosproject.net.PortNumber.portNumber; |
Jonathan Hart | 7d1ad60 | 2014-10-17 11:48:32 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
| 31 | import org.onosproject.net.host.DefaultHostDescription; |
| 32 | import org.onosproject.net.host.HostDescription; |
| 33 | import org.onosproject.net.host.HostProvider; |
| 34 | import org.onosproject.net.host.HostProviderRegistry; |
| 35 | import org.onosproject.net.host.HostProviderService; |
| 36 | import org.onosproject.net.provider.AbstractProvider; |
| 37 | import org.onosproject.net.provider.ProviderId; |
| 38 | import org.onosproject.net.topology.Topology; |
| 39 | import org.onosproject.net.topology.TopologyService; |
| 40 | import org.onosproject.openflow.controller.Dpid; |
| 41 | import org.onosproject.openflow.controller.OpenFlowController; |
| 42 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 43 | import org.onosproject.openflow.controller.PacketListener; |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 44 | import org.onlab.packet.ARP; |
| 45 | import org.onlab.packet.Ethernet; |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 46 | import org.onlab.packet.IPv4; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 47 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 48 | import org.onlab.packet.VlanId; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 49 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 50 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 51 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 52 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 53 | * end-station hosts. |
| 54 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 55 | @Component(immediate = true) |
alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame] | 56 | @Deprecated |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 57 | public class OpenFlowHostProvider extends AbstractProvider implements HostProvider { |
| 58 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 59 | private final Logger log = getLogger(getClass()); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 62 | protected HostProviderRegistry providerRegistry; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 63 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected OpenFlowController controller; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 66 | |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected TopologyService topologyService; |
| 69 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 70 | private HostProviderService providerService; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 71 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 72 | private final InternalHostProvider listener = new InternalHostProvider(); |
| 73 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 74 | private boolean ipLearn = true; |
| 75 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Creates an OpenFlow host provider. |
| 78 | */ |
| 79 | public OpenFlowHostProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 80 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Activate |
| 84 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 85 | providerService = providerRegistry.register(this); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 86 | controller.addPacketListener(10, listener); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 87 | log.info("Started"); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 92 | providerRegistry.unregister(this); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 93 | controller.removePacketListener(listener); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 94 | providerService = null; |
| 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void triggerProbe(Host host) { |
| 100 | log.info("Triggering probe on device {}", host); |
| 101 | } |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 102 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 103 | private class InternalHostProvider implements PacketListener { |
| 104 | |
| 105 | @Override |
| 106 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
| 107 | Ethernet eth = pktCtx.parsed(); |
| 108 | |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 109 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 110 | ConnectPoint heardOn = new ConnectPoint(deviceId(Dpid.uri(pktCtx.dpid())), |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 111 | portNumber(pktCtx.inPort())); |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 112 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 113 | // If this is not an edge port, bail out. |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 114 | Topology topology = topologyService.currentTopology(); |
| 115 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | HostLocation hloc = new HostLocation(deviceId(Dpid.uri(pktCtx.dpid())), |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 120 | portNumber(pktCtx.inPort()), |
| 121 | System.currentTimeMillis()); |
| 122 | |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 123 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 124 | |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 125 | // Potentially a new or moved host |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 126 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 127 | ARP arp = (ARP) eth.getPayload(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 128 | IpAddress ip = |
Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame] | 129 | IpAddress.valueOf(IpAddress.Version.INET, |
| 130 | arp.getSenderProtocolAddress()); |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 131 | HostDescription hdescr = |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 132 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 133 | providerService.hostDetected(hid, hdescr); |
| 134 | |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 135 | } else if (ipLearn && eth.getEtherType() == Ethernet.TYPE_IPV4) { |
| 136 | IPv4 pip = (IPv4) eth.getPayload(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 137 | IpAddress ip = |
| 138 | IpAddress.valueOf(pip.getSourceAddress()); |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 139 | HostDescription hdescr = |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 140 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip); |
alshabib | 0ff17ad | 2014-09-30 09:46:40 -0700 | [diff] [blame] | 141 | providerService.hostDetected(hid, hdescr); |
| 142 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 143 | } |
tom | 025e09f | 2014-09-15 15:29:24 -0700 | [diff] [blame] | 144 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 148 | } |