From: "cron2 (Code Review)" Date: Tue, 2 Sep 2025 14:56:49 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., lstipakov@...277... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="qPYgArFjaD0="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dco-win: add support for multipeer stats List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: stipa Cc: plaisthos , flichtenheld , openvpn-devel --qPYgArFjaD0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/11= 43?usp=3Demail ) Change subject: dco-win: add support for multipeer stats ...................................................................... dco-win: add support for multipeer stats Use the new driver API to fetch per-peer link and VPN byte counters in both client and server modes. Two usage modes are supported: - Single peer: pass the peer ID and a fixed-size output buffer. If the IOCTL is not supported (old driver), fall back to the legacy API. - All peers: first call the IOCTL with a small output buffer to get the required size, then allocate a buffer and call again to fetch stats for all peers. Change-Id: I525d7300e49f9a5a18e7146ee35ccc2af8184b8a Signed-off-by: Lev Stipakov Acked-by: Gert Doering Message-Id: <20250902122542.31023-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 744.html Signed-off-by: Gert Doering --- M src/openvpn/dco_win.c M src/openvpn/dco_win.h M src/openvpn/ovpn_dco_win.h 3 files changed, 184 insertions(+), 3 deletions(-) diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index 5317ac1..01ba017 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -30,6 +30,7 @@ #include "forward.h" #include "tun.h" #include "crypto.h" +#include "multi.h" #include "ssl_common.h" #include "openvpn.h" @@ -190,6 +191,8 @@ { dco_context_t *dco =3D &c->c1.tuntap->dco; + dco->c =3D c; + switch (c->mode) { case MODE_POINT_TO_POINT: @@ -714,12 +717,132 @@ int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_e= rr) { - /* Not implemented. */ - return 0; + struct gc_arena gc =3D gc_new(); + + int ret =3D 0; + struct tuntap *tt =3D dco->tt; + + if (!tuntap_defined(tt)) + { + ret =3D -1; + goto done; + } + + OVPN_GET_PEER_STATS ps =3D { + .PeerId =3D -1 + }; + + DWORD required_size =3D 0, bytes_returned =3D 0; + /* first, figure out buffer size */ + if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(= ps), &required_size, sizeof(DWORD), &bytes_returned, NULL)) + { + if (GetLastError() =3D=3D ERROR_MORE_DATA) + { + if (bytes_returned !=3D sizeof(DWORD)) + { + msg(M_WARN, "%s: invalid bytes returned for size query (%l= u, expected %zu)", __func__, bytes_returned, sizeof(DWORD)); + ret =3D -1; + goto done; + } + /* required_size now contains the size written by the driver */ + if (required_size =3D=3D 0) + { + ret =3D 0; /* no peers to process */ + goto done; + } + if (required_size < sizeof(OVPN_PEER_STATS)) + { + msg(M_WARN, "%s: invalid required size %lu (minimum %zu)",= __func__, required_size, sizeof(OVPN_PEER_STATS)); + ret =3D -1; + goto done; + } + } + else + { + msg(M_WARN | M_ERRNO, "%s: failed to fetch required buffer siz= e", __func__); + ret =3D -1; + goto done; + } + } + else + { + /* unexpected success? */ + if (bytes_returned =3D=3D 0) + { + ret =3D 0; /* no peers to process */ + goto done; + } + + msg(M_WARN, "%s: first DeviceIoControl call succeeded unexpectedly= (%lu bytes returned)", __func__, bytes_returned); + ret =3D -1; + goto done; + } + + + /* allocate the buffer and fetch stats */ + OVPN_PEER_STATS *peer_stats =3D gc_malloc(required_size, true, &gc); + if (!peer_stats) + { + msg(M_WARN, "%s: failed to allocate buffer of size %lu", __func__,= required_size); + ret =3D -1; + goto done; + } + + if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(= ps), peer_stats, required_size, &bytes_returned, NULL)) + { + /* unlikely case when a peer has been added since fetching buffer = size, not an error! */ + if (GetLastError() =3D=3D ERROR_MORE_DATA) + { + msg(M_WARN, "%s: peer has been added, skip fetching stats", __= func__); + ret =3D 0; + goto done; + } + + msg(M_WARN | M_ERRNO, "%s: failed to fetch multipeer stats", __fun= c__); + ret =3D -1; + goto done; + } + + /* iterate over stats and update peers */ + for (int i =3D 0; i < bytes_returned / sizeof(OVPN_PEER_STATS); ++i) + { + OVPN_PEER_STATS *stat =3D &peer_stats[i]; + + if (stat->PeerId >=3D dco->c->multi->max_clients) + { + msg(M_WARN, "%s: received out of bound peer_id %u (max=3D%u)",= __func__, stat->PeerId, + dco->c->multi->max_clients); + continue; + } + + struct multi_instance *mi =3D dco->c->multi->instances[stat->PeerI= d]; + if (!mi) + { + msg(M_WARN, "%s: received data for a non-existing peer %u", __= func__, stat->PeerId); + continue; + } + + /* update peer stats */ + struct context_2 *c2 =3D &mi->context.c2; + c2->dco_read_bytes =3D stat->LinkRxBytes; + c2->dco_write_bytes =3D stat->LinkTxBytes; + c2->tun_read_bytes =3D stat->VpnRxBytes; + c2->tun_write_bytes =3D stat->VpnTxBytes; + } + +done: + gc_free(&gc); + + if (raise_sigusr1_on_err && ret < 0) + { + register_signal(dco->c->sig, SIGUSR1, "dco peer stats error"); + } + + return ret; } int -dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) +dco_get_peer_stats_fallback(struct context *c, const bool raise_sigusr1_on= _err) { struct tuntap *tt =3D c->c1.tuntap; @@ -747,6 +870,48 @@ return 0; } +int +dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err) +{ + struct tuntap *tt =3D c->c1.tuntap; + + if (!tuntap_defined(tt)) + { + return -1; + } + + /* first, try a new ioctl */ + OVPN_GET_PEER_STATS ps =3D { .PeerId =3D c->c2.tls_multi->dco_peer_id = }; + + OVPN_PEER_STATS peer_stats =3D { 0 }; + DWORD bytes_returned =3D 0; + if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(= ps), &peer_stats, sizeof(peer_stats), + &bytes_returned, NULL)) + { + if (GetLastError() =3D=3D ERROR_INVALID_FUNCTION) + { + /* are we using the old driver? */ + return dco_get_peer_stats_fallback(c, raise_sigusr1_on_err); + } + + msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STA= TS) failed", __func__); + return -1; + } + + if (bytes_returned !=3D sizeof(OVPN_PEER_STATS)) + { + msg(M_WARN | M_ERRNO, "%s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STA= TS) returned invalid size", __func__); + return -1; + } + + c->c2.dco_read_bytes =3D peer_stats.LinkRxBytes; + c->c2.dco_write_bytes =3D peer_stats.LinkTxBytes; + c->c2.tun_read_bytes =3D peer_stats.VpnRxBytes; + c->c2.tun_write_bytes =3D peer_stats.VpnTxBytes; + + return 0; +} + void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) { diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h index a7f4865..4f3f028 100644 --- a/src/openvpn/dco_win.h +++ b/src/openvpn/dco_win.h @@ -57,6 +57,8 @@ uint64_t dco_read_bytes; uint64_t dco_write_bytes; + + struct context *c; }; typedef struct dco_context dco_context_t; diff --git a/src/openvpn/ovpn_dco_win.h b/src/openvpn/ovpn_dco_win.h index baf7214..9e1378a 100644 --- a/src/openvpn/ovpn_dco_win.h +++ b/src/openvpn/ovpn_dco_win.h @@ -83,6 +83,14 @@ LONG64 TunBytesReceived; } OVPN_STATS, * POVPN_STATS; +typedef struct _OVPN_PEER_STATS { + int PeerId; + LONG64 LinkRxBytes; + LONG64 LinkTxBytes; + LONG64 VpnRxBytes; + LONG64 VpnTxBytes; +} OVPN_PEER_STATS, * POVPN_PEER_STATS; + typedef enum _OVPN_KEY_SLOT { OVPN_KEY_SLOT_PRIMARY, OVPN_KEY_SLOT_SECONDARY @@ -185,6 +193,10 @@ int IPv6; } OVPN_MP_IROUTE, * POVPN_MP_IROUTE; +typedef struct _OVPN_GET_PEER_STATS { + int PeerId; // -1 for all peers stats +} OVPN_GET_PEER_STATS, * POVPN_GET_PEER_STATS; + #define OVPN_IOCTL_NEW_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_BU= FFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_GET_STATS CTL_CODE(FILE_DEVICE_UNKNOWN, 2, METHOD_BU= FFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_NEW_KEY CTL_CODE(FILE_DEVICE_UNKNOWN, 3, METHOD_BU= FFERED, FILE_ANY_ACCESS) @@ -207,3 +219,5 @@ #define OVPN_IOCTL_MP_ADD_IROUTE CTL_CODE(FILE_DEVICE_UNKNOWN, 17, METHOD_= BUFFERED, FILE_ANY_ACCESS) #define OVPN_IOCTL_MP_DEL_IROUTE CTL_CODE(FILE_DEVICE_UNKNOWN, 18, METHOD_= BUFFERED, FILE_ANY_ACCESS) + +#define OVPN_IOCTL_GET_PEER_STATS CTL_CODE(FILE_DEVICE_UNKNOWN, 19, METHOD= _BUFFERED, FILE_ANY_ACCESS) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1143?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I525d7300e49f9a5a18e7146ee35ccc2af8184b8a Gerrit-Change-Number: 1143 Gerrit-PatchSet: 3 Gerrit-Owner: stipa Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --qPYgArFjaD0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

