1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #ifndef __IPSEC_H__ 6 #define __IPSEC_H__ 7 8 #include <stdint.h> 9 10 #include <rte_byteorder.h> 11 #include <rte_crypto.h> 12 #include <rte_security.h> 13 #include <rte_flow.h> 14 #include <rte_ipsec.h> 15 16 #include "ipsec-secgw.h" 17 18 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2 19 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3 20 21 #define MAX_INFLIGHT 128 22 #define MAX_QP_PER_LCORE 256 23 24 #define MAX_DIGEST_SIZE 32 /* Bytes -- 256 bits */ 25 26 #define IPSEC_OFFLOAD_ESN_SOFTLIMIT 0xffffff00 27 28 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \ 29 sizeof(struct rte_crypto_sym_op)) 30 31 #define DEFAULT_MAX_CATEGORIES 1 32 33 #define INVALID_SPI (0) 34 35 #define DISCARD INVALID_SPI 36 #define BYPASS UINT32_MAX 37 38 #define IPSEC_XFORM_MAX 2 39 40 #define IP6_VERSION (6) 41 42 struct rte_crypto_xform; 43 struct ipsec_xform; 44 struct rte_mbuf; 45 46 struct ipsec_sa; 47 /* 48 * Keeps number of configured SA's for each address family: 49 */ 50 struct ipsec_sa_cnt { 51 uint32_t nb_v4; 52 uint32_t nb_v6; 53 }; 54 55 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa, 56 struct rte_crypto_op *cop); 57 58 struct ip_addr { 59 union { 60 uint32_t ip4; 61 union { 62 uint64_t ip6[2]; 63 uint8_t ip6_b[16]; 64 } ip6; 65 } ip; 66 }; 67 68 #define MAX_KEY_SIZE 36 69 70 /* 71 * application wide SA parameters 72 */ 73 struct app_sa_prm { 74 uint32_t enable; /* use librte_ipsec API for ipsec pkt processing */ 75 uint32_t window_size; /* replay window size */ 76 uint32_t enable_esn; /* enable/disable ESN support */ 77 uint32_t cache_sz; /* per lcore SA cache size */ 78 uint32_t udp_encap; /* enable/disable UDP Encapsulation */ 79 uint64_t flags; /* rte_ipsec_sa_prm.flags */ 80 }; 81 82 extern struct app_sa_prm app_sa_prm; 83 84 struct flow_info { 85 struct rte_flow *rx_def_flow; 86 }; 87 88 extern struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS]; 89 90 enum { 91 IPSEC_SESSION_PRIMARY = 0, 92 IPSEC_SESSION_FALLBACK = 1, 93 IPSEC_SESSION_MAX 94 }; 95 96 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1) 97 98 static inline struct ipsec_sa * 99 ipsec_mask_saptr(void *ptr) 100 { 101 uintptr_t i = (uintptr_t)ptr; 102 static const uintptr_t mask = IPSEC_SA_OFFLOAD_FALLBACK_FLAG; 103 104 i &= ~mask; 105 106 return (struct ipsec_sa *)i; 107 } 108 109 struct ipsec_sa { 110 struct rte_ipsec_session sessions[IPSEC_SESSION_MAX]; 111 uint32_t spi; 112 uint32_t cdev_id_qp; 113 uint64_t seq; 114 uint32_t salt; 115 uint32_t fallback_sessions; 116 enum rte_crypto_cipher_algorithm cipher_algo; 117 enum rte_crypto_auth_algorithm auth_algo; 118 enum rte_crypto_aead_algorithm aead_algo; 119 uint16_t digest_len; 120 uint16_t iv_len; 121 uint16_t block_size; 122 uint16_t flags; 123 #define IP4_TUNNEL (1 << 0) 124 #define IP6_TUNNEL (1 << 1) 125 #define TRANSPORT (1 << 2) 126 #define IP4_TRANSPORT (1 << 3) 127 #define IP6_TRANSPORT (1 << 4) 128 struct ip_addr src; 129 struct ip_addr dst; 130 uint8_t cipher_key[MAX_KEY_SIZE]; 131 uint16_t cipher_key_len; 132 uint8_t auth_key[MAX_KEY_SIZE]; 133 uint16_t auth_key_len; 134 uint16_t aad_len; 135 union { 136 struct rte_crypto_sym_xform *xforms; 137 struct rte_security_ipsec_xform *sec_xform; 138 }; 139 enum rte_security_ipsec_sa_direction direction; 140 uint8_t udp_encap; 141 uint16_t portid; 142 uint8_t fdir_qid; 143 uint8_t fdir_flag; 144 145 #define MAX_RTE_FLOW_PATTERN (4) 146 #define MAX_RTE_FLOW_ACTIONS (3) 147 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN]; 148 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS]; 149 struct rte_flow_attr attr; 150 union { 151 struct rte_flow_item_ipv4 ipv4_spec; 152 struct rte_flow_item_ipv6 ipv6_spec; 153 }; 154 struct rte_flow_item_esp esp_spec; 155 struct rte_flow *flow; 156 struct rte_security_session_conf sess_conf; 157 } __rte_cache_aligned; 158 159 struct ipsec_xf { 160 struct rte_crypto_sym_xform a; 161 struct rte_crypto_sym_xform b; 162 }; 163 164 struct ipsec_sad { 165 struct rte_ipsec_sad *sad_v4; 166 struct rte_ipsec_sad *sad_v6; 167 }; 168 169 struct sa_ctx { 170 void *satbl; /* pointer to array of rte_ipsec_sa objects*/ 171 struct ipsec_sad sad; 172 struct ipsec_xf *xf; 173 uint32_t nb_sa; 174 struct ipsec_sa sa[]; 175 }; 176 177 struct ipsec_mbuf_metadata { 178 struct ipsec_sa *sa; 179 struct rte_crypto_op cop; 180 struct rte_crypto_sym_op sym_cop; 181 uint8_t buf[32]; 182 } __rte_cache_aligned; 183 184 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT) 185 186 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL)) 187 188 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT)) 189 190 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT)) 191 192 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL) 193 194 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL) 195 196 /* 197 * Macro for getting ipsec_sa flags statuses without version of protocol 198 * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags). 199 */ 200 #define WITHOUT_TRANSPORT_VERSION(flags) \ 201 ((flags) & (IP4_TUNNEL | \ 202 IP6_TUNNEL | \ 203 TRANSPORT)) 204 205 struct cdev_qp { 206 uint16_t id; 207 uint16_t qp; 208 uint16_t in_flight; 209 uint16_t len; 210 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 211 }; 212 213 struct ipsec_ctx { 214 struct rte_hash *cdev_map; 215 struct sp_ctx *sp4_ctx; 216 struct sp_ctx *sp6_ctx; 217 struct sa_ctx *sa_ctx; 218 uint16_t nb_qps; 219 uint16_t last_qp; 220 struct cdev_qp tbl[MAX_QP_PER_LCORE]; 221 struct rte_mempool *session_pool; 222 struct rte_mempool *session_priv_pool; 223 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 224 uint16_t ol_pkts_cnt; 225 uint64_t ipv4_offloads; 226 uint64_t ipv6_offloads; 227 }; 228 229 struct cdev_key { 230 uint16_t lcore_id; 231 uint8_t cipher_algo; 232 uint8_t auth_algo; 233 uint8_t aead_algo; 234 }; 235 236 struct socket_ctx { 237 struct sa_ctx *sa_in; 238 struct sa_ctx *sa_out; 239 struct sp_ctx *sp_ip4_in; 240 struct sp_ctx *sp_ip4_out; 241 struct sp_ctx *sp_ip6_in; 242 struct sp_ctx *sp_ip6_out; 243 struct rt_ctx *rt_ip4; 244 struct rt_ctx *rt_ip6; 245 struct rte_mempool *mbuf_pool; 246 struct rte_mempool *mbuf_pool_indir; 247 struct rte_mempool *session_pool; 248 struct rte_mempool *session_priv_pool; 249 }; 250 251 struct cnt_blk { 252 uint32_t salt; 253 uint64_t iv; 254 uint32_t cnt; 255 } __rte_packed; 256 257 /* Socket ctx */ 258 extern struct socket_ctx socket_ctx[NB_SOCKETS]; 259 260 void 261 ipsec_poll_mode_worker(void); 262 263 int 264 ipsec_launch_one_lcore(void *args); 265 266 extern struct ipsec_sa *sa_out; 267 extern uint32_t nb_sa_out; 268 269 extern struct ipsec_sa *sa_in; 270 extern uint32_t nb_sa_in; 271 272 uint16_t 273 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 274 uint16_t nb_pkts, uint16_t len); 275 276 uint16_t 277 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 278 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len); 279 280 uint16_t 281 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 282 uint16_t len); 283 284 uint16_t 285 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 286 uint16_t len); 287 288 void 289 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 290 291 void 292 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 293 294 static inline uint16_t 295 ipsec_metadata_size(void) 296 { 297 return sizeof(struct ipsec_mbuf_metadata); 298 } 299 300 static inline struct ipsec_mbuf_metadata * 301 get_priv(struct rte_mbuf *m) 302 { 303 return rte_mbuf_to_priv(m); 304 } 305 306 static inline void * 307 get_cnt_blk(struct rte_mbuf *m) 308 { 309 struct ipsec_mbuf_metadata *priv = get_priv(m); 310 311 return &priv->buf[0]; 312 } 313 314 static inline void * 315 get_aad(struct rte_mbuf *m) 316 { 317 struct ipsec_mbuf_metadata *priv = get_priv(m); 318 319 return &priv->buf[16]; 320 } 321 322 static inline void * 323 get_sym_cop(struct rte_crypto_op *cop) 324 { 325 return (cop + 1); 326 } 327 328 static inline struct rte_ipsec_session * 329 ipsec_get_primary_session(struct ipsec_sa *sa) 330 { 331 return &sa->sessions[IPSEC_SESSION_PRIMARY]; 332 } 333 334 static inline struct rte_ipsec_session * 335 ipsec_get_fallback_session(struct ipsec_sa *sa) 336 { 337 return &sa->sessions[IPSEC_SESSION_FALLBACK]; 338 } 339 340 static inline enum rte_security_session_action_type 341 ipsec_get_action_type(struct ipsec_sa *sa) 342 { 343 struct rte_ipsec_session *ips; 344 ips = ipsec_get_primary_session(sa); 345 return ips->type; 346 } 347 348 int 349 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx); 350 351 void 352 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[], 353 void *sa[], uint16_t nb_pkts); 354 355 void 356 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[], 357 void *sa[], uint16_t nb_pkts); 358 359 void 360 sp4_init(struct socket_ctx *ctx, int32_t socket_id); 361 362 void 363 sp6_init(struct socket_ctx *ctx, int32_t socket_id); 364 365 /* 366 * Search through SP rules for given SPI. 367 * Returns first rule index if found(greater or equal then zero), 368 * or -ENOENT otherwise. 369 */ 370 int 371 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 372 uint32_t mask[2]); 373 int 374 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 375 uint32_t mask[2]); 376 377 /* 378 * Search through SA entries for given SPI. 379 * Returns first entry index if found(greater or equal then zero), 380 * or -ENOENT otherwise. 381 */ 382 int 383 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound); 384 385 void 386 sa_init(struct socket_ctx *ctx, int32_t socket_id); 387 388 void 389 rt_init(struct socket_ctx *ctx, int32_t socket_id); 390 391 int 392 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads, 393 uint64_t *tx_offloads); 394 395 int 396 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr); 397 398 void 399 enqueue_cop_burst(struct cdev_qp *cqp); 400 401 int 402 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa, 403 struct rte_ipsec_session *ips); 404 405 int 406 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa, 407 struct rte_ipsec_session *ips); 408 int 409 check_flow_params(uint16_t fdir_portid, uint8_t fdir_qid); 410 411 int 412 create_ipsec_esp_flow(struct ipsec_sa *sa); 413 414 uint32_t 415 get_nb_crypto_sessions(void); 416 417 #endif /* __IPSEC_H__ */ 418