Skip to content

Instantly share code, notes, and snippets.

@kkumar-fk
Last active July 23, 2019 04:30
Show Gist options
  • Save kkumar-fk/835e4cef5cc477f4934ba06b91dfdec5 to your computer and use it in GitHub Desktop.
Save kkumar-fk/835e4cef5cc477f4934ba06b91dfdec5 to your computer and use it in GitHub Desktop.
code blob
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 */
struct inet_listen_hashbucket *ilb = tcp_hashinfo->listening_hash[hash];
/* Keep track of the best matching LISTEN socket thus far, and it’s “score” */
struct sock *result = NULL, *sk;
int hi_score = 0;
for each socket, ‘sk’, in the selected hash bucket, ‘ilb’ {
/*
* Calculate the “score” of this LISTEN socket (sk) against the incoming skb.
* Score is computed on some parameters, such as, exact destination port#,
* destination IP address closest match (as against matching INADDR_ANY,
* for example), with each criteria getting a different weight.
*/
score = compute_score(sk, dst_port, dst_addr);
if (score > hi_score) {
/* Highest score - best matched socket till now */
if (sk->sk_reuseport) {
/*
* sk has SO_REUSEPORT feature enabled. Call inet_ehashfn()
* with dest_addr, dest_port, src_addr and src_port to compute a
* 2nd hash - phash.
*/
phash = inet_ehashfn(dst_addr, dst_port, src_addr, src_port);
/* Select socket from sk’s SO_REUSEPORT group using phash.*/
result = reuseport_select_sock(sk, phash);
if (result)
return result;
}
/* Update new best socket and it’s score */
result = sk;
hi_score = score;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment