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