4 * Copyright(c) 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.
39 #include "l3fwd_common.h"
42 * Update source and destination MAC addresses in the ethernet header.
43 * Perform RFC1812 checks and updates for IPV4 packets.
46 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
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 *);
57 ve[0] = val_eth[dst_port[0]];
58 te[0] = _mm_loadu_si128(p[0]);
60 ve[1] = val_eth[dst_port[1]];
61 te[1] = _mm_loadu_si128(p[1]);
63 ve[2] = val_eth[dst_port[2]];
64 te[2] = _mm_loadu_si128(p[2]);
66 ve[3] = val_eth[dst_port[3]];
67 te[3] = _mm_loadu_si128(p[3]);
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);
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]);
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);
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.
98 static inline uint16_t *
99 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
102 uint16_t u16[FWDSTEP + 1];
104 } *pnum = (void *)pn;
108 dp1 = _mm_cmpeq_epi16(dp1, dp2);
109 dp1 = _mm_unpacklo_epi16(dp1, dp1);
110 v = _mm_movemask_ps((__m128)dp1);
112 /* update last port counter. */
113 lp[0] += gptbl[v].lpv;
115 /* if dest port value has changed. */
117 pnum->u64 = gptbl[v].pnum;
118 pnum->u16[FWDSTEP] = 1;
119 lp = pnum->u16 + gptbl[v].idx;
126 * Process one packet:
127 * Update source and destination MAC addresses in the ethernet header.
128 * Perform RFC1812 checks and updates for IPV4 packets.
131 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
133 struct ether_hdr *eth_hdr;
136 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
138 te = _mm_loadu_si128((__m128i *)eth_hdr);
139 ve = val_eth[dst_port[0]];
141 rfc1812_process((struct ipv4_hdr *)(eth_hdr + 1), dst_port,
144 te = _mm_blend_epi16(te, ve, MASK_ETH);
145 _mm_storeu_si128((__m128i *)eth_hdr, te);
149 * Send packets burst from pkts_burst to the ports in dst_port array
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)
159 uint16_t pnum[MAX_PKT_BURST + 1];
162 * Finish packet processing and group consecutive
163 * packets with the same destination port.
165 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
172 processx4_step3(pkts_burst, dst_port);
174 /* dp1: <d[0], d[1], d[2], d[3], ... > */
175 dp1 = _mm_loadu_si128((__m128i *)dst_port);
177 for (j = FWDSTEP; j != k; j += FWDSTEP) {
178 processx4_step3(&pkts_burst[j], &dst_port[j]);
182 * <d[j-3], d[j-2], d[j-1], d[j], ... >
184 dp2 = _mm_loadu_si128((__m128i *)
185 &dst_port[j - FWDSTEP + 1]);
186 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
190 * <d[j], d[j+1], d[j+2], d[j+3], ... >
192 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
193 sizeof(dst_port[0]));
197 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
199 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
200 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
203 * remove values added by the last repeated
207 dlp = dst_port[j - 1];
209 /* set dlp and lp to the never used values. */
211 lp = pnum + MAX_PKT_BURST;
214 /* Process up to last 3 packets one by one. */
215 switch (nb_rx % FWDSTEP) {
217 process_packet(pkts_burst[j], dst_port + j);
218 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
222 process_packet(pkts_burst[j], dst_port + j);
223 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
227 process_packet(pkts_burst[j], dst_port + j);
228 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
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.
239 for (j = 0; j < nb_rx; j += k) {
247 if (likely(pn != BAD_PORT))
248 send_packetsx4(qconf, pn, pkts_burst + j, k);
250 for (m = j; m != j + k; m++)
251 rte_pktmbuf_free(pkts_burst[m]);
256 #endif /* _L3FWD_SSE_H_ */