Add cli show group and update rate calculation
[l2tpns.git] / cli.c
diff --git a/cli.c b/cli.c
index 09ea768..c39eac0 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -21,6 +21,7 @@
 #include <dlfcn.h>
 #include <netdb.h>
 #include <libcli.h>
 #include <dlfcn.h>
 #include <netdb.h>
 #include <libcli.h>
+#include <linux/rtnetlink.h>
 
 #include "l2tpns.h"
 #include "constants.h"
 
 #include "l2tpns.h"
 #include "constants.h"
@@ -77,6 +78,7 @@ static char *debug_levels[] = {
 #endif
 
 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
 #endif
 
 static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int argc);
+static int cmd_show_group(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_users(struct cli_def *cli, char *command, char **argv, int argc);
 static int cmd_show_radius(struct cli_def *cli, char *command, char **argv, int argc);
@@ -160,6 +162,7 @@ void init_cli()
        cli_register_command(cli, c, "remotelns-conf", cmd_show_rmtlnsconf, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show a list of remote LNS configuration");
 #endif
        cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
        cli_register_command(cli, c, "remotelns-conf", cmd_show_rmtlnsconf, PRIVILEGE_PRIVILEGED, MODE_EXEC, "Show a list of remote LNS configuration");
 #endif
        cli_register_command(cli, c, "session", cmd_show_session, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of sessions or details for a single session");
+       cli_register_command(cli, c, "group", cmd_show_group, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of groups or details for a single group");
        cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
        cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
        cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
        cli_register_command(cli, c, "tbf", cmd_show_tbf, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all token bucket filters in use");
        cli_register_command(cli, c, "throttle", cmd_show_throttle, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "List all throttled sessions and associated TBFs");
        cli_register_command(cli, c, "tunnels", cmd_show_tunnels, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, "Show a list of tunnels or details for a single tunnel");
@@ -564,9 +567,9 @@ static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int
                        "idle",
                        "Rem.Time",
 #ifdef LAC
                        "idle",
                        "Rem.Time",
 #ifdef LAC
-                       "LAC(L)/R LNS(R)",
+                       "LAC(L)/RLNS(R)/PPPOE(P)",
 #else
 #else
-                       "LAC",
+                       "LAC(L)/PPPOE(P)",
 #endif
                        "CLI");
 
 #endif
                        "CLI");
 
@@ -579,9 +582,9 @@ static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int
                else
                        rem_time = session[i].timeout ? (session[i].timeout - (time_now-session[i].opened)) : 0;
 #ifdef LAC
                else
                        rem_time = session[i].timeout ? (session[i].timeout - (time_now-session[i].opened)) : 0;
 #ifdef LAC
-               cli_print(cli, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-15s %s",
+               cli_print(cli, "%5d %7d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
 #else
 #else
-               cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %-15s %s",
+               cli_print(cli, "%5d %4d %-32s %-15s %s %s %s %s %10u %10lu %10lu %4u %10lu %3s%-20s %s",
 #endif
                                i,
 #ifdef LAC
 #endif
                                i,
 #ifdef LAC
@@ -600,9 +603,11 @@ static int cmd_show_session(struct cli_def *cli, char *command, char **argv, int
                                abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
                                (unsigned long)(rem_time),
 #ifdef LAC
                                abs(time_now - (session[i].last_packet ? session[i].last_packet : time_now)),
                                (unsigned long)(rem_time),
 #ifdef LAC
-                               tunnel[session[i].tunnel].isremotelns?"(R)":"(L)",
+                               (session[i].tunnel == TUNNEL_ID_PPPOE)?"(P)":(tunnel[session[i].tunnel].isremotelns?"(R)":"(L)"),
+#else
+                               (session[i].tunnel == TUNNEL_ID_PPPOE)?"(P)":"(L)",
 #endif
 #endif
-                               fmtaddr(htonl(tunnel[ session[i].tunnel ].ip), 1),
+                               (session[i].tunnel == TUNNEL_ID_PPPOE)?fmtMacAddr(session[i].src_hwaddr):fmtaddr(htonl(tunnel[session[i].tunnel].ip), 1),
                                session[i].calling[0] ? session[i].calling : "*");
        }
        return CLI_OK;
                                session[i].calling[0] ? session[i].calling : "*");
        }
        return CLI_OK;
@@ -686,18 +691,16 @@ static int cmd_show_tunnels(struct cli_def *cli, char *command, char **argv, int
                if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
 
                for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
                if (!show_all && (!tunnel[i].ip || tunnel[i].die)) continue;
 
                for (x = 0; x < MAXSESSION; x++) if (session[x].tunnel == i && session[x].opened && !session[x].die) sessions++;
-#ifdef LAC
                cli_print(cli, "%4d %20s %20s %6s %6d %s",
                cli_print(cli, "%4d %20s %20s %6s %6d %s",
-#else
-               cli_print(cli, "%4d %20s %20s %6s %6d",
-#endif
                                i,
                                *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
                                fmtaddr(htonl(tunnel[i].ip), 0),
                                states[tunnel[i].state],
                                sessions
 #ifdef LAC
                                i,
                                *tunnel[i].hostname ? tunnel[i].hostname : "(null)",
                                fmtaddr(htonl(tunnel[i].ip), 0),
                                states[tunnel[i].state],
                                sessions
 #ifdef LAC
-                               ,(tunnel[i].isremotelns?"Tunnel To Remote LNS":"Tunnel To LAC")
+                               ,(i == TUNNEL_ID_PPPOE)?"Tunnel pppoe":(tunnel[i].isremotelns?"Tunnel To Remote LNS":"Tunnel To LAC")
+#else
+                               ,(i == TUNNEL_ID_PPPOE)?"Tunnel pppoe":"Tunnel To LAC"
 #endif
                                );
        }
 #endif
                                );
        }
@@ -3106,7 +3109,7 @@ static int cmd_show_access_list(struct cli_def *cli, char *command, char **argv,
                }
 
                if (i)
                }
 
                if (i)
-                       cli_print(cli, "");
+                       cli_print(cli, " ");
 
                cli_print(cli, "%s IP access list %s",
                        ip_filters[f].extended ? "Extended" : "Standard",
 
                cli_print(cli, "%s IP access list %s",
                        ip_filters[f].extended ? "Extended" : "Standard",
@@ -3144,6 +3147,77 @@ static int cmd_reload(struct cli_def *cli, char *command, char **argv, int argc)
        return CLI_OK;
 }
 
        return CLI_OK;
 }
 
+static int cmd_show_group(struct cli_def *cli, char *command, char **argv, int argc)
+{
+       int i;
+       groupidt g;
+
+       if (CLI_HELP_REQUESTED)
+               return cli_arg_help(cli, 1,
+                       "<1-%d>", MAXGROUPE-1, "Show specific group by id",
+                       NULL);
+
+       time(&time_now);
+       if (argc > 0)
+       {
+               // Show individual group
+               for (i = 0; i < argc; i++)
+               {
+                       sessionidt s;
+
+                       g = atoi(argv[i]);
+                       if (g <= 0 || g >= MAXGROUPE)
+                       {
+                               cli_print(cli, "Invalid group id \"%s\"", argv[i]);
+                               continue;
+                       }
+
+                       cli_print(cli, "\r\nGroup %d:", g);
+                       cli_print(cli, "\tNb Session:\t\t%d", grpsession[g].nbsession);
+                       cli_print(cli, "\tNb Routes:\t\t%d", grpsession[g].nbroutesgrp);
+                       cli_print(cli, "\ttime_changed:\t\t%d\n", grpsession[g].time_changed);
+
+                       for (i = 0; i < grpsession[g].nbsession; i++)
+                       {
+                               if ((s = grpsession[g].sesslist[i].sid))
+                               {
+                                       cli_print(cli, "\tSession:\t%d\tTx Rate:%d Kbps\t\t\tweight:\t%d",
+                                                               s,
+                                                               grpsession[g].sesslist[i].tx_rate/(1024/8),
+                                                               grpsession[g].sesslist[i].weight);
+                               }
+                       }
+
+                       for (i = 0; i < grpsession[g].nbroutesgrp; i++)
+                       {
+                               if (grpsession[g].route[i].ip != 0)
+                               {
+                                       cli_print(cli, "\tRoute:\t%s/%d", fmtaddr(htonl(grpsession[g].route[i].ip), 0), grpsession[g].route[i].prefixlen);
+                               }
+                       }
+               }
+               return CLI_OK;
+       }
+
+       // Show Summary
+       cli_print(cli, "%5s %7s %9s %12s",
+                       "GID",
+                       "Nb Sess",
+                       "Nb Routes",
+                       "Time changed");
+
+       for (g = gnextgrpid; g != 0; g = grpsession[g].prev)
+       {
+               cli_print(cli, "%5d %7d %9d %12d",
+                                       g,
+                                       grpsession[g].nbsession,
+                                       grpsession[g].nbroutesgrp,
+                                       grpsession[g].time_changed);
+       }
+
+       return CLI_OK;
+}
+
 #ifdef LAC
 
 static int cmd_setforward(struct cli_def *cli, char *command, char **argv, int argc)
 #ifdef LAC
 
 static int cmd_setforward(struct cli_def *cli, char *command, char **argv, int argc)