Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : :
5 : : #ifndef __CN9K_IPSEC_LA_OPS_H__
6 : : #define __CN9K_IPSEC_LA_OPS_H__
7 : :
8 : : #include <rte_crypto_sym.h>
9 : : #include <rte_esp.h>
10 : : #include <rte_security.h>
11 : :
12 : : #include "roc_ie.h"
13 : :
14 : : #include "cn9k_ipsec.h"
15 : : #include "cnxk_cryptodev_ops.h"
16 : : #include "cnxk_security_ar.h"
17 : : #include "cnxk_sg.h"
18 : :
19 : : static __rte_always_inline int32_t
20 : : ipsec_po_out_rlen_get(struct cn9k_sec_session *sess, uint32_t plen, struct rte_mbuf *m_src)
21 : : {
22 : : uint32_t enc_payload_len;
23 : : int adj_len = 0;
24 : :
25 : 0 : if (sess->sa.out_sa.common_sa.ctl.ipsec_mode == ROC_IE_SA_MODE_TRANSPORT) {
26 : : adj_len = ROC_CPT_TUNNEL_IPV4_HDR_LEN;
27 : :
28 : 0 : uintptr_t data = (uintptr_t)m_src->buf_addr + m_src->data_off;
29 : 0 : struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)data;
30 : :
31 [ # # ]: 0 : if (unlikely(ip->version != IPVERSION)) {
32 : : struct rte_ipv6_hdr *ip6 = (struct rte_ipv6_hdr *)ip;
33 : : uint8_t *nxt_hdr = (uint8_t *)ip6;
34 : : uint8_t dest_op_cnt = 0;
35 : 0 : int nh = ip6->proto;
36 : :
37 : : PLT_ASSERT(ip->version == 6);
38 : :
39 : : adj_len = ROC_CPT_TUNNEL_IPV6_HDR_LEN;
40 : 0 : nxt_hdr += ROC_CPT_TUNNEL_IPV6_HDR_LEN;
41 [ # # ]: 0 : while (nh != -EINVAL) {
42 : 0 : size_t ext_len = 0;
43 : :
44 : 0 : nh = rte_ipv6_get_next_ext(nxt_hdr, nh, &ext_len);
45 : : /* With multiple dest ops headers, the ESP hdr will be before
46 : : * the 2nd dest ops and after the first dest ops header
47 : : */
48 [ # # ]: 0 : if ((nh == IPPROTO_DSTOPTS) && dest_op_cnt)
49 : : break;
50 [ # # ]: 0 : else if (nh == IPPROTO_DSTOPTS)
51 : 0 : dest_op_cnt++;
52 : 0 : adj_len += ext_len;
53 : 0 : nxt_hdr += ext_len;
54 : : }
55 : : }
56 : : }
57 : :
58 : 0 : enc_payload_len =
59 : 0 : RTE_ALIGN_CEIL(plen + sess->rlens.roundup_len - adj_len, sess->rlens.roundup_byte);
60 : :
61 : 0 : return sess->custom_hdr_len + sess->rlens.partial_len + enc_payload_len + adj_len;
62 : : }
63 : :
64 : : static __rte_always_inline int
65 : : process_outb_sa(struct cpt_qp_meta_info *m_info, struct rte_crypto_op *cop,
66 : : struct cn9k_sec_session *sess, struct cpt_inst_s *inst,
67 : : struct cpt_inflight_req *infl_req)
68 : : {
69 : 0 : const unsigned int hdr_len = sess->custom_hdr_len;
70 : : struct rte_crypto_sym_op *sym_op = cop->sym;
71 : 0 : struct rte_mbuf *m_src = sym_op->m_src;
72 : : uint32_t dlen, rlen, pkt_len, seq_lo;
73 : 0 : uint16_t data_off = m_src->data_off;
74 : : struct roc_ie_on_outb_hdr *hdr;
75 : : int32_t extend_tail;
76 : : uint64_t esn;
77 : :
78 : 0 : pkt_len = rte_pktmbuf_pkt_len(m_src);
79 [ # # ]: 0 : dlen = pkt_len + hdr_len;
80 : : rlen = ipsec_po_out_rlen_get(sess, pkt_len, m_src);
81 : :
82 : 0 : extend_tail = rlen - dlen;
83 : 0 : pkt_len += extend_tail;
84 : :
85 [ # # ]: 0 : if (likely(m_src->next == NULL)) {
86 [ # # ]: 0 : if (unlikely(extend_tail > rte_pktmbuf_tailroom(m_src))) {
87 : 0 : plt_dp_err("Not enough tail room (required: %d, available: %d)",
88 : : extend_tail, rte_pktmbuf_tailroom(m_src));
89 : 0 : return -ENOMEM;
90 : : }
91 : :
92 [ # # ]: 0 : if (unlikely(hdr_len > data_off)) {
93 : 0 : plt_dp_err("Not enough head room (required: %d, available: %d)", hdr_len,
94 : : rte_pktmbuf_headroom(m_src));
95 : 0 : return -ENOMEM;
96 : : }
97 : :
98 : 0 : m_src->data_len = pkt_len;
99 : :
100 : 0 : hdr = PLT_PTR_ADD(m_src->buf_addr, data_off - hdr_len);
101 : :
102 : 0 : inst->dptr = PLT_U64_CAST(hdr);
103 : 0 : inst->w4.u64 = sess->inst.w4 | dlen;
104 : : } else {
105 : : struct roc_sglist_comp *scatter_comp, *gather_comp;
106 : : uint32_t g_size_bytes, s_size_bytes;
107 : : struct rte_mbuf *last_seg;
108 : : uint8_t *in_buffer;
109 : : void *m_data;
110 : : int i;
111 : :
112 : 0 : last_seg = rte_pktmbuf_lastseg(m_src);
113 : :
114 [ # # ]: 0 : if (unlikely(extend_tail > rte_pktmbuf_tailroom(last_seg))) {
115 : 0 : plt_dp_err("Not enough tail room (required: %d, available: %d)",
116 : : extend_tail, rte_pktmbuf_tailroom(last_seg));
117 : 0 : return -ENOMEM;
118 : : }
119 : :
120 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
121 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
122 : 0 : plt_dp_err("Error allocating meta buffer for request");
123 : 0 : return -ENOMEM;
124 : : }
125 : :
126 : : hdr = m_data;
127 : :
128 : 0 : m_data = (uint8_t *)m_data + hdr_len;
129 : : in_buffer = m_data;
130 : :
131 : 0 : ((uint16_t *)in_buffer)[0] = 0;
132 : 0 : ((uint16_t *)in_buffer)[1] = 0;
133 : :
134 : : /*
135 : : * Input Gather List
136 : : */
137 : : i = 0;
138 : 0 : gather_comp = (struct roc_sglist_comp *)((uint8_t *)m_data + 8);
139 : :
140 [ # # ]: 0 : i = fill_sg_comp(gather_comp, i, (uint64_t)hdr, hdr_len);
141 : 0 : i = fill_ipsec_sg_comp_from_pkt(gather_comp, i, m_src);
142 [ # # ]: 0 : ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
143 : :
144 : 0 : g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
145 : :
146 : : /*
147 : : * output Scatter List
148 : : */
149 : 0 : last_seg->data_len += extend_tail;
150 : :
151 : : i = 0;
152 [ # # ]: 0 : scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
153 : :
154 : : i = fill_sg_comp(scatter_comp, i, (uint64_t)hdr, hdr_len);
155 : 0 : i = fill_ipsec_sg_comp_from_pkt(scatter_comp, i, m_src);
156 [ # # ]: 0 : ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
157 : :
158 : 0 : s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
159 : :
160 : 0 : dlen = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
161 : :
162 : 0 : inst->dptr = (uint64_t)in_buffer;
163 : :
164 : 0 : inst->w4.u64 = sess->inst.w4 | dlen;
165 : 0 : inst->w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
166 : : }
167 : :
168 : : #ifdef LA_IPSEC_DEBUG
169 : : if (sess->inst.w4 & ROC_IE_ON_PER_PKT_IV) {
170 : : memcpy(&hdr->iv[0],
171 : : rte_crypto_op_ctod_offset(cop, uint8_t *, sess->cipher_iv_off),
172 : : sess->cipher_iv_len);
173 : : }
174 : : #endif
175 : :
176 : 0 : m_src->pkt_len = pkt_len;
177 : 0 : esn = ++sess->esn;
178 : :
179 : : /* Set ESN seq hi */
180 [ # # ]: 0 : hdr->esn = rte_cpu_to_be_32(esn >> 32);
181 : :
182 : : /* Set ESN seq lo */
183 [ # # ]: 0 : seq_lo = rte_cpu_to_be_32(esn & (BIT_ULL(32) - 1));
184 : 0 : hdr->seq = seq_lo;
185 : :
186 : : /* Set IPID same as seq_lo */
187 : 0 : hdr->ip_id = seq_lo;
188 : :
189 : : /* Prepare CPT instruction */
190 : 0 : inst->w7.u64 = sess->inst.w7;
191 : :
192 : 0 : return 0;
193 : : }
194 : :
195 : : static __rte_always_inline int
196 : : process_inb_sa(struct cpt_qp_meta_info *m_info, struct rte_crypto_op *cop,
197 : : struct cn9k_sec_session *sess, struct cpt_inst_s *inst,
198 : : struct cpt_inflight_req *infl_req)
199 : : {
200 : : const unsigned int hdr_len = ROC_IE_ON_INB_RPTR_HDR;
201 : : struct rte_crypto_sym_op *sym_op = cop->sym;
202 : 0 : struct rte_mbuf *m_src = sym_op->m_src;
203 : : struct roc_ie_on_inb_hdr *hdr;
204 : : uint32_t dlen;
205 : :
206 : 0 : infl_req->op_flags |= CPT_OP_FLAGS_IPSEC_DIR_INBOUND;
207 [ # # ]: 0 : if (likely(m_src->next == NULL)) {
208 : 0 : dlen = rte_pktmbuf_pkt_len(m_src);
209 : 0 : inst->dptr = rte_pktmbuf_mtod(m_src, uint64_t);
210 : 0 : inst->w4.u64 = sess->inst.w4 | dlen;
211 : : } else {
212 : : struct roc_sglist_comp *scatter_comp, *gather_comp;
213 : : uint32_t g_size_bytes, s_size_bytes;
214 : : uint8_t *in_buffer;
215 : : void *m_data;
216 : : int i;
217 : :
218 [ # # ]: 0 : m_data = alloc_op_meta(NULL, m_info->mlen, m_info->pool, infl_req);
219 [ # # ]: 0 : if (unlikely(m_data == NULL)) {
220 : 0 : plt_dp_err("Error allocating meta buffer for request");
221 : 0 : return -ENOMEM;
222 : : }
223 : :
224 : : hdr = m_data;
225 : :
226 : 0 : m_data = (uint8_t *)m_data + hdr_len;
227 : : in_buffer = m_data;
228 : :
229 : 0 : ((uint16_t *)in_buffer)[0] = 0;
230 : 0 : ((uint16_t *)in_buffer)[1] = 0;
231 : :
232 : : /*
233 : : * Input Gather List
234 : : */
235 : : i = 0;
236 : 0 : gather_comp = (struct roc_sglist_comp *)((uint8_t *)m_data + 8);
237 : 0 : i = fill_ipsec_sg_comp_from_pkt(gather_comp, i, m_src);
238 [ # # ]: 0 : ((uint16_t *)in_buffer)[2] = rte_cpu_to_be_16(i);
239 : :
240 : 0 : g_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
241 : :
242 : : /*
243 : : * Output Scatter List
244 : : */
245 : : i = 0;
246 : 0 : scatter_comp = (struct roc_sglist_comp *)((uint8_t *)gather_comp + g_size_bytes);
247 [ # # ]: 0 : i = fill_sg_comp(scatter_comp, i, (uint64_t)hdr, hdr_len);
248 : 0 : i = fill_ipsec_sg_comp_from_pkt(scatter_comp, i, m_src);
249 [ # # ]: 0 : ((uint16_t *)in_buffer)[3] = rte_cpu_to_be_16(i);
250 : :
251 : 0 : s_size_bytes = ((i + 3) / 4) * sizeof(struct roc_sglist_comp);
252 : :
253 : 0 : dlen = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
254 : :
255 : 0 : inst->dptr = (uint64_t)in_buffer;
256 : 0 : inst->w4.u64 = sess->inst.w4 | dlen;
257 : 0 : inst->w4.s.opcode_major |= (uint64_t)ROC_DMA_MODE_SG;
258 : : }
259 : :
260 : : /* Prepare CPT instruction */
261 : 0 : inst->w7.u64 = sess->inst.w7;
262 : :
263 [ # # ]: 0 : if (unlikely(sess->replay_win_sz))
264 : 0 : infl_req->op_flags |= CPT_OP_FLAGS_IPSEC_INB_REPLAY;
265 : :
266 : : return 0;
267 : : }
268 : : #endif /* __CN9K_IPSEC_LA_OPS_H__ */
|