內核版本:3.15.2
以下是send()、sendto()、sendmsg()和sendmmsg()的發送流程圖,這四個函數除了在系統調用層面上有些差別,在Socket層和TCP層的實現都是相同的。
應用層可以使用以下Socket函數來發送數據:
- ssize_t write(int fd, const void *buf, size_t count);
- ssize_t send(int s, const void *buf, size_t len, int flags);
- ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
- ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
- int sendmmsg(int s, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags);
這些發送函數有什麼區別呢?當flags為0時,send()和write()功能相同。send(s, buf, len, flags)和sendto(s, buf, len, flags, NULL, 0)功能相同。write()和send()在套接字處於連接狀態時可以使用,而sendto()、sendmsg()和sendmmsg()在任何時候都可用。用戶層的數據最終都是以消息頭來描述的。
lstructmsghdr
- struct msghdr {
- void *msg_name; /* optional address,目的地址 */
- socklen_t msg_namelen; /* size of address,目的地址的長度 */
- struct iovec *msg_iov; /* scatter/gather array,分散的數據塊數組 */
- size_t msg_iovlen; /* #elements in msg_iov,分散的數據塊個數 */
- void *msg_control; /* ancillary data, 控制數據 */
- socklen_t msg_controllen; /* ancillary data buffer len,控制數據的長度 */
- int msg_flags; /* flags on received message */
- };
lstructiovec
- /* Structure for scatter/gather I/O. */
- struct iovec {
- void *iov_base; /* Pointer to data. */
- size_t iov_len; /* Length of data. */
- };
發送默認為阻塞發送,也可以設置為非阻塞發送。非阻塞標志:O_NONBLOCK、MSG_DONTWAIT。
發送函數是由glibc提供的,聲明位於include/sys/socket.h中,實現位於sysdeps/mach/hurd/connect.c中,主要是用來從用戶空間進入名為sys_socketcall的系統調用,並傳遞參數。sys_socketcall()實際上是所有
socket函數進入內核空間的共同入口。
- SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
- {
- ...
- switch(call) {
- ...
- case SYS_SEND:
- err = sys_send(a0, (void __user *)a1, a[2], a[3]);
- break;
- case SYS_SENDTO:
- err = sys_sendto(a0, (void __user *)a1 a[2], a[3], (struct sockaddr __user *)a[4], a[5]);
- break;
- ...
- case SYS_SENDMSG:
- err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
- break;
- case SYS_SENDMMSG:
- err = sys_sendmmsg(a0, (struct msghdr __user *)a1, a[2], a[3]);
- break;
- ...
- }
- }
lsend
send()其實是sendto()的一種特殊情況。
- SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, unsigned, flags)
- {
- return sys_sendto(fd, buff, len, flags, NULL, 0);
- }
lsendto
sendto()初始化了消息頭,接著就調用sock_sendmsg()來處理。
- SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, unsigned, flags,
- struct sockaddr __user *, addr, int, addr_len)
- {
- struct socket *sock;
- struct sockaddr_storage address;
- int err;
- struct msghdr msg;
- struct iovec iov;
- int fput_needed;
- if (len > INT_MAX)
- len = INT_MAX;
- /* 通過文件描述符fd,找到對應的socket實例。
- * 以fd為索引從當前進程的文件描述符表files_struct實例中找到對應的file實例,
- * 然後從file實例的private_data成員中獲取socket實例。
- */
- sock = sockfd_lookup_light(fd, &err, &fput_needed);
- if (! sock)
- goto out;
- /* 初始化消息頭 */
- iov.iov_base = buff;
- iov.iov_len = len;
- msg.msg_name = NULL;
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1; /* 只有一個數據塊 */
- msg.msg_control = NULL;
- msg.msg_controllen = 0;
- msg.msg_namelen = 0;
- if (addr) {
- /* 把套接字地址從用戶空間拷貝到內核空間 */
- err = move_addr_to_kernel(addr, addr_len, &address);
- if (err < 0)
- goto out_put;
- msg.msg_name = (struct sockaddr *)&address;
- msg.msg_namelen = addr_len;
- }
- /* 如果設置了非阻塞標志 */
- if (sock->file->f_flags & O_NONBLOCK)
- flags |= MSG_DONTWAIT;
- msg.msg_flags = flags;
- /* 調用統一的發送入口函數sock_sendmsg() */
- err = sock_sendmsg(sock , &msg, len);
- out_put:
- fput_light(sock->file, fput_needed);
- out:
- return err;
- }
lsock_sendmsg
sock_sendmsg()在初始化異步IO控制塊後,調用__sock_sendmsg()。
- int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
- {
- struct kiocb iocb;
- struct sock_iocb siocb;
- int ret;
- init_sync_kiocb(&iocb, NULL);
- iocb.private = &siocb;
- ret = __sock_sendmsg(&iocb, sock, msg, size);
- /* iocb queued, will get completion event */
- if (-EIOCBQUEUED == ret)
- ret = wait_on_sync_kiocb(&iocb);
- return ret;
- }
- /* AIO控制塊 */
- struct kiocb {
- struct file *ki_filp;
- struct kioctx *ki_ctx; /* NULL for sync ops,如果是同步的則為NULL */
- kiocb_cancel_fn *ki_cancel;
- void *private; /* 指向sock_iocb */
- union {
- void __user *user;
- struct task_struct *tsk; /* 執行io的進程 */
- } ki_obj;
- __u64 ki_user_data; /* user's data for completion */
- loff_t ki_pos;
- size_t ki_nbytes; /* copy of iocb->aio_nbytes */
- struct list_head ki_list; /* the aio core uses this for cancellation */
- /* If the aio_resfd field of the userspace iocb is not zero,
- * this is the underlying eventfd context to deliver events to.
- */
- struct eventfd_ctx *ki_eventfd;
- };
l__sock_sendmsg()
__sock_sendmsg()會調用Socket層的發送函數,如果是SOCK_STREAM,那麼接著就調用inet_sendmsg()處理。
- static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size)
- {
- int err = security_socket_sendmsg(sock, msg, size);
- return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
- }
l__sock_sendmsg_nosec
- static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
- struct msghdr *msg, size_t size)
- {
- struct sock_iocb *si = kiocb_to_siocb(iocb);
- si->sock = sock;
- si->scm = NULL;
- si->msg = msg;
- si->size = size;
- /* 調用Socket層的操作函數,如果是SOCK_STREAM,則proto_ops為inet_stream_ops, 函數指針指向inet_sendmsg()。
- */
- return sock->ops->sendmsg(iocb, sock, msg, size);
- }
sendmsg()和sendmmsg()在系統調用函數中也是拷貝用戶空間的數據到內核消息頭,最後調用Socket層的發送函數inet_sendmsg()進行下一步處理,這裡不再贅述。
SOCK_STREAM套接口的socket層操作函數集實例為inet_stream_ops,其中發送函數為inet_sendmsg()。
- const struct proto_ops inet_stream_ops = {
- .family = PF_INET,
- .owner = THIS_MODULE,
- ...
- .sendmsg = inet_sendmsg,
- ...
- };
linet_sendmsg
inet_sendmsg()主要調用TCP層的發送函數tcp_sendmsg()來處理。
- int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t size)
- {
- struct sock *sk = sock->sk;
- sock_rps_record_flow(sk);
- /* We may need to bnd the socket.
- * 如果連接還沒有分配本地端口,且允許自動綁定,那麼給連接綁定一個本地端口。
- * tcp_prot的no_autobaind為true,所以TCP是不允許自動綁定端口的。
- */
- if (! inet_sk(sk)->inet_num && ! sk->sk_prot->no_autobind && inet_autobind(s))
- return -EAGAIN;
- /* 如果傳輸層使用的是TCP,則sk_prot為tcp_prot,sendmsg指向tcp_sendmsg() */
- return sk->sk_prot->sendmsg(iocb, sk, msg, size);
- }
- /* Automatically bind an unbound socket. */
- static int inet_autobind(struct sock *sk)
- {
- struct inet_sock *inet;
- /* We may need to bind the socket. */
- lock_sock(sk);
- /* 如果還沒有分配本地端口 */
- if (! inet->inet_num) {
- /* SOCK_STREAM套接口的TCP操作函數集為tcp_prot,其中端口綁定函數為
- * inet_csk_get_port()。
- */
- if (sk->sk_prot->get_port(sk, 0)) {
- release_sock(sk);
- return -EAGAIN;
- }
- inet->inet_sport = htons(inet->inet_num);
- }
- release_sock(sk);
- return 0;
- }
函數調用(紅線)和數據結構關系(藍線)如下圖: