From: "ralf_lici (Code Review)" Date: Mon, 16 Feb 2026 13:54:49 +0000 References: Reply-To: ralf@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="jPUnOSgpaeE="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[release/2.6]: Refactor management bytecount tracking List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --jPUnOSgpaeE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1532?usp=3Demail to review the following change. Change subject: Refactor management bytecount tracking ...................................................................... Refactor management bytecount tracking There are few issues with it: - when using DCO, the server part doesn't output BYTECOUNT_CLI since process_incoming_link_part1/process_outgoing_link are not called - when using DCO, the server part applies bytecount timer to the each connection, unneccessary making too many calls to the kernel and also uses incorect BYTECOUNT output. - client part outputs counters using timer, server part utilizes traffic activity -> inconsistency Following changes have been made: - Use timer to output counters in client and server mode. Code which deals with bytecount on traffic activity has been removed. This unifies DCO and non-DCO, as well as client and server mode - In server mode, peers stats are fetched with the single ioctl call - Per-packet stats are not persisted anymore in the client mode during traffic activity. Instead cumulative stats (including DCO stats) are persisted when the session closes. GitHub: https://github.com/OpenVPN/openvpn/issues/820 Change-Id: I4e1e8a204d1371a314b01737aedaa3d68fe28531 Signed-off-by: Lev Stipakov --- M src/openvpn/forward.c M src/openvpn/manage.c M src/openvpn/manage.h M src/openvpn/multi.c 4 files changed, 77 insertions(+), 77 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/32/1532/1 diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 4e9e739..0f49784 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -824,7 +824,7 @@ #ifdef ENABLE_MANAGEMENT if (management) { - management_check_bytecount(c, management, &c->c2.timeval); + management_check_bytecount_client(c, management, &c->c2.timeval); } #endif /* ENABLE_MANAGEMENT */ } @@ -1006,13 +1006,6 @@ } #endif c->c2.original_recv_size =3D c->c2.buf.len; -#ifdef ENABLE_MANAGEMENT - if (management) - { - management_bytes_client(management, c->c2.buf.len, 0); - management_bytes_server(management, &c->c2.link_read_bytes, &c= ->c2.link_write_bytes, &c->c2.mda_context); - } -#endif } else { @@ -1852,13 +1845,6 @@ mmap_stats->link_write_bytes =3D link_write_bytes_glob= al; } #endif -#ifdef ENABLE_MANAGEMENT - if (management) - { - management_bytes_client(management, 0, size); - management_bytes_server(management, &c->c2.link_read_b= ytes, &c->c2.link_write_bytes, &c->c2.mda_context); - } -#endif } } diff --git a/src/openvpn/manage.c b/src/openvpn/manage.c index 6efe2d1..4ed0393 100644 --- a/src/openvpn/manage.c +++ b/src/openvpn/manage.c @@ -512,31 +512,27 @@ } static void -man_bytecount_output_client(struct management *man, - counter_type dco_read_bytes, - counter_type dco_write_bytes) +man_bytecount_output_client(counter_type bytes_in_total, counter_type byte= s_out_total) { char in[32]; char out[32]; /* do in a roundabout way to work around possible mingw or mingw-glibc= bug */ - openvpn_snprintf(in, sizeof(in), counter_format, man->persist.bytes_in= + dco_read_bytes); - openvpn_snprintf(out, sizeof(out), counter_format, man->persist.bytes_= out + dco_write_bytes); + openvpn_snprintf(in, sizeof(in), counter_format, bytes_in_total); + openvpn_snprintf(out, sizeof(out), counter_format, bytes_out_total); msg(M_CLIENT, ">BYTECOUNT:%s,%s", in, out); } -void -man_bytecount_output_server(const counter_type *bytes_in_total, - const counter_type *bytes_out_total, +static void +man_bytecount_output_server(const counter_type bytes_in_total, const count= er_type bytes_out_total, struct man_def_auth_context *mdac) { char in[32]; char out[32]; /* do in a roundabout way to work around possible mingw or mingw-glibc= bug */ - openvpn_snprintf(in, sizeof(in), counter_format, *bytes_in_total); - openvpn_snprintf(out, sizeof(out), counter_format, *bytes_out_total); + openvpn_snprintf(in, sizeof(in), counter_format, bytes_in_total); + openvpn_snprintf(out, sizeof(out), counter_format, bytes_out_total); msg(M_CLIENT, ">BYTECOUNT_CLI:%lu,%s,%s", mdac->cid, in, out); - mdac->bytecount_last_update =3D now; } static void @@ -4160,37 +4156,82 @@ } void -management_check_bytecount(struct context *c, struct management *man, stru= ct timeval *timeval) +management_check_bytecount_client(struct context *c, struct management *ma= n, struct timeval *timeval) { - if (event_timeout_trigger(&man->connection.bytecount_update_interval, - timeval, ETT_DEFAULT)) + if (man->persist.callback.flags & MCF_SERVER) { - counter_type dco_read_bytes =3D 0; - counter_type dco_write_bytes =3D 0; + return; + } - if (dco_enabled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0)) + if (event_timeout_trigger(&man->connection.bytecount_update_interval, = timeval, ETT_DEFAULT)) + { + if (dco_enabled(&c->options)) { - dco_read_bytes =3D c->c2.dco_read_bytes; - dco_write_bytes =3D c->c2.dco_write_bytes; + if (dco_get_peer_stats(c) < 0) + { + return; + } } - if (!(man->persist.callback.flags & MCF_SERVER)) - { - man_bytecount_output_client(man, dco_read_bytes, dco_write_byt= es); - } + man_bytecount_output_client(c->c2.dco_read_bytes + man->persist.by= tes_in + c->c2.link_read_bytes, + c->c2.dco_write_bytes + man->persist.b= ytes_out + c->c2.link_write_bytes); } } -/* DCO resets stats on reconnect. Since client expects stats - * to be preserved across reconnects, we need to save DCO +void +management_check_bytecount_server(struct multi_context *multi) +{ + if (!(management->persist.callback.flags & MCF_SERVER)) + { + return; + } + + struct timeval null; + CLEAR(null); + if (event_timeout_trigger(&management->connection.bytecount_update_int= erval, &null, ETT_DEFAULT)) + { + /* fetch counters from dco */ + if (dco_enabled(&multi->top.options)) + { + if (dco_get_peer_stats_multi(&multi->top.c1.tuntap->dco, multi= ) < 0) + { + return; + } + } + + /* iterate over peers and report counters for each connected peer = */ + struct hash_iterator hi; + struct hash_element *he; + hash_iterator_init(multi->hash, &hi); + while ((he =3D hash_iterator_next(&hi))) + { + struct multi_instance *mi =3D (struct multi_instance *)he->val= ue; + struct context_2 *c2 =3D &mi->context.c2; + + if ((c2->mda_context.flags & (DAF_CONNECTION_ESTABLISHED | DAF= _CONNECTION_CLOSED)) =3D=3D DAF_CONNECTION_ESTABLISHED) + { + man_bytecount_output_server(c2->dco_read_bytes + c2->link_= read_bytes, c2->dco_write_bytes + c2->link_write_bytes, &c2->mda_context); + } + } + hash_iterator_free(&hi); + } +} + +/* context_2 stats are reset on reconnect. Since client expects stats + * to be preserved across reconnects, we need to save context_2 * stats before tearing the tunnel down. */ void man_persist_client_stats(struct management *man, struct context *c) { + man->persist.bytes_in +=3D c->c2.link_read_bytes; + man->persist.bytes_out +=3D c->c2.link_write_bytes; + + /* no need to raise SIGUSR1 since we are already closing the instance = */ if (dco_enabled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0)) { - management_bytes_client(man, c->c2.dco_read_bytes, c->c2.dco_write= _bytes); + man->persist.bytes_in +=3D c->c2.dco_read_bytes; + man->persist.bytes_out +=3D c->c2.dco_write_bytes; } } diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h index 1896510..d3fe84f 100644 --- a/src/openvpn/manage.h +++ b/src/openvpn/manage.h @@ -70,8 +70,6 @@ unsigned int flags; unsigned int mda_key_id_counter; - - time_t bytecount_last_update; }; /* @@ -516,41 +514,9 @@ * These functions drive the bytecount in/out counters. */ -void -management_check_bytecount(struct context *c, - struct management *man, - struct timeval *timeval); +void management_check_bytecount_client(struct context *c, struct managemen= t *man, struct timeval *timeval); -static inline void -management_bytes_client(struct management *man, - const int size_in, - const int size_out) -{ - if (!(man->persist.callback.flags & MCF_SERVER)) - { - man->persist.bytes_in +=3D size_in; - man->persist.bytes_out +=3D size_out; - } -} - -void -man_bytecount_output_server(const counter_type *bytes_in_total, - const counter_type *bytes_out_total, - struct man_def_auth_context *mdac); - -static inline void -management_bytes_server(struct management *man, - const counter_type *bytes_in_total, - const counter_type *bytes_out_total, - struct man_def_auth_context *mdac) -{ - if (man->connection.bytecount_update_seconds > 0 - && now >=3D mdac->bytecount_last_update + man->connection.bytecoun= t_update_seconds - && (mdac->flags & (DAF_CONNECTION_ESTABLISHED | DAF_CONNECTION_CLO= SED)) =3D=3D DAF_CONNECTION_ESTABLISHED) - { - man_bytecount_output_server(bytes_in_total, bytes_out_total, mdac); - } -} +void management_check_bytecount_server(struct multi_context *multi); void man_persist_client_stats(struct management *man, struct context *c); diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 69497a6..8654aa5 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -3840,6 +3840,13 @@ { check_stale_routes(m); } + +#ifdef ENABLE_MANAGEMENT + if (management) + { + management_check_bytecount_server(m); + } +#endif /* ENABLE_MANAGEMENT */ } void -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1532?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newchange Gerrit-Project: openvpn Gerrit-Branch: release/2.6 Gerrit-Change-Id: I4e1e8a204d1371a314b01737aedaa3d68fe28531 Gerrit-Change-Number: 1532 Gerrit-PatchSet: 1 Gerrit-Owner: ralf_lici Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos --jPUnOSgpaeE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: plaisthos.

