net/tap: add netlink back-end for flow API
[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  * @return
59  *   netlink socket file descriptor on success, -1 otherwise.
60  */
61 int
62 nl_init(void)
63 {
64         int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
65         struct sockaddr_nl local = { .nl_family = AF_NETLINK };
66
67         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
68         if (fd < 0) {
69                 RTE_LOG(ERR, PMD, "Unable to create a netlink socket\n");
70                 return -1;
71         }
72         if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
73                 RTE_LOG(ERR, PMD, "Unable to set socket buffer send size\n");
74                 return -1;
75         }
76         if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
77                 RTE_LOG(ERR, PMD, "Unable to set socket buffer receive size\n");
78                 return -1;
79         }
80         if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
81                 RTE_LOG(ERR, PMD, "Unable to bind to the netlink socket\n");
82                 return -1;
83         }
84         return fd;
85 }
86
87 /**
88  * Clean up a netlink socket once all communicating with the kernel is finished.
89  *
90  * @param[in] nlsk_fd
91  *   The netlink socket file descriptor used for communication.
92  *
93  * @return
94  *   0 on success, -1 otherwise.
95  */
96 int
97 nl_final(int nlsk_fd)
98 {
99         if (close(nlsk_fd)) {
100                 RTE_LOG(ERR, PMD, "Failed to close netlink socket: %s (%d)\n",
101                         strerror(errno), errno);
102                 return -1;
103         }
104         return 0;
105 }
106
107 /**
108  * Send a message to the kernel on the netlink socket.
109  *
110  * @param[in] nlsk_fd
111  *   The netlink socket file descriptor used for communication.
112  * @param[in] nh
113  *   The netlink message send to the kernel.
114  *
115  * @return
116  *   the number of sent bytes on success, -1 otherwise.
117  */
118 int
119 nl_send(int nlsk_fd, struct nlmsghdr *nh)
120 {
121         /* man 7 netlink EXAMPLE */
122         struct sockaddr_nl sa = {
123                 .nl_family = AF_NETLINK,
124         };
125         struct iovec iov = {
126                 .iov_base = nh,
127                 .iov_len = nh->nlmsg_len,
128         };
129         struct msghdr msg = {
130                 .msg_name = &sa,
131                 .msg_namelen = sizeof(sa),
132                 .msg_iov = &iov,
133                 .msg_iovlen = 1,
134         };
135         int send_bytes;
136
137         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
138         nh->nlmsg_seq = (uint32_t)rte_rand();
139         send_bytes = sendmsg(nlsk_fd, &msg, 0);
140         if (send_bytes < 0) {
141                 RTE_LOG(ERR, PMD, "Failed to send netlink message: %s (%d)\n",
142                         strerror(errno), errno);
143                 return -1;
144         }
145         return send_bytes;
146 }
147
148 /**
149  * Check that the kernel sends an appropriate ACK in response to an nl_send().
150  *
151  * @param[in] nlsk_fd
152  *   The netlink socket file descriptor used for communication.
153  *
154  * @return
155  *   0 on success, -1 otherwise.
156  */
157 int
158 nl_recv_ack(int nlsk_fd)
159 {
160         return nl_recv(nlsk_fd, NULL, NULL);
161 }
162
163 /**
164  * Receive a message from the kernel on the netlink socket, following an
165  * nl_send().
166  *
167  * @param[in] nlsk_fd
168  *   The netlink socket file descriptor used for communication.
169  * @param[in] cb
170  *   The callback function to call for each netlink message received.
171  * @param[in, out] arg
172  *   Custom arguments for the callback.
173  *
174  * @return
175  *   0 on success, -1 otherwise.
176  */
177 int
178 nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
179 {
180         /* man 7 netlink EXAMPLE */
181         struct sockaddr_nl sa;
182         struct nlmsghdr *nh;
183         char buf[BUF_SIZE];
184         struct iovec iov = {
185                 .iov_base = buf,
186                 .iov_len = sizeof(buf),
187         };
188         struct msghdr msg = {
189                 .msg_name = &sa,
190                 .msg_namelen = sizeof(sa),
191                 .msg_iov = &iov,
192                 .msg_iovlen = 1,
193         };
194         int recv_bytes = 0, done = 0, multipart = 0, error = 0;
195
196 read:
197         recv_bytes = recvmsg(nlsk_fd, &msg, 0);
198         if (recv_bytes < 0)
199                 return -1;
200         for (nh = (struct nlmsghdr *)buf;
201              NLMSG_OK(nh, (unsigned int)recv_bytes);
202              nh = NLMSG_NEXT(nh, recv_bytes)) {
203                 /*
204                  * Multi-part messages and their following DONE message have the
205                  * NLM_F_MULTI flag set. Make note, in order to read the DONE
206                  * message afterwards.
207                  */
208                 if (nh->nlmsg_flags & NLM_F_MULTI)
209                         multipart = 1;
210                 if (nh->nlmsg_type == NLMSG_ERROR) {
211                         struct nlmsgerr *err_data = NLMSG_DATA(nh);
212
213                         if (err_data->error == 0)
214                                 RTE_LOG(DEBUG, PMD, "%s() ack message recvd\n",
215                                         __func__);
216                         else {
217                                 RTE_LOG(DEBUG, PMD,
218                                         "%s() error message recvd\n", __func__);
219                                 error = 1;
220                         }
221                 }
222                 /* The end of multipart message. */
223                 if (nh->nlmsg_type == NLMSG_DONE)
224                         /* No need to call the callback for a DONE message. */
225                         done = 1;
226                 else if (cb)
227                         if (cb(nh, arg) < 0)
228                                 error = 1;
229         }
230         if (multipart && !done)
231                 goto read;
232         if (error)
233                 return -1;
234         return 0;
235 }
236
237 /**
238  * Append a netlink attribute to a message.
239  *
240  * @param[in, out] nh
241  *   The netlink message to parse, received from the kernel.
242  * @param[in] type
243  *   The type of attribute to append.
244  * @param[in] data_len
245  *   The length of the data to append.
246  * @param[in] data
247  *   The data to append.
248  */
249 void
250 nlattr_add(struct nlmsghdr *nh, unsigned short type,
251            unsigned int data_len, const void *data)
252 {
253         /* see man 3 rtnetlink */
254         struct rtattr *rta;
255
256         rta = (struct rtattr *)NLMSG_TAIL(nh);
257         rta->rta_len = RTA_LENGTH(data_len);
258         rta->rta_type = type;
259         memcpy(RTA_DATA(rta), data, data_len);
260         nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
261 }
262
263 /**
264  * Append a uint8_t netlink attribute to a message.
265  *
266  * @param[in, out] nh
267  *   The netlink message to parse, received from the kernel.
268  * @param[in] type
269  *   The type of attribute to append.
270  * @param[in] data
271  *   The data to append.
272  */
273 void
274 nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
275 {
276         nlattr_add(nh, type, sizeof(uint8_t), &data);
277 }
278
279 /**
280  * Append a uint16_t netlink attribute to a message.
281  *
282  * @param[in, out] nh
283  *   The netlink message to parse, received from the kernel.
284  * @param[in] type
285  *   The type of attribute to append.
286  * @param[in] data
287  *   The data to append.
288  */
289 void
290 nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
291 {
292         nlattr_add(nh, type, sizeof(uint16_t), &data);
293 }
294
295 /**
296  * Append a uint16_t netlink attribute to a message.
297  *
298  * @param[in, out] nh
299  *   The netlink message to parse, received from the kernel.
300  * @param[in] type
301  *   The type of attribute to append.
302  * @param[in] data
303  *   The data to append.
304  */
305 void
306 nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
307 {
308         nlattr_add(nh, type, sizeof(uint32_t), &data);
309 }
310
311 /**
312  * Start a nested netlink attribute.
313  * It must be followed later by a call to nlattr_nested_finish().
314  *
315  * @param[in, out] msg
316  *   The netlink message where to edit the nested_tails metadata.
317  * @param[in] type
318  *   The nested attribute type to append.
319  *
320  * @return
321  *   -1 if adding a nested netlink attribute failed, 0 otherwise.
322  */
323 int
324 nlattr_nested_start(struct nlmsg *msg, uint16_t type)
325 {
326         struct nested_tail *tail;
327
328         tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
329         if (!tail) {
330                 RTE_LOG(ERR, PMD,
331                         "Couldn't allocate memory for nested netlink"
332                         " attribute\n");
333                 return -1;
334         }
335
336         tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
337
338         nlattr_add(&msg->nh, type, 0, NULL);
339
340         tail->prev = msg->nested_tails;
341
342         msg->nested_tails = tail;
343
344         return 0;
345 }
346
347 /**
348  * End a nested netlink attribute.
349  * It follows a call to nlattr_nested_start().
350  * In effect, it will modify the nested attribute length to include every bytes
351  * from the nested attribute start, up to here.
352  *
353  * @param[in, out] msg
354  *   The netlink message where to edit the nested_tails metadata.
355  */
356 void
357 nlattr_nested_finish(struct nlmsg *msg)
358 {
359         struct nested_tail *tail = msg->nested_tails;
360
361         tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
362
363         if (tail->prev)
364                 msg->nested_tails = tail->prev;
365
366         rte_free(tail);
367 }