Skip to content

Commit

Permalink
netif: refactor netif_rte_port_alloc with netif_alloc
Browse files Browse the repository at this point in the history
Signed-off-by: ywc689 <[email protected]>
  • Loading branch information
ywc689 committed Sep 13, 2024
1 parent 166b977 commit 257d89f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 90 deletions.
4 changes: 2 additions & 2 deletions include/netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ enum {

#define NETIF_ALIGN 32

#define NETIF_PORT_ID_INVALID 0xFF
#define NETIF_PORT_ID_INVALID NETIF_MAX_PORTS
#define NETIF_PORT_ID_ALL NETIF_PORT_ID_INVALID

#define NETIF_LCORE_ID_INVALID 0xFF
Expand Down Expand Up @@ -283,7 +283,7 @@ int netif_get_promisc(struct netif_port *dev, bool *promisc);
int netif_get_allmulticast(struct netif_port *dev, bool *allmulticast);
int netif_get_stats(struct netif_port *dev, struct rte_eth_stats *stats);
int netif_get_xstats(struct netif_port *dev, netif_nic_xstats_get_t **xstats);
struct netif_port *netif_alloc(size_t priv_size, const char *namefmt,
struct netif_port *netif_alloc(portid_t id, size_t priv_size, const char *namefmt,
unsigned int nrxq, unsigned int ntxq,
void (*setup)(struct netif_port *));
portid_t netif_port_count(void);
Expand Down
3 changes: 2 additions & 1 deletion src/ip_tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ static struct netif_port *tunnel_create(struct ip_tunnel_tab *tab,
if (!strlen(params.ifname))
snprintf(params.ifname, IFNAMSIZ, "%s%%d", ops->kind);

dev = netif_alloc(ops->priv_size, params.ifname, 1, 1, ops->setup);
dev = netif_alloc(NETIF_PORT_ID_INVALID, ops->priv_size, params.ifname,
1, 1, ops->setup);
if (!dev)
return NULL;

Expand Down
134 changes: 49 additions & 85 deletions src/netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -2766,8 +2766,6 @@ static inline void netif_lcore_cleanup(void)
}
}

/********************************************** kni *************************************************/

/* always update bond port macaddr and its KNI macaddr together */
static int update_bond_macaddr(struct netif_port *port)
{
Expand Down Expand Up @@ -2799,6 +2797,8 @@ static inline void free_mbufs(struct rte_mbuf **pkts, unsigned num)
}
}

/********************************************** kni *************************************************/

void kni_ingress(struct rte_mbuf *mbuf, struct netif_port *dev)
{
if (!kni_dev_exist(dev))
Expand Down Expand Up @@ -3224,7 +3224,7 @@ portid_t netif_port_count(void)
return port_id_end;
}

struct netif_port *netif_alloc(size_t priv_size, const char *namefmt,
struct netif_port *netif_alloc(portid_t id, size_t priv_size, const char *namefmt,
unsigned int nrxq, unsigned int ntxq,
void (*setup)(struct netif_port *))
{
Expand All @@ -3247,13 +3247,17 @@ struct netif_port *netif_alloc(size_t priv_size, const char *namefmt,
return NULL;
}

dev->id = netif_port_id_alloc();
if (id != NETIF_PORT_ID_INVALID && !netif_port_get(id))
dev->id = id;
else
dev->id = netif_port_id_alloc();

if (strstr(namefmt, "%d"))
snprintf(dev->name, sizeof(dev->name), namefmt, dev->id);
else
snprintf(dev->name, sizeof(dev->name), "%s", namefmt);

rte_rwlock_init(&dev->dev_lock);
dev->socket = SOCKET_ID_ANY;
dev->hw_header_len = sizeof(struct rte_ether_hdr); /* default */

Expand All @@ -3277,7 +3281,6 @@ struct netif_port *netif_alloc(size_t priv_size, const char *namefmt,
if (dev->mtu == 0)
dev->mtu = ETH_DATA_LEN;

rte_rwlock_init(&dev->dev_lock);
netif_mc_init(dev);

dev->in_ptr = rte_zmalloc(NULL, sizeof(struct inet_device), RTE_CACHE_LINE_SIZE);
Expand Down Expand Up @@ -3474,79 +3477,6 @@ static inline void setup_dev_of_flags(struct netif_port *port)
port->flag |= NETIF_PORT_FLAG_LLDP;
}

/* TODO: refactor it with netif_alloc */
static struct netif_port* netif_rte_port_alloc(portid_t id, int nrxq,
int ntxq, const struct rte_eth_conf *conf)
{
int ii;
struct netif_port *port;

port = rte_zmalloc("port", sizeof(struct netif_port) +
sizeof(union netif_bond), RTE_CACHE_LINE_SIZE);
if (!port) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
return NULL;
}

port->id = id;
port->bond = (union netif_bond *)(port + 1);
if (is_physical_port(id)) {
port->type = PORT_TYPE_GENERAL; /* update later in netif_rte_port_alloc */
port->netif_ops = &dpdk_netif_ops;
} else if (is_bond_port(id)) {
port->type = PORT_TYPE_BOND_MASTER;
port->netif_ops = &bond_netif_ops;
} else {
RTE_LOG(ERR, NETIF, "%s: invalid port id: %d\n", __func__, id);
rte_free(port);
return NULL;
}

if (port_name_alloc(id, port->name, sizeof(port->name)) != EDPVS_OK) {
RTE_LOG(ERR, NETIF, "%s: fail to get port name for port%d\n",
__func__, id);
rte_free(port);
return NULL;
}

port->nrxq = nrxq; // update after port_rx_queues_get();
port->ntxq = ntxq; // update after port_tx_queues_get();
port->socket = rte_eth_dev_socket_id(id);
port->hw_header_len = sizeof(struct rte_ether_hdr);
if (port->socket == SOCKET_ID_ANY)
port->socket = rte_socket_id();
port->mbuf_pool = pktmbuf_pool[port->socket];
rte_eth_macaddr_get((uint8_t)id, &port->addr); // bonding mac is zero here
rte_eth_dev_get_mtu((uint8_t)id, &port->mtu);
rte_eth_dev_info_get((uint8_t)id, &port->dev_info);
port->dev_conf = *conf;
rte_rwlock_init(&port->dev_lock);
netif_mc_init(port);

setup_dev_of_flags(port);

port->in_ptr = rte_zmalloc(NULL, sizeof(struct inet_device), RTE_CACHE_LINE_SIZE);
if (!port->in_ptr) {
RTE_LOG(ERR, NETIF, "%s: no memory\n", __func__);
rte_free(port);
return NULL;
}
port->in_ptr->dev = port;

for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
INIT_LIST_HEAD(&port->in_ptr->ifa_list[ii]);
INIT_LIST_HEAD(&port->in_ptr->ifm_list[ii]);
}

if (tc_init_dev(port) != EDPVS_OK) {
RTE_LOG(ERR, NETIF, "%s: fail to init TC\n", __func__);
rte_free(port);
return NULL;
}

return port;
}

