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