]> Git Repo - qemu.git/blob - slirp/if.c
slirp: Factorizing tcpiphdr structure with an union
[qemu.git] / slirp / if.c
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 "qemu/osdep.h"
9 #include <slirp.h>
10 #include "qemu/timer.h"
11
12 static void
13 ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
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
21 static void
22 ifs_remque(struct mbuf *ifm)
23 {
24         ifm->ifs_prev->ifs_next = ifm->ifs_next;
25         ifm->ifs_next->ifs_prev = ifm->ifs_prev;
26 }
27
28 void
29 if_init(Slirp *slirp)
30 {
31     slirp->if_fastq.ifq_next = slirp->if_fastq.ifq_prev = &slirp->if_fastq;
32     slirp->if_batchq.ifq_next = slirp->if_batchq.ifq_prev = &slirp->if_batchq;
33     slirp->next_m = &slirp->if_batchq;
34 }
35
36 /*
37  * if_output: Queue packet into an output queue.
38  * There are 2 output queue's, if_fastq and if_batchq.
39  * 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
43  * from the next session, etc.  Packets on the if_fastq get absolute
44  * 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  */
49 void
50 if_output(struct socket *so, struct mbuf *ifm)
51 {
52         Slirp *slirp = ifm->slirp;
53         struct mbuf *ifq;
54         int on_fastq = 1;
55
56         DEBUG_CALL("if_output");
57         DEBUG_ARG("so = %p", so);
58         DEBUG_ARG("ifm = %p", ifm);
59
60         /*
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         }
69
70         /*
71          * See if there's already a batchq list for this session.
72          * 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          */
77         for (ifq = slirp->if_batchq.ifq_prev; ifq != &slirp->if_batchq;
78              ifq = ifq->ifq_prev) {
79                 if (so == ifq->ifq_so) {
80                         /* A match! */
81                         ifm->ifq_so = so;
82                         ifs_insque(ifm, ifq->ifs_prev);
83                         goto diddit;
84                 }
85         }
86
87         /* No match, check which queue to put it on */
88         if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
89                 ifq = slirp->if_fastq.ifq_prev;
90                 on_fastq = 1;
91                 /*
92                  * Check if this packet is a part of the last
93                  * packet's session
94                  */
95                 if (ifq->ifq_so == so) {
96                         ifm->ifq_so = so;
97                         ifs_insque(ifm, ifq->ifs_prev);
98                         goto diddit;
99                 }
100         } else {
101                 ifq = slirp->if_batchq.ifq_prev;
102                 /* Set next_m if the queue was empty so far */
103                 if (slirp->next_m == &slirp->if_batchq) {
104                     slirp->next_m = ifm;
105                 }
106         }
107
108         /* Create a new doubly linked list for this session */
109         ifm->ifq_so = so;
110         ifs_init(ifm);
111         insque(ifm, ifq);
112
113 diddit:
114         if (so) {
115                 /* Update *_queued */
116                 so->so_queued++;
117                 so->so_nqueued++;
118                 /*
119                  * Check if the interactive session should be downgraded to
120                  * the batchq.  A session is downgraded if it has queued 6
121                  * packets without pausing, and at least 3 of those packets
122                  * have been sent over the link
123                  * (XXX These are arbitrary numbers, probably not optimal..)
124                  */
125                 if (on_fastq && ((so->so_nqueued >= 6) &&
126                                  (so->so_nqueued - so->so_queued) >= 3)) {
127
128                         /* Remove from current queue... */
129                         remque(ifm->ifs_next);
130
131                         /* ...And insert in the new.  That'll teach ya! */
132                         insque(ifm->ifs_next, &slirp->if_batchq);
133                 }
134         }
135
136 #ifndef FULL_BOLT
137         /*
138          * This prevents us from malloc()ing too many mbufs
139          */
140         if_start(ifm->slirp);
141 #endif
142 }
143
144 /*
145  * Send a packet
146  * We choose a packet based on its position in the output queues;
147  * If there are packets on the fastq, they are sent FIFO, before
148  * everything else.  Otherwise we choose the first packet from the
149  * batchq and send it.  the next packet chosen will be from the session
150  * after this one, then the session after that one, and so on..  So,
151  * for example, if there are 3 ftp session's fighting for bandwidth,
152  * one packet will be sent from the first session, then one packet
153  * from the second session, then one packet from the third, then back
154  * to the first, etc. etc.
155  */
156 void if_start(Slirp *slirp)
157 {
158     uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
159     bool from_batchq, next_from_batchq;
160     struct mbuf *ifm, *ifm_next, *ifqt;
161
162     DEBUG_CALL("if_start");
163
164     if (slirp->if_start_busy) {
165         return;
166     }
167     slirp->if_start_busy = true;
168
169     if (slirp->if_fastq.ifq_next != &slirp->if_fastq) {
170         ifm_next = slirp->if_fastq.ifq_next;
171         next_from_batchq = false;
172     } else if (slirp->next_m != &slirp->if_batchq) {
173         /* Nothing on fastq, pick up from batchq via next_m */
174         ifm_next = slirp->next_m;
175         next_from_batchq = true;
176     } else {
177         ifm_next = NULL;
178     }
179
180     while (ifm_next) {
181         ifm = ifm_next;
182         from_batchq = next_from_batchq;
183
184         ifm_next = ifm->ifq_next;
185         if (ifm_next == &slirp->if_fastq) {
186             /* No more packets in fastq, switch to batchq */
187             ifm_next = slirp->next_m;
188             next_from_batchq = true;
189         }
190         if (ifm_next == &slirp->if_batchq) {
191             /* end of batchq */
192             ifm_next = NULL;
193         }
194
195         /* Try to send packet unless it already expired */
196         if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
197             /* Packet is delayed due to pending ARP or NDP resolution */
198             continue;
199         }
200
201         if (ifm == slirp->next_m) {
202             /* Set which packet to send on next iteration */
203             slirp->next_m = ifm->ifq_next;
204         }
205
206         /* Remove it from the queue */
207         ifqt = ifm->ifq_prev;
208         remque(ifm);
209
210         /* If there are more packets for this session, re-queue them */
211         if (ifm->ifs_next != ifm) {
212             struct mbuf *next = ifm->ifs_next;
213
214             insque(next, ifqt);
215             ifs_remque(ifm);
216
217             if (!from_batchq) {
218                 /* Next packet in fastq is from the same session */
219                 ifm_next = next;
220                 next_from_batchq = false;
221             } else if (slirp->next_m == &slirp->if_batchq) {
222                 /* Set next_m and ifm_next if the session packet is now the
223                  * only one on batchq */
224                 slirp->next_m = ifm_next = next;
225             }
226         }
227
228         /* Update so_queued */
229         if (ifm->ifq_so && --ifm->ifq_so->so_queued == 0) {
230             /* If there's no more queued, reset nqueued */
231             ifm->ifq_so->so_nqueued = 0;
232         }
233
234         m_free(ifm);
235     }
236
237     slirp->if_start_busy = false;
238 }
This page took 0.036446 seconds and 4 git commands to generate.