omer
omer

Reputation: 1442

What is the best why to synchronize access to net_device structures?

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:

  1. The dev_base_lock lock.

  2. the rtnl_lock() function.

My questions are:

  1. What is the difference between the two?

  2. Is there any thumb rule for using one of the above?

  3. Which one of them should i use for synchronizing access to a single net_device structure?

Thanks!

Upvotes: 3

Views: 1411

Answers (1)

Arthur
Arthur

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

Related Questions