net/tap: add link status notification
[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 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 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 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 to an nl_send().
157  *
158  * @param[in] nlsk_fd
159  *   The netlink socket file descriptor used for communication.
160  *
161  * @return
162  *   0 on success, -1 otherwise.
163  */
164 int
165 nl_recv_ack(int nlsk_fd)
166 {
167         return nl_recv(nlsk_fd, NULL, NULL);
168 }
169
170 /**
171  * Receive a message from the kernel on the netlink socket, following an
172  * nl_send().
173  *
174  * @param[in] nlsk_fd
175  *   The netlink socket file descriptor used for communication.
176  * @param[in] cb
177  *   The callback function to call for each netlink message received.
178  * @param[in, out] arg
179  *   Custom arguments for the callback.
180  *
181  * @return
182  *   0 on success, -1 otherwise.
183  */
184 int
185 nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
186 {
187         /* man 7 netlink EXAMPLE */
188         struct sockaddr_nl sa;
189         struct nlmsghdr *nh;
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                 .msg_iovlen = 1,
200         };
201         int recv_bytes = 0, done = 0, multipart = 0, error = 0;
202
203 read:
204         recv_bytes = recvmsg(nlsk_fd, &msg, 0);
205         if (recv_bytes < 0)
206                 return -1;
207         for (nh = (struct nlmsghdr *)buf;
208              NLMSG_OK(nh, (unsigned int)recv_bytes);
209              nh = NLMSG_NEXT(nh, recv_bytes)) {
210                 /*
211                  * Multi-part messages and their following DONE message have the
212                  * NLM_F_MULTI flag set. Make note, in order to read the DONE
213                  * message afterwards.
214                  */
215                 if (nh->nlmsg_flags & NLM_F_MULTI)
216                         multipart = 1;
217                 if (nh->nlmsg_type == NLMSG_ERROR) {
218                         struct nlmsgerr *err_data = NLMSG_DATA(nh);
219
220                         if (err_data->error == 0)
221                                 RTE_LOG(DEBUG, PMD, "%s() ack message recvd\n",
222                                         __func__);
223                         else {
224                                 RTE_LOG(DEBUG, PMD,
225                                         "%s() error message recvd\n", __func__);
226                                 error = 1;
227                         }
228                 }
229                 /* The end of multipart message. */
230                 if (nh->nlmsg_type == NLMSG_DONE)
231                         /* No need to call the callback for a DONE message. */
232                         done = 1;
233                 else if (cb)
234                         if (cb(nh, arg) < 0)
235                                 error = 1;
236         }
237         if (multipart && !done)
238                 goto read;
239         if (error)
240                 return -1;
241         return 0;
242 }
243
244 /**
245  * Append a netlink attribute to a message.
246  *
247  * @param[in, out] nh
248  *   The netlink message to parse, received from the kernel.
249  * @param[in] type
250  *   The type of attribute to append.
251  * @param[in] data_len
252  *   The length of the data to append.
253  * @param[in] data
254  *   The data to append.
255  */
256 void
257 nlattr_add(struct nlmsghdr *nh, unsigned short type,
258            unsigned int data_len, const void *data)
259 {
260         /* see man 3 rtnetlink */
261         struct rtattr *rta;
262
263         rta = (struct rtattr *)NLMSG_TAIL(nh);
264         rta->rta_len = RTA_LENGTH(data_len);
265         rta->rta_type = type;
266         memcpy(RTA_DATA(rta), data, data_len);
267         nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
268 }
269
270 /**
271  * Append a uint8_t netlink attribute to a message.
272  *
273  * @param[in, out] nh
274  *   The netlink message to parse, received from the kernel.
275  * @param[in] type
276  *   The type of attribute to append.
277  * @param[in] data
278  *   The data to append.
279  */
280 void
281 nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
282 {
283         nlattr_add(nh, type, sizeof(uint8_t), &data);
284 }
285
286 /**
287  * Append a uint16_t netlink attribute to a message.
288  *
289  * @param[in, out] nh
290  *   The netlink message to parse, received from the kernel.
291  * @param[in] type
292  *   The type of attribute to append.
293  * @param[in] data
294  *   The data to append.
295  */
296 void
297 nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
298 {
299         nlattr_add(nh, type, sizeof(uint16_t), &data);
300 }
301
302 /**
303  * Append a uint16_t netlink attribute to a message.
304  *
305  * @param[in, out] nh
306  *   The netlink message to parse, received from the kernel.
307  * @param[in] type
308  *   The type of attribute to append.
309  * @param[in] data
310  *   The data to append.
311  */
312 void
313 nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
314 {
315         nlattr_add(nh, type, sizeof(uint32_t), &data);
316 }
317
318 /**
319  * Start a nested netlink attribute.
320  * It must be followed later by a call to nlattr_nested_finish().
321  *
322  * @param[in, out] msg
323  *   The netlink message where to edit the nested_tails metadata.
324  * @param[in] type
325  *   The nested attribute type to append.
326  *
327  * @return
328  *   -1 if adding a nested netlink attribute failed, 0 otherwise.
329  */
330 int
331 nlattr_nested_start(struct nlmsg *msg, uint16_t type)
332 {
333         struct nested_tail *tail;
334
335         tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
336         if (!tail) {
337                 RTE_LOG(ERR, PMD,
338                         "Couldn't allocate memory for nested netlink"
339                         " attribute\n");
340                 return -1;
341         }
342
343         tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
344
345         nlattr_add(&msg->nh, type, 0, NULL);
346
347         tail->prev = msg->nested_tails;
348
349         msg->nested_tails = tail;
350
351         return 0;
352 }
353
354 /**
355  * End a nested netlink attribute.
356  * It follows a call to nlattr_nested_start().
357  * In effect, it will modify the nested attribute length to include every bytes
358  * from the nested attribute start, up to here.
359  *
360  * @param[in, out] msg
361  *   The netlink message where to edit the nested_tails metadata.
362  */
363 void
364 nlattr_nested_finish(struct nlmsg *msg)
365 {
366         struct nested_tail *tail = msg->nested_tails;
367
368         tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
369
370         if (tail->prev)
371                 msg->nested_tails = tail->prev;
372
373         rte_free(tail);
374 }