dco-win: add support for multipeer stats
<= br>Use the new driver API to fetch per-peer link and VPN byte counters
i= n both client and server modes.

Two usage modes are supported:
- Single peer: pass the peer ID and a fixed-size output buffer. If the IOCTL is not supported (old driver), fall back to the legacy API.
<= br> - All peers: first call the IOCTL with a small output buffer to get
= the required size, then allocate a buffer and call again to fetch
= stats for all peers.

Change-Id: I525d7300e49f9a5a18e7146ee35ccc2af81= 84b8a
Signed-off-by: Lev Stipakov <lev@...515...>
Acked-by: Ger= t Doering <gert@...1296...>
Message-Id: <20250902122542.31023-1= -gert@...1296...>
URL: https://www.mail-archive.com/openvpn-devel@...1484...= ts.sourceforge.net/msg32744.html
Signed-off-by: Gert Doering <gert@...1186...= .1296...>
---
M src/openvpn/dco_win.c
M src/openvpn/dco_win.hM src/openvpn/ovpn_dco_win.h
3 files changed, 184 insertions(+), 3 del= etions(-)

diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c
index 5317ac1..01ba017 100644
--- a/src/openvpn/dco_win.= c
+++ b/src/openvpn/dco_win.c
@@ -30,6 +30,= 7 @@
#include "forward.h"
#incl= ude "tun.h"
#include "crypto.h"=
+#include "multi.h&quo= t;
#include "ssl_common.h"
#inc= lude "openvpn.h"

