tap: report IFF_DETACH_QUEUE in TUNGETIFF
Subsystems (3)
PATCH v2 1/1 tap: report IFF_DETACH_QUEUE in TUNGETIFF
tun sets IFF_DETACH_QUEUE in the flags returned by TUNGETIFF when the queue is detached, since commit 3d407a80b62f ("tun: Report whether the queue is attached or not"). tap does not, so userspace cannot tell a detached macvtap or ipvtap queue from an attached one.
Cloud Hypervisor ran into this when checking queue state on macvtap.
Report the flag from q->enabled, as tun does. Unlike tun, tap accepts TUNSETIFF on a bound fd, so ignore IFF_DETACH_QUEUE there to keep writing the TUNGETIFF flags back working.
---
Notes:
v2:
- ignore IFF_DETACH_QUEUE in TUNSETIFF so the TUNGETIFF flags can be written back, and describe the change as parity with tun (Sashiko)
- drop Willem's Reviewed-by because of the new TUNSETIFF hunk
v1: https://lore.kernel.org/netdev/[email protected]/
drivers/net/tap.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/net/tap.c +5 -0
@@ -932,6 +932,9 @@ static long tap_ioctl(struct file *file, unsigned int cmd,if (get_user (u, &ifr->ifr_flags))return -EFAULT ; +/* TUNGETIFF may report IFF_DETACH_QUEUE, ignore it here */ + u &= ~IFF_DETACH_QUEUE ; + ret =0 ;if ((u & ~TAP_IFFEATURES ) != (IFF_NO_PI |IFF_TAP )) ret = -EINVAL ;
@@ -950,6 +953,8 @@ static long tap_ioctl(struct file *file, unsigned int cmd, ret =0 ; u = q->flags; +if (!q->enabled) + u |=IFF_DETACH_QUEUE ;if (copy_to_user (&ifr->ifr_name, tap->dev->name,IFNAMSIZ ) ||put_user (u, &ifr->ifr_flags)) ret = -EFAULT ;
base-commit: cfb7793d1bc0f7d90571611979654cf1b3886b29
Signature · 1 line
2.53.0
Willem de Bruijn
maintainerQuoted 30 lines · Umang Pokhriyal
tun sets
IFF_DETACH_QUEUEin the flags returned by TUNGETIFF when the queue is detached, since commit3d407a80b62f("tun: Report whether the queue is attached or not"). tap does not, so userspace cannot tell a detached macvtap or ipvtap queue from an attached one.Cloud Hypervisor ran into this when checking queue state on macvtap.
Report the flag from
q->enabled, as tun does. Unlike tun, tap accepts TUNSETIFF on a bound fd, so ignoreIFF_DETACH_QUEUEthere to keep writing the TUNGETIFF flags back working.Assisted-by LLMSigned-off-by Umang Pokhriyal <[email protected]>---
Notes:
v2:
- ignoreIFF_DETACH_QUEUEin TUNSETIFF so the TUNGETIFF flags can be written back, and describe the change as parity with tun (Sashiko)
- drop Willem's Reviewed-by because of the new TUNSETIFF hunk
v1: https://lore.kernel.org/netdev/[email protected]/drivers/net/tap.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/net/tap.c +0
-0
drivers/net/tap.c static long tap_ioctl(struct file *file, unsigned int cmd, · line 937
Quoted 6 lines
drivers/net/tap.c +3
-0@@ -932,6 +932,9 @@ static long tap_ioctl(struct file *file, unsigned int cmd,if (get_user (u, &ifr->ifr_flags))return -EFAULT ; +/* TUNGETIFF may report IFF_DETACH_QUEUE, ignore it here */ + u &= ~IFF_DETACH_QUEUE ; +
I suppose we want tap to expose this state through TUNGETIFF, like tun, because applications already use that?
Else cleaner than working around bits would be a whole new TUNGETQUEUE call to match TUNSETQUEUE. Rather than to squish this into TUNGETIFF, while that has no equivalent in TUNSETIFF.
But, that approach does not help existing applications.
So this is probably the right way. Just want to quickly check.
Quoted 17 lines
drivers/net/tap.c +2
-0ret =0 ;if ((u & ~TAP_IFFEATURES ) != (IFF_NO_PI |IFF_TAP )) ret = -EINVAL ;@@ -950,6 +953,8 @@ static long tap_ioctl(struct file *file, unsigned int cmd, ret =0 ; u = q->flags; +if (!q->enabled) + u |=IFF_DETACH_QUEUE ;if (copy_to_user (&ifr->ifr_name, tap->dev->name,IFNAMSIZ ) ||put_user (u, &ifr->ifr_flags)) ret = -EFAULT ;base-commit:
cfb7793d1bc0f7d90571611979654cf1b3886b29Signature · 1 line
2.53.0
Quoted 2 lines · Willem de Bruijn
I suppose we want tap to expose this state through TUNGETIFF, like tun, because applications already use that?
Yes. CRIU reads IFF_DETACH_QUEUE from TUNGETIFF when it dumps a tun file, and Cloud Hypervisor did the same until it found that macvtap reports every queue as attached. Code written for tun's queue API gets the wrong answer on macvtap, even though macvtap accepts the same TUNSETQUEUE.
Quoted 5 lines
Else cleaner than working around bits would be a whole new TUNGETQUEUE call to match TUNSETQUEUE. Rather than to squish this into TUNGETIFF, while that has no equivalent in TUNSETIFF.
But, that approach does not help existing applications.
Right, those applications would have to be changed to use a new TUNGETQUEUE.
Thanks for taking another look.
Quoted 14 lines · Umang Pokhriyal
Quoted 2 lines · Willem de Bruijn
I suppose we want tap to expose this state through TUNGETIFF, like tun, because applications already use that?
Yes. CRIU reads
IFF_DETACH_QUEUEfrom TUNGETIFF when it dumps a tun file, and Cloud Hypervisor did the same until it found that macvtap reports every queue as attached. Code written for tun's queue API gets the wrong answer on macvtap, even though macvtap accepts the same TUNSETQUEUE.Quoted 4 lines
Else cleaner than working around bits would be a whole new TUNGETQUEUE call to match TUNSETQUEUE. Rather than to squish this into TUNGETIFF, while that has no equivalent in TUNSETIFF.
Quoted 6 lines
Quoted 1 line
But, that approach does not help existing applications.
Right, those applications would have to be changed to use a new TUNGETQUEUE.
Thanks for taking another look.
Thanks for verifying.
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 8 lines · On Sun, 4 Oct 2026 23:17:38 +0530 you wrote:
tun sets
IFF_DETACH_QUEUEin the flags returned by TUNGETIFF when the queue is detached, since commit3d407a80b62f("tun: Report whether the queue is attached or not"). tap does not, so userspace cannot tell a detached macvtap or ipvtap queue from an attached one.Cloud Hypervisor ran into this when checking queue state on macvtap.
[...]
Here is the summary with links:
- [net-next,v2] tap: report IFF_DETACH_QUEUE in TUNGETIFF
https://git.kernel.org/netdev/net-next/c/60b5268444a1
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html