git: 36d42c862f4a - main - ena: Support srd metrics with sysctl

From: Arthur Kiyanovski <akiyano_at_FreeBSD.org>
Date: Thu, 28 Dec 2023 13:57:20 UTC
The branch main has been updated by akiyano:

URL: https://cgit.FreeBSD.org/src/commit/?id=36d42c862f4a5643f6e2395e8d7b7e5c4580499a

commit 36d42c862f4a5643f6e2395e8d7b7e5c4580499a
Author:     Osama Abboud <osamaabb@amazon.com>
AuthorDate: 2023-09-12 11:17:45 +0000
Commit:     Arthur Kiyanovski <akiyano@FreeBSD.org>
CommitDate: 2023-12-28 13:56:30 +0000

    ena: Support srd metrics with sysctl
    
    This commit introduces SRD metrics through sysctl.
    The metrics can be queried using the following sysctl node:
    sysctl dev.ena.<device index>.ena_srd_info
    
    Approved by: cperciva (mentor)
    MFC after: 2 weeks
    Sponsored by: Amazon, Inc.
---
 sys/dev/ena/ena.c        | 10 +++++++
 sys/dev/ena/ena.h        |  1 +
 sys/dev/ena/ena_sysctl.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/sys/dev/ena/ena.c b/sys/dev/ena/ena.c
index 64c3d79f00db..45bca630b4c2 100644
--- a/sys/dev/ena/ena.c
+++ b/sys/dev/ena/ena.c
@@ -166,6 +166,7 @@ static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *);
 static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
 static void unimplemented_aenq_handler(void *, struct ena_admin_aenq_entry *);
 static int ena_copy_eni_metrics(struct ena_adapter *);
+static int ena_copy_srd_metrics(struct ena_adapter *);
 static int ena_copy_customer_metrics(struct ena_adapter *);
 static void ena_timer_service(void *);
 
@@ -3306,6 +3307,12 @@ ena_copy_eni_metrics(struct ena_adapter *adapter)
 	return (rc);
 }
 
+static int
+ena_copy_srd_metrics(struct ena_adapter *adapter)
+{
+	return ena_com_get_ena_srd_info(adapter->ena_dev, &adapter->ena_srd_info);
+}
+
 static int
 ena_copy_customer_metrics(struct ena_adapter *adapter)
 {
@@ -3568,6 +3575,9 @@ ena_metrics_task(void *arg, int pending)
 	else if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENI_STATS))
 		(void)ena_copy_eni_metrics(adapter);
 
+	if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENA_SRD_INFO))
+		(void)ena_copy_srd_metrics(adapter);
+
 	ENA_LOCK_UNLOCK();
 }
 
diff --git a/sys/dev/ena/ena.h b/sys/dev/ena/ena.h
index 18988d719a18..5647fe973502 100644
--- a/sys/dev/ena/ena.h
+++ b/sys/dev/ena/ena.h
@@ -487,6 +487,7 @@ struct ena_adapter {
 	struct ena_stats_dev dev_stats;
 	struct ena_hw_stats hw_stats;
 	struct ena_admin_eni_stats eni_metrics;
+	struct ena_admin_ena_srd_info ena_srd_info;
 	uint64_t *customer_metrics_array;
 
 	enum ena_regs_reset_reason_types reset_reason;
diff --git a/sys/dev/ena/ena_sysctl.c b/sys/dev/ena/ena_sysctl.c
index 82030a2a7539..5efad01f372e 100644
--- a/sys/dev/ena/ena_sysctl.c
+++ b/sys/dev/ena/ena_sysctl.c
@@ -38,6 +38,7 @@ static void ena_sysctl_add_wd(struct ena_adapter *);
 static void ena_sysctl_add_stats(struct ena_adapter *);
 static void ena_sysctl_add_eni_metrics(struct ena_adapter *);
 static void ena_sysctl_add_customer_metrics(struct ena_adapter *);
+static void ena_sysctl_add_srd_info(struct ena_adapter *);
 static void ena_sysctl_add_tuneables(struct ena_adapter *);
 static void ena_sysctl_add_irq_affinity(struct ena_adapter *);
 /* Kernel option RSS prevents manipulation of key hash and indirection table. */
@@ -59,18 +60,46 @@ static int ena_sysctl_rss_indir_table(SYSCTL_HANDLER_ARGS);
 #define ENA_METRICS_MAX_SAMPLE_INTERVAL 3600
 #define ENA_HASH_KEY_MSG_SIZE (ENA_HASH_KEY_SIZE * 2 + 1)
 
-#define SYSCTL_GSTRING_LEN 64
+#define SYSCTL_GSTRING_LEN 128
 
 #define ENA_METRIC_ENI_ENTRY(stat, desc) { \
         .name = #stat, \
         .description = #desc, \
 }
 
