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