Skip to contentSkip to conversation

src: attr: Add mnl_attr_get_uint() function

PATCH In review Danielle Ratson 5 participants 14 messages Jul 31, 2024 – Oct 1, 2024 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 src: attr: Add mnl_attr_get_uint() function

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary.

There are some NLA_UINT attributes that lack an appropriate getter function.

Add a function mnl_attr_get_uint() to cover that extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

---
 include/libmnl/libmnl.h |  1 +
 src/attr.c              | 22 ++++++++++++++++++++++
 src/libmnl.map          |  4 ++++
 3 files changed, 27 insertions(+)
include/libmnl/libmnl.h +1 -0
index 4bd0b92..9c03280 100644
@@ -92,6 +92,7 @@ extern uint8_t mnl_attr_get_u8(const struct nlattr *attr);
 extern uint16_t mnl_attr_get_u16(const struct nlattr *attr);
 extern uint32_t mnl_attr_get_u32(const struct nlattr *attr);
 extern uint64_t mnl_attr_get_u64(const struct nlattr *attr);
+extern uint64_t mnl_attr_get_uint(const struct nlattr *attr);
 extern const char *mnl_attr_get_str(const struct nlattr *attr);
 
 /* TLV attribute putters */
src/attr.c +22 -0
index bc39df4..399318e 100644
@@ -389,6 +389,28 @@ EXPORT_SYMBOL uint64_t mnl_attr_get_u64(const struct nlattr *attr)
 	return tmp;
 }
 
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+	switch (mnl_attr_get_payload_len(attr)) {
+	case sizeof(uint8_t):
+		return mnl_attr_get_u8(attr);
+	case sizeof(uint16_t):
+		return mnl_attr_get_u16(attr);
+	case sizeof(uint32_t):
+		return mnl_attr_get_u32(attr);
+	case sizeof(uint64_t):
+		return mnl_attr_get_u64(attr);
+	}
+
+	return -1ULL;
+}
+
 /**
  * mnl_attr_get_str - get pointer to string attribute
  * \param attr pointer to netlink attribute
src/libmnl.map +4 -0
index e5920e5..cd58863 100644
@@ -77,3 +77,7 @@ LIBMNL_1.2 {
   mnl_socket_open2;
   mnl_socket_fdopen;
 } LIBMNL_1.1;
+
+LIBMNL_1.3 {
+  mnl_attr_get_uint;
+} LIBMNL_1.2;
Signature · 1 line
2.45.0
reviewer
#1 lore raw
Quoted 10 lines · Danielle Ratson

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary.

There are some NLA_UINT attributes that lack an appropriate getter function.

Add a function mnl_attr_get_uint() to cover that extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

Patch applied, thanks!

#2 lore raw

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

Thanks,
Danielle

Quoted 22 lines

-----Original Message-----
From: Phil Sutter <[email protected]>
Sent: Wednesday, 31 July 2024 10:15
To: Danielle Ratson <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; mlxsw <[email protected]>
Subject: Re: [PATCH libmnl] src: attr: Add mnl_attr_get_uint() function

Quoted 4 lines · Danielle Ratson

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary.

There are some NLA_UINT attributes that lack an appropriate getter

function.

Quoted 6 lines

Add a function mnl_attr_get_uint() to cover that extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

Patch applied, thanks!

#3 lore raw
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

Quoted 25 lines

Thanks,
Danielle

Quoted 22 lines

-----Original Message-----
From: Phil Sutter <[email protected]>
Sent: Wednesday, 31 July 2024 10:15
To: Danielle Ratson <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; mlxsw <[email protected]>
Subject: Re: [PATCH libmnl] src: attr: Add mnl_attr_get_uint() function

Quoted 4 lines · Danielle Ratson

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary.

There are some NLA_UINT attributes that lack an appropriate getter

function.

Quoted 6 lines

Add a function mnl_attr_get_uint() to cover that extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

Patch applied, thanks!

#4 lore raw

Cc'ing Jakub.

Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr) +{
+ switch (mnl_attr_get_payload_len(attr)) {
+ case sizeof(uint8_t):
+ return mnl_attr_get_u8(attr);
+ case sizeof(uint16_t):
+ return mnl_attr_get_u16(attr);
+ case sizeof(uint32_t):
+ return mnl_attr_get_u32(attr);
+ case sizeof(uint64_t):
+ return mnl_attr_get_u64(attr);
+ }
+
+ return -1ULL;
+}

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

Thanks.

#5 lore raw
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

src/attr.c +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

#6 lore raw
Quoted 54 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

src/attr.c +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 6 lines
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Not sure. @Danielle: could you clarify your intention?

If this is to support NLA_UINT, I'd prefer to stick to NLA_UINT semantics.

@Jakub: is there any plan to augment NLA_UINT in the future? What the assumption from userspace that this will always return 32-bits else 64-bits value?

Thanks.

#7 lore raw
Quoted 55 lines · Pablo Neira Ayuso
Quoted 54 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

#8 lore raw
Quoted 65 lines · Petr Machata
Quoted 62 lines · Pablo Neira Ayuso
Quoted 60 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Quoted 5 lines
Quoted 1 line

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

Which helper are your referring to? Is it modeled after NLA_UINT?

I don't think this patch is fine. This also returns -1ULL so there is no way to know if size is not correct or payload length is 64 bits using UINT64_MAX?

Thanks.

#9 lore raw
Quoted 72 lines · Pablo Neira Ayuso
Quoted 65 lines · Petr Machata
Quoted 62 lines · Pablo Neira Ayuso
Quoted 60 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Quoted 5 lines
Quoted 1 line

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

Quoted 5 lines

Which helper are your referring to? Is it modeled after NLA_UINT?

I don't think this patch is fine. This also returns -1ULL so there is no way to know if size is not correct or payload length is 64 bits using UINT64_MAX?

I found it:

static inline __u64 rta_getattr_uint(const struct rtattr *rta)

This only has one user in the tree so far, right?

include/libnetlink.h:static inline __u64 rta_getattr_uint(const struct rtattr *rta)

ip/ipnexthop.c:                 nh_grp_stats->packets = rta_getattr_uint(rta);
ip/ipnexthop.c:                 nh_grp_stats->packets_hw = rta_getattr_uint(rta);

is this attribute for ipnexthop of NLA_UINT type?

#10 lore raw
Quoted 79 lines · Pablo Neira Ayuso
Quoted 72 lines · Pablo Neira Ayuso
Quoted 65 lines · Petr Machata
Quoted 62 lines · Pablo Neira Ayuso
Quoted 60 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Quoted 5 lines
Quoted 1 line

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

Quoted 5 lines

Which helper are your referring to? Is it modeled after NLA_UINT?

I don't think this patch is fine. This also returns -1ULL so there is no way to know if size is not correct or payload length is 64 bits using UINT64_MAX?

Quoted 5 lines

I found it:

static inline __u64 rta_getattr_uint(const struct rtattr *rta)

This only has one user in the tree so far, right?

Well, this is a matter of documenting behaviour.

Quoted 5 lines

include/libnetlink.h:static inline __u64 rta_getattr_uint(const struct rtattr *rta)

ip/ipnexthop.c:                 nh_grp_stats->packets = rta_getattr_uint(rta);
ip/ipnexthop.c:                 nh_grp_stats->packets_hw = rta_getattr_uint(rta);

is this attribute for ipnexthop of NLA_UINT type?

But it seems intention is to support NLA_UINT according to iproute's commit.

commit 95836fbf35d352f7c031ddac2e6093a935308cc9

Author: Petr Machata <[email protected]>
Date:   Thu Mar 14 15:52:12 2024 +0100

libnetlink: Add rta_getattr_uint()

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary. Add a function to extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

so it went further to make it universal for 2^3..2^6 values.

I am going to submit a patch to provide more info on this helper function.

#11 lore raw
Quoted 103 lines · Pablo Neira Ayuso
Quoted 79 lines · Pablo Neira Ayuso
Quoted 72 lines · Pablo Neira Ayuso
Quoted 65 lines · Petr Machata
Quoted 62 lines · Pablo Neira Ayuso
Quoted 60 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Quoted 5 lines
Quoted 1 line

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

Quoted 5 lines

Which helper are your referring to? Is it modeled after NLA_UINT?

I don't think this patch is fine. This also returns -1ULL so there is no way to know if size is not correct or payload length is 64 bits using UINT64_MAX?

Quoted 5 lines

I found it:

static inline __u64 rta_getattr_uint(const struct rtattr *rta)

This only has one user in the tree so far, right?

Well, this is a matter of documenting behaviour.

Quoted 5 lines

include/libnetlink.h:static inline __u64 rta_getattr_uint(const struct rtattr *rta)

ip/ipnexthop.c:                 nh_grp_stats->packets = rta_getattr_uint(rta);
ip/ipnexthop.c:                 nh_grp_stats->packets_hw = rta_getattr_uint(rta);

is this attribute for ipnexthop of NLA_UINT type?

But it seems intention is to support NLA_UINT according to iproute's commit.

commit 95836fbf35d352f7c031ddac2e6093a935308cc9

Author: Petr Machata <[email protected]>
Date:   Thu Mar 14 15:52:12 2024 +0100

libnetlink: Add rta_getattr_uint()

Quoted 8 lines

NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one if necessary. Add a function to extract these. Since we need to dispatch on length anyway, make the getter truly universal by supporting also u8 and u16.

so it went further to make it universal for 2^3..2^6 values.

I am going to submit a patch to provide more info on this helper function.

Yeah, sorry, I wrote this to explain the rationale:

Quoted 3 lines

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

... but of course I should have written <32-bit payloads, not >32-bit.

#12 lore raw
In reply to Pablo Neira Ayuso
Quoted 73 lines · Pablo Neira Ayuso
Quoted 72 lines · Pablo Neira Ayuso
Quoted 70 lines · Petr Machata
Quoted 55 lines · Pablo Neira Ayuso
Quoted 54 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype, u64 value)

{
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64);
}

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Not sure. @Danielle: could you clarify your intention?

This follows the iproute2 helper, where I was asked to support >32-bit fields purely as a service to the users, so that one helper can be used for any integral field.

Quoted 5 lines
Quoted 5 lines

Which helper are your referring to? Is it modeled after NLA_UINT?

I don't think this patch is fine. This also returns -1ULL so there is no way to know if size is not correct or payload length is 64 bits using UINT64_MAX?

That's no different from the other mnl_attr_get_uX() helpers that just assume the user has passed in the right attribute type.

Quoted 3 lines

I found it:

static inline __u64 rta_getattr_uint(const struct rtattr *rta)

That's the one.

Quoted 1 line

This only has one user in the tree so far, right?

Well, two, but yeah, the u8 / u16 stuff is not currently used.

Quoted 5 lines

include/libnetlink.h:static inline __u64 rta_getattr_uint(const struct rtattr *rta)

ip/ipnexthop.c:                 nh_grp_stats->packets = rta_getattr_uint(rta);
ip/ipnexthop.c:                 nh_grp_stats->packets_hw = rta_getattr_uint(rta);

is this attribute for ipnexthop of NLA_UINT type?

Yeah, both NHA_GROUP_STATS_ENTRY_PACKETS and
NHA_GROUP_STATS_ENTRY_PACKETS_HW are NLA_UINT.

#13 lore raw
In reply to Pablo Neira Ayuso
Quoted 62 lines

From: Pablo Neira Ayuso <[email protected]>
Sent: Monday, 30 September 2024 15:48
To: Jakub Kicinski <[email protected]>
Cc: Danielle Ratson <[email protected]>; Phil Sutter <[email protected]>; [email protected]; [email protected]; mlxsw <[email protected]>
Subject: Re: [PATCH libmnl] src: attr: Add mnl_attr_get_uint() function

Quoted 54 lines · Jakub Kicinski
Quoted 53 lines · On Mon, 30 Sep 2024 12:56:20 +0200 Pablo Neira Ayuso wrote:
Quoted 7 lines · Pablo Neira Ayuso
Quoted 4 lines · Danielle Ratson

Hi,

Is there a plan to build a new version soon?
I am asking since I am planning to use this function in ethtool.

ASAP

but one question before... Is this related to NLA_UINT in the kernel?

/**
* nla_put_uint - Add a variable-size unsigned int to a socket
buffer
* @skb: socket buffer to add attribute to
* @attrtype: attribute type
* @value: numeric value
*/
static inline int nla_put_uint(struct sk_buff *skb, int attrtype,

u64 value) {
        u64 tmp64 = value;
        u32 tmp32 = value;

        if (tmp64 == tmp32)
                return nla_put_u32(skb, attrtype, tmp32);
        return nla_put(skb, attrtype, sizeof(u64), &tmp64); }