ralf_lici would like plaisthos to review this change.

V= iew Change

Refactor management bytecount tracking
<= br>There are few issues with it:

- when using DCO, the server part = doesn't output BYTECOUNT_CLI
since process_incoming_link_part1/proc= ess_outgoing_link are not called

- when using DCO, the server part = applies bytecount timer to the each
connection, unneccessary making too= many calls to the kernel and also
uses incorect BYTECOUNT output.
<= br> - client part outputs counters using timer, server part utilizes
tr= affic activity -> inconsistency

Following changes have been made:=

- Use timer to output counters in client and server mode. Code whi= ch
deals with bytecount on traffic activity has been removed. This unif= ies
DCO and non-DCO, as well as client and server mode

- In ser= ver mode, peers stats are fetched with the single ioctl call

- Per-= packet stats are not persisted anymore in the client mode during
traf= fic activity. Instead cumulative stats (including DCO stats) are
pers= isted when the session closes.

GitHub: https://github.com/OpenVPN/op= envpn/issues/820

Change-Id: I4e1e8a204d1371a314b01737aedaa3d68fe2853= 1
Signed-off-by: Lev Stipakov <lev@...515...>
---
M src/open= vpn/forward.c
M src/openvpn/manage.c
M src/openvpn/manage.h
M src/= openvpn/multi.c
4 files changed, 77 insertions(+), 77 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/=
changes/32/1532/1
diff --git a/src/openvpn/forward.c b/src/openvpn/=
forward.c
index 4e9e739..0f49784 100644
---= a/src/openvpn/forward.c
+++ b/src/openvpn/forward.c=
@@ -824,7 +824,7 @@
#ifdef ENABLE_MANAGEMENT
if (management)
{
- management_check_bytecount(c, ma= nagement, &c->c2.timeval);
+ management_check_bytecount_client(c, management, &= ;c->c2.timeval);
}
#endif /* ENABL= E_MANAGEMENT */
}
@@ -1006,13 +1006,6 @@
}
#endif
= c->c2.original_recv_size =3D c->c2.buf.len;
-#ifdef ENABLE_MANAGEMENT
- if (management)
- {
- management_bytes_client(management, c->= ;c2.buf.len, 0);
- = management_bytes_server(management, &c->c2.link_read_bytes, &= amp;c->c2.link_write_bytes, &c->c2.mda_context);
- }
-#endif
}
= else
{
@@ -1852,13 +1845,6 @@<= br> mmap_stats->link_write_bytes =3D link_writ= e_bytes_global;
}
#endif<= /span>
-#ifdef ENABLE_MANAGEME= NT
- if = (management)
- = {
- = management_bytes_client(management, 0, size);
- management_bytes_server= (management, &c->c2.link_read_bytes, &c->c2.link_write_bytes,= &c->c2.mda_context);
- }
-ma= n_bytecount_output_client(struct management *man,
- counter_type dco_re= ad_bytes,
- = counter_type dco_write_bytes)
+man_bytecount_output_client(counter_type bytes_in= _total, counter_type bytes_out_total)
{
= char in[32];
char out[32];
=
/* do in a roundabout way to work around possible mingw or m= ingw-glibc bug */
- = openvpn_snprintf(in, sizeof(in), counter_format, man->persist.bytes_in += dco_read_bytes);
- = openvpn_snprintf(out, sizeof(out), counter_format, man->persist.bytes_ou= t + dco_write_bytes);
+ openvpn_snprintf(in, sizeof(in), counter_format, bytes_in_total);
+ openvpn_snprintf(ou= t, sizeof(out), counter_format, bytes_out_total);
msg(= M_CLIENT, ">BYTECOUNT:%s,%s", in, out);
}

