Skip to content

Commit cc17c68

Browse files
committed
Minor refactor of network filtering
The filter_ifindex() and filter_network_ns() functios are often used together. Therefore, add a filter_network() function that budles these two together. Signed-off-by: Simon Sundberg <simon.sundberg@kau.se>
1 parent d621b4f commit cc17c68

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

netstacklat/netstacklat.bpf.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,17 @@ static bool filter_network_ns(struct sk_buff *skb, struct sock *sk)
188188
return get_network_ns(skb, sk) == user_config.network_ns;
189189
}
190190

191+
static bool filter_network(struct sk_buff *skb, struct sock *sk)
192+
{
193+
if (!filter_ifindex(skb ? skb->skb_iif : sk ? sk->sk_rx_dst_ifindex : 0))
194+
return false;
195+
196+
return filter_network_ns(skb, sk);
197+
}
191198

192199
static void record_skb_latency(struct sk_buff *skb, struct sock *sk, enum netstacklat_hook hook)
193200
{
194201
struct hist_key key = { .hook = hook };
195-
u32 ifindex;
196202

197203
if (bpf_core_field_exists(skb->tstamp_type)) {
198204
/*
@@ -217,15 +223,11 @@ static void record_skb_latency(struct sk_buff *skb, struct sock *sk, enum netsta
217223
return;
218224
}
219225

220-
ifindex = skb->skb_iif;
221-
if (!filter_ifindex(ifindex))
222-
return;
223-
224-
if (!filter_network_ns(skb, sk))
226+
if (!filter_network(skb, sk))
225227
return;
226228

227229
if (user_config.groupby_ifindex)
228-
key.ifindex = ifindex;
230+
key.ifindex = skb->skb_iif;
229231

230232
record_latency_since(skb->tstamp, &key);
231233
}
@@ -305,7 +307,6 @@ static void record_socket_latency(struct sock *sk, struct sk_buff *skb,
305307
{
306308
struct hist_key key = { .hook = hook };
307309
u64 cgroup = 0;
308-
u32 ifindex;
309310

310311
if (!filter_min_sockqueue_len(sk))
311312
return;
@@ -316,15 +317,11 @@ static void record_socket_latency(struct sock *sk, struct sk_buff *skb,
316317
if (!filter_current_task(cgroup))
317318
return;
318319

319-
ifindex = skb ? skb->skb_iif : sk->sk_rx_dst_ifindex;
320-
if (!filter_ifindex(ifindex))
321-
return;
322-
323-
if (!filter_network_ns(skb, sk))
320+
if (!filter_network(skb, sk))
324321
return;
325322

326323
if (user_config.groupby_ifindex)
327-
key.ifindex = ifindex;
324+
key.ifindex = skb ? skb->skb_iif : sk->sk_rx_dst_ifindex;
328325
if (user_config.groupby_cgroup)
329326
key.cgroup = cgroup;
330327

0 commit comments

Comments
 (0)