xref: /f-stack/lib/ff_memory.h (revision 2317ada5)
1 /*
2  * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #ifndef __FSTACK_MEMORY_H
28 #define __FSTACK_MEMORY_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define MEMPOOL_CACHE_SIZE 256
35 
36 #define DISPATCH_RING_SIZE 2048
37 
38 #define MSG_RING_SIZE 32
39 
40 /*
41  * Configurable number of RX/TX ring descriptors
42  */
43 #define RX_QUEUE_SIZE 512
44 #define TX_QUEUE_SIZE 512
45 
46 /*
47  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
48  */
49 #define MAX_TX_BURST    (MAX_PKT_BURST / 2)
50 
51 #define NB_SOCKETS 8
52 
53 /* Configure how many packets ahead to prefetch, when reading packets */
54 #define PREFETCH_OFFSET    3
55 
56 #define MAX_RX_QUEUE_PER_LCORE 16
57 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
58 #define MAX_RX_QUEUE_PER_PORT 128
59 
60 struct ff_dpdk_if_context {
61     void *sc;
62     void *ifp;
63     uint16_t port_id;
64     struct ff_hw_features hw_features;
65 } __rte_cache_aligned;
66 
67 struct mbuf_table {
68     uint16_t len;
69     struct rte_mbuf *m_table[MAX_PKT_BURST];
70 #ifdef FF_USE_PAGE_ARRAY
71     void*            bsd_m_table[MAX_PKT_BURST];            // save bsd mbuf address which will be enquene into txring after NIC transmitted pkt.
72 #endif
73 };
74 
75 struct lcore_rx_queue {
76     uint16_t port_id;
77     uint16_t queue_id;
78 } __rte_cache_aligned;
79 
80 struct lcore_conf {
81     uint16_t proc_id;
82     uint16_t socket_id;
83     uint16_t nb_queue_list[RTE_MAX_ETHPORTS];
84     struct ff_port_cfg *port_cfgs;
85 
86     uint16_t nb_rx_queue;
87     struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
88     uint16_t nb_tx_port;
89     uint16_t tx_port_id[RTE_MAX_ETHPORTS];
90     uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
91     struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
92     //char *pcap[RTE_MAX_ETHPORTS];
93 } __rte_cache_aligned;
94 
95 #ifdef FF_USE_PAGE_ARRAY
96 //  mbuf_txring save mbuf which had bursted into NIC,  m_tables has same length with NIC dev's sw_ring.
97 //  Then when txring.m_table[x] is reused, the packet in txring.m_table[x] had been transmited by NIC.
98 //  that means the mbuf can be freed safely.
99 struct mbuf_txring{
100     void* m_table[TX_QUEUE_SIZE];
101     uint16_t head;        // next available element.
102 };
103 
104 void ff_init_ref_pool(int nb_mbuf, int socketid);
105 int ff_mmap_init();
106 int ff_if_send_onepkt(struct ff_dpdk_if_context *ctx, void *m, int total);
107 int ff_enq_tx_bsdmbuf(uint8_t portid, void *p_mbuf, int nb_segs);
108 #endif
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif
115 
116 
117