VirtualBox

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

最後變更 在這個檔案從48252是 43439,由 vboxsync 提交於 12 年 前

VBoxNetAdp/FreeBSD: Implement media state (thanks to Ed Maste)

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.3 KB
 
1/* $Id: VBoxNetAdp-freebsd.c 43439 2012-09-26 11:19:04Z 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* Header Files *
33*******************************************************************************/
34#include <sys/param.h>
35#undef PVM
36#include <sys/types.h>
37#include <sys/module.h>
38#include <sys/systm.h>
39#include <sys/errno.h>
40#include <sys/kernel.h>
41#include <sys/fcntl.h>
42#include <sys/conf.h>
43#include <sys/uio.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46
47#include <net/if.h>
48#include <net/if_var.h>
49#include <net/route.h>
50#include <net/if_dl.h>
51#include <net/if_media.h>
52#include <net/if_types.h>
53#include <net/ethernet.h>
54#include <net/bpf.h>
55
56#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
57#include <VBox/version.h>
58#include <VBox/err.h>
59#include <VBox/log.h>
60#include <iprt/initterm.h>
61#include <iprt/string.h>
62#include <iprt/spinlock.h>
63#include <iprt/process.h>
64#include <iprt/assert.h>
65#include <iprt/uuid.h>
66#include <iprt/alloc.h>
67#include <iprt/err.h>
68
69#define VBOXNETADP_OS_SPECFIC 1
70#include "../VBoxNetAdpInternal.h"
71
72#if defined(__FreeBSD_version) && __FreeBSD_version >= 800500
73# include <sys/jail.h>
74# include <net/vnet.h>
75
76# define VBOXCURVNET_SET(arg) CURVNET_SET_QUIET(arg)
77# define VBOXCURVNET_SET_FROM_UCRED() VBOXCURVNET_SET(CRED_TO_VNET(curthread->td_ucred))
78# define VBOXCURVNET_RESTORE() CURVNET_RESTORE()
79
80#else /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
81
82# define VBOXCURVNET_SET(arg)
83# define VBOXCURVNET_SET_FROM_UCRED()
84# define VBOXCURVNET_RESTORE()
85
86#endif /* !defined(__FreeBSD_version) || __FreeBSD_version < 800500 */
87
88static int VBoxNetAdpFreeBSDCtrlioctl(struct cdev *, u_long, caddr_t, int flags,
89 struct thread *);
90static struct cdevsw vboxnetadp_cdevsw =
91{
92 .d_version = D_VERSION,
93 .d_ioctl = VBoxNetAdpFreeBSDCtrlioctl,
94 .d_read = (d_read_t *)nullop,
95 .d_write = (d_write_t *)nullop,
96 .d_name = VBOXNETADP_CTL_DEV_NAME,
97};
98
99static struct cdev *VBoxNetAdpFreeBSDcdev;
100
101static int VBoxNetAdpFreeBSDModuleEvent(struct module *, int, void *);
102static moduledata_t g_VBoxNetAdpFreeBSDModule = {
103 "vboxnetadp",
104 VBoxNetAdpFreeBSDModuleEvent,
105 NULL
106};
107
108/** Declare the module as a pseudo device. */
109DECLARE_MODULE(vboxnetadp, g_VBoxNetAdpFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
110MODULE_VERSION(vboxnetadp, 1);
111MODULE_DEPEND(vboxnetadp, vboxdrv, 1, 1, 1);
112MODULE_DEPEND(vboxnetadp, vboxnetflt, 1, 1, 1);
113
114/**
115 * Module event handler
116 */
117static int
118VBoxNetAdpFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg)
119{
120 int rc = 0;
121
122 Log(("VBoxNetAdpFreeBSDModuleEvent\n"));
123
124 switch (enmEventType)
125 {
126 case MOD_LOAD:
127 rc = RTR0Init(0);
128 if (RT_FAILURE(rc))
129 {
130 Log(("RTR0Init failed %d\n", rc));
131 return RTErrConvertToErrno(rc);
132 }
133 rc = vboxNetAdpInit();
134 if (RT_FAILURE(rc))
135 {
136 RTR0Term();
137 Log(("vboxNetAdpInit failed %d\n", rc));
138 return RTErrConvertToErrno(rc);
139 }
140 /* Create dev node */
141 VBoxNetAdpFreeBSDcdev = make_dev(&vboxnetadp_cdevsw, 0,
142 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
143
144 break;
145
146 case MOD_UNLOAD:
147 vboxNetAdpShutdown();
148 destroy_dev(VBoxNetAdpFreeBSDcdev);
149 RTR0Term();
150 break;
151 case MOD_SHUTDOWN:
152 case MOD_QUIESCE:
153 default:
154 return EOPNOTSUPP;
155 }
156
157 if (RT_SUCCESS(rc))
158 return 0;
159 return RTErrConvertToErrno(rc);
160}
161
162/**
163 * Device I/O Control entry point.
164 */
165static int
166VBoxNetAdpFreeBSDCtrlioctl(struct cdev *dev, u_long iCmd, caddr_t data, int flags, struct thread *td)
167{
168 PVBOXNETADP pAdp;
169 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)data;
170 struct ifnet *ifp;
171 int rc;
172
173 switch (iCmd)
174 {
175 case VBOXNETADP_CTL_ADD:
176 if ( !(iCmd & IOC_INOUT) /* paranoia*/
177 || IOCPARM_LEN(iCmd) < sizeof(*pReq))
178 return EINVAL;
179
180 rc = vboxNetAdpCreate(&pAdp,
181 pReq->szName[0] && RTStrEnd(pReq->szName, RT_MIN(IOCPARM_LEN(iCmd), sizeof(pReq->szName))) ?
182 pReq->szName : NULL);
183 if (RT_FAILURE(rc))
184 return EINVAL;
185
186 strncpy(pReq->szName, pAdp->szName, sizeof(pReq->szName) - 1);
187 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
188 break;
189
190 case VBOXNETADP_CTL_REMOVE:
191 if (!RTStrEnd(pReq->szName, RT_MIN(sizeof(pReq->szName), IOCPARM_LEN(iCmd))))
192 return EINVAL;
193
194 pAdp = vboxNetAdpFindByName(pReq->szName);
195 if (!pAdp)
196 return EINVAL;
197
198 rc = vboxNetAdpDestroy(pAdp);
199 if (RT_FAILURE(rc))
200 return EINVAL;
201
202 break;
203
204 default:
205 return EINVAL;
206 }
207 return 0;
208}
209
210/**
211 * Initialize device, just set the running flag.
212 */
213static void VBoxNetAdpFreeBSDNetinit(void *priv)
214{
215 PVBOXNETADP pThis = priv;
216 struct ifnet *ifp = pThis->u.s.ifp;
217
218 ifp->if_drv_flags |= IFF_DRV_RUNNING;
219}
220
221/**
222 * Transmit packets.
223 * netflt has already done everything for us so we just hand the
224 * packets to BPF and increment the packet stats.
225 */
226static void VBoxNetAdpFreeBSDNetstart(struct ifnet *ifp)
227{
228 PVBOXNETADP pThis = ifp->if_softc;
229 struct mbuf *m;
230
231 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING)
232 return;
233
234 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
235 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
236 {
237 ifp->if_opackets++;
238 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
239 BPF_MTAP(ifp, m);
240 m_freem(m);
241 }
242 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
243}
244
245/**
246 * Interface ioctl handling
247 */
248static int VBoxNetAdpFreeBSDNetioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
249{
250 int error = 0;
251
252 switch (cmd)
253 {
254 case SIOCSIFFLAGS:
255 if (ifp->if_flags & IFF_UP)
256 {
257 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
258 ifp->if_init(ifp->if_softc);
259 }
260 else
261 {
262 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
263 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
264 }
265 break;
266 case SIOCGIFMEDIA:
267 {
268 struct ifmediareq *ifmr;
269 int count;
270
271 ifmr = (struct ifmediareq *)data;
272 count = ifmr->ifm_count;
273 ifmr->ifm_count = 1;
274 ifmr->ifm_status = IFM_AVALID;
275 ifmr->ifm_active = IFM_ETHER;
276 ifmr->ifm_status |= IFM_ACTIVE;
277 ifmr->ifm_current = ifmr->ifm_active;
278 if (count >= 1)
279 {
280 int media = IFM_ETHER;
281 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
282 }
283 break;
284 }
285 default:
286 return ether_ioctl(ifp, cmd, data);
287 }
288 return error;
289}
290
291int vboxNetAdpOsInit(PVBOXNETADP pThis)
292{
293 pThis->u.s.ifp = NULL;
294 return VINF_SUCCESS;;
295}
296
297int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMac)
298{
299 struct ifnet *ifp;
300
301 VBOXCURVNET_SET_FROM_UCRED();
302 ifp = if_alloc(IFT_ETHER);
303 if (ifp == NULL)
304 return VERR_NO_MEMORY;
305
306 if_initname(ifp, VBOXNETADP_NAME, pThis->iUnit);
307 ifp->if_softc = pThis;
308 ifp->if_mtu = ETHERMTU;
309 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
310 ifp->if_ioctl = VBoxNetAdpFreeBSDNetioctl;
311 ifp->if_start = VBoxNetAdpFreeBSDNetstart;
312 ifp->if_init = VBoxNetAdpFreeBSDNetinit;
313 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
314 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
315 IFQ_SET_READY(&ifp->if_snd);
316 ether_ifattach(ifp, (void *)pMac);
317 ifp->if_baudrate = 0;
318
319 strncpy(pThis->szName, ifp->if_xname, VBOXNETADP_MAX_NAME_LEN);
320 pThis->u.s.ifp = ifp;
321 VBOXCURVNET_RESTORE();
322 return 0;
323}
324
325void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
326{
327 struct ifnet *ifp;
328
329 ifp = pThis->u.s.ifp;
330 VBOXCURVNET_SET(ifp->if_vnet);
331 ether_ifdetach(ifp);
332 if_free(ifp);
333 VBOXCURVNET_RESTORE();
334}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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