Hi,

There exist functions to print routes from the kernel, but they
were not accessible from ddb.  Implement "show all routes" to print
routing tables, and "show route 0xfffffd807e9b0000" for a single
route entry.

Note that the rtable id is not part of a route entry, so it make
no sense to print it there.

ok?

bluhm

Index: sys/ddb/db_command.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/ddb/db_command.c,v
retrieving revision 1.94
diff -u -p -r1.94 db_command.c
--- sys/ddb/db_command.c        14 Apr 2022 19:47:12 -0000      1.94
+++ sys/ddb/db_command.c        27 Jul 2022 16:56:38 -0000
@@ -418,6 +418,31 @@ db_show_all_tdbs(db_expr_t addr, int hav
 }
 #endif
 
+void
+db_show_all_routes(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
+{
+       u_int rtableid = 0;
+
+       if (have_addr)
+               rtableid = addr;
+       if (count == -1)
+               count = 1;
+
+       while (count--) {
+               if (modif[0] != 'I')
+                       db_show_rtable(AF_INET, rtableid);
+               if (modif[0] != 'i')
+                       db_show_rtable(AF_INET6, rtableid);
+               rtableid++;
+       }
+}
+
+void
+db_show_route(db_expr_t addr, int have_addr, db_expr_t count, char *modif)
+{
+       db_show_rtentry((void *)addr, NULL, -1);
+}
+
 /*ARGSUSED*/
 void
 db_object_print_cmd(db_expr_t addr, int have_addr, db_expr_t count, char 
*modif)
@@ -568,6 +593,7 @@ const struct db_command db_show_all_cmds
        { "mounts",     db_show_all_mounts,     0, NULL },
        { "vnodes",     db_show_all_vnodes,     0, NULL },
        { "bufs",       db_show_all_bufs,       0, NULL },
+       { "routes",     db_show_all_routes,     0, NULL },
 #ifdef NFSCLIENT
        { "nfsreqs",    db_show_all_nfsreqs,    0, NULL },
        { "nfsnodes",   db_show_all_nfsnodes,   0, NULL },
@@ -604,6 +630,7 @@ const struct db_command db_show_cmds[] =
        { "pool",       db_pool_print_cmd,      0,      NULL },
        { "proc",       db_proc_print_cmd,      0,      NULL },
        { "registers",  db_show_regs,           0,      NULL },
+       { "route",      db_show_route,          0,      NULL },
        { "socket",     db_socket_print_cmd,    0,      NULL },
        { "struct",     db_ctf_show_struct,     CS_OWN, NULL },
 #ifdef IPSEC
Index: sys/ddb/db_interface.h
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/ddb/db_interface.h,v
retrieving revision 1.22
diff -u -p -r1.22 db_interface.h
--- sys/ddb/db_interface.h      7 Nov 2019 13:16:25 -0000       1.22
+++ sys/ddb/db_interface.h      27 Jul 2022 16:22:11 -0000
@@ -62,6 +62,10 @@ void m_print(void *, int (*)(const char 
 /* kern/uipc_socket.c */
 void so_print(void *, int (*)(const char *, ...));
 
+struct rtentry;
+int db_show_rtentry(struct rtentry *, void *, unsigned int);
+int db_show_rtable(int, unsigned int);
+
 /* nfs/nfs_debug.c */
 void db_show_all_nfsreqs(db_expr_t, int, db_expr_t, char *);
 void nfs_request_print(void *, int, int (*)(const char *, ...));
Index: sys/net/route.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/net/route.c,v
retrieving revision 1.412
diff -u -p -r1.412 route.c
--- sys/net/route.c     28 Jun 2022 10:01:13 -0000      1.412
+++ sys/net/route.c     27 Jul 2022 16:22:11 -0000
@@ -161,12 +161,6 @@ int        rt_clone(struct rtentry **, struct s
 struct sockaddr *rt_plentosa(sa_family_t, int, struct sockaddr_in6 *);
 static int rt_copysa(struct sockaddr *, struct sockaddr *, struct sockaddr **);
 
-#ifdef DDB
-void   db_print_sa(struct sockaddr *);
-void   db_print_ifa(struct ifaddr *);
-int    db_show_rtentry(struct rtentry *, void *, unsigned int);
-#endif
-
 #define        LABELID_MAX     50000
 
 struct rt_label {
@@ -1825,6 +1819,9 @@ rt_plen2mask(struct rtentry *rt, struct 
 #include <machine/db_machdep.h>
 #include <ddb/db_output.h>
 
+void   db_print_sa(struct sockaddr *);
+void   db_print_ifa(struct ifaddr *);
+
 void
 db_print_sa(struct sockaddr *sa)
 {
@@ -1873,8 +1870,8 @@ db_show_rtentry(struct rtentry *rt, void
 {
        db_printf("rtentry=%p", rt);
 
-       db_printf(" flags=0x%x refcnt=%u use=%llu expire=%lld rtableid=%u\n",
-           rt->rt_flags, rt->rt_refcnt.r_refs, rt->rt_use, rt->rt_expire, id);
+       db_printf(" flags=0x%x refcnt=%u use=%llu expire=%lld\n",
+           rt->rt_flags, rt->rt_refcnt.r_refs, rt->rt_use, rt->rt_expire);
 
        db_printf(" key="); db_print_sa(rt_key(rt));
        db_printf(" plen=%d", rt_plen(rt));
@@ -1883,19 +1880,19 @@ db_show_rtentry(struct rtentry *rt, void
        db_printf(" ifa=%p\n", rt->rt_ifa);
        db_print_ifa(rt->rt_ifa);
 
-       db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo);
+       db_printf(" gwroute=%p llinfo=%p priority=%d\n",
+           rt->rt_gwroute, rt->rt_llinfo, rt->rt_priority);
        return (0);
 }
 
 /*
  * Function to print all the route trees.
- * Use this from ddb:  "call db_show_arptab"
  */
 int
-db_show_arptab(void)
+db_show_rtable(int af, unsigned int rtableid)
 {
-       db_printf("Route tree for AF_INET\n");
-       rtable_walk(0, AF_INET, NULL, db_show_rtentry, NULL);
+       db_printf("Route tree for af %d, rtableid %u\n", af, rtableid);
+       rtable_walk(rtableid, af, NULL, db_show_rtentry, NULL);
        return (0);
 }
 #endif /* DDB */
Index: share/man/man4/ddb.4
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/share/man/man4/ddb.4,v
retrieving revision 1.101
diff -u -p -r1.101 ddb.4
--- share/man/man4/ddb.4        31 Mar 2022 17:27:20 -0000      1.101
+++ share/man/man4/ddb.4        27 Jul 2022 17:31:00 -0000
@@ -791,6 +791,12 @@ Note: The
 modifier is not supported on every machine, in which case
 incorrect information may be displayed.
 .\" --------------------
+.It Ic show route Ar addr
+Prints the
+.Li struct rtentry
+at
+.Ar addr .
+.\" --------------------
 .It Ic show socket Ar addr
 Prints the
 .Li struct socket
@@ -952,6 +958,26 @@ For each NFS requests, print a more deta
 See the
 .Ic show nfsreq
 command for more information.
+.El
+.\" --------------------
+.It Xo
+.Ic show all routes
+.Op Cm /iI
+.Op Ar rtableid
+.Op Ic \&, Ns Ar count
+.Xc
+Show internet routing tables.
+Default for
+.Ar rtableid
+is 0 and
+.Ar count
+is 1.
+.Pp
+.Bl -tag -width foo -compact
+.It Cm /i
+Restrict to AF_INET.
+.It Cm /I
+Restrict to AF_INET6.
 .El
 .\" --------------------
 .It Ic show all tdbs Op Cm /f

Reply via email to