From patchwork Mon Nov 9 11:21:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 56211 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp121683lbb; Mon, 9 Nov 2015 03:24:25 -0800 (PST) X-Received: by 10.140.17.105 with SMTP id 96mr29908528qgc.34.1447068265628; Mon, 09 Nov 2015 03:24:25 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id f23si10645672qhc.77.2015.11.09.03.24.25; Mon, 09 Nov 2015 03:24:25 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Received: by lists.linaro.org (Postfix, from userid 109) id 3DC0261B3D; Mon, 9 Nov 2015 11:24:25 +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.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID, 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 4350861915; Mon, 9 Nov 2015 11:23:54 +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 D7AD4618C9; Mon, 9 Nov 2015 11:23:44 +0000 (UTC) Received: from mail-lb0-f179.google.com (mail-lb0-f179.google.com [209.85.217.179]) by lists.linaro.org (Postfix) with ESMTPS id A2F0B61807 for ; Mon, 9 Nov 2015 11:22:01 +0000 (UTC) Received: by lbbwb3 with SMTP id wb3so96224878lbb.1 for ; Mon, 09 Nov 2015 03:22:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QOgcN8EoWzleNqRXx2xSTuyvNcjQEfNbWR5QioNscNQ=; b=JJ/5Zj1GQUkmhZw5h7jmUhtunlD6+G3wlRLHhyYTOznuZZhbP0y2DDNktdlAM5L6cj Wf4fWkz2HxyidpARxnKlq2hOAeUv6iVh5jR78ZkmDNzJOoGEpt/dA7w/ARGDLZhKAGVk Z0LyLoqcCEIFOeTHZ4BNc0JmR0Q4t7CW6LEWA1DIBkidWjshoCkexSPoeZLGfe/WgCrF 6AUsWsGND/dbJI/FsrCRBPoDpjiNgqSPnXesEU0dc0FMQ6+2V/Um5bvOYwU55ze+ZYzh p24s8yy5zvn6uC0P8PskJdru6f4YXmXIstwGj7bY99fGTOM/xAnuHlbhGQBdt+Bz+p/1 6dkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QOgcN8EoWzleNqRXx2xSTuyvNcjQEfNbWR5QioNscNQ=; b=cTfNvxbes/MK2PWwfowpSKmbD1Hn4BX+zd4cUYr+ipcXVaxhHNfAlzq7+uHLCz5Yut UcPvhUFE4/v2690BezD0ywMR1WVC1q884Vqiwu4rT0ohSHwny85j2h0hCBvYqOTF6/AI lMrgJkM85d++2ouaWdJmQq4ehrhViSW5GjxmP/62dfeSeV0Vu3AfWBiAhNOQhD0WZFtw fVAxKND2IkyMFcYzax1Iz5e2GyM010o0ZEYtVMSRO49zAdRk3zG5cO+fmn60CZlU24JT IkHVg3eIBA8ptcEk5Xz3C2XWElaB9l6JnPGf2+6UlEMcDSiY84IgRIqoHLvX3N6EYf8F IFww== X-Gm-Message-State: ALoCoQm0hZrLGl1kfcAw39hF6rYPcMB7ONheOwwSaHBU/mJxGN1BExW4sa64Q3SMqdFPx/hLX241 X-Received: by 10.112.173.38 with SMTP id bh6mr6140571lbc.56.1447068120476; Mon, 09 Nov 2015 03:22:00 -0800 (PST) Received: from localhost.localdomain (ppp91-76-161-180.pppoe.mtu-net.ru. [91.76.161.180]) by smtp.gmail.com with ESMTPSA id 200sm2299784lfz.48.2015.11.09.03.21.59 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 09 Nov 2015 03:21:59 -0800 (PST) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Mon, 9 Nov 2015 14:21:49 +0300 Message-Id: <1447068113-3733-2-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1447068113-3733-1-git-send-email-maxim.uvarov@linaro.org> References: <1447068113-3733-1-git-send-email-maxim.uvarov@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCH 1/5] linux-generic: sockets: implement pktio statistics counters 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: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Signed-off-by: Maxim Uvarov --- platform/linux-generic/Makefile.am | 1 + .../linux-generic/include/odp_packet_io_internal.h | 9 +++ platform/linux-generic/odp_packet_io.c | 53 +++++++++++++++++ platform/linux-generic/pktio/socket.c | 59 ++++++++++++++++++ platform/linux-generic/pktio/socket_mmap.c | 8 +++ platform/linux-generic/pktio/stats_sysfs.c | 69 ++++++++++++++++++++++ 6 files changed, 199 insertions(+) create mode 100644 platform/linux-generic/pktio/stats_sysfs.c diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 610c79c..e3e4954 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -170,6 +170,7 @@ __LIB__libodp_la_SOURCES = \ pktio/netmap.c \ pktio/socket.c \ pktio/socket_mmap.c \ + pktio/stats_sysfs.c \ odp_pool.c \ odp_queue.c \ odp_rwlock.c \ diff --git a/platform/linux-generic/include/odp_packet_io_internal.h b/platform/linux-generic/include/odp_packet_io_internal.h index 1a1118c..ec43e87 100644 --- a/platform/linux-generic/include/odp_packet_io_internal.h +++ b/platform/linux-generic/include/odp_packet_io_internal.h @@ -84,6 +84,7 @@ struct pktio_entry { STATE_STOP } state; classifier_t cls; /**< classifier linked with this pktio*/ + odp_pktio_stats_t stats; /**< statistic counters for pktio */ char name[PKTIO_NAME_LEN]; /**< name of pktio provided to pktio_open() */ odp_pktio_param_t param; @@ -107,6 +108,8 @@ typedef struct pktio_if_ops { int (*close)(pktio_entry_t *pktio_entry); int (*start)(pktio_entry_t *pktio_entry); int (*stop)(pktio_entry_t *pktio_entry); + int (*stats)(pktio_entry_t *pktio_entry, odp_pktio_stats_t *stats); + int (*stats_reset)(pktio_entry_t *pktio_entry); int (*recv)(pktio_entry_t *pktio_entry, odp_packet_t pkt_table[], unsigned len); int (*send)(pktio_entry_t *pktio_entry, odp_packet_t pkt_table[], @@ -159,6 +162,12 @@ extern const pktio_if_ops_t pcap_pktio_ops; #endif extern const pktio_if_ops_t * const pktio_if_ops[]; +int sysfs_stats(pktio_entry_t *pktio_entry, + odp_pktio_stats_t *stats); +int sock_stats_reset(pktio_entry_t *pktio_entry); +int sock_stats(pktio_entry_t *pktio_entry, + odp_pktio_stats_t *stats); + #ifdef __cplusplus } #endif diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c index 3ef400f..ba97629 100644 --- a/platform/linux-generic/odp_packet_io.c +++ b/platform/linux-generic/odp_packet_io.c @@ -862,3 +862,56 @@ void odp_pktio_print(odp_pktio_t id) ODP_PRINT("\n%s\n", str); } + +int odp_pktio_stats(odp_pktio_t pktio, + odp_pktio_stats_t *stats) +{ + pktio_entry_t *entry; + int ret = -1; + + entry = get_pktio_entry(pktio); + if (entry == NULL) { + ODP_DBG("pktio entry %d does not exist\n", pktio); + return -1; + } + + lock_entry(entry); + + if (odp_unlikely(is_free(entry))) { + unlock_entry(entry); + ODP_DBG("already freed pktio\n"); + return -1; + } + + if (entry->s.ops->stats) + ret = entry->s.ops->stats(entry, stats); + unlock_entry(entry); + + return ret; +} + +int odp_pktio_stats_reset(odp_pktio_t pktio) +{ + pktio_entry_t *entry; + int ret = -1; + + entry = get_pktio_entry(pktio); + if (entry == NULL) { + ODP_DBG("pktio entry %d does not exist\n", pktio); + return -1; + } + + lock_entry(entry); + + if (odp_unlikely(is_free(entry))) { + unlock_entry(entry); + ODP_DBG("already freed pktio\n"); + return -1; + } + + if (entry->s.ops->stats) + ret = entry->s.ops->stats_reset(entry); + unlock_entry(entry); + + return ret; +} diff --git a/platform/linux-generic/pktio/socket.c b/platform/linux-generic/pktio/socket.c index 5f5e0ae..0bda57b 100644 --- a/platform/linux-generic/pktio/socket.c +++ b/platform/linux-generic/pktio/socket.c @@ -254,6 +254,12 @@ static int sock_setup_pkt(pktio_entry_t *pktio_entry, const char *netdev, goto error; } + err = sock_stats_reset(pktio_entry); + if (err != 0) { + ODP_ERR("reset stats\n"); + goto error; + } + return 0; error: @@ -467,6 +473,57 @@ static int sock_promisc_mode_get(pktio_entry_t *pktio_entry) pktio_entry->s.name); } +int sock_stats(pktio_entry_t *pktio_entry, + odp_pktio_stats_t *stats) +{ + odp_pktio_stats_t cur_stats; + int ret; + + memset(&cur_stats, 0, sizeof(odp_pktio_stats_t)); + ret = sysfs_stats(pktio_entry, &cur_stats); + if (ret) + ODP_ABORT("sysfs stats error\n"); + + stats->in_octets = cur_stats.in_octets - + pktio_entry->s.stats.in_octets; + stats->in_ucast_pkts = cur_stats.in_ucast_pkts - + pktio_entry->s.stats.in_ucast_pkts; + stats->in_discards = cur_stats.in_discards - + pktio_entry->s.stats.in_discards; + stats->in_errors = cur_stats.in_errors - + pktio_entry->s.stats.in_errors; + stats->in_unknown_protos = cur_stats.in_unknown_protos - + pktio_entry->s.stats.in_unknown_protos; + + stats->out_octets = cur_stats.out_octets - + pktio_entry->s.stats.out_octets; + stats->out_ucast_pkts = cur_stats.out_ucast_pkts - + pktio_entry->s.stats.out_ucast_pkts; + stats->out_discards = cur_stats.out_discards - + pktio_entry->s.stats.out_discards; + stats->out_errors = cur_stats.out_errors - + pktio_entry->s.stats.out_errors; + + return 0; +} + +int sock_stats_reset(pktio_entry_t *pktio_entry) +{ + int err = 0; + odp_pktio_stats_t cur; + + memset(&cur, 0, sizeof(odp_pktio_stats_t)); + err = sysfs_stats(pktio_entry, &cur); + if (err != 0) { + __odp_errno = errno; + ODP_ERR("sysfs stats error\n"); + } else { + memcpy(&pktio_entry->s.stats, &cur, sizeof(odp_pktio_stats_t)); + } + + return err; +} + const pktio_if_ops_t sock_mmsg_pktio_ops = { .init = NULL, .term = NULL, @@ -474,6 +531,8 @@ const pktio_if_ops_t sock_mmsg_pktio_ops = { .close = sock_close, .start = NULL, .stop = NULL, + .stats = sock_stats, + .stats_reset = sock_stats_reset, .recv = sock_mmsg_recv, .send = sock_mmsg_send, .mtu_get = sock_mtu_get, diff --git a/platform/linux-generic/pktio/socket_mmap.c b/platform/linux-generic/pktio/socket_mmap.c index 79ff82d..2031485 100644 --- a/platform/linux-generic/pktio/socket_mmap.c +++ b/platform/linux-generic/pktio/socket_mmap.c @@ -467,6 +467,12 @@ static int sock_mmap_open(odp_pktio_t id ODP_UNUSED, goto error; } + ret = sock_stats_reset(pktio_entry); + if (ret != 0) { + ODP_ERR("reset stats\n"); + goto error; + } + return 0; error: @@ -525,6 +531,8 @@ const pktio_if_ops_t sock_mmap_pktio_ops = { .close = sock_mmap_close, .start = NULL, .stop = NULL, + .stats = sock_stats, + .stats_reset = sock_stats_reset, .recv = sock_mmap_recv, .send = sock_mmap_send, .mtu_get = sock_mmap_mtu_get, diff --git a/platform/linux-generic/pktio/stats_sysfs.c b/platform/linux-generic/pktio/stats_sysfs.c new file mode 100644 index 0000000..5033b7e --- /dev/null +++ b/platform/linux-generic/pktio/stats_sysfs.c @@ -0,0 +1,69 @@ +/* Copyright (c) 2015, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include + +static uint64_t sysfs_get_val(const char *fname) +{ + FILE *file; + char str[128]; + uint64_t ret = -1; + + file = fopen(fname, "rt"); + if (file == NULL) { + /* File not found */ + return 0; + } + + if (fgets(str, sizeof(str), file) != NULL) { + /* Read cache line size */ + if (sscanf(str, "%" SCNx64, &ret) != 1) { + ODP_ERR("read %s\n", fname); + ret = 0; + } + } + + (void)fclose(file); + return ret; +} + +int sysfs_stats(pktio_entry_t *pktio_entry, + odp_pktio_stats_t *stats) +{ + char fname[256]; + const char *dev = pktio_entry->s.name; + + sprintf(fname, "/sys/class/net/%s/statistics/rx_bytes", dev); + stats->in_octets = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/rx_packets", dev); + stats->in_ucast_pkts = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/rx_droppped", dev); + stats->in_discards = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/rx_errors", dev); + stats->in_errors = sysfs_get_val(fname); + + stats->in_unknown_protos = 0; + + sprintf(fname, "/sys/class/net/%s/statistics/tx_bytes", dev); + stats->out_octets = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/tx_packets", dev); + stats->out_ucast_pkts = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/tx_dropped", dev); + stats->out_discards = sysfs_get_val(fname); + + sprintf(fname, "/sys/class/net/%s/statistics/tx_errors", dev); + stats->out_errors = sysfs_get_val(fname); + + return 0; +} +