1
2
3
4
5
6
7
8#include "qemu/osdep.h"
9#include "slirp.h"
10#include "qemu/timer.h"
11
12static void
13ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
14{
15 ifm->ifs_next = ifmhead->ifs_next;
16 ifmhead->ifs_next = ifm;
17 ifm->ifs_prev = ifmhead;
18 ifm->ifs_next->ifs_prev = ifm;
19}
20
21static void
22ifs_remque(struct mbuf *ifm)
23{
24 ifm->ifs_prev->ifs_next = ifm->ifs_next;
25 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
26}
27
28void
29if_init(Slirp *slirp)
30{
31 slirp->if_fastq.qh_link = slirp->if_fastq.qh_rlink = &slirp->if_fastq;
32 slirp->if_batchq.qh_link = slirp->if_batchq.qh_rlink = &slirp->if_batchq;
33 slirp->next_m = (struct mbuf *) &slirp->if_batchq;
34}
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49void
50if_output(struct socket *so, struct mbuf *ifm)
51{
52 Slirp *slirp = ifm->slirp;
53 struct mbuf *ifq;
54 int on_fastq = 1;
55
56 DEBUG_CALL("if_output");
57 DEBUG_ARG("so = %p", so);
58 DEBUG_ARG("ifm = %p", ifm);
59
60
61
62
63
64
65 if (ifm->m_flags & M_USEDLIST) {
66 remque(ifm);
67 ifm->m_flags &= ~M_USEDLIST;
68 }
69
70
71
72
73
74
75
76
77 for (ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
78 (struct quehead *) ifq != &slirp->if_batchq;
79 ifq = ifq->ifq_prev) {
80 if (so == ifq->ifq_so) {
81
82 ifm->ifq_so = so;
83 ifs_insque(ifm, ifq->ifs_prev);
84 goto diddit;
85 }
86 }
87
88
89 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
90 ifq = (struct mbuf *) slirp->if_fastq.qh_rlink;
91 on_fastq = 1;
92
93
94
95
96 if (ifq->ifq_so == so) {
97 ifm->ifq_so = so;
98 ifs_insque(ifm, ifq->ifs_prev);
99 goto diddit;
100 }
101 } else {
102 ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
103
104 if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
105 slirp->next_m = ifm;
106 }
107 }
108
109
110 ifm->ifq_so = so;
111 ifs_init(ifm);
112 insque(ifm, ifq);
113
114diddit:
115 if (so) {
116
117 so->so_queued++;
118 so->so_nqueued++;
119
120
121
122
123
124
125
126 if (on_fastq && ((so->so_nqueued >= 6) &&
127 (so->so_nqueued - so->so_queued) >= 3)) {
128
129
130 remque(ifm->ifs_next);
131
132
133 insque(ifm->ifs_next, &slirp->if_batchq);
134 }
135 }
136
137#ifndef FULL_BOLT
138
139
140
141 if_start(ifm->slirp);
142#endif
143}
144
145
146
147
148
149
150
151
152
153
154
155
156
157void if_start(Slirp *slirp)
158{
159 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
160 bool from_batchq, next_from_batchq;
161 struct mbuf *ifm, *ifm_next, *ifqt;
162
163 DEBUG_CALL("if_start");
164
165 if (slirp->if_start_busy) {
166 return;
167 }
168 slirp->if_start_busy = true;
169
170 if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
171 ifm_next = (struct mbuf *) slirp->if_fastq.qh_link;
172 next_from_batchq = false;
173 } else if ((struct quehead *) slirp->next_m != &slirp->if_batchq) {
174
175 ifm_next = slirp->next_m;
176 next_from_batchq = true;
177 } else {
178 ifm_next = NULL;
179 }
180
181 while (ifm_next) {
182 ifm = ifm_next;
183 from_batchq = next_from_batchq;
184
185 ifm_next = ifm->ifq_next;
186 if ((struct quehead *) ifm_next == &slirp->if_fastq) {
187
188 ifm_next = slirp->next_m;
189 next_from_batchq = true;
190 }
191 if ((struct quehead *) ifm_next == &slirp->if_batchq) {
192
193 ifm_next = NULL;
194 }
195
196
197 if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
198
199 continue;
200 }
201
202 if (ifm == slirp->next_m) {
203
204 slirp->next_m = ifm->ifq_next;
205 }
206
207
208 ifqt = ifm->ifq_prev;
209 remque(ifm);
210
211
212 if (ifm->ifs_next != ifm) {
213 struct mbuf *next = ifm->ifs_next;
214
215 insque(next, ifqt);
216 ifs_remque(ifm);
217
218 if (!from_batchq) {
219
220 ifm_next = next;
221 next_from_batchq = false;
222 } else if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
223
224
225 slirp->next_m = ifm_next = next;
226 }
227 }
228
229
230 if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
231
232 ifm->ifq_so->so_nqueued = 0;
233 }
234
235 m_free(ifm);
236 }
237
238 slirp->if_start_busy = false;
239}
240