VBoxNetFltBow-solaris.c revision b3722597c0cc236108a7ba09de71b32d7e098793
/* $Id$ */
/** @file
* VBoxNetFlt - Network Filter Driver (Host), Solaris Specific Code.
*/
/*
* Copyright (C) 2008 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_NET_FLT_DRV
#include <VBox/intnetinline.h>
#include <iprt/initterm.h>
#include <iprt/spinlock.h>
#include <sys/dls_mgmt.h>
#include <sys/vnic_mgmt.h>
#include <sys/mac_client.h>
#include <sys/mac_provider.h>
#if 0
#include "include/mac_provider.h" /* dependency for other headers */
#include "include/mac_client.h" /* for mac_* */
#include "include/mac_client_priv.h" /* for mac_info, mac_capab_get etc. */
#if 1
#include "include/vnic_impl.h" /* for vnic_dev_create */
#endif
#endif
#define VBOXNETFLT_OS_SPECFIC 1
#include "../VBoxNetFltInternal.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The module name. */
#define DEVICE_NAME "vboxflt"
/** The module descriptions as seen in 'modinfo'. */
#define DEVICE_DESC_DRV "VirtualBox NetBow"
/** The dynamically created VNIC name */
#define VBOXFLT_VNIC_NAME "vboxvnic"
/** Debugging switch for using symbols in kmdb */
# define LOCAL static
/** VBOXNETFLTVNIC::u32Magic */
# define VBOXNETFLTVNIC_MAGIC 0x0ddfaced
#if defined(DEBUG_ramshankar)
# define LOCAL
#endif
/** VLAN tag masking, should probably be in IPRT? */
typedef struct VLANHEADER
{
} VLANHEADER;
typedef struct VLANHEADER *PVLANHEADER;
/*******************************************************************************
* Kernel Entry Hooks *
*******************************************************************************/
LOCAL int VBoxNetFltSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
*/
static struct cb_ops g_VBoxNetFltSolarisCbOps =
{
nulldev, /* c open */
nulldev, /* c close */
nodev, /* b strategy */
nodev, /* b dump */
nodev, /* b print */
nodev, /* c read */
nodev, /* c write*/
nodev, /* c ioctl*/
nodev, /* c devmap */
nodev, /* c mmap */
nodev, /* c segmap */
nochpoll, /* c poll */
ddi_prop_op, /* property ops */
NULL, /* streamtab */
CB_REV, /* revision */
nodev, /* c aread */
nodev /* c awrite */
};
/**
* dev_ops: for driver device operations
*/
static struct dev_ops g_VBoxNetFltSolarisDevOps =
{
DEVO_REV, /* driver build revision */
0, /* ref count */
nulldev, /* identify */
nulldev, /* probe */
nodev, /* reset */
NULL, /* bus ops */
nodev, /* power */
};
/**
* modldrv: export driver specifics to the kernel
*/
static struct modldrv g_VBoxNetFltSolarisModule =
{
&mod_driverops, /* extern from kernel */
};
/**
*/
static struct modlinkage g_VBoxNetFltSolarisModLinkage =
{
{
NULL,
}
};
/**
* VBOXNETFLTVNIC: Per-VNIC instance data.
*/
typedef struct VBOXNETFLTVNIC
{
bool fCreated; /* Whether we created the VNIC or not */
void *pvIf; /* The VirtualBox interface */
typedef struct VBOXNETFLTVNIC *PVBOXNETFLTVNIC;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Global Device handle we only support one instance. */
/** Global Mutex (actually an rw lock). */
/** The (common) global data. */
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
LOCAL int vboxNetFltSolarisMBlkToSG(PVBOXNETFLTINS pThis, mblk_t *pMsg, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc);
LOCAL void vboxNetFltSolarisRecv(void *pvData, mac_resource_handle_t hResource, mblk_t *pMsg, boolean_t fLoopback);
/**
* Kernel entry points
*/
int _init(void)
{
/*
* Prevent module autounloading.
*/
if (pModCtl)
else
/*
* Initialize IPRT.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize Solaris specific globals here.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the globals and connect to the support driver.
*
* This will call back vboxNetFltOsOpenSupDrv (and maybe vboxNetFltOsCloseSupDrv)
* for establishing the connect to the support driver.
*/
if (RT_SUCCESS(rc))
{
if (!rc)
return rc;
}
else
}
RTR0Term();
}
else
return RTErrConvertToErrno(rc);
}
int _fini(void)
{
int rc;
/*
* Undo the work done during start (in reverse order).
*/
if (RT_FAILURE(rc))
{
return EBUSY;
}
if (!rc)
{
{
}
RTR0Term();
}
return rc;
}
{
return rc;
}
/**
* Attach entry point, to attach a device to the system or resume it.
*
* @param pDip The module structure instance.
*
* @returns corresponding solaris error code.
*/
{
switch (enmCmd)
{
case DDI_ATTACH:
{
int rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0, "none", "none", 0666);
if (rc == DDI_SUCCESS)
{
return DDI_SUCCESS;
}
else
return DDI_FAILURE;
}
case DDI_RESUME:
{
/* Nothing to do here... */
return DDI_SUCCESS;
}
/* case DDI_PM_RESUME: */
default:
return DDI_FAILURE;
}
}
/**
* Detach entry point, to detach a device to the system or suspend it.
*
* @param pDip The module structure instance.
*
* @returns corresponding solaris error code.
*/
{
switch (enmCmd)
{
case DDI_DETACH:
{
return DDI_SUCCESS;
}
case DDI_RESUME:
{
/* Nothing to do here... */
return DDI_SUCCESS;
}
/* case DDI_PM_SUSPEND: */
/* case DDI_HOT_PLUG_DETACH: */
default:
return DDI_FAILURE;
}
}
/**
* Info entry point, called by solaris kernel for obtaining driver info.
*
* @param pDip The module structure instance (do not use).
* @param enmCmd Information request type.
* @param pvArg Type specific argument.
* @param ppvResult Where to store the requested info.
*
* @returns corresponding solaris error code.
*/
LOCAL int VBoxNetFltSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppResult)
{
LogFlow((DEVICE_NAME ":VBoxNetFltSolarisGetInfo pDip=%p enmCmd=%d pArg=%p instance=%d\n", pDip, enmCmd, getminor((dev_t)pvArg)));
switch (enmCmd)
{
case DDI_INFO_DEVT2DEVINFO:
{
return DDI_SUCCESS;
}
case DDI_INFO_DEVT2INSTANCE:
{
return DDI_SUCCESS;
}
}
return DDI_FAILURE;
}
/**
* Create a solaris message block from the SG list.
*
* @param pThis The instance.
* @param pSG Pointer to the scatter-gather list.
*
* @returns Solaris message block.
*/
{
if (RT_UNLIKELY(!pMsg))
{
LogRel((DEVICE_NAME ":vboxNetFltSolarisMBlkFromSG failed to alloc %d bytes for mblk_t.\n", pSG->cbTotal));
return NULL;
}
/*
* Single buffer copy. Maybe later explore the
* need/possibility for using a mblk_t chain rather.
*/
{
{
}
}
return pMsg;
}
/**
* Calculate the number of segments required for this message block.
*
* @param pThis The instance
* @param pMsg Pointer to the data message.
*
* @returns Number of segments.
*/
{
unsigned cSegs = 0;
cSegs++;
#ifdef PADD_RUNT_FRAMES_FROM_HOST
cSegs++;
#endif
}
/**
* Initializes an SG list from the given message block.
*
* @param pThis The instance.
* @param pMsg Pointer to the data message.
The caller must ensure it's not a control message block.
* @param pSG Pointer to the SG.
* @param cSegs Number of segments in the SG.
* This should match the number in the message block exactly!
* @param fSrc The source of the message.
*
* @returns VBox status code.
*/
LOCAL int vboxNetFltSolarisMBlkToSG(PVBOXNETFLTINS pThis, mblk_t *pMsg, PINTNETSG pSG, unsigned cSegs, uint32_t fSrc)
{
LogFlow((DEVICE_NAME ":vboxNetFltSolarisMBlkToSG pThis=%p pMsg=%p pSG=%p cSegs=%d\n", pThis, pMsg, pSG, cSegs));
/*
* Convert the message block to segments. Works cbTotal and sets cSegsUsed.
*/
unsigned iSeg = 0;
while (pCur)
{
if (cbSeg)
{
iSeg++;
}
}
#ifdef PADD_RUNT_FRAMES_FROM_HOST
{
}
#endif
LogFlow((DEVICE_NAME ":vboxNetFltSolarisMBlkToSG iSeg=%d pSG->cbTotal=%d msgdsize=%d\n", iSeg, pSG->cbTotal, msgdsize(pMsg)));
return VINF_SUCCESS;
}
/**
* Simple packet dump, used for internal debugging.
*
* @param pMsg Pointer to the message to analyze and dump.
*/
{
{
{
LogRel((DEVICE_NAME ":ICMP D=%.6Rhxs S=%.6Rhxs T=%04x\n", pb, pb + 6, RT_BE2H_U16(*(uint16_t *)(pb + 12))));
{
{
LogRel((DEVICE_NAME ":UDP bootp ack D=%.6Rhxs S=%.6Rhxs UDP_CheckSum=%04x Computex=%04x\n", pb, pb + 6,
}
}
}
else
{
}
}
{
PVLANHEADER pVlanHdr = (PVLANHEADER)(pMsg->b_rptr + sizeof(RTNETETHERHDR) - sizeof(pEthHdr->EtherType));
LogRel((DEVICE_NAME ":VLAN Pcp=%u Cfi=%u Id=%u\n", VLAN_PRI(RT_BE2H_U16(pVlanHdr->Data)), VLAN_CFI(RT_BE2H_U16(pVlanHdr->Data)), VLAN_ID(RT_BE2H_U16(pVlanHdr->Data))));
}
{
}
{
}
{
}
else
{
LogRel((DEVICE_NAME ":Unknown EtherType=%x D=%.6Rhxs S=%.6Rhxs\n", RT_H2BE_U16(pEthHdr->EtherType), &pEthHdr->DstMac,
/* LogFlow((DEVICE_NAME ":%.*Rhxd\n", MBLKL(pMsg), pMsg->b_rptr)); */
}
}
/**
* Helper.
*/
{
}
/**
* Receive (rx) entry point.
*
* @param pvData Private data.
* @param hResource The resource handle.
* @param pMsg The packet.
* @param fLoopback Whether this is a loopback packet or not.
*/
LOCAL void vboxNetFltSolarisRecv(void *pvData, mac_resource_handle_t hResource, mblk_t *pMsg, boolean_t fLoopback)
{
LogFlow((DEVICE_NAME ":vboxNetFltSolarisRecv pvData=%p pMsg=%p fLoopback=%d cbData=%d\n", pvData, pMsg, fLoopback, pMsg ? MBLKL(pMsg) : 0));
/*
* Active? Retain the instance and increment the busy counter.
*/
return;
/*
* Route all received packets into the internal network.
*/
{
if (RT_SUCCESS(rc))
else
cFailed++;
}
if (RT_UNLIKELY(cFailed))
}
/**
* Report capabilities and MAC address to IntNet.
*
* @param pThis The instance.
* @remarks Retains the instance while doing it's job.
*/
{
if (!pThis->u.s.fReportedInfo)
{
{
pThis->pSwitchPort->pfnReportGsoCapabilities(pThis->pSwitchPort, 0, INTNETTRUNKDIR_WIRE | INTNETTRUNKDIR_HOST);
pThis->u.s.fReportedInfo = true;
}
}
}
/**
* Initialize a VNIC.
*
* @param pThis The instance.
* @param pVNIC Pointer to the VNIC.
*
* @returns Solaris error code (errno).
*/
{
/*
* Some paranoia.
*/
NULL, /* name of this client */
MAC_OPEN_FLAGS_USE_DATALINK_NAME | /* client name same as underlying NIC */
MAC_OPEN_FLAGS_MULTI_PRIMARY /* allow multiple primary unicasts */
);
{
/*
* Set the RX callback.
*/
NULL /* MAC address, use existing VNIC address */,
MAC_UNICAST_PRIMARY | /* Use Primary address of the VNIC */
MAC_UNICAST_NODUPCHECK, /* Don't fail for conflicting MAC/VLAN-id combinations */
0 /* VLAN-id */,
&Diag,
vboxNetFltSolarisRecv, /* RX callback */
pThis /* callback private data */
);
{
if (!pThis->u.s.fReportedInfo)
{
/*
* Obtain the MAC address of the underlying physical interface.
*/
{
}
else
{
LogRel((DEVICE_NAME ":vboxNetFltSolarisInitVNIC failed to get lower MAC handle for '%s'\n", pThis->szName));
}
}
if (!rc)
{
LogFlow((DEVICE_NAME ":vboxNetFltSolarisInitVNIC successfully initialized VNIC '%s'\n", pVNIC->szName));
return 0;
}
}
else
LogRel((DEVICE_NAME ":vboxNetFltSolarisInitVNIC failed to set RX callback. rc=%d Diag=%d\n", rc, Diag));
}
else
LogRel((DEVICE_NAME ":vboxNetFltSolarisInitVNIC failed to open mac client for '%s' rc=%d\n", pThis->szName, rc));
return RTErrConvertFromErrno(rc);
}
/**
* Allocate a VNIC structure.
*
* @returns An allocated VNIC structure or NULL in case of errors.
*/
{
if (RT_UNLIKELY(!pVNIC))
return NULL;
return pVNIC;
}
/**
* Frees an allocated VNIC.
*
* @param pVNIC Pointer to the VNIC.
*/
{
if (pVNIC)
}
/**
* Destroy a created VNIC if it was created by us, or just
* de-initializes the VNIC freeing up resources handles.
*
* @param pVNIC Pointer to the VNIC.
*/
{
if (pVNIC)
{
if (pVNIC->hPromiscuous)
{
}
{
{
}
}
if (pVNIC->hInterface)
{
}
{
}
}
}
/**
* Create a non-persistent VNIC over the given interface.
*
* @param pThis The VM connection instance.
* @param ppVNIC Where to store the created VNIC.
*
* @returns corresponding VBox error code.
*/
{
if (RT_UNLIKELY(!pVNIC))
return VERR_NO_MEMORY;
RTStrPrintf(pVNIC->szName, sizeof(pVNIC->szName), "%s%RU64", VBOXFLT_VNIC_NAME, pThis->u.s.uInstance);
/*
* Set a random MAC address for now. It will be changed to the VM interface's
* MAC address later, see vboxNetFltPortOsNotifyMacAddress().
*/
int MacSlot = 0;
int rc = vnic_create(pVNIC->szName, pThis->szName, &AddrType, &MacLen, GuestMac.au8, &MacSlot, 0 /* Mac-Prefix Length */, 0 /* VLAN-ID */,
if (!rc)
{
/*
* Now try opening the created VNIC.
*/
if (!rc)
{
{
LogFlow((DEVICE_NAME ":vboxNetFltSolarisCreateVNIC successfully created VNIC '%s' over '%s'\n", pVNIC->szName, pThis->szName));
return VINF_SUCCESS;
}
else
}
else
{
LogRel((DEVICE_NAME ":vboxNetFltSolarisCreateVNIC failed to open VNIC '%s' over '%s'. rc=%d\n", pVNIC->szName,
}
}
else
{
LogRel((DEVICE_NAME ":vboxNetFltSolarisCreateVNIC failed to create VNIC '%s' over '%s' rc=%d Diag=%d\n", pVNIC->szName,
}
return rc;
}
/* -=-=-=-=-=- Common Hooks -=-=-=-=-=- */
{
#if 0
if (fActive)
{
/*
* Activate promiscuous mode.
*/
if (!pThis->u.s.hPromiscuous)
{
int rc = mac_promisc_add(pThis->u.s.hClient, MAC_CLIENT_PROMISC_ALL, vboxNetFltSolarisRecv, pThis, &pThis->u.s.hPromiscuous,
if (rc)
LogRel((DEVICE_NAME ":vboxNetFltPortOsSetActive cannot enable promiscuous mode for '%s' rc=%d\n", pThis->szName, rc));
}
}
else
{
/*
* Deactivate promiscuous mode.
*/
if (pThis->u.s.hPromiscuous)
{
}
}
#endif
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
/*
* Destroy all managed VNICs. If a VNIC was passed to us, there
* will be only 1 item in the list, otherwise as many interfaces
* that were somehow not destroyed using DisconnectInterface() will be
* present.
*/
{
}
}
{
/*
*/
{
if (!rc)
{
/*
* This is NOT a VNIC. Just pretend success for now.
* We will create a VNIC per VM interface later, see vboxNetFltPortOsConnectInterface().
*/
return VINF_SUCCESS;
}
{
/*
* Obtain the data link ID for this VNIC, it's needed for modifying the MAC address among other things.
*/
{
/*
* Initialize the VNIC and add it to the list of managed VNICs.
*/
if (!rc)
{
return VINF_SUCCESS;
}
else
}
else
LogRel((DEVICE_NAME ":vboxNetFltOsInitInstance failed to get link id for '%s'. rc=%d\n", pThis->szName, rc));
}
else
}
else
LogRel((DEVICE_NAME ":vboxNetFltOsInitInstance failed to open link '%s'! rc=%d\n", pThis->szName, rc));
return RTErrConvertFromErrno(rc);
}
{
/*
* Init. the solaris specific data.
*/
pThis->u.s.fReportedInfo = false;
return VINF_SUCCESS;
}
{
/*
* @todo Think about this.
*/
return false;
}
{
/*
* Validate parameters.
*/
("Invalid pvIfData=%p magic=%#x (expected %#x)\n", pvIfData, pVNIC ? pVNIC->u32Magic : 0, VBOXNETFLTVNIC_MAGIC),
/*
* Xmit the packet down the appropriate VNIC interface.
*/
int rc = VINF_SUCCESS;
{
mac_tx_cookie_t pXmitCookie = mac_tx(pVNIC->hClient, pMsg, 0 /* Hint */, MAC_DROP_ON_NO_DESC, NULL /* return message */);
if (RT_LIKELY(!pXmitCookie))
return VINF_SUCCESS;
}
else
{
rc = VERR_NO_MEMORY;
}
return rc;
}
{
/*
* Validate parameters.
*/
("Invalid pvIfData=%p magic=%#x (expected %#x)\n", pvIfData, pVNIC ? pVNIC->u32Magic : 0, VBOXNETFLTVNIC_MAGIC));
/*
* Set the MAC address of the VNIC to the one used by the VM interface.
*/
int MacSlot = 0;
int rc = vnic_modify_addr(pVNIC->hLinkId, &AddrType, &MacLen, au8GuestMac, &MacSlot, 0 /* Mac-Prefix Length */, &Diag);
if (RT_UNLIKELY(rc))
}
{
int rc = VINF_SUCCESS;
/*
* If the underlying interface is not a VNIC, we need to create
* a VNIC per guest NIC.
*/
{
if (RT_SUCCESS(rc))
{
/*
*/
/*
* Add the created VNIC to the list of VNICs we manage.
*/
LogFlow((DEVICE_NAME ":vboxNetFltPortOsConnectInterface successfully created VNIC '%s'.\n", pVNIC->szName));
return VINF_SUCCESS;
}
else
}
else
{
{
LogFlow((DEVICE_NAME ":vboxNetFltPortOsConnectInterface set VNIC '%s' private data\n", pVNIC->szName));
}
else
{
return VERR_GENERAL_FAILURE;
}
}
return rc;
}
{
("Invalid pvIfData=%p magic=%#x (expected %#x)\n", pvIfData, pVNIC ? pVNIC->u32Magic : 0, VBOXNETFLTVNIC_MAGIC),
/*
* If the underlying interface is not a VNIC, we need to delete the created VNIC.
*/
{
/*
* Remove the VNIC from the list, destroy and free it.
*/
}
return VINF_SUCCESS;
}