xref: /freebsd/lib/librss/librss.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*
2  * Copyright (c) 2016 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/cpuset.h>
34 #include <sys/sysctl.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <strings.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <string.h>
43 #include <errno.h>
44 
45 #include <netinet/in.h>
46 
47 #include "librss.h"
48 
49 int
50 rss_sock_set_bindmulti(int fd, int af, int val)
51 {
52 	int opt;
53 	socklen_t optlen;
54 	int retval;
55 
56 	/* Set bindmulti */
57 	opt = val;
58 	optlen = sizeof(opt);
59 	retval = setsockopt(fd,
60 	    af == AF_INET ? IPPROTO_IP : IPPROTO_IPV6,
61 	    af == AF_INET ? IP_BINDMULTI : IPV6_BINDMULTI,
62 	    &opt,
63 	    optlen);
64 	if (retval < 0) {
65 		warn("%s: setsockopt(IP_BINDMULTI)", __func__);
66 		return (-1);
67 	}
68 	return (0);
69 }
70 
71 int
72 rss_sock_set_rss_bucket(int fd, int af, int rss_bucket)
73 {
74 	int opt;
75 	socklen_t optlen;
76 	int retval;
77 	int f, p;
78 
79 	switch (af) {
80 	case AF_INET:
81 		p = IPPROTO_IP;
82 		f = IP_RSS_LISTEN_BUCKET;
83 		break;
84 	case AF_INET6:
85 		p = IPPROTO_IPV6;
86 		f = IPV6_RSS_LISTEN_BUCKET;
87 		break;
88 	default:
89 		return (-1);
90 	}
91 
92 	/* Set RSS bucket */
93 	opt = rss_bucket;
94 	optlen = sizeof(opt);
95 	retval = setsockopt(fd, p, f, &opt, optlen);
96 	if (retval < 0) {
97 		warn("%s: setsockopt(IP_RSS_LISTEN_BUCKET)", __func__);
98 		return (-1);
99 	}
100 	return (0);
101 }
102 
103 int
104 rss_sock_set_recvrss(int fd, int af, int val)
105 {
106 	int opt, retval;
107 	socklen_t optlen;
108 	int f1, f2, p;
109 
110 	switch (af) {
111 	case AF_INET:
112 		p = IPPROTO_IP;
113 		f1 = IP_RECVFLOWID;
114 		f2 = IP_RECVRSSBUCKETID;
115 		break;
116 	case AF_INET6:
117 		p = IPPROTO_IPV6;
118 		f1 = IPV6_RECVFLOWID;
119 		f2 = IPV6_RECVRSSBUCKETID;
120 		break;
121 	default:
122 		return (-1);
123 	}
124 
125 	/* Enable/disable flowid */
126 	opt = val;
127 	optlen = sizeof(opt);
128 	retval = setsockopt(fd, p, f1, &opt, optlen);
129 	if (retval < 0) {
130 		warn("%s: setsockopt(IP_RECVFLOWID)", __func__);
131 		return (-1);
132 	}
133 
134 	/* Enable/disable RSS bucket reception */
135 	opt = val;
136 	optlen = sizeof(opt);
137 	retval = setsockopt(fd, p, f2, &opt, optlen);
138 	if (retval < 0) {
139 		warn("%s: setsockopt(IP_RECVRSSBUCKETID)", __func__);
140 		return (-1);
141 	}
142 
143 	return (0);
144 }
145 
146 static int
147 rss_getsysctlint(const char *s)
148 {
149 	int val, retval;
150 	size_t rlen;
151 
152 	rlen = sizeof(int);
153 	retval = sysctlbyname(s, &val, &rlen, NULL, 0);
154 	if (retval < 0) {
155 		warn("sysctlbyname (%s)", s);
156 		return (-1);
157 	}
158 
159 	return (val);
160 }
161 
162 static int
163 rss_getbucketmap(int *bucket_map, int nbuckets)
164 {
165 	/* XXX I'm lazy; so static string it is */
166 	char bstr[2048];
167 	int retval;
168 	size_t rlen;
169 	char *s, *ss;
170 	int r, b, c;
171 
172 	/* Paranoia */
173 	memset(bstr, '\0', sizeof(bstr));
174 
175 	rlen = sizeof(bstr) - 1;
176 	retval = sysctlbyname("net.inet.rss.bucket_mapping", bstr, &rlen, NULL, 0);
177 	if (retval < 0) {
178 		warn("sysctlbyname (net.inet.rss.bucket_mapping)");
179 		return (-1);
180 	}
181 
182 	ss = bstr;
183 	while ((s = strsep(&ss, " ")) != NULL) {
184 		r = sscanf(s, "%d:%d", &b, &c);
185 		if (r != 2) {
186 			fprintf(stderr, "%s: string (%s) not parsable\n",
187 			    __func__,
188 			    s);
189 			return (-1);
190 		}
191 		if (b > nbuckets) {
192 			fprintf(stderr, "%s: bucket %d > nbuckets %d\n",
193 			    __func__,
194 			    b,
195 			    nbuckets);
196 			return (-1);
197 		}
198 		/* XXX no maxcpu check */
199 		bucket_map[b] = c;
200 	}
201 	return (0);
202 }
203 
204 struct rss_config *
205 rss_config_get(void)
206 {
207 	struct rss_config *rc = NULL;
208 
209 	rc = calloc(1, sizeof(*rc));
210 	if (rc == NULL) {
211 		warn("%s: calloc", __func__);
212 		goto error;
213 	}
214 
215 	rc->rss_ncpus = rss_getsysctlint("net.inet.rss.ncpus");
216 	if (rc->rss_ncpus < 0) {
217 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.ncpus\n", __func__);
218 		goto error;
219 	}
220 
221 	rc->rss_nbuckets = rss_getsysctlint("net.inet.rss.buckets");
222 	if (rc->rss_nbuckets < 0) {
223 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.nbuckets\n", __func__);
224 		goto error;
225 	}
226 
227 	rc->rss_basecpu = rss_getsysctlint("net.inet.rss.basecpu");
228 	if (rc->rss_basecpu< 0) {
229 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.basecpu\n", __func__);
230 		goto error;
231 	}
232 
233 	rc->rss_bucket_map = calloc(rc->rss_nbuckets, sizeof(int));
234 	if (rc->rss_bucket_map == NULL) {
235 		warn("%s: calloc (rss buckets; %d entries)", __func__, rc->rss_nbuckets);
236 		goto error;
237 	}
238 
239 	if (rss_getbucketmap(rc->rss_bucket_map, rc->rss_nbuckets) != 0) {
240 		fprintf(stderr, "%s: rss_getbucketmap failed\n", __func__);
241 		goto error;
242 	}
243 
244 	return (rc);
245 
246 error:
247 	if (rc != NULL) {
248 		free(rc->rss_bucket_map);
249 		free(rc);
250 	}
251 	return (NULL);
252 }
253 
254 void
255 rss_config_free(struct rss_config *rc)
256 {
257 
258 	if ((rc != NULL) && rc->rss_bucket_map)
259 		free(rc->rss_bucket_map);
260 	if (rc != NULL)
261 		free(rc);
262 }
263 
264 int
265 rss_config_get_bucket_count(struct rss_config *rc)
266 {
267 
268 	if (rc == NULL)
269 		return (-1);
270 	return (rc->rss_nbuckets);
271 }
272 
273 int
274 rss_get_bucket_cpuset(struct rss_config *rc, rss_bucket_type_t btype,
275     int bucket, cpuset_t *cs)
276 {
277 
278 	if (bucket < 0 || bucket >= rc->rss_nbuckets) {
279 		errno = EINVAL;
280 		return (-1);
281 	}
282 
283 	/*
284 	 * For now all buckets are the same, but eventually we'll want
285 	 * to allow administrators to set separate RSS cpusets for
286 	 * {kernel,user} {tx, rx} combinations.
287 	 */
288 	if (btype <= RSS_BUCKET_TYPE_NONE || btype > RSS_BUCKET_TYPE_MAX) {
289 		errno = ENOTSUP;
290 		return (-1);
291 	}
292 
293 	CPU_ZERO(cs);
294 	CPU_SET(rc->rss_bucket_map[bucket], cs);
295 
296 	return (0);
297 }
298 
299 int
300 rss_set_bucket_rebalance_cb(rss_bucket_rebalance_cb_t *cb, void *cbdata)
301 {
302 
303 	(void) cb;
304 	(void) cbdata;
305 
306 	/*
307 	 * For now there's no rebalance callback, so
308 	 * just return 0 and ignore it.
309 	 */
310 	return (0);
311 }
312