]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: proxy: make the proxy lookup functions more user-friendly
authorWilly Tarreau <w@1wt.eu>
Tue, 26 May 2015 09:24:42 +0000 (11:24 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 May 2015 09:24:42 +0000 (11:24 +0200)
First, findproxy() was renamed proxy_find_by_name() so that its explicit
that a name is required for the lookup. Second, we give this function
the ability to search for tables if needed. Third we now provide inline
wrappers to pass the appropriate PR_CAP_* flags and to explicitly look
up a frontend, backend or table.

include/proto/proxy.h
src/cfgparse.c
src/dumpstats.c
src/hlua.c
src/proxy.c
src/sample.c
src/stream.c

index 139791f8c952cdd83a8c7e957d88096db21c3646..07ccc9650c71f5ea1d3f564c83a8028345b089fe 100644 (file)
@@ -49,7 +49,7 @@ const char *proxy_cap_str(int cap);
 const char *proxy_mode_str(int mode);
 void proxy_store_name(struct proxy *px);
 struct proxy *findproxy_mode(const char *name, int mode, int cap);
-struct proxy *findproxy(const char *name, int cap);
+struct proxy *proxy_find_by_name(const char *name, int cap, int table);
 struct server *findserver(const struct proxy *px, const char *name);
 int proxy_cfg_ensure_no_http(struct proxy *curproxy);
 void init_new_proxy(struct proxy *p);
@@ -65,6 +65,30 @@ static inline const char *proxy_type_str(struct proxy *proxy)
        return proxy_cap_str(proxy->cap);
 }
 