-void
-man_bytecount_output_ser= ver(const counter_type *bytes_in_total,
- const counter_type *bytes_out= _total,
+static void<= /span>
+man_bytecount_output= _server(const counter_type bytes_in_total, const counter_type bytes_out_tot= al,
struct man_def_auth_contex= t *mdac)
{
char in[32];
char out[32];

/* do in a roundabout way to wor= k around possible mingw or mingw-glibc bug */
- openvpn_snprintf(in, sizeof(in), counter_format= , *bytes_in_total);
- = openvpn_snprintf(out, sizeof(out), counter_format, *bytes_out_total);
+ openvpn_snprintf(in= , sizeof(in), counter_format, bytes_in_total);
+ openvpn_snprintf(out, sizeof(out), counter_f= ormat, bytes_out_total);
msg(M_CLIENT, ">BYTEC= OUNT_CLI:%lu,%s,%s", mdac->cid, in, out);
- mdac->bytecount_last_update =3D now;
}

static void
@@ -4160,37 +4156,82 @@

}

= void
-management= _check_bytecount(struct context *c, struct management *man, struct timeval = *timeval)
+management= _check_bytecount_client(struct context *c, struct management *man, struct t= imeval *timeval)
{
- if (event_timeout_trigger(&man->connection.bytecou= nt_update_interval,
- = timeval, ETT_DEFAULT))
+ if (man->persist.callback.flags &= MCF_SERVER)
{
- counter_type dco_read_bytes =3D 0;
- counter_type dco_write_bytes =3D= 0;
+ return;<= /span>
+ }

- if (dco_en= abled(&c->options) && (dco_get_peer_stats(c) =3D=3D 0))
+ if (event_timeout_tr= igger(&man->connection.bytecount_update_interval, timeval, ETT_DEFAU= LT))
+ {+ if (dco_enabled(&= c->options))
{
- dco_read_bytes =3D c->c2.dco_read_bytes= ;
- dco_writ= e_bytes =3D c->c2.dco_write_bytes;
+ if (dco_get_peer_stats(c) < 0)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ return;
+ }

= }

- if (!(man->persist.callback.flags & MCF_SERVER))<= br>- {
- man_bytecount_output_client(m= an, dco_read_bytes, dco_write_bytes);
- }
+ man_bytecount_output_client(c->c2.dco_read_bytes + man->= ;persist.bytes_in + c->c2.link_read_bytes,
+ c->c2.dco_= write_bytes + man->persist.bytes_out + c->c2.link_write_bytes);
}
}

