netfilter: ipset: Separate memsize calculation code into dedicated function

Hash types already has it's memsize calculation code in separate
functions. Clean up and do the same for *bitmap* and *list* sets.

Ported from a patch proposed by Sergey Popovich <popovich_sergei@mail.ua>.

Suggested-by: Sergey Popovich <popovich_sergei@mail.ua>
Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
This commit is contained in:
Jozsef Kadlecsik 2016-10-10 21:44:32 +02:00
Родитель 57982edc27
Коммит 722a94519a
2 изменённых файлов: 27 добавлений и 7 удалений

Просмотреть файл

@ -22,6 +22,7 @@
#define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
#define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
#define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
#define mtype_memsize IPSET_TOKEN(MTYPE, _memsize)
#define mtype_flush IPSET_TOKEN(MTYPE, _flush)
#define mtype_head IPSET_TOKEN(MTYPE, _head)
#define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
@ -84,12 +85,20 @@ mtype_flush(struct ip_set *set)
memset(map->members, 0, map->memsize);
}
/* Calculate the actual memory size of the set data */
static size_t
mtype_memsize(const struct mtype *map, size_t dsize)
{
return sizeof(*map) + map->memsize +
map->elements * dsize;
}
static int
mtype_head(struct ip_set *set, struct sk_buff *skb)
{
const struct mtype *map = set->data;
struct nlattr *nested;
size_t memsize = sizeof(*map) + map->memsize;
size_t memsize = mtype_memsize(map, set->dsize);
nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
if (!nested)

Просмотреть файл

@ -441,12 +441,12 @@ list_set_destroy(struct ip_set *set)
set->data = NULL;
}
static int
list_set_head(struct ip_set *set, struct sk_buff *skb)
/* Calculate the actual memory size of the set data */
static size_t
list_set_memsize(const struct list_set *map, size_t dsize)
{
const struct list_set *map = set->data;
struct nlattr *nested;
struct set_elem *e;
size_t memsize;
u32 n = 0;
rcu_read_lock();
@ -454,13 +454,24 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)
n++;
rcu_read_unlock();
memsize = sizeof(*map) + n * dsize;
return memsize;
}
static int
list_set_head(struct ip_set *set, struct sk_buff *skb)
{
const struct list_set *map = set->data;
struct nlattr *nested;
size_t memsize = list_set_memsize(map, set->dsize);
nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
if (!nested)
goto nla_put_failure;
if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref)) ||
nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
htonl(sizeof(*map) + n * set->dsize)))
nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
goto nla_put_failure;
if (unlikely(ip_set_put_flags(skb, set)))
goto nla_put_failure;