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