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