VirtualBox

Changeset 105070 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
Jun 27, 2024 9:48:17 PM (7 months ago)
Author:
vboxsync
Message:

Devices/Network: code cleanup. bugref:10268

Location:
trunk/src/VBox/Devices/Network
Files:
2 edited

Legend:

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

    r105069 r105070  
    7777
    7878/**
    79  * @callback_method_impl{FNPDMTHREADDRV}
    80  */
    81 static DECLCALLBACK(int) drvNATUrgRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    82 {
    83     PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    84 
    85     if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
    86         return VINF_SUCCESS;
    87 
    88     while (pThread->enmState == PDMTHREADSTATE_RUNNING)
    89     {
    90         RTReqQueueProcess(pThis->hUrgRecvReqQueue, 0);
    91         if (ASMAtomicReadU32(&pThis->cUrgPkts) == 0)
    92         {
    93             int rc = RTSemEventWait(pThis->EventUrgRecv, RT_INDEFINITE_WAIT);
    94             AssertRC(rc);
    95         }
    96     }
    97     return VINF_SUCCESS;
    98 }
    99 
    100 /**
    101  * @callback_method_impl{FNPDMTHREADWAKEUPDRV}
    102  */
    103 static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
    104 {
    105     RT_NOREF(pThread);
    106     PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
    107     int rc = RTSemEventSignal(pThis->EventUrgRecv);
    108     AssertRC(rc);
    109 
    110     return VINF_SUCCESS;
    111 }
    112 
    113 /**
    11479 * @brief Processes incoming packet (to guest).
    11580 *
     
    12489    int rc;
    12590    STAM_PROFILE_START(&pThis->StatNATRecv, a);
    126 
    127     while (ASMAtomicReadU32(&pThis->cUrgPkts) != 0)
    128     {
    129         rc = RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
    130         if (   RT_FAILURE(rc)
    131             && (   rc == VERR_TIMEOUT
    132                 || rc == VERR_INTERRUPTED))
    133             goto done_unlocked;
    134     }
    13591
    13692    rc = RTCritSectEnter(&pThis->DevAccessLock);
     
    156112    rc = RTCritSectLeave(&pThis->DevAccessLock);
    157113    AssertRC(rc);
    158 
    159 done_unlocked:
    160114    ASMAtomicDecU32(&pThis->cPkts);
    161 
    162115    drvNATNotifyNATThread(pThis, "drvNATRecvWorker");
    163 
    164     RTMemFree(pBuf);
    165     pBuf = NULL;
    166 
    167116    STAM_PROFILE_STOP(&pThis->StatNATRecv, a);
    168117}
     
    426375    size_t cbIgnored;
    427376    rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
    428 #else
    429     /* kick WSAWaitForMultipleEvents */
    430     rc = WSASetEvent(pThis->hWakeupEvent);
    431377#endif
    432378    AssertRC(rc);
     
    628574 * @thread  ?
    629575 */
    630 static int get_revents_cb(int idx, void *opaque)
     576static int drvNAT_getREventsCb(int idx, void *opaque)
    631577{
    632578    PDRVNAT pThis = (PDRVNAT)opaque;
     
    706652
    707653
    708         slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0, get_revents_cb /* SlirpGetREventsCb */, pThis /* opaque */);
     654        slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0, drvNAT_getREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
    709655        if (pThis->pNATState->polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
    710656        {
     
    751697        {
    752698            /* only check for slow/fast timers */
    753             slirp_pollfds_poll(pThis->pNATState->pSlirp, false /*select error*/, get_revents_cb /* SlirpGetREventsCb */, pThis /* opaque */);
     699            slirp_pollfds_poll(pThis->pNATState->pSlirp, false /*select error*/, drvNAT_getREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
    754700            RTReqQueueProcess(pThis->hSlirpReqQueue, 0);
    755701            continue;
     
    757703        /* poll the sockets in any case */
    758704        Log2(("%s: poll\n", __FUNCTION__));
    759         slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0 /*select error*/, get_revents_cb /* SlirpGetREventsCb */, pThis /* opaque */);
     705        slirp_pollfds_poll(pThis->pNATState->pSlirp, cChangedFDs < 0 /*select error*/, drvNAT_getREventsCb /* SlirpGetREventsCb */, pThis /* opaque */);
    760706
    761707        /* process _all_ outstanding requests but don't wait */
     
    831777 *
    832778 * @returns VBox status code.
     779 * @param   uInstance       ?
     780 * @param   pThis           ?
    833781 * @param   pCfg            The configuration handle.
     782 * @param   pNetwork        Unused.
     783 *
     784 * @thread  ?
    834785 */
    835786static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfg, PRTNETADDRIPV4 pNetwork)
    836787{
     788    /** @todo r=jack: rewrite to support IPv6? */
    837789    PPDMDRVINS pDrvIns = pThis->pDrvIns;
    838790    PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
     
    914866}
    915867
     868/**
     869 * Applies port forwarding between guest and host.
     870 *
     871 * @param   pThis           Pointer to DRVNAT state for current context.
     872 * @param   fRemove         Flag to remove port forward instead of create.
     873 * @param   fUdp            Flag specifying if UDP. If false, TCP.
     874 * @param   pHostIp         String of host IP address.
     875 * @param   u16HostPort     Host port to forward to.
     876 * @param   pGuestIp        String of guest IP address.
     877 * @param   u16GuestPort    Guest port to forward.
     878 *
     879 * @thread  ?
     880 */
    916881static DECLCALLBACK(void) drvNATNotifyApplyPortForwardCommand(PDRVNAT pThis, bool fRemove,
    917882                                                              bool fUdp, const char *pHostIp,
    918883                                                              uint16_t u16HostPort, const char *pGuestIp, uint16_t u16GuestPort)
    919884{
     885    /** @todo r=jack:
     886     * - rewrite for IPv6
     887     * - do we want to lock the guestIp to the VMs IP?
     888     */
    920889    struct in_addr guestIp, hostIp;
    921890
     
    970939}
    971940
     941/**
     942 * Update the timeout field in given list of Slirp timers.
     943 *
     944 * @param uTimeout  Pointer to timeout value.
     945 * @param opaque    Pointer to NAT State context.
     946 *
     947 * @thread  ?
     948 */
    972949static void slirpUpdateTimeout(uint32_t *uTimeout, void *opaque)
    973950{
     
    994971}
    995972
     973/**
     974 * Check if timeout has passed in given list of Slirp timers.
     975 *
     976 * @param   opaque  Pointer to NAT State context.
     977 *
     978 * @thread  ?
     979 */
    996980static void slirpCheckTimeout(void *opaque)
    997981{
     
    10191003/**
    10201004 * CALLBACKS
     1005 */
     1006
     1007/**
     1008 * Callback called by libslirp to send packet into guest.
     1009 *
     1010 * @param   pBuf    Pointer to packet buffer.
     1011 * @param   cb      Size of packet.
     1012 * @param   opaque  Pointer to NAT State context.
     1013 *
     1014 * @returns Size of packet received or -1 on error.
     1015 *
     1016 * @thread  ?
    10211017 */
    10221018static DECLCALLBACK(ssize_t) slirpSendPacketCb(const void *pBuf, size_t cb, void *opaque /* PDRVNAT */)
     
    10461042}
    10471043
     1044/**
     1045 * Callback called by libslirp on an error from a guest.
     1046 *
     1047 * @param   pMsg    Error message string.
     1048 * @param   opaque  Pointer to NAT State context.
     1049 *
     1050 * @thread  ?
     1051 */
    10481052static DECLCALLBACK(void) slirpGuestErrorCb(const char *pMsg, void *opaque)
    10491053{
     
    10561060}
    10571061
     1062/**
     1063 * Callback called by libslirp to get the current timestamp in nanoseconds.
     1064 *
     1065 * @param   opaque  Pointer to NAT State context.
     1066 *
     1067 * @returns 64-bit signed integer representing time in nanoseconds.
     1068 */
    10581069static DECLCALLBACK(int64_t) slirpClockGetNsCb(void *opaque)
    10591070{
     
    10641075}
    10651076
     1077
     1078/**
     1079 * Callback called by slirp to create a new timer and insert it into the given list.
     1080 *
     1081 * @param   slirpTimeCb     Callback function supplied to the new timer upon timer expiry.
     1082 *                          Called later by the timeout handler.
     1083 * @param   cb_opaque       Opaque object supplied to slirpTimeCb when called. Should be
     1084 *                          Identical to the opaque parameter.
     1085 * @param   opaque          Pointer to NAT State context.
     1086 *
     1087 * @returns Pointer to new timer.
     1088 */
    10661089static DECLCALLBACK(void *) slirpTimerNewCb(SlirpTimerCb slirpTimeCb, void *cb_opaque, void *opaque)
    10671090{
     
    10821105}
    10831106
     1107/**
     1108 * Callback called by slirp to free a timer.
     1109 *
     1110 * @param   pTimer  Pointer to slirpTimer object to be freed.
     1111 * @param   opaque  Pointer to NAT State context.
     1112 */
    10841113static DECLCALLBACK(void) slirpTimerFreeCb(void *pTimer, void *opaque)
    10851114{
     
    11011130}
    11021131
     1132/**
     1133 * Callback called by slirp to modify a timer.
     1134 *
     1135 * @param   pTimer      Pointer to slirpTimer object to be modified.
     1136 * @param   expireTime  Signed 64-bit integer representing the new expiry time.
     1137 * @param   opaque      Pointer to NAT State context.
     1138 */
    11031139static DECLCALLBACK(void) slirpTimerModCb(void *pTimer, int64_t expireTime, void *opaque)
    11041140{
     
    11091145}
    11101146
     1147/**
     1148 * Callback called by slirp when there is I/O that needs to happen.
     1149 *
     1150 * @param   opaque  Pointer to NAT State context.
     1151 */
    11111152static DECLCALLBACK(void) slirpNotifyCb(void *opaque)
    11121153{
     
    11441185    pThis->hSlirpReqQueue = NIL_RTREQQUEUE;
    11451186
    1146     RTReqQueueDestroy(pThis->hUrgRecvReqQueue);
    1147     pThis->hUrgRecvReqQueue = NIL_RTREQQUEUE;
    1148 
    11491187    RTReqQueueDestroy(pThis->hRecvReqQueue);
    11501188    pThis->hRecvReqQueue = NIL_RTREQQUEUE;
     
    11521190    RTSemEventDestroy(pThis->EventRecv);
    11531191    pThis->EventRecv = NIL_RTSEMEVENT;
    1154 
    1155     RTSemEventDestroy(pThis->EventUrgRecv);
    1156     pThis->EventUrgRecv = NIL_RTSEMEVENT;
    11571192
    11581193    if (RTCritSectIsInitialized(&pThis->DevAccessLock))
     
    11651200    RTPipeClose(pThis->hPipeRead);
    11661201    RTPipeClose(pThis->hPipeWrite);
    1167 #endif
    1168 
    1169 #ifdef RT_OS_DARWIN
    1170     /* Cleanup the DNS watcher. */
    1171     if (pThis->hRunLoopSrcDnsWatcher != NULL)
    1172     {
    1173         CFRunLoopRef hRunLoopMain = CFRunLoopGetMain();
    1174         CFRetain(hRunLoopMain);
    1175         CFRunLoopRemoveSource(hRunLoopMain, pThis->hRunLoopSrcDnsWatcher, kCFRunLoopCommonModes);
    1176         CFRelease(hRunLoopMain);
    1177         CFRelease(pThis->hRunLoopSrcDnsWatcher);
    1178         pThis->hRunLoopSrcDnsWatcher = NULL;
    1179     }
    11801202#endif
    11811203}
     
    12021224    pThis->pNATState                    = (SlirpState *)RTMemAlloc(sizeof(SlirpState));
    12031225    if(pThis->pNATState == NULL)
    1204     {
    12051226        return VERR_NO_MEMORY;
    1206     }
    12071227    else
    12081228    {
     
    12131233    }
    12141234    pThis->hSlirpReqQueue               = NIL_RTREQQUEUE;
    1215     pThis->hUrgRecvReqQueue             = NIL_RTREQQUEUE;
    12161235    pThis->EventRecv                    = NIL_RTSEMEVENT;
    1217     pThis->EventUrgRecv                 = NIL_RTSEMEVENT;
    1218 #ifdef RT_OS_DARWIN
    1219     pThis->hRunLoopSrcDnsWatcher        = NULL;
    1220 #endif
    12211236
    12221237    /* IBase */
     
    13671382    pSlirpCfg->if_mtu = MTU;
    13681383
    1369 #ifndef RT_OS_WINDOWS
    13701384    inet_pton(AF_INET6, "fd00::3", &pSlirpCfg->vnameserver6);
    1371 #else
    1372     inet_pton(23, "fd00::3", &pSlirpCfg->vnameserver6);
    1373 #endif
    13741385
    13751386    pSlirpCfg->vdnssearch = NULL;
     
    13971408    pThis->pNATState->pSlirp = pSlirp;
    13981409
    1399     // pThis->pNATState->polls = NULL;
    1400 
    14011410    rc = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfg, &Network);
    14021411    AssertLogRelRCReturn(rc, rc);
     
    14091418
    14101419    rc = RTReqQueueCreate(&pThis->hRecvReqQueue);
    1411     AssertLogRelRCReturn(rc, rc);
    1412 
    1413     rc = RTReqQueueCreate(&pThis->hUrgRecvReqQueue);
    14141420    AssertLogRelRCReturn(rc, rc);
    14151421
     
    14191425
    14201426    rc = RTSemEventCreate(&pThis->EventRecv);
    1421     AssertRCReturn(rc, rc);
    1422 
    1423     rc = RTSemEventCreate(&pThis->EventUrgRecv);
    1424     AssertRCReturn(rc, rc);
    1425 
    1426     rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pUrgRecvThread, pThis, drvNATUrgRecv,
    1427                                 drvNATUrgRecvWakeup, 256 * _1K, RTTHREADTYPE_IO, "NATURGRX");
    14281427    AssertRCReturn(rc, rc);
    14291428
     
    14451444
    14461445#ifndef RT_OS_WINDOWS
    1447     /*
    1448         * Create the control pipe.
    1449         */
     1446    /**
     1447     * Create the control pipe.
     1448     */
    14501449    rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
    14511450    AssertRCReturn(rc, rc);
    1452 #else
    1453     pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
    1454     pThis->pNATState->phEvents[VBOX_WAKEUP_EVENT_INDEX] = pThis->hWakeupEvent;
    1455     pThis->pNATState->phEvents[VBOX_SOCKET_EVENT_INDEX] = CreateEvent(NULL, FALSE, FALSE, NULL);
    14561451#endif
    14571452
  • trunk/src/VBox/Devices/Network/DrvNATlibslirp.h

    r105069 r105070  
    103103#define VBOX_NAT_DELAY_HACK
    104104
    105 /*
    106  * ICMP handle state change
    107  */
    108 # define VBOX_ICMP_EVENT_INDEX           -1
    109 
    110 /**
    111  * This event is for
    112  *  - slirp_input
    113  *  - slirp_link_up
    114  *  - slirp_link_down
    115  *  - wakeup
    116  */
    117 # define VBOX_WAKEUP_EVENT_INDEX       0
    118 
    119 /*
    120  * UDP/TCP socket state change (socket ready to receive, to send, ...)
    121  */
    122 # define VBOX_SOCKET_EVENT_INDEX       1
    123 
    124 /*
    125  * The number of events for WSAWaitForMultipleEvents().
    126  */
    127 # define VBOX_EVENT_COUNT              2
    128 
    129105#define GET_EXTRADATA(pdrvins, node, name, rc, type, type_name, var)                                  \
    130106do {                                                                                                \
     
    179155} while (0)
    180156
    181 // timer struct
     157/** Slirp Timer */
    182158typedef struct slirpTimer {
    183159    struct slirpTimer *next;
     
    193169{
    194170    unsigned int nsock;
    195 # ifndef RT_OS_WINDOWS
    196     /* counter of sockets needed for allocation enough room to
    197      * process sockets with poll/epoll
    198      *
    199      * NSOCK_INC/DEC should be injected before every
    200      * operation on socket queue (tcb, udb)
    201      */
    202 #  define NSOCK_INC() do {pData->nsock++;} while (0)
    203 #  define NSOCK_DEC() do {pData->nsock--;} while (0)
    204 #  define NSOCK_INC_EX(ex) do {ex->pData->nsock++;} while (0)
    205 #  define NSOCK_DEC_EX(ex) do {ex->pData->nsock--;} while (0)
    206 # else
    207 #  define NSOCK_INC() do {} while (0)
    208 #  define NSOCK_DEC() do {} while (0)
    209 #  define NSOCK_INC_EX(ex) do {} while (0)
    210 #  define NSOCK_DEC_EX(ex) do {} while (0)
    211 # endif
    212 
    213 #if defined(RT_OS_WINDOWS)
    214 # define VBOX_SOCKET_EVENT (pData->phEvents[VBOX_SOCKET_EVENT_INDEX])
    215     HANDLE phEvents[VBOX_EVENT_COUNT];
    216 #endif
    217171
    218172    Slirp *pSlirp;
    219173    struct pollfd *polls;
    220174
    221     // Num Polls (not bytes)
     175    /** Num Polls (not bytes) */
    222176    unsigned int uPollCap = 0;
    223177
     
    280234    /** thread delivering packets for receiving by the guest */
    281235    PPDMTHREAD              pRecvThread;
    282     /** thread delivering urg packets for receiving by the guest */
    283     PPDMTHREAD              pUrgRecvThread;
    284236    /** event to wakeup the guest receive thread */
    285237    RTSEMEVENT              EventRecv;
    286     /** event to wakeup the guest urgent receive thread */
    287     RTSEMEVENT              EventUrgRecv;
    288238    /** Receive Req queue (deliver packets to the guest) */
    289239    RTREQQUEUE              hRecvReqQueue;
    290     /** Receive Urgent Req queue (deliver packets to the guest). */
    291     RTREQQUEUE              hUrgRecvReqQueue;
    292240
    293241    /** makes access to device func RecvAvail and Recv atomical. */
    294242    RTCRITSECT              DevAccessLock;
    295     /** Number of in-flight urgent packets. */
    296     volatile uint32_t       cUrgPkts;
    297     /** Number of in-flight regular packets. */
     243    /** Number of in-flight packets. */
    298244    volatile uint32_t       cPkts;
    299245
     
    306252#endif /* !VBOX_INCLUDED_SRC_Network_DrvNATlibslirp_h */
    307253} DRVNAT;
    308 // AssertCompileMemberAlignment(DRVNAT, StatNATRecvWakeups, 8);
     254AssertCompileMemberAlignment(DRVNAT, StatNATRecvWakeups, 8);
    309255/** Pointer to the NAT driver instance data. */
    310256typedef DRVNAT *PDRVNAT;
     
    312258static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS, PPDMTHREAD);
    313259static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS, PPDMTHREAD);
    314 static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS, PPDMTHREAD);
    315260static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT, void *, int);
    316261static void drvNATFreeSgBuf(PDRVNAT, PPDMSCATTERGATHER);
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