The branch main has been updated by markj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=a471d2b401c9b2d3a95d9139c9a6b123f686a8e9

commit a471d2b401c9b2d3a95d9139c9a6b123f686a8e9
Author:     Mark Johnston <ma...@freebsd.org>
AuthorDate: 2024-11-04 14:40:30 +0000
Commit:     Mark Johnston <ma...@freebsd.org>
CommitDate: 2024-11-04 14:56:19 +0000

    sockstat: Add a -f option to show FIB numbers of sockets
    
    Reviewed by:    zlei
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Sponsored by:   Stormshield
    Differential Revision:  https://reviews.freebsd.org/D47316
---
 usr.bin/sockstat/sockstat.1 |  7 +++++--
 usr.bin/sockstat/sockstat.c | 22 ++++++++++++++++++----
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/usr.bin/sockstat/sockstat.1 b/usr.bin/sockstat/sockstat.1
index d7174b48f8d5..ca486a088b13 100644
--- a/usr.bin/sockstat/sockstat.1
+++ b/usr.bin/sockstat/sockstat.1
@@ -25,7 +25,7 @@
 .\" (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 July 9, 2024
+.Dd October 15, 2024
 .Dt SOCKSTAT 1
 .Os
 .Sh NAME
@@ -33,7 +33,7 @@
 .Nd list open sockets
 .Sh SYNOPSIS
 .Nm
-.Op Fl 46CcIiLlnqSsUuvw
+.Op Fl 46CcfIiLlnqSsUuvw
 .Op Fl j Ar jail
 .Op Fl p Ar ports
 .Op Fl P Ar protocols
@@ -59,6 +59,8 @@ Display the congestion control module, if applicable.
 This is currently only implemented for TCP.
 .It Fl c
 Show connected sockets.
+.It Fl f
+Show the FIB number of each socket.
 .It Fl I
 Show the local address of the socket to which the current socket is spliced, if
 any.
@@ -228,6 +230,7 @@ $ sockstat -6 -P tcp
 .Xr fstat 1 ,
 .Xr netstat 1 ,
 .Xr procstat 1 ,
+.Xr setfib 1 ,
 .Xr inet 4 ,
 .Xr inet6 4 ,
 .Xr protocols 5
diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c
index d2581d9f042e..b18c56ca6f88 100644
--- a/usr.bin/sockstat/sockstat.c
+++ b/usr.bin/sockstat/sockstat.c
@@ -82,6 +82,7 @@ static int     opt_4;         /* Show IPv4 sockets */
 static int      opt_6;         /* Show IPv6 sockets */
 static int      opt_C;         /* Show congestion control */
 static int      opt_c;         /* Show connected sockets */
+static int      opt_f;         /* Show FIB numbers */
 static int      opt_I;         /* Show spliced socket addresses */
 static int      opt_i;         /* Show inp_gencnt */
 static int      opt_j;         /* Show specified jail */
@@ -140,6 +141,7 @@ struct sock {
        int family;
        int proto;
        int state;
+       int fibnum;
        const char *protoname;
        char stack[TCP_FUNCTION_NAME_LEN_MAX];
        char cc[TCP_CA_NAME_MAX];
@@ -771,6 +773,7 @@ gather_inet(int proto)
                sock->splice_socket = so->so_splice_so;
                sock->proto = proto;
                sock->inp_gencnt = xip->inp_gencnt;
+               sock->fibnum = so->so_fibnum;
                if (xip->inp_vflag & INP_IPV4) {
                        sock->family = AF_INET;
                        sockaddr(&laddr->address, sock->family,
@@ -1204,6 +1207,12 @@ displaysock(struct sock *s, int pos)
                default:
                        abort();
                }
+               if (opt_f) {
+                       while (pos < offset)
+                               pos += xprintf(" ");
+                       pos += xprintf("%d", s->fibnum);
+                       offset += 7;
+               }
                if (opt_I) {
                        if (s->splice_socket != 0) {
                                struct sock *sp;
@@ -1329,6 +1338,9 @@ display(void)
                    "USER", "COMMAND", "PID", "FD", "PROTO",
                    opt_w ? 45 : 21, "LOCAL ADDRESS",
                    opt_w ? 45 : 21, "FOREIGN ADDRESS");
+               if (opt_f)
+                       /* RT_MAXFIBS is 65535. */
+                       printf(" %-6s", "FIB");
                if (opt_I)
                        printf(" %-*s", opt_w ? 45 : 21, "SPLICE ADDRESS");
                if (opt_i)
@@ -1453,9 +1465,8 @@ jail_getvnet(int jid)
 static void
 usage(void)
 {
-       fprintf(stderr,
-    "usage: sockstat [-46CcIiLlnqSsUuvw] [-j jid] [-p ports] [-P 
protocols]\n");
-       exit(1);
+       errx(1,
+    "usage: sockstat [-46CcfIiLlnqSsUuvw] [-j jid] [-p ports] [-P protocols]");
 }
 
 int
@@ -1469,7 +1480,7 @@ main(int argc, char *argv[])
        int o, i;
 
        opt_j = -1;
-       while ((o = getopt(argc, argv, "46CcIij:Llnp:P:qSsUuvw")) != -1)
+       while ((o = getopt(argc, argv, "46CcfIij:Llnp:P:qSsUuvw")) != -1)
                switch (o) {
                case '4':
                        opt_4 = 1;
@@ -1483,6 +1494,9 @@ main(int argc, char *argv[])
                case 'c':
                        opt_c = 1;
                        break;
+               case 'f':
+                       opt_f = 1;
+                       break;
                case 'I':
                        opt_I = 1;
                        break;

Reply via email to