VirtualBox

Ignore:
Timestamp:
Jun 27, 2024 2:46:31 PM (5 months ago)
Author:
vboxsync
Message:

Devices/Network: clean up code and comments. bugref:10268

File:
1 edited

Legend:

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

    r105025 r105064  
    185185        Assert(!pSgBuf->pvUser);
    186186        RTMemFree(pSgBuf->aSegs[0].pvSeg);
    187         pSgBuf->pvAllocator = NULL;
    188187    }
    189188    else if (pSgBuf->pvUser)
     
    202201 * @param   pThis               Pointer to the NAT instance.
    203202 * @param   pSgBuf              The scatter/gather buffer.
    204  *
    205203 * @thread  NAT
    206204 */
    207205static DECLCALLBACK(void) drvNATSendWorker(PDRVNAT pThis, PPDMSCATTERGATHER pSgBuf)
    208206{
     207    LogFlowFunc(("pThis=%p pSgBuf=%p\n", pThis, pSgBuf));
     208
    209209    if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
    210210    {
     
    215215             * A normal frame.
    216216             */
     217            LogFlowFunc(("m=%p\n", m));
    217218            slirp_input(pThis->pNATState->pSlirp, (uint8_t const *)pSgBuf->pvAllocator, (int)pSgBuf->cbUsed);
    218219        }
     
    220221        {
    221222            /*
    222              * Large/GSO frame.
     223             * M_EXT buf, need to segment it.
    223224             */
    224225
     
    249250    }
    250251
     252    LogFlowFunc(("leave\n"));
    251253    drvNATFreeSgBuf(pThis, pSgBuf);
    252254}
     
    254256/**
    255257 * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
    256  *
    257  * @param   pInterface      Pointer to UP network interface.
    258  * @param   fOnWorkerThread Unused.
    259  *
    260  * @returns VBox status code.
    261  *
    262  * @thread ?
    263258 */
    264259static DECLCALLBACK(int) drvNATNetworkUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
     
    278273/**
    279274 * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
    280  *
    281  * Allocates a scatter/gather buffer for packet/frame handling.
    282  *
    283  * @param   pInterface  Pointer to UP network interface.
    284  * @param   cbMin       Minimum size of buffer to hold packet/frame.
    285  * @param   pGso        The GSO context.
    286  * @param   ppSgBuf     Pointer to the resulting scatter/gather buffer.
    287  *
    288  * @returns VBox status code
    289  *
    290  * @thread ?
    291275 */
    292276static DECLCALLBACK(int) drvNATNetworkUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
     
    295279    PDRVNAT pThis = RT_FROM_MEMBER(pInterface, DRVNAT, INetworkUp);
    296280    Assert(RTCritSectIsOwner(&pThis->XmitLock));
     281
     282    LogFlowFunc(("enter\n"));
    297283
    298284    /*
     
    377363/**
    378364 * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
    379  *
    380  * Wrapper function for drvNATFreeSgBuff.
    381  *
    382  * @param   pInterface  Pointer to UP network interface.
    383  * @param   pSgBuf      Pointer to scatter/gather buffer to be freed.
    384  *
    385  * @thread  ?
    386365 */
    387366static DECLCALLBACK(int) drvNATNetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
     
    395374/**
    396375 * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
    397  *
    398  * Sends packet/frame out to network (up from guest).
    399  *
    400  * @param   pInterface      Pointer to UP network interface.
    401  * @param   pSgBuf          Pointer to scatter/gather packet/frame buffer.
    402  * @param   fOnWorkerThread Unused.
    403  *
    404  * @thread  ?
    405376 */
    406377static DECLCALLBACK(int) drvNATNetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
     
    411382    Assert(RTCritSectIsOwner(&pThis->XmitLock));
    412383
     384    LogFlowFunc(("enter\n"));
     385
    413386    int rc;
    414387    if (pThis->pSlirpThread->enmState == PDMTHREADSTATE_RUNNING)
     
    420393        {
    421394            drvNATNotifyNATThread(pThis, "drvNATNetworkUp_SendBuf");
     395            LogFlowFunc(("leave success\n"));
    422396            return VINF_SUCCESS;
    423397        }
     
    427401    else
    428402        rc = VERR_NET_DOWN;
    429 
    430403    drvNATFreeSgBuf(pThis, pSgBuf);
     404    LogFlowFunc(("leave rc=%Rrc\n", rc));
    431405    return rc;
    432406}
     
    434408/**
    435409 * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
    436  *
    437  * End transmission.
    438  *
    439  * @param   pInterface  Pointer to UP network interface.
    440  *
    441  * @thread  ?
    442410 */
    443411static DECLCALLBACK(void) drvNATNetworkUp_EndXmit(PPDMINETWORKUP pInterface)
     
    449417/**
    450418 * Get the NAT thread out of poll/WSAWaitForMultipleEvents
    451  *
    452  * @param   pThis   Pointer to DRVNAT state for current context.
    453  * @param   pszWho  String identifying caller.
    454  *
    455  * @thread  ?
    456419 */
    457420static void drvNATNotifyNATThread(PDRVNAT pThis, const char *pszWho)
     
    463426    size_t cbIgnored;
    464427    rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
     428#else
     429    /* kick WSAWaitForMultipleEvents */
     430    rc = WSASetEvent(pThis->hWakeupEvent);
    465431#endif
    466432    AssertRC(rc);
     
    480446 * Worker function for drvNATNetworkUp_NotifyLinkChanged().
    481447 * @thread "NAT" thread.
     448 *
     449 * @param   pThis           Pointer to DRVNAT state for current context.
     450 * @param   enmLinkState    Enum value of link state.
     451 *
     452 * @thread  NAT
    482453 */
    483454static DECLCALLBACK(void) drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
     
    488459        case PDMNETWORKLINKSTATE_UP:
    489460            LogRel(("NAT: Link up\n"));
    490             // slirp_link_up(pThis->pNATState);
    491461            break;
    492462
     
    494464        case PDMNETWORKLINKSTATE_DOWN_RESUME:
    495465            LogRel(("NAT: Link down\n"));
    496             // slirp_link_down(pThis->pNATState);
    497466            break;
    498467
     
    507476 * @param   pInterface      Pointer to the interface structure containing the called function pointer.
    508477 * @param   enmLinkState    The new link state.
     478 *
    509479 * @thread  EMT
    510480 */
     
    537507}
    538508
    539 /** @todo r=jack: move this to the right spot. temp for dev. */
    540 static void register_poll_fd(int fd, void *opaque) {
     509/**
     510 * Registers poll. Unused function (other than logging).
     511 */
     512static void drvNAT_RegisterPoll(int fd, void *opaque) {
    541513    RT_NOREF(fd, opaque);
    542 }
    543 
    544 static void unregister_poll_fd(int fd, void *opaque) {
     514    Log4(("Poll registered\n"));
     515}
     516
     517/**
     518 * Unregisters poll. Unused function (other than logging).
     519 */
     520static void drvNAT_UnregisterPoll(int fd, void *opaque) {
    545521    RT_NOREF(fd, opaque);
    546 }
    547 
    548 static int slirp_to_poll(int events) {
    549     int ret = 0;
     522    Log4(("Poll unregistered\n"));
     523}
     524
     525/**
     526 * Converts slirp representation of poll events to host representation.
     527 *
     528 * @param   iEvents     Integer representing slirp type poll events.
     529 *
     530 * @returns Integer representing host type poll events.
     531 *
     532 * @thread  ?
     533 */
     534static int drvNAT_PollEventSlirpToHost(int iEvents) {
     535    int iRet = 0;
    550536#ifndef RT_OS_WINDOWS
    551     if (events & SLIRP_POLL_IN)  ret |= POLLIN;
    552     if (events & SLIRP_POLL_OUT) ret |= POLLOUT;
    553     if (events & SLIRP_POLL_PRI) ret |= POLLPRI;
    554     if (events & SLIRP_POLL_ERR) ret |= POLLERR;
    555     if (events & SLIRP_POLL_HUP) ret |= POLLHUP;
     537    if (iEvents & SLIRP_POLL_IN)  iRet |= POLLIN;
     538    if (iEvents & SLIRP_POLL_OUT) iRet |= POLLOUT;
     539    if (iEvents & SLIRP_POLL_PRI) iRet |= POLLPRI;
     540    if (iEvents & SLIRP_POLL_ERR) iRet |= POLLERR;
     541    if (iEvents & SLIRP_POLL_HUP) iRet |= POLLHUP;
    556542#else
    557     if (events & SLIRP_POLL_IN)  ret |= (POLLRDNORM | POLLRDBAND);
    558     if (events & SLIRP_POLL_OUT) ret |= POLLWRNORM;
    559     if (events & SLIRP_POLL_PRI) ret |= (POLLIN);
    560     if (events & SLIRP_POLL_ERR) ret |= 0;
    561     if (events & SLIRP_POLL_HUP) ret |= 0;
     543    if (iEvents & SLIRP_POLL_IN)  iRet |= (POLLRDNORM | POLLRDBAND);
     544    if (iEvents & SLIRP_POLL_OUT) iRet |= POLLWRNORM;
     545    if (iEvents & SLIRP_POLL_PRI) iRet |= (POLLIN);
     546    if (iEvents & SLIRP_POLL_ERR) iRet |= 0;
     547    if (iEvents & SLIRP_POLL_HUP) iRet |= 0;
    562548#endif
    563     return ret;
    564 }
    565 
    566 static int poll_to_slirp(int events) {
    567     int ret = 0;
     549    return iRet;
     550}
     551
     552/**
     553 * Converts host representation of poll events to slirp representation.
     554 *
     555 * @param   iEvents     Integer representing host type poll events.
     556 *
     557 * @returns Integer representing slirp type poll events.
     558 *
     559 * @thread  ?
     560 */
     561static int drvNAT_PollEventHostToSlirp(int iEvents) {
     562    int iRet = 0;
    568563#ifndef RT_OS_WINDOWS
    569     if (events & POLLIN)  ret |= SLIRP_POLL_IN;
    570     if (events & POLLOUT) ret |= SLIRP_POLL_OUT;
    571     if (events & POLLPRI) ret |= SLIRP_POLL_PRI;
    572     if (events & POLLERR) ret |= SLIRP_POLL_ERR;
    573     if (events & POLLHUP) ret |= SLIRP_POLL_HUP;
     564    if (iEvents & POLLIN)  iRet |= SLIRP_POLL_IN;
     565    if (iEvents & POLLOUT) iRet |= SLIRP_POLL_OUT;
     566    if (iEvents & POLLPRI) iRet |= SLIRP_POLL_PRI;
     567    if (iEvents & POLLERR) iRet |= SLIRP_POLL_ERR;
     568    if (iEvents & POLLHUP) iRet |= SLIRP_POLL_HUP;
    574569#else
    575     if (events & (POLLRDNORM | POLLRDBAND))  ret |= SLIRP_POLL_IN;
    576     if (events & POLLWRNORM) ret |= SLIRP_POLL_OUT;
    577     if (events & (POLLPRI)) ret |= SLIRP_POLL_PRI;
    578     if (events & POLLERR) ret |= SLIRP_POLL_ERR;
    579     if (events & POLLHUP) ret |= SLIRP_POLL_HUP;
     570    if (iEvents & (POLLRDNORM | POLLRDBAND))  iRet |= SLIRP_POLL_IN;
     571    if (iEvents & POLLWRNORM) iRet |= SLIRP_POLL_OUT;
     572    if (iEvents & (POLLPRI)) iRet |= SLIRP_POLL_PRI;
     573    if (iEvents & POLLERR) iRet |= SLIRP_POLL_ERR;
     574    if (iEvents & POLLHUP) iRet |= SLIRP_POLL_HUP;
    580575#endif
    581     return ret;
    582 }
    583 
    584 static int add_poll_cb(int fd, int events, void *opaque)
    585 {
    586     // LogFlow(("add_poll_cb: fd=%d, events=%d\n", fd, events));
     576    return iRet;
     577}
     578
     579/**
     580 * Callback function to add entry to pollfd array.
     581 *
     582 * @param   iFd     Integer of system file descriptor of socket.
     583 *                  (on windows, this is a VBox internal, not system, value).
     584 * @param   iEvents Integer of slirp type poll events.
     585 * @param   opaque  Pointer to NAT State context.
     586 *
     587 * @returns Index of latest pollfd entry.
     588 *
     589 * @thread  ?
     590 */
     591static int drvNAT_addPollCb(int iFd, int iEvents, void *opaque)
     592{
    587593    PDRVNAT pThis = (PDRVNAT)opaque;
    588594
     
    596602            pThis->pNATState->uPollCap *= 2;
    597603        }
    598         else {
    599             LogFlow(("add_poll_cb return -1"));
     604        else
    600605            return -1;
    601         }
    602606    }
    603607
    604608    int idx = pThis->pNATState->nsock;
    605609#ifdef RT_OS_WINDOWS
    606     pThis->pNATState->polls[idx].fd = libslirp_wrap_RTHandleTableLookup(fd);
     610    pThis->pNATState->polls[idx].fd = libslirp_wrap_RTHandleTableLookup(iFd);
    607611#else
    608     pThis->pNATState->polls[idx].fd = fd;
     612    pThis->pNATState->polls[idx].fd = iFd;
    609613#endif
    610     pThis->pNATState->polls[idx].events = slirp_to_poll(events);
     614    pThis->pNATState->polls[idx].events = drvNAT_PollEventSlirpToHost(iEvents);
    611615    pThis->pNATState->polls[idx].revents = 0;
    612616    pThis->pNATState->nsock += 1;
     
    614618}
    615619
     620/**
     621 * Get translated revents from a poll at a given index.
     622 *
     623 * @param   idx     Integer index of poll.
     624 * @param   opaque  Pointer to NAT State context.
     625 *
     626 * @returns Integer representing transalted revents.
     627 *
     628 * @thread  ?
     629 */
    616630static int get_revents_cb(int idx, void *opaque)
    617631{
    618632    PDRVNAT pThis = (PDRVNAT)opaque;
    619633    struct pollfd* polls = pThis->pNATState->polls;
    620     return poll_to_slirp(polls[idx].revents);
     634    return drvNAT_PollEventHostToSlirp(polls[idx].revents);
    621635}
    622636
     
    630644 * wants to deliver packets to the guest, it enqueues a request into
    631645 * hRecvReqQueue which is later handled by the Recv thread.
     646 *
     647 * @param   pDrvIns     Pointer to PDM driver context.
     648 * @param   pThread     Pointer to calling thread context.
     649 *
     650 * @returns VBox status code
     651 *
     652 * @thread  NAT
    632653 */
    633654static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     
    638659#else /* RT_OS_WINDOWS */
    639660    unsigned int cPollNegRet = 0;
    640     add_poll_cb(RTPipeToNative(pThis->hPipeRead), SLIRP_POLL_IN | SLIRP_POLL_HUP, pThis);
     661    drvNAT_addPollCb(RTPipeToNative(pThis->hPipeRead), SLIRP_POLL_IN | SLIRP_POLL_HUP, pThis);
    641662    pThis->pNATState->polls[0].fd = RTPipeToNative(pThis->hPipeRead);
    642663    pThis->pNATState->polls[0].events = POLLRDNORM | POLLPRI | POLLRDBAND;
     
    664685        pThis->pNATState->nsock = 1;
    665686
    666         slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, add_poll_cb /* SlirpAddPollCb */, pThis /* opaque */);
     687        slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, drvNAT_addPollCb /* SlirpAddPollCb */, pThis /* opaque */);
    667688        slirpUpdateTimeout(&uTimeout, pThis);
    668689
     
    712733        uint32_t uTimeout = 0;
    713734        pThis->pNATState->nsock = 0;
    714         slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, add_poll_cb /* SlirpAddPollCb */, pThis /* opaque */);
     735        slirp_pollfds_fill(pThis->pNATState->pSlirp, &uTimeout, drvNAT_addPollCb /* SlirpAddPollCb */, pThis /* opaque */);
    715736        slirpUpdateTimeout(&uTimeout, pThis);
    716737
     
    760781 * @param   pDevIns     The pcnet device instance.
    761782 * @param   pThread     The send thread.
     783 *
     784 * @thread  ?
    762785 */
    763786static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
     
    770793}
    771794
     795/** @todo r=jack: do we need? */
    772796static DECLCALLBACK(int) drvNATHostResThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    773797{
     
    13971421    slirpCallbacks->timer_free = &slirpTimerFreeCb;
    13981422    slirpCallbacks->timer_mod = &slirpTimerModCb;
    1399     slirpCallbacks->register_poll_fd = &register_poll_fd;
    1400     slirpCallbacks->unregister_poll_fd = &unregister_poll_fd;
     1423    slirpCallbacks->register_poll_fd = &drvNAT_RegisterPoll;
     1424    slirpCallbacks->unregister_poll_fd = &drvNAT_UnregisterPoll;
    14011425    slirpCallbacks->notify = &slirpNotifyCb;
    14021426    slirpCallbacks->init_completed = NULL;
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