af_xdp: update af_xdp driver plugin to depend on libxdp
[vpp.git] / src / plugins / af_xdp / device.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <stdio.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 #include <linux/ethtool.h>
22 #include <linux/if_link.h>
23 #include <linux/sockios.h>
24 #include <linux/limits.h>
25 #include <bpf/libbpf.h>
26 #include <vlib/vlib.h>
27 #include <vlib/unix/unix.h>
28 #include <vlib/pci/pci.h>
29 #include <vppinfra/linux/netns.h>
30 #include <vppinfra/linux/sysfs.h>
31 #include <vppinfra/unix.h>
32 #include <vnet/ethernet/ethernet.h>
33 #include <vnet/interface/rx_queue_funcs.h>
34 #include <vnet/interface/tx_queue_funcs.h>
35 #include "af_xdp.h"
36
37 #ifndef XDP_UMEM_MIN_CHUNK_SIZE
38 #define XDP_UMEM_MIN_CHUNK_SIZE 2048
39 #endif
40
41 af_xdp_main_t af_xdp_main;
42
43 typedef struct
44 {
45   u32 prod;
46   u32 cons;
47 } gdb_af_xdp_pair_t;
48
49 gdb_af_xdp_pair_t
50 gdb_af_xdp_get_prod (const struct xsk_ring_prod *prod)
51 {
52   gdb_af_xdp_pair_t pair = { *prod->producer, *prod->consumer };
53   return pair;
54 }
55
56 gdb_af_xdp_pair_t
57 gdb_af_xdp_get_cons (const struct xsk_ring_cons * cons)
58 {
59   gdb_af_xdp_pair_t pair = { *cons->producer, *cons->consumer };
60   return pair;
61 }
62
63 static clib_error_t *
64 af_xdp_mac_change (vnet_hw_interface_t * hw, const u8 * old, const u8 * new)
65 {
66   af_xdp_main_t *am = &af_xdp_main;
67   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
68   errno_t err = memcpy_s (ad->hwaddr, sizeof (ad->hwaddr), new, 6);
69   if (err)
70     return clib_error_return_code (0, -err, CLIB_ERROR_ERRNO_VALID,
71                                    "mac change failed");
72   return 0;
73 }
74
75 static clib_error_t *
76 af_xdp_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hw,
77                            u32 frame_size)
78 {
79   af_xdp_main_t *am = &af_xdp_main;
80   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
81   af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set mtu not supported yet");
82   return vnet_error (VNET_ERR_UNSUPPORTED, 0);
83 }
84
85 static u32
86 af_xdp_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
87 {
88   af_xdp_main_t *am = &af_xdp_main;
89   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
90
91   switch (flags)
92     {
93     case 0:
94       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set unicast not supported yet");
95       return ~0;
96     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
97       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad,
98                   "set promiscuous not supported yet");
99       return ~0;
100     }
101
102   af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "unknown flag %x requested", flags);
103   return ~0;
104 }
105
106 int
107 af_xdp_enter_netns (char *netns, int *fds)
108 {
109   *fds = *(fds + 1) = -1;
110   if (netns != NULL)
111     {
112       *fds = clib_netns_open (NULL /* self */);
113       if ((*(fds + 1) = clib_netns_open ((u8 *) netns)) == -1)
114         return VNET_API_ERROR_SYSCALL_ERROR_8;
115       if (clib_setns (*(fds + 1)) == -1)
116         return VNET_API_ERROR_SYSCALL_ERROR_9;
117     }
118   return 0;
119 }
120
121 void
122 af_xdp_cleanup_netns (int *fds)
123 {
124   if (*fds != -1)
125     close (*fds);
126
127   if (*(fds + 1) != -1)
128     close (*(fds + 1));
129
130   *fds = *(fds + 1) = -1;
131 }
132
133 int
134 af_xdp_exit_netns (char *netns, int *fds)
135 {
136   int ret = 0;
137   if (netns != NULL)
138     {
139       if (*fds != -1)
140         ret = clib_setns (*fds);
141
142       af_xdp_cleanup_netns (fds);
143     }
144
145   return ret;
146 }
147
148 void
149 af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad)
150 {
151   vnet_main_t *vnm = vnet_get_main ();
152   af_xdp_main_t *axm = &af_xdp_main;
153   struct xsk_socket **xsk;
154   struct xsk_umem **umem;
155   int i;
156
157   if (ad->hw_if_index)
158     {
159       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
160       ethernet_delete_interface (vnm, ad->hw_if_index);
161     }
162
163   for (i = 0; i < ad->txq_num; i++)
164     clib_spinlock_free (&vec_elt (ad->txqs, i).lock);
165
166   vec_foreach (xsk, ad->xsk)
167     xsk_socket__delete (*xsk);
168
169   vec_foreach (umem, ad->umem)
170     xsk_umem__delete (*umem);
171
172   for (i = 0; i < ad->rxq_num; i++)
173     clib_file_del_by_index (&file_main, vec_elt (ad->rxqs, i).file_index);
174
175   if (ad->bpf_obj)
176     {
177       int ns_fds[2];
178       af_xdp_enter_netns (ad->netns, ns_fds);
179       bpf_xdp_detach (ad->linux_ifindex, XDP_FLAGS_UPDATE_IF_NOEXIST, NULL);
180       af_xdp_exit_netns (ad->netns, ns_fds);
181
182       bpf_object__close (ad->bpf_obj);
183     }
184
185   vec_free (ad->xsk);
186   vec_free (ad->umem);
187   vec_free (ad->buffer_template);
188   vec_free (ad->rxqs);
189   vec_free (ad->txqs);
190   vec_free (ad->name);
191   vec_free (ad->linux_ifname);
192   vec_free (ad->netns);
193   clib_error_free (ad->error);
194   pool_put (axm->devices, ad);
195 }
196
197 static int
198 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
199 {
200   int fd;
201   struct bpf_program *bpf_prog;
202   struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
203
204   if (setrlimit (RLIMIT_MEMLOCK, &r))
205     af_xdp_log (VLIB_LOG_LEVEL_WARNING, ad,
206                 "setrlimit(%s) failed: %s (errno %d)", ad->linux_ifname,
207                 strerror (errno), errno);
208
209   ad->linux_ifindex = if_nametoindex (ad->linux_ifname);
210   if (!ad->linux_ifindex)
211     {
212       args->rv = VNET_API_ERROR_INVALID_VALUE;
213       args->error =
214         clib_error_return_unix (0, "if_nametoindex(%s) failed",
215                                 ad->linux_ifname);
216       goto err0;
217     }
218
219   ad->bpf_obj = bpf_object__open_file (args->prog, NULL);
220   if (libbpf_get_error (ad->bpf_obj))
221     {
222       args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
223       args->error = clib_error_return_unix (
224         0, "bpf_object__open_file(%s) failed", args->prog);
225       goto err0;
226     }
227
228   bpf_prog = bpf_object__next_program (ad->bpf_obj, NULL);
229   if (!bpf_prog)
230     goto err1;
231
232   bpf_program__set_type (bpf_prog, BPF_PROG_TYPE_XDP);
233
234   if (bpf_object__load (ad->bpf_obj))
235     goto err1;
236
237   fd = bpf_program__fd (bpf_prog);
238
239   if (bpf_xdp_attach (ad->linux_ifindex, fd, XDP_FLAGS_UPDATE_IF_NOEXIST,
240                       NULL))
241     {
242       args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
243       args->error = clib_error_return_unix (0, "bpf_xdp_attach(%s) failed",
244                                             ad->linux_ifname);
245       goto err1;
246     }
247
248   return 0;
249
250 err1:
251   bpf_object__close (ad->bpf_obj);
252   ad->bpf_obj = 0;
253 err0:
254   ad->linux_ifindex = ~0;
255   return -1;
256 }
257
258 static int
259 af_xdp_create_queue (vlib_main_t *vm, af_xdp_create_if_args_t *args,
260                      af_xdp_device_t *ad, int qid)
261 {
262   struct xsk_umem **umem;
263   struct xsk_socket **xsk;
264   af_xdp_rxq_t *rxq;
265   af_xdp_txq_t *txq;
266   struct xsk_umem_config umem_config;
267   struct xsk_socket_config sock_config;
268   struct xdp_options opt;
269   socklen_t optlen;
270   const int is_rx = qid < ad->rxq_num;
271   const int is_tx = qid < ad->txq_num;
272
273   umem = vec_elt_at_index (ad->umem, qid);
274   xsk = vec_elt_at_index (ad->xsk, qid);
275   rxq = vec_elt_at_index (ad->rxqs, qid);
276   txq = vec_elt_at_index (ad->txqs, qid);
277
278   /*
279    * fq and cq must always be allocated even if unused
280    * whereas rx and tx indicates whether we want rxq, txq, or both
281    */
282   struct xsk_ring_cons *rx = is_rx ? &rxq->rx : 0;
283   struct xsk_ring_prod *fq = &rxq->fq;
284   struct xsk_ring_prod *tx = is_tx ? &txq->tx : 0;
285   struct xsk_ring_cons *cq = &txq->cq;
286   int fd;
287
288   memset (&umem_config, 0, sizeof (umem_config));
289   umem_config.fill_size = args->rxq_size;
290   umem_config.comp_size = args->txq_size;
291   umem_config.frame_size =
292     sizeof (vlib_buffer_t) + vlib_buffer_get_default_data_size (vm);
293   umem_config.frame_headroom = sizeof (vlib_buffer_t);
294   umem_config.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
295   if (xsk_umem__create
296       (umem, uword_to_pointer (vm->buffer_main->buffer_mem_start, void *),
297        vm->buffer_main->buffer_mem_size, fq, cq, &umem_config))
298     {
299       uword sys_page_size = clib_mem_get_page_size ();
300       args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
301       args->error = clib_error_return_unix (0, "xsk_umem__create() failed");
302       /* this should mimic the Linux kernel net/xdp/xdp_umem.c:xdp_umem_reg()
303        * check */
304       if (umem_config.frame_size < XDP_UMEM_MIN_CHUNK_SIZE ||
305           umem_config.frame_size > sys_page_size)
306         args->error = clib_error_return (
307           args->error,
308           "(unsupported data-size? (should be between %d and %d))",
309           XDP_UMEM_MIN_CHUNK_SIZE - sizeof (vlib_buffer_t),
310           sys_page_size - sizeof (vlib_buffer_t));
311       goto err0;
312     }
313
314   memset (&sock_config, 0, sizeof (sock_config));
315   sock_config.rx_size = args->rxq_size;
316   sock_config.tx_size = args->txq_size;
317   sock_config.bind_flags = XDP_USE_NEED_WAKEUP;
318   switch (args->mode)
319     {
320     case AF_XDP_MODE_AUTO:
321       break;
322     case AF_XDP_MODE_COPY:
323       sock_config.bind_flags |= XDP_COPY;
324       break;
325     case AF_XDP_MODE_ZERO_COPY:
326       sock_config.bind_flags |= XDP_ZEROCOPY;
327       break;
328     }
329   if (xsk_socket__create
330       (xsk, ad->linux_ifname, qid, *umem, rx, tx, &sock_config))
331     {
332       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
333       args->error =
334         clib_error_return_unix (0,
335                                 "xsk_socket__create() failed (is linux netdev %s up?)",
336                                 ad->linux_ifname);
337       goto err1;
338     }
339
340   fd = xsk_socket__fd (*xsk);
341   optlen = sizeof (opt);
342 #ifndef SOL_XDP
343 #define SOL_XDP 283
344 #endif
345   if (getsockopt (fd, SOL_XDP, XDP_OPTIONS, &opt, &optlen))
346     {
347       args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
348       args->error =
349         clib_error_return_unix (0, "getsockopt(XDP_OPTIONS) failed");
350       goto err2;
351     }
352   if (opt.flags & XDP_OPTIONS_ZEROCOPY)
353     ad->flags |= AF_XDP_DEVICE_F_ZEROCOPY;
354
355   rxq->xsk_fd = is_rx ? fd : -1;
356
357   if (is_tx)
358     {
359       txq->xsk_fd = fd;
360       clib_spinlock_init (&txq->lock);
361       if (is_rx && (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK))
362         {
363           /* This is a shared rx+tx queue and we need to lock before syscalls.
364            * Prior to Linux 5.6 there is a race condition preventing to call
365            * poll() and sendto() concurrently on AF_XDP sockets. This was
366            * fixed with commit 11cc2d21499cabe7e7964389634ed1de3ee91d33
367            * to workaround this issue, we protect the syscalls with a
368            * spinlock. Note that it also prevents to use interrupt mode in
369            * multi workers setup, because in this case the poll() is done in
370            * the framework w/o any possibility to protect it.
371            * See
372            * https://lore.kernel.org/bpf/BYAPR11MB365382C5DB1E5FCC53242609C1549@BYAPR11MB3653.namprd11.prod.outlook.com/
373            */
374           clib_spinlock_init (&rxq->syscall_lock);
375           txq->syscall_lock = rxq->syscall_lock;
376         }
377     }
378   else
379     {
380       txq->xsk_fd = -1;
381     }
382
383   return 0;
384
385 err2:
386   xsk_socket__delete (*xsk);
387 err1:
388   xsk_umem__delete (*umem);
389 err0:
390   *umem = 0;
391   *xsk = 0;
392   return -1;
393 }
394
395 static int
396 af_xdp_get_numa (const char *ifname)
397 {
398   char *path;
399   clib_error_t *err;
400   int numa;
401
402   path =
403     (char *) format (0, "/sys/class/net/%s/device/numa_node%c", ifname, 0);
404   err = clib_sysfs_read (path, "%d", &numa);
405   if (err || numa < 0)
406     numa = 0;
407
408   clib_error_free (err);
409   vec_free (path);
410   return numa;
411 }
412
413 static void
414 af_xdp_get_q_count (const char *ifname, int *rxq_num, int *txq_num)
415 {
416   struct ethtool_channels ec = { .cmd = ETHTOOL_GCHANNELS };
417   struct ifreq ifr = { .ifr_data = (void *) &ec };
418   int fd, err;
419
420   *rxq_num = *txq_num = 1;
421
422   fd = socket (AF_INET, SOCK_DGRAM, 0);
423   if (fd < 0)
424     return;
425
426   snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", ifname);
427   err = ioctl (fd, SIOCETHTOOL, &ifr);
428
429   close (fd);
430
431   if (err)
432     return;
433
434   *rxq_num = clib_max (ec.combined_count, ec.rx_count);
435   *txq_num = clib_max (ec.combined_count, ec.tx_count);
436 }
437
438 static clib_error_t *
439 af_xdp_device_rxq_read_ready (clib_file_t * f)
440 {
441   vnet_hw_if_rx_queue_set_int_pending (vnet_get_main (), f->private_data);
442   return 0;
443 }
444
445 static clib_error_t *
446 af_xdp_device_set_rxq_mode (const af_xdp_device_t *ad, af_xdp_rxq_t *rxq,
447                             const af_xdp_rxq_mode_t mode)
448 {
449   clib_file_main_t *fm = &file_main;
450   clib_file_update_type_t update;
451   clib_file_t *f;
452
453   if (rxq->mode == mode)
454     return 0;
455
456   switch (mode)
457     {
458     case AF_XDP_RXQ_MODE_POLLING:
459       update = UNIX_FILE_UPDATE_DELETE;
460       break;
461     case AF_XDP_RXQ_MODE_INTERRUPT:
462       if (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK)
463         return clib_error_create (
464           "kernel workaround incompatible with interrupt mode");
465       update = UNIX_FILE_UPDATE_ADD;
466       break;
467     default:
468       ASSERT (0);
469       return clib_error_create ("unknown rxq mode %i", mode);
470     }
471
472   f = clib_file_get (fm, rxq->file_index);
473   fm->file_update (f, update);
474   rxq->mode = mode;
475   return 0;
476 }
477
478 static u32
479 af_xdp_find_rxq_for_thread (vnet_main_t *vnm, const af_xdp_device_t *ad,
480                             const u32 thread)
481 {
482   u32 i;
483   for (i = 0; i < ad->rxq_num; i++)
484     {
485       const u32 qid = vec_elt (ad->rxqs, i).queue_index;
486       const u32 tid = vnet_hw_if_get_rx_queue (vnm, qid)->thread_index;
487       if (tid == thread)
488         return i;
489     }
490   return ~0;
491 }
492
493 static clib_error_t *
494 af_xdp_finalize_queues (vnet_main_t *vnm, af_xdp_device_t *ad,
495                         const int n_vlib_mains)
496 {
497   clib_error_t *err = 0;
498   int i;
499
500   for (i = 0; i < ad->rxq_num; i++)
501     {
502       af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
503       rxq->queue_index = vnet_hw_if_register_rx_queue (
504         vnm, ad->hw_if_index, i, VNET_HW_IF_RXQ_THREAD_ANY);
505       u8 *desc = format (0, "%U rxq %d", format_af_xdp_device_name,
506                          ad->dev_instance, i);
507       clib_file_t f = {
508         .file_descriptor = rxq->xsk_fd,
509         .private_data = rxq->queue_index,
510         .read_function = af_xdp_device_rxq_read_ready,
511         .description = desc,
512       };
513       rxq->file_index = clib_file_add (&file_main, &f);
514       vnet_hw_if_set_rx_queue_file_index (vnm, rxq->queue_index,
515                                           rxq->file_index);
516       err = af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING);
517       if (err)
518         return err;
519     }
520
521   for (i = 0; i < ad->txq_num; i++)
522     vec_elt (ad->txqs, i).queue_index =
523       vnet_hw_if_register_tx_queue (vnm, ad->hw_if_index, i);
524
525   /* We set the rxq and txq of the same queue pair on the same thread
526    * by default to avoid locking because of the syscall lock. */
527   int last_qid = clib_min (ad->rxq_num, ad->txq_num - 1);
528   for (i = 0; i < n_vlib_mains; i++)
529     {
530       /* search for the 1st rxq assigned on this thread, if any */
531       u32 qid = af_xdp_find_rxq_for_thread (vnm, ad, i);
532       /* if this rxq is combined with a txq, use it. Otherwise, we'll
533        * assign txq in a round-robin fashion. We start from the 1st txq
534        * not shared with a rxq if possible... */
535       qid = qid < ad->txq_num ? qid : (last_qid++ % ad->txq_num);
536       vnet_hw_if_tx_queue_assign_thread (
537         vnm, vec_elt (ad->txqs, qid).queue_index, i);
538     }
539
540   vnet_hw_if_update_runtime_data (vnm, ad->hw_if_index);
541   return 0;
542 }
543
544 void
545 af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args)
546 {
547   vnet_main_t *vnm = vnet_get_main ();
548   vlib_thread_main_t *tm = vlib_get_thread_main ();
549   vnet_eth_interface_registration_t eir = {};
550   af_xdp_main_t *am = &af_xdp_main;
551   af_xdp_device_t *ad;
552   vnet_sw_interface_t *sw;
553   int rxq_num, txq_num, q_num;
554   int ns_fds[2];
555   int i, ret;
556
557   args->rxq_size = args->rxq_size ? args->rxq_size : 2 * VLIB_FRAME_SIZE;
558   args->txq_size = args->txq_size ? args->txq_size : 2 * VLIB_FRAME_SIZE;
559   args->rxq_num = args->rxq_num ? args->rxq_num : 1;
560
561   if (!args->linux_ifname)
562     {
563       args->rv = VNET_API_ERROR_INVALID_VALUE;
564       args->error = clib_error_return (0, "missing host interface");
565       goto err0;
566     }
567
568   if (args->rxq_size < VLIB_FRAME_SIZE || args->txq_size < VLIB_FRAME_SIZE ||
569       args->rxq_size > 65535 || args->txq_size > 65535 ||
570       !is_pow2 (args->rxq_size) || !is_pow2 (args->txq_size))
571     {
572       args->rv = VNET_API_ERROR_INVALID_VALUE;
573       args->error =
574         clib_error_return (0,
575                            "queue size must be a power of two between %i and 65535",
576                            VLIB_FRAME_SIZE);
577       goto err0;
578     }
579
580   ret = af_xdp_enter_netns (args->netns, ns_fds);
581   if (ret)
582     {
583       args->rv = ret;
584       args->error = clib_error_return (0, "enter netns %s failed, ret %d",
585                                        args->netns, args->rv);
586       goto err0;
587     }
588
589   af_xdp_get_q_count (args->linux_ifname, &rxq_num, &txq_num);
590   if (args->rxq_num > rxq_num && AF_XDP_NUM_RX_QUEUES_ALL != args->rxq_num)
591     {
592       args->rv = VNET_API_ERROR_INVALID_VALUE;
593       args->error = clib_error_create ("too many rxq requested (%d > %d)",
594                                        args->rxq_num, rxq_num);
595       goto err1;
596     }
597   rxq_num = clib_min (rxq_num, args->rxq_num);
598   txq_num = clib_min (txq_num, tm->n_vlib_mains);
599
600   pool_get_zero (am->devices, ad);
601
602   if (tm->n_vlib_mains > 1 &&
603       0 == (args->flags & AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK))
604     ad->flags |= AF_XDP_DEVICE_F_SYSCALL_LOCK;
605
606   ad->linux_ifname = (char *) format (0, "%s", args->linux_ifname);
607   vec_validate (ad->linux_ifname, IFNAMSIZ - 1);        /* libbpf expects ifname to be at least IFNAMSIZ */
608
609   ad->netns = (char *) format (0, "%s", args->netns);
610
611   if (args->prog && af_xdp_load_program (args, ad))
612     goto err2;
613
614   q_num = clib_max (rxq_num, txq_num);
615   ad->rxq_num = rxq_num;
616   ad->txq_num = txq_num;
617
618   vec_validate_aligned (ad->umem, q_num - 1, CLIB_CACHE_LINE_BYTES);
619   vec_validate_aligned (ad->xsk, q_num - 1, CLIB_CACHE_LINE_BYTES);
620   vec_validate_aligned (ad->rxqs, q_num - 1, CLIB_CACHE_LINE_BYTES);
621   vec_validate_aligned (ad->txqs, q_num - 1, CLIB_CACHE_LINE_BYTES);
622
623   for (i = 0; i < q_num; i++)
624     {
625       if (af_xdp_create_queue (vm, args, ad, i))
626         {
627           /*
628            * queue creation failed
629            * it is only a fatal error if we could not create the number of rx
630            * queues requested explicitely by the user and the user did not
631            * requested 'max'
632            * we might create less tx queues than workers but this is ok
633            */
634           af_xdp_log (VLIB_LOG_LEVEL_DEBUG, ad,
635                       "create interface failed to create queue qid=%d", i);
636
637           /* fixup vectors length */
638           vec_set_len (ad->umem, i);
639           vec_set_len (ad->xsk, i);
640           vec_set_len (ad->rxqs, i);
641           vec_set_len (ad->txqs, i);
642
643           ad->rxq_num = clib_min (i, rxq_num);
644           ad->txq_num = clib_min (i, txq_num);
645
646           if (i == 0 ||
647               (i < rxq_num && AF_XDP_NUM_RX_QUEUES_ALL != args->rxq_num))
648             {
649               ad->rxq_num = ad->txq_num = 0;
650               goto err2; /* failed creating requested rxq: fatal error, bailing
651                             out */
652             }
653
654
655           args->rv = 0;
656           clib_error_free (args->error);
657           break;
658         }
659     }
660
661   if (af_xdp_exit_netns (args->netns, ns_fds))
662     {
663       args->rv = VNET_API_ERROR_SYSCALL_ERROR_10;
664       args->error = clib_error_return (0, "exit netns failed");
665       goto err2;
666     }
667
668   ad->dev_instance = ad - am->devices;
669   ad->per_interface_next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
670   ad->pool =
671     vlib_buffer_pool_get_default_for_numa (vm,
672                                            af_xdp_get_numa
673                                            (ad->linux_ifname));
674   if (!args->name)
675     {
676       char *ifname = ad->linux_ifname;
677       if (args->netns != NULL && strncmp (args->netns, "pid:", 4) == 0)
678         {
679           ad->name =
680             (char *) format (0, "%s/%u", ifname, atoi (args->netns + 4));
681         }
682       else
683         ad->name = (char *) format (0, "%s/%d", ifname, ad->dev_instance);
684     }
685   else
686     ad->name = (char *) format (0, "%s", args->name);
687
688   ethernet_mac_address_generate (ad->hwaddr);
689
690   /* create interface */
691   eir.dev_class_index = af_xdp_device_class.index;
692   eir.dev_instance = ad->dev_instance;
693   eir.address = ad->hwaddr;
694   eir.cb.flag_change = af_xdp_flag_change;
695   eir.cb.set_max_frame_size = af_xdp_set_max_frame_size;
696   ad->hw_if_index = vnet_eth_register_interface (vnm, &eir);
697
698   sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
699   args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
700
701   vnet_hw_if_set_caps (vnm, ad->hw_if_index, VNET_HW_IF_CAP_INT_MODE);
702
703   vnet_hw_if_set_input_node (vnm, ad->hw_if_index, af_xdp_input_node.index);
704
705   args->error = af_xdp_finalize_queues (vnm, ad, tm->n_vlib_mains);
706   if (args->error)
707     {
708       args->rv = VNET_API_ERROR_SYSCALL_ERROR_7;
709       goto err2;
710     }
711
712   /* buffer template */
713   vec_validate_aligned (ad->buffer_template, 1, CLIB_CACHE_LINE_BYTES);
714   ad->buffer_template->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
715   ad->buffer_template->ref_count = 1;
716   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_RX] = ad->sw_if_index;
717   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
718   ad->buffer_template->buffer_pool_index = ad->pool;
719
720   return;
721
722 err2:
723   af_xdp_delete_if (vm, ad);
724 err1:
725   af_xdp_cleanup_netns (ns_fds);
726 err0:
727   vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
728 }
729
730 static clib_error_t *
731 af_xdp_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
732 {
733   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
734   af_xdp_main_t *am = &af_xdp_main;
735   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
736   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
737
738   if (ad->flags & AF_XDP_DEVICE_F_ERROR)
739     return clib_error_return (0, "device is in error state");
740
741   if (is_up)
742     {
743       vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
744                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
745       ad->flags |= AF_XDP_DEVICE_F_ADMIN_UP;
746       af_xdp_device_input_refill (ad);
747     }
748   else
749     {
750       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
751       ad->flags &= ~AF_XDP_DEVICE_F_ADMIN_UP;
752     }
753   return 0;
754 }
755
756 static clib_error_t *
757 af_xdp_interface_rx_mode_change (vnet_main_t *vnm, u32 hw_if_index, u32 qid,
758                                  vnet_hw_if_rx_mode mode)
759 {
760   af_xdp_main_t *am = &af_xdp_main;
761   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
762   af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
763   af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
764
765   switch (mode)
766     {
767     default:                         /* fallthrough */
768     case VNET_HW_IF_RX_MODE_UNKNOWN: /* fallthrough */
769     case VNET_HW_IF_NUM_RX_MODES:
770       return clib_error_create ("uknown rx mode - doing nothing");
771     case VNET_HW_IF_RX_MODE_DEFAULT: /* fallthrough */
772     case VNET_HW_IF_RX_MODE_POLLING:
773       return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING);
774     case VNET_HW_IF_RX_MODE_INTERRUPT: /* fallthrough */
775     case VNET_HW_IF_RX_MODE_ADAPTIVE:
776       return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_INTERRUPT);
777     }
778
779   ASSERT (0 && "unreachable");
780   return clib_error_create ("unreachable");
781 }
782
783 static void
784 af_xdp_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
785                                 u32 node_index)
786 {
787   af_xdp_main_t *am = &af_xdp_main;
788   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
789   af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
790
791   /* Shut off redirection */
792   if (node_index == ~0)
793     {
794       ad->per_interface_next_index = node_index;
795       return;
796     }
797
798   ad->per_interface_next_index =
799     vlib_node_add_next (vlib_get_main (), af_xdp_input_node.index,
800                         node_index);
801 }
802
803 static char *af_xdp_tx_func_error_strings[] = {
804 #define _(n,s) s,
805   foreach_af_xdp_tx_func_error
806 #undef _
807 };
808
809 static void
810 af_xdp_clear (u32 dev_instance)
811 {
812   af_xdp_main_t *am = &af_xdp_main;
813   af_xdp_device_t *ad = pool_elt_at_index (am->devices, dev_instance);
814   clib_error_free (ad->error);
815 }
816
817 /* *INDENT-OFF* */
818 VNET_DEVICE_CLASS (af_xdp_device_class) = {
819   .name = "AF_XDP interface",
820   .format_device = format_af_xdp_device,
821   .format_device_name = format_af_xdp_device_name,
822   .admin_up_down_function = af_xdp_interface_admin_up_down,
823   .rx_mode_change_function = af_xdp_interface_rx_mode_change,
824   .rx_redirect_to_node = af_xdp_set_interface_next_node,
825   .tx_function_n_errors = AF_XDP_TX_N_ERROR,
826   .tx_function_error_strings = af_xdp_tx_func_error_strings,
827   .mac_addr_change_function = af_xdp_mac_change,
828   .clear_counters = af_xdp_clear,
829 };
830 /* *INDENT-ON* */
831
832 clib_error_t *
833 af_xdp_init (vlib_main_t * vm)
834 {
835   af_xdp_main_t *am = &af_xdp_main;
836
837   am->log_class = vlib_log_register_class ("af_xdp", 0);
838
839   return 0;
840 }
841
842 VLIB_INIT_FUNCTION (af_xdp_init);
843
844 /*
845  * fd.io coding-style-patch-verification: ON
846  *
847  * Local Variables:
848  * eval: (c-set-style "gnu")
849  * End:
850  */