From patchwork Mon May 13 18:52:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 140041 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B9D424401D; Mon, 13 May 2024 20:55:13 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 023244068A; Mon, 13 May 2024 20:55:03 +0200 (CEST) Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by mails.dpdk.org (Postfix) with ESMTP id 2CE38402F1 for ; Mon, 13 May 2024 20:55:01 +0200 (CEST) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6f44d2b3130so3946869b3a.2 for ; Mon, 13 May 2024 11:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1715626500; x=1716231300; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1d2s+oymQF/Cac0yZxkI9dG4QhCl8fi/IJkLq497mW8=; b=ugHW8o3vz8WWYEX6mB8jwvOLbBqSd7nmI9rwUJndJYSmZE8xkEMnr3/U5iNI13+eGb ltuo2H5ikrbHvChSsHcBQIkZo04IGuF0nOLNd9JmB96ntbX6BJ8rYBSHdWwdC/elj2qy V+F0Wut0MPzbclQuW3vpW/tFphOOivrsFHqnD6QwvnBFdLXBe6m+EFU2njI6LGMhRnJe lXpC7kfKNuA4MEUADkt32MMWFhBbjNrH0fFyUWbugeIcBTFjqKsNJ/+D26LO+e+ylnqZ puN3kGLSr6ipZcRjWsk8xdQ5+BAbwRpxf04GVHiC0JQb5HgsilRYBrRRs5TpOnjezr42 AQYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715626500; x=1716231300; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1d2s+oymQF/Cac0yZxkI9dG4QhCl8fi/IJkLq497mW8=; b=LAdztUHGpw7OgjvoYJKPG23v+3nflxztbOHkGps4HN0gir3MkrB7E/w5XHnXPpDM1l xpEFld4XfL3xW315NDBy4WHvGEOxI0eITyws8GHryFeHJrsKqQoiZMk7+0WNlY7JhoEp 1kpaPMSR11ClNK7dYTKgev9r+hr1+jCMLkrysaVsz0RipVIM+8YC++btOYDGHusZ1zUc mQAOknCP5nTy0eF/smmjafNVxjfjj4iNpQU+TMH/lfvAXJSytxJh4alH9EFNmRdDiNgj dyv/odpSAtHgTHXMPg7HGVEJoYGRl3MKMlWqHMBQaahL4h/Nz9UMjTgcFkN4NrJ+NPDb E2gA== X-Gm-Message-State: AOJu0Yw7WIydFc6b5NkNCPbH3JjR/HT1yjbRUTXHS9G7RN0dBt+y5t3+ UR1EetnLUxYxJijlFw2kANWkhF+cdO3t6wZWg14mASi0ElTm4E8EbOqaPP8Nmz/C79w+QEQZCXQ Q7TmjfA== X-Google-Smtp-Source: AGHT+IHEElRMgpaL+SCQiEkpR6uSJEiK/RwkZY4Te0erUtHbMaK6BXVWIgwlc2Sne85KPWDkuDvTcA== X-Received: by 2002:a05:6a21:788e:b0:1af:a5b1:290a with SMTP id adf61e73a8af0-1afde0a994amr12052328637.13.1715626500154; Mon, 13 May 2024 11:55:00 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-634103f7237sm8154680a12.71.2024.05.13.11.54.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 11:54:59 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko Subject: [RFC v2 2/7] ethdev: add internal helper of SW driver statistics Date: Mon, 13 May 2024 11:52:12 -0700 Message-ID: <20240513185448.120356-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240513185448.120356-1-stephen@networkplumber.org> References: <20240510050507.14381-1-stephen@networkplumber.org> <20240513185448.120356-1-stephen@networkplumber.org> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This clones the staistic update code from virtio for use by other drivers. It also uses native uint64_t on 64 bit platform but atomic operations on 32 bit platforms. Signed-off-by: Stephen Hemminger ethdev: use atomic on 32 --- lib/ethdev/ethdev_swstats.c | 270 ++++++++++++++++++++++++++++++++++++ lib/ethdev/ethdev_swstats.h | 54 ++++++++ lib/ethdev/meson.build | 2 + lib/ethdev/version.map | 8 ++ 4 files changed, 334 insertions(+) create mode 100644 lib/ethdev/ethdev_swstats.c create mode 100644 lib/ethdev/ethdev_swstats.h diff --git a/lib/ethdev/ethdev_swstats.c b/lib/ethdev/ethdev_swstats.c new file mode 100644 index 0000000000..4c0fa36ac3 --- /dev/null +++ b/lib/ethdev/ethdev_swstats.c @@ -0,0 +1,270 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + +#include + +#include +#include + +#include "rte_ethdev.h" +#include "ethdev_swstats.h" + +static void +eth_counters_reset(struct rte_eth_counters *counters) +{ + unsigned int i; + + rte_counter64_reset(&counters->packets); + rte_counter64_reset(&counters->bytes); + rte_counter64_reset(&counters->multicast); + rte_counter64_reset(&counters->broadcast); + + for (i = 0; i < RTE_DIM(counters->size_bins); i++) + rte_counter64_reset(&counters->size_bins[i]); +} + +void +rte_eth_count_packet(struct rte_eth_counters *counters, uint32_t sz) +{ + uint32_t bin; + + if (sz == 64) { + bin = 1; + } else if (sz > 64 && sz < 1024) { + /* count zeros, and offset into correct bin */ + bin = (sizeof(sz) * 8) - rte_clz32(sz) - 5; + } else if (sz < 64) { + bin = 0; + } else if (sz < 1519) { + bin = 6; + } else { + bin = 7; + } + + rte_counter64_add(&counters->packets, 1); + rte_counter64_add(&counters->bytes, sz); + rte_counter64_add(&counters->size_bins[bin], 1); +} + +void +rte_eth_count_mbuf(struct rte_eth_counters *counters, const struct rte_mbuf *mbuf) +{ + const struct rte_ether_addr *ea; + + rte_eth_count_packet(counters, rte_pktmbuf_pkt_len(mbuf)); + + ea = rte_pktmbuf_mtod(mbuf, const struct rte_ether_addr *); + if (rte_is_multicast_ether_addr(ea)) { + if (rte_is_broadcast_ether_addr(ea)) + rte_counter64_add(&counters->broadcast, 1); + else + rte_counter64_add(&counters->multicast, 1); + } +} + +void +rte_eth_count_error(struct rte_eth_counters *counters) +{ + rte_counter64_add(&counters->errors, 1); +} + +int +rte_eth_counters_stats_get(const struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_stats *stats) +{ + unsigned int i; + uint64_t packets, bytes, errors; + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + const void *txq = dev->data->tx_queues[i]; + const struct rte_eth_counters *counters; + + if (txq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)txq + tx_offset); + packets = rte_counter64_fetch(&counters->packets); + bytes = rte_counter64_fetch(&counters->bytes); + errors = rte_counter64_fetch(&counters->errors); + + stats->opackets += packets; + stats->obytes += bytes; + stats->oerrors += errors; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_opackets[i] = packets; + stats->q_obytes[i] = bytes; + } + } + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + const void *rxq = dev->data->rx_queues[i]; + const struct rte_eth_counters *counters; + + if (rxq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)rxq + rx_offset); + packets = rte_counter64_fetch(&counters->packets); + bytes = rte_counter64_fetch(&counters->bytes); + errors = rte_counter64_fetch(&counters->errors); + + stats->ipackets += packets; + stats->ibytes += bytes; + stats->ierrors += errors; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_ipackets[i] = packets; + stats->q_ibytes[i] = bytes; + } + } + + stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; + return 0; +} + +int +rte_eth_counters_reset(struct rte_eth_dev *dev, size_t tx_offset, size_t rx_offset) +{ + unsigned int i; + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + void *txq = dev->data->tx_queues[i]; + struct rte_eth_counters *counters; + + if (txq == NULL) + continue; + + counters = (struct rte_eth_counters *)((char *)txq + tx_offset); + eth_counters_reset(counters); + } + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + void *rxq = dev->data->rx_queues[i]; + struct rte_eth_counters *counters; + + if (rxq == NULL) + continue; + + counters = (struct rte_eth_counters *)((char *)rxq + rx_offset); + eth_counters_reset(counters); + } + + return 0; +} + +struct xstats_name_off { + char name[RTE_ETH_XSTATS_NAME_SIZE]; + size_t offset; +}; + +/* [rt]x_qX_ is prepended to the name string here */ +static const struct xstats_name_off eth_swstats_strings[] = { + {"good_packets", offsetof(struct rte_eth_counters, packets)}, + {"good_bytes", offsetof(struct rte_eth_counters, bytes)}, + {"errors", offsetof(struct rte_eth_counters, errors)}, + {"multicast_packets", offsetof(struct rte_eth_counters, multicast)}, + {"broadcast_packets", offsetof(struct rte_eth_counters, broadcast)}, + {"undersize_packets", offsetof(struct rte_eth_counters, size_bins[0])}, + {"size_64_packets", offsetof(struct rte_eth_counters, size_bins[1])}, + {"size_65_127_packets", offsetof(struct rte_eth_counters, size_bins[2])}, + {"size_128_255_packets", offsetof(struct rte_eth_counters, size_bins[3])}, + {"size_256_511_packets", offsetof(struct rte_eth_counters, size_bins[4])}, + {"size_512_1023_packets", offsetof(struct rte_eth_counters, size_bins[5])}, + {"size_1024_1518_packets", offsetof(struct rte_eth_counters, size_bins[6])}, + {"size_1519_max_packets", offsetof(struct rte_eth_counters, size_bins[7])}, +}; +#define NUM_SWSTATS_XSTATS RTE_DIM(eth_swstats_strings) + + +int +rte_eth_counters_xstats_get_names(struct rte_eth_dev *dev, + struct rte_eth_xstat_name *xstats_names) +{ + unsigned int i, t, count = 0; + + if (xstats_names == NULL) + return (dev->data->nb_tx_queues + dev->data->nb_rx_queues) * NUM_SWSTATS_XSTATS; + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + const void *rxq = dev->data->rx_queues[i]; + + if (rxq == NULL) + continue; + + for (t = 0; t < NUM_SWSTATS_XSTATS; t++) { + snprintf(xstats_names[count].name, sizeof(xstats_names[count].name), + "rx_q%u_%s", i, eth_swstats_strings[t].name); + count++; + } + } + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + const void *txq = dev->data->tx_queues[i]; + + if (txq == NULL) + continue; + + for (t = 0; t < NUM_SWSTATS_XSTATS; t++) { + snprintf(xstats_names[count].name, sizeof(xstats_names[count].name), + "tx_q%u_%s", i, eth_swstats_strings[t].name); + count++; + } + } + return count; +} + +int +rte_eth_counters_xstats_get(struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_xstat *xstats, unsigned int n) +{ + unsigned int i, t, count = 0; + const unsigned int nstats + = (dev->data->nb_tx_queues + dev->data->nb_rx_queues) * NUM_SWSTATS_XSTATS; + + if (n < nstats) + return nstats; + + for (i = 0; i < dev->data->nb_rx_queues; i++) { + const void *rxq = dev->data->rx_queues[i]; + const struct rte_eth_counters *counters; + + if (rxq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)rxq + rx_offset); + for (t = 0; t < NUM_SWSTATS_XSTATS; t++) { + const uint64_t *valuep + = (const uint64_t *)((const char *)counters + + eth_swstats_strings[t].offset); + + xstats[count].value = *valuep; + xstats[count].id = count; + ++count; + } + } + + for (i = 0; i < dev->data->nb_tx_queues; i++) { + const void *txq = dev->data->tx_queues[i]; + const struct rte_eth_counters *counters; + + if (txq == NULL) + continue; + + counters = (const struct rte_eth_counters *)((const char *)txq + tx_offset); + for (t = 0; t < NUM_SWSTATS_XSTATS; t++) { + const uint64_t *valuep + = (const uint64_t *)((const char *)counters + + eth_swstats_strings[t].offset); + + xstats[count].value = *valuep; + xstats[count].id = count; + ++count; + } + } + + return count; +} diff --git a/lib/ethdev/ethdev_swstats.h b/lib/ethdev/ethdev_swstats.h new file mode 100644 index 0000000000..45b419b887 --- /dev/null +++ b/lib/ethdev/ethdev_swstats.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) Stephen Hemminger + */ + +#ifndef _RTE_ETHDEV_SWSTATS_H_ +#define _RTE_ETHDEV_SWSTATS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct rte_eth_counters { + rte_counter64_t packets; + rte_counter64_t bytes; + rte_counter64_t errors; + rte_counter64_t multicast; + rte_counter64_t broadcast; + /* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */ + rte_counter64_t size_bins[8]; +}; + +__rte_internal +void rte_eth_count_packet(struct rte_eth_counters *counters, uint32_t size); + +__rte_internal +void rte_eth_count_mbuf(struct rte_eth_counters *counters, const struct rte_mbuf *mbuf); + +__rte_internal +void rte_eth_count_error(struct rte_eth_counters *stats); + +__rte_internal +int rte_eth_counters_stats_get(const struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_stats *stats); + +__rte_internal +int rte_eth_counters_reset(struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset); + +__rte_internal +int rte_eth_counters_xstats_get_names(struct rte_eth_dev *dev, + struct rte_eth_xstat_name *xstats_names); +__rte_internal +int rte_eth_counters_xstats_get(struct rte_eth_dev *dev, + size_t tx_offset, size_t rx_offset, + struct rte_eth_xstat *xstats, unsigned int n); + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_ETHDEV_SWSTATS_H_ */ diff --git a/lib/ethdev/meson.build b/lib/ethdev/meson.build index f1d2586591..7ce29a46d4 100644 --- a/lib/ethdev/meson.build +++ b/lib/ethdev/meson.build @@ -3,6 +3,7 @@ sources = files( 'ethdev_driver.c', + 'ethdev_swstats.c', 'ethdev_private.c', 'ethdev_profile.c', 'ethdev_trace_points.c', @@ -42,6 +43,7 @@ driver_sdk_headers += files( 'ethdev_driver.h', 'ethdev_pci.h', 'ethdev_vdev.h', + 'ethdev_swstats.h', ) if is_linux diff --git a/lib/ethdev/version.map b/lib/ethdev/version.map index 79f6f5293b..1ca53e2c5d 100644 --- a/lib/ethdev/version.map +++ b/lib/ethdev/version.map @@ -358,4 +358,12 @@ INTERNAL { rte_eth_switch_domain_alloc; rte_eth_switch_domain_free; rte_flow_fp_default_ops; + + rte_eth_count_error; + rte_eth_count_mbuf; + rte_eth_count_packet; + rte_eth_counters_reset; + rte_eth_counters_stats_get; + rte_eth_counters_xstats_get; + rte_eth_counters_xstats_get_names; };