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