summaryrefslogtreecommitdiff
path: root/datapath-windows/ovsext/Stt.c
blob: b6272c3c7d6d45d788e417fe7a4c03b0950ff93f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (c) 2015 VMware, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "precomp.h"
#include "NetProto.h"
#include "Switch.h"
#include "Vport.h"
#include "Flow.h"
#include "Stt.h"
#include "IpHelper.h"
#include "Checksum.h"
#include "User.h"
#include "PacketIO.h"
#include "Flow.h"
#include "PacketParser.h"
#include "Atomic.h"
#include "Util.h"

#ifdef OVS_DBG_MOD
#undef OVS_DBG_MOD
#endif
#define OVS_DBG_MOD OVS_DBG_STT
#include "Debug.h"

static NDIS_STATUS
OvsDoEncapStt(POVS_VPORT_ENTRY vport, PNET_BUFFER_LIST curNbl,
              const OvsIPv4TunnelKey *tunKey,
              const POVS_FWD_INFO fwdInfo,
              POVS_PACKET_HDR_INFO layers,
              POVS_SWITCH_CONTEXT switchContext,
              PNET_BUFFER_LIST *newNbl);

/*
 * --------------------------------------------------------------------------
 * OvsInitSttTunnel --
 *    Initialize STT tunnel module.
 * --------------------------------------------------------------------------
 */
