1133c2c65SJasvinder Singh /* SPDX-License-Identifier: BSD-3-Clause 2133c2c65SJasvinder Singh * Copyright(c) 2010-2018 Intel Corporation 3133c2c65SJasvinder Singh */ 4133c2c65SJasvinder Singh 5133c2c65SJasvinder Singh #include <stdlib.h> 6133c2c65SJasvinder Singh #include <string.h> 7133c2c65SJasvinder Singh 8133c2c65SJasvinder Singh #include <rte_ethdev.h> 97959831bSJasvinder Singh #include <rte_string_fns.h> 10133c2c65SJasvinder Singh 11133c2c65SJasvinder Singh #include "link.h" 12133c2c65SJasvinder Singh #include "mempool.h" 13133c2c65SJasvinder Singh 14133c2c65SJasvinder Singh static struct link_list link_list; 15133c2c65SJasvinder Singh 16133c2c65SJasvinder Singh int 17133c2c65SJasvinder Singh link_init(void) 18133c2c65SJasvinder Singh { 19133c2c65SJasvinder Singh TAILQ_INIT(&link_list); 20133c2c65SJasvinder Singh 21133c2c65SJasvinder Singh return 0; 22133c2c65SJasvinder Singh } 23133c2c65SJasvinder Singh 24133c2c65SJasvinder Singh struct link * 25133c2c65SJasvinder Singh link_find(const char *name) 26133c2c65SJasvinder Singh { 27133c2c65SJasvinder Singh struct link *link; 28133c2c65SJasvinder Singh 29133c2c65SJasvinder Singh if (name == NULL) 30133c2c65SJasvinder Singh return NULL; 31133c2c65SJasvinder Singh 32133c2c65SJasvinder Singh TAILQ_FOREACH(link, &link_list, node) 33133c2c65SJasvinder Singh if (strcmp(link->name, name) == 0) 34133c2c65SJasvinder Singh return link; 35133c2c65SJasvinder Singh 36133c2c65SJasvinder Singh return NULL; 37133c2c65SJasvinder Singh } 38133c2c65SJasvinder Singh 39ecfc2b1cSKevin Laatz struct link * 40ecfc2b1cSKevin Laatz link_next(struct link *link) 41ecfc2b1cSKevin Laatz { 42ecfc2b1cSKevin Laatz return (link == NULL) ? TAILQ_FIRST(&link_list) : TAILQ_NEXT(link, node); 43ecfc2b1cSKevin Laatz } 44ecfc2b1cSKevin Laatz 45133c2c65SJasvinder Singh static struct rte_eth_conf port_conf_default = { 46133c2c65SJasvinder Singh .link_speeds = 0, 47133c2c65SJasvinder Singh .rxmode = { 48133c2c65SJasvinder Singh .mq_mode = ETH_MQ_RX_NONE, 49*1bb4a528SFerruh Yigit .mtu = 9000 - (RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN), /* Jumbo frame MTU */ 50133c2c65SJasvinder Singh .split_hdr_size = 0, /* Header split buffer size */ 51133c2c65SJasvinder Singh }, 52133c2c65SJasvinder Singh .rx_adv_conf = { 53133c2c65SJasvinder Singh .rss_conf = { 54133c2c65SJasvinder Singh .rss_key = NULL, 55133c2c65SJasvinder Singh .rss_key_len = 40, 56133c2c65SJasvinder Singh .rss_hf = 0, 57133c2c65SJasvinder Singh }, 58133c2c65SJasvinder Singh }, 59133c2c65SJasvinder Singh .txmode = { 60133c2c65SJasvinder Singh .mq_mode = ETH_MQ_TX_NONE, 61133c2c65SJasvinder Singh }, 62133c2c65SJasvinder Singh .lpbk_mode = 0, 63133c2c65SJasvinder Singh }; 64133c2c65SJasvinder Singh 65133c2c65SJasvinder Singh #define RETA_CONF_SIZE (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE) 66133c2c65SJasvinder Singh 67133c2c65SJasvinder Singh static int 68133c2c65SJasvinder Singh rss_setup(uint16_t port_id, 69133c2c65SJasvinder Singh uint16_t reta_size, 70133c2c65SJasvinder Singh struct link_params_rss *rss) 71133c2c65SJasvinder Singh { 72133c2c65SJasvinder Singh struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE]; 73133c2c65SJasvinder Singh uint32_t i; 74133c2c65SJasvinder Singh int status; 75133c2c65SJasvinder Singh 76133c2c65SJasvinder Singh /* RETA setting */ 77133c2c65SJasvinder Singh memset(reta_conf, 0, sizeof(reta_conf)); 78133c2c65SJasvinder Singh 79133c2c65SJasvinder Singh for (i = 0; i < reta_size; i++) 80133c2c65SJasvinder Singh reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX; 81133c2c65SJasvinder Singh 82133c2c65SJasvinder Singh for (i = 0; i < reta_size; i++) { 83133c2c65SJasvinder Singh uint32_t reta_id = i / RTE_RETA_GROUP_SIZE; 84133c2c65SJasvinder Singh uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE; 85133c2c65SJasvinder Singh uint32_t rss_qs_pos = i % rss->n_queues; 86133c2c65SJasvinder Singh 87133c2c65SJasvinder Singh reta_conf[reta_id].reta[reta_pos] = 88133c2c65SJasvinder Singh (uint16_t) rss->queue_id[rss_qs_pos]; 89133c2c65SJasvinder Singh } 90133c2c65SJasvinder Singh 91133c2c65SJasvinder Singh /* RETA update */ 92133c2c65SJasvinder Singh status = rte_eth_dev_rss_reta_update(port_id, 93133c2c65SJasvinder Singh reta_conf, 94133c2c65SJasvinder Singh reta_size); 95133c2c65SJasvinder Singh 96133c2c65SJasvinder Singh return status; 97133c2c65SJasvinder Singh } 98133c2c65SJasvinder Singh 99133c2c65SJasvinder Singh struct link * 100133c2c65SJasvinder Singh link_create(const char *name, struct link_params *params) 101133c2c65SJasvinder Singh { 102133c2c65SJasvinder Singh struct rte_eth_dev_info port_info; 103133c2c65SJasvinder Singh struct rte_eth_conf port_conf; 104133c2c65SJasvinder Singh struct link *link; 105133c2c65SJasvinder Singh struct link_params_rss *rss; 106133c2c65SJasvinder Singh struct mempool *mempool; 107133c2c65SJasvinder Singh uint32_t cpu_id, i; 108133c2c65SJasvinder Singh int status; 109133c2c65SJasvinder Singh uint16_t port_id; 110133c2c65SJasvinder Singh 111133c2c65SJasvinder Singh /* Check input params */ 112133c2c65SJasvinder Singh if ((name == NULL) || 113133c2c65SJasvinder Singh link_find(name) || 114133c2c65SJasvinder Singh (params == NULL) || 115133c2c65SJasvinder Singh (params->rx.n_queues == 0) || 116133c2c65SJasvinder Singh (params->rx.queue_size == 0) || 117133c2c65SJasvinder Singh (params->tx.n_queues == 0) || 118133c2c65SJasvinder Singh (params->tx.queue_size == 0)) 119133c2c65SJasvinder Singh return NULL; 120133c2c65SJasvinder Singh 121133c2c65SJasvinder Singh port_id = params->port_id; 122133c2c65SJasvinder Singh if (params->dev_name) { 123133c2c65SJasvinder Singh status = rte_eth_dev_get_port_by_name(params->dev_name, 124133c2c65SJasvinder Singh &port_id); 125133c2c65SJasvinder Singh 126133c2c65SJasvinder Singh if (status) 127133c2c65SJasvinder Singh return NULL; 128133c2c65SJasvinder Singh } else 129133c2c65SJasvinder Singh if (!rte_eth_dev_is_valid_port(port_id)) 130133c2c65SJasvinder Singh return NULL; 131133c2c65SJasvinder Singh 132089e5ed7SIvan Ilchenko if (rte_eth_dev_info_get(port_id, &port_info) != 0) 133089e5ed7SIvan Ilchenko return NULL; 134133c2c65SJasvinder Singh 135133c2c65SJasvinder Singh mempool = mempool_find(params->rx.mempool_name); 136133c2c65SJasvinder Singh if (mempool == NULL) 137133c2c65SJasvinder Singh return NULL; 138133c2c65SJasvinder Singh 139133c2c65SJasvinder Singh rss = params->rx.rss; 140133c2c65SJasvinder Singh if (rss) { 141133c2c65SJasvinder Singh if ((port_info.reta_size == 0) || 142133c2c65SJasvinder Singh (port_info.reta_size > ETH_RSS_RETA_SIZE_512)) 143133c2c65SJasvinder Singh return NULL; 144133c2c65SJasvinder Singh 145133c2c65SJasvinder Singh if ((rss->n_queues == 0) || 146133c2c65SJasvinder Singh (rss->n_queues >= LINK_RXQ_RSS_MAX)) 147133c2c65SJasvinder Singh return NULL; 148133c2c65SJasvinder Singh 149133c2c65SJasvinder Singh for (i = 0; i < rss->n_queues; i++) 150133c2c65SJasvinder Singh if (rss->queue_id[i] >= port_info.max_rx_queues) 151133c2c65SJasvinder Singh return NULL; 152133c2c65SJasvinder Singh } 153133c2c65SJasvinder Singh 154133c2c65SJasvinder Singh /** 155133c2c65SJasvinder Singh * Resource create 156133c2c65SJasvinder Singh */ 157133c2c65SJasvinder Singh /* Port */ 158133c2c65SJasvinder Singh memcpy(&port_conf, &port_conf_default, sizeof(port_conf)); 159133c2c65SJasvinder Singh if (rss) { 160133c2c65SJasvinder Singh port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS; 1612e1141b6SCristian Dumitrescu port_conf.rx_adv_conf.rss_conf.rss_hf = 1622e1141b6SCristian Dumitrescu (ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) & 1632e1141b6SCristian Dumitrescu port_info.flow_type_rss_offloads; 164133c2c65SJasvinder Singh } 165133c2c65SJasvinder Singh 166133c2c65SJasvinder Singh cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id); 167133c2c65SJasvinder Singh if (cpu_id == (uint32_t) SOCKET_ID_ANY) 168133c2c65SJasvinder Singh cpu_id = 0; 169133c2c65SJasvinder Singh 170133c2c65SJasvinder Singh status = rte_eth_dev_configure( 171133c2c65SJasvinder Singh port_id, 172133c2c65SJasvinder Singh params->rx.n_queues, 173133c2c65SJasvinder Singh params->tx.n_queues, 174133c2c65SJasvinder Singh &port_conf); 175133c2c65SJasvinder Singh 176133c2c65SJasvinder Singh if (status < 0) 177133c2c65SJasvinder Singh return NULL; 178133c2c65SJasvinder Singh 179f430bbceSIvan Ilchenko if (params->promiscuous) { 180f430bbceSIvan Ilchenko status = rte_eth_promiscuous_enable(port_id); 181f430bbceSIvan Ilchenko if (status != 0) 182f430bbceSIvan Ilchenko return NULL; 183f430bbceSIvan Ilchenko } 184133c2c65SJasvinder Singh 185133c2c65SJasvinder Singh /* Port RX */ 186133c2c65SJasvinder Singh for (i = 0; i < params->rx.n_queues; i++) { 187133c2c65SJasvinder Singh status = rte_eth_rx_queue_setup( 188133c2c65SJasvinder Singh port_id, 189133c2c65SJasvinder Singh i, 190133c2c65SJasvinder Singh params->rx.queue_size, 191133c2c65SJasvinder Singh cpu_id, 192133c2c65SJasvinder Singh NULL, 193133c2c65SJasvinder Singh mempool->m); 194133c2c65SJasvinder Singh 195133c2c65SJasvinder Singh if (status < 0) 196133c2c65SJasvinder Singh return NULL; 197133c2c65SJasvinder Singh } 198133c2c65SJasvinder Singh 199133c2c65SJasvinder Singh /* Port TX */ 200133c2c65SJasvinder Singh for (i = 0; i < params->tx.n_queues; i++) { 201133c2c65SJasvinder Singh status = rte_eth_tx_queue_setup( 202133c2c65SJasvinder Singh port_id, 203133c2c65SJasvinder Singh i, 204133c2c65SJasvinder Singh params->tx.queue_size, 205133c2c65SJasvinder Singh cpu_id, 206133c2c65SJasvinder Singh NULL); 207133c2c65SJasvinder Singh 208133c2c65SJasvinder Singh if (status < 0) 209133c2c65SJasvinder Singh return NULL; 210133c2c65SJasvinder Singh } 211133c2c65SJasvinder Singh 212133c2c65SJasvinder Singh /* Port start */ 213133c2c65SJasvinder Singh status = rte_eth_dev_start(port_id); 214133c2c65SJasvinder Singh if (status < 0) 215133c2c65SJasvinder Singh return NULL; 216133c2c65SJasvinder Singh 217133c2c65SJasvinder Singh if (rss) { 218133c2c65SJasvinder Singh status = rss_setup(port_id, port_info.reta_size, rss); 219133c2c65SJasvinder Singh 220133c2c65SJasvinder Singh if (status) { 221133c2c65SJasvinder Singh rte_eth_dev_stop(port_id); 222133c2c65SJasvinder Singh return NULL; 223133c2c65SJasvinder Singh } 224133c2c65SJasvinder Singh } 225133c2c65SJasvinder Singh 226133c2c65SJasvinder Singh /* Port link up */ 227133c2c65SJasvinder Singh status = rte_eth_dev_set_link_up(port_id); 228133c2c65SJasvinder Singh if ((status < 0) && (status != -ENOTSUP)) { 229133c2c65SJasvinder Singh rte_eth_dev_stop(port_id); 230133c2c65SJasvinder Singh return NULL; 231133c2c65SJasvinder Singh } 232133c2c65SJasvinder Singh 233133c2c65SJasvinder Singh /* Node allocation */ 234133c2c65SJasvinder Singh link = calloc(1, sizeof(struct link)); 235133c2c65SJasvinder Singh if (link == NULL) { 236133c2c65SJasvinder Singh rte_eth_dev_stop(port_id); 237133c2c65SJasvinder Singh return NULL; 238133c2c65SJasvinder Singh } 239133c2c65SJasvinder Singh 240133c2c65SJasvinder Singh /* Node fill in */ 2417959831bSJasvinder Singh strlcpy(link->name, name, sizeof(link->name)); 242133c2c65SJasvinder Singh link->port_id = port_id; 243133c2c65SJasvinder Singh link->n_rxq = params->rx.n_queues; 244133c2c65SJasvinder Singh link->n_txq = params->tx.n_queues; 245133c2c65SJasvinder Singh 246133c2c65SJasvinder Singh /* Node add to list */ 247133c2c65SJasvinder Singh TAILQ_INSERT_TAIL(&link_list, link, node); 248133c2c65SJasvinder Singh 249133c2c65SJasvinder Singh return link; 250133c2c65SJasvinder Singh } 251133c2c65SJasvinder Singh 252133c2c65SJasvinder Singh int 253133c2c65SJasvinder Singh link_is_up(const char *name) 254133c2c65SJasvinder Singh { 255133c2c65SJasvinder Singh struct rte_eth_link link_params; 256133c2c65SJasvinder Singh struct link *link; 257133c2c65SJasvinder Singh 258133c2c65SJasvinder Singh /* Check input params */ 259133c2c65SJasvinder Singh if (name == NULL) 260133c2c65SJasvinder Singh return 0; 261133c2c65SJasvinder Singh 262133c2c65SJasvinder Singh link = link_find(name); 263133c2c65SJasvinder Singh if (link == NULL) 264133c2c65SJasvinder Singh return 0; 265133c2c65SJasvinder Singh 266133c2c65SJasvinder Singh /* Resource */ 26722e5c73bSIgor Romanov if (rte_eth_link_get(link->port_id, &link_params) < 0) 26822e5c73bSIgor Romanov return 0; 269133c2c65SJasvinder Singh 270133c2c65SJasvinder Singh return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1; 271133c2c65SJasvinder Singh } 272