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