net/core/sock_map.c
Source file repositories/reference/linux-study-clean/net/core/sock_map.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/sock_map.c- Extension
.c- Size
- 48426 bytes
- Lines
- 1971
- 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.hlinux/btf_ids.hlinux/filter.hlinux/errno.hlinux/file.hlinux/net.hlinux/workqueue.hlinux/skmsg.hlinux/list.hlinux/jhash.hlinux/sock_diag.hnet/udp.h
Detected Declarations
struct bpf_stabstruct sock_map_seq_infostruct bpf_iter__sockmapstruct bpf_shtab_elemstruct bpf_shtab_bucketstruct bpf_shtabstruct sock_hash_seq_infostruct sockmap_linkfunction sock_map_get_from_fdfunction sock_map_prog_detachfunction sock_map_sk_acquirefunction sock_map_sk_releasefunction sock_map_add_linkfunction sock_map_del_linkfunction sock_map_unreffunction sock_map_init_protofunction sock_map_linkfunction sock_map_freefunction sock_map_release_progsfunction __sock_map_deletefunction sock_map_delete_from_linkfunction sock_map_delete_elemfunction sock_map_get_next_keyfunction sock_map_update_commonfunction sock_map_op_okayfunction sock_map_redirect_allowedfunction sock_map_sk_is_suitablefunction sock_map_sk_state_allowedfunction sock_map_update_elem_sysfunction sock_map_update_elemfunction sock_map_seq_showfunction sock_map_seq_stopfunction sock_map_init_seq_privatefunction sock_map_fini_seq_privatefunction sock_map_mem_usagefunction sock_hash_bucket_hashfunction sock_hash_lookup_elem_rawfunction hlist_for_each_entry_rcufunction sock_hash_free_elemfunction sock_hash_delete_from_linkfunction sock_hash_delete_elemfunction sock_hash_update_commonfunction sock_hash_get_next_keyfunction sock_hash_freefunction hlist_for_each_entry_safefunction sock_hash_release_progsfunction sock_hash_seq_showfunction sock_hash_seq_stop
Annotated Snippet
struct bpf_stab {
struct bpf_map map;
struct sock **sks;
struct sk_psock_progs progs;
spinlock_t lock;
};
#define SOCK_CREATE_FLAG_MASK \
(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
/* This mutex is used to
* - protect race between prog/link attach/detach and link prog update, and
* - protect race between releasing and accessing map in bpf_link.
* A single global mutex lock is used since it is expected contention is low.
*/
static DEFINE_MUTEX(sockmap_mutex);
static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
struct bpf_prog *old, struct bpf_link *link,
u32 which);
static struct sk_psock_progs *sock_map_progs(struct bpf_map *map);
static struct bpf_map *sock_map_alloc(union bpf_attr *attr)
{
struct bpf_stab *stab;
if (attr->max_entries == 0 ||
attr->key_size != 4 ||
(attr->value_size != sizeof(u32) &&
attr->value_size != sizeof(u64)) ||
attr->map_flags & ~SOCK_CREATE_FLAG_MASK)
return ERR_PTR(-EINVAL);
stab = bpf_map_area_alloc(sizeof(*stab), NUMA_NO_NODE);
if (!stab)
return ERR_PTR(-ENOMEM);
bpf_map_init_from_attr(&stab->map, attr);
spin_lock_init(&stab->lock);
stab->sks = bpf_map_area_alloc((u64) stab->map.max_entries *
sizeof(struct sock *),
stab->map.numa_node);
if (!stab->sks) {
bpf_map_area_free(stab);
return ERR_PTR(-ENOMEM);
}
return &stab->map;
}
int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog)
{
struct bpf_map *map;
int ret;
if (attr->attach_flags || attr->replace_bpf_fd)
return -EINVAL;
CLASS(fd, f)(attr->target_fd);
map = __bpf_map_get(f);
if (IS_ERR(map))
return PTR_ERR(map);
mutex_lock(&sockmap_mutex);
ret = sock_map_prog_update(map, prog, NULL, NULL, attr->attach_type);
mutex_unlock(&sockmap_mutex);
return ret;
}
int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
{
struct bpf_prog *prog;
struct bpf_map *map;
int ret;
if (attr->attach_flags || attr->replace_bpf_fd)
return -EINVAL;
CLASS(fd, f)(attr->target_fd);
map = __bpf_map_get(f);
if (IS_ERR(map))
return PTR_ERR(map);
prog = bpf_prog_get(attr->attach_bpf_fd);
if (IS_ERR(prog))
return PTR_ERR(prog);
if (prog->type != ptype) {
ret = -EINVAL;
goto put_prog;
Annotation
- Immediate include surface: `linux/bpf.h`, `linux/btf_ids.h`, `linux/filter.h`, `linux/errno.h`, `linux/file.h`, `linux/net.h`, `linux/workqueue.h`, `linux/skmsg.h`.
- Detected declarations: `struct bpf_stab`, `struct sock_map_seq_info`, `struct bpf_iter__sockmap`, `struct bpf_shtab_elem`, `struct bpf_shtab_bucket`, `struct bpf_shtab`, `struct sock_hash_seq_info`, `struct sockmap_link`, `function sock_map_get_from_fd`, `function sock_map_prog_detach`.
- 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.