Commit 9e8ca27a authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov

net: bpf: Add bpf_seq_afinfo in udp_iter_state

Similar to tcp_iter_state, a new field bpf_seq_afinfo is
added to udp_iter_state to provide bpf udp iterator
afinfo.

This does not change /proc/net/{udp, udp6} behavior. But
it enables bpf iterator to avoid get afinfo from PDE_DATA
and iterate through all udp and udp6 sockets in one pass.
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200623230812.3988347-1-yhs@fb.com
parent 478cfbdf
...@@ -440,6 +440,7 @@ struct udp_seq_afinfo { ...@@ -440,6 +440,7 @@ struct udp_seq_afinfo {
struct udp_iter_state { struct udp_iter_state {
struct seq_net_private p; struct seq_net_private p;
int bucket; int bucket;
struct udp_seq_afinfo *bpf_seq_afinfo;
}; };
void *udp_seq_start(struct seq_file *seq, loff_t *pos); void *udp_seq_start(struct seq_file *seq, loff_t *pos);
......
...@@ -2826,10 +2826,15 @@ EXPORT_SYMBOL(udp_prot); ...@@ -2826,10 +2826,15 @@ EXPORT_SYMBOL(udp_prot);
static struct sock *udp_get_first(struct seq_file *seq, int start) static struct sock *udp_get_first(struct seq_file *seq, int start)
{ {
struct sock *sk; struct sock *sk;
struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); struct udp_seq_afinfo *afinfo;
struct udp_iter_state *state = seq->private; struct udp_iter_state *state = seq->private;
struct net *net = seq_file_net(seq); struct net *net = seq_file_net(seq);
if (state->bpf_seq_afinfo)
afinfo = state->bpf_seq_afinfo;
else
afinfo = PDE_DATA(file_inode(seq->file));
for (state->bucket = start; state->bucket <= afinfo->udp_table->mask; for (state->bucket = start; state->bucket <= afinfo->udp_table->mask;
++state->bucket) { ++state->bucket) {
struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket]; struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket];
...@@ -2841,7 +2846,8 @@ static struct sock *udp_get_first(struct seq_file *seq, int start) ...@@ -2841,7 +2846,8 @@ static struct sock *udp_get_first(struct seq_file *seq, int start)
sk_for_each(sk, &hslot->head) { sk_for_each(sk, &hslot->head) {
if (!net_eq(sock_net(sk), net)) if (!net_eq(sock_net(sk), net))
continue; continue;
if (sk->sk_family == afinfo->family) if (afinfo->family == AF_UNSPEC ||
sk->sk_family == afinfo->family)
goto found; goto found;
} }
spin_unlock_bh(&hslot->lock); spin_unlock_bh(&hslot->lock);
...@@ -2853,13 +2859,20 @@ static struct sock *udp_get_first(struct seq_file *seq, int start) ...@@ -2853,13 +2859,20 @@ static struct sock *udp_get_first(struct seq_file *seq, int start)
static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk) static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
{ {
struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); struct udp_seq_afinfo *afinfo;
struct udp_iter_state *state = seq->private; struct udp_iter_state *state = seq->private;
struct net *net = seq_file_net(seq); struct net *net = seq_file_net(seq);
if (state->bpf_seq_afinfo)
afinfo = state->bpf_seq_afinfo;
else
afinfo = PDE_DATA(file_inode(seq->file));
do { do {
sk = sk_next(sk); sk = sk_next(sk);
} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family)); } while (sk && (!net_eq(sock_net(sk), net) ||
(afinfo->family != AF_UNSPEC &&
sk->sk_family != afinfo->family)));
if (!sk) { if (!sk) {
if (state->bucket <= afinfo->udp_table->mask) if (state->bucket <= afinfo->udp_table->mask)
...@@ -2904,9 +2917,14 @@ EXPORT_SYMBOL(udp_seq_next); ...@@ -2904,9 +2917,14 @@ EXPORT_SYMBOL(udp_seq_next);
void udp_seq_stop(struct seq_file *seq, void *v) void udp_seq_stop(struct seq_file *seq, void *v)
{ {
struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file)); struct udp_seq_afinfo *afinfo;
struct udp_iter_state *state = seq->private; struct udp_iter_state *state = seq->private;
if (state->bpf_seq_afinfo)
afinfo = state->bpf_seq_afinfo;
else
afinfo = PDE_DATA(file_inode(seq->file));
if (state->bucket <= afinfo->udp_table->mask) if (state->bucket <= afinfo->udp_table->mask)
spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock); spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment