netfilter: ipvs: Add Maglev consistent hashing scheduler
Subsystems (3)
cover netfilter: ipvs: Add Maglev consistent hashing scheduler
Implements the Google's Maglev hashing algorithm as a IPVS scheduler. Basically it provides consistent hashing but offers some special features about disruption and load balancing.
Seel also: [3.4 Consistent Hasing]
in https://static.googleusercontent.com/media/research.google.com/ko//pubs/archive/44824.pdf
V2: Fix some methods and add features based on Julian's advice.
Inju Song (3):
netfilter: ipvs: handle memory allocation fail in mh scheduler.
netfilter: ipvs: release refcnt and improve ip_vs_mh_reassign()
netfilter: ipvs: change method to fill lookup table based on weight.
net/netfilter/ipvs/ip_vs_mh.c | 147 +++++++++++++++++++++++++++++------------- 1 file changed, 101 insertions(+), 46 deletions(-)
Signature · 6 lines
1.8.3.1 -- Inju Song NAVER Corporation
PATCH 1/3 netfilter: ipvs: handle memory allocation fail in mh scheduler.
1) change svc->num_dests to fixed size when allocate memory.
2) return -ENOMEM if memory allocation is faild(non-failing methods)
--- net/netfilter/ipvs/ip_vs_mh.c | 101 +++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 36 deletions(-)
net/netfilter/ipvs/ip_vs_mh.c +65 -36
@@ -19,11 +19,17 @@ struct ip_vs_mh_lookup { };/* for IPVS MH entry hash table */ -#define IP_VS_MH_LOOKUP_SIZE 65537/* Must be prime number */ +#ifndef CONFIG_IP_VS_MH_TAB_BITS +#define CONFIG_IP_VS_MH_TAB_BITS 8 +#endif +#define IP_VS_MH_TAB_BITS CONFIG_IP_VS_MH_TAB_BITS +#define IP_VS_MH_TAB_SIZE BIT(IP_VS_MH_TAB_BITS) +#define IP_VS_MH_LOOKUP_SIZE 65537/* Must be prime number */ struct ip_vs_mh_state {struct rcu_head rcu_head;struct ip_vs_mh_lookup lookup[IP_VS_MH_LOOKUP_SIZE ]; +unsigned int **permutation;hsiphash_key_t hash1, hash2; };
@@ -61,25 +67,21 @@ static inline bool is_unavailable(struct ip_vs_dest *dest)return hsiphash (&v,sizeof (v), key); } -static inline unsigned int ** -ip_vs_mh_permutate (struct ip_vs_mh_state *s,struct ip_vs_service *svc) +static inline int ip_vs_mh_permutate (struct ip_vs_mh_state *s, +struct ip_vs_service *svc) {int i, j; -unsigned int **permutation;struct list_head *p;struct ip_vs_dest *dest;unsigned int offset, skip; -int dcnt; - dcnt = svc->num_dests; - permutation =kcalloc (dcnt,sizeof (unsigned int *),GFP_KERNEL ); - permutation[0 ] =kcalloc (dcnt *IP_VS_MH_LOOKUP_SIZE , -sizeof (unsigned int ),GFP_KERNEL ); -for (i =1 ; i < dcnt; i++) - permutation[i] = permutation[i -1 ] +IP_VS_MH_LOOKUP_SIZE ; +/* extending permutation table to 2d arrays */ +for (i =1 ; i < svc->num_dests; i++) + s->permutation[i] = s->permutation[i -1 ] + +IP_VS_MH_LOOKUP_SIZE ; - p = &svc->destinations; i =0 ; + p = &svc->destinations;while ((p = p->next) != &svc->destinations) { dest = list_entry(p, struct ip_vs_dest, n_list); offset = ip_vs_mh_hashkey(svc->af, &dest->addr, dest->port,
@@ -89,44 +91,53 @@ static inline bool is_unavailable(struct ip_vs_dest *dest) (IP_VS_MH_LOOKUP_SIZE -1 ) +1 ;for (j =0 ; j <IP_VS_MH_LOOKUP_SIZE ; j++) { - permutation[i][j] = (offset + (j * skip)) % + s->permutation[i][j] = (offset + (j * skip)) %IP_VS_MH_LOOKUP_SIZE ; } i++; } -return permutation; +return 0 ; }static inline int -ip_vs_mh_populate (struct ip_vs_mh_state *s,struct ip_vs_service *svc, -unsigned int **permutation) +ip_vs_mh_populate (struct ip_vs_mh_state *s,struct ip_vs_service *svc) { -int i; +int i, ret;unsigned int *next; +unsigned int **pmt;struct ip_vs_mh_lookup *entry, *l;struct list_head *p;struct ip_vs_dest *dest; -int dcnt;unsigned int n, c; - dcnt = svc->num_dests; - next =kcalloc (dcnt,sizeof (unsigned int ),GFP_KERNEL ); + ret =0 ; + next =kcalloc (IP_VS_MH_TAB_SIZE ,sizeof (unsigned int ), +GFP_KERNEL ); +if (!next) +return -ENOMEM ; + entry =kcalloc (IP_VS_MH_LOOKUP_SIZE ,sizeof (*entry),GFP_KERNEL ); +if (!entry) { + ret = -ENOMEM ; + goto err_alloc; + } +for (i =0 ; i <IP_VS_MH_LOOKUP_SIZE ; i++)RCU_INIT_POINTER (entry[i].dest,NULL ); n =0 ; + pmt = s->permutation;while (n <IP_VS_MH_LOOKUP_SIZE ) { p = &svc->destinations; -for (i =0 ; i < dcnt; i++) { +for (i =0 ; i < svc->num_dests; i++) { p = p->next; - c = permutation[i][next[i]]; + c = pmt[i][next[i]];while (entry[c].dest) { next[i] = next[i] +1 ; - c = permutation[i][next[i]]; + c = pmt[i][next[i]]; } dest = list_entry(p, struct ip_vs_dest, n_list);
@@ -152,10 +163,11 @@ static inline bool is_unavailable(struct ip_vs_dest *dest)RCU_INIT_POINTER (entry[i].dest,NULL ); l++; } -kfree (next); -kfree (entry); -return 0 ; +kfree (entry); +err_alloc: +kfree (next); +return ret; }/* Get ip_vs_dest associated with supplied parameters. */
@@ -236,24 +248,41 @@ static void ip_vs_mh_flush(struct ip_vs_mh_state *s)static int ip_vs_mh_reassign (struct ip_vs_mh_state *s,struct ip_vs_service *svc) { -int dcnt; -unsigned int **permutation; +int ret;/* flush all the hash entry before assigning mh entry */ ip_vs_mh_flush (s); -/* if destination number is zero, skip mh assign */ - dcnt = svc->num_dests; -if (dcnt <=0 ) -return 0 ; +/* ip_vs_mh_reassign is responsible for assigning +* and releasing s->permutation table +*/ + s->permutation =kcalloc (IP_VS_MH_TAB_SIZE ,sizeof (unsigned int *), +GFP_KERNEL ); +if (!s->permutation) +return -ENOMEM ; - permutation =ip_vs_mh_permutate (s, svc); -ip_vs_mh_populate (s, svc, permutation); + s->permutation[0 ] =kcalloc (IP_VS_MH_TAB_SIZE *IP_VS_MH_LOOKUP_SIZE , +sizeof (unsigned int ),GFP_KERNEL ); +if (!s->permutation[0 ]) { + ret = -ENOMEM ; + goto err_alloc; + } -kfree (permutation[0 ]); -kfree (permutation); + ret =ip_vs_mh_permutate (s, svc); +if (ret <0 ) + goto err_out; + + ret =ip_vs_mh_populate (s, svc); +if (ret <0 ) + goto err_out;return 0 ; + +err_out: +kfree (s->permutation[0 ]); +err_alloc: +kfree (s->permutation); +return ret; }static int ip_vs_mh_init_svc (struct ip_vs_service *svc)
Signature · 6 lines
1.8.3.1 -- Inju Song NAVER Corporation
PATCH 2/3 netfilter: ipvs: release refcnt and improve ip_vs_mh_reassign()
In reply to Inju Song1) release refcnt of old dests in lookup entry
2) handle exception about svc->num_dests == 0
- skip permutate and populate if dest list is empty
3) change allocation method
- in case of allocating permutate array, use vmalloc() instead of kalloc()
--- net/netfilter/ipvs/ip_vs_mh.c | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-)
net/netfilter/ipvs/ip_vs_mh.c +39 -19
@@ -5,6 +5,7 @@#include <linux/slab.h> #include <linux/module.h> #include <linux/kernel.h> +#include <linux/vmalloc.h> #include <linux/skbuff.h> #include <net/ip_vs.h>
@@ -74,6 +75,12 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,struct list_head *p;struct ip_vs_dest *dest;unsigned int offset, skip; +bool empty; + + p = &svc->destinations; + empty =list_empty (p); +if (empty) +return 0 ;/* extending permutation table to 2d arrays */ for (i =1 ; i < svc->num_dests; i++)
@@ -81,7 +88,6 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,IP_VS_MH_LOOKUP_SIZE ; i =0 ; - p = &svc->destinations;while ((p = p->next) != &svc->destinations) { dest = list_entry(p, struct ip_vs_dest, n_list); offset = ip_vs_mh_hashkey(svc->af, &dest->addr, dest->port,
@@ -110,6 +116,7 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,struct list_head *p;struct ip_vs_dest *dest;unsigned int n, c; +bool empty; ret =0 ; next = kcalloc(IP_VS_MH_TAB_SIZE, sizeof(unsigned int),
@@ -128,11 +135,17 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,RCU_INIT_POINTER (entry[i].dest,NULL ); n =0 ; + p = &svc->destinations; + empty =list_empty (p); +if (empty) + goto out; + pmt = s->permutation;while (n <IP_VS_MH_LOOKUP_SIZE ) { - p = &svc->destinations;for (i =0 ; i < svc->num_dests; i++) { - p = p->next; +if (p == &svc->destinations) + p = p->next; + c = pmt[i][next[i]];while (entry[c].dest) {
@@ -147,20 +160,29 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s, n++;if (n ==IP_VS_MH_LOOKUP_SIZE )break ; + + p = p->next; } } +out: l = &s->lookup[0 ];for (i =0 ; i <IP_VS_MH_LOOKUP_SIZE ; i++) { dest =rcu_dereference_protected (entry[i].dest,1 ); -ip_vs_dest_hold (dest); -RCU_INIT_POINTER (l->dest, dest); +if (dest) +ip_vs_dest_put (dest); +if (empty) { +RCU_INIT_POINTER (l->dest,NULL ); + }else { +ip_vs_dest_hold (dest); +RCU_INIT_POINTER (l->dest, dest); -IP_VS_DBG_BUF (6 ,"assigned i: %d dest: %s weight: %d\n" , - i,IP_VS_DBG_ADDR (dest->af, &dest->addr), -atomic_read (&dest->weight)); +IP_VS_DBG_BUF (6 ,"assigned i: %d dest: %s weight: %d\n" , + i,IP_VS_DBG_ADDR (dest->af, &dest->addr), +atomic_read (&dest->weight)); -RCU_INIT_POINTER (entry[i].dest,NULL ); +RCU_INIT_POINTER (entry[i].dest,NULL ); + } l++; }
@@ -250,19 +272,15 @@ static void ip_vs_mh_flush(struct ip_vs_mh_state *s) {int ret; -/* flush all the hash entry before assigning mh entry */ -ip_vs_mh_flush (s); -/* ip_vs_mh_reassign is responsible for assigning * and releasing s->permutation table */ - s->permutation =kcalloc (IP_VS_MH_TAB_SIZE ,sizeof (unsigned int *), -GFP_KERNEL ); + s->permutation =vzalloc (IP_VS_MH_TAB_SIZE *sizeof (unsigned int *));if (!s->permutation)return -ENOMEM ; - s->permutation[0 ] =kcalloc (IP_VS_MH_TAB_SIZE *IP_VS_MH_LOOKUP_SIZE , -sizeof (unsigned int ),GFP_KERNEL ); + s->permutation[0 ] =vzalloc (IP_VS_MH_TAB_SIZE *IP_VS_MH_LOOKUP_SIZE * +sizeof (unsigned int ));if (!s->permutation[0 ]) { ret = -ENOMEM ; goto err_alloc;
@@ -276,12 +294,14 @@ static void ip_vs_mh_flush(struct ip_vs_mh_state *s)if (ret <0 ) goto err_out; -return 0 ; +IP_VS_DBG_BUF (6 ,"MH: reassign lookup table of %s:%d\n" , +IP_VS_DBG_ADDR (svc->af, &svc->addr), +ntohs (svc->port)); err_out: -kfree (s->permutation[0 ]); +vfree (s->permutation[0 ]); err_alloc: -kfree (s->permutation); +vfree (s->permutation);return ret; }
Signature · 6 lines
1.8.3.1 -- Inju Song NAVER Corporation
PATCH 3/3 netfilter: ipvs: change method to fill lookup table based on weight.
In reply to Inju Song Fill lookup table with dests based on their weight.
- They are assigned by considering weight as the relative frequency of the backends’ turn.
--- net/netfilter/ipvs/ip_vs_mh.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
net/netfilter/ipvs/ip_vs_mh.c +12 -6
@@ -109,7 +109,7 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,static inline int ip_vs_mh_populate (struct ip_vs_mh_state *s,struct ip_vs_service *svc) { -int i, ret; +int i, ret, dw_count;unsigned int *next;unsigned int **pmt;struct ip_vs_mh_lookup *entry, *l;
@@ -134,18 +134,20 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,for (i =0 ; i <IP_VS_MH_LOOKUP_SIZE ; i++)RCU_INIT_POINTER (entry[i].dest,NULL ); - n =0 ; p = &svc->destinations; empty =list_empty (p);if (empty) goto out; + n =0 ; + dw_count =0 ; pmt = s->permutation;while (n <IP_VS_MH_LOOKUP_SIZE ) { -for (i =0 ; i < svc->num_dests; i++) { -if (p == &svc->destinations) - p = p->next; +if (p == &svc->destinations) + p = p->next; + i =0 ; +while (p != &svc->destinations) { c = pmt[i][next[i]];while (entry[c].dest) {
@@ -161,7 +163,11 @@ static inline int ip_vs_mh_permutate(struct ip_vs_mh_state *s,if (n ==IP_VS_MH_LOOKUP_SIZE )break ; - p = p->next; +if (++dw_count >=atomic_read (&dest->weight)) { + p = p->next; + dw_count =0 ; + i++; + } } }
Signature · 6 lines
1.8.3.1 -- Inju Song NAVER Corporation
Julian Anastasov
maintainerHello,
Quoted 8 lines · Inju Song
Implements the Google's Maglev hashing algorithm as a IPVS scheduler. Basically it provides consistent hashing but offers some special features about disruption and load balancing.
Seel also: [3.4 Consistent Hasing]
in https://static.googleusercontent.com/media/research.google.com/ko//pubs/archive/44824.pdfV2: Fix some methods and add features based on Julian's advice.
It is good to see incremental changes but at the final
step it is better to see single patch with a well-formed subject, with new version, eg:
[PATCHv2 nf-next] ipvs: Add Maglev consistent hashing
where the used tree is net-next, nf-next or ipvs-next.
Few notes:
- .upd_dest: changing weight to 0 should be ignored but if we want to allow changing the weight from/to non-0 values then we should save in array the latest non-0 weights for the current dests. As it is difficult to track the dest deletions may be we can add last_weight in struct ip_vs_dest. Then MH will work with dest->last_weight.
- it is good to have configurable table size, just like IP_VS_SH_TAB_BITS in Kconfig. You can even use 5-6 prime numbers like 65537 and below, if needed. Large value is not so fatal, table is updated only when servers are added (at start), so if one uses little differences in weights and small number of servers the small prime number can reduce the cache usage. As we know, the table should be able to map all possible dests based on relative weights. To give few examples:
- for 2 servers with same weight 5 we need 2 table entries, one per server
- for 2 servers with weight 5 and 10 we need 3 entries, 1 for first server and 2 for the second server
- But in practice we can run hundreds of servers with relative weight difference 1-64 if number of CPUs are considered, for example.
- To avoid waste of memory due to kmalloc's orders we can use the previous prime number, 65521, others can be 32749, 16381, 8191, 4093, 2039, 1021, some no so useful values due to page size:
- 509 (2KB with 4-byte ptr, 4KB for 8-byte ptr)
- 251 (1KB/2KB of 4KB page)
With such primes the table should be a separate allocation.
- when table has constant size, it can be part of the state, as result, it will be allocated only once by ip_vs_mh_init_svc where any errors are handled properly, any reallocations in ip_vs_mh_reassign are unwanted because currently the errors are ignored by the IPVS core (add_dest/upd_dest/del_dest), so we should keep the s->permutation[] allocated. But as it is too large, see below how to avoid s->permutation[]...
- no need to use "inline" for ip_vs_mh_permutate and ip_vs_mh_populate, they are used only on configuration time, better the compiler to decide
- another possible issue is that users can use crazy values for weight, for example, 64 and 127. In such case even the gcd() function can not help enough for something that would be 1:2 relation. But we can warn users to use as small as possible values for weights. If needed, we can rshift the weights, so that only the highest 5-6 bits of the largest weight are considered. I.e. if we find the largest weight is 2000 we see that 2000 occupies 11 bits, so we decide to use the highest 6 bits and rshift with 5 (11-6) all weights. All resulting weights will fit in 6 bits.
- ip_vs_mh_populate allocates IP_VS_MH_TAB_SIZE ints for next[] but later next[i] can go up to num_dests which can be above the IP_VS_MH_TAB_SIZE. We should use IP_VS_MH_TAB_SIZE as M and Kconfig should show the above prime numbers as options. Or we can configure IP_VS_MH_TAB_BITS as 8..16 and to use it as index to get the actual prime value from array:
static int primes[] = { 251, ... 65521 };
- we can avoid large allocations for s->permutation(N*M), instead we can use:
struct ip_vs_mh_dest_setup {
int offset; /* Starting offset */
int skip; /* skip */
int turns; /* weight / gcd() */
int perm; /* next_offset */
};struct ip_vs_mh_dest_setup *dests; /* 1..num_dests */
dests should be allocated (-ENOMEM) and freed on
every ip_vs_mh_reassign.
Initially, we should calculate .offset and .skip for all
dests. Then we should set dests[i].perm to dests[i].offset. When adding weight to the picture, we can calculate greatest common divisor with the gcd() function and then to use it for calculating 'turns' per dest. ip_vs_wrr_gcd_weight() is a good example how to get gcd() among all dests.
When populating table:
The operation 'c = permutation[i][next[i]];' becomes
c = dests[i].perm;
The operation 'next[i] = next[i] + 1;' becomes:
/* Add skip, mod M */ dests[i].perm += dests[i].skip; if (dests[i].perm >= M) dests[i].perm -= M; This should be cheaper than '% 65537'. And it is correct
because:
1. offset is 0 .. M-1
2. skip is 1 .. M-1
- Lets do not put limit for number of dests. We can add only such check:
if (svc->num_dest > IP_VS_MH_LOOKUP_SIZE) return -EINVAL;
- IP_VS_MH_LOOKUP_SIZE should be replaced with IP_VS_MH_TAB_SIZE
Note that I only check the implementation for problems,
I don't run any tests for the algorithm. I hope you have some test program that pushes bunch of random IP[:PORT] values via the algorithm and the result is correct distribution based on the relative weights.
Quoted 7 lines
Inju Song (3):
netfilter: ipvs: handle memory allocation fail in mh scheduler.
netfilter: ipvs: release refcnt and improveip_vs_mh_reassign()
netfilter: ipvs: change method to fill lookup table based on weight.net/netfilter/ipvs/ip_vs_mh.c | 147 +++++++++++++++++++++++++++++------------- 1 file changed, 101 insertions(+), 46 deletions(-)
Regards
--
Julian Anastasov <[email protected]>
Quoted 12 lines · Julian Anastasov
Hello,
Quoted 7 lines · Inju Song
Implements the Google's Maglev hashing algorithm as a IPVS scheduler. Basically it provides consistent hashing but offers some special features about disruption and load balancing.
Seel also: [3.4 Consistent Hasing]
in https://static.googleusercontent.com/media/research.google.com/ko//pubs/archive/44824.pdf
Quoted 8 lines
Quoted 1 line
V2: Fix some methods and add features based on Julian's advice.
It is good to see incremental changes but at the final
step it is better to see single patch with a well-formed subject, with new version, eg:[PATCHv2 nf-next] ipvs: Add Maglev consistent hashing
yes. I will make next maglev patch as single but maybe
there will be some patchsets for v2 If it needed(ie. add some config to Kconfig).
Quoted 11 lines
where the used tree is net-next, nf-next or ipvs-next.
Few notes:
- .upd_dest: changing weight to 0 should be ignored but if we want to allow changing the weight from/to non-0 values then we should save in array the latest non-0 weights for the current dests. As it is difficult to track the dest deletions may be we can add
last_weightin structip_vs_dest. Then MH will work withdest->last_weight.
I think that It needs to allow changing the weight from/to
non-0 values so I will add to allow only if is non-0 values.
Quoted 19 lines
- it is good to have configurable table size, just like
IP_VS_SH_TAB_BITSinKconfig. You can even use 5-6 prime numbers like 65537 and below, if needed. Large value is not so fatal, table is updated only when servers are added (at start), so if one uses little differences in weights and small number of servers the small prime number can reduce the cache usage. As we know, the table should be able to map all possible dests based on relative weights. To give few examples:- for 2 servers with same weight 5 we need 2 table entries, one per server
- for 2 servers with weight 5 and 10 we need 3 entries, 1 for first server and 2 for the second server
- But in practice we can run hundreds of servers with relative weight difference 1-64 if number of CPUs are considered, for example.
Quoted 9 lines
- To avoid waste of memory due to kmalloc's orders we can use the previous prime number, 65521, others can be 32749, 16381, 8191, 4093, 2039, 1021, some no so useful values due to page size:
- 509 (2KB with 4-byte ptr, 4KB for 8-byte ptr)
- 251 (1KB/2KB of 4KB page)With such primes the table should be a separate allocation.
I agress this. To avoid waste of memory and provide various
prime number, it is saved with static array like below.
// available prime number
static int PRIMES[] =
{ 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
I want to proivde larger space then 65521, so add 131071 to
last index of primes. If large value is not so fatal I think it is better in case of disruption of consistent hashing. I think It is helpful for users who have high performance machine and want more minimal disruption when destinations are add/deleted.
Quoted 11 lines
- when table has constant size, it can be part of the state, as result, it will be allocated only once by
ip_vs_mh_init_svcwhere any errors are handled properly, any reallocations inip_vs_mh_reassignare unwanted because currently the errors are ignored by the IPVS core (add_dest/upd_dest/del_dest), so we should keep the s->permutation[] allocated. But as it is too large, see below how to avoid s->permutation[]...- no need to use "inline" for
ip_vs_mh_permutateandip_vs_mh_populate, they are used only on configuration time, better the compiler to decide
Yes. I will remove it from ip_vs_mh_permutate and ip_vs_mh_populate.
Quoted 11 lines
- another possible issue is that users can use crazy values for weight, for example, 64 and 127. In such case even the
gcd()function can not help enough for something that would be 1:2 relation. But we can warn users to use as small as possible values for weights. If needed, we can rshift the weights, so that only the highest 5-6 bits of the largest weight are considered. I.e. if we find the largest weight is 2000 we see that 2000 occupies 11 bits, so we decide to use the highest 6 bits and rshift with 5 (11-6) all weights. All resulting weights will fit in 6 bits.
I agree that huge weight values are considerd and rshift is
good idea I think.
To handle it, both rshift and last_weight are considerd. I
wrote my opinion below.
Quoted 9 lines
-
ip_vs_mh_populateallocatesIP_VS_MH_TAB_SIZEints for next[] but later next[i] can go up tonum_destswhich can be above theIP_VS_MH_TAB_SIZE. We should useIP_VS_MH_TAB_SIZEas M andKconfigshould show the above prime numbers as options. Or we can configureIP_VS_MH_TAB_BITSas 8..16 and to use it as index to get the actual prime value from array:static int primes[] = { 251, ... 65521 };
I will add IP_VS_MH_BITS and help message in Kconfig to able to
configure it like below.
config IP_VS_MH_TAB_INDEX range 8..16 default 12 // and some helpful messages.
and
#define IP_VS_MH_TAB_INDEX CONFIG_IP_VS_MH_TAB_INDEX - 8 #define IP_VS_MH_TAB_SIZE PRIMES[IP_VS_MH_TAB_INDEX]
in header of ip_vs_mh.c
Quoted 36 lines
- we can avoid large allocations for
s->permutation(N*M), instead we can use:struct ip_vs_mh_dest_setup { int offset; /* Starting offset */ int skip; /* skip */ int turns; /* weight / gcd() */ int perm; /* next_offset */ };struct
ip_vs_mh_dest_setup*dests; /* 1..num_dests */dests should be allocated (
-ENOMEM) and freed on
everyip_vs_mh_reassign.Initially, we should calculate .offset and .skip for all
dests. Then we should set dests[i].perm to dests[i].offset. When adding weight to the picture, we can calculate greatest common divisor with thegcd()function and then to use it for calculating 'turns' per dest.ip_vs_wrr_gcd_weight()is a good example how to getgcd()among all dests.When populating table:
The operation 'c = permutation[i][next[i]];' becomes
c = dests[i].perm;
The operation 'next[i] = next[i] + 1;' becomes:
/* Add skip, mod M */ dests[i].perm += dests[i].skip; if (dests[i].perm >= M) dests[i].perm -= M; This should be cheaper than '% 65537'. And it is correct
Quoted 4 lines
because:
1. offset is 0 .. M-1
2. skip is 1 .. M-1
This is very good idea I think. It shoud be used so I will
change ip_vs_mh_permutate and ip_vs_mh_populate to avoid allocating s->permutation.
And re-configuring weight of dests maybe have some flow inip_vs_init_svc and ip_vs_mh_ dest_changed like below.
1. check max weight, decide value of bits range for rshift and set last weigit to dests->last_weight with rshift.
- last_weight should be set only when it is changed.
- If weight is zero, skip to set last_weight
2. get gcd with all dests
- maybe gcd() will use dest->last_weight value not dest->weight.
3. set offset, skip and turns in ip_vs_mh_permutate
dests[i].tunrs = dest->last_weight / gcd;
4. use relative weight in ip_vs_mh_populate
if (++dt_count >= dests[i].turns) {
p = p -> next;
dt_count = 0;
i++;
}
and I think that the configuring weight flow need to be protectedby spin_lock.
Quoted 6 lines
- Lets do not put limit for number of dests. We can add only such check:
if (svc->num_dest > IP_VS_MH_LOOKUP_SIZE) return -EINVAL;
yes. I will add such check.
Quoted 2 lines
-
IP_VS_MH_LOOKUP_SIZEshould be replaced withIP_VS_MH_TAB_SIZE
It is will be replaced with IP_VS_MH_TAB_SIZE.
Quoted 6 lines
Note that I only check the implementation for problems,
I don't run any tests for the algorithm. I hope you have some test program that pushes bunch of random IP[:PORT] values via the algorithm and the result is correct distribution based on the relative weights.
I alredy have some a kind of test program with destination as
container and always run it when I add some features and make patches. so I will attach some result about correct distribtion at next patch if needed.
Quoted 5 lines
Quoted 5 lines
Inju Song (3):
netfilter: ipvs: handle memory allocation fail in mh scheduler.
netfilter: ipvs: release refcnt and improveip_vs_mh_reassign()
netfilter: ipvs: change method to fill lookup table based on weight.
Quoted 7 lines
Quoted 2 lines
net/netfilter/ipvs/ip_vs_mh.c | 147 +++++++++++++++++++++++++++++------------- 1 file changed, 101 insertions(+), 46 deletions(-)Regards
--
Julian Anastasov <[email protected]>
Regards
Signature · 2 lines
Inju Song NAVER Corporation
Julian Anastasov
maintainerHello,
Quoted 5 lines · Inju Song
Quoted 1 line · Julian Anastasov
yes. I will make next maglev patch as single but maybe
there will be some patchsets for v2 If it needed(ie. add some config toKconfig).
Yes, Kconfig, Makefile should be in same patch.
Quoted 10 lines
Quoted 7 lines
- .upd_dest: changing weight to 0 should be ignored but if we want to allow changing the weight from/to non-0 values then we should save in array the latest non-0 weights for the current dests. As it is difficult to track the dest deletions may be we can add
last_weightin structip_vs_dest. Then MH will work withdest->last_weight.I think that It needs to allow changing the weight from/to
non-0 values so I will add to allow only if is non-0 values.
Yep, then we should catch .upd_dest and to use last_weight.
In fact, adding last_weight to IPVS core should be separate patch to apply before the MH patch.
Quoted 7 lines
Quoted 7 lines
- To avoid waste of memory due to kmalloc's orders we can use the previous prime number, 65521, others can be 32749, 16381, 8191, 4093, 2039, 1021, some no so useful values due to page size:
- 509 (2KB with 4-byte ptr, 4KB for 8-byte ptr)
- 251 (1KB/2KB of 4KB page)
Quoted 8 lines
Quoted 2 lines
With such primes the table should be a separate allocation.
I agress this. To avoid waste of memory and provide various
prime number, it is saved with static array like below.// available prime number
static int PRIMES[] =
Do not use upper case for var names...
Quoted 7 lines
{ 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
I want to proivde larger space then 65521, so add 131071 to
last index of primes. If large value is not so fatal I think it is better in case of disruption of consistent hashing. I think It is helpful for users who have high performance machine and want more minimal disruption when destinations are add/deleted.
OK, may be 131071 can help if using hundreds of servers...
Quoted 24 lines
Quoted 11 lines
- another possible issue is that users can use crazy values for weight, for example, 64 and 127. In such case even the
gcd()function can not help enough for something that would be 1:2 relation. But we can warn users to use as small as possible values for weights. If needed, we can rshift the weights, so that only the highest 5-6 bits of the largest weight are considered. I.e. if we find the largest weight is 2000 we see that 2000 occupies 11 bits, so we decide to use the highest 6 bits and rshift with 5 (11-6) all weights. All resulting weights will fit in 6 bits.I agree that huge weight values are considerd and rshift is
good idea I think.
To handle it, both rshift andlast_weightare considerd. I
wrote my opinion below.Quoted 7 lines
-
ip_vs_mh_populateallocatesIP_VS_MH_TAB_SIZEints for next[] but later next[i] can go up tonum_destswhich can be above theIP_VS_MH_TAB_SIZE. We should useIP_VS_MH_TAB_SIZEas M andKconfigshould show the above prime numbers as options. Or we can configureIP_VS_MH_TAB_BITSas 8..16 and to use it as index to get the actual prime value from array:
Quoted 8 lines
Quoted 2 lines
static int primes[] = { 251, ... 65521 };
I will add
IP_VS_MH_BITSand help message inKconfigto able to
configure it like below.config IP_VS_MH_TAB_INDEX range 8..16
OK. May be 8 .. 17 if we include 131071
Quoted 9 lines
default 12 // and some helpful messages.and
#define IP_VS_MH_TAB_INDEX CONFIG_IP_VS_MH_TAB_INDEX - 8 #define IP_VS_MH_TAB_SIZE PRIMES[IP_VS_MH_TAB_INDEX]in header of
ip_vs_mh.c
OK
Quoted 45 lines
Quoted 36 lines
- we can avoid large allocations for
s->permutation(N*M), instead we can use:struct ip_vs_mh_dest_setup { int offset; /* Starting offset */ int skip; /* skip */ int turns; /* weight / gcd() */ int perm; /* next_offset */ };struct
ip_vs_mh_dest_setup*dests; /* 1..num_dests */dests should be allocated (
-ENOMEM) and freed on
everyip_vs_mh_reassign.Initially, we should calculate .offset and .skip for all
dests. Then we should set dests[i].perm to dests[i].offset. When adding weight to the picture, we can calculate greatest common divisor with thegcd()function and then to use it for calculating 'turns' per dest.ip_vs_wrr_gcd_weight()is a good example how to getgcd()among all dests.When populating table:
The operation 'c = permutation[i][next[i]];' becomes
c = dests[i].perm;
The operation 'next[i] = next[i] + 1;' becomes:
/* Add skip, mod M */ dests[i].perm += dests[i].skip; if (dests[i].perm >= M) dests[i].perm -= M; This should be cheaper than '% 65537'. And it is correctQuoted 4 lines
because:
1. offset is 0 .. M-1
2. skip is 1 .. M-1This is very good idea I think. It shoud be used so I will
changeip_vs_mh_permutateandip_vs_mh_populateto avoid allocatings->permutation.
Quoted 7 lines
And re-configuring weight of dests maybe have some flow in
ip_vs_init_svcandip_vs_mh_dest_changedlike below.1. check max weight, decide value of bits range for rshift and set last weigit to
dests->last_weightwith rshift.
-last_weightshould be set only when it is changed.
- If weight is zero, skip to setlast_weight
Yes, but as I said last_weight change in ip_vs_ctl.c
should be a separate patch, MH will just use last_weight for turns. If last_weight is 0 (server added with weight=0), we should ignore the server for MH table.
Quoted 2 lines
2. get gcd with all dests
- maybegcd()will usedest->last_weightvalue notdest->weight.
Yep
Quoted 3 lines
3. set offset, skip and turns in
ip_vs_mh_permutatedests[i].tunrs =
dest->last_weight/ gcd;
Yep. But we have to be careful, gcd is usually >= 1 but
if all servers are with last_weight=0 then we should avoid divizion by zero and should populate the table with NULLs. ip_vs_mh_gcd_weight will give a clue if there is a server with last_weight>0, i.e. it should return 0 if all servers are with last_weight=0.
Quoted 7 lines
4. use relative weight in ip_vs_mh_populate
if (++dt_count >= dests[i].turns) {
p = p -> next;
dt_count = 0;
i++;
}Yep
Quoted 3 lines
and I think that the configuring weight flow need to be protected
byspin_lock.
locks are not needed, configuration is serialzed by
using __ip_vs_mutex, the configuration methods run concurrently only with packet processing and we use RCU for the table.
Quoted 21 lines
Quoted 6 lines
- Lets do not put limit for number of dests. We can add only such check:
if (svc->num_dest > IP_VS_MH_LOOKUP_SIZE) return -EINVAL;yes. I will add such check.
Quoted 2 lines
-
IP_VS_MH_LOOKUP_SIZEshould be replaced withIP_VS_MH_TAB_SIZEIt is will be replaced with IP_VS_MH_TAB_SIZE.
Quoted 6 lines
Note that I only check the implementation for problems,
I don't run any tests for the algorithm. I hope you have some test program that pushes bunch of random IP[:PORT] values via the algorithm and the result is correct distribution based on the relative weights.
Quoted 4 lines
I alredy have some a kind of test program with destination as
container and always run it when I add some features and make patches. so I will attach some result about correct distribtion at next patch if needed.
As you wish, it is enough to know that the algorithm
was tested to work as expected.
Regards
--
Julian Anastasov <[email protected]>
Hello,
Thanks for your comments. I will send patchsets for v2 of MH.
Quoted 21 lines · Julian Anastasov
Hello,
Quoted 5 lines · Inju Song
Quoted 1 line · Julian Anastasov
yes. I will make next maglev patch as single but maybe
there will be some patchsets for v2 If it needed(ie. add some config toKconfig).Yes,
Kconfig,Makefileshould be in same patch.Quoted 8 lines
Quoted 7 lines
- .upd_dest: changing weight to 0 should be ignored but if we want to allow changing the weight from/to non-0 values then we should save in array the latest non-0 weights for the current dests. As it is difficult to track the dest deletions may be we can add
last_weightin structip_vs_dest. Then MH will work withdest->last_weight.
Quoted 7 lines
Quoted 2 lines
I think that It needs to allow changing the weight from/to
non-0 values so I will add to allow only if is non-0 values.Yep, then we should catch .upd_dest and to use
last_weight.
In fact, addinglast_weightto IPVS core should be separate patch to apply before the MH patch.
Ok. So should I send patch about adding last_weight to IPVS core
before appling the MH patch? Then I will.
Quoted 140 lines
Quoted 7 lines
Quoted 7 lines
- To avoid waste of memory due to kmalloc's orders we can use the previous prime number, 65521, others can be 32749, 16381, 8191, 4093, 2039, 1021, some no so useful values due to page size:
- 509 (2KB with 4-byte ptr, 4KB for 8-byte ptr)
- 251 (1KB/2KB of 4KB page)Quoted 8 lines
Quoted 2 lines
With such primes the table should be a separate allocation.
I agress this. To avoid waste of memory and provide various
prime number, it is saved with static array like below.// available prime number
static int PRIMES[] =Do not use upper case for var names...
Quoted 7 lines
{ 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
I want to proivde larger space then 65521, so add 131071 to
last index of primes. If large value is not so fatal I think it is better in case of disruption of consistent hashing. I think It is helpful for users who have high performance machine and want more minimal disruption when destinations are add/deleted.OK, may be 131071 can help if using hundreds of servers...
Quoted 24 lines
Quoted 11 lines
- another possible issue is that users can use crazy values for weight, for example, 64 and 127. In such case even the
gcd()function can not help enough for something that would be 1:2 relation. But we can warn users to use as small as possible values for weights. If needed, we can rshift the weights, so that only the highest 5-6 bits of the largest weight are considered. I.e. if we find the largest weight is 2000 we see that 2000 occupies 11 bits, so we decide to use the highest 6 bits and rshift with 5 (11-6) all weights. All resulting weights will fit in 6 bits.I agree that huge weight values are considerd and rshift is
good idea I think.
To handle it, both rshift andlast_weightare considerd. I
wrote my opinion below.Quoted 7 lines
-
ip_vs_mh_populateallocatesIP_VS_MH_TAB_SIZEints for next[] but later next[i] can go up tonum_destswhich can be above theIP_VS_MH_TAB_SIZE. We should useIP_VS_MH_TAB_SIZEas M andKconfigshould show the above prime numbers as options. Or we can configureIP_VS_MH_TAB_BITSas 8..16 and to use it as index to get the actual prime value from array:Quoted 8 lines
Quoted 2 lines
static int primes[] = { 251, ... 65521 };
I will add
IP_VS_MH_BITSand help message inKconfigto able to
configure it like below.config IP_VS_MH_TAB_INDEX range 8..16OK. May be 8 .. 17 if we include 131071Quoted 9 lines
default 12 // and some helpful messages.and
#define IP_VS_MH_TAB_INDEX CONFIG_IP_VS_MH_TAB_INDEX - 8 #define IP_VS_MH_TAB_SIZE PRIMES[IP_VS_MH_TAB_INDEX]in header of
ip_vs_mh.cOK
Quoted 45 lines
Quoted 36 lines
- we can avoid large allocations for
s->permutation(N*M), instead we can use:struct ip_vs_mh_dest_setup { int offset; /* Starting offset */ int skip; /* skip */ int turns; /* weight / gcd() */ int perm; /* next_offset */ };struct
ip_vs_mh_dest_setup*dests; /* 1..num_dests */dests should be allocated (
-ENOMEM) and freed on
everyip_vs_mh_reassign.Initially, we should calculate .offset and .skip for all
dests. Then we should set dests[i].perm to dests[i].offset. When adding weight to the picture, we can calculate greatest common divisor with thegcd()function and then to use it for calculating 'turns' per dest.ip_vs_wrr_gcd_weight()is a good example how to getgcd()among all dests.When populating table:
The operation 'c = permutation[i][next[i]];' becomes
c = dests[i].perm;
The operation 'next[i] = next[i] + 1;' becomes:
/* Add skip, mod M */ dests[i].perm += dests[i].skip; if (dests[i].perm >= M) dests[i].perm -= M; This should be cheaper than '% 65537'. And it is correctQuoted 4 lines
because:
1. offset is 0 .. M-1
2. skip is 1 .. M-1This is very good idea I think. It shoud be used so I will
changeip_vs_mh_permutateandip_vs_mh_populateto avoid allocatings->permutation.Quoted 7 lines
And re-configuring weight of dests maybe have some flow in
ip_vs_init_svcandip_vs_mh_dest_changedlike below.1. check max weight, decide value of bits range for rshift and set last weigit to
dests->last_weightwith rshift.
-last_weightshould be set only when it is changed.
- If weight is zero, skip to setlast_weightYes, but as I said
last_weightchange inip_vs_ctl.c
should be a separate patch, MH will just uselast_weightfor turns. Iflast_weightis 0 (server added with weight=0), we should ignore the server for MH table.Quoted 2 lines
2. get gcd with all dests
- maybegcd()will usedest->last_weightvalue notdest->weight.Yep
Quoted 2 lines
3. set offset, skip and turns in
ip_vs_mh_permutate
Quoted 9 lines
Quoted 1 line
dests[i].tunrs = dest->last_weight / gcd;Yep. But we have to be careful, gcd is usually >= 1 but
if all servers are with last_weight=0 then we should avoid divizion by zero and should populate the table with NULLs.ip_vs_mh_gcd_weightwill give a clue if there is a server with last_weight>0, i.e. it should return 0 if all servers are with last_weight=0.
Of course. These case should be handled. Thanks for your
advice.
Quoted 11 lines
Quoted 7 lines
4. use relative weight in ip_vs_mh_populate if (++dt_count >= dests[i].turns) { p = p -> next; dt_count = 0; i++; }Yep
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
and I think that the configuring weight flow need to be protected
byspin_lock.locks are not needed, configuration is serialzed by
using__ip_vs_mutex, the configuration methods run concurrently only with packet processing and we use RCU for the table.
Ok. then I will use RCU for the mh_setup table.
Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
- Lets do not put limit for number of dests. We can add only such check:
if (svc->num_dest > IP_VS_MH_LOOKUP_SIZE) return -EINVAL;yes. I will add such check.
Quoted 2 lines
-
IP_VS_MH_LOOKUP_SIZEshould be replaced withIP_VS_MH_TAB_SIZEIt is will be replaced with IP_VS_MH_TAB_SIZE.
Quoted 6 lines
Note that I only check the implementation for problems,
I don't run any tests for the algorithm. I hope you have some test program that pushes bunch of random IP[:PORT] values via the algorithm and the result is correct distribution based on the relative weights.
Quoted 7 lines
Quoted 4 lines
I alredy have some a kind of test program with destination as
container and always run it when I add some features and make patches. so I will attach some result about correct distribtion at next patch if needed.As you wish, it is enough to know that the algorithm
was tested to work as expected.
Regards
Signature · 2 lines
Inju Song NAVER Corporation
Julian Anastasov
maintainerHello,
Quoted 7 lines · Inju Song
Quoted 4 lines
Yep, then we should catch .upd_dest and to use
last_weight.
In fact, addinglast_weightto IPVS core should be separate patch to apply before the MH patch.Ok. So should I send patch about adding
last_weightto IPVS core
before appling the MH patch? Then I will.
Yes, keep both patches in same patchset, patch 1 forlast_weight and patch 2 for MH. You should send them by changing version until all issues are resolved. As result, it would be easier for Simon to apply them.
Quoted 6 lines
Quoted 4 lines
locks are not needed, configuration is serialzed by
using__ip_vs_mutex, the configuration methods run concurrently only with packet processing and we use RCU for the table.Ok. then I will use RCU for the
mh_setuptable.
Nothing different from what was used in last MH version and
what is in SH (ip_vs_sh_reassign): changing table entries with RCU primitives.
Regards
--
Julian Anastasov <[email protected]>