diff --git a/core/lb_proto_tcp.c b/core/lb_proto_tcp.c index 555c2f7..0b61f28 100644 --- a/core/lb_proto_tcp.c +++ b/core/lb_proto_tcp.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -718,3 +719,66 @@ tcp_conn_dump_cmd_cb(int fd, __attribute__((unused)) char *argv[], UNIXCTL_CMD_REGISTER("tcp/conn/dump", "", "Dump TCP connections.", 0, 0, tcp_conn_dump_cmd_cb); + +static void +tcp_conn_stats_normal(int fd) { + uint32_t lcore_id; + struct lb_conn_table *ct; + + unixctl_command_reply(fd, " "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + unixctl_command_reply(fd, "lcore%-5u ", lcore_id); + } + unixctl_command_reply(fd, "\n"); + + unixctl_command_reply(fd, "avail_conns "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + unixctl_command_reply(fd, "%-10u ", rte_mempool_avail_count(ct->mp)); + } + unixctl_command_reply(fd, "\n"); + + unixctl_command_reply(fd, "inuse_conns "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + unixctl_command_reply(fd, "%-10u ", rte_mempool_in_use_count(ct->mp)); + } + unixctl_command_reply(fd, "\n"); +} + +static void +tcp_conn_stats_json(int fd) { + uint32_t lcore_id; + struct lb_conn_table *ct; + uint8_t json_first_obj = 1; + + unixctl_command_reply(fd, "["); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + if (json_first_obj) { + json_first_obj = 0; + unixctl_command_reply(fd, "{"); + } else { + unixctl_command_reply(fd, ",{"); + } + unixctl_command_reply(fd, JSON_KV_32_FMT("lcore", ","), lcore_id); + unixctl_command_reply(fd, JSON_KV_32_FMT("avail_conns", ","), + rte_mempool_avail_count(ct->mp)); + unixctl_command_reply(fd, JSON_KV_32_FMT("inuse_conns", ""), + rte_mempool_in_use_count(ct->mp)); + unixctl_command_reply(fd, "}"); + } + unixctl_command_reply(fd, "]\n"); +} + +static void +tcp_conn_stats_cmd_cb(int fd, char *argv[], int argc) { + if (argc > 0 && strcmp(argv[0], "--json") == 0) + tcp_conn_stats_json(fd); + else + tcp_conn_stats_normal(fd); +} + +UNIXCTL_CMD_REGISTER("tcp/conn/stats", "[--json].", + "Show the number of TCP connections.", 0, 1, + tcp_conn_stats_cmd_cb); \ No newline at end of file diff --git a/core/lb_proto_udp.c b/core/lb_proto_udp.c index 24744fd..8e0f802 100644 --- a/core/lb_proto_udp.c +++ b/core/lb_proto_udp.c @@ -1,6 +1,7 @@ /* Copyright (c) 2018. TIG developer. */ #include +#include #include #include @@ -220,3 +221,66 @@ udp_conn_dump_cmd_cb(int fd, __attribute__((unused)) char *argv[], UNIXCTL_CMD_REGISTER("udp/conn/dump", "", "Dump UDP connections.", 0, 0, udp_conn_dump_cmd_cb); + +static void +udp_conn_stats_normal(int fd) { + uint32_t lcore_id; + struct lb_conn_table *ct; + + unixctl_command_reply(fd, " "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + unixctl_command_reply(fd, "lcore%-5u ", lcore_id); + } + unixctl_command_reply(fd, "\n"); + + unixctl_command_reply(fd, "avail_conns "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + unixctl_command_reply(fd, "%-10u ", rte_mempool_avail_count(ct->mp)); + } + unixctl_command_reply(fd, "\n"); + + unixctl_command_reply(fd, "inuse_conns "); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + unixctl_command_reply(fd, "%-10u ", rte_mempool_in_use_count(ct->mp)); + } + unixctl_command_reply(fd, "\n"); +} + +static void +udp_conn_stats_json(int fd) { + uint32_t lcore_id; + struct lb_conn_table *ct; + uint8_t json_first_obj = 1; + + unixctl_command_reply(fd, "["); + RTE_LCORE_FOREACH_SLAVE(lcore_id) { + ct = &lb_conn_tbls[lcore_id]; + if (json_first_obj) { + json_first_obj = 0; + unixctl_command_reply(fd, "{"); + } else { + unixctl_command_reply(fd, ",{"); + } + unixctl_command_reply(fd, JSON_KV_32_FMT("lcore", ","), lcore_id); + unixctl_command_reply(fd, JSON_KV_32_FMT("avail_conns", ","), + rte_mempool_avail_count(ct->mp)); + unixctl_command_reply(fd, JSON_KV_32_FMT("inuse_conns", ""), + rte_mempool_in_use_count(ct->mp)); + unixctl_command_reply(fd, "}"); + } + unixctl_command_reply(fd, "]\n"); +} + +static void +udp_conn_stats_cmd_cb(int fd, char *argv[], int argc) { + if (argc > 0 && strcmp(argv[0], "--json") == 0) + udp_conn_stats_json(fd); + else + udp_conn_stats_normal(fd); +} + +UNIXCTL_CMD_REGISTER("udp/conn/stats", "[--json].", + "Show the number of UDP connections.", 0, 1, + udp_conn_stats_cmd_cb); \ No newline at end of file