831760f02a719180cbec62a567a1e21cc1bbc068
[deb_dpdk.git] / examples / l3fwd / l3fwd_sse.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34
35 #ifndef _L3FWD_SSE_H_
36 #define _L3FWD_SSE_H_
37
38 #include "l3fwd.h"
39 #include "l3fwd_common.h"
40
41 /*
42  * Update source and destination MAC addresses in the ethernet header.
43  * Perform RFC1812 checks and updates for IPV4 packets.
44  */
45 static inline void
46 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
47 {
48         __m128i te[FWDSTEP];
49         __m128i ve[FWDSTEP];
50         __m128i *p[FWDSTEP];
51
52         p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
53         p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
54         p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
55         p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
56
57         ve[0] = val_eth[dst_port[0]];
58         te[0] = _mm_loadu_si128(p[0]);
59
60         ve[1] = val_eth[dst_port[1]];
61         te[1] = _mm_loadu_si128(p[1]);
62
63         ve[2] = val_eth[dst_port[2]];
64         te[2] = _mm_loadu_si128(p[2]);
65
66         ve[3] = val_eth[dst_port[3]];
67         te[3] = _mm_loadu_si128(p[3]);
68
69         /* Update first 12 bytes, keep rest bytes intact. */
70         te[0] =  _mm_blend_epi16(te[0], ve[0], MASK_ETH);
71         te[1] =  _mm_blend_epi16(te[1], ve[1], MASK_ETH);
72         te[2] =  _mm_blend_epi16(te[2], ve[2], MASK_ETH);
73         te[3] =  _mm_blend_epi16(te[3], ve[3], MASK_ETH);
74
75         _mm_storeu_si128(p[0], te[0]);
76         _mm_storeu_si128(p[1], te[1]);
77         _mm_storeu_si128(p[2], te[2]);
78         _mm_storeu_si128(p[3], te[3]);
79
80         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
81                 &dst_port[0], pkt[0]->packet_type);
82         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
83                 &dst_port[1], pkt[1]->packet_type);
84         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
85                 &dst_port[2], pkt[2]->packet_type);
86         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
87                 &dst_port[3], pkt[3]->packet_type);
88 }
89
90 /*
91  * Group consecutive packets with the same destination port in bursts of 4.
92  * Suppose we have array of destionation ports:
93  * dst_port[] = {a, b, c, d,, e, ... }
94  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
95  * We doing 4 comparisons at once and the result is 4 bit mask.
96  * This mask is used as an index into prebuild array of pnum values.
97  */
98 static inline uint16_t *
99 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
100 {
101         union {
102                 uint16_t u16[FWDSTEP + 1];
103                 uint64_t u64;
104         } *pnum = (void *)pn;
105
106         int32_t v;
107
108         dp1 = _mm_cmpeq_epi16(dp1, dp2);
109         dp1 = _mm_unpacklo_epi16(dp1, dp1);
110         v = _mm_movemask_ps((__m128)dp1);
111
112         /* update last port counter. */
113         lp[0] += gptbl[v].lpv;
114
115         /* if dest port value has changed. */
116         if (v != GRPMSK) {
117                 pnum->u64 = gptbl[v].pnum;
118                 pnum->u16[FWDSTEP] = 1;
119                 lp = pnum->u16 + gptbl[v].idx;
120         }
121
122         return lp;
123 }
124
125 /**
126  * Process one packet:
127  * Update source and destination MAC addresses in the ethernet header.
128  * Perform RFC1812 checks and updates for IPV4 packets.
129  */
130 static inline void
131 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
132 {
133         struct ether_hdr *eth_hdr;
134         __m128i te, ve;
135
136         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
137
138         te = _mm_loadu_si128((__m128i *)eth_hdr);
139         ve = val_eth[dst_port[0]];
140
141         rfc1812_process((struct ipv4_hdr *)(eth_hdr + 1), dst_port,
142                         pkt->packet_type);
143
144         te =  _mm_blend_epi16(te, ve, MASK_ETH);
145         _mm_storeu_si128((__m128i *)eth_hdr, te);
146 }
147
148 /**
149  * Send packets burst from pkts_burst to the ports in dst_port array
150  */
151 static __rte_always_inline void
152 send_packets_multi(struct lcore_conf *qconf, struct rte_mbuf **pkts_burst,
153                 uint16_t dst_port[MAX_PKT_BURST], int nb_rx)
154 {
155         int32_t k;
156         int j = 0;
157         uint16_t dlp;
158         uint16_t *lp;
159         uint16_t pnum[MAX_PKT_BURST + 1];
160
161         /*
162          * Finish packet processing and group consecutive
163          * packets with the same destination port.
164          */
165         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
166         if (k != 0) {
167                 __m128i dp1, dp2;
168
169                 lp = pnum;
170                 lp[0] = 1;
171
172                 processx4_step3(pkts_burst, dst_port);
173
174                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
175                 dp1 = _mm_loadu_si128((__m128i *)dst_port);
176
177                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
178                         processx4_step3(&pkts_burst[j], &dst_port[j]);
179
180                         /*
181                          * dp2:
182                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
183                          */
184                         dp2 = _mm_loadu_si128((__m128i *)
185                                         &dst_port[j - FWDSTEP + 1]);
186                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
187
188                         /*
189                          * dp1:
190                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
191                          */
192                         dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
193                                                 sizeof(dst_port[0]));
194                 }
195
196                 /*
197                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
198                  */
199                 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
200                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
201
202                 /*
203                  * remove values added by the last repeated
204                  * dst port.
205                  */
206                 lp[0]--;
207                 dlp = dst_port[j - 1];
208         } else {
209                 /* set dlp and lp to the never used values. */
210                 dlp = BAD_PORT - 1;
211                 lp = pnum + MAX_PKT_BURST;
212         }
213
214         /* Process up to last 3 packets one by one. */
215         switch (nb_rx % FWDSTEP) {
216         case 3:
217                 process_packet(pkts_burst[j], dst_port + j);
218                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
219                 j++;
220                 /* fall-through */
221         case 2:
222                 process_packet(pkts_burst[j], dst_port + j);
223                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
224                 j++;
225                 /* fall-through */
226         case 1:
227                 process_packet(pkts_burst[j], dst_port + j);
228                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
229                 j++;
230         }
231
232         /*
233          * Send packets out, through destination port.
234          * Consecutive packets with the same destination port
235          * are already grouped together.
236          * If destination port for the packet equals BAD_PORT,
237          * then free the packet without sending it out.
238          */
239         for (j = 0; j < nb_rx; j += k) {
240
241                 int32_t m;
242                 uint16_t pn;
243
244                 pn = dst_port[j];
245                 k = pnum[j];
246
247                 if (likely(pn != BAD_PORT))
248                         send_packetsx4(qconf, pn, pkts_burst + j, k);
249                 else
250                         for (m = j; m != j + k; m++)
251                                 rte_pktmbuf_free(pkts_burst[m]);
252
253         }
254 }
255
256 #endif /* _L3FWD_SSE_H_ */