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