VirtualBox

Changeset 22360 in vbox


Ignore:
Timestamp:
Aug 20, 2009 9:18:13 AM (15 years ago)
Author:
vboxsync
Message:

NAT: DrvNAT coding style and comments. No functionality change.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/DrvNAT.cpp

    r22354 r22360  
    148148    char                   *pszNextServer;
    149149    /* polling thread */
    150     PPDMTHREAD              pThread;
     150    PPDMTHREAD              pSlirpThread;
    151151    /** Queue for NAT-thread-external events. */
    152     PRTREQQUEUE             pReqQueue;
     152    PRTREQQUEUE             pSlirpReqQueue;
    153153    /* Send queue */
    154154#ifndef SLIRP_SPLIT_CAN_OUTPUT
     
    172172    STAMCOUNTER             StatConsumerFalse;
    173173#ifdef SLIRP_SPLIT_CAN_OUTPUT
    174     PPDMTHREAD              thrNATRx;
    175     RTSEMEVENT              semNATRx;
    176     STAMCOUNTER             StatNATRxWakeups;
    177     PRTREQQUEUE             pGuestQueue;
     174    PPDMTHREAD              pRecvThread;
     175    RTSEMEVENT              EventRecv;
     176    STAMCOUNTER             StatNATRecvWakeups;
     177    PRTREQQUEUE             pRecvReqQueue;
    178178#endif
    179179} DRVNAT;
     
    198198
    199199
    200 static void drvNATNotifyNATThr(PDRVNAT pThis);
     200static void drvNATNotifyNATThread(PDRVNAT pThis);
     201
     202
     203/** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
     204#define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface)   ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
     205
    201206
    202207#ifdef SLIRP_SPLIT_CAN_OUTPUT
    203 static DECLCALLBACK(int) drvNATRx(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     208static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    204209 {
    205     uint16_t iSendCounter = 0;
    206210    PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
     211
    207212    if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
    208213        return VINF_SUCCESS;
     214
    209215    while (pThread->enmState == PDMTHREADSTATE_RUNNING)
    210216    {
    211         RTReqProcess(pThis->pGuestQueue, 0);
    212         RTSemEventWait(pThis->semNATRx, RT_INDEFINITE_WAIT);
     217        RTReqProcess(pThis->pRecvReqQueue, 0);
     218        RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
    213219    }
    214220    return VINF_SUCCESS;
     
    216222
    217223
    218 static DECLCALLBACK(int) drvNATRxWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     224static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    219225{
    220226    PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    221     int rc = RTSemEventSignal(pThis->semNATRx);
    222     STAM_COUNTER_INC(&pThis->StatNATRxWakeups);
     227    int rc = RTSemEventSignal(pThis->EventRecv);
     228    STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
    223229    AssertReleaseRC(rc);
    224230    return VINF_SUCCESS;
    225231}
    226232
    227 static DECLCALLBACK(void) drvNATRxWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb)
     233static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb)
    228234{
    229235    int rc;
     
    237243}
    238244#endif
    239 
    240 /** Converts a pointer to NAT::INetworkConnector to a PRDVNAT. */
    241 #define PDMINETWORKCONNECTOR_2_DRVNAT(pInterface)   ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkConnector)) )
    242 
    243245
    244246/**
     
    273275    int rc;
    274276    void *buf;
     277
    275278    /* don't queue new requests when the NAT thread is about to stop */
    276     if (pThis->pThread->enmState != PDMTHREADSTATE_RUNNING)
     279    if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
    277280        return VINF_SUCCESS;
     281
    278282#ifndef VBOX_WITH_SLIRP_MT
    279     rc = RTReqAlloc(pThis->pReqQueue, &pReq, RTREQTYPE_INTERNAL);
     283    rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
    280284#else
    281285    rc = RTReqAlloc((PRTREQQUEUE)slirp_get_queue(pThis->pNATState), &pReq, RTREQTYPE_INTERNAL);
     
    301305    rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
    302306    AssertReleaseRC(rc);
    303     drvNATNotifyNATThr(pThis);
     307    drvNATNotifyNATThread(pThis);
    304308    LogFlow(("drvNATSend: end\n"));
    305309    return VINF_SUCCESS;
    306310}
    307311
    308 static void drvNATNotifyNATThr(PDRVNAT pThis)
     312
     313/**
     314 * Get the NAT thread out of poll/WSAWaitForMultipleEvents
     315 */
     316static void drvNATNotifyNATThread(PDRVNAT pThis)
    309317{
    310318    int rc;
     
    319327}
    320328
     329
    321330/**
    322331 * Set promiscuous mode.
     
    376385
    377386    PRTREQ pReq = NULL;
     387
    378388    /* don't queue new requests when the NAT thread is about to stop */
    379     if (pThis->pThread->enmState != PDMTHREADSTATE_RUNNING)
     389    if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
    380390        return;
    381     int rc = RTReqAlloc(pThis->pReqQueue, &pReq, RTREQTYPE_INTERNAL);
     391
     392    int rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
    382393    AssertReleaseRC(rc);
    383394    pReq->u.Internal.pfn      = (PFNRT)drvNATNotifyLinkChangedWorker;
     
    389400    if (RT_LIKELY(rc == VERR_TIMEOUT))
    390401    {
    391         drvNATNotifyNATThr(pThis);
     402        drvNATNotifyNATThread(pThis);
    392403        rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
    393404        AssertReleaseRC(rc);
     
    398409}
    399410
    400 
     411/**
     412 * NAT thread handling the slirp stuff. The slirp implementation is single-threaded
     413 * so we execute this enginre in a dedicated thread. We take care that this thread
     414 * does not become the bottleneck: If the guest wants to send, a request is enqueued
     415 * into the pSlirpReqQueue and handled asynchronously by this thread. If this thread
     416 * wants to deliver packets to the guest, it enqueues a request into pRecvReqQueue
     417 * which is later handled by the Recv thread.
     418 */
    401419static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    402420{
     
    487505        }
    488506        /* process _all_ outstanding requests but don't wait */
    489         RTReqProcess(pThis->pReqQueue, 0);
     507        RTReqProcess(pThis->pSlirpReqQueue, 0);
    490508        RTMemFree(polls);
    491509#else /* RT_OS_WINDOWS */
     
    512530        slirp_select_poll(pThis->pNATState, /* fTimeout=*/false, /* fIcmp=*/(event == WSA_WAIT_EVENT_0));
    513531        /* process _all_ outstanding requests but don't wait */
    514         RTReqProcess(pThis->pReqQueue, 0);
     532        RTReqProcess(pThis->pSlirpReqQueue, 0);
    515533# ifdef VBOX_NAT_DELAY_HACK
    516534        if (cBreak++ > 128)
     
    527545
    528546
    529         /**
    530          * Unblock the send thread so it can respond to a state change.
    531          *
    532          * @returns VBox status code.
    533          * @param   pDevIns     The pcnet device instance.
    534          * @param   pThread     The send thread.
    535          */
    536         static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    537         {
    538             PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    539 
    540         #ifndef RT_OS_WINDOWS
    541             /* kick select() */
    542             int rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
    543             AssertRC(rc);
    544         #else /* !RT_OS_WINDOWS */
    545             /* kick WSAWaitForMultipleEvents() */
    546             WSASetEvent(pThis->hWakeupEvent);
    547         #endif /* RT_OS_WINDOWS */
    548 
    549             return VINF_SUCCESS;
    550         }
    551 
    552         #ifdef VBOX_WITH_SLIRP_MT
    553 
    554         static DECLCALLBACK(int) drvNATAsyncIoGuest(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    555         {
    556             PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    557 
    558             if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
    559                 return VINF_SUCCESS;
    560             while (pThread->enmState == PDMTHREADSTATE_RUNNING)
    561             {
    562                 slirp_process_queue(pThis->pNATState);
    563             }
    564             return VINF_SUCCESS;
    565         }
    566 
    567 
    568         static DECLCALLBACK(int) drvNATAsyncIoGuestWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    569         {
    570             PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    571 
    572             return VINF_SUCCESS;
    573         }
    574 
    575         #endif /* VBOX_WITH_SLIRP_MT */
     547/**
     548 * Unblock the send thread so it can respond to a state change.
     549 *
     550 * @returns VBox status code.
     551 * @param   pDevIns     The pcnet device instance.
     552 * @param   pThread     The send thread.
     553 */
     554static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     555{
     556    PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
     557
     558    drvNATNotifyNATThread(pThis);
     559    return VINF_SUCCESS;
     560}
     561
     562#ifdef VBOX_WITH_SLIRP_MT
     563
     564static DECLCALLBACK(int) drvNATAsyncIoGuest(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     565{
     566    PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
     567
     568    if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
     569        return VINF_SUCCESS;
     570
     571    while (pThread->enmState == PDMTHREADSTATE_RUNNING)
     572        slirp_process_queue(pThis->pNATState);
     573
     574    return VINF_SUCCESS;
     575}
     576
     577
     578static DECLCALLBACK(int) drvNATAsyncIoGuestWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     579{
     580    PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
     581
     582    return VINF_SUCCESS;
     583}
     584
     585#endif /* VBOX_WITH_SLIRP_MT */
     586
    576587
    577588/**
     
    591602{
    592603    PDRVNAT pThis = (PDRVNAT)pvUser;
     604    Assert(pThis);
    593605
    594606    LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
    595607    Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
    596 
    597     /** @todo r-bird: Why do you reset the counters every time? You won't ever count
    598      *        higher than ONE then. If you want to record what happened to the last
    599      *        queued item, use a U8/bool instead to two 64-bit values. */
    600     //STAM_COUNTER_RESET(&pThis->StatQueuePktDropped);
    601     //STAM_COUNTER_RESET(&pThis->StatQueuePktSent);
    602     Assert(pThis);
    603608
    604609#ifndef SLIRP_SPLIT_CAN_OUTPUT
     
    626631#else
    627632    PRTREQ pReq = NULL;
     633
    628634    /* don't queue new requests when the NAT thread is about to stop */
    629     if (pThis->pThread->enmState != PDMTHREADSTATE_RUNNING)
     635    if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
    630636        return;
    631     int rc = RTReqAlloc(pThis->pGuestQueue, &pReq, RTREQTYPE_INTERNAL);
     637
     638    int rc = RTReqAlloc(pThis->pRecvReqQueue, &pReq, RTREQTYPE_INTERNAL);
    632639    AssertReleaseRC(rc);
    633     pReq->u.Internal.pfn      = (PFNRT)drvNATRxWorker;
     640    pReq->u.Internal.pfn      = (PFNRT)drvNATRecvWorker;
    634641    pReq->u.Internal.cArgs    = 3;
    635642    pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
     
    639646    rc = RTReqQueue(pReq, 0);
    640647    AssertReleaseRC(rc);
    641     drvNATRxWakeup(pThis->pDrvIns, pThis->thrNATRx);
     648    drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
    642649    STAM_COUNTER_INC(&pThis->StatQueuePktSent);
    643650#endif
     
    970977                               " queue", "/Drivers/NAT%u/PDMConsumerFalse", pDrvIns->iInstance);
    971978# ifdef SLIRP_SPLIT_CAN_OUTPUT
    972         PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatNATRxWakeups, STAMTYPE_COUNTER,
     979        PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatNATRecvWakeups, STAMTYPE_COUNTER,
    973980                               STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "counting wakeups of NATRX"
    974                                " thread", "/Drivers/NAT%u/NATRxWakeups", pDrvIns->iInstance);
     981                               " thread", "/Drivers/NAT%u/NATRecvWakeups", pDrvIns->iInstance);
    975982# endif
    976983#endif
     
    987994                                       NULL, NULL, NULL, NULL, NULL, drvNATLoadDone);
    988995            AssertRC(rc2);
    989             rc = RTReqCreateQueue(&pThis->pReqQueue);
     996            rc = RTReqCreateQueue(&pThis->pSlirpReqQueue);
    990997            if (RT_FAILURE(rc))
    991998            {
     
    10041011            }
    10051012#else
    1006             rc = RTReqCreateQueue(&pThis->pGuestQueue);
     1013            rc = RTReqCreateQueue(&pThis->pRecvReqQueue);
    10071014            if (RT_FAILURE(rc))
    10081015            {
     
    10101017                return rc;
    10111018            }
    1012             rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->thrNATRx, pThis, drvNATRx,
    1013                                           drvNATRxWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
     1019            rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
     1020                                          drvNATRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
    10141021            AssertReleaseRC(rc);
    1015             rc = RTSemEventCreate(&pThis->semNATRx);
     1022            rc = RTSemEventCreate(&pThis->EventRecv);
    10161023#endif
    10171024
     
    10351042#endif
    10361043
    1037             rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvNATAsyncIoThread,
     1044            rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
    10381045                                          drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
    10391046            AssertReleaseRC(rc);
Note: See TracChangeset for help on using the changeset viewer.

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