Sample Header Ad - 728x90

How does the Linux Kernel store hardware TX and RX filter modes?

3 votes
2 answers
284 views
I am working on a C program which gets the timestamping information for a given network interface, like my own version of ethtool. My goal is to get the information printed by $ ethtool -T myNetIf. Something like:
Time stamping parameters for myNetIf:
Capabilities:
	hardware-transmit     (SOF_TIMESTAMPING_TX_HARDWARE)
	software-transmit     (SOF_TIMESTAMPING_TX_SOFTWARE)
	hardware-receive      (SOF_TIMESTAMPING_RX_HARDWARE)
	software-receive      (SOF_TIMESTAMPING_RX_SOFTWARE)
	software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
	hardware-raw-clock    (SOF_TIMESTAMPING_RAW_HARDWARE)
PTP Hardware Clock: 0
Hardware Transmit Timestamp Modes:
	off                   (HWTSTAMP_TX_OFF)
	on                    (HWTSTAMP_TX_ON)
Hardware Receive Filter Modes:
	none                  (HWTSTAMP_FILTER_NONE)
	all                   (HWTSTAMP_FILTER_ALL)
Since I don't want to just scrape the command line output, I have figured out that I can use an ioctl call to query this information from ethtool and get my flags back as an unsigned integer.
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
struct ethtool_ts_info etsi;
memset(&etsi, 0, sizeof(struct ethtool_ts_info));

etsi.cmd = ETHTOOL_GET_TS_INFO;
strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); // dev is read from stdin
ifr.ifr_data = (void *)&etsi;

ioctl(sock, SIOCETHTOOL, &ifr); // sock is an AF_INET socket
The ethtool_ts_info struct which (I believe) contains the data I want is defined [here](https://github.com/torvalds/linux/blob/2ac2b1665d3fbec6ca709dd6ef3ea05f4a51ee4c/include/uapi/linux/ethtool.h#L1455) . Specifically, I grab the so_timestamping, tx_types, and rx_filters fields. Using the same example interface, the value of so_timestamping is 0x5f, or 0101 1111. A look at net_tstamp.h [confirms](https://github.com/torvalds/linux/blob/2ac2b1665d3fbec6ca709dd6ef3ea05f4a51ee4c/include/uapi/linux/net_tstamp.h#L17) that these are the expected flags. My issue, however, is in interpreting the values of tx_types and rx_filters. I assumed that the value of tx_types would be something like 0x3 (0011, where the 3rd bit is HWTSTAMP_TX_ON and the 4th is HWTSTAMP_TX_OFF). Alternatively, since the possible transmit modes are defined in [an enum](https://github.com/torvalds/linux/blob/2ac2b1665d3fbec6ca709dd6ef3ea05f4a51ee4c/include/uapi/linux/net_tstamp.h#L98) with 4 values, perhaps the result would be 0100, where each int enum value is 2 bits. This is not the case. The actual value of tx_types is 0x7fdd. How on Earth am I supposed to get "HW_TX_OFF and ON" from 0x7fdd? I find the value of rx_filters even more confusing. What is 0x664758eb supposed to mean? Besides the kernel source code itself, I haven't been able to find much helpful information on this. I think I've done everything right, I just need some help understanding my results.
Asked by Robbie (63 rep)
Mar 21, 2024, 09:08 PM
Last activity: Mar 25, 2024, 06:30 PM