bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 10 | #define ifs_init(ifm) ((ifm)->ifs_next = (ifm)->ifs_prev = (ifm)) |
| 11 | |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 12 | static void |
blueswir1 | a5f1b96 | 2008-08-17 20:21:51 +0000 | [diff] [blame] | 13 | ifs_insque(struct mbuf *ifm, struct mbuf *ifmhead) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 | |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 21 | static void |
blueswir1 | a5f1b96 | 2008-08-17 20:21:51 +0000 | [diff] [blame] | 22 | ifs_remque(struct mbuf *ifm) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 23 | { |
| 24 | ifm->ifs_prev->ifs_next = ifm->ifs_next; |
| 25 | ifm->ifs_next->ifs_prev = ifm->ifs_prev; |
| 26 | } |
| 27 | |
| 28 | void |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 29 | if_init(Slirp *slirp) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 30 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 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; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 36 | /* |
| 37 | * if_output: Queue packet into an output queue. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 38 | * There are 2 output queue's, if_fastq and if_batchq. |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 43 | * from the next session, etc. Packets on the if_fastq get absolute |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 50 | if_output(struct socket *so, struct mbuf *ifm) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 51 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 52 | Slirp *slirp = ifm->slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 53 | struct mbuf *ifq; |
| 54 | int on_fastq = 1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 55 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 56 | DEBUG_CALL("if_output"); |
| 57 | DEBUG_ARG("so = %lx", (long)so); |
| 58 | DEBUG_ARG("ifm = %lx", (long)ifm); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 59 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 69 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 70 | /* |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 71 | * See if there's already a batchq list for this session. |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 | */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 77 | for (ifq = slirp->if_batchq.ifq_prev; ifq != &slirp->if_batchq; |
| 78 | ifq = ifq->ifq_prev) { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 86 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 87 | /* No match, check which queue to put it on */ |
| 88 | if (so && (so->so_iptos & IPTOS_LOWDELAY)) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 89 | ifq = slirp->if_fastq.ifq_prev; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 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 |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 101 | ifq = slirp->if_batchq.ifq_prev; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 102 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 103 | /* Create a new doubly linked list for this session */ |
| 104 | ifm->ifq_so = so; |
| 105 | ifs_init(ifm); |
| 106 | insque(ifm, ifq); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 107 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 108 | diddit: |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 109 | slirp->if_queued++; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 110 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 111 | if (so) { |
| 112 | /* Update *_queued */ |
| 113 | so->so_queued++; |
| 114 | so->so_nqueued++; |
| 115 | /* |
| 116 | * Check if the interactive session should be downgraded to |
| 117 | * the batchq. A session is downgraded if it has queued 6 |
| 118 | * packets without pausing, and at least 3 of those packets |
| 119 | * have been sent over the link |
| 120 | * (XXX These are arbitrary numbers, probably not optimal..) |
| 121 | */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 122 | if (on_fastq && ((so->so_nqueued >= 6) && |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 123 | (so->so_nqueued - so->so_queued) >= 3)) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 124 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 125 | /* Remove from current queue... */ |
| 126 | remque(ifm->ifs_next); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 127 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 128 | /* ...And insert in the new. That'll teach ya! */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 129 | insque(ifm->ifs_next, &slirp->if_batchq); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | #ifndef FULL_BOLT |
| 134 | /* |
| 135 | * This prevents us from malloc()ing too many mbufs |
| 136 | */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 137 | if_start(ifm->slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 138 | #endif |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Send a packet |
| 143 | * We choose a packet based on it's position in the output queues; |
| 144 | * If there are packets on the fastq, they are sent FIFO, before |
| 145 | * everything else. Otherwise we choose the first packet from the |
| 146 | * batchq and send it. the next packet chosen will be from the session |
| 147 | * after this one, then the session after that one, and so on.. So, |
| 148 | * for example, if there are 3 ftp session's fighting for bandwidth, |
| 149 | * one packet will be sent from the first session, then one packet |
| 150 | * from the second session, then one packet from the third, then back |
| 151 | * to the first, etc. etc. |
| 152 | */ |
| 153 | void |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 154 | if_start(Slirp *slirp) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 155 | { |
| 156 | struct mbuf *ifm, *ifqt; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 157 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 158 | DEBUG_CALL("if_start"); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 159 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 160 | if (slirp->if_queued == 0) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 161 | return; /* Nothing to do */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 162 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 163 | again: |
| 164 | /* check if we can really output */ |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 165 | if (!slirp_can_output(slirp->opaque)) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 166 | return; |
| 167 | |
| 168 | /* |
| 169 | * See which queue to get next packet from |
| 170 | * If there's something in the fastq, select it immediately |
| 171 | */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 172 | if (slirp->if_fastq.ifq_next != &slirp->if_fastq) { |
| 173 | ifm = slirp->if_fastq.ifq_next; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 174 | } else { |
| 175 | /* Nothing on fastq, see if next_m is valid */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 176 | if (slirp->next_m != &slirp->if_batchq) |
| 177 | ifm = slirp->next_m; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 178 | else |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 179 | ifm = slirp->if_batchq.ifq_next; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 180 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 181 | /* Set which packet to send on next iteration */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 182 | slirp->next_m = ifm->ifq_next; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 183 | } |
| 184 | /* Remove it from the queue */ |
| 185 | ifqt = ifm->ifq_prev; |
| 186 | remque(ifm); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 187 | slirp->if_queued--; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 188 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 189 | /* If there are more packets for this session, re-queue them */ |
| 190 | if (ifm->ifs_next != /* ifm->ifs_prev != */ ifm) { |
| 191 | insque(ifm->ifs_next, ifqt); |
| 192 | ifs_remque(ifm); |
| 193 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 194 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 195 | /* Update so_queued */ |
| 196 | if (ifm->ifq_so) { |
| 197 | if (--ifm->ifq_so->so_queued == 0) |
| 198 | /* If there's no more queued, reset nqueued */ |
| 199 | ifm->ifq_so->so_nqueued = 0; |
| 200 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 201 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 202 | /* Encapsulate the packet for sending */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 203 | if_encap(slirp, (uint8_t *)ifm->m_data, ifm->m_len); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 204 | |
bellard | c9a6211 | 2004-09-13 21:42:51 +0000 | [diff] [blame] | 205 | m_free(ifm); |
| 206 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 207 | if (slirp->if_queued) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 208 | goto again; |
| 209 | } |