For Linux kernel ioctl functions-general Linux technology-Linux programming and kernel information, see the following for details. Generally, IOCTL is called as follows: ioctl (int fd, int command, (char *) argstruct) has network-related code, therefore, the file description symbol fd is returned by the socket () system call, and the command parameter can be/usr/include/linux/sockios. h. In the header file, these commands are divided into multiple types based on the aspects involved in the issues it can solve.
For example:
Change the route table (SIOCADDRT, SIOCDELRT)
Read or update ARP/RARP cache (SIOCDARP, SIOCSRARP)
General network-related functions (SIOCGIFNAME, SIOCSIFADDR, etc)
The Goodies directory contains many sample programs that demonstrate ioctl usage. When you look at these programs, learn the specific call parameter structure based on the ioctl command type. For example: the IOCTL related to the route table uses the RTENTRY structure. The rtentry structure is defined in/usr/include/linux/route. in the H file, the arpreq structure used for the ARP-related ioctl call is defined in the/usr/include/linux/if_arp.h file. the most representative characteristics of ioctl commands related to network interfaces are that they all start with S or G, which is actually to set or get data, getifinfo. c program uses these commands to read IP address information, hardware address information, broadcast address information, and network interface-related flags. for these ioctl, the third parameter is an IFREQ struct, which is defined in/usr/include/linux/if. in the header file h, in some cases, the new ioctl command may be needed (except for For example, if the WAVELAN wireless network card maintains the signal strength information, the information may be useful to the user program. How does the user program access this information? Our first response is to define a new command in sockios. in the header file h, for example, SIOCGIFWVLNSS, unfortunately, this command is meaningless on other network interfaces, in addition, we try to use this name on other interfaces, instead of using it on the wireless network port for illegal access. What we need is to define the Mechanism of New Interface commands. Fortunately, the LINUX operating system has built-in hooks for this purpose. If you look at the header file sockios. h. You will notice that every device has a predefined SIOCDEVPRIVATE command, and the task of implementing it will be handed over to the programmer who writes the device driver. according to the General Convention, a user program calls a specific ioctl command: ioctl (sockid, SIOCDEVPRIVATE, (char *) & ifr) Here ifr is an ifreq struct variable, it fills the ifr NAME field with an interface NAME associated with the device. For example, the aforementioned wireless Nic Interface NAME Is eth1.
Without losing its universality, a user program will also need to exchange command parameters and operation results with the kernel, and these have passed through a domain ifr. ifr_data is filled. For example, the signal strength of the ENI is returned to this domain. The LINUX Source Code contains two special devices de4x5 and ewrk3. They define and implement special ioctl commands ., the source code of these devices is in the following file: de4x5. h, de4x5. c, ewrk3.h, and ewrk3.c. Both of them define their own private structure for data exchange between the user space and the driver. Before ioctl, the user program fills in the required data and changes the ifr. ifr_data points to this struct.
Before entering the code, let's track several steps for processing the ioctl system call. All ioctl requests of interface types lead to dev_ioctl () called. This ioctl is only a package, and most of the actual operations are left to dev_ifsioc ()., the only thing that dev_ioctl () needs to do is to check whether the call process has a suitable license to issue the command. Then, one of the first things that dev_ifsioc () needs to do is to get and name the domain ifr. the device structure corresponding to ifr_name is implemented after a large switch block code.
The SIOCDEVPRIVATE command and the SIOCDEVPRIVATE + 15 command parameters are all handed over to the default operation. These are switch branch statements. what happens here is that the kernel checks whether a special ioctl callback of a device has been set in the device structure, which is a function pointer maintained in the device structure. If the callback has been set, the kernel will call it.
Therefore, to implement a special ioctl, you need to write a callback for a special ioctl, and then let the do_ioctl domain in the device structure point to it. For EWK3 devices, this function is called ewrk3_ioctl (). The device structure is initialized in ewrk3_init (). The Code of ewrk3_ioctl () clearly shows ifr. ifr_data is used to exchange information between user programs and drivers. Note that this area of the memory serves to exchange data in two directions. For example, in the ewrk3 driver code, ifr. the first two bytes of ifr_data are used to pass the desired action to the driver. The buffer to which the fifth byte points is used to exchange other information.
When you browse the ewrk3_ioctl () code, remember that in an application, user space commands cannot access the kernel space. For this reason, two special steps are provided to the driver writer. they are memcpy_tofs () and memcpy_fromfs (). In the kernel, we use memcpy_tofs () to copy the kernel data to the user space. Similar to memcpy_fromfs (), we only copy the user data to the kernel space .. These program steps are executed because verify_area () is called to ensure that data access is not illegal. Similarly, remember that printk () is used to print debugging information. This function is very similar to printf (), but it cannot process floating point data. printf () functions cannot be used in the kernel. The output generated by printk () is dumped to a directory./usr/adm/messages.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service