net/tap: add tap_fd_get_mtu() to query host tap MTU

Add a helper function to read the host tap device's configured
MTU via SIOCGIFMTU. Since this ioctl requires a socket rather than
the tap character device fd itself, a separate control socket is
opened to perform the query, following the same pattern used by
net/can/can_socketcan.c.

This is implemented for Linux only, with stub implementations
returning -1 for BSD, Solaris, and the tap-stub backend, matching
the existing pattern used by tap_fd_get_ifname() and
tap_fd_set_steering_ebpf() in those files.

This is a preparatory patch; the function is not yet called from
anywhere.

Signed-off-by: Nariman Sayed <narimansayed28@gmail.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Tested-by: Aniket Sahu <asahu1x@linux.ibm.com>
Acked-by: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Message-ID: <20261007050718.926-2-narimansayed28@gmail.com>
This commit is contained in:
Nariman Sayed authored and Michael S. Tsirkin committed 2026-10-29 14:27:26 -04:00
1 parent 7a3c1331bf
commit 0eb6b5fb11
5 files changed
+46

No files matched your search

+5
View File
@@ -264,6 +264,11 @@ int tap_fd_get_ifname(int fd, char *ifname)
return -1;
}
int tap_fd_get_mtu(int fd)
{
return -1;
}
int tap_fd_set_steering_ebpf(int fd, int prog_fd)
{
return -1;
+30
View File
@@ -34,6 +34,7 @@
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/cutils.h"
#include "qemu/sockets.h"
#define PATH_NET_TUN "/dev/net/tun"
@@ -346,6 +347,35 @@ int tap_fd_get_ifname(int fd, char *ifname)
return 0;
}
int tap_fd_get_mtu(int fd)
{
struct ifreq ifr;
int s, ret, mtu;
memset(&ifr, 0, sizeof(ifr));
if (tap_fd_get_ifname(fd, ifr.ifr_name) != 0) {
return -1;
}
s = qemu_socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
error_report("could not create control socket: %s",
strerror(errno));
return -1;
}
ret = ioctl(s, SIOCGIFMTU, &ifr);
if (ret != 0) {
error_report("SIOCGIFMTU ioctl() failed: %s", strerror(errno));
close(s);
return -1;
}
mtu = ifr.ifr_mtu;
close(s);
return mtu;
}
int tap_fd_set_steering_ebpf(int fd, int prog_fd)
{
if (ioctl(fd, TUNSETSTEERINGEBPF, (void *) &prog_fd) != 0) {
+5
View File
@@ -293,6 +293,11 @@ int tap_fd_get_ifname(int fd, char *ifname)
return -1;
}
int tap_fd_get_mtu(int fd)
{
return -1;
}
int tap_fd_set_steering_ebpf(int fd, int prog_fd)
{
return -1;
+5
View File
@@ -91,6 +91,11 @@ int tap_fd_get_ifname(int fd, char *ifname)
return -1;
}
int tap_fd_get_mtu(int fd)
{
return -1;
}
int tap_fd_set_steering_ebpf(int fd, int prog_fd)
{
return -1;
+1
View File
@@ -45,6 +45,7 @@ int tap_fd_set_vnet_be(int fd, int vnet_is_be);
int tap_fd_enable(int fd);
int tap_fd_disable(int fd);
int tap_fd_get_ifname(int fd, char *ifname);
int tap_fd_get_mtu(int fd);
int tap_fd_set_steering_ebpf(int fd, int prog_fd);
#endif /* NET_TAP_INT_H */