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