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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <linux/if_link.h>
21 #include <bpf/libbpf.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/pci/pci.h>
25 #include <vppinfra/linux/sysfs.h>
26 #include <vppinfra/unix.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/interface/rx_queue_funcs.h>
31 af_xdp_main_t af_xdp_main;
40 gdb_af_xdp_get_prod (const struct xsk_ring_prod *prod)
42 gdb_af_xdp_pair_t pair = { *prod->producer, *prod->consumer };
47 gdb_af_xdp_get_cons (const struct xsk_ring_cons * cons)
49 gdb_af_xdp_pair_t pair = { *cons->producer, *cons->consumer };
54 af_xdp_mac_change (vnet_hw_interface_t * hw, const u8 * old, const u8 * new)
56 af_xdp_main_t *am = &af_xdp_main;
57 af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
58 errno_t err = memcpy_s (ad->hwaddr, sizeof (ad->hwaddr), new, 6);
60 return clib_error_return_code (0, -err, CLIB_ERROR_ERRNO_VALID,
66 af_xdp_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
68 af_xdp_main_t *am = &af_xdp_main;
69 af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
74 af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set unicast not supported yet");
76 case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
77 af_xdp_log (VLIB_LOG_LEVEL_ERR, ad,
78 "set promiscuous not supported yet");
80 case ETHERNET_INTERFACE_FLAG_MTU:
81 af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set mtu not supported yet");
85 af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "unknown flag %x requested", flags);
90 af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad)
92 vnet_main_t *vnm = vnet_get_main ();
93 af_xdp_main_t *axm = &af_xdp_main;
94 struct xsk_socket **xsk;
95 struct xsk_umem **umem;
100 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
101 ethernet_delete_interface (vnm, ad->hw_if_index);
104 for (i = 0; i < ad->rxq_num; i++)
105 clib_file_del_by_index (&file_main, vec_elt (ad->rxqs, i).file_index);
107 for (i = 0; i < ad->txq_num; i++)
108 clib_spinlock_free (&vec_elt (ad->txqs, i).lock);
110 vec_foreach (xsk, ad->xsk)
111 xsk_socket__delete (*xsk);
113 vec_foreach (umem, ad->umem)
114 xsk_umem__delete (*umem);
118 bpf_set_link_xdp_fd (ad->linux_ifindex, -1, 0);
119 bpf_object__unload (ad->bpf_obj);
124 vec_free (ad->buffer_template);
127 clib_error_free (ad->error);
128 pool_put (axm->devices, ad);
132 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
136 ad->linux_ifindex = if_nametoindex (ad->linux_ifname);
137 if (!ad->linux_ifindex)
139 args->rv = VNET_API_ERROR_INVALID_VALUE;
141 clib_error_return_unix (0, "if_nametoindex(%s) failed",
146 if (bpf_prog_load (args->prog, BPF_PROG_TYPE_XDP, &ad->bpf_obj, &fd))
148 args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
150 clib_error_return_unix (0, "bpf_prog_load(%s) failed", args->prog);
154 #ifndef XDP_FLAGS_REPLACE
155 #define XDP_FLAGS_REPLACE 0
157 if (bpf_set_link_xdp_fd (ad->linux_ifindex, fd, XDP_FLAGS_REPLACE))
159 args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
161 clib_error_return_unix (0, "bpf_set_link_xdp_fd(%s) failed",
169 bpf_object__unload (ad->bpf_obj);
172 ad->linux_ifindex = ~0;
177 af_xdp_create_queue (vlib_main_t *vm, af_xdp_create_if_args_t *args,
178 af_xdp_device_t *ad, int qid)
180 struct xsk_umem **umem;
181 struct xsk_socket **xsk;
184 struct xsk_umem_config umem_config;
185 struct xsk_socket_config sock_config;
186 struct xdp_options opt;
188 const int is_rx = qid < ad->rxq_num;
189 const int is_tx = qid < ad->txq_num;
191 vec_validate_aligned (ad->umem, qid, CLIB_CACHE_LINE_BYTES);
192 umem = vec_elt_at_index (ad->umem, qid);
194 vec_validate_aligned (ad->xsk, qid, CLIB_CACHE_LINE_BYTES);
195 xsk = vec_elt_at_index (ad->xsk, qid);
197 vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
198 rxq = vec_elt_at_index (ad->rxqs, qid);
200 vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
201 txq = vec_elt_at_index (ad->txqs, qid);
204 * fq and cq must always be allocated even if unused
205 * whereas rx and tx indicates whether we want rxq, txq, or both
207 struct xsk_ring_cons *rx = is_rx ? &rxq->rx : 0;
208 struct xsk_ring_prod *fq = &rxq->fq;
209 struct xsk_ring_prod *tx = is_tx ? &txq->tx : 0;
210 struct xsk_ring_cons *cq = &txq->cq;
213 memset (&umem_config, 0, sizeof (umem_config));
214 umem_config.fill_size = args->rxq_size;
215 umem_config.comp_size = args->txq_size;
216 umem_config.frame_size =
217 sizeof (vlib_buffer_t) + vlib_buffer_get_default_data_size (vm);
218 umem_config.frame_headroom = sizeof (vlib_buffer_t);
219 umem_config.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
221 (umem, uword_to_pointer (vm->buffer_main->buffer_mem_start, void *),
222 vm->buffer_main->buffer_mem_size, fq, cq, &umem_config))
224 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
225 args->error = clib_error_return_unix (0, "xsk_umem__create() failed");
229 memset (&sock_config, 0, sizeof (sock_config));
230 sock_config.rx_size = args->rxq_size;
231 sock_config.tx_size = args->txq_size;
232 sock_config.bind_flags = XDP_USE_NEED_WAKEUP;
235 case AF_XDP_MODE_AUTO:
237 case AF_XDP_MODE_COPY:
238 sock_config.bind_flags |= XDP_COPY;
240 case AF_XDP_MODE_ZERO_COPY:
241 sock_config.bind_flags |= XDP_ZEROCOPY;
244 if (xsk_socket__create
245 (xsk, ad->linux_ifname, qid, *umem, rx, tx, &sock_config))
247 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
249 clib_error_return_unix (0,
250 "xsk_socket__create() failed (is linux netdev %s up?)",
255 fd = xsk_socket__fd (*xsk);
256 optlen = sizeof (opt);
257 if (getsockopt (fd, SOL_XDP, XDP_OPTIONS, &opt, &optlen))
259 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
261 clib_error_return_unix (0, "getsockopt(XDP_OPTIONS) failed");
264 if (opt.flags & XDP_OPTIONS_ZEROCOPY)
265 ad->flags |= AF_XDP_DEVICE_F_ZEROCOPY;
267 rxq->xsk_fd = is_rx ? fd : -1;
272 if (is_rx && (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK))
274 /* This is a shared rx+tx queue and we need to lock before syscalls.
275 * Prior to Linux 5.6 there is a race condition preventing to call
276 * poll() and sendto() concurrently on AF_XDP sockets. This was
277 * fixed with commit 11cc2d21499cabe7e7964389634ed1de3ee91d33
278 * to workaround this issue, we protect the syscalls with a
279 * spinlock. Note that it also prevents to use interrupt mode in
280 * multi workers setup, because in this case the poll() is done in
281 * the framework w/o any possibility to protect it.
283 * https://lore.kernel.org/bpf/BYAPR11MB365382C5DB1E5FCC53242609C1549@BYAPR11MB3653.namprd11.prod.outlook.com/
285 clib_spinlock_init (&rxq->syscall_lock);
286 txq->syscall_lock = rxq->syscall_lock;
297 xsk_socket__delete (*xsk);
299 xsk_umem__delete (*umem);
307 af_xdp_get_numa (const char *ifname)
314 (char *) format (0, "/sys/class/net/%s/device/numa_node%c", ifname, 0);
315 err = clib_sysfs_read (path, "%d", &numa);
319 clib_error_free (err);
324 static clib_error_t *
325 af_xdp_device_rxq_read_ready (clib_file_t * f)
327 vnet_hw_if_rx_queue_set_int_pending (vnet_get_main (), f->private_data);
331 static clib_error_t *
332 af_xdp_device_set_rxq_mode (const af_xdp_device_t *ad, af_xdp_rxq_t *rxq,
333 const af_xdp_rxq_mode_t mode)
335 clib_file_main_t *fm = &file_main;
336 clib_file_update_type_t update;
339 if (rxq->mode == mode)
344 case AF_XDP_RXQ_MODE_POLLING:
345 update = UNIX_FILE_UPDATE_DELETE;
347 case AF_XDP_RXQ_MODE_INTERRUPT:
348 if (ad->flags & AF_XDP_DEVICE_F_SYSCALL_LOCK)
349 return clib_error_create (
350 "kernel workaround incompatible with interrupt mode");
351 update = UNIX_FILE_UPDATE_ADD;
355 return clib_error_create ("unknown rxq mode %i", mode);
358 f = clib_file_get (fm, rxq->file_index);
359 fm->file_update (f, update);
365 af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args)
367 vnet_main_t *vnm = vnet_get_main ();
368 vlib_thread_main_t *tm = vlib_get_thread_main ();
369 af_xdp_main_t *am = &af_xdp_main;
371 vnet_sw_interface_t *sw;
372 vnet_hw_interface_t *hw;
373 int rxq_num, txq_num, q_num;
376 args->rxq_size = args->rxq_size ? args->rxq_size : 2 * VLIB_FRAME_SIZE;
377 args->txq_size = args->txq_size ? args->txq_size : 2 * VLIB_FRAME_SIZE;
378 rxq_num = args->rxq_num ? args->rxq_num : 1;
379 txq_num = tm->n_vlib_mains;
381 if (!args->linux_ifname)
383 args->rv = VNET_API_ERROR_INVALID_VALUE;
384 args->error = clib_error_return (0, "missing host interface");
388 if (args->rxq_size < VLIB_FRAME_SIZE || args->txq_size < VLIB_FRAME_SIZE ||
389 args->rxq_size > 65535 || args->txq_size > 65535 ||
390 !is_pow2 (args->rxq_size) || !is_pow2 (args->txq_size))
392 args->rv = VNET_API_ERROR_INVALID_VALUE;
394 clib_error_return (0,
395 "queue size must be a power of two between %i and 65535",
400 pool_get_zero (am->devices, ad);
402 if (tm->n_vlib_mains > 1 &&
403 0 == (args->flags & AF_XDP_CREATE_FLAGS_NO_SYSCALL_LOCK))
404 ad->flags |= AF_XDP_DEVICE_F_SYSCALL_LOCK;
406 ad->linux_ifname = (char *) format (0, "%s", args->linux_ifname);
407 vec_validate (ad->linux_ifname, IFNAMSIZ - 1); /* libbpf expects ifname to be at least IFNAMSIZ */
409 if (args->prog && af_xdp_load_program (args, ad))
412 q_num = clib_max (rxq_num, txq_num);
413 ad->rxq_num = rxq_num;
414 ad->txq_num = txq_num;
415 for (i = 0; i < q_num; i++)
417 if (af_xdp_create_queue (vm, args, ad, i))
420 * queue creation failed
421 * it is only a fatal error if we could not create the number of rx
422 * queues requested explicitely by the user and the user did not
424 * we might create less tx queues than workers but this is ok
427 /* fixup vectors length */
428 vec_set_len (ad->umem, i);
429 vec_set_len (ad->xsk, i);
430 vec_set_len (ad->rxqs, i);
431 vec_set_len (ad->txqs, i);
433 ad->rxq_num = clib_min (i, rxq_num);
434 ad->txq_num = clib_min (i, txq_num);
436 if (i < rxq_num && AF_XDP_NUM_RX_QUEUES_ALL != rxq_num)
438 ad->rxq_num = ad->txq_num = 0;
439 goto err1; /* failed creating requested rxq: fatal error, bailing
445 /* we created less txq than threads not an error but initialize lock for shared txq */
446 for (i = 0; i < ad->txq_num; i++)
447 clib_spinlock_init (&vec_elt (ad->txqs, i).lock);
451 clib_error_free (args->error);
456 ad->dev_instance = ad - am->devices;
457 ad->per_interface_next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
459 vlib_buffer_pool_get_default_for_numa (vm,
464 (char *) format (0, "%s/%d", ad->linux_ifname, ad->dev_instance);
466 ad->name = (char *) format (0, "%s", args->name);
468 ethernet_mac_address_generate (ad->hwaddr);
470 /* create interface */
471 if (ethernet_register_interface (vnm, af_xdp_device_class.index,
472 ad->dev_instance, ad->hwaddr,
473 &ad->hw_if_index, af_xdp_flag_change))
475 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
477 clib_error_return (0, "ethernet_register_interface() failed");
481 sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
482 hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
483 args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
484 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
486 vnet_hw_if_set_input_node (vnm, ad->hw_if_index, af_xdp_input_node.index);
488 for (i = 0; i < ad->rxq_num; i++)
490 af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
491 rxq->queue_index = vnet_hw_if_register_rx_queue (
492 vnm, ad->hw_if_index, i, VNET_HW_IF_RXQ_THREAD_ANY);
493 u8 *desc = format (0, "%U rxq %d", format_af_xdp_device_name,
494 ad->dev_instance, i);
496 .file_descriptor = rxq->xsk_fd,
497 .private_data = rxq->queue_index,
498 .read_function = af_xdp_device_rxq_read_ready,
501 rxq->file_index = clib_file_add (&file_main, &f);
502 vnet_hw_if_set_rx_queue_file_index (vnm, rxq->queue_index,
504 if (af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING))
508 vnet_hw_if_update_runtime_data (vnm, ad->hw_if_index);
510 /* buffer template */
511 vec_validate_aligned (ad->buffer_template, 1, CLIB_CACHE_LINE_BYTES);
512 ad->buffer_template->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
513 ad->buffer_template->ref_count = 1;
514 vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_RX] = ad->sw_if_index;
515 vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
516 ad->buffer_template->buffer_pool_index = ad->pool;
521 af_xdp_delete_if (vm, ad);
523 vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
526 static clib_error_t *
527 af_xdp_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
529 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
530 af_xdp_main_t *am = &af_xdp_main;
531 af_xdp_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
532 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
534 if (ad->flags & AF_XDP_DEVICE_F_ERROR)
535 return clib_error_return (0, "device is in error state");
539 vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
540 VNET_HW_INTERFACE_FLAG_LINK_UP);
541 ad->flags |= AF_XDP_DEVICE_F_ADMIN_UP;
542 af_xdp_device_input_refill (ad);
546 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
547 ad->flags &= ~AF_XDP_DEVICE_F_ADMIN_UP;
552 static clib_error_t *
553 af_xdp_interface_rx_mode_change (vnet_main_t *vnm, u32 hw_if_index, u32 qid,
554 vnet_hw_if_rx_mode mode)
556 af_xdp_main_t *am = &af_xdp_main;
557 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
558 af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
559 af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
563 default: /* fallthrough */
564 case VNET_HW_IF_RX_MODE_UNKNOWN: /* fallthrough */
565 case VNET_HW_IF_NUM_RX_MODES:
566 return clib_error_create ("uknown rx mode - doing nothing");
567 case VNET_HW_IF_RX_MODE_DEFAULT: /* fallthrough */
568 case VNET_HW_IF_RX_MODE_POLLING:
569 return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_POLLING);
570 case VNET_HW_IF_RX_MODE_INTERRUPT: /* fallthrough */
571 case VNET_HW_IF_RX_MODE_ADAPTIVE:
572 return af_xdp_device_set_rxq_mode (ad, rxq, AF_XDP_RXQ_MODE_INTERRUPT);
575 ASSERT (0 && "unreachable");
576 return clib_error_create ("unreachable");
580 af_xdp_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
583 af_xdp_main_t *am = &af_xdp_main;
584 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
585 af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
587 /* Shut off redirection */
588 if (node_index == ~0)
590 ad->per_interface_next_index = node_index;
594 ad->per_interface_next_index =
595 vlib_node_add_next (vlib_get_main (), af_xdp_input_node.index,
599 static char *af_xdp_tx_func_error_strings[] = {
601 foreach_af_xdp_tx_func_error
606 af_xdp_clear (u32 dev_instance)
608 af_xdp_main_t *am = &af_xdp_main;
609 af_xdp_device_t *ad = pool_elt_at_index (am->devices, dev_instance);
610 clib_error_free (ad->error);
614 VNET_DEVICE_CLASS (af_xdp_device_class) = {
615 .name = "AF_XDP interface",
616 .format_device = format_af_xdp_device,
617 .format_device_name = format_af_xdp_device_name,
618 .admin_up_down_function = af_xdp_interface_admin_up_down,
619 .rx_mode_change_function = af_xdp_interface_rx_mode_change,
620 .rx_redirect_to_node = af_xdp_set_interface_next_node,
621 .tx_function_n_errors = AF_XDP_TX_N_ERROR,
622 .tx_function_error_strings = af_xdp_tx_func_error_strings,
623 .mac_addr_change_function = af_xdp_mac_change,
624 .clear_counters = af_xdp_clear,
629 af_xdp_init (vlib_main_t * vm)
631 af_xdp_main_t *am = &af_xdp_main;
633 am->log_class = vlib_log_register_class ("af_xdp", 0);
638 VLIB_INIT_FUNCTION (af_xdp_init);
641 * fd.io coding-style-patch-verification: ON
644 * eval: (c-set-style "gnu")