Linux内核网络协议注册与初始化:从proto_register到tcp_v4_init_sock的深度解析
一、协议注册:proto_register
的核心使命
在Linux网络协议栈中,proto_register
是协议初始化的基石,主要完成三项关键任务:
-
Slab缓存创建(内存管理核心)
prot->slab = kmem_cache_create_usercopy(prot->name, prot->obj_size, ...); if (prot->twsk_prot) prot->twsk_prot->twsk_slab = kmem_cache_create(...);
-
为主协议对象(如
struct sock
)创建高效内存池 -
为TIME_WAIT状态对象(
struct tcp_timewait_sock
)单独创建缓存 -
使用
SLAB_HWCACHE_ALIGN
优化CPU缓存对齐
-
-
全局协议注册
mutex_lock(&proto_list_mutex); list_add(&prot->node, &proto_list); // 加入全局协议链表 assign_proto_idx(prot