net/core/sock_reuseport.c
Source file repositories/reference/linux-study-clean/net/core/sock_reuseport.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/sock_reuseport.c- Extension
.c- Size
- 19652 bytes
- Lines
- 749
- 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.
- 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
net/ip.hnet/sock_reuseport.hlinux/bpf.hlinux/idr.hlinux/filter.hlinux/rcupdate.h
Detected Declarations
function reuseport_has_conns_setfunction __reuseport_get_incoming_cpufunction __reuseport_put_incoming_cpufunction reuseport_get_incoming_cpufunction reuseport_put_incoming_cpufunction reuseport_update_incoming_cpufunction reuseport_sock_indexfunction __reuseport_add_sockfunction __reuseport_detach_sockfunction __reuseport_add_closed_sockfunction __reuseport_detach_closed_sockfunction reuseport_allocfunction reuseport_free_rcufunction reuseport_add_sockfunction reuseport_resurrectfunction shutdownfunction reuseport_detach_sockfunction reuseport_stop_listen_sockfunction packetfunction socketfunction reuseport_attach_progfunction reuseport_detach_progfunction reuseport_growexport reuseport_has_conns_setexport reuseport_allocexport reuseport_add_sockexport reuseport_detach_sockexport reuseport_stop_listen_sockexport reuseport_select_sockexport reuseport_migrate_sockexport reuseport_attach_progexport reuseport_detach_prog
Annotated Snippet
if (reuse->num_closed_socks) {
/* sk was shutdown()ed before */
ret = reuseport_resurrect(sk, reuse, NULL, bind_inany);
goto out;
}
/* Only set reuse->bind_inany if the bind_inany is true.
* Otherwise, it will overwrite the reuse->bind_inany
* which was set by the bind/hash path.
*/
if (bind_inany)
reuse->bind_inany = bind_inany;
goto out;
}
reuse = __reuseport_alloc(INIT_SOCKS);
if (!reuse) {
ret = -ENOMEM;
goto out;
}
id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
if (id < 0) {
kfree(reuse);
ret = id;
goto out;
}
reuse->reuseport_id = id;
reuse->bind_inany = bind_inany;
reuse->socks[0] = sk;
reuse->num_socks = 1;
reuseport_get_incoming_cpu(sk, reuse);
rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
out:
spin_unlock_bh(&reuseport_lock);
return ret;
}
EXPORT_SYMBOL(reuseport_alloc);
static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
{
struct sock_reuseport *more_reuse;
u32 more_socks_size, i;
more_socks_size = reuse->max_socks * 2U;
if (more_socks_size > U16_MAX) {
if (reuse->num_closed_socks) {
/* Make room by removing a closed sk.
* The child has already been migrated.
* Only reqsk left at this point.
*/
struct sock *sk;
sk = reuse->socks[reuse->max_socks - reuse->num_closed_socks];
RCU_INIT_POINTER(sk->sk_reuseport_cb, NULL);
__reuseport_detach_closed_sock(sk, reuse);
return reuse;
}
return NULL;
}
more_reuse = __reuseport_alloc(more_socks_size);
if (!more_reuse)
return NULL;
more_reuse->num_socks = reuse->num_socks;
more_reuse->num_closed_socks = reuse->num_closed_socks;
more_reuse->prog = reuse->prog;
more_reuse->reuseport_id = reuse->reuseport_id;
more_reuse->bind_inany = reuse->bind_inany;
more_reuse->has_conns = reuse->has_conns;
more_reuse->incoming_cpu = reuse->incoming_cpu;
memcpy(more_reuse->socks, reuse->socks,
reuse->num_socks * sizeof(struct sock *));
memcpy(more_reuse->socks +
(more_reuse->max_socks - more_reuse->num_closed_socks),
reuse->socks + (reuse->max_socks - reuse->num_closed_socks),
reuse->num_closed_socks * sizeof(struct sock *));
more_reuse->synq_overflow_ts = READ_ONCE(reuse->synq_overflow_ts);
for (i = 0; i < reuse->max_socks; ++i)
rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
more_reuse);
Annotation
- Immediate include surface: `net/ip.h`, `net/sock_reuseport.h`, `linux/bpf.h`, `linux/idr.h`, `linux/filter.h`, `linux/rcupdate.h`.
- Detected declarations: `function reuseport_has_conns_set`, `function __reuseport_get_incoming_cpu`, `function __reuseport_put_incoming_cpu`, `function reuseport_get_incoming_cpu`, `function reuseport_put_incoming_cpu`, `function reuseport_update_incoming_cpu`, `function reuseport_sock_index`, `function __reuseport_add_sock`, `function __reuseport_detach_sock`, `function __reuseport_add_closed_sock`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: integration implementation candidate.
- 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.