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