This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.sin_family = AF_INET; | |
server.sin_port = htons(SERVER_PORT); | |
bcopy(server_ent->h_addr, &server.sin_addr.s_addr, server_ent->h_length); | |
/* Connect to server, and set the socket's destination IP address and port# | |
* based on above parameters. Also, request the kernel to automatically set | |
* the Source IP and port# if the application did not call bind() prior to connect(). | |
*/ | |
connect(fd, (struct sockaddr *)&server, sizeof server); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sock *inet_csk_accept(struct sock *sk) | |
{ | |
struct sock *newsk = NULL; /* client socket */ | |
/* Make sure that this socket is listening, and that it has something pending. */ | |
lock_sock(sk); | |
if (sk->sk_state == TCP_LISTEN) | |
if ("there are completed connections waiting to be accepted") | |
newsk = get_first_connection(sk); | |
release_sock(sk); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ss -tlnpe | grep :45000 | |
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=1975,fd=3)) ino:3854935788 sk:37d59c | |
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=1974,fd=3)) ino:3854935786 sk:37d59d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ss -tlnpe | grep :45000 | |
LISTEN 0 128 *:45000 *:* users:(("my_server",pid=3020,fd=3),("my_server",pid=3019,fd=3)) ino:3854904087 sk:37d5a0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned int index = reciprocal_scale(phash, num_socks); | |
return reuse->socks[index]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int inet_reuseport_add_sock(struct sock *new_sk) | |
{ | |
/* First check if another identical LISTEN socket, prev_sk, | |
* exists. ... Then do the following: | |
*/ | |
if (prev_sk) { | |
/* | |
* Not the first listener - do the following: | |
* - Grow prev_sk->sk_reuseport_cb structure if required. | |
* - Save new_sk socket pointer in prev_sk's socks[]. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sock_reuseport { | |
u16 max_socks; /* Allocated size of socks[] array */ | |
u16 num_socks; /* #Elements in socks[] */ | |
struct sock *socks[0]; /* All sockets added to this group */ | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sock *reuseport_select_sock(struct sock *sk, unsigned int phash) | |
{ | |
/* Get control block of sockets in this SO_REUSEPORT group */ | |
struct sock_reuseport *reuse = sk->sk_reuseport_cb; | |
/* Get count of sockets in the group */ | |
int num_socks = reuse->num_socks; | |
/* Calculate value between 0 and 'num_socks-1' (both inclusive) */ | |
unsigned int index = reciprocal_scale(phash, num_socks); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sock *__inet_lookup_listener(tcp_hashinfo, skb, src_addr, src_port, dst_addr, dst_port) | |
{ | |
/* | |
* Use the destination port# to calculate a hash table slot# of the listen socket. | |
* inet_lhashfn() returns a number between 0 and INET_LHTABLE_SIZE-1 (both | |
* inclusive). | |
*/ | |
unsigned int hash = inet_lhashfn(dst_port); | |
/* Use this hash slot# to index the global LISTEN hash table */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct sock *__inet_lookup(tcp_hashinfo, skb, src_addr, src_port, dst_addr, dst_port) | |
{ | |
/* Convert dest_port# from network to host byte order */ | |
u16 hnum = ntohs(dst_port); | |
/* First look for an established socket ... */ | |
sk = __inet_lookup_established(tcp_hashinfo, src_addr, src_port, dst_addr, hnum); | |
if (sk) | |
return sk; |