Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F133302989
D34139.id102316.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
14 KB
Referenced Files
None
Subscribers
None
D34139.id102316.diff
View Options
diff --git a/usr.sbin/tcpsso/Makefile b/usr.sbin/tcpsso/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.sbin/tcpsso/Makefile
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+PROG= tcpsso
+MAN= tcpsso.8
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/tcpsso/Makefile.depend b/usr.sbin/tcpsso/Makefile.depend
new file mode 100644
--- /dev/null
+++ b/usr.sbin/tcpsso/Makefile.depend
@@ -0,0 +1,17 @@
+# $FreeBSD$
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ gnu/lib/csu \
+ include \
+ include/xlocale \
+ lib/${CSU_DIR} \
+ lib/libc \
+ lib/libcompiler_rt \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/usr.sbin/tcpsso/tcpsso.8 b/usr.sbin/tcpsso/tcpsso.8
new file mode 100644
--- /dev/null
+++ b/usr.sbin/tcpsso/tcpsso.8
@@ -0,0 +1,212 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+.\"
+.\" Copyright (c) 2022 Michael Tuexen <tuexen@FreeBSD.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.Dd February 3, 2022
+.Dt TCPSSO 8
+.Os
+.Sh NAME
+.Nm tcpsso
+.Nd set a socket option on a TCP endpoint
+.Sh SYNOPSIS
+.Nm
+.Fl i Ar id
+.Ar optname
+.Ar optval
+.Nm
+.Fl a
+.Ar optname
+.Ar optval
+.Nm
+.Fl C Ar cc-algo
+.Op Fl S Ar stack
+.Op Fl s Ar state
+.Ar optname
+.Ar optval
+.Nm
+.Op Fl C Ar cc-algo
+.Fl S Ar stack
+.Op Fl s Ar state
+.Ar optname
+.Ar optval
+.Nm
+.Op Fl C Ar cc-algo
+.Op Fl S Ar stack
+.Fl s Ar state
+.Ar optname
+.Ar optval
+.Sh DESCRIPTION
+The
+.Nm
+command applies a
+.Dv IPPROTO_TCP
+level socket option with name
+.Ar optname
+and value
+.Ar optval
+on a TCP endpoint from the command line.
+.Ar optname
+can be specified as a non negative number or a symbolic name like
+.Dv TCP_LOG ,
+.Dv TCP_CONGESTION ,
+or
+.Dv TCP_FUNCTION_BLK .
+.Ar optval
+can be in integer value, which will be converted to a binary value and
+passed as an int value.
+If it cannot be parsed as an integer value, it will be processed as a string.
+If the
+.Ar optname
+is
+.Dv TCP_FUNCTION_BLK
+then
+.Ar optval
+is converted to a
+.Vt "struct tcp_function_set" .
+.Pp
+If
+.Fl i Ar id
+is specified then
+.Nm
+will apply the socket option to the TCP endpoint with the
+.Dv inp_gencnt
+provided as
+.Ar id .
+The
+.Dv inp_gencnt
+for existing TCP endpoints can be determined by using
+.Xr sockstat 1 .
+.Pp
+If
+.Fl a
+is specified then
+.Nm
+will apply the socket option to all TCP endpoints not being in the state
+.Dv TIME_WAIT .
+.Pp
+If
+.Fl C Ar cc-algo
+is specified then
+.Nm
+will apply the socket option to all TCP endpoints using the TCP
+congestion control algorithm
+.Ar cc-algo
+and not being in the state
+.Dv TIME_WAIT .
+.Pp
+If
+.Fl S Ar stack
+is specified then
+.Nm
+will apply the socket option to all TCP endpoints using the TCP
+stack
+.Ar stack
+and not being in the state
+.Dv TIME_WAIT .
+.Pp
+If
+.Fl s Ar state
+is specified then
+.Nm
+will apply the socket option to all TCP endpoints being in the state
+.Ar state .
+.Ar state
+is one of
+.Dv CLOSED ,
+.Dv LISTEN ,
+.Dv SYN_SENT ,
+.Dv SYN_RCVD ,
+.Dv ESTABLISHED ,
+.Dv CLOSE_WAIT ,
+.Dv FIN_WAIT_1 ,
+.Dv CLOSING ,
+.Dv LAST_ACK ,
+.Dv FIN_WAIT_2 .
+.Pp
+If multiple of
+.Fl C Ar cc-algo ,
+.Fl S Ar stack ,
+and
+.Fl s Ar state
+are specified,
+.Nm
+will apply the socket option to all TCP endpoints not being in the
+state
+.Dv TIME_WAIT
+and using the congestion control algorithm
+.Ar cc-algo ,
+being in the state
+.Ar state ,
+and using the TCP stack
+.Ar stack ,
+if specified.
+.Pp
+If none of the
+.Fl a ,
+.Fl C ,
+.Fl S ,
+or
+.Fl s
+options are specified then the option
+.Fl i
+must be specified.
+.Sh EXIT STATUS
+.Ex -std
+.Sh EXAMPLES
+To diagnose a problem with a particular TCP connection to
+.Xr thttpd 8 ,
+first determine its
+.Dv inp_gencnt
+using
+.Xr sockstat 1 :
+.Bd -literal -offset indent
+# sockstat -4 -c -i -p 80 -P tcp -q
+nobody thttpd 827 2 tcp4 \e
+ 192.168.1.1:80 192.168.1.2:53736 435
+.Ed
+.Pp
+Then, use the following command to enable Black Box Logging on it:
+.Bd -literal -offset indent
+# tcpsso -i 435 TCP_LOG 4
+.Ed
+.Pp
+To switch all TCP endpoints from using the freebsd stack to the rack stack use:
+.Bd -literal -offset indent
+# tcpsso -S freebsd TCP_FUNCTION_BLK rack
+.Ed
+.Pp
+The following command will set the congestion control module of all TCP
+endpoints currently using cubic as its congestion control algorithm to the
+congestion control algorithm new-reno:
+.Bd -literal -offset indent
+# tcpsso -C cubic TCP_CONGESTION new-reno
+.Ed
+.Sh SEE ALSO
+.Xr setsockopt 2 ,
+.Xr sockstat 1 ,
+.Xr tcp 4 ,
+.Xr tcp_functions 9
+.Sh AUTHORS
+.An Michael Tuexen Aq Mt tuexen@FreeBSD.org
diff --git a/usr.sbin/tcpsso/tcpsso.c b/usr.sbin/tcpsso/tcpsso.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/tcpsso/tcpsso.c
@@ -0,0 +1,318 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2022 Michael Tuexen <tuexen@FreeBSD.org>
+ * Copyright (c) 2009 Juli Mallett <jmallett@FreeBSD.org>
+ * Copyright (c) 2004 Markus Friedl <markus@openbsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/sysctl.h>
+
+#include <netinet/in.h>
+#include <netinet/in_pcb.h>
+#define TCPSTATES
+#include <netinet/tcp_fsm.h>
+#include <netinet/tcp_var.h>
+
+#include <err.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static struct xinpgen *
+getxpcblist(const char *name)
+{
+ struct xinpgen *xinp;
+ size_t len;
+ int rv;
+
+ len = 0;
+ rv = sysctlbyname(name, NULL, &len, NULL, 0);
+ if (rv == -1)
+ err(1, "sysctlbyname %s", name);
+
+ if (len == 0)
+ errx(1, "%s is empty", name);
+
+ xinp = malloc(len);
+ if (xinp == NULL)
+ errx(1, "malloc failed");
+
+ rv = sysctlbyname(name, xinp, &len, NULL, 0);
+ if (rv == -1)
+ err(1, "sysctlbyname %s", name);
+
+ return (xinp);
+}
+
+static bool
+tcpsso(uint64_t id, struct sockopt_parameters *params, size_t optlen)
+{
+ int rv;
+
+ params->sop_id = id;
+ rv = sysctlbyname("net.inet.tcp.setsockopt", NULL, NULL, params,
+ sizeof(struct sockopt_parameters) + optlen);
+ if (rv == -1) {
+ warn("Failed for id %" PRIu64, params->sop_id);
+ return (false);
+ } else
+ return (true);
+}
+
+static bool
+tcpssoall(const char *ca_name, const char *stack, int state,
+ struct sockopt_parameters *params, size_t optlen)
+{
+ struct xinpgen *head, *xinp;
+ struct xtcpcb *xtp;
+ struct xinpcb *xip;
+ bool ok;
+
+ ok = true;
+
+ head = getxpcblist("net.inet.tcp.pcblist");
+
+#define XINP_NEXT(xinp) \
+ ((struct xinpgen *)(uintptr_t)((uintptr_t)(xinp) + (xinp)->xig_len))
+
+ for (xinp = XINP_NEXT(head); xinp->xig_len > sizeof *xinp;
+ xinp = XINP_NEXT(xinp)) {
+ xtp = (struct xtcpcb *)xinp;
+ xip = &xtp->xt_inp;
+
+ /* Ignore PCBs which were freed during copyout. */
+ if (xip->inp_gencnt > head->xig_gen)
+ continue;
+
+
+ /* Skip endpoints in TIME WAIT. */
+ if (xtp->t_state == TCPS_TIME_WAIT)
+ continue;
+
+ /* If requested, skip sockets not having the requested state. */
+ if ((state != -1) && (xtp->t_state != state))
+ continue;
+
+ /*
+ * If requested, skip sockets not having the requested
+ * congestion control algorithm.
+ */
+ if (ca_name[0] != '\0' &&
+ strncmp(xtp->xt_cc, ca_name, TCP_CA_NAME_MAX))
+ continue;
+
+ /* If requested, skip sockets not having the requested stack. */
+ if (stack[0] != '\0' &&
+ strncmp(xtp->xt_stack, stack, TCP_FUNCTION_NAME_LEN_MAX))
+ continue;
+
+ params->sop_inc = xip->inp_inc;
+ if (!tcpsso(xip->inp_gencnt, params, optlen))
+ ok = false;
+ }
+ free(head);
+
+ return (ok);
+}
+
+struct int_name {
+ int value;
+ const char *name;
+};
+
+#define entry(arg) { arg, #arg }
+
+static struct int_name tcp_so_names[] = {
+ entry(TCP_LOG),
+ entry(TCP_CONGESTION),
+ entry(TCP_FUNCTION_BLK),
+ {0, NULL}
+};
+
+static struct sockopt_parameters *
+create_parameters(char *optname_str, char *optval_str, size_t *optlen)
+{
+ long long arg;
+ int i, optname, optval_int;
+ struct sockopt_parameters *params;
+ char *end;
+ bool optval_is_int;
+
+ if (optname_str == NULL || *optname_str == '\0')
+ return (NULL);
+ arg = strtoll(optname_str, &end, 0);
+ if (*end != '\0') {
+ for (i = 0; tcp_so_names[i].name != NULL; i++)
+ if (strcmp(optname_str, tcp_so_names[i].name) == 0) {
+ optname = tcp_so_names[i].value;
+ break;
+ }
+ if (tcp_so_names[i].name == NULL) {
+ errx(1, "unsupported option name %s", optname_str);
+ return (NULL);
+ }
+ } else {
+ if (arg < 0) {
+ errx(1, "option name negative %s", optname_str);
+ return (NULL);
+ } else if (arg > INT_MAX) {
+ errx(1, "option name too large %s", optname_str);
+ return (NULL);
+ } else
+ optname = (int)arg;
+ }
+ if (optval_str == NULL || *optval_str == '\0')
+ return (NULL);
+ arg = strtol(optval_str, &end, 0);
+ optval_is_int = (*end == '\0');
+ if (optval_is_int) {
+ if (arg < INT_MIN) {
+ errx(1, "option name too small %s", optval_str);
+ return (NULL);
+ } else if (arg > INT_MAX) {
+ errx(1, "option name too large %s", optval_str);
+ return (NULL);
+ } else
+ optval_int = (int)arg;
+ }
+ switch (optname) {
+ case TCP_FUNCTION_BLK:
+ *optlen = sizeof(struct tcp_function_set);
+ break;
+ default:
+ if (optval_is_int)
+ *optlen = sizeof(int);
+ else
+ *optlen = strlen(optval_str) + 1;
+ break;
+ }
+ params = malloc(sizeof(struct sockopt_parameters) + *optlen);
+ if (params == NULL)
+ return (NULL);
+ memset(params, 0, sizeof(struct sockopt_parameters) + *optlen);
+ params->sop_level = IPPROTO_TCP;
+ params->sop_optname = optname;
+ switch (optname) {
+ case TCP_FUNCTION_BLK:
+ strlcpy(params->sop_optval, optval_str,
+ TCP_FUNCTION_NAME_LEN_MAX);
+ break;
+ default:
+ if (optval_is_int)
+ memcpy(params->sop_optval, &optval_int, *optlen);
+ else
+ memcpy(params->sop_optval, optval_str, *optlen);
+ }
+ return (params);
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr,
+"usage: tcpsso -i id opt-name opt-value\n"
+" tcpsso -a opt-name opt-value\n"
+" tcpsso -C cc-algo [-S stack] [-s state] opt-name opt-value\n"
+" tcpsso [-C cc-algo] -S stack [-s state] opt-name opt-value\n"
+" tcpsso [-C cc-algo] [-S stack] -s state opt-name opt-value\n");
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct sockopt_parameters *params;
+ uint64_t id;
+ size_t optlen;
+ int ch, state;
+ char stack[TCP_FUNCTION_NAME_LEN_MAX];
+ char ca_name[TCP_CA_NAME_MAX];
+ bool ok, apply_all, apply_subset, apply_specific;
+
+ apply_all = false;
+ apply_subset = false;
+ apply_specific = false;
+ ca_name[0] = '\0';
+ stack[0] = '\0';
+ state = -1;
+ id = 0;
+
+ while ((ch = getopt(argc, argv, "aC:i:S:s:")) != -1) {
+ switch (ch) {
+ case 'a':
+ apply_all = true;
+ break;
+ case 'C':
+ apply_subset = true;
+ strlcpy(ca_name, optarg, sizeof(ca_name));
+ break;
+ case 'i':
+ apply_specific = true;
+ id = strtoull(optarg, NULL, 0);
+ break;
+ case 'S':
+ apply_subset = true;
+ strlcpy(stack, optarg, sizeof(stack));
+ break;
+ case 's':
+ apply_subset = true;
+ for (state = 0; state < TCP_NSTATES; state++) {
+ if (strcmp(tcpstates[state], optarg) == 0)
+ break;
+ }
+ break;
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if ((state == TCP_NSTATES) ||
+ (state == TCPS_TIME_WAIT) ||
+ (argc != 2) ||
+ (apply_all && apply_subset) ||
+ (apply_all && apply_specific) ||
+ (apply_subset && apply_specific) ||
+ !(apply_all || apply_subset || apply_specific))
+ usage();
+ params = create_parameters(argv[0], argv[1], &optlen);
+ if (params != NULL) {
+ if (apply_specific)
+ ok = tcpsso(id, params, optlen);
+ else
+ ok = tcpssoall(ca_name, stack, state, params, optlen);
+ free(params);
+ } else
+ ok = false;
+ exit(ok ? 0 : 1);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Oct 25, 7:10 PM (23 m, 38 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
24185418
Default Alt Text
D34139.id102316.diff (14 KB)
Attached To
Mode
D34139: tcpsso, a tool to set socket options for TCP endpoints via the command line
Attached
Detach File
Event Timeline
Log In to Comment