3bc20eee4b81c344a8d7b594ee4f429a6998d92d
[cascardo/ovs.git] / datapath-windows / ovsext / Switch.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  * This file contains the definition of the switch object for the OVS.
19  */
20
21 #ifndef __SWITCH_H_
22 #define __SWITCH_H_ 1
23
24 #include "NetProto.h"
25 #include "BufferMgmt.h"
26 #include "TunnelIntf.h"
27 #define OVS_MAX_VPORT_ARRAY_SIZE 1024
28 #define OVS_MAX_PID_ARRAY_SIZE   1024
29
30 #define OVS_VPORT_MASK (OVS_MAX_VPORT_ARRAY_SIZE - 1)
31 #define OVS_PID_MASK (OVS_MAX_PID_ARRAY_SIZE - 1)
32
33 #define OVS_INTERNAL_VPORT_DEFAULT_INDEX 0
34
35 //Tunnel port indicies
36 #define RESERVED_START_INDEX1    1
37 #define OVS_TUNNEL_INDEX_START RESERVED_START_INDEX1
38 #define OVS_VXLAN_VPORT_INDEX    2
39 #define OVS_GRE_VPORT_INDEX      3
40 #define OVS_GRE64_VPORT_INDEX    4
41 #define OVS_TUNNEL_INDEX_END OVS_GRE64_VPORT_INDEX
42
43 #define OVS_MAX_PHYS_ADAPTERS    32
44 #define OVS_MAX_IP_VPOR          32
45
46 #define OVS_HASH_BASIS   0x13578642
47
48 typedef struct _OVS_VPORT_ENTRY *POVS_VPORT_ENTRY;
49
50 typedef struct _OVS_DATAPATH
51 {
52    PLIST_ENTRY             flowTable;       // Contains OvsFlows.
53    UINT32                  nFlows;          // Number of entries in flowTable.
54
55    // List_Links              queues[64];      // Hash table of queue IDs.
56
57    /* Statistics. */
58    UINT64                  hits;            // Number of flow table hits.
59    UINT64                  misses;          // Number of flow table misses.
60    UINT64                  lost;            // Number of dropped misses.
61
62    /* Used to protect the flows in the flowtable. */
63    PNDIS_RW_LOCK_EX        lock;
64 } OVS_DATAPATH, *POVS_DATAPATH;
65
66 /*
67  * OVS_SWITCH_CONTEXT
68  *
69  * The context allocated per switch., For OVS, we only
70  * support one switch which corresponding to one datapath.
71  * Each datapath can have multiple logical bridges configured
72  * which is maintained by vswitchd.
73  */
74
75 typedef enum OVS_SWITCH_DATAFLOW_STATE
76 {
77     OvsSwitchPaused,
78     OvsSwitchRunning
79 } OVS_SWITCH_DATAFLOW_STATE, *POVS_SWITCH_DATAFLOW_STATE;
80
81 typedef enum OVS_SWITCH_CONTROFLOW_STATE
82 {
83     OvsSwitchUnknown,
84     OvsSwitchAttached,
85     OvsSwitchDetached
86 } OVS_SWITCH_CONTROLFLOW_STATE, *POVS_SWITCH_CONTROLFLOW_STATE;
87
88 // XXX: Take care of alignment and grouping members by cacheline
89 typedef struct _OVS_SWITCH_CONTEXT
90 {
91     /* Coarse and fine-grained switch states. */
92     OVS_SWITCH_DATAFLOW_STATE dataFlowState;
93     OVS_SWITCH_CONTROLFLOW_STATE controlFlowState;
94     BOOLEAN                 isActivated;
95     BOOLEAN                 isActivateFailed;
96
97     UINT32                  dpNo;
98
99     /*
100      * 'virtualExternalVport' represents default external interface. This is
101      * a virtual interface. The friendly name of such an interface has
102      * been observed to be: "Microsoft Default External Interface". This NIC
103      * has 'NicIndex' == 0.
104      *
105      * The "real" physical external NIC has 'NicIndex' > 0. For each
106      * external interface, virtual or physical, NDIS gives an NIC level
107      * OID callback. Note that, even though there are multile "NICs",
108      * there's only one underlying Hyper-V port. Thus, we get a single
109      * NDIS port-level callback, but multiple NDIS NIC-level callbacks.
110      *
111      * The virtual external NIC can be accessed at 'virtualExternalVport', and
112      * is assigned the name "external.defaultAdapter". The virtual external
113      * NIC is not inserted into the 'portIdHashArray' since the port must not
114      * be exposed to OVS userspace.
115      *
116      * The physical external NICs are assigned names "external.%INDEX%",
117      * where '%INDEX%' represents the 'NicIndex' of the NIC.
118      *
119      * While adding a physical external NIC in OvsInitConfiguredSwitchNics(),
120      * some required properties of the vport are available only at the
121      * NDIS port-level. So, these are copied from 'virtualExternalVport'.
122      * The vport created for the physical external NIC is inserted into the
123      * 'portIdHashArray'.
124      *
125      * When the virtual external NIC is torn down or deleted, the
126      * corresponding physical external ports are also torn down or
127      * deleted. The number of physical external NICs is tracked by
128      * 'numPhysicalNics'.
129      */
130     NDIS_SWITCH_PORT_ID     virtualExternalPortId;
131     NDIS_SWITCH_PORT_ID     internalPortId;
132     POVS_VPORT_ENTRY        virtualExternalVport;   // the virtual adapter vport
133     POVS_VPORT_ENTRY        internalVport;
134
135     /*
136      * 'portIdHashArray' ONLY contains ports that exist on the Hyper-V switch,
137      * namely: VIF (vNIC) ports, external port and Hyper-V internal port.
138      * 'numHvVports' counts the ports in 'portIdHashArray'. If a port got
139      * deleted on the Hyper-V switch, it gets deleted from 'portIdHashArray'.
140      * The port itself will not get deallocated if it has been added from OVS
141      * userspace. 'numHvVports' is decremented when the port is deallocated.
142      *
143      * 'portNoHashArray' ONLY contains ports that are added from OVS userspace,
144      * regardless of whether that port exists on the Hyper-V switch or not.
145      * Tunnel ports and bridge-internal ports are examples of ports that do not
146      * exist on the Hyper-V switch, and 'numNonHvVports' counts such ports in
147      * 'portNoHashArray'.
148      *
149      * 'tunnelVportsArray' contains tunnel ports that are added from OVS
150      * userspace. Currently only VXLAN tunnels are added in this list.
151      *
152      * 'ovsPortNameHashArray' contains the same entries as 'portNoHashArray' but
153      * hashed on a different key.
154      */
155     PLIST_ENTRY             portIdHashArray;        // based on Hyper-V portId
156     PLIST_ENTRY             portNoHashArray;        // based on ovs port number
157     PLIST_ENTRY             tunnelVportsArray;      // based on ovs dst port number
158     PLIST_ENTRY             ovsPortNameHashArray;   // based on ovsName
159     PLIST_ENTRY             pidHashArray;           // based on packet pids
160     NDIS_SPIN_LOCK          pidHashLock;            // Lock for pidHash table
161
162     UINT32                  numPhysicalNics;        // the number of physical
163                                                     // external NICs.
164     UINT32                  numHvVports;
165     UINT32                  numNonHvVports;
166
167     /* Lock taken over the switch. This protects the ports on the switch. */
168     PNDIS_RW_LOCK_EX        dispatchLock;
169
170     /* The flowtable. */
171     OVS_DATAPATH            datapath;
172
173     /* Handle to the OVSExt filter driver. Same as 'gOvsExtDriverHandle'. */
174     NDIS_HANDLE NdisFilterHandle;
175
176     /* Handle and callbacks exposed by the underlying hyper-v switch. */
177     NDIS_SWITCH_CONTEXT NdisSwitchContext;
178     NDIS_SWITCH_OPTIONAL_HANDLERS NdisSwitchHandlers;
179
180     volatile LONG pendingInjectedNblCount;
181     volatile LONG pendingOidCount;
182
183     OVS_NBL_POOL            ovsPool;
184 } OVS_SWITCH_CONTEXT, *POVS_SWITCH_CONTEXT;
185
186
187 static __inline VOID
188 OvsAcquireDatapathRead(OVS_DATAPATH *datapath,
189                        LOCK_STATE_EX *lockState,
190                        BOOLEAN dispatch)
191 {
192     ASSERT(datapath);
193     NdisAcquireRWLockRead(datapath->lock, lockState,
194                           dispatch ? NDIS_RWL_AT_DISPATCH_LEVEL : 0);
195 }
196
197 static __inline VOID
198 OvsAcquireDatapathWrite(OVS_DATAPATH *datapath,
199                         LOCK_STATE_EX *lockState,
200                         BOOLEAN dispatch)
201 {
202     ASSERT(datapath);
203     NdisAcquireRWLockWrite(datapath->lock, lockState,
204                            dispatch ? NDIS_RWL_AT_DISPATCH_LEVEL : 0);
205 }
206
207 static __inline VOID
208 OvsReleaseDatapath(OVS_DATAPATH *datapath,
209                    LOCK_STATE_EX *lockState)
210 {
211     ASSERT(datapath);
212     NdisReleaseRWLock(datapath->lock, lockState);
213 }
214
215 BOOLEAN
216 OvsAcquireSwitchContext(VOID);
217
218 VOID
219 OvsReleaseSwitchContext(POVS_SWITCH_CONTEXT switchContext);
220
221 #endif /* __SWITCH_H_ */