1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Intel Corporation.
3 * Copyright(c) 2017-2018 Linaro Limited.
7 #ifndef _L3FWD_COMMON_H_
8 #define _L3FWD_COMMON_H_
10 #ifdef DO_RFC_1812_CHECKS
12 #define IPV4_MIN_VER_IHL 0x45
13 #define IPV4_MAX_VER_IHL 0x4f
14 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
16 /* Minimum value of IPV4 total length (20B) in network byte order. */
17 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
20 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
21 * - The IP version number must be 4.
22 * - The IP header length field must be large enough to hold the
23 * minimum length legal IP datagram (20 bytes = 5 words).
24 * - The IP total length field must be large enough to hold the IP
25 * datagram header, whose length is specified in the IP header length
27 * If we encounter invalid IPV4 packet, then set destination port for it
30 static __rte_always_inline void
31 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
35 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
36 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
38 ipv4_hdr->time_to_live--;
39 ipv4_hdr->hdr_checksum++;
41 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
42 ((uint8_t)ipv4_hdr->total_length == 0 &&
43 ipv4_hdr->total_length < IPV4_MIN_LEN_BE))
50 #define rfc1812_process(mb, dp, ptype) do { } while (0)
51 #endif /* DO_RFC_1812_CHECKS */
54 * We group consecutive packets with the same destionation port into one burst.
55 * To avoid extra latency this is done together with some other packet
56 * processing, but after we made a final decision about packet's destination.
57 * To do this we maintain:
58 * pnum - array of number of consecutive packets with the same dest port for
59 * each packet in the input burst.
60 * lp - pointer to the last updated element in the pnum.
61 * dlp - dest port value lp corresponds to.
64 #define GRPSZ (1 << FWDSTEP)
65 #define GRPMSK (GRPSZ - 1)
67 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
68 if (likely((dlp) == (dcp)[(idx)])) { \
72 (lp) = (pn) + (idx); \
78 uint64_t pnum; /* prebuild 4 values for pnum[]. */
79 int32_t idx; /* index for new last updated elemnet. */
80 uint16_t lpv; /* add value to the last updated element. */
83 /* 0: a != b, b != c, c != d, d != e */
84 .pnum = UINT64_C(0x0001000100010001),
89 /* 1: a == b, b != c, c != d, d != e */
90 .pnum = UINT64_C(0x0001000100010002),
95 /* 2: a != b, b == c, c != d, d != e */
96 .pnum = UINT64_C(0x0001000100020001),
101 /* 3: a == b, b == c, c != d, d != e */
102 .pnum = UINT64_C(0x0001000100020003),
107 /* 4: a != b, b != c, c == d, d != e */
108 .pnum = UINT64_C(0x0001000200010001),
113 /* 5: a == b, b != c, c == d, d != e */
114 .pnum = UINT64_C(0x0001000200010002),
119 /* 6: a != b, b == c, c == d, d != e */
120 .pnum = UINT64_C(0x0001000200030001),
125 /* 7: a == b, b == c, c == d, d != e */
126 .pnum = UINT64_C(0x0001000200030004),
131 /* 8: a != b, b != c, c != d, d == e */
132 .pnum = UINT64_C(0x0002000100010001),
137 /* 9: a == b, b != c, c != d, d == e */
138 .pnum = UINT64_C(0x0002000100010002),
143 /* 0xa: a != b, b == c, c != d, d == e */
144 .pnum = UINT64_C(0x0002000100020001),
149 /* 0xb: a == b, b == c, c != d, d == e */
150 .pnum = UINT64_C(0x0002000100020003),
155 /* 0xc: a != b, b != c, c == d, d == e */
156 .pnum = UINT64_C(0x0002000300010001),
161 /* 0xd: a == b, b != c, c == d, d == e */
162 .pnum = UINT64_C(0x0002000300010002),
167 /* 0xe: a != b, b == c, c == d, d == e */
168 .pnum = UINT64_C(0x0002000300040001),
173 /* 0xf: a == b, b == c, c == d, d == e */
174 .pnum = UINT64_C(0x0002000300040005),
180 static __rte_always_inline void
181 send_packetsx4(struct lcore_conf *qconf, uint16_t port, struct rte_mbuf *m[],
186 len = qconf->tx_mbufs[port].len;
189 * If TX buffer for that queue is empty, and we have enough packets,
190 * then send them straightway.
192 if (num >= MAX_TX_BURST && len == 0) {
193 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
194 if (unlikely(n < num)) {
196 rte_pktmbuf_free(m[n]);
203 * Put packets into TX buffer for that queue.
207 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
210 switch (n % FWDSTEP) {
213 qconf->tx_mbufs[port].m_table[len + j] = m[j];
217 qconf->tx_mbufs[port].m_table[len + j] = m[j];
221 qconf->tx_mbufs[port].m_table[len + j] = m[j];
225 qconf->tx_mbufs[port].m_table[len + j] = m[j];
232 /* enough pkts to be sent */
233 if (unlikely(len == MAX_PKT_BURST)) {
235 send_burst(qconf, MAX_PKT_BURST, port);
237 /* copy rest of the packets into the TX buffer. */
240 switch (len % FWDSTEP) {
243 qconf->tx_mbufs[port].m_table[j] = m[n + j];
247 qconf->tx_mbufs[port].m_table[j] = m[n + j];
251 qconf->tx_mbufs[port].m_table[j] = m[n + j];
255 qconf->tx_mbufs[port].m_table[j] = m[n + j];
261 qconf->tx_mbufs[port].len = len;
264 #endif /* _L3FWD_COMMON_H_ */