public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Stephen Hemminger <shemminger@vyatta•com>
To: David Woodhouse <dwmw2@infradead•org>
Cc: netdev@vger•kernel.org, Mark McLoughlin <markmc@redhat•com>
Subject: Re: tun: add tun_flags, owner, group attributes in sysfs
Date: Mon, 4 May 2009 07:47:53 -0700	[thread overview]
Message-ID: <20090504074753.6d1c5bcd@nehalam> (raw)
In-Reply-To: <1241433136.6126.70.camel@macbook.infradead.org>

On Mon, 04 May 2009 11:32:16 +0100
David Woodhouse <dwmw2@infradead•org> wrote:

> This patch adds three attribute files in /sys/class/net/$dev/ for tun
> devices; allowing userspace to obtain the information which TUNGETIFF
> offers, and more, but without having to attach to the device in question
> (which may not be possible if it's in use).
> 
> It also fixes a bug which has been present in the TUNGETIFF ioctl since
> its inception, where it would never set IFF_TUN or IFF_TAP according to
> the device type. (Look carefully at the code which I remove from
> tun_get_iff() and how the new tun_flags() helper is subtly different).
> 
> Signed-off-by: David Woodhouse <David.Woodhouse@intel•com>
> ---
> I don't have to explicitly remove the files, do I? They go away
> naturally when the device is unregistered?
> 
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 94622e5..4cda69b 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
> @@ -865,6 +865,52 @@ static struct proto tun_proto = {
>  	.obj_size	= sizeof(struct tun_sock),
>  };
>  
> +static int tun_flags(struct tun_struct *tun)
> +{
> +	int flags = 0;
> +
> +	if (tun->flags & TUN_TUN_DEV)
> +		flags |= IFF_TUN;
> +	else
> +		flags |= IFF_TAP;
> +
> +	if (tun->flags & TUN_NO_PI)
> +		flags |= IFF_NO_PI;
> +
> +	if (tun->flags & TUN_ONE_QUEUE)
> +		flags |= IFF_ONE_QUEUE;
> +
> +	if (tun->flags & TUN_VNET_HDR)
> +		flags |= IFF_VNET_HDR;
> +
> +	return flags;
> +}
> +
> +static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
> +			      char *buf)
> +{
> +	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
> +	return sprintf(buf, "0x%x\n", tun_flags(tun));
> +}
> +
> +static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
> +			      char *buf)
> +{
> +	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
> +	return sprintf(buf, "%d\n", tun->owner);
> +}
> +
> +static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
> +			      char *buf)
> +{
> +	struct tun_struct *tun = netdev_priv(to_net_dev(dev));
> +	return sprintf(buf, "%d\n", tun->group);
> +}
> +
> +static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
> +static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
> +static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
> +
>  static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
>  {
>  	struct sock *sk;
> @@ -950,6 +996,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
>  		if (err < 0)
>  			goto err_free_sk;
>  
> +		if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
> +		    device_create_file(&tun->dev->dev, &dev_attr_owner) ||
> +		    device_create_file(&tun->dev->dev, &dev_attr_group))
> +			printk(KERN_ERR "Failed to create tun sysfs files\n");
> +
>  		sk->sk_destruct = tun_sock_destruct;
>  
>  		err = tun_attach(tun, file);
> @@ -1002,21 +1053,7 @@ static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr)
>  
>  	strcpy(ifr->ifr_name, tun->dev->name);
>  
> -	ifr->ifr_flags = 0;
> -
> -	if (ifr->ifr_flags & TUN_TUN_DEV)
> -		ifr->ifr_flags |= IFF_TUN;
> -	else
> -		ifr->ifr_flags |= IFF_TAP;
> -
> -	if (tun->flags & TUN_NO_PI)
> -		ifr->ifr_flags |= IFF_NO_PI;
> -
> -	if (tun->flags & TUN_ONE_QUEUE)
> -		ifr->ifr_flags |= IFF_ONE_QUEUE;
> -
> -	if (tun->flags & TUN_VNET_HDR)
> -		ifr->ifr_flags |= IFF_VNET_HDR;
> +	ifr->ifr_flags = tun_flags(tun);
>  
>  	tun_put(tun);
>  	return 0;

Netlink please not sysfs. also, any sysfs attributes have to handle unregistration
issues.
      ( sleep 900; read owner ) </sys/class/net/tun0/owner &
      rmmod tun

  parent reply	other threads:[~2009-05-04 14:47 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-04 10:32 tun: add tun_flags, owner, group attributes in sysfs David Woodhouse
2009-05-04 10:36 ` Michael Tokarev
2009-05-05 22:18   ` David Woodhouse
2009-05-04 10:42 ` [iproute2 patch]: Add 'ip tuntap' facility for managing tun/tap devices David Woodhouse
2009-05-04 14:38   ` Stephen Hemminger
2009-05-04 14:49     ` David Woodhouse
2009-05-27 16:32       ` David Woodhouse
2009-05-27 16:38         ` Stephen Hemminger
2009-05-27 16:49           ` David Woodhouse
2009-05-27 17:06             ` Stephen Hemminger
2009-05-27 20:44               ` David Miller
2009-05-27 20:43             ` David Miller
2009-05-28  4:58   ` Stephen Hemminger
2009-05-28  8:12     ` David Woodhouse
2009-06-13  8:55       ` David Woodhouse
2009-07-20 23:44         ` David Woodhouse
2009-05-04 14:47 ` Stephen Hemminger [this message]
2009-05-04 14:58   ` tun: add tun_flags, owner, group attributes in sysfs David Woodhouse
2009-05-09 20:27 ` David Miller
2009-05-09 22:27   ` David Woodhouse
2009-05-09 22:55     ` David Woodhouse
2009-05-09 22:56     ` David Miller
2009-05-10  5:54 ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090504074753.6d1c5bcd@nehalam \
    --to=shemminger@vyatta$(echo .)com \
    --cc=dwmw2@infradead$(echo .)org \
    --cc=markmc@redhat$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox