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