This is the next step in refactoring the output functions in bgpctl.

This diff changes the way individual show_XY() functions are called. Up
until now they got passed an imsg and then did stuff based on the imsg
type. Now show() will look at the imsg and based on the type call the
right function for this type.

Every show_XY() function is now only displaying one object. Because of
this some additional functions have been added. Diff looks big but it is
mostly reindents because the case statement in the individual functions
got removed.

OK?
-- 
:wq Claudio

Index: bgpctl.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpctl/bgpctl.c,v
retrieving revision 1.250
diff -u -p -r1.250 bgpctl.c
--- bgpctl.c    13 Dec 2019 11:11:22 -0000      1.250
+++ bgpctl.c    13 Dec 2019 12:06:59 -0000
@@ -51,10 +51,9 @@ enum neighbor_views {
 int             main(int, char *[]);
 int             show(struct imsg *, struct parse_result *);
 char           *fmt_peer(const char *, const struct bgpd_addr *, int);
-int             show_summary_msg(struct imsg *);
-int             show_summary_terse_msg(struct imsg *);
-int             show_neighbor_terse(struct imsg *);
-int             show_neighbor_msg(struct imsg *, enum neighbor_views);
+void            show_summary(struct peer *);
+void            show_neighbor_full(struct peer *, struct parse_result *);
+void            show_neighbor(struct peer *, struct parse_result *res);
 void            print_neighbor_capa_mp(struct peer *);
 void            print_neighbor_capa_restart(struct peer *);
 void            print_neighbor_msgstats(struct peer *);
@@ -62,15 +61,16 @@ void                 print_timer(const char *, time_t)
 static char    *fmt_timeframe(time_t t);
 static char    *fmt_timeframe_core(time_t t);
 void            show_fib_flags(u_int16_t);
-int             show_fib_msg(struct imsg *);
-int             show_nexthop_msg(struct imsg *);
-int             show_interface_msg(struct imsg *);
+void            show_fib(struct kroute_full *);
+void            show_fib_table(struct ktable *);
+void            show_nexthop(struct ctl_show_nexthop *);
+void            show_interface(struct ctl_show_interface *);
 void            print_prefix(struct bgpd_addr *, u_int8_t, u_int8_t, u_int8_t);
 const char *    print_origin(u_int8_t, int);
 const char *    print_ovs(u_int8_t, int);
 void            print_flags(u_int8_t, int);
-int             show_rib_summary_msg(struct imsg *);
-int             show_rib_detail_msg(struct imsg *, int);
+void            show_rib(struct ctl_show_rib *, u_char *, size_t,
+                   struct parse_result *);
 void            show_rib_brief(struct ctl_show_rib *, u_char *, size_t);
 void            show_rib_detail(struct ctl_show_rib *, u_char *, size_t, int);
 void            show_attr(void *, u_int16_t, int);
@@ -78,7 +78,8 @@ void           show_communities(u_char *, size_t
 void            show_community(u_char *, u_int16_t);
 void            show_large_community(u_char *, u_int16_t);
 void            show_ext_community(u_char *, u_int16_t);
-int             show_rib_memory_msg(struct imsg *);
+void            show_rib_mem(struct rde_memstats *);
+void            show_rib_hash(struct rde_hashstats *);
 void            send_filterset(struct imsgbuf *, struct filter_set_head *);
 const char     *get_errstr(u_int8_t, u_int8_t);
 void            show_mrt_dump_neighbors(struct mrt_rib *, struct mrt_peer *,
@@ -415,10 +416,84 @@ main(int argc, char *argv[])
 int
 show(struct imsg *imsg, struct parse_result *res)
 {
-       u_int rescode;
-       int done = 0;
+       struct peer             *p;
+       struct ctl_timer        *t;
+       struct ctl_show_interface       *iface;
+       struct ctl_show_nexthop *nh;
+       struct kroute_full      *kf;
+       struct ktable           *kt;
+       struct ctl_show_rib      rib;
+       u_char                  *asdata;
+       struct rde_memstats     stats;
+       struct rde_hashstats    hash;
+       u_int                   rescode, ilen;
+       size_t                  aslen;
 
        switch (imsg->hdr.type) {
+       case IMSG_CTL_SHOW_NEIGHBOR:
+               p = imsg->data;
+               show_neighbor(p, res);
+               break;
+       case IMSG_CTL_SHOW_TIMER:
+               t = imsg->data;
+               if (t->type > 0 && t->type < Timer_Max)
+                       print_timer(timernames[t->type], t->val);
+               break;
+       case IMSG_CTL_SHOW_INTERFACE:
+               iface = imsg->data;
+               show_interface(iface);
+               break;
+       case IMSG_CTL_SHOW_NEXTHOP:
+               nh = imsg->data;
+               show_nexthop(nh);
+               break;
+       case IMSG_CTL_KROUTE:
+       case IMSG_CTL_SHOW_NETWORK:
+               if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(*kf))
+                       errx(1, "wrong imsg len");
+               kf = imsg->data;
+               show_fib(kf);
+               break;
+       case IMSG_CTL_SHOW_FIB_TABLES:
+               if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(*kt))
+                       errx(1, "wrong imsg len");
+               kt = imsg->data;
+
+               show_fib_table(kt);
+               break;
+       case IMSG_CTL_SHOW_RIB:
+               if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(rib))
+                       errx(1, "wrong imsg len");
+               memcpy(&rib, imsg->data, sizeof(rib));
+               aslen = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof(rib);
+               asdata = imsg->data;
+               asdata += sizeof(rib);
+               show_rib(&rib, asdata, aslen, res);
+               break;
+       case IMSG_CTL_SHOW_RIB_COMMUNITIES:
+               ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
+               if (ilen % sizeof(struct community)) {
+                       warnx("bad IMSG_CTL_SHOW_RIB_COMMUNITIES received");
+                       break;
+               }
+               show_communities(imsg->data, ilen, res->flags);
+               break;
+       case IMSG_CTL_SHOW_RIB_ATTR:
+               ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
+               if (ilen < 3) {
+                       warnx("bad IMSG_CTL_SHOW_RIB_ATTR received");
+                       break;
+               }
+               show_attr(imsg->data, ilen, res->flags);
+               break;
+       case IMSG_CTL_SHOW_RIB_MEM:
+               memcpy(&stats, imsg->data, sizeof(stats));
+               show_rib_mem(&stats);
+               break;
+       case IMSG_CTL_SHOW_RIB_HASH:
+               memcpy(&hash, imsg->data, sizeof(hash));
+               show_rib_hash(&hash);
+               break;
        case IMSG_CTL_RESULT:
                if (imsg->hdr.len != IMSG_HEADER_SIZE + sizeof(rescode)) {
                        warnx("got IMSG_CTL_RESULT with wrong len");
@@ -430,70 +505,11 @@ show(struct imsg *imsg, struct parse_res
        case IMSG_CTL_END:
                return (1);
        default:
+               warnx("unknown imsg %d received", imsg->hdr.type);
                break;
        }
 
-       switch (res->action) {
-       case SHOW:
-       case SHOW_SUMMARY:
-               done = show_summary_msg(imsg);
-               break;
-       case SHOW_SUMMARY_TERSE:
-               done = show_summary_terse_msg(imsg);
-               break;
-       case SHOW_FIB:
-       case SHOW_FIB_TABLES:
-       case NETWORK_SHOW:
-               done = show_fib_msg(imsg);
-               break;
-       case SHOW_NEXTHOP:
-               done = show_nexthop_msg(imsg);
-               break;
-       case SHOW_INTERFACE:
-               done = show_interface_msg(imsg);
-               break;
-       case SHOW_NEIGHBOR:
-               done = show_neighbor_msg(imsg, NV_DEFAULT);
-               break;
-       case SHOW_NEIGHBOR_TIMERS:
-               done = show_neighbor_msg(imsg, NV_TIMERS);
-               break;
-       case SHOW_NEIGHBOR_TERSE:
-               done = show_neighbor_terse(imsg);
-               break;
-       case SHOW_RIB:
-               if (res->flags & F_CTL_DETAIL)
-                       done = show_rib_detail_msg(imsg,
-                           res->flags);
-               else
-                       done = show_rib_summary_msg(imsg);
-               break;
-       case SHOW_RIB_MEM:
-               done = show_rib_memory_msg(imsg);
-               break;
-       case NEIGHBOR:
-       case NEIGHBOR_UP:
-       case NEIGHBOR_DOWN:
-       case NEIGHBOR_CLEAR:
-       case NEIGHBOR_RREFRESH:
-       case NEIGHBOR_DESTROY:
-       case NONE:
-       case RELOAD:
-       case FIB:
-       case FIB_COUPLE:
-       case FIB_DECOUPLE:
-       case NETWORK_ADD:
-       case NETWORK_REMOVE:
-       case NETWORK_FLUSH:
-       case NETWORK_BULK_ADD:
-       case NETWORK_BULK_REMOVE:
-       case LOG_VERBOSE:
-       case LOG_BRIEF:
-       case SHOW_MRT:
-       case NETWORK_MRT:
-               break;
-       }
-       return (done);
+       return (0);
 }
 
 char *
@@ -503,7 +519,7 @@ fmt_peer(const char *descr, const struct
        const char      *ip;
        char            *p;
 
-       if (descr[0] && !nodescr) {
+       if (descr && descr[0] && !nodescr) {
                if ((p = strdup(descr)) == NULL)
                        err(1, NULL);
                return (p);
@@ -522,127 +538,44 @@ fmt_peer(const char *descr, const struct
        return (p);
 }
 
-int
-show_summary_msg(struct imsg *imsg)
+void
+show_summary(struct peer *p)
 {
-       struct peer             *p;
        char                    *s;
        const char              *a;
        size_t                  alen;
 
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_NEIGHBOR:
-               p = imsg->data;
-               s = fmt_peer(p->conf.descr, &p->conf.remote_addr,
-                   p->conf.remote_masklen);
-
-               a = log_as(p->conf.remote_as);
-               alen = strlen(a);
-               /* max displayed length of the peers name is 28 */
-               if (alen < 28) {
-                       if (strlen(s) > 28 - alen)
-                               s[28 - alen] = 0;
-               } else
-                       alen = 0;
-
-               printf("%-*s %s %10llu %10llu %5u %-8s ",
-                   (28 - (int)alen), s, a,
-                   p->stats.msg_rcvd_open + p->stats.msg_rcvd_notification +
-                   p->stats.msg_rcvd_update + p->stats.msg_rcvd_keepalive +
-                   p->stats.msg_rcvd_rrefresh,
-                   p->stats.msg_sent_open + p->stats.msg_sent_notification +
-                   p->stats.msg_sent_update + p->stats.msg_sent_keepalive +
-                   p->stats.msg_sent_rrefresh,
-                   p->wbuf.queued,
-                   fmt_timeframe(p->stats.last_updown));
-               if (p->state == STATE_ESTABLISHED) {
-                       printf("%6u", p->stats.prefix_cnt);
-                       if (p->conf.max_prefix != 0)
-                               printf("/%u", p->conf.max_prefix);
-               } else if (p->conf.template)
-                       printf("Template");
-               else
-                       printf("%s", statenames[p->state]);
-               printf("\n");
-               free(s);
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
-
-       return (0);
-}
-
-int
-show_summary_terse_msg(struct imsg *imsg)
-{
-       struct peer             *p;
-       char                    *s;
-
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_NEIGHBOR:
-               p = imsg->data;
-               s = fmt_peer(p->conf.descr, &p->conf.remote_addr,
-                   p->conf.remote_masklen);
-               printf("%s %s %s\n", s, log_as(p->conf.remote_as),
-                   p->conf.template ? "Template" : statenames[p->state]);
-               free(s);
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
+       s = fmt_peer(p->conf.descr, &p->conf.remote_addr,
+           p->conf.remote_masklen);
 
-       return (0);
-}
+       a = log_as(p->conf.remote_as);
+       alen = strlen(a);
+       /* max displayed length of the peers name is 28 */
+       if (alen < 28) {
+               if (strlen(s) > 28 - alen)
+                       s[28 - alen] = 0;
+       } else
+               alen = 0;
 
-int
-show_neighbor_terse(struct imsg *imsg)
-{
-       struct peer             *p;
-       char                    *s;
-
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_NEIGHBOR:
-               p = imsg->data;
-               if ((p->conf.remote_addr.aid == AID_INET &&
-                   p->conf.remote_masklen != 32) ||
-                   (p->conf.remote_addr.aid == AID_INET6 &&
-                   p->conf.remote_masklen != 128)) {
-                       if (asprintf(&s, "%s/%u",
-                           log_addr(&p->conf.remote_addr),
-                           p->conf.remote_masklen) == -1)
-                               err(1, NULL);
-               } else
-                       if ((s = strdup(log_addr(&p->conf.remote_addr))) ==
-                           NULL)
-                               err(1, "strdup");
-
-               printf("%llu %llu %llu %llu %llu %llu %llu %llu %llu "
-                   "%llu %u %u %llu %llu %llu %llu %s %s \"%s\"\n",
-                   p->stats.msg_sent_open, p->stats.msg_rcvd_open,
-                   p->stats.msg_sent_notification,
-                   p->stats.msg_rcvd_notification,
-                   p->stats.msg_sent_update, p->stats.msg_rcvd_update,
-                   p->stats.msg_sent_keepalive, p->stats.msg_rcvd_keepalive,
-                   p->stats.msg_sent_rrefresh, p->stats.msg_rcvd_rrefresh,
-                   p->stats.prefix_cnt, p->conf.max_prefix,
-                   p->stats.prefix_sent_update, p->stats.prefix_rcvd_update,
-                   p->stats.prefix_sent_withdraw,
-                   p->stats.prefix_rcvd_withdraw, s,
-                   log_as(p->conf.remote_as), p->conf.descr);
-               free(s);
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
-
-       return (0);
+       printf("%-*s %s %10llu %10llu %5u %-8s ",
+           (28 - (int)alen), s, a,
+           p->stats.msg_rcvd_open + p->stats.msg_rcvd_notification +
+           p->stats.msg_rcvd_update + p->stats.msg_rcvd_keepalive +
+           p->stats.msg_rcvd_rrefresh,
+           p->stats.msg_sent_open + p->stats.msg_sent_notification +
+           p->stats.msg_sent_update + p->stats.msg_sent_keepalive +
+           p->stats.msg_sent_rrefresh,
+           p->wbuf.queued,
+           fmt_timeframe(p->stats.last_updown));
+       if (p->state == STATE_ESTABLISHED) {
+               printf("%6u", p->stats.prefix_cnt);
+               if (p->conf.max_prefix != 0)
+                       printf("/%u", p->conf.max_prefix);
+       } else if (p->conf.template)
+               printf("Template");
+       else
+               printf("%s", statenames[p->state]);
+       printf("\n");
 }
 
 const char *
@@ -665,135 +598,162 @@ print_auth_method(enum auth_method metho
        }
 }
 
-int
-show_neighbor_msg(struct imsg *imsg, enum neighbor_views nv)
+void
+show_neighbor_full(struct peer *p, struct parse_result *res)
 {
-       struct peer             *p;
-       struct ctl_timer        *t;
        struct in_addr           ina;
        char                    *s;
        int                      hascapamp = 0;
        u_int8_t                 i;
 
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_NEIGHBOR:
-               p = imsg->data;
-               if ((p->conf.remote_addr.aid == AID_INET &&
-                   p->conf.remote_masklen != 32) ||
-                   (p->conf.remote_addr.aid == AID_INET6 &&
-                   p->conf.remote_masklen != 128)) {
-                       if (asprintf(&s, "%s/%u",
-                           log_addr(&p->conf.remote_addr),
-                           p->conf.remote_masklen) == -1)
-                               err(1, NULL);
-               } else
-                       if ((s = strdup(log_addr(&p->conf.remote_addr))) ==
-                           NULL)
-                               err(1, "strdup");
+       if ((p->conf.remote_addr.aid == AID_INET &&
+           p->conf.remote_masklen != 32) ||
+           (p->conf.remote_addr.aid == AID_INET6 &&
+           p->conf.remote_masklen != 128)) {
+               if (asprintf(&s, "%s/%u",
+                   log_addr(&p->conf.remote_addr),
+                   p->conf.remote_masklen) == -1)
+                       err(1, NULL);
+       } else
+               if ((s = strdup(log_addr(&p->conf.remote_addr))) ==
+                   NULL)
+                       err(1, "strdup");
 
-               ina.s_addr = p->remote_bgpid;
-               printf("BGP neighbor is %s, ", s);
-               free(s);
-               if (p->conf.remote_as == 0 && p->conf.template)
-                       printf("remote AS: accept any");
-               else
-                       printf("remote AS %s", log_as(p->conf.remote_as));
-               if (p->conf.template)
-                       printf(", Template");
-               if (p->template)
-                       printf(", Cloned");
-               if (p->conf.passive)
-                       printf(", Passive");
-               if (p->conf.ebgp && p->conf.distance > 1)
-                       printf(", Multihop (%u)", (int)p->conf.distance);
+       ina.s_addr = p->remote_bgpid;
+       printf("BGP neighbor is %s, ", s);
+       free(s);
+       if (p->conf.remote_as == 0 && p->conf.template)
+               printf("remote AS: accept any");
+       else
+               printf("remote AS %s", log_as(p->conf.remote_as));
+       if (p->conf.template)
+               printf(", Template");
+       if (p->template)
+               printf(", Cloned");
+       if (p->conf.passive)
+               printf(", Passive");
+       if (p->conf.ebgp && p->conf.distance > 1)
+               printf(", Multihop (%u)", (int)p->conf.distance);
+       printf("\n");
+       if (p->conf.descr[0])
+               printf(" Description: %s\n", p->conf.descr);
+       if (p->conf.max_prefix) {
+               printf(" Max-prefix: %u", p->conf.max_prefix);
+               if (p->conf.max_prefix_restart)
+                       printf(" (restart %u)",
+                           p->conf.max_prefix_restart);
                printf("\n");
-               if (p->conf.descr[0])
-                       printf(" Description: %s\n", p->conf.descr);
-               if (p->conf.max_prefix) {
-                       printf(" Max-prefix: %u", p->conf.max_prefix);
-                       if (p->conf.max_prefix_restart)
-                               printf(" (restart %u)",
-                                   p->conf.max_prefix_restart);
-                       printf("\n");
+       }
+       printf("  BGP version 4, remote router-id %s",
+           inet_ntoa(ina));
+       printf("%s\n", print_auth_method(p->auth.method));
+       printf("  BGP state = %s", statenames[p->state]);
+       if (p->conf.down) {
+               printf(", marked down");
+               if (*(p->conf.shutcomm)) {
+                       printf(" with shutdown reason \"%s\"",
+                           log_shutcomm(p->conf.shutcomm));
                }
-               printf("  BGP version 4, remote router-id %s",
-                   inet_ntoa(ina));
-               printf("%s\n", print_auth_method(p->auth.method));
-               printf("  BGP state = %s", statenames[p->state]);
-               if (p->conf.down) {
-                       printf(", marked down");
-                       if (*(p->conf.shutcomm)) {
-                               printf(" with shutdown reason \"%s\"",
-                                   log_shutcomm(p->conf.shutcomm));
-                       }
+       }
+       if (p->stats.last_updown != 0)
+               printf(", %s for %s",
+                   p->state == STATE_ESTABLISHED ? "up" : "down",
+                   fmt_timeframe(p->stats.last_updown));
+       printf("\n");
+       printf("  Last read %s, holdtime %us, keepalive interval %us\n",
+           fmt_timeframe(p->stats.last_read),
+           p->holdtime, p->holdtime/3);
+       for (i = 0; i < AID_MAX; i++)
+               if (p->capa.peer.mp[i])
+                       hascapamp = 1;
+       if (hascapamp || p->capa.peer.refresh ||
+           p->capa.peer.grestart.restart || p->capa.peer.as4byte) {
+               printf("  Neighbor capabilities:\n");
+               if (hascapamp) {
+                       printf("    Multiprotocol extensions: ");
+                       print_neighbor_capa_mp(p);
+                       printf("\n");
                }
-               if (p->stats.last_updown != 0)
-                       printf(", %s for %s",
-                           p->state == STATE_ESTABLISHED ? "up" : "down",
-                           fmt_timeframe(p->stats.last_updown));
-               printf("\n");
-               printf("  Last read %s, holdtime %us, keepalive interval %us\n",
-                   fmt_timeframe(p->stats.last_read),
-                   p->holdtime, p->holdtime/3);
-               for (i = 0; i < AID_MAX; i++)
-                       if (p->capa.peer.mp[i])
-                               hascapamp = 1;
-               if (hascapamp || p->capa.peer.refresh ||
-                   p->capa.peer.grestart.restart || p->capa.peer.as4byte) {
-                       printf("  Neighbor capabilities:\n");
-                       if (hascapamp) {
-                               printf("    Multiprotocol extensions: ");
-                               print_neighbor_capa_mp(p);
-                               printf("\n");
-                       }
-                       if (p->capa.peer.refresh)
-                               printf("    Route Refresh\n");
-                       if (p->capa.peer.grestart.restart) {
-                               printf("    Graceful Restart");
-                               print_neighbor_capa_restart(p);
-                               printf("\n");
-                       }
-                       if (p->capa.peer.as4byte)
-                               printf("    4-byte AS numbers\n");
+               if (p->capa.peer.refresh)
+                       printf("    Route Refresh\n");
+               if (p->capa.peer.grestart.restart) {
+                       printf("    Graceful Restart");
+                       print_neighbor_capa_restart(p);
+                       printf("\n");
                }
+               if (p->capa.peer.as4byte)
+                       printf("    4-byte AS numbers\n");
+       }
+       printf("\n");
+
+       if (res->action == SHOW_NEIGHBOR_TIMERS)
+               return;
+
+       print_neighbor_msgstats(p);
+       printf("\n");
+       if (*(p->stats.last_shutcomm)) {
+               printf("  Last received shutdown reason: \"%s\"\n",
+                   log_shutcomm(p->stats.last_shutcomm));
+       }
+       if (p->state == STATE_IDLE) {
+               static const char       *errstr;
+
+               errstr = get_errstr(p->stats.last_sent_errcode,
+                   p->stats.last_sent_suberr);
+               if (errstr)
+                       printf("  Last error: %s\n\n", errstr);
+       } else {
+               printf("  Local host:  %20s, Local port:  %5u\n",
+                   log_addr(&p->local), p->local_port);
+
+               printf("  Remote host: %20s, Remote port: %5u\n",
+                   log_addr(&p->remote), p->remote_port);
                printf("\n");
-               if (nv == NV_TIMERS)
-                       break;
-               print_neighbor_msgstats(p);
-               printf("\n");
-               if (*(p->stats.last_shutcomm)) {
-                       printf("  Last received shutdown reason: \"%s\"\n",
-                           log_shutcomm(p->stats.last_shutcomm));
-               }
-               if (p->state == STATE_IDLE) {
-                       static const char       *errstr;
-
-                       errstr = get_errstr(p->stats.last_sent_errcode,
-                           p->stats.last_sent_suberr);
-                       if (errstr)
-                               printf("  Last error: %s\n\n", errstr);
-               } else {
-                       printf("  Local host:  %20s, Local port:  %5u\n",
-                           log_addr(&p->local), p->local_port);
+       }
+}
 
-                       printf("  Remote host: %20s, Remote port: %5u\n",
-                           log_addr(&p->remote), p->remote_port);
-                       printf("\n");
-               }
+void
+show_neighbor(struct peer *p, struct parse_result *res)
+{
+       char *s;
+
+       switch (res->action) {
+       case SHOW:
+       case SHOW_SUMMARY:
+               show_summary(p);
                break;
-       case IMSG_CTL_SHOW_TIMER:
-               t = imsg->data;
-               if (t->type > 0 && t->type < Timer_Max)
-                       print_timer(timernames[t->type], t->val);
+       case SHOW_SUMMARY_TERSE:
+               s = fmt_peer(p->conf.descr, &p->conf.remote_addr,
+                   p->conf.remote_masklen);
+               printf("%s %s %s\n", s, log_as(p->conf.remote_as),
+                   p->conf.template ? "Template" : statenames[p->state]);
+               free(s);
                break;
-       case IMSG_CTL_END:
-               return (1);
+       case SHOW_NEIGHBOR:
+       case SHOW_NEIGHBOR_TIMERS:
+               show_neighbor_full(p, res);
+               break;
+       case SHOW_NEIGHBOR_TERSE:
+               s = fmt_peer(NULL, &p->conf.remote_addr,
+                   p->conf.remote_masklen);
+               printf("%llu %llu %llu %llu %llu %llu %llu %llu %llu "
+                   "%llu %u %u %llu %llu %llu %llu %s %s \"%s\"\n",
+                   p->stats.msg_sent_open, p->stats.msg_rcvd_open,
+                   p->stats.msg_sent_notification,
+                   p->stats.msg_rcvd_notification,
+                   p->stats.msg_sent_update, p->stats.msg_rcvd_update,
+                   p->stats.msg_sent_keepalive, p->stats.msg_rcvd_keepalive,
+                   p->stats.msg_sent_rrefresh, p->stats.msg_rcvd_rrefresh,
+                   p->stats.prefix_cnt, p->conf.max_prefix,
+                   p->stats.prefix_sent_update, p->stats.prefix_rcvd_update,
+                   p->stats.prefix_sent_withdraw,
+                   p->stats.prefix_rcvd_withdraw, s,
+                   log_as(p->conf.remote_as), p->conf.descr);
+               free(s);
                break;
        default:
                break;
        }
-
-       return (0);
 }
 
 void
@@ -956,145 +916,97 @@ show_fib_flags(u_int16_t flags)
        printf(" ");
 }
 
-int
-show_fib_msg(struct imsg *imsg)
+void
+show_fib(struct kroute_full *kf)
 {
-       struct kroute_full      *kf;
-       struct ktable           *kt;
        char                    *p;
 
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_KROUTE:
-       case IMSG_CTL_SHOW_NETWORK:
-               if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(*kf))
-                       errx(1, "wrong imsg len");
-               kf = imsg->data;
-
-               show_fib_flags(kf->flags);
+       show_fib_flags(kf->flags);
 
-               if (asprintf(&p, "%s/%u", log_addr(&kf->prefix),
-                   kf->prefixlen) == -1)
-                       err(1, NULL);
-               printf("%4i %-20s ", kf->priority, p);
-               free(p);
-
-               if (kf->flags & F_CONNECTED)
-                       printf("link#%u", kf->ifindex);
-               else
-                       printf("%s", log_addr(&kf->nexthop));
-               printf("\n");
-
-               break;
-       case IMSG_CTL_SHOW_FIB_TABLES:
-               if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(*kt))
-                       errx(1, "wrong imsg len");
-               kt = imsg->data;
-
-               printf("%5i %-20s %-8s%s\n", kt->rtableid, kt->descr,
-                   kt->fib_sync ? "coupled" : "decoupled",
-                   kt->fib_sync != kt->fib_conf ? "*" : "");
+       if (asprintf(&p, "%s/%u", log_addr(&kf->prefix), kf->prefixlen) == -1)
+               err(1, NULL);
+       printf("%4i %-20s ", kf->priority, p);
+       free(p);
 
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
+       if (kf->flags & F_CONNECTED)
+               printf("link#%u", kf->ifindex);
+       else
+               printf("%s", log_addr(&kf->nexthop));
+       printf("\n");
+}
 
-       return (0);
+void
+show_fib_table(struct ktable *kt)
+{
+       printf("%5i %-20s %-8s%s\n", kt->rtableid, kt->descr,
+           kt->fib_sync ? "coupled" : "decoupled",
+           kt->fib_sync != kt->fib_conf ? "*" : "");
 }
 
-int
-show_nexthop_msg(struct imsg *imsg)
+void
+show_nexthop(struct ctl_show_nexthop *nh)
 {
-       struct ctl_show_nexthop *p;
        struct kroute           *k;
        struct kroute6          *k6;
        char                    *s;
 
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_NEXTHOP:
-               p = imsg->data;
-               printf("%s %-15s ", p->valid ? "*" : " ", log_addr(&p->addr));
-               if (!p->krvalid) {
-                       printf("\n");
-                       return (0);
-               }
-               switch (p->addr.aid) {
-               case AID_INET:
-                       k = &p->kr.kr4;
-                       if (asprintf(&s, "%s/%u", inet_ntoa(k->prefix),
-                           k->prefixlen) == -1)
-                               err(1, NULL);
-                       printf("%-20s", s);
-                       free(s);
-                       printf("%3i %-15s ", k->priority,
-                           k->flags & F_CONNECTED ? "connected" :
-                           inet_ntoa(k->nexthop));
-                       break;
-               case AID_INET6:
-                       k6 = &p->kr.kr6;
-                       if (asprintf(&s, "%s/%u", log_in6addr(&k6->prefix),
-                           k6->prefixlen) == -1)
-                               err(1, NULL);
-                       printf("%-20s", s);
-                       free(s);
-                       printf("%3i %-15s ", k6->priority,
-                           k6->flags & F_CONNECTED ? "connected" :
-                           log_in6addr(&k6->nexthop));
-                       break;
-               default:
-                       printf("unknown address family\n");
-                       return (0);
-               }
-               if (p->iface.ifname[0]) {
-                       printf("%s (%s, %s)", p->iface.ifname,
-                           p->iface.is_up ? "UP" : "DOWN",
-                           p->iface.baudrate ?
-                           get_baudrate(p->iface.baudrate, "bps") :
-                           p->iface.linkstate);
-               }
+       printf("%s %-15s ", nh->valid ? "*" : " ", log_addr(&nh->addr));
+       if (!nh->krvalid) {
                printf("\n");
-               break;
-       case IMSG_CTL_END:
-               return (1);
+               return;
+       }
+       switch (nh->addr.aid) {
+       case AID_INET:
+               k = &nh->kr.kr4;
+               if (asprintf(&s, "%s/%u", inet_ntoa(k->prefix),
+                   k->prefixlen) == -1)
+                       err(1, NULL);
+               printf("%-20s", s);
+               free(s);
+               printf("%3i %-15s ", k->priority,
+                   k->flags & F_CONNECTED ? "connected" :
+                   inet_ntoa(k->nexthop));
+               break;
+       case AID_INET6:
+               k6 = &nh->kr.kr6;
+               if (asprintf(&s, "%s/%u", log_in6addr(&k6->prefix),
+                   k6->prefixlen) == -1)
+                       err(1, NULL);
+               printf("%-20s", s);
+               free(s);
+               printf("%3i %-15s ", k6->priority,
+                   k6->flags & F_CONNECTED ? "connected" :
+                   log_in6addr(&k6->nexthop));
                break;
        default:
-               break;
+               printf("unknown address family\n");
+               return;
        }
-
-       return (0);
+       if (nh->iface.ifname[0]) {
+               printf("%s (%s, %s)", nh->iface.ifname,
+                   nh->iface.is_up ? "UP" : "DOWN",
+                   nh->iface.baudrate ?
+                   get_baudrate(nh->iface.baudrate, "bps") :
+                   nh->iface.linkstate);
+       }
+       printf("\n");
 }
 
-int
-show_interface_msg(struct imsg *imsg)
+void
+show_interface(struct ctl_show_interface *iface)
 {
-       struct ctl_show_interface       *iface;
-
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_INTERFACE:
-               iface = imsg->data;
-               printf("%-15s", iface->ifname);
-               printf("%-9u", iface->rdomain);
-               printf("%-9s", iface->nh_reachable ? "ok" : "invalid");
-               printf("%-7s", iface->is_up ? "UP" : "");
-
-               if (iface->media[0])
-                       printf("%s, ", iface->media);
-               printf("%s", iface->linkstate);
-
-               if (iface->baudrate > 0)
-                       printf(", %s", get_baudrate(iface->baudrate, "Bit/s"));
-               printf("\n");
-               break;
-       case IMSG_CTL_END:
-               return (1);
-               break;
-       default:
-               break;
-       }
-
-       return (0);
+       printf("%-15s", iface->ifname);
+       printf("%-9u", iface->rdomain);
+       printf("%-9s", iface->nh_reachable ? "ok" : "invalid");
+       printf("%-7s", iface->is_up ? "UP" : "");
+
+       if (iface->media[0])
+               printf("%s, ", iface->media);
+       printf("%s", iface->linkstate);
+
+       if (iface->baudrate > 0)
+               printf(", %s", get_baudrate(iface->baudrate, "Bit/s"));
+       printf("\n");
 }
 
 void
@@ -1176,66 +1088,14 @@ print_ovs(u_int8_t validation_state, int
        }
 }
 
-int
-show_rib_summary_msg(struct imsg *imsg)
-{
-       struct ctl_show_rib      rib;
-       u_char                  *asdata;
-       size_t                   aslen;
-
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_RIB:
-               memcpy(&rib, imsg->data, sizeof(rib));
-               asdata = imsg->data;
-               asdata += sizeof(rib);
-               aslen = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof(rib);
-               show_rib_brief(&rib, asdata, aslen);
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
-
-       return (0);
-}
-
-int
-show_rib_detail_msg(struct imsg *imsg, int flag0)
+void
+show_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen,
+    struct parse_result *res)
 {
-       struct ctl_show_rib      rib;
-       u_char                  *asdata;
-       size_t                   aslen;
-       u_int16_t                ilen;
-
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_RIB:
-               memcpy(&rib, imsg->data, sizeof(rib));
-               asdata = imsg->data;
-               asdata += sizeof(rib);
-               aslen = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof(rib);
-               show_rib_detail(&rib, asdata, aslen, flag0);
-               break;
-       case IMSG_CTL_SHOW_RIB_COMMUNITIES:
-               ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
-               if (ilen % sizeof(struct community))
-                       errx(1, "bad IMSG_CTL_SHOW_RIB_COMMUNITIES received");
-               show_communities(imsg->data, ilen, flag0);
-               break;
-       case IMSG_CTL_SHOW_RIB_ATTR:
-               ilen = imsg->hdr.len - IMSG_HEADER_SIZE;
-               if (ilen < 3)
-                       errx(1, "bad IMSG_CTL_SHOW_RIB_ATTR received");
-               show_attr(imsg->data, ilen, flag0);
-               break;
-       case IMSG_CTL_END:
-               printf("\n");
-               return (1);
-       default:
-               break;
-       }
-
-       return (0);
+       if (res->flags & F_CTL_DETAIL)
+               show_rib_detail(r, asdata, aslen, res->flags);
+       else
+               show_rib_brief(r, asdata, aslen);
 }
 
 void
