Reputation: 1442
I am working with linux kernel version 2.6. I would like to synchronize access to a single net_device structure. While searching for an answer in the kernel code, i came across two synchronization mechanisms relevant to this issue:
The dev_base_lock lock.
the rtnl_lock() function.
My questions are:
What is the difference between the two?
Is there any thumb rule for using one of the above?
Thanks!
Upvotes: 3
Views: 1411
Reputation: 578
You can refer to the chapter "8.15. Locking" in << understanding linux network internals >>
The dev_base
list and the two hash tables dev_name_head
and dev_name_index
are protected by the dev_base_lock
. That lock, however, is used only to serialize accesses to the list and tables, not to serialize changes to the contents of net_device data structures. net_device content changes are taken care of by the Routing Netlink semaphore (rtnl_sem), which is acquired and released with rtnl_lock
and rtnl_unlock
, respectively.[*] This semaphore is used to serialize changes to net_device instances from:
Runtime events
For example, when the link state changes (e.g., a network cable is plugged or unplugged), the kernel needs to change the device state by modifying dev->flags.
Configuration changes
When the user applies a configuration change with commands such as ifconfig and route from the net-tools package, or ip from the IPROUTE2 package, the kernel is notified via ioctl commands and the Netlink socket, respectively. The routines invoked via these interfaces must use locks.
[*] Other routines can also be used to acquire and release the semaphore. See include/linux/rtnetlink.h for more details.
Upvotes: 4