VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetAdp/solaris/VBoxNetAdp-solaris.c@ 24684

Last change on this file since 24684 was 24684, checked in by vboxsync, 15 years ago

Solaris/VBoxNetAdp: RT_XSTR.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
1/* $Id: VBoxNetAdp-solaris.c 24684 2009-11-16 09:30:40Z vboxsync $ */
2/** @file
3 * VBoxNetAdapter - Network Adapter Driver (Host), Solaris Specific Code.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
26#include <VBox/log.h>
27#include <VBox/err.h>
28#include <VBox/version.h>
29#include <iprt/assert.h>
30#include <iprt/semaphore.h>
31#include <iprt/initterm.h>
32#include <iprt/assert.h>
33#include <iprt/mem.h>
34#include <iprt/rand.h>
35
36#include <sys/types.h>
37#include <sys/dlpi.h>
38#include <sys/types.h>
39#include <sys/param.h>
40#include <sys/stat.h>
41#include <sys/stream.h>
42#include <sys/stropts.h>
43#include <sys/strsun.h>
44#include <sys/modctl.h>
45#include <sys/ddi.h>
46#include <sys/sunddi.h>
47#include <sys/sunldi.h>
48#include <sys/gld.h>
49
50#include "../VBoxNetAdpInternal.h"
51
52/*******************************************************************************
53* Defined Constants And Macros *
54*******************************************************************************/
55#define DEVICE_NAME "vboxnet"
56/** The module descriptions as seen in 'modinfo'. */
57#define DEVICE_DESC_DRV "VirtualBox NetAdp"
58#define VBOXNETADP_MTU 1500
59
60#if defined(DEBUG_ramshankar)
61# undef Log
62# define Log LogRel
63# undef LogFlow
64# define LogFlow LogRel
65#endif
66
67static int VBoxNetAdpSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
68static int VBoxNetAdpSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
69
70/**
71 * Streams: module info.
72 */
73static struct module_info g_VBoxNetAdpSolarisModInfo =
74{
75 0x0dd, /* module id */
76 DEVICE_NAME,
77 0, /* min. packet size */
78 INFPSZ, /* max. packet size */
79 0, /* hi-water mark */
80 0 /* lo-water mark */
81};
82
83/**
84 * Streams: read queue hooks.
85 */
86static struct qinit g_VBoxNetAdpSolarisReadQ =
87{
88 NULL, /* read */
89 gld_rsrv,
90 gld_open,
91 gld_close,
92 NULL, /* admin (reserved) */
93 &g_VBoxNetAdpSolarisModInfo,
94 NULL /* module stats */
95};
96
97/**
98 * Streams: write queue hooks.
99 */
100static struct qinit g_VBoxNetAdpSolarisWriteQ =
101{
102 gld_wput,
103 gld_wsrv,
104 NULL, /* open */
105 NULL, /* close */
106 NULL, /* admin (reserved) */
107 &g_VBoxNetAdpSolarisModInfo,
108 NULL /* module stats */
109};
110
111/**
112 * Streams: IO stream tab.
113 */
114static struct streamtab g_VBoxNetAdpSolarisStreamTab =
115{
116 &g_VBoxNetAdpSolarisReadQ,
117 &g_VBoxNetAdpSolarisWriteQ,
118 NULL, /* muxread init */
119 NULL /* muxwrite init */
120};
121
122/**
123 * cb_ops: driver char/block entry points
124 */
125static struct cb_ops g_VBoxNetAdpSolarisCbOps =
126{
127 nulldev, /* cb open */
128 nulldev, /* cb close */
129 nodev, /* b strategy */
130 nodev, /* b dump */
131 nodev, /* b print */
132 nodev, /* cb read */
133 nodev, /* cb write */
134 nodev, /* cb ioctl */
135 nodev, /* c devmap */
136 nodev, /* c mmap */
137 nodev, /* c segmap */
138 nochpoll, /* c poll */
139 ddi_prop_op, /* property ops */
140 &g_VBoxNetAdpSolarisStreamTab,
141 D_MP, /* compat. flag */
142 CB_REV /* revision */
143};
144
145/**
146 * dev_ops: driver entry/exit and other ops.
147 */
148static struct dev_ops g_VBoxNetAdpSolarisDevOps =
149{
150 DEVO_REV, /* driver build revision */
151 0, /* ref count */
152 gld_getinfo,
153 nulldev, /* identify */
154 nulldev, /* probe */
155 VBoxNetAdpSolarisAttach,
156 VBoxNetAdpSolarisDetach,
157 nodev, /* reset */
158 &g_VBoxNetAdpSolarisCbOps,
159 (struct bus_ops *)0,
160 nodev /* power */
161};
162
163/**
164 * modldrv: export driver specifics to kernel
165 */
166static struct modldrv g_VBoxNetAdpSolarisDriver =
167{
168 &mod_driverops, /* extern from kernel */
169 DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
170 &g_VBoxNetAdpSolarisDevOps
171};
172
173/**
174 * modlinkage: export install/remove/info to the kernel
175 */
176static struct modlinkage g_VBoxNetAdpSolarisModLinkage =
177{
178 MODREV_1, /* loadable module system revision */
179 &g_VBoxNetAdpSolarisDriver, /* adapter streams driver framework */
180 NULL /* terminate array of linkage structures */
181};
182
183
184/*******************************************************************************
185* Global Variables *
186*******************************************************************************/
187/** The default ethernet broadcast address */
188static uchar_t achBroadcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
189
190/**
191 * vboxnetadp_state_t: per-instance data
192 */
193typedef struct vboxnetadp_state_t
194{
195 dev_info_t *pDip; /* device info. */
196 RTMAC FactoryMac; /* default 'factory' MAC address */
197 RTMAC CurrentMac; /* current MAC address */
198} vboxnetadp_state_t;
199
200
201/*******************************************************************************
202* Internal Functions *
203*******************************************************************************/
204static int vboxNetAdpSolarisGenerateMac(PRTMAC pMac);
205static int vboxNetAdpSolarisSetMacAddress(gld_mac_info_t *pMacInfo, unsigned char *pszMacAddr);
206static int vboxNetAdpSolarisSend(gld_mac_info_t *pMacInfo, mblk_t *pMsg);
207static int vboxNetAdpSolarisStub(gld_mac_info_t *pMacInfo);
208static int vboxNetAdpSolarisSetPromisc(gld_mac_info_t *pMacInfo, int fPromisc);
209static int vboxNetAdpSolarisSetMulticast(gld_mac_info_t *pMacInfo, unsigned char *pMulticastAddr, int fMulticast);
210
211
212/**
213 * Kernel entry points
214 */
215int _init(void)
216{
217 LogFlow((DEVICE_NAME ":_init\n"));
218
219 /*
220 * Prevent module autounloading.
221 */
222 modctl_t *pModCtl = mod_getctl(&g_VBoxNetAdpSolarisModLinkage);
223 if (pModCtl)
224 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
225 else
226 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
227
228 /*
229 * Initialize IPRT.
230 */
231 int rc = RTR0Init(0);
232 if (RT_SUCCESS(rc))
233 {
234 rc = mod_install(&g_VBoxNetAdpSolarisModLinkage);
235 if (!rc)
236 return rc;
237
238 LogRel((DEVICE_NAME ":mod_install failed. rc=%d\n", rc));
239 RTR0Term();
240 }
241 else
242 LogRel((DEVICE_NAME ":failed to initialize IPRT (rc=%d)\n", rc));
243
244 return RTErrConvertToErrno(rc);
245}
246
247
248int _fini(void)
249{
250 int rc;
251 LogFlow((DEVICE_NAME ":_fini\n"));
252
253 /*
254 * Undo the work done during start (in reverse order).
255 */
256 RTR0Term();
257
258 return mod_remove(&g_VBoxNetAdpSolarisModLinkage);
259}
260
261
262int _info(struct modinfo *pModInfo)
263{
264 LogFlow((DEVICE_NAME ":_info\n"));
265
266 int rc = mod_info(&g_VBoxNetAdpSolarisModLinkage, pModInfo);
267
268 LogFlow((DEVICE_NAME ":_info returns %d\n", rc));
269 return rc;
270}
271
272
273/**
274 * Attach entry point, to attach a device to the system or resume it.
275 *
276 * @param pDip The module structure instance.
277 * @param enmCmd Operation type (attach/resume).
278 *
279 * @returns corresponding solaris error code.
280 */
281static int VBoxNetAdpSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
282{
283 LogFlow((DEVICE_NAME ":VBoxNetAdpSolarisAttach pDip=%p enmCmd=%d\n", pDip, enmCmd));
284
285 int rc = -1;
286 switch (enmCmd)
287 {
288 case DDI_ATTACH:
289 {
290 gld_mac_info_t *pMacInfo = gld_mac_alloc(pDip);
291 if (pMacInfo)
292 {
293 vboxnetadp_state_t *pState = RTMemAllocZ(sizeof(vboxnetadp_state_t));
294 if (pState)
295 {
296 pState->pDip = pDip;
297
298 /*
299 * Setup GLD MAC layer registeration info.
300 */
301 pMacInfo->gldm_reset = vboxNetAdpSolarisStub;
302 pMacInfo->gldm_start = vboxNetAdpSolarisStub;
303 pMacInfo->gldm_stop = vboxNetAdpSolarisStub;
304 pMacInfo->gldm_set_mac_addr = vboxNetAdpSolarisSetMacAddress;
305 pMacInfo->gldm_set_multicast = vboxNetAdpSolarisSetMulticast;
306 pMacInfo->gldm_set_promiscuous = vboxNetAdpSolarisSetPromisc;
307 pMacInfo->gldm_send = vboxNetAdpSolarisSend;
308 pMacInfo->gldm_intr = NULL;
309 pMacInfo->gldm_get_stats = NULL;
310 pMacInfo->gldm_ioctl = NULL;
311 pMacInfo->gldm_ident = DEVICE_NAME;
312 pMacInfo->gldm_type = DL_ETHER;
313 pMacInfo->gldm_minpkt = 0;
314 pMacInfo->gldm_maxpkt = VBOXNETADP_MTU;
315
316 AssertCompile(sizeof(RTMAC) == ETHERADDRL);
317
318 pMacInfo->gldm_addrlen = ETHERADDRL;
319 pMacInfo->gldm_saplen = -2;
320 pMacInfo->gldm_broadcast_addr = achBroadcastAddr;
321 pMacInfo->gldm_ppa = ddi_get_instance(pState->pDip);
322 pMacInfo->gldm_devinfo = pState->pDip;
323 pMacInfo->gldm_private = (caddr_t)pState;
324
325 /*
326 * We use a semi-random MAC addresses similar to a guest NIC's MAC address
327 * as the default factory address of the interface.
328 */
329 rc = vboxNetAdpSolarisGenerateMac(&pState->FactoryMac);
330 if (RT_SUCCESS(rc))
331 {
332 bcopy(&pState->FactoryMac, &pState->CurrentMac, sizeof(RTMAC));
333 pMacInfo->gldm_vendor_addr = (unsigned char *)&pState->FactoryMac;
334
335 /*
336 * Now try registering our GLD with the MAC layer.
337 * Registeration can fail on some S10 versions when the MTU size is more than 1500.
338 * When we implement jumbo frames we should probably retry with MTU 1500 for S10.
339 */
340 rc = gld_register(pDip, (char *)ddi_driver_name(pDip), pMacInfo);
341 if (rc == DDI_SUCCESS)
342 {
343 ddi_report_dev(pDip);
344 return DDI_SUCCESS;
345 }
346 else
347 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to register GLD. rc=%d\n", rc));
348 }
349 else
350 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to generate mac address.rc=%d\n"));
351
352 RTMemFree(pState);
353 }
354 else
355 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to alloc state.\n"));
356
357 gld_mac_free(pMacInfo);
358 }
359 else
360 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to alloc mac structure.\n"));
361 return DDI_FAILURE;
362 }
363
364 case DDI_RESUME:
365 {
366 /* Nothing to do here... */
367 return DDI_SUCCESS;
368 }
369 }
370 return DDI_FAILURE;
371}
372
373
374/**
375 * Detach entry point, to detach a device to the system or suspend it.
376 *
377 * @param pDip The module structure instance.
378 * @param enmCmd Operation type (detach/suspend).
379 *
380 * @returns corresponding solaris error code.
381 */
382static int VBoxNetAdpSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
383{
384 LogFlow((DEVICE_NAME ":VBoxNetAdpSolarisDetach pDip=%p enmCmd=%d\n", pDip, enmCmd));
385
386 switch (enmCmd)
387 {
388 case DDI_DETACH:
389 {
390 /*
391 * Unregister and clean up.
392 */
393 gld_mac_info_t *pMacInfo = ddi_get_driver_private(pDip);
394 if (pMacInfo)
395 {
396 vboxnetadp_state_t *pState = (vboxnetadp_state_t *)pMacInfo->gldm_private;
397 if (pState)
398 {
399 int rc = gld_unregister(pMacInfo);
400 if (rc == DDI_SUCCESS)
401 {
402 gld_mac_free(pMacInfo);
403 RTMemFree(pState);
404 return DDI_SUCCESS;
405 }
406 else
407 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to unregister GLD from MAC layer.rc=%d\n", rc));
408 }
409 else
410 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to get internal state.\n"));
411 }
412 else
413 LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to get driver private GLD data.\n"));
414
415 return DDI_FAILURE;
416 }
417
418 case DDI_RESUME:
419 {
420 /* Nothing to do here... */
421 return DDI_SUCCESS;
422 }
423 }
424 return DDI_FAILURE;
425}
426
427
428static int vboxNetAdpSolarisGenerateMac(PRTMAC pMac)
429{
430 pMac->au8[0] = 0x08;
431 pMac->au8[1] = 0x00;
432 pMac->au8[2] = 0x27;
433 RTRandBytes(&pMac->au8[3], 3);
434 LogFlow((DEVICE_NAME ":VBoxNetAdpSolarisGenerateMac Generated %.*Rhxs\n", sizeof(RTMAC), &pMac));
435 return VINF_SUCCESS;
436}
437
438
439static int vboxNetAdpSolarisSetMacAddress(gld_mac_info_t *pMacInfo, unsigned char *pszMacAddr)
440{
441 vboxnetadp_state_t *pState = (vboxnetadp_state_t *)pMacInfo->gldm_private;
442 if (pState)
443 {
444 bcopy(pszMacAddr, &pState->CurrentMac, sizeof(RTMAC));
445 LogFlow((DEVICE_NAME ":vboxNetAdpSolarisSetMacAddress updated MAC %.*Rhxs\n", sizeof(RTMAC), &pState->CurrentMac));
446 return GLD_SUCCESS;
447 }
448 else
449 LogRel((DEVICE_NAME ":vboxNetAdpSolarisSetMacAddress failed to get internal state.\n"));
450 return GLD_FAILURE;
451}
452
453
454static int vboxNetAdpSolarisSend(gld_mac_info_t *pMacInfo, mblk_t *pMsg)
455{
456 freemsg(pMsg);
457 return GLD_SUCCESS;
458}
459
460
461static int vboxNetAdpSolarisStub(gld_mac_info_t *pMacInfo)
462{
463 return GLD_SUCCESS;
464}
465
466
467static int vboxNetAdpSolarisSetMulticast(gld_mac_info_t *pMacInfo, unsigned char *pMulticastAddr, int fMulticast)
468{
469 return GLD_SUCCESS;
470}
471
472
473static int vboxNetAdpSolarisSetPromisc(gld_mac_info_t *pMacInfo, int fPromisc)
474{
475 /* Host requesting promiscuous intnet connection... */
476 return GLD_SUCCESS;
477}
478
Note: See TracBrowser for help on using the repository browser.

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