Skip to content

Commit 6d11a9a

Browse files
committed
net: wireguard: stats: Add statistics support
Collect Wireguard VPN statistics and allow user to fetch it. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 40683e0 commit 6d11a9a

File tree

6 files changed

+315
-1
lines changed

6 files changed

+315
-1
lines changed

include/zephyr/net/net_stats.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ enum net_request_stats_cmd {
724724
NET_REQUEST_STATS_CMD_GET_PM,
725725
NET_REQUEST_STATS_CMD_GET_WIFI,
726726
NET_REQUEST_STATS_CMD_RESET_WIFI,
727+
NET_REQUEST_STATS_CMD_GET_VPN,
727728
};
728729

729730
/** @endcond */
@@ -853,6 +854,16 @@ NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PPP);
853854
/** @endcond */
854855
#endif /* CONFIG_NET_STATISTICS_PPP */
855856

857+
#if defined(CONFIG_NET_STATISTICS_VPN)
858+
/** Request VPN statistics */
859+
#define NET_REQUEST_STATS_GET_VPN \
860+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_VPN)
861+
862+
/** @cond INTERNAL_HIDDEN */
863+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_VPN);
864+
/** @endcond */
865+
#endif /* CONFIG_NET_STATISTICS_VPN */
866+
856867
#endif /* CONFIG_NET_STATISTICS_USER_API */
857868

858869
#if defined(CONFIG_NET_STATISTICS_POWER_MANAGEMENT)

include/zephyr/net/wireguard.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,52 @@ struct wireguard_peer_config {
8989
uint16_t keepalive_interval;
9090
};
9191

92+
/** Wireguard VPN statistics */
93+
struct net_stats_vpn {
94+
/** Number of keepalive packets received */
95+
uint32_t keepalive_rx;
96+
/** Number of keepalive packets sent */
97+
uint32_t keepalive_tx;
98+
/** Number of peer not found errors */
99+
uint32_t peer_not_found;
100+
/** Number of key expired errors */
101+
uint32_t key_expired;
102+
/** Number of invalid packets */
103+
uint32_t invalid_packet;
104+
/** Number of invalid key errors */
105+
uint32_t invalid_key;
106+
/** Number of invalid MIC errors */
107+
uint32_t invalid_mic;
108+
/** Number of invalid packet length errors */
109+
uint32_t invalid_packet_len;
110+
/** Number of invalid keepalive errors */
111+
uint32_t invalid_keepalive;
112+
/** Number of invalid handshake errors */
113+
uint32_t invalid_handshake;
114+
/** Number of invalid cookie errors */
115+
uint32_t invalid_cookie;
116+
/** Number of invalid MAC1 errors */
117+
uint32_t invalid_mac1;
118+
/** Number of invalid MAC2 errors */
119+
uint32_t invalid_mac2;
120+
/** Number of decrypt failed errors */
121+
uint32_t decrypt_failed;
122+
/** Number of dropped RX packets */
123+
uint32_t drop_rx;
124+
/** Number of allocation failures */
125+
uint32_t alloc_failed;
126+
/** Number of invalid IP version */
127+
uint32_t invalid_ip_version;
128+
/** Number of invalid IP address family */
129+
uint32_t invalid_ip_family;
130+
/** Number of denied IP address */
131+
uint32_t denied_ip;
132+
/** Number of replay errors */
133+
uint32_t replay_error;
134+
/** Number of valid packets received */
135+
uint32_t valid_rx;
136+
};
137+
92138
/**
93139
* @brief Add a Wireguard peer to the system.
94140
*

subsys/net/ip/Kconfig.stats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,11 @@ config NET_STATISTICS_VIA_PROMETHEUS
162162
Enable this option to expose the network statistics
163163
to Prometheus monitoring system.
164164

165+
config NET_STATISTICS_VPN
166+
bool "Wireguard VPN statistics"
167+
depends on WIREGUARD
168+
default y
169+
help
170+
Keep track of Wireguard VPN related statistics
171+
165172
endif # NET_STATISTICS

subsys/net/lib/wireguard/wg.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ struct wg_iface_context {
9191
struct wg_context *wg_ctx;
9292
struct wg_peer *peer;
9393

94+
#if defined(CONFIG_NET_STATISTICS_VPN)
95+
struct net_stats_vpn stats;
96+
#endif /* CONFIG_NET_STATISTICS_VPN */
97+
9498
uint8_t public_key[WG_PUBLIC_KEY_LEN];
9599
uint8_t private_key[WG_PRIVATE_KEY_LEN];
96100

