From 9e0bb1013eae4e63808643a6019b095ff0445a3c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 26 May 2015 11:24:42 +0200 Subject: [PATCH] CLEANUP: proxy: make the proxy lookup functions more user-friendly 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 | 26 +++++++++++++++++++++++++- src/cfgparse.c | 12 ++++++------ src/dumpstats.c | 4 ++-- src/hlua.c | 6 +++--- src/proxy.c | 15 +++++++++++---- src/sample.c | 6 +++--- src/stream.c | 2 +- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/include/proto/proxy.h b/include/proto/proxy.h index 139791f8c9..07ccc9650c 100644 --- a/include/proto/proxy.h +++ b/include/proto/proxy.h @@ -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 . 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 . 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 . 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) { diff --git a/src/cfgparse.c b/src/cfgparse.c index d4fac8cdf9..84420d11f7 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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 @@ int check_config_validity() } 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, diff --git a/src/dumpstats.c b/src/dumpstats.c index 885a15939c..559f229652 100644 --- a/src/dumpstats.c +++ b/src/dumpstats.c @@ -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; diff --git a/src/hlua.c b/src/hlua.c index a937e4f079..5e1bdc3227 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -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")); } diff --git a/src/proxy.c b/src/proxy.c index ed3b58262d..56af7820b8 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -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 , or id if * 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 + * 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; diff --git a/src/sample.c b/src/sample.c index 03540e140a..3ad2d20810 100644 --- a/src/sample.c +++ b/src/sample.c @@ -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) { diff --git a/src/stream.c b/src/stream.c index 4d62c715fa..0f9fbe25aa 100644 --- a/src/stream.c +++ b/src/stream.c @@ -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; }