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