Skip to content

Commit 993a24b

Browse files
committed
Improve ifqueue handling.
1 parent d404254 commit 993a24b

File tree

13 files changed

+175
-88
lines changed

13 files changed

+175
-88
lines changed

AirportItlwm/AirportItlwm.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ IOService* AirportItlwm::probe(IOService *provider, SInt32 *score)
103103
return NULL;
104104
}
105105

106-
bool AirportItlwm::configureInterface(IONetworkInterface *netif) {
106+
bool AirportItlwm::configureInterface(IONetworkInterface *netif)
107+
{
107108
IONetworkData *nd;
109+
struct _ifnet *ifp = &fHalService->get80211Controller()->ic_ac.ac_if;
108110

109111
if (super::configureInterface(netif) == false) {
110112
XYLog("super failed\n");
@@ -116,8 +118,8 @@ bool AirportItlwm::configureInterface(IONetworkInterface *netif) {
116118
XYLog("network statistics buffer unavailable?\n");
117119
return false;
118120
}
119-
fHalService->get80211Controller()->ic_ac.ac_if.netStat = fpNetStats;
120-
fHalService->get80211Controller()->ic_ac.ac_if.iface = OSDynamicCast(IOEthernetInterface, netif);
121+
ifp->netStat = fpNetStats;
122+
ether_ifattach(ifp, OSDynamicCast(IOEthernetInterface, netif));
121123
fpNetStats->collisions = 0;
122124
#ifdef __PRIVATE_SPI__
123125
netif->configureOutputPullModel(fHalService->getDriverInfo()->getTxQueueSize(), 0, 0, IOEthernetInterface::kOutputPacketSchedulingModelNormal, 0);
@@ -515,9 +517,9 @@ void AirportItlwm::stop(IOService *provider)
515517
disableAdapter(fNetIf);
516518
setLinkStatus(kIONetworkLinkValid);
517519
fHalService->detach(pciNub);
520+
ether_ifdetach(ifp);
518521
detachInterface(fNetIf, true);
519522
OSSafeReleaseNULL(fNetIf);
520-
ifp->iface = NULL;
521523
releaseAll();
522524
}
523525

@@ -535,17 +537,15 @@ setLinkStatus(UInt32 status, const IONetworkMedium * activeMedium, UInt64 speed,
535537
#ifdef __PRIVATE_SPI__
536538
fNetIf->startOutputThread();
537539
#endif
538-
ifq_set_oactive(&ifq->if_snd);
539540
getCommandGate()->runAction(setLinkStateGated, (void *)kIO80211NetworkLinkUp, (void *)0);
540541
fNetIf->setLinkQualityMetric(100);
541542
} else if (!(status & kIONetworkLinkNoNetworkChange)) {
542543
#ifdef __PRIVATE_SPI__
543544
fNetIf->stopOutputThread();
544545
fNetIf->flushOutputQueue();
545546
#endif
546-
ifq->if_snd->lockFlush();
547+
ifq_flush(&ifq->if_snd);
547548
mq_purge(&fHalService->get80211Controller()->ic_mgtq);
548-
ifq_clr_oactive(&ifq->if_snd);
549549
getCommandGate()->runAction(setLinkStateGated, (void *)kIO80211NetworkLinkDown, (void *)fHalService->get80211Controller()->ic_deauth_reason);
550550
}
551551
}
@@ -687,14 +687,12 @@ IOReturn AirportItlwm::outputStart(IONetworkInterface *interface, IOOptionBits o
687687
{
688688
struct _ifnet *ifp = &fHalService->get80211Controller()->ic_ac.ac_if;
689689
mbuf_t m = NULL;
690-
if (!ifq_is_oactive(&ifp->if_snd)) {
690+
if (ifq_is_oactive(&ifp->if_snd))
691691
return kIOReturnNoResources;
692-
}
693692
while (kIOReturnSuccess == interface->dequeueOutputPackets(1, &m)) {
694693
outputPacket(m, NULL);
695-
if (!ifq_is_oactive(&ifp->if_snd)) {
694+
if (ifq_is_oactive(&ifp->if_snd))
696695
return kIOReturnNoResources;
697-
}
698696
}
699697
return kIOReturnSuccess;
700698
}
@@ -706,7 +704,7 @@ UInt32 AirportItlwm::outputPacket(mbuf_t m, void *param)
706704
IOReturn ret = kIOReturnOutputSuccess;
707705
struct _ifnet *ifp = &fHalService->get80211Controller()->ic_ac.ac_if;
708706

709-
if (fHalService->get80211Controller()->ic_state != IEEE80211_S_RUN || ifp->if_snd == NULL) {
707+
if (fHalService->get80211Controller()->ic_state != IEEE80211_S_RUN || ifp->if_snd.queue == NULL) {
710708
if (m && mbuf_type(m) != MBUF_TYPE_FREE) {
711709
freePacket(m);
712710
}
@@ -728,7 +726,7 @@ UInt32 AirportItlwm::outputPacket(mbuf_t m, void *param)
728726
ifp->netStat->outputErrors++;
729727
ret = kIOReturnOutputDropped;
730728
}
731-
if (!ifp->if_snd->lockEnqueue(m)) {
729+
if (!ifp->if_snd.queue->lockEnqueue(m)) {
732730
freePacket(m);
733731
ret = kIOReturnOutputDropped;
734732
}

itl80211/openbsd/net80211/ieee80211.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,15 @@ ieee80211_channel_init(struct _ifnet *ifp)
215215
}
216216

217217
void
218-
ieee80211_ifattach(struct _ifnet *ifp)
218+
ieee80211_ifattach(struct _ifnet *ifp, IOEthernetController *controller)
219219
{
220220
IOLog("ieee80211_ifattach\n");
221221
struct ieee80211com *ic = (struct ieee80211com *)ifp;
222222

223+
ifp->controller = controller;
224+
ifq_init(&ifp->if_snd, ifp, 2048);
223225
memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr,
224226
ETHER_ADDR_LEN);
225-
// ether_ifattach(ifp);
226227
if (ifp->if_sadl) {
227228
::free(ifp->if_sadl);
228229
}
@@ -273,11 +274,7 @@ ieee80211_ifdetach(struct _ifnet *ifp)
273274
ieee80211_crypto_detach(ifp);
274275
ieee80211_node_detach(ifp);
275276
ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
276-
if (ifp->if_snd) {
277-
ifp->if_snd->flush();
278-
ifp->if_snd->release();
279-
ifp->if_snd = NULL;
280-
}
277+
ifq_destroy(&ifp->if_snd);
281278
if (ifp->if_slowtimo) {
282279
ifp->if_slowtimo->release();
283280
ifp->if_slowtimo = NULL;
@@ -289,7 +286,6 @@ ieee80211_ifdetach(struct _ifnet *ifp)
289286
ifp->netStat = NULL;
290287
ifp->controller = NULL;
291288
ifp->iface = NULL;
292-
// ether_ifdetach(ifp);
293289
}
294290

295291
/*

itl80211/openbsd/net80211/ieee80211_pae_output.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ ieee80211_send_eapol_key(struct ieee80211com *ic, mbuf_t m,
139139
timeout_add_msec(&ni->ni_eapol_to, 100);
140140
#endif
141141

142-
if (!ifp->if_snd->lockEnqueue(m)) {
142+
error = ifq_enqueue(&ifp->if_snd, m);
143+
if (error) {
143144
XYLog("%s enqueue fail!!\n", __FUNCTION__);
144-
mbuf_freem(m);
145-
return -1;
145+
return (error);
146146
}
147147
(*ifp->if_start)(ifp);
148148
return 0;

itl80211/openbsd/net80211/ieee80211_var.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ struct ieee80211_ess {
671671
#define IEEE80211_EVT_COUNTRY_CODE_UPDATE 3
672672
#define IEEE80211_EVT_SCAN_DONE 4
673673

674-
void ieee80211_ifattach(struct _ifnet *);
674+
void ieee80211_ifattach(struct _ifnet *, IOEthernetController *controller);
675675
void ieee80211_ifdetach(struct _ifnet *);
676676
void ieee80211_channel_init(struct _ifnet *);
677677
void ieee80211_media_init(struct _ifnet *);

itl80211/openbsd/sys/_if_ether.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <sys/queue.h>
6161
#include <sys/CTimeout.hpp>
6262
#include <sys/_if_media.h>
63+
#include <sys/_ifq.h>
6364
#include <net/if_dl.h>
6465

6566
#include <IOKit/network/IOPacketQueue.h>
@@ -225,9 +226,9 @@ struct _ifnet { /* and the entries */
225226
int (*if_wol)(struct _ifnet *, int); /* WoL routine **/
226227

227228
/* queues */
228-
IOPacketQueue *if_snd; /* transmit queue */
229-
struct ifqueue **if_ifqs; /* [I] pointer to an array of sndqs */
230-
void (*if_qstart)(struct ifqueue *);
229+
struct _ifqueue if_snd; /* transmit queue */
230+
struct _ifqueue **if_ifqs; /* [I] pointer to an array of sndqs */
231+
void (*if_qstart)(struct _ifqueue *);
231232
unsigned int if_nifqs; /* [I] number of output queues */
232233
unsigned int if_txmit; /* [c] txmitigation amount */
233234

@@ -283,4 +284,16 @@ if_detach(struct _ifnet *ifp)
283284
return 0;
284285
}
285286

287+
static inline int
288+
ether_ifattach(struct _ifnet *ifp, IOEthernetInterface *iface)
289+
{
290+
ifp->iface = iface;
291+
}
292+
293+
static inline void
294+
ether_ifdetach(struct _ifnet *ifp)
295+
{
296+
ifp->iface = NULL;
297+
}
298+
286299
#endif /* _if_ether_h */

itl80211/openbsd/sys/_ifq.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// _ifq.cpp
3+
// itlwm
4+
//
5+
// Created by zxystd on 2022/8/13.
6+
// Copyright © 2022 钟先耀. All rights reserved.
7+
//
8+
9+
#include <sys/_ifq.h>
10+
11+
void ifq_init(struct _ifqueue *ifq, struct _ifnet *ifp, unsigned int maxLen)
12+
{
13+
if (!ifq->queue)
14+
ifq->queue = IOPacketQueue::withCapacity(maxLen);
15+
else
16+
ifq->queue->setCapacity(maxLen);
17+
ifq->ifq_oactive = 0;
18+
}
19+
20+
void ifq_destroy(struct _ifqueue *ifq)
21+
{
22+
if (ifq->queue) {
23+
ifq->queue->release();
24+
ifq->queue = nullptr;
25+
}
26+
}
27+
28+
void ifq_flush(struct _ifqueue *ifq)
29+
{
30+
if (ifq->queue)
31+
ifq->queue->lockFlush();
32+
}
33+
34+
bool ifq_empty(struct _ifqueue *ifq)
35+
{
36+
return ifq->queue->getSize() == 0;
37+
}
38+
39+
uint32_t ifq_len(struct _ifqueue *ifq)
40+
{
41+
return ifq->queue->getSize();
42+
}
43+
44+
void ifq_set_maxlen(struct _ifqueue *ifq, uint32_t maxLen)
45+
{
46+
ifq->queue->setCapacity(maxLen);
47+
}
48+
49+
void ifq_set_oactive(struct _ifqueue *ifq)
50+
{
51+
ifq->ifq_oactive = 1;
52+
}
53+
54+
unsigned int ifq_is_oactive(struct _ifqueue *ifq)
55+
{
56+
return ifq->ifq_oactive;
57+
}
58+
59+
void ifq_clr_oactive(struct _ifqueue *ifq)
60+
{
61+
ifq->ifq_oactive = 0;
62+
}
63+
64+
mbuf_t ifq_dequeue(struct _ifqueue *ifq)
65+
{
66+
return ifq->queue->lockDequeue();
67+
}
68+
69+
int ifq_enqueue(struct _ifqueue *ifq, mbuf_t m)
70+
{
71+
ifq->queue->lockEnqueueWithDrop(m);
72+
return 0;
73+
}

itl80211/openbsd/sys/_ifq.h

+26-25
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,31 @@
2424
#include <net/if_var.h>
2525
#include <IOKit/network/IOPacketQueue.h>
2626

27-
static int ifq_oactive;
28-
29-
static inline void
30-
ifq_set_oactive(IOPacketQueue **ifq)
31-
{
32-
ifq_oactive = 1;
33-
}
34-
35-
static inline void
36-
ifq_clr_oactive(IOPacketQueue **ifq)
37-
{
38-
ifq_oactive = 0;
39-
}
40-
41-
static inline unsigned int
42-
ifq_is_oactive(IOPacketQueue **ifq)
43-
{
44-
return (ifq_oactive);
45-
}
46-
47-
static inline mbuf_t
48-
ifq_dequeue(IOPacketQueue **ifq)
49-
{
50-
return (*ifq)->lockDequeue();
51-
}
27+
struct _ifqueue {
28+
unsigned int ifq_oactive;
29+
IOPacketQueue *queue;
30+
};
31+
32+
void ifq_init(struct _ifqueue *ifq, struct _ifnet *ifp, unsigned int maxLen);
33+
34+
void ifq_destroy(struct _ifqueue *ifq);
35+
36+
void ifq_flush(struct _ifqueue *ifq);
37+
38+
bool ifq_empty(struct _ifqueue *ifq);
39+
40+
uint32_t ifq_len(struct _ifqueue *ifq);
41+
42+
void ifq_set_maxlen(struct _ifqueue *ifq, uint32_t maxLen);
43+
44+
void ifq_set_oactive(struct _ifqueue *ifq);
45+
46+
unsigned int ifq_is_oactive(struct _ifqueue *ifq);
47+
48+
void ifq_clr_oactive(struct _ifqueue *ifq);
49+
50+
mbuf_t ifq_dequeue(struct _ifqueue *ifq);
51+
52+
int ifq_enqueue(struct _ifqueue *ifq, mbuf_t m);
5253

5354
#endif /* _ifq_h */

itl80211/openbsd/sys/_mbuf.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#include <linux/types.h>
6060
#include <sys/_if_ether.h>
61+
#include <sys/_ifq.h>
6162
#include <sys/mbuf.h>
6263
#include <sys/kpi_mbuf.h>
6364
#include <sys/errno.h>
@@ -442,10 +443,9 @@ int if_input(struct _ifnet *ifq, struct mbuf_list *ml);
442443

443444
static inline int if_enqueue(struct _ifnet *ifq, mbuf_t m)
444445
{
445-
if (!ifq->if_snd->lockEnqueue(m)) {
446+
if (ifq_enqueue(&ifq->if_snd, m)) {
446447
XYLog("%s if_enqueue fail!!\n", __FUNCTION__);
447-
mbuf_freem(m);
448-
return -1;
448+
return -ENOSPC;
449449
}
450450
return 0;
451451
}

0 commit comments

Comments
 (0)