]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/if.c
Delayed IP packets
[mirror_qemu.git] / slirp / if.c
CommitLineData
f0cbd3ec
FB
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>
1ab74cea 9#include "qemu-timer.h"
f0cbd3ec 10
f0cbd3ec
FB
11#define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm))
12
674bb261 13static void
a5f1b965 14ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
f0cbd3ec
FB
15{
16 ifm->ifs_next = ifmhead->ifs_next;
17 ifmhead->ifs_next = ifm;
18 ifm->ifs_prev = ifmhead;
19 ifm->ifs_next->ifs_prev = ifm;
20}
21
674bb261 22static void
a5f1b965 23ifs_remque(struct mbuf *ifm)
f0cbd3ec
FB
24{
25 ifm->ifs_prev->ifs_next = ifm->ifs_next;
26 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
27}
28
29void
460fec67 30if_init(Slirp *slirp)
f0cbd3ec 31{
460fec67
JK
32 slirp->if_fastq.ifq_next = slirp->if_fastq.ifq_prev = &slirp->if_fastq;
33 slirp->if_batchq.ifq_next = slirp->if_batchq.ifq_prev = &slirp->if_batchq;
34 slirp->next_m = &slirp->if_batchq;
f0cbd3ec
FB
35}
36
f0cbd3ec
FB
37/*
38 * if_output: Queue packet into an output queue.
5fafdf24 39 * There are 2 output queue's, if_fastq and if_batchq.
f0cbd3ec
FB
40 * Each output queue is a doubly linked list of double linked lists
41 * of mbufs, each list belonging to one "session" (socket). This
42 * way, we can output packets fairly by sending one packet from each
43 * session, instead of all the packets from one session, then all packets
5fafdf24 44 * from the next session, etc. Packets on the if_fastq get absolute
f0cbd3ec
FB
45 * priority, but if one session hogs the link, it gets "downgraded"
46 * to the batchq until it runs out of packets, then it'll return
47 * to the fastq (eg. if the user does an ls -alR in a telnet session,
48 * it'll temporarily get downgraded to the batchq)
49 */
50void
511d2b14 51if_output(struct socket *so, struct mbuf *ifm)
f0cbd3ec 52{
460fec67 53 Slirp *slirp = ifm->slirp;
f0cbd3ec
FB
54 struct mbuf *ifq;
55 int on_fastq = 1;
5fafdf24 56
f0cbd3ec
FB
57 DEBUG_CALL("if_output");
58 DEBUG_ARG("so = %lx", (long)so);
59 DEBUG_ARG("ifm = %lx", (long)ifm);
5fafdf24 60
f0cbd3ec
FB
61 /*
62 * First remove the mbuf from m_usedlist,
63 * since we're gonna use m_next and m_prev ourselves
64 * XXX Shouldn't need this, gotta change dtom() etc.
65 */
66 if (ifm->m_flags & M_USEDLIST) {
67 remque(ifm);
68 ifm->m_flags &= ~M_USEDLIST;
69 }
5fafdf24 70
f0cbd3ec 71 /*
3b46e624 72 * See if there's already a batchq list for this session.
f0cbd3ec
FB
73 * This can include an interactive session, which should go on fastq,
74 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
75 * We mustn't put this packet back on the fastq (or we'll send it out of order)
76 * XXX add cache here?
77 */
460fec67
JK
78 for (ifq = slirp->if_batchq.ifq_prev; ifq != &slirp->if_batchq;
79 ifq = ifq->ifq_prev) {
f0cbd3ec
FB
80 if (so == ifq->ifq_so) {
81 /* A match! */
82 ifm->ifq_so = so;
83 ifs_insque(ifm, ifq->ifs_prev);
84 goto diddit;
85 }
86 }
5fafdf24 87
f0cbd3ec
FB
88 /* No match, check which queue to put it on */
89 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
460fec67 90 ifq = slirp->if_fastq.ifq_prev;
f0cbd3ec
FB
91 on_fastq = 1;
92 /*
93 * Check if this packet is a part of the last
94 * packet's session
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
460fec67 102 ifq = slirp->if_batchq.ifq_prev;
5fafdf24 103
f0cbd3ec
FB
104 /* Create a new doubly linked list for this session */
105 ifm->ifq_so = so;
106 ifs_init(ifm);
107 insque(ifm, ifq);
5fafdf24 108
1ab74cea
FC
109 /* Expiration date = Now + 1 second */
110 ifm->expiration_date = qemu_get_clock_ns(rt_clock) + 1000000000ULL;
111
f0cbd3ec 112diddit:
460fec67 113 slirp->if_queued++;
5fafdf24 114
f0cbd3ec
FB
115 if (so) {
116 /* Update *_queued */
117 so->so_queued++;
118 so->so_nqueued++;
119 /*
120 * Check if the interactive session should be downgraded to
121 * the batchq. A session is downgraded if it has queued 6
122 * packets without pausing, and at least 3 of those packets
123 * have been sent over the link
124 * (XXX These are arbitrary numbers, probably not optimal..)
125 */
5fafdf24 126 if (on_fastq && ((so->so_nqueued >= 6) &&
f0cbd3ec 127 (so->so_nqueued - so->so_queued) >= 3)) {
3b46e624 128
f0cbd3ec
FB
129 /* Remove from current queue... */
130 remque(ifm->ifs_next);
3b46e624 131
f0cbd3ec 132 /* ...And insert in the new. That'll teach ya! */
460fec67 133 insque(ifm->ifs_next, &slirp->if_batchq);
f0cbd3ec
FB
134 }
135 }
136
137#ifndef FULL_BOLT
138 /*
139 * This prevents us from malloc()ing too many mbufs
140 */
460fec67 141 if_start(ifm->slirp);
f0cbd3ec
FB
142#endif
143}
144
145/*
146 * Send a packet
147 * We choose a packet based on it's position in the output queues;
148 * If there are packets on the fastq, they are sent FIFO, before
149 * everything else. Otherwise we choose the first packet from the
150 * batchq and send it. the next packet chosen will be from the session
151 * after this one, then the session after that one, and so on.. So,
152 * for example, if there are 3 ftp session's fighting for bandwidth,
153 * one packet will be sent from the first session, then one packet
154 * from the second session, then one packet from the third, then back
155 * to the first, etc. etc.
156 */
157void
460fec67 158if_start(Slirp *slirp)
f0cbd3ec 159{
1ab74cea
FC
160 int requeued = 0;
161 uint64_t now;
162
f0cbd3ec 163 struct mbuf *ifm, *ifqt;
5fafdf24 164
f0cbd3ec 165 DEBUG_CALL("if_start");
5fafdf24 166
460fec67 167 if (slirp->if_queued == 0)
f0cbd3ec 168 return; /* Nothing to do */
5fafdf24 169
f0cbd3ec
FB
170 again:
171 /* check if we can really output */
9f8bd042 172 if (!slirp_can_output(slirp->opaque))
f0cbd3ec
FB
173 return;
174
1ab74cea
FC
175 now = qemu_get_clock_ns(rt_clock);
176
f0cbd3ec
FB
177 /*
178 * See which queue to get next packet from
179 * If there's something in the fastq, select it immediately
180 */
460fec67
JK
181 if (slirp->if_fastq.ifq_next != &slirp->if_fastq) {
182 ifm = slirp->if_fastq.ifq_next;
f0cbd3ec
FB
183 } else {
184 /* Nothing on fastq, see if next_m is valid */
460fec67
JK
185 if (slirp->next_m != &slirp->if_batchq)
186 ifm = slirp->next_m;
f0cbd3ec 187 else
460fec67 188 ifm = slirp->if_batchq.ifq_next;
3b46e624 189
f0cbd3ec 190 /* Set which packet to send on next iteration */
460fec67 191 slirp->next_m = ifm->ifq_next;
f0cbd3ec
FB
192 }
193 /* Remove it from the queue */
194 ifqt = ifm->ifq_prev;
195 remque(ifm);
460fec67 196 slirp->if_queued--;
5fafdf24 197
f0cbd3ec
FB
198 /* If there are more packets for this session, re-queue them */
199 if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
200 insque(ifm->ifs_next, ifqt);
201 ifs_remque(ifm);
202 }
5fafdf24 203
f0cbd3ec
FB
204 /* Update so_queued */
205 if (ifm->ifq_so) {
206 if (--ifm->ifq_so->so_queued == 0)
207 /* If there's no more queued, reset nqueued */
208 ifm->ifq_so->so_nqueued = 0;
209 }
5fafdf24 210
1ab74cea
FC
211 if (ifm->expiration_date < now) {
212 /* Expired */
213 m_free(ifm);
214 } else {
215 /* Encapsulate the packet for sending */
216 if (if_encap(slirp, ifm)) {
217 m_free(ifm);
218 } else {
219 /* re-queue */
220 insque(ifm, ifqt);
221 requeued++;
222 }
223 }
c9a62117 224
460fec67 225 if (slirp->if_queued)
f0cbd3ec 226 goto again;
1ab74cea
FC
227
228 slirp->if_queued = requeued;
f0cbd3ec 229}