]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - tools/lib/bpf/nlattr.h
Merge tag 'powerpc-5.3-4' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-eoan-kernel.git] / tools / lib / bpf / nlattr.h
CommitLineData
1bc38b8f 1/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
bbf48c18
EL
2
3/*
4 * NETLINK Netlink attributes
5 *
bbf48c18
EL
6 * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
7 */
8
eff81908
AI
9#ifndef __LIBBPF_NLATTR_H
10#define __LIBBPF_NLATTR_H
bbf48c18
EL
11
12#include <stdint.h>
13#include <linux/netlink.h>
14/* avoid multiple definition of netlink features */
15#define __LINUX_NETLINK_H
16
17/**
18 * Standard attribute types to specify validation policy
19 */
20enum {
f04bc8a4
AI
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,
bbf48c18
EL
31};
32
f04bc8a4 33#define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
bbf48c18
EL
34
35/**
36 * @ingroup attr
37 * Attribute validation policy.
38 *
39 * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
40 */
f04bc8a4
AI
41struct libbpf_nla_policy {
42 /** Type of attribute or LIBBPF_NLA_UNSPEC */
bbf48c18
EL
43 uint16_t type;
44
45 /** Minimal length of payload required */
46 uint16_t minlen;
47
48 /** Maximal length of payload allowed */
49 uint16_t maxlen;
50};
51
52/**
53 * @ingroup attr
54 * Iterate over a stream of attributes
55 * @arg pos loop counter, set to current attribute
56 * @arg head head of attribute stream
57 * @arg len length of attribute stream
58 * @arg rem initialized to len, holds bytes currently remaining in stream
59 */
f04bc8a4 60#define libbpf_nla_for_each_attr(pos, head, len, rem) \
bbf48c18
EL
61 for (pos = head, rem = len; \
62 nla_ok(pos, rem); \
63 pos = nla_next(pos, &(rem)))
64
36f1678d 65/**
f04bc8a4 66 * libbpf_nla_data - head of payload
36f1678d
YS
67 * @nla: netlink attribute
68 */
f04bc8a4 69static inline void *libbpf_nla_data(const struct nlattr *nla)
36f1678d
YS
70{
71 return (char *) nla + NLA_HDRLEN;
72}
73
f04bc8a4 74static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
36f1678d 75{
f04bc8a4 76 return *(uint8_t *)libbpf_nla_data(nla);
36f1678d
YS
77}
78
f04bc8a4 79static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
36f1678d 80{
f04bc8a4 81 return *(uint32_t *)libbpf_nla_data(nla);
36f1678d
YS
82}
83
f04bc8a4 84static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
36f1678d 85{
f04bc8a4 86 return (const char *)libbpf_nla_data(nla);
36f1678d
YS
87}
88
89/**
f04bc8a4 90 * libbpf_nla_len - length of payload
36f1678d
YS
91 * @nla: netlink attribute
92 */
f04bc8a4 93static inline int libbpf_nla_len(const struct nlattr *nla)
36f1678d
YS
94{
95 return nla->nla_len - NLA_HDRLEN;
96}
97
f04bc8a4
AI
98int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
99 int len, struct libbpf_nla_policy *policy);
100int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
101 struct nlattr *nla,
102 struct libbpf_nla_policy *policy);
36f1678d 103
f04bc8a4 104int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
bbf48c18 105
eff81908 106#endif /* __LIBBPF_NLATTR_H */