Skip to content

Commit 139d360

Browse files
committed
refactor ethernet code
1 parent 4da14f9 commit 139d360

23 files changed

+122
-46
lines changed

ans/ans_main.c

+81-28
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ static uint8_t ans_get_port_rx_queues_nb(const uint8_t port, struct ans_user_con
380380
}
381381
return (uint8_t)(++queue);
382382
}
383+
383384
/**********************************************************************
384385
*@description:
385386
*
@@ -391,24 +392,47 @@ static uint8_t ans_get_port_rx_queues_nb(const uint8_t port, struct ans_user_con
391392
*@return values:
392393
*
393394
**********************************************************************/
394-
static uint8_t ans_get_port_rx_qmapping(const uint8_t port, uint8_t qmapping_size, struct ans_port_qmapping *qmapping, struct ans_user_config *user_conf)
395+
static void ans_get_port_queue(const uint8_t port, struct ans_port_queue *port_queue, struct ans_lcore_queue *lcore_conf)
395396
{
396397
uint16_t i;
397-
uint8_t queue_nb = 0;
398+
uint8_t lcore_id;
399+
struct ans_lcore_queue *lcore_queue;
398400

399-
for (i = 0; i < user_conf->lcore_param_nb && queue_nb < qmapping_size; ++i)
401+
port_queue->rxq_nb = 0;
402+
port_queue->txq_nb = 0;
403+
404+
for(lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
400405
{
401-
if (user_conf->lcore_param[i].port_id == port)
406+
if (rte_lcore_is_enabled(lcore_id) == 0)
407+
continue;
408+
409+
lcore_queue = &lcore_conf[lcore_id];
410+
411+
/* get rx queue */
412+
for(i = 0; i < lcore_queue->n_rx_queue; i++)
413+
{
414+
if(lcore_queue->rx_queue[i].port_id == port)
415+
{
416+
port_queue->rx_qmapping[port_queue->rxq_nb].lcore_id = lcore_id;
417+
port_queue->rx_qmapping[port_queue->rxq_nb].queue_id = lcore_queue->rx_queue[i].queue_id;
418+
port_queue->rxq_nb++;
419+
}
420+
}
421+
422+
/* get tx queue */
423+
if(lcore_queue->tx_queue[port].queue_id != INVALID_QUEUE_ID)
402424
{
403-
qmapping[queue_nb].lcore_id = user_conf->lcore_param[i].lcore_id;
404-
qmapping[queue_nb].queue_id = user_conf->lcore_param[i].queue_id;
405-
queue_nb++;
425+
port_queue->tx_qmapping[port_queue->txq_nb].lcore_id = lcore_id;
426+
port_queue->tx_qmapping[port_queue->txq_nb].queue_id = lcore_queue->tx_queue[port].queue_id;
427+
port_queue->txq_nb++;
406428
}
429+
407430
}
408431

409-
return (queue_nb);
432+
return;
410433
}
411434

435+
412436
/**********************************************************************
413437
*@description:
414438
*
@@ -427,7 +451,6 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
427451
uint16_t queueid;
428452
unsigned lcore_id;
429453
uint8_t nb_rx_queue =0;
430-
uint8_t max_rx_queue =0;
431454
uint8_t queue, socketid;
432455
uint32_t n_tx_queue, nb_lcores, nb_mbuf;
433456
struct ether_addr eth_addr;
@@ -438,7 +461,7 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
438461
nb_lcores = rte_lcore_count();
439462
n_tx_queue = nb_lcores;
440463
if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
441-
n_tx_queue = MAX_TX_QUEUE_PER_PORT;
464+
n_tx_queue = MAX_TX_QUEUE_PER_PORT;
442465

443466
printf("\nStart to Init port \n" );
444467

@@ -461,9 +484,12 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
461484

462485
nb_rx_queue = ans_get_port_rx_queues_nb(portid, user_conf);
463486

464-
if(max_rx_queue < nb_rx_queue)
465-
max_rx_queue = nb_rx_queue;
466-
487+
/*
488+
if(dev_info.max_rx_queues < nb_rx_queue)
489+
{
490+
rte_exit(EXIT_FAILURE, "Cannot configure not existed rxq: ""port=%d\n", portid);
491+
}
492+
*/
467493
printf("\t Creating queues: rx queue number=%d tx queue number=%u... \n", nb_rx_queue, (unsigned)n_tx_queue );
468494

469495
ret = rte_eth_dev_configure(portid, nb_rx_queue, (uint16_t)n_tx_queue, &ans_port_conf);
@@ -482,8 +508,12 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
482508
for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
483509
{
484510
if (rte_lcore_is_enabled(lcore_id) == 0)
485-
continue;
486-
511+
{
512+
/* if lcore is enable, set as a invalid queue id */
513+
lcore_conf[lcore_id].tx_queue[portid].queue_id = INVALID_QUEUE_ID;
514+
continue;
515+
}
516+
487517
if (user_conf->numa_on)
488518
socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
489519
else
@@ -500,7 +530,7 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
500530
/* user default tx conf */
501531

502532
/* txconf = &ans_tx_conf; */
503-
txconf->txq_flags = 0; /* enable NIC all TX offload */
533+
txconf->txq_flags = 0; /* enable NIC all TX offload, shall set it to 0 for some nic to enable hw offload */
504534

505535
printf("\t lcore id:%u, tx queue id:%d, socket id:%d \n", lcore_id, queueid, socketid);
506536
printf("\t Conf-- tx pthresh:%d, tx hthresh:%d, tx wthresh:%d, txq_flags:0x%x \n", txconf->tx_thresh.pthresh,
@@ -510,9 +540,14 @@ static int ans_init_ports(unsigned short nb_ports, struct ans_user_config *user
510540
if (ret < 0)
511541
rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, " "port=%d\n", ret, portid);
512542

513-
lcore_conf[lcore_id].tx_queue[portid].queue_id = queueid;
514543

544+
struct ans_lcore_queue * qconf = &lcore_conf[lcore_id];
545+
qconf->tx_queue[portid].queue_id = queueid;
515546
queueid++;
547+
548+
qconf->port_id[qconf->n_tx_port] = portid;
549+
qconf->n_tx_port++;
550+
516551
}
517552

518553
printf("\n");
@@ -704,6 +739,21 @@ static inline int ans_send_packet(uint8_t port, struct rte_mbuf *m)
704739
return 0;
705740
}
706741

742+
/**********************************************************************
743+
*@description:
744+
*
745+
*
746+
*@parameters:
747+
* [in]:
748+
* [in]:
749+
*
750+
*@return values:
751+
*
752+
**********************************************************************/
753+
uint16_t ans_tx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
754+
{
755+
return rte_eth_tx_burst(port_id, queue_id, tx_pkts, nb_pkts);
756+
}
707757
/**********************************************************************
708758
*@description:
709759
*
@@ -763,8 +813,7 @@ static void ans_init_timer()
763813
**********************************************************************/
764814
static int ans_main_loop(__attribute__((unused)) void *dummy)
765815
{
766-
unsigned nb_ports;
767-
int i, j, nb_rx;
816+
int i, nb_rx;
768817
unsigned lcore_id;
769818
uint64_t prev_tsc, diff_tsc, cur_tsc;
770819
uint8_t portid, queueid;
@@ -795,8 +844,7 @@ static int ans_main_loop(__attribute__((unused)) void *dummy)
795844
RTE_LOG(INFO, USER8, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id, portid, queueid);
796845
}
797846

798-
nb_ports = rte_eth_dev_count();
799-
printf("nb ports %d hz: %ld \n", nb_ports, rte_get_tsc_hz());
847+
printf("hz: %ld \n", rte_get_tsc_hz());
800848

801849
timer_10ms_tsc = rte_get_tsc_hz() / 100;
802850

@@ -835,14 +883,18 @@ static int ans_main_loop(__attribute__((unused)) void *dummy)
835883
* This could be optimized (use queueid instead of
836884
* portid), but it is not called so often
837885
*/
838-
for (portid = 0; portid < nb_ports; portid++)
886+
for (i = 0; i < qconf->n_tx_port; i++)
839887
{
888+
portid = qconf->port_id[i];
889+
ans_eth_tx_flush(portid);
890+
/*
840891
tx_queue = &qconf->tx_queue[portid];
841892
if(tx_queue->pkts_nb == 0)
842893
continue;
843894
844895
ans_send_burst(portid, tx_queue->queue_id, tx_queue->pkts, tx_queue->pkts_nb);
845896
tx_queue->pkts_nb = 0;
897+
*/
846898
}
847899

848900
prev_tsc = cur_tsc;
@@ -882,7 +934,6 @@ static int ans_main_loop(__attribute__((unused)) void *dummy)
882934
**********************************************************************/
883935
static void ans_signal_handler(int signum)
884936
{
885-
int nb_ports;
886937
if (signum == SIGINT || signum == SIGTERM)
887938
{
888939
printf("\nSignal %d received, preparing to exit...\n", signum);
@@ -1002,7 +1053,7 @@ int main(int argc, char **argv)
10021053
}
10031054

10041055
init_conf.ip_sync = ans_user_conf.ipsync_on;
1005-
init_conf.port_send = ans_send_packet;
1056+
init_conf.port_send = ans_tx_burst;
10061057
init_conf.port_bypass = ans_bypass_packet;
10071058

10081059
ret = ans_initialize(&init_conf);
@@ -1017,7 +1068,6 @@ int main(int argc, char **argv)
10171068
uint16_t kni_id;
10181069
struct ether_addr eth_addr;
10191070
uint16_t qmapping_nb;
1020-
struct ans_port_qmapping qmapping[32];
10211071
struct rte_eth_dev_info dev_info;
10221072

10231073
for(portid= 0; portid < nb_ports; portid++)
@@ -1052,10 +1102,13 @@ int main(int argc, char **argv)
10521102

10531103
ans_iface_add(portid, kni_id, ifname, &eth_addr);
10541104

1055-
/* set port rx queue mapping */
1056-
qmapping_nb = ans_get_port_rx_qmapping(portid, 32, qmapping, &ans_user_conf);
1105+
/* set port queue mapping */
1106+
struct ans_port_queue port_queue;
1107+
ans_get_port_queue(portid, &port_queue, g_lcore_queue);
10571108

1058-
ans_iface_set_queue(ifname, qmapping_nb, qmapping);
1109+
ret = ans_iface_set_queue(ifname, &port_queue);
1110+
if (ret != 0)
1111+
rte_exit(EXIT_FAILURE, "set queue failed \n");
10591112

10601113
/* host byte order */
10611114
int ip_addr = 0x0a000002;

ans/ans_main.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
#define MAX_RX_QUEUE_PER_LCORE 16
7070
#define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
7171
#define MAX_RX_QUEUE_PER_PORT 128
72+
#define INVALID_QUEUE_ID 0xff
73+
7274

7375
#define CMD_LINE_OPT_CONFIG "config"
7476
#define CMD_LINE_OPT_NO_NUMA "no-numa"
@@ -122,6 +124,8 @@ struct ans_lcore_queue
122124
uint16_t n_rx_queue;
123125
struct ans_rx_queue rx_queue[MAX_RX_QUEUE_PER_LCORE];
124126

127+
uint16_t n_tx_port;
128+
uint16_t port_id[RTE_MAX_ETHPORTS];
125129
struct ans_tx_queue tx_queue[RTE_MAX_ETHPORTS];
126130

127131
} __rte_cache_aligned;
@@ -134,8 +138,8 @@ struct ans_lcore_queue
134138
/*
135139
* Configurable number of RX/TX ring descriptors
136140
*/
137-
#define ANS_RX_DESC_DEFAULT 128
138-
#define ANS_TX_DESC_DEFAULT 512
141+
#define ANS_RX_DESC_DEFAULT 1024
142+
#define ANS_TX_DESC_DEFAULT 2048
139143

140144
#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */
141145

librte_ans/include/ans_errno.h

+1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@
380380
#define ANS_EMZ 1002
381381
#define ANS_ESOCKET 1003
382382
#define ANS_ESEM 1004
383+
#define ANS_EMSGTYPE 1005
383384

384385
#define ANS_EIPFRAG 2000
385386

librte_ans/include/ans_init.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ struct ans_init_config
5656
cpu_set_t cpu_set; /**< system default cpu set */
5757
struct rte_mempool *pktmbuf_pool[ANS_MAX_NB_SOCKETS]; /**< mbuf pools for each sockets */
5858

59-
int (*port_send)(uint8_t port, struct rte_mbuf *m); /** callback for sending one mbuf to port */
60-
61-
int (*port_bypass)(uint8_t port, struct rte_mbuf *m); /** callback for bypassing one mbuf to linux */
59+
uint16_t (*port_send)(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **tx_pkts, uint16_t nb_pkts); /** callback for sending one mbuf to port */
6260

61+
int (*port_bypass)(uint8_t port, struct rte_mbuf *m); /** callback for bypassing one mbuf to linux */
6362

6463
} __rte_cache_aligned;
6564

librte_ans/include/ans_ip_intf.h

+32-13
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,25 @@
7272
*/
7373

7474
/*
75-
* define rx queue to lcore mapping
75+
* define queue id
7676
*/
77-
struct ans_port_qmapping
77+
struct ans_qmaping
7878
{
79+
uint8_t lcore_id;
7980
uint8_t queue_id;
80-
uint8_t lcore_id;
81+
};
82+
83+
/*
84+
* define queue to lcore mapping of port.
85+
*/
86+
87+
struct ans_port_queue
88+
{
89+
uint8_t rxq_nb;
90+
struct ans_qmaping rx_qmapping[RTE_MAX_LCORE];
91+
92+
uint8_t txq_nb;
93+
struct ans_qmaping tx_qmapping[RTE_MAX_LCORE];
8194
};
8295

8396
/**
@@ -95,6 +108,16 @@ struct ans_port_qmapping
95108
*/
96109
void ans_eth_rx_burst(uint8_t portid, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts);
97110

111+
/**
112+
* Send packets in the buffer.
113+
*
114+
* @param port_id
115+
* port id.
116+
*
117+
* @return
118+
*
119+
*/
120+
void ans_eth_tx_flush(uint16_t port_id);
98121

99122
/**
100123
* Statistics dropped packets by port
@@ -169,31 +192,27 @@ int ans_iface_set_mtu(char *if_name, uint16_t mtu);
169192
*
170193
* @param name
171194
* name of the interface for which the queue mapping is retrieved
172-
* @param qmapping_nb
173-
* queue_mapping array size, and also return the queue mapping number.
174-
* @param qmapping
175-
* rx queue to lcore mapping
195+
* @param port_queue
196+
* retrieved tx/rx queue of the port.
176197
*
177198
* @return 0 - SUCCESS, non-zero - FAILURE
178199
*
179200
*/
180-
int ans_iface_get_queue(char *if_name, uint8_t *qmapping_nb, struct ans_port_qmapping *qmapping);
201+
int ans_iface_get_queue(char *if_name, struct ans_port_queue *port_queue);
181202

182203

183204
/**
184205
* Set rx queue to lcore mapping for an interface.
185206
*
186207
* @param name
187208
* name of the interface for which the queue mapping is retrieved
188-
* @param qmapping_nb
189-
* queue_mapping array size.
190-
* @param qmapping
191-
* rx queue to lcore mapping
209+
* @param port_queue
210+
* tx/rx queue of the port.
192211
*
193212
* @return 0 - SUCCESS, non-zero - FAILURE
194213
*
195214
*/
196-
int ans_iface_set_queue(char *if_name, uint8_t qmapping_nb, struct ans_port_qmapping *qmapping);
215+
int ans_iface_set_queue(char *if_name, struct ans_port_queue *port_queue);
197216

198217
/**
199218
* Routing table addition.

librte_ans/librte_ans_broadwell.a

9.38 KB
Binary file not shown.

librte_ans/librte_ans_haswell.a

9.38 KB
Binary file not shown.

librte_ans/librte_ans_ivybridge.a

9.36 KB
Binary file not shown.

librte_ans/librte_ans_knl.a

9.15 KB
Binary file not shown.

librte_ans/librte_ans_sandybridge.a

9.36 KB
Binary file not shown.

librte_ans/librte_ans_westmere.a

9.36 KB
Binary file not shown.
7.41 KB
Binary file not shown.

librte_anscli/librte_anscli_haswell.a

7.41 KB
Binary file not shown.
7.41 KB
Binary file not shown.

librte_anscli/librte_anscli_knl.a

7.75 KB
Binary file not shown.
7.41 KB
Binary file not shown.
7.41 KB
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

librte_anssock/librte_anssock_knl.a

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)