-
Notifications
You must be signed in to change notification settings - Fork 585
External Execution Interface #4616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
448184f
442c928
e0f9d13
adb9ad9
882e630
da9e0b2
94efc44
9695592
4554407
40d1e84
74835cf
f73a471
b743020
dd7abe0
be0855e
919ae67
0e4b73d
94b4c3f
446a9f3
f39e7f8
b15ba57
4a86d10
bd22e7d
c5b97f8
d2d209c
3264294
9603086
a06aa81
8c7c05b
8948a57
2e1bf82
74b8cb9
9ccc97d
4cab2f5
7c4b651
ad32894
2039771
a44b999
da26362
e5731f4
38feb45
fd4a623
630aa78
f387952
cfefdf8
2924b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,7 @@ Each of these threads handle both the datapath (i.e., UDP) and QUIC layers by de | |
Using a single worker thread for both layers helps MsQuic can achieve lower latency and using separate threads for the two layers can help achieve higher throughput. | ||
MsQuic aligns its processing logic with the rest of the networking stack (including hardware RSS) to ensure that all processing stays on the same NUMA node, and ideally, the same processor. | ||
|
||
The complexity of aligning processing across various threads and processors is the primary reason for MsQuic to manage its own threading. | ||
The complexity of aligning processing across various threads and processors is the primary reason for MsQuic to manage its own threading. | ||
This provides developers with a performant abstraction of both functionality and threading model, which simplifies application development using MsQuic, ensuring that things "just work" efficiently for QUIC by default. | ||
|
||
Each thread manages the execution of one or more connections. | ||
|
@@ -116,6 +116,78 @@ graph TD | |
end | ||
``` | ||
|
||
## Custom Execution | ||
|
||
MsQuic also supports scenarios where the application layer creates the threads that MsQuic uses to execute on. | ||
In this mode, the application creates one or more execution contexts that MsQuic will use to run all its internal logic. | ||
The application is responsible for calling down into MsQuic to allow these execution contexts to run. | ||
|
||
To create an execution context, the app much first create an event queue object, which is a platform specific type: | ||
|
||
- Windows: IOCP | ||
- Linux: epoll | ||
- macOS: kqueue | ||
|
||
On Windows, the following types are defined: | ||
|
||
```c++ | ||
typedef HANDLE QUIC_EVENTQ; | ||
|
||
typedef OVERLAPPED_ENTRY CXPLAT_CQE; | ||
|
||
typedef | ||
_IRQL_requires_max_(PASSIVE_LEVEL) | ||
void | ||
(CXPLAT_EVENT_COMPLETION)( | ||
_In_ CXPLAT_CQE* Cqe | ||
); | ||
typedef CXPLAT_EVENT_COMPLETION *CXPLAT_EVENT_COMPLETION_HANDLER; | ||
|
||
typedef struct CXPLAT_SQE { | ||
OVERLAPPED Overlapped; | ||
CXPLAT_EVENT_COMPLETION_HANDLER Completion; | ||
} CXPLAT_SQE; | ||
``` | ||
|
||
You will also notice the definiton for `QUIC_SQE` (SQE stands for submission queue entry), which defines the format that all completion events must take so they may be generically processed from the event queue (more on this below). | ||
|
||
Once the app has the event queue, it may create the execution context with the `ExecutionCreate` function: | ||
|
||
```c++ | ||
HANDLE IOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1); | ||
QUIC_EXECUTION_CONFIG ExecConfig = { 0, &IOCP }; | ||
|
||
QUIC_EXECUTION* ExecContext = nullptr; | ||
QUIC_STATUS Status = MsQuic->ExecutionCreate(QUIC_GLOBAL_EXECUTION_CONFIG_FLAG_NONE, 0, 1, &ExecConfig, &ExecContext); | ||
``` | ||
|
||
The above code createa a new IOCP (for Windows), sets up an execution config, indicating an ideal processor of 0 and the pointer to the IOCP, and then calls MsQuic to create 1 execution context. | ||
nibanks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
An application may expand this code to create multiple execution contexts, depending on their needs. | ||
|
||
To drive this execution context, the app will need to to periodically call `ExecutionPoll` and use the platform specific function to drain completion events from the event queue. | ||
|
||
```c | ||
bool AllDone = false; | ||
while (!AllDone) { | ||
uint32_t WaitTime = MsQuic->ExecutionPoll(ExecContext); | ||
|
||
ULONG OverlappedCount = 0; | ||
OVERLAPPED_ENTRY Overlapped[8]; | ||
if (GetQueuedCompletionStatusEx(IOCP, Overlapped, ARRAYSIZE(Overlapped), &OverlappedCount, WaitTime, FALSE)) { | ||
for (ULONG i = 0; i < OverlappedCount; ++i) { | ||
QUIC_SQE* Sqe = CONTAINING_RECORD(Overlapped[i].lpOverlapped, QUIC_SQE, Overlapped); | ||
Sqe->Completion(&Overlapped[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can user quic callback event be invoked on this user thread here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the app I added in this PR is single threaded. It works and I have the logs to prove it. |
||
} | ||
} | ||
} | ||
``` | ||
|
||
Above, you can see a simple loop that properly drives a single execution context on Windows. | ||
`OVERLAPPED_ENTRY` objects received from `GetQueuedCompletionStatusEx` are used to get the submission queue entry and then call its completion handler. | ||
|
||
In a real application, these completion events may come both from MsQuic and the application itself, therefore, this means **the application must use the same base format for its own submission entries**. | ||
This is necessary to be able to share the same event queue object. | ||
|
||
# See Also | ||
|
||
[QUIC_STREAM_CALLBACK](api/QUIC_STREAM_CALLBACK.md)<br> | ||
|
Uh oh!
There was an error while loading. Please reload this page.