[ovs-dev] [patch v2 2/3] dpctl conntrack: Add get number of connections.

Darrell Ball dlu998 at gmail.com
Mon Jan 8 23:18:43 UTC 2018


A get command is added for number of conntrack connections.
This command is only supported in the userspace datapath
at this time.

Signed-off-by: Darrell Ball <dlu998 at gmail.com>
Signed-off-by: Antonio Fischetti <antonio.fischetti at intel.com>
Co-authored-by: Antonio Fischetti <antonio.fischetti at intel.com>
---
 NEWS                |  1 +
 lib/conntrack.c     |  7 +++++++
 lib/conntrack.h     |  1 +
 lib/ct-dpif.c       |  8 ++++++++
 lib/ct-dpif.h       |  1 +
 lib/dpctl.c         | 22 ++++++++++++++++++++++
 lib/dpctl.man       |  5 +++++
 lib/dpif-netdev.c   |  9 +++++++++
 lib/dpif-netlink.c  |  1 +
 lib/dpif-provider.h |  2 ++
 10 files changed, 57 insertions(+)

diff --git a/NEWS b/NEWS
index 886aacc..edfc5c2 100644
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,7 @@ Post-v2.8.0
      * Datapath IDs may now be specified as 0x1 (etc.) instead of 16 digits.
    - Add dpctl/ct-set-maxconns and dpctl/ct-get-maxconns commands; supported in
      userspace datapath.
+   - Add dpctl/ct-get-nconns commands; supported in userspace datapath.
 
 
 v2.8.0 - 31 Aug 2017
diff --git a/lib/conntrack.c b/lib/conntrack.c
index 0b15dd6..0a706ce 100644
--- a/lib/conntrack.c
+++ b/lib/conntrack.c
@@ -2576,6 +2576,13 @@ conntrack_get_maxconns(struct conntrack *ct, uint32_t *maxconns)
     return 0;
 }
 
+int
+conntrack_get_nconns(struct conntrack *ct, uint32_t *nconns)
+{
+    *nconns = atomic_count_get(&ct->n_conn);
+    return 0;
+}
+
 /* This function must be called with the ct->resources read lock taken. */
 static struct alg_exp_node *
 expectation_lookup(struct hmap *alg_expectations,
diff --git a/lib/conntrack.h b/lib/conntrack.h
index c7f9b77..e453170 100644
--- a/lib/conntrack.h
+++ b/lib/conntrack.h
@@ -116,6 +116,7 @@ int conntrack_dump_done(struct conntrack_dump *);
 int conntrack_flush(struct conntrack *, const uint16_t *zone);
 int conntrack_set_maxconns(struct conntrack *ct, uint32_t maxconns);
 int conntrack_get_maxconns(struct conntrack *ct, uint32_t *maxconns);
+int conntrack_get_nconns(struct conntrack *ct, uint32_t *nconns);
 
 /* 'struct ct_lock' is a wrapper for an adaptive mutex.  It's useful to try
  * different types of locks (e.g. spinlocks) */
diff --git a/lib/ct-dpif.c b/lib/ct-dpif.c
index 21f900f..5fa3a97 100644
--- a/lib/ct-dpif.c
+++ b/lib/ct-dpif.c
@@ -156,6 +156,14 @@ ct_dpif_get_maxconns(struct dpif *dpif, uint32_t *maxconns)
             : EOPNOTSUPP);
 }
 