@@ -190,6 +191= ,8 @@
{
dco_context_t *dco =3D &c= ->c1.tuntap->dco;

+ dco->c =3D c;
+
switch (c->mode)
= {
case MODE_POINT_TO_POINT:
<= span>@@ -714,12 +717,132 @@
int
dco_get_p= eer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)=
{
- /* No= t implemented. */
- = return 0;
+ struct= gc_arena gc =3D gc_new();
+
+ int ret = =3D 0;
+ struct tu= ntap *tt =3D dco->tt;
+
+ if (!tunta= p_defined(tt))
+ {=
+ ret =3D -1;=
+ goto done;<= /span>
+ }
+

+ OVPN_GET_PEER_STATS ps =3D {
+ .PeerId =3D -1
+ };
+
+ DWORD required_size =3D 0, bytes_returned =3D 0;
+ /* first, figure out buffer size= */
+ if (!DeviceI= oControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &= required_size, sizeof(DWORD), &bytes_returned, NULL))
+ {
+ if (GetLastError() =3D=3D ERROR_MORE_DATA)<= /span>
+ {
= + if (bytes_returned= !=3D sizeof(DWORD))
= + {
+ = msg(M_WARN, "%s: invalid bytes returned for size query (%lu= , expected %zu)", __func__, bytes_returned, sizeof(DWORD));
= + ret =3D -1;
+ goto d= one;
+ }
+ /* requir= ed_size now contains the size written by the driver */
+ if (required_size =3D=3D 0)<= /span>
+ {=
+ ret =3D 0;= /* no peers to process */
+ goto done;
+ }
+ if (required_size < sizeof(OVPN_PEER_STATS))<= br>+ {
+ msg(M_WARN, "= ;%s: invalid required size %lu (minimum %zu)", __func__, required_size= , sizeof(OVPN_PEER_STATS));

+ ret =3D -1;
+ goto done;
+ }
+ }
+ else
+= {
+ = msg(M_WARN | M_ERRNO, "%s: failed to fetch required buffer size"= ;, __func__);
+ = ret =3D -1;
+ = goto done;
= + }
+ }

+ else
+ {
+ /* unexpected success? */
+ if (bytes_returned =3D=3D = 0)
+ {<= br>+ ret =3D 0; /* n= o peers to process */
+ goto done;
+ }
+

+ msg(M_WARN, &quo= t;%s: first DeviceIoControl call succeeded unexpectedly (%lu bytes returned= )", __func__, bytes_returned);
+ ret =3D -1;
+ goto done;
+ }
= +
+
+ /* allocate the buffer and fetch = stats */
+ OVPN_PE= ER_STATS *peer_stats =3D gc_malloc(required_size, true, &gc);+ if (!peer_stats)+ {
+ msg(M_WARN, "%s: failed to a= llocate buffer of size %lu", __func__, required_size);
+ ret =3D -1;
+ goto done;
+ }
+
+ if (!DeviceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &= ;ps, sizeof(ps), peer_stats, required_size, &bytes_returned, NULL))

+ {
+ /* unlikely case when a peer = has been added since fetching buffer size, not an error! */
+ if (GetLastError() =3D=3D E= RROR_MORE_DATA)
+ = {
+ ms= g(M_WARN, "%s: peer has been added, skip fetching stats", __func_= _);
+ ret = =3D 0;
+ g= oto done;
+ }<= /span>
+
+ msg(M_WARN | M_ERRNO, "%s:= failed to fetch multipeer stats", __func__);
+ ret =3D -1;
+ goto done;
+ }
+
+ = /* iterate over stats and update peers */
+ for (int i =3D 0; i < bytes_returned / sizeof= (OVPN_PEER_STATS); ++i)
+ {
+ OV= PN_PEER_STATS *stat =3D &peer_stats[i];
+
+ if (stat->PeerId >=3D dco->c->multi->max_client= s)
+ {<= br>+ msg(M_WARN, &qu= ot;%s: received out of bound peer_id %u (max=3D%u)", __func__, stat-&g= t;PeerId,
+ = dco->c->multi->max_clients);
+ continue;
+ }
+
+ = struct multi_instance *mi =3D dco->c->multi->instances[stat-&= gt;PeerId];
+ = if (!mi)
+ {
+ msg(M_WAR= N, "%s: received data for a non-existing peer %u", __func__, stat= ->PeerId);
+ = continue;
+ = }
+
+ /* update peer stats */
+ struct contex= t_2 *c2 =3D &mi->context.c2;
+ c2->dco_read_bytes =3D stat->LinkRxBytes;

+ c2->dco_writ= e_bytes =3D stat->LinkTxBytes;
+ c2->tun_read_bytes =3D stat->VpnRxBytes;=
+ c2->tun_write_b= ytes =3D stat->VpnTxBytes;
+ }
+
+done:
+ gc_free(&gc);
+

+ if (raise_sigusr1_on_err && ret < 0)
+ {
+ register_signal(dco->c-&g= t;sig, SIGUSR1, "dco peer stats error");
+ }
+
+ = return ret;
}