+#define ENA_STAT_ENTRY(stat, desc, stat_type) { \
+        .name = #stat, \
+        .description = #desc, \
+        .stat_offset = offsetof(struct ena_admin_##stat_type, stat) / sizeof(u64), \
+}
+
+#define ENA_STAT_ENA_SRD_ENTRY(stat, desc) \
+	ENA_STAT_ENTRY(stat, desc, ena_srd_stats)
+
 struct ena_hw_metrics {
         char name[SYSCTL_GSTRING_LEN];
         char description[SYSCTL_GSTRING_LEN];
 };
 
+struct ena_srd_metrics {
+        char name[SYSCTL_GSTRING_LEN];
+        char description[SYSCTL_GSTRING_LEN];
+        int stat_offset;
+};
+
+static const struct ena_srd_metrics ena_srd_stats_strings[] = {
+        ENA_STAT_ENA_SRD_ENTRY(
+	    ena_srd_tx_pkts, Number of packets transmitted over ENA SRD),
+        ENA_STAT_ENA_SRD_ENTRY(
+	    ena_srd_eligible_tx_pkts, Number of packets transmitted or could
+	    have been transmitted over ENA SRD),
+        ENA_STAT_ENA_SRD_ENTRY(
+	    ena_srd_rx_pkts, Number of packets received over ENA SRD),
+        ENA_STAT_ENA_SRD_ENTRY(
+	    ena_srd_resource_utilization, Percentage of the ENA SRD resources
+	    that are in use),
+};
+
 static const struct ena_hw_metrics ena_hw_stats_strings[] = {
         ENA_METRIC_ENI_ENTRY(
 	    bw_in_allowance_exceeded, Inbound BW allowance exceeded),
@@ -91,6 +120,7 @@ static const struct ena_hw_metrics ena_hw_stats_strings[] = {
 #endif
 
 #define ENA_CUSTOMER_METRICS_ARRAY_SIZE      ARRAY_SIZE(ena_hw_stats_strings)
+#define ENA_SRD_METRICS_ARRAY_SIZE           ARRAY_SIZE(ena_srd_stats_strings)
 
 static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
     "ENA driver parameters");
@@ -153,6 +183,9 @@ ena_sysctl_add_nodes(struct ena_adapter *adapter)
 	else if (ena_com_get_cap(dev, ENA_ADMIN_ENI_STATS))
 		ena_sysctl_add_eni_metrics(adapter);
 
+	if (ena_com_get_cap(adapter->ena_dev, ENA_ADMIN_ENA_SRD_INFO))
+		ena_sysctl_add_srd_info(adapter);
+
 	ena_sysctl_add_wd(adapter);
 	ena_sysctl_add_stats(adapter);
 	ena_sysctl_add_tuneables(adapter);
@@ -379,6 +412,47 @@ ena_sysctl_add_stats(struct ena_adapter *adapter)
 	    &admin_stats->no_completion, 0, "Commands not completed");
 }
 
+static void
+ena_sysctl_add_srd_info(struct ena_adapter *adapter)
+{
+	device_t dev;
+
+	struct sysctl_oid *ena_srd_info;
+	struct sysctl_oid_list *srd_list;
+
+	struct sysctl_ctx_list *ctx;
+	struct sysctl_oid *tree;
+	struct sysctl_oid_list *child;
+
+	struct ena_admin_ena_srd_stats *srd_stats_ptr;
+	struct ena_srd_metrics cur_stat_strings;
+
+	int i;
+
+	dev = adapter->pdev;
+
+	ctx = device_get_sysctl_ctx(dev);
+	tree = device_get_sysctl_tree(dev);
+	child = SYSCTL_CHILDREN(tree);
+
+	ena_srd_info = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "ena_srd_info",
+	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ENA's SRD information");
+	srd_list = SYSCTL_CHILDREN(ena_srd_info);
+
+	SYSCTL_ADD_U64(ctx, srd_list, OID_AUTO, "ena_srd_mode",
+            CTLFLAG_RD, &adapter->ena_srd_info.flags, 0,
+            "Describes which ENA-express features are enabled");
+
+	srd_stats_ptr = &adapter->ena_srd_info.ena_srd_stats;
+
+	for (i = 0 ; i < ENA_SRD_METRICS_ARRAY_SIZE; i++) {
+		cur_stat_strings = ena_srd_stats_strings[i];
+		SYSCTL_ADD_U64(ctx, srd_list, OID_AUTO, cur_stat_strings.name,
+		    CTLFLAG_RD, (u64 *)srd_stats_ptr + cur_stat_strings.stat_offset,
+		    0, cur_stat_strings.description);
+	}
+}
+
 static void
 ena_sysctl_add_customer_metrics(struct ena_adapter *adapter)
 {