]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/examples/l3fwd/l3fwd.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / dpdk / examples / l3fwd / l3fwd.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
3 */
4
5 #ifndef __L3_FWD_H__
6 #define __L3_FWD_H__
7
8 #include <rte_vect.h>
9
10 #define DO_RFC_1812_CHECKS
11
12 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
13
14 #if !defined(NO_HASH_MULTI_LOOKUP) && defined(RTE_MACHINE_CPUFLAG_NEON)
15 #define NO_HASH_MULTI_LOOKUP 1
16 #endif
17
18 #define MAX_PKT_BURST 32
19 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
20
21 #define MAX_RX_QUEUE_PER_LCORE 16
22
23 /*
24 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
25 */
26 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
27
28 #define NB_SOCKETS 8
29
30 /* Configure how many packets ahead to prefetch, when reading packets */
31 #define PREFETCH_OFFSET 3
32
33 /* Used to mark destination port as 'invalid'. */
34 #define BAD_PORT ((uint16_t)-1)
35
36 #define FWDSTEP 4
37
38 /* replace first 12B of the ethernet header. */
39 #define MASK_ETH 0x3f
40
41 /* Hash parameters. */
42 #ifdef RTE_ARCH_64
43 /* default to 4 million hash entries (approx) */
44 #define L3FWD_HASH_ENTRIES (1024*1024*4)
45 #else
46 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
47 #define L3FWD_HASH_ENTRIES (1024*1024*1)
48 #endif
49 #define HASH_ENTRY_NUMBER_DEFAULT 4
50
51 struct mbuf_table {
52 uint16_t len;
53 struct rte_mbuf *m_table[MAX_PKT_BURST];
54 };
55
56 struct lcore_rx_queue {
57 uint16_t port_id;
58 uint8_t queue_id;
59 } __rte_cache_aligned;
60
61 struct lcore_conf {
62 uint16_t n_rx_queue;
63 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
64 uint16_t n_tx_port;
65 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
66 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
67 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
68 void *ipv4_lookup_struct;
69 void *ipv6_lookup_struct;
70 } __rte_cache_aligned;
71
72 extern volatile bool force_quit;
73
74 /* ethernet addresses of ports */
75 extern uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
76 extern struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
77
78 /* mask of enabled ports */
79 extern uint32_t enabled_port_mask;
80
81 /* Used only in exact match mode. */
82 extern int ipv6; /**< ipv6 is false by default. */
83 extern uint32_t hash_entry_number;
84
85 extern xmm_t val_eth[RTE_MAX_ETHPORTS];
86
87 extern struct lcore_conf lcore_conf[RTE_MAX_LCORE];
88
89 /* Send burst of packets on an output interface */
90 static inline int
91 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
92 {
93 struct rte_mbuf **m_table;
94 int ret;
95 uint16_t queueid;
96
97 queueid = qconf->tx_queue_id[port];
98 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
99
100 ret = rte_eth_tx_burst(port, queueid, m_table, n);
101 if (unlikely(ret < n)) {
102 do {
103 rte_pktmbuf_free(m_table[ret]);
104 } while (++ret < n);
105 }
106
107 return 0;
108 }
109
110 /* Enqueue a single packet, and send burst if queue is filled */
111 static inline int
112 send_single_packet(struct lcore_conf *qconf,
113 struct rte_mbuf *m, uint16_t port)
114 {
115 uint16_t len;
116
117 len = qconf->tx_mbufs[port].len;
118 qconf->tx_mbufs[port].m_table[len] = m;
119 len++;
120
121 /* enough pkts to be sent */
122 if (unlikely(len == MAX_PKT_BURST)) {
123 send_burst(qconf, MAX_PKT_BURST, port);
124 len = 0;
125 }
126
127 qconf->tx_mbufs[port].len = len;
128 return 0;
129 }
130
131 #ifdef DO_RFC_1812_CHECKS
132 static inline int
133 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
134 {
135 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
136 /*
137 * 1. The packet length reported by the Link Layer must be large
138 * enough to hold the minimum length legal IP datagram (20 bytes).
139 */
140 if (link_len < sizeof(struct ipv4_hdr))
141 return -1;
142
143 /* 2. The IP checksum must be correct. */
144 /* this is checked in H/W */
145
146 /*
147 * 3. The IP version number must be 4. If the version number is not 4
148 * then the packet may be another version of IP, such as IPng or
149 * ST-II.
150 */
151 if (((pkt->version_ihl) >> 4) != 4)
152 return -3;
153 /*
154 * 4. The IP header length field must be large enough to hold the
155 * minimum length legal IP datagram (20 bytes = 5 words).
156 */
157 if ((pkt->version_ihl & 0xf) < 5)
158 return -4;
159
160 /*
161 * 5. The IP total length field must be large enough to hold the IP
162 * datagram header, whose length is specified in the IP header length
163 * field.
164 */
165 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
166 return -5;
167
168 return 0;
169 }
170 #endif /* DO_RFC_1812_CHECKS */
171
172 /* Function pointers for LPM or EM functionality. */
173 void
174 setup_lpm(const int socketid);
175
176 void
177 setup_hash(const int socketid);
178
179 int
180 em_check_ptype(int portid);
181
182 int
183 lpm_check_ptype(int portid);
184
185 uint16_t
186 em_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
187 uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
188
189 uint16_t
190 lpm_cb_parse_ptype(uint16_t port, uint16_t queue, struct rte_mbuf *pkts[],
191 uint16_t nb_pkts, uint16_t max_pkts, void *user_param);
192
193 int
194 em_main_loop(__attribute__((unused)) void *dummy);
195
196 int
197 lpm_main_loop(__attribute__((unused)) void *dummy);
198
199 /* Return ipv4/ipv6 fwd lookup struct for LPM or EM. */
200 void *
201 em_get_ipv4_l3fwd_lookup_struct(const int socketid);
202
203 void *
204 em_get_ipv6_l3fwd_lookup_struct(const int socketid);
205
206 void *
207 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid);
208
209 void *
210 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid);
211
212 #endif /* __L3_FWD_H__ */