Skip to content

Commit 5793e4c

Browse files
add clarifying comments + refactor functions
1 parent ec6fd24 commit 5793e4c

2 files changed

Lines changed: 119 additions & 84 deletions

File tree

src/tools/xdpmap/orchestrator/orchestrator.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,9 @@ main(
311311
(void)getchar();
312312

313313
//
314-
// Cleanup: closing the program handle detaches XDP. Closing the map
315-
// handle releases our reference (consumer may still hold one).
314+
// Cleanup: closing the program handle detaches XDP and destroys the
315+
// rules. The XSKMAP stays alive as long as quicxdpmapserver.exe still
316+
// holds a handle to it, but without rules no traffic will be redirected.
316317
//
317318
printf("Detaching XDP program and cleaning up.\n");
318319
CloseHandle(Program);

src/tools/xdpmap/quicxdpmapserver/quicxdpmapserver.c

Lines changed: 116 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const uint16_t UdpPort = 4567;
5353

5454
const QUIC_API_TABLE* MsQuic;
5555
HQUIC Registration;
56-
HQUIC Configuration;
5756

5857
static
5958
BOOLEAN
@@ -112,6 +111,51 @@ PrintUsage(void)
112111
);
113112
}
114113

114+
typedef struct SERVER_ARGS {
115+
UINT32 XdpMapIfIndex;
116+
const char* CertHash;
117+
const char* CertFile;
118+
const char* KeyFile;
119+
const char* Password;
120+
const char* CibirIdHex;
121+
} SERVER_ARGS;
122+
123+
static
124+
BOOLEAN
125+
ParseArgs(
126+
_In_ int argc,
127+
_In_reads_(argc) _Null_terminated_ char* argv[],
128+
_Out_ SERVER_ARGS* Args
129+
)
130+
{
131+
memset(Args, 0, sizeof(*Args));
132+
133+
const char* IfIndexStr = GetValue(argc, argv, "xdp_map_ifindex");
134+
if (IfIndexStr == NULL) {
135+
printf("Missing required argument '-xdp_map_ifindex:<N>'.\n");
136+
return FALSE;
137+
}
138+
Args->XdpMapIfIndex = (UINT32)atoi(IfIndexStr);
139+
if (Args->XdpMapIfIndex == 0) {
140+
printf("Invalid interface index '%s'.\n", IfIndexStr);
141+
return FALSE;
142+
}
143+
144+
Args->CertHash = GetValue(argc, argv, "cert_hash");
145+
Args->CertFile = GetValue(argc, argv, "cert_file");
146+
Args->KeyFile = GetValue(argc, argv, "key_file");
147+
Args->Password = GetValue(argc, argv, "password");
148+
Args->CibirIdHex = GetValue(argc, argv, "cibir_id");
149+
150+
if (Args->CertHash == NULL &&
151+
(Args->CertFile == NULL || Args->KeyFile == NULL)) {
152+
printf("Must specify '-cert_hash' or '-cert_file' with '-key_file'.\n");
153+
return FALSE;
154+
}
155+
156+
return TRUE;
157+
}
158+
115159
typedef struct QUIC_CREDENTIAL_CONFIG_HELPER {
116160
QUIC_CREDENTIAL_CONFIG CredConfig;
117161
union {
@@ -201,11 +245,11 @@ ServerListenerCallback(
201245
)
202246
{
203247
UNREFERENCED_PARAMETER(Listener);
204-
UNREFERENCED_PARAMETER(Context);
205248

206249
if (Event->Type == QUIC_LISTENER_EVENT_NEW_CONNECTION) {
250+
HQUIC Config = (HQUIC)Context;
207251
MsQuic->SetCallbackHandler(Event->NEW_CONNECTION.Connection, (void*)ServerConnectionCallback, NULL);
208-
return MsQuic->ConnectionSetConfiguration(Event->NEW_CONNECTION.Connection, Configuration);
252+
return MsQuic->ConnectionSetConfiguration(Event->NEW_CONNECTION.Connection, Config);
209253
}
210254

211255
return QUIC_STATUS_NOT_SUPPORTED;
@@ -214,12 +258,13 @@ ServerListenerCallback(
214258
static
215259
BOOLEAN
216260
LoadServerConfiguration(
217-
_In_ int argc,
218-
_In_reads_(argc) _Null_terminated_ char* argv[]
261+
_In_ const SERVER_ARGS* Args,
262+
_Out_ HQUIC* ConfigurationOut
219263
)
220264
{
221265
QUIC_SETTINGS Settings = {0};
222266
QUIC_STATUS Status;
267+
HQUIC Config = NULL;
223268

224269
QUIC_SETTINGS XdpSettings = {0};
225270
XdpSettings.XdpEnabled = TRUE;
@@ -230,91 +275,72 @@ LoadServerConfiguration(
230275
return FALSE;
231276
}
232277

233-
QUIC_CREDENTIAL_CONFIG_HELPER Config;
234-
memset(&Config, 0, sizeof(Config));
235-
Config.CredConfig.Flags = QUIC_CREDENTIAL_FLAG_NONE;
278+
QUIC_CREDENTIAL_CONFIG_HELPER CredHelper;
279+
memset(&CredHelper, 0, sizeof(CredHelper));
280+
CredHelper.CredConfig.Flags = QUIC_CREDENTIAL_FLAG_NONE;
236281

237-
const char* Cert;
238-
const char* KeyFile;
239-
if ((Cert = GetValue(argc, argv, "cert_hash")) != NULL) {
282+
if (Args->CertHash != NULL) {
240283
uint32_t CertHashLen =
241284
XdpMapDecodeHexBuffer(
242-
Cert,
243-
sizeof(Config.CertHash.ShaHash),
244-
Config.CertHash.ShaHash);
245-
if (CertHashLen != sizeof(Config.CertHash.ShaHash)) {
285+
Args->CertHash,
286+
sizeof(CredHelper.CertHash.ShaHash),
287+
CredHelper.CertHash.ShaHash);
288+
if (CertHashLen != sizeof(CredHelper.CertHash.ShaHash)) {
246289
printf("Invalid cert hash length.\n");
247290
return FALSE;
248291
}
249-
Config.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_HASH;
250-
Config.CredConfig.CertificateHash = &Config.CertHash;
251-
} else if ((Cert = GetValue(argc, argv, "cert_file")) != NULL &&
252-
(KeyFile = GetValue(argc, argv, "key_file")) != NULL) {
253-
const char* Password = GetValue(argc, argv, "password");
254-
if (Password != NULL) {
255-
Config.CertFileProtected.CertificateFile = (char*)Cert;
256-
Config.CertFileProtected.PrivateKeyFile = (char*)KeyFile;
257-
Config.CertFileProtected.PrivateKeyPassword = (char*)Password;
258-
Config.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE_PROTECTED;
259-
Config.CredConfig.CertificateFileProtected = &Config.CertFileProtected;
292+
CredHelper.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_HASH;
293+
CredHelper.CredConfig.CertificateHash = &CredHelper.CertHash;
294+
} else {
295+
if (Args->Password != NULL) {
296+
CredHelper.CertFileProtected.CertificateFile = (char*)Args->CertFile;
297+
CredHelper.CertFileProtected.PrivateKeyFile = (char*)Args->KeyFile;
298+
CredHelper.CertFileProtected.PrivateKeyPassword = (char*)Args->Password;
299+
CredHelper.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE_PROTECTED;
300+
CredHelper.CredConfig.CertificateFileProtected = &CredHelper.CertFileProtected;
260301
} else {
261-
Config.CertFile.CertificateFile = (char*)Cert;
262-
Config.CertFile.PrivateKeyFile = (char*)KeyFile;
263-
Config.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE;
264-
Config.CredConfig.CertificateFile = &Config.CertFile;
302+
CredHelper.CertFile.CertificateFile = (char*)Args->CertFile;
303+
CredHelper.CertFile.PrivateKeyFile = (char*)Args->KeyFile;
304+
CredHelper.CredConfig.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE;
305+
CredHelper.CredConfig.CertificateFile = &CredHelper.CertFile;
265306
}
266-
} else {
267-
printf("Must specify '-cert_hash' or '-cert_file' with '-key_file'.\n");
268-
return FALSE;
269307
}
270308

271-
if (QUIC_FAILED(Status = MsQuic->ConfigurationOpen(Registration, &Alpn, 1, &Settings, sizeof(Settings), NULL, &Configuration))) {
309+
if (QUIC_FAILED(Status = MsQuic->ConfigurationOpen(Registration, &Alpn, 1, &Settings, sizeof(Settings), NULL, &Config))) {
272310
printf("ConfigurationOpen failed, 0x%x!\n", Status);
273311
return FALSE;
274312
}
275313

276-
if (QUIC_FAILED(Status = MsQuic->ConfigurationLoadCredential(Configuration, &Config.CredConfig))) {
314+
if (QUIC_FAILED(Status = MsQuic->ConfigurationLoadCredential(Config, &CredHelper.CredConfig))) {
277315
printf("ConfigurationLoadCredential failed, 0x%x!\n", Status);
316+
MsQuic->ConfigurationClose(Config);
278317
return FALSE;
279318
}
280319

320+
*ConfigurationOut = Config;
281321
return TRUE;
282322
}
283323

284324
static
285325
BOOLEAN
286-
ConfigureXdpMap(
287-
_In_ int argc,
288-
_In_reads_(argc) _Null_terminated_ char* argv[]
326+
PromptForXdpMapHandle(
327+
_In_ const SERVER_ARGS* Args,
328+
_Out_ UINT_PTR* HandleValueOut
289329
)
290330
{
291-
const char* MapIfIndexStr = GetValue(argc, argv, "xdp_map_ifindex");
292-
if (MapIfIndexStr == NULL) {
293-
printf("Missing required argument '-xdp_map_ifindex:<N>'.\n");
294-
return FALSE;
295-
}
296-
297-
UINT32 MapIfIndex = (UINT32)atoi(MapIfIndexStr);
298-
if (MapIfIndex == 0) {
299-
printf("Invalid interface index '%s'.\n", MapIfIndexStr);
300-
return FALSE;
301-
}
302-
303331
char InputBuf[64];
304-
UINT_PTR HandleValue;
305332

306333
printf("=== XDP Map Mode (Consumer) ===\n");
307334
printf(" PID: %u\n", (unsigned)GetCurrentProcessId());
308-
printf(" IfIndex: %u\n\n", MapIfIndex);
335+
printf(" IfIndex: %u\n\n", Args->XdpMapIfIndex);
309336
printf("Start orchestrator in another terminal:\n");
310337

311-
const char* CibirHint = GetValue(argc, argv, "cibir_id");
312-
if (CibirHint != NULL) {
338+
if (Args->CibirIdHex != NULL) {
313339
printf(" ./orchestrator.exe -TargetPid %u -IfIndex %u -UdpPort %u -CibirId %s\n",
314-
(unsigned)GetCurrentProcessId(), MapIfIndex, UdpPort, CibirHint);
340+
(unsigned)GetCurrentProcessId(), Args->XdpMapIfIndex, UdpPort, Args->CibirIdHex);
315341
} else {
316342
printf(" ./orchestrator.exe -TargetPid %u -IfIndex %u -UdpPort %u\n",
317-
(unsigned)GetCurrentProcessId(), MapIfIndex, UdpPort);
343+
(unsigned)GetCurrentProcessId(), Args->XdpMapIfIndex, UdpPort);
318344
}
319345

320346
printf("\nPaste XSKMAP handle value here (hex): ");
@@ -325,15 +351,25 @@ ConfigureXdpMap(
325351
return FALSE;
326352
}
327353

328-
HandleValue = (UINT_PTR)_strtoui64(InputBuf, NULL, 16);
329-
if (HandleValue == 0 || HandleValue == (UINT_PTR)INVALID_HANDLE_VALUE) {
354+
*HandleValueOut = (UINT_PTR)_strtoui64(InputBuf, NULL, 16);
355+
if (*HandleValueOut == 0 || *HandleValueOut == (UINT_PTR)INVALID_HANDLE_VALUE) {
330356
printf("Invalid handle value: %s\n", InputBuf);
331357
return FALSE;
332358
}
333359

360+
return TRUE;
361+
}
362+
363+
static
364+
BOOLEAN
365+
ConfigureXdpMap(
366+
_In_ UINT32 IfIndex,
367+
_In_ QUIC_XDP_MAP_HANDLE MapHandle
368+
)
369+
{
334370
QUIC_XDP_MAP_CONFIG MapConfig;
335-
MapConfig.InterfaceIndex = MapIfIndex;
336-
MapConfig.MapHandle = (QUIC_XDP_MAP_HANDLE)HandleValue;
371+
MapConfig.InterfaceIndex = IfIndex;
372+
MapConfig.MapHandle = MapHandle;
337373

338374
QUIC_STATUS Status = MsQuic->SetParam(
339375
NULL,
@@ -345,37 +381,36 @@ ConfigureXdpMap(
345381
return FALSE;
346382
}
347383

348-
printf("XDP map config set (IfIndex=%u, MapHandle=0x%IX).\n\n", MapIfIndex, HandleValue);
384+
printf("XDP map config set (IfIndex=%u, MapHandle=0x%IX).\n\n", IfIndex, (UINT_PTR)MapHandle);
349385
return TRUE;
350386
}
351387

352388
static
353389
void
354390
RunServer(
355-
_In_ int argc,
356-
_In_reads_(argc) _Null_terminated_ char* argv[]
391+
_In_ const SERVER_ARGS* Args
357392
)
358393
{
359394
QUIC_STATUS Status;
360395
HQUIC Listener = NULL;
396+
HQUIC Configuration = NULL;
361397

362-
if (!LoadServerConfiguration(argc, argv)) {
398+
if (!LoadServerConfiguration(Args, &Configuration)) {
363399
return;
364400
}
365401

366402
QUIC_ADDR Address = {0};
367403
QuicAddrSetFamily(&Address, QUIC_ADDRESS_FAMILY_UNSPEC);
368404
QuicAddrSetPort(&Address, UdpPort);
369405

370-
if (QUIC_FAILED(Status = MsQuic->ListenerOpen(Registration, ServerListenerCallback, NULL, &Listener))) {
406+
if (QUIC_FAILED(Status = MsQuic->ListenerOpen(Registration, ServerListenerCallback, Configuration, &Listener))) {
371407
printf("ListenerOpen failed, 0x%x!\n", Status);
372408
goto Error;
373409
}
374410

375-
const char* CibirIdHex = GetValue(argc, argv, "cibir_id");
376-
if (CibirIdHex != NULL) {
411+
if (Args->CibirIdHex != NULL) {
377412
uint8_t CibirId[XDPMAP_CIBIR_RAW_MAX_LEN];
378-
uint32_t CibirIdLen = XdpMapDecodeHexBuffer(CibirIdHex, sizeof(CibirId), CibirId);
413+
uint32_t CibirIdLen = XdpMapDecodeHexBuffer(Args->CibirIdHex, sizeof(CibirId), CibirId);
379414
if (CibirIdLen < 2) {
380415
printf("CIBIR ID too short (need offset + >=1 CID byte).\n");
381416
goto Error;
@@ -400,6 +435,9 @@ RunServer(
400435
if (Listener != NULL) {
401436
MsQuic->ListenerClose(Listener);
402437
}
438+
if (Configuration != NULL) {
439+
MsQuic->ConfigurationClose(Configuration);
440+
}
403441
}
404442

405443
int
@@ -416,15 +454,8 @@ main(
416454
return 0;
417455
}
418456

419-
if (GetValue(argc, argv, "xdp_map_ifindex") == NULL) {
420-
printf("Missing required argument '-xdp_map_ifindex:<N>'.\n");
421-
PrintUsage();
422-
return (int)QUIC_STATUS_INVALID_PARAMETER;
423-
}
424-
425-
if (GetValue(argc, argv, "cert_hash") == NULL &&
426-
(GetValue(argc, argv, "cert_file") == NULL || GetValue(argc, argv, "key_file") == NULL)) {
427-
printf("Must specify '-cert_hash' or '-cert_file' with '-key_file'.\n");
457+
SERVER_ARGS Args;
458+
if (!ParseArgs(argc, argv, &Args)) {
428459
PrintUsage();
429460
return (int)QUIC_STATUS_INVALID_PARAMETER;
430461
}
@@ -434,7 +465,13 @@ main(
434465
goto Error;
435466
}
436467

437-
if (!ConfigureXdpMap(argc, argv)) {
468+
UINT_PTR MapHandleValue;
469+
if (!PromptForXdpMapHandle(&Args, &MapHandleValue)) {
470+
Status = QUIC_STATUS_INVALID_PARAMETER;
471+
goto Error;
472+
}
473+
474+
if (!ConfigureXdpMap(Args.XdpMapIfIndex, (QUIC_XDP_MAP_HANDLE)MapHandleValue)) {
438475
Status = QUIC_STATUS_INVALID_PARAMETER;
439476
goto Error;
440477
}
@@ -444,13 +481,10 @@ main(
444481
goto Error;
445482
}
446483

447-
RunServer(argc, argv);
484+
RunServer(&Args);
448485

449486
Error:
450487
if (MsQuic != NULL) {
451-
if (Configuration != NULL) {
452-
MsQuic->ConfigurationClose(Configuration);
453-
}
454488
if (Registration != NULL) {
455489
MsQuic->RegistrationClose(Registration);
456490
}

0 commit comments

Comments
 (0)