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