int
-dco_get_peer_stats(struct= context *c, const bool raise_sigusr1_on_err)
+dco_get_peer_stats_fallback(struct context *c, co= nst bool raise_sigusr1_on_err)
{
stru= ct tuntap *tt =3D c->c1.tuntap;

@@ -74= 7,6 +870,48 @@
return 0;
}
=
+int+dco_get_peer_stats(struct co= ntext *c, const bool raise_sigusr1_on_err)
+{
+ struct tuntap *tt =3D c->c1.tuntap;
+
+ if (!tuntap_defined(tt))
+ {
+ return -1;
+
= + /* first, try a new ioctl = */
+ OVPN_GET_PEER= _STATS ps =3D { .PeerId =3D c->c2.tls_multi->dco_peer_id };+
+ OVPN_PEER_STATS peer_stats =3D { 0 };=
+ DWORD bytes_returned = =3D 0;
+ if (!Devi= ceIoControl(tt->hand, OVPN_IOCTL_GET_PEER_STATS, &ps, sizeof(ps), &a= mp;peer_stats, sizeof(peer_stats),
+ &bytes_returned, NULL))<= br>+ {
+ if (GetLastError() =3D=3D ERROR_I= NVALID_FUNCTION)
+ = {
+ /= * are we using the old driver? */
+ return dco_get_peer_stats_fallback(c, raise_sigus= r1_on_err);
+ = }
+
+ msg(M_WARN | M_ERRNO, "%= s: DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) failed", __func__);
+ return -1;

+ }
+
+ if (bytes_returned !=3D sizeof(OVPN_PEER_STATS))
+ {
+ msg(M_WARN | M_ERRNO, "%s:= DeviceIoControl(OVPN_IOCTL_GET_PEER_STATS) returned invalid size", __= func__);
+ ret= urn -1;
+ }=
+
+ c->c2.dco_read_bytes =3D peer_stats.Li= nkRxBytes;
+ c->= ;c2.dco_write_bytes =3D peer_stats.LinkTxBytes;
+ c->c2.tun_read_bytes =3D peer_stats.VpnR= xBytes;
+ c->c2= .tun_write_bytes =3D peer_stats.VpnTxBytes;
+
+ return 0;
+}=
+
v= oid
dco_event_set(dco_context_t *dco, struct event_set *es= , void *arg)
{
diff --git a/src/openvpn/dc= o_win.h b/src/openvpn/dco_win.h
index a7f4865..4f3f028 1006= 44
--- a/src/openvpn/dco_win.h
+++ b/src/op= envpn/dco_win.h
@@ -57,6 +57,8 @@
<= br> uint64_t dco_read_bytes;
uint64_t dco_wr= ite_bytes;
++ struct context *c;
};

typedef struct dco_contex= t dco_context_t;
diff --git a/src/openvpn/ovpn_dco_win.h b/= src/openvpn/ovpn_dco_win.h
index baf7214..9e1378a 100644
--- a/src/openvpn/ovpn_dco_win.h
+++ b/src/op= envpn/ovpn_dco_win.h
@@ -83,6 +83,14 @@
L= ONG64 TunBytesReceived;
} OVPN_STATS, * POVPN_STATS;

