NetworkAdapterImpl.cpp revision fb7940000e0c1e00d24d63bd6bffa8301c27e1bd
/* $Id$ */
/** @file
* Implementation of INetworkAdapter in VBoxSVC.
*/
/*
* Copyright (C) 2006-2014 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 "HostImpl.h"
#include "SystemPropertiesImpl.h"
#include "VirtualBoxImpl.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 */
/* 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.
*
* @param aReshare
* When false, the original object will remain a data owner.
* Otherwise, data ownership will be transferred from the original
* object to this one.
*
* @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);
/* sanity */
if (aReshare)
{
}
else
{
}
/* 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;
}
// wrapped INetworkAdapter properties
////////////////////////////////////////////////////////////////////////////////
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
/*
* Are we supposed to generate a MAC?
*/
if (aMACAddress.isEmpty())
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)
}
}
return rc;
}
{
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* Changing the MAC via the Main API during runtime is not allowed,
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
}
return rc;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
/* there must an internal network name */
{
Log(("Internal network name not defined, setting to default \"intnet\"\n"));
}
/* there must a NAT network name */
{
Log(("NAT network name not defined, setting to default \"NatNetwork\"\n"));
}
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
}
return S_OK;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
* turned off */
if (aBridgedInterface.isEmpty()
{
tr("Empty or null bridged interface name is not valid"));
}
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;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
* turned off */
if ( aHostOnlyInterface.isEmpty()
{
tr("Empty or null host only interface name is not valid"));
}
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;
}
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
* turned off */
if (aNATNetwork.isEmpty()
tr("Empty or null NAT network name is not valid"));
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;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
/* leave the lock before informing callbacks */
}
return S_OK;
}
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
switch (aPromiscModePolicy)
{
break;
default:
}
AutoCaller autoCaller(this);
{
{
m_fModified = true;
}
}
return hrc;
}
{
return S_OK;
}
{
/* the machine needs to be mutable */
{
m_fModified = true;
// leave the lock before informing callbacks
/* Adapt the CFGM logic changeAdapter=TRUE */
}
return S_OK;
}
{
return S_OK;
}
{
/* 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;
}
{
return S_OK;
}
{
return S_OK;
}
{
/* 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;
}
// wrapped INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
{
aValue = "";
return S_OK;
}
{
LogFlowThisFunc(("\n"));
/* The machine needs to be mutable. */
/* Generic properties processing.
* Look up the old value first; if nothing's changed then do nothing.
*/
if (strOldValue != aValue)
{
else
/* leave the lock before informing callbacks */
/* Avoid deadlock when the event triggers a call to a method of this
* interface. */
}
return S_OK;
}
{
/// @todo make use of aNames according to the documentation
size_t i = 0;
++it, ++i)
{
}
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 group */
{
group->i_reference();
}
// leave the lock before setting attachment type
// 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);
mNATEngine->i_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::i_isModified() {
bool fChanged = m_fModified;
return fChanged;
}
/**
* @note Locks this object for writing.
*/
void NetworkAdapter::i_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::i_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;
}
}
{
return mPeer;
}
// private methods
////////////////////////////////////////////////////////////////////////////////
/**
* 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::i_generateMACAddress()
{
}
{
{
* of the group was checked when it was attached. */
}
return hrc;
}
{
/* the machine needs to be mutable */
if (aBandwidthGroup)
{
if (!strBwGroup.isEmpty())
{
of the group was checked when it was attached. */
}
m_fModified = true;
// leave the lock before informing callbacks
/* TODO: changeAdapter=???. */
}
return S_OK;
}
{
{
HRESULT hrc = mParent->i_getBandwidthGroup(mData->mBandwidthGroup, pOldBwGroup, false /* fSetError */);
the group was checked when it was attached. */
}
if (!pOldBwGroup.isNull())
{
pOldBwGroup->i_release();
}
if (aBwGroup)
{
aBwGroup->i_reference();
}
}
{
return hrc;
if (state == MachineState_Running)
{
if (natCount == -1)
return E_INVALIDARG; /* no such network */
}
return S_OK;
}
{
return hrc;
if (state == MachineState_Running)
{
if (natCount == -1)
return E_INVALIDARG; /* not found */
}
return S_OK;
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */