diff mbox series

[ovs-dev,v1,2/3] dpctl conntrack: Add get number of connections command.

Message ID 1513614439-40419-3-git-send-email-dlu998@gmail.com
State Changes Requested
Headers show
Series dpctl conntrack: Add nconns/maxconns commands. | expand

Commit Message

Darrell Ball Dec. 18, 2017, 4:27 p.m. UTC
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@gmail.com>
Signed-off-by: Antonio Fischetti <antonio.fischetti@intel.com>
Co-authored-by: Antonio Fischetti <antonio.fischetti@intel.com>
---
 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       |  4 ++++
 lib/dpif-netdev.c   |  9 +++++++++
 lib/dpif-netlink.c  |  1 +
 lib/dpif-provider.h |  2 ++
 9 files changed, 55 insertions(+)

Comments

Ben Pfaff Jan. 4, 2018, 9:32 p.m. UTC | #1
On Mon, Dec 18, 2017 at 08:27:18AM -0800, Darrell Ball wrote:
> 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@gmail.com>
> Signed-off-by: Antonio Fischetti <antonio.fischetti@intel.com>
> Co-authored-by: Antonio Fischetti <antonio.fischetti@intel.com>

Thanks for the patch.

Please mention the new feature in NEWS.

Thanks,

Ben.
diff mbox series

Patch

diff --git a/lib/conntrack.c b/lib/conntrack.c
index 4761fa0..9a47daa 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 9d90f15..5884ef2 100644
--- a/lib/dpctl.c
+++ b/lib/dpctl.c
@@ -1724,6 +1724,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
@@ -2022,6 +2043,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 bd55930..0ebe7d5 100644
--- a/lib/dpctl.man
+++ b/lib/dpctl.man
@@ -261,3 +261,7 @@  normally happens due to connection expiry.
 .TP
 \*(DX\fBct\-get\-maxconns\fR [\fIdp\fR]
 Read the maximum limit of connection tracker connections.
+.
+.TP
+\*(DX\fBct\-get\-nconns\fR [\fIdp\fR]
+Read the current number of connection tracker connections.
diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index f14aa4a..e7fc11b 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -5755,6 +5755,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,
@@ -5802,6 +5810,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 */