|
FD.io VPP
v21.10.1-2-g0a485f517
Vector Packet Processing
|
Go to the documentation of this file.
19 #include <sys/types.h>
21 #include <sys/socket.h>
24 #include <linux/if_tun.h>
25 #include <sys/ioctl.h>
26 #include <linux/ethtool.h>
27 #include <linux/sockios.h>
28 #include <sys/eventfd.h>
29 #include <net/if_arp.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
48 #define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50 #define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
53 #define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
57 tap_log_err (vif, "%U", format_clib_error, err); \
69 #define TUN_MAX_PACKET_BYTES 65355
70 #define TUN_MIN_PACKET_BYTES 64
71 #define TUN_DEFAULT_PACKET_BYTES 1500
82 #define TAP_MAX_INSTANCE 1024
103 _IOCTL (vif->
tap_fds[0], TUNSETPERSIST, (
void *) (uintptr_t) 0);
132 int i, num_vhost_queues;
133 int old_netns_fd = -1;
134 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
135 struct ifreq get_ifr = {.ifr_flags = 0 };
140 unsigned int tap_features;
141 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
142 char *host_if_name = 0;
143 unsigned int offload = 0;
150 args->
rv = VNET_API_ERROR_INVALID_INTERFACE;
162 args->
rv = VNET_API_ERROR_UNSPECIFIED;
171 vif->
type = VIRTIO_IF_TYPE_TUN;
172 ifr.ifr_flags |= IFF_TUN;
182 sndbuf = INT_MAX - 1;
186 vif->
type = VIRTIO_IF_TYPE_TAP;
187 ifr.ifr_flags |= IFF_TAP;
200 args->
rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
209 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
216 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
231 if ((tfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
233 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
240 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
241 tap_log_dbg (vif,
"TUNGETFEATURES: features 0x%lx", tap_features);
242 if ((tap_features & IFF_VNET_HDR) == 0)
244 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
249 if ((tap_features & IFF_MULTI_QUEUE) == 0)
253 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
260 ifr.ifr_flags |= IFF_MULTI_QUEUE;
262 hdrsz =
sizeof (virtio_net_hdr_v1_t);
265 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
268 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
270 offload = TUN_F_CSUM;
274 _IOCTL (tfd, TUNSETIFF, (
void *) &ifr);
275 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", tfd,
276 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
282 host_if_name = ifr.ifr_ifrn.ifrn_name;
292 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 0);
299 _IOCTL (tfd, TUNSETPERSIST, (
void *) (uintptr_t) 1);
303 _IOCTL (tfd, TUNGETIFF, (
void *) &get_ifr);
304 tap_log_dbg (vif,
"TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
305 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
307 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
318 if ((qfd = open (
"/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
320 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
324 _IOCTL (qfd, TUNSETIFF, (
void *) &ifr);
325 tap_log_dbg (vif,
"TUNSETIFF fd %d name %s flags 0x%x", qfd,
326 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
332 tap_log_dbg (vif,
"TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
334 _IOCTL (vif->
tap_fds[
i], TUNSETVNETHDRSZ, &hdrsz);
338 _IOCTL (vif->
tap_fds[
i], TUNSETSNDBUF, &sndbuf);
342 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
344 if (fcntl (vif->
tap_fds[
i], F_SETFL, O_NONBLOCK) < 0)
354 for (
i = 0;
i < num_vhost_queues;
i++)
356 if ((vfd = open (
"/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
358 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_1;
374 args->
rv = VNET_API_ERROR_UNSUPPORTED;
376 "VIRTIO_NET_F_MRG_RXBUF feature");
383 args->
rv = VNET_API_ERROR_UNSUPPORTED;
385 "VIRTIO_RING_F_INDIRECT_DESC feature");
391 args->
rv = VNET_API_ERROR_UNSUPPORTED;
393 "VIRTIO_F_VERSION_1 features");
403 if (!(args->
tap_flags & TAP_FLAG_ATTACH))
412 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
421 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
426 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
433 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
441 if (vif->
type == VIRTIO_IF_TYPE_TAP)
449 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
460 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
473 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
485 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
493 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
502 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
512 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
523 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
533 args->
rv = VNET_API_ERROR_NETLINK_ERROR;
545 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_2;
552 for (
i = 0;
i < num_vhost_queues;
i++)
554 if (i < vif->num_rxqs && (args->
error =
558 args->
rv = VNET_API_ERROR_INIT_FAILED;
562 if (i < vif->num_txqs && (args->
error =
566 args->
rv = VNET_API_ERROR_INIT_FAILED;
583 "guest_phys_addr 0x%lx userspace_addr 0x%lx",
i,
589 for (
i = 0;
i < num_vhost_queues;
i++)
600 for (
i = 0;
i < num_vhost_queues * 2;
i++)
634 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
656 if (vif->
type == VIRTIO_IF_TYPE_TAP)
676 if (vif->
type != VIRTIO_IF_TYPE_TUN)
685 args->
rv = VNET_API_ERROR_INVALID_REGISTRATION;
695 tun_device_hw_interface_class.index, vif->
dev_instance);
711 else if (args->
tap_flags & TAP_FLAG_CSUM_OFFLOAD)
717 && (args->
tap_flags & TAP_FLAG_GRO_COALESCE))
721 if (vif->
type == VIRTIO_IF_TYPE_TUN)
734 vif->
flags |= VIRTIO_IF_FLAG_ADMIN_UP;
751 args->
rv = VNET_API_ERROR_SYSCALL_ERROR_3;
759 if (old_netns_fd != -1)
760 close (old_netns_fd);
775 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
779 if ((vif->
type != VIRTIO_IF_TYPE_TAP) && (vif->
type != VIRTIO_IF_TYPE_TUN))
780 return VNET_API_ERROR_INVALID_INTERFACE;
786 if (vif->
type == VIRTIO_IF_TYPE_TAP)
811 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
815 const unsigned int csum_offload_on = TUN_F_CSUM;
816 const unsigned int csum_offload_off = 0;
817 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
819 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
841 clib_warning (
"Error %s checksum offload on sw_if_index %d",
842 enable_disable ?
"enabling" :
"disabling",
sw_if_index);
843 return VNET_API_ERROR_SYSCALL_ERROR_3;
850 int is_packet_coalesce)
862 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
866 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
867 const unsigned int gso_off = 0;
868 unsigned int offload = enable_disable ? gso_on : gso_off;
870 _IOCTL (vif->
tap_fds[
i], TUNSETOFFLOAD, offload);
880 if (is_packet_coalesce)
899 enable_disable ?
"enabling" :
"disabling",
sw_if_index);
900 return VNET_API_ERROR_SYSCALL_ERROR_3;
918 if ((vif->
type != VIRTIO_IF_TYPE_TAP)
919 && (vif->
type != VIRTIO_IF_TYPE_TUN))
962 *out_tapids = r_tapids;
988 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
991 clib_warning (
"ioctl (TUNSETCARRIER) returned %d", ret);
1029 int old_netns_fd = -1;
1033 struct ethtool_cmd ecmd;
1053 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1059 ecmd.cmd = ETHTOOL_GSET;
1063 ifr.ifr_data = (
void *) &ecmd;
1064 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1070 if (ethtool_cmd_speed (&ecmd) != speed)
1072 ecmd.cmd = ETHTOOL_SSET;
1073 ethtool_cmd_speed_set (&ecmd, speed);
1074 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1082 if (old_netns_fd != -1)
1088 close (old_netns_fd);
ip6_address_t host_ip6_addr
vnet_device_class_t virtio_device_class
ip4_address_t host_ip4_gw
#define VHOST_SET_VRING_KICK
clib_error_t * vnet_netlink_add_ip4_route(void *dst, u8 dst_len, void *gw)
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, vnet_sw_interface_flags_t flags)
static clib_error_t * tap_init(vlib_main_t *vm)
static clib_error_t * tap_mtu_config(vlib_main_t *vm, unformat_input_t *input)
void vnet_sw_interface_set_mtu(vnet_main_t *vnm, u32 sw_if_index, u32 mtu)
int tap_gso_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable, int is_packet_coalesce)
ip4_address_t host_ip4_addr
#define VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
vnet_hw_interface_capabilities_t caps
#define TX_QUEUE_ACCESS(X)
#define clib_memcpy(d, s, n)
void tap_create_if(vlib_main_t *vm, tap_create_if_args_t *args)
static uword pointer_to_uword(const void *p)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
clib_error_t * vnet_netlink_set_link_netns(int ifindex, int netns_fd, char *new_ifname)
#define VHOST_SET_VRING_ADDR
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
static void clib_mem_free(void *p)
#define TUN_MIN_PACKET_BYTES
#define clib_error_return(e, args...)
unsigned int if_nametoindex(const char *ifname)
u32 per_interface_next_index
#define pool_put(P, E)
Free an object E in pool P.
VNET_HW_INTERFACE_CLASS(tun_device_hw_interface_class, static)
static int ethernet_mac_address_is_zero(const u8 *mac)
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
clib_error_t * vnet_netlink_add_ip4_addr(int ifindex, void *addr, int pfx_len)
@ VNET_HW_INTERFACE_FLAG_LINK_UP
#define VIRTIO_FEATURE(X)
vl_api_dhcp_client_state_t state
clib_error_t * vnet_netlink_set_link_addr(int ifindex, u8 *mac)
ip6_address_t host_ip6_gw
__clib_export int clib_netns_open(u8 *netns_u8)
static uword clib_bitmap_first_clear(uword *ai)
Return the lowest numbered clear bit in a bitmap.
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
mac_address_t host_mac_addr
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
#define VHOST_SET_VRING_NUM
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM
#define VLIB_CONFIG_FUNCTION(x, n,...)
ip6_address_t host_ip6_addr
virtio_main_t virtio_main
void virtio_set_packet_coalesce(virtio_if_t *vif)
#define pool_foreach(VAR, POOL)
Iterate through pool.
int tap_csum_offload_enable_disable(vlib_main_t *vm, u32 sw_if_index, int enable_disable)
ip4_address_t host_ip4_addr
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
virtio_vring_t * txq_vrings
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
vlib_physmem_main_t physmem_main
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
ip6_address_t host_ip6_addr
#define VHOST_SET_VRING_CALL
clib_error_t * vnet_netlink_set_link_mtu(int ifindex, int mtu)
clib_error_t * vnet_netlink_set_link_state(int ifindex, int up)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
vnet_main_t * vnet_get_main(void)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO
vlib_log_class_t log_default
#define tap_log_dbg(dev, f,...)
#define VHOST_SET_MEM_TABLE
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define vec_foreach_index(var, v)
Iterate over vector indices.
void vnet_delete_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
#define VHOST_SET_FEATURES
ip4_address_t host_ip4_addr
int tap_delete_if(vlib_main_t *vm, u32 sw_if_index)
void virtio_set_net_hdr_size(virtio_if_t *vif)
__clib_export int clib_setns(int nfd)
#define vlib_log_debug(...)
#define VHOST_NET_SET_BACKEND
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap.
u32 max_supported_packet_bytes
#define VHOST_GET_FEATURES
#define vec_free(V)
Free vector's memory (no header).
clib_error_t * vnet_netlink_add_ip6_addr(int ifindex, void *addr, int pfx_len)
static void ethernet_mac_address_generate(u8 *mac)
#define virtio_log_debug(vif, f,...)
#define RX_QUEUE_ACCESS(X)
description fragment has unexpected format
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VLIB_INIT_FUNCTION(x)
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
TAP interface details struct.
u32 min_supported_packet_bytes
int tap_set_carrier(u32 hw_if_index, u32 carrier_up)
static void tap_free(vlib_main_t *vm, virtio_if_t *vif)
#define vec_foreach(var, vec)
Vector iterator.
#define clib_error_return_unix(e, args...)
#define tap_log_err(dev, f,...)
virtio_vring_t * rxq_vrings
clib_error_t * vnet_netlink_set_link_master(int ifindex, char *master_ifname)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM
@ VNET_HW_INTERFACE_CLASS_FLAG_P2P
a point 2 point interface
#define clib_warning(format, args...)
@ VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE
#define clib_error_free(e)
static u32 virtio_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
int tap_dump_ifs(tap_interface_details_t **out_tapids)
void virtio_vring_set_rx_queues(vlib_main_t *vm, virtio_if_t *vif)
static vlib_thread_main_t * vlib_get_thread_main()
#define TUN_DEFAULT_PACKET_BYTES
vl_api_interface_index_t sw_if_index
vhost_memory_region_t regions[0]
static void * clib_mem_alloc(uword size)
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
clib_error_t * vnet_netlink_add_ip6_route(void *dst, u8 dst_len, void *gw)
mac_address_t host_mac_addr
int tap_set_speed(u32 hw_if_index, u32 speed)
#define TUN_MAX_PACKET_BYTES
vl_api_wireguard_peer_flags_t flags