• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright (c) 2021 Linux Test Project
3  */
4 
5 #ifndef TST_RTNETLINK_H
6 #define TST_RTNETLINK_H
7 
8 struct tst_rtnl_context;
9 
10 struct tst_rtnl_attr_list {
11 	unsigned short type;
12 	const void *data;
13 	ssize_t len;
14 	const struct tst_rtnl_attr_list *sublist;
15 };
16 
17 struct tst_rtnl_message {
18 	struct nlmsghdr *header;
19 	struct nlmsgerr *err;
20 	void *payload;
21 	size_t payload_size;
22 };
23 
24 /* Open a netlink socket */
25 struct tst_rtnl_context *tst_rtnl_create_context(const char *file,
26 	const int lineno);
27 #define RTNL_CREATE_CONTEXT() tst_rtnl_create_context(__FILE__, __LINE__)
28 
29 /* Free a tst_rtnl_message array returned by tst_rtnl_recv() */
30 void tst_rtnl_free_message(struct tst_rtnl_message *msg);
31 #define RTNL_FREE_MESSAGE tst_rtnl_free_message
32 
33 /* Close netlink socket */
34 void tst_rtnl_destroy_context(const char *file, const int lineno,
35 	struct tst_rtnl_context *ctx);
36 #define RTNL_DESTROY_CONTEXT(ctx) \
37 	tst_rtnl_destroy_context(__FILE__, __LINE__, (ctx))
38 
39 /* Send all messages in given buffer */
40 int tst_rtnl_send(const char *file, const int lineno,
41 	struct tst_rtnl_context *ctx);
42 #define RTNL_SEND(ctx) tst_rtnl_send(__FILE__, __LINE__, (ctx))
43 
44 /* Send all messages in given buffer and validate kernel response */
45 int tst_rtnl_send_validate(const char *file, const int lineno,
46 	struct tst_rtnl_context *ctx);
47 #define RTNL_SEND_VALIDATE(ctx) \
48 	tst_rtnl_send_validate(__FILE__, __LINE__, (ctx))
49 
50 /* Wait until data is available for reading from the netlink socket */
51 int tst_rtnl_wait(struct tst_rtnl_context *ctx);
52 #define RTNL_WAIT tst_rtnl_wait
53 
54 /*
55  * Read from netlink socket and return an array of partially parsed messages.
56  * header == NULL indicates end of array.
57  */
58 struct tst_rtnl_message *tst_rtnl_recv(const char *file, const int lineno,
59 	struct tst_rtnl_context *ctx);
60 #define RTNL_RECV(ctx) tst_rtnl_recv(__FILE__, __LINE__, (ctx))
61 
62 /* Add new message to buffer */
63 int tst_rtnl_add_message(const char *file, const int lineno,
64 	struct tst_rtnl_context *ctx, const struct nlmsghdr *header,
65 	const void *payload, size_t payload_size);
66 #define RTNL_ADD_MESSAGE(ctx, header, payload, psize) \
67 	tst_rtnl_add_message(__FILE__, __LINE__, (ctx), (header), (payload), \
68 		(psize))
69 
70 /* Add arbitrary attribute to last message */
71 int tst_rtnl_add_attr(const char *file, const int lineno,
72 	struct tst_rtnl_context *ctx, unsigned short type, const void *data,
73 	unsigned short len);
74 #define RTNL_ADD_ATTR(ctx, type, data, len) \
75 	tst_rtnl_add_attr(__FILE__, __LINE__, (ctx), (type), (data), (len))
76 
77 /* Add string attribute to last message */
78 int tst_rtnl_add_attr_string(const char *file, const int lineno,
79 	struct tst_rtnl_context *ctx, unsigned short type, const char *data);
80 #define RTNL_ADD_ATTR_STRING(ctx, type, data) \
81 	tst_rtnl_add_attr_string(__FILE__, __LINE__, (ctx), (type), (data))
82 
83 /*
84  * Add list of arbitrary attributes to last message. The list is terminated
85  * by attribute with negative length. Nested sublists are supported.
86  */
87 int tst_rtnl_add_attr_list(const char *file, const int lineno,
88 	struct tst_rtnl_context *ctx, const struct tst_rtnl_attr_list *list);
89 #define RTNL_ADD_ATTR_LIST(ctx, list) \
90 	tst_rtnl_add_attr_list(__FILE__, __LINE__, (ctx), (list))
91 
92 /* Check that all sent messages with NLM_F_ACK flag have been acked without
93  * error. Usage:
94  *
95  * tst_rtnl_send(ctx);
96  * tst_rtnl_wait(ctx);
97  * response = tst_rtnl_recv(ctx);
98  * if (!tst_rtnl_check_acks(ctx, response)) { ... }
99  * tst_rtnl_free_message(response);
100  */
101 int tst_rtnl_check_acks(const char *file, const int lineno,
102 	struct tst_rtnl_context *ctx, struct tst_rtnl_message *response);
103 #define RTNL_CHECK_ACKS(ctx, response) \
104 	tst_rtnl_context(__FILE__, __LINE__, (ctx), (response))
105 
106 #endif /* TST_RTNETLINK_H */
107