4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef __L3FWD_LPM_SSE_H__
35 #define __L3FWD_LPM_SSE_H__
37 static inline __attribute__((always_inline)) void
38 send_packetsx4(struct lcore_conf *qconf, uint8_t port,
39 struct rte_mbuf *m[], uint32_t num)
43 len = qconf->tx_mbufs[port].len;
46 * If TX buffer for that queue is empty, and we have enough packets,
47 * then send them straightway.
49 if (num >= MAX_TX_BURST && len == 0) {
50 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
51 if (unlikely(n < num)) {
53 rte_pktmbuf_free(m[n]);
60 * Put packets into TX buffer for that queue.
64 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
67 switch (n % FWDSTEP) {
70 qconf->tx_mbufs[port].m_table[len + j] = m[j];
73 qconf->tx_mbufs[port].m_table[len + j] = m[j];
76 qconf->tx_mbufs[port].m_table[len + j] = m[j];
79 qconf->tx_mbufs[port].m_table[len + j] = m[j];
86 /* enough pkts to be sent */
87 if (unlikely(len == MAX_PKT_BURST)) {
89 send_burst(qconf, MAX_PKT_BURST, port);
91 /* copy rest of the packets into the TX buffer. */
94 switch (len % FWDSTEP) {
97 qconf->tx_mbufs[port].m_table[j] = m[n + j];
100 qconf->tx_mbufs[port].m_table[j] = m[n + j];
103 qconf->tx_mbufs[port].m_table[j] = m[n + j];
106 qconf->tx_mbufs[port].m_table[j] = m[n + j];
112 qconf->tx_mbufs[port].len = len;
115 #ifdef DO_RFC_1812_CHECKS
117 #define IPV4_MIN_VER_IHL 0x45
118 #define IPV4_MAX_VER_IHL 0x4f
119 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
121 /* Minimum value of IPV4 total length (20B) in network byte order. */
122 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
125 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
126 * - The IP version number must be 4.
127 * - The IP header length field must be large enough to hold the
128 * minimum length legal IP datagram (20 bytes = 5 words).
129 * - The IP total length field must be large enough to hold the IP
130 * datagram header, whose length is specified in the IP header length
132 * If we encounter invalid IPV4 packet, then set destination port for it
135 static inline __attribute__((always_inline)) void
136 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
140 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
141 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
143 ipv4_hdr->time_to_live--;
144 ipv4_hdr->hdr_checksum++;
146 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
147 ((uint8_t)ipv4_hdr->total_length == 0 &&
148 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
155 #define rfc1812_process(mb, dp) do { } while (0)
156 #endif /* DO_RFC_1812_CHECKS */
158 static inline __attribute__((always_inline)) uint16_t
159 get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
160 uint32_t dst_ipv4, uint8_t portid)
163 struct ipv6_hdr *ipv6_hdr;
164 struct ether_hdr *eth_hdr;
166 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
167 if (rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
170 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
171 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
172 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
173 if (rte_lpm6_lookup(qconf->ipv6_lookup_struct,
174 ipv6_hdr->dst_addr, &next_hop) != 0)
184 process_packet(struct lcore_conf *qconf, struct rte_mbuf *pkt,
185 uint16_t *dst_port, uint8_t portid)
187 struct ether_hdr *eth_hdr;
188 struct ipv4_hdr *ipv4_hdr;
193 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
194 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
196 dst_ipv4 = ipv4_hdr->dst_addr;
197 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
198 dp = get_dst_port(qconf, pkt, dst_ipv4, portid);
200 te = _mm_loadu_si128((__m128i *)eth_hdr);
204 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
206 te = _mm_blend_epi16(te, ve, MASK_ETH);
207 _mm_storeu_si128((__m128i *)eth_hdr, te);
211 * Read packet_type and destination IPV4 addresses from 4 mbufs.
214 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
218 struct ipv4_hdr *ipv4_hdr;
219 struct ether_hdr *eth_hdr;
220 uint32_t x0, x1, x2, x3;
222 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
223 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
224 x0 = ipv4_hdr->dst_addr;
225 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
227 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
228 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
229 x1 = ipv4_hdr->dst_addr;
230 ipv4_flag[0] &= pkt[1]->packet_type;
232 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
233 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
234 x2 = ipv4_hdr->dst_addr;
235 ipv4_flag[0] &= pkt[2]->packet_type;
237 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
238 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
239 x3 = ipv4_hdr->dst_addr;
240 ipv4_flag[0] &= pkt[3]->packet_type;
242 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
246 * Lookup into LPM for destination port.
247 * If lookup fails, use incoming port (portid) as destination port.
250 processx4_step2(const struct lcore_conf *qconf,
254 struct rte_mbuf *pkt[FWDSTEP],
255 uint16_t dprt[FWDSTEP])
258 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
259 4, 5, 6, 7, 0, 1, 2, 3);
261 /* Byte swap 4 IPV4 addresses. */
262 dip = _mm_shuffle_epi8(dip, bswap_mask);
264 /* if all 4 packets are IPV4. */
265 if (likely(ipv4_flag)) {
266 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
269 dprt[0] = get_dst_port(qconf, pkt[0], dst.u32[0], portid);
270 dprt[1] = get_dst_port(qconf, pkt[1], dst.u32[1], portid);
271 dprt[2] = get_dst_port(qconf, pkt[2], dst.u32[2], portid);
272 dprt[3] = get_dst_port(qconf, pkt[3], dst.u32[3], portid);
277 * Update source and destination MAC addresses in the ethernet header.
278 * Perform RFC1812 checks and updates for IPV4 packets.
281 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
287 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
288 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
289 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
290 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
292 ve[0] = val_eth[dst_port[0]];
293 te[0] = _mm_loadu_si128(p[0]);
295 ve[1] = val_eth[dst_port[1]];
296 te[1] = _mm_loadu_si128(p[1]);
298 ve[2] = val_eth[dst_port[2]];
299 te[2] = _mm_loadu_si128(p[2]);
301 ve[3] = val_eth[dst_port[3]];
302 te[3] = _mm_loadu_si128(p[3]);
304 /* Update first 12 bytes, keep rest bytes intact. */
305 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
306 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
307 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
308 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
310 _mm_storeu_si128(p[0], te[0]);
311 _mm_storeu_si128(p[1], te[1]);
312 _mm_storeu_si128(p[2], te[2]);
313 _mm_storeu_si128(p[3], te[3]);
315 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
316 &dst_port[0], pkt[0]->packet_type);
317 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
318 &dst_port[1], pkt[1]->packet_type);
319 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
320 &dst_port[2], pkt[2]->packet_type);
321 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
322 &dst_port[3], pkt[3]->packet_type);
326 * We group consecutive packets with the same destionation port into one burst.
327 * To avoid extra latency this is done together with some other packet
328 * processing, but after we made a final decision about packet's destination.
329 * To do this we maintain:
330 * pnum - array of number of consecutive packets with the same dest port for
331 * each packet in the input burst.
332 * lp - pointer to the last updated element in the pnum.
333 * dlp - dest port value lp corresponds to.
336 #define GRPSZ (1 << FWDSTEP)
337 #define GRPMSK (GRPSZ - 1)
339 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
340 if (likely((dlp) == (dcp)[(idx)])) { \
343 (dlp) = (dcp)[idx]; \
344 (lp) = (pn) + (idx); \
350 * Group consecutive packets with the same destination port in bursts of 4.
351 * Suppose we have array of destionation ports:
352 * dst_port[] = {a, b, c, d,, e, ... }
353 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
354 * We doing 4 comparisions at once and the result is 4 bit mask.
355 * This mask is used as an index into prebuild array of pnum values.
357 static inline uint16_t *
358 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
360 static const struct {
361 uint64_t pnum; /* prebuild 4 values for pnum[]. */
362 int32_t idx; /* index for new last updated elemnet. */
363 uint16_t lpv; /* add value to the last updated element. */
366 /* 0: a != b, b != c, c != d, d != e */
367 .pnum = UINT64_C(0x0001000100010001),
372 /* 1: a == b, b != c, c != d, d != e */
373 .pnum = UINT64_C(0x0001000100010002),
378 /* 2: a != b, b == c, c != d, d != e */
379 .pnum = UINT64_C(0x0001000100020001),
384 /* 3: a == b, b == c, c != d, d != e */
385 .pnum = UINT64_C(0x0001000100020003),
390 /* 4: a != b, b != c, c == d, d != e */
391 .pnum = UINT64_C(0x0001000200010001),
396 /* 5: a == b, b != c, c == d, d != e */
397 .pnum = UINT64_C(0x0001000200010002),
402 /* 6: a != b, b == c, c == d, d != e */
403 .pnum = UINT64_C(0x0001000200030001),
408 /* 7: a == b, b == c, c == d, d != e */
409 .pnum = UINT64_C(0x0001000200030004),
414 /* 8: a != b, b != c, c != d, d == e */
415 .pnum = UINT64_C(0x0002000100010001),
420 /* 9: a == b, b != c, c != d, d == e */
421 .pnum = UINT64_C(0x0002000100010002),
426 /* 0xa: a != b, b == c, c != d, d == e */
427 .pnum = UINT64_C(0x0002000100020001),
432 /* 0xb: a == b, b == c, c != d, d == e */
433 .pnum = UINT64_C(0x0002000100020003),
438 /* 0xc: a != b, b != c, c == d, d == e */
439 .pnum = UINT64_C(0x0002000300010001),
444 /* 0xd: a == b, b != c, c == d, d == e */
445 .pnum = UINT64_C(0x0002000300010002),
450 /* 0xe: a != b, b == c, c == d, d == e */
451 .pnum = UINT64_C(0x0002000300040001),
456 /* 0xf: a == b, b == c, c == d, d == e */
457 .pnum = UINT64_C(0x0002000300040005),
464 uint16_t u16[FWDSTEP + 1];
466 } *pnum = (void *)pn;
470 dp1 = _mm_cmpeq_epi16(dp1, dp2);
471 dp1 = _mm_unpacklo_epi16(dp1, dp1);
472 v = _mm_movemask_ps((__m128)dp1);
474 /* update last port counter. */
475 lp[0] += gptbl[v].lpv;
477 /* if dest port value has changed. */
479 lp = pnum->u16 + gptbl[v].idx;
481 pnum->u64 = gptbl[v].pnum;
488 * Buffer optimized handling of packets, invoked
492 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
493 uint8_t portid, struct lcore_conf *qconf)
498 uint16_t dst_port[MAX_PKT_BURST];
499 __m128i dip[MAX_PKT_BURST / FWDSTEP];
500 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
501 uint16_t pnum[MAX_PKT_BURST + 1];
503 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
504 for (j = 0; j != k; j += FWDSTEP) {
505 processx4_step1(&pkts_burst[j],
507 &ipv4_flag[j / FWDSTEP]);
510 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
511 for (j = 0; j != k; j += FWDSTEP) {
512 processx4_step2(qconf, dip[j / FWDSTEP],
513 ipv4_flag[j / FWDSTEP], portid,
514 &pkts_burst[j], &dst_port[j]);
518 * Finish packet processing and group consecutive
519 * packets with the same destination port.
521 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
528 processx4_step3(pkts_burst, dst_port);
530 /* dp1: <d[0], d[1], d[2], d[3], ... > */
531 dp1 = _mm_loadu_si128((__m128i *)dst_port);
533 for (j = FWDSTEP; j != k; j += FWDSTEP) {
534 processx4_step3(&pkts_burst[j], &dst_port[j]);
538 * <d[j-3], d[j-2], d[j-1], d[j], ... >
540 dp2 = _mm_loadu_si128((__m128i *)
541 &dst_port[j - FWDSTEP + 1]);
542 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
546 * <d[j], d[j+1], d[j+2], d[j+3], ... >
548 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
549 sizeof(dst_port[0]));
553 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
555 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
556 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
559 * remove values added by the last repeated
563 dlp = dst_port[j - 1];
565 /* set dlp and lp to the never used values. */
567 lp = pnum + MAX_PKT_BURST;
570 /* Process up to last 3 packets one by one. */
571 switch (nb_rx % FWDSTEP) {
573 process_packet(qconf, pkts_burst[j], dst_port + j, portid);
574 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
577 process_packet(qconf, pkts_burst[j], dst_port + j, portid);
578 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
581 process_packet(qconf, pkts_burst[j], dst_port + j, portid);
582 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
587 * Send packets out, through destination port.
588 * Consecuteve pacekts with the same destination port
589 * are already grouped together.
590 * If destination port for the packet equals BAD_PORT,
591 * then free the packet without sending it out.
593 for (j = 0; j < nb_rx; j += k) {
601 if (likely(pn != BAD_PORT)) {
602 send_packetsx4(qconf, pn, pkts_burst + j, k);
604 for (m = j; m != j + k; m++)
605 rte_pktmbuf_free(pkts_burst[m]);
610 #endif /* __L3FWD_LPM_SSE_H__ */