examples/ipsec-secgw: add test scripts for 3DES-CBC
[dpdk.git] / examples / l3fwd / l3fwd_em_hlm.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation.
3  * Copyright(c) 2017-2018 Linaro Limited.
4  */
5
6 #ifndef __L3FWD_EM_HLM_H__
7 #define __L3FWD_EM_HLM_H__
8
9 #if defined RTE_ARCH_X86
10 #include "l3fwd_sse.h"
11 #include "l3fwd_em_hlm_sse.h"
12 #elif defined RTE_MACHINE_CPUFLAG_NEON
13 #include "l3fwd_neon.h"
14 #include "l3fwd_em_hlm_neon.h"
15 #endif
16
17 #ifdef RTE_ARCH_ARM64
18 #define EM_HASH_LOOKUP_COUNT 16
19 #else
20 #define EM_HASH_LOOKUP_COUNT 8
21 #endif
22
23
24 static __rte_always_inline void
25 em_get_dst_port_ipv4xN(struct lcore_conf *qconf, struct rte_mbuf *m[],
26                 uint16_t portid, uint16_t dst_port[])
27 {
28         int i;
29         int32_t ret[EM_HASH_LOOKUP_COUNT];
30         union ipv4_5tuple_host key[EM_HASH_LOOKUP_COUNT];
31         const void *key_array[EM_HASH_LOOKUP_COUNT];
32
33         for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++) {
34                 get_ipv4_5tuple(m[i], mask0.x, &key[i]);
35                 key_array[i] = &key[i];
36         }
37
38         rte_hash_lookup_bulk(qconf->ipv4_lookup_struct, &key_array[0],
39                              EM_HASH_LOOKUP_COUNT, ret);
40
41         for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++) {
42                 dst_port[i] = ((ret[i] < 0) ?
43                                 portid : ipv4_l3fwd_out_if[ret[i]]);
44
45                 if (dst_port[i] >= RTE_MAX_ETHPORTS ||
46                                 (enabled_port_mask & 1 << dst_port[i]) == 0)
47                         dst_port[i] = portid;
48         }
49 }
50
51 static __rte_always_inline void
52 em_get_dst_port_ipv6xN(struct lcore_conf *qconf, struct rte_mbuf *m[],
53                 uint16_t portid, uint16_t dst_port[])
54 {
55         int i;
56         int32_t ret[EM_HASH_LOOKUP_COUNT];
57         union ipv6_5tuple_host key[EM_HASH_LOOKUP_COUNT];
58         const void *key_array[EM_HASH_LOOKUP_COUNT];
59
60         for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++) {
61                 get_ipv6_5tuple(m[i], mask1.x, mask2.x, &key[i]);
62                 key_array[i] = &key[i];
63         }
64
65         rte_hash_lookup_bulk(qconf->ipv6_lookup_struct, &key_array[0],
66                              EM_HASH_LOOKUP_COUNT, ret);
67
68         for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++) {
69                 dst_port[i] = ((ret[i] < 0) ?
70                                 portid : ipv6_l3fwd_out_if[ret[i]]);
71
72                 if (dst_port[i] >= RTE_MAX_ETHPORTS ||
73                                 (enabled_port_mask & 1 << dst_port[i]) == 0)
74                         dst_port[i] = portid;
75         }
76 }
77
78 static __rte_always_inline uint16_t
79 em_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
80                 uint16_t portid)
81 {
82         uint16_t next_hop;
83         struct ipv4_hdr *ipv4_hdr;
84         struct ipv6_hdr *ipv6_hdr;
85         uint32_t tcp_or_udp;
86         uint32_t l3_ptypes;
87
88         tcp_or_udp = pkt->packet_type & (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP);
89         l3_ptypes = pkt->packet_type & RTE_PTYPE_L3_MASK;
90
91         if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV4)) {
92
93                 /* Handle IPv4 headers.*/
94                 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv4_hdr *,
95                                 sizeof(struct ether_hdr));
96
97                 next_hop = em_get_ipv4_dst_port(ipv4_hdr, portid,
98                                 qconf->ipv4_lookup_struct);
99
100                 if (next_hop >= RTE_MAX_ETHPORTS ||
101                                 (enabled_port_mask & 1 << next_hop) == 0)
102                         next_hop = portid;
103
104                 return next_hop;
105
106         } else if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV6)) {
107
108                 /* Handle IPv6 headers.*/
109                 ipv6_hdr = rte_pktmbuf_mtod_offset(pkt, struct ipv6_hdr *,
110                                 sizeof(struct ether_hdr));
111
112                 next_hop = em_get_ipv6_dst_port(ipv6_hdr, portid,
113                                 qconf->ipv6_lookup_struct);
114
115                 if (next_hop >= RTE_MAX_ETHPORTS ||
116                                 (enabled_port_mask & 1 << next_hop) == 0)
117                         next_hop = portid;
118
119                 return next_hop;
120
121         }
122
123         return portid;
124 }
125
126 /*
127  * Buffer optimized handling of packets, invoked
128  * from main_loop.
129  */
130 static inline void
131 l3fwd_em_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
132                 uint16_t portid, struct lcore_conf *qconf)
133 {
134         int32_t i, j, pos;
135         uint16_t dst_port[MAX_PKT_BURST];
136
137         /*
138          * Send nb_rx - nb_rx % EM_HASH_LOOKUP_COUNT packets
139          * in groups of EM_HASH_LOOKUP_COUNT.
140          */
141         int32_t n = RTE_ALIGN_FLOOR(nb_rx, EM_HASH_LOOKUP_COUNT);
142
143         for (j = 0; j < EM_HASH_LOOKUP_COUNT && j < nb_rx; j++) {
144                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j],
145                                                struct ether_hdr *) + 1);
146         }
147
148         for (j = 0; j < n; j += EM_HASH_LOOKUP_COUNT) {
149
150                 uint32_t pkt_type = RTE_PTYPE_L3_MASK |
151                                     RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP;
152                 uint32_t l3_type, tcp_or_udp;
153
154                 for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++)
155                         pkt_type &= pkts_burst[j + i]->packet_type;
156
157                 l3_type = pkt_type & RTE_PTYPE_L3_MASK;
158                 tcp_or_udp = pkt_type & (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP);
159
160                 for (i = 0, pos = j + EM_HASH_LOOKUP_COUNT;
161                      i < EM_HASH_LOOKUP_COUNT && pos < nb_rx; i++, pos++) {
162                         rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[pos],
163                                                        struct ether_hdr *) + 1);
164                 }
165
166                 if (tcp_or_udp && (l3_type == RTE_PTYPE_L3_IPV4)) {
167
168                         em_get_dst_port_ipv4xN(qconf, &pkts_burst[j], portid,
169                                                &dst_port[j]);
170
171                 } else if (tcp_or_udp && (l3_type == RTE_PTYPE_L3_IPV6)) {
172
173                         em_get_dst_port_ipv6xN(qconf, &pkts_burst[j], portid,
174                                                &dst_port[j]);
175
176                 } else {
177                         for (i = 0; i < EM_HASH_LOOKUP_COUNT; i++)
178                                 dst_port[j + i] = em_get_dst_port(qconf,
179                                                 pkts_burst[j + i], portid);
180                 }
181         }
182
183         for (; j < nb_rx; j++)
184                 dst_port[j] = em_get_dst_port(qconf, pkts_burst[j], portid);
185
186         send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
187
188 }
189 #endif /* __L3FWD_EM_HLM_H__ */