1 | /* $Id: VBoxNetIntIf.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetIntIf - IntNet Interface Client Routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DEFAULT
|
---|
23 | #include "VBoxNetLib.h"
|
---|
24 | #include <VBox/intnet.h>
|
---|
25 | #include <VBox/intnetinline.h>
|
---|
26 | #include <VBox/sup.h>
|
---|
27 | #include <VBox/vmm/vmm.h>
|
---|
28 | #include <iprt/errcore.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 | #include <iprt/string.h>
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Flushes the send buffer.
|
---|
37 | *
|
---|
38 | * @returns VBox status code.
|
---|
39 | * @param pSession The support driver session.
|
---|
40 | * @param hIf The interface handle to flush.
|
---|
41 | */
|
---|
42 | int VBoxNetIntIfFlush(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf)
|
---|
43 | {
|
---|
44 | INTNETIFSENDREQ SendReq;
|
---|
45 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
46 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
47 | SendReq.pSession = pSession;
|
---|
48 | SendReq.hIf = hIf;
|
---|
49 | return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Copys the SG segments into the specified fram.
|
---|
55 | *
|
---|
56 | * @param pvFrame The frame buffer.
|
---|
57 | * @param cSegs The number of segments.
|
---|
58 | * @param paSegs The segments.
|
---|
59 | */
|
---|
60 | static void vboxnetIntIfCopySG(void *pvFrame, size_t cSegs, PCINTNETSEG paSegs)
|
---|
61 | {
|
---|
62 | uint8_t *pbDst = (uint8_t *)pvFrame;
|
---|
63 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
64 | {
|
---|
65 | memcpy(pbDst, paSegs[iSeg].pv, paSegs[iSeg].cb);
|
---|
66 | pbDst += paSegs[iSeg].cb;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Writes a frame packet to the buffer.
|
---|
73 | *
|
---|
74 | * @returns VBox status code.
|
---|
75 | * @param pBuf The buffer.
|
---|
76 | * @param pRingBuf The ring buffer to read from.
|
---|
77 | * @param cSegs The number of segments.
|
---|
78 | * @param paSegs The segments.
|
---|
79 | */
|
---|
80 | int VBoxNetIntIfRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, size_t cSegs, PCINTNETSEG paSegs)
|
---|
81 | {
|
---|
82 | RT_NOREF(pBuf);
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Validate input.
|
---|
86 | */
|
---|
87 | AssertPtr(pBuf);
|
---|
88 | AssertPtr(pRingBuf);
|
---|
89 | AssertPtr(paSegs);
|
---|
90 | Assert(cSegs > 0);
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * Calc frame size.
|
---|
94 | */
|
---|
95 | uint32_t cbFrame = 0;
|
---|
96 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
97 | cbFrame += paSegs[iSeg].cb;
|
---|
98 | Assert(cbFrame >= sizeof(RTMAC) * 2);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Allocate a frame, copy the data and commit it.
|
---|
102 | */
|
---|
103 | PINTNETHDR pHdr = NULL;
|
---|
104 | void *pvFrame = NULL;
|
---|
105 | int rc = IntNetRingAllocateFrame(pRingBuf, cbFrame, &pHdr, &pvFrame);
|
---|
106 | if (RT_SUCCESS(rc))
|
---|
107 | {
|
---|
108 | vboxnetIntIfCopySG(pvFrame, cSegs, paSegs);
|
---|
109 | IntNetRingCommitFrame(pRingBuf, pHdr);
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return rc;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Sends a frame
|
---|
119 | *
|
---|
120 | * @returns VBox status code.
|
---|
121 | * @param pSession The support driver session.
|
---|
122 | * @param hIf The interface handle.
|
---|
123 | * @param pBuf The interface buffer.
|
---|
124 | * @param cSegs The number of segments.
|
---|
125 | * @param paSegs The segments.
|
---|
126 | * @param fFlush Whether to flush the write.
|
---|
127 | */
|
---|
128 | int VBoxNetIntIfSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
|
---|
129 | size_t cSegs, PCINTNETSEG paSegs, bool fFlush)
|
---|
130 | {
|
---|
131 | int rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
132 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
133 | {
|
---|
134 | VBoxNetIntIfFlush(pSession, hIf);
|
---|
135 | rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
136 | }
|
---|
137 | if (RT_SUCCESS(rc) && fFlush)
|
---|
138 | rc = VBoxNetIntIfFlush(pSession, hIf);
|
---|
139 | return rc;
|
---|
140 | }
|
---|