VirtualBox

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

Last change on this file since 34079 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: VBoxNetAdp-freebsd.c 33540 2010-10-28 09:27:05Z 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_types.h>
52#include <net/ethernet.h>
53#include <net/bpf.h>
54
55#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
56#include <VBox/version.h>
57#include <VBox/err.h>
58#include <VBox/log.h>
59#include <iprt/initterm.h>
60#include <iprt/string.h>
61#include <iprt/spinlock.h>
62#include <iprt/process.h>
63#include <iprt/assert.h>
64#include <iprt/uuid.h>
65#include <iprt/alloc.h>
66#include <iprt/err.h>
67
68#define VBOXNETADP_OS_SPECFIC 1
69#include "../VBoxNetAdpInternal.h"
70
71static int VBoxNetAdpFreeBSDCtrlioctl(struct cdev *, u_long, caddr_t, int flags,
72 struct thread *);
73static struct cdevsw vboxnetadp_cdevsw =
74{
75 .d_version = D_VERSION,
76 .d_ioctl = VBoxNetAdpFreeBSDCtrlioctl,
77 .d_read = (d_read_t *)nullop,
78 .d_write = (d_write_t *)nullop,
79 .d_name = VBOXNETADP_CTL_DEV_NAME,
80};
81
82static struct cdev *VBoxNetAdpFreeBSDcdev;
83
84static int VBoxNetAdpFreeBSDModuleEvent(struct module *, int, void *);
85static moduledata_t g_VBoxNetAdpFreeBSDModule = {
86 "vboxnetadp",
87 VBoxNetAdpFreeBSDModuleEvent,
88 NULL
89};
90
91/** Declare the module as a pseudo device. */
92DECLARE_MODULE(vboxnetadp, g_VBoxNetAdpFreeBSDModule, SI_SUB_PSEUDO, SI_ORDER_ANY);
93MODULE_VERSION(vboxnetadp, 1);
94MODULE_DEPEND(vboxnetadp, vboxdrv, 1, 1, 1);
95MODULE_DEPEND(vboxnetadp, vboxnetflt, 1, 1, 1);
96
97/**
98 * Module event handler
99 */
100static int
101VBoxNetAdpFreeBSDModuleEvent(struct module *pMod, int enmEventType, void *pvArg)
102{
103 int rc = 0;
104
105 Log(("VBoxNetAdpFreeBSDModuleEvent\n"));
106
107 switch (enmEventType)
108 {
109 case MOD_LOAD:
110 rc = RTR0Init(0);
111 if (RT_FAILURE(rc))
112 {
113 Log(("RTR0Init failed %d\n", rc));
114 return RTErrConvertToErrno(rc);
115 }
116 rc = vboxNetAdpInit();
117 if (RT_FAILURE(rc))
118 {
119 RTR0Term();
120 Log(("vboxNetAdpInit failed %d\n", rc));
121 return RTErrConvertToErrno(rc);
122 }
123 /* Create dev node */
124 VBoxNetAdpFreeBSDcdev = make_dev(&vboxnetadp_cdevsw, 0,
125 UID_ROOT, GID_WHEEL, 0600, VBOXNETADP_CTL_DEV_NAME);
126
127 break;
128
129 case MOD_UNLOAD:
130 vboxNetAdpShutdown();
131 destroy_dev(VBoxNetAdpFreeBSDcdev);
132 RTR0Term();
133 break;
134 case MOD_SHUTDOWN:
135 case MOD_QUIESCE:
136 default:
137 return EOPNOTSUPP;
138 }
139
140 if (RT_SUCCESS(rc))
141 return 0;
142 return RTErrConvertToErrno(rc);
143}
144
145/**
146 * Device I/O Control entry point.
147 */
148static int
149VBoxNetAdpFreeBSDCtrlioctl(struct cdev *dev, u_long iCmd, caddr_t data, int flags, struct thread *td)
150{
151 PVBOXNETADP pAdp;
152 PVBOXNETADPREQ pReq = (PVBOXNETADPREQ)data;
153 struct ifnet *ifp;
154 int rc;
155
156 switch (iCmd)
157 {
158 case VBOXNETADP_CTL_ADD:
159 if ( !(iCmd & IOC_OUT) /* paranoia*/
160 || IOCPARM_LEN(iCmd) < sizeof(*pReq))
161 return EINVAL;
162
163 rc = vboxNetAdpCreate(&pAdp);
164 if (RT_FAILURE(rc))
165 return EINVAL;
166
167 strncpy(pReq->szName, pAdp->szName, sizeof(pReq->szName) - 1);
168 pReq->szName[sizeof(pReq->szName) - 1] = '\0';
169 break;
170
171 case VBOXNETADP_CTL_REMOVE:
172 if (!RTStrEnd(pReq->szName, RT_MIN(sizeof(pReq->szName), IOCPARM_LEN(iCmd))))
173 return EINVAL;
174
175 pAdp = vboxNetAdpFindByName(pReq->szName);
176 if (!pAdp)
177 return EINVAL;
178
179 rc = vboxNetAdpDestroy(pAdp);
180 if (RT_FAILURE(rc))
181 return EINVAL;
182
183 break;
184
185 default:
186 return EINVAL;
187 }
188 return 0;
189}
190
191/**
192 * Initialize device, just set the running flag.
193 */
194static void VBoxNetAdpFreeBSDNetinit(void *priv)
195{
196 PVBOXNETADP pThis = priv;
197 struct ifnet *ifp = pThis->u.s.ifp;
198
199 ifp->if_drv_flags |= IFF_DRV_RUNNING;
200}
201
202/**
203 * Transmit packets.
204 * netflt has already done everything for us so we just hand the
205 * packets to BPF and increment the packet stats.
206 */
207static void VBoxNetAdpFreeBSDNetstart(struct ifnet *ifp)
208{
209 PVBOXNETADP pThis = ifp->if_softc;
210 struct mbuf *m;
211
212 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != IFF_DRV_RUNNING)
213 return;
214
215 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
216 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
217 {
218 ifp->if_opackets++;
219 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
220 BPF_MTAP(ifp, m);
221 m_freem(m);
222 }
223 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
224}
225
226/**
227 * Interface ioctl handling
228 */
229static int VBoxNetAdpFreeBSDNetioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
230{
231 switch (cmd)
232 {
233 case SIOCSIFFLAGS:
234 if (ifp->if_flags & IFF_UP)
235 {
236 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
237 ifp->if_init(ifp->if_softc);
238 }
239 else
240 {
241 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
242 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
243 }
244 break;
245 default:
246 return ether_ioctl(ifp, cmd, data);
247 }
248 return 0;
249}
250
251int vboxNetAdpOsInit(PVBOXNETADP pThis)
252{
253 pThis->u.s.ifp = NULL;
254 return VINF_SUCCESS;;
255}
256
257int vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMac)
258{
259 struct ifnet *ifp;
260
261 ifp = if_alloc(IFT_ETHER);
262 if (ifp == NULL)
263 return VERR_NO_MEMORY;
264
265 if_initname(ifp, VBOXNETADP_NAME, pThis->uUnit);
266 ifp->if_softc = pThis;
267 ifp->if_mtu = ETHERMTU;
268 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
269 ifp->if_ioctl = VBoxNetAdpFreeBSDNetioctl;
270 ifp->if_start = VBoxNetAdpFreeBSDNetstart;
271 ifp->if_init = VBoxNetAdpFreeBSDNetinit;
272 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
273 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
274 IFQ_SET_READY(&ifp->if_snd);
275 ether_ifattach(ifp, (void *)pMac);
276 ifp->if_baudrate = 0;
277
278 strncpy(pThis->szName, ifp->if_xname, VBOXNETADP_MAX_NAME_LEN);
279 pThis->u.s.ifp = ifp;
280 return 0;
281}
282
283void vboxNetAdpOsDestroy(PVBOXNETADP pThis)
284{
285 struct ifnet *ifp;
286
287 ifp = pThis->u.s.ifp;
288 ether_ifdetach(ifp);
289 if_free(ifp);
290}
Note: See TracBrowser for help on using the repository browser.

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