+int
+ct_dpif_get_nconns(struct dpif *dpif, uint32_t *nconns)
+{
+    return (dpif->dpif_class->ct_get_nconns
+            ? dpif->dpif_class->ct_get_nconns(dpif, nconns)
+            : EOPNOTSUPP);
+}
+
 void
 ct_dpif_entry_uninit(struct ct_dpif_entry *entry)
 {
diff --git a/lib/ct-dpif.h b/lib/ct-dpif.h
index 37001b4..09e7698 100644
--- a/lib/ct-dpif.h
+++ b/lib/ct-dpif.h
@@ -199,6 +199,7 @@ int ct_dpif_flush(struct dpif *, const uint16_t *zone,
                   const struct ct_dpif_tuple *);
 int ct_dpif_set_maxconns(struct dpif *dpif, uint32_t maxconns);
 int ct_dpif_get_maxconns(struct dpif *dpif, uint32_t *maxconns);
+int ct_dpif_get_nconns(struct dpif *dpif, uint32_t *nconns);
 void ct_dpif_entry_uninit(struct ct_dpif_entry *);
 void ct_dpif_format_entry(const struct ct_dpif_entry *, struct ds *,
                           bool verbose, bool print_stats);
diff --git a/lib/dpctl.c b/lib/dpctl.c
index 0fe86e0..87f0412 100644
--- a/lib/dpctl.c
+++ b/lib/dpctl.c
@@ -1725,6 +1725,27 @@ dpctl_ct_get_maxconns(int argc, const char *argv[],
     return error;
 }
 
+static int
+dpctl_ct_get_nconns(int argc, const char *argv[],
+                    struct dpctl_params *dpctl_p)
+{
+    struct dpif *dpif;
+    int error = dpctl_ct_open_dp(argc, argv, dpctl_p, &dpif, 2);
+    if (!error) {
+        uint32_t nconns;
+        error = ct_dpif_get_nconns(dpif, &nconns);
+
+        if (!error) {
+            dpctl_print(dpctl_p, "%u\n", nconns);
+        } else {
+            dpctl_error(dpctl_p, error, "nconns could not be retrieved");
+        }
+        dpif_close(dpif);
+    }
+
+    return error;
+}
+
 /* Undocumented commands for unit testing. */
 
 static int
@@ -2023,6 +2044,7 @@ static const struct dpctl_command all_commands[] = {
     { "ct-bkts", "[dp] [gt=N]", 0, 2, dpctl_ct_bkts, DP_RO },
     { "ct-set-maxconns", "[dp] maxconns", 1, 2, dpctl_ct_set_maxconns, DP_RW },
     { "ct-get-maxconns", "[dp]", 0, 1, dpctl_ct_get_maxconns, DP_RO },
+    { "ct-get-nconns", "[dp]", 0, 1, dpctl_ct_get_nconns, DP_RO },
     { "help", "", 0, INT_MAX, dpctl_help, DP_RO },
     { "list-commands", "", 0, INT_MAX, dpctl_list_commands, DP_RO },
 
diff --git a/lib/dpctl.man b/lib/dpctl.man
index b859c5a..9e9d2dc 100644
--- a/lib/dpctl.man
+++ b/lib/dpctl.man
@@ -263,3 +263,8 @@ datapath.
 \*(DX\fBct\-get\-maxconns\fR [\fIdp\fR]
 Read the maximum limit of connection tracker connections.
 Only supported for userspace datapath.
+.
+.TP
+\*(DX\fBct\-get\-nconns\fR [\fIdp\fR]
+Read the current number of connection tracker connections.
+Only supported for userspace datapath.
diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index bcfde03..b24525c 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -5862,6 +5862,14 @@ dpif_netdev_ct_get_maxconns(struct dpif *dpif, uint32_t *maxconns)
     return conntrack_get_maxconns(&dp->conntrack, maxconns);
 }
 
+static int
+dpif_netdev_ct_get_nconns(struct dpif *dpif, uint32_t *nconns)
+{
+    struct dp_netdev *dp = get_dp_netdev(dpif);
+
+    return conntrack_get_nconns(&dp->conntrack, nconns);
+}
+
 const struct dpif_class dpif_netdev_class = {
     "netdev",
     dpif_netdev_init,
@@ -5909,6 +5917,7 @@ const struct dpif_class dpif_netdev_class = {
     dpif_netdev_ct_flush,
     dpif_netdev_ct_set_maxconns,
     dpif_netdev_ct_get_maxconns,
+    dpif_netdev_ct_get_nconns,
     dpif_netdev_meter_get_features,
     dpif_netdev_meter_set,
     dpif_netdev_meter_get,
diff --git a/lib/dpif-netlink.c b/lib/dpif-netlink.c
index c080e66..f8d75eb 100644
--- a/lib/dpif-netlink.c
+++ b/lib/dpif-netlink.c
@@ -2991,6 +2991,7 @@ const struct dpif_class dpif_netlink_class = {
     dpif_netlink_ct_flush,
     NULL,                       /* ct_set_maxconns */
     NULL,                       /* ct_get_maxconns */
+    NULL,                       /* ct_get_nconns */
     dpif_netlink_meter_get_features,
     dpif_netlink_meter_set,
     dpif_netlink_meter_get,
diff --git a/lib/dpif-provider.h b/lib/dpif-provider.h
index 98f4115..62b3598 100644
--- a/lib/dpif-provider.h
+++ b/lib/dpif-provider.h
@@ -441,6 +441,8 @@ struct dpif_class {
     int (*ct_set_maxconns)(struct dpif *, uint32_t maxconns);
     /* Get max connections allowed. */
     int (*ct_get_maxconns)(struct dpif *, uint32_t *maxconns);
+    /* Get number of connections tracked. */
+    int (*ct_get_nconns)(struct dpif *, uint32_t *nconns);
 
     /* Meters */
 
-- 
1.9.1



More information about the dev mailing list