struct netif_port* netif_port_get(portid_t id)
{
int hash = port_tab_hashkey(id);
Expand Down Expand Up @@ -4336,14 +4266,42 @@ static char *find_conf_kni_name(portid_t id)
return NULL;
}

static void dpdk_port_setup(struct netif_port *dev)
{
dev->type = PORT_TYPE_GENERAL;
dev->netif_ops = &dpdk_netif_ops;
dev->socket = rte_eth_dev_socket_id(dev->id);
dev->dev_conf = default_port_conf;
dev->bond = (union netif_bond *)(dev + 1);

rte_eth_macaddr_get(dev->id, &dev->addr);
rte_eth_dev_get_mtu(dev->id, &dev->mtu);
rte_eth_dev_info_get(dev->id, &dev->dev_info);
setup_dev_of_flags(dev);
}

static void bond_port_setup(struct netif_port *dev)
{
dev->type = PORT_TYPE_BOND_MASTER;
dev->netif_ops = &bond_netif_ops;
dev->socket = rte_eth_dev_socket_id(dev->id);
dev->dev_conf = default_port_conf;
dev->bond = (union netif_bond *)(dev + 1);

rte_eth_macaddr_get(dev->id, &dev->addr);
rte_eth_dev_get_mtu(dev->id, &dev->mtu);
rte_eth_dev_info_get(dev->id, &dev->dev_info);
setup_dev_of_flags(dev);
}

/* Allocate and register all DPDK ports available */
static void netif_port_init(void)
{
int nports, nports_cfg;
portid_t pid;
struct netif_port *port;
struct rte_eth_conf this_eth_conf;
char *kni_name;
char ifname[IFNAMSIZ];

nports = dpvs_rte_eth_dev_count();
if (nports <= 0)
Expand All @@ -4358,17 +4316,23 @@ static void netif_port_init(void)
port_tab_init();
port_ntab_init();

this_eth_conf = default_port_conf;

kni_init();

for (pid = 0; pid < nports; pid++) {
if (port_name_alloc(pid, ifname, sizeof(ifname)) != EDPVS_OK)
rte_exit(EXIT_FAILURE, "Port name allocation failed, exiting...\n");

/* queue number will be filled on device start */
port = netif_rte_port_alloc(pid, 0, 0, &this_eth_conf);
port = NULL;
if (is_physical_port(pid))
port = netif_alloc(pid, sizeof(union netif_bond), ifname, 0, 0, dpdk_port_setup);
else if (is_bond_port(pid))
port = netif_alloc(pid, sizeof(union netif_bond), ifname, 0, 0, bond_port_setup);
if (!port)
rte_exit(EXIT_FAILURE, "Port allocate fail, exiting...\n");
rte_exit(EXIT_FAILURE, "Port allocation failed, exiting...\n");

if (netif_port_register(port) < 0)
rte_exit(EXIT_FAILURE, "Port register fail, exiting...\n");
rte_exit(EXIT_FAILURE, "Port registration failed, exiting...\n");
}

if (relate_bonding_device() < 0)
Expand Down Expand Up @@ -4490,7 +4454,7 @@ int netif_vdevs_add(void)
__func__, bond_cfg->name, bond_cfg->mode, bond_cfg->numa_node);
return EDPVS_CALLBACKFAIL;
}
bond_cfg->port_id = ret; /* relate port_id with port_name, used by netif_rte_port_alloc */
bond_cfg->port_id = ret; /* relate port_id with port_name, used by port_name_alloc */
RTE_LOG(INFO, NETIF, "created bondig device %s: mode=%d, primary=%s, numa_node=%d\n",
bond_cfg->name, bond_cfg->mode, bond_cfg->primary, bond_cfg->numa_node);

Expand Down
4 changes: 2 additions & 2 deletions src/vlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ int vlan_add_dev(struct netif_port *real_dev, const char *ifname,
}

/* allocate and register netif device */
dev = netif_alloc(sizeof(struct vlan_dev_priv), name_buf,
real_dev->nrxq, real_dev->ntxq, vlan_setup);
dev = netif_alloc(NETIF_PORT_ID_INVALID, sizeof(struct vlan_dev_priv),
name_buf, real_dev->nrxq, real_dev->ntxq, vlan_setup);
if (!dev) {
err = EDPVS_NOMEM;
goto out;
Expand Down

0 comments on commit 257d89f

Please sign in to comment.