ipsec: add inbound SAD API
[dpdk.git] / lib / librte_ipsec / rte_ipsec_sad.h
1
2 /* SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2019 Intel Corporation
4  */
5
6 #ifndef _RTE_IPSEC_SAD_H_
7 #define _RTE_IPSEC_SAD_H_
8
9 #include <rte_compat.h>
10
11 /**
12  * @file rte_ipsec_sad.h
13  * @b EXPERIMENTAL: this API may change without prior notice
14  *
15  * RTE IPsec security association database (SAD) support.
16  * Contains helper functions to lookup and maintain SAD
17  */
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 struct rte_ipsec_sad;
24
25 /** Type of key */
26 enum {
27         RTE_IPSEC_SAD_SPI_ONLY = 0,
28         RTE_IPSEC_SAD_SPI_DIP,
29         RTE_IPSEC_SAD_SPI_DIP_SIP,
30         RTE_IPSEC_SAD_KEY_TYPE_MASK,
31 };
32
33 struct rte_ipsec_sadv4_key {
34         uint32_t spi;
35         uint32_t dip;
36         uint32_t sip;
37 };
38
39 struct rte_ipsec_sadv6_key {
40         uint32_t spi;
41         uint8_t dip[16];
42         uint8_t sip[16];
43 };
44
45 union rte_ipsec_sad_key {
46         struct rte_ipsec_sadv4_key      v4;
47         struct rte_ipsec_sadv6_key      v6;
48 };
49
50 /** Flag to create SAD with ipv6 dip and sip addresses */
51 #define RTE_IPSEC_SAD_FLAG_IPV6                 0x1
52 /** Flag to support reader writer concurrency */
53 #define RTE_IPSEC_SAD_FLAG_RW_CONCURRENCY       0x2
54
55 /** IPsec SAD configuration structure */
56 struct rte_ipsec_sad_conf {
57         /** CPU socket ID where rte_ipsec_sad should be allocated */
58         int             socket_id;
59         /** maximum number of SA for each type of key */
60         uint32_t        max_sa[RTE_IPSEC_SAD_KEY_TYPE_MASK];
61         /** RTE_IPSEC_SAD_FLAG_* flags */
62         uint32_t        flags;
63 };
64
65 /**
66  * Add a rule into the SAD. Could be safely called with concurrent lookups
67  *  if RTE_IPSEC_SAD_FLAG_RW_CONCURRENCY flag was configured on creation time.
68  *  While with this flag multi-reader - one-writer model Is MT safe,
69  *  multi-writer model is not and required extra synchronisation.
70  *
71  * @param sad
72  *   SAD object handle
73  * @param key
74  *   pointer to the key
75  * @param key_type
76  *   key type (spi only/spi+dip/spi+dip+sip)
77  * @param sa
78  *   Pointer associated with the key to save in a SAD
79  *   Must be 4 bytes aligned.
80  * @return
81  *   0 on success, negative value otherwise
82  */
83 __rte_experimental
84 int
85 rte_ipsec_sad_add(struct rte_ipsec_sad *sad,
86         const union rte_ipsec_sad_key *key,
87         int key_type, void *sa);
88
89 /**
90  * Delete a rule from the SAD. Could be safely called with concurrent lookups
91  *  if RTE_IPSEC_SAD_FLAG_RW_CONCURRENCY flag was configured on creation time.
92  *  While with this flag multi-reader - one-writer model Is MT safe,
93  *  multi-writer model is not and required extra synchronisation.
94  *
95  * @param sad
96  *   SAD object handle
97  * @param key
98  *   pointer to the key
99  * @param key_type
100  *   key type (spi only/spi+dip/spi+dip+sip)
101  * @return
102  *   0 on success, negative value otherwise
103  */
104 __rte_experimental
105 int
106 rte_ipsec_sad_del(struct rte_ipsec_sad *sad,
107         const union rte_ipsec_sad_key *key,
108         int key_type);
109 /*
110  * Create SAD
111  *
112  * @param name
113  *  SAD name
114  * @param conf
115  *  Structure containing the configuration
116  * @return
117  *  Handle to SAD object on success
118  *  NULL otherwise with rte_errno set to an appropriate values.
119  */
120 __rte_experimental
121 struct rte_ipsec_sad *
122 rte_ipsec_sad_create(const char *name, const struct rte_ipsec_sad_conf *conf);
123
124 /**
125  * Find an existing SAD object and return a pointer to it.
126  *
127  * @param name
128  *  Name of the SAD object as passed to rte_ipsec_sad_create()
129  * @return
130  *  Pointer to sad object or NULL if object not found with rte_errno
131  *  set appropriately. Possible rte_errno values include:
132  *   - ENOENT - required entry not available to return.
133  */
134 __rte_experimental
135 struct rte_ipsec_sad *
136 rte_ipsec_sad_find_existing(const char *name);
137
138 /**
139  * Destroy SAD object.
140  *
141  * @param sad
142  *   pointer to the SAD object
143  * @return
144  *   None
145  */
146 __rte_experimental
147 void
148 rte_ipsec_sad_destroy(struct rte_ipsec_sad *sad);
149
150 /**
151  * Lookup multiple keys in the SAD.
152  *
153  * @param sad
154  *   SAD object handle
155  * @param keys
156  *   Array of keys to be looked up in the SAD
157  * @param sa
158  *   Pointer assocoated with the keys.
159  *   If the lookup for the given key failed, then corresponding sa
160  *   will be NULL
161  * @param n
162  *   Number of elements in keys array to lookup.
163  *  @return
164  *   -EINVAL for incorrect arguments, otherwise number of successful lookups.
165  */
166 __rte_experimental
167 int
168 rte_ipsec_sad_lookup(const struct rte_ipsec_sad *sad,
169         const union rte_ipsec_sad_key *keys[],
170         void *sa[], uint32_t n);
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif /* _RTE_IPSEC_SAD_H_ */