1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
4 #include <net/inet6_hashtables.h>
5 #include "en_accel/en_accel.h"
6 #include "en_accel/tls.h"
7 #include "en_accel/ktls_txrx.h"
8 #include "en_accel/ktls_utils.h"
9 #include "en_accel/fs_tcp.h"
12 struct work_struct work;
13 struct mlx5e_priv *priv;
14 struct mlx5_flow_handle *rule;
17 #define PROGRESS_PARAMS_WRITE_UNIT 64
18 #define PROGRESS_PARAMS_PADDED_SIZE \
19 (ALIGN(sizeof(struct mlx5_wqe_tls_progress_params_seg), \
20 PROGRESS_PARAMS_WRITE_UNIT))
22 struct mlx5e_ktls_rx_resync_buf {
24 struct mlx5_wqe_tls_progress_params_seg progress;
25 u8 pad[PROGRESS_PARAMS_PADDED_SIZE];
26 } ____cacheline_aligned_in_smp;
28 struct mlx5e_ktls_offload_context_rx *priv_rx;
32 MLX5E_PRIV_RX_FLAG_DELETING,
33 MLX5E_NUM_PRIV_RX_FLAGS,
36 struct mlx5e_ktls_rx_resync_ctx {
37 struct tls_offload_resync_async core;
38 struct work_struct work;
39 struct mlx5e_priv *priv;
45 struct mlx5e_ktls_offload_context_rx {
46 struct tls12_crypto_info_aes_gcm_128 crypto_info;
47 struct accel_rule rule;
49 struct mlx5e_rq_stats *stats;
50 struct completion add_ctx;
54 DECLARE_BITMAP(flags, MLX5E_NUM_PRIV_RX_FLAGS);
57 struct mlx5e_ktls_rx_resync_ctx resync;
60 static bool mlx5e_ktls_priv_rx_put(struct mlx5e_ktls_offload_context_rx *priv_rx)
62 if (!refcount_dec_and_test(&priv_rx->resync.refcnt))
69 static void mlx5e_ktls_priv_rx_get(struct mlx5e_ktls_offload_context_rx *priv_rx)
71 refcount_inc(&priv_rx->resync.refcnt);
74 static int mlx5e_ktls_create_tir(struct mlx5_core_dev *mdev, u32 *tirn, u32 rqtn)
80 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
81 in = kvzalloc(inlen, GFP_KERNEL);
85 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
87 MLX5_SET(tirc, tirc, transport_domain, mdev->mlx5e_res.hw_objs.td.tdn);
88 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
89 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
90 MLX5_SET(tirc, tirc, indirect_table, rqtn);
91 MLX5_SET(tirc, tirc, tls_en, 1);
92 MLX5_SET(tirc, tirc, self_lb_block,
93 MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST |
94 MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST);
96 err = mlx5_core_create_tir(mdev, in, tirn);
102 static void accel_rule_handle_work(struct work_struct *work)
104 struct mlx5e_ktls_offload_context_rx *priv_rx;
105 struct accel_rule *accel_rule;
106 struct mlx5_flow_handle *rule;
108 accel_rule = container_of(work, struct accel_rule, work);
109 priv_rx = container_of(accel_rule, struct mlx5e_ktls_offload_context_rx, rule);
110 if (unlikely(test_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags)))
113 rule = mlx5e_accel_fs_add_sk(accel_rule->priv, priv_rx->sk,
114 priv_rx->tirn, MLX5_FS_DEFAULT_FLOW_TAG);
115 if (!IS_ERR_OR_NULL(rule))
116 accel_rule->rule = rule;
118 complete(&priv_rx->add_ctx);
121 static void accel_rule_init(struct accel_rule *rule, struct mlx5e_priv *priv,
124 INIT_WORK(&rule->work, accel_rule_handle_work);
128 static void icosq_fill_wi(struct mlx5e_icosq *sq, u16 pi,
129 struct mlx5e_icosq_wqe_info *wi)
131 sq->db.wqe_info[pi] = *wi;
134 static struct mlx5_wqe_ctrl_seg *
135 post_static_params(struct mlx5e_icosq *sq,
136 struct mlx5e_ktls_offload_context_rx *priv_rx)
138 struct mlx5e_set_tls_static_params_wqe *wqe;
139 struct mlx5e_icosq_wqe_info wi;
140 u16 pi, num_wqebbs, room;
142 num_wqebbs = MLX5E_TLS_SET_STATIC_PARAMS_WQEBBS;
143 room = mlx5e_stop_room_for_wqe(num_wqebbs);
144 if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, room)))
145 return ERR_PTR(-ENOSPC);
147 pi = mlx5e_icosq_get_next_pi(sq, num_wqebbs);
148 wqe = MLX5E_TLS_FETCH_SET_STATIC_PARAMS_WQE(sq, pi);
149 mlx5e_ktls_build_static_params(wqe, sq->pc, sq->sqn, &priv_rx->crypto_info,
150 priv_rx->tirn, priv_rx->key_id,
151 priv_rx->resync.seq, false,
152 TLS_OFFLOAD_CTX_DIR_RX);
153 wi = (struct mlx5e_icosq_wqe_info) {
154 .wqe_type = MLX5E_ICOSQ_WQE_UMR_TLS,
155 .num_wqebbs = num_wqebbs,
156 .tls_set_params.priv_rx = priv_rx,
158 icosq_fill_wi(sq, pi, &wi);
159 sq->pc += num_wqebbs;
164 static struct mlx5_wqe_ctrl_seg *
165 post_progress_params(struct mlx5e_icosq *sq,
166 struct mlx5e_ktls_offload_context_rx *priv_rx,
167 u32 next_record_tcp_sn)
169 struct mlx5e_set_tls_progress_params_wqe *wqe;
170 struct mlx5e_icosq_wqe_info wi;
171 u16 pi, num_wqebbs, room;
173 num_wqebbs = MLX5E_TLS_SET_PROGRESS_PARAMS_WQEBBS;
174 room = mlx5e_stop_room_for_wqe(num_wqebbs);
175 if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, room)))
176 return ERR_PTR(-ENOSPC);
178 pi = mlx5e_icosq_get_next_pi(sq, num_wqebbs);
179 wqe = MLX5E_TLS_FETCH_SET_PROGRESS_PARAMS_WQE(sq, pi);
180 mlx5e_ktls_build_progress_params(wqe, sq->pc, sq->sqn, priv_rx->tirn, false,
182 TLS_OFFLOAD_CTX_DIR_RX);
183 wi = (struct mlx5e_icosq_wqe_info) {
184 .wqe_type = MLX5E_ICOSQ_WQE_SET_PSV_TLS,
185 .num_wqebbs = num_wqebbs,
186 .tls_set_params.priv_rx = priv_rx,
189 icosq_fill_wi(sq, pi, &wi);
190 sq->pc += num_wqebbs;
195 static int post_rx_param_wqes(struct mlx5e_channel *c,
196 struct mlx5e_ktls_offload_context_rx *priv_rx,
197 u32 next_record_tcp_sn)
199 struct mlx5_wqe_ctrl_seg *cseg;
200 struct mlx5e_icosq *sq;
204 sq = &c->async_icosq;
205 spin_lock_bh(&c->async_icosq_lock);
207 cseg = post_static_params(sq, priv_rx);
210 cseg = post_progress_params(sq, priv_rx, next_record_tcp_sn);
214 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
216 spin_unlock_bh(&c->async_icosq_lock);
221 priv_rx->stats->tls_resync_req_skip++;
223 complete(&priv_rx->add_ctx);
228 mlx5e_set_ktls_rx_priv_ctx(struct tls_context *tls_ctx,
229 struct mlx5e_ktls_offload_context_rx *priv_rx)
231 struct mlx5e_ktls_offload_context_rx **ctx =
232 __tls_driver_ctx(tls_ctx, TLS_OFFLOAD_CTX_DIR_RX);
234 BUILD_BUG_ON(sizeof(struct mlx5e_ktls_offload_context_rx *) >
235 TLS_OFFLOAD_CONTEXT_SIZE_RX);
240 static struct mlx5e_ktls_offload_context_rx *
241 mlx5e_get_ktls_rx_priv_ctx(struct tls_context *tls_ctx)
243 struct mlx5e_ktls_offload_context_rx **ctx =
244 __tls_driver_ctx(tls_ctx, TLS_OFFLOAD_CTX_DIR_RX);
250 /* Runs in work context */
252 resync_post_get_progress_params(struct mlx5e_icosq *sq,
253 struct mlx5e_ktls_offload_context_rx *priv_rx)
255 struct mlx5e_get_tls_progress_params_wqe *wqe;
256 struct mlx5e_ktls_rx_resync_buf *buf;
257 struct mlx5e_icosq_wqe_info wi;
258 struct mlx5_wqe_ctrl_seg *cseg;
259 struct mlx5_seg_get_psv *psv;
264 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
265 if (unlikely(!buf)) {
270 pdev = mlx5_core_dma_dev(sq->channel->priv->mdev);
271 buf->dma_addr = dma_map_single(pdev, &buf->progress,
272 PROGRESS_PARAMS_PADDED_SIZE, DMA_FROM_DEVICE);
273 if (unlikely(dma_mapping_error(pdev, buf->dma_addr))) {
278 buf->priv_rx = priv_rx;
280 BUILD_BUG_ON(MLX5E_KTLS_GET_PROGRESS_WQEBBS != 1);
282 spin_lock_bh(&sq->channel->async_icosq_lock);
284 if (unlikely(!mlx5e_wqc_has_room_for(&sq->wq, sq->cc, sq->pc, 1))) {
285 spin_unlock_bh(&sq->channel->async_icosq_lock);
290 pi = mlx5e_icosq_get_next_pi(sq, 1);
291 wqe = MLX5E_TLS_FETCH_GET_PROGRESS_PARAMS_WQE(sq, pi);
293 #define GET_PSV_DS_CNT (DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS))
296 cseg->opmod_idx_opcode =
297 cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_GET_PSV |
298 (MLX5_OPC_MOD_TLS_TIR_PROGRESS_PARAMS << 24));
300 cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) | GET_PSV_DS_CNT);
303 psv->num_psv = 1 << 4;
304 psv->l_key = sq->channel->mkey_be;
305 psv->psv_index[0] = cpu_to_be32(priv_rx->tirn);
306 psv->va = cpu_to_be64(buf->dma_addr);
308 wi = (struct mlx5e_icosq_wqe_info) {
309 .wqe_type = MLX5E_ICOSQ_WQE_GET_PSV_TLS,
311 .tls_get_params.buf = buf,
313 icosq_fill_wi(sq, pi, &wi);
315 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
316 spin_unlock_bh(&sq->channel->async_icosq_lock);
321 dma_unmap_single(pdev, buf->dma_addr, PROGRESS_PARAMS_PADDED_SIZE, DMA_FROM_DEVICE);
325 priv_rx->stats->tls_resync_req_skip++;
329 /* Function is called with elevated refcount.
330 * It decreases it only if no WQE is posted.
332 static void resync_handle_work(struct work_struct *work)
334 struct mlx5e_ktls_offload_context_rx *priv_rx;
335 struct mlx5e_ktls_rx_resync_ctx *resync;
336 struct mlx5e_channel *c;
337 struct mlx5e_icosq *sq;
339 resync = container_of(work, struct mlx5e_ktls_rx_resync_ctx, work);
340 priv_rx = container_of(resync, struct mlx5e_ktls_offload_context_rx, resync);
342 if (unlikely(test_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags))) {
343 mlx5e_ktls_priv_rx_put(priv_rx);
347 c = resync->priv->channels.c[priv_rx->rxq];
348 sq = &c->async_icosq;
350 if (resync_post_get_progress_params(sq, priv_rx))
351 mlx5e_ktls_priv_rx_put(priv_rx);
354 static void resync_init(struct mlx5e_ktls_rx_resync_ctx *resync,
355 struct mlx5e_priv *priv)
357 INIT_WORK(&resync->work, resync_handle_work);
359 refcount_set(&resync->refcnt, 1);
362 /* Function can be called with the refcount being either elevated or not.
363 * It does not affect the refcount.
365 static int resync_handle_seq_match(struct mlx5e_ktls_offload_context_rx *priv_rx,
366 struct mlx5e_channel *c)
368 struct tls12_crypto_info_aes_gcm_128 *info = &priv_rx->crypto_info;
369 struct mlx5_wqe_ctrl_seg *cseg;
370 struct mlx5e_icosq *sq;
373 memcpy(info->rec_seq, &priv_rx->resync.sw_rcd_sn_be, sizeof(info->rec_seq));
376 sq = &c->async_icosq;
377 spin_lock_bh(&c->async_icosq_lock);
379 cseg = post_static_params(sq, priv_rx);
381 priv_rx->stats->tls_resync_res_skip++;
385 /* Do not increment priv_rx refcnt, CQE handling is empty */
386 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, cseg);
387 priv_rx->stats->tls_resync_res_ok++;
389 spin_unlock_bh(&c->async_icosq_lock);
394 /* Function can be called with the refcount being either elevated or not.
395 * It decreases the refcount and may free the kTLS priv context.
396 * Refcount is not elevated only if tls_dev_del has been called, but GET_PSV was
399 void mlx5e_ktls_handle_get_psv_completion(struct mlx5e_icosq_wqe_info *wi,
400 struct mlx5e_icosq *sq)
402 struct mlx5e_ktls_rx_resync_buf *buf = wi->tls_get_params.buf;
403 struct mlx5e_ktls_offload_context_rx *priv_rx;
404 struct mlx5e_ktls_rx_resync_ctx *resync;
405 u8 tracker_state, auth_state, *ctx;
409 priv_rx = buf->priv_rx;
410 resync = &priv_rx->resync;
411 dev = mlx5_core_dma_dev(resync->priv->mdev);
412 if (unlikely(test_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags)))
415 dma_sync_single_for_cpu(dev, buf->dma_addr, PROGRESS_PARAMS_PADDED_SIZE,
418 ctx = buf->progress.ctx;
419 tracker_state = MLX5_GET(tls_progress_params, ctx, record_tracker_state);
420 auth_state = MLX5_GET(tls_progress_params, ctx, auth_state);
421 if (tracker_state != MLX5E_TLS_PROGRESS_PARAMS_RECORD_TRACKER_STATE_TRACKING ||
422 auth_state != MLX5E_TLS_PROGRESS_PARAMS_AUTH_STATE_NO_OFFLOAD) {
423 priv_rx->stats->tls_resync_req_skip++;
427 hw_seq = MLX5_GET(tls_progress_params, ctx, hw_resync_tcp_sn);
428 tls_offload_rx_resync_async_request_end(priv_rx->sk, cpu_to_be32(hw_seq));
429 priv_rx->stats->tls_resync_req_end++;
431 mlx5e_ktls_priv_rx_put(priv_rx);
432 dma_unmap_single(dev, buf->dma_addr, PROGRESS_PARAMS_PADDED_SIZE, DMA_FROM_DEVICE);
437 * Function elevates the refcount, unless no work is queued.
439 static bool resync_queue_get_psv(struct sock *sk)
441 struct mlx5e_ktls_offload_context_rx *priv_rx;
442 struct mlx5e_ktls_rx_resync_ctx *resync;
444 priv_rx = mlx5e_get_ktls_rx_priv_ctx(tls_get_ctx(sk));
445 if (unlikely(!priv_rx))
448 if (unlikely(test_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags)))
451 resync = &priv_rx->resync;
452 mlx5e_ktls_priv_rx_get(priv_rx);
453 if (unlikely(!queue_work(resync->priv->tls->rx_wq, &resync->work)))
454 mlx5e_ktls_priv_rx_put(priv_rx);
460 static void resync_update_sn(struct mlx5e_rq *rq, struct sk_buff *skb)
462 struct ethhdr *eth = (struct ethhdr *)(skb->data);
463 struct net_device *netdev = rq->netdev;
464 struct sock *sk = NULL;
465 unsigned int datalen;
471 __vlan_get_protocol(skb, eth->h_proto, &depth);
472 iph = (struct iphdr *)(skb->data + depth);
474 if (iph->version == 4) {
475 depth += sizeof(struct iphdr);
476 th = (void *)iph + sizeof(struct iphdr);
478 sk = inet_lookup_established(dev_net(netdev), &tcp_hashinfo,
479 iph->saddr, th->source, iph->daddr,
480 th->dest, netdev->ifindex);
481 #if IS_ENABLED(CONFIG_IPV6)
483 struct ipv6hdr *ipv6h = (struct ipv6hdr *)iph;
485 depth += sizeof(struct ipv6hdr);
486 th = (void *)ipv6h + sizeof(struct ipv6hdr);
488 sk = __inet6_lookup_established(dev_net(netdev), &tcp_hashinfo,
489 &ipv6h->saddr, th->source,
490 &ipv6h->daddr, ntohs(th->dest),
495 depth += sizeof(struct tcphdr);
500 if (unlikely(sk->sk_state == TCP_TIME_WAIT))
503 if (unlikely(!resync_queue_get_psv(sk)))
507 datalen = skb->len - depth;
508 tls_offload_rx_resync_async_request_start(sk, seq, datalen);
509 rq->stats->tls_resync_req_start++;
515 void mlx5e_ktls_rx_resync(struct net_device *netdev, struct sock *sk,
518 struct mlx5e_ktls_offload_context_rx *priv_rx;
519 struct mlx5e_ktls_rx_resync_ctx *resync;
520 struct mlx5e_priv *priv;
521 struct mlx5e_channel *c;
523 priv_rx = mlx5e_get_ktls_rx_priv_ctx(tls_get_ctx(sk));
524 if (unlikely(!priv_rx))
527 resync = &priv_rx->resync;
528 resync->sw_rcd_sn_be = *(__be64 *)rcd_sn;
531 priv = netdev_priv(netdev);
532 c = priv->channels.c[priv_rx->rxq];
534 resync_handle_seq_match(priv_rx, c);
537 /* End of resync section */
539 void mlx5e_ktls_handle_rx_skb(struct mlx5e_rq *rq, struct sk_buff *skb,
540 struct mlx5_cqe64 *cqe, u32 *cqe_bcnt)
542 struct mlx5e_rq_stats *stats = rq->stats;
544 switch (get_cqe_tls_offload(cqe)) {
545 case CQE_TLS_OFFLOAD_DECRYPTED:
547 stats->tls_decrypted_packets++;
548 stats->tls_decrypted_bytes += *cqe_bcnt;
550 case CQE_TLS_OFFLOAD_RESYNC:
551 stats->tls_resync_req_pkt++;
552 resync_update_sn(rq, skb);
554 default: /* CQE_TLS_OFFLOAD_ERROR: */
560 void mlx5e_ktls_handle_ctx_completion(struct mlx5e_icosq_wqe_info *wi)
562 struct mlx5e_ktls_offload_context_rx *priv_rx = wi->tls_set_params.priv_rx;
563 struct accel_rule *rule = &priv_rx->rule;
565 if (unlikely(test_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags))) {
566 complete(&priv_rx->add_ctx);
569 queue_work(rule->priv->tls->rx_wq, &rule->work);
572 static int mlx5e_ktls_sk_get_rxq(struct sock *sk)
574 int rxq = sk_rx_queue_get(sk);
576 if (unlikely(rxq == -1))
582 int mlx5e_ktls_add_rx(struct net_device *netdev, struct sock *sk,
583 struct tls_crypto_info *crypto_info,
584 u32 start_offload_tcp_sn)
586 struct mlx5e_ktls_offload_context_rx *priv_rx;
587 struct mlx5e_ktls_rx_resync_ctx *resync;
588 struct tls_context *tls_ctx;
589 struct mlx5_core_dev *mdev;
590 struct mlx5e_priv *priv;
594 tls_ctx = tls_get_ctx(sk);
595 priv = netdev_priv(netdev);
597 priv_rx = kzalloc(sizeof(*priv_rx), GFP_KERNEL);
598 if (unlikely(!priv_rx))
601 err = mlx5_ktls_create_key(mdev, crypto_info, &priv_rx->key_id);
605 priv_rx->crypto_info =
606 *(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
608 rxq = mlx5e_ktls_sk_get_rxq(sk);
612 priv_rx->stats = &priv->channel_stats[rxq].rq;
613 mlx5e_set_ktls_rx_priv_ctx(tls_ctx, priv_rx);
615 rqtn = priv->direct_tir[rxq].rqt.rqtn;
617 err = mlx5e_ktls_create_tir(mdev, &priv_rx->tirn, rqtn);
621 init_completion(&priv_rx->add_ctx);
623 accel_rule_init(&priv_rx->rule, priv, sk);
624 resync = &priv_rx->resync;
625 resync_init(resync, priv);
626 tls_offload_ctx_rx(tls_ctx)->resync_async = &resync->core;
627 tls_offload_rx_resync_set_type(sk, TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC);
629 err = post_rx_param_wqes(priv->channels.c[rxq], priv_rx, start_offload_tcp_sn);
633 priv_rx->stats->tls_ctx++;
638 mlx5_core_destroy_tir(mdev, priv_rx->tirn);
640 mlx5_ktls_destroy_key(mdev, priv_rx->key_id);
646 void mlx5e_ktls_del_rx(struct net_device *netdev, struct tls_context *tls_ctx)
648 struct mlx5e_ktls_offload_context_rx *priv_rx;
649 struct mlx5e_ktls_rx_resync_ctx *resync;
650 struct mlx5_core_dev *mdev;
651 struct mlx5e_priv *priv;
653 priv = netdev_priv(netdev);
656 priv_rx = mlx5e_get_ktls_rx_priv_ctx(tls_ctx);
657 set_bit(MLX5E_PRIV_RX_FLAG_DELETING, priv_rx->flags);
658 mlx5e_set_ktls_rx_priv_ctx(tls_ctx, NULL);
659 synchronize_net(); /* Sync with NAPI */
660 if (!cancel_work_sync(&priv_rx->rule.work))
661 /* completion is needed, as the priv_rx in the add flow
662 * is maintained on the wqe info (wi), not on the socket.
664 wait_for_completion(&priv_rx->add_ctx);
665 resync = &priv_rx->resync;
666 if (cancel_work_sync(&resync->work))
667 mlx5e_ktls_priv_rx_put(priv_rx);
669 priv_rx->stats->tls_del++;
670 if (priv_rx->rule.rule)
671 mlx5e_accel_fs_del_sk(priv_rx->rule.rule);
673 mlx5_core_destroy_tir(mdev, priv_rx->tirn);
674 mlx5_ktls_destroy_key(mdev, priv_rx->key_id);
675 /* priv_rx should normally be freed here, but if there is an outstanding
676 * GET_PSV, deallocation will be delayed until the CQE for GET_PSV is
679 mlx5e_ktls_priv_rx_put(priv_rx);