1 | /* $Id: VBoxNetIntIf.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetIntIf - IntNet Interface Client Routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_DEFAULT
|
---|
22 | #include "VBoxNetLib.h"
|
---|
23 | #include <VBox/intnet.h>
|
---|
24 | #include <VBox/intnetinline.h>
|
---|
25 | #include <VBox/sup.h>
|
---|
26 | #include <VBox/vmm.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/log.h>
|
---|
29 |
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Flushes the send buffer.
|
---|
36 | *
|
---|
37 | * @returns VBox status code.
|
---|
38 | * @param pSession The support driver session.
|
---|
39 | * @param hIf The interface handle to flush.
|
---|
40 | */
|
---|
41 | int VBoxNetIntIfFlush(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf)
|
---|
42 | {
|
---|
43 | INTNETIFSENDREQ SendReq;
|
---|
44 | SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
|
---|
45 | SendReq.Hdr.cbReq = sizeof(SendReq);
|
---|
46 | SendReq.pSession = pSession;
|
---|
47 | SendReq.hIf = hIf;
|
---|
48 | return SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Copys the SG segments into the specified fram.
|
---|
54 | *
|
---|
55 | * @param pvFrame The frame buffer.
|
---|
56 | * @param cSegs The number of segments.
|
---|
57 | * @param paSegs The segments.
|
---|
58 | */
|
---|
59 | static void vboxnetIntIfCopySG(void *pvFrame, size_t cSegs, PCINTNETSEG paSegs)
|
---|
60 | {
|
---|
61 | uint8_t *pbDst = (uint8_t *)pvFrame;
|
---|
62 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
63 | {
|
---|
64 | memcpy(pbDst, paSegs[iSeg].pv, paSegs[iSeg].cb);
|
---|
65 | pbDst += paSegs[iSeg].cb;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Writes a frame packet to the buffer.
|
---|
72 | *
|
---|
73 | * @returns VBox status code.
|
---|
74 | * @param pBuf The buffer.
|
---|
75 | * @param pRingBuf The ring buffer to read from.
|
---|
76 | * @param cSegs The number of segments.
|
---|
77 | * @param paSegs The segments.
|
---|
78 | */
|
---|
79 | int VBoxNetIntIfRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, size_t cSegs, PCINTNETSEG paSegs)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Validate input.
|
---|
83 | */
|
---|
84 | AssertPtr(pBuf);
|
---|
85 | AssertPtr(pRingBuf);
|
---|
86 | AssertPtr(paSegs);
|
---|
87 | Assert(cSegs > 0);
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Calc frame size.
|
---|
91 | */
|
---|
92 | uint32_t cbFrame = 0;
|
---|
93 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
94 | cbFrame += paSegs[iSeg].cb;
|
---|
95 | Assert(cbFrame >= sizeof(RTMAC) * 2);
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Allocate a frame, copy the data and commit it.
|
---|
99 | */
|
---|
100 | PINTNETHDR pHdr;
|
---|
101 | void *pvFrame;
|
---|
102 | int rc = IntNetRingAllocateFrame(pRingBuf, cbFrame, &pHdr, &pvFrame);
|
---|
103 | if (RT_SUCCESS(rc))
|
---|
104 | {
|
---|
105 | vboxnetIntIfCopySG(pvFrame, cSegs, paSegs);
|
---|
106 | IntNetRingCommitFrame(pRingBuf, pHdr);
|
---|
107 | return VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Sends a frame
|
---|
116 | *
|
---|
117 | * @returns VBox status code.
|
---|
118 | * @param pSession The support driver session.
|
---|
119 | * @param hIf The interface handle.
|
---|
120 | * @param pBuf The interface buffer.
|
---|
121 | * @param cSegs The number of segments.
|
---|
122 | * @param paSegs The segments.
|
---|
123 | * @param fFlush Whether to flush the write.
|
---|
124 | */
|
---|
125 | int VBoxNetIntIfSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
|
---|
126 | size_t cSegs, PCINTNETSEG paSegs, bool fFlush)
|
---|
127 | {
|
---|
128 | int rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
129 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
130 | {
|
---|
131 | VBoxNetIntIfFlush(pSession, hIf);
|
---|
132 | rc = VBoxNetIntIfRingWriteFrame(pBuf, &pBuf->Send, cSegs, paSegs);
|
---|
133 | }
|
---|
134 | if (RT_SUCCESS(rc) && fFlush)
|
---|
135 | rc = VBoxNetIntIfFlush(pSession, hIf);
|
---|
136 | return rc;
|
---|
137 | }
|
---|