1 | /* $Id: VBoxNetIntIf.cpp 62481 2016-07-22 18:30:21Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetIntIf - IntNet Interface Client Routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2016 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 <VBox/err.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 | /*
|
---|
83 | * Validate input.
|
---|
84 | */
|
---|
85 | AssertPtr(pBuf);
|
---|
86 | AssertPtr(pRingBuf);
|
---|
87 | AssertPtr(paSegs);
|
---|
88 | Assert(cSegs > 0);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Calc frame size.
|
---|
92 | */
|
---|
93 | uint32_t cbFrame = 0;
|
---|
94 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
95 | cbFrame += paSegs[iSeg].cb;
|
---|
96 | Assert(cbFrame >= sizeof(RTMAC) * 2);
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Allocate a frame, copy the data and commit it.
|
---|
100 | */
|
---|
101 | PINTNETHDR pHdr = NULL;
|
---|
102 | void *pvFrame = NULL;
|
---|
103 | int rc = IntNetRingAllocateFrame(pRingBuf, cbFrame, &pHdr, &pvFrame);
|
---|
104 | if (RT_SUCCESS(rc))
|
---|
105 | {
|
---|
106 | vboxnetIntIfCopySG(pvFrame, cSegs, paSegs);
|
---|
107 | IntNetRingCommitFrame(pRingBuf, pHdr);
|
---|
108 | return VINF_SUCCESS;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return rc;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Sends a frame
|
---|
117 | *
|
---|
118 | * @returns VBox status code.
|
---|
119 | * @param pSession The support driver session.
|
---|
120 | * @param hIf The interface handle.
|
---|
121 | * @param pBuf The interface buffer.
|
---|
122 | * @param cSegs The number of segments.
|
---|
123 | * @param paSegs The segments.
|
---|
124 | * @param fFlush Whether to flush the write.
|
---|
125 | */
|
---|
126 | int VBoxNetIntIfSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
|
---|
127 | size_t cSegs, PCINTNETSEG paSegs, bool fFlush)
|
---|
128 | {
|
---|
129 | int rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
130 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
131 | {
|
---|
132 | VBoxNetIntIfFlush(pSession, hIf);
|
---|
133 | rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
134 | }
|
---|
135 | if (RT_SUCCESS(rc) && fFlush)
|
---|
136 | rc = VBoxNetIntIfFlush(pSession, hIf);
|
---|
137 | return rc;
|
---|
138 | }
|
---|