]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/if.c
pcie: set link state inactive/active after hot unplug/plug
[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
7df7482b 8#include "qemu/osdep.h"
a9c94277 9#include "slirp.h"
1de7afc9 10#include "qemu/timer.h"
f0cbd3ec 11
674bb261 12static void
a5f1b965 13ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
f0cbd3ec
FB
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
674bb261 21static void
a5f1b965 22ifs_remque(struct mbuf *ifm)
f0cbd3ec
FB
23{
24 ifm->ifs_prev->ifs_next = ifm->ifs_next;
25 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
26}
27
28void
460fec67 29if_init(Slirp *slirp)
f0cbd3ec 30{
67e3eee4
ST
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;
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 55 DEBUG_CALL("if_output");
ecc804ca
SW
56 DEBUG_ARG("so = %p", so);
57 DEBUG_ARG("ifm = %p", 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 */
13146a83
KC
76 if (so) {
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 /* A match! */
82 ifm->ifq_so = so;
83 ifs_insque(ifm, ifq->ifs_prev);
84 goto diddit;
85 }
f0cbd3ec
FB
86 }
87 }
5fafdf24 88
f0cbd3ec
FB
89 /* No match, check which queue to put it on */
90 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
67e3eee4 91 ifq = (struct mbuf *) slirp->if_fastq.qh_rlink;
f0cbd3ec
FB
92 on_fastq = 1;
93 /*
94 * Check if this packet is a part of the last
95 * packet's session
96 */
97 if (ifq->ifq_so == so) {
98 ifm->ifq_so = so;
99 ifs_insque(ifm, ifq->ifs_prev);
100 goto diddit;
101 }
d6536b2c 102 } else {
67e3eee4 103 ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
d6536b2c 104 }
5fafdf24 105
f0cbd3ec
FB
106 /* Create a new doubly linked list for this session */
107 ifm->ifq_so = so;
108 ifs_init(ifm);
109 insque(ifm, ifq);
5fafdf24 110
f0cbd3ec 111diddit:
f0cbd3ec
FB
112 if (so) {
113 /* Update *_queued */
114 so->so_queued++;
115 so->so_nqueued++;
116 /*
117 * Check if the interactive session should be downgraded to
118 * the batchq. A session is downgraded if it has queued 6
119 * packets without pausing, and at least 3 of those packets
120 * have been sent over the link
121 * (XXX These are arbitrary numbers, probably not optimal..)
122 */
5fafdf24 123 if (on_fastq && ((so->so_nqueued >= 6) &&
f0cbd3ec 124 (so->so_nqueued - so->so_queued) >= 3)) {
3b46e624 125
f0cbd3ec
FB
126 /* Remove from current queue... */
127 remque(ifm->ifs_next);
3b46e624 128
f0cbd3ec 129 /* ...And insert in the new. That'll teach ya! */
460fec67 130 insque(ifm->ifs_next, &slirp->if_batchq);
f0cbd3ec
FB
131 }
132 }
133
134#ifndef FULL_BOLT
135 /*
136 * This prevents us from malloc()ing too many mbufs
137 */
460fec67 138 if_start(ifm->slirp);
f0cbd3ec
FB
139#endif
140}
141
142/*
e2aad34d 143 * Send one packet from each session.
f0cbd3ec 144 * If there are packets on the fastq, they are sent FIFO, before
e2aad34d
KC
145 * everything else. Then we choose the first packet from each
146 * batchq session (socket) and send it.
147 * For example, if there are 3 ftp sessions fighting for bandwidth,
f0cbd3ec 148 * one packet will be sent from the first session, then one packet
e2aad34d 149 * from the second session, then one packet from the third.
f0cbd3ec 150 */
b87ffa16 151void if_start(Slirp *slirp)
f0cbd3ec 152{
bc72ad67 153 uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
e2aad34d 154 bool from_batchq = false;
e3078bf4 155 struct mbuf *ifm, *ifm_next, *ifqt;
5fafdf24 156
b87ffa16 157 DEBUG_CALL("if_start");
5fafdf24 158
953e7f54
JK
159 if (slirp->if_start_busy) {
160 return;
161 }
162 slirp->if_start_busy = true;
163
e2aad34d
KC
164 struct mbuf *batch_head = NULL;
165 if (slirp->if_batchq.qh_link != &slirp->if_batchq) {
166 batch_head = (struct mbuf *) slirp->if_batchq.qh_link;
167 }
168
67e3eee4
ST
169 if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
170 ifm_next = (struct mbuf *) slirp->if_fastq.qh_link;
e2aad34d
KC
171 } else if (batch_head) {
172 /* Nothing on fastq, pick up from batchq */
173 ifm_next = batch_head;
174 from_batchq = true;
e3078bf4
JK
175 } else {
176 ifm_next = NULL;
177 }
178
179 while (ifm_next) {
e3078bf4 180 ifm = ifm_next;
e3078bf4
JK
181
182 ifm_next = ifm->ifq_next;
67e3eee4 183 if ((struct quehead *) ifm_next == &slirp->if_fastq) {
e3078bf4 184 /* No more packets in fastq, switch to batchq */
e2aad34d
KC
185 ifm_next = batch_head;
186 from_batchq = true;
e3078bf4 187 }
67e3eee4 188 if ((struct quehead *) ifm_next == &slirp->if_batchq) {
e3078bf4
JK
189 /* end of batchq */
190 ifm_next = NULL;
b87ffa16 191 }
b248ede2 192
b248ede2
JK
193 /* Try to send packet unless it already expired */
194 if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
0d6ff71a 195 /* Packet is delayed due to pending ARP or NDP resolution */
b87ffa16 196 continue;
b248ede2
JK
197 }
198
b87ffa16
JK
199 /* Remove it from the queue */
200 ifqt = ifm->ifq_prev;
201 remque(ifm);
5fafdf24 202
b87ffa16
JK
203 /* If there are more packets for this session, re-queue them */
204 if (ifm->ifs_next != ifm) {
e3078bf4
JK
205 struct mbuf *next = ifm->ifs_next;
206
207 insque(next, ifqt);
b87ffa16 208 ifs_remque(ifm);
e3078bf4 209 if (!from_batchq) {
e3078bf4 210 ifm_next = next;
d6536b2c 211 }
b87ffa16 212 }
5fafdf24 213
b87ffa16
JK
214 /* Update so_queued */
215 if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
216 /* If there's no more queued, reset nqueued */
217 ifm->ifq_so->so_nqueued = 0;
218 }
5fafdf24 219
b248ede2 220 m_free(ifm);
b87ffa16 221 }
1ab74cea 222
953e7f54 223 slirp->if_start_busy = false;
f0cbd3ec 224}