1 | /* SPDX-License-Identifier: BSD-3-Clause */
|
---|
2 | /*
|
---|
3 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include "slirp.h"
|
---|
7 |
|
---|
8 | static void ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
|
---|
9 | {
|
---|
10 | ifm->ifs_next = ifmhead->ifs_next;
|
---|
11 | ifmhead->ifs_next = ifm;
|
---|
12 | ifm->ifs_prev = ifmhead;
|
---|
13 | ifm->ifs_next->ifs_prev = ifm;
|
---|
14 | }
|
---|
15 |
|
---|
16 | static void ifs_remque(struct mbuf *ifm)
|
---|
17 | {
|
---|
18 | ifm->ifs_prev->ifs_next = ifm->ifs_next;
|
---|
19 | ifm->ifs_next->ifs_prev = ifm->ifs_prev;
|
---|
20 | }
|
---|
21 |
|
---|
22 | void if_init(Slirp *slirp)
|
---|
23 | {
|
---|
24 | slirp->if_fastq.qh_link = slirp->if_fastq.qh_rlink = &slirp->if_fastq;
|
---|
25 | slirp->if_batchq.qh_link = slirp->if_batchq.qh_rlink = &slirp->if_batchq;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * if_output: Queue packet into an output queue.
|
---|
30 | * There are 2 output queue's, if_fastq and if_batchq.
|
---|
31 | * Each output queue is a doubly linked list of double linked lists
|
---|
32 | * of mbufs, each list belonging to one "session" (socket). This
|
---|
33 | * way, we can output packets fairly by sending one packet from each
|
---|
34 | * session, instead of all the packets from one session, then all packets
|
---|
35 | * from the next session, etc. Packets on the if_fastq get absolute
|
---|
36 | * priority, but if one session hogs the link, it gets "downgraded"
|
---|
37 | * to the batchq until it runs out of packets, then it'll return
|
---|
38 | * to the fastq (eg. if the user does an ls -alR in a telnet session,
|
---|
39 | * it'll temporarily get downgraded to the batchq)
|
---|
40 | */
|
---|
41 | void if_output(struct socket *so, struct mbuf *ifm)
|
---|
42 | {
|
---|
43 | Slirp *slirp = ifm->slirp;
|
---|
44 | M_DUP_DEBUG(slirp, ifm, 0, 0);
|
---|
45 |
|
---|
46 | struct mbuf *ifq;
|
---|
47 | int on_fastq = 1;
|
---|
48 |
|
---|
49 | DEBUG_CALL("if_output");
|
---|
50 | DEBUG_ARG("so = %p", so);
|
---|
51 | DEBUG_ARG("ifm = %p", ifm);
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * First remove the mbuf from m_usedlist,
|
---|
55 | * since we're gonna use m_next and m_prev ourselves
|
---|
56 | * XXX Shouldn't need this, gotta change dtom() etc.
|
---|
57 | */
|
---|
58 | if (ifm->m_flags & M_USEDLIST) {
|
---|
59 | slirp_remque(ifm);
|
---|
60 | ifm->m_flags &= ~M_USEDLIST;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * See if there's already a batchq list for this session.
|
---|
65 | * This can include an interactive session, which should go on fastq,
|
---|
66 | * but gets too greedy... hence it'll be downgraded from fastq to batchq.
|
---|
67 | * We mustn't put this packet back on the fastq (or we'll send it out of
|
---|
68 | * order)
|
---|
69 | * XXX add cache here?
|
---|
70 | */
|
---|
71 | if (so) {
|
---|
72 | for (ifq = (struct mbuf *)slirp->if_batchq.qh_rlink;
|
---|
73 | (struct slirp_quehead *)ifq != &slirp->if_batchq;
|
---|
74 | ifq = ifq->ifq_prev) {
|
---|
75 | if (so == ifq->ifq_so) {
|
---|
76 | /* A match! */
|
---|
77 | ifm->ifq_so = so;
|
---|
78 | ifs_insque(ifm, ifq->ifs_prev);
|
---|
79 | goto diddit;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* No match, check which queue to put it on */
|
---|
85 | if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
|
---|
86 | ifq = (struct mbuf *)slirp->if_fastq.qh_rlink;
|
---|
87 | on_fastq = 1;
|
---|
88 | /*
|
---|
89 | * Check if this packet is a part of the last
|
---|
90 | * packet's session
|
---|
91 | */
|
---|
92 | if (ifq->ifq_so == so) {
|
---|
93 | ifm->ifq_so = so;
|
---|
94 | ifs_insque(ifm, ifq->ifs_prev);
|
---|
95 | goto diddit;
|
---|
96 | }
|
---|
97 | } else {
|
---|
98 | ifq = (struct mbuf *)slirp->if_batchq.qh_rlink;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Create a new doubly linked list for this session */
|
---|
102 | ifm->ifq_so = so;
|
---|
103 | ifs_init(ifm);
|
---|
104 | slirp_insque(ifm, ifq);
|
---|
105 |
|
---|
106 | diddit:
|
---|
107 | if (so) {
|
---|
108 | /* Update *_queued */
|
---|
109 | so->so_queued++;
|
---|
110 | so->so_nqueued++;
|
---|
111 | /*
|
---|
112 | * Check if the interactive session should be downgraded to
|
---|
113 | * the batchq. A session is downgraded if it has queued 6
|
---|
114 | * packets without pausing, and at least 3 of those packets
|
---|
115 | * have been sent over the link
|
---|
116 | * (XXX These are arbitrary numbers, probably not optimal..)
|
---|
117 | */
|
---|
118 | if (on_fastq &&
|
---|
119 | ((so->so_nqueued >= 6) && (so->so_nqueued - so->so_queued) >= 3)) {
|
---|
120 | /* Remove from current queue... */
|
---|
121 | slirp_remque(ifm->ifs_next);
|
---|
122 |
|
---|
123 | /* ...And insert in the new. That'll teach ya! */
|
---|
124 | slirp_insque(ifm->ifs_next, &slirp->if_batchq);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * This prevents us from malloc()ing too many mbufs
|
---|
130 | */
|
---|
131 | if_start(ifm->slirp);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Send one packet from each session.
|
---|
136 | * If there are packets on the fastq, they are sent FIFO, before
|
---|
137 | * everything else. Then we choose the first packet from each
|
---|
138 | * batchq session (socket) and send it.
|
---|
139 | * For example, if there are 3 ftp sessions fighting for bandwidth,
|
---|
140 | * one packet will be sent from the first session, then one packet
|
---|
141 | * from the second session, then one packet from the third.
|
---|
142 | */
|
---|
143 | void if_start(Slirp *slirp)
|
---|
144 | {
|
---|
145 | uint64_t now = slirp->cb->clock_get_ns(slirp->opaque);
|
---|
146 | bool from_batchq = false;
|
---|
147 | struct mbuf *ifm, *ifm_next, *ifqt;
|
---|
148 |
|
---|
149 | DEBUG_VERBOSE_CALL("if_start");
|
---|
150 |
|
---|
151 | if (slirp->if_start_busy) {
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | slirp->if_start_busy = true;
|
---|
155 |
|
---|
156 | struct mbuf *batch_head = NULL;
|
---|
157 | if (slirp->if_batchq.qh_link != &slirp->if_batchq) {
|
---|
158 | batch_head = (struct mbuf *)slirp->if_batchq.qh_link;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
|
---|
162 | ifm_next = (struct mbuf *)slirp->if_fastq.qh_link;
|
---|
163 | } else if (batch_head) {
|
---|
164 | /* Nothing on fastq, pick up from batchq */
|
---|
165 | ifm_next = batch_head;
|
---|
166 | from_batchq = true;
|
---|
167 | } else {
|
---|
168 | ifm_next = NULL;
|
---|
169 | }
|
---|
170 |
|
---|
171 | while (ifm_next) {
|
---|
172 | ifm = ifm_next;
|
---|
173 |
|
---|
174 | ifm_next = ifm->ifq_next;
|
---|
175 | if ((struct slirp_quehead *)ifm_next == &slirp->if_fastq) {
|
---|
176 | /* No more packets in fastq, switch to batchq */
|
---|
177 | ifm_next = batch_head;
|
---|
178 | from_batchq = true;
|
---|
179 | }
|
---|
180 | if ((struct slirp_quehead *)ifm_next == &slirp->if_batchq) {
|
---|
181 | /* end of batchq */
|
---|
182 | ifm_next = NULL;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* Try to send packet unless it already expired */
|
---|
186 | if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
|
---|
187 | /* Packet is delayed due to pending ARP or NDP resolution */
|
---|
188 | continue;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* Remove it from the queue */
|
---|
192 | ifqt = ifm->ifq_prev;
|
---|
193 | slirp_remque(ifm);
|
---|
194 |
|
---|
195 | /* If there are more packets for this session, re-queue them */
|
---|
196 | if (ifm->ifs_next != ifm) {
|
---|
197 | struct mbuf *next = ifm->ifs_next;
|
---|
198 |
|
---|
199 | slirp_insque(next, ifqt);
|
---|
200 | ifs_remque(ifm);
|
---|
201 | if (!from_batchq) {
|
---|
202 | ifm_next = next;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Update so_queued */
|
---|
207 | if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
|
---|
208 | /* If there's no more queued, reset nqueued */
|
---|
209 | ifm->ifq_so->so_nqueued = 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | m_free(ifm);
|
---|
213 | }
|
---|
214 |
|
---|
215 | slirp->if_start_busy = false;
|
---|
216 | }
|
---|