This helper may be useful for other AF_XDP tests, such as xsk_hw. Moving it out so we don't need to copy-paste that function. I also changed the function from directly calling error(1, errno, ...) to returning an error because I don't think it makes sense for a library function to outright kill the process if the function fails. Signed-off-by: YiFei Zhu <zhuyifei@xxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 27 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 2 ++ tools/testing/selftests/bpf/xdp_hw_metadata.c | 27 ++----------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 35250e6cde7f..4c3bef07df23 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -569,6 +569,33 @@ int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param) return 0; } +int rxq_num(const char *ifname) +{ + struct ethtool_channels ch = { + .cmd = ETHTOOL_GCHANNELS, + }; + struct ifreq ifr = { + .ifr_data = (void *)&ch, + }; + strncpy(ifr.ifr_name, ifname, IF_NAMESIZE - 1); + int fd, ret, err; + + fd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (fd < 0) + return -errno; + + ret = ioctl(fd, SIOCETHTOOL, &ifr); + if (ret < 0) { + err = errno; + close(fd); + return -err; + } + + close(fd); + + return ch.rx_count + ch.combined_count; +} + struct send_recv_arg { int fd; uint32_t bytes; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 883c7ea9d8d5..b09c3bbd5b62 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -72,6 +72,8 @@ int get_socket_local_port(int sock_fd); int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); +int rxq_num(const char *ifname); + struct nstoken; /** * open_netns() - Switch to specified network namespace by name. diff --git a/tools/testing/selftests/bpf/xdp_hw_metadata.c b/tools/testing/selftests/bpf/xdp_hw_metadata.c index 6f9956eed797..f038a624fd1f 100644 --- a/tools/testing/selftests/bpf/xdp_hw_metadata.c +++ b/tools/testing/selftests/bpf/xdp_hw_metadata.c @@ -495,31 +495,6 @@ static int verify_metadata(struct xsk *rx_xsk, int rxq, int server_fd, clockid_t return 0; } -static int rxq_num(const char *ifname) -{ - struct ethtool_channels ch = { - .cmd = ETHTOOL_GCHANNELS, - }; - - struct ifreq ifr = { - .ifr_data = (void *)&ch, - }; - strncpy(ifr.ifr_name, ifname, IF_NAMESIZE - 1); - int fd, ret; - - fd = socket(AF_UNIX, SOCK_DGRAM, 0); - if (fd < 0) - error(1, errno, "socket"); - - ret = ioctl(fd, SIOCETHTOOL, &ifr); - if (ret < 0) - error(1, errno, "ioctl(SIOCETHTOOL)"); - - close(fd); - - return ch.rx_count + ch.combined_count; -} - static void hwtstamp_ioctl(int op, const char *ifname, struct hwtstamp_config *cfg) { struct ifreq ifr = { @@ -668,6 +643,8 @@ int main(int argc, char *argv[]) read_args(argc, argv); rxq = rxq_num(ifname); + if (rxq < 0) + error(1, -rxq, "rxq_num"); printf("rxq: %d\n", rxq); -- 2.45.2.505.gda0bf45e8d-goog