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