Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a confusing function:netdev_priv

1,641 views
Skip to first unread message

!truth

unread,
Jul 3, 2007, 5:27:08 AM7/3/07
to
Hi all,
I cannot understand how the function 'netdev_priv' works, while
reading linux-driver.
The prototype of the function is as follows:

#define NETDEV_ALIGN 32
#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)


static inline void *netdev_priv(struct net_device *dev)
{ return (char *)dev + ((sizeof(struct net_device)+
NETDEV_ALIGN_CONST)
& ~NETDEV_ALIGN_CONST);
}

what made me confused is that
it says 'netdev_priv' is used for getting a member called 'priv' in
the structure 'net_device', but I think it can only get the last
member.
As you know the member 'priv' is not the last one, what's the story?

el...@no.spam

unread,
Jul 3, 2007, 6:11:50 AM7/3/07
to
In article <1183454828....@a26g2000pre.googlegroups.com>,
!truth <noddy...@asustek.com.cn> wrote:

It returns a pointer to the beginning of the private data part
of the structure. In other words the address just beyond the
public part of the structure padded by the alignment needed.

--
http://www.spinics.net/lists/netdev/

!truth

unread,
Jul 3, 2007, 7:21:50 AM7/3/07
to
On 7 3 , 6 11 , e...@no.spam () wrote:
> In article <1183454828.384659.19...@a26g2000pre.googlegroups.com>,
> --http://www.spinics.net/lists/netdev/- -
>
> - -

thanks, but what does 'beyond the public part of the structure ' mean?
Is there something marked for it?

!truth

unread,
Jul 3, 2007, 7:31:03 AM7/3/07
to
On 7 3 , 6 11 , e...@no.spam () wrote:
> In article <1183454828.384659.19...@a26g2000pre.googlegroups.com>,
>
>
>
>
>
> !truth <noddy_zh...@asustek.com.cn> wrote:
> > I cannot understand how the function 'netdev_priv' works, while
> >reading linux-driver.
> >The prototype of the function is as follows:
>
> >#define NETDEV_ALIGN 32
> >#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)
>
> >static inline void *netdev_priv(struct net_device *dev)
> >{ return (char *)dev + ((sizeof(struct net_device)+
> >NETDEV_ALIGN_CONST)
> > & ~NETDEV_ALIGN_CONST);
> > }
>
> >what made me confused is that
> >it says 'netdev_priv' is used for getting a member called 'priv' in
> >the structure 'net_device', but I think it can only get the last
> >member.
> >As you know the member 'priv' is not the last one, what's the story?
>
> It returns a pointer to the beginning of the private data part
> of the structure. In other words the address just beyond the
> public part of the structure padded by the alignment needed.
>

I write down a similar program to show my question:
In fact , the result are not equal

#include <stdio.h>
struct device{
int a;
void *priv;
int c;
char d;
};


#define NETDEV_ALIGN 32
#define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1)

static inline void *netdev_priv(struct device *dev)
{ return (char *)dev + ((sizeof(struct device)+
NETDEV_ALIGN_CONST)
& ~NETDEV_ALIGN_CONST);
}
int main()
{
struct device devs;
struct device *dev;
dev=&devs;

/*get pointer by dev->priv*/
printf("%p\n",dev->priv);

/*get pointer using function:netdev_priv*/
printf("%p\n",netdev_priv(dev));
return 0;
}

Gil Hamilton

unread,
Jul 3, 2007, 7:31:59 AM7/3/07
to
el...@no.spam () wrote in news:11834575...@no.spam:

> In article <1183454828....@a26g2000pre.googlegroups.com>,
> !truth <noddy...@asustek.com.cn> wrote:
>
>> I cannot understand how the function 'netdev_priv' works, while
>>reading linux-driver.

>>it says 'netdev_priv' is used for getting a member called 'priv' in


>>the structure 'net_device', but I think it can only get the last
>>member.
>>As you know the member 'priv' is not the last one, what's the story?
>
> It returns a pointer to the beginning of the private data part
> of the structure. In other words the address just beyond the
> public part of the structure padded by the alignment needed.

I would just add that drivers don't need to use netdev_priv (although
many drivers seem to do so, I think for historical reasons). netdev_priv
is used inside the function alloc_netdev() to set up the value of the
'priv' member to point to the private data area allocated just beyond the
net_device portion. Once set up, drivers can simply refer to the priv
member:

struct net_device *my_dev = alloc_netdev(sizeof(struct my_priv),
"eth%d", ether_setup);
struct my_priv *my_private = my_dev->priv;


The last line above would be exactly equivalent to:

struct my_priv *my_private = netdev_priv(my_dev);

GH

Gil Hamilton

unread,
Jul 3, 2007, 7:34:52 AM7/3/07
to
!truth <noddy...@asustek.com.cn> wrote in news:1183462263.324580.17010
@m37g2000prh.googlegroups.com:

> I write down a similar program to show my question:
> In fact , the result are not equal

> /*get pointer by dev->priv*/
> printf("%p\n",dev->priv);
>
> /*get pointer using function:netdev_priv*/
> printf("%p\n",netdev_priv(dev));

See my previous post. After you have called alloc_netdev(), the two *will*
be equivalent.

GH

el...@no.spam

unread,
Jul 3, 2007, 7:15:01 PM7/3/07
to
In article <1183461710.6...@x35g2000prf.googlegroups.com>,
!truth <noddy...@asustek.com.cn> wrote:

>Is there something marked for it?

Marked?

--
http://www.spinics.net/lists/netdev/

!truth

unread,
Jul 3, 2007, 11:48:59 PM7/3/07
to
On Jul 3, 7:34 am, Gil Hamilton <gil_hamil...@hotmail.com> wrote:
> !truth <noddy_zh...@asustek.com.cn> wrote in news:1183462263.324580.17010
> GH

Thanks , I have read the code about alloc_netdev, and found that it
made the member 'priv' point to the end of structure. Right?

But I'm very curious that why it does so?

el...@no.spam

unread,
Jul 4, 2007, 4:57:59 AM7/4/07
to
In article <1183520939.9...@e9g2000prf.googlegroups.com>,
!truth <noddy...@asustek.com.cn> wrote:

>But I'm very curious that why it does so?

You've been told why several times. Perhaps C just
isn't your forte.

--
http://www.spinics.net/lists/netdev/
http://www.spinics.net/lists/linux-net/


!truth

unread,
Jul 4, 2007, 6:13:24 AM7/4/07
to
On 7 4 , 4 57 , e...@no.spam () wrote:
> In article <1183520939.933084.243...@e9g2000prf.googlegroups.com>,

>
> !truth <noddy_zh...@asustek.com.cn> wrote:
> >But I'm very curious that why it does so?
>
> You've been told why several times. Perhaps C just
> isn't your forte.
>
> --http://www.spinics.net/lists/netdev/http://www.spinics.net/lists/linux-net/

oh, My god.I want to change my job.

0 new messages