e7a346
From b027d2fdd184d2ee2b2c4236603200be344156f8 Mon Sep 17 00:00:00 2001
e7a346
From: Atin Mukherjee <amukherj@redhat.com>
e7a346
Date: Thu, 10 Aug 2017 18:31:55 +0530
e7a346
Subject: [PATCH 45/74] glusterd: introduce max-port range
e7a346
e7a346
glusterd.vol file always had an option (commented out) to indicate the
e7a346
base-port to start the portmapper allocation. This patch brings in the
e7a346
max-port configuration where one can limit the range of ports which
e7a346
gluster can be allowed to bind.
e7a346
e7a346
>Fixes: #305
e7a346
>Change-Id: Id7a864f818227b9530a07e13d605138edacd9aa9
e7a346
>Signed-off-by: Atin Mukherjee <amukherj@redhat.com>
e7a346
>Reviewed-on: https://review.gluster.org/18016
e7a346
>Smoke: Gluster Build System <jenkins@build.gluster.org>
e7a346
>Reviewed-by: Prashanth Pai <ppai@redhat.com>
e7a346
>Reviewed-by: Niels de Vos <ndevos@redhat.com>
e7a346
>CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
e7a346
>Reviewed-by: Gaurav Yadav <gyadav@redhat.com>
e7a346
e7a346
Change-Id: Id7a864f818227b9530a07e13d605138edacd9aa9
e7a346
BUG: 1474745
e7a346
Signed-off-by: Atin Mukherjee <amukherj@redhat.com>
e7a346
Reviewed-on: https://code.engineering.redhat.com/gerrit/123060
e7a346
Reviewed-by: Gaurav Yadav <gyadav@redhat.com>
e7a346
---
e7a346
 extras/glusterd.vol.in                         |  1 +
e7a346
 xlators/mgmt/glusterd/src/glusterd-messages.h  | 10 +++++++++-
e7a346
 xlators/mgmt/glusterd/src/glusterd-pmap.c      | 20 +++++++++++---------
e7a346
 xlators/mgmt/glusterd/src/glusterd-pmap.h      |  3 ++-
e7a346
 xlators/mgmt/glusterd/src/glusterd-snapd-svc.c |  8 ++++++++
e7a346
 xlators/mgmt/glusterd/src/glusterd-utils.c     | 18 +++++++++++++++++-
e7a346
 xlators/mgmt/glusterd/src/glusterd.c           | 17 +++++++++++++++--
e7a346
 xlators/mgmt/glusterd/src/glusterd.h           |  1 +
e7a346
 8 files changed, 64 insertions(+), 14 deletions(-)
e7a346
e7a346
diff --git a/extras/glusterd.vol.in b/extras/glusterd.vol.in
e7a346
index 957b277..0152996 100644
e7a346
--- a/extras/glusterd.vol.in
e7a346
+++ b/extras/glusterd.vol.in
e7a346
@@ -9,4 +9,5 @@ volume management
e7a346
     option event-threads 1
e7a346
 #   option transport.address-family inet6
e7a346
 #   option base-port 49152
e7a346
+#   option max-port  65535
e7a346
 end-volume
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd-messages.h b/xlators/mgmt/glusterd/src/glusterd-messages.h
e7a346
index 8bb4c43..de9ae92 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd-messages.h
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd-messages.h
e7a346
@@ -41,7 +41,7 @@
e7a346
 
e7a346
 #define GLUSTERD_COMP_BASE      GLFS_MSGID_GLUSTERD
e7a346
 
e7a346
-#define GLFS_NUM_MESSAGES       611
e7a346
+#define GLFS_NUM_MESSAGES       612
e7a346
 
e7a346
 #define GLFS_MSGID_END          (GLUSTERD_COMP_BASE + GLFS_NUM_MESSAGES + 1)
e7a346
 /* Messaged with message IDs */
e7a346
@@ -4945,6 +4945,14 @@
e7a346
  */
e7a346
 #define GD_MSG_SVC_START_FAIL                      (GLUSTERD_COMP_BASE + 590)
e7a346
 
e7a346
+/*!
e7a346
+ * @messageid
e7a346
+ * @diagnosis
e7a346
+ * @recommendedaction
e7a346
+ *
e7a346
+ */
e7a346
+#define GD_MSG_PORTS_EXHAUSTED (GLUSTERD_COMP_BASE + 612)
e7a346
+
e7a346
 /*------------*/
e7a346
 
e7a346
 #define glfs_msg_end_x GLFS_MSGID_END, "Invalid: End of messages"
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd-pmap.c b/xlators/mgmt/glusterd/src/glusterd-pmap.c
e7a346
index 1b547e7..4f045ab 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd-pmap.c
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd-pmap.c
e7a346
@@ -61,8 +61,8 @@ pmap_registry_new (xlator_t *this)
e7a346
 
e7a346
         pmap->base_port = pmap->last_alloc =
e7a346
                 ((glusterd_conf_t *)(this->private))->base_port;
e7a346
-
e7a346
-        for (i = pmap->base_port; i <= GF_PORT_MAX; i++) {
e7a346
+        pmap->max_port = ((glusterd_conf_t *)(this->private))->max_port;
e7a346
+        for (i = pmap->base_port; i <= pmap->max_port; i++) {
e7a346
                 if (pmap_port_isfree (i))
e7a346
                         pmap->ports[i].type = GF_PMAP_PORT_FREE;
e7a346
                 else
e7a346
@@ -184,10 +184,12 @@ pmap_registry_search_by_xprt (xlator_t *this, void *xprt,
e7a346
 static char *
e7a346
 pmap_registry_search_by_port (xlator_t *this, int port)
e7a346
 {
e7a346
-        struct pmap_registry *pmap = NULL;
e7a346
-        char *brickname = NULL;
e7a346
+        struct  pmap_registry *pmap = NULL;
e7a346
+        char   *brickname           = NULL;
e7a346
+        int     max_port            = 0;
e7a346
 
e7a346
-        if (port > GF_PORT_MAX)
e7a346
+        max_port = ((glusterd_conf_t *)(this->private))->max_port;
e7a346
+        if (port > max_port)
e7a346
                 goto out;
e7a346
 
e7a346
         pmap = pmap_registry_get (this);
e7a346
@@ -209,7 +211,7 @@ pmap_registry_alloc (xlator_t *this)
e7a346
 
e7a346
         pmap = pmap_registry_get (this);
e7a346
 
e7a346
-        for (p = pmap->base_port; p <= GF_PORT_MAX; p++) {
e7a346
+        for (p = pmap->base_port; p <= pmap->max_port; p++) {
e7a346
                 /* GF_PMAP_PORT_FOREIGN may be freed up ? */
e7a346
                 if ((pmap->ports[p].type == GF_PMAP_PORT_FREE) ||
e7a346
                     (pmap->ports[p].type == GF_PMAP_PORT_FOREIGN)) {
e7a346
@@ -261,7 +263,7 @@ pmap_registry_bind (xlator_t *this, int port, const char *brickname,
e7a346
 
e7a346
         pmap = pmap_registry_get (this);
e7a346
 
e7a346
-        if (port > GF_PORT_MAX)
e7a346
+        if (port > pmap->max_port)
e7a346
                 goto out;
e7a346
 
e7a346
         p = port;
e7a346
@@ -297,7 +299,7 @@ pmap_registry_extend (xlator_t *this, int port, const char *brickname)
e7a346
 
e7a346
         pmap = pmap_registry_get (this);
e7a346
 
e7a346
-        if (port > GF_PORT_MAX) {
e7a346
+        if (port > pmap->max_port) {
e7a346
                 return -1;
e7a346
         }
e7a346
 
e7a346
@@ -357,7 +359,7 @@ pmap_registry_remove (xlator_t *this, int port, const char *brickname,
e7a346
                 goto out;
e7a346
 
e7a346
         if (port) {
e7a346
-                if (port > GF_PORT_MAX)
e7a346
+                if (port > pmap->max_port)
e7a346
                         goto out;
e7a346
 
e7a346
                 p = port;
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd-pmap.h b/xlators/mgmt/glusterd/src/glusterd-pmap.h
e7a346
index 253b4cc..f642d66 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd-pmap.h
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd-pmap.h
e7a346
@@ -31,8 +31,9 @@ struct pmap_port_status {
e7a346
 
e7a346
 struct pmap_registry {
e7a346
         int     base_port;
e7a346
+        int     max_port;
e7a346
         int     last_alloc;
e7a346
-        struct  pmap_port_status ports[65536];
e7a346
+        struct  pmap_port_status ports[GF_PORT_MAX + 1];
e7a346
 };
e7a346
 
e7a346
 int pmap_assign_port (xlator_t *this, int port, const char *path);
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd-snapd-svc.c b/xlators/mgmt/glusterd/src/glusterd-snapd-svc.c
e7a346
index 59d8fbd..5621852 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd-snapd-svc.c
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd-snapd-svc.c
e7a346
@@ -300,6 +300,14 @@ glusterd_snapdsvc_start (glusterd_svc_t *svc, int flags)
e7a346
                          "-S", svc->conn.sockpath, NULL);
e7a346
 
e7a346
         snapd_port = pmap_assign_port (THIS, volinfo->snapd.port, snapd_id);
e7a346
+        if (!snapd_port) {
e7a346
+                gf_msg (this->name, GF_LOG_ERROR, 0, GD_MSG_PORTS_EXHAUSTED,
e7a346
+                        "All the ports in the range are exhausted, can't start "
e7a346
+                        "snapd for volume %s", volinfo->volname);
e7a346
+                ret = -1;
e7a346
+                goto out;
e7a346
+        }
e7a346
+
e7a346
         volinfo->snapd.port = snapd_port;
e7a346
 
e7a346
         runner_add_arg (&runner, "--brick-port");
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd-utils.c b/xlators/mgmt/glusterd/src/glusterd-utils.c
e7a346
index 18de517..55c4fa7 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd-utils.c
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd-utils.c
e7a346
@@ -2002,7 +2002,14 @@ glusterd_volume_start_glusterfs (glusterd_volinfo_t  *volinfo,
e7a346
         }
e7a346
 
e7a346
         port = pmap_assign_port (THIS, brickinfo->port, brickinfo->path);
e7a346
-
e7a346
+        if (!port) {
e7a346
+                gf_msg (this->name, GF_LOG_ERROR, 0, GD_MSG_PORTS_EXHAUSTED,
e7a346
+                        "All the ports in the range are exhausted, can't start "
e7a346
+                        "brick %s for volume %s", brickinfo->path,
e7a346
+                        volinfo->volname);
e7a346
+                ret = -1;
e7a346
+                goto out;
e7a346
+        }
e7a346
         /* Build the exp_path, before starting the glusterfsd even in
e7a346
            valgrind mode. Otherwise all the glusterfsd processes start
e7a346
            writing the valgrind log to the same file.
e7a346
@@ -2076,6 +2083,15 @@ retry:
e7a346
                           brickinfo->path);
e7a346
                 rdma_port = pmap_assign_port (THIS, brickinfo->rdma_port,
e7a346
                                               rdma_brick_path);
e7a346
+                if (!rdma_port) {
e7a346
+                        gf_msg (this->name, GF_LOG_ERROR, 0,
e7a346
+                                GD_MSG_PORTS_EXHAUSTED, "All rdma ports in the "
e7a346
+                                "range are exhausted, can't start brick %s for "
e7a346
+                                "volume %s", rdma_brick_path,
e7a346
+                                volinfo->volname);
e7a346
+                        ret = -1;
e7a346
+                        goto out;
e7a346
+                }
e7a346
                 runner_argprintf (&runner, "%d,%d", port, rdma_port);
e7a346
                 runner_add_arg (&runner, "--xlator-option");
e7a346
                 runner_argprintf (&runner, "%s-server.transport.rdma.listen-port=%d",
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd.c b/xlators/mgmt/glusterd/src/glusterd.c
e7a346
index 81a3206..68d3e90 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd.c
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd.c
e7a346
@@ -1824,12 +1824,20 @@ init (xlator_t *this)
e7a346
         if (ret)
e7a346
                 goto out;
e7a346
 
e7a346
-         conf->base_port = GF_IANA_PRIV_PORTS_START;
e7a346
-         if (dict_get_uint32(this->options, "base-port", &conf->base_port) == 0) {
e7a346
+        conf->base_port = GF_IANA_PRIV_PORTS_START;
e7a346
+        if (dict_get_uint32 (this->options, "base-port",
e7a346
+                             &conf->base_port) == 0) {
e7a346
                 gf_msg (this->name, GF_LOG_INFO, 0,
e7a346
                         GD_MSG_DICT_SET_FAILED,
e7a346
                         "base-port override: %d", conf->base_port);
e7a346
          }
e7a346
+         conf->max_port = GF_PORT_MAX;
e7a346
+         if (dict_get_uint32 (this->options, "max-port",
e7a346
+                              &conf->max_port) == 0) {
e7a346
+                gf_msg (this->name, GF_LOG_INFO, 0,
e7a346
+                        GD_MSG_DICT_SET_FAILED,
e7a346
+                        "max-port override: %d", conf->max_port);
e7a346
+         }
e7a346
 
e7a346
         /* Set option to run bricks on valgrind if enabled in glusterd.vol */
e7a346
         this->ctx->cmd_args.valgrind = valgrind;
e7a346
@@ -2135,6 +2143,11 @@ struct volume_options options[] = {
e7a346
           .type = GF_OPTION_TYPE_INT,
e7a346
           .description = "Sets the base port for portmap query"
e7a346
         },
e7a346
+        { .key = {"max-port"},
e7a346
+          .type = GF_OPTION_TYPE_INT,
e7a346
+          .max = GF_PORT_MAX,
e7a346
+          .description = "Sets the max port for portmap query"
e7a346
+        },
e7a346
         { .key = {"snap-brick-path"},
e7a346
           .type = GF_OPTION_TYPE_STR,
e7a346
           .description = "directory where the bricks for the snapshots will be created"
e7a346
diff --git a/xlators/mgmt/glusterd/src/glusterd.h b/xlators/mgmt/glusterd/src/glusterd.h
e7a346
index d4bb236..291f2f7 100644
e7a346
--- a/xlators/mgmt/glusterd/src/glusterd.h
e7a346
+++ b/xlators/mgmt/glusterd/src/glusterd.h
e7a346
@@ -187,6 +187,7 @@ typedef struct {
e7a346
         gf_boolean_t               restart_done;
e7a346
         rpcsvc_t                  *uds_rpc; /* RPCSVC for the unix domain socket */
e7a346
         uint32_t                   base_port;
e7a346
+        uint32_t                   max_port;
e7a346
         char                      *snap_bricks_directory;
e7a346
         gf_store_handle_t         *missed_snaps_list_shandle;
e7a346
         struct cds_list_head       missed_snaps_list;
e7a346
-- 
e7a346
1.8.3.1
e7a346