-/* DCO resets stats on reconnect. Since cl= ient expects stats
- * = to be preserved across reconnects, we need to save DCO
+void
+management_check_bytecount_server(struct multi_context = *multi)
+{
= + if (!(management->persi= st.callback.flags & MCF_SERVER))
+ {
+ = }
+
+ struct timeval null;
+ CLEAR(null);
+ if (event_timeout_trigger(&a= mp;management->connection.bytecount_update_interval, &null, ETT_DEFA= ULT))

+ {+ /* fetch counters fr= om dco */
+ if= (dco_enabled(&multi->top.options))
+ {
+ if (dco_get_peer_stats_multi(&multi->top.c1= .tuntap->dco, multi) < 0)
+ {
+ return;
+ }
+ }
+
+ /* iterate over p= eers and report counters for each connected peer */
+ struct hash_iterator hi;+ struct hash_element = *he;
+ hash_it= erator_init(multi->hash, &hi);
+ while ((he =3D hash_iterator_next(&hi)))
+ {
+ struct multi_instance = *mi =3D (struct multi_instance *)he->value;

+ struct context_2 *c2 =3D &mi->= ;context.c2;
+=
+ if ((c2->md= a_context.flags & (DAF_CONNECTION_ESTABLISHED | DAF_CONNECTION_CLOSED))= =3D=3D DAF_CONNECTION_ESTABLISHED)
+ {
+ man_bytecount_output_server(c2->dco_read_bytes= + c2->link_read_bytes, c2->dco_write_bytes + c2->link_write_bytes= , &c2->mda_context);
+ }
+ }
+ = hash_iterator_free(&hi);
+ }
+}
+
+/* context_2 stats are reset on reconnect= . Since client expects stats
+ * to be preserved across reconnects, we need to save context_2
* stats before tearing the tunnel down.
*= /
void
man_persist_client_stats(struct ma= nagement *man, struct context *c)
{
+ man->persist.bytes_in +=3D c->c= 2.link_read_bytes;
+ = man->persist.bytes_out +=3D c->c2.link_write_bytes;
+

+ /* no need to raise SIGUSR1 since we are already = closing the instance */
if (dco_enabled(&c->opt= ions) && (dco_get_peer_stats(c) =3D=3D 0))
{
- management_byte= s_client(man, c->c2.dco_read_bytes, c->c2.dco_write_bytes);+ man->persist.bytes= _in +=3D c->c2.dco_read_bytes;
+ man->persist.bytes_out +=3D c->c2.dco_write_byt= es;
}
}

