2 * Copyright (c) 2014 VMware, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * XXX: OVS_USE_NL_INTERFACE is being used to keep the legacy DPIF interface
19 * alive while we transition over to the netlink based interface.
20 * OVS_USE_NL_INTERFACE = 0 => legacy inteface to use with dpif-windows.c
21 * OVS_USE_NL_INTERFACE = 1 => netlink inteface to use with ported dpif-linux.c
41 #define OVS_DBG_MOD OVS_DBG_DATAPATH
44 #define NETLINK_FAMILY_NAME_LEN 48
48 * Netlink messages are grouped by family (aka type), and each family supports
49 * a set of commands, and can be passed both from kernel -> userspace or
50 * vice-versa. To call into the kernel, userspace uses a device operation which
51 * is outside of a netlink message.
53 * Each command results in the invocation of a handler function to implement the
54 * request functionality.
56 * Expectedly, only certain combinations of (device operation, netlink family,
59 * Here, we implement the basic infrastructure to perform validation on the
60 * incoming message, version checking, and also to invoke the corresponding
61 * handler to do the heavy-lifting.
65 * Handler for a given netlink command. Not all the parameters are used by all
68 typedef NTSTATUS(NetlinkCmdHandler)(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
71 typedef struct _NETLINK_CMD {
73 NetlinkCmdHandler *handler;
74 UINT32 supportedDevOp; /* Supported device operations. */
75 BOOLEAN validateDpIndex; /* Does command require a valid DP argument. */
76 } NETLINK_CMD, *PNETLINK_CMD;
78 /* A netlink family is a group of commands. */
79 typedef struct _NETLINK_FAMILY {
86 NETLINK_CMD *cmds; /* Array of netlink commands and handlers. */
88 } NETLINK_FAMILY, *PNETLINK_FAMILY;
90 /* Handlers for the various netlink commands. */
91 static NetlinkCmdHandler OvsPendEventCmdHandler,
92 OvsPendPacketCmdHandler,
93 OvsSubscribeEventCmdHandler,
94 OvsSubscribePacketCmdHandler,
95 OvsReadEventCmdHandler,
96 OvsReadPacketCmdHandler,
101 NetlinkCmdHandler OvsGetNetdevCmdHandler,
102 OvsGetVportCmdHandler,
103 OvsSetVportCmdHandler,
104 OvsNewVportCmdHandler,
105 OvsDeleteVportCmdHandler;
107 static NTSTATUS HandleGetDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
109 static NTSTATUS HandleGetDpDump(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
111 static NTSTATUS HandleDpTransactionCommon(
112 POVS_USER_PARAMS_CONTEXT usrParamsCtx, UINT32 *replyLen);
113 static NTSTATUS OvsGetPidHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
117 * The various netlink families, along with the supported commands. Most of
118 * these families and commands are part of the openvswitch specification for a
119 * netlink datapath. In addition, each platform can implement a few families
120 * and commands as extensions.
123 /* Netlink control family: this is a Windows specific family. */
124 NETLINK_CMD nlControlFamilyCmdOps[] = {
125 { .cmd = OVS_CTRL_CMD_WIN_PEND_REQ,
126 .handler = OvsPendEventCmdHandler,
127 .supportedDevOp = OVS_WRITE_DEV_OP,
128 .validateDpIndex = TRUE,
130 { .cmd = OVS_CTRL_CMD_WIN_PEND_PACKET_REQ,
131 .handler = OvsPendPacketCmdHandler,
132 .supportedDevOp = OVS_WRITE_DEV_OP,
133 .validateDpIndex = TRUE,
135 { .cmd = OVS_CTRL_CMD_MC_SUBSCRIBE_REQ,
136 .handler = OvsSubscribeEventCmdHandler,
137 .supportedDevOp = OVS_WRITE_DEV_OP,
138 .validateDpIndex = TRUE,
140 { .cmd = OVS_CTRL_CMD_PACKET_SUBSCRIBE_REQ,
141 .handler = OvsSubscribePacketCmdHandler,
142 .supportedDevOp = OVS_WRITE_DEV_OP,
143 .validateDpIndex = TRUE,
145 { .cmd = OVS_CTRL_CMD_EVENT_NOTIFY,
146 .handler = OvsReadEventCmdHandler,
147 .supportedDevOp = OVS_READ_DEV_OP,
148 .validateDpIndex = FALSE,
150 { .cmd = OVS_CTRL_CMD_READ_NOTIFY,
151 .handler = OvsReadPacketCmdHandler,
152 .supportedDevOp = OVS_READ_DEV_OP,
153 .validateDpIndex = FALSE,
157 NETLINK_FAMILY nlControlFamilyOps = {
158 .name = OVS_WIN_CONTROL_FAMILY,
159 .id = OVS_WIN_NL_CTRL_FAMILY_ID,
160 .version = OVS_WIN_CONTROL_VERSION,
161 .maxAttr = OVS_WIN_CONTROL_ATTR_MAX,
162 .cmds = nlControlFamilyCmdOps,
163 .opsCount = ARRAY_SIZE(nlControlFamilyCmdOps)
166 /* Netlink datapath family. */
167 NETLINK_CMD nlDatapathFamilyCmdOps[] = {
168 { .cmd = OVS_DP_CMD_NEW,
169 .handler = OvsNewDpCmdHandler,
170 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
171 .validateDpIndex = FALSE
173 { .cmd = OVS_DP_CMD_GET,
174 .handler = OvsGetDpCmdHandler,
175 .supportedDevOp = OVS_WRITE_DEV_OP | OVS_READ_DEV_OP |
176 OVS_TRANSACTION_DEV_OP,
177 .validateDpIndex = FALSE
179 { .cmd = OVS_DP_CMD_SET,
180 .handler = OvsSetDpCmdHandler,
181 .supportedDevOp = OVS_WRITE_DEV_OP | OVS_READ_DEV_OP |
182 OVS_TRANSACTION_DEV_OP,
183 .validateDpIndex = TRUE
187 NETLINK_FAMILY nlDatapathFamilyOps = {
188 .name = OVS_DATAPATH_FAMILY,
189 .id = OVS_WIN_NL_DATAPATH_FAMILY_ID,
190 .version = OVS_DATAPATH_VERSION,
191 .maxAttr = OVS_DP_ATTR_MAX,
192 .cmds = nlDatapathFamilyCmdOps,
193 .opsCount = ARRAY_SIZE(nlDatapathFamilyCmdOps)
196 /* Netlink packet family. */
198 NETLINK_CMD nlPacketFamilyCmdOps[] = {
199 { .cmd = OVS_PACKET_CMD_EXECUTE,
200 .handler = OvsNlExecuteCmdHandler,
201 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
202 .validateDpIndex = TRUE
206 NETLINK_FAMILY nlPacketFamilyOps = {
207 .name = OVS_PACKET_FAMILY,
208 .id = OVS_WIN_NL_PACKET_FAMILY_ID,
209 .version = OVS_PACKET_VERSION,
210 .maxAttr = OVS_PACKET_ATTR_MAX,
211 .cmds = nlPacketFamilyCmdOps,
212 .opsCount = ARRAY_SIZE(nlPacketFamilyCmdOps)
215 /* Netlink vport family. */
216 NETLINK_CMD nlVportFamilyCmdOps[] = {
217 { .cmd = OVS_VPORT_CMD_GET,
218 .handler = OvsGetVportCmdHandler,
219 .supportedDevOp = OVS_WRITE_DEV_OP | OVS_READ_DEV_OP |
220 OVS_TRANSACTION_DEV_OP,
221 .validateDpIndex = TRUE
223 { .cmd = OVS_VPORT_CMD_NEW,
224 .handler = OvsNewVportCmdHandler,
225 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
226 .validateDpIndex = TRUE
228 { .cmd = OVS_VPORT_CMD_SET,
229 .handler = OvsSetVportCmdHandler,
230 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
231 .validateDpIndex = TRUE
233 { .cmd = OVS_VPORT_CMD_DEL,
234 .handler = OvsDeleteVportCmdHandler,
235 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
236 .validateDpIndex = TRUE
240 NETLINK_FAMILY nlVportFamilyOps = {
241 .name = OVS_VPORT_FAMILY,
242 .id = OVS_WIN_NL_VPORT_FAMILY_ID,
243 .version = OVS_VPORT_VERSION,
244 .maxAttr = OVS_VPORT_ATTR_MAX,
245 .cmds = nlVportFamilyCmdOps,
246 .opsCount = ARRAY_SIZE(nlVportFamilyCmdOps)
249 /* Netlink flow family. */
251 NETLINK_CMD nlFlowFamilyCmdOps[] = {
252 { .cmd = OVS_FLOW_CMD_NEW,
253 .handler = OvsFlowNlCmdHandler,
254 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
255 .validateDpIndex = TRUE
257 { .cmd = OVS_FLOW_CMD_SET,
258 .handler = OvsFlowNlCmdHandler,
259 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
260 .validateDpIndex = TRUE
262 { .cmd = OVS_FLOW_CMD_DEL,
263 .handler = OvsFlowNlCmdHandler,
264 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
265 .validateDpIndex = TRUE
267 { .cmd = OVS_FLOW_CMD_GET,
268 .handler = OvsFlowNlGetCmdHandler,
269 .supportedDevOp = OVS_TRANSACTION_DEV_OP |
270 OVS_WRITE_DEV_OP | OVS_READ_DEV_OP,
271 .validateDpIndex = TRUE
275 NETLINK_FAMILY nlFLowFamilyOps = {
276 .name = OVS_FLOW_FAMILY,
277 .id = OVS_WIN_NL_FLOW_FAMILY_ID,
278 .version = OVS_FLOW_VERSION,
279 .maxAttr = OVS_FLOW_ATTR_MAX,
280 .cmds = nlFlowFamilyCmdOps,
281 .opsCount = ARRAY_SIZE(nlFlowFamilyCmdOps)
284 /* Netlink netdev family. */
285 NETLINK_CMD nlNetdevFamilyCmdOps[] = {
286 { .cmd = OVS_WIN_NETDEV_CMD_GET,
287 .handler = OvsGetNetdevCmdHandler,
288 .supportedDevOp = OVS_TRANSACTION_DEV_OP,
289 .validateDpIndex = FALSE
293 NETLINK_FAMILY nlNetdevFamilyOps = {
294 .name = OVS_WIN_NETDEV_FAMILY,
295 .id = OVS_WIN_NL_NETDEV_FAMILY_ID,
296 .version = OVS_WIN_NETDEV_VERSION,
297 .maxAttr = OVS_WIN_NETDEV_ATTR_MAX,
298 .cmds = nlNetdevFamilyCmdOps,
299 .opsCount = ARRAY_SIZE(nlNetdevFamilyCmdOps)
302 static NTSTATUS MapIrpOutputBuffer(PIRP irp,
304 UINT32 requiredLength,
306 static NTSTATUS ValidateNetlinkCmd(UINT32 devOp,
307 POVS_OPEN_INSTANCE instance,
309 NETLINK_FAMILY *nlFamilyOps);
310 static NTSTATUS InvokeNetlinkCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
311 NETLINK_FAMILY *nlFamilyOps,
314 /* Handles to the device object for communication with userspace. */
315 NDIS_HANDLE gOvsDeviceHandle;
316 PDEVICE_OBJECT gOvsDeviceObject;
318 _Dispatch_type_(IRP_MJ_CREATE)
319 _Dispatch_type_(IRP_MJ_CLOSE)
320 DRIVER_DISPATCH OvsOpenCloseDevice;
322 _Dispatch_type_(IRP_MJ_CLEANUP)
323 DRIVER_DISPATCH OvsCleanupDevice;
325 _Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
326 DRIVER_DISPATCH OvsDeviceControl;
329 #pragma alloc_text(INIT, OvsCreateDeviceObject)
330 #pragma alloc_text(PAGE, OvsOpenCloseDevice)
331 #pragma alloc_text(PAGE, OvsCleanupDevice)
332 #pragma alloc_text(PAGE, OvsDeviceControl)
333 #endif // ALLOC_PRAGMA
336 * We might hit this limit easily since userspace opens a netlink descriptor for
337 * each thread, and at least one descriptor per vport. Revisit this later.
339 #define OVS_MAX_OPEN_INSTANCES 512
340 #define OVS_SYSTEM_DP_NAME "ovs-system"
342 POVS_OPEN_INSTANCE ovsOpenInstanceArray[OVS_MAX_OPEN_INSTANCES];
343 UINT32 ovsNumberOfOpenInstances;
344 extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
346 NDIS_SPIN_LOCK ovsCtrlLockObj;
347 PNDIS_SPIN_LOCK gOvsCtrlLock;
350 InitUserDumpState(POVS_OPEN_INSTANCE instance,
353 /* Clear the dumpState from a previous dump sequence. */
354 ASSERT(instance->dumpState.ovsMsg == NULL);
357 instance->dumpState.ovsMsg =
358 (POVS_MESSAGE)OvsAllocateMemoryWithTag(sizeof(OVS_MESSAGE),
359 OVS_DATAPATH_POOL_TAG);
360 if (instance->dumpState.ovsMsg == NULL) {
361 return STATUS_NO_MEMORY;
363 RtlCopyMemory(instance->dumpState.ovsMsg, ovsMsg,
364 sizeof *instance->dumpState.ovsMsg);
365 RtlZeroMemory(instance->dumpState.index,
366 sizeof instance->dumpState.index);
368 return STATUS_SUCCESS;
372 FreeUserDumpState(POVS_OPEN_INSTANCE instance)
374 if (instance->dumpState.ovsMsg != NULL) {
375 OvsFreeMemoryWithTag(instance->dumpState.ovsMsg,
376 OVS_DATAPATH_POOL_TAG);
377 RtlZeroMemory(&instance->dumpState, sizeof instance->dumpState);
384 gOvsCtrlLock = &ovsCtrlLockObj;
385 NdisAllocateSpinLock(gOvsCtrlLock);
392 OvsCleanupEventQueue();
394 NdisFreeSpinLock(gOvsCtrlLock);
402 NdisAcquireSpinLock(gOvsCtrlLock);
408 NdisReleaseSpinLock(gOvsCtrlLock);
413 * --------------------------------------------------------------------------
414 * Creates the communication device between user and kernel, and also
415 * initializes the data associated data structures.
416 * --------------------------------------------------------------------------
419 OvsCreateDeviceObject(NDIS_HANDLE ovsExtDriverHandle)
421 NDIS_STATUS status = NDIS_STATUS_SUCCESS;
422 UNICODE_STRING deviceName;
423 UNICODE_STRING symbolicDeviceName;
424 PDRIVER_DISPATCH dispatchTable[IRP_MJ_MAXIMUM_FUNCTION+1];
425 NDIS_DEVICE_OBJECT_ATTRIBUTES deviceAttributes;
426 OVS_LOG_TRACE("ovsExtDriverHandle: %p", ovsExtDriverHandle);
428 RtlZeroMemory(dispatchTable,
429 (IRP_MJ_MAXIMUM_FUNCTION + 1) * sizeof (PDRIVER_DISPATCH));
430 dispatchTable[IRP_MJ_CREATE] = OvsOpenCloseDevice;
431 dispatchTable[IRP_MJ_CLOSE] = OvsOpenCloseDevice;
432 dispatchTable[IRP_MJ_CLEANUP] = OvsCleanupDevice;
433 dispatchTable[IRP_MJ_DEVICE_CONTROL] = OvsDeviceControl;
435 NdisInitUnicodeString(&deviceName, OVS_DEVICE_NAME_NT);
436 NdisInitUnicodeString(&symbolicDeviceName, OVS_DEVICE_NAME_DOS);
438 RtlZeroMemory(&deviceAttributes, sizeof (NDIS_DEVICE_OBJECT_ATTRIBUTES));
440 OVS_INIT_OBJECT_HEADER(&deviceAttributes.Header,
441 NDIS_OBJECT_TYPE_DEVICE_OBJECT_ATTRIBUTES,
442 NDIS_DEVICE_OBJECT_ATTRIBUTES_REVISION_1,
443 sizeof (NDIS_DEVICE_OBJECT_ATTRIBUTES));
445 deviceAttributes.DeviceName = &deviceName;
446 deviceAttributes.SymbolicName = &symbolicDeviceName;
447 deviceAttributes.MajorFunctions = dispatchTable;
448 deviceAttributes.ExtensionSize = sizeof (OVS_DEVICE_EXTENSION);
450 status = NdisRegisterDeviceEx(ovsExtDriverHandle,
454 if (status != NDIS_STATUS_SUCCESS) {
455 POVS_DEVICE_EXTENSION ovsExt =
456 (POVS_DEVICE_EXTENSION)NdisGetDeviceReservedExtension(gOvsDeviceObject);
457 ASSERT(gOvsDeviceObject != NULL);
458 ASSERT(gOvsDeviceHandle != NULL);
461 ovsExt->numberOpenInstance = 0;
464 OvsRegisterSystemProvider((PVOID)gOvsDeviceObject);
467 OVS_LOG_TRACE("DeviceObject: %p", gOvsDeviceObject);
473 OvsDeleteDeviceObject()
475 if (gOvsDeviceHandle) {
477 POVS_DEVICE_EXTENSION ovsExt = (POVS_DEVICE_EXTENSION)
478 NdisGetDeviceReservedExtension(gOvsDeviceObject);
480 ASSERT(ovsExt->numberOpenInstance == 0);
484 ASSERT(gOvsDeviceObject);
485 NdisDeregisterDeviceEx(gOvsDeviceHandle);
486 gOvsDeviceHandle = NULL;
487 gOvsDeviceObject = NULL;
489 OvsUnregisterSystemProvider();
494 OvsGetOpenInstance(PFILE_OBJECT fileObject,
497 POVS_OPEN_INSTANCE instance = (POVS_OPEN_INSTANCE)fileObject->FsContext;
499 ASSERT(instance->fileObject == fileObject);
500 if (gOvsSwitchContext->dpNo != dpNo) {
508 OvsFindOpenInstance(PFILE_OBJECT fileObject)
511 for (i = 0, j = 0; i < OVS_MAX_OPEN_INSTANCES &&
512 j < ovsNumberOfOpenInstances; i++) {
513 if (ovsOpenInstanceArray[i]) {
514 if (ovsOpenInstanceArray[i]->fileObject == fileObject) {
515 return ovsOpenInstanceArray[i];
524 OvsAddOpenInstance(POVS_DEVICE_EXTENSION ovsExt,
525 PFILE_OBJECT fileObject)
527 POVS_OPEN_INSTANCE instance =
528 (POVS_OPEN_INSTANCE)OvsAllocateMemoryWithTag(sizeof(OVS_OPEN_INSTANCE),
529 OVS_DATAPATH_POOL_TAG);
532 if (instance == NULL) {
533 return STATUS_NO_MEMORY;
535 OvsAcquireCtrlLock();
536 ASSERT(OvsFindOpenInstance(fileObject) == NULL);
538 if (ovsNumberOfOpenInstances >= OVS_MAX_OPEN_INSTANCES) {
539 OvsReleaseCtrlLock();
540 OvsFreeMemoryWithTag(instance, OVS_DATAPATH_POOL_TAG);
541 return STATUS_INSUFFICIENT_RESOURCES;
543 RtlZeroMemory(instance, sizeof (OVS_OPEN_INSTANCE));
545 for (i = 0; i < OVS_MAX_OPEN_INSTANCES; i++) {
546 if (ovsOpenInstanceArray[i] == NULL) {
547 ovsOpenInstanceArray[i] = instance;
548 ovsNumberOfOpenInstances++;
549 instance->cookie = i;
553 ASSERT(i < OVS_MAX_OPEN_INSTANCES);
554 instance->fileObject = fileObject;
555 ASSERT(fileObject->FsContext == NULL);
556 instance->pid = (UINT32)InterlockedIncrement((LONG volatile *)&ovsExt->pidCount);
557 if (instance->pid == 0) {
558 /* XXX: check for rollover. */
560 fileObject->FsContext = instance;
561 OvsReleaseCtrlLock();
562 return STATUS_SUCCESS;
566 OvsCleanupOpenInstance(PFILE_OBJECT fileObject)
568 POVS_OPEN_INSTANCE instance = (POVS_OPEN_INSTANCE)fileObject->FsContext;
570 ASSERT(fileObject == instance->fileObject);
571 OvsCleanupEvent(instance);
572 OvsCleanupPacketQueue(instance);
576 OvsRemoveOpenInstance(PFILE_OBJECT fileObject)
578 POVS_OPEN_INSTANCE instance;
579 ASSERT(fileObject->FsContext);
580 instance = (POVS_OPEN_INSTANCE)fileObject->FsContext;
581 ASSERT(instance->cookie < OVS_MAX_OPEN_INSTANCES);
583 OvsAcquireCtrlLock();
584 fileObject->FsContext = NULL;
585 ASSERT(ovsOpenInstanceArray[instance->cookie] == instance);
586 ovsOpenInstanceArray[instance->cookie] = NULL;
587 ovsNumberOfOpenInstances--;
588 OvsReleaseCtrlLock();
589 ASSERT(instance->eventQueue == NULL);
590 ASSERT (instance->packetQueue == NULL);
591 OvsFreeMemoryWithTag(instance, OVS_DATAPATH_POOL_TAG);
595 OvsCompleteIrpRequest(PIRP irp,
599 irp->IoStatus.Information = infoPtr;
600 irp->IoStatus.Status = status;
601 IoCompleteRequest(irp, IO_NO_INCREMENT);
607 OvsOpenCloseDevice(PDEVICE_OBJECT deviceObject,
610 PIO_STACK_LOCATION irpSp;
611 NTSTATUS status = STATUS_SUCCESS;
612 PFILE_OBJECT fileObject;
613 POVS_DEVICE_EXTENSION ovsExt =
614 (POVS_DEVICE_EXTENSION)NdisGetDeviceReservedExtension(deviceObject);
616 ASSERT(deviceObject == gOvsDeviceObject);
617 ASSERT(ovsExt != NULL);
619 irpSp = IoGetCurrentIrpStackLocation(irp);
620 fileObject = irpSp->FileObject;
621 OVS_LOG_TRACE("DeviceObject: %p, fileObject:%p, instance: %u",
622 deviceObject, fileObject,
623 ovsExt->numberOpenInstance);
625 switch (irpSp->MajorFunction) {
627 status = OvsAddOpenInstance(ovsExt, fileObject);
628 if (STATUS_SUCCESS == status) {
629 InterlockedIncrement((LONG volatile *)&ovsExt->numberOpenInstance);
633 ASSERT(ovsExt->numberOpenInstance > 0);
634 OvsRemoveOpenInstance(fileObject);
635 InterlockedDecrement((LONG volatile *)&ovsExt->numberOpenInstance);
640 return OvsCompleteIrpRequest(irp, (ULONG_PTR)0, status);
643 _Use_decl_annotations_
645 OvsCleanupDevice(PDEVICE_OBJECT deviceObject,
649 PIO_STACK_LOCATION irpSp;
650 PFILE_OBJECT fileObject;
652 NTSTATUS status = STATUS_SUCCESS;
654 POVS_DEVICE_EXTENSION ovsExt =
655 (POVS_DEVICE_EXTENSION)NdisGetDeviceReservedExtension(deviceObject);
657 ASSERT(ovsExt->numberOpenInstance > 0);
660 UNREFERENCED_PARAMETER(deviceObject);
662 ASSERT(deviceObject == gOvsDeviceObject);
663 irpSp = IoGetCurrentIrpStackLocation(irp);
664 fileObject = irpSp->FileObject;
666 ASSERT(irpSp->MajorFunction == IRP_MJ_CLEANUP);
668 OvsCleanupOpenInstance(fileObject);
670 return OvsCompleteIrpRequest(irp, (ULONG_PTR)0, status);
675 * --------------------------------------------------------------------------
676 * IOCTL function handler for the device.
677 * --------------------------------------------------------------------------
680 OvsDeviceControl(PDEVICE_OBJECT deviceObject,
683 PIO_STACK_LOCATION irpSp;
684 NTSTATUS status = STATUS_SUCCESS;
685 PFILE_OBJECT fileObject;
686 PVOID inputBuffer = NULL;
687 PVOID outputBuffer = NULL;
688 UINT32 inputBufferLen, outputBufferLen;
689 UINT32 code, replyLen = 0;
690 POVS_OPEN_INSTANCE instance;
692 OVS_MESSAGE ovsMsgReadOp;
694 NETLINK_FAMILY *nlFamilyOps;
695 OVS_USER_PARAMS_CONTEXT usrParamsCtx;
698 POVS_DEVICE_EXTENSION ovsExt =
699 (POVS_DEVICE_EXTENSION)NdisGetDeviceReservedExtension(deviceObject);
700 ASSERT(deviceObject == gOvsDeviceObject);
702 ASSERT(ovsExt->numberOpenInstance > 0);
704 UNREFERENCED_PARAMETER(deviceObject);
707 irpSp = IoGetCurrentIrpStackLocation(irp);
709 ASSERT(irpSp->MajorFunction == IRP_MJ_DEVICE_CONTROL);
710 ASSERT(irpSp->FileObject != NULL);
712 fileObject = irpSp->FileObject;
713 instance = (POVS_OPEN_INSTANCE)fileObject->FsContext;
714 code = irpSp->Parameters.DeviceIoControl.IoControlCode;
715 inputBufferLen = irpSp->Parameters.DeviceIoControl.InputBufferLength;
716 outputBufferLen = irpSp->Parameters.DeviceIoControl.OutputBufferLength;
717 inputBuffer = irp->AssociatedIrp.SystemBuffer;
719 /* Check if the extension is enabled. */
720 if (NULL == gOvsSwitchContext) {
721 status = STATUS_NOT_FOUND;
725 if (!OvsAcquireSwitchContext()) {
726 status = STATUS_NOT_FOUND;
730 /* Concurrent netlink operations are not supported. */
731 if (InterlockedCompareExchange((LONG volatile *)&instance->inUse, 1, 0)) {
732 status = STATUS_RESOURCE_IN_USE;
737 * Validate the input/output buffer arguments depending on the type of the
741 case OVS_IOCTL_GET_PID:
742 /* Both input buffer and output buffer use the same location. */
743 outputBuffer = irp->AssociatedIrp.SystemBuffer;
744 if (outputBufferLen != 0) {
745 InitUserParamsCtx(irp, instance, 0, NULL,
746 inputBuffer, inputBufferLen,
747 outputBuffer, outputBufferLen,
750 ASSERT(outputBuffer);
752 status = STATUS_NDIS_INVALID_LENGTH;
756 status = OvsGetPidHandler(&usrParamsCtx, &replyLen);
759 case OVS_IOCTL_TRANSACT:
760 /* Both input buffer and output buffer are mandatory. */
761 if (outputBufferLen != 0) {
762 status = MapIrpOutputBuffer(irp, outputBufferLen,
763 sizeof *ovsMsg, &outputBuffer);
764 if (status != STATUS_SUCCESS) {
767 ASSERT(outputBuffer);
769 status = STATUS_NDIS_INVALID_LENGTH;
773 if (inputBufferLen < sizeof (*ovsMsg)) {
774 status = STATUS_NDIS_INVALID_LENGTH;
778 ovsMsg = inputBuffer;
779 devOp = OVS_TRANSACTION_DEV_OP;
782 case OVS_IOCTL_READ_EVENT:
783 case OVS_IOCTL_READ_PACKET:
785 * Output buffer is mandatory. These IOCTLs are used to read events and
786 * packets respectively. It is convenient to have separate ioctls.
788 if (outputBufferLen != 0) {
789 status = MapIrpOutputBuffer(irp, outputBufferLen,
790 sizeof *ovsMsg, &outputBuffer);
791 if (status != STATUS_SUCCESS) {
794 ASSERT(outputBuffer);
796 status = STATUS_NDIS_INVALID_LENGTH;
802 ovsMsg = &ovsMsgReadOp;
803 RtlZeroMemory(ovsMsg, sizeof *ovsMsg);
804 ovsMsg->nlMsg.nlmsgLen = sizeof *ovsMsg;
805 ovsMsg->nlMsg.nlmsgType = nlControlFamilyOps.id;
806 ovsMsg->nlMsg.nlmsgPid = instance->pid;
808 /* An "artificial" command so we can use NL family function table*/
809 ovsMsg->genlMsg.cmd = (code == OVS_IOCTL_READ_EVENT) ?
810 OVS_CTRL_CMD_EVENT_NOTIFY :
811 OVS_CTRL_CMD_READ_NOTIFY;
812 ovsMsg->genlMsg.version = nlControlFamilyOps.version;
814 devOp = OVS_READ_DEV_OP;
818 /* Output buffer is mandatory. */
819 if (outputBufferLen != 0) {
820 status = MapIrpOutputBuffer(irp, outputBufferLen,
821 sizeof *ovsMsg, &outputBuffer);
822 if (status != STATUS_SUCCESS) {
825 ASSERT(outputBuffer);
827 status = STATUS_NDIS_INVALID_LENGTH;
832 * Operate in the mode that read ioctl is similar to ReadFile(). This
833 * might change as the userspace code gets implemented.
839 * For implementing read (ioctl or otherwise), we need to store some
840 * state in the instance to indicate the command that started the dump
841 * operation. The state can setup 'ovsMsgReadOp' appropriately. Note
842 * that 'ovsMsgReadOp' is needed only in this function to call into the
843 * appropriate handler. The handler itself can access the state in the
846 * In the absence of a dump start, return 0 bytes.
848 if (instance->dumpState.ovsMsg == NULL) {
850 status = STATUS_SUCCESS;
853 RtlCopyMemory(&ovsMsgReadOp, instance->dumpState.ovsMsg,
854 sizeof (ovsMsgReadOp));
856 /* Create an NL message for consumption. */
857 ovsMsg = &ovsMsgReadOp;
858 devOp = OVS_READ_DEV_OP;
862 case OVS_IOCTL_WRITE:
863 /* Input buffer is mandatory. */
864 if (inputBufferLen < sizeof (*ovsMsg)) {
865 status = STATUS_NDIS_INVALID_LENGTH;
869 ovsMsg = inputBuffer;
870 devOp = OVS_WRITE_DEV_OP;
874 status = STATUS_INVALID_DEVICE_REQUEST;
879 switch (ovsMsg->nlMsg.nlmsgType) {
880 case OVS_WIN_NL_CTRL_FAMILY_ID:
881 nlFamilyOps = &nlControlFamilyOps;
883 case OVS_WIN_NL_DATAPATH_FAMILY_ID:
884 nlFamilyOps = &nlDatapathFamilyOps;
886 case OVS_WIN_NL_FLOW_FAMILY_ID:
887 nlFamilyOps = &nlFLowFamilyOps;
889 case OVS_WIN_NL_PACKET_FAMILY_ID:
890 nlFamilyOps = &nlPacketFamilyOps;
892 case OVS_WIN_NL_VPORT_FAMILY_ID:
893 nlFamilyOps = &nlVportFamilyOps;
895 case OVS_WIN_NL_NETDEV_FAMILY_ID:
896 nlFamilyOps = &nlNetdevFamilyOps;
899 status = STATUS_INVALID_PARAMETER;
904 * For read operation, avoid duplicate validation since 'ovsMsg' is either
905 * "artificial" or was copied from a previously validated 'ovsMsg'.
907 if (devOp != OVS_READ_DEV_OP) {
908 status = ValidateNetlinkCmd(devOp, instance, ovsMsg, nlFamilyOps);
909 if (status != STATUS_SUCCESS) {
914 InitUserParamsCtx(irp, instance, devOp, ovsMsg,
915 inputBuffer, inputBufferLen,
916 outputBuffer, outputBufferLen,
919 status = InvokeNetlinkCmdHandler(&usrParamsCtx, nlFamilyOps, &replyLen);
922 OvsReleaseSwitchContext(gOvsSwitchContext);
928 /* Should not complete a pending IRP unless proceesing is completed */
929 if (status == STATUS_PENDING) {
932 return OvsCompleteIrpRequest(irp, (ULONG_PTR)replyLen, status);
937 * --------------------------------------------------------------------------
938 * Function to validate a netlink command. Only certain combinations of
939 * (device operation, netlink family, command) are valid.
940 * --------------------------------------------------------------------------
943 ValidateNetlinkCmd(UINT32 devOp,
944 POVS_OPEN_INSTANCE instance,
946 NETLINK_FAMILY *nlFamilyOps)
948 NTSTATUS status = STATUS_INVALID_PARAMETER;
951 for (i = 0; i < nlFamilyOps->opsCount; i++) {
952 if (nlFamilyOps->cmds[i].cmd == ovsMsg->genlMsg.cmd) {
953 /* Validate if the command is valid for the device operation. */
954 if ((devOp & nlFamilyOps->cmds[i].supportedDevOp) == 0) {
955 status = STATUS_INVALID_PARAMETER;
959 /* Validate the version. */
960 if (nlFamilyOps->version > ovsMsg->genlMsg.version) {
961 status = STATUS_INVALID_PARAMETER;
965 /* Validate the DP for commands that require a DP. */
966 if (nlFamilyOps->cmds[i].validateDpIndex == TRUE) {
967 if (ovsMsg->ovsHdr.dp_ifindex !=
968 (INT)gOvsSwitchContext->dpNo) {
969 status = STATUS_INVALID_PARAMETER;
974 /* Validate the PID. */
975 if (ovsMsg->nlMsg.nlmsgPid != instance->pid) {
976 status = STATUS_INVALID_PARAMETER;
980 status = STATUS_SUCCESS;
990 * --------------------------------------------------------------------------
991 * Function to invoke the netlink command handler. The function also stores
992 * the return value of the handler function to construct a 'NL_ERROR' message,
993 * and in turn returns success to the caller.
994 * --------------------------------------------------------------------------
997 InvokeNetlinkCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
998 NETLINK_FAMILY *nlFamilyOps,
1001 NTSTATUS status = STATUS_INVALID_PARAMETER;
1004 for (i = 0; i < nlFamilyOps->opsCount; i++) {
1005 if (nlFamilyOps->cmds[i].cmd == usrParamsCtx->ovsMsg->genlMsg.cmd) {
1006 NetlinkCmdHandler *handler = nlFamilyOps->cmds[i].handler;
1009 status = handler(usrParamsCtx, replyLen);
1016 * Netlink socket semantics dictate that the return value of the netlink
1017 * function should be an error ONLY under fatal conditions. If the message
1018 * made it all the way to the handler function, it is not a fatal condition.
1019 * Absorb the error returned by the handler function into a 'struct
1020 * NL_ERROR' and populate the 'output buffer' to return to userspace.
1022 * This behavior is obviously applicable only to netlink commands that
1023 * specify an 'output buffer'. For other commands, we return the error as
1026 * 'STATUS_PENDING' is a special return value and userspace is equipped to
1029 if (status != STATUS_SUCCESS && status != STATUS_PENDING) {
1030 if (usrParamsCtx->devOp != OVS_WRITE_DEV_OP && *replyLen == 0) {
1031 NL_ERROR nlError = NlMapStatusToNlErr(status);
1032 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1033 POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR)
1034 usrParamsCtx->outputBuffer;
1037 NlBuildErrorMsg(msgIn, msgError, nlError);
1038 *replyLen = msgError->nlMsg.nlmsgLen;
1041 if (*replyLen != 0) {
1042 status = STATUS_SUCCESS;
1047 if (usrParamsCtx->devOp != OVS_WRITE_DEV_OP) {
1048 ASSERT(status == STATUS_PENDING || *replyLen != 0 || status == STATUS_SUCCESS);
1056 * --------------------------------------------------------------------------
1057 * Handler for 'OVS_IOCTL_GET_PID'.
1059 * Each handle on the device is assigned a unique PID when the handle is
1060 * created. This function passes the PID to userspace using METHOD_BUFFERED
1062 * --------------------------------------------------------------------------
1065 OvsGetPidHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1068 NTSTATUS status = STATUS_SUCCESS;
1069 PUINT32 msgOut = (PUINT32)usrParamsCtx->outputBuffer;
1071 if (usrParamsCtx->outputLength >= sizeof *msgOut) {
1072 POVS_OPEN_INSTANCE instance =
1073 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1075 RtlZeroMemory(msgOut, sizeof *msgOut);
1076 RtlCopyMemory(msgOut, &instance->pid, sizeof(*msgOut));
1077 *replyLen = sizeof *msgOut;
1079 *replyLen = sizeof *msgOut;
1080 status = STATUS_NDIS_INVALID_LENGTH;
1087 * --------------------------------------------------------------------------
1088 * Utility function to fill up information about the datapath in a reply to
1090 * --------------------------------------------------------------------------
1093 OvsDpFillInfo(POVS_SWITCH_CONTEXT ovsSwitchContext,
1098 OVS_MESSAGE msgOutTmp;
1099 OVS_DATAPATH *datapath = &ovsSwitchContext->datapath;
1102 ASSERT(NlBufAt(nlBuf, 0, 0) != 0 && NlBufRemLen(nlBuf) >= sizeof *msgIn);
1104 msgOutTmp.nlMsg.nlmsgType = OVS_WIN_NL_DATAPATH_FAMILY_ID;
1105 msgOutTmp.nlMsg.nlmsgFlags = 0; /* XXX: ? */
1106 msgOutTmp.nlMsg.nlmsgSeq = msgIn->nlMsg.nlmsgSeq;
1107 msgOutTmp.nlMsg.nlmsgPid = msgIn->nlMsg.nlmsgPid;
1109 msgOutTmp.genlMsg.cmd = OVS_DP_CMD_GET;
1110 msgOutTmp.genlMsg.version = nlDatapathFamilyOps.version;
1111 msgOutTmp.genlMsg.reserved = 0;
1113 msgOutTmp.ovsHdr.dp_ifindex = ovsSwitchContext->dpNo;
1115 writeOk = NlMsgPutHead(nlBuf, (PCHAR)&msgOutTmp, sizeof msgOutTmp);
1117 writeOk = NlMsgPutTailString(nlBuf, OVS_DP_ATTR_NAME,
1118 OVS_SYSTEM_DP_NAME);
1121 OVS_DP_STATS dpStats;
1123 dpStats.n_hit = datapath->hits;
1124 dpStats.n_missed = datapath->misses;
1125 dpStats.n_lost = datapath->lost;
1126 dpStats.n_flows = datapath->nFlows;
1127 writeOk = NlMsgPutTailUnspec(nlBuf, OVS_DP_ATTR_STATS,
1128 (PCHAR)&dpStats, sizeof dpStats);
1130 nlMsg = (PNL_MSG_HDR)NlBufAt(nlBuf, 0, 0);
1131 nlMsg->nlmsgLen = NlBufSize(nlBuf);
1133 return writeOk ? STATUS_SUCCESS : STATUS_INVALID_BUFFER_SIZE;
1137 * --------------------------------------------------------------------------
1138 * Handler for queueing an IRP used for event notification. The IRP is
1139 * completed when a port state changes. STATUS_PENDING is returned on
1140 * success. User mode keep a pending IRP at all times.
1141 * --------------------------------------------------------------------------
1144 OvsPendEventCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1149 UNREFERENCED_PARAMETER(replyLen);
1151 POVS_OPEN_INSTANCE instance =
1152 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1153 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1154 OVS_EVENT_POLL poll;
1156 poll.dpNo = msgIn->ovsHdr.dp_ifindex;
1157 status = OvsWaitEventIoctl(usrParamsCtx->irp, instance->fileObject,
1158 &poll, sizeof poll);
1163 * --------------------------------------------------------------------------
1164 * Handler for the subscription for the event queue
1165 * --------------------------------------------------------------------------
1168 OvsSubscribeEventCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1172 OVS_EVENT_SUBSCRIBE request;
1176 const NL_POLICY policy[] = {
1177 [OVS_NL_ATTR_MCAST_GRP] = {.type = NL_A_U32 },
1178 [OVS_NL_ATTR_MCAST_JOIN] = {.type = NL_A_U8 },
1181 UNREFERENCED_PARAMETER(replyLen);
1183 POVS_OPEN_INSTANCE instance =
1184 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1185 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1187 rc = NlAttrParse(&msgIn->nlMsg, sizeof (*msgIn),
1188 NlMsgAttrsLen((PNL_MSG_HDR)msgIn), policy, attrs, ARRAY_SIZE(attrs));
1190 status = STATUS_INVALID_PARAMETER;
1194 /* XXX Ignore the MC group for now */
1195 join = NlAttrGetU8(attrs[OVS_NL_ATTR_MCAST_JOIN]);
1196 request.dpNo = msgIn->ovsHdr.dp_ifindex;
1197 request.subscribe = join;
1198 request.mask = OVS_EVENT_MASK_ALL;
1200 status = OvsSubscribeEventIoctl(instance->fileObject, &request,
1207 * --------------------------------------------------------------------------
1208 * Command Handler for 'OVS_DP_CMD_NEW'.
1209 * --------------------------------------------------------------------------
1212 OvsNewDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1215 return HandleDpTransactionCommon(usrParamsCtx, replyLen);
1219 * --------------------------------------------------------------------------
1220 * Command Handler for 'OVS_DP_CMD_GET'.
1222 * The function handles both the dump based as well as the transaction based
1223 * 'OVS_DP_CMD_GET' command. In the dump command, it handles the initial
1224 * call to setup dump state, as well as subsequent calls to continue dumping
1226 * --------------------------------------------------------------------------
1229 OvsGetDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1232 if (usrParamsCtx->devOp == OVS_TRANSACTION_DEV_OP) {
1233 return HandleDpTransactionCommon(usrParamsCtx, replyLen);
1235 return HandleGetDpDump(usrParamsCtx, replyLen);
1240 * --------------------------------------------------------------------------
1241 * Function for handling the transaction based 'OVS_DP_CMD_GET' command.
1242 * --------------------------------------------------------------------------
1245 HandleGetDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1248 return HandleDpTransactionCommon(usrParamsCtx, replyLen);
1253 * --------------------------------------------------------------------------
1254 * Function for handling the dump-based 'OVS_DP_CMD_GET' command.
1255 * --------------------------------------------------------------------------
1258 HandleGetDpDump(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1261 POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
1262 POVS_OPEN_INSTANCE instance =
1263 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1265 if (usrParamsCtx->devOp == OVS_WRITE_DEV_OP) {
1267 OvsSetupDumpStart(usrParamsCtx);
1271 POVS_MESSAGE msgIn = instance->dumpState.ovsMsg;
1273 ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
1275 if (instance->dumpState.ovsMsg == NULL) {
1277 return STATUS_INVALID_DEVICE_STATE;
1280 /* Dump state must have been deleted after previous dump operation. */
1281 ASSERT(instance->dumpState.index[0] == 0);
1283 /* Output buffer has been validated while validating read dev op. */
1284 ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
1286 NlBufInit(&nlBuf, usrParamsCtx->outputBuffer,
1287 usrParamsCtx->outputLength);
1289 status = OvsDpFillInfo(gOvsSwitchContext, msgIn, &nlBuf);
1291 if (status != STATUS_SUCCESS) {
1293 FreeUserDumpState(instance);
1297 /* Increment the dump index. */
1298 instance->dumpState.index[0] = 1;
1299 *replyLen = msgOut->nlMsg.nlmsgLen;
1301 /* Free up the dump state, since there's no more data to continue. */
1302 FreeUserDumpState(instance);
1305 return STATUS_SUCCESS;
1310 * --------------------------------------------------------------------------
1311 * Command Handler for 'OVS_DP_CMD_SET'.
1312 * --------------------------------------------------------------------------
1315 OvsSetDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1318 return HandleDpTransactionCommon(usrParamsCtx, replyLen);
1322 * --------------------------------------------------------------------------
1323 * Function for handling transaction based 'OVS_DP_CMD_NEW', 'OVS_DP_CMD_GET'
1324 * and 'OVS_DP_CMD_SET' commands.
1326 * 'OVS_DP_CMD_NEW' is implemented to keep userspace code happy. Creation of a
1327 * new datapath is not supported currently.
1328 * --------------------------------------------------------------------------
1331 HandleDpTransactionCommon(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1334 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1335 POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
1336 NTSTATUS status = STATUS_SUCCESS;
1338 NL_ERROR nlError = NL_ERROR_SUCCESS;
1339 static const NL_POLICY ovsDatapathSetPolicy[] = {
1340 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .maxLen = IFNAMSIZ },
1341 [OVS_DP_ATTR_UPCALL_PID] = { .type = NL_A_U32, .optional = TRUE },
1342 [OVS_DP_ATTR_USER_FEATURES] = { .type = NL_A_U32, .optional = TRUE },
1344 PNL_ATTR dpAttrs[ARRAY_SIZE(ovsDatapathSetPolicy)];
1346 UNREFERENCED_PARAMETER(msgOut);
1348 /* input buffer has been validated while validating write dev op. */
1349 ASSERT(msgIn != NULL && usrParamsCtx->inputLength >= sizeof *msgIn);
1351 /* Parse any attributes in the request. */
1352 if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_SET ||
1353 usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_NEW) {
1354 if (!NlAttrParse((PNL_MSG_HDR)msgIn,
1355 NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN,
1356 NlMsgAttrsLen((PNL_MSG_HDR)msgIn),
1357 ovsDatapathSetPolicy, dpAttrs, ARRAY_SIZE(dpAttrs))) {
1358 return STATUS_INVALID_PARAMETER;
1362 * XXX: Not clear at this stage if there's any role for the
1363 * OVS_DP_ATTR_UPCALL_PID and OVS_DP_ATTR_USER_FEATURES attributes passed
1368 RtlZeroMemory(dpAttrs, sizeof dpAttrs);
1371 /* Output buffer has been validated while validating transact dev op. */
1372 ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
1374 NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength);
1376 if (dpAttrs[OVS_DP_ATTR_NAME] != NULL) {
1377 if (!OvsCompareString(NlAttrGet(dpAttrs[OVS_DP_ATTR_NAME]),
1378 OVS_SYSTEM_DP_NAME)) {
1380 /* Creation of new datapaths is not supported. */
1381 if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_SET) {
1382 nlError = NL_ERROR_NOTSUPP;
1386 nlError = NL_ERROR_NODEV;
1389 } else if ((UINT32)msgIn->ovsHdr.dp_ifindex != gOvsSwitchContext->dpNo) {
1390 nlError = NL_ERROR_NODEV;
1394 if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_NEW) {
1395 nlError = NL_ERROR_EXIST;
1399 status = OvsDpFillInfo(gOvsSwitchContext, msgIn, &nlBuf);
1401 *replyLen = NlBufSize(&nlBuf);
1404 if (nlError != NL_ERROR_SUCCESS) {
1405 POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR)
1406 usrParamsCtx->outputBuffer;
1408 NlBuildErrorMsg(msgIn, msgError, nlError);
1409 *replyLen = msgError->nlMsg.nlmsgLen;
1412 return STATUS_SUCCESS;
1417 OvsSetupDumpStart(POVS_USER_PARAMS_CONTEXT usrParamsCtx)
1419 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1420 POVS_OPEN_INSTANCE instance =
1421 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1423 /* input buffer has been validated while validating write dev op. */
1424 ASSERT(msgIn != NULL && usrParamsCtx->inputLength >= sizeof *msgIn);
1426 /* A write operation that does not indicate dump start is invalid. */
1427 if ((msgIn->nlMsg.nlmsgFlags & NLM_F_DUMP) != NLM_F_DUMP) {
1428 return STATUS_INVALID_PARAMETER;
1430 /* XXX: Handle other NLM_F_* flags in the future. */
1433 * This operation should be setting up the dump state. If there's any
1434 * previous state, clear it up so as to set it up afresh.
1436 FreeUserDumpState(instance);
1438 return InitUserDumpState(instance, msgIn);
1443 * --------------------------------------------------------------------------
1444 * Utility function to map the output buffer in an IRP. The buffer is assumed
1445 * to have been passed down using METHOD_OUT_DIRECT (Direct I/O).
1446 * --------------------------------------------------------------------------
1449 MapIrpOutputBuffer(PIRP irp,
1450 UINT32 bufferLength,
1451 UINT32 requiredLength,
1456 ASSERT(bufferLength);
1457 ASSERT(requiredLength);
1458 if (!buffer || !irp || bufferLength == 0 || requiredLength == 0) {
1459 return STATUS_INVALID_PARAMETER;
1462 if (bufferLength < requiredLength) {
1463 return STATUS_NDIS_INVALID_LENGTH;
1465 if (irp->MdlAddress == NULL) {
1466 return STATUS_INVALID_PARAMETER;
1468 *buffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress,
1469 NormalPagePriority);
1470 if (*buffer == NULL) {
1471 return STATUS_INSUFFICIENT_RESOURCES;
1474 return STATUS_SUCCESS;
1478 * --------------------------------------------------------------------------
1479 * Utility function to fill up information about the state of a port in a reply
1481 * --------------------------------------------------------------------------
1484 OvsPortFillInfo(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1485 POVS_EVENT_ENTRY eventEntry,
1490 OVS_MESSAGE msgOutTmp;
1492 POVS_VPORT_ENTRY vport;
1494 ASSERT(NlBufAt(nlBuf, 0, 0) != 0 && nlBuf->bufRemLen >= sizeof msgOutTmp);
1496 msgOutTmp.nlMsg.nlmsgType = OVS_WIN_NL_VPORT_FAMILY_ID;
1497 msgOutTmp.nlMsg.nlmsgFlags = 0; /* XXX: ? */
1499 /* driver intiated messages should have zerp seq number*/
1500 msgOutTmp.nlMsg.nlmsgSeq = 0;
1501 msgOutTmp.nlMsg.nlmsgPid = usrParamsCtx->ovsInstance->pid;
1503 msgOutTmp.genlMsg.version = nlVportFamilyOps.version;
1504 msgOutTmp.genlMsg.reserved = 0;
1506 /* we don't have netdev yet, treat link up/down a adding/removing a port*/
1507 if (eventEntry->status & (OVS_EVENT_LINK_UP | OVS_EVENT_CONNECT)) {
1508 msgOutTmp.genlMsg.cmd = OVS_VPORT_CMD_NEW;
1509 } else if (eventEntry->status &
1510 (OVS_EVENT_LINK_DOWN | OVS_EVENT_DISCONNECT)) {
1511 msgOutTmp.genlMsg.cmd = OVS_VPORT_CMD_DEL;
1514 return STATUS_UNSUCCESSFUL;
1516 msgOutTmp.ovsHdr.dp_ifindex = gOvsSwitchContext->dpNo;
1518 ok = NlMsgPutHead(nlBuf, (PCHAR)&msgOutTmp, sizeof msgOutTmp);
1520 status = STATUS_INVALID_BUFFER_SIZE;
1524 vport = OvsFindVportByPortNo(gOvsSwitchContext, eventEntry->portNo);
1526 status = STATUS_DEVICE_DOES_NOT_EXIST;
1530 ok = NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_PORT_NO, eventEntry->portNo) &&
1531 NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_TYPE, vport->ovsType) &&
1532 NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_UPCALL_PID,
1533 vport->upcallPid) &&
1534 NlMsgPutTailString(nlBuf, OVS_VPORT_ATTR_NAME, vport->ovsName);
1536 status = STATUS_INVALID_BUFFER_SIZE;
1540 /* XXXX Should we add the port stats attributes?*/
1541 nlMsg = (PNL_MSG_HDR)NlBufAt(nlBuf, 0, 0);
1542 nlMsg->nlmsgLen = NlBufSize(nlBuf);
1543 status = STATUS_SUCCESS;
1551 * --------------------------------------------------------------------------
1552 * Handler for reading events from the driver event queue. This handler is
1553 * executed when user modes issues a socket receive on a socket assocaited
1554 * with the MC group for events.
1555 * XXX user mode should read multiple events in one system call
1556 * --------------------------------------------------------------------------
1559 OvsReadEventCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1563 POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
1564 POVS_OPEN_INSTANCE instance =
1565 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1569 OVS_EVENT_ENTRY eventEntry;
1571 ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
1573 /* Should never read events with a dump socket */
1574 ASSERT(instance->dumpState.ovsMsg == NULL);
1576 /* Must have an event queue */
1577 ASSERT(instance->eventQueue != NULL);
1579 /* Output buffer has been validated while validating read dev op. */
1580 ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
1582 NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength);
1584 /* remove an event entry from the event queue */
1585 status = OvsRemoveEventEntry(usrParamsCtx->ovsInstance, &eventEntry);
1586 if (status != STATUS_SUCCESS) {
1587 /* If there were not elements, read should return no data. */
1588 status = STATUS_SUCCESS;
1593 status = OvsPortFillInfo(usrParamsCtx, &eventEntry, &nlBuf);
1594 if (status == NDIS_STATUS_SUCCESS) {
1595 *replyLen = NlBufSize(&nlBuf);
1603 * --------------------------------------------------------------------------
1604 * Handler for reading missed pacckets from the driver event queue. This
1605 * handler is executed when user modes issues a socket receive on a socket
1606 * --------------------------------------------------------------------------
1609 OvsReadPacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1613 POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
1615 POVS_OPEN_INSTANCE instance =
1616 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1619 ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
1621 /* Should never read events with a dump socket */
1622 ASSERT(instance->dumpState.ovsMsg == NULL);
1624 /* Must have an packet queue */
1625 ASSERT(instance->packetQueue != NULL);
1627 /* Output buffer has been validated while validating read dev op. */
1628 ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
1630 /* Read a packet from the instance queue */
1631 status = OvsReadDpIoctl(instance->fileObject, usrParamsCtx->outputBuffer,
1632 usrParamsCtx->outputLength, replyLen);
1637 * --------------------------------------------------------------------------
1638 * Handler for the subscription for a packet queue
1639 * --------------------------------------------------------------------------
1642 OvsSubscribePacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1649 const NL_POLICY policy[] = {
1650 [OVS_NL_ATTR_PACKET_PID] = {.type = NL_A_U32 },
1651 [OVS_NL_ATTR_PACKET_SUBSCRIBE] = {.type = NL_A_U8 }
1653 PNL_ATTR attrs[ARRAY_SIZE(policy)];
1655 UNREFERENCED_PARAMETER(replyLen);
1657 POVS_OPEN_INSTANCE instance =
1658 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1659 POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
1661 rc = NlAttrParse(&msgIn->nlMsg, sizeof (*msgIn),
1662 NlMsgAttrsLen((PNL_MSG_HDR)msgIn), policy, attrs, ARRAY_SIZE(attrs));
1664 status = STATUS_INVALID_PARAMETER;
1668 join = NlAttrGetU8(attrs[OVS_NL_ATTR_PACKET_PID]);
1669 pid = NlAttrGetU32(attrs[OVS_NL_ATTR_PACKET_PID]);
1671 /* The socket subscribed with must be the same socket we perform receive*/
1672 ASSERT(pid == instance->pid);
1674 status = OvsSubscribeDpIoctl(instance, pid, join);
1677 * XXX Need to add this instance to a global data structure
1678 * which hold all packet based instances. The data structure (hash)
1679 * should be searched through the pid field of the instance for
1680 * placing the missed packet into the correct queue
1687 * --------------------------------------------------------------------------
1688 * Handler for queueing an IRP used for missed packet notification. The IRP is
1689 * completed when a packet received and mismatched. STATUS_PENDING is returned
1690 * on success. User mode keep a pending IRP at all times.
1691 * --------------------------------------------------------------------------
1694 OvsPendPacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
1697 UNREFERENCED_PARAMETER(replyLen);
1699 POVS_OPEN_INSTANCE instance =
1700 (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
1703 * XXX access to packet queue must be through acquiring a lock as user mode
1704 * could unsubscribe and the instnace will be freed.
1706 return OvsWaitDpIoctl(usrParamsCtx->irp, instance->fileObject);