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