本系列以 Linux Kernel 1.2.13 為分析對象,逐層拆解內核網絡子系統的實現原理。之所以挑選這個看似「古董」的版本,是因為它恰好站在一個歷史節點上:網絡協議棧的分層已經成型,功能邊界清晰,同時又沒有被後續版本不斷追加的複雜度所淹沒。
選擇這個版本的理由大致有四點:
系統上電後依次執行 bootsect.S、setup.S 與 head.S,這一段的邏輯與 0.11 內核基本一致,此處不再重複。完成前期準備後,控制權交給 init/main.c 中的 start_kernel()。
start_kernel() 依序完成平台初始化、內存初始化、陷阱表初始化、中斷初始化、進程調度初始化與緩衝區初始化,接著調用 sock_init() 建立網絡子系統,最後開中斷並執行 init() 進入用戶空間階段。
該函數位於 net/socket.c:
void sock_init(void) /* 網絡棧初始化 */
{
int i;
printk("Swansea University Computer Society NET3.019\n");
/* Initialize all address (protocol) families. */
for (i = 0; i < NPROTO; ++i) pops[i] = NULL;
/* Initialize the protocols module. */
proto_init();
#ifdef CONFIG_NET
/* Initialize the DEV module. */
dev_init();
/* And the bottom half handler */
bh_base[NET_BH].routine = net_bh;
enable_bh(NET_BH);
#endif
}
第一句實質工作是清空地址族分派表。NPROTO 在頭文件中定義為 16:
#define NPROTO 16 /* should be enough for now.. */
static struct proto_ops *pops[NPROTO];
proto_ops 定義在 include/linux/net.h,本質上是一張函數指針表,把「BSD 套接字」這層抽象接口映射到 INET 等具體協議族的操作集合。可以把它理解為文件系統裡 file_operations 的網絡版本:所有操作都通過傳入的 socket 對象完成。
struct proto_ops {
int family;
int (*create) (struct socket *sock, int protocol);
int (*dup) (struct socket *newsock, struct socket *oldsock);
int (*release) (struct socket *sock, struct socket *peer);
int (*bind) (struct socket *sock, struct sockaddr *umyaddr,
int sockaddr_len);
int (*connect) (struct socket *sock, struct sockaddr *uservaddr,
int sockaddr_len, int flags);
int (*socketpair)(struct socket *sock1, struct socket *sock2);
int (*accept) (struct socket *sock, struct socket *newsock,
int flags);
int (*getname) (struct socket *sock, struct sockaddr *uaddr,
int *usockaddr_len, int peer);
int (*read) (struct socket *sock, char *ubuf, int size,
int nonblock);
int (*write) (struct socket *sock, char *ubuf, int size,
int nonblock);
int (*select) (struct socket *sock, int sel_type,
select_table *wait);
int (*ioctl) (struct socket *sock, unsigned int cmd,
unsigned long arg);
int (*listen) (struct socket *sock, int len);
int (*send) (struct socket *sock, void *buff, int len, int nonblock,
unsigned flags);
int (*recv) (struct socket *sock, void *buff, int len, int nonblock,
unsigned flags);
int (*sendto) (struct socket *sock, void *buff, int len, int nonblock,
unsigned flags, struct sockaddr *, int addr_len);
int (*recvfrom) (struct socket *sock, void *buff, int len, int nonblock,
unsigned flags, struct sockaddr *, int *addr_len);
int (*shutdown) (struct socket *sock, int flags);
int (*setsockopt)(struct socket *sock, int level, int optname,
char *optval, int optlen);
int (*getsockopt)(struct socket *sock, int level, int optname,
char *optval, int *optlen);
int (*fcntl) (struct socket *sock, unsigned int cmd,
unsigned long arg);
};
void proto_init(void)
{
extern struct net_proto protocols[]; /* 定義在 protocols.c */
struct net_proto *pro;
/* Kick all configured protocols. */
pro = protocols;
while (pro->name != NULL) {
(*pro->init_func)(pro);
pro++;
}
/* We're all done... */
}
全局協議表的內容由編譯配置決定:
struct net_proto protocols[] = {
#ifdef CONFIG_UNIX
{ "UNIX", unix_proto_init },
#endif
#if defined(CONFIG_IPX) || defined(CONFIG_ATALK)
{ "802.2", p8022_proto_init },
{ "SNAP", snap_proto_init },
#endif
#ifdef CONFIG_AX25
{ "AX.25", ax25_proto_init },
#endif
#ifdef CONFIG_INET
{ "INET", inet_proto_init },
#endif
#ifdef CONFIG_IPX
{ "IPX", ipx_proto_init },
#endif
#ifdef CONFIG_ATALK
{ "DDP", atalk_proto_init },
#endif
{ NULL, NULL }
};
struct net_proto {
char *name; /* Protocol name */
void (*init_func)(struct net_proto *); /* Bootstrap */
};
後續討論聚焦於最標準的 INET 域。
inet_proto_init() 實現於 net/inet/af_inet.c,其中關鍵一步是向通用層註冊操作集合:
(void) sock_register(inet_proto_ops.family, &inet_proto_ops);
sock_register() 的實現很樸素:關中斷,在 pops 表中找第一個空位填入,回填 family 字段,再開中斷返回下標。
int sock_register(int family, struct proto_ops *ops)
{
int i;
cli(); /* 關中斷 */
for (i = 0; i < NPROTO; i++) { /* 查找空閒表項 */
if (pops[i] != NULL) continue; /* 不空則跳過 */
pops[i] = ops;
pops[i]->family = family;
sti(); /* 開中斷 */
return i; /* 返回協議向量號 */
}
sti(); /* 異常也要開中斷 */
return -ENOMEM;
}
傳入的 inet_proto_ops 就是 INET 域的操作函數表,首字段 AF_INET 的宏值為 2:
static struct proto_ops inet_proto_ops = {
AF_INET,
inet_create,
inet_dup,
inet_release,
inet_bind,
inet_connect,
inet_socketpair,
inet_accept,
inet_getname,
inet_read,
inet_write,
inet_select,
inet_ioctl,
inet_listen,
inet_send,
inet_recv,
inet_sendto,
inet_recvfrom,
inet_shutdown,
inet_setsockopt,
inet_getsockopt,
inet_fcntl,
};
接著把內建於內核的 IP 協議掛入哈希鏈表,並完成地址解析層與 IP 層的初始化:
printk("IP Protocols: ");
for (p = inet_protocol_base; p != NULL;) {
struct inet_protocol *tmp = (struct inet_protocol *) p->next;
inet_add_protocol(p);
printk("%s%s", p->name, tmp ? ", " : "\n");
p = tmp;
}
/* Set the ARP module up */
arp_init();
/* Set the IP module up */
ip_init();
協議初始化結束後,再執行 dev_init() 完成設備層的初始化。至此,網絡子系統的骨架搭建完畢。
本文只勾勒了初始化的主幹路徑,細節留待後續文章結合源碼逐段展開。