3.2、INET层socket(inet_socket)和传输层socket(struct sock)创建
函数inet_create完成了上述功能,并初始化了sock的属性值,将socket的sk属性指向sock结构
- static int inet_create(struct net *net, struct socket *sock, int protocol,
- int kern)
- {
- struct sock *sk;
- struct inet_protosw *answer;
- struct inet_sock *inet;
- struct proto *answer_prot;
- unsigned char answer_flags;
- char answer_no_check;
- int try_loading_module = 0;
- int err;
- if (unlikely(!inet_ehash_secret))
- if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
- build_ehash_secret();
- sock->state = SS_UNCONNECTED;
- /* Look for the requested type/protocol pair. */
- lookup_protocol:
- err = -ESOCKTNOSUPPORT;
- rcu_read_lock();
- //根据传输层协议的类型创建sock结构
- //遍历inetsw链表
- list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
- err = 0;
- /* Check the non-wild match. */
- if (protocol == answer->protocol) {
- if (protocol != IPPROTO_IP)
- break;//找到了适配的inetsw[]元素
- } else {//处理RAW类型的套接字
- /* Check for the two wild cases. */
- if (IPPROTO_IP == protocol) {
- protocol = answer->protocol;
- break;
- }
- if (IPPROTO_IP == answer->protocol)
- break;
- }
- err = -EPROTONOSUPPORT;
- }
- //到这里answer指向了合适的inetsw结构,若是TCP协议,answer指向内容如下
- /*
- * .type = SOCK_STREAM,
- * .protocol = IPPROTO_TCP,
- * .prot = &tcp_prot,
- * .ops = &inet_stream_ops,
- * .no_check = 0,
- * .flags = INET_PROTOSW_PERMANENT |
- * INET_PROTOSW_ICSK,
- */
- if (unlikely(err)) {
- if (try_loading_module < 2) {
- rcu_read_unlock();
- /*
- * Be more specific, e.g. net-pf-2-proto-132-type-1
- * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
- */
- if (++try_loading_module == 1)
- request_module("net-pf-%d-proto-%d-type-%d",
- PF_INET, protocol, sock->type);
- /*
- * Fall back to generic, e.g. net-pf-2-proto-132
- * (net-pf-PF_INET-proto-IPPROTO_SCTP)
- */
- else
- request_module("net-pf-%d-proto-%d",
- PF_INET, protocol);
- goto lookup_protocol;
- } else
- goto out_rcu_unlock;
- }
- err = -EPERM;
- if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
- goto out_rcu_unlock;
- err = -EAFNOSUPPORT;
- if (!inet_netns_ok(net, protocol))
- goto out_rcu_unlock;
- sock->ops = answer->ops;
- answer_prot = answer->prot;
- answer_no_check = answer->no_check;
- answer_flags = answer->flags;
- rcu_read_unlock();
- WARN_ON(answer_prot->slab == NULL);
- err = -ENOBUFS;
- //分配sock结构体内存
- sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
- if (sk == NULL)
- goto out;
- err = 0;
- sk->sk_no_check = answer_no_check;
- if (INET_PROTOSW_REUSE & answer_flags)
- sk->sk_reuse = 1;
- inet = inet_sk(sk);
- inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
- inet->nodefrag = 0;
- if (SOCK_RAW == sock->type) {
- inet->inet_num = protocol;
- if (IPPROTO_RAW == protocol)
- inet->hdrincl = 1;
- }
- if (ipv4_config.no_pmtu_disc)
- inet->pmtudisc = IP_PMTUDISC_DONT;
- else
- inet->pmtudisc = IP_PMTUDISC_WANT;
- inet->inet_id = 0;
- //对sk进行初始化设置并将sock中的sk指针指向sk结构
- sock_init_data(sock, sk);
- //进一步设置sk的其他属性信息
- sk->sk_destruct = inet_sock_destruct;
- sk->sk_protocol = protocol;
- sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
- inet->uc_ttl = -1;
- inet->mc_loop = 1;
- inet->mc_ttl = 1;
- inet->mc_all = 1;
- inet->mc_index = 0;
- inet->mc_list = NULL;
- sk_refcnt_debug_inc(sk);
- if (inet->inet_num) {
- /* It assumes that any protocol which allows
- * the user to assign a number at socket
- * creation time automatically
- * shares.
- */
- inet->inet_sport = htons(inet->inet_num);
- /* Add to protocol hash chains. */
- sk->sk_prot->hash(sk);//调用inet_hash函数
- }
- if (sk->sk_prot->init) {
- err = sk->sk_prot->init(sk);//调用tcp_v4_init_sock函数进行进一步的初始化,由于在函数sk_alloc中一些属性被设置成0了,所以在此调用进行初始化
- if (err)
- sk_common_release(sk);
- }
- out:
- return err;
- out_rcu_unlock:
- rcu_read_unlock();
- goto out;
- }
关于套接字struct sock与struct inet_sock、struct tcp_sock、struct inet_connection_sock结构之间的关系有待进一步了解。
更多请查看 Linux内核--网络内核实现分析
