From patchwork Thu Dec 21 16:00:15 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: 122580 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp1082674qgn; Thu, 21 Dec 2017 08:07:36 -0800 (PST) X-Google-Smtp-Source: ACJfBot7TLwRKp6YCv2zAm9tPmdCWYk6CFAFxlYJCwrKcEfFptd4KEV1XNsDaU8Cw9l98tDZc/7Y X-Received: by 10.55.122.134 with SMTP id v128mr15413324qkc.354.1513872456501; Thu, 21 Dec 2017 08:07:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513872456; cv=none; d=google.com; s=arc-20160816; b=J9w3tqEDLOJSiUKs4Xc7VSMxCD/jynmju4dUHaoWRwIODGVAEYz41pFMDg+6g+/Lpc CabF3y8gW91vfNqR5uZkMX5FfLN/lHLuHiVP4yYzHNayWASzvFwyhfWi97dPdQQRVCjL TGyO6p7bp+41JwhwZLEtEkTH8SfLFcwKz+40x8IAG2tHTsALaltXUR7GutkuLWhI7W8s cGtzKSxLn05d83f8hD+XSaGMX9XSl2cgN38cQYjcDB+F3MMzclClIKfoN0ZrD1I82crK +a6GPCQ1OqTcsjnBqgor+t1xbYBleDtdBqTfcg4Ll4sKnDQhb7x1zWYPSqUqNJdwka1J LB5Q== 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=zsuDn27e7z3pBhzTE7FLZUkbCXqIEmvF2caJLwh6lJc=; b=bLg2FXuAh0FMcjhvxndQR2rf9FTN2XcmeVi1hQmi0+O4hQPMzAGeCwfFvaxnmxfrHC eNTE/PTGGQPQRJ9D0sUh3jYSFbwtnrZncPyPWlostA9cZ+WYOiE48zCgCoej5lsZX5pf jbSA2RXDPe2SNu51nXKtsR7hVzEe5/MrLiuMo4Bal2aXNCqGHcrDcu1uyQc+nsa4FzMP XZ9SrEsffK3ueDhmvGKhh0o+pasjbL1aiBSvF6SvKcIvt5bp29Agf53kkfVEoYVKeJSo ZqZdSjxiJ/vtjpqkJ13zPoi5hR22YeEHUAjmbmgJRrELJK1u7+niNQXmpgzsT+0rn/rr PMjg== 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 z26si1592124qtg.436.2017.12.21.08.07.36; Thu, 21 Dec 2017 08:07:36 -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 3EE636173F; Thu, 21 Dec 2017 16:07:36 +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_H3,RCVD_IN_MSPIKE_WL,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 CBEB661519; Thu, 21 Dec 2017 16:01:41 +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 4245361514; Thu, 21 Dec 2017 16:01:17 +0000 (UTC) Received: from forward106j.mail.yandex.net (forward106j.mail.yandex.net [5.45.198.249]) by lists.linaro.org (Postfix) with ESMTPS id EE97260C5E for ; Thu, 21 Dec 2017 16:00:20 +0000 (UTC) Received: from mxback11j.mail.yandex.net (mxback11j.mail.yandex.net [IPv6:2a02:6b8:0:1619::84]) by forward106j.mail.yandex.net (Yandex) with ESMTP id 82BF31804BC6 for ; Thu, 21 Dec 2017 19:00:19 +0300 (MSK) Received: from smtp1j.mail.yandex.net (smtp1j.mail.yandex.net [2a02:6b8:0:801::ab]) by mxback11j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id JEwa42gPtg-0JOek7PX; Thu, 21 Dec 2017 19:00:19 +0300 Received: by smtp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id bLqbqs0pBY-0IP89Uf2; Thu, 21 Dec 2017 19:00:18 +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 19:00:15 +0300 Message-Id: <1513872016-2819-4-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1513872016-2819-1-git-send-email-odpbot@yandex.ru> References: <1513872016-2819-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 359 Subject: [lng-odp] [PATCH CATERPILLAR v1 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: 0e9885d54ac547d0bdf599d65b6fe32de445adb7 **/ 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_ */