hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/tools/lib/bpf/nlattr.h
....@@ -1,18 +1,13 @@
1
-/* SPDX-License-Identifier: LGPL-2.1 */
1
+/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
22
33 /*
44 * NETLINK Netlink attributes
55 *
6
- * This library is free software; you can redistribute it and/or
7
- * modify it under the terms of the GNU Lesser General Public
8
- * License as published by the Free Software Foundation version 2.1
9
- * of the License.
10
- *
116 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
127 */
138
14
-#ifndef __NLATTR_H
15
-#define __NLATTR_H
9
+#ifndef __LIBBPF_NLATTR_H
10
+#define __LIBBPF_NLATTR_H
1611
1712 #include <stdint.h>
1813 #include <linux/netlink.h>
....@@ -23,19 +18,19 @@
2318 * Standard attribute types to specify validation policy
2419 */
2520 enum {
26
- NLA_UNSPEC, /**< Unspecified type, binary data chunk */
27
- NLA_U8, /**< 8 bit integer */
28
- NLA_U16, /**< 16 bit integer */
29
- NLA_U32, /**< 32 bit integer */
30
- NLA_U64, /**< 64 bit integer */
31
- NLA_STRING, /**< NUL terminated character string */
32
- NLA_FLAG, /**< Flag */
33
- NLA_MSECS, /**< Micro seconds (64bit) */
34
- NLA_NESTED, /**< Nested attributes */
35
- __NLA_TYPE_MAX,
21
+ LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */
22
+ LIBBPF_NLA_U8, /**< 8 bit integer */
23
+ LIBBPF_NLA_U16, /**< 16 bit integer */
24
+ LIBBPF_NLA_U32, /**< 32 bit integer */
25
+ LIBBPF_NLA_U64, /**< 64 bit integer */
26
+ LIBBPF_NLA_STRING, /**< NUL terminated character string */
27
+ LIBBPF_NLA_FLAG, /**< Flag */
28
+ LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */
29
+ LIBBPF_NLA_NESTED, /**< Nested attributes */
30
+ __LIBBPF_NLA_TYPE_MAX,
3631 };
3732
38
-#define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
33
+#define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
3934
4035 /**
4136 * @ingroup attr
....@@ -43,8 +38,8 @@
4338 *
4439 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
4540 */
46
-struct nla_policy {
47
- /** Type of attribute or NLA_UNSPEC */
41
+struct libbpf_nla_policy {
42
+ /** Type of attribute or LIBBPF_NLA_UNSPEC */
4843 uint16_t type;
4944
5045 /** Minimal length of payload required */
....@@ -62,11 +57,50 @@
6257 * @arg len length of attribute stream
6358 * @arg rem initialized to len, holds bytes currently remaining in stream
6459 */
65
-#define nla_for_each_attr(pos, head, len, rem) \
60
+#define libbpf_nla_for_each_attr(pos, head, len, rem) \
6661 for (pos = head, rem = len; \
6762 nla_ok(pos, rem); \
6863 pos = nla_next(pos, &(rem)))
6964
70
-int nla_dump_errormsg(struct nlmsghdr *nlh);
65
+/**
66
+ * libbpf_nla_data - head of payload
67
+ * @nla: netlink attribute
68
+ */
69
+static inline void *libbpf_nla_data(const struct nlattr *nla)
70
+{
71
+ return (char *) nla + NLA_HDRLEN;
72
+}
7173
72
-#endif /* __NLATTR_H */
74
+static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
75
+{
76
+ return *(uint8_t *)libbpf_nla_data(nla);
77
+}
78
+
79
+static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
80
+{
81
+ return *(uint32_t *)libbpf_nla_data(nla);
82
+}
83
+
84
+static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
85
+{
86
+ return (const char *)libbpf_nla_data(nla);
87
+}
88
+
89
+/**
90
+ * libbpf_nla_len - length of payload
91
+ * @nla: netlink attribute
92
+ */
93
+static inline int libbpf_nla_len(const struct nlattr *nla)
94
+{
95
+ return nla->nla_len - NLA_HDRLEN;
96
+}
97
+
98
+int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
99
+ int len, struct libbpf_nla_policy *policy);
100
+int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
101
+ struct nlattr *nla,
102
+ struct libbpf_nla_policy *policy);
103
+
104
+int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
105
+
106
+#endif /* __LIBBPF_NLATTR_H */