bnxt_en: Allow the user to set ethtool stats-block-usecs to 0.
[linux-2.6/btrfs-unstable.git] / net / ipv4 / tcp_ulp.c
blob2417f55374c593c89b2aeb1ec4f6e6e74bb1395f
1 /*
2 * Pluggable TCP upper layer protocol support.
4 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
7 */
9 #include<linux/module.h>
10 #include <linux/mm.h>
11 #include <linux/types.h>
12 #include <linux/list.h>
13 #include <linux/gfp.h>
14 #include <net/tcp.h>
16 static DEFINE_SPINLOCK(tcp_ulp_list_lock);
17 static LIST_HEAD(tcp_ulp_list);
19 /* Simple linear search, don't expect many entries! */
20 static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
22 struct tcp_ulp_ops *e;
24 list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
25 if (strcmp(e->name, name) == 0)
26 return e;
29 return NULL;
32 static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
34 const struct tcp_ulp_ops *ulp = NULL;
36 rcu_read_lock();
37 ulp = tcp_ulp_find(name);
39 #ifdef CONFIG_MODULES
40 if (!ulp && capable(CAP_NET_ADMIN)) {
41 rcu_read_unlock();
42 request_module("%s", name);
43 rcu_read_lock();
44 ulp = tcp_ulp_find(name);
46 #endif
47 if (!ulp || !try_module_get(ulp->owner))
48 ulp = NULL;
50 rcu_read_unlock();
51 return ulp;
54 /* Attach new upper layer protocol to the list
55 * of available protocols.
57 int tcp_register_ulp(struct tcp_ulp_ops *ulp)
59 int ret = 0;
61 spin_lock(&tcp_ulp_list_lock);
62 if (tcp_ulp_find(ulp->name)) {
63 pr_notice("%s already registered or non-unique name\n",
64 ulp->name);
65 ret = -EEXIST;
66 } else {
67 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
69 spin_unlock(&tcp_ulp_list_lock);
71 return ret;
73 EXPORT_SYMBOL_GPL(tcp_register_ulp);
75 void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
77 spin_lock(&tcp_ulp_list_lock);
78 list_del_rcu(&ulp->list);
79 spin_unlock(&tcp_ulp_list_lock);
81 synchronize_rcu();
83 EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
85 /* Build string with list of available upper layer protocl values */
86 void tcp_get_available_ulp(char *buf, size_t maxlen)
88 struct tcp_ulp_ops *ulp_ops;
89 size_t offs = 0;
91 *buf = '\0';
92 rcu_read_lock();
93 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
94 offs += snprintf(buf + offs, maxlen - offs,
95 "%s%s",
96 offs == 0 ? "" : " ", ulp_ops->name);
98 rcu_read_unlock();
101 void tcp_cleanup_ulp(struct sock *sk)
103 struct inet_connection_sock *icsk = inet_csk(sk);
105 if (!icsk->icsk_ulp_ops)
106 return;
108 if (icsk->icsk_ulp_ops->release)
109 icsk->icsk_ulp_ops->release(sk);
110 module_put(icsk->icsk_ulp_ops->owner);
113 /* Change upper layer protocol for socket */
114 int tcp_set_ulp(struct sock *sk, const char *name)
116 struct inet_connection_sock *icsk = inet_csk(sk);
117 const struct tcp_ulp_ops *ulp_ops;
118 int err = 0;
120 if (icsk->icsk_ulp_ops)
121 return -EEXIST;
123 ulp_ops = __tcp_ulp_find_autoload(name);
124 if (!ulp_ops)
125 err = -ENOENT;
126 else
127 err = ulp_ops->init(sk);
129 if (err)
130 goto out;
132 icsk->icsk_ulp_ops = ulp_ops;
133 out:
134 return err;