1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2020 Intel Corporation 3 */ 4 #include <sys/types.h> 5 #include <netinet/in.h> 6 #include <netinet/ip.h> 7 8 #include <rte_branch_prediction.h> 9 #include <rte_log.h> 10 #include <rte_cryptodev.h> 11 #include <rte_ethdev.h> 12 #include <rte_mbuf.h> 13 14 #include "ipsec.h" 15 #include "ipsec-secgw.h" 16 17 #define SATP_OUT_IPV4(t) \ 18 ((((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TRANS && \ 19 (((t) & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4)) || \ 20 ((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TUNLV4) 21 22 /* helper routine to free bulk of crypto-ops and related packets */ 23 static inline void 24 free_cops(struct rte_crypto_op *cop[], uint32_t n) 25 { 26 uint32_t i; 27 28 for (i = 0; i != n; i++) 29 rte_pktmbuf_free(cop[i]->sym->m_src); 30 } 31 32 /* helper routine to enqueue bulk of crypto ops */ 33 static inline void 34 enqueue_cop_bulk(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num) 35 { 36 uint32_t i, k, len, n; 37 38 len = cqp->len; 39 40 /* 41 * if cqp is empty and we have enough ops, 42 * then queue them to the PMD straightway. 43 */ 44 if (num >= RTE_DIM(cqp->buf) * 3 / 4 && len == 0) { 45 n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cop, num); 46 cqp->in_flight += n; 47 free_cops(cop + n, num - n); 48 return; 49 } 50 51 k = 0; 52 53 do { 54 n = RTE_DIM(cqp->buf) - len; 55 n = RTE_MIN(num - k, n); 56 57 /* put packets into cqp */ 58 for (i = 0; i != n; i++) 59 cqp->buf[len + i] = cop[k + i]; 60 61 len += n; 62 k += n; 63 64 /* if cqp is full then, enqueue crypto-ops to PMD */ 65 if (len == RTE_DIM(cqp->buf)) { 66 n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, 67 cqp->buf, len); 68 cqp->in_flight += n; 69 free_cops(cqp->buf + n, len - n); 70 len = 0; 71 } 72 73 74 } while (k != num); 75 76 cqp->len = len; 77 } 78 79 static inline int 80 fill_ipsec_session(struct rte_ipsec_session *ss, struct ipsec_ctx *ctx, 81 struct ipsec_sa *sa) 82 { 83 int32_t rc; 84 85 /* setup crypto section */ 86 if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE || 87 ss->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) { 88 RTE_ASSERT(ss->crypto.ses == NULL); 89 rc = create_lookaside_session(ctx, sa, ss); 90 if (rc != 0) 91 return rc; 92 /* setup session action type */ 93 } else if (ss->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) { 94 RTE_ASSERT(ss->security.ses == NULL); 95 rc = create_lookaside_session(ctx, sa, ss); 96 if (rc != 0) 97 return rc; 98 } else 99 RTE_ASSERT(0); 100 101 rc = rte_ipsec_session_prepare(ss); 102 if (rc != 0) 103 memset(ss, 0, sizeof(*ss)); 104 105 return rc; 106 } 107 108 /* 109 * group input packets byt the SA they belong to. 110 */ 111 static uint32_t 112 sa_group(void *sa_ptr[], struct rte_mbuf *pkts[], 113 struct rte_ipsec_group grp[], uint32_t num) 114 { 115 uint32_t i, n, spi; 116 void *sa; 117 void * const nosa = &spi; 118 119 sa = nosa; 120 grp[0].m = pkts; 121 for (i = 0, n = 0; i != num; i++) { 122 123 if (sa != sa_ptr[i]) { 124 grp[n].cnt = pkts + i - grp[n].m; 125 n += (sa != nosa); 126 grp[n].id.ptr = sa_ptr[i]; 127 grp[n].m = pkts + i; 128 sa = sa_ptr[i]; 129 } 130 } 131 132 /* terminate last group */ 133 if (sa != nosa) { 134 grp[n].cnt = pkts + i - grp[n].m; 135 n++; 136 } 137 138 return n; 139 } 140 141 /* 142 * helper function, splits processed packets into ipv4/ipv6 traffic. 143 */ 144 static inline void 145 copy_to_trf(struct ipsec_traffic *trf, uint64_t satp, struct rte_mbuf *mb[], 146 uint32_t num) 147 { 148 uint32_t j, ofs, s; 149 struct traffic_type *out; 150 151 /* 152 * determine traffic type(ipv4/ipv6) and offset for ACL classify 153 * based on SA type 154 */ 155 if ((satp & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) { 156 if ((satp & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) { 157 out = &trf->ip4; 158 ofs = offsetof(struct ip, ip_p); 159 } else { 160 out = &trf->ip6; 161 ofs = offsetof(struct ip6_hdr, ip6_nxt); 162 } 163 } else if (SATP_OUT_IPV4(satp)) { 164 out = &trf->ip4; 165 ofs = offsetof(struct ip, ip_p); 166 } else { 167 out = &trf->ip6; 168 ofs = offsetof(struct ip6_hdr, ip6_nxt); 169 } 170 171 for (j = 0, s = out->num; j != num; j++) { 172 out->data[s + j] = rte_pktmbuf_mtod_offset(mb[j], 173 void *, ofs); 174 out->pkts[s + j] = mb[j]; 175 } 176 177 out->num += num; 178 } 179 180 static uint32_t 181 ipsec_prepare_crypto_group(struct ipsec_ctx *ctx, struct ipsec_sa *sa, 182 struct rte_ipsec_session *ips, struct rte_mbuf **m, 183 unsigned int cnt) 184 { 185 struct cdev_qp *cqp; 186 struct rte_crypto_op *cop[cnt]; 187 uint32_t j, k; 188 struct ipsec_mbuf_metadata *priv; 189 190 cqp = &ctx->tbl[sa->cdev_id_qp]; 191 192 /* for that app each mbuf has it's own crypto op */ 193 for (j = 0; j != cnt; j++) { 194 priv = get_priv(m[j]); 195 cop[j] = &priv->cop; 196 /* 197 * this is just to satisfy inbound_sa_check() 198 * should be removed in future. 199 */ 200 priv->sa = sa; 201 } 202 203 /* prepare and enqueue crypto ops */ 204 k = rte_ipsec_pkt_crypto_prepare(ips, m, cop, cnt); 205 if (k != 0) 206 enqueue_cop_bulk(cqp, cop, k); 207 208 return k; 209 } 210 211 /* 212 * helper routine for inline and cpu(synchronous) processing 213 * this is just to satisfy inbound_sa_check() and get_hop_for_offload_pkt(). 214 * Should be removed in future. 215 */ 216 static inline void 217 prep_process_group(void *sa, struct rte_mbuf *mb[], uint32_t cnt) 218 { 219 uint32_t j; 220 struct ipsec_mbuf_metadata *priv; 221 222 for (j = 0; j != cnt; j++) { 223 priv = get_priv(mb[j]); 224 priv->sa = sa; 225 } 226 } 227 228 /* 229 * finish processing of packets successfully decrypted by an inline processor 230 */ 231 static uint32_t 232 ipsec_process_inline_group(struct rte_ipsec_session *ips, void *sa, 233 struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt) 234 { 235 uint64_t satp; 236 uint32_t k; 237 238 /* get SA type */ 239 satp = rte_ipsec_sa_type(ips->sa); 240 prep_process_group(sa, mb, cnt); 241 242 k = rte_ipsec_pkt_process(ips, mb, cnt); 243 copy_to_trf(trf, satp, mb, k); 244 return k; 245 } 246 247 /* 248 * process packets synchronously 249 */ 250 static uint32_t 251 ipsec_process_cpu_group(struct rte_ipsec_session *ips, void *sa, 252 struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt) 253 { 254 uint64_t satp; 255 uint32_t k; 256 257 /* get SA type */ 258 satp = rte_ipsec_sa_type(ips->sa); 259 prep_process_group(sa, mb, cnt); 260 261 k = rte_ipsec_pkt_cpu_prepare(ips, mb, cnt); 262 k = rte_ipsec_pkt_process(ips, mb, k); 263 copy_to_trf(trf, satp, mb, k); 264 return k; 265 } 266 267 /* 268 * Process ipsec packets. 269 * If packet belong to SA that is subject of inline-crypto, 270 * then process it immediately. 271 * Otherwise do necessary preparations and queue it to related 272 * crypto-dev queue. 273 */ 274 void 275 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf) 276 { 277 uint32_t i, k, n; 278 struct ipsec_sa *sa; 279 struct rte_ipsec_group *pg; 280 struct rte_ipsec_session *ips; 281 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)]; 282 283 n = sa_group(trf->ipsec.saptr, trf->ipsec.pkts, grp, trf->ipsec.num); 284 285 for (i = 0; i != n; i++) { 286 287 pg = grp + i; 288 sa = ipsec_mask_saptr(pg->id.ptr); 289 290 /* fallback to cryptodev with RX packets which inline 291 * processor was unable to process 292 */ 293 if (sa != NULL) 294 ips = (pg->id.val & IPSEC_SA_OFFLOAD_FALLBACK_FLAG) ? 295 ipsec_get_fallback_session(sa) : 296 ipsec_get_primary_session(sa); 297 298 /* no valid HW session for that SA, try to create one */ 299 if (sa == NULL || (ips->crypto.ses == NULL && 300 fill_ipsec_session(ips, ctx, sa) != 0)) 301 k = 0; 302 303 /* process packets inline */ 304 else { 305 switch (ips->type) { 306 /* enqueue packets to crypto dev */ 307 case RTE_SECURITY_ACTION_TYPE_NONE: 308 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL: 309 k = ipsec_prepare_crypto_group(ctx, sa, ips, 310 pg->m, pg->cnt); 311 break; 312 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO: 313 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL: 314 k = ipsec_process_inline_group(ips, sa, 315 trf, pg->m, pg->cnt); 316 break; 317 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO: 318 k = ipsec_process_cpu_group(ips, sa, 319 trf, pg->m, pg->cnt); 320 break; 321 default: 322 k = 0; 323 } 324 } 325 326 /* drop packets that cannot be enqueued/processed */ 327 if (k != pg->cnt) 328 free_pkts(pg->m + k, pg->cnt - k); 329 } 330 } 331 332 static inline uint32_t 333 cqp_dequeue(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num) 334 { 335 uint32_t n; 336 337 if (cqp->in_flight == 0) 338 return 0; 339 340 n = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, cop, num); 341 RTE_ASSERT(cqp->in_flight >= n); 342 cqp->in_flight -= n; 343 344 return n; 345 } 346 347 static inline uint32_t 348 ctx_dequeue(struct ipsec_ctx *ctx, struct rte_crypto_op *cop[], uint32_t num) 349 { 350 uint32_t i, n; 351 352 n = 0; 353 354 for (i = ctx->last_qp; n != num && i != ctx->nb_qps; i++) 355 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n); 356 357 for (i = 0; n != num && i != ctx->last_qp; i++) 358 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n); 359 360 ctx->last_qp = i; 361 return n; 362 } 363 364 /* 365 * dequeue packets from crypto-queues and finalize processing. 366 */ 367 void 368 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf) 369 { 370 uint64_t satp; 371 uint32_t i, k, n, ng; 372 struct rte_ipsec_session *ss; 373 struct traffic_type *out; 374 struct rte_ipsec_group *pg; 375 struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)]; 376 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)]; 377 378 trf->ip4.num = 0; 379 trf->ip6.num = 0; 380 381 out = &trf->ipsec; 382 383 /* dequeue completed crypto-ops */ 384 n = ctx_dequeue(ctx, cop, RTE_DIM(cop)); 385 if (n == 0) 386 return; 387 388 /* group them by ipsec session */ 389 ng = rte_ipsec_pkt_crypto_group((const struct rte_crypto_op **) 390 (uintptr_t)cop, out->pkts, grp, n); 391 392 /* process each group of packets */ 393 for (i = 0; i != ng; i++) { 394 395 pg = grp + i; 396 ss = pg->id.ptr; 397 satp = rte_ipsec_sa_type(ss->sa); 398 399 k = rte_ipsec_pkt_process(ss, pg->m, pg->cnt); 400 copy_to_trf(trf, satp, pg->m, k); 401 402 /* free bad packets, if any */ 403 free_pkts(pg->m + k, pg->cnt - k); 404 405 n -= pg->cnt; 406 } 407 408 /* we should never have packet with unknown SA here */ 409 RTE_VERIFY(n == 0); 410 } 411