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