datapath-windows: Removed hardcoded names for internal/external vports
[cascardo/ovs.git] / datapath-windows / ovsext / Vport.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 #ifndef __VPORT_H_
18 #define __VPORT_H_ 1
19
20 #include "Switch.h"
21 #include "VxLan.h"
22 #include "Stt.h"
23
24 #define OVS_MAX_DPPORTS             MAXUINT16
25 #define OVS_DPPORT_NUMBER_INVALID   OVS_MAX_DPPORTS
26 /*
27  * The local port (0) is a reserved port, that is not allowed to be be
28  * created by the netlink command vport add. On linux, this port is created
29  * at netlink command datapath new. However, on windows, we do not need to
30  * create it, and more, we shouldn't. The userspace attempts to create two
31  * internal vports, the LOCAL port (0) and the internal port (with any other
32  * port number). The non-LOCAL internal port is used in the userspace when it
33  * requests the internal port.
34  */
35 #define OVS_DPPORT_NUMBER_LOCAL    0
36
37 /*
38  * A Vport, or Virtual Port, is a port on the OVS. It can be one of the
39  * following types. Some of the Vports are "real" ports on the hyper-v switch,
40  * and some are not:
41  * - VIF port (VM's NIC)
42  * - External Adapters (physical NIC)
43  * - Internal Adapter (Virtual adapter exposed on the host).
44  * - Tunnel ports created by OVS userspace.
45  */
46
47 typedef enum {
48     OVS_STATE_UNKNOWN,
49     OVS_STATE_PORT_CREATED,
50     OVS_STATE_NIC_CREATED,
51     OVS_STATE_CONNECTED,
52     OVS_STATE_PORT_TEAR_DOWN,
53     OVS_STATE_PORT_DELETED,
54 } OVS_VPORT_STATE;
55
56 typedef struct _OVS_VPORT_STATS {
57     UINT64 rxPackets;
58     UINT64 txPackets;
59     UINT64 rxBytes;
60     UINT64 txBytes;
61 } OVS_VPORT_STATS;
62
63 typedef struct _OVS_VPORT_ERR_STATS {
64     UINT64  rxErrors;
65     UINT64  txErrors;
66     UINT64  rxDropped;
67     UINT64  txDropped;
68 } OVS_VPORT_ERR_STATS;
69
70 /* used for vport netlink commands. */
71 typedef struct _OVS_VPORT_FULL_STATS {
72     OVS_VPORT_STATS;
73     OVS_VPORT_ERR_STATS;
74 }OVS_VPORT_FULL_STATS;
75 /*
76  * Each internal, external adapter or vritual adapter has
77  * one vport entry. In addition, we have one vport for each
78  * tunnel type, such as vxlan, gre
79  */
80 typedef struct _OVS_VPORT_ENTRY {
81     LIST_ENTRY             ovsNameLink;
82     LIST_ENTRY             portIdLink;
83     LIST_ENTRY             portNoLink;
84     LIST_ENTRY             tunnelVportLink;
85
86     OVS_VPORT_STATE        ovsState;
87     OVS_VPORT_TYPE         ovsType;
88     OVS_VPORT_STATS        stats;
89     OVS_VPORT_ERR_STATS    errStats;
90     UINT32                 portNo;
91     UINT32                 mtu;
92     /* ovsName is the ovs (datapath) port name - it is null terminated. */
93     CHAR                   ovsName[OVS_MAX_PORT_NAME_LENGTH];
94
95     PVOID                  priv;
96     NDIS_SWITCH_PORT_ID    portId;
97     NDIS_SWITCH_NIC_INDEX  nicIndex;
98     UINT16                 numaNodeId;
99     NDIS_SWITCH_PORT_STATE portState;
100     NDIS_SWITCH_NIC_STATE  nicState;
101     NDIS_SWITCH_PORT_TYPE  portType;
102
103     UINT8                  permMacAddress[ETH_ADDR_LEN];
104     UINT8                  currMacAddress[ETH_ADDR_LEN];
105     UINT8                  vmMacAddress[ETH_ADDR_LEN];
106
107     NDIS_SWITCH_PORT_NAME  hvPortName;
108     IF_COUNTED_STRING      portFriendlyName;
109     NDIS_SWITCH_NIC_NAME   nicName;
110     NDIS_VM_NAME           vmName;
111     GUID                   netCfgInstanceId;
112     /*
113      * OVS userpace has a notion of bridges which basically defines an
114      * L2-domain. Each "bridge" has an "internal" port of type
115      * OVS_VPORT_TYPE_INTERNAL. Such a port is connected to the OVS datapath in
116      * one end, and the other end is a virtual adapter on the hypervisor host.
117      * This is akin to the Hyper-V "internal" NIC. It is intuitive to map the
118      * Hyper-V "internal" NIC to the OVS bridge's "internal" port, but there's
119      * only one Hyper-V NIC but multiple bridges. To support multiple OVS bridge
120      * "internal" ports, we use the flag 'isBridgeInternal' in each vport. We
121      * support addition of multiple bridge-internal ports. A vport with
122      * 'isBridgeInternal' == TRUE is a dummy port and has no backing currently.
123      * If a flow actions specifies the output port to be a bridge-internal port,
124      * the port is silently ignored.
125      */
126     BOOLEAN                isBridgeInternal;
127     BOOLEAN                isExternal;
128     UINT32                 upcallPid; /* netlink upcall port id */
129     PNL_ATTR               portOptions;
130     BOOLEAN                isAbsentOnHv; /* Is this port present on the
131                                              Hyper-V switch? */
132 } OVS_VPORT_ENTRY, *POVS_VPORT_ENTRY;
133
134 struct _OVS_SWITCH_CONTEXT;
135
136 POVS_VPORT_ENTRY OvsFindVportByPortNo(POVS_SWITCH_CONTEXT switchContext,
137                                       UINT32 portNo);
138 /* "name" is null-terminated */
139 POVS_VPORT_ENTRY OvsFindVportByOvsName(POVS_SWITCH_CONTEXT switchContext,
140                                        PSTR name);
141 POVS_VPORT_ENTRY OvsFindVportByHvNameA(POVS_SWITCH_CONTEXT switchContext,
142                                        PSTR name);
143 POVS_VPORT_ENTRY OvsFindVportByPortIdAndNicIndex(POVS_SWITCH_CONTEXT switchContext,
144                                                  NDIS_SWITCH_PORT_ID portId,
145                                                  NDIS_SWITCH_NIC_INDEX index);
146 POVS_VPORT_ENTRY OvsFindTunnelVportByDstPort(POVS_SWITCH_CONTEXT switchContext,
147                                              UINT16 dstPort,
148                                              OVS_VPORT_TYPE ovsVportType);
149
150 NDIS_STATUS OvsAddConfiguredSwitchPorts(struct _OVS_SWITCH_CONTEXT *switchContext);
151 NDIS_STATUS OvsInitConfiguredSwitchNics(struct _OVS_SWITCH_CONTEXT *switchContext);
152
153 VOID OvsClearAllSwitchVports(struct _OVS_SWITCH_CONTEXT *switchContext);
154
155 NDIS_STATUS HvCreateNic(POVS_SWITCH_CONTEXT switchContext,
156                         PNDIS_SWITCH_NIC_PARAMETERS nicParam);
157 NDIS_STATUS HvCreatePort(POVS_SWITCH_CONTEXT switchContext,
158                          PNDIS_SWITCH_PORT_PARAMETERS portParam);
159 NDIS_STATUS HvUpdatePort(POVS_SWITCH_CONTEXT switchContext,
160                          PNDIS_SWITCH_PORT_PARAMETERS portParam);
161 VOID HvTeardownPort(POVS_SWITCH_CONTEXT switchContext,
162                     PNDIS_SWITCH_PORT_PARAMETERS portParam);
163 VOID HvDeletePort(POVS_SWITCH_CONTEXT switchContext,
164                   PNDIS_SWITCH_PORT_PARAMETERS portParam);
165 VOID HvConnectNic(POVS_SWITCH_CONTEXT switchContext,
166                   PNDIS_SWITCH_NIC_PARAMETERS nicParam);
167 VOID HvUpdateNic(POVS_SWITCH_CONTEXT switchContext,
168                  PNDIS_SWITCH_NIC_PARAMETERS nicParam);
169 VOID HvDeleteNic(POVS_SWITCH_CONTEXT switchContext,
170                  PNDIS_SWITCH_NIC_PARAMETERS nicParam);
171 VOID HvDisconnectNic(POVS_SWITCH_CONTEXT switchContext,
172                      PNDIS_SWITCH_NIC_PARAMETERS nicParam);
173
174 static __inline BOOLEAN
175 OvsIsTunnelVportType(OVS_VPORT_TYPE ovsType)
176 {
177     return ovsType == OVS_VPORT_TYPE_VXLAN ||
178            ovsType == OVS_VPORT_TYPE_STT ||
179            ovsType == OVS_VPORT_TYPE_GRE;
180 }
181
182
183 static __inline PVOID
184 GetOvsVportPriv(POVS_VPORT_ENTRY ovsVport)
185 {
186     return ovsVport->priv;
187 }
188
189 static __inline BOOLEAN
190 OvsIsInternalVportType(OVS_VPORT_TYPE ovsType)
191 {
192     return ovsType == OVS_VPORT_TYPE_INTERNAL;
193 }
194
195 static __inline BOOLEAN
196 OvsIsBridgeInternalVport(POVS_VPORT_ENTRY vport)
197 {
198     if (vport->isBridgeInternal) {
199        ASSERT(vport->ovsType == OVS_VPORT_TYPE_INTERNAL);
200     }
201     return vport->isBridgeInternal == TRUE;
202 }
203
204 NTSTATUS OvsRemoveAndDeleteVport(PVOID usrParamsCtx,
205                                  POVS_SWITCH_CONTEXT switchContext,
206                                  POVS_VPORT_ENTRY vport,
207                                  BOOLEAN hvDelete, BOOLEAN ovsDelete);
208 static __inline POVS_VPORT_ENTRY
209 OvsGetExternalVport(POVS_SWITCH_CONTEXT switchContext)
210 {
211     return switchContext->virtualExternalVport;
212 }
213
214 static __inline UINT32
215 OvsGetExternalMtu(POVS_SWITCH_CONTEXT switchContext)
216 {
217     ASSERT(OvsGetExternalVport(switchContext));
218     return ((POVS_VPORT_ENTRY) OvsGetExternalVport(switchContext))->mtu;
219 }
220
221 static __inline UINT16
222 GetPortFromPriv(POVS_VPORT_ENTRY vport)
223 {
224     UINT16 dstPort = 0;
225     PVOID vportPriv = GetOvsVportPriv(vport);
226
227     /* XXX would better to have a commom tunnel "parent" structure */
228     ASSERT(vportPriv);
229     switch(vport->ovsType) {
230     case OVS_VPORT_TYPE_VXLAN:
231         dstPort = ((POVS_VXLAN_VPORT)vportPriv)->dstPort;
232         break;
233     case OVS_VPORT_TYPE_STT:
234         dstPort = ((POVS_STT_VPORT)vportPriv)->dstPort;
235         break;
236     default:
237         ASSERT(! "Port is not a tunnel port");
238     }
239     ASSERT(dstPort);
240     return dstPort;
241 }
242
243 NDIS_STATUS InitOvsVportCommon(POVS_SWITCH_CONTEXT switchContext,
244                                POVS_VPORT_ENTRY vport);
245 NTSTATUS OvsInitTunnelVport(PVOID usrParamsCtx, POVS_VPORT_ENTRY vport,
246                             OVS_VPORT_TYPE ovsType, UINT16 dstport);
247 NTSTATUS OvsInitBridgeInternalVport(POVS_VPORT_ENTRY vport);
248
249 POVS_VPORT_ENTRY OvsAllocateVport(VOID);
250
251 #endif /* __VPORT_H_ */