if I'm correct, it seems kernel always uses either u32 or u64.

Userspace assumes u8 and u16 are possible though:

(quoted diff) +21 -0
+/**
+ * mnl_attr_get_uint - returns 64-bit unsigned integer attribute.
+ * \param attr pointer to netlink attribute
+ *
+ * This function returns the 64-bit value of the attribute payload.
+ */
+EXPORT_SYMBOL uint64_t mnl_attr_get_uint(const struct nlattr *attr)
+{
+       switch (mnl_attr_get_payload_len(attr)) {
+       case sizeof(uint8_t):
+               return mnl_attr_get_u8(attr);
+       case sizeof(uint16_t):
+               return mnl_attr_get_u16(attr);
+       case sizeof(uint32_t):
+               return mnl_attr_get_u32(attr);
+       case sizeof(uint64_t):
+               return mnl_attr_get_u64(attr);
+       }
+
+       return -1ULL;
+}
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Or this is an attempt to provide a helper that allows you fetch for payload value of 2^3..2^6 bytes?

No preference here, FWIW. Looks like this patch does a different thing than the kernel. But maybe a broader "automatic" helper is useful for user space code.

Not sure. @Danielle: could you clarify your intention?

Hi,

Thanks for all the comments.

As I see it, there are at least two occurrences in ethtool for a code that tries to overcome the lack of this kind of uint helper when the uint attribute type exists in the kernel and seems like more recommended to use then before.

Therefore, as I see it, this helper seems more reasonable to have.

But I am trying to understand, are we discussing the need of the patch at all? Or some nits about the way it is implemented? Or something else? I am asking since the patch was already taken by @Pablo Neira Ayuso.

Quoted 8 lines

If this is to support NLA_UINT, I'd prefer to stick to NLA_UINT semantics.

@Jakub: is there any plan to augment NLA_UINT in the future? What the assumption from userspace that this will always return 32-bits else 64-bits value?

Thanks.