VirtualBox

source: vbox/trunk/include/VBox/vmm/pdmnetinline.h@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.3 KB
Line 
1/** @file
2 * PDM - Networking Helpers, Inlined Code. (DEV,++)
3 *
4 * This is all inlined because it's too tedious to create 2-3 libraries to
5 * contain it all (same bad excuse as for intnetinline.h).
6 */
7
8/*
9 * Copyright (C) 2010-2022 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * The contents of this file may alternatively be used under the terms
20 * of the Common Development and Distribution License Version 1.0
21 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
22 * VirtualBox OSE distribution, in which case the provisions of the
23 * CDDL are applicable instead of those of the GPL.
24 *
25 * You may elect to license modified versions of this file under the
26 * terms and conditions of either the GPL or the CDDL or both.
27 */
28
29#ifndef VBOX_INCLUDED_vmm_pdmnetinline_h
30#define VBOX_INCLUDED_vmm_pdmnetinline_h
31#ifndef RT_WITHOUT_PRAGMA_ONCE
32# pragma once
33#endif
34
35
36/*******************************************************************************
37* Header Files *
38*******************************************************************************/
39#include <VBox/log.h>
40#include <VBox/types.h>
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/net.h>
44#include <iprt/string.h>
45
46
47/** @defgroup grp_pdm_net_inline The PDM Networking Helper APIs
48 * @ingroup grp_pdm
49 * @{
50 */
51
52
53/**
54 * Checksum type.
55 */
56typedef enum PDMNETCSUMTYPE
57{
58 /** No checksum. */
59 PDMNETCSUMTYPE_NONE = 0,
60 /** Normal TCP checksum. */
61 PDMNETCSUMTYPE_COMPLETE,
62 /** Checksum on pseudo header (used with GSO). */
63 PDMNETCSUMTYPE_PSEUDO,
64 /** The usual 32-bit hack. */
65 PDMNETCSUMTYPE_32_BIT_HACK = 0x7fffffff
66} PDMNETCSUMTYPE;
67
68
69/**
70 * Validates the GSO context.
71 *
72 * @returns true if valid, false if not (not asserted or logged).
73 * @param pGso The GSO context.
74 * @param cbGsoMax The max size of the GSO context.
75 * @param cbFrame The max size of the GSO frame (use to validate
76 * the MSS).
77 */
78DECLINLINE(bool) PDMNetGsoIsValid(PCPDMNETWORKGSO pGso, size_t cbGsoMax, size_t cbFrame)
79{
80 PDMNETWORKGSOTYPE enmType;
81
82 if (RT_LIKELY(cbGsoMax >= sizeof(*pGso)))
83 { /* likely */ } else return false;
84
85 enmType = (PDMNETWORKGSOTYPE)pGso->u8Type;
86 if (RT_LIKELY( enmType > PDMNETWORKGSOTYPE_INVALID && enmType < PDMNETWORKGSOTYPE_END ))
87 { /* likely */ } else return false;
88
89 /* all types requires both headers. */
90 if (RT_LIKELY( pGso->offHdr1 >= sizeof(RTNETETHERHDR) ))
91 { /* likely */ } else return false;
92 if (RT_LIKELY( pGso->offHdr2 > pGso->offHdr1 ))
93 { /* likely */ } else return false;
94 if (RT_LIKELY( pGso->cbHdrsTotal > pGso->offHdr2 ))
95 { /* likely */ } else return false;
96
97 /* min size of the 1st header(s). */
98 switch (enmType)
99 {
100 case PDMNETWORKGSOTYPE_IPV4_TCP:
101 case PDMNETWORKGSOTYPE_IPV4_UDP:
102 if (RT_LIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 >= RTNETIPV4_MIN_LEN ))
103 { /* likely */ } else return false;
104 break;
105 case PDMNETWORKGSOTYPE_IPV6_TCP:
106 case PDMNETWORKGSOTYPE_IPV6_UDP:
107 if (RT_LIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 >= RTNETIPV6_MIN_LEN ))
108 { /* likely */ } else return false;
109 break;
110 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
111 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
112 if (RT_LIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 >= RTNETIPV4_MIN_LEN + RTNETIPV6_MIN_LEN ))
113 { /* likely */ } else return false;
114 break;
115 case PDMNETWORKGSOTYPE_INVALID:
116 case PDMNETWORKGSOTYPE_END:
117 break;
118 /* no default case! want gcc warnings. */
119 }
120
121 /* min size of the 2nd header. */
122 switch (enmType)
123 {
124 case PDMNETWORKGSOTYPE_IPV4_TCP:
125 case PDMNETWORKGSOTYPE_IPV6_TCP:
126 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
127 if (RT_LIKELY( (unsigned)pGso->cbHdrsTotal - pGso->offHdr2 >= RTNETTCP_MIN_LEN ))
128 { /* likely */ } else return false;
129 break;
130 case PDMNETWORKGSOTYPE_IPV4_UDP:
131 case PDMNETWORKGSOTYPE_IPV6_UDP:
132 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
133 if (RT_LIKELY( (unsigned)pGso->cbHdrsTotal - pGso->offHdr2 >= RTNETUDP_MIN_LEN ))
134 { /* likely */ } else return false;
135 break;
136 case PDMNETWORKGSOTYPE_INVALID:
137 case PDMNETWORKGSOTYPE_END:
138 break;
139 /* no default case! want gcc warnings. */
140 }
141
142 /* There must be at more than one segment. */
143 if (RT_LIKELY( cbFrame > pGso->cbHdrsTotal ))
144 { /* likely */ } else return false;
145 if (RT_LIKELY( cbFrame - pGso->cbHdrsTotal >= pGso->cbMaxSeg ))
146 { /* likely */ } else return false;
147
148 /* Make sure the segment size is enough to fit a UDP header. */
149 if (RT_LIKELY(enmType != PDMNETWORKGSOTYPE_IPV4_UDP || pGso->cbMaxSeg >= RTNETUDP_MIN_LEN))
150 { /* likely */ } else return false;
151
152 return true;
153}
154
155
156/**
157 * Returns the length of header for a particular segment/fragment.
158 *
159 * We cannot simply treat UDP header as a part of payload because we do not
160 * want to modify the payload but still need to modify the checksum field in
161 * UDP header. So we want to include UDP header when calculating the length
162 * of headers in the first segment getting it copied to a temporary buffer
163 * along with other headers.
164 *
165 * @returns Length of headers (including UDP header for the first fragment).
166 * @param pGso The GSO context.
167 * @param iSeg The segment index.
168 */
169DECLINLINE(uint8_t) pdmNetSegHdrLen(PCPDMNETWORKGSO pGso, uint32_t iSeg)
170{
171 return iSeg ? pGso->cbHdrsSeg : pGso->cbHdrsTotal;
172}
173
174/**
175 * Returns the length of payload for a particular segment/fragment.
176 *
177 * The first segment does not contain UDP header. The size of UDP header is
178 * determined as the difference between the total headers size and the size
179 * used during segmentation.
180 *
181 * @returns Length of payload (including UDP header for the first fragment).
182 * @param pGso The GSO context.
183 * @param iSeg The segment that we're carving out (0-based).
184 * @param cSegs The number of segments in the GSO frame.
185 * @param cbFrame The size of the GSO frame.
186 */
187DECLINLINE(uint32_t) pdmNetSegPayloadLen(PCPDMNETWORKGSO pGso, uint32_t iSeg, uint32_t cSegs, uint32_t cbFrame)
188{
189 if (iSeg + 1 == cSegs)
190 return cbFrame - iSeg * pGso->cbMaxSeg - pdmNetSegHdrLen(pGso, iSeg);
191 return pGso->cbMaxSeg - (iSeg ? 0 : pGso->cbHdrsTotal - pGso->cbHdrsSeg);
192}
193
194/**
195 * Calculates the number of segments a GSO frame will be segmented into.
196 *
197 * @returns Segment count.
198 * @param pGso The GSO context.
199 * @param cbFrame The GSO frame size (header proto + payload).
200 */
201DECLINLINE(uint32_t) PDMNetGsoCalcSegmentCount(PCPDMNETWORKGSO pGso, size_t cbFrame)
202{
203 size_t cbPayload;
204 Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
205 cbPayload = cbFrame - pGso->cbHdrsSeg;
206 return (uint32_t)((cbPayload + pGso->cbMaxSeg - 1) / pGso->cbMaxSeg);
207}
208
209
210/**
211 * Used to find the IPv6 header when handling 4to6 tunneling.
212 *
213 * @returns Offset of the IPv6 header.
214 * @param pbSegHdrs The headers / frame start.
215 * @param offIPv4Hdr The offset of the IPv4 header.
216 */
217DECLINLINE(uint8_t) pgmNetGsoCalcIpv6Offset(uint8_t *pbSegHdrs, uint8_t offIPv4Hdr)
218{
219 PCRTNETIPV4 pIPv4Hdr = (PCRTNETIPV4)&pbSegHdrs[offIPv4Hdr];
220 return offIPv4Hdr + pIPv4Hdr->ip_hl * 4;
221}
222
223
224/**
225 * Update an UDP header after carving out a segment
226 *
227 * @param u32PseudoSum The pseudo checksum.
228 * @param pbSegHdrs Pointer to the header bytes / frame start.
229 * @param offUdpHdr The offset into @a pbSegHdrs of the UDP header.
230 * @param pbPayload Pointer to the payload bytes.
231 * @param cbPayload The amount of payload.
232 * @param cbHdrs The size of all the headers.
233 * @param enmCsumType Whether to checksum the payload, the pseudo
234 * header or nothing.
235 * @internal
236 */
237DECLINLINE(void) pdmNetGsoUpdateUdpHdr(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, uint8_t offUdpHdr,
238 uint8_t const *pbPayload, uint32_t cbPayload, uint8_t cbHdrs,
239 PDMNETCSUMTYPE enmCsumType)
240{
241 PRTNETUDP pUdpHdr = (PRTNETUDP)&pbSegHdrs[offUdpHdr];
242 pUdpHdr->uh_ulen = RT_H2N_U16(cbPayload + cbHdrs - offUdpHdr);
243 switch (enmCsumType)
244 {
245 case PDMNETCSUMTYPE_NONE:
246 pUdpHdr->uh_sum = 0;
247 break;
248 case PDMNETCSUMTYPE_COMPLETE:
249 pUdpHdr->uh_sum = RTNetUDPChecksum(u32PseudoSum, pUdpHdr);
250 break;
251 case PDMNETCSUMTYPE_PSEUDO:
252 pUdpHdr->uh_sum = ~RTNetIPv4FinalizeChecksum(u32PseudoSum);
253 break;
254 default:
255 NOREF(pbPayload);
256 AssertFailed();
257 break;
258 }
259}
260
261
262/**
263 * Update an UDP header after carving out an IP fragment
264 *
265 * @param u32PseudoSum The pseudo checksum.
266 * @param pbSegHdrs Pointer to the header bytes copy
267 * @param pbFrame Pointer to the frame start.
268 * @param offUdpHdr The offset into @a pbSegHdrs of the UDP header.
269 *
270 * @internal
271 */
272DECLINLINE(void) pdmNetGsoUpdateUdpHdrUfo(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, const uint8_t *pbFrame, uint8_t offUdpHdr)
273{
274 PCRTNETUDP pcUdpHdrOrig = (PCRTNETUDP)&pbFrame[offUdpHdr];
275 PRTNETUDP pUdpHdr = (PRTNETUDP)&pbSegHdrs[offUdpHdr];
276 pUdpHdr->uh_sum = RTNetUDPChecksum(u32PseudoSum, pcUdpHdrOrig);
277}
278
279
280/**
281 * Update a TCP header after carving out a segment.
282 *
283 * @param u32PseudoSum The pseudo checksum.
284 * @param pbSegHdrs Pointer to the header bytes / frame start.
285 * @param offTcpHdr The offset into @a pbSegHdrs of the TCP header.
286 * @param pbPayload Pointer to the payload bytes.
287 * @param cbPayload The amount of payload.
288 * @param offPayload The offset into the payload that we're splitting
289 * up. We're ASSUMING that the payload follows
290 * immediately after the TCP header w/ options.
291 * @param cbHdrs The size of all the headers.
292 * @param fLastSeg Set if this is the last segment.
293 * @param enmCsumType Whether to checksum the payload, the pseudo
294 * header or nothing.
295 * @internal
296 */
297DECLINLINE(void) pdmNetGsoUpdateTcpHdr(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, uint8_t offTcpHdr,
298 uint8_t const *pbPayload, uint32_t cbPayload, uint32_t offPayload, uint8_t cbHdrs,
299 bool fLastSeg, PDMNETCSUMTYPE enmCsumType)
300{
301 PRTNETTCP pTcpHdr = (PRTNETTCP)&pbSegHdrs[offTcpHdr];
302 pTcpHdr->th_seq = RT_H2N_U32(RT_N2H_U32(pTcpHdr->th_seq) + offPayload);
303 if (!fLastSeg)
304 pTcpHdr->th_flags &= ~(RTNETTCP_F_FIN | RTNETTCP_F_PSH);
305 switch (enmCsumType)
306 {
307 case PDMNETCSUMTYPE_NONE:
308 pTcpHdr->th_sum = 0;
309 break;
310 case PDMNETCSUMTYPE_COMPLETE:
311 pTcpHdr->th_sum = RTNetTCPChecksum(u32PseudoSum, pTcpHdr, pbPayload, cbPayload);
312 break;
313 case PDMNETCSUMTYPE_PSEUDO:
314 pTcpHdr->th_sum = ~RTNetIPv4FinalizeChecksum(u32PseudoSum);
315 break;
316 default:
317 NOREF(cbHdrs);
318 AssertFailed();
319 break;
320 }
321}
322
323
324/**
325 * Updates a IPv6 header after carving out a segment.
326 *
327 * @returns 32-bit intermediary checksum value for the pseudo header.
328 * @param pbSegHdrs Pointer to the header bytes.
329 * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
330 * @param cbSegPayload The amount of segmented payload. Not to be
331 * confused with the IP payload.
332 * @param cbHdrs The size of all the headers.
333 * @param offPktHdr Offset of the protocol packet header. For the
334 * pseudo header checksum calulation.
335 * @param bProtocol The protocol type. For the pseudo header.
336 * @internal
337 */
338DECLINLINE(uint32_t) pdmNetGsoUpdateIPv6Hdr(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload, uint8_t cbHdrs,
339 uint8_t offPktHdr, uint8_t bProtocol)
340{
341 PRTNETIPV6 pIpHdr = (PRTNETIPV6)&pbSegHdrs[offIpHdr];
342 uint16_t cbPayload = (uint16_t)(cbHdrs - (offIpHdr + sizeof(RTNETIPV6)) + cbSegPayload);
343 pIpHdr->ip6_plen = RT_H2N_U16(cbPayload);
344 return RTNetIPv6PseudoChecksumEx(pIpHdr, bProtocol, (uint16_t)(cbHdrs - offPktHdr + cbSegPayload));
345}
346
347
348/**
349 * Updates a IPv4 header after carving out a segment.
350 *
351 * @returns 32-bit intermediary checksum value for the pseudo header.
352 * @param pbSegHdrs Pointer to the header bytes.
353 * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
354 * @param cbSegPayload The amount of segmented payload.
355 * @param iSeg The segment index.
356 * @param cbHdrs The size of all the headers.
357 * @internal
358 */
359DECLINLINE(uint32_t) pdmNetGsoUpdateIPv4Hdr(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload,
360 uint32_t iSeg, uint8_t cbHdrs)
361{
362 PRTNETIPV4 pIpHdr = (PRTNETIPV4)&pbSegHdrs[offIpHdr];
363 pIpHdr->ip_len = RT_H2N_U16(cbHdrs - offIpHdr + cbSegPayload);
364 pIpHdr->ip_id = RT_H2N_U16(RT_N2H_U16(pIpHdr->ip_id) + iSeg);
365 pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
366 return RTNetIPv4PseudoChecksum(pIpHdr);
367}
368
369
370/**
371 * Updates a IPv4 header after carving out an IP fragment.
372 *
373 * @param pbSegHdrs Pointer to the header bytes.
374 * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
375 * @param cbSegPayload The amount of segmented payload.
376 * @param offFragment The offset of this fragment for reassembly.
377 * @param cbHdrs The size of all the headers.
378 * @param fLastFragment True if this is the last fragment of datagram.
379 * @internal
380 */
381DECLINLINE(void) pdmNetGsoUpdateIPv4HdrUfo(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload,
382 uint32_t offFragment, uint8_t cbHdrs, bool fLastFragment)
383{
384 PRTNETIPV4 pIpHdr = (PRTNETIPV4)&pbSegHdrs[offIpHdr];
385 pIpHdr->ip_len = RT_H2N_U16(cbHdrs - offIpHdr + cbSegPayload);
386 pIpHdr->ip_off = RT_H2N_U16((offFragment / 8) | (fLastFragment ? 0 : RTNETIPV4_FLAGS_MF));
387 pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
388}
389
390
391/**
392 * Carves out the specified segment in a destructive manner.
393 *
394 * This is for sequentially carving out segments and pushing them along for
395 * processing or sending. To avoid allocating a temporary buffer for
396 * constructing the segment in, we trash the previous frame by putting the
397 * header at the end of it.
398 *
399 * @returns Pointer to the segment frame that we've carved out.
400 * @param pGso The GSO context data.
401 * @param pbFrame Pointer to the GSO frame.
402 * @param cbFrame The size of the GSO frame.
403 * @param pbHdrScatch Pointer to a pGso->cbHdrs sized area where we
404 * can save the original header prototypes on the
405 * first call (@a iSeg is 0) and retrieve it on
406 * susequent calls. (Just use a 256 bytes
407 * buffer to make life easy.)
408 * @param iSeg The segment that we're carving out (0-based).
409 * @param cSegs The number of segments in the GSO frame. Use
410 * PDMNetGsoCalcSegmentCount to find this.
411 * @param pcbSegFrame Where to return the size of the returned segment
412 * frame.
413 */
414DECLINLINE(void *) PDMNetGsoCarveSegmentQD(PCPDMNETWORKGSO pGso, uint8_t *pbFrame, size_t cbFrame, uint8_t *pbHdrScatch,
415 uint32_t iSeg, uint32_t cSegs, uint32_t *pcbSegFrame)
416{
417 /*
418 * Figure out where the payload is and where the header starts before we
419 * do the protocol specific carving.
420 *
421 * UDP GSO uses IPv4 fragmentation, meaning that UDP header is present in
422 * the first fragment only. When computing the total frame size of the
423 * first fragment we need to use PDMNETWORKGSO::cbHdrsTotal instead of
424 * PDMNETWORKGSO::cbHdrsSeg. In case of TCP GSO both cbHdrsTotal and
425 * cbHdrsSeg have the same value, so it will work as well.
426 */
427 uint8_t * const pbSegHdrs = pbFrame + pGso->cbMaxSeg * iSeg;
428 uint8_t * const pbSegPayload = pbSegHdrs + pGso->cbHdrsSeg;
429 uint32_t const cbSegPayload = pdmNetSegPayloadLen(pGso, iSeg, cSegs, (uint32_t)cbFrame);
430 uint32_t const cbSegFrame = cbSegPayload + (iSeg ? pGso->cbHdrsSeg : pGso->cbHdrsTotal);
431
432 /*
433 * Check assumptions (doing it after declaring the variables because of C).
434 */
435 Assert(iSeg < cSegs);
436 Assert(cSegs == PDMNetGsoCalcSegmentCount(pGso, cbFrame));
437 Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
438
439 /*
440 * Copy the header and do the protocol specific massaging of it.
441 */
442 if (iSeg != 0)
443 memcpy(pbSegHdrs, pbHdrScatch, pGso->cbHdrsSeg);
444 else
445 memcpy(pbHdrScatch, pbSegHdrs, pGso->cbHdrsSeg); /* There is no need to save UDP header */
446
447 switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
448 {
449 case PDMNETWORKGSOTYPE_IPV4_TCP:
450 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg),
451 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
452 pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
453 break;
454 case PDMNETWORKGSOTYPE_IPV4_UDP:
455 if (iSeg == 0)
456 {
457 /* uh_ulen shall not exceed cbFrame - pGso->offHdr2 (offset of UDP header) */
458 PRTNETUDP pUdpHdr = (PRTNETUDP)&pbFrame[pGso->offHdr2];
459 Assert(pGso->offHdr2 + RT_UOFFSET_AFTER(RTNETUDP, uh_ulen) <= cbFrame);
460 if ((unsigned)(pGso->offHdr2 + RT_BE2H_U16(pUdpHdr->uh_ulen)) > cbFrame)
461 {
462 size_t cbUdp = cbFrame - pGso->offHdr2;
463 if (cbUdp >= UINT16_MAX)
464 pUdpHdr->uh_ulen = UINT16_MAX;
465 else
466 pUdpHdr->uh_ulen = RT_H2BE_U16((uint16_t)cbUdp);
467 }
468 /* uh_ulen shall be at least the size of UDP header */
469 if (RT_BE2H_U16(pUdpHdr->uh_ulen) < sizeof(RTNETUDP))
470 pUdpHdr->uh_ulen = RT_H2BE_U16(sizeof(RTNETUDP));
471 pdmNetGsoUpdateUdpHdrUfo(RTNetIPv4PseudoChecksum((PRTNETIPV4)&pbFrame[pGso->offHdr1]),
472 pbSegHdrs, pbFrame, pGso->offHdr2);
473 }
474 pdmNetGsoUpdateIPv4HdrUfo(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg * pGso->cbMaxSeg,
475 pdmNetSegHdrLen(pGso, iSeg), iSeg + 1 == cSegs);
476 break;
477 case PDMNETWORKGSOTYPE_IPV6_TCP:
478 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, pGso->cbHdrsSeg,
479 pGso->offHdr2, RTNETIPV4_PROT_TCP),
480 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
481 pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
482 break;
483 case PDMNETWORKGSOTYPE_IPV6_UDP:
484 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, pGso->cbHdrsSeg,
485 pGso->offHdr2, RTNETIPV4_PROT_UDP),
486 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, pGso->cbHdrsSeg, PDMNETCSUMTYPE_COMPLETE);
487 break;
488 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
489 pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg);
490 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
491 cbSegPayload, pGso->cbHdrsSeg, pGso->offHdr2, RTNETIPV4_PROT_TCP),
492 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
493 pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
494 break;
495 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
496 pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg);
497 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
498 cbSegPayload, pGso->cbHdrsSeg, pGso->offHdr2, RTNETIPV4_PROT_UDP),
499 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, pGso->cbHdrsSeg, PDMNETCSUMTYPE_COMPLETE);
500 break;
501 case PDMNETWORKGSOTYPE_INVALID:
502 case PDMNETWORKGSOTYPE_END:
503 /* no default! wnat gcc warnings. */
504 break;
505 }
506
507 *pcbSegFrame = cbSegFrame;
508 return pbSegHdrs;
509}
510
511
512/**
513 * Carves out the specified segment in a non-destructive manner.
514 *
515 * The segment headers and segment payload is kept separate here. The GSO frame
516 * is still expected to be one linear chunk of data, but we don't modify any of
517 * it.
518 *
519 * @returns The offset into the GSO frame of the payload.
520 * @param pGso The GSO context data.
521 * @param pbFrame Pointer to the GSO frame. Used for retrieving
522 * the header prototype and for checksumming the
523 * payload. The buffer is not modified.
524 * @param cbFrame The size of the GSO frame.
525 * @param iSeg The segment that we're carving out (0-based).
526 * @param cSegs The number of segments in the GSO frame. Use
527 * PDMNetGsoCalcSegmentCount to find this.
528 * @param pbSegHdrs Where to return the headers for the segment
529 * that's been carved out. The buffer must be at
530 * least pGso->cbHdrs in size, using a 256 byte
531 * buffer is a recommended simplification.
532 * @param pcbSegHdrs Where to return the size of the returned
533 * segment headers.
534 * @param pcbSegPayload Where to return the size of the returned
535 * segment payload.
536 */
537DECLINLINE(uint32_t) PDMNetGsoCarveSegment(PCPDMNETWORKGSO pGso, const uint8_t *pbFrame, size_t cbFrame,
538 uint32_t iSeg, uint32_t cSegs, uint8_t *pbSegHdrs,
539 uint32_t *pcbSegHdrs, uint32_t *pcbSegPayload)
540{
541 /*
542 * Figure out where the payload is and where the header starts before we
543 * do the protocol specific carving.
544 */
545 uint32_t const cbSegHdrs = pdmNetSegHdrLen(pGso, iSeg);
546 uint8_t const * const pbSegPayload = pbFrame + cbSegHdrs + iSeg * pGso->cbMaxSeg;
547 uint32_t const cbSegPayload = pdmNetSegPayloadLen(pGso, iSeg, cSegs, (uint32_t)cbFrame);
548
549 /*
550 * Check assumptions (doing it after declaring the variables because of C).
551 */
552 Assert(iSeg < cSegs);
553 Assert(cSegs == PDMNetGsoCalcSegmentCount(pGso, cbFrame));
554 Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
555
556 /*
557 * Copy the header and do the protocol specific massaging of it.
558 */
559 memcpy(pbSegHdrs, pbFrame, pGso->cbHdrsTotal); /* include UDP header */
560
561 switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
562 {
563 case PDMNETWORKGSOTYPE_IPV4_TCP:
564 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs),
565 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
566 cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
567 break;
568 case PDMNETWORKGSOTYPE_IPV4_UDP:
569 if (iSeg == 0)
570 {
571 /* uh_ulen shall not exceed cbFrame - pGso->offHdr2 (offset of UDP header) */
572 PRTNETUDP pUdpHdr = (PRTNETUDP)&pbFrame[pGso->offHdr2];
573 Assert(pGso->offHdr2 + RT_UOFFSET_AFTER(RTNETUDP, uh_ulen) <= cbFrame);
574 if ((unsigned)(pGso->offHdr2 + RT_BE2H_U16(pUdpHdr->uh_ulen)) > cbFrame)
575 {
576 size_t cbUdp = cbFrame - pGso->offHdr2;
577 if (cbUdp >= UINT16_MAX)
578 pUdpHdr->uh_ulen = UINT16_MAX;
579 else
580 pUdpHdr->uh_ulen = RT_H2BE_U16((uint16_t)cbUdp);
581 }
582 /* uh_ulen shall be at least the size of UDP header */
583 if (RT_BE2H_U16(pUdpHdr->uh_ulen) < sizeof(RTNETUDP))
584 pUdpHdr->uh_ulen = RT_H2BE_U16(sizeof(RTNETUDP));
585 pdmNetGsoUpdateUdpHdrUfo(RTNetIPv4PseudoChecksum((PRTNETIPV4)&pbFrame[pGso->offHdr1]),
586 pbSegHdrs, pbFrame, pGso->offHdr2);
587 }
588 pdmNetGsoUpdateIPv4HdrUfo(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg * pGso->cbMaxSeg,
589 cbSegHdrs, iSeg + 1 == cSegs);
590 break;
591 case PDMNETWORKGSOTYPE_IPV6_TCP:
592 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, cbSegHdrs,
593 pGso->offHdr2, RTNETIPV4_PROT_TCP),
594 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
595 cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
596 break;
597 case PDMNETWORKGSOTYPE_IPV6_UDP:
598 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, cbSegHdrs,
599 pGso->offHdr2, RTNETIPV4_PROT_UDP),
600 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, cbSegHdrs, PDMNETCSUMTYPE_COMPLETE);
601 break;
602 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
603 pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs);
604 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
605 cbSegPayload, cbSegHdrs, pGso->offHdr2, RTNETIPV4_PROT_TCP),
606 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
607 cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
608 break;
609 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
610 pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs);
611 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
612 cbSegPayload, cbSegHdrs, pGso->offHdr2, RTNETIPV4_PROT_UDP),
613 pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, cbSegHdrs, PDMNETCSUMTYPE_COMPLETE);
614 break;
615 case PDMNETWORKGSOTYPE_INVALID:
616 case PDMNETWORKGSOTYPE_END:
617 /* no default! wnat gcc warnings. */
618 break;
619 }
620
621 *pcbSegHdrs = cbSegHdrs;
622 *pcbSegPayload = cbSegPayload;
623 return cbSegHdrs + iSeg * pGso->cbMaxSeg;
624}
625
626
627/**
628 * Prepares the GSO frame for direct use without any segmenting.
629 *
630 * @param pGso The GSO context.
631 * @param pvFrame The frame to prepare.
632 * @param cbFrame The frame size.
633 * @param enmCsumType Whether to checksum the payload, the pseudo
634 * header or nothing.
635 */
636DECLINLINE(void) PDMNetGsoPrepForDirectUse(PCPDMNETWORKGSO pGso, void *pvFrame, size_t cbFrame, PDMNETCSUMTYPE enmCsumType)
637{
638 /*
639 * Figure out where the payload is and where the header starts before we
640 * do the protocol bits.
641 */
642 uint8_t * const pbHdrs = (uint8_t *)pvFrame;
643 uint8_t * const pbPayload = pbHdrs + pGso->cbHdrsTotal;
644 uint32_t const cbFrame32 = (uint32_t)cbFrame;
645 uint32_t const cbPayload = cbFrame32 - pGso->cbHdrsTotal;
646
647 /*
648 * Check assumptions (doing it after declaring the variables because of C).
649 */
650 Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
651
652 /*
653 * Get down to busienss.
654 */
655 switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
656 {
657 case PDMNETWORKGSOTYPE_IPV4_TCP:
658 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbFrame32 - pGso->cbHdrsTotal, 0, pGso->cbHdrsTotal),
659 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
660 break;
661 case PDMNETWORKGSOTYPE_IPV4_UDP:
662 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbFrame32 - pGso->cbHdrsTotal, 0, pGso->cbHdrsTotal),
663 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
664 break;
665 case PDMNETWORKGSOTYPE_IPV6_TCP:
666 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pGso->offHdr1, cbPayload, pGso->cbHdrsTotal,
667 pGso->offHdr2, RTNETIPV4_PROT_TCP),
668 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
669 break;
670 case PDMNETWORKGSOTYPE_IPV6_UDP:
671 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pGso->offHdr1, cbPayload, pGso->cbHdrsTotal,
672 pGso->offHdr2, RTNETIPV4_PROT_UDP),
673 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
674 break;
675 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
676 pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbPayload, 0, pGso->cbHdrsTotal);
677 pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pgmNetGsoCalcIpv6Offset(pbHdrs, pGso->offHdr1),
678 cbPayload, pGso->cbHdrsTotal, pGso->offHdr2, RTNETIPV4_PROT_TCP),
679 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
680 break;
681 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
682 pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbPayload, 0, pGso->cbHdrsTotal);
683 pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pgmNetGsoCalcIpv6Offset(pbHdrs, pGso->offHdr1),
684 cbPayload, pGso->cbHdrsTotal, pGso->offHdr2, RTNETIPV4_PROT_UDP),
685 pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
686 break;
687 case PDMNETWORKGSOTYPE_INVALID:
688 case PDMNETWORKGSOTYPE_END:
689 /* no default! wnat gcc warnings. */
690 break;
691 }
692}
693
694
695/**
696 * Gets the GSO type name string.
697 *
698 * @returns Pointer to read only name string.
699 * @param enmType The type.
700 */
701DECLINLINE(const char *) PDMNetGsoTypeName(PDMNETWORKGSOTYPE enmType)
702{
703 switch (enmType)
704 {
705 case PDMNETWORKGSOTYPE_IPV4_TCP: return "TCPv4";
706 case PDMNETWORKGSOTYPE_IPV6_TCP: return "TCPv6";
707 case PDMNETWORKGSOTYPE_IPV4_UDP: return "UDPv4";
708 case PDMNETWORKGSOTYPE_IPV6_UDP: return "UDPv6";
709 case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP: return "4to6TCP";
710 case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP: return "4to6UDP";
711 case PDMNETWORKGSOTYPE_INVALID: return "invalid";
712 case PDMNETWORKGSOTYPE_END: return "end";
713 }
714 return "bad-gso-type";
715}
716
717/** @} */
718
719#endif /* !VBOX_INCLUDED_vmm_pdmnetinline_h */
720
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette