]> git.proxmox.com Git - mirror_ovs.git/blob - datapath-windows/ovsext/Conntrack.h
bc6580d7083cd7a15e9334ea25c9daa4418b50e6
[mirror_ovs.git] / datapath-windows / ovsext / Conntrack.h
1 /*
2 * Copyright (c) 2015, 2016 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 __OVS_CONNTRACK_H_
18 #define __OVS_CONNTRACK_H_ 1
19
20 #include "precomp.h"
21 #include "Actions.h"
22 #include "Debug.h"
23 #include "Flow.h"
24 #include <stddef.h>
25
26 #ifdef OVS_DBG_MOD
27 #undef OVS_DBG_MOD
28 #endif
29 #define OVS_DBG_MOD OVS_DBG_CONTRK
30
31 struct ct_addr {
32 union {
33 ovs_be32 ipv4;
34 struct in6_addr ipv6;
35 uint32_t ipv4_aligned;
36 struct in6_addr ipv6_aligned;
37 };
38 };
39
40 struct ct_endpoint {
41 struct ct_addr addr;
42 union {
43 struct {
44 ovs_be16 port;
45 uint16 pad_port;
46 };
47 struct {
48 ovs_be16 icmp_id;
49 uint8_t icmp_type;
50 uint8_t icmp_code;
51 };
52 };
53 };
54
55 typedef enum CT_UPDATE_RES {
56 CT_UPDATE_INVALID,
57 CT_UPDATE_VALID,
58 CT_UPDATE_NEW,
59 } CT_UPDATE_RES;
60
61 /* Metadata mark for masked write to conntrack mark */
62 typedef struct MD_MARK {
63 UINT32 value;
64 UINT32 mask;
65 } MD_MARK;
66
67 /* Metadata label for masked write to conntrack label. */
68 typedef struct MD_LABELS {
69 struct ovs_key_ct_labels value;
70 struct ovs_key_ct_labels mask;
71 } MD_LABELS;
72
73 typedef enum _NAT_ACTION {
74 NAT_ACTION_NONE = 0,
75 NAT_ACTION_REVERSE = 1 << 0,
76 NAT_ACTION_SRC = 1 << 1,
77 NAT_ACTION_SRC_PORT = 1 << 2,
78 NAT_ACTION_DST = 1 << 3,
79 NAT_ACTION_DST_PORT = 1 << 4,
80 } NAT_ACTION;
81
82 typedef struct _OVS_CT_KEY {
83 struct ct_endpoint src;
84 struct ct_endpoint dst;
85 UINT16 dl_type;
86 UINT8 nw_proto;
87 UINT16 zone;
88 UINT64 packetCount;
89 UINT64 byteCount;
90 } OVS_CT_KEY, *POVS_CT_KEY;
91
92 typedef struct _NAT_ACTION_INFO {
93 struct ct_addr minAddr;
94 struct ct_addr maxAddr;
95 uint16_t minPort;
96 uint16_t maxPort;
97 uint16_t natAction;
98 } NAT_ACTION_INFO, *PNAT_ACTION_INFO;
99
100 typedef struct OVS_CT_ENTRY {
101 NDIS_SPIN_LOCK lock; /* Protects OVS_CT_ENTRY. */
102 OVS_CT_KEY key;
103 OVS_CT_KEY rev_key;
104 UINT64 expiration;
105 LIST_ENTRY link;
106 UINT32 mark;
107 UINT64 timestampStart;
108 struct ovs_key_ct_labels labels;
109 NAT_ACTION_INFO natInfo;
110 PVOID parent; /* Points to main connection */
111 } OVS_CT_ENTRY, *POVS_CT_ENTRY;
112
113 typedef struct OVS_CT_REL_ENTRY {
114 OVS_CT_KEY key;
115 POVS_CT_ENTRY parent;
116 UINT64 expiration;
117 LIST_ENTRY link;
118 } OVS_CT_REL_ENTRY, *POVS_CT_REL_ENTRY;
119
120 typedef struct _OVS_CT_THREAD_CTX {
121 KEVENT event;
122 PVOID threadObject;
123 UINT32 exit;
124 } OVS_CT_THREAD_CTX, *POVS_CT_THREAD_CTX;
125
126 typedef struct OvsConntrackKeyLookupCtx {
127 OVS_CT_KEY key;
128 POVS_CT_ENTRY entry;
129 UINT32 hash;
130 BOOLEAN reply;
131 BOOLEAN related;
132 } OvsConntrackKeyLookupCtx;
133
134 /* Per zone strucuture. */
135 typedef struct _OVS_CT_ZONE_INFO {
136 ULONG limit;
137 ULONG entries;
138 } OVS_CT_ZONE_INFO, *POVS_CT_ZONE_INFO;
139
140 typedef struct _OVS_CT_ZONE_LIMIT {
141 int zone_id;
142 ULONG limit;
143 ULONG count;
144 } OVS_CT_ZONE_LIMIT, *POVS_CT_ZONE_LIMIT;
145
146 #define CT_MAX_ENTRIES 1 << 21
147 #define CT_HASH_TABLE_SIZE ((UINT32)1 << 10)
148 #define CT_HASH_TABLE_MASK (CT_HASH_TABLE_SIZE - 1)
149 #define CT_INTERVAL_SEC 10000000LL //1s
150 #define CT_ENTRY_TIMEOUT (2 * 60 * CT_INTERVAL_SEC) // 2m
151 #define CT_CLEANUP_INTERVAL (2 * 60 * CT_INTERVAL_SEC) // 2m
152
153
154 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
155 the STRUCT object. */
156 #define CONTAINER_OF(POINTER, STRUCT, MEMBER) \
157 ((STRUCT *) (void *) ((char *) (POINTER) - \
158 offsetof (STRUCT, MEMBER)))
159
160 static __inline void
161 OvsConntrackUpdateExpiration(OVS_CT_ENTRY *ctEntry,
162 long long now,
163 long long interval)
164 {
165 ctEntry->expiration = now + interval;
166 }
167
168 static const TCPHdr*
169 OvsGetTcpHeader(PNET_BUFFER_LIST nbl,
170 OVS_PACKET_HDR_INFO *layers,
171 VOID *storage,
172 UINT32 *tcpPayloadLen)
173 {
174 IPHdr *ipHdr;
175 TCPHdr *tcp;
176 VOID *dest = storage;
177
178 ipHdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl),
179 layers->l4Offset + sizeof(TCPHdr),
180 NULL, 1 /*no align*/, 0);
181 if (ipHdr == NULL) {
182 return NULL;
183 }
184
185 ipHdr = (IPHdr *)((PCHAR)ipHdr + layers->l3Offset);
186 tcp = (TCPHdr *)((PCHAR)ipHdr + ipHdr->ihl * 4);
187 if (tcp->doff * 4 >= sizeof *tcp) {
188 NdisMoveMemory(dest, tcp, sizeof(TCPHdr));
189 *tcpPayloadLen = TCP_DATA_LENGTH(ipHdr, tcp);
190 return storage;
191 }
192
193 return NULL;
194 }
195
196 VOID OvsCleanupConntrack(VOID);
197 NTSTATUS OvsInitConntrack(POVS_SWITCH_CONTEXT context);
198
199 NDIS_STATUS OvsExecuteConntrackAction(OvsForwardingContext *fwdCtx,
200 OvsFlowKey *key,
201 const PNL_ATTR a);
202 BOOLEAN OvsConntrackValidateTcpPacket(const TCPHdr *tcp);
203 BOOLEAN OvsConntrackValidateIcmpPacket(const ICMPHdr *icmp);
204 OVS_CT_ENTRY * OvsConntrackCreateTcpEntry(const TCPHdr *tcp,
205 UINT64 now,
206 UINT32 tcpPayloadLen);
207 NDIS_STATUS OvsCtMapTcpProtoInfoToNl(PNL_BUFFER nlBuf,
208 OVS_CT_ENTRY *conn_);
209 OVS_CT_ENTRY * OvsConntrackCreateOtherEntry(UINT64 now);
210 OVS_CT_ENTRY * OvsConntrackCreateIcmpEntry(UINT64 now);
211 enum CT_UPDATE_RES OvsConntrackUpdateTcpEntry(OVS_CT_ENTRY* conn_,
212 const TCPHdr *tcp,
213 BOOLEAN reply,
214 UINT64 now,
215 UINT32 tcpPayloadLen);
216 enum CT_UPDATE_RES OvsConntrackUpdateOtherEntry(OVS_CT_ENTRY *conn_,
217 BOOLEAN reply,
218 UINT64 now);
219 enum CT_UPDATE_RES OvsConntrackUpdateIcmpEntry(OVS_CT_ENTRY* conn_,
220 BOOLEAN reply,
221 UINT64 now);
222 NTSTATUS OvsCreateNlMsgFromCtEntry(POVS_CT_ENTRY entry,
223 PVOID outBuffer,
224 UINT32 outBufLen,
225 UINT8 eventType,
226 UINT32 nlmsgSeq,
227 UINT32 nlmsgPid,
228 UINT8 nfGenVersion,
229 UINT32 dpIfIndex);
230
231 /* Tracking related connections */
232 NTSTATUS OvsInitCtRelated(POVS_SWITCH_CONTEXT context);
233 VOID OvsCleanupCtRelated(VOID);
234 NDIS_STATUS OvsCtRelatedEntryCreate(UINT8 ipProto,
235 UINT16 dl_type,
236 UINT32 serverIp,
237 UINT32 clientIp,
238 UINT16 serverPort,
239 UINT16 clientPort,
240 UINT64 currentTime,
241 POVS_CT_ENTRY parent);
242 POVS_CT_ENTRY OvsCtRelatedLookup(OVS_CT_KEY key, UINT64 currentTime);
243
244 NDIS_STATUS OvsCtHandleFtp(PNET_BUFFER_LIST curNbl,
245 OvsFlowKey *key,
246 OVS_PACKET_HDR_INFO *layers,
247 UINT64 currentTime,
248 POVS_CT_ENTRY entry,
249 BOOLEAN request);
250 #endif /* __OVS_CONNTRACK_H_ */