DragoflyBSD网络堆栈简介

dragonflyseallyhs 发布于 2015/08/24 13:36
阅读 431
收藏 0

以下简介以TCP为例。为方便绘制,我们以两个CPU为例子。

 

DragonflyBSD上每个CPU有一个NETISR内核线程,而且被绑定从属CPU上。每个NETISR只访问他自己的TCP资源和路由资源。划分见下图:

我们用以下这段代码来介绍以下网络堆栈的执行过程:

s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, sizeof(A:Ap));

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);

首先来看socket()执行过程:

 

s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, sizeof(A:Ap));

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);


紧接着再看connect()执行过程:

 

s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, sizeof(A:Ap));

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);

          


s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, siezof(A:Ap));

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);

 

我们来看write,假设sizeof(buf)为8KB(msg嵌在mbuf中)



s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, sizeof(A:Ap));

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);

 

再就是read,假设sizeof(buf)大小为1.4KB



s = socket(AF_INET, SOCK_STREAM, 0);

connect(s, A:Ap, sizeof(A:Ap))

write(s, buf, sizeof(buf));

read(s, buf, sizeof(buf));

close(s);

 

最后close()



我们用以下这段代码来介绍以下服务端的网络堆栈执行过程:

 

s = socket(AF_INET, SOCK_STREAM, 0);

bind(s, A:Ap, sizeof(A:Ap));

listen(s, 0);

s1 = accept(s, NULL, NULL);

write(s1, buf1, sizeof(buf1));

read(s1, buf2, sizeof(buf2));

close(s1);

close(s);

 

红色部分在前面的段落中已经介绍过,将会略过。

 

我们从bind()开始:

s = socket(AF_INET, SOCK_STREAM, 0);

bind(s, A:Ap, sizeof(A:Ap));

listen(s, 0);

s1 = accept(s, NULL, NULL);

write(s1, buf1, sizeof(buf1));

read(s1, buf2, sizeof(buf2));

close(s1);

close(s);



紧接着来看listen()

s = socket(AF_INET, SOCK_STREAM, 0);

bind(s, A:Ap, sizeof(A:Ap));

listen(s, 0);

s1 = accept(s, NULL, NULL);

write(s1, buf1, sizeof(buf1));

read(s1, buf2, sizeof(buf2));

close(s1);

close(s);



     


再来看accept

s = socket(AF_INET, SOCK_STREAM, 0);

bind(s, A:Ap, sizeof(A:Ap));

listen(s, 0);

s1 = accept(s, NULL, NULL);

write(s1, buf1, sizeof(buf1));

read(s1, buf2, sizeof(buf2));

close(s1);

close(s);


      

             


最后来看listen socketclose()

s = socket(AF_INET, SOCK_STREAM, 0);

bind(s, A:Ap, sizeof(A:Ap));

listen(s, 0);

s1 = accept(s, NULL, NULL);

write(s1, buf1, sizeof(buf1));

read(s1, buf2, sizeof(buf2));

close(s1);

close(s);


DragonflyBSD官方微信:BSDchina 或 BSD操作系统 加关注  邮箱:seallyhs@dragonflybsd.org

加载中
返回顶部
顶部