Skip to content

Commit 2886c03

Browse files
authored
add driver start callback API (#740)
1 parent 1746795 commit 2886c03

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

published/external/xdp/details/xdpapi.h

+61
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,67 @@ XdpInterfaceOpen(
9090
#define IOCTL_INTERFACE_OFFLOAD_QEO_SET \
9191
CTL_CODE(FILE_DEVICE_NETWORK, 3, METHOD_BUFFERED, FILE_WRITE_ACCESS)
9292

93+
#ifdef _KERNEL_MODE
94+
95+
#define XDP_DRIVER_START_CALLBACK_NAME L"\\Callback\\XdpDriverStart"
96+
97+
inline
98+
_IRQL_requires_max_(PASSIVE_LEVEL)
99+
XDP_STATUS
100+
XdpRegisterDriverStartCallback(
101+
_Out_ XDP_CALLBACK_HANDLE *CallbackRegistration,
102+
_In_ PCALLBACK_FUNCTION CallbackFunction,
103+
_In_opt_ VOID *CallbackContext
104+
)
105+
{
106+
XDP_STATUS Status;
107+
OBJECT_ATTRIBUTES ObjectAttributes;
108+
UNICODE_STRING CallbackObjectName;
109+
PCALLBACK_OBJECT CallbackObject = NULL;
110+
111+
RtlInitUnicodeString(&CallbackObjectName, XDP_DRIVER_START_CALLBACK_NAME);
112+
InitializeObjectAttributes(
113+
&ObjectAttributes, &CallbackObjectName,
114+
OBJ_CASE_INSENSITIVE | OBJ_PERMANENT | OBJ_KERNEL_HANDLE, NULL, NULL);
115+
116+
Status = ExCreateCallback(&CallbackObject, &ObjectAttributes, TRUE, TRUE);
117+
if (!NT_SUCCESS(Status)) {
118+
goto Exit;
119+
}
120+
121+
*CallbackRegistration = ExRegisterCallback(CallbackObject, CallbackFunction, CallbackContext);
122+
if (*CallbackRegistration == NULL) {
123+
Status = STATUS_INSUFFICIENT_RESOURCES;
124+
goto Exit;
125+
}
126+
127+
Exit:
128+
129+
if (CallbackObject != NULL) {
130+
//
131+
// The callback registration holds an implicit reference on the callback
132+
// object, so release the initial reference. The callback will finally
133+
// be cleaned up when no registrations remain.
134+
//
135+
ObDereferenceObject(CallbackObject);
136+
}
137+
138+
return Status;
139+
}
140+
141+
inline
142+
_IRQL_requires_max_(PASSIVE_LEVEL)
143+
VOID
144+
XdpDeregisterDriverStartCallback(
145+
_In_ XDP_CALLBACK_HANDLE CallbackRegistration
146+
)
147+
{
148+
XDPAPI_ASSERT(CallbackRegistration != NULL);
149+
ExUnregisterCallback(CallbackRegistration);
150+
}
151+
152+
#endif // _KERNEL_MODE
153+
93154
#ifdef __cplusplus
94155
} // extern "C"
95156
#endif

published/external/xdpapi.h

+20
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ XdpInterfaceOpen(
5252
_Out_ HANDLE *InterfaceHandle
5353
);
5454

55+
#ifdef _KERNEL_MODE
56+
57+
DECLARE_HANDLE(XDP_CALLBACK_HANDLE);
58+
59+
_IRQL_requires_max_(PASSIVE_LEVEL)
60+
XDP_STATUS
61+
XdpRegisterDriverStartCallback(
62+
_Out_ XDP_CALLBACK_HANDLE *CallbackRegistration,
63+
_In_ PCALLBACK_FUNCTION CallbackFunction,
64+
_In_opt_ VOID *CallbackContext
65+
);
66+
67+
_IRQL_requires_max_(PASSIVE_LEVEL)
68+
VOID
69+
XdpDeregisterDriverStartCallback(
70+
_In_ XDP_CALLBACK_HANDLE CallbackRegistration
71+
);
72+
73+
#endif
74+
5575
#include <xdp/details/xdpapi.h>
5676
#endif
5777

src/xdp/dispatch.c

+45
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,42 @@ XdpStart(
505505
return Status;
506506
}
507507

508+
static
509+
NTSTATUS
510+
XdpNotifyDriverStart(
511+
VOID
512+
)
513+
{
514+
NTSTATUS Status;
515+
OBJECT_ATTRIBUTES ObjectAttributes;
516+
UNICODE_STRING CallbackObjectName;
517+
PCALLBACK_OBJECT CallbackObject = NULL;
518+
519+
TraceEnter(TRACE_CORE, "-");
520+
521+
RtlInitUnicodeString(&CallbackObjectName, XDP_DRIVER_START_CALLBACK_NAME);
522+
InitializeObjectAttributes(
523+
&ObjectAttributes, &CallbackObjectName,
524+
OBJ_CASE_INSENSITIVE | OBJ_PERMANENT | OBJ_KERNEL_HANDLE, NULL, NULL);
525+
526+
Status = ExCreateCallback(&CallbackObject, &ObjectAttributes, TRUE, TRUE);
527+
if (!NT_SUCCESS(Status)) {
528+
goto Exit;
529+
}
530+
531+
ExNotifyCallback(CallbackObject, NULL, NULL);
532+
533+
Exit:
534+
535+
if (CallbackObject != NULL) {
536+
ObDereferenceObject(CallbackObject);
537+
}
538+
539+
TraceExitStatus(TRACE_CORE);
540+
541+
return Status;
542+
}
543+
508544
_Use_decl_annotations_
509545
NTSTATUS
510546
DriverEntry(
@@ -552,6 +588,15 @@ DriverEntry(
552588
goto Exit;
553589
}
554590

591+
//
592+
// Indicate the driver has been loaded to any registered callbacks. This
593+
// should be done after the driver is ready to handle API requests.
594+
//
595+
Status = XdpNotifyDriverStart();
596+
if (!NT_SUCCESS(Status)) {
597+
goto Exit;
598+
}
599+
555600
Exit:
556601

557602
TraceExitStatus(TRACE_CORE);

0 commit comments

Comments
 (0)