NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef FLOW_COMMON_H_
27 #define FLOW_COMMON_H_
28 
29 #include <arpa/inet.h>
30 
31 #include <doca_flow.h>
32 
33 #include "ipsec_ctx.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define QUEUE_DEPTH (512) /* DOCA Flow queue depth */
40 #define SECURED_IDX (0) /* Index for secured network port in ports array */
41 #define UNSECURED_IDX (1) /* Index for unsecured network port in ports array */
42 #define DEFAULT_TIMEOUT_US (10000) /* default timeout for processing entries */
43 #define DEF_EXPECTED_ENTRIES (1024) /* default expected entries in the pipe */
44 #define SET_L4_PORT(layer, port, value) \
45  do { \
46  if (match.layer.l4_type_ext == DOCA_FLOW_L4_TYPE_EXT_TCP) \
47  match.layer.tcp.l4_port.port = (value); \
48  else if (match.layer.l4_type_ext == DOCA_FLOW_L4_TYPE_EXT_UDP) \
49  match.layer.udp.l4_port.port = (value); \
50  } while (0) /* Set match l4 port */
51 
52 #define BE_IPV4_ADDR(a, b, c, d) (RTE_BE32((a << 24) + (b << 16) + (c << 8) + d)) /* Big endian conversion */
53 #define SET_IP6_ADDR(addr, a, b, c, d) \
54  do { \
55  addr[0] = a; \
56  addr[1] = b; \
57  addr[2] = c; \
58  addr[3] = d; \
59  } while (0)
60 #define SET_MAC_ADDR(addr, a, b, c, d, e, f) \
61  do { \
62  addr[0] = a & 0xff; \
63  addr[1] = b & 0xff; \
64  addr[2] = c & 0xff; \
65  addr[3] = d & 0xff; \
66  addr[4] = e & 0xff; \
67  addr[5] = f & 0xff; \
68  } while (0) /* create source mac address */
69 
70 /* IPsec Security Gateway mapping between dpdk and doca flow port */
72  struct doca_flow_port *port; /* doca flow port pointer */
73  int port_id; /* dpdk port ID */
74  struct doca_flow_header_eth eth_header; /* doca flow eth header */
75 };
76 
77 /* user context struct that will be used in entries process callback */
79  bool failure; /* will be set to true if some entry status will not be success */
80  int nb_processed; /* number of entries that was already processed */
81  int entries_in_queue; /* number of entries in queue that is waiting to process */
82 };
83 
84 /* core context struct */
86  uint16_t queue_id; /* core queue ID */
87  struct ipsec_security_gw_config *config; /* application configuration struct */
88  struct encrypt_rule *encrypt_rules; /* encryption rules */
89  struct decrypt_rule *decrypt_rules; /* decryption rules */
90  int *nb_encrypt_rules; /* number of encryption rules */
91  struct ipsec_security_gw_ports_map **ports; /* application ports */
92 };
93 
94 /*
95  * This union describes the meaning of each bit in "meta.pkt_meta"
96  */
98  uint32_t u32;
99  struct {
100  uint32_t encrypt : 1; /* packet is on encrypt path */
101  uint32_t decrypt : 1; /* packet is on decrypt path */
102  uint32_t inner_ipv6 : 1; /* indicate if inner type is ipv6 for tunnel mode */
103  uint32_t decrypt_syndrome : 2; /* decrypt syndrome, set in debug mode when fwd to app */
104  uint32_t antireplay_syndrome : 2; /* anti-replay syndrome, set in debug mode when fwd to app*/
105  uint32_t rsvd0 : 5; /* must be set to 0 */
106  uint32_t rule_id : 20; /* indicate the rule ID */
107  };
108 } __attribute__((__packed__));
109 
110 /*
111  * Initialized DOCA Flow library and start DOCA Flow ports
112  *
113  * @app_cfg [in]: application configuration structure
114  * @nb_queues [in]: number of queues
115  * @ports [out]: initialized DOCA Flow ports
116  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
117  */
119  int nb_queues,
121 
122 /*
123  * Initialized status entries for each port
124  *
125  * @app_cfg [in]: application configuration structure
126  * @nb_queues [in]: number of queues
127  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
128  */
130 
131 /*
132  * Binding encrypt and decrypt rules
133  *
134  * @ports [in]: initialized DOCA Flow ports
135  * @app_cfg [in]: application configuration structure
136  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
137  */
140 
141 /*
142  * Destroy DOCA Flow resources
143  *
144  * @nb_ports [in]: number of ports to destroy
145  * @ports [in]: initialized DOCA Flow ports
146  */
148 
149 /*
150  * Process the added entries and check the status
151  *
152  * @port [in]: DOCA Flow port
153  * @status [in]: the entries status struct that monitor the entries in this specific port
154  * @timeout [in]: timeout for process entries
155  * @pipe_queue [in]: pipe queue to process
156  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
157  */
158 doca_error_t process_entries(struct doca_flow_port *port,
159  struct entries_status *status,
160  int timeout,
161  uint16_t pipe_queue);
162 
163 /*
164  * create root pipe for ingress in switch mode that forward the packets based on the port_id
165  *
166  * @ports [in]: array of struct ipsec_security_gw_ports_map
167  * @app_cfg [in]: application configuration struct
168  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
169  */
172 
173 /*
174  * create root pipe for egress in switch mode that forward the packets based on pkt meta
175  *
176  * @ports [in]: array of struct ipsec_security_gw_ports_map
177  * @app_cfg [in]: application configuration struct
178  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
179  */
182 
183 /*
184  * Create RSS pipe that fwd the packets to hairpin queue
185  *
186  * @port [in]: port of the pipe
187  * @nb_queues [in]: number of queues
188  * @rss_pipe [out]: pointer to created pipe
189  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
190  */
192  struct doca_flow_port *port,
193  uint16_t nb_queues,
194  struct doca_flow_pipe **rss_pipe);
195 
196 /*
197  * Create the DOCA Flow forward struct based on the running mode
198  *
199  * @app_cfg [in]: application configuration struct
200  * @port_id [in]: port ID of the pipe
201  * @encrypt [in]: true if direction is encrypt, false for decrypt
202  * @rss_queues [in]: rss queues array to fill in case of sw forward
203  * @rss_flags [in]: rss flags
204  * @fwd [out]: the created forward struct
205  */
207  int port_id,
208  bool encrypt,
209  uint16_t *rss_queues,
210  uint32_t rss_flags,
211  struct doca_flow_fwd *fwd);
212 
213 /*
214  * Remove trailing zeros from ipv4/ipv6 payload.
215  * Trailing zeros are added to ipv4/ipv6 payload so that it's larger than the minimal ethernet frame size.
216  *
217  * @m [in]: the mbuf to update
218  */
219 void remove_ethernet_padding(struct rte_mbuf **m);
220 
221 /*
222  * Convert icv length value to the correct enum doca_flow_crypto_icv_len
223  *
224  * @icv_len [in]: icv length value
225  * @return: enum doca_flow_crypto_icv_len with the correct icv length
226  */
227 uint32_t get_icv_len_int(enum doca_flow_crypto_icv_len icv_len);
228 
229 /*
230  * Release application allocated status entries
231  *
232  * @app_cfg [in]: application configuration struct
233  */
235 
236 /*
237  * Release application allocated resources
238  *
239  * @app_cfg [in]: application configuration struct
240  */
242 
243 #ifdef __cplusplus
244 } /* extern "C" */
245 #endif
246 
247 #endif /* FLOW_COMMON_H_ */
void doca_flow_cleanup(int nb_ports, struct ipsec_security_gw_ports_map *ports[])
Definition: flow_common.c:360
doca_error_t create_rss_pipe(struct ipsec_security_gw_config *app_cfg, struct doca_flow_port *port, uint16_t nb_queues, struct doca_flow_pipe **rss_pipe)
Definition: flow_common.c:384
void remove_ethernet_padding(struct rte_mbuf **m)
Definition: flow_common.c:870
doca_error_t process_entries(struct doca_flow_port *port, struct entries_status *status, int timeout, uint16_t pipe_queue)
Definition: flow_common.c:78
doca_error_t ipsec_security_gw_init_status(struct ipsec_security_gw_config *app_cfg, int nb_queues)
Definition: flow_common.c:315
doca_error_t create_switch_ingress_root_pipes(struct ipsec_security_gw_ports_map *ports[], struct ipsec_security_gw_config *app_cfg)
Definition: flow_common.c:815
uint32_t encrypt
Definition: flow_common.h:2
void security_gateway_free_status_entries(struct ipsec_security_gw_config *app_cfg)
Definition: flow_common.c:954
doca_error_t ipsec_security_gw_bind(struct ipsec_security_gw_ports_map *ports[], struct ipsec_security_gw_config *app_cfg)
Definition: flow_common.c:333
void security_gateway_free_resources(struct ipsec_security_gw_config *app_cfg)
Definition: flow_common.c:960
doca_error_t ipsec_security_gw_init_doca_flow(const struct ipsec_security_gw_config *app_cfg, int nb_queues, struct ipsec_security_gw_ports_map *ports[])
Definition: flow_common.c:163
void create_hairpin_pipe_fwd(struct ipsec_security_gw_config *app_cfg, int port_id, bool encrypt, uint16_t *rss_queues, uint32_t rss_flags, struct doca_flow_fwd *fwd)
Definition: flow_common.c:512
doca_error_t create_switch_egress_root_pipes(struct ipsec_security_gw_ports_map *ports[], struct ipsec_security_gw_config *app_cfg)
Definition: flow_common.c:840
uint32_t get_icv_len_int(enum doca_flow_crypto_icv_len icv_len)
Definition: flow_common.c:374
static uint16_t * rss_queues
Definition: flow_parser.c:114
static struct doca_flow_fwd fwd
Definition: flow_parser.c:109
static struct app_gpu_cfg app_cfg
enum doca_error doca_error_t
DOCA API return codes.
doca_flow_crypto_icv_len
doca flow crypto ICV length
union security_gateway_pkt_meta __attribute__((__packed__))
Definition: doca_flow.h:267
forwarding configuration
Definition: doca_flow.h:779
doca flow eth header
user context struct that will be used in entries process callback
Definition: flow_common.h:78
int entries_in_queue
Definition: flow_common.h:81
struct ipsec_security_gw_ports_map ** ports
Definition: flow_common.h:91
struct ipsec_security_gw_config * config
Definition: flow_common.h:87
struct decrypt_rule * decrypt_rules
Definition: flow_common.h:89
struct encrypt_rule * encrypt_rules
Definition: flow_common.h:88
struct doca_flow_port * port
Definition: flow_common.h:72
struct doca_flow_header_eth eth_header
Definition: flow_common.h:74
static int nb_ports
Definition: switch_core.c:44
static struct doca_flow_port * ports[FLOW_SWITCH_PORTS_MAX]
Definition: switch_core.c:42