+typedef= struct _OVPN_PEER_STATS {
+ int PeerId;
+ LONG64 LinkTxBytes;
+ LONG64 VpnRxBytes;
+ LONG64 VpnTxBytes;
+} OVPN_PEER_STATS, * POVPN_PEER_STATS;
+
typedef enum _OVPN_KE= Y_SLOT {
OVPN_KEY_SLOT_PRIMARY,
OVPN_KE= Y_SLOT_SECONDARY
@@ -185,6 +193,10 @@
= int IPv6;
} OVPN_MP_IROUTE, * POVPN_MP_IROUTE;
<= span>

+typedef struc= t _OVPN_GET_PEER_STATS {
+ int PeerId; // -1 for all peers stats
+} OVPN_GET_PEER_STATS, * POVPN_GET_PEER_STATS;
+
#de= fine OVPN_IOCTL_NEW_PEER CTL_CODE(FILE_DEVICE_UNKNOWN, 1, METHOD_BUFFER= ED, FILE_ANY_ACCESS)
#define OVPN_IOCTL_GET_STATS CTL_C= ODE(FILE_DEVICE_UNKNOWN, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define OVPN_IOCTL_NEW_KEY CTL_CODE(FILE_DEVICE_UNKNOWN, 3, METHOD= _BUFFERED, FILE_ANY_ACCESS)

@@ -207,3 +219,5 @@
<= span>

#define OVPN_IOCTL_MP_ADD_IROUTE CTL_CODE(FILE_DEVI= CE_UNKNOWN, 17, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define = OVPN_IOCTL_MP_DEL_IROUTE CTL_CODE(FILE_DEVICE_UNKNOWN, 18, METHOD_BUFFERED,= FILE_ANY_ACCESS)
+
+#define OVPN_IOCTL_GE= T_PEER_STATS CTL_CODE(FILE_DEVICE_UNKNOWN, 19, METHOD_BUFFERED, FILE_ANY_AC= CESS)

To view, visit change 1143. To unsubs= cribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I525d7300e49f9a5a18e7146ee35= ccc2af8184b8a
Gerrit-Change-Number: 1143
Gerrit-PatchSet: 3
Gerrit-Owner: stipa <lstipakov@...277...>= ;
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--qPYgArFjaD0=--