app/testpmd: batch MAC swap for performance on x86
[dpdk.git] / app / test-pmd / macswap_sse.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #ifndef _MACSWAP_SSE_H_
6 #define _MACSWAP_SSE_H_
7
8 #include "macswap_common.h"
9
10 static inline void
11 do_macswap(struct rte_mbuf *pkts[], uint16_t nb,
12                 struct rte_port *txp)
13 {
14         struct ether_hdr *eth_hdr[4];
15         struct rte_mbuf *mb[4];
16         uint64_t ol_flags;
17         int i;
18         int r;
19         __m128i addr0, addr1, addr2, addr3;
20         /**
21          * shuffle mask be used to shuffle the 16 bytes.
22          * byte 0-5 wills be swapped with byte 6-11.
23          * byte 12-15 will keep unchanged.
24          */
25         __m128i shfl_msk = _mm_set_epi8(15, 14, 13, 12,
26                                         5, 4, 3, 2,
27                                         1, 0, 11, 10,
28                                         9, 8, 7, 6);
29
30         ol_flags = ol_flags_init(txp->dev_conf.txmode.offloads);
31         vlan_qinq_set(pkts, nb, ol_flags,
32                         txp->tx_vlan_id, txp->tx_vlan_id_outer);
33
34         i = 0;
35         r = nb;
36
37         while (r >= 4) {
38                 mb[0] = pkts[i++];
39                 eth_hdr[0] = rte_pktmbuf_mtod(mb[0], struct ether_hdr *);
40                 addr0 = _mm_loadu_si128((__m128i *)eth_hdr[0]);
41
42                 mb[1] = pkts[i++];
43                 eth_hdr[1] = rte_pktmbuf_mtod(mb[1], struct ether_hdr *);
44                 addr1 = _mm_loadu_si128((__m128i *)eth_hdr[1]);
45
46
47                 mb[2] = pkts[i++];
48                 eth_hdr[2] = rte_pktmbuf_mtod(mb[2], struct ether_hdr *);
49                 addr2 = _mm_loadu_si128((__m128i *)eth_hdr[2]);
50
51                 mb[3] = pkts[i++];
52                 eth_hdr[3] = rte_pktmbuf_mtod(mb[3], struct ether_hdr *);
53                 addr3 = _mm_loadu_si128((__m128i *)eth_hdr[3]);
54
55                 addr0 = _mm_shuffle_epi8(addr0, shfl_msk);
56                 addr1 = _mm_shuffle_epi8(addr1, shfl_msk);
57                 addr2 = _mm_shuffle_epi8(addr2, shfl_msk);
58                 addr3 = _mm_shuffle_epi8(addr3, shfl_msk);
59
60                 _mm_storeu_si128((__m128i *)eth_hdr[0], addr0);
61                 _mm_storeu_si128((__m128i *)eth_hdr[1], addr1);
62                 _mm_storeu_si128((__m128i *)eth_hdr[2], addr2);
63                 _mm_storeu_si128((__m128i *)eth_hdr[3], addr3);
64
65                 mbuf_field_set(mb[0], ol_flags);
66                 mbuf_field_set(mb[1], ol_flags);
67                 mbuf_field_set(mb[2], ol_flags);
68                 mbuf_field_set(mb[3], ol_flags);
69                 r -= 4;
70         }
71
72         for ( ; i < nb; i++) {
73                 if (i < nb - 1)
74                         rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1], void *));
75                 mb[0] = pkts[i];
76                 eth_hdr[0] = rte_pktmbuf_mtod(mb[0], struct ether_hdr *);
77
78                 /* Swap dest and src mac addresses. */
79                 addr0 = _mm_loadu_si128((__m128i *)eth_hdr);
80                 addr0 = _mm_shuffle_epi8(addr0, shfl_msk);
81                 _mm_storeu_si128((__m128i *)eth_hdr[0], addr0);
82
83                 mbuf_field_set(mb[0], ol_flags);
84         }
85 }
86
87 #endif /* _MACSWAP_SSE_H_ */