NTSTATUS
OvsInitSttTunnel(POVS_VPORT_ENTRY vport,
                 UINT16 tcpDestPort)
{
    POVS_STT_VPORT sttPort;

    sttPort = (POVS_STT_VPORT) OvsAllocateMemoryWithTag(sizeof(*sttPort),
                                                        OVS_STT_POOL_TAG);
    if (!sttPort) {
        OVS_LOG_ERROR("Insufficient memory, can't allocate STT_VPORT");
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    RtlZeroMemory(sttPort, sizeof(*sttPort));
    sttPort->dstPort = tcpDestPort;
    vport->priv = (PVOID) sttPort;
    return STATUS_SUCCESS;
}

/*
 * --------------------------------------------------------------------------
 * OvsCleanupSttTunnel --
 *    Cleanup STT Tunnel module.
 * --------------------------------------------------------------------------
 */
void
OvsCleanupSttTunnel(POVS_VPORT_ENTRY vport)
{
    if (vport->ovsType != OVS_VPORT_TYPE_STT ||
        vport->priv == NULL) {
        return;
    }

    OvsFreeMemoryWithTag(vport->priv, OVS_STT_POOL_TAG);
    vport->priv = NULL;
}

/*
 * --------------------------------------------------------------------------
 * OvsEncapStt --
 *     Encapsulates a packet with an STT header.
 * --------------------------------------------------------------------------
 */
NDIS_STATUS
OvsEncapStt(POVS_VPORT_ENTRY vport,
            PNET_BUFFER_LIST curNbl,
            OvsIPv4TunnelKey *tunKey,
            POVS_SWITCH_CONTEXT switchContext,
            POVS_PACKET_HDR_INFO layers,
            PNET_BUFFER_LIST *newNbl)
{
    OVS_FWD_INFO fwdInfo;
    NDIS_STATUS status;

    UNREFERENCED_PARAMETER(switchContext);
    status = OvsLookupIPFwdInfo(tunKey->dst, &fwdInfo);
    if (status != STATUS_SUCCESS) {
        OvsFwdIPHelperRequest(NULL, 0, tunKey, NULL, NULL, NULL);
        /*
         * XXX This case where the ARP table is not populated is
         * currently not handled
         */
        return NDIS_STATUS_FAILURE;
    }

    status = OvsDoEncapStt(vport, curNbl, tunKey, &fwdInfo, layers,
                           switchContext, newNbl);
    return status;
}

/*
 * --------------------------------------------------------------------------
 * OvsDoEncapStt --
 *    Internal utility function which actually does the STT encap.
 * --------------------------------------------------------------------------
 */
NDIS_STATUS
OvsDoEncapStt(POVS_VPORT_ENTRY vport,
              PNET_BUFFER_LIST curNbl,
              const OvsIPv4TunnelKey *tunKey,
              const POVS_FWD_INFO fwdInfo,
              POVS_PACKET_HDR_INFO layers,
              POVS_SWITCH_CONTEXT switchContext,
              PNET_BUFFER_LIST *newNbl)
{
    NDIS_STATUS status = NDIS_STATUS_SUCCESS;
    PMDL curMdl = NULL;
    PNET_BUFFER curNb;
    PUINT8 buf = NULL;
    EthHdr *outerEthHdr;
    IPHdr *outerIpHdr;
    TCPHdr *outerTcpHdr;
    SttHdr *sttHdr;
    UINT32 innerFrameLen, ipTotalLen;
    POVS_STT_VPORT vportStt;
    UINT32 headRoom = OvsGetSttTunHdrSize();
    UINT32 tcpChksumLen;

    UNREFERENCED_PARAMETER(layers);

    curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
    if (layers->isTcp) {
        NDIS_TCP_LARGE_SEND_OFFLOAD_NET_BUFFER_LIST_INFO lsoInfo;

        lsoInfo.Value = NET_BUFFER_LIST_INFO(curNbl,
                TcpLargeSendNetBufferListInfo);
        if (lsoInfo.LsoV1Transmit.MSS) {
            /* XXX We don't handle LSO yet */
            OVS_LOG_ERROR("LSO on STT is not supported");
            return NDIS_STATUS_FAILURE;
        }
    }

    vportStt = (POVS_STT_VPORT) GetOvsVportPriv(vport);
    ASSERT(vportStt);

    *newNbl = OvsPartialCopyNBL(switchContext, curNbl, 0, headRoom,
                                FALSE /*copy NblInfo*/);
    if (*newNbl == NULL) {
        OVS_LOG_ERROR("Unable to copy NBL");
        return NDIS_STATUS_FAILURE;
    }

    curNbl = *newNbl;
    curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
    /* NB Chain should be split before */
    ASSERT(NET_BUFFER_NEXT_NB(curNb) == NULL);

    innerFrameLen = NET_BUFFER_DATA_LENGTH(curNb);
    /*
     * External port can't be removed as we hold the dispatch lock
     * We also check if the external port was removed beforecalling
     * port encapsulation functions
     */
    if (innerFrameLen > OvsGetExternalMtu(switchContext) - headRoom) {
        OVS_LOG_ERROR("Packet too large (size %d, mtu %d). Can't encapsulate",
                innerFrameLen, OvsGetExternalMtu(switchContext));
        status = NDIS_STATUS_FAILURE;
        goto ret_error;
    }

    status = NdisRetreatNetBufferDataStart(curNb, headRoom, 0, NULL);
    if (status != NDIS_STATUS_SUCCESS) {
        ASSERT(!"Unable to NdisRetreatNetBufferDataStart(headroom)");
        OVS_LOG_ERROR("Unable to NdisRetreatNetBufferDataStart(headroom)");
        goto ret_error;
    }

    /*
     * Make sure that the headroom for the tunnel header is continguous in
     * memory.
     */
    curMdl = NET_BUFFER_CURRENT_MDL(curNb);
    ASSERT((int) (MmGetMdlByteCount(curMdl) - NET_BUFFER_CURRENT_MDL_OFFSET(curNb))
                >= (int) headRoom);

    buf = (PUINT8) MmGetSystemAddressForMdlSafe(curMdl, LowPagePriority);
    if (!buf) {
        ASSERT(!"MmGetSystemAddressForMdlSafe failed");
        OVS_LOG_ERROR("MmGetSystemAddressForMdlSafe failed");
        status = NDIS_STATUS_RESOURCES;
        goto ret_error;
    }

    buf += NET_BUFFER_CURRENT_MDL_OFFSET(curNb);
    outerEthHdr = (EthHdr *)buf;
    outerIpHdr = (IPHdr *) (outerEthHdr + 1);
    outerTcpHdr = (TCPHdr *) (outerIpHdr + 1);
    sttHdr = (SttHdr *) (outerTcpHdr + 1);

    /* L2 header */
    ASSERT(((PCHAR)&fwdInfo->dstMacAddr + sizeof fwdInfo->dstMacAddr) ==
            (PCHAR)&fwdInfo->srcMacAddr);
    NdisMoveMemory(outerEthHdr->Destination, fwdInfo->dstMacAddr,
                    sizeof outerEthHdr->Destination + sizeof outerEthHdr->Source);
    outerEthHdr->Type = htons(ETH_TYPE_IPV4);

    /* L3 header */
    outerIpHdr->ihl = sizeof(IPHdr) >> 2;
    outerIpHdr->version = IPPROTO_IPV4;
    outerIpHdr->tos = tunKey->tos;

    ipTotalLen = sizeof(IPHdr) + sizeof(TCPHdr) + STT_HDR_LEN + innerFrameLen;
    outerIpHdr->tot_len = htons(ipTotalLen);
    ASSERT(ipTotalLen < 65536);

    outerIpHdr->id = (uint16) atomic_add64(&vportStt->ipId, innerFrameLen);
    outerIpHdr->frag_off = (tunKey->flags & OVS_TNL_F_DONT_FRAGMENT) ?
                           IP_DF_NBO : 0;
    outerIpHdr->ttl = tunKey->ttl? tunKey->ttl : 64;
    outerIpHdr->protocol = IPPROTO_TCP;
    outerIpHdr->check = 0;
    outerIpHdr->saddr = fwdInfo->srcIpAddr;
    outerIpHdr->daddr = tunKey->dst;
    outerIpHdr->check = IPChecksum((uint8 *)outerIpHdr, sizeof *outerIpHdr, 0);

    /* L4 header */
    RtlZeroMemory(outerTcpHdr, sizeof *outerTcpHdr);
    outerTcpHdr->source = htons(tunKey->flow_hash | 32768);
    outerTcpHdr->dest = htons(vportStt->dstPort);
    outerTcpHdr->seq = htonl((STT_HDR_LEN + innerFrameLen) <<
                             STT_SEQ_LEN_SHIFT);
    outerTcpHdr->ack_seq = htonl(atomic_inc64(&vportStt->ackNo));
    outerTcpHdr->doff = sizeof(TCPHdr) >> 2;
    outerTcpHdr->psh = 1;
    outerTcpHdr->ack = 1;
    outerTcpHdr->window = (uint16) ~0;

    /* Calculate pseudo header chksum */
    tcpChksumLen = sizeof(TCPHdr) + STT_HDR_LEN + innerFrameLen;
    ASSERT(tcpChksumLen < 65535);
    outerTcpHdr->check = IPPseudoChecksum(&fwdInfo->srcIpAddr,(uint32 *) &tunKey->dst,
                                          IPPROTO_TCP, (uint16) tcpChksumLen);
    sttHdr->version = 0;

    /* XXX need to peek into the inner packet, hard code for now */
    sttHdr->flags = STT_PROTO_IPV4;
    sttHdr->l4Offset = 0;

    sttHdr->reserved = 0;
    /* XXX Used for large TCP packets.Not sure how it is used, clarify */
    sttHdr->mss = 0;
    sttHdr->vlanTCI = 0;
    sttHdr->key = tunKey->tunnelId;
    /* Zero out stt padding */
    *(uint16 *)(sttHdr + 1) = 0;

    /* Calculate software tcp checksum */
    outerTcpHdr->check = CalculateChecksumNB(curNb, (uint16) tcpChksumLen,
                                             sizeof(EthHdr) + sizeof(IPHdr));
    if (outerTcpHdr->check == 0) {
        status = NDIS_STATUS_FAILURE;
        goto ret_error;
    }

    return STATUS_SUCCESS;

ret_error:
    OvsCompleteNBL(switchContext, *newNbl, TRUE);
    *newNbl = NULL;
    return status;
}

/*
 * --------------------------------------------------------------------------
 * OvsDecapStt --
 *     Decapsulates an STT packet.
 * --------------------------------------------------------------------------
 */
NDIS_STATUS
OvsDecapStt(POVS_SWITCH_CONTEXT switchContext,
            PNET_BUFFER_LIST curNbl,
            OvsIPv4TunnelKey *tunKey,
            PNET_BUFFER_LIST *newNbl)
{
    NDIS_STATUS status = NDIS_STATUS_FAILURE;
    PNET_BUFFER curNb;
    IPHdr *ipHdr;
    char *ipBuf[sizeof(IPHdr)];
    SttHdr *sttHdr;
    char *sttBuf[STT_HDR_LEN];
    UINT32 advanceCnt, hdrLen;

    curNb = NET_BUFFER_LIST_FIRST_NB(curNbl);
    ASSERT(NET_BUFFER_NEXT_NB(curNb) == NULL);

    if (NET_BUFFER_DATA_LENGTH(curNb) < OvsGetSttTunHdrSize()) {
        OVS_LOG_ERROR("Packet length received is less than the tunnel header:"
            " %d<%d\n", NET_BUFFER_DATA_LENGTH(curNb), OvsGetSttTunHdrSize());
        return NDIS_STATUS_INVALID_LENGTH;
    }

    /* Skip Eth header */
    hdrLen = sizeof(EthHdr);
    NdisAdvanceNetBufferDataStart(curNb, hdrLen, FALSE, NULL);
    advanceCnt = hdrLen;

    ipHdr = NdisGetDataBuffer(curNb, sizeof *ipHdr, (PVOID) &ipBuf,
                                                    1 /*no align*/, 0);
    ASSERT(ipHdr);

    /* Skip IP & TCP headers */
    hdrLen = sizeof(IPHdr) + sizeof(TCPHdr),
    NdisAdvanceNetBufferDataStart(curNb, hdrLen, FALSE, NULL);
    advanceCnt += hdrLen;

    /* STT Header */
    sttHdr = NdisGetDataBuffer(curNb, sizeof *sttHdr, (PVOID) &sttBuf,
                                                    1 /*no align*/, 0);
    ASSERT(sttHdr);

    /* Initialize the tunnel key */
    tunKey->dst = ipHdr->daddr;
    tunKey->src = ipHdr->saddr;
    tunKey->tunnelId = sttHdr->key;
    tunKey->flags = (OVS_TNL_F_CSUM | OVS_TNL_F_KEY);
    tunKey->tos = ipHdr->tos;
    tunKey->ttl = ipHdr->ttl;
    tunKey->pad = 0;

    /* Skip stt header, DataOffset points to inner pkt now. */
    hdrLen = STT_HDR_LEN;
    NdisAdvanceNetBufferDataStart(curNb, hdrLen, FALSE, NULL);
    advanceCnt += hdrLen;

    *newNbl = OvsPartialCopyNBL(switchContext, curNbl, OVS_DEFAULT_COPY_SIZE,
                                0, FALSE /*copy NBL info*/);

    ASSERT(advanceCnt == OvsGetSttTunHdrSize());
    status = NdisRetreatNetBufferDataStart(curNb, advanceCnt, 0, NULL);

    if (*newNbl == NULL) {
        OVS_LOG_ERROR("OvsDecapStt: Unable to allocate a new cloned NBL");
        status = NDIS_STATUS_RESOURCES;
    }

    return status;
}