<= span>diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h

index 1896510..d3fe84f 100644

--- a/src/openvpn/manage.h=
+++ b/src/openvpn/manage.h
@@ -70,8 +70,6 = @@
unsigned int flags;

unsigned int mda_key_id_counter;

-
- = time_t bytecount_last_update;
};

/*
@@ -516,41 +514,9 @@
* The= se functions drive the bytecount in/out counters.
*/

-void
-management_check_bytecoun= t(struct context *c,
- = struct management *man,
- struct timeval *time= val);
+void managemen= t_check_bytecount_client(struct context *c, struct management *man, struct = timeval *timeval);

-static inline void
-management_bytes_client(struct management *man,
- const int siz= e_in,

- = const int size_out)
-{
- if (!(man-= >persist.callback.flags & MCF_SERVER))
- {
- man->persist.bytes_in +=3D size_in;
- man->persist.bytes_out +=3D siz= e_out;
- }-}
-
-man_bytecount_= output_server(const counter_type *bytes_in_total,
- const counter_type = *bytes_out_total,
- = struct man_def_auth_context *mdac);
-

-static inline void
-management_bytes_server(struct management *man,
- const count= er_type *bytes_in_total,

- struct ma= n_def_auth_context *mdac)
-{
- if (man->= connection.bytecount_update_seconds > 0
- && now >=3D mdac->bytecount_last= _update + man->connection.bytecount_update_seconds
- && (mdac->flags & (D= AF_CONNECTION_ESTABLISHED | DAF_CONNECTION_CLOSED)) =3D=3D DAF_CONNECTION_E= STABLISHED)
- {
- man_bytecount_outp= ut_server(bytes_in_total, bytes_out_total, mdac);
- }
-}
+void m= anagement_check_bytecount_server(struct multi_context *multi);

void
man_persist_client_stats(struc= t management *man, struct context *c);
diff --git a/src/ope= nvpn/multi.c b/src/openvpn/multi.c
index 69497a6..8654aa5 1= 00644
--- a/src/openvpn/multi.c
+++ b/src/o= penvpn/multi.c
@@ -3840,6 +3840,13 @@
= {
check_stale_routes(m);
}
+
+#ifdef ENABLE_MANAGEMENT
+ if (management)
+ {
+ management_check_bytecount_server(m);
+ }
+#endif /* ENABLE_MANAGEMENT */<= br> }

void
=

To view, visit change 1532. To unsubscribe, or for help writing ma= il filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: release/2.6
Gerrit-Change-Id: I4e1e8a204d1371a314b01737aed= aa3d68fe28531
Gerrit-Change-Number: 1532
Gerrit-PatchSet: 1
Gerrit-Owner: ralf_lici <ralf@...2726...>= ;
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
= --jPUnOSgpaeE=--