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