datapath-windows: Some fixes in vport get code
[cascardo/ovs.git] / datapath-windows / ovsext / Datapath.h
1 /*
2  * Copyright (c) 2014 VMware, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
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
22  */
23 #if defined OVS_USE_NL_INTERFACE && OVS_USE_NL_INTERFACE == 0
24 #include "Ioctl.h"
25
26 #else
27
28 #ifndef __DATAPATH_H_
29 #define __DATAPATH_H_ 1
30
31 /*
32  * Structure of an error message sent as a reply from kernel.
33  */
34 typedef struct _OVS_MESSAGE_ERROR {
35     NL_MSG_HDR nlMsg;
36     NL_MSG_ERR errorMsg;
37 } OVS_MESSAGE_ERROR, *POVS_MESSAGE_ERROR;
38
39 /*
40  * Device operations to tag netlink commands with. This is a bitmask since it
41  * is possible that a particular command can be invoked via different device
42  * operations.
43  */
44 #define OVS_READ_DEV_OP          (1 << 0)
45 #define OVS_WRITE_DEV_OP         (1 << 1)
46 #define OVS_TRANSACTION_DEV_OP   (1 << 2)
47 #define OVS_READ_EVENT_DEV_OP    (1 << 3)
48
49 typedef struct _OVS_DEVICE_EXTENSION {
50     INT numberOpenInstance;
51     INT pidCount;
52 } OVS_DEVICE_EXTENSION, *POVS_DEVICE_EXTENSION;
53
54 /*
55  * Private context for each handle on the device.
56  */
57 typedef struct _OVS_OPEN_INSTANCE {
58     UINT32 cookie;
59     PFILE_OBJECT fileObject;
60     PVOID eventQueue;
61     PVOID packetQueue;
62     UINT32 pid;
63
64     /*
65      * On platforms that support netlink natively, there's generally some form of
66      * serialization between concurrent calls to netlink sockets. However, OVS
67      * userspace guarantees that a given netlink handle is not concurrently used.
68      * Despite this, we do want to have some basic checks in the kernel to make
69      * sure that things don't break if there are concurrent calls.
70      *
71      * This is generally not an issue since kernel data structure access should
72      * be sychronized anyway. Only reason to have this safeguared is to protect
73      * the state in "state-aware" read calls which rely on previous state. This
74      * restriction might go away as the userspace code gets implemented.
75      */
76      INT inUse;
77
78     struct {
79         POVS_MESSAGE ovsMsg;    /* OVS message passed during dump start. */
80         UINT32 index[2];        /* markers to continue dump from. One or more
81                                  * of them may be used. Eg. in flow dump, the
82                                  * markers can store the row and the column
83                                  * indices. */
84     } dumpState;                /* data to support dump commands. */
85 } OVS_OPEN_INSTANCE, *POVS_OPEN_INSTANCE;
86
87 NDIS_STATUS OvsCreateDeviceObject(NDIS_HANDLE ovsExtDriverHandle);
88 VOID OvsDeleteDeviceObject();
89
90 POVS_OPEN_INSTANCE OvsGetOpenInstance(PFILE_OBJECT fileObject,
91                                       UINT32 dpNo);
92
93 NTSTATUS OvsCompleteIrpRequest(PIRP irp, ULONG_PTR infoPtr, NTSTATUS status);
94
95 VOID OvsAcquireCtrlLock();
96 VOID OvsReleaseCtrlLock();
97
98 /* XXX: Move this to netlink.[ch] eventually. */
99 VOID BuildReplyMsgFromMsgIn(POVS_MESSAGE msgIn, POVS_MESSAGE msgOut,
100                             UINT16 flags);
101 VOID BuildErrorMsg(POVS_MESSAGE msgIn, POVS_MESSAGE_ERROR msgOut,
102                    UINT errorCode);
103
104 /*
105  * Utility structure and functions to collect in one place all the parameters
106  * passed during a call from userspace.
107  */
108 typedef struct _OVS_USER_PARAMS_CONTEXT {
109     PIRP irp;            /* The IRP used for the userspace call. */
110     POVS_OPEN_INSTANCE ovsInstance; /* Private data of the device handle. */
111     UINT32 devOp;        /* Device operation of the userspace call. */
112     POVS_MESSAGE ovsMsg; /* OVS message that userspace passed down. */
113     PVOID inputBuffer;   /* Input data specified by userspace. Maybe NULL. */
114     UINT32 inputLength;  /* Length of input buffer. */
115     PVOID outputBuffer;  /* Output buffer specified by userspace for reading
116                           * data. Maybe NULL. */
117     UINT32 outputLength; /* Length of output buffer. */
118 } OVS_USER_PARAMS_CONTEXT, *POVS_USER_PARAMS_CONTEXT;
119
120 static __inline VOID
121 InitUserParamsCtx(PIRP irp,
122                   POVS_OPEN_INSTANCE ovsInstance,
123                   UINT32 devOp,
124                   POVS_MESSAGE ovsMsg,
125                   PVOID inputBuffer,
126                   UINT32 inputLength,
127                   PVOID outputBuffer,
128                   UINT32 outputLength,
129                   POVS_USER_PARAMS_CONTEXT usrParamsCtx)
130 {
131     usrParamsCtx->irp = irp;
132     usrParamsCtx->ovsInstance = ovsInstance;
133     usrParamsCtx->devOp = devOp;
134     usrParamsCtx->ovsMsg = ovsMsg;
135     usrParamsCtx->inputBuffer = inputBuffer;
136     usrParamsCtx->inputLength = inputLength;
137     usrParamsCtx->outputBuffer = outputBuffer;
138     usrParamsCtx->outputLength = outputLength;
139 }
140
141 static __inline NTSTATUS
142 InitUserDumpState(POVS_OPEN_INSTANCE instance,
143                   POVS_MESSAGE ovsMsg)
144 {
145     /* Clear the dumpState from a previous dump sequence. */
146     ASSERT(instance->dumpState.ovsMsg == NULL);
147     ASSERT(ovsMsg);
148
149     instance->dumpState.ovsMsg =
150         (POVS_MESSAGE) OvsAllocateMemory(sizeof (OVS_MESSAGE));
151     if (instance->dumpState.ovsMsg == NULL) {
152         return STATUS_NO_MEMORY;
153     }
154     RtlCopyMemory(instance->dumpState.ovsMsg, ovsMsg,
155                   sizeof *instance->dumpState.ovsMsg);
156     RtlZeroMemory(instance->dumpState.index,
157                   sizeof instance->dumpState.index);
158
159     return STATUS_SUCCESS;
160 }
161
162 static __inline VOID
163 FreeUserDumpState(POVS_OPEN_INSTANCE instance)
164 {
165     if (instance->dumpState.ovsMsg != NULL) {
166         OvsFreeMemory(instance->dumpState.ovsMsg);
167         RtlZeroMemory(&instance->dumpState, sizeof instance->dumpState);
168     }
169 }
170
171 NTSTATUS OvsSetupDumpStart(POVS_USER_PARAMS_CONTEXT usrParamsCtx);
172
173 #endif /* __DATAPATH_H_ */
174
175 #endif /* OVS_USE_NL_INTERFACE */