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;
101 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
102 ethernet_delete_interface (vnm, ad->hw_if_index);
105 vec_foreach (rxq, ad->rxqs) clib_file_del_by_index (&file_main,
107 vec_foreach (txq, ad->txqs) clib_spinlock_free (&txq->lock);
108 vec_foreach (xsk, ad->xsk) xsk_socket__delete (*xsk);
109 vec_foreach (umem, ad->umem) xsk_umem__delete (*umem);
113 bpf_set_link_xdp_fd (ad->linux_ifindex, -1, 0);
114 bpf_object__unload (ad->bpf_obj);
119 vec_free (ad->buffer_template);
122 clib_error_free (ad->error);
123 pool_put (axm->devices, ad);
127 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
131 ad->linux_ifindex = if_nametoindex (ad->linux_ifname);
132 if (!ad->linux_ifindex)
134 args->rv = VNET_API_ERROR_INVALID_VALUE;
136 clib_error_return_unix (0, "if_nametoindex(%s) failed",
141 if (bpf_prog_load (args->prog, BPF_PROG_TYPE_XDP, &ad->bpf_obj, &fd))
143 args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
145 clib_error_return_unix (0, "bpf_prog_load(%s) failed", args->prog);
149 #ifndef XDP_FLAGS_REPLACE
150 #define XDP_FLAGS_REPLACE 0
152 if (bpf_set_link_xdp_fd (ad->linux_ifindex, fd, XDP_FLAGS_REPLACE))
154 args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
156 clib_error_return_unix (0, "bpf_set_link_xdp_fd(%s) failed",
164 bpf_object__unload (ad->bpf_obj);
167 ad->linux_ifindex = ~0;
172 af_xdp_create_queue (vlib_main_t * vm, af_xdp_create_if_args_t * args,
173 af_xdp_device_t * ad, int qid, int rxq_num, int txq_num)
175 struct xsk_umem **umem;
176 struct xsk_socket **xsk;
179 struct xsk_umem_config umem_config;
180 struct xsk_socket_config sock_config;
181 struct xdp_options opt;
184 vec_validate_aligned (ad->umem, qid, CLIB_CACHE_LINE_BYTES);
185 umem = vec_elt_at_index (ad->umem, qid);
187 vec_validate_aligned (ad->xsk, qid, CLIB_CACHE_LINE_BYTES);
188 xsk = vec_elt_at_index (ad->xsk, qid);
190 vec_validate_aligned (ad->rxqs, qid, CLIB_CACHE_LINE_BYTES);
191 rxq = vec_elt_at_index (ad->rxqs, qid);
193 vec_validate_aligned (ad->txqs, qid, CLIB_CACHE_LINE_BYTES);
194 txq = vec_elt_at_index (ad->txqs, qid);
197 * fq and cq must always be allocated even if unused
198 * whereas rx and tx indicates whether we want rxq, txq, or both
200 struct xsk_ring_cons *rx = qid < rxq_num ? &rxq->rx : 0;
201 struct xsk_ring_prod *fq = &rxq->fq;
202 struct xsk_ring_prod *tx = qid < txq_num ? &txq->tx : 0;
203 struct xsk_ring_cons *cq = &txq->cq;
206 memset (&umem_config, 0, sizeof (umem_config));
207 umem_config.fill_size = args->rxq_size;
208 umem_config.comp_size = args->txq_size;
209 umem_config.frame_size =
210 sizeof (vlib_buffer_t) + vlib_buffer_get_default_data_size (vm);
211 umem_config.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
213 (umem, uword_to_pointer (vm->buffer_main->buffer_mem_start, void *),
214 vm->buffer_main->buffer_mem_size, fq, cq, &umem_config))
216 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
217 args->error = clib_error_return_unix (0, "xsk_umem__create() failed");
221 memset (&sock_config, 0, sizeof (sock_config));
222 sock_config.rx_size = args->rxq_size;
223 sock_config.tx_size = args->txq_size;
224 sock_config.bind_flags = XDP_USE_NEED_WAKEUP;
227 case AF_XDP_MODE_AUTO:
229 case AF_XDP_MODE_COPY:
230 sock_config.bind_flags |= XDP_COPY;
232 case AF_XDP_MODE_ZERO_COPY:
233 sock_config.bind_flags |= XDP_ZEROCOPY;
236 if (xsk_socket__create
237 (xsk, ad->linux_ifname, qid, *umem, rx, tx, &sock_config))
239 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
241 clib_error_return_unix (0,
242 "xsk_socket__create() failed (is linux netdev %s up?)",
247 fd = xsk_socket__fd (*xsk);
248 optlen = sizeof (opt);
249 if (getsockopt (fd, SOL_XDP, XDP_OPTIONS, &opt, &optlen))
251 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
253 clib_error_return_unix (0, "getsockopt(XDP_OPTIONS) failed");
256 if (opt.flags & XDP_OPTIONS_ZEROCOPY)
257 ad->flags |= AF_XDP_DEVICE_F_ZEROCOPY;
259 rxq->xsk_fd = qid < rxq_num ? fd : -1;
260 txq->xsk_fd = qid < txq_num ? fd : -1;
265 xsk_socket__delete (*xsk);
267 xsk_umem__delete (*umem);
275 af_xdp_get_numa (const char *ifname)
282 (char *) format (0, "/sys/class/net/%s/device/numa_node%c", ifname, 0);
283 err = clib_sysfs_read (path, "%d", &numa);
287 clib_error_free (err);
292 static clib_error_t *
293 af_xdp_device_rxq_read_ready (clib_file_t * f)
295 vnet_hw_if_rx_queue_set_int_pending (vnet_get_main (), f->private_data);
300 af_xdp_device_set_rxq_mode (af_xdp_rxq_t *rxq, int is_polling)
302 clib_file_main_t *fm = &file_main;
305 if (rxq->is_polling == is_polling)
308 f = clib_file_get (fm, rxq->file_index);
309 fm->file_update (f, is_polling ? UNIX_FILE_UPDATE_DELETE :
310 UNIX_FILE_UPDATE_ADD);
311 rxq->is_polling = !!is_polling;
315 af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args)
317 vnet_main_t *vnm = vnet_get_main ();
318 vlib_thread_main_t *tm = vlib_get_thread_main ();
319 af_xdp_main_t *am = &af_xdp_main;
321 vnet_sw_interface_t *sw;
322 vnet_hw_interface_t *hw;
323 int rxq_num, txq_num, q_num;
326 args->rxq_size = args->rxq_size ? args->rxq_size : 2 * VLIB_FRAME_SIZE;
327 args->txq_size = args->txq_size ? args->txq_size : 2 * VLIB_FRAME_SIZE;
328 rxq_num = args->rxq_num ? args->rxq_num : 1;
329 txq_num = tm->n_vlib_mains;
331 if (!args->linux_ifname)
333 args->rv = VNET_API_ERROR_INVALID_VALUE;
334 args->error = clib_error_return (0, "missing host interface");
338 if (args->rxq_size < VLIB_FRAME_SIZE || args->txq_size < VLIB_FRAME_SIZE ||
339 args->rxq_size > 65535 || args->txq_size > 65535 ||
340 !is_pow2 (args->rxq_size) || !is_pow2 (args->txq_size))
342 args->rv = VNET_API_ERROR_INVALID_VALUE;
344 clib_error_return (0,
345 "queue size must be a power of two between %i and 65535",
350 pool_get_zero (am->devices, ad);
352 ad->linux_ifname = (char *) format (0, "%s", args->linux_ifname);
353 vec_validate (ad->linux_ifname, IFNAMSIZ - 1); /* libbpf expects ifname to be at least IFNAMSIZ */
355 if (args->prog && af_xdp_load_program (args, ad))
358 q_num = clib_max (rxq_num, txq_num);
359 ad->txq_num = txq_num;
360 for (i = 0; i < q_num; i++)
362 if (af_xdp_create_queue (vm, args, ad, i, rxq_num, txq_num))
365 * queue creation failed
366 * it is only a fatal error if we could not create the number of rx
367 * queues requested explicitely by the user and the user did not
369 * we might create less tx queues than workers but this is ok
372 /* fixup vectors length */
373 vec_set_len (ad->umem, i);
374 vec_set_len (ad->xsk, i);
375 vec_set_len (ad->rxqs, i);
376 vec_set_len (ad->txqs, i);
378 if (i < rxq_num && AF_XDP_NUM_RX_QUEUES_ALL != rxq_num)
379 goto err1; /* failed creating requested rxq: fatal error, bailing out */
383 /* we created less txq than threads not an error but initialize lock for shared txq */
386 vec_foreach (txq, ad->txqs) clib_spinlock_init (&txq->lock);
390 clib_error_free (args->error);
395 ad->dev_instance = ad - am->devices;
396 ad->per_interface_next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
398 vlib_buffer_pool_get_default_for_numa (vm,
403 (char *) format (0, "%s/%d", ad->linux_ifname, ad->dev_instance);
405 ad->name = (char *) format (0, "%s", args->name);
407 ethernet_mac_address_generate (ad->hwaddr);
409 /* create interface */
410 if (ethernet_register_interface (vnm, af_xdp_device_class.index,
411 ad->dev_instance, ad->hwaddr,
412 &ad->hw_if_index, af_xdp_flag_change))
414 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
416 clib_error_return (0, "ethernet_register_interface() failed");
420 sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
421 hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
422 args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
423 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
425 vnet_hw_if_set_input_node (vnm, ad->hw_if_index, af_xdp_input_node.index);
427 for (i = 0; i < vec_len (ad->rxqs); i++)
429 af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
430 rxq->queue_index = vnet_hw_if_register_rx_queue (
431 vnm, ad->hw_if_index, i, VNET_HW_IF_RXQ_THREAD_ANY);
432 u8 *desc = format (0, "%U rxq %d", format_af_xdp_device_name,
433 ad->dev_instance, i);
435 .file_descriptor = rxq->xsk_fd,
436 .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
437 .private_data = rxq->queue_index,
438 .read_function = af_xdp_device_rxq_read_ready,
441 rxq->file_index = clib_file_add (&file_main, &f);
442 vnet_hw_if_set_rx_queue_file_index (vnm, rxq->queue_index,
444 af_xdp_device_set_rxq_mode (rxq, 1 /* polling */);
447 vnet_hw_if_update_runtime_data (vnm, ad->hw_if_index);
449 /* buffer template */
450 vec_validate_aligned (ad->buffer_template, 1, CLIB_CACHE_LINE_BYTES);
451 ad->buffer_template->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
452 ad->buffer_template->ref_count = 1;
453 vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_RX] = ad->sw_if_index;
454 vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
455 ad->buffer_template->buffer_pool_index = ad->pool;
460 af_xdp_delete_if (vm, ad);
462 vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
465 static clib_error_t *
466 af_xdp_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
468 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
469 af_xdp_main_t *am = &af_xdp_main;
470 af_xdp_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
471 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
473 if (ad->flags & AF_XDP_DEVICE_F_ERROR)
474 return clib_error_return (0, "device is in error state");
478 vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
479 VNET_HW_INTERFACE_FLAG_LINK_UP);
480 ad->flags |= AF_XDP_DEVICE_F_ADMIN_UP;
484 vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
485 ad->flags &= ~AF_XDP_DEVICE_F_ADMIN_UP;
490 static clib_error_t *
491 af_xdp_interface_rx_mode_change (vnet_main_t *vnm, u32 hw_if_index, u32 qid,
492 vnet_hw_if_rx_mode mode)
494 af_xdp_main_t *am = &af_xdp_main;
495 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
496 af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
497 af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
501 case VNET_HW_IF_RX_MODE_UNKNOWN:
502 case VNET_HW_IF_NUM_RX_MODES: /* fallthrough */
503 return clib_error_create ("uknown rx mode - doing nothing");
504 case VNET_HW_IF_RX_MODE_DEFAULT:
505 case VNET_HW_IF_RX_MODE_POLLING: /* fallthrough */
508 af_xdp_device_set_rxq_mode (rxq, 1 /* polling */);
510 case VNET_HW_IF_RX_MODE_INTERRUPT:
511 case VNET_HW_IF_RX_MODE_ADAPTIVE: /* fallthrough */
512 if (0 == rxq->is_polling)
514 af_xdp_device_set_rxq_mode (rxq, 0 /* interrupt */);
522 af_xdp_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
525 af_xdp_main_t *am = &af_xdp_main;
526 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
527 af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
529 /* Shut off redirection */
530 if (node_index == ~0)
532 ad->per_interface_next_index = node_index;
536 ad->per_interface_next_index =
537 vlib_node_add_next (vlib_get_main (), af_xdp_input_node.index,
541 static char *af_xdp_tx_func_error_strings[] = {
543 foreach_af_xdp_tx_func_error
548 af_xdp_clear (u32 dev_instance)
550 af_xdp_main_t *am = &af_xdp_main;
551 af_xdp_device_t *ad = pool_elt_at_index (am->devices, dev_instance);
552 clib_error_free (ad->error);
556 VNET_DEVICE_CLASS (af_xdp_device_class) = {
557 .name = "AF_XDP interface",
558 .format_device = format_af_xdp_device,
559 .format_device_name = format_af_xdp_device_name,
560 .admin_up_down_function = af_xdp_interface_admin_up_down,
561 .rx_mode_change_function = af_xdp_interface_rx_mode_change,
562 .rx_redirect_to_node = af_xdp_set_interface_next_node,
563 .tx_function_n_errors = AF_XDP_TX_N_ERROR,
564 .tx_function_error_strings = af_xdp_tx_func_error_strings,
565 .mac_addr_change_function = af_xdp_mac_change,
566 .clear_counters = af_xdp_clear,
571 af_xdp_init (vlib_main_t * vm)
573 af_xdp_main_t *am = &af_xdp_main;
575 am->log_class = vlib_log_register_class ("af_xdp", 0);
580 VLIB_INIT_FUNCTION (af_xdp_init);
583 * fd.io coding-style-patch-verification: ON
586 * eval: (c-set-style "gnu")