How Linux kernel handles the send system call
2015-02-03 17:28
316 查看
This is a brief introduction about how Linux kernel handles the
This study is based on kernel version 3.7.2, which is the latest stable kernel when writing this study.
In the latest kernel, the system call is defined using the
the number of arguments. For example, in order to find the definition of
it has 6 arguments.
The definition of the system call
Here's the code of the
From the code we know that the
The definition of
Here's the definition of each argument from
call will set it to NULL
which
Here's the description about
use
and return it
This function will try to valid if the provided integer
file descriptor, then return the sockeet if it's valid, otherwise return
The message to be send will be assembled, then
be called to send the message, whose return value will be returned by
Below is the description of the
call
used as callback if the current system call is asynchronous.
call
The kernel will try to call
permission before transmitting, and then call
Function
send the message
The
which is a protocol-specific struct.
This is a protocol-specific. Depending on the socket type (IPv6/IPv4, UDP/TCP/RAW), there's different implementation of this struct.
For most socket types (at least for IPv4+TCP, IPv4+UDP, IPv4+RAW), the
In function
kernel will call
In
We finally reach the transport layer -> internet layer interface.
sendsystem call.
This study is based on kernel version 3.7.2, which is the latest stable kernel when writing this study.
How
system call is defined
In the latest kernel, the system call is defined using the SYSCALL_DEFINExmacro, in which
xis
the number of arguments. For example, in order to find the definition of
asmlinkage long sys_sendto(int, void __user *, size_t, unsigned, struct sockaddr __user *, int);, you need to
grepfor
SYSCALL_DEFINE6because
it has 6 arguments.
The definition of the system call
sendcan be found at
net/socket.c.
How
is the send
system call handled
Definition
of send
system call
Here's the code of the sendsystem call
[code]/* * Send a datagram down a socket. */ SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len, unsigned int, flags) { return sys_sendto(fd, buff, len, flags, NULL, 0); }
From the code we know that the
sendsystem call is just a simple wrapper function around another system call
sendto.
Definition
of sendto
system call
The definition of sendtosystem call is available in the same file,
net/socket.c.
[code]/* * Send a datagram to a given address. We move the address into kernel * space and check the user space data area is readable before invoking * the protocol. */ SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len, unsigned int, flags, struct sockaddr __user *, addr, int, addr_len)
Here's the definition of each argument from
man 2 sendto:
int fd: the file descriptor of the socket, which is used to send the data to
void __user * buff: the content which will be send by this system call
size_t len: the length of the
buff
unsigned int flags: the bitwise OR of some predefined constants
struct sockaddr __user * addr: the destination address. The
sendsystem
call will set it to NULL
int addr_len: the length of the
addrstruct,
which
sendsystem call will set it to zero
Here's the description about
sendto's behaviour:
use
sockfd_lookup_lightto find the corresponding socket descriptor
and return it
This function will try to valid if the provided integer
fdis a valid
file descriptor, then return the sockeet if it's valid, otherwise return
NULL.
The message to be send will be assembled, then
sock_sendmsgwill
be called to send the message, whose return value will be returned by
sendtothen returned to
sendcallee.
Definition
of sock_sendmsg
call
Below is the description of the sock_sendmsgfunction call
call
init_sync_kiocb, which just inited a new
kiocbstruct.
kiocbis
used as callback if the current system call is asynchronous.
call
__sock_sendmsg, which is a wrapper around
security_socket_sendmsgand
__sock_sendmsg_nosec.
The kernel will try to call
security_socket_sendmsgto check the
permission before transmitting, and then call
__sock_sendmsg_nosecto send it.
Function
__sock_sendmsg_nosecwill call
sock->ops->sendmsgto
send the message
The
socket->opsis a
proto_ops,
which is a protocol-specific struct.
Definition
of proto_ops
struct and its sendmsg
function
This is a protocol-specific. Depending on the socket type (IPv6/IPv4, UDP/TCP/RAW), there's different implementation of this struct.For most socket types (at least for IPv4+TCP, IPv4+UDP, IPv4+RAW), the
inet_sendmsgfunction will be called to sendmsg.
In function
inet_sendmsg, if the current socket is not
binded,
kernel will call
inet_autobindto bind the socket to a local IP/port.
In
inet_sendmsg, it will call
socket->sk_prot->sendmsg.
We finally reach the transport layer -> internet layer interface.
相关文章推荐
- How the Linux Kernel initcall Mechanism Works
- Add a system call to the linux kernel in Ubuntu
- How to Use `strace` to Trace the System Call in Linux
- Adding a New System Call into the Linux Kernel 2.6
- project 1 Adding a System Call to the Linux Kernel(初版)
- Linux System Programming: Talking Directly to the Kernel and, C Library [ILLUSTRATED] (Paperback) Sep.2007.eBook-BBL
- the implementation of system call in linux
- How to patch the linux kernel patch
- HOWTO Change the Linux Kernel Start Address
- linux新增system call(for kernel 2.6)
- Linux System Programming: Talking Directly to the Kernel and C Library
- HowTo use the CDT to navigate Linux kernel source
- Init Call Mechanism in the Linux Kernel
- How much memory is consumed by the Linux kernel per TCP/IP network connection?
- Linux System Programming: Talking Directly to the Kernel and C Library [ILLUSTRATED]
- How Does The Linux File System Work?
- How to solve the problem witch is the system send many emails when cron have something wrong
- Linux System Enhancements, Optimization and Compiling the Kernel
- [转]Init Call Mechanism in the Linux Kernel(很犀利)
- sockfs: the most simple file system in linux kernel