Skip to content

Commit 40c4f8b

Browse files
authored
Allow flow filtering to coexists with pkt drop and rtt ebpf hooks (#318)
Signed-off-by: Mohamed Mahmoud <[email protected]>
1 parent e41327e commit 40c4f8b

File tree

8 files changed

+74
-49
lines changed

8 files changed

+74
-49
lines changed

bpf/flows.c

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "flows_filter.h"
4444

4545
static inline int flow_monitor(struct __sk_buff *skb, u8 direction) {
46-
filter_action action = ACCEPT;
4746
// If sampling is defined, will only parse 1 out of "sampling" flows
4847
if (sampling > 1 && (bpf_get_prandom_u32() % sampling) != 0) {
4948
return TC_ACT_OK;
@@ -71,54 +70,9 @@ static inline int flow_monitor(struct __sk_buff *skb, u8 direction) {
7170
id.direction = direction;
7271

7372
// check if this packet need to be filtered if filtering feature is enabled
74-
if (enable_flows_filtering) {
75-
u32 *filter_counter_p = NULL;
76-
u32 initVal = 1, key = 0;
77-
if (is_flow_filtered(&id, &action) != 0 && action != MAX_FILTER_ACTIONS) {
78-
// we have matching rules follow through the actions to decide if we should accept or reject the flow
79-
// and update global counter for both cases
80-
u32 reject_key = FILTER_FLOWS_REJECT_KEY, accept_key = FILTER_FLOWS_ACCEPT_KEY;
81-
bool skip = false;
82-
83-
switch (action) {
84-
case REJECT:
85-
key = reject_key;
86-
skip = true;
87-
break;
88-
case ACCEPT:
89-
key = accept_key;
90-
break;
91-
// should never come here
92-
case MAX_FILTER_ACTIONS:
93-
return TC_ACT_OK;
94-
}
95-
96-
// update global counter for flows dropped by filter
97-
filter_counter_p = bpf_map_lookup_elem(&global_counters, &key);
98-
if (!filter_counter_p) {
99-
bpf_map_update_elem(&global_counters, &key, &initVal, BPF_ANY);
100-
} else {
101-
__sync_fetch_and_add(filter_counter_p, 1);
102-
}
103-
if (skip) {
104-
return TC_ACT_OK;
105-
}
106-
} else {
107-
// we have no matching rules so we update global counter for flows that are not matched by any rule
108-
key = FILTER_FLOWS_NOMATCH_KEY;
109-
filter_counter_p = bpf_map_lookup_elem(&global_counters, &key);
110-
if (!filter_counter_p) {
111-
bpf_map_update_elem(&global_counters, &key, &initVal, BPF_ANY);
112-
} else {
113-
__sync_fetch_and_add(filter_counter_p, 1);
114-
}
115-
// we have accept rule but no match so we can't let mismatched flows in the hashmap table.
116-
if (action == ACCEPT || action == MAX_FILTER_ACTIONS) {
117-
return TC_ACT_OK;
118-
} else {
119-
// we have reject rule and no match so we can add the flows to the hashmap table.
120-
}
121-
}
73+
bool skip = check_and_do_flow_filtering(&id);
74+
if (skip) {
75+
return TC_ACT_OK;
12276
}
12377

12478
int dns_errno = 0;

bpf/pkt_drops.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ static inline int trace_pkt_drop(void *ctx, u8 state, struct sk_buff *skb,
4747
return 0;
4848
}
4949

50+
// check if this packet need to be filtered if filtering feature is enabled
51+
bool skip = check_and_do_flow_filtering(&id);
52+
if (skip) {
53+
return 0;
54+
}
55+
5056
long ret = 0;
5157
for (direction dir = INGRESS; dir < MAX_DIRECTION; dir++) {
5258
id.direction = dir;

bpf/rtt_tracker.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ static inline int calculate_flow_rtt_tcp(struct sock *sk, struct sk_buff *skb) {
124124
rtt = BPF_CORE_READ(ts, srtt_us) >> 3;
125125
rtt *= 1000u;
126126

127+
// check if this packet need to be filtered if filtering feature is enabled
128+
bool skip = check_and_do_flow_filtering(&id);
129+
if (skip) {
130+
return 0;
131+
}
132+
127133
// update flow with rtt info
128134
id.direction = INGRESS;
129135
ret = rtt_lookup_and_update_flow(&id, flags, rtt);

bpf/utils.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "types.h"
55
#include "maps_definition.h"
6+
#include "flows_filter.h"
67

78
// sets the TCP header flags for connection information
89
static inline void set_flags(struct tcphdr *th, u16 *flags) {
@@ -276,4 +277,62 @@ static inline long pkt_drop_lookup_and_update_flow(struct sk_buff *skb, flow_id
276277
return -1;
277278
}
278279

280+
/*
281+
* check if flow filter is enabled and if we need to continue processing the packet or not
282+
*/
283+
static inline bool check_and_do_flow_filtering(flow_id *id) {
284+
// check if this packet need to be filtered if filtering feature is enabled
285+
if (enable_flows_filtering) {
286+
filter_action action = ACCEPT;
287+
u32 *filter_counter_p = NULL;
288+
u32 initVal = 1, key = 0;
289+
if (is_flow_filtered(id, &action) != 0 && action != MAX_FILTER_ACTIONS) {
290+
// we have matching rules follow through the actions to decide if we should accept or reject the flow
291+
// and update global counter for both cases
292+
u32 reject_key = FILTER_FLOWS_REJECT_KEY, accept_key = FILTER_FLOWS_ACCEPT_KEY;
293+
bool skip = false;
294+
295+
switch (action) {
296+
case REJECT:
297+
key = reject_key;
298+
skip = true;
299+
break;
300+
case ACCEPT:
301+
key = accept_key;
302+
break;
303+
// should never come here
304+
case MAX_FILTER_ACTIONS:
305+
return true;
306+
}
307+
308+
// update global counter for flows dropped by filter
309+
filter_counter_p = bpf_map_lookup_elem(&global_counters, &key);
310+
if (!filter_counter_p) {
311+
bpf_map_update_elem(&global_counters, &key, &initVal, BPF_ANY);
312+
} else {
313+
__sync_fetch_and_add(filter_counter_p, 1);
314+
}
315+
if (skip) {
316+
return true;
317+
}
318+
} else {
319+
// we have no matching rules so we update global counter for flows that are not matched by any rule
320+
key = FILTER_FLOWS_NOMATCH_KEY;
321+
filter_counter_p = bpf_map_lookup_elem(&global_counters, &key);
322+
if (!filter_counter_p) {
323+
bpf_map_update_elem(&global_counters, &key, &initVal, BPF_ANY);
324+
} else {
325+
__sync_fetch_and_add(filter_counter_p, 1);
326+
}
327+
// we have accept rule but no match so we can't let mismatched flows in the hashmap table.
328+
if (action == ACCEPT || action == MAX_FILTER_ACTIONS) {
329+
return true;
330+
} else {
331+
// we have reject rule and no match so we can add the flows to the hashmap table.
332+
}
333+
}
334+
}
335+
return false;
336+
}
337+
279338
#endif // __UTILS_H__

pkg/ebpf/bpf_arm64_bpfel.o

888 Bytes
Binary file not shown.

pkg/ebpf/bpf_powerpc_bpfel.o

888 Bytes
Binary file not shown.

pkg/ebpf/bpf_s390_bpfeb.o

872 Bytes
Binary file not shown.

pkg/ebpf/bpf_x86_bpfel.o

888 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)