[PATCH net-next v7 6/9] tun/tap: add helper functions to check file type
From: Simon Schippers <hidden>
Date: 2026-01-07 21:06:16
Also in:
kvm, lkml, virtualization
Subsystem:
networking drivers, the rest, tun/tap driver · Maintainers:
Andrew Lunn, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds, Willem de Bruijn, Jason Wang
Add tun_is_tun_file() and tap_is_tap_file() helper functions to check if a file is a TUN or TAP file, which will be utilized by vhost-net. Co-developed-by: Tim Gebauer <redacted> Signed-off-by: Tim Gebauer <redacted> Co-developed by: Jon Kohler [off-list ref] Signed-off-by: Jon Kohler <redacted> Signed-off-by: Simon Schippers <redacted> --- drivers/net/tap.c | 13 +++++++++++++ drivers/net/tun.c | 13 +++++++++++++ include/linux/if_tap.h | 5 +++++ include/linux/if_tun.h | 6 ++++++ 4 files changed, 37 insertions(+)
diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 4ffe4e95b5a6..cf19d7181c2f 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c@@ -1243,6 +1243,19 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file) } EXPORT_SYMBOL_GPL(tap_get_ptr_ring); +bool tap_is_tap_file(struct file *file) +{ + struct tap_queue *q; + + if (file->f_op != &tap_fops) + return false; + q = file->private_data; + if (!q) + return false; + return true; +} +EXPORT_SYMBOL_GPL(tap_is_tap_file); + int tap_queue_resize(struct tap_dev *tap) { struct net_device *dev = tap->dev;
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index d44d206c65e8..9d6f98e00661 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c@@ -3782,6 +3782,19 @@ struct ptr_ring *tun_get_tx_ring(struct file *file) } EXPORT_SYMBOL_GPL(tun_get_tx_ring); +bool tun_is_tun_file(struct file *file) +{ + struct tun_file *tfile; + + if (file->f_op != &tun_fops) + return false; + tfile = file->private_data; + if (!tfile) + return false; + return true; +} +EXPORT_SYMBOL_GPL(tun_is_tun_file); + module_init(tun_init); module_exit(tun_cleanup); MODULE_DESCRIPTION(DRV_DESCRIPTION);
diff --git a/include/linux/if_tap.h b/include/linux/if_tap.h
index 28326a69745a..14194342b784 100644
--- a/include/linux/if_tap.h
+++ b/include/linux/if_tap.h@@ -14,6 +14,7 @@ struct ptr_ring *tap_get_ptr_ring(struct file *file); int tap_ring_consume_batched(struct file *file, void **array, int n); void tap_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); +bool tap_is_tap_file(struct file *file); #else #include <linux/err.h> #include <linux/errno.h>
@@ -32,6 +33,10 @@ static inline int tap_ring_consume_batched(struct file *f, } static inline void tap_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)) {} +static inline bool tap_is_tap_file(struct file *f) +{ + return false; +} #endif /* CONFIG_TAP */ /*
diff --git a/include/linux/if_tun.h b/include/linux/if_tun.h
index 1274c6b34eb6..0910c6dbac20 100644
--- a/include/linux/if_tun.h
+++ b/include/linux/if_tun.h@@ -25,6 +25,7 @@ struct ptr_ring *tun_get_tx_ring(struct file *file); int tun_ring_consume_batched(struct file *file, void **array, int n); void tun_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)); +bool tun_is_tun_file(struct file *file); static inline bool tun_is_xdp_frame(void *ptr) {
@@ -67,6 +68,11 @@ static inline int tun_ring_consume_batched(struct file *file, static inline void tun_ring_unconsume(struct file *file, void **batch, int n, void (*destroy)(void *)) {} +static inline bool tun_is_tun_file(struct file *f) +{ + return false; +} + static inline bool tun_is_xdp_frame(void *ptr) { return false;
--
2.43.0