Statistics
| Branch: | Revision:

root / slirp / if.c @ 79e7e937

History | View | Annotate | Download (5.8 kB)

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 <slirp.h>
9
#include "qemu-timer.h"
10

    
11
static void
12
ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead)
13
{
14
        ifm->ifs_next = ifmhead->ifs_next;
15
        ifmhead->ifs_next = ifm;
16
        ifm->ifs_prev = ifmhead;
17
        ifm->ifs_next->ifs_prev = ifm;
18
}
19

    
20
static void
21
ifs_remque(struct mbuf *ifm)
22
{
23
        ifm->ifs_prev->ifs_next = ifm->ifs_next;
24
        ifm->ifs_next->ifs_prev = ifm->ifs_prev;
25
}
26

    
27
void
28
if_init(Slirp *slirp)
29
{
30
    slirp->if_fastq.ifq_next = slirp->if_fastq.ifq_prev = &slirp->if_fastq;
31
    slirp->if_batchq.ifq_next = slirp->if_batchq.ifq_prev = &slirp->if_batchq;
32
    slirp->next_m = &slirp->if_batchq;
33
}
34

    
35
/*
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)
47
 */
48
void
49
if_output(struct socket *so, struct mbuf *ifm)
50
{
51
        Slirp *slirp = ifm->slirp;
52
        struct mbuf *ifq;
53
        int on_fastq = 1;
54

    
55
        DEBUG_CALL("if_output");
56
        DEBUG_ARG("so = %lx", (long)so);
57
        DEBUG_ARG("ifm = %lx", (long)ifm);
58

    
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
        }
68

    
69
        /*
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?
75
         */
76
        for (ifq = slirp->if_batchq.ifq_prev; ifq != &slirp->if_batchq;
77
             ifq = ifq->ifq_prev) {
78
                if (so == ifq->ifq_so) {
79
                        /* A match! */
80
                        ifm->ifq_so = so;
81
                        ifs_insque(ifm, ifq->ifs_prev);
82
                        goto diddit;
83
                }
84
        }
85

    
86
        /* No match, check which queue to put it on */
87
        if (so && (so->so_iptos & IPTOS_LOWDELAY)) {
88
                ifq = slirp->if_fastq.ifq_prev;
89
                on_fastq = 1;
90
                /*
91
                 * Check if this packet is a part of the last
92
                 * packet's session
93
                 */
94
                if (ifq->ifq_so == so) {
95
                        ifm->ifq_so = so;
96
                        ifs_insque(ifm, ifq->ifs_prev);
97
                        goto diddit;
98
                }
99
        } else
100
                ifq = slirp->if_batchq.ifq_prev;
101

    
102
        /* Create a new doubly linked list for this session */
103
        ifm->ifq_so = so;
104
        ifs_init(ifm);
105
        insque(ifm, ifq);
106

    
107
diddit:
108
        slirp->if_queued++;
109

    
110
        if (so) {
111
                /* Update *_queued */
112
                so->so_queued++;
113
                so->so_nqueued++;
114
                /*
115
                 * Check if the interactive session should be downgraded to
116
                 * the batchq.  A session is downgraded if it has queued 6
117
                 * packets without pausing, and at least 3 of those packets
118
                 * have been sent over the link
119
                 * (XXX These are arbitrary numbers, probably not optimal..)
120
                 */
121
                if (on_fastq && ((so->so_nqueued >= 6) &&
122
                                 (so->so_nqueued - so->so_queued) >= 3)) {
123

    
124
                        /* Remove from current queue... */
125
                        remque(ifm->ifs_next);
126

    
127
                        /* ...And insert in the new.  That'll teach ya! */
128
                        insque(ifm->ifs_next, &slirp->if_batchq);
129
                }
130
        }
131

    
132
#ifndef FULL_BOLT
133
        /*
134
         * This prevents us from malloc()ing too many mbufs
135
         */
136
        if_start(ifm->slirp);
137
#endif
138
}
139

    
140
/*
141
 * Send a packet
142
 * We choose a packet based on it's position in the output queues;
143
 * If there are packets on the fastq, they are sent FIFO, before
144
 * everything else.  Otherwise we choose the first packet from the
145
 * batchq and send it.  the next packet chosen will be from the session
146
 * after this one, then the session after that one, and so on..  So,
147
 * for example, if there are 3 ftp session's fighting for bandwidth,
148
 * one packet will be sent from the first session, then one packet
149
 * from the second session, then one packet from the third, then back
150
 * to the first, etc. etc.
151
 */
152
void
153
if_start(Slirp *slirp)
154
{
155
    uint64_t now = qemu_get_clock_ns(rt_clock);
156
    int requeued = 0;
157
        struct mbuf *ifm, *ifqt;
158

    
159
        DEBUG_CALL("if_start");
160

    
161
        if (slirp->if_queued == 0)
162
           return; /* Nothing to do */
163

    
164
 again:
165
        /* check if we can really output */
166
        if (!slirp_can_output(slirp->opaque))
167
            return;
168

    
169
        /*
170
         * See which queue to get next packet from
171
         * If there's something in the fastq, select it immediately
172
         */
173
        if (slirp->if_fastq.ifq_next != &slirp->if_fastq) {
174
                ifm = slirp->if_fastq.ifq_next;
175
        } else {
176
                /* Nothing on fastq, see if next_m is valid */
177
                if (slirp->next_m != &slirp->if_batchq)
178
                   ifm = slirp->next_m;
179
                else
180
                   ifm = slirp->if_batchq.ifq_next;
181

    
182
                /* Set which packet to send on next iteration */
183
                slirp->next_m = ifm->ifq_next;
184
        }
185
        /* Remove it from the queue */
186
        ifqt = ifm->ifq_prev;
187
        remque(ifm);
188
        slirp->if_queued--;
189

    
190
        /* If there are more packets for this session, re-queue them */
191
        if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) {
192
                insque(ifm->ifs_next, ifqt);
193
                ifs_remque(ifm);
194
        }
195

    
196
        /* Update so_queued */
197
        if (ifm->ifq_so) {
198
                if (--ifm->ifq_so->so_queued == 0)
199
                   /* If there's no more queued, reset nqueued */
200
                   ifm->ifq_so->so_nqueued = 0;
201
        }
202

    
203
        if (ifm->expiration_date < now) {
204
            /* Expired */
205
            m_free(ifm);
206
        } else {
207
            /* Encapsulate the packet for sending */
208
            if (if_encap(slirp, ifm)) {
209
                m_free(ifm);
210
            } else {
211
                /* re-queue */
212
                insque(ifm, ifqt);
213
                requeued++;
214
            }
215
        }
216

    
217
        if (slirp->if_queued)
218
           goto again;
219

    
220
        slirp->if_queued = requeued;
221
}