Re: [PATCH net-next v8 02/14] ethtool: helper functions for netlink interface
From: Michal Kubecek
Date: Wed Dec 25 2019 - 06:07:48 EST
On Mon, Dec 23, 2019 at 08:08:55PM -0800, Florian Fainelli wrote:
>
>
> On 12/22/2019 3:45 PM, Michal Kubecek wrote:
> > Add common request/reply header definition and helpers to parse request
> > header and fill reply header. Provide ethnl_update_* helpers to update
> > structure members from request attributes (to be used for *_SET requests).
> >
> > Signed-off-by: Michal Kubecek <mkubecek@xxxxxxx>
> > ---
>
> [snip]
>
> > +/**
> > + * ethnl_update_u32() - update u32 value from NLA_U32 attribute
> > + * @dst: value to update
> > + * @attr: netlink attribute with new value or null
> > + * @mod: pointer to bool for modification tracking
> > + *
> > + * Copy the u32 value from NLA_U32 netlink attribute @attr into variable
> > + * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
> > + * is set to true if this function changed the value of *dst, otherwise it
> > + * is left as is.
> > + */
>
> I would find it more intuitive if an integer was returned: < 0 in case
> of error, 0 if no change and 1 if something changed.
This trick allows simpler code on caller side when we update multiple
values in a structure - we don't have to check each return value
separately (most update helpers cannot fail). It was a suggestion from
Jiri Pirko in one of earlier discussions.
> > +static inline void ethnl_update_u32(u32 *dst, const struct nlattr *attr,
> > + bool *mod)
> > +{
> > + u32 val;
> > +
> > + if (!attr)
> > + return;
> > + val = nla_get_u32(attr);
> > + if (*dst == val)
> > + return;
> > +
> > + *dst = val;
> > + *mod = true;
> > +}
> > +
> > +/**
> > + * ethnl_update_u8() - update u8 value from NLA_U8 attribute
> > + * @dst: value to update
> > + * @attr: netlink attribute with new value or null
> > + * @mod: pointer to bool for modification tracking
> > + *
> > + * Copy the u8 value from NLA_U8 netlink attribute @attr into variable
> > + * pointed to by @dst; do nothing if @attr is null. Bool pointed to by @mod
> > + * is set to true if this function changed the value of *dst, otherwise it
> > + * is left as is.
> > + */
> > +static inline void ethnl_update_u8(u8 *dst, const struct nlattr *attr,
> > + bool *mod)
> > +{
> > + u8 val;
> > +
> > + if (!attr)
> > + return;
> > + val = nla_get_u32(attr);
>
> Should not this be nla_get_u8() here? This sounds like it is going to
> break on BE machines.
Good catch, thank you. I originally wanted to use NLA_U32 for all
numeric values (as NLA_U8 and NLA_U16 don't save any space anyway) but
then changed those in LINKINFO_GET request to NLA_U8 and forgot to fix
the helper function.
I'll fix this in v9.
Michal