Statistics
| Branch: | Revision:

root / slirp / if.c @ 90d7416a

History | View | Annotate | Download (6 kB)

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