MEDIUM: stick-tables: use the sample type names

This patch removes the special stick tables types names and
use the standard sample type names. This avoid the maintainance
of two types and remove the switch/case for matching a sample
type for each stick table type.
This commit is contained in:
Thierry FOURNIER 2015-07-24 08:46:42 +02:00 committed by Willy Tarreau
parent bc8c404449
commit 5d24ebc3d7
5 changed files with 38 additions and 69 deletions

View File

@ -48,7 +48,7 @@ void tcp_res_cont_keywords_register(struct action_kw_list *kw_list);
int smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private);
/* Converts the INET/INET6 source address to a stick_table key usable for table
* lookups. <type> can be STKTABLE_TYPE_IP or STKTABLE_TYPE_IPV6. The function
* lookups. <type> can be SMP_T_IPV4 or SMP_T_IPV6. The function
* try to convert the incoming IP to the type expected by the sticktable.
* Returns either NULL if the source cannot be converted (eg: not IPv4) or a
* pointer to the converted result in static_table_key in the appropriate format
@ -59,12 +59,12 @@ static inline struct stktable_key *addr_to_stktable_key(struct sockaddr_storage
switch (addr->ss_family) {
case AF_INET:
/* Convert IPv4 to IPv4 key. */
if (type == STKTABLE_TYPE_IP) {
if (type == SMP_T_IPV4) {
static_table_key->key = (void *)&((struct sockaddr_in *)addr)->sin_addr;
break;
}
/* Convert IPv4 to IPv6 key. */
if (type == STKTABLE_TYPE_IPV6) {
if (type == SMP_T_IPV6) {
v4tov6(&static_table_key->data.ipv6, &((struct sockaddr_in *)addr)->sin_addr);
static_table_key->key = &static_table_key->data.ipv6;
break;
@ -73,14 +73,14 @@ static inline struct stktable_key *addr_to_stktable_key(struct sockaddr_storage
case AF_INET6:
/* Convert IPv6 to IPv4 key. This conversion can be failed. */
if (type == STKTABLE_TYPE_IP) {
if (type == SMP_T_IPV4) {
if (!v6tov4(&static_table_key->data.ipv4, &((struct sockaddr_in6 *)addr)->sin6_addr))
return NULL;
static_table_key->key = &static_table_key->data.ipv4;
break;
}
/* Convert IPv6 to IPv6 key. */
if (type == STKTABLE_TYPE_IPV6) {
if (type == SMP_T_IPV6) {
static_table_key->key = (void *)&((struct sockaddr_in6 *)addr)->sin6_addr;
break;
}

View File

@ -33,16 +33,6 @@
#include <types/freq_ctr.h>
#include <types/sample.h>
/* stick table key types */
enum {
STKTABLE_TYPE_IP = 0, /* table key is ipv4 */
STKTABLE_TYPE_IPV6, /* table key is ipv6 */
STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
STKTABLE_TYPE_STRING, /* table key is a null terminated string */
STKTABLE_TYPE_BINARY, /* table key is a buffer of data */
STKTABLE_TYPES /* Number of types, must always be last */
};
/* The types of extra data we can store in a stick table */
enum {
STKTABLE_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */

View File

@ -584,20 +584,20 @@ static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_int
chunk_appendf(msg, "%p:", entry);
if (proxy->table.type == STKTABLE_TYPE_IP) {
if (proxy->table.type == SMP_T_IPV4) {
char addr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
chunk_appendf(msg, " key=%s", addr);
}
else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
else if (proxy->table.type == SMP_T_IPV6) {
char addr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
chunk_appendf(msg, " key=%s", addr);
}
else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
else if (proxy->table.type == SMP_T_SINT) {
chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
}
else if (proxy->table.type == STKTABLE_TYPE_STRING) {
else if (proxy->table.type == SMP_T_STR) {
chunk_appendf(msg, " key=");
dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
}
@ -669,15 +669,15 @@ static void stats_sock_table_key_request(struct stream_interface *si, char **arg
}
switch (px->table.type) {
case STKTABLE_TYPE_IP:
case SMP_T_IPV4:
uint32_key = htonl(inetaddr_host(args[4]));
static_table_key->key = &uint32_key;
break;
case STKTABLE_TYPE_IPV6:
case SMP_T_IPV6:
inet_pton(AF_INET6, args[4], ip6_key);
static_table_key->key = &ip6_key;
break;
case STKTABLE_TYPE_INTEGER:
case SMP_T_SINT:
{
char *endptr;
unsigned long val;
@ -695,7 +695,7 @@ static void stats_sock_table_key_request(struct stream_interface *si, char **arg
break;
}
break;
case STKTABLE_TYPE_STRING:
case SMP_T_STR:
static_table_key->key = args[4];
static_table_key->key_len = strlen(args[4]);
break;

View File

@ -262,14 +262,14 @@ static int peer_prepare_updatemsg(struct stksess *ts, struct shared_table *st, c
}
/* encode the key */
if (st->table->type == STKTABLE_TYPE_STRING) {
if (st->table->type == SMP_T_STR) {
int stlen = strlen((char *)ts->key.key);
intencode(stlen, &cursor);
memcpy(cursor, ts->key.key, stlen);
cursor += stlen;
}
else if (st->table->type == STKTABLE_TYPE_INTEGER) {
else if (st->table->type == SMP_T_SINT) {
netinteger = htonl(*((uint32_t *)ts->key.key));
memcpy(cursor, &netinteger, sizeof(netinteger));
cursor += sizeof(netinteger);
@ -1028,7 +1028,7 @@ switchstate:
if (!newts)
goto ignore_msg;
if (st->table->type == STKTABLE_TYPE_STRING) {
if (st->table->type == SMP_T_STR) {
unsigned int to_read, to_store;
to_read = intdecode(&msg_cur, msg_end);
@ -1050,7 +1050,7 @@ switchstate:
newts->key.key[to_store] = 0;
msg_cur += to_read;
}
else if (st->table->type == STKTABLE_TYPE_INTEGER) {
else if (st->table->type == SMP_T_SINT) {
unsigned int netinteger;
if (msg_cur + sizeof(netinteger) > msg_end) {

View File

@ -64,7 +64,7 @@ void stksess_kill(struct stktable *t, struct stksess *ts)
*/
void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
{
if (t->type != STKTABLE_TYPE_STRING)
if (t->type != SMP_T_STR)
memcpy(ts->key.key, key->key, t->key_size);
else {
memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
@ -187,7 +187,7 @@ struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key
{
struct ebmb_node *eb;
if (t->type == STKTABLE_TYPE_STRING)
if (t->type == SMP_T_STR)
eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
else
eb = ebmb_lookup(&t->keys, key->key, t->key_size);
@ -228,7 +228,7 @@ struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
{
struct ebmb_node *eb;
if (t->type == STKTABLE_TYPE_STRING)
if (t->type == SMP_T_STR)
eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
else
eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
@ -413,12 +413,13 @@ int stktable_init(struct stktable *t)
/*
* Configuration keywords of known table types
*/
struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
{ "ipv6", 0, 16 },
{ "integer", 0, 4 },
{ "string", STK_F_CUSTOM_KEYSIZE, 32 },
{ "binary", STK_F_CUSTOM_KEYSIZE, 32 } };
struct stktable_type stktable_types[SMP_TYPES] = {
[SMP_T_SINT] = { "integer", 0, 4 },
[SMP_T_IPV4] = { "ip", 0, 4 },
[SMP_T_IPV6] = { "ipv6", 0, 16 },
[SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
[SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
};
/*
* Parse table type configuration.
@ -427,7 +428,9 @@ struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 },
*/
int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
{
for (*type = 0; *type < STKTABLE_TYPES; (*type)++) {
for (*type = 0; *type < SMP_TYPES; (*type)++) {
if (!stktable_types[*type].kw)
continue;
if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
continue;
@ -440,7 +443,7 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke
*key_size = atol(args[*myidx]);
if (!*key_size)
break;
if (*type == STKTABLE_TYPE_STRING) {
if (*type == SMP_T_STR) {
/* null terminated string needs +1 for '\0'. */
(*key_size)++;
}
@ -459,37 +462,24 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke
*/
struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
{
int type;
/* Map stick table type to sample types. */
switch (t->type) {
case STKTABLE_TYPE_IP: type = SMP_T_IPV4; break;
case STKTABLE_TYPE_IPV6: type = SMP_T_IPV6; break;
case STKTABLE_TYPE_INTEGER: type = SMP_T_SINT; break;
case STKTABLE_TYPE_STRING: type = SMP_T_STR; break;
case STKTABLE_TYPE_BINARY: type = SMP_T_BIN; break;
default: /* impossible case. */
return NULL;
}
/* Convert sample. */
if (!sample_convert(smp, type))
if (!sample_convert(smp, t->type))
return NULL;
/* Fill static_table_key. */
switch (t->type) {
case STKTABLE_TYPE_IP:
case SMP_T_IPV4:
static_table_key->key = &smp->data.u.ipv4;
static_table_key->key_len = 4;
break;
case STKTABLE_TYPE_IPV6:
case SMP_T_IPV6:
static_table_key->key = &smp->data.u.ipv6;
static_table_key->key_len = 16;
break;
case STKTABLE_TYPE_INTEGER:
case SMP_T_SINT:
/* The stick table require a 32bit unsigned int, "sint" is a
* signed 64 it, so we can convert it inplace.
*/
@ -498,7 +488,7 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
static_table_key->key_len = 4;
break;
case STKTABLE_TYPE_STRING:
case SMP_T_STR:
/* Must be NULL terminated. */
if (smp->data.u.str.len >= smp->data.u.str.size ||
smp->data.u.str.str[smp->data.u.str.len] != '\0') {
@ -512,7 +502,7 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
static_table_key->key_len = smp->data.u.str.len;
break;
case STKTABLE_TYPE_BINARY:
case SMP_T_BIN:
if (smp->data.u.str.len < t->key_size) {
/* This type needs padding with 0. */
if (smp->data.u.str.size < t->key_size)
@ -577,22 +567,11 @@ int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_typ
{
int out_type;
if (table_type >= STKTABLE_TYPES)
if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
return 0;
out_type = smp_expr_output_type(expr);
/* Map stick table type to sample types. */
switch (table_type) {
case STKTABLE_TYPE_IP: table_type = SMP_T_IPV4; break;
case STKTABLE_TYPE_IPV6: table_type = SMP_T_IPV6; break;
case STKTABLE_TYPE_INTEGER: table_type = SMP_T_SINT; break;
case STKTABLE_TYPE_STRING: table_type = SMP_T_STR; break;
case STKTABLE_TYPE_BINARY: table_type = SMP_T_BIN; break;
default: /* impossible case. */
return 0;
}
/* Convert sample. */
if (!sample_casts[out_type][table_type])
return 0;