spapr: move the interrupt presenters under machine_data
[qemu/ar7.git] / slirp / if.c
blob73e3705740f166d881008a8e067e77d6fcfda52e
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include "qemu/osdep.h"
9 #include "slirp.h"
10 #include "qemu/timer.h"
12 static void
13 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
15 ifm->ifs_next = ifmhead->ifs_next;
16 ifmhead->ifs_next = ifm;
17 ifm->ifs_prev = ifmhead;
18 ifm->ifs_next->ifs_prev = ifm;
21 static void
22 ifs_remque(struct mbuf *ifm)
24 ifm->ifs_prev->ifs_next = ifm->ifs_next;
25 ifm->ifs_next->ifs_prev = ifm->ifs_prev;
28 void
29 if_init(Slirp *slirp)
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;
36 * if_output: Queue packet into an output queue.
37 * There are 2 output queue's, if_fastq and if_batchq.
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
42 * from the next session, etc. Packets on the if_fastq get absolute
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)
48 void
49 if_output(struct socket *so, struct mbuf *ifm)
51 Slirp *slirp = ifm->slirp;
52 struct mbuf *ifq;
53 int on_fastq = 1;
55 DEBUG_CALL("if_output");
56 DEBUG_ARG("so = %p", so);
57 DEBUG_ARG("ifm = %p", ifm);
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.
64 if (ifm->m_flags & M_USEDLIST) {
65 remque(ifm);
66 ifm->m_flags &= ~M_USEDLIST;
70 * See if there's already a batchq list for this session.
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?
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;
89 /* No match, check which queue to put it on */
90 if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
91 ifq = (struct mbuf *) slirp->if_fastq.qh_rlink;
92 on_fastq = 1;
94 * Check if this packet is a part of the last
95 * packet's session
97 if (ifq->ifq_so == so) {
98 ifm->ifq_so = so;
99 ifs_insque(ifm, ifq->ifs_prev);
100 goto diddit;
102 } else {
103 ifq = (struct mbuf *) slirp->if_batchq.qh_rlink;
106 /* Create a new doubly linked list for this session */
107 ifm->ifq_so = so;
108 ifs_init(ifm);
109 insque(ifm, ifq);
111 diddit:
112 if (so) {
113 /* Update *_queued */
114 so->so_queued++;
115 so->so_nqueued++;
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..)
123 if (on_fastq && ((so->so_nqueued >= 6) &&
124 (so->so_nqueued - so->so_queued) >= 3)) {
126 /* Remove from current queue... */
127 remque(ifm->ifs_next);
129 /* ...And insert in the new. That'll teach ya! */
130 insque(ifm->ifs_next, &slirp->if_batchq);
135 * This prevents us from malloc()ing too many mbufs
137 if_start(ifm->slirp);
141 * Send one packet from each session.
142 * If there are packets on the fastq, they are sent FIFO, before
143 * everything else. Then we choose the first packet from each
144 * batchq session (socket) and send it.
145 * For example, if there are 3 ftp sessions fighting for bandwidth,
146 * one packet will be sent from the first session, then one packet
147 * from the second session, then one packet from the third.
149 void if_start(Slirp *slirp)
151 uint64_t now = slirp->cb->clock_get_ns();
152 bool from_batchq = false;
153 struct mbuf *ifm, *ifm_next, *ifqt;
155 DEBUG_CALL("if_start");
157 if (slirp->if_start_busy) {
158 return;
160 slirp->if_start_busy = true;
162 struct mbuf *batch_head = NULL;
163 if (slirp->if_batchq.qh_link != &slirp->if_batchq) {
164 batch_head = (struct mbuf *) slirp->if_batchq.qh_link;
167 if (slirp->if_fastq.qh_link != &slirp->if_fastq) {
168 ifm_next = (struct mbuf *) slirp->if_fastq.qh_link;
169 } else if (batch_head) {
170 /* Nothing on fastq, pick up from batchq */
171 ifm_next = batch_head;
172 from_batchq = true;
173 } else {
174 ifm_next = NULL;
177 while (ifm_next) {
178 ifm = ifm_next;
180 ifm_next = ifm->ifq_next;
181 if ((struct quehead *) ifm_next == &slirp->if_fastq) {
182 /* No more packets in fastq, switch to batchq */
183 ifm_next = batch_head;
184 from_batchq = true;
186 if ((struct quehead *) ifm_next == &slirp->if_batchq) {
187 /* end of batchq */
188 ifm_next = NULL;
191 /* Try to send packet unless it already expired */
192 if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
193 /* Packet is delayed due to pending ARP or NDP resolution */
194 continue;
197 /* Remove it from the queue */
198 ifqt = ifm->ifq_prev;
199 remque(ifm);
201 /* If there are more packets for this session, re-queue them */
202 if (ifm->ifs_next != ifm) {
203 struct mbuf *next = ifm->ifs_next;
205 insque(next, ifqt);
206 ifs_remque(ifm);
207 if (!from_batchq) {
208 ifm_next = next;
212 /* Update so_queued */
213 if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
214 /* If there's no more queued, reset nqueued */
215 ifm->ifq_so->so_nqueued = 0;
218 m_free(ifm);
221 slirp->if_start_busy = false;