ipsec: add Tx offload template into SA
[dpdk.git] / lib / librte_ipsec / sa.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #ifndef _SA_H_
6 #define _SA_H_
7
8 #include <rte_rwlock.h>
9
10 #define IPSEC_MAX_HDR_SIZE      64
11 #define IPSEC_MAX_IV_SIZE       16
12 #define IPSEC_MAX_IV_QWORD      (IPSEC_MAX_IV_SIZE / sizeof(uint64_t))
13
14 /* padding alignment for different algorithms */
15 enum {
16         IPSEC_PAD_DEFAULT = 4,
17         IPSEC_PAD_3DES_CBC = 8,
18         IPSEC_PAD_AES_CBC = IPSEC_MAX_IV_SIZE,
19         IPSEC_PAD_AES_CTR = IPSEC_PAD_DEFAULT,
20         IPSEC_PAD_AES_GCM = IPSEC_PAD_DEFAULT,
21         IPSEC_PAD_NULL = IPSEC_PAD_DEFAULT,
22 };
23
24 /* iv sizes for different algorithms */
25 enum {
26         IPSEC_IV_SIZE_DEFAULT = IPSEC_MAX_IV_SIZE,
27         IPSEC_AES_CTR_IV_SIZE = sizeof(uint64_t),
28         /* TripleDES supports IV size of 32bits or 64bits but he library
29          * only supports 64bits.
30          */
31         IPSEC_3DES_IV_SIZE = sizeof(uint64_t),
32 };
33
34 /* these definitions probably has to be in rte_crypto_sym.h */
35 union sym_op_ofslen {
36         uint64_t raw;
37         struct {
38                 uint32_t offset;
39                 uint32_t length;
40         };
41 };
42
43 union sym_op_data {
44 #ifdef __SIZEOF_INT128__
45         __uint128_t raw;
46 #endif
47         struct {
48                 uint8_t *va;
49                 rte_iova_t pa;
50         };
51 };
52
53 #define REPLAY_SQN_NUM          2
54 #define REPLAY_SQN_NEXT(n)      ((n) ^ 1)
55
56 struct replay_sqn {
57         rte_rwlock_t rwl;
58         uint64_t sqn;
59         __extension__ uint64_t window[0];
60 };
61
62 /*IPSEC SA supported algorithms */
63 enum sa_algo_type       {
64         ALGO_TYPE_NULL = 0,
65         ALGO_TYPE_3DES_CBC,
66         ALGO_TYPE_AES_CBC,
67         ALGO_TYPE_AES_CTR,
68         ALGO_TYPE_AES_GCM,
69         ALGO_TYPE_MAX
70 };
71
72 struct rte_ipsec_sa {
73
74         uint64_t type;     /* type of given SA */
75         uint64_t udata;    /* user defined */
76         uint32_t size;     /* size of given sa object */
77         uint32_t spi;
78         /* sqn calculations related */
79         uint64_t sqn_mask;
80         struct {
81                 uint32_t win_sz;
82                 uint16_t nb_bucket;
83                 uint16_t bucket_index_mask;
84         } replay;
85         /* template for crypto op fields */
86         struct {
87                 union sym_op_ofslen cipher;
88                 union sym_op_ofslen auth;
89         } ctp;
90         /* tx_offload template for tunnel mbuf */
91         struct {
92                 uint64_t msk;
93                 uint64_t val;
94         } tx_offload;
95         uint32_t salt;
96         uint8_t algo_type;
97         uint8_t proto;    /* next proto */
98         uint8_t aad_len;
99         uint8_t hdr_len;
100         uint8_t hdr_l3_off;
101         uint8_t icv_len;
102         uint8_t sqh_len;
103         uint8_t iv_ofs; /* offset for algo-specific IV inside crypto op */
104         uint8_t iv_len;
105         uint8_t pad_align;
106
107         /* template for tunnel header */
108         uint8_t hdr[IPSEC_MAX_HDR_SIZE];
109
110         /*
111          * sqn and replay window
112          * In case of SA handled by multiple threads *sqn* cacheline
113          * could be shared by multiple cores.
114          * To minimise perfomance impact, we try to locate in a separate
115          * place from other frequently accesed data.
116          */
117         union {
118                 union {
119                         rte_atomic64_t atom;
120                         uint64_t raw;
121                 } outb;
122                 struct {
123                         uint32_t rdidx; /* read index */
124                         uint32_t wridx; /* write index */
125                         struct replay_sqn *rsn[REPLAY_SQN_NUM];
126                 } inb;
127         } sqn;
128
129 } __rte_cache_aligned;
130
131 int
132 ipsec_sa_pkt_func_select(const struct rte_ipsec_session *ss,
133         const struct rte_ipsec_sa *sa, struct rte_ipsec_sa_pkt_func *pf);
134
135 #endif /* _SA_H_ */