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 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1 17 #define RTE_LOGTYPE_IPSEC_ESP RTE_LOGTYPE_USER2 18 #define RTE_LOGTYPE_IPSEC_IPIP RTE_LOGTYPE_USER3 19 20 #define MAX_PKT_BURST 32 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 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);\ 36 } while (0) 37 38 #define DEFAULT_MAX_CATEGORIES 1 39 40 #define INVALID_SPI (0) 41 42 #define DISCARD INVALID_SPI 43 #define BYPASS UINT32_MAX 44 45 #define IPSEC_XFORM_MAX 2 46 47 #define IP6_VERSION (6) 48 49 struct rte_crypto_xform; 50 struct ipsec_xform; 51 struct rte_mbuf; 52 53 struct ipsec_sa; 54 /* 55 * Keeps number of configured SA's for each address family: 56 */ 57 struct ipsec_sa_cnt { 58 uint32_t nb_v4; 59 uint32_t nb_v6; 60 }; 61 62 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa, 63 struct rte_crypto_op *cop); 64 65 struct ip_addr { 66 union { 67 uint32_t ip4; 68 union { 69 uint64_t ip6[2]; 70 uint8_t ip6_b[16]; 71 } ip6; 72 } ip; 73 }; 74 75 #define MAX_KEY_SIZE 32 76 77 /* 78 * application wide SA parameters 79 */ 80 struct app_sa_prm { 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 */ 86 }; 87 88 extern struct app_sa_prm app_sa_prm; 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 uint16_t portid; 141 142 #define MAX_RTE_FLOW_PATTERN (4) 143 #define MAX_RTE_FLOW_ACTIONS (3) 144 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN]; 145 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS]; 146 struct rte_flow_attr attr; 147 union { 148 struct rte_flow_item_ipv4 ipv4_spec; 149 struct rte_flow_item_ipv6 ipv6_spec; 150 }; 151 struct rte_flow_item_esp esp_spec; 152 struct rte_flow *flow; 153 struct rte_security_session_conf sess_conf; 154 } __rte_cache_aligned; 155 156 struct ipsec_mbuf_metadata { 157 struct ipsec_sa *sa; 158 struct rte_crypto_op cop; 159 struct rte_crypto_sym_op sym_cop; 160 uint8_t buf[32]; 161 } __rte_cache_aligned; 162 163 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT) 164 165 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL)) 166 167 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT)) 168 169 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT)) 170 171 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL) 172 173 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL) 174 175 /* 176 * Macro for getting ipsec_sa flags statuses without version of protocol 177 * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags). 178 */ 179 #define WITHOUT_TRANSPORT_VERSION(flags) \ 180 ((flags) & (IP4_TUNNEL | \ 181 IP6_TUNNEL | \ 182 TRANSPORT)) 183 184 struct cdev_qp { 185 uint16_t id; 186 uint16_t qp; 187 uint16_t in_flight; 188 uint16_t len; 189 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 190 }; 191 192 struct ipsec_ctx { 193 struct rte_hash *cdev_map; 194 struct sp_ctx *sp4_ctx; 195 struct sp_ctx *sp6_ctx; 196 struct sa_ctx *sa_ctx; 197 uint16_t nb_qps; 198 uint16_t last_qp; 199 struct cdev_qp tbl[MAX_QP_PER_LCORE]; 200 struct rte_mempool *session_pool; 201 struct rte_mempool *session_priv_pool; 202 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 203 uint16_t ol_pkts_cnt; 204 uint64_t ipv4_offloads; 205 uint64_t ipv6_offloads; 206 }; 207 208 struct cdev_key { 209 uint16_t lcore_id; 210 uint8_t cipher_algo; 211 uint8_t auth_algo; 212 uint8_t aead_algo; 213 }; 214 215 struct socket_ctx { 216 struct sa_ctx *sa_in; 217 struct sa_ctx *sa_out; 218 struct sp_ctx *sp_ip4_in; 219 struct sp_ctx *sp_ip4_out; 220 struct sp_ctx *sp_ip6_in; 221 struct sp_ctx *sp_ip6_out; 222 struct rt_ctx *rt_ip4; 223 struct rt_ctx *rt_ip6; 224 struct rte_mempool *mbuf_pool; 225 struct rte_mempool *mbuf_pool_indir; 226 struct rte_mempool *session_pool; 227 struct rte_mempool *session_priv_pool; 228 }; 229 230 struct cnt_blk { 231 uint32_t salt; 232 uint64_t iv; 233 uint32_t cnt; 234 } __attribute__((packed)); 235 236 struct traffic_type { 237 const uint8_t *data[MAX_PKT_BURST * 2]; 238 struct rte_mbuf *pkts[MAX_PKT_BURST * 2]; 239 void *saptr[MAX_PKT_BURST * 2]; 240 uint32_t res[MAX_PKT_BURST * 2]; 241 uint32_t num; 242 }; 243 244 struct ipsec_traffic { 245 struct traffic_type ipsec; 246 struct traffic_type ip4; 247 struct traffic_type ip6; 248 }; 249 250 uint16_t 251 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 252 uint16_t nb_pkts, uint16_t len); 253 254 uint16_t 255 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 256 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len); 257 258 uint16_t 259 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 260 uint16_t len); 261 262 uint16_t 263 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 264 uint16_t len); 265 266 void 267 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 268 269 void 270 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 271 272 static inline uint16_t 273 ipsec_metadata_size(void) 274 { 275 return sizeof(struct ipsec_mbuf_metadata); 276 } 277 278 static inline struct ipsec_mbuf_metadata * 279 get_priv(struct rte_mbuf *m) 280 { 281 return rte_mbuf_to_priv(m); 282 } 283 284 static inline void * 285 get_cnt_blk(struct rte_mbuf *m) 286 { 287 struct ipsec_mbuf_metadata *priv = get_priv(m); 288 289 return &priv->buf[0]; 290 } 291 292 static inline void * 293 get_aad(struct rte_mbuf *m) 294 { 295 struct ipsec_mbuf_metadata *priv = get_priv(m); 296 297 return &priv->buf[16]; 298 } 299 300 static inline void * 301 get_sym_cop(struct rte_crypto_op *cop) 302 { 303 return (cop + 1); 304 } 305 306 static inline struct rte_ipsec_session * 307 ipsec_get_primary_session(struct ipsec_sa *sa) 308 { 309 return &sa->sessions[IPSEC_SESSION_PRIMARY]; 310 } 311 312 static inline struct rte_ipsec_session * 313 ipsec_get_fallback_session(struct ipsec_sa *sa) 314 { 315 return &sa->sessions[IPSEC_SESSION_FALLBACK]; 316 } 317 318 static inline enum rte_security_session_action_type 319 ipsec_get_action_type(struct ipsec_sa *sa) 320 { 321 struct rte_ipsec_session *ips; 322 ips = ipsec_get_primary_session(sa); 323 return ips->type; 324 } 325 326 int 327 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx); 328 329 void 330 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[], 331 void *sa[], uint16_t nb_pkts); 332 333 void 334 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[], 335 void *sa[], uint16_t nb_pkts); 336 337 void 338 sp4_init(struct socket_ctx *ctx, int32_t socket_id); 339 340 void 341 sp6_init(struct socket_ctx *ctx, int32_t socket_id); 342 343 /* 344 * Search through SP rules for given SPI. 345 * Returns first rule index if found(greater or equal then zero), 346 * or -ENOENT otherwise. 347 */ 348 int 349 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 350 uint32_t mask[2]); 351 int 352 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 353 uint32_t mask[2]); 354 355 /* 356 * Search through SA entries for given SPI. 357 * Returns first entry index if found(greater or equal then zero), 358 * or -ENOENT otherwise. 359 */ 360 int 361 sa_spi_present(struct sa_ctx *sa_ctx, uint32_t spi, int inbound); 362 363 void 364 sa_init(struct socket_ctx *ctx, int32_t socket_id); 365 366 void 367 rt_init(struct socket_ctx *ctx, int32_t socket_id); 368 369 int 370 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads, 371 uint64_t *tx_offloads); 372 373 int 374 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr); 375 376 void 377 enqueue_cop_burst(struct cdev_qp *cqp); 378 379 int 380 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa, 381 struct rte_ipsec_session *ips); 382 383 int 384 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa, 385 struct rte_ipsec_session *ips); 386 387 #endif /* __IPSEC_H__ */ 388