New upstream version 17.11.4
[deb_dpdk.git] / lib / librte_security / rte_security.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 NXP.
5  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of NXP nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_malloc.h>
35 #include <rte_dev.h>
36
37 #include "rte_security.h"
38 #include "rte_security_driver.h"
39
40 struct rte_security_session *
41 rte_security_session_create(struct rte_security_ctx *instance,
42                             struct rte_security_session_conf *conf,
43                             struct rte_mempool *mp)
44 {
45         struct rte_security_session *sess = NULL;
46
47         if (conf == NULL)
48                 return NULL;
49
50         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_create, NULL);
51
52         if (rte_mempool_get(mp, (void **)&sess))
53                 return NULL;
54
55         if (instance->ops->session_create(instance->device, conf, sess, mp)) {
56                 rte_mempool_put(mp, (void *)sess);
57                 return NULL;
58         }
59         instance->sess_cnt++;
60
61         return sess;
62 }
63
64 int
65 rte_security_session_update(struct rte_security_ctx *instance,
66                             struct rte_security_session *sess,
67                             struct rte_security_session_conf *conf)
68 {
69         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_update, -ENOTSUP);
70         return instance->ops->session_update(instance->device, sess, conf);
71 }
72
73 int
74 rte_security_session_stats_get(struct rte_security_ctx *instance,
75                                struct rte_security_session *sess,
76                                struct rte_security_stats *stats)
77 {
78         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_stats_get, -ENOTSUP);
79         return instance->ops->session_stats_get(instance->device, sess, stats);
80 }
81
82 int
83 rte_security_session_destroy(struct rte_security_ctx *instance,
84                              struct rte_security_session *sess)
85 {
86         int ret;
87
88         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_destroy, -ENOTSUP);
89
90         if (instance->sess_cnt)
91                 instance->sess_cnt--;
92
93         ret = instance->ops->session_destroy(instance->device, sess);
94         if (!ret)
95                 rte_mempool_put(rte_mempool_from_obj(sess), (void *)sess);
96
97         return ret;
98 }
99
100 int
101 rte_security_set_pkt_metadata(struct rte_security_ctx *instance,
102                               struct rte_security_session *sess,
103                               struct rte_mbuf *m, void *params)
104 {
105         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->set_pkt_metadata, -ENOTSUP);
106         return instance->ops->set_pkt_metadata(instance->device,
107                                                sess, m, params);
108 }
109
110 const struct rte_security_capability *
111 rte_security_capabilities_get(struct rte_security_ctx *instance)
112 {
113         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL);
114         return instance->ops->capabilities_get(instance->device);
115 }
116
117 const struct rte_security_capability *
118 rte_security_capability_get(struct rte_security_ctx *instance,
119                             struct rte_security_capability_idx *idx)
120 {
121         const struct rte_security_capability *capabilities;
122         const struct rte_security_capability *capability;
123         uint16_t i = 0;
124
125         RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL);
126         capabilities = instance->ops->capabilities_get(instance->device);
127
128         if (capabilities == NULL)
129                 return NULL;
130
131         while ((capability = &capabilities[i++])->action
132                         != RTE_SECURITY_ACTION_TYPE_NONE) {
133                 if (capability->action  == idx->action &&
134                                 capability->protocol == idx->protocol) {
135                         if (idx->protocol == RTE_SECURITY_PROTOCOL_IPSEC) {
136                                 if (capability->ipsec.proto ==
137                                                 idx->ipsec.proto &&
138                                         capability->ipsec.mode ==
139                                                         idx->ipsec.mode &&
140                                         capability->ipsec.direction ==
141                                                         idx->ipsec.direction)
142                                         return capability;
143                         }
144                 }
145         }
146
147         return NULL;
148 }