Karthik Vegesna | 20ce41e | 2017-07-11 10:34:37 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.packetstats; |
| 17 | import com.codahale.metrics.Counter; |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.IPv4; |
| 25 | import org.onlab.packet.UDP; |
| 26 | import org.onosproject.cfg.ComponentConfigService; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.packet.InboundPacket; |
| 31 | import org.onosproject.net.packet.PacketContext; |
| 32 | import org.onosproject.net.packet.PacketProcessor; |
| 33 | import org.onosproject.net.packet.PacketService; |
| 34 | |
| 35 | import org.osgi.service.component.ComponentContext; |
| 36 | import org.slf4j.Logger; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | import org.onlab.metrics.MetricsComponent; |
| 39 | import org.onlab.metrics.MetricsFeature; |
| 40 | import org.onlab.metrics.MetricsService; |
| 41 | |
| 42 | /** |
| 43 | * Application for Packet Statistics. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | public class PacketStatistics { |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected PacketService packetService; |
| 50 | |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected FlowRuleService flowRuleService; |
| 54 | |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected ComponentConfigService cfgService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected MetricsService metricService; |
| 64 | |
| 65 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
| 68 | private ApplicationId appId; |
| 69 | |
| 70 | |
| 71 | private MetricsComponent packetStatisticsComponent; |
| 72 | private MetricsFeature arpFeature; |
| 73 | private Counter arpCounter; |
| 74 | private MetricsFeature dhcpFeature; |
| 75 | private Counter dhcpCounter; |
| 76 | private MetricsFeature lldpFeature; |
| 77 | private Counter lldpCounter; |
| 78 | private MetricsFeature vlanFeature; |
| 79 | private Counter vlanCounter; |
| 80 | private MetricsFeature tcpFeature; |
| 81 | private Counter tcpCounter; |
| 82 | private MetricsFeature icmpFeature; |
| 83 | private Counter icmpCounter; |
| 84 | private MetricsFeature igmpFeature; |
| 85 | private Counter igmpCounter; |
| 86 | private MetricsFeature pimFeature; |
| 87 | private Counter pimCounter; |
| 88 | private MetricsFeature bsnFeature; |
| 89 | private Counter bsnCounter; |
| 90 | private MetricsFeature rarpFeature; |
| 91 | private Counter rarpCounter; |
| 92 | private MetricsFeature mplsFeature; |
| 93 | private Counter mplsCounter; |
| 94 | private MetricsFeature unknownFeature; |
| 95 | private Counter unknownCounter; |
| 96 | |
| 97 | |
| 98 | |
| 99 | @Activate |
| 100 | public void activate(ComponentContext context) { |
| 101 | this.packetStatisticsComponent = |
| 102 | metricService.registerComponent("packetStatisticsComponent"); |
| 103 | this.arpFeature = |
| 104 | packetStatisticsComponent.registerFeature("arpFeature"); |
| 105 | this.dhcpFeature = |
| 106 | packetStatisticsComponent.registerFeature("dhcpFeature"); |
| 107 | this.lldpFeature = |
| 108 | packetStatisticsComponent.registerFeature("lldpFeature"); |
| 109 | this.vlanFeature = |
| 110 | packetStatisticsComponent.registerFeature("vlanFeature"); |
| 111 | this.tcpFeature = |
| 112 | packetStatisticsComponent.registerFeature("tcpFeature"); |
| 113 | this.icmpFeature = |
| 114 | packetStatisticsComponent.registerFeature("icmpFeature"); |
| 115 | this.igmpFeature = |
| 116 | packetStatisticsComponent.registerFeature("igmpFeature"); |
| 117 | this.pimFeature = |
| 118 | packetStatisticsComponent.registerFeature("pimFeature"); |
| 119 | this.bsnFeature = |
| 120 | packetStatisticsComponent.registerFeature("bsnFeature"); |
| 121 | this.rarpFeature = |
| 122 | packetStatisticsComponent.registerFeature("rarpFeature"); |
| 123 | this.mplsFeature = |
| 124 | packetStatisticsComponent.registerFeature("mplsFeature"); |
| 125 | this.unknownFeature = |
| 126 | packetStatisticsComponent.registerFeature("unknownFeature"); |
| 127 | this.arpCounter = |
| 128 | metricService.createCounter(packetStatisticsComponent, arpFeature, "ARP Packet Counter"); |
| 129 | this.dhcpCounter = |
| 130 | metricService.createCounter(packetStatisticsComponent, dhcpFeature, "DHCP Packet Counter"); |
| 131 | this.lldpCounter = |
| 132 | metricService.createCounter(packetStatisticsComponent, lldpFeature, "LLDP Packet Counter"); |
| 133 | this.vlanCounter = |
| 134 | metricService.createCounter(packetStatisticsComponent, vlanFeature, "VLAN Packet Counter"); |
| 135 | this.icmpCounter = |
| 136 | metricService.createCounter(packetStatisticsComponent, icmpFeature, "ICMP Packet Counter"); |
| 137 | this.igmpCounter = |
| 138 | metricService.createCounter(packetStatisticsComponent, igmpFeature, "IGMP Packet Counter"); |
| 139 | this.pimCounter = |
| 140 | metricService.createCounter(packetStatisticsComponent, pimFeature, "PIM Packet Counter"); |
| 141 | this.bsnCounter = |
| 142 | metricService.createCounter(packetStatisticsComponent, bsnFeature, "BSN Packet Counter"); |
| 143 | this.mplsCounter = |
| 144 | metricService.createCounter(packetStatisticsComponent, bsnFeature, "MPLS Packet Counter"); |
| 145 | this.unknownCounter = |
| 146 | metricService.createCounter(packetStatisticsComponent, bsnFeature, "Unknown Packet Counter"); |
| 147 | |
| 148 | |
| 149 | appId = coreService.registerApplication("org.onosproject.packet-stats"); |
| 150 | |
| 151 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 152 | log.info("Started", appId.id()); |
| 153 | |
| 154 | |
| 155 | } |
| 156 | |
| 157 | @Deactivate |
| 158 | public void deactivate() { |
| 159 | cfgService.unregisterProperties(getClass(), false); |
| 160 | flowRuleService.removeFlowRulesById(appId); |
| 161 | packetService.removeProcessor(processor); |
| 162 | processor = null; |
| 163 | log.info("Stopped"); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Packet processor responsible for forwarding packets along their paths. |
| 169 | */ |
| 170 | private class ReactivePacketProcessor implements PacketProcessor { |
| 171 | @Override |
| 172 | public void process(PacketContext context) { |
| 173 | InboundPacket pkt = context.inPacket(); |
| 174 | Ethernet ethPkt = pkt.parsed(); |
| 175 | |
| 176 | //Indicates whether this is an ARP Packet |
| 177 | if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) { |
| 178 | arpCounter.inc(); |
| 179 | |
| 180 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_LLDP) { |
| 181 | lldpCounter.inc(); |
| 182 | |
| 183 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_VLAN) { |
| 184 | vlanCounter.inc(); |
| 185 | |
| 186 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_BSN) { |
| 187 | bsnCounter.inc(); |
| 188 | |
| 189 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_RARP) { |
| 190 | rarpCounter.inc(); |
| 191 | |
| 192 | } else if (ethPkt.getEtherType() == Ethernet.MPLS_UNICAST |
| 193 | || ethPkt.getEtherType() == Ethernet.MPLS_MULTICAST) { |
| 194 | mplsCounter.inc(); |
| 195 | |
| 196 | } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV4) { |
| 197 | IPv4 ipv4Packet = (IPv4) ethPkt.getPayload(); |
| 198 | //Indicates whether this is a TCP Packet |
| 199 | if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_TCP) { |
| 200 | tcpCounter.inc(); |
| 201 | |
| 202 | } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) { |
| 203 | icmpCounter.inc(); |
| 204 | |
| 205 | } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_IGMP) { |
| 206 | igmpCounter.inc(); |
| 207 | |
| 208 | } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_PIM) { |
| 209 | pimCounter.inc(); |
| 210 | |
| 211 | } else if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { |
| 212 | UDP udpPacket = (UDP) ipv4Packet.getPayload(); |
| 213 | //Indicates whether this packet is a DHCP Packet |
| 214 | if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT |
| 215 | || udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) { |
| 216 | dhcpCounter.inc(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } else { |
| 221 | log.info("Packet is unknown."); |
| 222 | unknownCounter.inc(); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | } |