Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


It is exactly this function (create) that is invoked after memory has been reserved for the
socket.inet_createis used for Internet connections (both TCP and UDP). It creates a new instance
of a kernel-internalsocksocket, initializes it as far as possible, and inserts it in the kernel data
structures.

map_sock_fdgenerates a pseudo-file for the socket (the file operations are specified bysocket_ops). A
file descriptor is also allocated so that it can be returned as the result of the system call.

12.10.5 Receiving Data


Data are received using therecvfromandrecvsystem calls and the file-relatedreadvandreadfunc-
tions. Because the code of each of these functions is very similar and merges at an early point, only
sys_recvfrom, whose code flow diagram is shown in Figure 12-33, is discussed.

sys_recvfrom

fget_light

sock_from_file

sock_recvmsg

sock->ops->recvmsg

move_addr_to_user

Figure 12-33: Code flow diagram for
sys_recvfrom.

A file descriptor to identify the desired socket is passed to the system call. Consequently, the first task is
to find the relevantsocket.First,fget_lightreferences the descriptor table of the task structure to find
the correspondingfileinstance.sock_from_filedetermines the associated inode and ultimately the
associated socket by usingSOCKET_I.

After a few preparations (not discussed here)sock_recvmsginvokes the protocol-specific receive rou-
tinesock->ops->recv_msg0. For example, TCP usestcp_recvmsgto do this. The UDP equivalent is
udp_recvmsg. The implementation for UDP is not particularly complicated:

❑ If there is at least one packet on the receive queue (implemented by thereceive_queueelement
of thesockstructure), it is removed and returned.
❑ If the receive queue is empty, it is obvious that no data can be passed to the user process. In this
case, the process useswait_for_packetto put itself to sleep until data arrive.
As thedata_readyfunction of thesockstructure is always invoked when new data arrive, the
process can be woken at this point.

move_addr_to_usercopies the data from kernel space to userspace using thecopy_to_userfunctions
described in Chapter 2.
Free download pdf