Linux TCP sending process source code analysis-socket Layer

Source: Internet
Author: User
Tags sendmsg

Linux TCP sending process source code analysis-socket Layer
Source code analysis of the linuxTCP sending Process--Socket Layer -- lvyilong316

Kernel version: 3.15.2

Socket Data Structure

Sending Flowchart

The following is the sending flowchart of send (), sendto (), sendmsg (), and sendmmsg (). These four functions differ in addition to system calls, both the Socket layer and TCP layer have the same implementation.

Application Layer

The application layer can use the following Socket functions to send data:


   
   
  1. ssize_t write(int fd, const void *buf, size_t count);
  2. ssize_t send(int s, const void *buf, size_t len, int flags);
  3. ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
  4. ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
  5. int sendmmsg(int s, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags);


What are the differences between these sending functions? When flags is 0, the send () and write () functions are the same. The send (s, buf, len, flags) and sendto (s, buf, len, flags, NULL, 0) functions are the same. Write () and send () can be used when the socket is connected, while sendto (), sendmsg () and sendmmsg () are available at any time. The data at the user layer is finally described in the message header.

Lstructmsghdr


   
   
  1. Struct msghdr {
  2. Void * msg_name;/* optional address, Destination address */
  3. Socklen_t msg_namelen;/* size of address, the length of the destination address */
  4. Struct iovec * msg_iov;/* scatter/gather array, scattered data block array */
  5. Size_t msg_iovlen;/* # elements in msg_iov, number of scattered data blocks */
  6. Void * msg_control;/* ancillary data, control data */
  7. Socklen_t msg_controllen;/* ancillary data buffer len, controls the data Length */
  8. Int msg_flags;/* flags on received message */
  9. };


Lstructiovec


   
   
  1. /* Structure for scatter/gather I/O. */
  2. struct iovec {
  3. void *iov_base; /* Pointer to data. */
  4. size_t iov_len; /* Length of data. */
  5. };


By default, sending is blocked or non-blocking. Non-blocking signs: O_NONBLOCK and MSG_DONTWAIT.

System Call

The sending function is provided by glibc and is declared in include/sys/socket. h, implemented in sysdeps/mach/hurd/connect. c is mainly used to access the system call named sys_socketcall from the user space and pass parameters. Sys_socketcall () is actually all

The socket function enters the Common Entrance of the kernel space.


   
   
  1. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2. {
  3. ...
  4. switch(call) {
  5. ...
  6. case SYS_SEND:
  7. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  8. break;

  9. case SYS_SENDTO:
  10. err = sys_sendto(a0, (void __user *)a1 a[2], a[3], (struct sockaddr __user *)a[4], a[5]);
  11. break;

  12. ...
  13. case SYS_SENDMSG:
  14. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  15. break;

  16. case SYS_SENDMMSG:
  17. err = sys_sendmmsg(a0, (struct msghdr __user *)a1, a[2], a[3]);
  18. break;
  19. ...
  20. }
  21. }


Lsend

Sending () is actually a special case of sendto.


   
   
  1. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, unsigned, flags)
  2. {
  3. return sys_sendto(fd, buff, len, flags, NULL, 0);
  4. }


Lsendto

Sendto () initializes the message header, and then calls sock_sendmsg () for processing.


   
   
  1. SYSCALL_DEFINE6 (sendto, int, fd, void _ user *, buff, size_t, len, unsigned, flags,
  2. Struct sockaddr _ user *, addr, int, addr_len)
  3. {
  4. Struct socket * sock;
  5. Struct sockaddr_storage address;
  6. Int err;
  7. Struct msghdr msg;
  8. Struct iovec iov;
  9. Int fput_needed;

  10. If (len> INT_MAX)
  11. Len = INT_MAX;

  12. /* Find the corresponding socket instance through the file descriptor fd.
  13. * Use fd as the index to find the corresponding file instance from the file descriptor table files_struct instance of the current process,
  14. * Obtain the socket instance from the private_data Member of the file instance.
  15. */
  16. Sock = sockfd_lookup_light (fd, & err, & fput_needed );
  17. If (! Sock)
  18. Goto out;

  19. /* Initialize the message header */
  20. Iov. iov_base = buff;
  21. Iov. iov_len = len;
  22. Msg. msg_name = NULL;
  23. Msg. msg_iov = & iov;
  24. Msg. msg_iovlen = 1;/* only one data block */
  25. Msg. msg_control = NULL;
  26. Msg. msg_controllen = 0;
  27. Msg. msg_namelen = 0;

  28. If (addr ){
  29. /* Copy the socket address from the user space to the kernel space */
  30. Err = move_addr_to_kernel (addr, addr_len, & address );
  31. If (err <0)
  32. Goto out_put;

  33. Msg. msg_name = (struct sockaddr *) & address;
  34. Msg. msg_namelen = addr_len;
  35. }

  36. /* If a non-blocking flag is set */
  37. If (sock-> file-> f_flags & O_NONBLOCK)
  38. Flags | = MSG_DONTWAIT;
  39. Msg. msg_flags = flags;
  40. /* Call the unified sending entry function sock_sendmsg ()*/
  41. Err = sock_sendmsg (sock, & msg, len );

  42. Out_put:
  43. Fput_light (sock-> file, fput_needed );
  44. Out:
  45. Return err;
  46. }


Lsock_sendmsg

After _sendmsg () initializes the asynchronous IO control block, it calls _ sock_sendmsg ().


   
   
  1. Int sock_sendmsg (struct socket * sock, struct msghdr * msg, size_t size)
  2. {
  3. Struct kiocb iocb;
  4. Struct sock_iocb siocb;
  5. Int ret;

  6. Init_sync_kiocb (& iocb, NULL );
  7. Iocb. private = & siocb;

  8. Ret = _ sock_sendmsg (& iocb, sock, msg, size );

  9. /* Iocb queued, will get completion event */
  10. If (-EIOCBQUEUED = ret)
  11. Ret = wait_on_sync_kiocb (& iocb );

  12. Return ret;
  13. }

  14. /* AIO control block */
  15. Struct kiocb {
  16. Struct file * ki_filp;
  17. Struct kioctx * ki_ctx;/* NULL for sync ops. If it is synchronous, It is NULL */
  18. Kiocb_cancel_fn * ki_cancel;
  19. Void * private;/* points to sock_iocb */
  20. Union {
  21. Void _ user * user;
  22. Struct task_struct * tsk;/* processes that execute io */
  23. } Ki_obj;
  24. _ U64 ki_user_data;/* user's data for completion */
  25. Loff_t ki_pos;
  26. Size_t ki_nbytes;/* copy of iocb-> aio_nbytes */

  27. Struct list_head ki_list;/* the aio core uses this for cancellation */
  28. /* If the aio_resfd field of the userspace iocb is not zero,
  29. * This is the underlying eventfd context to deliver events.
  30. */
  31. Struct eventfd_ctx * ki_eventfd;
  32. };


L _ sock_sendmsg ()

_ Sock_sendmsg () will call the sending function at the Socket layer. If it is SOCK_STREAM, it will then call inet_sendmsg () for processing.


   
   
  1. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  2. struct msghdr *msg, size_t size)
  3. {
  4. int err = security_socket_sendmsg(sock, msg, size);
  5. return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
  6. }


L _ sock_sendmsg_nosec


   
   
  1. Static inline int _ sock_sendmsg_nosec (struct kiocb * iocb, struct socket * sock,
  2. Struct msghdr * msg, size_t size)
  3. {
  4. Struct sock_iocb * si = kiocb_to_siocb (iocb );
  5. Si-> sock = sock;
  6. Si-> scm = NULL;
  7. Si-> msg = msg;
  8. Si-> size = size;
  9. /* Call the Socket-layer operation function. If it is SOCK_STREAM, proto_ops is inet_stream_ops, and the function pointer points to inet_sendmsg ().
  10. */
  11. Return sock-> ops-> sendmsg (iocb, sock, msg, size );
  12. }


Sendmsg () and sendmmsg () are used to copy user space data to the kernel message header in the system call function. Finally, the inet_sendmsg () function of the Socket layer is called for next processing, I will not go into details here.

Socket Layer

The socket-layer operation function set instance of the SOCK_STREAM interface is inet_stream_ops, And the sending function is inet_sendmsg ().


   
   
  1. const struct proto_ops inet_stream_ops = {
  2. .family = PF_INET,
  3. .owner = THIS_MODULE,
  4. ...
  5. .sendmsg = inet_sendmsg,
  6. ...
  7. };


Linet_sendmsg

Inet_sendmsg () mainly calls the tcp_sendmsg () function of the TCP layer for processing.


   
   
  1. Int inet_sendmsg (struct kiocb * iocb, struct socket * sock, struct msghdr * msg, size_t size)
  2. {
  3. Struct sock * sk = sock-> sk;
  4. Sock_rps_record_flow (sk );

  5. /* We may need to bnd the socket.
  6. * If no local port is allocated for the connection and automatic binding is allowed, bind a local port to the connection.
  7. * The no_autobaind value of tcp_prot is true, so TCP does not allow automatic port binding.
  8. */
  9. If (! Inet_sk (sk)-> inet_num &&! Sk-> sk_prot-> no_autobind & inet_autobind (s ))
  10. Return-EAGAIN;

  11. /* If the transport layer uses TCP, sk_prot is tcp_prot, and sendmsg points to tcp_sendmsg ()*/
  12. Return sk-> sk_prot-> sendmsg (iocb, sk, msg, size );
  13. }

  14. /* Automatically bind an unbound socket .*/
  15. Static int inet_autobind (struct sock * sk)
  16. {
  17. Struct inet_sock * inet;

  18. /* We may need to bind the socket .*/
  19. Lock_sock (sk );

  20. /* If no local port is allocated */
  21. If (! Inet-> inet_num ){

  22. /* The TCP operation function set of the SOCK_STREAM interface is tcp_prot, and the port binding function is
  23. * Inet_csk_get_port ().
  24. */
  25. If (sk-> sk_prot-> get_port (sk, 0 )){
  26. Release_sock (sk );
  27. Return-EAGAIN;
  28. }
  29. Inet-> inet_sport = htons (inet-> inet_num );
  30. }

  31. Release_sock (sk );
  32. Return 0;
  33. }


The relationship between function calls (red lines) and data structures (blue lines) is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.