1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
10 #include <rte_byteorder.h>
11 #include <rte_crypto.h>
12 #include <rte_security.h>
14 #include <rte_ipsec.h>
16 #include "ipsec-secgw.h"
18 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2
19 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3
21 #define MAX_INFLIGHT 128
22 #define MAX_QP_PER_LCORE 256
24 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */
26 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00
28 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
29 sizeof(struct rte_crypto_sym_op))
31 #define uint32_t_to_char(ip, a, b, c, d) do {\
32 *a = (uint8_t)(ip >> 24 & 0xff);\
33 *b = (uint8_t)(ip >> 16 & 0xff);\
34 *c = (uint8_t)(ip >> 8 & 0xff);\
35 *d = (uint8_t)(ip & 0xff);\
38 #define DEFAULT_MAX_CATEGORIES 1
40 #define INVALID_SPI (0)
42 #define DISCARD INVALID_SPI
43 #define BYPASS UINT32_MAX
45 #define IPSEC_XFORM_MAX 2
47 #define IP6_VERSION (6)
49 struct rte_crypto_xform
;
55 * Keeps number of configured SA's for each address family:
62 typedef int32_t (*ipsec_xform_fn
)(struct rte_mbuf
*m
, struct ipsec_sa
*sa
,
63 struct rte_crypto_op
*cop
);
75 #define MAX_KEY_SIZE 36
78 * application wide SA parameters
81 uint32_t enable
; /* use librte_ipsec API for ipsec pkt processing */
82 uint32_t window_size
; /* replay window size */
83 uint32_t enable_esn
; /* enable/disable ESN support */
84 uint32_t cache_sz
; /* per lcore SA cache size */
85 uint64_t flags
; /* rte_ipsec_sa_prm.flags */
88 extern struct app_sa_prm app_sa_prm
;
91 struct rte_flow
*rx_def_flow
;
94 extern struct flow_info flow_info_tbl
[RTE_MAX_ETHPORTS
];
97 IPSEC_SESSION_PRIMARY
= 0,
98 IPSEC_SESSION_FALLBACK
= 1,
102 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1)
104 static inline struct ipsec_sa
*
105 ipsec_mask_saptr(void *ptr
)
107 uintptr_t i
= (uintptr_t)ptr
;
108 static const uintptr_t mask
= IPSEC_SA_OFFLOAD_FALLBACK_FLAG
;
112 return (struct ipsec_sa
*)i
;
116 struct rte_ipsec_session sessions
[IPSEC_SESSION_MAX
];
121 uint32_t fallback_sessions
;
122 enum rte_crypto_cipher_algorithm cipher_algo
;
123 enum rte_crypto_auth_algorithm auth_algo
;
124 enum rte_crypto_aead_algorithm aead_algo
;
129 #define IP4_TUNNEL (1 << 0)
130 #define IP6_TUNNEL (1 << 1)
131 #define TRANSPORT (1 << 2)
132 #define IP4_TRANSPORT (1 << 3)
133 #define IP6_TRANSPORT (1 << 4)
136 uint8_t cipher_key
[MAX_KEY_SIZE
];
137 uint16_t cipher_key_len
;
138 uint8_t auth_key
[MAX_KEY_SIZE
];
139 uint16_t auth_key_len
;
142 struct rte_crypto_sym_xform
*xforms
;
143 struct rte_security_ipsec_xform
*sec_xform
;
145 enum rte_security_ipsec_sa_direction direction
;
150 #define MAX_RTE_FLOW_PATTERN (4)
151 #define MAX_RTE_FLOW_ACTIONS (3)
152 struct rte_flow_item pattern
[MAX_RTE_FLOW_PATTERN
];
153 struct rte_flow_action action
[MAX_RTE_FLOW_ACTIONS
];
154 struct rte_flow_attr attr
;
156 struct rte_flow_item_ipv4 ipv4_spec
;
157 struct rte_flow_item_ipv6 ipv6_spec
;
159 struct rte_flow_item_esp esp_spec
;
160 struct rte_flow
*flow
;
161 struct rte_security_session_conf sess_conf
;
162 } __rte_cache_aligned
;
165 struct rte_crypto_sym_xform a
;
166 struct rte_crypto_sym_xform b
;
170 struct rte_ipsec_sad
*sad_v4
;
171 struct rte_ipsec_sad
*sad_v6
;
175 void *satbl
; /* pointer to array of rte_ipsec_sa objects*/
176 struct ipsec_sad sad
;
179 struct ipsec_sa sa
[];
182 struct ipsec_mbuf_metadata
{
184 struct rte_crypto_op cop
;
185 struct rte_crypto_sym_op sym_cop
;
187 } __rte_cache_aligned
;
189 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT)
191 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL))
193 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT))
195 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT))
197 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL)
199 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL)
202 * Macro for getting ipsec_sa flags statuses without version of protocol
203 * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags).
205 #define WITHOUT_TRANSPORT_VERSION(flags) \
206 ((flags) & (IP4_TUNNEL | \
215 struct rte_crypto_op
*buf
[MAX_PKT_BURST
] __rte_aligned(sizeof(void *));
219 struct rte_hash
*cdev_map
;
220 struct sp_ctx
*sp4_ctx
;
221 struct sp_ctx
*sp6_ctx
;
222 struct sa_ctx
*sa_ctx
;
225 struct cdev_qp tbl
[MAX_QP_PER_LCORE
];
226 struct rte_mempool
*session_pool
;
227 struct rte_mempool
*session_priv_pool
;
228 struct rte_mbuf
*ol_pkts
[MAX_PKT_BURST
] __rte_aligned(sizeof(void *));
229 uint16_t ol_pkts_cnt
;
230 uint64_t ipv4_offloads
;
231 uint64_t ipv6_offloads
;
242 struct sa_ctx
*sa_in
;
243 struct sa_ctx
*sa_out
;
244 struct sp_ctx
*sp_ip4_in
;
245 struct sp_ctx
*sp_ip4_out
;
246 struct sp_ctx
*sp_ip6_in
;
247 struct sp_ctx
*sp_ip6_out
;
248 struct rt_ctx
*rt_ip4
;
249 struct rt_ctx
*rt_ip6
;
250 struct rte_mempool
*mbuf_pool
;
251 struct rte_mempool
*mbuf_pool_indir
;
252 struct rte_mempool
*session_pool
;
253 struct rte_mempool
*session_priv_pool
;
263 extern struct socket_ctx socket_ctx
[NB_SOCKETS
];
266 ipsec_poll_mode_worker(void);
269 ipsec_launch_one_lcore(void *args
);
271 extern struct ipsec_sa
*sa_out
;
272 extern uint32_t nb_sa_out
;
274 extern struct ipsec_sa
*sa_in
;
275 extern uint32_t nb_sa_in
;
278 ipsec_inbound(struct ipsec_ctx
*ctx
, struct rte_mbuf
*pkts
[],
279 uint16_t nb_pkts
, uint16_t len
);
282 ipsec_outbound(struct ipsec_ctx
*ctx
, struct rte_mbuf
*pkts
[],
283 uint32_t sa_idx
[], uint16_t nb_pkts
, uint16_t len
);
286 ipsec_inbound_cqp_dequeue(struct ipsec_ctx
*ctx
, struct rte_mbuf
*pkts
[],
290 ipsec_outbound_cqp_dequeue(struct ipsec_ctx
*ctx
, struct rte_mbuf
*pkts
[],
294 ipsec_process(struct ipsec_ctx
*ctx
, struct ipsec_traffic
*trf
);
297 ipsec_cqp_process(struct ipsec_ctx
*ctx
, struct ipsec_traffic
*trf
);
299 static inline uint16_t
300 ipsec_metadata_size(void)
302 return sizeof(struct ipsec_mbuf_metadata
);
305 static inline struct ipsec_mbuf_metadata
*
306 get_priv(struct rte_mbuf
*m
)
308 return rte_mbuf_to_priv(m
);
312 get_cnt_blk(struct rte_mbuf
*m
)
314 struct ipsec_mbuf_metadata
*priv
= get_priv(m
);
316 return &priv
->buf
[0];
320 get_aad(struct rte_mbuf
*m
)
322 struct ipsec_mbuf_metadata
*priv
= get_priv(m
);
324 return &priv
->buf
[16];
328 get_sym_cop(struct rte_crypto_op
*cop
)
333 static inline struct rte_ipsec_session
*
334 ipsec_get_primary_session(struct ipsec_sa
*sa
)
336 return &sa
->sessions
[IPSEC_SESSION_PRIMARY
];
339 static inline struct rte_ipsec_session
*
340 ipsec_get_fallback_session(struct ipsec_sa
*sa
)
342 return &sa
->sessions
[IPSEC_SESSION_FALLBACK
];
345 static inline enum rte_security_session_action_type
346 ipsec_get_action_type(struct ipsec_sa
*sa
)
348 struct rte_ipsec_session
*ips
;
349 ips
= ipsec_get_primary_session(sa
);
354 inbound_sa_check(struct sa_ctx
*sa_ctx
, struct rte_mbuf
*m
, uint32_t sa_idx
);
357 inbound_sa_lookup(struct sa_ctx
*sa_ctx
, struct rte_mbuf
*pkts
[],
358 void *sa
[], uint16_t nb_pkts
);
361 outbound_sa_lookup(struct sa_ctx
*sa_ctx
, uint32_t sa_idx
[],
362 void *sa
[], uint16_t nb_pkts
);
365 sp4_init(struct socket_ctx
*ctx
, int32_t socket_id
);
368 sp6_init(struct socket_ctx
*ctx
, int32_t socket_id
);
371 * Search through SP rules for given SPI.
372 * Returns first rule index if found(greater or equal then zero),
373 * or -ENOENT otherwise.
376 sp4_spi_present(uint32_t spi
, int inbound
, struct ip_addr ip_addr
[2],
379 sp6_spi_present(uint32_t spi
, int inbound
, struct ip_addr ip_addr
[2],
383 * Search through SA entries for given SPI.
384 * Returns first entry index if found(greater or equal then zero),
385 * or -ENOENT otherwise.
388 sa_spi_present(struct sa_ctx
*sa_ctx
, uint32_t spi
, int inbound
);
391 sa_init(struct socket_ctx
*ctx
, int32_t socket_id
);
394 rt_init(struct socket_ctx
*ctx
, int32_t socket_id
);
397 sa_check_offloads(uint16_t port_id
, uint64_t *rx_offloads
,
398 uint64_t *tx_offloads
);
401 add_dst_ethaddr(uint16_t port
, const struct rte_ether_addr
*addr
);
404 enqueue_cop_burst(struct cdev_qp
*cqp
);
407 create_lookaside_session(struct ipsec_ctx
*ipsec_ctx
, struct ipsec_sa
*sa
,
408 struct rte_ipsec_session
*ips
);
411 create_inline_session(struct socket_ctx
*skt_ctx
, struct ipsec_sa
*sa
,
412 struct rte_ipsec_session
*ips
);
414 check_flow_params(uint16_t fdir_portid
, uint8_t fdir_qid
);
417 create_ipsec_esp_flow(struct ipsec_sa
*sa
);
420 get_nb_crypto_sessions(void);
422 #endif /* __IPSEC_H__ */