+/* Find the frontend having name <name>. The name may also start with a '#' to
+ * reference a numeric id. NULL is returned if not found.
+ */
+static inline struct proxy *proxy_fe_by_name(const char *name)
+{
+       return proxy_find_by_name(name, PR_CAP_FE, 0);
+}
+
+/* Find the backend having name <name>. The name may also start with a '#' to
+ * reference a numeric id. NULL is returned if not found.
+ */
+static inline struct proxy *proxy_be_by_name(const char *name)
+{
+       return proxy_find_by_name(name, PR_CAP_BE, 0);
+}
+
+/* Find the table having name <name>. The name may also start with a '#' to
+ * reference a numeric id. NULL is returned if not found.
+ */
+static inline struct proxy *proxy_tbl_by_name(const char *name)
+{
+       return proxy_find_by_name(name, 0, 1);
+}
+
 /* this function initializes all timeouts for proxy p */
 static inline void proxy_reset_timeouts(struct proxy *proxy)
 {
index d4fac8cdf9ef7a5e0a592f7c3315fab33f9a8a37..84420d11f7a97798bebc5cacb32be2b7371709c1 100644 (file)
@@ -7080,7 +7080,7 @@ int check_config_validity()
                                curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
 
                        if (mrule->table.name)
-                               target = findproxy(mrule->table.name, 0);
+                               target = proxy_tbl_by_name(mrule->table.name);
                        else
                                target = curproxy;
 
@@ -7113,7 +7113,7 @@ int check_config_validity()
                        curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
 
                        if (mrule->table.name)
-                               target = findproxy(mrule->table.name, 0);
+                               target = proxy_tbl_by_name(mrule->table.name);
                        else
                                target = curproxy;
 
@@ -7147,7 +7147,7 @@ int check_config_validity()
                                continue;
 
                        if (trule->act_prm.trk_ctr.table.n)
-                               target = findproxy(trule->act_prm.trk_ctr.table.n, 0);
+                               target = proxy_tbl_by_name(trule->act_prm.trk_ctr.table.n);
                        else
                                target = curproxy;
 
@@ -7186,7 +7186,7 @@ int check_config_validity()
                                continue;
 
                        if (trule->act_prm.trk_ctr.table.n)
-                               target = findproxy(trule->act_prm.trk_ctr.table.n, 0);
+                               target = proxy_tbl_by_name(trule->act_prm.trk_ctr.table.n);
                        else
                                target = curproxy;
 
@@ -7225,7 +7225,7 @@ int check_config_validity()
                                continue;
 
                        if (hrqrule->act_prm.trk_ctr.table.n)
-                               target = findproxy(hrqrule->act_prm.trk_ctr.table.n, 0);
+                               target = proxy_tbl_by_name(hrqrule->act_prm.trk_ctr.table.n);
                        else
                                target = curproxy;
 
@@ -7579,7 +7579,7 @@ out_uri_auth_compat:
                                }
 
                                if (pname) {
-                                       px = findproxy(pname, PR_CAP_BE);
+                                       px = proxy_be_by_name(pname);
                                        if (!px) {
                                                Alert("config : %s '%s', server '%s': unable to find required proxy '%s' for tracking.\n",
                                                        proxy_type_str(curproxy), curproxy->id,
index 885a15939c17f580830835f602a8055435f47071..559f229652c43036a646c7d953f33955bedadfc9 100644 (file)
@@ -924,7 +924,7 @@ static struct proxy *expect_frontend_admin(struct stream *s, struct stream_inter
                return NULL;
        }
 
-       px = findproxy(arg, PR_CAP_FE);
+       px = proxy_fe_by_name(arg);
        if (!px) {
                appctx->ctx.cli.msg = "No such frontend.\n";
                appctx->st0 = STAT_CLI_PRINT;
@@ -4622,7 +4622,7 @@ static int stats_process_http_post(struct stream_interface *si)
 
                        /* Now we can check the key to see what to do */
                        if (!px && (strcmp(key, "b") == 0)) {
-                               if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
+                               if ((px = proxy_be_by_name(value)) == NULL) {
                                        /* the backend name is unknown or ambiguous (duplicate names) */
                                        appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
                                        goto out;
index a937e4f07996dba8625d40f0190688068a4692d1..5e1bdc3227518eb366c7bae8f532e5307cbaf421 100644 (file)
@@ -632,7 +632,7 @@ __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
                                WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
                        memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
                        trash.str[argp[idx].data.str.len] = 0;
-                       argp[idx].data.prx = findproxy(trash.str, PR_CAP_FE);
+                       argp[idx].data.prx = proxy_fe_by_name(trash.str);
                        if (!argp[idx].data.prx)
                                WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist"));
                        argp[idx].type = ARGT_FE;
@@ -643,7 +643,7 @@ __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
                                WILL_LJMP(luaL_argerror(L, first + idx, "string expected"));
                        memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len);
                        trash.str[argp[idx].data.str.len] = 0;
-                       argp[idx].data.prx = findproxy(trash.str, PR_CAP_BE);
+                       argp[idx].data.prx = proxy_be_by_name(trash.str);
                        if (!argp[idx].data.prx)
                                WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
                        argp[idx].type = ARGT_BE;
@@ -669,7 +669,7 @@ __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
                        if (sname) {
                                *sname++ = '\0';
                                pname = trash.str;
-                               px = findproxy(pname, PR_CAP_BE);
+                               px = proxy_be_by_name(pname);
                                if (!px)
                                        WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist"));
                        }
index ed3b58262d850622d3f6fba9b6235a2b217b2d31..56af7820b8555fa6e461e607f933ea5df03a38f9 100644 (file)
@@ -106,7 +106,7 @@ int get_backend_server(const char *bk_name, const char *sv_name,
        if (*sv_name == '#')
                sid = atoi(sv_name + 1);
 
-       p = findproxy(bk_name, PR_CAP_BE);
+       p = proxy_be_by_name(bk_name);
        if (bk)
                *bk = p;
        if (!p)
@@ -390,10 +390,11 @@ struct proxy *findproxy_mode(const char *name, int mode, int cap) {
 
 /* Returns a pointer to the proxy matching either name <name>, or id <name> if
  * <name> begins with a '#'. NULL is returned if no match is found, as well as
- * if multiple matches are found (eg: too large capabilities mask).
+ * if multiple matches are found (eg: too large capabilities mask). If <table>
+ * is non-zero, it only considers proxies having a table.
  */
-struct proxy *findproxy(const char *name, int cap) {
-
+struct proxy *proxy_find_by_name(const char *name, int cap, int table)
+{
        struct proxy *curproxy, *target = NULL;
        int pid = -1;
 
@@ -411,6 +412,9 @@ struct proxy *findproxy(const char *name, int cap) {
                        if ((curproxy->cap & cap) != cap)
                                continue;
 
+                       if (table && !curproxy->table.size)
+                               continue;
+
                        if (target)
                                return NULL;
 
@@ -429,6 +433,9 @@ struct proxy *findproxy(const char *name, int cap) {
                        if ((curproxy->cap & cap) != cap)
                                continue;
 
+                       if (table && !curproxy->table.size)
+                               continue;
+
                        if (target)
                                return NULL;
 
index 03540e140afcf57ac6af36abc02f17d99c828c50..3ad2d208103cbb5cd053a7fc7ae331e74377c597 100644 (file)
@@ -1128,7 +1128,7 @@ int smp_resolve_args(struct proxy *p)
                                *sname++ = '\0';
                                pname = arg->data.str.str;
 
-                               px = findproxy(pname, PR_CAP_BE);
+                               px = proxy_be_by_name(pname);
                                if (!px) {
                                        Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
                                              cur->file, cur->line, pname,
@@ -1158,7 +1158,7 @@ int smp_resolve_args(struct proxy *p)
                case ARGT_FE:
                        if (arg->data.str.len) {
                                pname = arg->data.str.str;
-                               px = findproxy(pname, PR_CAP_FE);
+                               px = proxy_fe_by_name(pname);
                        }
 
                        if (!px) {
@@ -1186,7 +1186,7 @@ int smp_resolve_args(struct proxy *p)
                case ARGT_BE:
                        if (arg->data.str.len) {
                                pname = arg->data.str.str;
-                               px = findproxy(pname, PR_CAP_BE);
+                               px = proxy_be_by_name(pname);
                        }
 
                        if (!px) {
index 4d62c715faba232f4035ed5366137a5bbc0061d9..0f9fbe25aa5a3a085f742336b6e1d27ccf589fe1 100644 (file)
@@ -1053,7 +1053,7 @@ static int process_switching_rules(struct stream *s, struct channel *req, int an
                                        struct chunk *tmp = get_trash_chunk();
                                        if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
                                                break;
-                                       backend = findproxy(tmp->str, PR_CAP_BE);
+                                       backend = proxy_be_by_name(tmp->str);
                                        if (!backend)
                                                break;
                                }