MEDIUM: stick-table: implement lookup from a sample fetch

Currently we have stktable_fetch_key() which fetches a sample according
to an expression and returns a stick table key, but we also need a function
which does only the second half of it from a known sample. So let's cut the
function in two and introduce smp_to_stkey() to perform this lookup. The
first function was adapted to make use of it in order to avoid code
duplication.
This commit is contained in:
Willy Tarreau 2014-07-03 17:02:46 +02:00
parent ffcb2e4b42
commit 8fed9037cd
2 changed files with 30 additions and 19 deletions

View File

@ -46,6 +46,7 @@ struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local
struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts);
struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key);
struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key);
struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t);
struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px,
struct session *l4, void *l7, unsigned int opt,
struct sample_expr *expr, struct sample *smp);

View File

@ -597,27 +597,13 @@ static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = {
};
/*
* Process a fetch + format conversion as defined by the sample expression <expr>
* on request or response considering the <opt> parameter. Returns either NULL if
* no key could be extracted, or a pointer to the converted result stored in
* static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
* and its flags will be initialized so that the caller gets a copy of the input
* sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present).
/* Prepares a stktable_key from a sample <smp> to search into table <t>.
* Returns NULL if the sample could not be converted (eg: no matching type),
* otherwise a pointer to the static stktable_key filled with what is needed
* for the lookup.
*/
struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7,
unsigned int opt, struct sample_expr *expr, struct sample *smp)
struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
{
if (smp)
memset(smp, 0, sizeof(*smp));
smp = sample_process(px, l4, l7, opt, expr, smp);
if (!smp)
return NULL;
if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
return NULL; /* we can only use stable samples */
if (!sample_to_key[smp->type][t->type])
return NULL;
@ -659,6 +645,30 @@ struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, st
return static_table_key;
}
/*
* Process a fetch + format conversion as defined by the sample expression <expr>
* on request or response considering the <opt> parameter. Returns either NULL if
* no key could be extracted, or a pointer to the converted result stored in
* static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
* and its flags will be initialized so that the caller gets a copy of the input
* sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present).
*/
struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7,
unsigned int opt, struct sample_expr *expr, struct sample *smp)
{
if (smp)
memset(smp, 0, sizeof(*smp));
smp = sample_process(px, l4, l7, opt, expr, smp);
if (!smp)
return NULL;
if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
return NULL; /* we can only use stable samples */
return smp_to_stkey(smp, t);
}
/*
* Returns 1 if sample expression <expr> result can be converted to table key of
* type <table_type>, otherwise zero. Used in configuration check.