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