a10f647b1dd01e0ccfb96298ac5fd7208f8c01c4
[dpdk.git] / drivers / net / tap / tap_netlink.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <errno.h>
35 #include <inttypes.h>
36 #include <linux/netlink.h>
37 #include <string.h>
38 #include <sys/socket.h>
39 #include <unistd.h>
40
41 #include <rte_malloc.h>
42 #include <tap_netlink.h>
43 #include <rte_random.h>
44
45 /* Must be quite large to support dumping a huge list of QDISC or filters. */
46 #define BUF_SIZE (32 * 1024) /* Size of the buffer to receive kernel messages */
47 #define SNDBUF_SIZE 32768 /* Send buffer size for the netlink socket */
48 #define RCVBUF_SIZE 32768 /* Receive buffer size for the netlink socket */
49
50 struct nested_tail {
51         struct rtattr *tail;
52         struct nested_tail *prev;
53 };
54
55 /**
56  * Initialize a netlink socket for communicating with the kernel.
57  *
58  * @param nl_groups
59  *   Set it to a netlink group value (e.g. RTMGRP_LINK) to receive messages for
60  *   specific netlink multicast groups. Otherwise, no subscription will be made.
61  *
62  * @return
63  *   netlink socket file descriptor on success, -1 otherwise.
64  */
65 int
66 tap_nl_init(uint32_t nl_groups)
67 {
68         int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
69         struct sockaddr_nl local = {
70                 .nl_family = AF_NETLINK,
71                 .nl_groups = nl_groups,
72         };
73
74         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
75         if (fd < 0) {
76                 RTE_LOG(ERR, PMD, "Unable to create a netlink socket\n");
77                 return -1;
78         }
79         if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
80                 RTE_LOG(ERR, PMD, "Unable to set socket buffer send size\n");
81                 return -1;
82         }
83         if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
84                 RTE_LOG(ERR, PMD, "Unable to set socket buffer receive size\n");
85                 return -1;
86         }
87         if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
88                 RTE_LOG(ERR, PMD, "Unable to bind to the netlink socket\n");
89                 return -1;
90         }
91         return fd;
92 }
93
94 /**
95  * Clean up a netlink socket once all communicating with the kernel is finished.
96  *
97  * @param[in] nlsk_fd
98  *   The netlink socket file descriptor used for communication.
99  *
100  * @return
101  *   0 on success, -1 otherwise.
102  */
103 int
104 tap_nl_final(int nlsk_fd)
105 {
106         if (close(nlsk_fd)) {
107                 RTE_LOG(ERR, PMD, "Failed to close netlink socket: %s (%d)\n",
108                         strerror(errno), errno);
109                 return -1;
110         }
111         return 0;
112 }
113
114 /**
115  * Send a message to the kernel on the netlink socket.
116  *
117  * @param[in] nlsk_fd
118  *   The netlink socket file descriptor used for communication.
119  * @param[in] nh
120  *   The netlink message send to the kernel.
121  *
122  * @return
123  *   the number of sent bytes on success, -1 otherwise.
124  */
125 int
126 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
127 {
128         /* man 7 netlink EXAMPLE */
129         struct sockaddr_nl sa = {
130                 .nl_family = AF_NETLINK,
131         };
132         struct iovec iov = {
133                 .iov_base = nh,
134                 .iov_len = nh->nlmsg_len,
135         };
136         struct msghdr msg = {
137                 .msg_name = &sa,
138                 .msg_namelen = sizeof(sa),
139                 .msg_iov = &iov,
140                 .msg_iovlen = 1,
141         };
142         int send_bytes;
143
144         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
145         nh->nlmsg_seq = (uint32_t)rte_rand();
146         send_bytes = sendmsg(nlsk_fd, &msg, 0);
147         if (send_bytes < 0) {
148                 RTE_LOG(ERR, PMD, "Failed to send netlink message: %s (%d)\n",
149                         strerror(errno), errno);
150                 return -1;
151         }
152         return send_bytes;
153 }
154
155 /**
156  * Check that the kernel sends an appropriate ACK in response
157  * to an tap_nl_send().
158  *
159  * @param[in] nlsk_fd
160  *   The netlink socket file descriptor used for communication.
161  *
162  * @return
163  *   0 on success, -1 otherwise with errno set.
164  */
165 int
166 tap_nl_recv_ack(int nlsk_fd)
167 {
168         return tap_nl_recv(nlsk_fd, NULL, NULL);
169 }
170
171 /**
172  * Receive a message from the kernel on the netlink socket, following an
173  * tap_nl_send().
174  *
175  * @param[in] nlsk_fd
176  *   The netlink socket file descriptor used for communication.
177  * @param[in] cb
178  *   The callback function to call for each netlink message received.
179  * @param[in, out] arg
180  *   Custom arguments for the callback.
181  *
182  * @return
183  *   0 on success, -1 otherwise with errno set.
184  */
185 int
186 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
187 {
188         /* man 7 netlink EXAMPLE */
189         struct sockaddr_nl sa;
190         char buf[BUF_SIZE];
191         struct iovec iov = {
192                 .iov_base = buf,
193                 .iov_len = sizeof(buf),
194         };
195         struct msghdr msg = {
196                 .msg_name = &sa,
197                 .msg_namelen = sizeof(sa),
198                 .msg_iov = &iov,
199                 /* One message at a time */
200                 .msg_iovlen = 1,
201         };
202         int multipart = 0;
203         int ret = 0;
204
205         do {
206                 struct nlmsghdr *nh;
207                 int recv_bytes = 0;
208
209                 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
210                 if (recv_bytes < 0)
211                         return -1;
212                 for (nh = (struct nlmsghdr *)buf;
213                      NLMSG_OK(nh, (unsigned int)recv_bytes);
214                      nh = NLMSG_NEXT(nh, recv_bytes)) {
215                         if (nh->nlmsg_type == NLMSG_ERROR) {
216                                 struct nlmsgerr *err_data = NLMSG_DATA(nh);
217
218                                 if (err_data->error < 0) {
219                                         errno = -err_data->error;
220                                         return -1;
221                                 }
222                                 /* Ack message. */
223                                 return 0;
224                         }
225                         /* Multi-part msgs and their trailing DONE message. */
226                         if (nh->nlmsg_flags & NLM_F_MULTI) {
227                                 if (nh->nlmsg_type == NLMSG_DONE)
228                                         return 0;
229                                 multipart = 1;
230                         }
231                         if (cb)
232                                 ret = cb(nh, arg);
233                 }
234         } while (multipart);
235         return ret;
236 }
237
238 /**
239  * Append a netlink attribute to a message.
240  *
241  * @param[in, out] nh
242  *   The netlink message to parse, received from the kernel.
243  * @param[in] type
244  *   The type of attribute to append.
245  * @param[in] data_len
246  *   The length of the data to append.
247  * @param[in] data
248  *   The data to append.
249  */
250 void
251 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
252            unsigned int data_len, const void *data)
253 {
254         /* see man 3 rtnetlink */
255         struct rtattr *rta;
256
257         rta = (struct rtattr *)NLMSG_TAIL(nh);
258         rta->rta_len = RTA_LENGTH(data_len);
259         rta->rta_type = type;
260         memcpy(RTA_DATA(rta), data, data_len);
261         nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
262 }
263
264 /**
265  * Append a uint8_t netlink attribute to a message.
266  *
267  * @param[in, out] nh
268  *   The netlink message to parse, received from the kernel.
269  * @param[in] type
270  *   The type of attribute to append.
271  * @param[in] data
272  *   The data to append.
273  */
274 void
275 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
276 {
277         tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
278 }
279
280 /**
281  * Append a uint16_t netlink attribute to a message.
282  *
283  * @param[in, out] nh
284  *   The netlink message to parse, received from the kernel.
285  * @param[in] type
286  *   The type of attribute to append.
287  * @param[in] data
288  *   The data to append.
289  */
290 void
291 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
292 {
293         tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
294 }
295
296 /**
297  * Append a uint16_t netlink attribute to a message.
298  *
299  * @param[in, out] nh
300  *   The netlink message to parse, received from the kernel.
301  * @param[in] type
302  *   The type of attribute to append.
303  * @param[in] data
304  *   The data to append.
305  */
306 void
307 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
308 {
309         tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
310 }
311
312 /**
313  * Start a nested netlink attribute.
314  * It must be followed later by a call to tap_nlattr_nested_finish().
315  *
316  * @param[in, out] msg
317  *   The netlink message where to edit the nested_tails metadata.
318  * @param[in] type
319  *   The nested attribute type to append.
320  *
321  * @return
322  *   -1 if adding a nested netlink attribute failed, 0 otherwise.
323  */
324 int
325 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
326 {
327         struct nested_tail *tail;
328
329         tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
330         if (!tail) {
331                 RTE_LOG(ERR, PMD,
332                         "Couldn't allocate memory for nested netlink"
333                         " attribute\n");
334                 return -1;
335         }
336
337         tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
338
339         tap_nlattr_add(&msg->nh, type, 0, NULL);
340
341         tail->prev = msg->nested_tails;
342
343         msg->nested_tails = tail;
344
345         return 0;
346 }
347
348 /**
349  * End a nested netlink attribute.
350  * It follows a call to tap_nlattr_nested_start().
351  * In effect, it will modify the nested attribute length to include every bytes
352  * from the nested attribute start, up to here.
353  *
354  * @param[in, out] msg
355  *   The netlink message where to edit the nested_tails metadata.
356  */
357 void
358 tap_nlattr_nested_finish(struct nlmsg *msg)
359 {
360         struct nested_tail *tail = msg->nested_tails;
361
362         tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
363
364         if (tail->prev)
365                 msg->nested_tails = tail->prev;
366
367         rte_free(tail);
368 }