All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH libmnl] src: attr: Add mnl_attr_get_uint() function
@ 2024-07-31  6:35 Danielle Ratson
  2024-07-31  7:14 ` Phil Sutter
  0 siblings, 1 reply; 14+ messages in thread
From: Danielle Ratson @ 2024-07-31  6:35 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo, phil, fw, mlxsw, 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.

Signed-off-by: Danielle Ratson <danieller@nvidia.com>
---
 include/libmnl/libmnl.h |  1 +
 src/attr.c              | 22 ++++++++++++++++++++++
 src/libmnl.map          |  4 ++++
 3 files changed, 27 insertions(+)

diff --git a/include/libmnl/libmnl.h b/include/libmnl/libmnl.h
index 4bd0b92..9c03280 100644
--- a/include/libmnl/libmnl.h
+++ b/include/libmnl/libmnl.h
@@ -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 */
diff --git a/src/attr.c b/src/attr.c
index bc39df4..399318e 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -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
diff --git a/src/libmnl.map b/src/libmnl.map
index e5920e5..cd58863 100644
--- a/src/libmnl.map
+++ b/src/libmnl.map
@@ -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;
-- 
2.45.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-10-01  9:19 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-31  6:35 [PATCH libmnl] src: attr: Add mnl_attr_get_uint() function Danielle Ratson
2024-07-31  7:14 ` Phil Sutter
2024-09-29 10:42   ` Danielle Ratson
2024-09-30 10:28     ` Pablo Neira Ayuso
2024-09-30 10:56       ` Pablo Neira Ayuso
2024-09-30 11:45         ` Jakub Kicinski
2024-09-30 12:48           ` Pablo Neira Ayuso
2024-09-30 16:25             ` Petr Machata
2024-09-30 17:01               ` Pablo Neira Ayuso
2024-09-30 17:10                 ` Pablo Neira Ayuso
2024-09-30 17:52                   ` Pablo Neira Ayuso
2024-10-01  8:16                     ` Petr Machata
2024-10-01  7:51                   ` Petr Machata
2024-10-01  9:19             ` Danielle Ratson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.