xref: /linux/tools/testing/selftests/acct/netlink_helper.h (revision e5f92606156a6a823992294d214c285b49cd72e9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Shared generic netlink helpers for the acct selftests.
4  */
5 #ifndef ACSELFTESTS_ACCT_NETLINK_HELPER_H
6 #define ACSELFTESTS_ACCT_NETLINK_HELPER_H
7 
8 #include <stdbool.h>
9 #include <linux/netlink.h>
10 
11 #ifndef NLA_ALIGNTO
12 #define NLA_ALIGNTO 4
13 #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
14 #define NLA_HDRLEN ((int)NLA_ALIGN(sizeof(struct nlattr)))
15 #endif
16 
17 /* Fail an individual test case instead of hanging the whole binary. */
18 #define ACCT_RCV_TIMEOUT_SEC 2
19 
20 static inline void *nla_data(const struct nlattr *na)
21 {
22 	return (void *)((char *)na + NLA_HDRLEN);
23 }
24 
25 static inline bool nla_ok(const struct nlattr *na, int remaining)
26 {
27 	return remaining >= (int)sizeof(*na) &&
28 	       na->nla_len >= sizeof(*na) &&
29 	       na->nla_len <= remaining;
30 }
31 
32 static inline struct nlattr *nla_next(const struct nlattr *na, int *remaining)
33 {
34 	int aligned_len = NLA_ALIGN(na->nla_len);
35 
36 	*remaining -= aligned_len;
37 	return (struct nlattr *)((char *)na + aligned_len);
38 }
39 
40 int netlink_open(void);
41 int send_request(int fd, void *buf, size_t len);
42 int get_family_id(int fd, const char *name);
43 
44 #endif /* ACSELFTESTS_ACCT_NETLINK_HELPER_H */
45