TCP连接的建立(一)

来源:互联网 发布:zookeeper linux 下载 编辑:程序博客网 时间:2024/05/29 04:30

服务端建立连接过程

一般情况下,建立一个TCP连接的过程为:

客户端发送SYN段,标识希望连接的服务器端口以及初始序号

服务端发送回一个包含服务器初始序号以及对客户端SYN段确认的SYN+ACK段作为应答,由于一个SYN占用一个序号,因此确认序号设置为客户端初始序号加1

客户端发送确认序号为服务器初始序号加1的ACK段,对服务器SYN段进行确认。

对于服务端而言,从接收到客户端的连接请求起,必须等到接收到客户端对服务端SYN+ACK的确认,连接才算完成。在这个过程中,不但存在TCP连接的中间状态,而且还要等待用户进程调用accept(),因此需要在服务端把信息都保存起来,直到用户进程调用accept()为止。


从图中可以看到在TCP传输控制块inet_connection_sock结构中有个request_sock_queue结构类型的成员icsk_accept_queue,用来保存正在建立连接和已经建立连接但未被accept的传输控制块。在request_sock_queue结构中rskq_accept_head和rskq_accept_tail构成的链表中,保存了一个已经完成连接的连接请求块;而在listen_sock结构实例的syn_table散列表中保存着两个连接状态中德尔连接请求块,请求块之间使用dl_next形成链表。

虽然每个TCP传输控制块都有请求连接控制块icsk_accept_queue,但在创建之初是不完整的,request_sock_queue结构中的listen_opt指针为NULL,即还没有为保存SYN_RECV状态的请求连接控制块分配空间。

listen系统调用不仅使TCP进入LISTEN状态,同时还为保存SYN_RECV状态的请求连接控制块分配空间,其中syn_table散列表大小由listen系统调用的参数backlog控制。


bind的系统调用实现

bind系统调用通过套接口层的inet_bind()之后,便会调用传输层的函数,TCP中的传输层接口函数为inet_csk_get_port()。

如果待绑定端口为0,则自动为套接口分配一个可用的端口

如果是指定端口号,则需要在已绑定的信息中查找,如果找到,则说明该端口号已经被使用且不能复用,则bind失败;如果查找不到,则创建新的bind信息块添加到散列表中完成对bind的操作。

/* Obtain a reference to a local port for the given sock, * if snum is zero it means select any available local port. */int inet_csk_get_port(struct sock *sk, unsigned short snum){struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;struct inet_bind_hashbucket *head;struct hlist_node *node;struct inet_bind_bucket *tb;int ret, attempts = 5;struct net *net = sock_net(sk);int smallest_size = -1, smallest_rover;local_bh_disable();if (!snum) {int remaining, rover, low, high;again:inet_get_local_port_range(&low, &high);remaining = (high - low) + 1;smallest_rover = rover = net_random() % remaining + low;smallest_size = -1;do {head = &hashinfo->bhash[inet_bhashfn(net, rover,hashinfo->bhash_size)];spin_lock(&head->lock);inet_bind_bucket_for_each(tb, node, &head->chain)if (ib_net(tb) == net && tb->port == rover) {if (tb->fastreuse > 0 &&    sk->sk_reuse &&    sk->sk_state != TCP_LISTEN &&    (tb->num_owners < smallest_size || smallest_size == -1)) {smallest_size = tb->num_owners;smallest_rover = rover;if (atomic_read(&hashinfo->bsockets) > (high - low) + 1) {spin_unlock(&head->lock);snum = smallest_rover;goto have_snum;}}goto next;}break;next:spin_unlock(&head->lock);if (++rover > high)rover = low;} while (--remaining > 0);/* Exhausted local port range during search?  It is not * possible for us to be holding one of the bind hash * locks if this test triggers, because if 'remaining' * drops to zero, we broke out of the do/while loop at * the top level, not from the 'break;' statement. */ret = 1;if (remaining <= 0) {if (smallest_size != -1) {snum = smallest_rover;goto have_snum;}goto fail;}/* OK, here is the one we will use.  HEAD is * non-NULL and we hold it's mutex. */snum = rover;} else {have_snum:head = &hashinfo->bhash[inet_bhashfn(net, snum,hashinfo->bhash_size)];spin_lock(&head->lock);inet_bind_bucket_for_each(tb, node, &head->chain)if (ib_net(tb) == net && tb->port == snum)goto tb_found;}tb = NULL;goto tb_not_found;tb_found:if (!hlist_empty(&tb->owners)) {if (tb->fastreuse > 0 &&    sk->sk_reuse && sk->sk_state != TCP_LISTEN &&    smallest_size == -1) {goto success;} else {ret = 1;if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb)) {if (sk->sk_reuse && sk->sk_state != TCP_LISTEN &&    smallest_size != -1 && --attempts >= 0) {spin_unlock(&head->lock);goto again;}goto fail_unlock;}}}tb_not_found:ret = 1;if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,net, head, snum)) == NULL)goto fail_unlock;if (hlist_empty(&tb->owners)) {if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)tb->fastreuse = 1;elsetb->fastreuse = 0;} else if (tb->fastreuse &&   (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))tb->fastreuse = 0;success:if (!inet_csk(sk)->icsk_bind_hash)inet_bind_hash(sk, tb, snum);WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);ret = 0;fail_unlock:spin_unlock(&head->lock);fail:local_bh_enable();return ret;}
listen的系统调用实现

