xref: /illumos-gate/usr/src/uts/common/inet/cc/cc.c (revision 6d3b960aa395b3d268922a1938aed248a213725b)
1 /*
2  * Copyright (c) 2007-2008
3  *	Swinburne University of Technology, Melbourne, Australia.
4  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5  * Copyright (c) 2010 The FreeBSD Foundation
6  * All rights reserved.
7  * Copyright (c) 2017 by Delphix. All rights reserved.
8  *
9  * This software was developed at the Centre for Advanced Internet
10  * Architectures, Swinburne University of Technology, by Lawrence Stewart and
11  * James Healy, made possible in part by a grant from the Cisco University
12  * Research Program Fund at Community Foundation Silicon Valley.
13  *
14  * Portions of this software were developed at the Centre for Advanced
15  * Internet Architectures, Swinburne University of Technology, Melbourne,
16  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 /*
41  * This software was first released in 2007 by James Healy and Lawrence Stewart
42  * whilst working on the NewTCP research project at Swinburne University of
43  * Technology's Centre for Advanced Internet Architectures, Melbourne,
44  * Australia, which was made possible in part by a grant from the Cisco
45  * University Research Program Fund at Community Foundation Silicon Valley.
46  * More details are available at:
47  *   http://caia.swin.edu.au/urp/newtcp/
48  */
49 
50 #include <sys/param.h>
51 #include <sys/errno.h>
52 #include <sys/systm.h>
53 #include <sys/queue.h>
54 #include <inet/cc.h>
55 #include <inet/tcp.h>
56 #include <sys/sdt.h>
57 
58 #define	CC_KMODDIR	"cc"
59 
60 /*
61  * List of available cc algorithms on the current system. Access is
62  * synchronized using cc_list_lock.
63  */
64 static STAILQ_HEAD(cc_head, cc_algo) cc_list = STAILQ_HEAD_INITIALIZER(cc_list);
65 static kmutex_t cc_list_lock;
66 
67 static struct modlmisc cc_modlmisc = {
68 	&mod_miscops,
69 	"Pluggable Congestion Control Framework"
70 };
71 
72 static struct modlinkage cc_modlinkage = {
73 	MODREV_1,
74 	&cc_modlmisc,
75 	NULL
76 };
77 
78 /*
79  * Initialise CC subsystem on system boot.
80  */
81 int
82 _init(void)
83 {
84 	STAILQ_INIT(&cc_list);
85 
86 	return (mod_install(&cc_modlinkage));
87 }
88 
89 int
90 _fini(void)
91 {
92 	return (EBUSY);
93 }
94 
95 int
96 _info(struct modinfo *modinfop)
97 {
98 	return (mod_info(&cc_modlinkage, modinfop));
99 }
100 
101 int
102 cc_walk_algos(cc_walk_func_t *func, void *cd)
103 {
104 	struct cc_algo *algo;
105 	int ret = 0;
106 
107 	mutex_enter(&cc_list_lock);
108 	STAILQ_FOREACH(algo, &cc_list, entries) {
109 		if ((ret = func(cd, algo)) != 0) {
110 			break;
111 		}
112 	}
113 	mutex_exit(&cc_list_lock);
114 
115 	return (ret);
116 }
117 
118 /*
119  * Search for an algorithm of a given name, and return the corresponding set of
120  * operations. If there is no algorithm with the given name present, then this
121  * function returns NULL.
122  *
123  * Since this function is passed names from userland, it needs to be paranoid
124  * about the string, in case it's missing a terminating NUL character.
125  */
126 struct cc_algo *
127 cc_load_algo(const char *name)
128 {
129 	struct cc_algo *algo;
130 	boolean_t found = B_FALSE;
131 
132 	if (strnlen(name, CC_ALGO_NAME_MAX) >= CC_ALGO_NAME_MAX) {
133 		return (NULL);
134 	}
135 
136 	mutex_enter(&cc_list_lock);
137 	STAILQ_FOREACH(algo, &cc_list, entries) {
138 		if (strncmp(algo->name, name, CC_ALGO_NAME_MAX) == 0) {
139 			found = B_TRUE;
140 			break;
141 		}
142 	}
143 	mutex_exit(&cc_list_lock);
144 
145 	return (found ? algo : NULL);
146 }
147 
148 /*
149  * Returns non-zero on success, 0 on failure.
150  */
151 int
152 cc_deregister_algo(struct cc_algo *remove_cc)
153 {
154 	struct cc_algo *funcs, *tmpfuncs;
155 	int err = ENOENT;
156 
157 	mutex_enter(&cc_list_lock);
158 	STAILQ_FOREACH_SAFE(funcs, &cc_list, entries, tmpfuncs) {
159 		if (funcs == remove_cc) {
160 			STAILQ_REMOVE(&cc_list, funcs, cc_algo, entries);
161 			err = 0;
162 			break;
163 		}
164 	}
165 	mutex_exit(&cc_list_lock);
166 	return (err);
167 }
168 
169 /*
170  * Returns 0 on success, non-zero on failure.
171  */
172 int
173 cc_register_algo(struct cc_algo *add_cc)
174 {
175 	struct cc_algo *funcs;
176 	size_t nlen;
177 	int err = 0;
178 
179 	nlen = strnlen(add_cc->name, CC_ALGO_NAME_MAX);
180 	if (nlen == 0 || nlen >= CC_ALGO_NAME_MAX) {
181 		return (EINVAL);
182 	}
183 
184 	/*
185 	 * Iterate over list of registered CC algorithms and make sure
186 	 * we're not trying to add a duplicate.
187 	 */
188 	mutex_enter(&cc_list_lock);
189 	STAILQ_FOREACH(funcs, &cc_list, entries) {
190 		if (strncmp(funcs->name, add_cc->name, CC_ALGO_NAME_MAX) == 0)
191 			err = EEXIST;
192 	}
193 
194 	if (err == 0)
195 		STAILQ_INSERT_TAIL(&cc_list, add_cc, entries);
196 
197 	mutex_exit(&cc_list_lock);
198 
199 	return (err);
200 }
201