xref: /freebsd/sys/dev/mlx5/mlx5_accel/mlx5_ipsec_rxtx.c (revision 56b17de1e8360fe131d425de20b5e75ff3ea897c)
1 /*-
2  * Copyright (c) 2023 NVIDIA corporation & affiliates.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  */
26 
27 #include <sys/mbuf.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netipsec/keydb.h>
31 #include <netipsec/ipsec_offload.h>
32 #include <dev/mlx5/qp.h>
33 #include <dev/mlx5/mlx5_en/en.h>
34 #include <dev/mlx5/mlx5_accel/ipsec.h>
35 
36 #define MLX5_IPSEC_METADATA_HANDLE(ipsec_metadata) (ipsec_metadata & 0xFFFFFF)
37 
38 int
39 mlx5_accel_ipsec_rx_tag_add(if_t ifp, struct mlx5e_rq_mbuf *mr)
40 {
41 	struct mlx5e_priv *priv;
42 	struct ipsec_accel_in_tag *mtag;
43 
44 	priv = if_getsoftc(ifp);
45 	if (priv->ipsec == NULL)
46 		return (0);
47 	if (mr->ipsec_mtag != NULL)
48 		return (0);
49 
50 	mtag = (struct ipsec_accel_in_tag *)m_tag_get(
51 	    PACKET_TAG_IPSEC_ACCEL_IN, sizeof(*mtag), M_NOWAIT);
52 	if (mtag == NULL)
53 		return (-ENOMEM);
54 	mr->ipsec_mtag = mtag;
55 	return (0);
56 }
57 
58 void
59 mlx5e_accel_ipsec_handle_rx_cqe(struct mbuf *mb, struct mlx5_cqe64 *cqe,
60     struct mlx5e_rq_mbuf *mr)
61 {
62 	struct ipsec_accel_in_tag *mtag;
63 	u32 drv_spi;
64 
65 	drv_spi = MLX5_IPSEC_METADATA_HANDLE(be32_to_cpu(cqe->ft_metadata));
66 	mtag = mr->ipsec_mtag;
67 	WARN_ON(mtag == NULL);
68 	mr->ipsec_mtag = NULL;
69 	if (mtag != NULL) {
70 		mtag->drv_spi = drv_spi;
71 		m_tag_prepend(mb, &mtag->tag);
72 	}
73 }
74 
75 void
76 mlx5e_accel_ipsec_handle_tx_wqe(struct mbuf *mb, struct mlx5e_tx_wqe *wqe,
77     struct ipsec_accel_out_tag *tag)
78 {
79 	wqe->eth.flow_table_metadata = cpu_to_be32(
80 	    mlx5e_accel_ipsec_get_metadata(tag->drv_spi));
81 }
82