datapath-windows: Allow NdisSwitchPortTypeSynthetic to be updated
[cascardo/ovs.git] / datapath-windows / ovsext / Datapath.c
index e2d89a6..e566ea9 100644 (file)
  * OVS_USE_NL_INTERFACE = 0 => legacy inteface to use with dpif-windows.c
  * OVS_USE_NL_INTERFACE = 1 => netlink inteface to use with ported dpif-linux.c
  */
-#if defined OVS_USE_NL_INTERFACE && OVS_USE_NL_INTERFACE == 1
 
 #include "precomp.h"
+#include "Switch.h"
+#include "User.h"
 #include "Datapath.h"
 #include "Jhash.h"
-#include "Switch.h"
 #include "Vport.h"
 #include "Event.h"
 #include "User.h"
@@ -33,6 +33,7 @@
 #include "NetProto.h"
 #include "Flow.h"
 #include "User.h"
+#include "Vxlan.h"
 
 #ifdef OVS_DBG_MOD
 #undef OVS_DBG_MOD
@@ -86,19 +87,29 @@ typedef struct _NETLINK_FAMILY {
 } NETLINK_FAMILY, *PNETLINK_FAMILY;
 
 /* Handlers for the various netlink commands. */
-static NetlinkCmdHandler OvsGetPidCmdHandler,
-                         OvsGetDpCmdHandler,
-                         OvsPendEventCmdHandler,
+static NetlinkCmdHandler OvsPendEventCmdHandler,
+                         OvsPendPacketCmdHandler,
                          OvsSubscribeEventCmdHandler,
-                         OvsSetDpCmdHandler,
+                         OvsSubscribePacketCmdHandler,
                          OvsReadEventCmdHandler,
-                         OvsGetVportCmdHandler;
+                         OvsReadPacketCmdHandler,
+                         OvsNewDpCmdHandler,
+                         OvsGetDpCmdHandler,
+                         OvsSetDpCmdHandler;
+
+NetlinkCmdHandler        OvsGetNetdevCmdHandler,
+                         OvsGetVportCmdHandler,
+                         OvsSetVportCmdHandler,
+                         OvsNewVportCmdHandler,
+                         OvsDeleteVportCmdHandler;
 
 static NTSTATUS HandleGetDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                                        UINT32 *replyLen);
 static NTSTATUS HandleGetDpDump(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                                 UINT32 *replyLen);
-static NTSTATUS HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+static NTSTATUS HandleDpTransactionCommon(
+                    POVS_USER_PARAMS_CONTEXT usrParamsCtx, UINT32 *replyLen);
+static NTSTATUS OvsGetPidHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                                     UINT32 *replyLen);
 
 /*
@@ -110,25 +121,35 @@ static NTSTATUS HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
 
 /* Netlink control family: this is a Windows specific family. */
 NETLINK_CMD nlControlFamilyCmdOps[] = {
-    { .cmd             = OVS_CTRL_CMD_WIN_GET_PID,
-      .handler         = OvsGetPidCmdHandler,
-      .supportedDevOp  = OVS_TRANSACTION_DEV_OP,
-      .validateDpIndex = FALSE,
-    },
     { .cmd = OVS_CTRL_CMD_WIN_PEND_REQ,
       .handler = OvsPendEventCmdHandler,
       .supportedDevOp = OVS_WRITE_DEV_OP,
       .validateDpIndex = TRUE,
     },
+    { .cmd = OVS_CTRL_CMD_WIN_PEND_PACKET_REQ,
+      .handler = OvsPendPacketCmdHandler,
+      .supportedDevOp = OVS_WRITE_DEV_OP,
+      .validateDpIndex = TRUE,
+    },
     { .cmd = OVS_CTRL_CMD_MC_SUBSCRIBE_REQ,
       .handler = OvsSubscribeEventCmdHandler,
       .supportedDevOp = OVS_WRITE_DEV_OP,
       .validateDpIndex = TRUE,
     },
+    { .cmd = OVS_CTRL_CMD_PACKET_SUBSCRIBE_REQ,
+      .handler = OvsSubscribePacketCmdHandler,
+      .supportedDevOp = OVS_WRITE_DEV_OP,
+      .validateDpIndex = TRUE,
+    },
     { .cmd = OVS_CTRL_CMD_EVENT_NOTIFY,
       .handler = OvsReadEventCmdHandler,
       .supportedDevOp = OVS_READ_EVENT_DEV_OP,
       .validateDpIndex = FALSE,
+    },
+    { .cmd = OVS_CTRL_CMD_READ_NOTIFY,
+      .handler = OvsReadPacketCmdHandler,
+      .supportedDevOp = OVS_READ_PACKET_DEV_OP,
+      .validateDpIndex = FALSE,
     }
 };
 
@@ -143,6 +164,11 @@ NETLINK_FAMILY nlControlFamilyOps = {
 
 /* Netlink datapath family. */
 NETLINK_CMD nlDatapathFamilyCmdOps[] = {
+    { .cmd             = OVS_DP_CMD_NEW,
+      .handler         = OvsNewDpCmdHandler,
+      .supportedDevOp  = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = FALSE
+    },
     { .cmd             = OVS_DP_CMD_GET,
       .handler         = OvsGetDpCmdHandler,
       .supportedDevOp  = OVS_WRITE_DEV_OP | OVS_READ_DEV_OP |
@@ -167,14 +193,22 @@ NETLINK_FAMILY nlDatapathFamilyOps = {
 };
 
 /* Netlink packet family. */
-/* XXX: Add commands here. */
+
+NETLINK_CMD nlPacketFamilyCmdOps[] = {
+    { .cmd             = OVS_PACKET_CMD_EXECUTE,
+      .handler         = OvsNlExecuteCmdHandler,
+      .supportedDevOp  = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = TRUE
+    }
+};
+
 NETLINK_FAMILY nlPacketFamilyOps = {
     .name     = OVS_PACKET_FAMILY,
     .id       = OVS_WIN_NL_PACKET_FAMILY_ID,
     .version  = OVS_PACKET_VERSION,
     .maxAttr  = OVS_PACKET_ATTR_MAX,
-    .cmds     = NULL, /* XXX: placeholder. */
-    .opsCount = 0
+    .cmds     = nlPacketFamilyCmdOps,
+    .opsCount = ARRAY_SIZE(nlPacketFamilyCmdOps)
 };
 
 /* Netlink vport family. */
@@ -184,7 +218,22 @@ NETLINK_CMD nlVportFamilyCmdOps[] = {
       .supportedDevOp = OVS_WRITE_DEV_OP | OVS_READ_DEV_OP |
                         OVS_TRANSACTION_DEV_OP,
       .validateDpIndex = TRUE
-    }
+    },
+    { .cmd = OVS_VPORT_CMD_NEW,
+      .handler = OvsNewVportCmdHandler,
+      .supportedDevOp = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = TRUE
+    },
+    { .cmd = OVS_VPORT_CMD_SET,
+      .handler = OvsSetVportCmdHandler,
+      .supportedDevOp = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = TRUE
+    },
+    { .cmd = OVS_VPORT_CMD_DEL,
+      .handler = OvsDeleteVportCmdHandler,
+      .supportedDevOp = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = TRUE
+    },
 };
 
 NETLINK_FAMILY nlVportFamilyOps = {
@@ -231,6 +280,24 @@ NETLINK_FAMILY nlFLowFamilyOps = {
     .opsCount = ARRAY_SIZE(nlFlowFamilyCmdOps)
 };
 
+/* Netlink netdev family. */
+NETLINK_CMD nlNetdevFamilyCmdOps[] = {
+    { .cmd = OVS_WIN_NETDEV_CMD_GET,
+      .handler = OvsGetNetdevCmdHandler,
+      .supportedDevOp = OVS_TRANSACTION_DEV_OP,
+      .validateDpIndex = FALSE
+    },
+};
+
+NETLINK_FAMILY nlNetdevFamilyOps = {
+    .name     = OVS_WIN_NETDEV_FAMILY,
+    .id       = OVS_WIN_NL_NETDEV_FAMILY_ID,
+    .version  = OVS_WIN_NETDEV_VERSION,
+    .maxAttr  = OVS_WIN_NETDEV_ATTR_MAX,
+    .cmds     = nlNetdevFamilyCmdOps,
+    .opsCount = ARRAY_SIZE(nlNetdevFamilyCmdOps)
+};
+
 static NTSTATUS MapIrpOutputBuffer(PIRP irp,
                                    UINT32 bufferLength,
                                    UINT32 requiredLength,
@@ -278,6 +345,37 @@ extern POVS_SWITCH_CONTEXT gOvsSwitchContext;
 NDIS_SPIN_LOCK ovsCtrlLockObj;
 PNDIS_SPIN_LOCK gOvsCtrlLock;
 
+NTSTATUS
+InitUserDumpState(POVS_OPEN_INSTANCE instance,
+                  POVS_MESSAGE ovsMsg)
+{
+    /* Clear the dumpState from a previous dump sequence. */
+    ASSERT(instance->dumpState.ovsMsg == NULL);
+    ASSERT(ovsMsg);
+
+    instance->dumpState.ovsMsg =
+        (POVS_MESSAGE)OvsAllocateMemoryWithTag(sizeof(OVS_MESSAGE),
+                                               OVS_DATAPATH_POOL_TAG);
+    if (instance->dumpState.ovsMsg == NULL) {
+        return STATUS_NO_MEMORY;
+    }
+    RtlCopyMemory(instance->dumpState.ovsMsg, ovsMsg,
+                  sizeof *instance->dumpState.ovsMsg);
+    RtlZeroMemory(instance->dumpState.index,
+                  sizeof instance->dumpState.index);
+
+    return STATUS_SUCCESS;
+}
+
+VOID
+FreeUserDumpState(POVS_OPEN_INSTANCE instance)
+{
+    if (instance->dumpState.ovsMsg != NULL) {
+        OvsFreeMemoryWithTag(instance->dumpState.ovsMsg,
+                             OVS_DATAPATH_POOL_TAG);
+        RtlZeroMemory(&instance->dumpState, sizeof instance->dumpState);
+    }
+}
 
 VOID
 OvsInit()
@@ -285,7 +383,6 @@ OvsInit()
     gOvsCtrlLock = &ovsCtrlLockObj;
     NdisAllocateSpinLock(gOvsCtrlLock);
     OvsInitEventQueue();
-    OvsUserInit();
 }
 
 VOID
@@ -296,7 +393,6 @@ OvsCleanup()
         NdisFreeSpinLock(gOvsCtrlLock);
         gOvsCtrlLock = NULL;
     }
-    OvsUserCleanup();
 }
 
 VOID
@@ -364,9 +460,9 @@ OvsCreateDeviceObject(NDIS_HANDLE ovsExtDriverHandle)
             ovsExt->numberOpenInstance = 0;
         }
     } else {
-        /* Initialize the associated data structures. */
-        OvsInit();
+        OvsRegisterSystemProvider((PVOID)gOvsDeviceObject);
     }
+
     OVS_LOG_TRACE("DeviceObject: %p", gOvsDeviceObject);
     return status;
 }
@@ -388,8 +484,9 @@ OvsDeleteDeviceObject()
         NdisDeregisterDeviceEx(gOvsDeviceHandle);
         gOvsDeviceHandle = NULL;
         gOvsDeviceObject = NULL;
+
+        OvsUnregisterSystemProvider();
     }
-    OvsCleanup();
 }
 
 POVS_OPEN_INSTANCE
