xref: /freebsd/lib/librss/librss.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
135c05a4fSAdrian Chadd /*
235c05a4fSAdrian Chadd  * Copyright (c) 2016 Adrian Chadd <adrian@FreeBSD.org>
335c05a4fSAdrian Chadd  * All rights reserved.
435c05a4fSAdrian Chadd  *
535c05a4fSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
635c05a4fSAdrian Chadd  * modification, are permitted provided that the following conditions
735c05a4fSAdrian Chadd  * are met:
835c05a4fSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
935c05a4fSAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
1035c05a4fSAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
1135c05a4fSAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
1235c05a4fSAdrian Chadd  *    documentation and/or other materials provided with the distribution.
1335c05a4fSAdrian Chadd  *
1435c05a4fSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1535c05a4fSAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1635c05a4fSAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1735c05a4fSAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1835c05a4fSAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1935c05a4fSAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2035c05a4fSAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2135c05a4fSAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2235c05a4fSAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2335c05a4fSAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2435c05a4fSAdrian Chadd  * SUCH DAMAGE.
2535c05a4fSAdrian Chadd  */
2635c05a4fSAdrian Chadd 
2735c05a4fSAdrian Chadd #include <sys/param.h>
2835c05a4fSAdrian Chadd #include <sys/types.h>
2935c05a4fSAdrian Chadd #include <sys/socket.h>
3035c05a4fSAdrian Chadd #include <sys/cpuset.h>
3135c05a4fSAdrian Chadd #include <sys/sysctl.h>
3235c05a4fSAdrian Chadd 
3335c05a4fSAdrian Chadd #include <stdio.h>
3435c05a4fSAdrian Chadd #include <stdlib.h>
3535c05a4fSAdrian Chadd #include <unistd.h>
3635c05a4fSAdrian Chadd #include <strings.h>
3735c05a4fSAdrian Chadd #include <err.h>
3835c05a4fSAdrian Chadd #include <fcntl.h>
3935c05a4fSAdrian Chadd #include <string.h>
4035c05a4fSAdrian Chadd #include <errno.h>
4135c05a4fSAdrian Chadd 
4235c05a4fSAdrian Chadd #include <netinet/in.h>
4335c05a4fSAdrian Chadd 
4435c05a4fSAdrian Chadd #include "librss.h"
4535c05a4fSAdrian Chadd 
4635c05a4fSAdrian Chadd int
rss_sock_set_recvrss(int fd,int af,int val)4735c05a4fSAdrian Chadd rss_sock_set_recvrss(int fd, int af, int val)
4835c05a4fSAdrian Chadd {
4935c05a4fSAdrian Chadd 	int opt, retval;
5035c05a4fSAdrian Chadd 	socklen_t optlen;
5135c05a4fSAdrian Chadd 	int f1, f2, p;
5235c05a4fSAdrian Chadd 
5335c05a4fSAdrian Chadd 	switch (af) {
5435c05a4fSAdrian Chadd 	case AF_INET:
5535c05a4fSAdrian Chadd 		p = IPPROTO_IP;
5635c05a4fSAdrian Chadd 		f1 = IP_RECVFLOWID;
5735c05a4fSAdrian Chadd 		f2 = IP_RECVRSSBUCKETID;
5835c05a4fSAdrian Chadd 		break;
5935c05a4fSAdrian Chadd 	case AF_INET6:
6035c05a4fSAdrian Chadd 		p = IPPROTO_IPV6;
6135c05a4fSAdrian Chadd 		f1 = IPV6_RECVFLOWID;
6235c05a4fSAdrian Chadd 		f2 = IPV6_RECVRSSBUCKETID;
6335c05a4fSAdrian Chadd 		break;
6435c05a4fSAdrian Chadd 	default:
6535c05a4fSAdrian Chadd 		return (-1);
6635c05a4fSAdrian Chadd 	}
6735c05a4fSAdrian Chadd 
6835c05a4fSAdrian Chadd 	/* Enable/disable flowid */
6935c05a4fSAdrian Chadd 	opt = val;
7035c05a4fSAdrian Chadd 	optlen = sizeof(opt);
7135c05a4fSAdrian Chadd 	retval = setsockopt(fd, p, f1, &opt, optlen);
7235c05a4fSAdrian Chadd 	if (retval < 0) {
7335c05a4fSAdrian Chadd 		warn("%s: setsockopt(IP_RECVFLOWID)", __func__);
7435c05a4fSAdrian Chadd 		return (-1);
7535c05a4fSAdrian Chadd 	}
7635c05a4fSAdrian Chadd 
7735c05a4fSAdrian Chadd 	/* Enable/disable RSS bucket reception */
7835c05a4fSAdrian Chadd 	opt = val;
7935c05a4fSAdrian Chadd 	optlen = sizeof(opt);
8035c05a4fSAdrian Chadd 	retval = setsockopt(fd, p, f2, &opt, optlen);
8135c05a4fSAdrian Chadd 	if (retval < 0) {
8235c05a4fSAdrian Chadd 		warn("%s: setsockopt(IP_RECVRSSBUCKETID)", __func__);
8335c05a4fSAdrian Chadd 		return (-1);
8435c05a4fSAdrian Chadd 	}
8535c05a4fSAdrian Chadd 
8635c05a4fSAdrian Chadd 	return (0);
8735c05a4fSAdrian Chadd }
8835c05a4fSAdrian Chadd 
8935c05a4fSAdrian Chadd static int
rss_getsysctlint(const char * s)9035c05a4fSAdrian Chadd rss_getsysctlint(const char *s)
9135c05a4fSAdrian Chadd {
9235c05a4fSAdrian Chadd 	int val, retval;
9335c05a4fSAdrian Chadd 	size_t rlen;
9435c05a4fSAdrian Chadd 
9535c05a4fSAdrian Chadd 	rlen = sizeof(int);
9635c05a4fSAdrian Chadd 	retval = sysctlbyname(s, &val, &rlen, NULL, 0);
9735c05a4fSAdrian Chadd 	if (retval < 0) {
9835c05a4fSAdrian Chadd 		warn("sysctlbyname (%s)", s);
9935c05a4fSAdrian Chadd 		return (-1);
10035c05a4fSAdrian Chadd 	}
10135c05a4fSAdrian Chadd 
10235c05a4fSAdrian Chadd 	return (val);
10335c05a4fSAdrian Chadd }
10435c05a4fSAdrian Chadd 
10535c05a4fSAdrian Chadd static int
rss_getbucketmap(int * bucket_map,int nbuckets)10635c05a4fSAdrian Chadd rss_getbucketmap(int *bucket_map, int nbuckets)
10735c05a4fSAdrian Chadd {
10835c05a4fSAdrian Chadd 	/* XXX I'm lazy; so static string it is */
10935c05a4fSAdrian Chadd 	char bstr[2048];
11035c05a4fSAdrian Chadd 	int retval;
11135c05a4fSAdrian Chadd 	size_t rlen;
11235c05a4fSAdrian Chadd 	char *s, *ss;
11335c05a4fSAdrian Chadd 	int r, b, c;
11435c05a4fSAdrian Chadd 
11535c05a4fSAdrian Chadd 	/* Paranoia */
11635c05a4fSAdrian Chadd 	memset(bstr, '\0', sizeof(bstr));
11735c05a4fSAdrian Chadd 
11835c05a4fSAdrian Chadd 	rlen = sizeof(bstr) - 1;
11935c05a4fSAdrian Chadd 	retval = sysctlbyname("net.inet.rss.bucket_mapping", bstr, &rlen, NULL, 0);
12035c05a4fSAdrian Chadd 	if (retval < 0) {
12135c05a4fSAdrian Chadd 		warn("sysctlbyname (net.inet.rss.bucket_mapping)");
12235c05a4fSAdrian Chadd 		return (-1);
12335c05a4fSAdrian Chadd 	}
12435c05a4fSAdrian Chadd 
12535c05a4fSAdrian Chadd 	ss = bstr;
12635c05a4fSAdrian Chadd 	while ((s = strsep(&ss, " ")) != NULL) {
12735c05a4fSAdrian Chadd 		r = sscanf(s, "%d:%d", &b, &c);
12835c05a4fSAdrian Chadd 		if (r != 2) {
12935c05a4fSAdrian Chadd 			fprintf(stderr, "%s: string (%s) not parsable\n",
13035c05a4fSAdrian Chadd 			    __func__,
13135c05a4fSAdrian Chadd 			    s);
13235c05a4fSAdrian Chadd 			return (-1);
13335c05a4fSAdrian Chadd 		}
13435c05a4fSAdrian Chadd 		if (b > nbuckets) {
13535c05a4fSAdrian Chadd 			fprintf(stderr, "%s: bucket %d > nbuckets %d\n",
13635c05a4fSAdrian Chadd 			    __func__,
13735c05a4fSAdrian Chadd 			    b,
13835c05a4fSAdrian Chadd 			    nbuckets);
13935c05a4fSAdrian Chadd 			return (-1);
14035c05a4fSAdrian Chadd 		}
14135c05a4fSAdrian Chadd 		/* XXX no maxcpu check */
14235c05a4fSAdrian Chadd 		bucket_map[b] = c;
14335c05a4fSAdrian Chadd 	}
14435c05a4fSAdrian Chadd 	return (0);
14535c05a4fSAdrian Chadd }
14635c05a4fSAdrian Chadd 
14735c05a4fSAdrian Chadd struct rss_config *
rss_config_get(void)14835c05a4fSAdrian Chadd rss_config_get(void)
14935c05a4fSAdrian Chadd {
15035c05a4fSAdrian Chadd 	struct rss_config *rc = NULL;
15135c05a4fSAdrian Chadd 
15235c05a4fSAdrian Chadd 	rc = calloc(1, sizeof(*rc));
15335c05a4fSAdrian Chadd 	if (rc == NULL) {
15435c05a4fSAdrian Chadd 		warn("%s: calloc", __func__);
15535c05a4fSAdrian Chadd 		goto error;
15635c05a4fSAdrian Chadd 	}
15735c05a4fSAdrian Chadd 
15835c05a4fSAdrian Chadd 	rc->rss_ncpus = rss_getsysctlint("net.inet.rss.ncpus");
15935c05a4fSAdrian Chadd 	if (rc->rss_ncpus < 0) {
16035c05a4fSAdrian Chadd 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.ncpus\n", __func__);
16135c05a4fSAdrian Chadd 		goto error;
16235c05a4fSAdrian Chadd 	}
16335c05a4fSAdrian Chadd 
16435c05a4fSAdrian Chadd 	rc->rss_nbuckets = rss_getsysctlint("net.inet.rss.buckets");
16535c05a4fSAdrian Chadd 	if (rc->rss_nbuckets < 0) {
16635c05a4fSAdrian Chadd 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.nbuckets\n", __func__);
16735c05a4fSAdrian Chadd 		goto error;
16835c05a4fSAdrian Chadd 	}
16935c05a4fSAdrian Chadd 
17035c05a4fSAdrian Chadd 	rc->rss_basecpu = rss_getsysctlint("net.inet.rss.basecpu");
17135c05a4fSAdrian Chadd 	if (rc->rss_basecpu< 0) {
17235c05a4fSAdrian Chadd 		fprintf(stderr, "%s: couldn't fetch net.inet.rss.basecpu\n", __func__);
17335c05a4fSAdrian Chadd 		goto error;
17435c05a4fSAdrian Chadd 	}
17535c05a4fSAdrian Chadd 
17635c05a4fSAdrian Chadd 	rc->rss_bucket_map = calloc(rc->rss_nbuckets, sizeof(int));
17735c05a4fSAdrian Chadd 	if (rc->rss_bucket_map == NULL) {
17835c05a4fSAdrian Chadd 		warn("%s: calloc (rss buckets; %d entries)", __func__, rc->rss_nbuckets);
17935c05a4fSAdrian Chadd 		goto error;
18035c05a4fSAdrian Chadd 	}
18135c05a4fSAdrian Chadd 
18235c05a4fSAdrian Chadd 	if (rss_getbucketmap(rc->rss_bucket_map, rc->rss_nbuckets) != 0) {
18335c05a4fSAdrian Chadd 		fprintf(stderr, "%s: rss_getbucketmap failed\n", __func__);
18435c05a4fSAdrian Chadd 		goto error;
18535c05a4fSAdrian Chadd 	}
18635c05a4fSAdrian Chadd 
18735c05a4fSAdrian Chadd 	return (rc);
18835c05a4fSAdrian Chadd 
18935c05a4fSAdrian Chadd error:
190*3b243527SPedro F. Giffuni 	if (rc != NULL) {
19135c05a4fSAdrian Chadd 		free(rc->rss_bucket_map);
19235c05a4fSAdrian Chadd 		free(rc);
193*3b243527SPedro F. Giffuni 	}
19435c05a4fSAdrian Chadd 	return (NULL);
19535c05a4fSAdrian Chadd }
19635c05a4fSAdrian Chadd 
19735c05a4fSAdrian Chadd void
rss_config_free(struct rss_config * rc)19835c05a4fSAdrian Chadd rss_config_free(struct rss_config *rc)
19935c05a4fSAdrian Chadd {
20035c05a4fSAdrian Chadd 
20135c05a4fSAdrian Chadd 	if ((rc != NULL) && rc->rss_bucket_map)
20235c05a4fSAdrian Chadd 		free(rc->rss_bucket_map);
20335c05a4fSAdrian Chadd 	if (rc != NULL)
20435c05a4fSAdrian Chadd 		free(rc);
20535c05a4fSAdrian Chadd }
20635c05a4fSAdrian Chadd 
20735c05a4fSAdrian Chadd int
rss_config_get_bucket_count(struct rss_config * rc)20835c05a4fSAdrian Chadd rss_config_get_bucket_count(struct rss_config *rc)
20935c05a4fSAdrian Chadd {
21035c05a4fSAdrian Chadd 
21135c05a4fSAdrian Chadd 	if (rc == NULL)
21235c05a4fSAdrian Chadd 		return (-1);
21335c05a4fSAdrian Chadd 	return (rc->rss_nbuckets);
21435c05a4fSAdrian Chadd }
21535c05a4fSAdrian Chadd 
21635c05a4fSAdrian Chadd int
rss_get_bucket_cpuset(struct rss_config * rc,rss_bucket_type_t btype,int bucket,cpuset_t * cs)21735c05a4fSAdrian Chadd rss_get_bucket_cpuset(struct rss_config *rc, rss_bucket_type_t btype,
21835c05a4fSAdrian Chadd     int bucket, cpuset_t *cs)
21935c05a4fSAdrian Chadd {
22035c05a4fSAdrian Chadd 
22135c05a4fSAdrian Chadd 	if (bucket < 0 || bucket >= rc->rss_nbuckets) {
22235c05a4fSAdrian Chadd 		errno = EINVAL;
22335c05a4fSAdrian Chadd 		return (-1);
22435c05a4fSAdrian Chadd 	}
22535c05a4fSAdrian Chadd 
22635c05a4fSAdrian Chadd 	/*
22735c05a4fSAdrian Chadd 	 * For now all buckets are the same, but eventually we'll want
22835c05a4fSAdrian Chadd 	 * to allow administrators to set separate RSS cpusets for
22935c05a4fSAdrian Chadd 	 * {kernel,user} {tx, rx} combinations.
23035c05a4fSAdrian Chadd 	 */
23135c05a4fSAdrian Chadd 	if (btype <= RSS_BUCKET_TYPE_NONE || btype > RSS_BUCKET_TYPE_MAX) {
23235c05a4fSAdrian Chadd 		errno = ENOTSUP;
23335c05a4fSAdrian Chadd 		return (-1);
23435c05a4fSAdrian Chadd 	}
23535c05a4fSAdrian Chadd 
23635c05a4fSAdrian Chadd 	CPU_ZERO(cs);
23735c05a4fSAdrian Chadd 	CPU_SET(rc->rss_bucket_map[bucket], cs);
23835c05a4fSAdrian Chadd 
23935c05a4fSAdrian Chadd 	return (0);
24035c05a4fSAdrian Chadd }
24135c05a4fSAdrian Chadd 
24235c05a4fSAdrian Chadd int
rss_set_bucket_rebalance_cb(rss_bucket_rebalance_cb_t * cb,void * cbdata)24335c05a4fSAdrian Chadd rss_set_bucket_rebalance_cb(rss_bucket_rebalance_cb_t *cb, void *cbdata)
24435c05a4fSAdrian Chadd {
24535c05a4fSAdrian Chadd 
24635c05a4fSAdrian Chadd 	(void) cb;
24735c05a4fSAdrian Chadd 	(void) cbdata;
24835c05a4fSAdrian Chadd 
24935c05a4fSAdrian Chadd 	/*
25035c05a4fSAdrian Chadd 	 * For now there's no rebalance callback, so
25135c05a4fSAdrian Chadd 	 * just return 0 and ignore it.
25235c05a4fSAdrian Chadd 	 */
25335c05a4fSAdrian Chadd 	return (0);
25435c05a4fSAdrian Chadd }
255