ubus: add detailed peer statistics
authorFelix Fietkau <nbd@nbd.name>
Wed, 29 Jan 2025 20:18:38 +0000 (21:18 +0100)
committerFelix Fietkau <nbd@nbd.name>
Wed, 29 Jan 2025 20:18:40 +0000 (21:18 +0100)
Add idle time, tx/rx bytes, time since last handshake

Signed-off-by: Felix Fietkau <nbd@nbd.name>
host.h
ubus.c
wg-linux.c
wg-user.c
wg.c
wg.h

diff --git a/host.h b/host.h
index 536c24d80d095103a4a903971c11c594287eec4b..a63a60b26415a3418aa56572cace4f3b95592154 100644 (file)
--- a/host.h
+++ b/host.h
@@ -38,11 +38,12 @@ struct network_peer {
                union network_endpoint next_endpoint[__ENDPOINT_TYPE_MAX];
                uint64_t last_ep_update;
 
-               uint64_t rx_bytes;
+               uint64_t rx_bytes, tx_bytes;
                uint64_t last_handshake;
                uint64_t last_request;
                uint64_t last_query_sent;
 
+               int last_handshake_diff;
                int idle;
                int num_net_queries;
        } state;
diff --git a/ubus.c b/ubus.c
index 11bacc2dd08c2810059c5d5440e600c6d006f29d..011beaa6eddd2ef1c41be6734d26ca91fb68d2f5 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -82,6 +82,11 @@ __network_dump(struct blob_buf *buf, struct network *net)
                        snprintf(str + len, INET6_ADDRSTRLEN + 7 - len, ":%d",
                                 ntohs(ep->in.sin_port));
                        blobmsg_add_string_buffer(buf);
+
+                       blobmsg_add_u64(buf, "rx_bytes", peer->state.rx_bytes);
+                       blobmsg_add_u64(buf, "tx_bytes", peer->state.tx_bytes);
+                       blobmsg_add_u32(buf, "idle", peer->state.idle);
+                       blobmsg_add_u32(buf, "last_handshake_sec", peer->state.last_handshake_diff);
                }
 
                blobmsg_close_table(buf, p);
index 0a17c90bac7a79fc28335cba4391ce4fa69f511c..12f5c589e0da5cfb9f3f2238375592971429c444 100644 (file)
@@ -273,6 +273,9 @@ wg_linux_parse_peer(struct network *net, struct nlattr *data, time_t now)
        if ((cur = tb[WGPEER_A_RX_BYTES]) != NULL)
                wg_peer_set_rx_bytes(net, peer, nla_get_u64(cur));
 
+       if ((cur = tb[WGPEER_A_TX_BYTES]) != NULL)
+               wg_peer_set_tx_bytes(net, peer, nla_get_u64(cur));
+
        if ((cur = tb[WGPEER_A_ENDPOINT]) != NULL)
                wg_peer_set_endpoint(net, peer, nla_data(cur), nla_len(cur));
 
index bf057e51131b00b6dbab9951e9b044be92e2c4d1..e90b715d0646f7189862e7e1e3734a8ba0e3ada2 100644 (file)
--- a/wg-user.c
+++ b/wg-user.c
@@ -390,6 +390,13 @@ wg_user_peer_refresh(struct network *net)
                        continue;
                }
 
+               if (!strcmp(req.key, "tx_bytes")) {
+                       uint64_t bytes = strtoull(req.value, NULL, 0);
+
+                       wg_peer_set_tx_bytes(net, peer, bytes);
+                       continue;
+               }
+
                if (!strcmp(req.key, "endpoint")) {
                        struct addrinfo *resolved;
                        struct addrinfo hints = {
diff --git a/wg.c b/wg.c
index 1f80e00826c354e4558a60e8dbf970134d158bde..74e8aae76b56fe014007128b76b326c8f48536ed 100644 (file)
--- a/wg.c
+++ b/wg.c
@@ -69,6 +69,7 @@ void wg_peer_update_done(struct network *net, struct network_peer *peer)
 void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer,
                                uint64_t now, uint64_t sec)
 {
+       peer->state.last_handshake_diff = now - sec;
        if (sec == peer->state.last_handshake)
                return;
 
@@ -95,6 +96,12 @@ void wg_peer_set_rx_bytes(struct network *net, struct network_peer *peer,
        }
 }
 
+void wg_peer_set_tx_bytes(struct network *net, struct network_peer *peer,
+                         uint64_t bytes)
+{
+       peer->state.tx_bytes = bytes;
+}
+
 void wg_peer_set_endpoint(struct network *net, struct network_peer *peer,
                          void *data, size_t len)
 {
diff --git a/wg.h b/wg.h
index 2060eaf2259cf9fe6fd6fee1be44d8959b00cde1..835dae5266c6cf00efaa0c7ad247e1eecb48b67a 100644 (file)
--- a/wg.h
+++ b/wg.h
@@ -55,6 +55,8 @@ void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer,
                                uint64_t now, uint64_t sec);
 void wg_peer_set_rx_bytes(struct network *net, struct network_peer *peer,
                          uint64_t bytes);
+void wg_peer_set_tx_bytes(struct network *net, struct network_peer *peer,
+                         uint64_t bytes);
 void wg_peer_set_endpoint(struct network *net, struct network_peer *peer,
                          void *data, size_t len);