New upstream version 18.11-rc3
[deb_dpdk.git] / drivers / net / tap / tap_netlink.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <linux/netlink.h>
9 #include <string.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 #include <rte_malloc.h>
14 #include <tap_netlink.h>
15 #include <rte_random.h>
16 #include "tap_log.h"
17
18 /* Must be quite large to support dumping a huge list of QDISC or filters. */
19 #define BUF_SIZE (32 * 1024) /* Size of the buffer to receive kernel messages */
20 #define SNDBUF_SIZE 32768 /* Send buffer size for the netlink socket */
21 #define RCVBUF_SIZE 32768 /* Receive buffer size for the netlink socket */
22
23 struct nested_tail {
24         struct rtattr *tail;
25         struct nested_tail *prev;
26 };
27
28 /**
29  * Initialize a netlink socket for communicating with the kernel.
30  *
31  * @param nl_groups
32  *   Set it to a netlink group value (e.g. RTMGRP_LINK) to receive messages for
33  *   specific netlink multicast groups. Otherwise, no subscription will be made.
34  *
35  * @return
36  *   netlink socket file descriptor on success, -1 otherwise.
37  */
38 int
39 tap_nl_init(uint32_t nl_groups)
40 {
41         int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
42         struct sockaddr_nl local = {
43                 .nl_family = AF_NETLINK,
44                 .nl_groups = nl_groups,
45         };
46
47         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
48         if (fd < 0) {
49                 TAP_LOG(ERR, "Unable to create a netlink socket");
50                 return -1;
51         }
52         if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
53                 TAP_LOG(ERR, "Unable to set socket buffer send size");
54                 close(fd);
55                 return -1;
56         }
57         if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
58                 TAP_LOG(ERR, "Unable to set socket buffer receive size");
59                 close(fd);
60                 return -1;
61         }
62         if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
63                 TAP_LOG(ERR, "Unable to bind to the netlink socket");
64                 close(fd);
65                 return -1;
66         }
67         return fd;
68 }
69
70 /**
71  * Clean up a netlink socket once all communicating with the kernel is finished.
72  *
73  * @param[in] nlsk_fd
74  *   The netlink socket file descriptor used for communication.
75  *
76  * @return
77  *   0 on success, -1 otherwise.
78  */
79 int
80 tap_nl_final(int nlsk_fd)
81 {
82         if (close(nlsk_fd)) {
83                 TAP_LOG(ERR, "Failed to close netlink socket: %s (%d)",
84                         strerror(errno), errno);
85                 return -1;
86         }
87         return 0;
88 }
89
90 /**
91  * Send a message to the kernel on the netlink socket.
92  *
93  * @param[in] nlsk_fd
94  *   The netlink socket file descriptor used for communication.
95  * @param[in] nh
96  *   The netlink message send to the kernel.
97  *
98  * @return
99  *   the number of sent bytes on success, -1 otherwise.
100  */
101 int
102 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
103 {
104         /* man 7 netlink EXAMPLE */
105         struct sockaddr_nl sa = {
106                 .nl_family = AF_NETLINK,
107         };
108         struct iovec iov = {
109                 .iov_base = nh,
110                 .iov_len = nh->nlmsg_len,
111         };
112         struct msghdr msg = {
113                 .msg_name = &sa,
114                 .msg_namelen = sizeof(sa),
115                 .msg_iov = &iov,
116                 .msg_iovlen = 1,
117         };
118         int send_bytes;
119
120         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
121         nh->nlmsg_seq = (uint32_t)rte_rand();
122         send_bytes = sendmsg(nlsk_fd, &msg, 0);
123         if (send_bytes < 0) {
124                 TAP_LOG(ERR, "Failed to send netlink message: %s (%d)",
125                         strerror(errno), errno);
126                 return -1;
127         }
128         return send_bytes;
129 }
130
131 /**
132  * Check that the kernel sends an appropriate ACK in response
133  * to an tap_nl_send().
134  *
135  * @param[in] nlsk_fd
136  *   The netlink socket file descriptor used for communication.
137  *
138  * @return
139  *   0 on success, -1 otherwise with errno set.
140  */
141 int
142 tap_nl_recv_ack(int nlsk_fd)
143 {
144         return tap_nl_recv(nlsk_fd, NULL, NULL);
145 }
146
147 /**
148  * Receive a message from the kernel on the netlink socket, following an
149  * tap_nl_send().
150  *
151  * @param[in] nlsk_fd
152  *   The netlink socket file descriptor used for communication.
153  * @param[in] cb
154  *   The callback function to call for each netlink message received.
155  * @param[in, out] arg
156  *   Custom arguments for the callback.
157  *
158  * @return
159  *   0 on success, -1 otherwise with errno set.
160  */
161 int
162 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
163 {
164         /* man 7 netlink EXAMPLE */
165         struct sockaddr_nl sa;
166         char buf[BUF_SIZE];
167         struct iovec iov = {
168                 .iov_base = buf,
169                 .iov_len = sizeof(buf),
170         };
171         struct msghdr msg = {
172                 .msg_name = &sa,
173                 .msg_namelen = sizeof(sa),
174                 .msg_iov = &iov,
175                 /* One message at a time */
176                 .msg_iovlen = 1,
177         };
178         int multipart = 0;
179         int ret = 0;
180
181         do {
182                 struct nlmsghdr *nh;
183                 int recv_bytes = 0;
184
185                 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
186                 if (recv_bytes < 0)
187                         return -1;
188                 for (nh = (struct nlmsghdr *)buf;
189                      NLMSG_OK(nh, (unsigned int)recv_bytes);
190                      nh = NLMSG_NEXT(nh, recv_bytes)) {
191                         if (nh->nlmsg_type == NLMSG_ERROR) {
192                                 struct nlmsgerr *err_data = NLMSG_DATA(nh);
193
194                                 if (err_data->error < 0) {
195                                         errno = -err_data->error;
196                                         return -1;
197                                 }
198                                 /* Ack message. */
199                                 return 0;
200                         }
201                         /* Multi-part msgs and their trailing DONE message. */
202                         if (nh->nlmsg_flags & NLM_F_MULTI) {
203                                 if (nh->nlmsg_type == NLMSG_DONE)
204                                         return 0;
205                                 multipart = 1;
206                         }
207                         if (cb)
208                                 ret = cb(nh, arg);
209                 }
210         } while (multipart);
211         return ret;
212 }
213
214 /**
215  * Append a netlink attribute to a message.
216  *
217  * @param[in, out] nh
218  *   The netlink message to parse, received from the kernel.
219  * @param[in] type
220  *   The type of attribute to append.
221  * @param[in] data_len
222  *   The length of the data to append.
223  * @param[in] data
224  *   The data to append.
225  */
226 void
227 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
228            unsigned int data_len, const void *data)
229 {
230         /* see man 3 rtnetlink */
231         struct rtattr *rta;
232
233         rta = (struct rtattr *)NLMSG_TAIL(nh);
234         rta->rta_len = RTA_LENGTH(data_len);
235         rta->rta_type = type;
236         memcpy(RTA_DATA(rta), data, data_len);
237         nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
238 }
239
240 /**
241  * Append a uint8_t netlink attribute to a message.
242  *
243  * @param[in, out] nh
244  *   The netlink message to parse, received from the kernel.
245  * @param[in] type
246  *   The type of attribute to append.
247  * @param[in] data
248  *   The data to append.
249  */
250 void
251 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
252 {
253         tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
254 }
255
256 /**
257  * Append a uint16_t netlink attribute to a message.
258  *
259  * @param[in, out] nh
260  *   The netlink message to parse, received from the kernel.
261  * @param[in] type
262  *   The type of attribute to append.
263  * @param[in] data
264  *   The data to append.
265  */
266 void
267 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
268 {
269         tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
270 }
271
272 /**
273  * Append a uint16_t netlink attribute to a message.
274  *
275  * @param[in, out] nh
276  *   The netlink message to parse, received from the kernel.
277  * @param[in] type
278  *   The type of attribute to append.
279  * @param[in] data
280  *   The data to append.
281  */
282 void
283 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
284 {
285         tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
286 }
287
288 /**
289  * Start a nested netlink attribute.
290  * It must be followed later by a call to tap_nlattr_nested_finish().
291  *
292  * @param[in, out] msg
293  *   The netlink message where to edit the nested_tails metadata.
294  * @param[in] type
295  *   The nested attribute type to append.
296  *
297  * @return
298  *   -1 if adding a nested netlink attribute failed, 0 otherwise.
299  */
300 int
301 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
302 {
303         struct nested_tail *tail;
304
305         tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
306         if (!tail) {
307                 TAP_LOG(ERR,
308                         "Couldn't allocate memory for nested netlink attribute");
309                 return -1;
310         }
311
312         tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
313
314         tap_nlattr_add(&msg->nh, type, 0, NULL);
315
316         tail->prev = msg->nested_tails;
317
318         msg->nested_tails = tail;
319
320         return 0;
321 }
322
323 /**
324  * End a nested netlink attribute.
325  * It follows a call to tap_nlattr_nested_start().
326  * In effect, it will modify the nested attribute length to include every bytes
327  * from the nested attribute start, up to here.
328  *
329  * @param[in, out] msg
330  *   The netlink message where to edit the nested_tails metadata.
331  */
332 void
333 tap_nlattr_nested_finish(struct nlmsg *msg)
334 {
335         struct nested_tail *tail = msg->nested_tails;
336
337         tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
338
339         if (tail->prev)
340                 msg->nested_tails = tail->prev;
341
342         rte_free(tail);
343 }