@@ -399,8 +496,7 @@ OvsGetOpenInstance(PFILE_OBJECT fileObject,
     POVS_OPEN_INSTANCE instance = (POVS_OPEN_INSTANCE)fileObject->FsContext;
     ASSERT(instance);
     ASSERT(instance->fileObject == fileObject);
-    if (gOvsSwitchContext == NULL ||
-        gOvsSwitchContext->dpNo != dpNo) {
+    if (gOvsSwitchContext->dpNo != dpNo) {
         return NULL;
     }
     return instance;
@@ -428,7 +524,8 @@ OvsAddOpenInstance(POVS_DEVICE_EXTENSION ovsExt,
                    PFILE_OBJECT fileObject)
 {
     POVS_OPEN_INSTANCE instance =
-        (POVS_OPEN_INSTANCE) OvsAllocateMemory(sizeof (OVS_OPEN_INSTANCE));
+        (POVS_OPEN_INSTANCE)OvsAllocateMemoryWithTag(sizeof(OVS_OPEN_INSTANCE),
+                                                     OVS_DATAPATH_POOL_TAG);
     UINT32 i;
 
     if (instance == NULL) {
@@ -439,7 +536,7 @@ OvsAddOpenInstance(POVS_DEVICE_EXTENSION ovsExt,
 
     if (ovsNumberOfOpenInstances >= OVS_MAX_OPEN_INSTANCES) {
         OvsReleaseCtrlLock();
-        OvsFreeMemory(instance);
+        OvsFreeMemoryWithTag(instance, OVS_DATAPATH_POOL_TAG);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     RtlZeroMemory(instance, sizeof (OVS_OPEN_INSTANCE));
@@ -490,7 +587,7 @@ OvsRemoveOpenInstance(PFILE_OBJECT fileObject)
     OvsReleaseCtrlLock();
     ASSERT(instance->eventQueue == NULL);
     ASSERT (instance->packetQueue == NULL);
-    OvsFreeMemory(instance);
+    OvsFreeMemoryWithTag(instance, OVS_DATAPATH_POOL_TAG);
 }
 
 NTSTATUS
@@ -582,7 +679,6 @@ NTSTATUS
 OvsDeviceControl(PDEVICE_OBJECT deviceObject,
                  PIRP irp)
 {
-
     PIO_STACK_LOCATION irpSp;
     NTSTATUS status = STATUS_SUCCESS;
     PFILE_OBJECT fileObject;
@@ -619,6 +715,12 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
     outputBufferLen = irpSp->Parameters.DeviceIoControl.OutputBufferLength;
     inputBuffer = irp->AssociatedIrp.SystemBuffer;
 
+    /* Check if the extension is enabled. */
+    if (NULL == gOvsSwitchContext) {
+        status = STATUS_DEVICE_NOT_READY;
+        goto done;
+    }
+
     /* Concurrent netlink operations are not supported. */
     if (InterlockedCompareExchange((LONG volatile *)&instance->inUse, 1, 0)) {
         status = STATUS_RESOURCE_IN_USE;
@@ -630,8 +732,26 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
      * operation.
      */
     switch (code) {
+    case OVS_IOCTL_GET_PID:
+        /* Both input buffer and output buffer use the same location. */
+        outputBuffer = irp->AssociatedIrp.SystemBuffer;
+        if (outputBufferLen != 0) {
+            InitUserParamsCtx(irp, instance, 0, NULL,
+                              inputBuffer, inputBufferLen,
+                              outputBuffer, outputBufferLen,
+                              &usrParamsCtx);
+
+            ASSERT(outputBuffer);
+        } else {
+            status = STATUS_NDIS_INVALID_LENGTH;
+            goto done;
+        }
+
+        status = OvsGetPidHandler(&usrParamsCtx, &replyLen);
+        goto done;
+
     case OVS_IOCTL_TRANSACT:
-        /* Input buffer is mandatory, output buffer is optional. */
+        /* Both input buffer and output buffer are mandatory. */
         if (outputBufferLen != 0) {
             status = MapIrpOutputBuffer(irp, outputBufferLen,
                                         sizeof *ovsMsg, &outputBuffer);
@@ -639,6 +759,9 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
                 goto done;
             }
             ASSERT(outputBuffer);
+        } else {
+            status = STATUS_NDIS_INVALID_LENGTH;
+            goto done;
         }
 
         if (inputBufferLen < sizeof (*ovsMsg)) {
@@ -651,7 +774,11 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
         break;
 
     case OVS_IOCTL_READ_EVENT:
-        /* This IOCTL is used to read events */
+    case OVS_IOCTL_READ_PACKET:
+        /*
+         * Output buffer is mandatory. These IOCTLs are used to read events and
+         * packets respectively. It is convenient to have separate ioctls.
+         */
         if (outputBufferLen != 0) {
             status = MapIrpOutputBuffer(irp, outputBufferLen,
                                         sizeof *ovsMsg, &outputBuffer);
@@ -668,8 +795,11 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
 
         ovsMsg = &ovsMsgReadOp;
         ovsMsg->nlMsg.nlmsgType = OVS_WIN_NL_CTRL_FAMILY_ID;
+        ovsMsg->nlMsg.nlmsgPid = instance->pid;
         /* An "artificial" command so we can use NL family function table*/
-        ovsMsg->genlMsg.cmd = OVS_CTRL_CMD_EVENT_NOTIFY;
+        ovsMsg->genlMsg.cmd = (code == OVS_IOCTL_READ_EVENT) ?
+                              OVS_CTRL_CMD_EVENT_NOTIFY :
+                              OVS_CTRL_CMD_READ_NOTIFY;
         devOp = OVS_READ_DEV_OP;
         break;
 
@@ -699,7 +829,7 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
          * state in the instance to indicate the command that started the dump
          * operation. The state can setup 'ovsMsgReadOp' appropriately. Note
          * that 'ovsMsgReadOp' is needed only in this function to call into the
-         * appropraite handler. The handler itself can access the state in the
+         * appropriate handler. The handler itself can access the state in the
          * instance.
          *
          * In the absence of a dump start, return 0 bytes.
@@ -746,11 +876,14 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
          nlFamilyOps = &nlFLowFamilyOps;
          break;
     case OVS_WIN_NL_PACKET_FAMILY_ID:
-        status = STATUS_NOT_IMPLEMENTED;
-        goto done;
+         nlFamilyOps = &nlPacketFamilyOps;
+         break;
     case OVS_WIN_NL_VPORT_FAMILY_ID:
         nlFamilyOps = &nlVportFamilyOps;
         break;
+    case OVS_WIN_NL_NETDEV_FAMILY_ID:
+        nlFamilyOps = &nlNetdevFamilyOps;
+        break;
     default:
         status = STATUS_INVALID_PARAMETER;
         goto done;
@@ -777,6 +910,11 @@ OvsDeviceControl(PDEVICE_OBJECT deviceObject,
 done:
     KeMemoryBarrier();
     instance->inUse = 0;
+
+    /* Should not complete a pending IRP unless proceesing is completed */
+    if (status == STATUS_PENDING) {
+        return status;
+    }
     return OvsCompleteIrpRequest(irp, (ULONG_PTR)replyLen, status);
 }
 
@@ -814,7 +952,7 @@ ValidateNetlinkCmd(UINT32 devOp,
             if (nlFamilyOps->cmds[i].validateDpIndex == TRUE) {
                 OvsAcquireCtrlLock();
                 if (ovsMsg->ovsHdr.dp_ifindex !=
-                    (INT)gOvsSwitchContext->dpNo) {
+                                          (INT)gOvsSwitchContext->dpNo) {
                     status = STATUS_INVALID_PARAMETER;
                     OvsReleaseCtrlLock();
                     goto done;
@@ -823,11 +961,9 @@ ValidateNetlinkCmd(UINT32 devOp,
             }
 
             /* Validate the PID. */
-            if (ovsMsg->genlMsg.cmd != OVS_CTRL_CMD_WIN_GET_PID) {
-                if (ovsMsg->nlMsg.nlmsgPid != instance->pid) {
-                    status = STATUS_INVALID_PARAMETER;
-                    goto done;
-                }
+            if (ovsMsg->nlMsg.nlmsgPid != instance->pid) {
+                status = STATUS_INVALID_PARAMETER;
+                goto done;
             }
 
             status = STATUS_SUCCESS;
@@ -868,38 +1004,33 @@ InvokeNetlinkCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
 
 /*
  * --------------------------------------------------------------------------
- *  Command Handler for 'OVS_CTRL_CMD_WIN_GET_PID'.
+ *  Handler for 'OVS_IOCTL_GET_PID'.
  *
  *  Each handle on the device is assigned a unique PID when the handle is
- *  created. On platforms that support netlink natively, the PID is available
- *  to userspace when the netlink socket is created. However, without native
- *  netlink support on Windows, OVS datapath generates the PID and lets the
- *  userspace query it.
- *
- *  This function implements the query.
+ *  created. This function passes the PID to userspace using METHOD_BUFFERED
+ *  method.
  * --------------------------------------------------------------------------
  */
 static NTSTATUS
-OvsGetPidCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
-                    UINT32 *replyLen)
+OvsGetPidHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                 UINT32 *replyLen)
 {
-    POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
-    POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
+    NTSTATUS status = STATUS_SUCCESS;
+    PUINT32 msgOut = (PUINT32)usrParamsCtx->outputBuffer;
 
     if (usrParamsCtx->outputLength >= sizeof *msgOut) {
         POVS_OPEN_INSTANCE instance =
             (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
 
         RtlZeroMemory(msgOut, sizeof *msgOut);
-        msgOut->nlMsg.nlmsgSeq = msgIn->nlMsg.nlmsgSeq;
-        msgOut->nlMsg.nlmsgPid = instance->pid;
+        RtlCopyMemory(msgOut, &instance->pid, sizeof(*msgOut));
         *replyLen = sizeof *msgOut;
-        /* XXX: We might need to return the DP index as well. */
     } else {
-        return STATUS_NDIS_INVALID_LENGTH;
+        *replyLen = sizeof *msgOut;
+        status = STATUS_NDIS_INVALID_LENGTH;
     }
 
-    return STATUS_SUCCESS;
+    return status;
 }
 
 /*
@@ -1005,7 +1136,7 @@ OvsSubscribeEventCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
     POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
 
     rc = NlAttrParse(&msgIn->nlMsg, sizeof (*msgIn),
-         NlMsgAttrsLen((PNL_MSG_HDR)msgIn), policy, attrs, 2);
+         NlMsgAttrsLen((PNL_MSG_HDR)msgIn), policy, attrs, ARRAY_SIZE(attrs));
     if (!rc) {
         status = STATUS_INVALID_PARAMETER;
         goto done;
@@ -1023,6 +1154,17 @@ done:
     return status;
 }
 
+/*
+ * --------------------------------------------------------------------------
+ *  Command Handler for 'OVS_DP_CMD_NEW'.
+ * --------------------------------------------------------------------------
+ */
+static NTSTATUS
+OvsNewDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                   UINT32 *replyLen)
+{
+    return HandleDpTransactionCommon(usrParamsCtx, replyLen);
+}
 
 /*
  * --------------------------------------------------------------------------
@@ -1039,7 +1181,7 @@ OvsGetDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                    UINT32 *replyLen)
 {
     if (usrParamsCtx->devOp == OVS_TRANSACTION_DEV_OP) {
-        return HandleGetDpTransaction(usrParamsCtx, replyLen);
+        return HandleDpTransactionCommon(usrParamsCtx, replyLen);
     } else {
         return HandleGetDpDump(usrParamsCtx, replyLen);
     }
@@ -1054,7 +1196,7 @@ static NTSTATUS
 HandleGetDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                        UINT32 *replyLen)
 {
-    return HandleDpTransaction(usrParamsCtx, replyLen);
+    return HandleDpTransactionCommon(usrParamsCtx, replyLen);
 }
 
 
@@ -1088,6 +1230,7 @@ HandleGetDpDump(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
 
         /* Dump state must have been deleted after previous dump operation. */
         ASSERT(instance->dumpState.index[0] == 0);
+
         /* Output buffer has been validated while validating read dev op. */
         ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
 
@@ -1095,13 +1238,6 @@ HandleGetDpDump(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                   usrParamsCtx->outputLength);
 
         OvsAcquireCtrlLock();
-        if (!gOvsSwitchContext) {
-            /* Treat this as a dump done. */
-            OvsReleaseCtrlLock();
-            *replyLen = 0;
-            FreeUserDumpState(instance);
-            return STATUS_SUCCESS;
-        }
         status = OvsDpFillInfo(gOvsSwitchContext, msgIn, &nlBuf);
         OvsReleaseCtrlLock();
 
@@ -1132,23 +1268,27 @@ static NTSTATUS
 OvsSetDpCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                    UINT32 *replyLen)
 {
-    return HandleDpTransaction(usrParamsCtx, replyLen);
+    return HandleDpTransactionCommon(usrParamsCtx, replyLen);
 }
 
 /*
  * --------------------------------------------------------------------------
- *  Function for handling transaction based 'OVS_DP_CMD_GET' and
- *  'OVS_DP_CMD_SET' commands.
+ *  Function for handling transaction based 'OVS_DP_CMD_NEW', 'OVS_DP_CMD_GET'
+ *  and 'OVS_DP_CMD_SET' commands.
+ *
+ * 'OVS_DP_CMD_NEW' is implemented to keep userspace code happy. Creation of a
+ * new datapath is not supported currently.
  * --------------------------------------------------------------------------
  */
 static NTSTATUS
-HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
-                    UINT32 *replyLen)
+HandleDpTransactionCommon(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                          UINT32 *replyLen)
 {
     POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
     POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
     NTSTATUS status = STATUS_SUCCESS;
     NL_BUFFER nlBuf;
+    NL_ERROR nlError = NL_ERROR_SUCCESS;
     static const NL_POLICY ovsDatapathSetPolicy[] = {
         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .maxLen = IFNAMSIZ },
         [OVS_DP_ATTR_UPCALL_PID] = { .type = NL_A_U32, .optional = TRUE },
@@ -1156,11 +1296,14 @@ HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
     };
     PNL_ATTR dpAttrs[ARRAY_SIZE(ovsDatapathSetPolicy)];
 
+    UNREFERENCED_PARAMETER(msgOut);
+
     /* input buffer has been validated while validating write dev op. */
     ASSERT(msgIn != NULL && usrParamsCtx->inputLength >= sizeof *msgIn);
 
     /* Parse any attributes in the request. */
-    if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_SET) {
+    if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_SET ||
+        usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_NEW) {
         if (!NlAttrParse((PNL_MSG_HDR)msgIn,
                         NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN,
                         NlMsgAttrsLen((PNL_MSG_HDR)msgIn),
@@ -1178,24 +1321,35 @@ HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
         RtlZeroMemory(dpAttrs, sizeof dpAttrs);
     }
 
-    /* Output buffer is optional for OVS_TRANSACTION_DEV_OP. */
-    if (msgOut == NULL || usrParamsCtx->outputLength < sizeof *msgOut) {
-        return STATUS_NDIS_INVALID_LENGTH;
-    }
+    /* Output buffer has been validated while validating transact dev op. */
+    ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
+
     NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength);
 
     OvsAcquireCtrlLock();
     if (dpAttrs[OVS_DP_ATTR_NAME] != NULL) {
-        if (!gOvsSwitchContext &&
-            !OvsCompareString(NlAttrGet(dpAttrs[OVS_DP_ATTR_NAME]),
+        if (!OvsCompareString(NlAttrGet(dpAttrs[OVS_DP_ATTR_NAME]),
                               OVS_SYSTEM_DP_NAME)) {
             OvsReleaseCtrlLock();
-            status = STATUS_NOT_FOUND;
+
+            /* Creation of new datapaths is not supported. */
+            if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_SET) {
+                nlError = NL_ERROR_NOTSUPP;
+                goto cleanup;
+            }
+
+            nlError = NL_ERROR_NODEV;
             goto cleanup;
         }
     } else if ((UINT32)msgIn->ovsHdr.dp_ifindex != gOvsSwitchContext->dpNo) {
         OvsReleaseCtrlLock();
-        status = STATUS_NOT_FOUND;
+        nlError = NL_ERROR_NODEV;
+        goto cleanup;
+    }
+
+    if (usrParamsCtx->ovsMsg->genlMsg.cmd == OVS_DP_CMD_NEW) {
+        OvsReleaseCtrlLock();
+        nlError = NL_ERROR_EXIST;
         goto cleanup;
     }
 
@@ -1205,7 +1359,15 @@ HandleDpTransaction(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
     *replyLen = NlBufSize(&nlBuf);
 
 cleanup:
-    return status;
+    if (nlError != NL_ERROR_SUCCESS) {
+        POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR)
+            usrParamsCtx->outputBuffer;
+
+        NlBuildErrorMsg(msgIn, msgError, nlError);
+        *replyLen = msgError->nlMsg.nlmsgLen;
+    }
+
+    return STATUS_SUCCESS;
 }
 
 
@@ -1229,356 +1391,11 @@ OvsSetupDumpStart(POVS_USER_PARAMS_CONTEXT usrParamsCtx)
      * This operation should be setting up the dump state. If there's any
      * previous state, clear it up so as to set it up afresh.
      */
-    if (instance->dumpState.ovsMsg != NULL) {
-        FreeUserDumpState(instance);
-    }
+    FreeUserDumpState(instance);
 
     return InitUserDumpState(instance, msgIn);
 }
 
-static VOID
-BuildMsgOut(POVS_MESSAGE msgIn, POVS_MESSAGE msgOut, UINT16 type,
-                     UINT32 length, UINT16 flags)
-{
-    msgOut->nlMsg.nlmsgType = type;
-    msgOut->nlMsg.nlmsgFlags = flags;
-    msgOut->nlMsg.nlmsgSeq = msgIn->nlMsg.nlmsgSeq;
-    msgOut->nlMsg.nlmsgPid = msgIn->nlMsg.nlmsgPid;
-    msgOut->nlMsg.nlmsgLen = length;
-
-    msgOut->genlMsg.cmd = msgIn->genlMsg.cmd;
-    msgOut->genlMsg.version = nlDatapathFamilyOps.version;
-    msgOut->genlMsg.reserved = 0;
-}
-
-static VOID
-BuildReplyMsgFromMsgIn(POVS_MESSAGE msgIn, POVS_MESSAGE msgOut, UINT16 flags)
-{
-    BuildMsgOut(msgIn, msgOut, msgIn->nlMsg.nlmsgType, sizeof(OVS_MESSAGE),
-                flags);
-}
-
-static VOID
-BuildErrorMsg(POVS_MESSAGE msgIn, POVS_MESSAGE_ERROR msgOut, UINT errorCode)
-{
-    BuildMsgOut(msgIn, (POVS_MESSAGE)msgOut, NLMSG_ERROR,
-                sizeof(OVS_MESSAGE_ERROR), 0);
-
-    msgOut->errorMsg.error = errorCode;
-    msgOut->errorMsg.nlMsg = msgIn->nlMsg;
-}
-
-static NTSTATUS
-OvsCreateMsgFromVport(POVS_VPORT_ENTRY vport,
-                      POVS_MESSAGE msgIn,
-                      PVOID outBuffer,
-                      UINT32 outBufLen,
-                      int dpIfIndex)
-{
-    NL_BUFFER nlBuffer;
-    OVS_VPORT_FULL_STATS vportStats;
-    BOOLEAN ok;
-    OVS_MESSAGE msgOut;
-    PNL_MSG_HDR nlMsg;
-
-    NlBufInit(&nlBuffer, outBuffer, outBufLen);
-
-    BuildReplyMsgFromMsgIn(msgIn, &msgOut, NLM_F_MULTI);
-    msgOut.ovsHdr.dp_ifindex = dpIfIndex;
-
-    ok = NlMsgPutHead(&nlBuffer, (PCHAR)&msgOut, sizeof msgOut);
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    ok = NlMsgPutTailU32(&nlBuffer, OVS_VPORT_ATTR_PORT_NO, vport->portNo);
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    ok = NlMsgPutTailU32(&nlBuffer, OVS_VPORT_ATTR_TYPE, vport->ovsType);
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    ok = NlMsgPutTailString(&nlBuffer, OVS_VPORT_ATTR_NAME, vport->ovsName);
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    /*
-     * XXX: when we implement OVS_DP_ATTR_USER_FEATURES in datapath,
-     * we'll need to check the OVS_DP_F_VPORT_PIDS flag: if it is set,
-     * it means we have an array of pids, instead of a single pid.
-     * ATM we assume we have one pid only.
-    */
-
-    ok = NlMsgPutTailU32(&nlBuffer, OVS_VPORT_ATTR_UPCALL_PID,
-                         vport->upcallPid);
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    /*stats*/
-    vportStats.rxPackets = vport->stats.rxPackets;
-    vportStats.rxBytes = vport->stats.rxBytes;
-    vportStats.txPackets = vport->stats.txPackets;
-    vportStats.txBytes = vport->stats.txBytes;
-    vportStats.rxErrors = vport->errStats.rxErrors;
-    vportStats.txErrors = vport->errStats.txErrors;
-    vportStats.rxDropped = vport->errStats.rxDropped;
-    vportStats.txDropped = vport->errStats.txDropped;
-
-    ok = NlMsgPutTailUnspec(&nlBuffer, OVS_VPORT_ATTR_STATS,
-                            (PCHAR)&vportStats,
-                            sizeof(OVS_VPORT_FULL_STATS));
-    if (!ok) {
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    /*
-     * XXX: when vxlan udp dest port becomes configurable, we will also need
-     * to add vport options
-    */
-
-    nlMsg = (PNL_MSG_HDR)NlBufAt(&nlBuffer, 0, 0);
-    nlMsg->nlmsgLen = NlBufSize(&nlBuffer);
-
-    return STATUS_SUCCESS;
-}
-
-static NTSTATUS
-OvsGetVportDumpNext(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
-                    UINT32 *replyLen)
-{
-    POVS_MESSAGE msgIn;
-    POVS_OPEN_INSTANCE instance =
-        (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
-    LOCK_STATE_EX lockState;
-    UINT32 i = OVS_MAX_VPORT_ARRAY_SIZE;
-
-    /*
-     * XXX: this function shares some code with other dump command(s).
-     * In the future, we will need to refactor the dump functions
-    */
-
-    ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
-
-    if (instance->dumpState.ovsMsg == NULL) {
-        ASSERT(FALSE);
-        return STATUS_INVALID_DEVICE_STATE;
-    }
-
-    /* Output buffer has been validated while validating read dev op. */
-    ASSERT(usrParamsCtx->outputBuffer != NULL);
-
-    msgIn = instance->dumpState.ovsMsg;
-
-    OvsAcquireCtrlLock();
-    if (!gOvsSwitchContext) {
-        /* Treat this as a dump done. */
-        OvsReleaseCtrlLock();
-        *replyLen = 0;
-        FreeUserDumpState(instance);
-        return STATUS_SUCCESS;
-    }
-
-    /*
-     * XXX: when we implement OVS_DP_ATTR_USER_FEATURES in datapath,
-     * we'll need to check the OVS_DP_F_VPORT_PIDS flag: if it is set,
-     * it means we have an array of pids, instead of a single pid.
-     * ATM we assume we have one pid only.
-    */
-
-    NdisAcquireRWLockRead(gOvsSwitchContext->dispatchLock, &lockState, 0);
-
-    if (gOvsSwitchContext->numVports > 0) {
-        /* inBucket: the bucket, used for lookup */
-        UINT32 inBucket = instance->dumpState.index[0];
-        /* inIndex: index within the given bucket, used for lookup */
-        UINT32 inIndex = instance->dumpState.index[1];
-        /* the bucket to be used for the next dump operation */
-        UINT32 outBucket = 0;
-        /* the index within the outBucket to be used for the next dump */
-        UINT32 outIndex = 0;
-
-        for (i = inBucket; i < OVS_MAX_VPORT_ARRAY_SIZE; i++) {
-            PLIST_ENTRY head, link;
-            head = &(gOvsSwitchContext->portHashArray[i]);
-            POVS_VPORT_ENTRY vport = NULL;
-
-            outIndex = 0;
-            LIST_FORALL(head, link) {
-
-                /*
-                 * if one or more dumps were previously done on this same bucket,
-                 * inIndex will be > 0, so we'll need to reply with the
-                 * inIndex + 1 vport from the bucket.
-                */
-                if (outIndex >= inIndex) {
-                    vport = CONTAINING_RECORD(link, OVS_VPORT_ENTRY, portLink);
-
-                    if (vport->portNo != 0) {
-                        OvsCreateMsgFromVport(vport, msgIn,
-                                              usrParamsCtx->outputBuffer,
-                                              usrParamsCtx->outputLength,
-                                              gOvsSwitchContext->dpNo);
-                        ++outIndex;
-                        break;
-                    } else {
-                        vport = NULL;
-                    }
-                }
-
-                ++outIndex;
-            }
-
-            if (vport) {
-                break;
-            }
-
-            /*
-             * if no vport was found above, check the next bucket, beginning
-             * with the first (i.e. index 0) elem from within that bucket
-            */
-            inIndex = 0;
-        }
-
-        outBucket = i;
-
-        /* XXX: what about NLMSG_DONE (as msg type)? */
-        instance->dumpState.index[0] = outBucket;
-        instance->dumpState.index[1] = outIndex;
-    }
-
-    NdisReleaseRWLock(gOvsSwitchContext->dispatchLock, &lockState);
-
-    OvsReleaseCtrlLock();
-
-    /* if i < OVS_MAX_VPORT_ARRAY_SIZE => vport was found */
-    if (i < OVS_MAX_VPORT_ARRAY_SIZE) {
-        POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
-        *replyLen = msgOut->nlMsg.nlmsgLen;
-    } else {
-        /*
-         * if i >= OVS_MAX_VPORT_ARRAY_SIZE => vport was not found =>
-         * it's dump done
-         */
-        *replyLen = 0;
-        /* Free up the dump state, since there's no more data to continue. */
-        FreeUserDumpState(instance);
-    }
-
-    return STATUS_SUCCESS;
-}
-
-static NTSTATUS
-OvsGetVport(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
-            UINT32 *replyLen)
-{
-    NTSTATUS status = STATUS_SUCCESS;
-    LOCK_STATE_EX lockState;
-
-    POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
-    POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
-    POVS_VPORT_ENTRY vport = NULL;
-    NL_ERROR nlError = NL_ERROR_SUCCESS;
-
-    static const NL_POLICY ovsVportPolicy[] = {
-        [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32, .optional = TRUE },
-        [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING,
-                                  .minLen = 2,
-                                  .maxLen = IFNAMSIZ,
-                                  .optional = TRUE},
-    };
-    PNL_ATTR vportAttrs[ARRAY_SIZE(ovsVportPolicy)];
-
-    /* input buffer has been validated while validating write dev op. */
-    ASSERT(usrParamsCtx->inputBuffer != NULL);
-
-    if (!NlAttrParse((PNL_MSG_HDR)msgIn,
-        NLMSG_HDRLEN + GENL_HDRLEN + OVS_HDRLEN,
-        NlMsgAttrsLen((PNL_MSG_HDR)msgIn),
-        ovsVportPolicy, vportAttrs, ARRAY_SIZE(vportAttrs))) {
-        return STATUS_INVALID_PARAMETER;
-    }
-
-    if (msgOut == NULL || usrParamsCtx->outputLength < sizeof *msgOut) {
-        return STATUS_INVALID_BUFFER_SIZE;
-    }
-
-    OvsAcquireCtrlLock();
-    if (!gOvsSwitchContext) {
-        OvsReleaseCtrlLock();
-        return STATUS_INVALID_PARAMETER;
-    }
-    OvsReleaseCtrlLock();
-
-    if (vportAttrs[OVS_VPORT_ATTR_NAME] != NULL) {
-        vport = OvsFindVportByOvsName(gOvsSwitchContext,
-            NlAttrGet(vportAttrs[OVS_VPORT_ATTR_NAME]),
-            NlAttrGetSize(vportAttrs[OVS_VPORT_ATTR_NAME]) - 1);
-    } else if (vportAttrs[OVS_VPORT_ATTR_PORT_NO] != NULL) {
-        vport = OvsFindVportByPortNo(gOvsSwitchContext,
-            NlAttrGetU32(vportAttrs[OVS_VPORT_ATTR_PORT_NO]));
-    } else {
-        nlError = NL_ERROR_INVAL;
-        goto Cleanup;
-    }
-
-    if (!vport) {
-        nlError = NL_ERROR_NODEV;
-        goto Cleanup;
-    }
-
-    NdisAcquireRWLockRead(gOvsSwitchContext->dispatchLock, &lockState, 0);
-    status = OvsCreateMsgFromVport(vport, msgIn, usrParamsCtx->outputBuffer,
-                                   usrParamsCtx->outputLength,
-                                   gOvsSwitchContext->dpNo);
-    NdisReleaseRWLock(gOvsSwitchContext->dispatchLock, &lockState);
-
-    *replyLen = msgOut->nlMsg.nlmsgLen;
-
-Cleanup:
-    if (nlError != NL_ERROR_SUCCESS) {
-        POVS_MESSAGE_ERROR msgError = (POVS_MESSAGE_ERROR)
-            usrParamsCtx->outputBuffer;
-
-        BuildErrorMsg(msgIn, msgError, nlError);
-        *replyLen = msgError->nlMsg.nlmsgLen;
-    }
-
-    return STATUS_SUCCESS;
-}
-
-/*
- * --------------------------------------------------------------------------
- *  Handler for the get vport command. The function handles the initial call to
- *  setup the dump state, as well as subsequent calls to continue dumping data.
- * --------------------------------------------------------------------------
-*/
-static NTSTATUS
-OvsGetVportCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
-                      UINT32 *replyLen)
-{
-    *replyLen = 0;
-
-    switch (usrParamsCtx->devOp)
-    {
-    case OVS_WRITE_DEV_OP:
-        return OvsSetupDumpStart(usrParamsCtx);
-
-    case OVS_READ_DEV_OP:
-        return OvsGetVportDumpNext(usrParamsCtx, replyLen);
-
-    case OVS_TRANSACTION_DEV_OP:
-        return OvsGetVport(usrParamsCtx, replyLen);
-
-    default:
-        return STATUS_INVALID_DEVICE_REQUEST;
-    }
-
-}
 
 /*
  * --------------------------------------------------------------------------
@@ -1628,7 +1445,7 @@ OvsPortFillInfo(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
                 PNL_BUFFER nlBuf)
 {
     NTSTATUS status;
-    BOOLEAN rc;
+    BOOLEAN ok;
     OVS_MESSAGE msgOutTmp;
     PNL_MSG_HDR nlMsg;
     POVS_VPORT_ENTRY vport;
@@ -1657,8 +1474,8 @@ OvsPortFillInfo(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
     }
     msgOutTmp.ovsHdr.dp_ifindex = gOvsSwitchContext->dpNo;
 
-    rc = NlMsgPutHead(nlBuf, (PCHAR)&msgOutTmp, sizeof msgOutTmp);
-    if (!rc) {
+    ok = NlMsgPutHead(nlBuf, (PCHAR)&msgOutTmp, sizeof msgOutTmp);
+    if (!ok) {
         status = STATUS_INVALID_BUFFER_SIZE;
         goto cleanup;
     }
@@ -1669,10 +1486,12 @@ OvsPortFillInfo(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
         goto cleanup;
     }
 
-    rc = NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_PORT_NO, eventEntry->portNo) ||
-         NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_TYPE, vport->ovsType) ||
+    ok = NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_PORT_NO, eventEntry->portNo) &&
+         NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_TYPE, vport->ovsType) &&
+         NlMsgPutTailU32(nlBuf, OVS_VPORT_ATTR_UPCALL_PID,
+                         vport->upcallPid) &&
          NlMsgPutTailString(nlBuf, OVS_VPORT_ATTR_NAME, vport->ovsName);
-    if (!rc) {
+    if (!ok) {
         status = STATUS_INVALID_BUFFER_SIZE;
         goto cleanup;
     }
@@ -1722,14 +1541,13 @@ OvsReadEventCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
     NlBufInit(&nlBuf, usrParamsCtx->outputBuffer, usrParamsCtx->outputLength);
 
     OvsAcquireCtrlLock();
-    if (!gOvsSwitchContext) {
-        status = STATUS_SUCCESS;
-        goto cleanup;
-    }
 
     /* remove an event entry from the event queue */
     status = OvsRemoveEventEntry(usrParamsCtx->ovsInstance, &eventEntry);
     if (status != STATUS_SUCCESS) {
+        /* If there were not elements, read should return no data. */
+        status = STATUS_SUCCESS;
+        *replyLen = 0;
         goto cleanup;
     }
 
@@ -1742,4 +1560,110 @@ cleanup:
     OvsReleaseCtrlLock();
     return status;
 }
-#endif /* OVS_USE_NL_INTERFACE */
+
+/*
+ * --------------------------------------------------------------------------
+ * Handler for reading missed pacckets from the driver event queue. This
+ * handler is executed when user modes issues a socket receive on a socket
+ * --------------------------------------------------------------------------
+ */
+static NTSTATUS
+OvsReadPacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                       UINT32 *replyLen)
+{
+#ifdef DBG
+    POVS_MESSAGE msgOut = (POVS_MESSAGE)usrParamsCtx->outputBuffer;
+#endif
+    POVS_OPEN_INSTANCE instance =
+        (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
+    NTSTATUS status;
+
+    ASSERT(usrParamsCtx->devOp == OVS_READ_DEV_OP);
+
+    /* Should never read events with a dump socket */
+    ASSERT(instance->dumpState.ovsMsg == NULL);
+
+    /* Must have an packet queue */
+    ASSERT(instance->packetQueue != NULL);
+
+    /* Output buffer has been validated while validating read dev op. */
+    ASSERT(msgOut != NULL && usrParamsCtx->outputLength >= sizeof *msgOut);
+
+    /* Read a packet from the instance queue */
+    status = OvsReadDpIoctl(instance->fileObject, usrParamsCtx->outputBuffer,
+                            usrParamsCtx->outputLength, replyLen);
+    return status;
+}
+
+/*
+ * --------------------------------------------------------------------------
+ *  Handler for the subscription for a packet queue
+ * --------------------------------------------------------------------------
+ */
+static NTSTATUS
+OvsSubscribePacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                            UINT32 *replyLen)
+{
+    NDIS_STATUS status;
+    BOOLEAN rc;
+    UINT8 join;
+    UINT32 pid;
+    const NL_POLICY policy[] =  {
+        [OVS_NL_ATTR_PACKET_PID] = {.type = NL_A_U32 },
+        [OVS_NL_ATTR_PACKET_SUBSCRIBE] = {.type = NL_A_U8 }
+        };
+    PNL_ATTR attrs[ARRAY_SIZE(policy)];
+
+    UNREFERENCED_PARAMETER(replyLen);
+
+    POVS_OPEN_INSTANCE instance =
+        (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
+    POVS_MESSAGE msgIn = (POVS_MESSAGE)usrParamsCtx->inputBuffer;
+
+    rc = NlAttrParse(&msgIn->nlMsg, sizeof (*msgIn),
+         NlMsgAttrsLen((PNL_MSG_HDR)msgIn), policy, attrs, ARRAY_SIZE(attrs));
+    if (!rc) {
+        status = STATUS_INVALID_PARAMETER;
+        goto done;
+    }
+
+    join = NlAttrGetU8(attrs[OVS_NL_ATTR_PACKET_PID]);
+    pid = NlAttrGetU32(attrs[OVS_NL_ATTR_PACKET_PID]);
+
+    /* The socket subscribed with must be the same socket we perform receive*/
+    ASSERT(pid == instance->pid);
+
+    status = OvsSubscribeDpIoctl(instance, pid, join);
+
+    /*
+     * XXX Need to add this instance to a global data structure
+     * which hold all packet based instances. The data structure (hash)
+     * should be searched through the pid field of the instance for
+     * placing the missed packet into the correct queue
+     */
+done:
+    return status;
+}
+
+/*
+ * --------------------------------------------------------------------------
+ * Handler for queueing an IRP used for missed packet notification. The IRP is
+ * completed when a packet received and mismatched. STATUS_PENDING is returned
+ * on success. User mode keep a pending IRP at all times.
+ * --------------------------------------------------------------------------
+ */
+static NTSTATUS
+OvsPendPacketCmdHandler(POVS_USER_PARAMS_CONTEXT usrParamsCtx,
+                       UINT32 *replyLen)
+{
+    UNREFERENCED_PARAMETER(replyLen);
+
+    POVS_OPEN_INSTANCE instance =
+        (POVS_OPEN_INSTANCE)usrParamsCtx->ovsInstance;
+
+    /*
+     * XXX access to packet queue must be through acquiring a lock as user mode
+     * could unsubscribe and the instnace will be freed.
+     */
+    return OvsWaitDpIoctl(usrParamsCtx->irp, instance->fileObject);
+}