I want to find the path of a UDP packet in the Linux kernel. For this, I want to read up on some documentation ( I have this so far, which is for TCP) and then have some printk statements in the relevant kernel functions to confirm that. I will do this by recompiling the kernel code.
Is this the way to go about it? Do you have any suggestions/references?
If you prefer a more visual way, try flame-grahps. Here is an example of UDP transmit flow (using netperf to transmit UDP packets):
And here is the same graph zoomed-in on udp_send_skb:
You can do the same for any relevant flow in the kernel. You can also search for specific functions or key-words and zoom in/out. This also gives you an idea of the heavier functions in the flow.
Hope this helps.
Specifically answering your question, to understand UDP processing for IPv4 you can use ftrace, as is done here:
At the ingress (receiving side):
Another part of the tracing show below:
And for egress of networking code, some snippets are extracted below:
The above is called function graph in ftrace:
How to make a linux kernel function available to ftrace function_graph tracer?
And my bashscript for tracing udp are as follows (to be run as root):
Now the output file is locate inside the /tmp/tracing.out where is the shell script process. The purpose of 20 seconds is to allow userspace activities to happen - just starts lots of UDP activities at this point. You can also remove "echo udp_* >/debug/tracing/set_ftrace_filter" from above script, because the default is to trace everything.
The linux networking stack is a big piece of the kernel and you need to spend some time studying it. I think that this books may help (Focused on older kernels 2.4 and 2.6, but the logic remain the same for the latest kernels 3.x):
Understanding Linux Network Internals
The Linux Networking Architecture - Design and Implementation of Network Protocols in the Linux Kernel
You can also checkout this links:
http://e-university.wisdomjobs.com/linux/chapter-189-277/sending-the-data-from-the-socket-through-udp-and-tcp.html
http://www.linuxfoundation.org/collaborate/workgroups/networking/kernel_flow
http://wiki.openwrt.org/doc/networking/praxis
http://www.ibm.com/developerworks/linux/library/l-linux-networking-stack/?ca=dgr-lnxw01lnxNetStack
http://gicl.cs.drexel.edu/people/sevy/network/Linux_network_stack_walkthrough.html
You need also to browse the kernel source :
http://lxr.linux.no/#linux+v3.7.3/
Begin your road to the network sub-system with this function : ip_rcv which is called when a packet is received. other functions are then called (
ip_rcv_finish
,ip_local_deliver
andip_local_deliver_finish
=> This function is responsible for choosing the good transport layer)