blob: 51ae0d0e9a38f665889c9e97d3f9cb372979df10 [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
3 *
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
7
Peter Maydell7df74822016-01-29 17:49:59 +00008#include "qemu/osdep.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +02009#include "slirp.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010010#include "qemu/timer.h"
bellardf0cbd3e2004-04-22 00:10:48 +000011
blueswir1674bb262008-09-30 18:18:27 +000012static void
blueswir1a5f1b962008-08-17 20:21:51 +000013ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
bellardf0cbd3e2004-04-22 00:10:48 +000014{
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
blueswir1674bb262008-09-30 18:18:27 +000021static void
blueswir1a5f1b962008-08-17 20:21:51 +000022ifs_remque(struct mbuf *ifm)
bellardf0cbd3e2004-04-22 00:10:48 +000023{
24 ifm->ifs_prev->ifs_next = ifm->ifs_next;
25 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
26}
27
28void
Jan Kiszka460fec62009-06-24 14:42:31 +020029if_init(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000030{
Samuel Thibault67e3eee2016-02-22 22:29:21 +010031 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;
bellardf0cbd3e2004-04-22 00:10:48 +000034}
35
bellardf0cbd3e2004-04-22 00:10:48 +000036/*
37 * if_output: Queue packet into an output queue.
ths5fafdf22007-09-16 21:08:06 +000038 * There are 2 output queue's, if_fastq and if_batchq.
bellardf0cbd3e2004-04-22 00:10:48 +000039 * Each output queue is a doubly linked list of double linked lists
40 * of mbufs, each list belonging to one "session" (socket). This
41 * way, we can output packets fairly by sending one packet from each
42 * session, instead of all the packets from one session, then all packets
ths5fafdf22007-09-16 21:08:06 +000043 * from the next session, etc. Packets on the if_fastq get absolute
bellardf0cbd3e2004-04-22 00:10:48 +000044 * priority, but if one session hogs the link, it gets "downgraded"
45 * to the batchq until it runs out of packets, then it'll return
46 * to the fastq (eg. if the user does an ls -alR in a telnet session,
47 * it'll temporarily get downgraded to the batchq)
48 */
49void
blueswir1511d2b12009-03-07 15:32:56 +000050if_output(struct socket *so, struct mbuf *ifm)
bellardf0cbd3e2004-04-22 00:10:48 +000051{
Jan Kiszka460fec62009-06-24 14:42:31 +020052 Slirp *slirp = ifm->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +000053 struct mbuf *ifq;
54 int on_fastq = 1;
ths5fafdf22007-09-16 21:08:06 +000055
bellardf0cbd3e2004-04-22 00:10:48 +000056 DEBUG_CALL("if_output");
Stefan Weilecc804c2015-08-29 09:12:35 +020057 DEBUG_ARG("so = %p", so);
58 DEBUG_ARG("ifm = %p", ifm);
ths5fafdf22007-09-16 21:08:06 +000059
bellardf0cbd3e2004-04-22 00:10:48 +000060 /*
61 * First remove the mbuf from m_usedlist,
62 * since we're gonna use m_next and m_prev ourselves
63 * XXX Shouldn't need this, gotta change dtom() etc.
64 */
65 if (ifm->m_flags & M_USEDLIST) {
66 remque(ifm);
67 ifm->m_flags &= ~M_USEDLIST;
68 }
ths5fafdf22007-09-16 21:08:06 +000069
bellardf0cbd3e2004-04-22 00:10:48 +000070 /*
ths3b46e622007-09-17 08:09:54 +000071 * See if there's already a batchq list for this session.
bellardf0cbd3e2004-04-22 00:10:48 +000072 * This can include an interactive session, which should go on fastq,
73 * but gets too greedy... hence it'll be downgraded from fastq to batchq.
74 * We mustn't put this packet back on the fastq (or we'll send it out of order)
75 * XXX add cache here?
76 */
Samuel Thibault67e3eee2016-02-22 22:29:21 +010077 for (ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
78 (struct quehead *) ifq != &slirp->if_batchq;
Jan Kiszka460fec62009-06-24 14:42:31 +020079 ifq = ifq->ifq_prev) {
bellardf0cbd3e2004-04-22 00:10:48 +000080 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 }
ths5fafdf22007-09-16 21:08:06 +000087
bellardf0cbd3e2004-04-22 00:10:48 +000088 /* No match, check which queue to put it on */
89 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
Samuel Thibault67e3eee2016-02-22 22:29:21 +010090 ifq = (struct mbuf *) slirp->if_fastq.qh_rlink;
bellardf0cbd3e2004-04-22 00:10:48 +000091 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 }
Jan Kiszkad6536b22012-02-29 09:27:33 +0100101 } else {
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100102 ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
Jan Kiszkad6536b22012-02-29 09:27:33 +0100103 /* Set next_m if the queue was empty so far */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100104 if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
Jan Kiszkad6536b22012-02-29 09:27:33 +0100105 slirp->next_m = ifm;
106 }
107 }
ths5fafdf22007-09-16 21:08:06 +0000108
bellardf0cbd3e2004-04-22 00:10:48 +0000109 /* Create a new doubly linked list for this session */
110 ifm->ifq_so = so;
111 ifs_init(ifm);
112 insque(ifm, ifq);
ths5fafdf22007-09-16 21:08:06 +0000113
bellardf0cbd3e2004-04-22 00:10:48 +0000114diddit:
bellardf0cbd3e2004-04-22 00:10:48 +0000115 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 */
ths5fafdf22007-09-16 21:08:06 +0000126 if (on_fastq && ((so->so_nqueued >= 6) &&
bellardf0cbd3e2004-04-22 00:10:48 +0000127 (so->so_nqueued - so->so_queued) >= 3)) {
ths3b46e622007-09-17 08:09:54 +0000128
bellardf0cbd3e2004-04-22 00:10:48 +0000129 /* Remove from current queue... */
130 remque(ifm->ifs_next);
ths3b46e622007-09-17 08:09:54 +0000131
bellardf0cbd3e2004-04-22 00:10:48 +0000132 /* ...And insert in the new. That'll teach ya! */
Jan Kiszka460fec62009-06-24 14:42:31 +0200133 insque(ifm->ifs_next, &slirp->if_batchq);
bellardf0cbd3e2004-04-22 00:10:48 +0000134 }
135 }
136
137#ifndef FULL_BOLT
138 /*
139 * This prevents us from malloc()ing too many mbufs
140 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200141 if_start(ifm->slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000142#endif
143}
144
145/*
146 * Send a packet
Stefan Weil59b00962013-10-11 21:34:33 +0200147 * We choose a packet based on its position in the output queues;
bellardf0cbd3e2004-04-22 00:10:48 +0000148 * 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 */
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100157void if_start(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +0000158{
Alex Blighbc72ad62013-08-21 16:03:08 +0100159 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100160 bool from_batchq, next_from_batchq;
161 struct mbuf *ifm, *ifm_next, *ifqt;
ths5fafdf22007-09-16 21:08:06 +0000162
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100163 DEBUG_CALL("if_start");
ths5fafdf22007-09-16 21:08:06 +0000164
Jan Kiszka953e7f52012-03-05 19:50:39 +0100165 if (slirp->if_start_busy) {
166 return;
167 }
168 slirp->if_start_busy = true;
169
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100170 if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
171 ifm_next = (struct mbuf *) slirp->if_fastq.qh_link;
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100172 next_from_batchq = false;
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100173 } else if ((struct quehead *) slirp->next_m != &slirp->if_batchq) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100174 /* Nothing on fastq, pick up from batchq via next_m */
175 ifm_next = slirp->next_m;
176 next_from_batchq = true;
177 } else {
178 ifm_next = NULL;
179 }
180
181 while (ifm_next) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100182 ifm = ifm_next;
183 from_batchq = next_from_batchq;
184
185 ifm_next = ifm->ifq_next;
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100186 if ((struct quehead *) ifm_next == &slirp->if_fastq) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100187 /* No more packets in fastq, switch to batchq */
188 ifm_next = slirp->next_m;
189 next_from_batchq = true;
190 }
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100191 if ((struct quehead *) ifm_next == &slirp->if_batchq) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100192 /* end of batchq */
193 ifm_next = NULL;
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100194 }
Jan Kiszkab248ede2012-02-17 16:26:38 +0100195
Jan Kiszkab248ede2012-02-17 16:26:38 +0100196 /* Try to send packet unless it already expired */
197 if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100198 /* Packet is delayed due to pending ARP or NDP resolution */
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100199 continue;
Jan Kiszkab248ede2012-02-17 16:26:38 +0100200 }
201
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100202 if (ifm == slirp->next_m) {
Jan Kiszkab248ede2012-02-17 16:26:38 +0100203 /* Set which packet to send on next iteration */
204 slirp->next_m = ifm->ifq_next;
205 }
206
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100207 /* Remove it from the queue */
208 ifqt = ifm->ifq_prev;
209 remque(ifm);
ths5fafdf22007-09-16 21:08:06 +0000210
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100211 /* If there are more packets for this session, re-queue them */
212 if (ifm->ifs_next != ifm) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100213 struct mbuf *next = ifm->ifs_next;
214
215 insque(next, ifqt);
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100216 ifs_remque(ifm);
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100217
218 if (!from_batchq) {
219 /* Next packet in fastq is from the same session */
220 ifm_next = next;
221 next_from_batchq = false;
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100222 } else if ((struct quehead *) slirp->next_m == &slirp->if_batchq) {
Jan Kiszkae3078bf2012-03-06 00:00:07 +0100223 /* Set next_m and ifm_next if the session packet is now the
224 * only one on batchq */
225 slirp->next_m = ifm_next = next;
Jan Kiszkad6536b22012-02-29 09:27:33 +0100226 }
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100227 }
ths5fafdf22007-09-16 21:08:06 +0000228
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100229 /* Update so_queued */
230 if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
231 /* If there's no more queued, reset nqueued */
232 ifm->ifq_so->so_nqueued = 0;
233 }
ths5fafdf22007-09-16 21:08:06 +0000234
Jan Kiszkab248ede2012-02-17 16:26:38 +0100235 m_free(ifm);
Jan Kiszkab87ffa12012-02-17 16:35:36 +0100236 }
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200237
Jan Kiszka953e7f52012-03-05 19:50:39 +0100238 slirp->if_start_busy = false;
bellardf0cbd3e2004-04-22 00:10:48 +0000239}