2828/* Number of elements to store in an initial array block */
2929#define AHASH_INIT_SIZE 4
3030/* Max number of elements to store in an array block */
31- #define AHASH_MAX_SIZE (3*4)
31+ #define AHASH_MAX_SIZE (3*AHASH_INIT_SIZE)
32+
33+ /* Max number of elements can be tuned */
34+ #ifdef IP_SET_HASH_WITH_MULTI
35+ #define AHASH_MAX (h ) ((h)->ahash_max)
36+
37+ static inline u8
38+ tune_ahash_max (u8 curr , u32 multi )
39+ {
40+ u32 n ;
41+
42+ if (multi < curr )
43+ return curr ;
44+
45+ n = curr + AHASH_INIT_SIZE ;
46+ /* Currently, at listing one hash bucket must fit into a message.
47+ * Therefore we have a hard limit here.
48+ */
49+ return n > curr && n <= 64 ? n : curr ;
50+ }
51+ #define TUNE_AHASH_MAX (h , multi ) \
52+ ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
53+ #else
54+ #define AHASH_MAX (h ) AHASH_MAX_SIZE
55+ #define TUNE_AHASH_MAX (h , multi )
56+ #endif
3257
3358/* A hash bucket */
3459struct hbucket {
@@ -60,6 +85,9 @@ struct ip_set_hash {
6085 u32 timeout ; /* timeout value, if enabled */
6186 struct timer_list gc ; /* garbage collection when timeout enabled */
6287 struct type_pf_next next ; /* temporary storage for uadd */
88+ #ifdef IP_SET_HASH_WITH_MULTI
89+ u8 ahash_max ; /* max elements in an array block */
90+ #endif
6391#ifdef IP_SET_HASH_WITH_NETMASK
6492 u8 netmask ; /* netmask value for subnets to store */
6593#endif
@@ -211,12 +239,16 @@ ip_set_hash_destroy(struct ip_set *set)
211239 set -> data = NULL ;
212240}
213241
214- #define HKEY (data , initval , htable_bits ) \
215- (jhash2((u32 *)(data), sizeof(struct type_pf_elem)/sizeof(u32), initval) \
216- & jhash_mask(htable_bits))
217-
218242#endif /* _IP_SET_AHASH_H */
219243
244+ #ifndef HKEY_DATALEN
245+ #define HKEY_DATALEN sizeof(struct type_pf_elem)
246+ #endif
247+
248+ #define HKEY (data , initval , htable_bits ) \
249+ (jhash2((u32 *)(data), HKEY_DATALEN/sizeof(u32), initval) \
250+ & jhash_mask(htable_bits))
251+
220252#define CONCAT (a , b , c ) a##b##c
221253#define TOKEN (a , b , c ) CONCAT(a, b, c)
222254
@@ -275,12 +307,13 @@ ip_set_hash_destroy(struct ip_set *set)
275307/* Add an element to the hash table when resizing the set:
276308 * we spare the maintenance of the internal counters. */
277309static int
278- type_pf_elem_add (struct hbucket * n , const struct type_pf_elem * value )
310+ type_pf_elem_add (struct hbucket * n , const struct type_pf_elem * value ,
311+ u8 ahash_max )
279312{
280313 if (n -> pos >= n -> size ) {
281314 void * tmp ;
282315
283- if (n -> size >= AHASH_MAX_SIZE )
316+ if (n -> size >= ahash_max )
284317 /* Trigger rehashing */
285318 return - EAGAIN ;
286319
@@ -335,7 +368,7 @@ type_pf_resize(struct ip_set *set, bool retried)
335368 for (j = 0 ; j < n -> pos ; j ++ ) {
336369 data = ahash_data (n , j );
337370 m = hbucket (t , HKEY (data , h -> initval , htable_bits ));
338- ret = type_pf_elem_add (m , data );
371+ ret = type_pf_elem_add (m , data , AHASH_MAX ( h ) );
339372 if (ret < 0 ) {
340373 read_unlock_bh (& set -> lock );
341374 ahash_destroy (t );
@@ -359,7 +392,7 @@ type_pf_resize(struct ip_set *set, bool retried)
359392 return 0 ;
360393}
361394
362- static void
395+ static inline void
363396type_pf_data_next (struct ip_set_hash * h , const struct type_pf_elem * d );
364397
365398/* Add an element to a hash and update the internal counters when succeeded,
@@ -372,7 +405,7 @@ type_pf_add(struct ip_set *set, void *value, u32 timeout, u32 flags)
372405 const struct type_pf_elem * d = value ;
373406 struct hbucket * n ;
374407 int i , ret = 0 ;
375- u32 key ;
408+ u32 key , multi = 0 ;
376409
377410 if (h -> elements >= h -> maxelem )
378411 return - IPSET_ERR_HASH_FULL ;
@@ -382,12 +415,12 @@ type_pf_add(struct ip_set *set, void *value, u32 timeout, u32 flags)
382415 key = HKEY (value , h -> initval , t -> htable_bits );
383416 n = hbucket (t , key );
384417 for (i = 0 ; i < n -> pos ; i ++ )
385- if (type_pf_data_equal (ahash_data (n , i ), d )) {
418+ if (type_pf_data_equal (ahash_data (n , i ), d , & multi )) {
386419 ret = - IPSET_ERR_EXIST ;
387420 goto out ;
388421 }
389-
390- ret = type_pf_elem_add (n , value );
422+ TUNE_AHASH_MAX ( h , multi );
423+ ret = type_pf_elem_add (n , value , AHASH_MAX ( h ) );
391424 if (ret != 0 ) {
392425 if (ret == - EAGAIN )
393426 type_pf_data_next (h , d );
@@ -415,13 +448,13 @@ type_pf_del(struct ip_set *set, void *value, u32 timeout, u32 flags)
415448 struct hbucket * n ;
416449 int i ;
417450 struct type_pf_elem * data ;
418- u32 key ;
451+ u32 key , multi = 0 ;
419452
420453 key = HKEY (value , h -> initval , t -> htable_bits );
421454 n = hbucket (t , key );
422455 for (i = 0 ; i < n -> pos ; i ++ ) {
423456 data = ahash_data (n , i );
424- if (!type_pf_data_equal (data , d ))
457+ if (!type_pf_data_equal (data , d , & multi ))
425458 continue ;
426459 if (i != n -> pos - 1 )
427460 /* Not last one */
@@ -462,17 +495,17 @@ type_pf_test_cidrs(struct ip_set *set, struct type_pf_elem *d, u32 timeout)
462495 struct hbucket * n ;
463496 const struct type_pf_elem * data ;
464497 int i , j = 0 ;
465- u32 key ;
498+ u32 key , multi = 0 ;
466499 u8 host_mask = SET_HOST_MASK (set -> family );
467500
468501 pr_debug ("test by nets\n" );
469- for (; j < host_mask && h -> nets [j ].cidr ; j ++ ) {
502+ for (; j < host_mask && h -> nets [j ].cidr && ! multi ; j ++ ) {
470503 type_pf_data_netmask (d , h -> nets [j ].cidr );
471504 key = HKEY (d , h -> initval , t -> htable_bits );
472505 n = hbucket (t , key );
473506 for (i = 0 ; i < n -> pos ; i ++ ) {
474507 data = ahash_data (n , i );
475- if (type_pf_data_equal (data , d ))
508+ if (type_pf_data_equal (data , d , & multi ))
476509 return 1 ;
477510 }
478511 }
@@ -490,7 +523,7 @@ type_pf_test(struct ip_set *set, void *value, u32 timeout, u32 flags)
490523 struct hbucket * n ;
491524 const struct type_pf_elem * data ;
492525 int i ;
493- u32 key ;
526+ u32 key , multi = 0 ;
494527
495528#ifdef IP_SET_HASH_WITH_NETS
496529 /* If we test an IP address and not a network address,
@@ -503,7 +536,7 @@ type_pf_test(struct ip_set *set, void *value, u32 timeout, u32 flags)
503536 n = hbucket (t , key );
504537 for (i = 0 ; i < n -> pos ; i ++ ) {
505538 data = ahash_data (n , i );
506- if (type_pf_data_equal (data , d ))
539+ if (type_pf_data_equal (data , d , & multi ))
507540 return 1 ;
508541 }
509542 return 0 ;
@@ -660,14 +693,14 @@ type_pf_data_timeout_set(struct type_pf_elem *data, u32 timeout)
660693
661694static int
662695type_pf_elem_tadd (struct hbucket * n , const struct type_pf_elem * value ,
663- u32 timeout )
696+ u8 ahash_max , u32 timeout )
664697{
665698 struct type_pf_elem * data ;
666699
667700 if (n -> pos >= n -> size ) {
668701 void * tmp ;
669702
670- if (n -> size >= AHASH_MAX_SIZE )
703+ if (n -> size >= ahash_max )
671704 /* Trigger rehashing */
672705 return - EAGAIN ;
673706
@@ -772,7 +805,7 @@ type_pf_tresize(struct ip_set *set, bool retried)
772805 for (j = 0 ; j < n -> pos ; j ++ ) {
773806 data = ahash_tdata (n , j );
774807 m = hbucket (t , HKEY (data , h -> initval , htable_bits ));
775- ret = type_pf_elem_tadd (m , data ,
808+ ret = type_pf_elem_tadd (m , data , AHASH_MAX ( h ),
776809 type_pf_data_timeout (data ));
777810 if (ret < 0 ) {
778811 read_unlock_bh (& set -> lock );
@@ -803,9 +836,9 @@ type_pf_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
803836 const struct type_pf_elem * d = value ;
804837 struct hbucket * n ;
805838 struct type_pf_elem * data ;
806- int ret = 0 , i , j = AHASH_MAX_SIZE + 1 ;
839+ int ret = 0 , i , j = AHASH_MAX ( h ) + 1 ;
807840 bool flag_exist = flags & IPSET_FLAG_EXIST ;
808- u32 key ;
841+ u32 key , multi = 0 ;
809842
810843 if (h -> elements >= h -> maxelem )
811844 /* FIXME: when set is full, we slow down here */
@@ -819,18 +852,18 @@ type_pf_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
819852 n = hbucket (t , key );
820853 for (i = 0 ; i < n -> pos ; i ++ ) {
821854 data = ahash_tdata (n , i );
822- if (type_pf_data_equal (data , d )) {
855+ if (type_pf_data_equal (data , d , & multi )) {
823856 if (type_pf_data_expired (data ) || flag_exist )
824857 j = i ;
825858 else {
826859 ret = - IPSET_ERR_EXIST ;
827860 goto out ;
828861 }
829- } else if (j == AHASH_MAX_SIZE + 1 &&
862+ } else if (j == AHASH_MAX ( h ) + 1 &&
830863 type_pf_data_expired (data ))
831864 j = i ;
832865 }
833- if (j != AHASH_MAX_SIZE + 1 ) {
866+ if (j != AHASH_MAX ( h ) + 1 ) {
834867 data = ahash_tdata (n , j );
835868#ifdef IP_SET_HASH_WITH_NETS
836869 del_cidr (h , data -> cidr , HOST_MASK );
@@ -840,7 +873,8 @@ type_pf_tadd(struct ip_set *set, void *value, u32 timeout, u32 flags)
840873 type_pf_data_timeout_set (data , timeout );
841874 goto out ;
842875 }
843- ret = type_pf_elem_tadd (n , d , timeout );
876+ TUNE_AHASH_MAX (h , multi );
877+ ret = type_pf_elem_tadd (n , d , AHASH_MAX (h ), timeout );
844878 if (ret != 0 ) {
845879 if (ret == - EAGAIN )
846880 type_pf_data_next (h , d );
@@ -865,13 +899,13 @@ type_pf_tdel(struct ip_set *set, void *value, u32 timeout, u32 flags)
865899 struct hbucket * n ;
866900 int i ;
867901 struct type_pf_elem * data ;
868- u32 key ;
902+ u32 key , multi = 0 ;
869903
870904 key = HKEY (value , h -> initval , t -> htable_bits );
871905 n = hbucket (t , key );
872906 for (i = 0 ; i < n -> pos ; i ++ ) {
873907 data = ahash_tdata (n , i );
874- if (!type_pf_data_equal (data , d ))
908+ if (!type_pf_data_equal (data , d , & multi ))
875909 continue ;
876910 if (type_pf_data_expired (data ))
877911 return - IPSET_ERR_EXIST ;
@@ -911,16 +945,16 @@ type_pf_ttest_cidrs(struct ip_set *set, struct type_pf_elem *d, u32 timeout)
911945 struct type_pf_elem * data ;
912946 struct hbucket * n ;
913947 int i , j = 0 ;
914- u32 key ;
948+ u32 key , multi = 0 ;
915949 u8 host_mask = SET_HOST_MASK (set -> family );
916950
917- for (; j < host_mask && h -> nets [j ].cidr ; j ++ ) {
951+ for (; j < host_mask && h -> nets [j ].cidr && ! multi ; j ++ ) {
918952 type_pf_data_netmask (d , h -> nets [j ].cidr );
919953 key = HKEY (d , h -> initval , t -> htable_bits );
920954 n = hbucket (t , key );
921955 for (i = 0 ; i < n -> pos ; i ++ ) {
922956 data = ahash_tdata (n , i );
923- if (type_pf_data_equal (data , d ))
957+ if (type_pf_data_equal (data , d , & multi ))
924958 return !type_pf_data_expired (data );
925959 }
926960 }
@@ -936,7 +970,7 @@ type_pf_ttest(struct ip_set *set, void *value, u32 timeout, u32 flags)
936970 struct type_pf_elem * data , * d = value ;
937971 struct hbucket * n ;
938972 int i ;
939- u32 key ;
973+ u32 key , multi = 0 ;
940974
941975#ifdef IP_SET_HASH_WITH_NETS
942976 if (d -> cidr == SET_HOST_MASK (set -> family ))
@@ -946,7 +980,7 @@ type_pf_ttest(struct ip_set *set, void *value, u32 timeout, u32 flags)
946980 n = hbucket (t , key );
947981 for (i = 0 ; i < n -> pos ; i ++ ) {
948982 data = ahash_tdata (n , i );
949- if (type_pf_data_equal (data , d ))
983+ if (type_pf_data_equal (data , d , & multi ))
950984 return !type_pf_data_expired (data );
951985 }
952986 return 0 ;
@@ -1054,6 +1088,8 @@ type_pf_gc_init(struct ip_set *set)
10541088 IPSET_GC_PERIOD (h -> timeout ));
10551089}
10561090
1091+ #undef HKEY_DATALEN
1092+ #undef HKEY
10571093#undef type_pf_data_equal
10581094#undef type_pf_data_isnull
10591095#undef type_pf_data_copy
0 commit comments