@@ -1821,88 +1681,77 @@ fmt_mem(long long num)
        return (buf);
 }
 
-size_t  pt_sizes[AID_MAX] = AID_PTSIZE;
-
-int
-show_rib_memory_msg(struct imsg *imsg)
+void
+show_rib_mem(struct rde_memstats *stats)
 {
-       struct rde_memstats     stats;
-       struct rde_hashstats    hash;
+       static size_t  pt_sizes[AID_MAX] = AID_PTSIZE;
        size_t                  pts = 0;
        int                     i;
-       double                  avg, dev;
 
-       switch (imsg->hdr.type) {
-       case IMSG_CTL_SHOW_RIB_MEM:
-               memcpy(&stats, imsg->data, sizeof(stats));
-               printf("RDE memory statistics\n");
-               for (i = 0; i < AID_MAX; i++) {
-                       if (stats.pt_cnt[i] == 0)
-                               continue;
-                       pts += stats.pt_cnt[i] * pt_sizes[i];
-                       printf("%10lld %s network entries using %s of memory\n",
-                           stats.pt_cnt[i], aid_vals[i].name,
-                           fmt_mem(stats.pt_cnt[i] * pt_sizes[i]));
-               }
-               printf("%10lld rib entries using %s of memory\n",
-                   stats.rib_cnt, fmt_mem(stats.rib_cnt *
-                   sizeof(struct rib_entry)));
-               printf("%10lld prefix entries using %s of memory\n",
-                   stats.prefix_cnt, fmt_mem(stats.prefix_cnt *
-                   sizeof(struct prefix)));
-               printf("%10lld BGP path attribute entries using %s of memory\n",
-                   stats.path_cnt, fmt_mem(stats.path_cnt *
-                   sizeof(struct rde_aspath)));
-               printf("\t   and holding %lld references\n",
-                   stats.path_refs);
-               printf("%10lld BGP AS-PATH attribute entries using "
-                   "%s of memory\n\t   and holding %lld references\n",
-                   stats.aspath_cnt, fmt_mem(stats.aspath_size),
-                   stats.aspath_refs);
-               printf("%10lld entries for %lld BGP communities "
-                   "using %s of memory\n", stats.comm_cnt, stats.comm_nmemb,
-                   fmt_mem(stats.comm_cnt * sizeof(struct rde_community) +
-                   stats.comm_size * sizeof(struct community)));
-               printf("\t   and holding %lld references\n",
-                   stats.comm_refs);
-               printf("%10lld BGP attributes entries using %s of memory\n",
-                   stats.attr_cnt, fmt_mem(stats.attr_cnt *
-                   sizeof(struct attr)));
-               printf("\t   and holding %lld references\n",
-                   stats.attr_refs);
-               printf("%10lld BGP attributes using %s of memory\n",
-                   stats.attr_dcnt, fmt_mem(stats.attr_data));
-               printf("%10lld as-set elements in %lld tables using "
-                   "%s of memory\n", stats.aset_nmemb, stats.aset_cnt,
-                   fmt_mem(stats.aset_size));
-               printf("%10lld prefix-set elements using %s of memory\n",
-                   stats.pset_cnt, fmt_mem(stats.pset_size));
-               printf("RIB using %s of memory\n", fmt_mem(pts +
-                   stats.prefix_cnt * sizeof(struct prefix) +
-                   stats.rib_cnt * sizeof(struct rib_entry) +
-                   stats.path_cnt * sizeof(struct rde_aspath) +
-                   stats.aspath_size + stats.attr_cnt * sizeof(struct attr) +
-                   stats.attr_data));
-               printf("Sets using %s of memory\n", fmt_mem(stats.aset_size +
-                   stats.pset_size));
-               printf("\nRDE hash statistics\n");
-               break;
-       case IMSG_CTL_SHOW_RIB_HASH:
-               memcpy(&hash, imsg->data, sizeof(hash));
-               printf("\t%s: size %lld, %lld entries\n", hash.name, hash.num,
-                   hash.sum);
-               avg = (double)hash.sum / (double)hash.num;
-               dev = sqrt(fmax(0, hash.sumq / hash.num - avg * avg));
-               printf("\t    min %lld max %lld avg/std-dev = %.3f/%.3f\n",
-                   hash.min, hash.max, avg, dev);
-               break;
-       case IMSG_CTL_END:
-               return (1);
-       default:
-               break;
-       }
+       printf("RDE memory statistics\n");
+       for (i = 0; i < AID_MAX; i++) {
+               if (stats->pt_cnt[i] == 0)
+                       continue;
+               pts += stats->pt_cnt[i] * pt_sizes[i];
+               printf("%10lld %s network entries using %s of memory\n",
+                   stats->pt_cnt[i], aid_vals[i].name,
+                   fmt_mem(stats->pt_cnt[i] * pt_sizes[i]));
+       }
+       printf("%10lld rib entries using %s of memory\n",
+           stats->rib_cnt, fmt_mem(stats->rib_cnt *
+           sizeof(struct rib_entry)));
+       printf("%10lld prefix entries using %s of memory\n",
+           stats->prefix_cnt, fmt_mem(stats->prefix_cnt *
+           sizeof(struct prefix)));
+       printf("%10lld BGP path attribute entries using %s of memory\n",
+           stats->path_cnt, fmt_mem(stats->path_cnt *
+           sizeof(struct rde_aspath)));
+       printf("\t   and holding %lld references\n",
+           stats->path_refs);
+       printf("%10lld BGP AS-PATH attribute entries using "
+           "%s of memory\n\t   and holding %lld references\n",
+           stats->aspath_cnt, fmt_mem(stats->aspath_size),
+           stats->aspath_refs);
+       printf("%10lld entries for %lld BGP communities "
+           "using %s of memory\n", stats->comm_cnt, stats->comm_nmemb,
+           fmt_mem(stats->comm_cnt * sizeof(struct rde_community) +
+           stats->comm_size * sizeof(struct community)));
+       printf("\t   and holding %lld references\n",
+           stats->comm_refs);
+       printf("%10lld BGP attributes entries using %s of memory\n",
+           stats->attr_cnt, fmt_mem(stats->attr_cnt *
+           sizeof(struct attr)));
+       printf("\t   and holding %lld references\n",
+           stats->attr_refs);
+       printf("%10lld BGP attributes using %s of memory\n",
+           stats->attr_dcnt, fmt_mem(stats->attr_data));
+       printf("%10lld as-set elements in %lld tables using "
+           "%s of memory\n", stats->aset_nmemb, stats->aset_cnt,
+           fmt_mem(stats->aset_size));
+       printf("%10lld prefix-set elements using %s of memory\n",
+           stats->pset_cnt, fmt_mem(stats->pset_size));
+       printf("RIB using %s of memory\n", fmt_mem(pts +
+           stats->prefix_cnt * sizeof(struct prefix) +
+           stats->rib_cnt * sizeof(struct rib_entry) +
+           stats->path_cnt * sizeof(struct rde_aspath) +
+           stats->aspath_size + stats->attr_cnt * sizeof(struct attr) +
+           stats->attr_data));
+       printf("Sets using %s of memory\n", fmt_mem(stats->aset_size +
+           stats->pset_size));
+       printf("\nRDE hash statistics\n");
+}
 
-       return (0);
+void
+show_rib_hash(struct rde_hashstats *hash)
+{
+       double avg, dev;
+
+       printf("\t%s: size %lld, %lld entries\n", hash->name, hash->num,
+           hash->sum);
+       avg = (double)hash->sum / (double)hash->num;
+       dev = sqrt(fmax(0, hash->sumq / hash->num - avg * avg));
+       printf("\t    min %lld max %lld avg/std-dev = %.3f/%.3f\n",
+           hash->min, hash->max, avg, dev);
 }
 
 void

Reply via email to