1 #ifndef _IP_SET_AHASH_H
2 #define _IP_SET_AHASH_H
8 #define CONCAT(a, b, c) a##b##c
9 #define TOKEN(a, b, c) CONCAT(a, b, c)
11 #define type_pf_next TOKEN(TYPE, PF, _elem)
29 #define AHASH_INIT_SIZE 4
31 #define AHASH_MAX_SIZE (3*AHASH_INIT_SIZE)
34 #ifdef IP_SET_HASH_WITH_MULTI
35 #define AHASH_MAX(h) ((h)->ahash_max)
49 return n > curr && n <= 64 ? n :
curr;
51 #define TUNE_AHASH_MAX(h, multi) \
52 ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi))
54 #define AHASH_MAX(h) AHASH_MAX_SIZE
55 #define TUNE_AHASH_MAX(h, multi)
71 #define hbucket(h, i) (&((h)->bucket[i]))
88 #ifdef IP_SET_HASH_WITH_MULTI
91 #ifdef IP_SET_HASH_WITH_NETMASK
94 #ifdef IP_SET_HASH_WITH_RBTREE
97 #ifdef IP_SET_HASH_WITH_NETS
103 htable_size(
u8 hbits)
111 if ((((
size_t)-1) -
sizeof(
struct htable))/
sizeof(
struct hbucket)
120 htable_bits(
u32 hashsize)
123 u8 bits = fls(hashsize - 1);
126 bits = fls(hashsize);
131 #ifdef IP_SET_HASH_WITH_NETS
132 #ifdef IP_SET_HASH_WITH_NETS_PACKED
134 #define CIDR(cidr) (cidr + 1)
136 #define CIDR(cidr) (cidr)
139 #define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
140 #ifdef IP_SET_HASH_WITH_MULTI
141 #define NETS_LENGTH(family) (SET_HOST_MASK(family) + 1)
143 #define NETS_LENGTH(family) SET_HOST_MASK(family)
154 for (i = 0, j = -1; i < nets_length && h->nets[
i].nets; i++) {
157 else if (h->nets[i].cidr < cidr)
159 else if (h->nets[i].cidr == cidr) {
166 h->nets[
i].cidr = h->nets[i - 1].cidr;
167 h->nets[
i].nets = h->nets[i - 1].nets;
170 h->nets[
i].cidr = cidr;
179 for (i = 0; i < nets_length - 1 && h->nets[
i].cidr != cidr; i++)
183 if (h->nets[i].nets != 0)
186 for (j = i; j < nets_length - 1 && h->nets[
j].nets; j++) {
187 h->nets[
j].cidr = h->nets[j + 1].cidr;
188 h->nets[
j].nets = h->nets[j + 1].nets;
192 #define NETS_LENGTH(family) 0
197 ahash_destroy(
struct htable *
t)
214 ahash_memsize(
const struct ip_set_hash *h,
size_t dsize,
u8 nets_length)
220 #ifdef IP_SET_HASH_WITH_NETS
226 memsize += t->
bucket[i].size * dsize;
233 ip_set_hash_flush(
struct ip_set *
set)
248 #ifdef IP_SET_HASH_WITH_NETS
257 ip_set_hash_destroy(
struct ip_set *
set)
264 ahash_destroy(h->
table);
265 #ifdef IP_SET_HASH_WITH_RBTREE
266 rbtree_destroy(&h->rbtree);
276 #define HKEY_DATALEN sizeof(struct type_pf_elem)
279 #define HKEY(data, initval, htable_bits) \
280 (jhash2((u32 *)(data), HKEY_DATALEN/sizeof(u32), initval) \
281 & jhash_mask(htable_bits))
285 #define type_pf_data_equal TOKEN(TYPE, PF, _data_equal)
286 #define type_pf_data_isnull TOKEN(TYPE, PF, _data_isnull)
287 #define type_pf_data_copy TOKEN(TYPE, PF, _data_copy)
288 #define type_pf_data_zero_out TOKEN(TYPE, PF, _data_zero_out)
289 #define type_pf_data_netmask TOKEN(TYPE, PF, _data_netmask)
290 #define type_pf_data_list TOKEN(TYPE, PF, _data_list)
291 #define type_pf_data_tlist TOKEN(TYPE, PF, _data_tlist)
292 #define type_pf_data_next TOKEN(TYPE, PF, _data_next)
293 #define type_pf_data_flags TOKEN(TYPE, PF, _data_flags)
294 #ifdef IP_SET_HASH_WITH_NETS
295 #define type_pf_data_match TOKEN(TYPE, PF, _data_match)
297 #define type_pf_data_match(d) 1
300 #define type_pf_elem TOKEN(TYPE, PF, _elem)
301 #define type_pf_telem TOKEN(TYPE, PF, _telem)
302 #define type_pf_data_timeout TOKEN(TYPE, PF, _data_timeout)
303 #define type_pf_data_expired TOKEN(TYPE, PF, _data_expired)
304 #define type_pf_data_timeout_set TOKEN(TYPE, PF, _data_timeout_set)
306 #define type_pf_elem_add TOKEN(TYPE, PF, _elem_add)
307 #define type_pf_add TOKEN(TYPE, PF, _add)
308 #define type_pf_del TOKEN(TYPE, PF, _del)
309 #define type_pf_test_cidrs TOKEN(TYPE, PF, _test_cidrs)
310 #define type_pf_test TOKEN(TYPE, PF, _test)
312 #define type_pf_elem_tadd TOKEN(TYPE, PF, _elem_tadd)
313 #define type_pf_del_telem TOKEN(TYPE, PF, _ahash_del_telem)
314 #define type_pf_expire TOKEN(TYPE, PF, _expire)
315 #define type_pf_tadd TOKEN(TYPE, PF, _tadd)
316 #define type_pf_tdel TOKEN(TYPE, PF, _tdel)
317 #define type_pf_ttest_cidrs TOKEN(TYPE, PF, _ahash_ttest_cidrs)
318 #define type_pf_ttest TOKEN(TYPE, PF, _ahash_ttest)
320 #define type_pf_resize TOKEN(TYPE, PF, _resize)
321 #define type_pf_tresize TOKEN(TYPE, PF, _tresize)
322 #define type_pf_flush ip_set_hash_flush
323 #define type_pf_destroy ip_set_hash_destroy
324 #define type_pf_head TOKEN(TYPE, PF, _head)
325 #define type_pf_list TOKEN(TYPE, PF, _list)
326 #define type_pf_tlist TOKEN(TYPE, PF, _tlist)
327 #define type_pf_same_set TOKEN(TYPE, PF, _same_set)
328 #define type_pf_kadt TOKEN(TYPE, PF, _kadt)
329 #define type_pf_uadt TOKEN(TYPE, PF, _uadt)
330 #define type_pf_gc TOKEN(TYPE, PF, _gc)
331 #define type_pf_gc_init TOKEN(TYPE, PF, _gc_init)
332 #define type_pf_variant TOKEN(TYPE, PF, _variant)
333 #define type_pf_tvariant TOKEN(TYPE, PF, _tvariant)
338 #define ahash_data(n, i) \
339 ((struct type_pf_elem *)((n)->value) + (i))
345 u8 ahash_max,
u32 cadt_flags)
352 if (n->
size >= ahash_max)
371 #ifdef IP_SET_HASH_WITH_NETS
396 pr_debug(
"attempt to resize set %s from %u to %u, t %p\n",
400 pr_warning(
"Cannot increase the hashsize of set %s further\n",
413 for (j = 0; j < n->
pos; j++) {
433 pr_debug(
"set %s resized from %u (%p) to %u (%p)\n",
set->name,
454 u32 cadt_flags = flags >> 16;
458 pr_warning(
"Set %s is full, maxelem %u reached\n",
467 for (i = 0; i < n->
pos; i++)
469 #ifdef IP_SET_HASH_WITH_NETS
486 #ifdef IP_SET_HASH_WITH_NETS
491 rcu_read_unlock_bh();
511 for (i = 0; i < n->
pos; i++) {
521 #ifdef IP_SET_HASH_WITH_NETS
524 if (n->
pos + AHASH_INIT_SIZE < n->
size) {
542 #ifdef IP_SET_HASH_WITH_NETS
558 for (; j < nets_length && h->nets[
j].nets && !
multi; j++) {
562 for (i = 0; i < n->
pos; i++) {
584 #ifdef IP_SET_HASH_WITH_NETS
587 if (CIDR(d->cidr) == SET_HOST_MASK(
set->family))
593 for (i = 0; i < n->
pos; i++) {
610 memsize = ahash_memsize(h, with_timeout(h->
timeout)
618 goto nla_put_failure;
622 goto nla_put_failure;
623 #ifdef IP_SET_HASH_WITH_NETMASK
626 goto nla_put_failure;
632 goto nla_put_failure;
647 struct nlattr *atd, *nested;
660 incomplete = skb_tail_pointer(skb);
662 pr_debug(
"cb->args[2]: %lu, t %p n %p\n", cb->
args[2], t, n);
663 for (i = 0; i < n->
pos; i++) {
665 pr_debug(
"list hash %lu hbucket %p i %u, data %p\n",
666 cb->
args[2], n, i, data);
669 if (cb->
args[2] == first) {
670 nla_nest_cancel(skb, atd);
673 goto nla_put_failure;
676 goto nla_put_failure;
687 nlmsg_trim(skb, incomplete);
690 pr_warning(
"Can't list set %s: one bucket does not fit into "
691 "a message. Please report it!\n",
set->name);
724 #define ahash_tdata(n, i) \
725 (struct type_pf_elem *)((struct type_pf_telem *)((n)->value) + (i))
733 return tdata->timeout;
742 return ip_set_timeout_expired(tdata->timeout);
750 tdata->timeout = ip_set_timeout_set(timeout);
755 u8 ahash_max,
u32 cadt_flags,
u32 timeout)
762 if (n->
size >= ahash_max)
782 #ifdef IP_SET_HASH_WITH_NETS
802 for (j = 0; j < n->
pos; j++) {
806 #ifdef IP_SET_HASH_WITH_NETS
807 del_cidr(h, CIDR(data->cidr), nets_length);
817 if (n->
pos + AHASH_INIT_SIZE < n->size) {
859 pr_warning(
"Cannot increase the hashsize of set %s further\n",
872 for (j = 0; j < n->
pos; j++) {
909 u32 cadt_flags = flags >> 16;
916 pr_warning(
"Set %s is full, maxelem %u reached\n",
925 for (i = 0; i < n->
pos; i++) {
941 #ifdef IP_SET_HASH_WITH_NETS
947 #ifdef IP_SET_HASH_WITH_NETS
960 #ifdef IP_SET_HASH_WITH_NETS
965 rcu_read_unlock_bh();
982 for (i = 0; i < n->
pos; i++) {
994 #ifdef IP_SET_HASH_WITH_NETS
997 if (n->
pos + AHASH_INIT_SIZE < n->size) {
1015 #ifdef IP_SET_HASH_WITH_NETS
1027 for (; j < nets_length && h->nets[
j].nets && !
multi; j++) {
1031 for (i = 0; i < n->
pos; i++) {
1033 #ifdef IP_SET_HASH_WITH_MULTI
1060 #ifdef IP_SET_HASH_WITH_NETS
1061 if (CIDR(d->cidr) == SET_HOST_MASK(
set->family))
1066 for (i = 0; i < n->
pos; i++) {
1081 struct nlattr *atd, *nested;
1093 incomplete = skb_tail_pointer(skb);
1095 for (i = 0; i < n->
pos; i++) {
1103 if (cb->
args[2] == first) {
1104 nla_nest_cancel(skb, atd);
1107 goto nla_put_failure;
1110 goto nla_put_failure;
1121 nlmsg_trim(skb, incomplete);
1124 pr_warning(
"Can't list set %s: one bucket does not fit into "
1125 "a message. Please report it!\n",
set->name);
1169 h->
gc.data = (
unsigned long)
set;
1173 pr_debug(
"gc initialized, run in every %u\n",
1179 #undef type_pf_data_equal
1180 #undef type_pf_data_isnull
1181 #undef type_pf_data_copy
1182 #undef type_pf_data_zero_out
1183 #undef type_pf_data_netmask
1184 #undef type_pf_data_list
1185 #undef type_pf_data_tlist
1186 #undef type_pf_data_next
1187 #undef type_pf_data_flags
1188 #undef type_pf_data_match
1191 #undef type_pf_telem
1192 #undef type_pf_data_timeout
1193 #undef type_pf_data_expired
1194 #undef type_pf_data_timeout_set
1196 #undef type_pf_elem_add
1199 #undef type_pf_test_cidrs
1202 #undef type_pf_elem_tadd
1203 #undef type_pf_del_telem
1204 #undef type_pf_expire
1207 #undef type_pf_ttest_cidrs
1208 #undef type_pf_ttest
1210 #undef type_pf_resize
1211 #undef type_pf_tresize
1212 #undef type_pf_flush
1213 #undef type_pf_destroy
1216 #undef type_pf_tlist
1217 #undef type_pf_same_set
1221 #undef type_pf_gc_init
1222 #undef type_pf_variant
1223 #undef type_pf_tvariant