xref: /OK3568_Linux_fs/kernel/net/tls/tls_toe.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3*4882a593Smuzhiyun  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
6*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
7*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
8*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
9*4882a593Smuzhiyun  * OpenIB.org BSD license below:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
12*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
13*4882a593Smuzhiyun  *     conditions are met:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
16*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
17*4882a593Smuzhiyun  *        disclaimer.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
22*4882a593Smuzhiyun  *        provided with the distribution.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31*4882a593Smuzhiyun  * SOFTWARE.
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <linux/list.h>
35*4882a593Smuzhiyun #include <linux/rcupdate.h>
36*4882a593Smuzhiyun #include <linux/spinlock.h>
37*4882a593Smuzhiyun #include <net/inet_connection_sock.h>
38*4882a593Smuzhiyun #include <net/tls.h>
39*4882a593Smuzhiyun #include <net/tls_toe.h>
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static LIST_HEAD(device_list);
42*4882a593Smuzhiyun static DEFINE_SPINLOCK(device_spinlock);
43*4882a593Smuzhiyun 
tls_toe_sk_destruct(struct sock * sk)44*4882a593Smuzhiyun static void tls_toe_sk_destruct(struct sock *sk)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
47*4882a593Smuzhiyun 	struct tls_context *ctx = tls_get_ctx(sk);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	ctx->sk_destruct(sk);
50*4882a593Smuzhiyun 	/* Free ctx */
51*4882a593Smuzhiyun 	rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
52*4882a593Smuzhiyun 	tls_ctx_free(sk, ctx);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
tls_toe_bypass(struct sock * sk)55*4882a593Smuzhiyun int tls_toe_bypass(struct sock *sk)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	struct tls_toe_device *dev;
58*4882a593Smuzhiyun 	struct tls_context *ctx;
59*4882a593Smuzhiyun 	int rc = 0;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	spin_lock_bh(&device_spinlock);
62*4882a593Smuzhiyun 	list_for_each_entry(dev, &device_list, dev_list) {
63*4882a593Smuzhiyun 		if (dev->feature && dev->feature(dev)) {
64*4882a593Smuzhiyun 			ctx = tls_ctx_create(sk);
65*4882a593Smuzhiyun 			if (!ctx)
66*4882a593Smuzhiyun 				goto out;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 			ctx->sk_destruct = sk->sk_destruct;
69*4882a593Smuzhiyun 			sk->sk_destruct = tls_toe_sk_destruct;
70*4882a593Smuzhiyun 			ctx->rx_conf = TLS_HW_RECORD;
71*4882a593Smuzhiyun 			ctx->tx_conf = TLS_HW_RECORD;
72*4882a593Smuzhiyun 			update_sk_prot(sk, ctx);
73*4882a593Smuzhiyun 			rc = 1;
74*4882a593Smuzhiyun 			break;
75*4882a593Smuzhiyun 		}
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun out:
78*4882a593Smuzhiyun 	spin_unlock_bh(&device_spinlock);
79*4882a593Smuzhiyun 	return rc;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
tls_toe_unhash(struct sock * sk)82*4882a593Smuzhiyun void tls_toe_unhash(struct sock *sk)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	struct tls_context *ctx = tls_get_ctx(sk);
85*4882a593Smuzhiyun 	struct tls_toe_device *dev;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	spin_lock_bh(&device_spinlock);
88*4882a593Smuzhiyun 	list_for_each_entry(dev, &device_list, dev_list) {
89*4882a593Smuzhiyun 		if (dev->unhash) {
90*4882a593Smuzhiyun 			kref_get(&dev->kref);
91*4882a593Smuzhiyun 			spin_unlock_bh(&device_spinlock);
92*4882a593Smuzhiyun 			dev->unhash(dev, sk);
93*4882a593Smuzhiyun 			kref_put(&dev->kref, dev->release);
94*4882a593Smuzhiyun 			spin_lock_bh(&device_spinlock);
95*4882a593Smuzhiyun 		}
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 	spin_unlock_bh(&device_spinlock);
98*4882a593Smuzhiyun 	ctx->sk_proto->unhash(sk);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
tls_toe_hash(struct sock * sk)101*4882a593Smuzhiyun int tls_toe_hash(struct sock *sk)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	struct tls_context *ctx = tls_get_ctx(sk);
104*4882a593Smuzhiyun 	struct tls_toe_device *dev;
105*4882a593Smuzhiyun 	int err;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	err = ctx->sk_proto->hash(sk);
108*4882a593Smuzhiyun 	spin_lock_bh(&device_spinlock);
109*4882a593Smuzhiyun 	list_for_each_entry(dev, &device_list, dev_list) {
110*4882a593Smuzhiyun 		if (dev->hash) {
111*4882a593Smuzhiyun 			kref_get(&dev->kref);
112*4882a593Smuzhiyun 			spin_unlock_bh(&device_spinlock);
113*4882a593Smuzhiyun 			err |= dev->hash(dev, sk);
114*4882a593Smuzhiyun 			kref_put(&dev->kref, dev->release);
115*4882a593Smuzhiyun 			spin_lock_bh(&device_spinlock);
116*4882a593Smuzhiyun 		}
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 	spin_unlock_bh(&device_spinlock);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (err)
121*4882a593Smuzhiyun 		tls_toe_unhash(sk);
122*4882a593Smuzhiyun 	return err;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
tls_toe_register_device(struct tls_toe_device * device)125*4882a593Smuzhiyun void tls_toe_register_device(struct tls_toe_device *device)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	spin_lock_bh(&device_spinlock);
128*4882a593Smuzhiyun 	list_add_tail(&device->dev_list, &device_list);
129*4882a593Smuzhiyun 	spin_unlock_bh(&device_spinlock);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun EXPORT_SYMBOL(tls_toe_register_device);
132*4882a593Smuzhiyun 
tls_toe_unregister_device(struct tls_toe_device * device)133*4882a593Smuzhiyun void tls_toe_unregister_device(struct tls_toe_device *device)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	spin_lock_bh(&device_spinlock);
136*4882a593Smuzhiyun 	list_del(&device->dev_list);
137*4882a593Smuzhiyun 	spin_unlock_bh(&device_spinlock);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun EXPORT_SYMBOL(tls_toe_unregister_device);
140