From patchwork Thu Dec 21 17:00:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 122588 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp1153699qgn; Thu, 21 Dec 2017 09:05:59 -0800 (PST) X-Google-Smtp-Source: ACJfBot0PVsIRgjfSL/n1Llo3ChlqrIfpwnbwRd9YcRc7Yc7nSaumWtZ0WhJbxeUGvbtzXpmjaje X-Received: by 10.55.126.7 with SMTP id z7mr14988230qkc.306.1513875959314; Thu, 21 Dec 2017 09:05:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513875959; cv=none; d=google.com; s=arc-20160816; b=FHxSjXri/n4ePbdCeIPLEECUbT7vke1jpjmMWFqSuaDOrubUws8aoNmnJuxvmnW4ld KpctDiKQh9oZHLlenCdg/mT011pIk0W6Q+e0y5NqSXBBE3qYhfpT5Fi+JporkYU4grG5 e8jHlJAl6EhuyI/PYVovAHAfznv7+Qf0tqpsi5beeoxhww2jjpDsRPxLp3pktmsDGIq+ flggqjy5Bccxcn4TxsN4pgudsshBQFSKU7yQorHPgSz6fPDaWo3iyAlYA1se6Cl37g97 gSoIhEhG264yMa5OuyO5vkc+Lk7N2u7bfYE444NnTx2WLSJ+h2IX3UKu/oDXGevsz4iW zLng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=p3j57mwDMTgBqS71DCMat9+00vyKELQKkTNBrXfVvHo=; b=k9aWtwCVVhZ1BKLDn8afx2/N7DMLLcvOH462R8uvenrF4u2WcFvvTG+fdb5N6lP0vB tHjqhuUkDGWzdoBdSe/YeId17lEMT85MFJha8A0zE0S5STdQUcJYT3ANcw3nlKulKboM MUieJWzAPK06pHJRLQX6hurOBnfyeuuFfCMeuGCJIlqLb3xSQpfEUnU+AEBJka7aDSuW ZEJcLPTlNWKlqnRL8aeZXctPSrxi90jQrEAUL/N5GhlHKX4lGjH0qRKKJwiJ3qsuIRmh ggoGR4VLVpDPyiZEPh8Z+DGN7yLUsPpcFff3232S8iuwAjVQsXzlUHAIQmQ3EGYsSFA5 7EUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (ec2-54-197-127-237.compute-1.amazonaws.com. [54.197.127.237]) by mx.google.com with ESMTP id a185si616517qke.290.2017.12.21.09.05.59; Thu, 21 Dec 2017 09:05:59 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) client-ip=54.197.127.237; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id E78CF6152C; Thu, 21 Dec 2017 17:05:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id CD9F06150B; Thu, 21 Dec 2017 17:02:17 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id B58226097F; Thu, 21 Dec 2017 17:01:58 +0000 (UTC) Received: from forward101p.mail.yandex.net (forward101p.mail.yandex.net [77.88.28.101]) by lists.linaro.org (Postfix) with ESMTPS id 33C5260A6E for ; Thu, 21 Dec 2017 17:00:36 +0000 (UTC) Received: from mxback8o.mail.yandex.net (mxback8o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::22]) by forward101p.mail.yandex.net (Yandex) with ESMTP id C72F16A8356B for ; Thu, 21 Dec 2017 20:00:34 +0300 (MSK) Received: from smtp3o.mail.yandex.net (smtp3o.mail.yandex.net [2a02:6b8:0:1a2d::27]) by mxback8o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id aRdR4En7L7-0Yaqql9Q; Thu, 21 Dec 2017 20:00:34 +0300 Received: by smtp3o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id x1tp8yxdAP-0Y7aYdLj; Thu, 21 Dec 2017 20:00:34 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Thu, 21 Dec 2017 20:00:24 +0300 Message-Id: <1513875625-25260-4-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1513875625-25260-1-git-send-email-odpbot@yandex.ru> References: <1513875625-25260-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 359 Subject: [lng-odp] [PATCH CATERPILLAR v2 3/4] linux-gen: extend sysfs parsing helper functions X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Mykyta Iziumtsev Added char* and uint64_t sysfs attribute read functions to support upcoming native and mediated device drivers. Updated sysfs_netif_stats() to take interface name as argument instead of pktio_entry, because pktio_entry->s.name is not necessarily recognisable Linux network interface name. Signed-off-by: Mykyta Iziumtsev --- /** Email created from pull request 359 (MykytaI:caterpillar_mdev_auxiliary) ** https://github.com/Linaro/odp/pull/359 ** Patch: https://github.com/Linaro/odp/pull/359.patch ** Base sha: 63fd88635cc10caaa02fdccd3f52c9494487bdd2 ** Merge commit sha: d90db23824f481750cb9bfe1de16df25e2b1767a **/ platform/linux-generic/pktio/common.c | 4 +- platform/linux-generic/pktio/socket.c | 2 +- platform/linux-generic/pktio/socket_mmap.c | 2 +- platform/linux-generic/pktio/sysfs.c | 161 ++++++++++++++++++++++------- platform/linux-generic/pktio/sysfs.h | 33 +++++- 5 files changed, 154 insertions(+), 48 deletions(-) diff --git a/platform/linux-generic/pktio/common.c b/platform/linux-generic/pktio/common.c index 900dcbba2..e6eb09900 100644 --- a/platform/linux-generic/pktio/common.c +++ b/platform/linux-generic/pktio/common.c @@ -45,7 +45,7 @@ int sock_stats_reset_fd(pktio_entry_t *pktio_entry, int fd) pktio_entry->s.name, &cur_stats); } else if (pktio_entry->s.stats_type == STATS_SYSFS) { - err = sysfs_stats(pktio_entry, &cur_stats); + err = sysfs_netif_stats(pktio_entry->s.name, &cur_stats); if (err != 0) ODP_ERR("stats error\n"); } @@ -78,7 +78,7 @@ int sock_stats_fd(pktio_entry_t *pktio_entry, pktio_entry->s.name, &cur_stats); } else if (pktio_entry->s.stats_type == STATS_SYSFS) { - sysfs_stats(pktio_entry, &cur_stats); + sysfs_netif_stats(pktio_entry->s.name, &cur_stats); } stats->in_octets = cur_stats.in_octets - diff --git a/platform/linux-generic/pktio/socket.c b/platform/linux-generic/pktio/socket.c index 7e0485b99..320de973c 100644 --- a/platform/linux-generic/pktio/socket.c +++ b/platform/linux-generic/pktio/socket.c @@ -204,7 +204,7 @@ static int sock_setup_pkt(pktio_entry_t *pktio_entry, const char *netdev, pktio_entry->s.name, &cur_stats); if (err != 0) { - err = sysfs_stats(pktio_entry, &cur_stats); + err = sysfs_netif_stats(pktio_entry->s.name, &cur_stats); if (err != 0) { pktio_entry->s.stats_type = STATS_UNSUPPORTED; ODP_DBG("pktio: %s unsupported stats\n", diff --git a/platform/linux-generic/pktio/socket_mmap.c b/platform/linux-generic/pktio/socket_mmap.c index 271c2f0f7..26d0a2957 100644 --- a/platform/linux-generic/pktio/socket_mmap.c +++ b/platform/linux-generic/pktio/socket_mmap.c @@ -593,7 +593,7 @@ static int sock_mmap_open(odp_pktio_t id ODP_UNUSED, pktio_entry->s.name, &cur_stats); if (ret != 0) { - ret = sysfs_stats(pktio_entry, &cur_stats); + ret = sysfs_netif_stats(pktio_entry->s.name, &cur_stats); if (ret != 0) { pktio_entry->s.stats_type = STATS_UNSUPPORTED; ODP_DBG("pktio: %s unsupported stats\n", diff --git a/platform/linux-generic/pktio/sysfs.c b/platform/linux-generic/pktio/sysfs.c index f674ac42c..ae66296fb 100644 --- a/platform/linux-generic/pktio/sysfs.c +++ b/platform/linux-generic/pktio/sysfs.c @@ -7,74 +7,157 @@ #include "config.h" #include -#include +#include #include + #include #include #include +#include +#include +#include -static int sysfs_get_val(const char *fname, uint64_t *val) +static int _sysfs_attr_raw_get(char *buf, size_t buf_size, const char *fmt, + va_list args) { - FILE *file; - char str[128]; - int ret = -1; + char path[256]; + FILE *file; + int ret; - file = fopen(fname, "rt"); - if (file == NULL) { + ret = vsnprintf(path, sizeof(path), fmt, args); + + if (ret < 0) { __odp_errno = errno; - /* do not print debug err if sysfs is not supported by - * kernel driver. - */ - if (errno != ENOENT) - ODP_ERR("fopen %s: %s\n", fname, strerror(errno)); - return 0; + return -1; + } + + if (ret >= (ssize_t)sizeof(path)) { + __odp_errno = EINVAL; + return -1; } - if (fgets(str, sizeof(str), file) != NULL) - ret = sscanf(str, "%" SCNx64, val); + file = fopen(path, "rt"); + if (file == NULL) { + __odp_errno = errno; + return -1; + } + buf = fgets(buf, buf_size, file); (void)fclose(file); - if (ret != 1) { - ODP_ERR("read %s\n", fname); + if (buf == NULL) { + __odp_errno = errno; return -1; } return 0; } -int sysfs_stats(pktio_entry_t *pktio_entry, - odp_pktio_stats_t *stats) +ODP_PRINTF_FORMAT(3, 4) +int sysfs_attr_raw_get(char *buf, size_t buf_size, const char *fmt, ...) { - char fname[256]; - const char *dev = pktio_entry->s.name; - int ret = 0; + va_list args; + int ret; - sprintf(fname, "/sys/class/net/%s/statistics/rx_bytes", dev); - ret -= sysfs_get_val(fname, &stats->in_octets); + va_start(args, fmt); + ret = _sysfs_attr_raw_get(buf, buf_size, fmt, args); + va_end(args); - sprintf(fname, "/sys/class/net/%s/statistics/rx_packets", dev); - ret -= sysfs_get_val(fname, &stats->in_ucast_pkts); + return ret; +} + +ODP_PRINTF_FORMAT(2, 3) +int sysfs_attr_u64_get(uint64_t *value, const char *fmt, ...) +{ + char buf[20 + 1 + 1]; /* 20 digits (UINT64_MAX) + '\n' + '\0' */ + va_list args; + char *endptr; + int ret; - sprintf(fname, "/sys/class/net/%s/statistics/rx_droppped", dev); - ret -= sysfs_get_val(fname, &stats->in_discards); + va_start(args, fmt); + ret = _sysfs_attr_raw_get(buf, sizeof(buf), fmt, args); + va_end(args); - sprintf(fname, "/sys/class/net/%s/statistics/rx_errors", dev); - ret -= sysfs_get_val(fname, &stats->in_errors); + if (ret < 0) + return -1; + + if (buf[0] == '\0') { + __odp_errno = EINVAL; + return -1; + } + + *value = strtoull(buf, &endptr, 0); + + /* It is OK to have '\n' in sysfs */ + if (*endptr == '\n') + endptr++; + + if (*endptr != '\0') { + __odp_errno = EINVAL; + return -1; + } + + return 0; +} + +int sysfs_netif_stats(const char *netif_name, odp_pktio_stats_t *stats) +{ + int ret; + + /* + * Do not print debug err if sysfs is not supported by + * kernel driver. + */ + + ret = sysfs_attr_u64_get(&stats->in_octets, + "/sys/class/net/%s/statistics/rx_bytes", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; + + ret = sysfs_attr_u64_get(&stats->in_ucast_pkts, + "/sys/class/net/%s/statistics/rx_packets", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; + + ret = sysfs_attr_u64_get(&stats->in_discards, + "/sys/class/net/%s/statistics/rx_droppped", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; + + ret = sysfs_attr_u64_get(&stats->in_errors, + "/sys/class/net/%s/statistics/rx_errors", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; /* stats->in_unknown_protos is not supported in sysfs */ - sprintf(fname, "/sys/class/net/%s/statistics/tx_bytes", dev); - ret -= sysfs_get_val(fname, &stats->out_octets); + ret = sysfs_attr_u64_get(&stats->out_octets, + "/sys/class/net/%s/statistics/tx_bytes", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; - sprintf(fname, "/sys/class/net/%s/statistics/tx_packets", dev); - ret -= sysfs_get_val(fname, &stats->out_ucast_pkts); + ret = sysfs_attr_u64_get(&stats->out_ucast_pkts, + "/sys/class/net/%s/statistics/tx_packets", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; - sprintf(fname, "/sys/class/net/%s/statistics/tx_dropped", dev); - ret -= sysfs_get_val(fname, &stats->out_discards); + ret = sysfs_attr_u64_get(&stats->out_discards, + "/sys/class/net/%s/statistics/tx_dropped", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; - sprintf(fname, "/sys/class/net/%s/statistics/tx_errors", dev); - ret -= sysfs_get_val(fname, &stats->out_errors); + ret = sysfs_attr_u64_get(&stats->out_errors, + "/sys/class/net/%s/statistics/tx_errors", + netif_name); + if (ret < 0 && __odp_errno != ENOENT) + return -1; - return ret; + return 0; } diff --git a/platform/linux-generic/pktio/sysfs.h b/platform/linux-generic/pktio/sysfs.h index f8fe3072a..71ac4726f 100644 --- a/platform/linux-generic/pktio/sysfs.h +++ b/platform/linux-generic/pktio/sysfs.h @@ -8,16 +8,39 @@ #define ODP_PKTIO_SYSFS_H_ /** - * Get statistics for a pktio entry + * Read an attribute from /sysfs as raw string * - * @param pktio_entry Packet IO entry - * @param stats[out] Output buffer for counters + * @param buf[out] Attribute value + * @param buf_size Maximum number of bytes to read (including '\0') + * @param fmt printf-like path to the attribute + * + * @retval 0 on success, buf[] is guaranteed to be '\0'-terminated + * @retval != 0 on failure + */ +ODP_PRINTF_FORMAT(3, 4) +int sysfs_attr_raw_get(char *buf, size_t buf_size, const char *fmt, ...); + +/** + * Read an attribute from /sysfs as uint64_t + * + * @param value[out] Attribute value + * @param fmt printf-like path to the attribute * * @retval 0 on success * @retval != 0 on failure */ +ODP_PRINTF_FORMAT(2, 3) +int sysfs_attr_u64_get(uint64_t *value, const char *fmt, ...); -int sysfs_stats(pktio_entry_t *pktio_entry, - odp_pktio_stats_t *stats); +/** + * Get statistics for a network interface + * + * @param netif_name Network interface name + * @param stats[out] Output buffer for counters + * + * @retval 0 on success + * @retval != 0 on failure + */ +int sysfs_netif_stats(const char *netif_name, odp_pktio_stats_t *stats); #endif /* ODP_PKTIO_SYSFS_H_ */