2 * Copyright (c) 2016 Adrian Chadd <adrian@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
45 #include <netinet/in.h>
50 rss_sock_set_recvrss(int fd, int af, int val)
60 f2 = IP_RECVRSSBUCKETID;
65 f2 = IPV6_RECVRSSBUCKETID;
71 /* Enable/disable flowid */
74 retval = setsockopt(fd, p, f1, &opt, optlen);
76 warn("%s: setsockopt(IP_RECVFLOWID)", __func__);
80 /* Enable/disable RSS bucket reception */
83 retval = setsockopt(fd, p, f2, &opt, optlen);
85 warn("%s: setsockopt(IP_RECVRSSBUCKETID)", __func__);
93 rss_getsysctlint(const char *s)
99 retval = sysctlbyname(s, &val, &rlen, NULL, 0);
101 warn("sysctlbyname (%s)", s);
109 rss_getbucketmap(int *bucket_map, int nbuckets)
111 /* XXX I'm lazy; so static string it is */
119 memset(bstr, '\0', sizeof(bstr));
121 rlen = sizeof(bstr) - 1;
122 retval = sysctlbyname("net.inet.rss.bucket_mapping", bstr, &rlen, NULL, 0);
124 warn("sysctlbyname (net.inet.rss.bucket_mapping)");
129 while ((s = strsep(&ss, " ")) != NULL) {
130 r = sscanf(s, "%d:%d", &b, &c);
132 fprintf(stderr, "%s: string (%s) not parsable\n",
138 fprintf(stderr, "%s: bucket %d > nbuckets %d\n",
144 /* XXX no maxcpu check */
153 struct rss_config *rc = NULL;
155 rc = calloc(1, sizeof(*rc));
157 warn("%s: calloc", __func__);
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__);
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__);
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__);
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);
185 if (rss_getbucketmap(rc->rss_bucket_map, rc->rss_nbuckets) != 0) {
186 fprintf(stderr, "%s: rss_getbucketmap failed\n", __func__);
194 free(rc->rss_bucket_map);
201 rss_config_free(struct rss_config *rc)
204 if ((rc != NULL) && rc->rss_bucket_map)
205 free(rc->rss_bucket_map);
211 rss_config_get_bucket_count(struct rss_config *rc)
216 return (rc->rss_nbuckets);
220 rss_get_bucket_cpuset(struct rss_config *rc, rss_bucket_type_t btype,
221 int bucket, cpuset_t *cs)
224 if (bucket < 0 || bucket >= rc->rss_nbuckets) {
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.
234 if (btype <= RSS_BUCKET_TYPE_NONE || btype > RSS_BUCKET_TYPE_MAX) {
240 CPU_SET(rc->rss_bucket_map[bucket], cs);
246 rss_set_bucket_rebalance_cb(rss_bucket_rebalance_cb_t *cb, void *cbdata)
253 * For now there's no rebalance callback, so
254 * just return 0 and ignore it.