callback function in socket as sk_data_ready()

c, linux, linux-kernel, networking, performance

Solution

The default ->sk_data_ready() callback is sock_def_readable():

static void sock_def_readable(struct sock *sk, int len)
{                       
        struct socket_wq *wq;

        rcu_read_lock();
        wq = rcu_dereference(sk->sk_wq);
        if (wq_has_sleeper(wq))
                wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
                                                POLLRDNORM | POLLRDBAND);
        sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
        rcu_read_unlock();
}

which basically wakes up the process waiting for these data, for example, recv(), and let them process the data in ->sk_receive_queue.

Some protocol may override this, for example, netlink, see __netlink_kernel_create().

BTW, you can use `perf top` command to see which kernel function consumes most CPU cycles.

Problem

I was trying to calculate the CPU cycles consumed by various layers & functions defined in linux kernel for TCP/IP network stack for processing a packet. so i used TSC for CPU consumtion by various functions. which shows that single call to sk_data_ready() function takes a lot of CPU cycles. So i follow the source code for TCP/IP stack in linux kernel for raw sockets & got the information as the packets are en-queued finally in the receiving circular linked list of the particular socket. But after en-queuing the packet the function defined in sock.c as sock_queue_rcv_skb() calls ``` sk->sk_data_ready(sk, skb_len); ``` which is the callback function(i think). but i am not able to get any source code of this callback function. can anyone help me to find the code & how it works? does recvfrom() function is also related to the above defined callback function?

Original source