From a237d1d8515d602e152546f182b1769fd31fa8b7 Mon Sep 17 00:00:00 2001 From: jiangheng Date: Sat, 18 Jan 2025 21:32:23 +0800 Subject: [PATCH] add xdp tx checksum/tso offload --- src/lstack/core/lstack_cfg.c | 20 ++++++++++---------- src/lstack/core/lstack_dpdk.c | 21 ++++++--------------- src/lstack/include/lstack_cfg.h | 8 ++++++++ src/lstack/include/lstack_dpdk.h | 1 - src/lstack/netif/lstack_ethdev.c | 11 +++++++++-- src/lstack/netif/lstack_vdev.c | 2 +- 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/lstack/core/lstack_cfg.c b/src/lstack/core/lstack_cfg.c index 3d49cc3..04ceb89 100644 --- a/src/lstack/core/lstack_cfg.c +++ b/src/lstack/core/lstack_cfg.c @@ -91,7 +91,6 @@ static int32_t parse_flow_bifurcation(void); static int32_t parse_stack_interrupt(void); static int32_t parse_stack_num(void); static int32_t parse_xdp_eth_name(void); -static bool xdp_eth_enabled(void); #define PARSE_ARG(_arg, _arg_string, _default_val, _min_val, _max_val, _ret) \ do { \ @@ -1491,6 +1490,7 @@ static int dpdk_dev_get_iface_name(char *vdev_str) char *iface_value = NULL; char *next_token = NULL; char vdev_str_cp[strlen(vdev_str) + 1]; + int idx = 0; /* To prevent the original string from being modified, use a copied string. */ if (strcpy_s(vdev_str_cp, sizeof(vdev_str_cp), vdev_str) != 0) { @@ -1498,7 +1498,15 @@ static int dpdk_dev_get_iface_name(char *vdev_str) return -1; } - token = strtok_s(vdev_str_cp, ",", &next_token); + while (vdev_str_cp[idx] == ' ') { + idx++; + } + + if (strncmp(&vdev_str_cp[idx], "net_af_xdp", strlen("net_af_xdp")) != 0) { + return 0; + } + + token = strtok_s(&vdev_str_cp[idx], ",", &next_token); while (token != NULL) { if (strncmp(token, VDEV_ARG_IFACE, strlen(VDEV_ARG_IFACE)) == 0) { iface_value = token + strlen(VDEV_ARG_IFACE) + 1; @@ -1535,11 +1543,3 @@ static int32_t parse_xdp_eth_name(void) return ret; } - -static bool xdp_eth_enabled(void) -{ - if (strlen(g_config_params.xdp_eth_name)) { - return true; - } - return false; -} diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c index 3023a6c..fcb78ca 100644 --- a/src/lstack/core/lstack_dpdk.c +++ b/src/lstack/core/lstack_dpdk.c @@ -52,7 +52,6 @@ struct eth_params { uint16_t port_id; - bool is_xdp; uint16_t nb_queues; uint16_t nb_rx_desc; @@ -155,7 +154,12 @@ struct rte_mempool *create_pktmbuf_mempool(const char *name, uint32_t nb_mbuf, } /* time stamp before pbuf_custom as priv_data */ - uint16_t private_size = RTE_ALIGN(sizeof(struct mbuf_private), RTE_CACHE_LINE_SIZE); + uint16_t private_size = sizeof(struct mbuf_private); + if (xdp_eth_enabled()) { + /* reserved for xdp metadata, see struct xsk_tx_metadata in /usr/include/linux/if_xdp.h */ + private_size += 24; + } + private_size = RTE_ALIGN(private_size, RTE_CACHE_LINE_SIZE); pool = rte_pktmbuf_pool_create(pool_name, nb_mbuf, mbuf_cache_size, private_size, MBUF_SZ, numa_id); if (pool == NULL) { LSTACK_LOG(ERR, LSTACK, "cannot create %s pool rte_err=%d\n", pool_name, rte_errno); @@ -400,16 +404,6 @@ static int eth_params_rss(struct rte_eth_conf *conf, struct rte_eth_dev_info *de return rss_enable; } -bool dpdk_nic_is_xdp(void) -{ - struct protocol_stack_group *stack_group = get_protocol_stack_group(); - /* eth_params is null in ltran mode */ - if (stack_group->eth_params == NULL) { - return false; - } - return stack_group->eth_params->is_xdp; -} - static int eth_params_init(struct eth_params *eth_params, uint16_t port_id, uint16_t nb_queues, int *rss_enable) { struct rte_eth_dev_info dev_info; @@ -439,9 +433,6 @@ static int eth_params_init(struct eth_params *eth_params, uint16_t port_id, uint eth_params->conf.intr_conf.rxq = get_global_cfg_params()->stack_interrupt; eth_params_checksum(ð_params->conf, &dev_info); - if (strcmp(dev_info.driver_name, "net_af_xdp") == 0) { - eth_params->is_xdp = true; - } if (!get_global_cfg_params()->tuple_filter) { *rss_enable = eth_params_rss(ð_params->conf, &dev_info); diff --git a/src/lstack/include/lstack_cfg.h b/src/lstack/include/lstack_cfg.h index 07a97cb..d59407b 100644 --- a/src/lstack/include/lstack_cfg.h +++ b/src/lstack/include/lstack_cfg.h @@ -165,6 +165,14 @@ static inline uint8_t use_ltran(void) return get_global_cfg_params()->use_ltran; } +static inline bool xdp_eth_enabled(void) +{ + if (strlen(get_global_cfg_params()->xdp_eth_name)) { + return true; + } + return false; +} + int cfg_init(void); int gazelle_param_init(int *argc, char **argv); int gazelle_copy_param(const char *param, bool is_double, int *argc, char argv[][PATH_MAX]); diff --git a/src/lstack/include/lstack_dpdk.h b/src/lstack/include/lstack_dpdk.h index c2142d6..6251be7 100644 --- a/src/lstack/include/lstack_dpdk.h +++ b/src/lstack/include/lstack_dpdk.h @@ -64,7 +64,6 @@ int32_t dpdk_init_lstack_kni(void); void dpdk_nic_xstats_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id); void dpdk_nic_features_get(struct gazelle_stack_dfx_data *dfx, uint16_t port_id); -bool dpdk_nic_is_xdp(void); uint32_t dpdk_pktmbuf_mempool_num(void); uint32_t dpdk_total_socket_memory(void); diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c index 315cced..3b859d2 100644 --- a/src/lstack/netif/lstack_ethdev.c +++ b/src/lstack/netif/lstack_ethdev.c @@ -364,14 +364,21 @@ static err_t eth_dev_init(struct netif *netif) netif->hwaddr_len = ETHER_ADDR_LEN; - if (dpdk_nic_is_xdp()) { + if (xdp_eth_enabled()) { netif_set_rxol_flags(netif, RTE_ETH_RX_OFFLOAD_TCP_CKSUM | RTE_ETH_RX_OFFLOAD_UDP_CKSUM | RTE_ETH_RX_OFFLOAD_IPV4_CKSUM); + netif_set_txol_flags(netif, RTE_ETH_TX_OFFLOAD_TCP_CKSUM | RTE_ETH_TX_OFFLOAD_TCP_TSO); + /* 16: see kernel MAX_SKB_FRAGS define in skbuff.h */ + netif_set_max_pbuf_frags(netif, 16); } else { netif_set_rxol_flags(netif, get_protocol_stack_group()->rx_offload); + netif_set_txol_flags(netif, get_protocol_stack_group()->tx_offload); + /* 40: dpdk pmd support 40 max segs */ + netif_set_max_pbuf_frags(netif, 40); } - netif_set_txol_flags(netif, get_protocol_stack_group()->tx_offload); + netif_set_min_tso_seglen(netif, 256); + if (get_global_cfg_params()->stack_mode_rtc) { netif_set_rtc_mode(netif); } diff --git a/src/lstack/netif/lstack_vdev.c b/src/lstack/netif/lstack_vdev.c index b1d1a1b..2eaeb1f 100644 --- a/src/lstack/netif/lstack_vdev.c +++ b/src/lstack/netif/lstack_vdev.c @@ -147,7 +147,7 @@ static uint32_t vdev_rx_poll(struct protocol_stack *stack, struct rte_mbuf **pkt } if (get_protocol_stack_group()->rx_offload == 0 || /* skip gro when tcp/ip cksum offloads disable */ - dpdk_nic_is_xdp() || /* kernel has done GRO */ + xdp_eth_enabled() || /* kernel has done GRO */ (get_global_cfg_params()->vlan_mode >= 0 && !(get_protocol_stack_group()->rx_offload & RTE_ETH_RX_OFFLOAD_VLAN_STRIP))) { return pkt_num; -- 2.33.0