From: "stipa (Code Review)" Date: Thu, 20 Feb 2025 08:06:12 +0000 In-Reply-To: References: Reply-To: lstipakov@...277..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <96f6b673a2b00e19ac711ee8ff8fe227d46be831-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="MQAb4y9fxDI="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dco-win: kernel notifications List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , cron2 , flichtenheld Cc: openvpn-devel --MQAb4y9fxDI= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos, stipa. Hello cron2, flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/816?usp=3Demail to look at the new patch set (#21). The following approvals got outdated and were removed: Code-Review+1 by cron2 Change subject: dco-win: kernel notifications ...................................................................... dco-win: kernel notifications The driver supports notifications mechanism, which is used to notify userspace about various events, such as peer keepalive timeout, key expire and so on. This uses existing framework of subscribing and receiving dco notifications, used by FreeBSD and Linux implementations. On Windows we use overlapped IO, which state we keep in DCO context. We create an event, which is associated with overlapped operation, and inject it into openvpn event loop. When event is signalled, we read overlapped result into DCO context, which is later used by already existing code which handles peer deletion. Change-Id: Iedc10616225f6769c66d3c29d4a462b622ebbc6e Signed-off-by: Lev Stipakov --- M src/openvpn/dco_win.c M src/openvpn/dco_win.h M src/openvpn/forward.c M src/openvpn/mudp.c M src/openvpn/multi.c 5 files changed, 137 insertions(+), 9 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/16/816/21 diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index a3c7b63..f46c93d 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -126,6 +126,17 @@ ASSERT(dco->ifmode =3D=3D DCO_MODE_UNINIT); dco->ifmode =3D DCO_MODE_MP; + /* Use manual reset event so it remains signalled until + * explicitly reset. This way we won't lose notifications + */ + dco->ov.hEvent =3D CreateEvent(NULL, TRUE, FALSE, NULL); + if (dco->ov.hEvent =3D=3D NULL) + { + msg(M_ERR, "Error: ovpn_dco_init: CreateEvent failed"); + } + + dco->rwhandle.read =3D dco->ov.hEvent; + /* open DCO device */ struct gc_arena gc =3D gc_new(); const char *device_guid; @@ -629,11 +640,74 @@ } } +/** + * @brief Handles successful completion of overlapped operation. + * + * We use overlapped I/O (Windows term for asynchronous I/O) to get + * notifications from kernel to userspace. This gets the result of overlap= ped + * operation and, in case of success, copies data from kernel-filled buffer + * into userspace-provided dco context. + * + * @param dco Pointer to the dco context + * @param queued true if operation was queued, false if it has completed i= mmediately + */ +static void +dco_handle_overlapped_success(dco_context_t *dco, bool queued) +{ + DWORD bytes_read =3D 0; + BOOL res =3D GetOverlappedResult(dco->tt->hand, &dco->ov, &bytes_read,= FALSE); + if (res) + { + msg(D_DCO_DEBUG, "%s: completion%s success [%ld]", __func__, queue= d ? "" : " non-queued", bytes_read); + + dco->dco_message_peer_id =3D dco->notif_buf.PeerId; + dco->dco_message_type =3D dco->notif_buf.Cmd; + dco->dco_del_peer_reason =3D dco->notif_buf.DelPeerReason; + } + else + { + msg(D_DCO_DEBUG | M_ERRNO, "%s: completion%s error", __func__, que= ued ? "" : " non-queued"); + } +} + int dco_do_read(dco_context_t *dco) { - /* no-op on windows */ - ASSERT(0); + if (dco->ifmode !=3D DCO_MODE_MP) + { + ASSERT(false); + } + + dco->dco_message_peer_id =3D -1; + dco->dco_message_type =3D 0; + + switch (dco->iostate) + { + case IOSTATE_QUEUED: + dco_handle_overlapped_success(dco, true); + + ASSERT(ResetEvent(dco->ov.hEvent)); + dco->iostate =3D IOSTATE_INITIAL; + + break; + + case IOSTATE_IMMEDIATE_RETURN: + dco->iostate =3D IOSTATE_INITIAL; + ASSERT(ResetEvent(dco->ov.hEvent)); + + if (dco->ov_ret =3D=3D ERROR_SUCCESS) + { + dco_handle_overlapped_success(dco, false); + } + else + { + SetLastError(dco->ov_ret); + msg(D_DCO_DEBUG | M_ERRNO, "%s: completion non-queued erro= r", __func__); + } + + break; + } + return 0; } @@ -676,8 +750,48 @@ void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg) { - /* no-op on windows */ - ASSERT(0); + if (dco->ifmode !=3D DCO_MODE_MP) + { + /* mp only */ + return; + } + + event_ctl(es, &dco->rwhandle, EVENT_READ, arg); + + if (dco->iostate =3D=3D IOSTATE_INITIAL) + { + /* the overlapped IOCTL will signal this event on I/O completion */ + ASSERT(ResetEvent(dco->ov.hEvent)); + + if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_NOTIFY_EVENT, NULL,= 0, &dco->notif_buf, sizeof(dco->notif_buf), NULL, &dco->ov)) + { + DWORD err =3D GetLastError(); + if (err =3D=3D ERROR_IO_PENDING) /* operation queued? */ + { + dco->iostate =3D IOSTATE_QUEUED; + dco->ov_ret =3D ERROR_SUCCESS; + + msg(D_DCO_DEBUG, "%s: notify ioctl queued", __func__); + } + else + { + /* error occured */ + ASSERT(SetEvent(dco->ov.hEvent)); + dco->iostate =3D IOSTATE_IMMEDIATE_RETURN; + dco->ov_ret =3D err; + + msg(D_DCO_DEBUG | M_ERRNO, "%s: notify ioctl error", __fun= c__); + } + } + else + { + ASSERT(SetEvent(dco->ov.hEvent)); + dco->iostate =3D IOSTATE_IMMEDIATE_RETURN; + dco->ov_ret =3D ERROR_SUCCESS; + + msg(D_DCO_DEBUG, "%s: notify ioctl immediate return", __func__= ); + } + } } const char * diff --git a/src/openvpn/dco_win.h b/src/openvpn/dco_win.h index dc7fbc1..309badf 100644 --- a/src/openvpn/dco_win.h +++ b/src/openvpn/dco_win.h @@ -41,6 +41,18 @@ struct tuntap *tt; dco_mode_type ifmode; + OVPN_NOTIFY_EVENT notif_buf; /**< Buffer for incoming notifications. */ + OVERLAPPED ov; /**< Used by overlapped I/O for async IOCTL. */ + int iostate; /**< State of overlapped I/O; see definitions in win32.h.= */ + struct rw_handle rwhandle; /**< Used to hook async I/O to the OpenVPN = event loop. */ + int ov_ret; /**< Win32 error code for overlapped operation, 0 for succ= ess */ + + int dco_message_peer_id; + int dco_message_type; + int dco_del_peer_reason; + + uint64_t dco_read_bytes; + uint64_t dco_write_bytes; }; typedef struct dco_context dco_context_t; diff --git a/src/openvpn/forward.c b/src/openvpn/forward.c index 82c54c2..8b94469 100644 --- a/src/openvpn/forward.c +++ b/src/openvpn/forward.c @@ -2083,8 +2083,8 @@ #ifdef ENABLE_ASYNC_PUSH static uintptr_t file_shift =3D FILE_SHIFT; #endif -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) - static uintptr_t dco_shift =3D DCO_SHIFT; /* Event from DCO linux k= ernel module */ +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN= 32) + static uintptr_t dco_shift =3D DCO_SHIFT; /* Event from DCO kernel = module */ #endif /* @@ -2197,7 +2197,7 @@ &c->c2.link_sockets[i]->ev_arg, NULL); } tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)tun_shift, NULL= ); -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN= 32) if (socket & EVENT_READ && c->c2.did_open_tun) { dco_event_set(&c->c1.tuntap->dco, c->c2.event_set, (void *)dco_shi= ft); diff --git a/src/openvpn/mudp.c b/src/openvpn/mudp.c index 5f43db9..e0b27af 100644 --- a/src/openvpn/mudp.c +++ b/src/openvpn/mudp.c @@ -406,7 +406,8 @@ multi_process_file_closed(m, mpp_flags); } #endif -#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) +#if defined(ENABLE_DCO) \ + && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET= _WIN32)) else if (status & DCO_READ) { if (!IS_SIG(&m->top)) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index f76dad8..7ab9289 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -3234,7 +3234,8 @@ } #endif -#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) +#if defined(ENABLE_DCO) \ + && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET= _WIN32)) static void process_incoming_del_peer(struct multi_context *m, struct multi_instance *= mi, dco_context_t *dco) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/816?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: Iedc10616225f6769c66d3c29d4a462b622ebbc6e Gerrit-Change-Number: 816 Gerrit-PatchSet: 21 Gerrit-Owner: stipa Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-Attention: stipa Gerrit-MessageType: newpatchset --MQAb4y9fxDI= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos, s= tipa.

stipa uploaded patch set #21 to this change.

View Change

The following approvals got outdated and were removed: Code-Review+1 by cron2

dco-win: kernel notifications

The driver supp= orts notifications mechanism, which
is used to notify userspace about va= rious events,
such as peer keepalive timeout, key expire and so on.
<= br>This uses existing framework of subscribing and
receiving dco notific= ations, used by FreeBSD and Linux
implementations. On Windows we use ove= rlapped IO,
which state we keep in DCO context. We create an event,
w= hich is associated with overlapped operation,
and inject it into openvpn= event loop. When event is
signalled, we read overlapped result into DCO= context,
which is later used by already existing code which
handles = peer deletion.

Change-Id: Iedc10616225f6769c66d3c29d4a462b622ebbc6e<= br>Signed-off-by: Lev Stipakov <lev@...515...>
---
M src/openvp= n/dco_win.c
M src/openvpn/dco_win.h
M src/openvpn/forward.c
M src/= openvpn/mudp.c
M src/openvpn/multi.c
5 files changed, 137 insertions(= +), 9 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvp=
n refs/changes/16/816/21
diff --git a/src/openvpn/dco_win.c b/src/o=
penvpn/dco_win.c
index a3c7b63..f46c93d 100644
--- a/src/openvpn/dco_win.c
+++ b/src/openvpn/dco_win.c=
@@ -126,6 +126,17 @@
ASSERT(dco->i= fmode =3D=3D DCO_MODE_UNINIT);
dco->ifmode =3D DCO_= MODE_MP;

+ /* Use manual reset event so it remains signalled until+ * explicitly reset. Thi= s way we won't lose notifications
+ */
+ dco->ov.hEvent =3D CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (dco->ov.hEvent= =3D=3D NULL)
+ {<= /span>
+ msg(M_ERR, &= quot;Error: ovpn_dco_init: CreateEvent failed");
+ }
+
= + dco->rwhandle.read =3D dco->ov.hEvent;
+
/* open DCO device */
struct gc_arena gc =3D gc_new();
c= onst char *device_guid;
@@ -629,11 +640,74 @@
}

}