@@ -105,6 +109,8 @@ struct wg_iface_context {
105109
bool init_done : 1;
106110
};
107111

112+
#include "wg_stats.h"
113+
108114
static int create_packet(struct net_if *iface,
109115
struct sockaddr *src,
110116
struct sockaddr *dst,
@@ -282,6 +288,10 @@ static int wg_send_keepalive(struct wg_iface_context *ctx,
282288
net_if_get_by_iface(ctx->iface),
283289
ret);
284290

291+
if (ret == 0) {
292+
vpn_stats_update_keepalive_tx(ctx);
293+
}
294+
285295
return ret;
286296
}
287297

@@ -307,7 +317,7 @@ static void wg_periodic_timer(struct k_work *work)
307317
}
308318

309319
if (should_send_keepalive(peer)) {
310-
wg_send_keepalive(peer->ctx, peer);
320+
(void)wg_send_keepalive(peer->ctx, peer);
311321
}
312322

313323
if (should_send_init(peer)) {
@@ -1894,3 +1904,62 @@ void wireguard_peer_foreach(wg_peer_cb_t cb, void *user_data)
18941904

18951905
k_mutex_unlock(&lock);
18961906
}
1907+
1908+
#if defined(CONFIG_NET_STATISTICS_VPN) && defined(CONFIG_NET_STATISTICS_USER_API)
1909+
1910+
static int wg_stats_get(uint32_t mgmt_request, struct net_if *iface,
1911+
void *data, size_t len)
1912+
{
1913+
size_t len_chk = 0;
1914+
struct net_stats_vpn *src = NULL;
1915+
struct wg_peer *peer, *next;
1916+
int ret = -ENOENT;
1917+
1918+
switch (NET_MGMT_GET_COMMAND(mgmt_request)) {
1919+
case NET_REQUEST_STATS_CMD_GET_VPN:
1920+
if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
1921+
return -ENOENT;
1922+
}
1923+
1924+
if (net_virtual_get_iface_capabilities(iface) != VIRTUAL_INTERFACE_VPN) {
1925+
return -ENOENT;
1926+
}
1927+
1928+
len_chk = sizeof(struct net_stats_vpn);
1929+
1930+
k_mutex_lock(&lock, K_FOREVER);
1931+
1932+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&active_peers, peer, next, node) {
1933+
if (peer->iface != iface) {
1934+
continue;
1935+
}
1936+
1937+
src = &peer->ctx->stats;
1938+
ret = 0;
1939+
break;
1940+
}
1941+
1942+
k_mutex_unlock(&lock);
1943+
break;
1944+
1945+
default:
1946+
ret = -ENOTSUP;
1947+
break;
1948+
}
1949+
1950+
if (ret < 0) {
1951+
return ret;
1952+
}
1953+
1954+
if (len != len_chk || src == NULL) {
1955+
return -EINVAL;
1956+
}
1957+
1958+
memcpy(data, src, len);
1959+
1960+
return 0;
1961+
}
1962+
1963+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_VPN, wg_stats_get);
1964+
1965+
#endif /* CONFIG_NET_STATISTICS_USER_API && CONFIG_NET_STATISTICS_VPN */

0 commit comments

Comments
 (0)