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