123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- /****************************************************************************
- * net/procfs/net_statistics.c
- *
- * Copyright (C) 2015 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <gnutt@nuttx.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- * used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include <nuttx/config.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <string.h>
- #include <debug.h>
- #include <nuttx/net/netstats.h>
- #include "procfs/procfs.h"
- #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
- !defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && defined(CONFIG_NET_STATISTICS)
- #if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6) || \
- defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP) || \
- defined(CONFIG_NET_ICMP) || defined(CONFIG_NET_ICMPv6)
- /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
- /* Line generating functions */
- static int netprocfs_header(FAR struct netprocfs_file_s *netfile);
- static int netprocfs_received(FAR struct netprocfs_file_s *netfile);
- static int netprocfs_dropped(FAR struct netprocfs_file_s *netfile);
- #ifdef CONFIG_NET_IPv4
- static int netprocfs_ipv4_dropped(FAR struct netprocfs_file_s *netfile);
- #endif /* CONFIG_NET_IPv4 */
- #ifdef CONFIG_NET_IPv6
- static int netprocfs_ipv6_dropped(FAR struct netprocfs_file_s *netfile);
- #endif /* CONFIG_NET_IPv4 */
- static int netprocfs_checksum(FAR struct netprocfs_file_s *netfile);
- #ifdef CONFIG_NET_TCP
- static int netprocfs_tcp_dropped_1(FAR struct netprocfs_file_s *netfile);
- static int netprocfs_tcp_dropped_2(FAR struct netprocfs_file_s *netfile);
- #endif /* CONFIG_NET_TCP */
- static int netprocfs_prototype(FAR struct netprocfs_file_s *netfile);
- static int netprocfs_sent(FAR struct netprocfs_file_s *netfile);
- #ifdef CONFIG_NET_TCP
- static int netprocfs_retransmissions(FAR struct netprocfs_file_s *netfile);
- #endif /* CONFIG_NET_TCP */
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- /* Line generating functions */
- static const linegen_t g_stat_linegen[] =
- {
- netprocfs_header,
- netprocfs_received,
- netprocfs_dropped,
- #ifdef CONFIG_NET_IPv4
- netprocfs_ipv4_dropped,
- #endif /* CONFIG_NET_IPv4 */
- #ifdef CONFIG_NET_IPv6
- netprocfs_ipv6_dropped,
- #endif /* CONFIG_NET_IPv4 */
- netprocfs_checksum,
- #ifdef CONFIG_NET_TCP
- netprocfs_tcp_dropped_1,
- netprocfs_tcp_dropped_2,
- #endif /* CONFIG_NET_TCP */
- netprocfs_prototype,
- netprocfs_sent
- #ifdef CONFIG_NET_TCP
- , netprocfs_retransmissions
- #endif /* CONFIG_NET_TCP */
- };
- #define NSTAT_LINES (sizeof(g_stat_linegen) / sizeof(linegen_t))
- /****************************************************************************
- * Private Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: netprocfs_header
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_header(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " IPv4");
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " IPv6");
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " TCP");
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " UDP");
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ICMP");
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ICMPv6");
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_received
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_received(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "Received ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv4.recv);
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv6.recv);
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.tcp.recv);
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.udp.recv);
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmp.recv);
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmpv6.recv);
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_dropped
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_dropped(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "Dropped ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv4.drop);
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv6.drop);
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.tcp.drop);
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.udp.drop);
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmp.drop);
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmpv6.drop);
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_ipv4_dropped
- ****************************************************************************/
- #if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_IPv4)
- static int netprocfs_ipv4_dropped(FAR struct netprocfs_file_s *netfile)
- {
- return snprintf(netfile->line, NET_LINELEN,
- " IPv4 VHL: %04x Frg: %04x\n",
- g_netstats.ipv4.vhlerr, g_netstats.ipv4.fragerr);
- }
- #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_IPv4 */
- /****************************************************************************
- * Name: netprocfs_ipv6_dropped
- ****************************************************************************/
- #if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_IPv6)
- static int netprocfs_ipv6_dropped(FAR struct netprocfs_file_s *netfile)
- {
- return snprintf(netfile->line, NET_LINELEN,
- " IPv6 VHL: %04x\n",
- g_netstats.ipv6.vhlerr);
- }
- #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_IPv6 */
- /****************************************************************************
- * Name: netprocfs_checksum
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_checksum(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " Checksum ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv4.chkerr);
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.tcp.chkerr);
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.udp.chkerr);
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_tcp_dropped_1
- ****************************************************************************/
- #if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_TCP)
- static int netprocfs_tcp_dropped_1(FAR struct netprocfs_file_s *netfile)
- {
- return snprintf(netfile->line, NET_LINELEN,
- " TCP ACK: %04x SYN: %04x\n",
- g_netstats.tcp.ackerr, g_netstats.tcp.syndrop);
- }
- #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
- /****************************************************************************
- * Name: netprocfs_tcp_dropped_2
- ****************************************************************************/
- #if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_TCP)
- static int netprocfs_tcp_dropped_2(FAR struct netprocfs_file_s *netfile)
- {
- return snprintf(netfile->line, NET_LINELEN,
- " RST: %04x %04x\n",
- g_netstats.tcp.rst, g_netstats.tcp.synrst);
- }
- #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
- /****************************************************************************
- * Name: netprocfs_prototype
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_prototype(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " Type ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv4.protoerr);
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv6.protoerr);
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmp.typeerr);
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmpv6.typeerr);
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_sent
- ****************************************************************************/
- #ifdef CONFIG_NET_STATISTICS
- static int netprocfs_sent(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "Sent ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv4.sent);
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.ipv6.sent);
- #endif
- #ifdef CONFIG_NET_TCP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.tcp.sent);
- #endif
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.udp.sent);
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmp.sent);
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.icmpv6.sent);
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS */
- /****************************************************************************
- * Name: netprocfs_retransmissions
- ****************************************************************************/
- #if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_NET_TCP)
- static int netprocfs_retransmissions(FAR struct netprocfs_file_s *netfile)
- {
- int len = 0;
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " Rexmit ");
- #ifdef CONFIG_NET_IPv4
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_IPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x",
- g_netstats.tcp.rexmit);
- #ifdef CONFIG_NET_UDP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_ICMP
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- #ifdef CONFIG_NET_ICMPv6
- len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----");
- #endif
- len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n");
- return len;
- }
- #endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: netprocfs_read_netstats
- *
- * Description:
- * Read and format network layer statistics.
- *
- * Input Parameters:
- * priv - A reference to the network procfs file structure
- * buffer - The user-provided buffer into which network status will be
- * returned.
- * bulen - The size in bytes of the user provided buffer.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned
- * on failure.
- *
- ****************************************************************************/
- ssize_t netprocfs_read_netstats(FAR struct netprocfs_file_s *priv,
- FAR char *buffer, size_t buflen)
- {
- return netprocfs_read_linegen(priv, buffer, buflen, g_stat_linegen, NSTAT_LINES);
- }
- #else
- ssize_t netprocfs_read_netstats(FAR struct netprocfs_file_s *priv,
- FAR char *buffer, size_t buflen)
- {
- return OK;
- }
- #endif /* CONFIG_NET_IPv4 || CONFIG_NET_IPv6 || CONFIG_NET_TCP || \
- * CONFIG_NET_UDP || CONFIG_NET_ICMP || CONFIG_NET_ICMPv6 */
- #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
- * !CONFIG_FS_PROCFS_EXCLUDE_NET */
|