VBoxNetAdp-solaris.c revision ee1c2a1c43d1ac3a8b30a99c4f47a14adc835837
869N/A/* $Id$ */
869N/A/** @file
869N/A * VBoxNetAdapter - Network Adapter Driver (Host), Solaris Specific Code.
869N/A */
869N/A
869N/A/*
869N/A * Copyright (C) 2009 Oracle Corporation
869N/A *
869N/A * This file is part of VirtualBox Open Source Edition (OSE), as
869N/A * available from http://www.virtualbox.org. This file is free software;
869N/A * you can redistribute it and/or modify it under the terms of the GNU
869N/A * General Public License (GPL) as published by the Free Software
869N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
869N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
869N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
869N/A */
869N/A
873N/A/*******************************************************************************
869N/A* Header Files *
869N/A*******************************************************************************/
869N/A#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
869N/A#include <VBox/log.h>
869N/A#include <VBox/err.h>
869N/A#include <VBox/version.h>
869N/A#include <iprt/assert.h>
0N/A#include <iprt/semaphore.h>
0N/A#include <iprt/initterm.h>
0N/A#include <iprt/assert.h>
0N/A#include <iprt/mem.h>
869N/A#include <iprt/rand.h>
0N/A
0N/A#include <sys/types.h>
0N/A#include <sys/dlpi.h>
869N/A#include <sys/types.h>
0N/A#include <sys/param.h>
869N/A#include <sys/stat.h>
0N/A#include <sys/stream.h>
869N/A#include <sys/stropts.h>
869N/A#include <sys/strsun.h>
869N/A#include <sys/modctl.h>
0N/A#include <sys/ddi.h>
869N/A#include <sys/sunddi.h>
48N/A#include <sys/sunldi.h>
869N/A#include <sys/gld.h>
0N/A
869N/A#include "../VBoxNetAdpInternal.h"
716N/A
869N/A/*******************************************************************************
0N/A* Defined Constants And Macros *
0N/A*******************************************************************************/
869N/A#define DEVICE_NAME "vboxnet"
868N/A/** The module descriptions as seen in 'modinfo'. */
983N/A#define DEVICE_DESC_DRV "VirtualBox NetAdp"
1004N/A#define VBOXNETADP_MTU 1500
1004N/A
869N/A#if defined(DEBUG_ramshankar)
869N/A# undef LogFlowFunc
1004N/A# define LogFlowFunc LogRel
0N/A# undef Log
0N/A# define Log LogRel
869N/A# undef LogFlow
868N/A# define LogFlow LogRel
0N/A#endif
0N/A
65N/Astatic int VBoxNetAdpSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
869N/Astatic int VBoxNetAdpSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
868N/A
65N/A/**
869N/A * Streams: module info.
65N/A */
65N/Astatic struct module_info g_VBoxNetAdpSolarisModInfo =
65N/A{
65N/A 0x0dd, /* module id */
65N/A DEVICE_NAME,
65N/A 0, /* min. packet size */
65N/A INFPSZ, /* max. packet size */
65N/A 0, /* hi-water mark */
65N/A 0 /* lo-water mark */
65N/A};
65N/A
65N/A/**
65N/A * Streams: read queue hooks.
65N/A */
65N/Astatic struct qinit g_VBoxNetAdpSolarisReadQ =
0N/A{
869N/A NULL, /* read */
868N/A gld_rsrv,
0N/A gld_open,
0N/A gld_close,
0N/A NULL, /* admin (reserved) */
0N/A &g_VBoxNetAdpSolarisModInfo,
0N/A NULL /* module stats */
0N/A};
0N/A
869N/A/**
0N/A * Streams: write queue hooks.
0N/A */
869N/Astatic struct qinit g_VBoxNetAdpSolarisWriteQ =
868N/A{
0N/A gld_wput,
0N/A gld_wsrv,
869N/A NULL, /* open */
0N/A NULL, /* close */
0N/A NULL, /* admin (reserved) */
869N/A &g_VBoxNetAdpSolarisModInfo,
868N/A NULL /* module stats */
869N/A};
869N/A
868N/A/**
868N/A * Streams: IO stream tab.
869N/A */
869N/Astatic struct streamtab g_VBoxNetAdpSolarisStreamTab =
0N/A{
0N/A &g_VBoxNetAdpSolarisReadQ,
48N/A &g_VBoxNetAdpSolarisWriteQ,
869N/A NULL, /* muxread init */
869N/A NULL /* muxwrite init */
869N/A};
868N/A
869N/A/**
868N/A * cb_ops: driver char/block entry points
869N/A */
0N/Astatic struct cb_ops g_VBoxNetAdpSolarisCbOps =
0N/A{
869N/A nulldev, /* cb open */
868N/A nulldev, /* cb close */
0N/A nodev, /* b strategy */
0N/A nodev, /* b dump */
848N/A nodev, /* b print */
848N/A nodev, /* cb read */
848N/A nodev, /* cb write */
869N/A nodev, /* cb ioctl */
868N/A nodev, /* c devmap */
848N/A nodev, /* c mmap */
0N/A nodev, /* c segmap */
0N/A nochpoll, /* c poll */
0N/A ddi_prop_op, /* property ops */
869N/A &g_VBoxNetAdpSolarisStreamTab,
0N/A D_MP, /* compat. flag */
0N/A CB_REV /* revision */
0N/A};
868N/A
868N/A/**
868N/A * dev_ops: driver entry/exit and other ops.
868N/A */
869N/Astatic struct dev_ops g_VBoxNetAdpSolarisDevOps =
868N/A{
115N/A DEVO_REV, /* driver build revision */
868N/A 0, /* ref count */
868N/A gld_getinfo,
868N/A nulldev, /* identify */
868N/A nulldev, /* probe */
868N/A VBoxNetAdpSolarisAttach,
869N/A VBoxNetAdpSolarisDetach,
868N/A nodev, /* reset */
868N/A &g_VBoxNetAdpSolarisCbOps,
868N/A (struct bus_ops *)0,
868N/A nodev /* power */
868N/A};
868N/A
868N/A/**
869N/A * modldrv: export driver specifics to kernel
868N/A */
868N/Astatic struct modldrv g_VBoxNetAdpSolarisDriver =
868N/A{
868N/A &mod_driverops, /* extern from kernel */
868N/A DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
869N/A &g_VBoxNetAdpSolarisDevOps
868N/A};
868N/A
868N/A/**
868N/A * modlinkage: export install/remove/info to the kernel
868N/A */
869N/Astatic struct modlinkage g_VBoxNetAdpSolarisModLinkage =
868N/A{
868N/A MODREV_1, /* loadable module system revision */
868N/A {
868N/A &g_VBoxNetAdpSolarisDriver, /* adapter streams driver framework */
868N/A NULL /* terminate array of linkage structures */
868N/A }
868N/A};
869N/A
868N/A
868N/A/*******************************************************************************
868N/A* Global Variables *
868N/A*******************************************************************************/
868N/A/** The default ethernet broadcast address */
868N/Astatic uchar_t achBroadcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
868N/A
869N/A/**
868N/A * vboxnetadp_state_t: per-instance data
0N/A */
869N/Atypedef struct vboxnetadp_state_t
0N/A{
868N/A dev_info_t *pDip; /* device info. */
869N/A RTMAC FactoryMac; /* default 'factory' MAC address */
868N/A RTMAC CurrentMac; /* current MAC address */
0N/A} vboxnetadp_state_t;
869N/A
0N/A
868N/A/*******************************************************************************
869N/A* Internal Functions *
868N/A*******************************************************************************/
868N/Astatic int vboxNetAdpSolarisGenerateMac(PRTMAC pMac);
869N/Astatic int vboxNetAdpSolarisSetMacAddress(gld_mac_info_t *pMacInfo, unsigned char *pszMacAddr);
0N/Astatic int vboxNetAdpSolarisSend(gld_mac_info_t *pMacInfo, mblk_t *pMsg);
869N/Astatic int vboxNetAdpSolarisStub(gld_mac_info_t *pMacInfo);
869N/Astatic int vboxNetAdpSolarisSetPromisc(gld_mac_info_t *pMacInfo, int fPromisc);
868N/Astatic int vboxNetAdpSolarisSetMulticast(gld_mac_info_t *pMacInfo, unsigned char *pMulticastAddr, int fMulticast);
868N/A
868N/A
868N/A/**
869N/A * Kernel entry points
869N/A */
869N/Aint _init(void)
869N/A{
869N/A LogFlowFunc((DEVICE_NAME ":_init\n"));
869N/A
869N/A /*
869N/A * Prevent module autounloading.
869N/A */
869N/A modctl_t *pModCtl = mod_getctl(&g_VBoxNetAdpSolarisModLinkage);
869N/A if (pModCtl)
869N/A pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
869N/A else
869N/A LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
869N/A
869N/A /*
869N/A * Initialize IPRT.
869N/A */
869N/A int rc = RTR0Init(0);
869N/A if (RT_SUCCESS(rc))
869N/A {
869N/A rc = mod_install(&g_VBoxNetAdpSolarisModLinkage);
869N/A if (!rc)
869N/A return rc;
869N/A
869N/A LogRel((DEVICE_NAME ":mod_install failed. rc=%d\n", rc));
869N/A RTR0Term();
869N/A }
869N/A else
869N/A LogRel((DEVICE_NAME ":failed to initialize IPRT (rc=%d)\n", rc));
869N/A
869N/A return RTErrConvertToErrno(rc);
869N/A}
869N/A
869N/A
869N/Aint _fini(void)
869N/A{
869N/A LogFlowFunc((DEVICE_NAME ":_fini\n"));
869N/A
869N/A /*
869N/A * Undo the work done during start (in reverse order).
869N/A */
869N/A RTR0Term();
869N/A
869N/A return mod_remove(&g_VBoxNetAdpSolarisModLinkage);
869N/A}
869N/A
869N/A
869N/Aint _info(struct modinfo *pModInfo)
869N/A{
869N/A LogFlowFunc((DEVICE_NAME ":_info\n"));
869N/A
869N/A int rc = mod_info(&g_VBoxNetAdpSolarisModLinkage, pModInfo);
869N/A
869N/A LogFlow((DEVICE_NAME ":_info returns %d\n", rc));
869N/A return rc;
869N/A}
869N/A
869N/A
869N/A/**
869N/A * Attach entry point, to attach a device to the system or resume it.
869N/A *
869N/A * @param pDip The module structure instance.
0N/A * @param enmCmd Operation type (attach/resume).
824N/A *
869N/A * @returns corresponding solaris error code.
868N/A */
824N/Astatic int VBoxNetAdpSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
824N/A{
824N/A LogFlowFunc((DEVICE_NAME ":VBoxNetAdpSolarisAttach pDip=%p enmCmd=%d\n", pDip, enmCmd));
824N/A
824N/A int rc = -1;
869N/A switch (enmCmd)
824N/A {
824N/A case DDI_ATTACH:
869N/A {
869N/A gld_mac_info_t *pMacInfo = gld_mac_alloc(pDip);
869N/A if (pMacInfo)
869N/A {
869N/A vboxnetadp_state_t *pState = RTMemAllocZ(sizeof(vboxnetadp_state_t));
869N/A if (pState)
869N/A {
869N/A pState->pDip = pDip;
869N/A
869N/A /*
869N/A * Setup GLD MAC layer registration info.
869N/A */
869N/A pMacInfo->gldm_reset = vboxNetAdpSolarisStub;
869N/A pMacInfo->gldm_start = vboxNetAdpSolarisStub;
869N/A pMacInfo->gldm_stop = vboxNetAdpSolarisStub;
869N/A pMacInfo->gldm_set_mac_addr = vboxNetAdpSolarisSetMacAddress;
869N/A pMacInfo->gldm_set_multicast = vboxNetAdpSolarisSetMulticast;
869N/A pMacInfo->gldm_set_promiscuous = vboxNetAdpSolarisSetPromisc;
869N/A pMacInfo->gldm_send = vboxNetAdpSolarisSend;
869N/A pMacInfo->gldm_intr = NULL;
869N/A pMacInfo->gldm_get_stats = NULL;
869N/A pMacInfo->gldm_ioctl = NULL;
869N/A pMacInfo->gldm_ident = DEVICE_NAME;
869N/A pMacInfo->gldm_type = DL_ETHER;
869N/A pMacInfo->gldm_minpkt = 0;
869N/A pMacInfo->gldm_maxpkt = VBOXNETADP_MTU;
869N/A
869N/A AssertCompile(sizeof(RTMAC) == ETHERADDRL);
869N/A
849N/A pMacInfo->gldm_addrlen = ETHERADDRL;
0N/A pMacInfo->gldm_saplen = -2;
869N/A pMacInfo->gldm_broadcast_addr = achBroadcastAddr;
868N/A pMacInfo->gldm_ppa = ddi_get_instance(pState->pDip);
0N/A pMacInfo->gldm_devinfo = pState->pDip;
0N/A pMacInfo->gldm_private = (caddr_t)pState;
0N/A
0N/A /*
0N/A * We use a semi-random MAC addresses similar to a guest NIC's MAC address
0N/A * as the default factory address of the interface.
869N/A */
869N/A rc = vboxNetAdpSolarisGenerateMac(&pState->FactoryMac);
869N/A if (RT_SUCCESS(rc))
869N/A {
869N/A bcopy(&pState->FactoryMac, &pState->CurrentMac, sizeof(RTMAC));
869N/A pMacInfo->gldm_vendor_addr = (unsigned char *)&pState->FactoryMac;
869N/A
869N/A /*
0N/A * Now try registering our GLD with the MAC layer.
869N/A * Registration can fail on some S10 versions when the MTU size is more than 1500.
0N/A * When we implement jumbo frames we should probably retry with MTU 1500 for S10.
0N/A */
869N/A rc = gld_register(pDip, (char *)ddi_driver_name(pDip), pMacInfo);
869N/A if (rc == DDI_SUCCESS)
869N/A {
869N/A ddi_report_dev(pDip);
869N/A return DDI_SUCCESS;
868N/A }
0N/A else
824N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to register GLD. rc=%d\n", rc));
824N/A }
824N/A else
824N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to generate mac address.rc=%d\n"));
869N/A
869N/A RTMemFree(pState);
869N/A }
869N/A else
869N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to alloc state.\n"));
869N/A
869N/A gld_mac_free(pMacInfo);
869N/A }
869N/A else
868N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisAttach failed to alloc mac structure.\n"));
824N/A return DDI_FAILURE;
868N/A }
869N/A
869N/A case DDI_RESUME:
868N/A {
869N/A /* Nothing to do here... */
869N/A return DDI_SUCCESS;
824N/A }
824N/A
869N/A /* case DDI_PM_RESUME: */
869N/A default:
869N/A return DDI_FAILURE;
869N/A }
869N/A}
869N/A
869N/A
869N/A/**
0N/A * Detach entry point, to detach a device to the system or suspend it.
869N/A *
869N/A * @param pDip The module structure instance.
869N/A * @param enmCmd Operation type (detach/suspend).
0N/A *
0N/A * @returns corresponding solaris error code.
869N/A */
0N/Astatic int VBoxNetAdpSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
869N/A{
869N/A LogFlowFunc((DEVICE_NAME ":VBoxNetAdpSolarisDetach pDip=%p enmCmd=%d\n", pDip, enmCmd));
0N/A
0N/A switch (enmCmd)
0N/A {
0N/A case DDI_DETACH:
869N/A {
868N/A /*
0N/A * Unregister and clean up.
0N/A */
0N/A gld_mac_info_t *pMacInfo = ddi_get_driver_private(pDip);
0N/A if (pMacInfo)
0N/A {
869N/A vboxnetadp_state_t *pState = (vboxnetadp_state_t *)pMacInfo->gldm_private;
0N/A if (pState)
0N/A {
0N/A int rc = gld_unregister(pMacInfo);
0N/A if (rc == DDI_SUCCESS)
0N/A {
0N/A gld_mac_free(pMacInfo);
46N/A RTMemFree(pState);
869N/A return DDI_SUCCESS;
0N/A }
0N/A else
46N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to unregister GLD from MAC layer.rc=%d\n", rc));
0N/A }
0N/A else
0N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to get internal state.\n"));
0N/A }
869N/A else
0N/A LogRel((DEVICE_NAME ":VBoxNetAdpSolarisDetach failed to get driver private GLD data.\n"));
0N/A
0N/A return DDI_FAILURE;
0N/A }
0N/A
0N/A case DDI_RESUME:
869N/A {
0N/A /* Nothing to do here... */
0N/A return DDI_SUCCESS;
0N/A }
756N/A
869N/A /* case DDI_SUSPEND: */
868N/A /* case DDI_HOTPLUG_DETACH: */
756N/A default:
756N/A return DDI_FAILURE;
988N/A }
988N/A}
988N/A
988N/A
988N/Astatic int vboxNetAdpSolarisGenerateMac(PRTMAC pMac)
988N/A{
988N/A pMac->au8[0] = 0x08;
756N/A pMac->au8[1] = 0x00;
756N/A pMac->au8[2] = 0x27;
756N/A RTRandBytes(&pMac->au8[3], 3);
869N/A LogFlow((DEVICE_NAME ":VBoxNetAdpSolarisGenerateMac Generated %.*Rhxs\n", sizeof(RTMAC), &pMac));
756N/A return VINF_SUCCESS;
869N/A}
756N/A
0N/A
869N/Astatic int vboxNetAdpSolarisSetMacAddress(gld_mac_info_t *pMacInfo, unsigned char *pszMacAddr)
868N/A{
0N/A vboxnetadp_state_t *pState = (vboxnetadp_state_t *)pMacInfo->gldm_private;
0N/A if (pState)
0N/A {
0N/A bcopy(pszMacAddr, &pState->CurrentMac, sizeof(RTMAC));
0N/A LogFlow((DEVICE_NAME ":vboxNetAdpSolarisSetMacAddress updated MAC %.*Rhxs\n", sizeof(RTMAC), &pState->CurrentMac));
869N/A return GLD_SUCCESS;
869N/A }
0N/A else
0N/A LogRel((DEVICE_NAME ":vboxNetAdpSolarisSetMacAddress failed to get internal state.\n"));
0N/A return GLD_FAILURE;
0N/A}
824N/A
869N/A
868N/Astatic int vboxNetAdpSolarisSend(gld_mac_info_t *pMacInfo, mblk_t *pMsg)
824N/A{
824N/A while (pMsg)
824N/A {
869N/A mblk_t *pMsgNext = pMsg->b_cont;
868N/A pMsg->b_cont = NULL;
824N/A freemsg(pMsg);
869N/A pMsg = pMsgNext;
824N/A }
869N/A return GLD_SUCCESS;
869N/A}
869N/A
869N/A
869N/Astatic int vboxNetAdpSolarisStub(gld_mac_info_t *pMacInfo)
869N/A{
869N/A return GLD_SUCCESS;
869N/A}
869N/A
869N/A
869N/Astatic int vboxNetAdpSolarisSetMulticast(gld_mac_info_t *pMacInfo, unsigned char *pMulticastAddr, int fMulticast)
869N/A{
869N/A return GLD_SUCCESS;
869N/A}
869N/A
868N/A
868N/Astatic int vboxNetAdpSolarisSetPromisc(gld_mac_info_t *pMacInfo, int fPromisc)
869N/A{
868N/A /* Host requesting promiscuous intnet connection... */
868N/A return GLD_SUCCESS;
869N/A}
868N/A
824N/A