Skip to content

Commit 17d365d

Browse files
committed
Implement space saving algorithm
1 parent 80a665b commit 17d365d

File tree

3 files changed

+559
-1
lines changed

3 files changed

+559
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ SRCS-y += sol/main.c
4444
# Libraries.
4545
SRCS-y += lib/mailbox.c lib/net.c lib/flow.c lib/ipip.c \
4646
lib/luajit-ffi-cdata.c lib/launch.c lib/lpm.c lib/acl.c lib/varip.c \
47-
lib/l2.c
47+
lib/l2.c lib/space_saving.c
4848

4949
LDLIBS += $(LDIR) -Bstatic -lluajit-5.1 -Bdynamic -lm -lmnl
5050
CFLAGS += $(WERROR_FLAGS) -I${GATEKEEPER}/include -I/usr/local/include/luajit-2.0/

include/space_saving.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Gatekeeper - DoS protection system.
3+
* Copyright (C) 2016 Digirati LTDA.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _SPACE_SAVING_H_
20+
#define _SPACE_SAVING_H_
21+
22+
#define OVRFLOW 1000000
23+
24+
#include <netinet/in.h>
25+
26+
#include <rte_ether.h>
27+
#include <rte_hash.h>
28+
#include <rte_jhash.h>
29+
30+
#include "list.h"
31+
//#include "gatekeeper_gk.h"
32+
//#include "gatekeeper_net.h"
33+
//#include "gatekeeper_flow.h"
34+
35+
#define DEFAULT_HASH_FUNC rte_jhash
36+
37+
extern struct ip_key *hh_table;
38+
extern struct list_head bkt_head_ip4;
39+
extern struct list_head bkt_head_ip6;
40+
extern int streamlen;
41+
extern int mx;
42+
43+
struct ip_key {
44+
uint16_t proto;
45+
union {
46+
struct v4{
47+
struct in_addr src;
48+
struct in_addr dst;
49+
} v4;
50+
51+
struct v6{
52+
struct in6_addr src;
53+
struct in6_addr dst;
54+
} v6;
55+
} k;
56+
};
57+
58+
/* Data structure for Counter bucket. */
59+
struct counter_bucket
60+
{
61+
uint16_t proto;
62+
int bkt_id;
63+
64+
union {
65+
/* Bucket for IPV4 address. */
66+
struct rte_hash *bkt_ip4;
67+
68+
/* Bucket for IPV6 address. */
69+
struct rte_hash *bkt_ip6;
70+
} bkt;
71+
72+
struct list_head list;
73+
};
74+
75+
/* Data structure of IP data. */
76+
struct ip_data
77+
{
78+
int err;
79+
int bkt_id;
80+
struct ip_key key;
81+
struct counter_bucket ct_bucket;
82+
};
83+
84+
/*
85+
* Create a counter table of size = 1.0/epsion.
86+
* @epsilon is the error parameter for space saving algorithm.
87+
*/
88+
struct rte_hash *
89+
create_counter_table(unsigned int socket_id, uint16_t proto, int counter_id,
90+
int ht_size);
91+
92+
/* Destroy a counter table. */
93+
void destroy_counter_table(uint16_t proto, int counter_id);
94+
95+
/*
96+
* Create a counter bucket.
97+
* Size of each bucket is set to 100 by default.
98+
* TODO: Find a way to vary the size of a bucket to ensure
99+
* optimum memory usage.
100+
*/
101+
struct rte_hash *
102+
create_bucket(unsigned int socket_id, uint16_t proto, int bkt_id);
103+
104+
/* Increment Counter Algorithm. */
105+
static int
106+
increment_counter(unsigned int socket_id, uint16_t proto, struct ip_data **element);
107+
108+
/* Space Saving algorithm. */
109+
//static
110+
int space_saving(unsigned int socket_id, uint16_t proto, struct ip_key *key,
111+
struct rte_hash *ct_table);
112+
113+
int SSiterate(struct rte_hash *ct_table, int proto, int threshold);
114+
115+
int SSEstLow(struct rte_hash *ct_table, struct ip_key *key);
116+
117+
int SSEstUpp(struct rte_hash *ct_table, struct ip_key *key);
118+
119+
#endif /* _SPACE_SAVING_H */

0 commit comments

Comments
 (0)