VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/NetworkAdapterImpl.cpp@ 38251

Last change on this file since 38251 was 37927, checked in by vboxsync, 13 years ago

Main: less LogRel noise

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.9 KB
Line 
1/* $Id: NetworkAdapterImpl.cpp 37927 2011-07-13 15:48:41Z vboxsync $ */
2/** @file
3 * Implementation of INetworkAdaptor in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
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
18#include "NetworkAdapterImpl.h"
19#include "NATEngineImpl.h"
20#include "AutoCaller.h"
21#include "Logging.h"
22#include "MachineImpl.h"
23#include "GuestOSTypeImpl.h"
24#include "HostImpl.h"
25
26#include <iprt/string.h>
27#include <iprt/cpp/utils.h>
28
29#include <VBox/err.h>
30#include <VBox/settings.h>
31
32#include "AutoStateDep.h"
33
34// constructor / destructor
35////////////////////////////////////////////////////////////////////////////////
36
37NetworkAdapter::NetworkAdapter()
38 : mParent(NULL)
39{
40}
41
42NetworkAdapter::~NetworkAdapter()
43{
44}
45
46HRESULT NetworkAdapter::FinalConstruct()
47{
48
49 return BaseFinalConstruct();
50}
51
52void NetworkAdapter::FinalRelease()
53{
54 uninit();
55 BaseFinalRelease();
56}
57
58// public initializer/uninitializer for internal purposes only
59////////////////////////////////////////////////////////////////////////////////
60
61/**
62 * Initializes the network adapter object.
63 *
64 * @param aParent Handle of the parent object.
65 */
66HRESULT NetworkAdapter::init(Machine *aParent, ULONG aSlot)
67{
68 LogFlowThisFunc(("aParent=%p, aSlot=%d\n", aParent, aSlot));
69
70 ComAssertRet(aParent, E_INVALIDARG);
71 ComAssertRet(aSlot < SchemaDefs::NetworkAdapterCount, E_INVALIDARG);
72
73 /* Enclose the state transition NotReady->InInit->Ready */
74 AutoInitSpan autoInitSpan(this);
75 AssertReturn(autoInitSpan.isOk(), E_FAIL);
76
77 unconst(mParent) = aParent;
78 unconst(mNATEngine).createObject();
79 mNATEngine->init(aParent, this);
80 /* mPeer is left null */
81
82 m_fModified = false;
83
84 mData.allocate();
85
86 /* initialize data */
87 mData->mSlot = aSlot;
88
89 /* default to Am79C973 */
90 mData->mAdapterType = NetworkAdapterType_Am79C973;
91
92 /* generate the MAC address early to guarantee it is the same both after
93 * changing some other property (i.e. after mData.backup()) and after the
94 * subsequent mData.rollback(). */
95 generateMACAddress();
96
97 /* Confirm a successful initialization */
98 autoInitSpan.setSucceeded();
99
100 return S_OK;
101}
102
103/**
104 * Initializes the network adapter object given another network adapter object
105 * (a kind of copy constructor). This object shares data with
106 * the object passed as an argument.
107 *
108 * @note This object must be destroyed before the original object
109 * it shares data with is destroyed.
110 *
111 * @note Locks @a aThat object for reading.
112 */
113HRESULT NetworkAdapter::init(Machine *aParent, NetworkAdapter *aThat)
114{
115 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
116
117 ComAssertRet(aParent && aThat, E_INVALIDARG);
118
119 /* Enclose the state transition NotReady->InInit->Ready */
120 AutoInitSpan autoInitSpan(this);
121 AssertReturn(autoInitSpan.isOk(), E_FAIL);
122
123 unconst(mParent) = aParent;
124 unconst(mPeer) = aThat;
125 unconst(mNATEngine).createObject();
126 mNATEngine->init(aParent, this, aThat->mNATEngine);
127
128 AutoCaller thatCaller (aThat);
129 AssertComRCReturnRC(thatCaller.rc());
130
131 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
132 mData.share (aThat->mData);
133
134 /* Confirm a successful initialization */
135 autoInitSpan.setSucceeded();
136
137 return S_OK;
138}
139
140/**
141 * Initializes the guest object given another guest object
142 * (a kind of copy constructor). This object makes a private copy of data
143 * of the original object passed as an argument.
144 *
145 * @note Locks @a aThat object for reading.
146 */
147HRESULT NetworkAdapter::initCopy(Machine *aParent, NetworkAdapter *aThat)
148{
149 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
150
151 ComAssertRet(aParent && aThat, E_INVALIDARG);
152
153 /* Enclose the state transition NotReady->InInit->Ready */
154 AutoInitSpan autoInitSpan(this);
155 AssertReturn(autoInitSpan.isOk(), E_FAIL);
156
157 unconst(mParent) = aParent;
158 /* mPeer is left null */
159
160 unconst(mNATEngine).createObject();
161 mNATEngine->initCopy(aParent, this, aThat->mNATEngine);
162
163 AutoCaller thatCaller (aThat);
164 AssertComRCReturnRC(thatCaller.rc());
165
166 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
167 mData.attachCopy (aThat->mData);
168
169 /* Confirm a successful initialization */
170 autoInitSpan.setSucceeded();
171
172 return S_OK;
173}
174
175/**
176 * Uninitializes the instance and sets the ready flag to FALSE.
177 * Called either from FinalRelease() or by the parent when it gets destroyed.
178 */
179void NetworkAdapter::uninit()
180{
181 LogFlowThisFunc(("\n"));
182
183 /* Enclose the state transition Ready->InUninit->NotReady */
184 AutoUninitSpan autoUninitSpan(this);
185 if (autoUninitSpan.uninitDone())
186 return;
187
188 mData.free();
189
190 unconst(mNATEngine).setNull();
191 unconst(mPeer) = NULL;
192 unconst(mParent) = NULL;
193}
194
195// INetworkAdapter properties
196////////////////////////////////////////////////////////////////////////////////
197
198STDMETHODIMP NetworkAdapter::COMGETTER(AdapterType) (NetworkAdapterType_T *aAdapterType)
199{
200 CheckComArgOutPointerValid(aAdapterType);
201
202 AutoCaller autoCaller(this);
203 if (FAILED(autoCaller.rc())) return autoCaller.rc();
204
205 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
206
207 *aAdapterType = mData->mAdapterType;
208
209 return S_OK;
210}
211
212STDMETHODIMP NetworkAdapter::COMSETTER(AdapterType) (NetworkAdapterType_T aAdapterType)
213{
214 AutoCaller autoCaller(this);
215 if (FAILED(autoCaller.rc())) return autoCaller.rc();
216
217 /* the machine needs to be mutable */
218 AutoMutableStateDependency adep(mParent);
219 if (FAILED(adep.rc())) return adep.rc();
220
221 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
222
223 /* make sure the value is allowed */
224 switch (aAdapterType)
225 {
226 case NetworkAdapterType_Am79C970A:
227 case NetworkAdapterType_Am79C973:
228#ifdef VBOX_WITH_E1000
229 case NetworkAdapterType_I82540EM:
230 case NetworkAdapterType_I82543GC:
231 case NetworkAdapterType_I82545EM:
232#endif
233#ifdef VBOX_WITH_VIRTIO
234 case NetworkAdapterType_Virtio:
235#endif /* VBOX_WITH_VIRTIO */
236 break;
237 default:
238 return setError(E_FAIL,
239 tr("Invalid network adapter type '%d'"),
240 aAdapterType);
241 }
242
243 if (mData->mAdapterType != aAdapterType)
244 {
245 mData.backup();
246 mData->mAdapterType = aAdapterType;
247
248 m_fModified = true;
249 // leave the lock before informing callbacks
250 alock.release();
251
252 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
253 mParent->setModified(Machine::IsModified_NetworkAdapters);
254 mlock.release();
255
256 /* Changing the network adapter type during runtime is not allowed,
257 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
258 mParent->onNetworkAdapterChange(this, FALSE);
259 }
260
261 return S_OK;
262}
263
264STDMETHODIMP NetworkAdapter::COMGETTER(Slot) (ULONG *aSlot)
265{
266 CheckComArgOutPointerValid(aSlot);
267
268 AutoCaller autoCaller(this);
269 if (FAILED(autoCaller.rc())) return autoCaller.rc();
270
271 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
272
273 *aSlot = mData->mSlot;
274
275 return S_OK;
276}
277
278STDMETHODIMP NetworkAdapter::COMGETTER(Enabled) (BOOL *aEnabled)
279{
280 CheckComArgOutPointerValid(aEnabled);
281
282 AutoCaller autoCaller(this);
283 if (FAILED(autoCaller.rc())) return autoCaller.rc();
284
285 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
286
287 *aEnabled = mData->mEnabled;
288
289 return S_OK;
290}
291
292STDMETHODIMP NetworkAdapter::COMSETTER(Enabled) (BOOL aEnabled)
293{
294 AutoCaller autoCaller(this);
295 if (FAILED(autoCaller.rc())) return autoCaller.rc();
296
297 /* the machine needs to be mutable */
298 AutoMutableStateDependency adep(mParent);
299 if (FAILED(adep.rc())) return adep.rc();
300
301 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
302
303 if (mData->mEnabled != aEnabled)
304 {
305 mData.backup();
306 mData->mEnabled = aEnabled;
307
308 m_fModified = true;
309 // leave the lock before informing callbacks
310 alock.release();
311
312 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
313 mParent->setModified(Machine::IsModified_NetworkAdapters);
314 mlock.release();
315
316 /* Disabling the network adapter during runtime is not allowed
317 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
318 mParent->onNetworkAdapterChange(this, FALSE);
319 }
320
321 return S_OK;
322}
323
324STDMETHODIMP NetworkAdapter::COMGETTER(MACAddress)(BSTR *aMACAddress)
325{
326 CheckComArgOutPointerValid(aMACAddress);
327
328 AutoCaller autoCaller(this);
329 if (FAILED(autoCaller.rc())) return autoCaller.rc();
330
331 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
332
333 ComAssertRet(!mData->mMACAddress.isEmpty(), E_FAIL);
334
335 mData->mMACAddress.cloneTo(aMACAddress);
336
337 return S_OK;
338}
339
340HRESULT NetworkAdapter::updateMacAddress(Utf8Str aMACAddress)
341{
342 HRESULT rc = S_OK;
343
344 /*
345 * Are we supposed to generate a MAC?
346 */
347 if (aMACAddress.isEmpty())
348 generateMACAddress();
349 else
350 {
351 if (mData->mMACAddress != aMACAddress)
352 {
353 /*
354 * Verify given MAC address
355 */
356 char *macAddressStr = aMACAddress.mutableRaw();
357 int i = 0;
358 while ((i < 13) && macAddressStr && *macAddressStr && (rc == S_OK))
359 {
360 char c = *macAddressStr;
361 /* canonicalize hex digits to capital letters */
362 if (c >= 'a' && c <= 'f')
363 {
364 /** @todo the runtime lacks an ascii lower/upper conv */
365 c &= 0xdf;
366 *macAddressStr = c;
367 }
368 /* we only accept capital letters */
369 if (((c < '0') || (c > '9')) &&
370 ((c < 'A') || (c > 'F')))
371 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
372 /* the second digit must have even value for unicast addresses */
373 if ((i == 1) && (!!(c & 1) == (c >= '0' && c <= '9')))
374 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
375
376 macAddressStr++;
377 i++;
378 }
379 /* we must have parsed exactly 12 characters */
380 if (i != 12)
381 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
382
383 if (SUCCEEDED(rc))
384 mData->mMACAddress = aMACAddress;
385 }
386 }
387
388 return rc;
389}
390
391STDMETHODIMP NetworkAdapter::COMSETTER(MACAddress)(IN_BSTR aMACAddress)
392{
393 AutoCaller autoCaller(this);
394 if (FAILED(autoCaller.rc())) return autoCaller.rc();
395
396 /* the machine needs to be mutable */
397 AutoMutableStateDependency adep(mParent);
398 if (FAILED(adep.rc())) return adep.rc();
399
400
401 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
402 mData.backup();
403
404 HRESULT rc = updateMacAddress(aMACAddress);
405 if (SUCCEEDED(rc))
406 {
407 m_fModified = true;
408 // leave the lock before informing callbacks
409 alock.release();
410
411 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
412 mParent->setModified(Machine::IsModified_NetworkAdapters);
413 mlock.release();
414
415 /* Changing the MAC via the Main API during runtime is not allowed,
416 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
417 mParent->onNetworkAdapterChange(this, FALSE);
418 }
419
420 return rc;
421}
422
423STDMETHODIMP NetworkAdapter::COMGETTER(AttachmentType)(
424 NetworkAttachmentType_T *aAttachmentType)
425{
426 CheckComArgOutPointerValid(aAttachmentType);
427
428 AutoCaller autoCaller(this);
429 if (FAILED(autoCaller.rc())) return autoCaller.rc();
430
431 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
432
433 *aAttachmentType = mData->mAttachmentType;
434
435 return S_OK;
436}
437
438STDMETHODIMP NetworkAdapter::COMSETTER(AttachmentType)(
439 NetworkAttachmentType_T aAttachmentType)
440{
441 AutoCaller autoCaller(this);
442 if (FAILED(autoCaller.rc())) return autoCaller.rc();
443
444 /* the machine needs to be mutable */
445 AutoMutableStateDependency adep(mParent);
446 if (FAILED(adep.rc())) return adep.rc();
447
448 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
449
450 if (mData->mAttachmentType != aAttachmentType)
451 {
452 mData.backup();
453
454 /* there must an internal network name */
455 if (mData->mInternalNetwork.isEmpty())
456 {
457 Log(("Internal network name not defined, setting to default \"intnet\"\n"));
458 mData->mInternalNetwork = "intnet";
459 }
460
461 mData->mAttachmentType = aAttachmentType;
462
463 m_fModified = true;
464 // leave the lock before informing callbacks
465 alock.release();
466
467 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
468 mParent->setModified(Machine::IsModified_NetworkAdapters);
469 mlock.release();
470
471 /* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
472 mParent->onNetworkAdapterChange(this, TRUE);
473 }
474
475 return S_OK;
476}
477
478STDMETHODIMP NetworkAdapter::COMGETTER(BridgedInterface)(BSTR *aBridgedInterface)
479{
480 CheckComArgOutPointerValid(aBridgedInterface);
481
482 AutoCaller autoCaller(this);
483 if (FAILED(autoCaller.rc())) return autoCaller.rc();
484
485 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
486
487 mData->mBridgedInterface.cloneTo(aBridgedInterface);
488
489 return S_OK;
490}
491
492STDMETHODIMP NetworkAdapter::COMSETTER(BridgedInterface)(IN_BSTR aBridgedInterface)
493{
494 Bstr bstrEmpty("");
495 if (!aBridgedInterface)
496 aBridgedInterface = bstrEmpty.raw();
497
498 AutoCaller autoCaller(this);
499 if (FAILED(autoCaller.rc())) return autoCaller.rc();
500
501 /* the machine needs to be mutable */
502 AutoMutableStateDependency adep(mParent);
503 if (FAILED(adep.rc())) return adep.rc();
504
505 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
506
507 if (mData->mBridgedInterface != aBridgedInterface)
508 {
509 mData.backup();
510 mData->mBridgedInterface = aBridgedInterface;
511
512 m_fModified = true;
513 // leave the lock before informing callbacks
514 alock.release();
515
516 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
517 mParent->setModified(Machine::IsModified_NetworkAdapters);
518 mlock.release();
519
520 /* When changing the host adapter, adapt the CFGM logic to make this
521 * change immediately effect and to notify the guest that the network
522 * might have changed, therefore changeAdapter=TRUE. */
523 mParent->onNetworkAdapterChange(this, TRUE);
524 }
525
526 return S_OK;
527}
528
529STDMETHODIMP NetworkAdapter::COMGETTER(HostOnlyInterface)(BSTR *aHostOnlyInterface)
530{
531 CheckComArgOutPointerValid(aHostOnlyInterface);
532
533 AutoCaller autoCaller(this);
534 if (FAILED(autoCaller.rc())) return autoCaller.rc();
535
536 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
537
538 mData->mHostOnlyInterface.cloneTo(aHostOnlyInterface);
539
540 return S_OK;
541}
542
543STDMETHODIMP NetworkAdapter::COMSETTER(HostOnlyInterface)(IN_BSTR aHostOnlyInterface)
544{
545 Bstr bstrEmpty("");
546 if (!aHostOnlyInterface)
547 aHostOnlyInterface = bstrEmpty.raw();
548
549 AutoCaller autoCaller(this);
550 if (FAILED(autoCaller.rc())) return autoCaller.rc();
551
552 /* the machine needs to be mutable */
553 AutoMutableStateDependency adep(mParent);
554 if (FAILED(adep.rc())) return adep.rc();
555
556 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
557
558 if (mData->mHostOnlyInterface != aHostOnlyInterface)
559 {
560 mData.backup();
561 mData->mHostOnlyInterface = aHostOnlyInterface;
562
563 m_fModified = true;
564 // leave the lock before informing callbacks
565 alock.release();
566
567 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
568 mParent->setModified(Machine::IsModified_NetworkAdapters);
569 mlock.release();
570
571 /* When changing the host adapter, adapt the CFGM logic to make this
572 * change immediately effect and to notify the guest that the network
573 * might have changed, therefore changeAdapter=TRUE. */
574 mParent->onNetworkAdapterChange(this, TRUE);
575 }
576
577 return S_OK;
578}
579
580STDMETHODIMP NetworkAdapter::COMGETTER(InternalNetwork) (BSTR *aInternalNetwork)
581{
582 CheckComArgOutPointerValid(aInternalNetwork);
583
584 AutoCaller autoCaller(this);
585 if (FAILED(autoCaller.rc())) return autoCaller.rc();
586
587 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
588
589 mData->mInternalNetwork.cloneTo(aInternalNetwork);
590
591 return S_OK;
592}
593
594STDMETHODIMP NetworkAdapter::COMSETTER(InternalNetwork) (IN_BSTR aInternalNetwork)
595{
596 AutoCaller autoCaller(this);
597 if (FAILED(autoCaller.rc())) return autoCaller.rc();
598
599 /* the machine needs to be mutable */
600 AutoMutableStateDependency adep(mParent);
601 if (FAILED(adep.rc())) return adep.rc();
602
603 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
604
605 if (mData->mInternalNetwork != aInternalNetwork)
606 {
607 /* if an empty/null string is to be set, internal networking must be
608 * turned off */
609 if ( (aInternalNetwork == NULL || *aInternalNetwork == '\0')
610 && mData->mAttachmentType == NetworkAttachmentType_Internal)
611 {
612 return setError(E_FAIL,
613 tr("Empty or null internal network name is not valid"));
614 }
615
616 mData.backup();
617 mData->mInternalNetwork = aInternalNetwork;
618
619 m_fModified = true;
620 // leave the lock before informing callbacks
621 alock.release();
622
623 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
624 mParent->setModified(Machine::IsModified_NetworkAdapters);
625 mlock.release();
626
627 /* When changing the internal network, adapt the CFGM logic to make this
628 * change immediately effect and to notify the guest that the network
629 * might have changed, therefore changeAdapter=TRUE. */
630 mParent->onNetworkAdapterChange(this, TRUE);
631 }
632
633 return S_OK;
634}
635
636STDMETHODIMP NetworkAdapter::COMGETTER(NATNetwork) (BSTR *aNATNetwork)
637{
638 CheckComArgOutPointerValid(aNATNetwork);
639
640 AutoCaller autoCaller(this);
641 if (FAILED(autoCaller.rc())) return autoCaller.rc();
642
643 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
644
645 mData->mNATNetwork.cloneTo(aNATNetwork);
646
647 return S_OK;
648}
649
650STDMETHODIMP NetworkAdapter::COMSETTER(NATNetwork) (IN_BSTR aNATNetwork)
651{
652 Bstr bstrEmpty("");
653 if (!aNATNetwork)
654 aNATNetwork = bstrEmpty.raw();
655
656 AutoCaller autoCaller(this);
657 if (FAILED(autoCaller.rc())) return autoCaller.rc();
658
659 /* the machine needs to be mutable */
660 AutoMutableStateDependency adep(mParent);
661 if (FAILED(adep.rc())) return adep.rc();
662
663 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
664
665 if (mData->mNATNetwork != aNATNetwork)
666 {
667 mData.backup();
668 mData->mNATNetwork = aNATNetwork;
669
670 m_fModified = true;
671 // leave the lock before informing callbacks
672 alock.release();
673
674 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
675 mParent->setModified(Machine::IsModified_NetworkAdapters);
676 mlock.release();
677
678 /* Changing the NAT network isn't allowed during runtime, therefore
679 * no immediate replug in CFGM logic => changeAdapter=FALSE */
680 mParent->onNetworkAdapterChange(this, FALSE);
681 }
682
683 return S_OK;
684}
685
686STDMETHODIMP NetworkAdapter::COMGETTER(GenericDriver)(BSTR *aGenericDriver)
687{
688 CheckComArgOutPointerValid(aGenericDriver);
689
690 AutoCaller autoCaller(this);
691 if (FAILED(autoCaller.rc())) return autoCaller.rc();
692
693 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
694
695 mData->mGenericDriver.cloneTo(aGenericDriver);
696
697 return S_OK;
698}
699
700STDMETHODIMP NetworkAdapter::COMSETTER(GenericDriver)(IN_BSTR aGenericDriver)
701{
702 Bstr bstrEmpty("");
703 if (!aGenericDriver)
704 aGenericDriver = bstrEmpty.raw();
705
706 AutoCaller autoCaller(this);
707 if (FAILED(autoCaller.rc())) return autoCaller.rc();
708
709 /* the machine needs to be mutable */
710 AutoMutableStateDependency adep(mParent);
711 if (FAILED(adep.rc())) return adep.rc();
712
713 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
714
715 if (mData->mGenericDriver != aGenericDriver)
716 {
717 mData.backup();
718 mData->mGenericDriver = aGenericDriver;
719
720 /* leave the lock before informing callbacks */
721 alock.release();
722
723 mParent->onNetworkAdapterChange(this, FALSE);
724 }
725
726 return S_OK;
727}
728
729STDMETHODIMP NetworkAdapter::COMGETTER(CableConnected) (BOOL *aConnected)
730{
731 CheckComArgOutPointerValid(aConnected);
732
733 AutoCaller autoCaller(this);
734 if (FAILED(autoCaller.rc())) return autoCaller.rc();
735
736 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
737
738 *aConnected = mData->mCableConnected;
739
740 return S_OK;
741}
742
743STDMETHODIMP NetworkAdapter::COMSETTER(CableConnected) (BOOL aConnected)
744{
745 AutoCaller autoCaller(this);
746 if (FAILED(autoCaller.rc())) return autoCaller.rc();
747
748 /* the machine needs to be mutable */
749 AutoMutableStateDependency adep(mParent);
750 if (FAILED(adep.rc())) return adep.rc();
751
752 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
753
754 if (aConnected != mData->mCableConnected)
755 {
756 mData.backup();
757 mData->mCableConnected = aConnected;
758
759 m_fModified = true;
760 // leave the lock before informing callbacks
761 alock.release();
762
763 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
764 mParent->setModified(Machine::IsModified_NetworkAdapters);
765 mlock.release();
766
767 /* No change in CFGM logic => changeAdapter=FALSE. */
768 mParent->onNetworkAdapterChange(this, FALSE);
769 }
770
771 return S_OK;
772}
773
774STDMETHODIMP NetworkAdapter::COMGETTER(LineSpeed) (ULONG *aSpeed)
775{
776 CheckComArgOutPointerValid(aSpeed);
777
778 AutoCaller autoCaller(this);
779 if (FAILED(autoCaller.rc())) return autoCaller.rc();
780
781 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
782
783 *aSpeed = mData->mLineSpeed;
784
785 return S_OK;
786}
787
788STDMETHODIMP NetworkAdapter::COMSETTER(LineSpeed) (ULONG aSpeed)
789{
790 AutoCaller autoCaller(this);
791 if (FAILED(autoCaller.rc())) return autoCaller.rc();
792
793 /* the machine needs to be mutable */
794 AutoMutableStateDependency adep(mParent);
795 if (FAILED(adep.rc())) return adep.rc();
796
797 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
798
799 if (aSpeed != mData->mLineSpeed)
800 {
801 mData.backup();
802 mData->mLineSpeed = aSpeed;
803
804 m_fModified = true;
805 // leave the lock before informing callbacks
806 alock.release();
807
808 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
809 mParent->setModified(Machine::IsModified_NetworkAdapters);
810 mlock.release();
811
812 /* No change in CFGM logic => changeAdapter=FALSE. */
813 mParent->onNetworkAdapterChange(this, FALSE);
814 }
815
816 return S_OK;
817}
818
819
820STDMETHODIMP NetworkAdapter::COMGETTER(PromiscModePolicy)(NetworkAdapterPromiscModePolicy_T *aPromiscModePolicy)
821{
822 CheckComArgOutPointerValid(aPromiscModePolicy);
823
824 AutoCaller autoCaller(this);
825 HRESULT hrc = autoCaller.rc();
826 if (SUCCEEDED(hrc))
827 {
828 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
829 *aPromiscModePolicy = mData->mPromiscModePolicy;
830 }
831 return hrc;
832}
833
834STDMETHODIMP NetworkAdapter::COMSETTER(PromiscModePolicy)(NetworkAdapterPromiscModePolicy_T aPromiscModePolicy)
835{
836 switch (aPromiscModePolicy)
837 {
838 case NetworkAdapterPromiscModePolicy_Deny:
839 case NetworkAdapterPromiscModePolicy_AllowNetwork:
840 case NetworkAdapterPromiscModePolicy_AllowAll:
841 break;
842 default:
843 return setError(E_INVALIDARG, tr("Invalid promiscuous mode policy (%d)"), aPromiscModePolicy);
844 }
845
846 AutoCaller autoCaller(this);
847 HRESULT hrc = autoCaller.rc();
848
849 if (SUCCEEDED(hrc))
850 {
851 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
852 if (aPromiscModePolicy != mData->mPromiscModePolicy)
853 {
854 mData.backup();
855 mData->mPromiscModePolicy = aPromiscModePolicy;
856 m_fModified = true;
857
858 alock.release();
859 mParent->setModifiedLock(Machine::IsModified_NetworkAdapters);
860 mParent->onNetworkAdapterChange(this, TRUE);
861 }
862 }
863
864 return hrc;
865}
866
867STDMETHODIMP NetworkAdapter::COMGETTER(TraceEnabled) (BOOL *aEnabled)
868{
869 CheckComArgOutPointerValid(aEnabled);
870
871 AutoCaller autoCaller(this);
872 if (FAILED(autoCaller.rc())) return autoCaller.rc();
873
874 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
875
876 *aEnabled = mData->mTraceEnabled;
877 return S_OK;
878}
879
880STDMETHODIMP NetworkAdapter::COMSETTER(TraceEnabled) (BOOL aEnabled)
881{
882 AutoCaller autoCaller(this);
883 if (FAILED(autoCaller.rc())) return autoCaller.rc();
884
885 /* the machine needs to be mutable */
886 AutoMutableStateDependency adep(mParent);
887 if (FAILED(adep.rc())) return adep.rc();
888
889 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
890
891 if (aEnabled != mData->mTraceEnabled)
892 {
893 mData.backup();
894 mData->mTraceEnabled = aEnabled;
895
896 m_fModified = true;
897 // leave the lock before informing callbacks
898 alock.release();
899
900 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
901 mParent->setModified(Machine::IsModified_NetworkAdapters);
902 mlock.release();
903
904 /* Adapt the CFGM logic changeAdapter=TRUE */
905 mParent->onNetworkAdapterChange(this, TRUE);
906 }
907
908 return S_OK;
909}
910
911STDMETHODIMP NetworkAdapter::COMGETTER(TraceFile) (BSTR *aTraceFile)
912{
913 CheckComArgOutPointerValid(aTraceFile);
914
915 AutoCaller autoCaller(this);
916 if (FAILED(autoCaller.rc())) return autoCaller.rc();
917
918 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
919
920 mData->mTraceFile.cloneTo(aTraceFile);
921
922 return S_OK;
923}
924
925STDMETHODIMP NetworkAdapter::COMSETTER(TraceFile) (IN_BSTR aTraceFile)
926{
927 AutoCaller autoCaller(this);
928 if (FAILED(autoCaller.rc())) return autoCaller.rc();
929
930 /* the machine needs to be mutable */
931 AutoMutableStateDependency adep(mParent);
932 if (FAILED(adep.rc())) return adep.rc();
933
934 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
935
936 if (mData->mTraceFile != aTraceFile)
937 {
938 mData.backup();
939 mData->mTraceFile = aTraceFile;
940
941 m_fModified = true;
942 // leave the lock before informing callbacks
943 alock.release();
944
945 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
946 mParent->setModified(Machine::IsModified_NetworkAdapters);
947 mlock.release();
948
949 /* No change in CFGM logic => changeAdapter=FALSE. */
950 mParent->onNetworkAdapterChange(this, FALSE);
951 }
952
953 return S_OK;
954}
955
956STDMETHODIMP NetworkAdapter::COMGETTER(NatDriver) (INATEngine **aNatDriver)
957{
958 CheckComArgOutPointerValid(aNatDriver);
959
960 AutoCaller autoCaller(this);
961 if (FAILED(autoCaller.rc())) return autoCaller.rc();
962
963 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
964
965 mNATEngine.queryInterfaceTo(aNatDriver);
966
967 return S_OK;
968}
969
970STDMETHODIMP NetworkAdapter::COMGETTER(BootPriority) (ULONG *aBootPriority)
971{
972 CheckComArgOutPointerValid(aBootPriority);
973
974 AutoCaller autoCaller(this);
975 if (FAILED(autoCaller.rc())) return autoCaller.rc();
976
977 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
978
979 *aBootPriority = mData->mBootPriority;
980
981 return S_OK;
982}
983
984STDMETHODIMP NetworkAdapter::COMSETTER(BootPriority) (ULONG aBootPriority)
985{
986 AutoCaller autoCaller(this);
987 if (FAILED(autoCaller.rc())) return autoCaller.rc();
988
989 /* the machine needs to be mutable */
990 AutoMutableStateDependency adep(mParent);
991 if (FAILED(adep.rc())) return adep.rc();
992
993 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
994
995 if (aBootPriority != mData->mBootPriority)
996 {
997 mData.backup();
998 mData->mBootPriority = aBootPriority;
999
1000 m_fModified = true;
1001 // leave the lock before informing callbacks
1002 alock.release();
1003
1004 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
1005 mParent->setModified(Machine::IsModified_NetworkAdapters);
1006 mlock.release();
1007
1008 /* No change in CFGM logic => changeAdapter=FALSE. */
1009 mParent->onNetworkAdapterChange(this, FALSE);
1010 }
1011
1012 return S_OK;
1013}
1014
1015// INetworkAdapter methods
1016////////////////////////////////////////////////////////////////////////////////
1017
1018STDMETHODIMP NetworkAdapter::GetProperty(IN_BSTR aKey, BSTR *aValue)
1019{
1020 CheckComArgOutPointerValid(aValue);
1021
1022 AutoCaller autoCaller(this);
1023 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1024
1025 Bstr key = aKey;
1026 Bstr value;
1027
1028 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1029
1030 Utf8Str strKey(key);
1031 settings::StringsMap::const_iterator it = mData->mGenericProperties.find(strKey);
1032 if (it != mData->mGenericProperties.end())
1033 {
1034 value = it->second; // source is a Utf8Str
1035 value.cloneTo(aValue);
1036 }
1037
1038 return S_OK;
1039}
1040
1041STDMETHODIMP NetworkAdapter::SetProperty(IN_BSTR aKey, IN_BSTR aValue)
1042{
1043 LogFlowThisFunc(("\n"));
1044
1045 AutoCaller autoCaller(this);
1046 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1047
1048 /* The machine needs to be mutable. */
1049 AutoMutableStateDependency adep(mParent);
1050 if (FAILED(adep.rc())) return adep.rc();
1051
1052 Bstr key = aKey;
1053
1054 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1055
1056 bool fGenericChange = (mData->mAttachmentType == NetworkAttachmentType_Generic);
1057
1058 /* Generic properties processing.
1059 * Look up the old value first; if nothing's changed then do nothing.
1060 */
1061 Utf8Str strValue(aValue);
1062 Utf8Str strKey(aKey);
1063 Utf8Str strOldValue;
1064
1065 settings::StringsMap::const_iterator it = mData->mGenericProperties.find(strKey);
1066 if (it != mData->mGenericProperties.end())
1067 strOldValue = it->second;
1068
1069 if (strOldValue != strValue)
1070 {
1071 if (strValue.isEmpty())
1072 mData->mGenericProperties.erase(strKey);
1073 else
1074 mData->mGenericProperties[strKey] = strValue;
1075
1076 /* leave the lock before informing callbacks */
1077 alock.release();
1078
1079 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS);
1080 mParent->setModified(Machine::IsModified_NetworkAdapters);
1081 mlock.release();
1082
1083 /* Avoid deadlock when the event triggers a call to a method of this
1084 * interface. */
1085 adep.release();
1086
1087 mParent->onNetworkAdapterChange(this, fGenericChange);
1088 }
1089
1090 return S_OK;
1091}
1092
1093STDMETHODIMP NetworkAdapter::GetProperties(IN_BSTR aNames,
1094 ComSafeArrayOut(BSTR, aReturnNames),
1095 ComSafeArrayOut(BSTR, aReturnValues))
1096{
1097 CheckComArgOutSafeArrayPointerValid(aReturnNames);
1098 CheckComArgOutSafeArrayPointerValid(aReturnValues);
1099
1100 AutoCaller autoCaller(this);
1101 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1102
1103 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1104
1105 /// @todo make use of aNames according to the documentation
1106 NOREF(aNames);
1107
1108 com::SafeArray<BSTR> names(mData->mGenericProperties.size());
1109 com::SafeArray<BSTR> values(mData->mGenericProperties.size());
1110 size_t i = 0;
1111
1112 for (settings::StringsMap::const_iterator it = mData->mGenericProperties.begin();
1113 it != mData->mGenericProperties.end();
1114 ++it)
1115 {
1116 it->first.cloneTo(&names[i]);
1117 it->second.cloneTo(&values[i]);
1118 ++i;
1119 }
1120
1121 names.detachTo(ComSafeArrayOutArg(aReturnNames));
1122 values.detachTo(ComSafeArrayOutArg(aReturnValues));
1123
1124 return S_OK;
1125}
1126
1127
1128
1129// public methods only for internal purposes
1130////////////////////////////////////////////////////////////////////////////////
1131
1132/**
1133 * Loads settings from the given adapter node.
1134 * May be called once right after this object creation.
1135 *
1136 * @param aAdapterNode <Adapter> node.
1137 *
1138 * @note Locks this object for writing.
1139 */
1140HRESULT NetworkAdapter::loadSettings(BandwidthControl *bwctl,
1141 const settings::NetworkAdapter &data)
1142{
1143 AutoCaller autoCaller(this);
1144 AssertComRCReturnRC(autoCaller.rc());
1145
1146 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1147
1148 /* Note: we assume that the default values for attributes of optional
1149 * nodes are assigned in the Data::Data() constructor and don't do it
1150 * here. It implies that this method may only be called after constructing
1151 * a new BIOSSettings object while all its data fields are in the default
1152 * values. Exceptions are fields whose creation time defaults don't match
1153 * values that should be applied when these fields are not explicitly set
1154 * in the settings file (for backwards compatibility reasons). This takes
1155 * place when a setting of a newly created object must default to A while
1156 * the same setting of an object loaded from the old settings file must
1157 * default to B. */
1158
1159 HRESULT rc = S_OK;
1160
1161 mData->mAdapterType = data.type;
1162 mData->mEnabled = data.fEnabled;
1163 /* MAC address (can be null) */
1164 rc = updateMacAddress(data.strMACAddress);
1165 if (FAILED(rc)) return rc;
1166 /* cable (required) */
1167 mData->mCableConnected = data.fCableConnected;
1168 /* line speed (defaults to 100 Mbps) */
1169 mData->mLineSpeed = data.ulLineSpeed;
1170 mData->mPromiscModePolicy = data.enmPromiscModePolicy;
1171 /* tracing (defaults to false) */
1172 mData->mTraceEnabled = data.fTraceEnabled;
1173 mData->mTraceFile = data.strTraceFile;
1174 /* boot priority (defaults to 0, i.e. lowest) */
1175 mData->mBootPriority = data.ulBootPriority;
1176 /* bandwidth group */
1177 if (data.strBandwidthGroup.isEmpty())
1178 updateBandwidthGroup(NULL);
1179 else
1180 {
1181 ComObjPtr<BandwidthGroup> group;
1182 rc = bwctl->getBandwidthGroupByName(data.strBandwidthGroup, group, true);
1183 if (FAILED(rc)) return rc;
1184 }
1185
1186 mNATEngine->loadSettings(data.nat);
1187 mData->mBridgedInterface = data.strBridgedName;
1188 mData->mInternalNetwork = data.strInternalNetworkName;
1189 mData->mHostOnlyInterface = data.strHostOnlyName;
1190 mData->mGenericDriver = data.strGenericDriver;
1191 mData->mGenericProperties = data.genericProperties;
1192
1193 // leave the lock before setting attachment type
1194 alock.release();
1195
1196 rc = COMSETTER(AttachmentType)(data.mode);
1197 if (FAILED(rc)) return rc;
1198
1199 // after loading settings, we are no longer different from the XML on disk
1200 m_fModified = false;
1201
1202 return S_OK;
1203}
1204
1205/**
1206 * Saves settings to the given adapter node.
1207 *
1208 * Note that the given Adapter node is completely empty on input.
1209 *
1210 * @param aAdapterNode <Adapter> node.
1211 *
1212 * @note Locks this object for reading.
1213 */
1214HRESULT NetworkAdapter::saveSettings(settings::NetworkAdapter &data)
1215{
1216 AutoCaller autoCaller(this);
1217 AssertComRCReturnRC(autoCaller.rc());
1218
1219 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1220
1221 data.fEnabled = !!mData->mEnabled;
1222 data.strMACAddress = mData->mMACAddress;
1223 data.fCableConnected = !!mData->mCableConnected;
1224
1225 data.enmPromiscModePolicy = mData->mPromiscModePolicy;
1226 data.ulLineSpeed = mData->mLineSpeed;
1227
1228 data.fTraceEnabled = !!mData->mTraceEnabled;
1229
1230 data.strTraceFile = mData->mTraceFile;
1231
1232 data.ulBootPriority = mData->mBootPriority;
1233
1234 if (mData->mBandwidthGroup.isNull())
1235 data.strBandwidthGroup = "";
1236 else
1237 data.strBandwidthGroup = mData->mBandwidthGroup->getName();
1238
1239 data.type = mData->mAdapterType;
1240
1241 data.mode = mData->mAttachmentType;
1242
1243 mNATEngine->commit();
1244 mNATEngine->saveSettings(data.nat);
1245
1246 data.strBridgedName = mData->mBridgedInterface;
1247
1248 data.strHostOnlyName = mData->mHostOnlyInterface;
1249
1250 data.strInternalNetworkName = mData->mInternalNetwork;
1251
1252 data.strGenericDriver = mData->mGenericDriver;
1253 data.genericProperties = mData->mGenericProperties;
1254
1255 // after saving settings, we are no longer different from the XML on disk
1256 m_fModified = false;
1257
1258 return S_OK;
1259}
1260
1261/**
1262 * Returns true if any setter method has modified settings of this instance.
1263 * @return
1264 */
1265bool NetworkAdapter::isModified() {
1266 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1267 bool fChanged = m_fModified;
1268 fChanged |= (mData->mAdapterType == NetworkAttachmentType_NAT? mNATEngine->isModified() : false);
1269 return fChanged;
1270}
1271
1272/**
1273 * @note Locks this object for writing.
1274 */
1275void NetworkAdapter::rollback()
1276{
1277 /* sanity */
1278 AutoCaller autoCaller(this);
1279 AssertComRCReturnVoid(autoCaller.rc());
1280
1281 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1282
1283 mData.rollback();
1284}
1285
1286/**
1287 * @note Locks this object for writing, together with the peer object (also
1288 * for writing) if there is one.
1289 */
1290void NetworkAdapter::commit()
1291{
1292 /* sanity */
1293 AutoCaller autoCaller(this);
1294 AssertComRCReturnVoid (autoCaller.rc());
1295
1296 /* sanity too */
1297 AutoCaller peerCaller (mPeer);
1298 AssertComRCReturnVoid (peerCaller.rc());
1299
1300 /* lock both for writing since we modify both (mPeer is "master" so locked
1301 * first) */
1302 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
1303
1304 if (mData.isBackedUp())
1305 {
1306 mData.commit();
1307 if (mPeer)
1308 {
1309 /* attach new data to the peer and reshare it */
1310 mPeer->mData.attach (mData);
1311 }
1312 }
1313}
1314
1315/**
1316 * @note Locks this object for writing, together with the peer object
1317 * represented by @a aThat (locked for reading).
1318 */
1319void NetworkAdapter::copyFrom (NetworkAdapter *aThat)
1320{
1321 AssertReturnVoid (aThat != NULL);
1322
1323 /* sanity */
1324 AutoCaller autoCaller(this);
1325 AssertComRCReturnVoid (autoCaller.rc());
1326
1327 /* sanity too */
1328 AutoCaller thatCaller (aThat);
1329 AssertComRCReturnVoid (thatCaller.rc());
1330
1331 /* peer is not modified, lock it for reading (aThat is "master" so locked
1332 * first) */
1333 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
1334 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
1335
1336 /* this will back up current data */
1337 mData.assignCopy (aThat->mData);
1338}
1339
1340void NetworkAdapter::applyDefaults (GuestOSType *aOsType)
1341{
1342 AssertReturnVoid (aOsType != NULL);
1343
1344 /* sanity */
1345 AutoCaller autoCaller(this);
1346 AssertComRCReturnVoid (autoCaller.rc());
1347
1348 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1349
1350 bool e1000enabled = false;
1351#ifdef VBOX_WITH_E1000
1352 e1000enabled = true;
1353#endif // VBOX_WITH_E1000
1354
1355 NetworkAdapterType_T defaultType = aOsType->networkAdapterType();
1356
1357 /* Set default network adapter for this OS type */
1358 if (defaultType == NetworkAdapterType_I82540EM ||
1359 defaultType == NetworkAdapterType_I82543GC ||
1360 defaultType == NetworkAdapterType_I82545EM)
1361 {
1362 if (e1000enabled) mData->mAdapterType = defaultType;
1363 }
1364 else mData->mAdapterType = defaultType;
1365
1366 /* Enable and connect the first one adapter to the NAT */
1367 if (mData->mSlot == 0)
1368 {
1369 mData->mEnabled = true;
1370 mData->mAttachmentType = NetworkAttachmentType_NAT;
1371 mData->mCableConnected = true;
1372 }
1373}
1374
1375// private methods
1376////////////////////////////////////////////////////////////////////////////////
1377
1378/**
1379 * Generates a new unique MAC address based on our vendor ID and
1380 * parts of a GUID.
1381 *
1382 * @note Must be called from under the object's write lock or within the init
1383 * span.
1384 */
1385void NetworkAdapter::generateMACAddress()
1386{
1387 Utf8Str mac;
1388 Host::generateMACAddress(mac);
1389 LogFlowThisFunc(("generated MAC: '%s'\n", mac.c_str()));
1390 mData->mMACAddress = mac;
1391}
1392
1393STDMETHODIMP NetworkAdapter::COMGETTER(BandwidthGroup) (IBandwidthGroup **aBwGroup)
1394{
1395 LogFlowThisFuncEnter();
1396 CheckComArgOutPointerValid(aBwGroup);
1397
1398 AutoCaller autoCaller(this);
1399 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1400
1401 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1402
1403 mData->mBandwidthGroup.queryInterfaceTo(aBwGroup);
1404
1405 LogFlowThisFuncLeave();
1406 return S_OK;
1407}
1408
1409STDMETHODIMP NetworkAdapter::COMSETTER(BandwidthGroup) (IBandwidthGroup *aBwGroup)
1410{
1411 LogFlowThisFuncEnter();
1412
1413 AutoCaller autoCaller(this);
1414 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1415
1416 /* the machine needs to be mutable */
1417 AutoMutableStateDependency adep(mParent);
1418 if (FAILED(adep.rc())) return adep.rc();
1419
1420 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1421
1422 if (mData->mBandwidthGroup != aBwGroup)
1423 {
1424 mData.backup();
1425
1426 updateBandwidthGroup(static_cast<BandwidthGroup*>(aBwGroup));
1427
1428 m_fModified = true;
1429 // leave the lock before informing callbacks
1430 alock.release();
1431
1432 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS);
1433 mParent->setModified(Machine::IsModified_NetworkAdapters);
1434 mlock.release();
1435
1436 /* TODO: changeAdapter=???. */
1437 mParent->onNetworkAdapterChange(this, FALSE);
1438 }
1439
1440 LogFlowThisFuncLeave();
1441 return S_OK;
1442}
1443
1444void NetworkAdapter::updateBandwidthGroup(BandwidthGroup *aBwGroup)
1445{
1446 LogFlowThisFuncEnter();
1447 Assert(isWriteLockOnCurrentThread());
1448
1449 mData.backup();
1450 if (!mData->mBandwidthGroup.isNull())
1451 {
1452 mData->mBandwidthGroup->release();
1453 mData->mBandwidthGroup.setNull();
1454 }
1455
1456 if (aBwGroup)
1457 {
1458 mData->mBandwidthGroup = aBwGroup;
1459 mData->mBandwidthGroup->reference();
1460 }
1461
1462 LogFlowThisFuncLeave();
1463}
1464/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette