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 IPSEC_SA_MAX_ENTRIES (128) /* must be power of 2, max 2 power 30 */ 41 #define SPI2IDX(spi) (spi & (IPSEC_SA_MAX_ENTRIES - 1)) 42 #define INVALID_SPI (0) 43 44 #define DISCARD INVALID_SPI 45 #define BYPASS UINT32_MAX 46 47 #define IPSEC_XFORM_MAX 2 48 49 #define IP6_VERSION (6) 50 51 struct rte_crypto_xform; 52 struct ipsec_xform; 53 struct rte_mbuf; 54 55 struct ipsec_sa; 56 57 typedef int32_t (*ipsec_xform_fn)(struct rte_mbuf *m, struct ipsec_sa *sa, 58 struct rte_crypto_op *cop); 59 60 struct ip_addr { 61 union { 62 uint32_t ip4; 63 union { 64 uint64_t ip6[2]; 65 uint8_t ip6_b[16]; 66 } ip6; 67 } ip; 68 }; 69 70 #define MAX_KEY_SIZE 32 71 72 /* 73 * application wide SA parameters 74 */ 75 struct app_sa_prm { 76 uint32_t enable; /* use librte_ipsec API for ipsec pkt processing */ 77 uint32_t window_size; /* replay window size */ 78 uint32_t enable_esn; /* enable/disable ESN support */ 79 uint64_t flags; /* rte_ipsec_sa_prm.flags */ 80 }; 81 82 extern struct app_sa_prm app_sa_prm; 83 84 enum { 85 IPSEC_SESSION_PRIMARY = 0, 86 IPSEC_SESSION_FALLBACK = 1, 87 IPSEC_SESSION_MAX 88 }; 89 90 #define IPSEC_SA_OFFLOAD_FALLBACK_FLAG (1) 91 92 static inline struct ipsec_sa * 93 ipsec_mask_saptr(void *ptr) 94 { 95 uintptr_t i = (uintptr_t)ptr; 96 static const uintptr_t mask = IPSEC_SA_OFFLOAD_FALLBACK_FLAG; 97 98 i &= ~mask; 99 100 return (struct ipsec_sa *)i; 101 } 102 103 struct ipsec_sa { 104 struct rte_ipsec_session sessions[IPSEC_SESSION_MAX]; 105 uint32_t spi; 106 uint32_t cdev_id_qp; 107 uint64_t seq; 108 uint32_t salt; 109 uint32_t fallback_sessions; 110 enum rte_crypto_cipher_algorithm cipher_algo; 111 enum rte_crypto_auth_algorithm auth_algo; 112 enum rte_crypto_aead_algorithm aead_algo; 113 uint16_t digest_len; 114 uint16_t iv_len; 115 uint16_t block_size; 116 uint16_t flags; 117 #define IP4_TUNNEL (1 << 0) 118 #define IP6_TUNNEL (1 << 1) 119 #define TRANSPORT (1 << 2) 120 #define IP4_TRANSPORT (1 << 3) 121 #define IP6_TRANSPORT (1 << 4) 122 struct ip_addr src; 123 struct ip_addr dst; 124 uint8_t cipher_key[MAX_KEY_SIZE]; 125 uint16_t cipher_key_len; 126 uint8_t auth_key[MAX_KEY_SIZE]; 127 uint16_t auth_key_len; 128 uint16_t aad_len; 129 union { 130 struct rte_crypto_sym_xform *xforms; 131 struct rte_security_ipsec_xform *sec_xform; 132 }; 133 enum rte_security_ipsec_sa_direction direction; 134 uint16_t portid; 135 136 #define MAX_RTE_FLOW_PATTERN (4) 137 #define MAX_RTE_FLOW_ACTIONS (3) 138 struct rte_flow_item pattern[MAX_RTE_FLOW_PATTERN]; 139 struct rte_flow_action action[MAX_RTE_FLOW_ACTIONS]; 140 struct rte_flow_attr attr; 141 union { 142 struct rte_flow_item_ipv4 ipv4_spec; 143 struct rte_flow_item_ipv6 ipv6_spec; 144 }; 145 struct rte_flow_item_esp esp_spec; 146 struct rte_flow *flow; 147 struct rte_security_session_conf sess_conf; 148 } __rte_cache_aligned; 149 150 struct ipsec_mbuf_metadata { 151 struct ipsec_sa *sa; 152 struct rte_crypto_op cop; 153 struct rte_crypto_sym_op sym_cop; 154 uint8_t buf[32]; 155 } __rte_cache_aligned; 156 157 #define IS_TRANSPORT(flags) ((flags) & TRANSPORT) 158 159 #define IS_TUNNEL(flags) ((flags) & (IP4_TUNNEL | IP6_TUNNEL)) 160 161 #define IS_IP4(flags) ((flags) & (IP4_TUNNEL | IP4_TRANSPORT)) 162 163 #define IS_IP6(flags) ((flags) & (IP6_TUNNEL | IP6_TRANSPORT)) 164 165 #define IS_IP4_TUNNEL(flags) ((flags) & IP4_TUNNEL) 166 167 #define IS_IP6_TUNNEL(flags) ((flags) & IP6_TUNNEL) 168 169 /* 170 * Macro for getting ipsec_sa flags statuses without version of protocol 171 * used for transport (IP4_TRANSPORT and IP6_TRANSPORT flags). 172 */ 173 #define WITHOUT_TRANSPORT_VERSION(flags) \ 174 ((flags) & (IP4_TUNNEL | \ 175 IP6_TUNNEL | \ 176 TRANSPORT)) 177 178 struct cdev_qp { 179 uint16_t id; 180 uint16_t qp; 181 uint16_t in_flight; 182 uint16_t len; 183 struct rte_crypto_op *buf[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 184 }; 185 186 struct ipsec_ctx { 187 struct rte_hash *cdev_map; 188 struct sp_ctx *sp4_ctx; 189 struct sp_ctx *sp6_ctx; 190 struct sa_ctx *sa_ctx; 191 uint16_t nb_qps; 192 uint16_t last_qp; 193 struct cdev_qp tbl[MAX_QP_PER_LCORE]; 194 struct rte_mempool *session_pool; 195 struct rte_mempool *session_priv_pool; 196 struct rte_mbuf *ol_pkts[MAX_PKT_BURST] __rte_aligned(sizeof(void *)); 197 uint16_t ol_pkts_cnt; 198 uint64_t ipv4_offloads; 199 uint64_t ipv6_offloads; 200 }; 201 202 struct cdev_key { 203 uint16_t lcore_id; 204 uint8_t cipher_algo; 205 uint8_t auth_algo; 206 uint8_t aead_algo; 207 }; 208 209 struct socket_ctx { 210 struct sa_ctx *sa_in; 211 struct sa_ctx *sa_out; 212 struct sp_ctx *sp_ip4_in; 213 struct sp_ctx *sp_ip4_out; 214 struct sp_ctx *sp_ip6_in; 215 struct sp_ctx *sp_ip6_out; 216 struct rt_ctx *rt_ip4; 217 struct rt_ctx *rt_ip6; 218 struct rte_mempool *mbuf_pool; 219 struct rte_mempool *mbuf_pool_indir; 220 struct rte_mempool *session_pool; 221 struct rte_mempool *session_priv_pool; 222 }; 223 224 struct cnt_blk { 225 uint32_t salt; 226 uint64_t iv; 227 uint32_t cnt; 228 } __attribute__((packed)); 229 230 struct traffic_type { 231 const uint8_t *data[MAX_PKT_BURST * 2]; 232 struct rte_mbuf *pkts[MAX_PKT_BURST * 2]; 233 void *saptr[MAX_PKT_BURST * 2]; 234 uint32_t res[MAX_PKT_BURST * 2]; 235 uint32_t num; 236 }; 237 238 struct ipsec_traffic { 239 struct traffic_type ipsec; 240 struct traffic_type ip4; 241 struct traffic_type ip6; 242 }; 243 244 uint16_t 245 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 246 uint16_t nb_pkts, uint16_t len); 247 248 uint16_t 249 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 250 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len); 251 252 uint16_t 253 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 254 uint16_t len); 255 256 uint16_t 257 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 258 uint16_t len); 259 260 void 261 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 262 263 void 264 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf); 265 266 static inline uint16_t 267 ipsec_metadata_size(void) 268 { 269 return sizeof(struct ipsec_mbuf_metadata); 270 } 271 272 static inline struct ipsec_mbuf_metadata * 273 get_priv(struct rte_mbuf *m) 274 { 275 return rte_mbuf_to_priv(m); 276 } 277 278 static inline void * 279 get_cnt_blk(struct rte_mbuf *m) 280 { 281 struct ipsec_mbuf_metadata *priv = get_priv(m); 282 283 return &priv->buf[0]; 284 } 285 286 static inline void * 287 get_aad(struct rte_mbuf *m) 288 { 289 struct ipsec_mbuf_metadata *priv = get_priv(m); 290 291 return &priv->buf[16]; 292 } 293 294 static inline void * 295 get_sym_cop(struct rte_crypto_op *cop) 296 { 297 return (cop + 1); 298 } 299 300 static inline struct rte_ipsec_session * 301 ipsec_get_primary_session(struct ipsec_sa *sa) 302 { 303 return &sa->sessions[IPSEC_SESSION_PRIMARY]; 304 } 305 306 static inline struct rte_ipsec_session * 307 ipsec_get_fallback_session(struct ipsec_sa *sa) 308 { 309 return &sa->sessions[IPSEC_SESSION_FALLBACK]; 310 } 311 312 static inline enum rte_security_session_action_type 313 ipsec_get_action_type(struct ipsec_sa *sa) 314 { 315 struct rte_ipsec_session *ips; 316 ips = ipsec_get_primary_session(sa); 317 return ips->type; 318 } 319 320 int 321 inbound_sa_check(struct sa_ctx *sa_ctx, struct rte_mbuf *m, uint32_t sa_idx); 322 323 void 324 inbound_sa_lookup(struct sa_ctx *sa_ctx, struct rte_mbuf *pkts[], 325 void *sa[], uint16_t nb_pkts); 326 327 void 328 outbound_sa_lookup(struct sa_ctx *sa_ctx, uint32_t sa_idx[], 329 void *sa[], uint16_t nb_pkts); 330 331 void 332 sp4_init(struct socket_ctx *ctx, int32_t socket_id); 333 334 void 335 sp6_init(struct socket_ctx *ctx, int32_t socket_id); 336 337 /* 338 * Search through SP rules for given SPI. 339 * Returns first rule index if found(greater or equal then zero), 340 * or -ENOENT otherwise. 341 */ 342 int 343 sp4_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 344 uint32_t mask[2]); 345 int 346 sp6_spi_present(uint32_t spi, int inbound, struct ip_addr ip_addr[2], 347 uint32_t mask[2]); 348 349 /* 350 * Search through SA entries for given SPI. 351 * Returns first entry index if found(greater or equal then zero), 352 * or -ENOENT otherwise. 353 */ 354 int 355 sa_spi_present(uint32_t spi, int inbound); 356 357 void 358 sa_init(struct socket_ctx *ctx, int32_t socket_id); 359 360 void 361 rt_init(struct socket_ctx *ctx, int32_t socket_id); 362 363 int 364 sa_check_offloads(uint16_t port_id, uint64_t *rx_offloads, 365 uint64_t *tx_offloads); 366 367 int 368 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr); 369 370 void 371 enqueue_cop_burst(struct cdev_qp *cqp); 372 373 int 374 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa, 375 struct rte_ipsec_session *ips); 376 377 int 378 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa, 379 struct rte_ipsec_session *ips); 380 381 #endif /* __IPSEC_H__ */ 382