net/ipv4/udp.c
Source file repositories/reference/linux-study-clean/net/ipv4/udp.c
File Facts
- System
- Linux kernel
- Corpus path
net/ipv4/udp.c- Extension
.c- Size
- 99370 bytes
- Lines
- 3893
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bpf-cgroup.hlinux/uaccess.hasm/ioctls.hlinux/memblock.hlinux/highmem.hlinux/types.hlinux/fcntl.hlinux/module.hlinux/socket.hlinux/sockios.hlinux/igmp.hlinux/inetdevice.hlinux/in.hlinux/errno.hlinux/timer.hlinux/mm.hlinux/inet.hlinux/netdevice.hlinux/slab.hlinux/sock_diag.hnet/tcp_states.hlinux/skbuff.hlinux/proc_fs.hlinux/seq_file.hnet/aligned_data.hnet/net_namespace.hnet/icmp.hnet/inet_common.hnet/inet_hashtables.hnet/ip.hnet/ip_tunnels.hnet/route.h
Detected Declarations
struct bpf_iter__udpstruct bpf_udp_iter_statefunction udp_lib_lport_inusefunction sk_for_eachfunction udp_lib_lport_inuse2function udp_reuseport_add_sockfunction sk_for_eachfunction udp_lib_get_portfunction udp_v4_get_portfunction compute_scorefunction udp_ehashfnfunction udp4_lib_lookup1function sk_for_each_rcufunction udp_rehash4function udp_rehash4function udp_unhash4function udp_lib_hash4function udp4_hash4function __udp_is_mcast_sockfunction udp_encap_enablefunction udp_encap_disablefunction __udp4_lib_err_encap_no_skfunction endpointsfunction udp_errfunction udp_flush_pending_framesfunction udp4_hwcsumfunction skb_walk_fragsfunction udp_set_csumfunction udp_send_skbfunction udp_push_pending_framesfunction __udp_cmsg_sendfunction udp_cmsg_sendfunction for_each_cmsghdrfunction udp_sendmsgfunction udp_splice_eoffunction udp_rcvfunction udp_set_dev_scratchfunction udp_skb_csum_unnecessary_setfunction udp_skb_truesizefunction udp_skb_has_head_statefunction udp_rmem_releasefunction __udp_enqueue_schedule_skbfunction udp_skb_dtor_lockedfunction udp_rmem_schedulefunction __udp_enqueue_schedule_skbfunction llist_for_each_entry_safefunction udp_destruct_commonfunction udp_destruct_sock
Annotated Snippet
struct bpf_iter__udp {
__bpf_md_ptr(struct bpf_iter_meta *, meta);
__bpf_md_ptr(struct udp_sock *, udp_sk);
uid_t uid __aligned(8);
int bucket __aligned(8);
};
union bpf_udp_iter_batch_item {
struct sock *sk;
__u64 cookie;
};
struct bpf_udp_iter_state {
struct udp_iter_state state;
unsigned int cur_sk;
unsigned int end_sk;
unsigned int max_sk;
union bpf_udp_iter_batch_item *batch;
};
static int bpf_iter_udp_realloc_batch(struct bpf_udp_iter_state *iter,
unsigned int new_batch_sz, gfp_t flags);
static struct sock *bpf_iter_udp_resume(struct sock *first_sk,
union bpf_udp_iter_batch_item *cookies,
int n_cookies)
{
struct sock *sk = NULL;
int i;
for (i = 0; i < n_cookies; i++) {
sk = first_sk;
udp_portaddr_for_each_entry_from(sk)
if (cookies[i].cookie == atomic64_read(&sk->sk_cookie))
goto done;
}
done:
return sk;
}
static struct sock *bpf_iter_udp_batch(struct seq_file *seq)
{
struct bpf_udp_iter_state *iter = seq->private;
struct udp_iter_state *state = &iter->state;
unsigned int find_cookie, end_cookie;
struct net *net = seq_file_net(seq);
struct udp_table *udptable;
unsigned int batch_sks = 0;
int resume_bucket;
int resizes = 0;
struct sock *sk;
int err = 0;
resume_bucket = state->bucket;
/* The current batch is done, so advance the bucket. */
if (iter->cur_sk == iter->end_sk)
state->bucket++;
udptable = net->ipv4.udp_table;
again:
/* New batch for the next bucket.
* Iterate over the hash table to find a bucket with sockets matching
* the iterator attributes, and return the first matching socket from
* the bucket. The remaining matched sockets from the bucket are batched
* before releasing the bucket lock. This allows BPF programs that are
* called in seq_show to acquire the bucket lock if needed.
*/
find_cookie = iter->cur_sk;
end_cookie = iter->end_sk;
iter->cur_sk = 0;
iter->end_sk = 0;
batch_sks = 0;
for (; state->bucket <= udptable->mask; state->bucket++) {
struct udp_hslot *hslot2 = &udptable->hash2[state->bucket].hslot;
if (hlist_empty(&hslot2->head))
goto next_bucket;
spin_lock_bh(&hslot2->lock);
sk = hlist_entry_safe(hslot2->head.first, struct sock,
__sk_common.skc_portaddr_node);
/* Resume from the first (in iteration order) unseen socket from
* the last batch that still exists in resume_bucket. Most of
* the time this will just be where the last iteration left off
* in resume_bucket unless that socket disappeared between
* reads.
*/
if (state->bucket == resume_bucket)
Annotation
- Immediate include surface: `linux/bpf-cgroup.h`, `linux/uaccess.h`, `asm/ioctls.h`, `linux/memblock.h`, `linux/highmem.h`, `linux/types.h`, `linux/fcntl.h`, `linux/module.h`.
- Detected declarations: `struct bpf_iter__udp`, `struct bpf_udp_iter_state`, `function udp_lib_lport_inuse`, `function sk_for_each`, `function udp_lib_lport_inuse2`, `function udp_reuseport_add_sock`, `function sk_for_each`, `function udp_lib_get_port`, `function udp_v4_get_port`, `function compute_score`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.