af_xdp: workaround kernel race between poll() and sendmsg()
[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 <linux/if_link.h>
21 #include <bpf/libbpf.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/pci/pci.h>
25 #include <vppinfra/linux/sysfs.h>
26 #include <vppinfra/unix.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/interface/rx_queue_funcs.h>
29 #include "af_xdp.h"
30
31 af_xdp_main_t af_xdp_main;
32
33 typedef struct
34 {
35   u32 prod;
36   u32 cons;
37 } gdb_af_xdp_pair_t;
38
39 gdb_af_xdp_pair_t
40 gdb_af_xdp_get_prod (const struct xsk_ring_prod *prod)
41 {
42   gdb_af_xdp_pair_t pair = { *prod->producer, *prod->consumer };
43   return pair;
44 }
45
46 gdb_af_xdp_pair_t
47 gdb_af_xdp_get_cons (const struct xsk_ring_cons * cons)
48 {
49   gdb_af_xdp_pair_t pair = { *cons->producer, *cons->consumer };
50   return pair;
51 }
52
53 static clib_error_t *
54 af_xdp_mac_change (vnet_hw_interface_t * hw, const u8 * old, const u8 * new)
55 {
56   af_xdp_main_t *am = &af_xdp_main;
57   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
58   errno_t err = memcpy_s (ad->hwaddr, sizeof (ad->hwaddr), new, 6);
59   if (err)
60     return clib_error_return_code (0, -err, CLIB_ERROR_ERRNO_VALID,
61                                    "mac change failed");
62   return 0;
63 }
64
65 static u32
66 af_xdp_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
67 {
68   af_xdp_main_t *am = &af_xdp_main;
69   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
70
71   switch (flags)
72     {
73     case 0:
74       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set unicast not supported yet");
75       return ~0;
76     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
77       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad,
78                   "set promiscuous not supported yet");
79       return ~0;
80     case ETHERNET_INTERFACE_FLAG_MTU:
81       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set mtu not supported yet");
82       return ~0;
83     }
84
85   af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "unknown flag %x requested", flags);
86   return ~0;
87 }
88
89 void
90 af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad)
91 {
92   vnet_main_t *vnm = vnet_get_main ();
93   af_xdp_main_t *axm = &af_xdp_main;
94   struct xsk_socket **xsk;
95   struct xsk_umem **umem;
96   int i;
97
98   if (ad->hw_if_index)
99     {
100       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
101       ethernet_delete_interface (vnm, ad->hw_if_index);
102     }
103
104   for (i = 0; i < ad->rxq_num; i++)
105     clib_file_del_by_index (&file_main, vec_elt (ad->rxqs, i).file_index);
106
107   for (i = 0; i < ad->txq_num; i++)
108     clib_spinlock_free (&vec_elt (ad->txqs, i).lock);
109
110   vec_foreach (xsk, ad->xsk)
111     xsk_socket__delete (*xsk);
112
113   vec_foreach (umem, ad->umem)
114     xsk_umem__delete (*umem);
115
116   if (ad->bpf_obj)
117     {
118       bpf_set_link_xdp_fd (ad->linux_ifindex, -1, 0);
119       bpf_object__unload (ad->bpf_obj);
120     }
121
122   vec_free (ad->xsk);
123   vec_free (ad->umem);
124   vec_free (ad->buffer_template);
125   vec_free (ad->rxqs);
126   vec_free (ad->txqs);
127   clib_error_free (ad->error);
128   pool_put (axm->devices, ad);
129 }
130
131 static int
132 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
133 {
134   int fd;
135
136   ad->linux_ifindex = if_nametoindex (ad->linux_ifname);
137   if (!ad->linux_ifindex)
138     {
139       args->rv = VNET_API_ERROR_INVALID_VALUE;
140       args->error =
141         clib_error_return_unix (0, "if_nametoindex(%s) failed",
142                                 ad->linux_ifname);
143       goto err0;
144     }
145
146   if (bpf_prog_load (args->prog, BPF_PROG_TYPE_XDP, &ad->bpf_obj, &fd))
147     {
148       args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
149       args->error =
150         clib_error_return_unix (0, "bpf_prog_load(%s) failed", args->prog);
151       goto err0;
152     }
153
154 #ifndef XDP_FLAGS_REPLACE
155 #define XDP_FLAGS_REPLACE 0
156 #endif
157   if (bpf_set_link_xdp_fd (ad->linux_ifindex, fd, XDP_FLAGS_REPLACE))
158     {
159       args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
160       args->error =
161         clib_error_return_unix (0, "bpf_set_link_xdp_fd(%s) failed",
162                                 ad->linux_ifname);
163       goto err1;
164     }
165
166   return 0;
167
168 err1:
169   bpf_object__unload (ad->bpf_obj);
170   ad->bpf_obj = 0;
171 err0:
172   ad->linux_ifindex = ~0;
173   return -1;
174 }
175
176 static int
177 af_xdp_create_queue (vlib_main_t *vm, af_xdp_create_if_args_t *args,
178                      af_xdp_device_t *ad, int qid)
179 {
180   struct xsk_umem **umem;
181   struct xsk_socket **xsk;
182   af_xdp_rxq_t *rxq;
183   af_xdp_txq_t *txq;
184   struct xsk_umem_config umem_config;
185   struct xsk_socket_config sock_config;
186   struct xdp_options opt;
187   socklen_t optlen;
188   const int is_rx = qid < ad->rxq_num;
189   const int is_tx = qid < ad->txq_num;
190
191   vec_validate_aligned (ad->umem, qid, CLIB_CACHE_LINE_BYTES);
192   umem = vec_elt_at_index (ad->umem, qid);
193
194   vec_validate_aligned (ad->xsk, qid, CLIB_CACHE_LINE_BYTES);
195   xsk = vec_elt_at_index (ad->xsk, qid);
196
197   vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
198   rxq = vec_elt_at_index (ad->rxqs, qid);
199
200   vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
201   txq = vec_elt_at_index (ad->txqs, qid);
202
203   /*
204    * fq and cq must always be allocated even if unused
205    * whereas rx and tx indicates whether we want rxq, txq, or both
206    */
207   struct xsk_ring_cons *rx = is_rx ? &rxq->rx : 0;
208   struct xsk_ring_prod *fq = &rxq->fq;
209   struct xsk_ring_prod *tx = is_tx ? &txq->tx : 0;
210   struct xsk_ring_cons *cq = &txq->cq;
211   int fd;
212
213   memset (&umem_config, 0, sizeof (umem_config));
214   umem_config.fill_size = args->rxq_size;
215   umem_config.comp_size = args->txq_size;
216   umem_config.frame_size =
217     sizeof (vlib_buffer_t) + vlib_buffer_get_default_data_size (vm);
218   /*
219    * Note about headroom: for some reasons, there seem to be a discrepency
220    * between 0-copy and copy mode:
221    *   - 0-copy: XDP_PACKET_HEADROOM will be added to the user headroom
222    *   - copy: nothing is added to the user headroom
223    * We privileged 0-copy and set headroom so that frame_headroom +
224    * XDP_PACKET_HEADROOM == sizeof(vlib_buffer_t), ie data will correctly
225    * point to vlib_buffer_t->data for 0-copy. In copy mode, we have to add
226    * XDP_PACKET_HEADROOM to desc offset during refill.
227    */
228   STATIC_ASSERT (sizeof (vlib_buffer_t) >= XDP_PACKET_HEADROOM, "wrong size");
229   umem_config.frame_headroom = sizeof (vlib_buffer_t) - XDP_PACKET_HEADROOM;
230   umem_config.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
231   if (xsk_umem__create
232       (umem, uword_to_pointer (vm->buffer_main->buffer_mem_start, void *),
233        vm->buffer_main->buffer_mem_size, fq, cq, &umem_config))
234     {
235       args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
236       args->error = clib_error_return_unix (0, "xsk_umem__create() failed");
237       goto err0;
238     }
239
240   memset (&sock_config, 0, sizeof (sock_config));
241   sock_config.rx_size = args->rxq_size;
242   sock_config.tx_size = args->txq_size;
243   sock_config.bind_flags = XDP_USE_NEED_WAKEUP;
244   switch (args->mode)
245     {
246     case AF_XDP_MODE_AUTO:
247       break;
248     case AF_XDP_MODE_COPY:
249       sock_config.bind_flags |= XDP_COPY;
250       break;
251     case AF_XDP_MODE_ZERO_COPY:
252       sock_config.bind_flags |= XDP_ZEROCOPY;
253       break;
254     }
255   if (xsk_socket__create
256       (xsk, ad->linux_ifname, qid, *umem, rx, tx, &sock_config))
257     {
258       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
259       args->error =
260         clib_error_return_unix (0,
261                                 "xsk_socket__create() failed (is linux netdev %s up?)",
262                                 ad->linux_ifname);
263       goto err1;
264     }
265
266   fd = xsk_socket__fd (*xsk);
267   optlen = sizeof (opt);
268   if (getsockopt (fd, SOL_XDP, XDP_OPTIONS, &opt, &optlen))
269     {
270       args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
271       args->error =
272         clib_error_return_unix (0, "getsockopt(XDP_OPTIONS) failed");
273       goto err2;
274     }
275   if (opt.flags & XDP_OPTIONS_ZEROCOPY)
276     ad->flags |= AF_XDP_DEVICE_F_ZEROCOPY;
277
278   rxq->xsk_fd = is_rx ? fd : -1;
279
280   if (is_tx)
281     {
282       txq->xsk_fd = fd;
283       if (is_rx && (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK))
284         {
285           /* This is a shared rx+tx queue and we need to lock before syscalls.
286            * Prior to Linux 5.6 there is a race condition preventing to call
287            * poll() and sendto() concurrently on AF_XDP sockets. This was
288            * fixed with commit 11cc2d21499cabe7e7964389634ed1de3ee91d33
289            * to workaround this issue, we protect the syscalls with a
290            * spinlock. Note that it also prevents to use interrupt mode in
291            * multi workers setup, because in this case the poll() is done in
292            * the framework w/o any possibility to protect it.
293            * See
294            * https://lore.kernel.org/bpf/BYAPR11MB365382C5DB1E5FCC53242609C1549@BYAPR11MB3653.namprd11.prod.outlook.com/
295            */
296           clib_spinlock_init (&rxq->syscall_lock);
297           txq->syscall_lock = rxq->syscall_lock;
298         }
299     }
300   else
301     {
302       txq->xsk_fd = -1;
303     }
304
305   return 0;
306
307 err2:
308   xsk_socket__delete (*xsk);
309 err1:
310   xsk_umem__delete (*umem);
311 err0:
312   *umem = 0;
313   *xsk = 0;
314   return -1;
315 }
316
317 static int
318 af_xdp_get_numa (const char *ifname)
319 {
320   char *path;
321   clib_error_t *err;
322   int numa;
323
324   path =
325     (char *) format (0, "/sys/class/net/%s/device/numa_node%c", ifname, 0);
326   err = clib_sysfs_read (path, "%d", &numa);
327   if (err || numa < 0)
328     numa = 0;
329
330   clib_error_free (err);
331   vec_free (path);
332   return numa;
333 }
334
335 static clib_error_t *
336 af_xdp_device_rxq_read_ready (clib_file_t * f)
337 {
338   vnet_hw_if_rx_queue_set_int_pending (vnet_get_main (), f->private_data);
339   return 0;
340 }
341
342 static clib_error_t *
343 af_xdp_device_set_rxq_mode (const af_xdp_device_t *ad, af_xdp_rxq_t *rxq,
344                             const af_xdp_rxq_mode_t mode)
345 {
346   clib_file_main_t *fm = &file_main;
347   clib_file_update_type_t update;
348   clib_file_t *f;
349
350   if (rxq->mode == mode)
351     return 0;
352
353   switch (mode)
354     {
355     case AF_XDP_RXQ_MODE_POLLING:
356       update = UNIX_FILE_UPDATE_DELETE;
357       break;
358     case AF_XDP_RXQ_MODE_INTERRUPT:
359       if (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK)
360         return clib_error_create (
361           "kernel workaround incompatible with interrupt mode");
362       update = UNIX_FILE_UPDATE_ADD;
363       break;
364     default:
365       ASSERT (0);
366       return clib_error_create ("unknown rxq mode %i", mode);
367     }
368
369   f = clib_file_get (fm, rxq->file_index);
370   fm->file_update (f, update);
371   rxq->mode = mode;
372   return 0;
373 }
374
375 void
376 af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args)
377 {
378   vnet_main_t *vnm = vnet_get_main ();
379   vlib_thread_main_t *tm = vlib_get_thread_main ();
380   af_xdp_main_t *am = &af_xdp_main;
381   af_xdp_device_t *ad;
382   vnet_sw_interface_t *sw;
383   vnet_hw_interface_t *hw;
384   int rxq_num, txq_num, q_num;
385   int i;
386
387   args->rxq_size = args->rxq_size ? args->rxq_size : 2 * VLIB_FRAME_SIZE;
388   args->txq_size = args->txq_size ? args->txq_size : 2 * VLIB_FRAME_SIZE;
389   rxq_num = args->rxq_num ? args->rxq_num : 1;
390   txq_num = tm->n_vlib_mains;
391
392   if (!args->linux_ifname)
393     {
394       args->rv = VNET_API_ERROR_INVALID_VALUE;
395       args->error = clib_error_return (0, "missing host interface");
396       goto err0;
397     }
398
399   if (args->rxq_size < VLIB_FRAME_SIZE || args->txq_size < VLIB_FRAME_SIZE ||
400       args->rxq_size > 65535 || args->txq_size > 65535 ||
401       !is_pow2 (args->rxq_size) || !is_pow2 (args->txq_size))
402     {
403       args->rv = VNET_API_ERROR_INVALID_VALUE;
404       args->error =
405         clib_error_return (0,
406                            "queue size must be a power of two between %i and 65535",
407                            VLIB_FRAME_SIZE);
408       goto err0;
409     }
410
411   pool_get_zero (am->devices, ad);
412
413   if (tm->n_vlib_mains > 1 &&
414       0 == (args->flags & AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK))
415     ad->flags |= AF_XDP_DEVICE_F_SYSCALL_LOCK;
416
417   ad->linux_ifname = (char *) format (0, "%s", args->linux_ifname);
418   vec_validate (ad->linux_ifname, IFNAMSIZ - 1);        /* libbpf expects ifname to be at least IFNAMSIZ */
419
420   if (args->prog && af_xdp_load_program (args, ad))
421     goto err1;
422
423   q_num = clib_max (rxq_num, txq_num);
424   ad->rxq_num = rxq_num;
425   ad->txq_num = txq_num;
426   for (i = 0; i < q_num; i++)
427     {
428       if (af_xdp_create_queue (vm, args, ad, i))
429         {
430           /*
431            * queue creation failed
432            * it is only a fatal error if we could not create the number of rx
433            * queues requested explicitely by the user and the user did not
434            * requested 'max'
435            * we might create less tx queues than workers but this is ok
436            */
437
438           /* fixup vectors length */
439           vec_set_len (ad->umem, i);
440           vec_set_len (ad->xsk, i);
441           vec_set_len (ad->rxqs, i);
442           vec_set_len (ad->txqs, i);
443
444           ad->rxq_num = clib_min (i, rxq_num);
445           ad->txq_num = clib_min (i, txq_num);
446
447           if (i < rxq_num && AF_XDP_NUM_RX_QUEUES_ALL != rxq_num)
448             {
449               ad->rxq_num = ad->txq_num = 0;
450               goto err1; /* failed creating requested rxq: fatal error, bailing
451                             out */
452             }
453
454           if (i < txq_num)
455             {
456               /* we created less txq than threads not an error but initialize lock for shared txq */
457               for (i = 0; i < ad->txq_num; i++)
458                 clib_spinlock_init (&vec_elt (ad->txqs, i).lock);
459             }
460
461           args->rv = 0;
462           clib_error_free (args->error);
463           break;
464         }
465     }
466
467   ad->dev_instance = ad - am->devices;
468   ad->per_interface_next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
469   ad->pool =
470     vlib_buffer_pool_get_default_for_numa (vm,
471                                            af_xdp_get_numa
472                                            (ad->linux_ifname));
473   if (!args->name)
474     ad->name =
475       (char *) format (0, "%s/%d", ad->linux_ifname, ad->dev_instance);
476   else
477     ad->name = (char *) format (0, "%s", args->name);
478
479   ethernet_mac_address_generate (ad->hwaddr);
480
481   /* create interface */
482   if (ethernet_register_interface (vnm, af_xdp_device_class.index,
483                                    ad->dev_instance, ad->hwaddr,
484                                    &ad->hw_if_index, af_xdp_flag_change))
485     {
486       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
487       args->error =
488         clib_error_return (0, "ethernet_register_interface() failed");
489       goto err1;
490     }
491
492   sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
493   hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
494   args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
495   hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
496
497   vnet_hw_if_set_input_node (vnm, ad->hw_if_index, af_xdp_input_node.index);
498
499   for (i = 0; i < ad->rxq_num; i++)
500     {
501       af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
502       rxq->queue_index = vnet_hw_if_register_rx_queue (
503         vnm, ad->hw_if_index, i, VNET_HW_IF_RXQ_THREAD_ANY);
504       u8 *desc = format (0, "%U rxq %d", format_af_xdp_device_name,
505                          ad->dev_instance, i);
506       clib_file_t f = {
507         .file_descriptor = rxq->xsk_fd,
508         .private_data = rxq->queue_index,
509         .read_function = af_xdp_device_rxq_read_ready,
510         .description = desc,
511       };
512       rxq->file_index = clib_file_add (&file_main, &f);
513       vnet_hw_if_set_rx_queue_file_index (vnm, rxq->queue_index,
514                                           rxq->file_index);
515       if (af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING))
516         goto err1;
517     }
518
519   vnet_hw_if_update_runtime_data (vnm, ad->hw_if_index);
520
521   /* buffer template */
522   vec_validate_aligned (ad->buffer_template, 1, CLIB_CACHE_LINE_BYTES);
523   ad->buffer_template->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
524   ad->buffer_template->ref_count = 1;
525   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_RX] = ad->sw_if_index;
526   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
527   ad->buffer_template->buffer_pool_index = ad->pool;
528
529   return;
530
531 err1:
532   af_xdp_delete_if (vm, ad);
533 err0:
534   vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
535 }
536
537 static clib_error_t *
538 af_xdp_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
539 {
540   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
541   af_xdp_main_t *am = &af_xdp_main;
542   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
543   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
544
545   if (ad->flags & AF_XDP_DEVICE_F_ERROR)
546     return clib_error_return (0, "device is in error state");
547
548   if (is_up)
549     {
550       vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
551                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
552       ad->flags |= AF_XDP_DEVICE_F_ADMIN_UP;
553     }
554   else
555     {
556       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
557       ad->flags &= ~AF_XDP_DEVICE_F_ADMIN_UP;
558     }
559   return 0;
560 }
561
562 static clib_error_t *
563 af_xdp_interface_rx_mode_change (vnet_main_t *vnm, u32 hw_if_index, u32 qid,
564                                  vnet_hw_if_rx_mode mode)
565 {
566   af_xdp_main_t *am = &af_xdp_main;
567   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
568   af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
569   af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
570
571   switch (mode)
572     {
573     default:                         /* fallthrough */
574     case VNET_HW_IF_RX_MODE_UNKNOWN: /* fallthrough */
575     case VNET_HW_IF_NUM_RX_MODES:
576       return clib_error_create ("uknown rx mode - doing nothing");
577     case VNET_HW_IF_RX_MODE_DEFAULT: /* fallthrough */
578     case VNET_HW_IF_RX_MODE_POLLING:
579       return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING);
580     case VNET_HW_IF_RX_MODE_INTERRUPT: /* fallthrough */
581     case VNET_HW_IF_RX_MODE_ADAPTIVE:
582       return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_INTERRUPT);
583     }
584
585   ASSERT (0 && "unreachable");
586   return clib_error_create ("unreachable");
587 }
588
589 static void
590 af_xdp_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
591                                 u32 node_index)
592 {
593   af_xdp_main_t *am = &af_xdp_main;
594   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
595   af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
596
597   /* Shut off redirection */
598   if (node_index == ~0)
599     {
600       ad->per_interface_next_index = node_index;
601       return;
602     }
603
604   ad->per_interface_next_index =
605     vlib_node_add_next (vlib_get_main (), af_xdp_input_node.index,
606                         node_index);
607 }
608
609 static char *af_xdp_tx_func_error_strings[] = {
610 #define _(n,s) s,
611   foreach_af_xdp_tx_func_error
612 #undef _
613 };
614
615 static void
616 af_xdp_clear (u32 dev_instance)
617 {
618   af_xdp_main_t *am = &af_xdp_main;
619   af_xdp_device_t *ad = pool_elt_at_index (am->devices, dev_instance);
620   clib_error_free (ad->error);
621 }
622
623 /* *INDENT-OFF* */
624 VNET_DEVICE_CLASS (af_xdp_device_class) = {
625   .name = "AF_XDP interface",
626   .format_device = format_af_xdp_device,
627   .format_device_name = format_af_xdp_device_name,
628   .admin_up_down_function = af_xdp_interface_admin_up_down,
629   .rx_mode_change_function = af_xdp_interface_rx_mode_change,
630   .rx_redirect_to_node = af_xdp_set_interface_next_node,
631   .tx_function_n_errors = AF_XDP_TX_N_ERROR,
632   .tx_function_error_strings = af_xdp_tx_func_error_strings,
633   .mac_addr_change_function = af_xdp_mac_change,
634   .clear_counters = af_xdp_clear,
635 };
636 /* *INDENT-ON* */
637
638 clib_error_t *
639 af_xdp_init (vlib_main_t * vm)
640 {
641   af_xdp_main_t *am = &af_xdp_main;
642
643   am->log_class = vlib_log_register_class ("af_xdp", 0);
644
645   return 0;
646 }
647
648 VLIB_INIT_FUNCTION (af_xdp_init);
649
650 /*
651  * fd.io coding-style-patch-verification: ON
652  *
653  * Local Variables:
654  * eval: (c-set-style "gnu")
655  * End:
656  */