+/**
+ * @brief Handles successful completion of overlapped operation.=
+ *
+ * We use overlapped I/O (Windows te= rm for asynchronous I/O) to get
+ * notifications from kernel to userspace. This gets the result= of overlapped
+ * op= eration and, in case of success, copies data from kernel-filled buffer
+ * into userspace-provid= ed dco context.
+ *
+ * @param dco Pointer= to the dco context
+= * @param queued true if operation was queued, false if it has completed im= mediately
+ */=
+static void
+dco_handle_overlapped_success(dco_= context_t *dco, bool queued)

+{
+ DWORD= bytes_read =3D 0;
+ = BOOL res =3D GetOverlappedResult(dco->tt->hand, &dco->ov, &= amp;bytes_read, FALSE);
+ if (res)
+ = {
+ msg(D_DCO= _DEBUG, "%s: completion%s success [%ld]", __func__, queued ? &quo= t;" : " non-queued", bytes_read);
+
+ dco->dco_message_peer_id =3D dco->notif_buf.PeerId;=
+ dco->dco= _message_type =3D dco->notif_buf.Cmd;
+ dco->dco_del_peer_reason =3D dco->notif_= buf.DelPeerReason;
+ = }
+ else=
+ {
+ msg(D_DCO_DEBUG | M_ERRNO, "= ;%s: completion%s error", __func__, queued ? "" : " non= -queued");
+ = }
+}
+
int
dco_do_read(dco_context_t *dco)

{
- /* no-op on windows */
- ASSERT(0);
+ if (dco->ifmode !=3D DCO_MODE_MP)<= /span>
+ {
+ ASSERT(false);

<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ dco->dco_message_peer_id =3D -1;
+ dco->dco_message_type =3D 0;+
+ switch (dco->iostate)
+ {
+ case IOSTATE_QUEUED:
+ dco_handle_overlapped_success= (dco, true);
+=
+ ASSERT(ResetEv= ent(dco->ov.hEvent));
+ dco->iostate =3D IOSTATE_INITIAL;
+
+ break;
+
+ = case IOSTATE_IMMEDIATE_RETURN:
+ dco->iostate =3D IOSTATE_INITIAL;
+ ASSERT(ResetEvent(dc= o->ov.hEvent));

+<= /span>
+ if (dco-= >ov_ret =3D=3D ERROR_SUCCESS)
+ {
+ dco_handle_overlapped_success(dco, false);+ }
+ else

+ {
+ SetLastError(dco->ov_ret)= ;
+ ms= g(D_DCO_DEBUG | M_ERRNO, "%s: completion non-queued error", __fun= c__);
+ }<= /span>
+
+ break;
+ }
+
return 0;
}

@@ -676,8 +750,48 @@
void
dco_event_set(dco_context_t *dco, struct event_set *es, voi= d *arg)
{
- /* no-op on windows */
- ASSERT(0);
+ if (dco->ifmode !=3D DCO_MODE_MP)
+ {
+ /* mp only */
+ return;
+ }
+
+ event_ctl(es, &= dco->rwhandle, EVENT_READ, arg);
+
+ = if (dco->iostate =3D=3D IOSTATE_INITIAL)
+ {
+ /* the overlapped IOCTL will signal this event on I/O co= mpletion */
+ = ASSERT(ResetEvent(dco->ov.hEvent));
+
+= if (!DeviceIoControl(dco->tt->hand, OVPN_IOCTL_NOTIFY_EVENT, = NULL, 0, &dco->notif_buf, sizeof(dco->notif_buf), NULL, &dco-= >ov))
+ {
+ DWORD err= =3D GetLastError();
= + if (err =3D=3D ERROR_IO_PENDING) /* operation queued? */
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ dco->iostate= =3D IOSTATE_QUEUED;

= + dco->ov_ret =3D ERROR_SUCCESS;
+
+ msg(D_DCO_DEBUG, "%s: notify ioctl queued&= quot;, __func__);
+ = }
+ = else
+ = {
+ /= * error occured */
+ = ASSERT(SetEvent(dco->ov.hEvent));
+ dco->iostate =3D IOSTA= TE_IMMEDIATE_RETURN;
= + dco->ov_ret =3D err;
+
+ msg(D_DCO_DEBUG | M_ERRNO, "%s: notify ioctl error&q= uot;, __func__);
+ = }
+ }=
+ else=
+ {
+ ASSERT(SetEvent(dco->= ov.hEvent));
+ = dco->iostate =3D IOSTATE_IMMEDIATE_RETURN;
+ dco->ov_ret =3D ERROR_SUCCESS= ;
+
+ msg(D_DCO_DEBUG, "%s= : notify ioctl immediate return", __func__);
+ }
+ }
}

const char *

diff --git a/src/openvpn/dco_win.h b/src/= openvpn/dco_win.h
index dc7fbc1..309badf 100644
<= span>--- a/src/openvpn/dco_win.h

+++ b/src/openvpn/dco_win.= h
@@ -41,6 +41,18 @@
struct tuntap *tt= ;
dco_mode_type ifmode;

+ OVPN_NOTIFY_EVENT notif_buf;= /**< Buffer for incoming notifications. */

+ OVERLAPPED ov; /**< Used by overlapped I/= O for async IOCTL. */
+ int iostate; /**< State of overlapped I/O; see definitions in win3= 2.h. */
+ struct r= w_handle rwhandle; /**< Used to hook async I/O to the OpenVPN event loop= . */
+ int ov_ret;= /**< Win32 error code for overlapped operation, 0 for success */=
+
+ int dco_message_peer_id;
+ int dco_message_type;
= + int dco_del_peer_reason;
+
+ uint64_t dco_read_bytes;
<= span style=3D"color: hsl(120, 100%, 40%);">+ uint64_t dco_write_bytes;
};

typedef struct dco_co= ntext dco_context_t;
diff --git a/src/openvpn/forward.c b/s= rc/openvpn/forward.c
index 82c54c2..8b94469 100644--- a/src/openvpn/forward.c
+++ b/src/openvpn/forwa= rd.c
@@ -2083,8 +2083,8 @@
#ifdef ENABLE_A= SYNC_PUSH
static uintptr_t file_shift =3D FILE_SHIFT;<= /span>
#endif
-#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
- static uintptr_t dco_shift =3D DCO_SHI= FT; /* Event from DCO linux kernel module */
+#if defined(TARGET_LINUX) || defined(TARGET_FRE= EBSD) || defined(TARGET_WIN32)
+ static uintptr_t dco_shift =3D DCO_SHIFT; /* Event from D= CO kernel module */
#endif

/*

@@ -2197,7 +2197,7 @@
= &c->c2.link_sockets[i]->ev_arg, NULL);
= }
tun_set(c->c1.tuntap, c->c2.event_set, tu= ntap, (void *)tun_shift, NULL);
-#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)
+#if defined(TARGET_LINUX) || def= ined(TARGET_FREEBSD) || defined(TARGET_WIN32)

if (sock= et & EVENT_READ && c->c2.did_open_tun)
= {
dco_event_set(&c->c1.tuntap->dco, c-&g= t;c2.event_set, (void *)dco_shift);
diff --git a/src/openvp= n/mudp.c b/src/openvpn/mudp.c
index 5f43db9..e0b27af 100644=
--- a/src/openvpn/mudp.c
+++ b/src/openvpn= /mudp.c
@@ -406,7 +406,8 @@
multi_= process_file_closed(m, mpp_flags);
}
= #endif
-#if defined(ENA= BLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
+#if defined(ENABLE_DCO) = \
+ && (de= fined(TARGET_LINUX) || defined(TARGET_FREEBSD) || defined(TARGET_WIN32))

else if (status & DCO_READ)
{
if (!IS_SIG(&m->top))
diff -= -git a/src/openvpn/multi.c b/src/openvpn/multi.c
index f76d= ad8..7ab9289 100644
--- a/src/openvpn/multi.c
+++ b/src/openvpn/multi.c

@@ -3234,7 +3234,8 @@ }
#endif

-#if defined(ENABLE_DCO) && (defined= (TARGET_LINUX) || defined(TARGET_FREEBSD))
+#if defined(ENABLE_DCO) \
+ && (defined(TARGET_LINUX) || defin= ed(TARGET_FREEBSD) || defined(TARGET_WIN32))
static void
process_incoming_del_peer(struct multi_context *m, struct m= ulti_instance *mi,
dco_context_t= *dco)

To view, visit change 816. To unsubsc= ribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Iedc10616225f6769c66d3c29d4a= 462b622ebbc6e
Gerrit-Change-Number: 816
Gerrit-PatchSet: 21
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-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: stipa <lstipakov@...1663...= .>
Gerrit-MessageType: newpatchset
--MQAb4y9fxDI=--