inet_listen()函数为listen系统调用套接口层的实现,该函数会做一些简单的检查,然后调用inet_csk_listen_start()函数实现侦听。实现侦听的过程:为管理连接请求块的散列表分配存储空间,接着使TCP传输控制块的状态迁移到LISTEN状态,然后将传输控制块添加到侦听散列表中。

int inet_csk_listen_start(struct sock *sk, const int nr_table_entries){struct inet_sock *inet = inet_sk(sk);struct inet_connection_sock *icsk = inet_csk(sk);int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);if (rc != 0)return rc;sk->sk_max_ack_backlog = 0;sk->sk_ack_backlog = 0;inet_csk_delack_init(sk);/* There is race window here: we announce ourselves listening, * but this transition is still not validated by get_port(). * It is OK, because this socket enters to hash table only * after validation is complete. */sk->sk_state = TCP_LISTEN;if (!sk->sk_prot->get_port(sk, inet->num)) {inet->sport = htons(inet->num);sk_dst_reset(sk);sk->sk_prot->hash(sk);return 0;}sk->sk_state = TCP_CLOSE;__reqsk_queue_destroy(&icsk->icsk_accept_queue);return -EADDRINUSE;}
accept的系统调用实现

inet_accept()为accept系统调用的套接口层的函数实现

/* *Accept a pending connection. The TCP layer now gives BSD semantics. */int inet_accept(struct socket *sock, struct socket *newsock, int flags){struct sock *sk1 = sock->sk;int err = -EINVAL;struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);if (!sk2)goto do_err;lock_sock(sk2);WARN_ON(!((1 << sk2->sk_state) &  (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));sock_graft(sk2, newsock);newsock->state = SS_CONNECTED;err = 0;release_sock(sk2);do_err:return err;}
首先根据套接口获取对应的传输控制块,调用accept的传输层接口的函数inet_csk_accept()获取已完成的连接的传输控制块,称之为子传输控制块。

如果accept成功,则需要调用sock_graft()把子套接口和传输控制块关联起来以便这两者之间相互索引。最后设置子套接口状态为已连接的状态SS_CONNECTED。

inet_csk_accept()为accept系统调用的传输层实现。如果有完成连接的传输控制块,则将其从连接请求容器中取出;如果没有,则根据是否阻塞来决定返回或是等待新连接。

/* * This will accept the next outstanding connection. */struct sock *inet_csk_accept(struct sock *sk, int flags, int *err){struct inet_connection_sock *icsk = inet_csk(sk);struct sock *newsk;int error;lock_sock(sk);/* We need to make sure that this socket is listening, * and that it has something pending. */error = -EINVAL;if (sk->sk_state != TCP_LISTEN)goto out_err;/* Find already established connection */if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);/* If this is a non blocking socket don't sleep */error = -EAGAIN;if (!timeo)goto out_err;error = inet_csk_wait_for_connect(sk, timeo);if (error)goto out_err;}newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);WARN_ON(newsk->sk_state == TCP_SYN_RECV);out:release_sock(sk);return newsk;out_err:newsk = NULL;*err = error;goto out;}

0 0
原创粉丝点击