VirtualBox

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

Last change on this file since 23636 was 22285, checked in by vboxsync, 16 years ago

Solaris/VBoxNetAdp: fixed default MAC address.

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