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