forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipset_bitmap.c
103 lines (84 loc) · 2.5 KB
/
ipset_bitmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2021 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "ipset/bitops.h"
#include "ipset/ipset_bitmap.h"
#define do(adt, ...) set->variant->bitmap.do_##adt(__VA_ARGS__)
static int
bitmap_add(struct ipset *set, void *value, uint16_t flag)
{
struct bitmap_map *map = set->data;
struct bitmap_elem *e = value;
int ret = do(test, value, map, set->dsize);
if (e->id >= map->elements)
return EDPVS_INVAL;
/* To avoid same IP, different MAC or other elements */
if (ret || test_bit(e->id, map->members)) {
if (flag & IPSET_F_FORCE)
return EDPVS_OK;
return EDPVS_EXIST;
}
set_bit(e->id, map->members);
set->elements++;
return EDPVS_OK;
}
static int
bitmap_del(struct ipset *set, void *value, uint16_t flag)
{
struct bitmap_map *map = set->data;
struct bitmap_elem *e = value;
if (e->id >= map->elements)
return EDPVS_INVAL;
if (!do(del, value, map))
return EDPVS_NOTEXIST;
set->elements--;
return EDPVS_OK;
}
static int
bitmap_test(struct ipset *set, void *value, uint16_t flag)
{
struct bitmap_map *map = set->data;
struct bitmap_elem *e = value;
if (e->id >= map->elements)
return 0;
return do(test, value, map, set->dsize);
}
ipset_adtfn bitmap_adtfn[IPSET_ADT_MAX] = { bitmap_add, bitmap_del, bitmap_test };
void
bitmap_flush(struct ipset *set)
{
struct bitmap_map *map = set->data;
bitmap_zero(map->members, map->elements);
set->elements = 0;
}
void
bitmap_destroy(struct ipset *set)
{
rte_free(set->data);
}
void
bitmap_list(struct ipset *set, struct ipset_info *info)
{
struct bitmap_map *map = set->data;
strcpy(info->name, set->name);
strcpy(info->type, set->type->name);
info->comment = set->comment? true : false;
info->af = AF_INET;
info->entries = set->elements;
info->size = map->size;
do(list, set, &info->bitmap, info->members);
}