Understanding Input-Output Control (IOCTLs) in Linux

Input/Output controls (IOCTLs) are system calls that are used in the Linux operating system for device specific configuration as noted below

  1. Communicating between Linux user space and kernel space
    • reading values from kernel special files (hardware, virtual devices, system subsytems /dev)
    • writing values to configuring kernel parameters
  2. Configuring hardware (As all hardware are treated as special files in Linux)

If a specific setting or operation cannot be performed via any other means, then the IOCTL is the “goto” means to achieve that operation. There are various discussions on whether IOCTLs are good to use or one should desist from using them. This below link from LWN.net provides one such discussion.

ioctl() forever?

The man-page for Linux provides the IOCTL API as shown below:

The IOCTL supports 4 different fundamental operations as present in the ioctl.h .

  1. _IO(type, nr) – command with no argument or passing an integer value to kernel. Used for simple commands like resetting or clearing device state.
  2. _IOR(type, nr, size) – read a value from a kernel structure or variable. Data transfer from kernel space to user space
  3. _IOW(type, nr, arg_type)– write a value to a kernel structure or variable. Data transfer from user space to kernel space
  4. _IOWR(type, nr, arg_type) – allows bi-directional data transfer

Where the parameters passed are described as (from the Linux documentation – IOCTL based interfaces)

  1. type – An 8-bit number, often a character literal, specific to a subsystem or driver, and listed in Ioctl Numbers
  2. nr – An 8-bit number identifying the specific command, unique for a give value of ‘type’
  3. arg_type – the type of element. Though this is mentioned as size in the header file, this actually refers to the argument type. The MACROs defined in ioctl.h map it to size by performing sizeof(arg_type)

The networking and wireless sub-system has moved away from IOCTLs mostly. However, certain IOCTL implementation do remain in certain device drivers. They use a standardized range of IOCTL numbers provided by the MACRO SIOCDEVPRIVATE. The range of IOCTLs are SIOCDEVPRIVATE+0 till SIOCDEVPRIVATE+15.

In the next article, we will introduce an IOCTL to the networking loopback driver that we are developing in the previous articles.

Adding IOCTL interface to networking loopback device driver

Leave a Reply

Your email address will not be published. Required fields are marked *