node: add IPv4 lookup for arm64
[dpdk.git] / lib / librte_node / ip4_lookup.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #include <arpa/inet.h>
6 #include <sys/socket.h>
7
8 #include <rte_debug.h>
9 #include <rte_ethdev.h>
10 #include <rte_ether.h>
11 #include <rte_graph.h>
12 #include <rte_graph_worker.h>
13 #include <rte_ip.h>
14 #include <rte_lpm.h>
15 #include <rte_mbuf.h>
16 #include <rte_tcp.h>
17 #include <rte_udp.h>
18
19 #include "rte_node_ip4_api.h"
20
21 #include "node_private.h"
22
23 #define IPV4_L3FWD_LPM_MAX_RULES 1024
24 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
25
26 /* IP4 Lookup global data struct */
27 struct ip4_lookup_node_main {
28         struct rte_lpm *lpm_tbl[RTE_MAX_NUMA_NODES];
29 };
30
31 #if defined(RTE_MACHINE_CPUFLAG_NEON)
32 #include "ip4_lookup_neon.h"
33 #else
34
35 static uint16_t
36 ip4_lookup_node_process(struct rte_graph *graph, struct rte_node *node,
37                         void **objs, uint16_t nb_objs)
38 {
39         struct rte_ipv4_hdr *ipv4_hdr;
40         void **to_next, **from;
41         uint16_t last_spec = 0;
42         struct rte_mbuf *mbuf;
43         rte_edge_t next_index;
44         struct rte_lpm *lpm;
45         uint16_t held = 0;
46         uint32_t drop_nh;
47         int i, rc;
48
49         /* Speculative next */
50         next_index = RTE_NODE_IP4_LOOKUP_NEXT_REWRITE;
51         /* Drop node */
52         drop_nh = ((uint32_t)RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP) << 16;
53
54         /* Get socket specific LPM from ctx */
55         lpm = *((struct rte_lpm **)node->ctx);
56         from = objs;
57
58         /* Get stream for the speculated next node */
59         to_next = rte_node_next_stream_get(graph, node, next_index, nb_objs);
60         for (i = 0; i < nb_objs; i++) {
61                 uint32_t next_hop;
62                 uint16_t next;
63
64                 mbuf = (struct rte_mbuf *)objs[i];
65
66                 /* Extract DIP of mbuf0 */
67                 ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *,
68                                 sizeof(struct rte_ether_hdr));
69                 /* Extract cksum, ttl as ipv4 hdr is in cache */
70                 node_mbuf_priv1(mbuf)->cksum = ipv4_hdr->hdr_checksum;
71                 node_mbuf_priv1(mbuf)->ttl = ipv4_hdr->time_to_live;
72
73                 rc = rte_lpm_lookup(lpm, rte_be_to_cpu_32(ipv4_hdr->dst_addr),
74                                     &next_hop);
75                 next_hop = (rc == 0) ? next_hop : drop_nh;
76
77                 node_mbuf_priv1(mbuf)->nh = (uint16_t)next_hop;
78                 next_hop = next_hop >> 16;
79                 next = (uint16_t)next_hop;
80
81                 if (unlikely(next_index != next)) {
82                         /* Copy things successfully speculated till now */
83                         rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
84                         from += last_spec;
85                         to_next += last_spec;
86                         held += last_spec;
87                         last_spec = 0;
88
89                         rte_node_enqueue_x1(graph, node, next, from[0]);
90                         from += 1;
91                 } else {
92                         last_spec += 1;
93                 }
94         }
95
96         /* !!! Home run !!! */
97         if (likely(last_spec == nb_objs)) {
98                 rte_node_next_stream_move(graph, node, next_index);
99                 return nb_objs;
100         }
101         held += last_spec;
102         rte_memcpy(to_next, from, last_spec * sizeof(from[0]));
103         rte_node_next_stream_put(graph, node, next_index, held);
104
105         return nb_objs;
106 }
107
108 #endif
109
110 static int
111 ip4_lookup_node_init(const struct rte_graph *graph, struct rte_node *node)
112 {
113         RTE_SET_USED(graph);
114         RTE_SET_USED(node);
115
116         node_dbg("ip4_lookup", "Initialized ip4_lookup node");
117
118         return 0;
119 }
120
121 static struct rte_node_register ip4_lookup_node = {
122         .process = ip4_lookup_node_process,
123         .name = "ip4_lookup",
124
125         .init = ip4_lookup_node_init,
126
127         .nb_edges = RTE_NODE_IP4_LOOKUP_NEXT_MAX,
128         .next_nodes = {
129                 [RTE_NODE_IP4_LOOKUP_NEXT_REWRITE] = "ip4_rewrite",
130                 [RTE_NODE_IP4_LOOKUP_NEXT_PKT_DROP] = "pkt_drop",
131         },
132 };
133
134 RTE_NODE_REGISTER(ip4_lookup_node);