VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetAdp/freebsd/VBoxNetAdp-freebsd.c@ 69371

最後變更 在這個檔案從69371是 57358,由 vboxsync 提交於 9 年 前

*: scm cleanup run.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.4 KB
 
1/* $Id: VBoxNetAdp-freebsd.c 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), FreeBSD Specific Code.
4 */
5
6/*-
7 * Copyright (c) 2009 Fredrik Lindberg <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#include <sys/param.h>
36#undef PVM
37#include <sys/types.h>
38#include <sys/module.h>
39#include <sys/systm.h>
40#include <sys/errno.h>
41#include <sys/kernel.h>
42#include <sys/fcntl.h>
43#include <sys/conf.h>
44#include <sys/uio.h>
45#include <sys/socket.h>
46#include <sys/sockio.h>
47
48#include <net/if.h>
49#include <net/if_var.h>
50#include <net/route.h>
51#include <net/if_dl.h>
52#include <net/if_media.h>
53#include <net/if_types.h>
54#include <net/ethernet.h>
55#include <net/bpf.h>
56
57#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
58#include <VBox/version.h>
59#include <VBox/err.h>
60#include <VBox/log.h>
61#include <iprt/initterm.h>
62#include <iprt/string.h>
63#include <iprt/spinlock.h>
64#include <iprt/process.h>
65#include <iprt/assert.h>
66#include <iprt/uuid.h>
67#include <iprt/alloc.h>
68#include <iprt/err.h>
69
70#define VBOXNETADP_OS_SPECFIC 1
71#include "../VBoxNetAdpInternal.h"
72
73#if defined(__FreeBSD_version) && __FreeBSD_version >= 800500
74# include <sys/jail.h>
75# include <net/vnet.h>
76
77# define VBOXCURVNET_SET(arg) CURVNET_SET_QUIET(arg)
78# define VBOXCURVNET_SET_FROM_UCRED() VBOXCURVNET_SET(CRED_TO_VNET(curthread->td_ucred))
79# define VBOXCURVNET_RESTORE() CURVNET_RESTORE()
80
81#else /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
82
83# define VBOXCURVNET_SET(arg)
84# define VBOXCURVNET_SET_FROM_UCRED()
85# define VBOXCURVNET_RESTORE()
86
87#endif /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
88
89static int VBoxNetAdpFreeBSDCtrlioctl(struct cdev *, u_long, caddr_t, int flags,
90 struct thread *);
91static struct cdevsw vboxnetadp_cdevsw =
92{
93 .d_version = D_VERSION,
94 .d_ioctl = VBoxNetAdpFreeBSDCtrlioctl,
95 .d_read = (d_read_t *)nullop,
96 .d_write = (d_write_t *)nullop,
97 .d_name = VBOXNETADP_CTL_DEV_NAME,
98};
99
100static struct cdev *VBoxNetAdpFreeBSDcdev;
101
102static int VBoxNetAdpFreeBSDModuleEvent(struct module *, int, void *);
103static moduledata_t g_VBoxNetAdpFreeBSDModule = {
104 "vboxnetadp",
105 VBoxNetAdpFreeBSDModuleEvent,
106 NULL
107};
108
109/** Declare the module as a pseudo device. */
110DECLARE_MODULE(vboxnetadp, g_VBoxNetAdpFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
111MODULE_VERSION(vboxnetadp, 1);
112MODULE_DEPEND(vboxnetadp, vboxdrv, 1, 1, 1);
113MODULE_DEPEND(vboxnetadp, vboxnetflt, 1, 1, 1);
114
115/**
116 * Module event handler
117 */
118static int
119VBoxNetAdpFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg)
120{
121 int rc = 0;
122
123 Log(("VBoxNetAdpFreeBSDModuleEvent\n"));
124
125 switch (enmEventType)
126 {
127 case MOD_LOAD:
128 rc = RTR0Init(0);
129 if (RT_FAILURE(rc))
130 {
131 Log(("RTR0Init failed %d\n", rc));
132 return RTErrConvertToErrno(rc);
133 }
134 rc = vboxNetAdpInit();
135 if (RT_FAILURE(rc))
136 {
137 RTR0Term();
138 Log(("vboxNetAdpInit failed %d\n", rc));
139 return RTErrConvertToErrno(rc);
140 }
141 /* Create dev node */
142 VBoxNetAdpFreeBSDcdev = make_dev(&vboxnetadp_cdevsw, 0,
143 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
144
145 break;
146
147 case MOD_UNLOAD:
148 vboxNetAdpShutdown();
149 destroy_dev(VBoxNetAdpFreeBSDcdev);
150 RTR0Term();
151 break;
152 case MOD_SHUTDOWN:
153 case MOD_QUIESCE:
154 default:
155 return EOPNOTSUPP;
156 }
157
158 if (RT_SUCCESS(rc))
159 return 0;
160 return RTErrConvertToErrno(rc);
161}
162
163/**
164 * Device I/O Control entry point.
165 */
166static int
167VBoxNetAdpFreeBSDCtrlioctl(struct cdev *dev, u_long iCmd, caddr_t data, int flags, struct thread *td)
168{
169 PVBOXNETADP pAdp;
170 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)data;
171 struct ifnet *ifp;
172 int rc;
173
174 switch (iCmd)
175 {
176 case VBOXNETADP_CTL_ADD:
177 if ( !(iCmd & IOC_INOUT) /* paranoia*/
178 || IOCPARM_LEN(iCmd) < sizeof(*pReq))
179 return EINVAL;
180
181 rc = vboxNetAdpCreate(&pAdp,
182 pReq->szName[0] && RTStrEnd(pReq->szName, RT_MIN(IOCPARM_LEN(iCmd), sizeof(pReq->szName))) ?
183 pReq->szName : NULL);
184 if (RT_FAILURE(rc))
185 return EINVAL;
186
187 strncpy(pReq->szName, pAdp->szName, sizeof(pReq->szName) - 1);
188 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
189 break;
190
191 case VBOXNETADP_CTL_REMOVE:
192 if (!RTStrEnd(pReq->szName, RT_MIN(sizeof(pReq->szName), IOCPARM_LEN(iCmd))))
193 return EINVAL;
194
195 pAdp = vboxNetAdpFindByName(pReq->szName);
196 if (!pAdp)
197 return EINVAL;
198
199 rc = vboxNetAdpDestroy(pAdp);
200 if (RT_FAILURE(rc))
201 return EINVAL;
202
203 break;
204
205 default:
206 return EINVAL;
207 }
208 return 0;
209}
210
211/**
212 * Initialize device, just set the running flag.
213 */
214static void VBoxNetAdpFreeBSDNetinit(void *priv)
215{
216 PVBOXNETADP pThis = priv;
217 struct ifnet *ifp = pThis->u.s.ifp;
218
219 ifp->if_drv_flags |= IFF_DRV_RUNNING;
220}
221
222/**
223 * Transmit packets.
224 * netflt has already done everything for us so we just hand the
225 * packets to BPF and increment the packet stats.
226 */
227static void VBoxNetAdpFreeBSDNetstart(struct ifnet *ifp)
228{
229 PVBOXNETADP pThis = ifp->if_softc;
230 struct mbuf *m;
231
232 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING)
233 return;
234
235 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
236 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
237 {
238 ifp->if_opackets++;
239 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
240 BPF_MTAP(ifp, m);
241 m_freem(m);
242 }
243 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
244}
245
246/**
247 * Interface ioctl handling
248 */
249static int VBoxNetAdpFreeBSDNetioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
250{
251 int error = 0;
252
253 switch (cmd)
254 {
255 case SIOCSIFFLAGS:
256 if (ifp->if_flags & IFF_UP)
257 {
258 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
259 ifp->if_init(ifp->if_softc);
260 }
261 else
262 {
263 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
264 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
265 }
266 break;
267 case SIOCGIFMEDIA:
268 {
269 struct ifmediareq *ifmr;
270 int count;
271
272 ifmr = (struct ifmediareq *)data;
273 count = ifmr->ifm_count;
274 ifmr->ifm_count = 1;
275 ifmr->ifm_status = IFM_AVALID;
276 ifmr->ifm_active = IFM_ETHER;
277 ifmr->ifm_status |= IFM_ACTIVE;
278 ifmr->ifm_current = ifmr->ifm_active;
279 if (count >= 1)
280 {
281 int media = IFM_ETHER;
282 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
283 }
284 break;
285 }
286 default:
287 return ether_ioctl(ifp, cmd, data);
288 }
289 return error;
290}
291
292int vboxNetAdpOsInit(PVBOXNETADP pThis)
293{
294 pThis->u.s.ifp = NULL;
295 return VINF_SUCCESS;;
296}
297
298int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMac)
299{
300 struct ifnet *ifp;
301
302 VBOXCURVNET_SET_FROM_UCRED();
303 ifp = if_alloc(IFT_ETHER);
304 if (ifp == NULL)
305 return VERR_NO_MEMORY;
306
307 if_initname(ifp, VBOXNETADP_NAME, pThis->iUnit);
308 ifp->if_softc = pThis;
309 ifp->if_mtu = ETHERMTU;
310 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
311 ifp->if_ioctl = VBoxNetAdpFreeBSDNetioctl;
312 ifp->if_start = VBoxNetAdpFreeBSDNetstart;
313 ifp->if_init = VBoxNetAdpFreeBSDNetinit;
314 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
315 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
316 IFQ_SET_READY(&ifp->if_snd);
317 ether_ifattach(ifp, (void *)pMac);
318 ifp->if_baudrate = 0;
319
320 strncpy(pThis->szName, ifp->if_xname, VBOXNETADP_MAX_NAME_LEN);
321 pThis->u.s.ifp = ifp;
322 VBOXCURVNET_RESTORE();
323 return 0;
324}
325
326void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
327{
328 struct ifnet *ifp;
329
330 ifp = pThis->u.s.ifp;
331 VBOXCURVNET_SET(ifp->if_vnet);
332 ether_ifdetach(ifp);
333 if_free(ifp);
334 VBOXCURVNET_RESTORE();
335}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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