NetworkAdapterImpl.cpp revision 626f8a507cce4b57fa90be1570ed10f2f7e2b048
/* $Id$ */
/** @file
* Implementation of INetworkAdaptor in VBoxSVC.
*/
/*
* Copyright (C) 2006-2010 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.
*/
#include "NetworkAdapterImpl.h"
#include "NATEngineImpl.h"
#include "AutoCaller.h"
#include "Logging.h"
#include "MachineImpl.h"
#include "GuestOSTypeImpl.h"
#include <VBox/settings.h>
#include "AutoStateDep.h"
// constructor / destructor
////////////////////////////////////////////////////////////////////////////////
{
}
{
}
{
return BaseFinalConstruct();
}
void NetworkAdapter::FinalRelease()
{
uninit();
}
// public initializer/uninitializer for internal purposes only
////////////////////////////////////////////////////////////////////////////////
/**
* Initializes the network adapter object.
*
* @param aParent Handle of the parent object.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
/* mPeer is left null */
m_fModified = false;
/* initialize data */
mData->mBandwidthLimit = 0;
/* default to Am79C973 */
/* generate the MAC address early to guarantee it is the same both after
* changing some other property (i.e. after mData.backup()) and after the
* subsequent mData.rollback(). */
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the network adapter object given another network adapter object
* (a kind of copy constructor). This object shares data with
* the object passed as an argument.
*
* @note This object must be destroyed before the original object
* it shares data with is destroyed.
*
* @note Locks @a aThat object for reading.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the guest object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*
* @note Locks @a aThat object for reading.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
/* mPeer is left null */
/* Confirm a successful initialization */
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void NetworkAdapter::uninit()
{
LogFlowThisFunc(("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
}
// INetworkAdapter properties
////////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
/* make sure the value is allowed */
switch (aAdapterType)
{
#ifdef VBOX_WITH_E1000
#endif
#ifdef VBOX_WITH_VIRTIO
#endif /* VBOX_WITH_VIRTIO */
break;
default:
tr("Invalid network adapter type '%d'"),
}
{
m_fModified = true;
// leave the lock before informing callbacks
/* Changing the network adapter type during runtime is not allowed,
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* Disabling the network adapter during runtime is not allowed
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
bool emitChangeEvent = false;
/*
* Are we supposed to generate a MAC?
*/
if (!aMACAddress || !*aMACAddress)
{
emitChangeEvent = true;
m_fModified = true;
// leave the lock before informing callbacks
}
else
{
{
/*
* Verify given MAC address
*/
int i = 0;
{
char c = *macAddressStr;
/* canonicalize hex digits to capital letters */
if (c >= 'a' && c <= 'f')
{
c &= 0xdf;
*macAddressStr = c;
}
/* we only accept capital letters */
if (((c < '0') || (c > '9')) &&
((c < 'A') || (c > 'F')))
/* the second digit must have even value for unicast addresses */
if ((i == 1) && (!!(c & 1) == (c >= '0' && c <= '9')))
i++;
}
/* we must have parsed exactly 12 characters */
if (i != 12)
{
emitChangeEvent = true;
m_fModified = true;
// leave the lock before informing callbacks
}
}
}
// we have left the lock in any case at this point
if (emitChangeEvent)
{
/* Changing the MAC via the Main API during runtime is not allowed,
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
}
return rc;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
if (!aHostInterface)
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* When changing the host adapter, adapt the CFGM logic to make this
* change immediately effect and to notify the guest that the network
* might have changed, therefore changeAdapter=TRUE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
* turned off */
{
tr("Empty or null internal network name is not valid"));
}
m_fModified = true;
// leave the lock before informing callbacks
/* When changing the internal network, adapt the CFGM logic to make this
* change immediately effect and to notify the guest that the network
* might have changed, therefore changeAdapter=TRUE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
if (!aNATNetwork)
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* Changing the NAT network isn't allowed during runtime, therefore
* no immediate replug in CFGM logic => changeAdapter=FALSE */
}
return S_OK;
}
{
#if defined(VBOX_WITH_VDE)
AutoCaller autoCaller(this);
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
#if defined(VBOX_WITH_VDE)
if (!aVDENetwork)
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
}
return S_OK;
#else
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* No change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* No change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(PromiscModePolicy)(NetworkAdapterPromiscModePolicy_T *aPromiscModePolicy)
{
AutoCaller autoCaller(this);
{
}
return hrc;
}
STDMETHODIMP NetworkAdapter::COMSETTER(PromiscModePolicy)(NetworkAdapterPromiscModePolicy_T aPromiscModePolicy)
{
switch (aPromiscModePolicy)
{
break;
default:
}
AutoCaller autoCaller(this);
{
{
m_fModified = true;
}
}
return hrc;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine doesn't need to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* No change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic changeAdapter=TRUE */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* No change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* No change in CFGM logic => changeAdapter=FALSE. */
}
return S_OK;
}
// INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
// Commented this for now as it resets the parameter mData->mNATNetwork
// which is essential while changing the Attachment dynamically.
//detach();
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
/* don't do anything if we're already host interface attached */
{
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
{
/* If changing the attachment failed then we can't assume that the
* previous attachment will attach correctly and thus return error
* along with detaching all attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
/* don't do anything if we're already internal network attached */
{
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mInternalNetwork
// which is essential while changing the Attachment dynamically.
//detach();
/* there must an internal network name */
{
LogRel (("Internal network name not defined, "
"setting to default \"intnet\"\n"));
}
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
/* don't do anything if we're already host interface attached */
{
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic and notify the guest => changeAdpater=TRUE. */
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
{
#if defined(VBOX_WITH_VDE)
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
/* don't do anything if we're already host interface attached */
{
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
/* leave the lock before informing callbacks */
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
#else /* !VBOX_WITH_VDE */
return E_NOTIMPL;
#endif
}
{
AutoCaller autoCaller(this);
/* the machine needs to be mutable */
{
detach();
m_fModified = true;
// leave the lock before informing callbacks
/* adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
}
return S_OK;
}
// public methods only for internal purposes
////////////////////////////////////////////////////////////////////////////////
/**
* Loads settings from the given adapter node.
* May be called once right after this object creation.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for writing.
*/
{
AutoCaller autoCaller(this);
/* Note: we assume that the default values for attributes of optional
* nodes are assigned in the Data::Data() constructor and don't do it
* here. It implies that this method may only be called after constructing
* a new BIOSSettings object while all its data fields are in the default
* values. Exceptions are fields whose creation time defaults don't match
* values that should be applied when these fields are not explicitly set
* in the settings file (for backwards compatibility reasons). This takes
* place when a setting of a newly created object must default to A while
* the same setting of an object loaded from the old settings file must
* default to B. */
/* MAC address (can be null) */
/* cable (required) */
/* line speed (defaults to 100 Mbps) */
/* tracing (defaults to false) */
/* boot priority (defaults to 0, i.e. lowest) */
/* Bandwidth limit in Mbps. */
{
rc = AttachToNAT();
break;
break;
break;
#if defined(VBOX_WITH_NETFLT)
#endif
break;
#if defined(VBOX_WITH_VDE)
rc = AttachToVDE();
break;
#endif
break;
}
if (data.fHasDisabledNAT)
// after loading settings, we are no longer different from the XML on disk
m_fModified = false;
return S_OK;
}
/**
* Saves settings to the given adapter node.
*
* Note that the given Adapter node is completely empty on input.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller(this);
{
break;
data.fHasDisabledNAT = 0;
mNATEngine->commit();
break;
break;
break;
break;
#if defined(VBOX_WITH_VDE)
break;
#endif
}
{
mNATEngine->commit();
}
// after saving settings, we are no longer different from the XML on disk
m_fModified = false;
return S_OK;
}
/**
* Returns true if any setter method has modified settings of this instance.
* @return
*/
bool NetworkAdapter::isModified() {
bool fChanged = m_fModified;
return fChanged;
}
/**
* @note Locks this object for writing.
*/
void NetworkAdapter::rollback()
{
/* sanity */
AutoCaller autoCaller(this);
}
/**
* @note Locks this object for writing, together with the peer object (also
* for writing) if there is one.
*/
void NetworkAdapter::commit()
{
/* sanity */
AutoCaller autoCaller(this);
/* sanity too */
/* lock both for writing since we modify both (mPeer is "master" so locked
* first) */
if (mData.isBackedUp())
{
if (mPeer)
{
/* attach new data to the peer and reshare it */
}
}
}
/**
* @note Locks this object for writing, together with the peer object
* represented by @a aThat (locked for reading).
*/
{
/* sanity */
AutoCaller autoCaller(this);
/* sanity too */
/* peer is not modified, lock it for reading (aThat is "master" so locked
* first) */
/* this will back up current data */
}
{
/* sanity */
AutoCaller autoCaller(this);
bool e1000enabled = false;
#ifdef VBOX_WITH_E1000
e1000enabled = true;
#endif // VBOX_WITH_E1000
/* Set default network adapter for this OS type */
if (defaultType == NetworkAdapterType_I82540EM ||
{
}
/* Enable and connect the first one adapter to the NAT */
{
mData->mCableConnected = true;
}
}
// private methods
////////////////////////////////////////////////////////////////////////////////
/**
* Worker routine for detach handling. No locking, no notifications.
* @note Must be called from under the object's write lock.
*/
void NetworkAdapter::detach()
{
switch (mData->mAttachmentType)
{
{
/* nothing to do here */
break;
}
{
break;
}
{
/* reset handle and device name */
break;
}
{
break;
}
{
#if defined(VBOX_WITH_NETFLT)
/* reset handle and device name */
#endif
break;
}
}
}
/**
* Generates a new unique MAC address based on our vendor ID and
* parts of a GUID.
*
* @note Must be called from under the object's write lock or within the init
* span.
*/
void NetworkAdapter::generateMACAddress()
{
/*
* Our strategy is as follows: the first three bytes are our fixed
* vendor ID (080027). The remaining 3 bytes will be taken from the
* start of a GUID. This is a fairly safe algorithm.
*/
char strMAC[13];
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */