New upstream version 17.11.4
[deb_dpdk.git] / examples / l3fwd / l3fwd_em.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <stdbool.h>
45 #include <netinet/in.h>
46
47 #include <rte_debug.h>
48 #include <rte_ether.h>
49 #include <rte_ethdev.h>
50 #include <rte_cycles.h>
51 #include <rte_mbuf.h>
52 #include <rte_ip.h>
53 #include <rte_tcp.h>
54 #include <rte_udp.h>
55 #include <rte_hash.h>
56
57 #include "l3fwd.h"
58
59 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_CRC32)
60 #define EM_HASH_CRC 1
61 #endif
62
63 #ifdef EM_HASH_CRC
64 #include <rte_hash_crc.h>
65 #define DEFAULT_HASH_FUNC       rte_hash_crc
66 #else
67 #include <rte_jhash.h>
68 #define DEFAULT_HASH_FUNC       rte_jhash
69 #endif
70
71 #define IPV6_ADDR_LEN 16
72
73 struct ipv4_5tuple {
74         uint32_t ip_dst;
75         uint32_t ip_src;
76         uint16_t port_dst;
77         uint16_t port_src;
78         uint8_t  proto;
79 } __attribute__((__packed__));
80
81 union ipv4_5tuple_host {
82         struct {
83                 uint8_t  pad0;
84                 uint8_t  proto;
85                 uint16_t pad1;
86                 uint32_t ip_src;
87                 uint32_t ip_dst;
88                 uint16_t port_src;
89                 uint16_t port_dst;
90         };
91         xmm_t xmm;
92 };
93
94 #define XMM_NUM_IN_IPV6_5TUPLE 3
95
96 struct ipv6_5tuple {
97         uint8_t  ip_dst[IPV6_ADDR_LEN];
98         uint8_t  ip_src[IPV6_ADDR_LEN];
99         uint16_t port_dst;
100         uint16_t port_src;
101         uint8_t  proto;
102 } __attribute__((__packed__));
103
104 union ipv6_5tuple_host {
105         struct {
106                 uint16_t pad0;
107                 uint8_t  proto;
108                 uint8_t  pad1;
109                 uint8_t  ip_src[IPV6_ADDR_LEN];
110                 uint8_t  ip_dst[IPV6_ADDR_LEN];
111                 uint16_t port_src;
112                 uint16_t port_dst;
113                 uint64_t reserve;
114         };
115         xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
116 };
117
118
119
120 struct ipv4_l3fwd_em_route {
121         struct ipv4_5tuple key;
122         uint8_t if_out;
123 };
124
125 struct ipv6_l3fwd_em_route {
126         struct ipv6_5tuple key;
127         uint8_t if_out;
128 };
129
130 static struct ipv4_l3fwd_em_route ipv4_l3fwd_em_route_array[] = {
131         {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
132         {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
133         {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
134         {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1),  102, 12, IPPROTO_TCP}, 3},
135 };
136
137 static struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
138         {{
139         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
140         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
141         101, 11, IPPROTO_TCP}, 0},
142
143         {{
144         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
145         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
146         102, 12, IPPROTO_TCP}, 1},
147
148         {{
149         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
150         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
151         101, 11, IPPROTO_TCP}, 2},
152
153         {{
154         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
155         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
156         102, 12, IPPROTO_TCP}, 3},
157 };
158
159 struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
160 struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
161
162 static inline uint32_t
163 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
164                 uint32_t init_val)
165 {
166         const union ipv4_5tuple_host *k;
167         uint32_t t;
168         const uint32_t *p;
169
170         k = data;
171         t = k->proto;
172         p = (const uint32_t *)&k->port_src;
173
174 #ifdef EM_HASH_CRC
175         init_val = rte_hash_crc_4byte(t, init_val);
176         init_val = rte_hash_crc_4byte(k->ip_src, init_val);
177         init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
178         init_val = rte_hash_crc_4byte(*p, init_val);
179 #else
180         init_val = rte_jhash_1word(t, init_val);
181         init_val = rte_jhash_1word(k->ip_src, init_val);
182         init_val = rte_jhash_1word(k->ip_dst, init_val);
183         init_val = rte_jhash_1word(*p, init_val);
184 #endif
185
186         return init_val;
187 }
188
189 static inline uint32_t
190 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
191                 uint32_t init_val)
192 {
193         const union ipv6_5tuple_host *k;
194         uint32_t t;
195         const uint32_t *p;
196 #ifdef EM_HASH_CRC
197         const uint32_t  *ip_src0, *ip_src1, *ip_src2, *ip_src3;
198         const uint32_t  *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
199 #endif
200
201         k = data;
202         t = k->proto;
203         p = (const uint32_t *)&k->port_src;
204
205 #ifdef EM_HASH_CRC
206         ip_src0 = (const uint32_t *) k->ip_src;
207         ip_src1 = (const uint32_t *)(k->ip_src+4);
208         ip_src2 = (const uint32_t *)(k->ip_src+8);
209         ip_src3 = (const uint32_t *)(k->ip_src+12);
210         ip_dst0 = (const uint32_t *) k->ip_dst;
211         ip_dst1 = (const uint32_t *)(k->ip_dst+4);
212         ip_dst2 = (const uint32_t *)(k->ip_dst+8);
213         ip_dst3 = (const uint32_t *)(k->ip_dst+12);
214         init_val = rte_hash_crc_4byte(t, init_val);
215         init_val = rte_hash_crc_4byte(*ip_src0, init_val);
216         init_val = rte_hash_crc_4byte(*ip_src1, init_val);
217         init_val = rte_hash_crc_4byte(*ip_src2, init_val);
218         init_val = rte_hash_crc_4byte(*ip_src3, init_val);
219         init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
220         init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
221         init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
222         init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
223         init_val = rte_hash_crc_4byte(*p, init_val);
224 #else
225         init_val = rte_jhash_1word(t, init_val);
226         init_val = rte_jhash(k->ip_src,
227                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
228         init_val = rte_jhash(k->ip_dst,
229                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
230         init_val = rte_jhash_1word(*p, init_val);
231 #endif
232         return init_val;
233 }
234
235 #define IPV4_L3FWD_EM_NUM_ROUTES \
236         (sizeof(ipv4_l3fwd_em_route_array) / sizeof(ipv4_l3fwd_em_route_array[0]))
237
238 #define IPV6_L3FWD_EM_NUM_ROUTES \
239         (sizeof(ipv6_l3fwd_em_route_array) / sizeof(ipv6_l3fwd_em_route_array[0]))
240
241 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
242 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
243
244 static rte_xmm_t mask0;
245 static rte_xmm_t mask1;
246 static rte_xmm_t mask2;
247
248 #if defined(RTE_MACHINE_CPUFLAG_SSE2)
249 static inline xmm_t
250 em_mask_key(void *key, xmm_t mask)
251 {
252         __m128i data = _mm_loadu_si128((__m128i *)(key));
253
254         return _mm_and_si128(data, mask);
255 }
256 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
257 static inline xmm_t
258 em_mask_key(void *key, xmm_t mask)
259 {
260         int32x4_t data = vld1q_s32((int32_t *)key);
261
262         return vandq_s32(data, mask);
263 }
264 #elif defined(RTE_MACHINE_CPUFLAG_ALTIVEC)
265 static inline xmm_t
266 em_mask_key(void *key, xmm_t mask)
267 {
268         xmm_t data = vec_ld(0, (xmm_t *)(key));
269
270         return vec_and(data, mask);
271 }
272 #else
273 #error No vector engine (SSE, NEON, ALTIVEC) available, check your toolchain
274 #endif
275
276 static inline uint16_t
277 em_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
278 {
279         int ret = 0;
280         union ipv4_5tuple_host key;
281         struct rte_hash *ipv4_l3fwd_lookup_struct =
282                 (struct rte_hash *)lookup_struct;
283
284         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
285
286         /*
287          * Get 5 tuple: dst port, src port, dst IP address,
288          * src IP address and protocol.
289          */
290         key.xmm = em_mask_key(ipv4_hdr, mask0.x);
291
292         /* Find destination port */
293         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
294         return (ret < 0) ? portid : ipv4_l3fwd_out_if[ret];
295 }
296
297 static inline uint16_t
298 em_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
299 {
300         int ret = 0;
301         union ipv6_5tuple_host key;
302         struct rte_hash *ipv6_l3fwd_lookup_struct =
303                 (struct rte_hash *)lookup_struct;
304
305         ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
306         void *data0 = ipv6_hdr;
307         void *data1 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t);
308         void *data2 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t) + sizeof(xmm_t);
309
310         /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
311         key.xmm[0] = em_mask_key(data0, mask1.x);
312
313         /*
314          * Get part of 5 tuple: dst IP address lower 96 bits
315          * and src IP address higher 32 bits.
316          */
317         key.xmm[1] = *(xmm_t *)data1;
318
319         /*
320          * Get part of 5 tuple: dst port and src port
321          * and dst IP address higher 32 bits.
322          */
323         key.xmm[2] = em_mask_key(data2, mask2.x);
324
325         /* Find destination port */
326         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
327         return (ret < 0) ? portid : ipv6_l3fwd_out_if[ret];
328 }
329
330 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
331 #if defined(NO_HASH_MULTI_LOOKUP)
332 #include "l3fwd_em_sequential.h"
333 #else
334 #include "l3fwd_em_hlm.h"
335 #endif
336 #else
337 #include "l3fwd_em.h"
338 #endif
339
340 static void
341 convert_ipv4_5tuple(struct ipv4_5tuple *key1,
342                 union ipv4_5tuple_host *key2)
343 {
344         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
345         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
346         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
347         key2->port_src = rte_cpu_to_be_16(key1->port_src);
348         key2->proto = key1->proto;
349         key2->pad0 = 0;
350         key2->pad1 = 0;
351 }
352
353 static void
354 convert_ipv6_5tuple(struct ipv6_5tuple *key1,
355                 union ipv6_5tuple_host *key2)
356 {
357         uint32_t i;
358
359         for (i = 0; i < 16; i++) {
360                 key2->ip_dst[i] = key1->ip_dst[i];
361                 key2->ip_src[i] = key1->ip_src[i];
362         }
363         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
364         key2->port_src = rte_cpu_to_be_16(key1->port_src);
365         key2->proto = key1->proto;
366         key2->pad0 = 0;
367         key2->pad1 = 0;
368         key2->reserve = 0;
369 }
370
371 #define BYTE_VALUE_MAX 256
372 #define ALL_32_BITS 0xffffffff
373 #define BIT_8_TO_15 0x0000ff00
374
375 static inline void
376 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
377 {
378         uint32_t i;
379         int32_t ret;
380
381         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
382                                 ALL_32_BITS, ALL_32_BITS} };
383
384         for (i = 0; i < IPV4_L3FWD_EM_NUM_ROUTES; i++) {
385                 struct ipv4_l3fwd_em_route  entry;
386                 union ipv4_5tuple_host newkey;
387
388                 entry = ipv4_l3fwd_em_route_array[i];
389                 convert_ipv4_5tuple(&entry.key, &newkey);
390                 ret = rte_hash_add_key(h, (void *) &newkey);
391                 if (ret < 0) {
392                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
393                                 " to the l3fwd hash.\n", i);
394                 }
395                 ipv4_l3fwd_out_if[ret] = entry.if_out;
396         }
397         printf("Hash: Adding 0x%" PRIx64 " keys\n",
398                 (uint64_t)IPV4_L3FWD_EM_NUM_ROUTES);
399 }
400
401 #define BIT_16_TO_23 0x00ff0000
402 static inline void
403 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
404 {
405         uint32_t i;
406         int32_t ret;
407
408         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
409                                 ALL_32_BITS, ALL_32_BITS} };
410
411         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
412
413         for (i = 0; i < IPV6_L3FWD_EM_NUM_ROUTES; i++) {
414                 struct ipv6_l3fwd_em_route entry;
415                 union ipv6_5tuple_host newkey;
416
417                 entry = ipv6_l3fwd_em_route_array[i];
418                 convert_ipv6_5tuple(&entry.key, &newkey);
419                 ret = rte_hash_add_key(h, (void *) &newkey);
420                 if (ret < 0) {
421                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
422                                 " to the l3fwd hash.\n", i);
423                 }
424                 ipv6_l3fwd_out_if[ret] = entry.if_out;
425         }
426         printf("Hash: Adding 0x%" PRIx64 "keys\n",
427                 (uint64_t)IPV6_L3FWD_EM_NUM_ROUTES);
428 }
429
430 #define NUMBER_PORT_USED 4
431 static inline void
432 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
433                 unsigned int nr_flow)
434 {
435         unsigned i;
436
437         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
438                                 ALL_32_BITS, ALL_32_BITS} };
439
440         for (i = 0; i < nr_flow; i++) {
441                 struct ipv4_l3fwd_em_route entry;
442                 union ipv4_5tuple_host newkey;
443
444                 uint8_t a = (uint8_t)
445                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
446                 uint8_t b = (uint8_t)
447                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
448                 uint8_t c = (uint8_t)
449                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
450
451                 /* Create the ipv4 exact match flow */
452                 memset(&entry, 0, sizeof(entry));
453                 switch (i & (NUMBER_PORT_USED - 1)) {
454                 case 0:
455                         entry = ipv4_l3fwd_em_route_array[0];
456                         entry.key.ip_dst = IPv4(101, c, b, a);
457                         break;
458                 case 1:
459                         entry = ipv4_l3fwd_em_route_array[1];
460                         entry.key.ip_dst = IPv4(201, c, b, a);
461                         break;
462                 case 2:
463                         entry = ipv4_l3fwd_em_route_array[2];
464                         entry.key.ip_dst = IPv4(111, c, b, a);
465                         break;
466                 case 3:
467                         entry = ipv4_l3fwd_em_route_array[3];
468                         entry.key.ip_dst = IPv4(211, c, b, a);
469                         break;
470                 };
471                 convert_ipv4_5tuple(&entry.key, &newkey);
472                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
473
474                 if (ret < 0)
475                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
476
477                 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
478
479         }
480         printf("Hash: Adding 0x%x keys\n", nr_flow);
481 }
482
483 static inline void
484 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
485                 unsigned int nr_flow)
486 {
487         unsigned i;
488
489         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
490                                 ALL_32_BITS, ALL_32_BITS} };
491         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
492
493         for (i = 0; i < nr_flow; i++) {
494                 struct ipv6_l3fwd_em_route entry;
495                 union ipv6_5tuple_host newkey;
496
497                 uint8_t a = (uint8_t)
498                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
499                 uint8_t b = (uint8_t)
500                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
501                 uint8_t c = (uint8_t)
502                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
503
504                 /* Create the ipv6 exact match flow */
505                 memset(&entry, 0, sizeof(entry));
506                 switch (i & (NUMBER_PORT_USED - 1)) {
507                 case 0:
508                         entry = ipv6_l3fwd_em_route_array[0];
509                         break;
510                 case 1:
511                         entry = ipv6_l3fwd_em_route_array[1];
512                         break;
513                 case 2:
514                         entry = ipv6_l3fwd_em_route_array[2];
515                         break;
516                 case 3:
517                         entry = ipv6_l3fwd_em_route_array[3];
518                         break;
519                 };
520                 entry.key.ip_dst[13] = c;
521                 entry.key.ip_dst[14] = b;
522                 entry.key.ip_dst[15] = a;
523                 convert_ipv6_5tuple(&entry.key, &newkey);
524                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
525
526                 if (ret < 0)
527                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
528
529                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
530
531         }
532         printf("Hash: Adding 0x%x keys\n", nr_flow);
533 }
534
535 /* Requirements:
536  * 1. IP packets without extension;
537  * 2. L4 payload should be either TCP or UDP.
538  */
539 int
540 em_check_ptype(int portid)
541 {
542         int i, ret;
543         int ptype_l3_ipv4_ext = 0;
544         int ptype_l3_ipv6_ext = 0;
545         int ptype_l4_tcp = 0;
546         int ptype_l4_udp = 0;
547         uint32_t ptype_mask = RTE_PTYPE_L3_MASK | RTE_PTYPE_L4_MASK;
548
549         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
550         if (ret <= 0)
551                 return 0;
552
553         uint32_t ptypes[ret];
554
555         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
556         for (i = 0; i < ret; ++i) {
557                 switch (ptypes[i]) {
558                 case RTE_PTYPE_L3_IPV4_EXT:
559                         ptype_l3_ipv4_ext = 1;
560                         break;
561                 case RTE_PTYPE_L3_IPV6_EXT:
562                         ptype_l3_ipv6_ext = 1;
563                         break;
564                 case RTE_PTYPE_L4_TCP:
565                         ptype_l4_tcp = 1;
566                         break;
567                 case RTE_PTYPE_L4_UDP:
568                         ptype_l4_udp = 1;
569                         break;
570                 }
571         }
572
573         if (ptype_l3_ipv4_ext == 0)
574                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4_EXT\n", portid);
575         if (ptype_l3_ipv6_ext == 0)
576                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6_EXT\n", portid);
577         if (!ptype_l3_ipv4_ext || !ptype_l3_ipv6_ext)
578                 return 0;
579
580         if (ptype_l4_tcp == 0)
581                 printf("port %d cannot parse RTE_PTYPE_L4_TCP\n", portid);
582         if (ptype_l4_udp == 0)
583                 printf("port %d cannot parse RTE_PTYPE_L4_UDP\n", portid);
584         if (ptype_l4_tcp && ptype_l4_udp)
585                 return 1;
586
587         return 0;
588 }
589
590 static inline void
591 em_parse_ptype(struct rte_mbuf *m)
592 {
593         struct ether_hdr *eth_hdr;
594         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
595         uint16_t ether_type;
596         void *l3;
597         int hdr_len;
598         struct ipv4_hdr *ipv4_hdr;
599         struct ipv6_hdr *ipv6_hdr;
600
601         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
602         ether_type = eth_hdr->ether_type;
603         l3 = (uint8_t *)eth_hdr + sizeof(struct ether_hdr);
604         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
605                 ipv4_hdr = (struct ipv4_hdr *)l3;
606                 hdr_len = (ipv4_hdr->version_ihl & IPV4_HDR_IHL_MASK) *
607                           IPV4_IHL_MULTIPLIER;
608                 if (hdr_len == sizeof(struct ipv4_hdr)) {
609                         packet_type |= RTE_PTYPE_L3_IPV4;
610                         if (ipv4_hdr->next_proto_id == IPPROTO_TCP)
611                                 packet_type |= RTE_PTYPE_L4_TCP;
612                         else if (ipv4_hdr->next_proto_id == IPPROTO_UDP)
613                                 packet_type |= RTE_PTYPE_L4_UDP;
614                 } else
615                         packet_type |= RTE_PTYPE_L3_IPV4_EXT;
616         } else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
617                 ipv6_hdr = (struct ipv6_hdr *)l3;
618                 if (ipv6_hdr->proto == IPPROTO_TCP)
619                         packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
620                 else if (ipv6_hdr->proto == IPPROTO_UDP)
621                         packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
622                 else
623                         packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
624         }
625
626         m->packet_type = packet_type;
627 }
628
629 uint16_t
630 em_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
631                   struct rte_mbuf *pkts[], uint16_t nb_pkts,
632                   uint16_t max_pkts __rte_unused,
633                   void *user_param __rte_unused)
634 {
635         unsigned i;
636
637         for (i = 0; i < nb_pkts; ++i)
638                 em_parse_ptype(pkts[i]);
639
640         return nb_pkts;
641 }
642
643 /* main processing loop */
644 int
645 em_main_loop(__attribute__((unused)) void *dummy)
646 {
647         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
648         unsigned lcore_id;
649         uint64_t prev_tsc, diff_tsc, cur_tsc;
650         int i, nb_rx;
651         uint8_t queueid;
652         uint16_t portid;
653         struct lcore_conf *qconf;
654         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
655                 US_PER_S * BURST_TX_DRAIN_US;
656
657         prev_tsc = 0;
658
659         lcore_id = rte_lcore_id();
660         qconf = &lcore_conf[lcore_id];
661
662         if (qconf->n_rx_queue == 0) {
663                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
664                 return 0;
665         }
666
667         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
668
669         for (i = 0; i < qconf->n_rx_queue; i++) {
670
671                 portid = qconf->rx_queue_list[i].port_id;
672                 queueid = qconf->rx_queue_list[i].queue_id;
673                 RTE_LOG(INFO, L3FWD,
674                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
675                         lcore_id, portid, queueid);
676         }
677
678         while (!force_quit) {
679
680                 cur_tsc = rte_rdtsc();
681
682                 /*
683                  * TX burst queue drain
684                  */
685                 diff_tsc = cur_tsc - prev_tsc;
686                 if (unlikely(diff_tsc > drain_tsc)) {
687
688                         for (i = 0; i < qconf->n_tx_port; ++i) {
689                                 portid = qconf->tx_port_id[i];
690                                 if (qconf->tx_mbufs[portid].len == 0)
691                                         continue;
692                                 send_burst(qconf,
693                                         qconf->tx_mbufs[portid].len,
694                                         portid);
695                                 qconf->tx_mbufs[portid].len = 0;
696                         }
697
698                         prev_tsc = cur_tsc;
699                 }
700
701                 /*
702                  * Read packet from RX queues
703                  */
704                 for (i = 0; i < qconf->n_rx_queue; ++i) {
705                         portid = qconf->rx_queue_list[i].port_id;
706                         queueid = qconf->rx_queue_list[i].queue_id;
707                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
708                                 MAX_PKT_BURST);
709                         if (nb_rx == 0)
710                                 continue;
711
712 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
713                         l3fwd_em_send_packets(nb_rx, pkts_burst,
714                                                         portid, qconf);
715 #else
716                         l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
717                                                         portid, qconf);
718 #endif
719                 }
720         }
721
722         return 0;
723 }
724
725 /*
726  * Initialize exact match (hash) parameters.
727  */
728 void
729 setup_hash(const int socketid)
730 {
731         struct rte_hash_parameters ipv4_l3fwd_hash_params = {
732                 .name = NULL,
733                 .entries = L3FWD_HASH_ENTRIES,
734                 .key_len = sizeof(union ipv4_5tuple_host),
735                 .hash_func = ipv4_hash_crc,
736                 .hash_func_init_val = 0,
737         };
738
739         struct rte_hash_parameters ipv6_l3fwd_hash_params = {
740                 .name = NULL,
741                 .entries = L3FWD_HASH_ENTRIES,
742                 .key_len = sizeof(union ipv6_5tuple_host),
743                 .hash_func = ipv6_hash_crc,
744                 .hash_func_init_val = 0,
745         };
746
747         char s[64];
748
749         /* create ipv4 hash */
750         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
751         ipv4_l3fwd_hash_params.name = s;
752         ipv4_l3fwd_hash_params.socket_id = socketid;
753         ipv4_l3fwd_em_lookup_struct[socketid] =
754                 rte_hash_create(&ipv4_l3fwd_hash_params);
755         if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
756                 rte_exit(EXIT_FAILURE,
757                         "Unable to create the l3fwd hash on socket %d\n",
758                         socketid);
759
760         /* create ipv6 hash */
761         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
762         ipv6_l3fwd_hash_params.name = s;
763         ipv6_l3fwd_hash_params.socket_id = socketid;
764         ipv6_l3fwd_em_lookup_struct[socketid] =
765                 rte_hash_create(&ipv6_l3fwd_hash_params);
766         if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
767                 rte_exit(EXIT_FAILURE,
768                         "Unable to create the l3fwd hash on socket %d\n",
769                         socketid);
770
771         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
772                 /* For testing hash matching with a large number of flows we
773                  * generate millions of IP 5-tuples with an incremented dst
774                  * address to initialize the hash table. */
775                 if (ipv6 == 0) {
776                         /* populate the ipv4 hash */
777                         populate_ipv4_many_flow_into_table(
778                                 ipv4_l3fwd_em_lookup_struct[socketid],
779                                 hash_entry_number);
780                 } else {
781                         /* populate the ipv6 hash */
782                         populate_ipv6_many_flow_into_table(
783                                 ipv6_l3fwd_em_lookup_struct[socketid],
784                                 hash_entry_number);
785                 }
786         } else {
787                 /*
788                  * Use data in ipv4/ipv6 l3fwd lookup table
789                  * directly to initialize the hash table.
790                  */
791                 if (ipv6 == 0) {
792                         /* populate the ipv4 hash */
793                         populate_ipv4_few_flow_into_table(
794                                 ipv4_l3fwd_em_lookup_struct[socketid]);
795                 } else {
796                         /* populate the ipv6 hash */
797                         populate_ipv6_few_flow_into_table(
798                                 ipv6_l3fwd_em_lookup_struct[socketid]);
799                 }
800         }
801 }
802
803 /* Return ipv4/ipv6 em fwd lookup struct. */
804 void *
805 em_get_ipv4_l3fwd_lookup_struct(const int socketid)
806 {
807         return ipv4_l3fwd_em_lookup_struct[socketid];
808 }
809
810 void *
811 em_get_ipv6_l3fwd_lookup_struct(const int socketid)
812 {
813         return ipv6_l3fwd_em_lookup_struct[socketid];
814 }