examples: use common macro RTE_DIM
[dpdk.git] / examples / l3fwd / l3fwd_lpm.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 <sys/socket.h>
17 #include <arpa/inet.h>
18
19 #include <rte_debug.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev.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_lpm.h>
28 #include <rte_lpm6.h>
29
30 #include "l3fwd.h"
31 #include "l3fwd_event.h"
32
33 struct ipv4_l3fwd_lpm_route {
34         uint32_t ip;
35         uint8_t  depth;
36         uint8_t  if_out;
37 };
38
39 struct ipv6_l3fwd_lpm_route {
40         uint8_t ip[16];
41         uint8_t  depth;
42         uint8_t  if_out;
43 };
44
45 /* 198.18.0.0/16 are set aside for RFC2544 benchmarking (RFC5735). */
46 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
47         {RTE_IPV4(198, 18, 0, 0), 24, 0},
48         {RTE_IPV4(198, 18, 1, 0), 24, 1},
49         {RTE_IPV4(198, 18, 2, 0), 24, 2},
50         {RTE_IPV4(198, 18, 3, 0), 24, 3},
51         {RTE_IPV4(198, 18, 4, 0), 24, 4},
52         {RTE_IPV4(198, 18, 5, 0), 24, 5},
53         {RTE_IPV4(198, 18, 6, 0), 24, 6},
54         {RTE_IPV4(198, 18, 7, 0), 24, 7},
55 };
56
57 /* 2001:0200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180) */
58 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = {
59         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 48, 0},
60         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 48, 1},
61         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0}, 48, 2},
62         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0}, 48, 3},
63         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0}, 48, 4},
64         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0}, 48, 5},
65         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0}, 48, 6},
66         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0}, 48, 7},
67 };
68
69 #define IPV4_L3FWD_LPM_MAX_RULES         1024
70 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
71 #define IPV6_L3FWD_LPM_MAX_RULES         1024
72 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
73
74 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
75 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
76
77 static inline uint16_t
78 lpm_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
79 {
80         uint32_t next_hop;
81         struct rte_lpm *ipv4_l3fwd_lookup_struct =
82                 (struct rte_lpm *)lookup_struct;
83
84         return (uint16_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
85                 rte_be_to_cpu_32(((struct rte_ipv4_hdr *)ipv4_hdr)->dst_addr),
86                 &next_hop) == 0) ? next_hop : portid);
87 }
88
89 static inline uint16_t
90 lpm_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
91 {
92         uint32_t next_hop;
93         struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
94                 (struct rte_lpm6 *)lookup_struct;
95
96         return (uint16_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
97                         ((struct rte_ipv6_hdr *)ipv6_hdr)->dst_addr,
98                         &next_hop) == 0) ?  next_hop : portid);
99 }
100
101 static __rte_always_inline uint16_t
102 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
103                 uint16_t portid)
104 {
105         struct rte_ipv6_hdr *ipv6_hdr;
106         struct rte_ipv4_hdr *ipv4_hdr;
107         struct rte_ether_hdr *eth_hdr;
108
109         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
110
111                 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
112                 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
113
114                 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
115                                              qconf->ipv4_lookup_struct);
116         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
117
118                 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
119                 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
120
121                 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
122                                              qconf->ipv6_lookup_struct);
123         }
124
125         return portid;
126 }
127
128 /*
129  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
130  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
131  * header and dst_ipv4 value is not used.
132  */
133 static __rte_always_inline uint16_t
134 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
135         uint32_t dst_ipv4, uint16_t portid)
136 {
137         uint32_t next_hop;
138         struct rte_ipv6_hdr *ipv6_hdr;
139         struct rte_ether_hdr *eth_hdr;
140
141         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
142                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
143                                                    dst_ipv4, &next_hop) == 0)
144                                    ? next_hop : portid);
145
146         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
147
148                 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
149                 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
150
151                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
152                                 ipv6_hdr->dst_addr, &next_hop) == 0)
153                                 ? next_hop : portid);
154
155         }
156
157         return portid;
158 }
159
160 #if defined(RTE_ARCH_X86)
161 #include "l3fwd_lpm_sse.h"
162 #elif defined RTE_MACHINE_CPUFLAG_NEON
163 #include "l3fwd_lpm_neon.h"
164 #elif defined(RTE_ARCH_PPC_64)
165 #include "l3fwd_lpm_altivec.h"
166 #else
167 #include "l3fwd_lpm.h"
168 #endif
169
170 /* main processing loop */
171 int
172 lpm_main_loop(__attribute__((unused)) void *dummy)
173 {
174         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
175         unsigned lcore_id;
176         uint64_t prev_tsc, diff_tsc, cur_tsc;
177         int i, nb_rx;
178         uint16_t portid;
179         uint8_t queueid;
180         struct lcore_conf *qconf;
181         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
182                 US_PER_S * BURST_TX_DRAIN_US;
183
184         prev_tsc = 0;
185
186         lcore_id = rte_lcore_id();
187         qconf = &lcore_conf[lcore_id];
188
189         if (qconf->n_rx_queue == 0) {
190                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
191                 return 0;
192         }
193
194         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
195
196         for (i = 0; i < qconf->n_rx_queue; i++) {
197
198                 portid = qconf->rx_queue_list[i].port_id;
199                 queueid = qconf->rx_queue_list[i].queue_id;
200                 RTE_LOG(INFO, L3FWD,
201                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
202                         lcore_id, portid, queueid);
203         }
204
205         while (!force_quit) {
206
207                 cur_tsc = rte_rdtsc();
208
209                 /*
210                  * TX burst queue drain
211                  */
212                 diff_tsc = cur_tsc - prev_tsc;
213                 if (unlikely(diff_tsc > drain_tsc)) {
214
215                         for (i = 0; i < qconf->n_tx_port; ++i) {
216                                 portid = qconf->tx_port_id[i];
217                                 if (qconf->tx_mbufs[portid].len == 0)
218                                         continue;
219                                 send_burst(qconf,
220                                         qconf->tx_mbufs[portid].len,
221                                         portid);
222                                 qconf->tx_mbufs[portid].len = 0;
223                         }
224
225                         prev_tsc = cur_tsc;
226                 }
227
228                 /*
229                  * Read packet from RX queues
230                  */
231                 for (i = 0; i < qconf->n_rx_queue; ++i) {
232                         portid = qconf->rx_queue_list[i].port_id;
233                         queueid = qconf->rx_queue_list[i].queue_id;
234                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
235                                 MAX_PKT_BURST);
236                         if (nb_rx == 0)
237                                 continue;
238
239 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON \
240                          || defined RTE_ARCH_PPC_64
241                         l3fwd_lpm_send_packets(nb_rx, pkts_burst,
242                                                 portid, qconf);
243 #else
244                         l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
245                                                         portid, qconf);
246 #endif /* X86 */
247                 }
248         }
249
250         return 0;
251 }
252
253 static __rte_always_inline uint16_t
254 lpm_process_event_pkt(const struct lcore_conf *lconf, struct rte_mbuf *mbuf)
255 {
256         mbuf->port = lpm_get_dst_port(lconf, mbuf, mbuf->port);
257
258 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON \
259         || defined RTE_ARCH_PPC_64
260         process_packet(mbuf, &mbuf->port);
261 #else
262
263         struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf,
264                         struct rte_ether_hdr *);
265 #ifdef DO_RFC_1812_CHECKS
266         struct rte_ipv4_hdr *ipv4_hdr;
267         if (RTE_ETH_IS_IPV4_HDR(mbuf->packet_type)) {
268                 /* Handle IPv4 headers.*/
269                 ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf,
270                                 struct rte_ipv4_hdr *,
271                                 sizeof(struct rte_ether_hdr));
272
273                 if (is_valid_ipv4_pkt(ipv4_hdr, mbuf->pkt_len)
274                                 < 0) {
275                         mbuf->port = BAD_PORT;
276                         continue;
277                 }
278                 /* Update time to live and header checksum */
279                 --(ipv4_hdr->time_to_live);
280                 ++(ipv4_hdr->hdr_checksum);
281         }
282 #endif
283         /* dst addr */
284         *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[mbuf->port];
285
286         /* src addr */
287         rte_ether_addr_copy(&ports_eth_addr[mbuf->port],
288                         &eth_hdr->s_addr);
289 #endif
290         return mbuf->port;
291 }
292
293 static __rte_always_inline void
294 lpm_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
295                 const uint8_t flags)
296 {
297         const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
298         const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
299                 evt_rsrc->evq.nb_queues - 1];
300         const uint8_t event_d_id = evt_rsrc->event_d_id;
301         struct lcore_conf *lconf;
302         unsigned int lcore_id;
303         struct rte_event ev;
304
305         if (event_p_id < 0)
306                 return;
307
308         lcore_id = rte_lcore_id();
309         lconf = &lcore_conf[lcore_id];
310
311         RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
312         while (!force_quit) {
313                 if (!rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1, 0))
314                         continue;
315
316                 if (lpm_process_event_pkt(lconf, ev.mbuf) == BAD_PORT) {
317                         rte_pktmbuf_free(ev.mbuf);
318                         continue;
319                 }
320
321                 if (flags & L3FWD_EVENT_TX_ENQ) {
322                         ev.queue_id = tx_q_id;
323                         ev.op = RTE_EVENT_OP_FORWARD;
324                         while (rte_event_enqueue_burst(event_d_id, event_p_id,
325                                                 &ev, 1) && !force_quit)
326                                 ;
327                 }
328
329                 if (flags & L3FWD_EVENT_TX_DIRECT) {
330                         rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0);
331                         while (!rte_event_eth_tx_adapter_enqueue(event_d_id,
332                                                 event_p_id, &ev, 1, 0) &&
333                                         !force_quit)
334                                 ;
335                 }
336         }
337 }
338
339 static __rte_always_inline void
340 lpm_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
341                 const uint8_t flags)
342 {
343         const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
344         const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
345                 evt_rsrc->evq.nb_queues - 1];
346         const uint8_t event_d_id = evt_rsrc->event_d_id;
347         const uint16_t deq_len = evt_rsrc->deq_depth;
348         struct rte_event events[MAX_PKT_BURST];
349         struct lcore_conf *lconf;
350         unsigned int lcore_id;
351         int i, nb_enq, nb_deq;
352
353         if (event_p_id < 0)
354                 return;
355
356         lcore_id = rte_lcore_id();
357
358         lconf = &lcore_conf[lcore_id];
359
360         RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
361
362         while (!force_quit) {
363                 /* Read events from RX queues */
364                 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
365                                 events, deq_len, 0);
366                 if (nb_deq == 0) {
367                         rte_pause();
368                         continue;
369                 }
370
371                 for (i = 0; i < nb_deq; i++) {
372                         if (flags & L3FWD_EVENT_TX_ENQ) {
373                                 events[i].queue_id = tx_q_id;
374                                 events[i].op = RTE_EVENT_OP_FORWARD;
375                         }
376
377                         if (flags & L3FWD_EVENT_TX_DIRECT)
378                                 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
379                                                                  0);
380
381                         lpm_process_event_pkt(lconf, events[i].mbuf);
382                 }
383
384                 if (flags & L3FWD_EVENT_TX_ENQ) {
385                         nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
386                                         events, nb_deq);
387                         while (nb_enq < nb_deq && !force_quit)
388                                 nb_enq += rte_event_enqueue_burst(event_d_id,
389                                                 event_p_id, events + nb_enq,
390                                                 nb_deq - nb_enq);
391                 }
392
393                 if (flags & L3FWD_EVENT_TX_DIRECT) {
394                         nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
395                                         event_p_id, events, nb_deq, 0);
396                         while (nb_enq < nb_deq && !force_quit)
397                                 nb_enq += rte_event_eth_tx_adapter_enqueue(
398                                                 event_d_id, event_p_id,
399                                                 events + nb_enq,
400                                                 nb_deq - nb_enq, 0);
401                 }
402         }
403 }
404
405 static __rte_always_inline void
406 lpm_event_loop(struct l3fwd_event_resources *evt_rsrc,
407                  const uint8_t flags)
408 {
409         if (flags & L3FWD_EVENT_SINGLE)
410                 lpm_event_loop_single(evt_rsrc, flags);
411         if (flags & L3FWD_EVENT_BURST)
412                 lpm_event_loop_burst(evt_rsrc, flags);
413 }
414
415 int __rte_noinline
416 lpm_event_main_loop_tx_d(__attribute__((unused)) void *dummy)
417 {
418         struct l3fwd_event_resources *evt_rsrc =
419                                         l3fwd_get_eventdev_rsrc();
420
421         lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
422         return 0;
423 }
424
425 int __rte_noinline
426 lpm_event_main_loop_tx_d_burst(__attribute__((unused)) void *dummy)
427 {
428         struct l3fwd_event_resources *evt_rsrc =
429                                         l3fwd_get_eventdev_rsrc();
430
431         lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
432         return 0;
433 }
434
435 int __rte_noinline
436 lpm_event_main_loop_tx_q(__attribute__((unused)) void *dummy)
437 {
438         struct l3fwd_event_resources *evt_rsrc =
439                                         l3fwd_get_eventdev_rsrc();
440
441         lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
442         return 0;
443 }
444
445 int __rte_noinline
446 lpm_event_main_loop_tx_q_burst(__attribute__((unused)) void *dummy)
447 {
448         struct l3fwd_event_resources *evt_rsrc =
449                                         l3fwd_get_eventdev_rsrc();
450
451         lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
452         return 0;
453 }
454
455 void
456 setup_lpm(const int socketid)
457 {
458         struct rte_lpm6_config config;
459         struct rte_lpm_config config_ipv4;
460         unsigned i;
461         int ret;
462         char s[64];
463         char abuf[INET6_ADDRSTRLEN];
464
465         /* create the LPM table */
466         config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
467         config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
468         config_ipv4.flags = 0;
469         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
470         ipv4_l3fwd_lpm_lookup_struct[socketid] =
471                         rte_lpm_create(s, socketid, &config_ipv4);
472         if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
473                 rte_exit(EXIT_FAILURE,
474                         "Unable to create the l3fwd LPM table on socket %d\n",
475                         socketid);
476
477         /* populate the LPM table */
478         for (i = 0; i < RTE_DIM(ipv4_l3fwd_lpm_route_array); i++) {
479                 struct in_addr in;
480
481                 /* skip unused ports */
482                 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
483                                 enabled_port_mask) == 0)
484                         continue;
485
486                 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
487                         ipv4_l3fwd_lpm_route_array[i].ip,
488                         ipv4_l3fwd_lpm_route_array[i].depth,
489                         ipv4_l3fwd_lpm_route_array[i].if_out);
490
491                 if (ret < 0) {
492                         rte_exit(EXIT_FAILURE,
493                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
494                                 i, socketid);
495                 }
496
497                 in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip);
498                 printf("LPM: Adding route %s / %d (%d)\n",
499                        inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
500                         ipv4_l3fwd_lpm_route_array[i].depth,
501                         ipv4_l3fwd_lpm_route_array[i].if_out);
502         }
503
504         /* create the LPM6 table */
505         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
506
507         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
508         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
509         config.flags = 0;
510         ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
511                                 &config);
512         if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
513                 rte_exit(EXIT_FAILURE,
514                         "Unable to create the l3fwd LPM table on socket %d\n",
515                         socketid);
516
517         /* populate the LPM table */
518         for (i = 0; i < RTE_DIM(ipv6_l3fwd_lpm_route_array); i++) {
519
520                 /* skip unused ports */
521                 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
522                                 enabled_port_mask) == 0)
523                         continue;
524
525                 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
526                         ipv6_l3fwd_lpm_route_array[i].ip,
527                         ipv6_l3fwd_lpm_route_array[i].depth,
528                         ipv6_l3fwd_lpm_route_array[i].if_out);
529
530                 if (ret < 0) {
531                         rte_exit(EXIT_FAILURE,
532                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
533                                 i, socketid);
534                 }
535
536                 printf("LPM: Adding route %s / %d (%d)\n",
537                        inet_ntop(AF_INET6, ipv6_l3fwd_lpm_route_array[i].ip,
538                                  abuf, sizeof(abuf)),
539                        ipv6_l3fwd_lpm_route_array[i].depth,
540                        ipv6_l3fwd_lpm_route_array[i].if_out);
541         }
542 }
543
544 int
545 lpm_check_ptype(int portid)
546 {
547         int i, ret;
548         int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
549         uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
550
551         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
552         if (ret <= 0)
553                 return 0;
554
555         uint32_t ptypes[ret];
556
557         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
558         for (i = 0; i < ret; ++i) {
559                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
560                         ptype_l3_ipv4 = 1;
561                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
562                         ptype_l3_ipv6 = 1;
563         }
564
565         if (ptype_l3_ipv4 == 0)
566                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
567
568         if (ptype_l3_ipv6 == 0)
569                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
570
571         if (ptype_l3_ipv4 && ptype_l3_ipv6)
572                 return 1;
573
574         return 0;
575
576 }
577
578 static inline void
579 lpm_parse_ptype(struct rte_mbuf *m)
580 {
581         struct rte_ether_hdr *eth_hdr;
582         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
583         uint16_t ether_type;
584
585         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
586         ether_type = eth_hdr->ether_type;
587         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
588                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
589         else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
590                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
591
592         m->packet_type = packet_type;
593 }
594
595 uint16_t
596 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
597                    struct rte_mbuf *pkts[], uint16_t nb_pkts,
598                    uint16_t max_pkts __rte_unused,
599                    void *user_param __rte_unused)
600 {
601         unsigned int i;
602
603         if (unlikely(nb_pkts == 0))
604                 return nb_pkts;
605         rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
606         for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
607                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
608                         struct ether_hdr *));
609                 lpm_parse_ptype(pkts[i]);
610         }
611         lpm_parse_ptype(pkts[i]);
612
613         return nb_pkts;
614 }
615
616 /* Return ipv4/ipv6 lpm fwd lookup struct. */
617 void *
618 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
619 {
620         return ipv4_l3fwd_lpm_lookup_struct[socketid];
621 }
622
623 void *
624 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
625 {
626         return ipv6_l3fwd_lpm_lookup_struct[socketid];
627 }