|
FD.io VPP
v21.06-3-gbb25fbf28
Vector Packet Processing
|
Go to the documentation of this file.
47 #include <sys/types.h>
51 #include <sys/ioctl.h>
53 #include <linux/ethtool.h>
54 #include <linux/sockios.h>
56 #include <sys/eventfd.h>
61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
63 #define pci_log_debug(vm, dev, f, ...) \
64 vlib_log(VLIB_LOG_LEVEL_DEBUG, pci_main.log_default, "%U: " f, \
65 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
66 #define pci_log_err(vm, dev, f, ...) \
67 vlib_log(VLIB_LOG_LEVEL_ERR, pci_main.log_default, "%U: " f, \
68 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
183 struct vfio_irq_info ii = { 0 };
185 ii.argsz =
sizeof (
struct vfio_irq_info);
186 ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
187 if (ioctl (d->
fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
215 di->addr.as_u32 =
addr->as_u32;
220 f =
format (0,
"%v/config%c", dev_dir_name, 0);
221 fd = open ((
char *)
f, O_RDWR);
225 fd = open ((
char *)
f, O_RDONLY);
235 if (read (fd, &
di->config_data, sizeof (
di->config_data)) <
236 sizeof (
di->config0))
248 if (!memcmp (&
di->config0.header, &all_ones, sizeof (all_ones)))
256 if (
di->config0.header.header_type == 0)
263 f =
format (
f,
"%v/numa_node%c", dev_dir_name, 0);
270 if (
di->numa_node == -1)
282 f =
format (
f,
"%v/class%c", dev_dir_name, 0);
286 di->device_class =
tmp >> 8;
289 f =
format (
f,
"%v/vendor%c", dev_dir_name, 0);
296 f =
format (
f,
"%v/device%c", dev_dir_name, 0);
303 f =
format (
f,
"%v/driver%c", dev_dir_name, 0);
305 if (!
di->driver_name)
306 di->driver_name =
format (0,
"<NONE>%c", 0);
308 di->iommu_group = -1;
310 f =
format (
f,
"%v/iommu_group%c", dev_dir_name, 0);
314 di->iommu_group = atoi ((
char *) tmpstr);
319 f =
format (
f,
"%v/iommu_group/name%c", dev_dir_name, 0);
323 if (strncmp ((
char *) tmpstr,
"vfio-noiommu", 12) == 0)
333 f =
format (
f,
"%v/vpd%c", dev_dir_name, 0);
334 fd = open ((
char *)
f, O_RDONLY);
343 if (read (fd, &tag, 3) != 3)
346 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
349 len = (tag[2] << 8) | tag[1];
359 else if (tag[0] == 0x90)
361 else if (tag[0] == 0x91)
389 struct stat s = { 0 };
390 if (stat (
path, &s) == -1)
393 return S_ISDIR (s.st_mode);
401 u8 *s = 0, *driver_name = 0;
405 int fd, clear_driver_override = 0;
416 int vfio_pci_loaded = 0;
421 if (
di->iommu_group != -1)
424 if (!vfio_pci_loaded)
427 "is bound to IOMMU group and "
428 "vfio-pci driver is not loaded",
438 if (vfio_pci_loaded &&
449 "kernel VFIO or UIO driver",
457 s =
format (s,
"%v/driver%c", dev_dir_name, 0);
462 ((strcmp (
"vfio-pci", (
char *) driver_name) == 0) ||
463 (strcmp (
"uio_pci_generic", (
char *) driver_name) == 0) ||
464 (strcmp (
"igb_uio", (
char *) driver_name) == 0)))
469 dir = opendir (
"/sys/class/net");
475 "read /sys/class/net",
480 fd = socket (PF_INET, SOCK_DGRAM, 0);
487 while ((e = readdir (dir)))
490 struct ethtool_drvinfo drvinfo;
492 if (e->d_name[0] ==
'.')
497 ifr.ifr_data = (
char *) &drvinfo;
498 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
500 drvinfo.cmd = ETHTOOL_GDRVINFO;
501 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
504 if ((errno != ENOTSUP) && (errno != ENODEV))
510 if (strcmp ((
char *) s, drvinfo.bus_info))
514 clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
516 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
524 if (ifr.ifr_flags & IFF_UP)
527 "Skipping PCI device %U as host "
538 s =
format (s,
"%v/driver/unbind%c", dev_dir_name, 0);
542 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
543 if (access ((
char *) s, F_OK) == 0)
546 clear_driver_override = 1;
561 if (clear_driver_override)
563 s =
format (s,
"%v/driver_override%c", dev_dir_name, 0);
598 struct vfio_irq_info ii = { 0 };
599 struct vfio_irq_set *irq_set = (
struct vfio_irq_set *)
buf;
602 ii.argsz =
sizeof (
struct vfio_irq_info);
605 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
610 __func__, ii.index, ii.count,
611 ii.flags & VFIO_IRQ_INFO_EVENTFD ?
"eventfd " :
"",
612 ii.flags & VFIO_IRQ_INFO_MASKABLE ?
"maskable " :
"",
613 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ?
"automasked " :
"",
614 ii.flags & VFIO_IRQ_INFO_NORESIZE ?
"noresize " :
"",
617 if (ii.count < start +
count)
624 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
628 flags |= VFIO_IRQ_SET_DATA_NONE;
630 ASSERT ((
flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
631 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
633 irq_set->argsz =
sizeof (
struct vfio_irq_set) +
data_len;
634 irq_set->index =
index;
635 irq_set->start = start;
636 irq_set->count =
count;
637 irq_set->flags =
flags;
639 if (ioctl (p->
fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
641 "[index = %u, start = %u, count = %u, "
652 int __attribute__ ((unused))
rv;
672 VFIO_IRQ_SET_ACTION_UNMASK, 0);
687 int __attribute__ ((unused))
rv;
706 int __attribute__ ((unused))
rv;
744 p->
config_fd = open ((
char *) s, O_RDWR);
761 p->
fd = open ((
char *) s, O_RDWR);
768 if (
r &&
r->interrupt_handler)
771 if (
r &&
r->init_function)
797 struct vfio_irq_info ii = { 0 };
798 ii.argsz =
sizeof (
struct vfio_irq_info);
799 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
800 if (ioctl (p->
fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
804 __func__, ii.index, ii.count,
805 ii.flags & VFIO_IRQ_INFO_EVENTFD ?
"eventfd " :
"",
806 ii.flags & VFIO_IRQ_INFO_MASKABLE ?
"maskable " :
"",
807 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ?
"automasked " :
"",
808 ii.flags & VFIO_IRQ_INFO_NORESIZE ?
"noresize " :
"",
814 irq->
fd = eventfd (0, EFD_NONBLOCK);
855 for (
i = start;
i < start +
count;
i++)
861 irq->
fd = eventfd (0, EFD_NONBLOCK);
906 for (
i = start;
i < start +
count;
i++)
913 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
938 for (
i = start;
i < start +
count;
i++)
942 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
950 struct vfio_device_info device_info = { 0 };
951 struct vfio_region_info reg = { 0 };
964 device_info.argsz =
sizeof (device_info);
965 if (ioctl (p->
fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
972 reg.argsz =
sizeof (
struct vfio_region_info);
973 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
974 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
987 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
988 if (ioctl (p->
fd, VFIO_DEVICE_RESET) < 0)
995 if (
r &&
r->interrupt_handler)
1016 if (
r &&
r->init_function)
1049 ?
"read" :
"write");
1061 u64 _size = 0, _offset = 0;
1070 struct stat stat_buf;
1074 _fd = open ((
char *) file_name, O_RDWR);
1082 if (fstat (_fd, &stat_buf) < 0)
1091 _size = stat_buf.st_size;
1096 struct vfio_region_info *
r;
1097 u32 sz =
sizeof (
struct vfio_region_info);
1103 if (ioctl (p->
fd, VFIO_DEVICE_GET_REGION_INFO,
r) < 0)
1115 _offset =
r->offset;
1136 int flags = MAP_SHARED;
1142 if ((
error = vlib_pci_read_config_u16 (
vm,
h, 4, &command)))
1149 if ((
error = vlib_pci_write_config_u16 (
vm,
h, 4, &command)))
1227 ?
"read" :
"write");
1264 for (
i = ids;
i->vendor_id != 0;
i++)
1265 if (
i->vendor_id ==
di->vendor_id &&
i->device_id ==
di->device_id)
1268 if (
i->vendor_id == 0)
1273 p->
addr.as_u32 =
di->addr.as_u32;
1282 pci_log_debug (
vm, p,
"open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1283 di->vendor_id,
di->device_id,
di->driver_name,
1288 else if (
clib_strncmp (
"uio_pci_generic", (
char *)
di->driver_name, 8) == 0)
1292 "'uio_pci_generic' kernel module");
1335 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1346 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1396 for (
i =
r->supported_devices;
i->vendor_id != 0;
i++)
1397 if (
i->vendor_id ==
di->vendor_id &&
i->device_id ==
di->device_id)
1399 if (
di->iommu_group != -1)
1409 r =
r->next_registration;
1420 vlib_pci_addr_t
addr, **addrv = arg;
1427 if (!
unformat (&input,
"/sys/bus/pci/devices/%U",
1443 vlib_pci_addr_t *a1 = v1;
1444 vlib_pci_addr_t *a2 = v2;
1446 if (a1->domain > a2->domain)
1448 if (a1->domain < a2->domain)
1450 if (a1->bus > a2->bus)
1452 if (a1->bus < a2->bus)
1454 if (a1->slot > a2->slot)
1456 if (a1->slot < a2->slot)
1458 if (a1->function > a2->function)
1460 if (a1->function < a2->function)
1468 vlib_pci_addr_t *addrs = 0;
1487 vlib_pci_addr_t *
addr = 0, *addrs;
1491 ASSERT (
sizeof (vlib_pci_addr_t) ==
sizeof (
u32));
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_error_t * pci_bus_init(vlib_main_t *vm)
static const char uio_drv_name[]
void vlib_pci_set_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h, uword private_data)
#define clib_strncpy(d, s, n)
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
pci_msix_handler_function_t * msix_handler
clib_file_main_t file_main
static clib_error_t * linux_pci_uio_read_ready(clib_file_t *uf)
__clib_export u8 * clib_sysfs_link_to_name(char *link)
format_function_t format_vfio_region_info
static clib_error_t * vlib_pci_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, int *fd, u64 *size, u64 *offset)
clib_error_t * foreach_directory_file(char *dir_name, clib_error_t *(*f)(void *arg, u8 *path_name, u8 *file_name), void *arg, int scan_dirs)
clib_error_t * vlib_pci_disable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
@ LINUX_PCI_DEVICE_TYPE_UIO
u32 vlib_pci_get_numa_node(vlib_main_t *vm, vlib_pci_dev_handle_t h)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
static void clib_mem_free(void *p)
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
pci_device_registration_t * pci_device_registrations
static int directory_exists(char *path)
__clib_export void * clib_mem_vm_map_shared(void *base, uword size, int fd, uword offset, char *fmt,...)
__clib_export int clib_mem_vm_unmap(void *base)
vlib_log_class_t log_default
#define clib_error_return(e, args...)
clib_file_function_t * read_function
u32 vlib_pci_dev_handle_t
clib_error_t * vlib_pci_enable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
static const char * sysfs_pci_drv_path
#define pci_log_err(vm, dev, f,...)
#define pool_put(P, E)
Free an object E in pool P.
vlib_buffer_main_t * buffer_main
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
clib_error_t * vlib_pci_register_msix_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 start, u32 count, pci_msix_handler_function_t *msix_handler)
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
static clib_error_t * vfio_set_irqs(vlib_main_t *vm, linux_pci_device_t *p, u32 index, u32 start, u32 count, u32 flags, int *efds)
clib_error_t * vlib_pci_bind_to_uio(vlib_main_t *vm, vlib_pci_addr_t *addr, char *uio_drv_name)
#define clib_error_report(e)
#define CLIB_MEM_VM_MAP_FAILED
vnet_hw_if_output_node_runtime_t * r
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
static int pci_addr_cmp(void *v1, void *v2)
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
#define clib_unix_warning(format, args...)
struct _pci_device_registration pci_device_registration_t
static clib_error_t * vlib_pci_intr_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
clib_error_t * vlib_pci_map_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
__clib_export clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
clib_error_t * vfio_map_physmem_page(vlib_main_t *vm, void *addr)
__clib_export clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
void init_device_from_registered(vlib_main_t *vm, vlib_pci_device_info_t *di)
static char * sysfs_mod_vfio_noiommu
void() pci_intx_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle)
static const char * sysfs_pci_dev_path
uword vlib_pci_get_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h)
#define clib_error_create(args...)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
linux_pci_region_t * regions
clib_error_t * vlib_pci_io_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
struct clib_bihash_value offset
template key/value backing page structure
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
linux_pci_device_type_t type
static clib_error_t * linux_pci_vfio_error_ready(clib_file_t *uf)
pci_intx_handler_function_t * intx_handler
#define PCI_COMMAND_MEMORY
sll srl srl sll sra u16x4 i
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
manual_print typedef address
int vlib_pci_supports_virtual_addr_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h)
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_main_t *vm, vlib_pci_addr_t *addr, clib_error_t **error)
clib_error_t * vlib_pci_register_intx_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, pci_intx_handler_function_t *intx_handler)
clib_error_t * vlib_pci_read_write_config(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
@ LINUX_PCI_DEVICE_TYPE_UNKNOWN
u32 vlib_pci_get_num_msix_interrupts(vlib_main_t *vm, vlib_pci_dev_handle_t h)
clib_error_t * vlib_pci_map_region_fixed(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
static clib_error_t * scan_pci_addr(void *arg, u8 *dev_dir_name, u8 *ignored)
static clib_error_t * linux_pci_uio_error_ready(clib_file_t *uf)
#define vec_free(V)
Free vector's memory (no header).
template key/value backing page structure
vlib_buffer_pool_t * buffer_pools
description fragment has unexpected format
static clib_error_t * add_device_uio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header,...
#define clib_strncmp(s1, s2, n)
#define VLIB_INIT_FUNCTION(x)
static clib_error_t * linux_pci_vfio_intx_read_ready(clib_file_t *uf)
linux_pci_main_t linux_pci_main
clib_error_t * linux_pci_init(vlib_main_t *vm)
#define vec_foreach(var, vec)
Vector iterator.
#define clib_error_return_unix(e, args...)
vlib_pci_dev_handle_t handle
static linux_pci_device_t * linux_pci_get_device(vlib_pci_dev_handle_t h)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
void vlib_log(vlib_log_level_t level, vlib_log_class_t class, char *fmt,...)
@ LINUX_PCI_DEVICE_TYPE_VFIO
static clib_error_t * linux_pci_vfio_unmask_intx(vlib_main_t *vm, linux_pci_device_t *d)
void() pci_msix_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle, u16 line)
static clib_error_t * linux_pci_vfio_msix_read_ready(clib_file_t *uf)
#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static vlib_main_t * vlib_get_main(void)
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
clib_error_t * vlib_pci_read_write_io(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword offset, void *data, u32 length)
uword vlib_pci_get_msix_file_index(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 index)
vlib_pci_addr_t * vlib_pci_get_addr(vlib_main_t *vm, vlib_pci_dev_handle_t h)
static void clib_file_del_by_index(clib_file_main_t *um, uword index)
clib_error_t * vlib_pci_device_open(vlib_main_t *vm, vlib_pci_addr_t *addr, pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
#define clib_error_free(e)
static clib_error_t * vlib_pci_map_region_int(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, u8 *addr, void **result)
static void pci_config_type1_little_to_host(pci_config_type1_regs_t *r)
clib_file_function_t * error_function
void vlib_pci_device_close(vlib_main_t *vm, vlib_pci_dev_handle_t h)
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
linux_pci_device_t * linux_pci_devices
static clib_error_t * add_device_vfio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
static void * clib_mem_alloc(uword size)
__clib_export uword unformat_bitmap_list(unformat_input_t *input, va_list *va)
unformat a list of bit ranges into a bitmap (eg "0-3,5-7,11" )
#define pci_log_debug(vm, dev, f,...)
clib_error_t * linux_vfio_group_get_device_fd(vlib_pci_addr_t *addr, int *fdp, int *is_noiommu)
linux_pci_irq_t * msix_irqs
vl_api_wireguard_peer_flags_t flags