xref: /freebsd/lib/librss/librss.c (revision 1b10e191f341111fad7be32ead11484dfd09b800)
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_rss_bucket(int fd, int af, int rss_bucket)
51 {
52 	int opt;
53 	socklen_t optlen;
54 	int retval;
55 	int f, p;
56 
57 	switch (af) {
58 	case AF_INET:
59 		p = IPPROTO_IP;
60 		f = IP_RSS_LISTEN_BUCKET;
61 		break;
62 	case AF_INET6:
63 		p = IPPROTO_IPV6;
64 		f = IPV6_RSS_LISTEN_BUCKET;
65 		break;
66 	default:
67 		return (-1);
68 	}
69 
70 	/* Set RSS bucket */
71 	opt = rss_bucket;
72 	optlen = sizeof(opt);
73 	retval = setsockopt(fd, p, f, &opt, optlen);
74 	if (retval < 0) {
75 		warn("%s: setsockopt(IP_RSS_LISTEN_BUCKET)", __func__);
76 		return (-1);
77 	}
78 	return (0);
79 }
80 
81 int
82 rss_sock_set_recvrss(int fd, int af, int val)
83 {
84 	int opt, retval;
85 	socklen_t optlen;
86 	int f1, f2, p;
87 
88 	switch (af) {
89 	case AF_INET:
90 		p = IPPROTO_IP;
91 		f1 = IP_RECVFLOWID;
92 		f2 = IP_RECVRSSBUCKETID;
93 		break;
94 	case AF_INET6:
95 		p = IPPROTO_IPV6;
96 		f1 = IPV6_RECVFLOWID;
97 		f2 = IPV6_RECVRSSBUCKETID;
98 		break;
99 	default:
100 		return (-1);
101 	}
102 
103 	/* Enable/disable flowid */
104 	opt = val;
105 	optlen = sizeof(opt);
106 	retval = setsockopt(fd, p, f1, &opt, optlen);
107 	if (retval < 0) {
108 		warn("%s: setsockopt(IP_RECVFLOWID)", __func__);
109 		return (-1);
110 	}
111 
112 	/* Enable/disable RSS bucket reception */
113 	opt = val;
114 	optlen = sizeof(opt);
115 	retval = setsockopt(fd, p, f2, &opt, optlen);
116 	if (retval < 0) {
117 		warn("%s: setsockopt(IP_RECVRSSBUCKETID)", __func__);
118 		return (-1);
119 	}
120 
121 	return (0);
122 }
123 
124 static int
125 rss_getsysctlint(const char *s)
126 {
127 	int val, retval;
128 	size_t rlen;
129 
130 	rlen = sizeof(int);
131 	retval = sysctlbyname(s, &val, &rlen, NULL, 0);
132 	if (retval < 0) {
133 		warn("sysctlbyname (%s)", s);
134 		return (-1);
135 	}
136 
137 	return (val);
138 }
139 
140 static int
141 rss_getbucketmap(int *bucket_map, int nbuckets)
142 {
143 	/* XXX I'm lazy; so static string it is */
144 	char bstr[2048];
145 	int retval;
146 	size_t rlen;
147 	char *s, *ss;
148 	int r, b, c;
149 
150 	/* Paranoia */
151 	memset(bstr, '\0', sizeof(bstr));
152 
153 	rlen = sizeof(bstr) - 1;
154 	retval = sysctlbyname("net.inet.rss.bucket_mapping", bstr, &rlen, NULL, 0);
155 	if (retval < 0) {
156 		warn("sysctlbyname (net.inet.rss.bucket_mapping)");
157 		return (-1);
158 	}
159 
160 	ss = bstr;
161 	while ((s = strsep(&ss, " ")) != NULL) {
162 		r = sscanf(s, "%d:%d", &b, &c);
163 		if (r != 2) {
164 			fprintf(stderr, "%s: string (%s) not parsable\n",
165 			    __func__,
166 			    s);
167 			return (-1);
168 		}
169 		if (b > nbuckets) {
170 			fprintf(stderr, "%s: bucket %d > nbuckets %d\n",
171 			    __func__,
172 			    b,
173 			    nbuckets);
174 			return (-1);
175 		}
176 		/* XXX no maxcpu check */
177 		bucket_map[b] = c;
178 	}
179 	return (0);
180 }
181 
182 struct rss_config *
183 rss_config_get(void)
184 {
185 	struct rss_config *rc = NULL;
186 
187 	rc = calloc(1, sizeof(*rc));
188 	if (rc == NULL) {
189 		warn("%s: calloc", __func__);
190 		goto error;
191 	}
192 
193 	rc->rss_ncpus = rss_getsysctlint("net.inet.rss.ncpus");
194 	if (rc->rss_ncpus < 0) {
195 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.ncpus\n", __func__);
196 		goto error;
197 	}
198 
199 	rc->rss_nbuckets = rss_getsysctlint("net.inet.rss.buckets");
200 	if (rc->rss_nbuckets < 0) {
201 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.nbuckets\n", __func__);
202 		goto error;
203 	}
204 
205 	rc->rss_basecpu = rss_getsysctlint("net.inet.rss.basecpu");
206 	if (rc->rss_basecpu< 0) {
207 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.basecpu\n", __func__);
208 		goto error;
209 	}
210 
211 	rc->rss_bucket_map = calloc(rc->rss_nbuckets, sizeof(int));
212 	if (rc->rss_bucket_map == NULL) {
213 		warn("%s: calloc (rss buckets; %d entries)", __func__, rc->rss_nbuckets);
214 		goto error;
215 	}
216 
217 	if (rss_getbucketmap(rc->rss_bucket_map, rc->rss_nbuckets) != 0) {
218 		fprintf(stderr, "%s: rss_getbucketmap failed\n", __func__);
219 		goto error;
220 	}
221 
222 	return (rc);
223 
224 error:
225 	if (rc != NULL) {
226 		free(rc->rss_bucket_map);
227 		free(rc);
228 	}
229 	return (NULL);
230 }
231 
232 void
233 rss_config_free(struct rss_config *rc)
234 {
235 
236 	if ((rc != NULL) && rc->rss_bucket_map)
237 		free(rc->rss_bucket_map);
238 	if (rc != NULL)
239 		free(rc);
240 }
241 
242 int
243 rss_config_get_bucket_count(struct rss_config *rc)
244 {
245 
246 	if (rc == NULL)
247 		return (-1);
248 	return (rc->rss_nbuckets);
249 }
250 
251 int
252 rss_get_bucket_cpuset(struct rss_config *rc, rss_bucket_type_t btype,
253     int bucket, cpuset_t *cs)
254 {
255 
256 	if (bucket < 0 || bucket >= rc->rss_nbuckets) {
257 		errno = EINVAL;
258 		return (-1);
259 	}
260 
261 	/*
262 	 * For now all buckets are the same, but eventually we'll want
263 	 * to allow administrators to set separate RSS cpusets for
264 	 * {kernel,user} {tx, rx} combinations.
265 	 */
266 	if (btype <= RSS_BUCKET_TYPE_NONE || btype > RSS_BUCKET_TYPE_MAX) {
267 		errno = ENOTSUP;
268 		return (-1);
269 	}
270 
271 	CPU_ZERO(cs);
272 	CPU_SET(rc->rss_bucket_map[bucket], cs);
273 
274 	return (0);
275 }
276 
277 int
278 rss_set_bucket_rebalance_cb(rss_bucket_rebalance_cb_t *cb, void *cbdata)
279 {
280 
281 	(void) cb;
282 	(void) cbdata;
283 
284 	/*
285 	 * For now there's no rebalance callback, so
286 	 * just return 0 and ignore it.
287 	 */
288 	return (0);
289 }
290