Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 3 | * Copyright (c) 2009 Red Hat, Inc. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | * of this software and associated documentation files (the "Software"), to deal |
| 7 | * in the Software without restriction, including without limitation the rights |
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | * copies of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | * THE SOFTWARE. |
| 22 | */ |
| 23 | |
Mark McLoughlin | e1144d0 | 2009-10-23 17:52:16 +0100 | [diff] [blame] | 24 | #include "net/queue.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 25 | #include "qemu/queue.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 26 | #include "net/net.h" |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 27 | |
| 28 | /* The delivery handler may only return zero if it will call |
| 29 | * qemu_net_queue_flush() when it determines that it is once again able |
| 30 | * to deliver packets. It must also call qemu_net_queue_purge() in its |
| 31 | * cleanup path. |
| 32 | * |
| 33 | * If a sent callback is provided to send(), the caller must handle a |
| 34 | * zero return from the delivery handler by not sending any more packets |
| 35 | * until we have invoked the callback. Only in that case will we queue |
| 36 | * the packet. |
| 37 | * |
| 38 | * If a sent callback isn't provided, we just drop the packet to avoid |
| 39 | * unbounded queueing. |
| 40 | */ |
| 41 | |
| 42 | struct NetPacket { |
| 43 | QTAILQ_ENTRY(NetPacket) entry; |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 44 | NetClientState *sender; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 45 | unsigned flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 46 | int size; |
| 47 | NetPacketSent *sent_cb; |
| 48 | uint8_t data[0]; |
| 49 | }; |
| 50 | |
| 51 | struct NetQueue { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 52 | void *opaque; |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 53 | uint32_t nq_maxlen; |
| 54 | uint32_t nq_count; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame^] | 55 | NetQueueDeliverFunc *deliver; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 56 | |
| 57 | QTAILQ_HEAD(packets, NetPacket) packets; |
| 58 | |
| 59 | unsigned delivering : 1; |
| 60 | }; |
| 61 | |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame^] | 62 | NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 63 | { |
| 64 | NetQueue *queue; |
| 65 | |
Markus Armbruster | 58889fe | 2014-12-04 14:28:17 +0100 | [diff] [blame] | 66 | queue = g_new0(NetQueue, 1); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 67 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 68 | queue->opaque = opaque; |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 69 | queue->nq_maxlen = 10000; |
| 70 | queue->nq_count = 0; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame^] | 71 | queue->deliver = deliver; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 72 | |
| 73 | QTAILQ_INIT(&queue->packets); |
| 74 | |
| 75 | queue->delivering = 0; |
| 76 | |
| 77 | return queue; |
| 78 | } |
| 79 | |
| 80 | void qemu_del_net_queue(NetQueue *queue) |
| 81 | { |
| 82 | NetPacket *packet, *next; |
| 83 | |
| 84 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 85 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 86 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 89 | g_free(queue); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 92 | static void qemu_net_queue_append(NetQueue *queue, |
| 93 | NetClientState *sender, |
| 94 | unsigned flags, |
| 95 | const uint8_t *buf, |
| 96 | size_t size, |
| 97 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 98 | { |
| 99 | NetPacket *packet; |
| 100 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 101 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 102 | return; /* drop if queue full and no callback */ |
| 103 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 104 | packet = g_malloc(sizeof(NetPacket) + size); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 105 | packet->sender = sender; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 106 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 107 | packet->size = size; |
| 108 | packet->sent_cb = sent_cb; |
| 109 | memcpy(packet->data, buf, size); |
| 110 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 111 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 112 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 115 | static void qemu_net_queue_append_iov(NetQueue *queue, |
| 116 | NetClientState *sender, |
| 117 | unsigned flags, |
| 118 | const struct iovec *iov, |
| 119 | int iovcnt, |
| 120 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 121 | { |
| 122 | NetPacket *packet; |
| 123 | size_t max_len = 0; |
| 124 | int i; |
| 125 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 126 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 127 | return; /* drop if queue full and no callback */ |
| 128 | } |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 129 | for (i = 0; i < iovcnt; i++) { |
| 130 | max_len += iov[i].iov_len; |
| 131 | } |
| 132 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 133 | packet = g_malloc(sizeof(NetPacket) + max_len); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 134 | packet->sender = sender; |
| 135 | packet->sent_cb = sent_cb; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 136 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 137 | packet->size = 0; |
| 138 | |
| 139 | for (i = 0; i < iovcnt; i++) { |
| 140 | size_t len = iov[i].iov_len; |
| 141 | |
| 142 | memcpy(packet->data + packet->size, iov[i].iov_base, len); |
| 143 | packet->size += len; |
| 144 | } |
| 145 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 146 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 147 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static ssize_t qemu_net_queue_deliver(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 151 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 152 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 153 | const uint8_t *data, |
| 154 | size_t size) |
| 155 | { |
| 156 | ssize_t ret = -1; |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 157 | struct iovec iov = { |
| 158 | .iov_base = (void *)data, |
| 159 | .iov_len = size |
| 160 | }; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 161 | |
| 162 | queue->delivering = 1; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame^] | 163 | ret = queue->deliver(sender, flags, &iov, 1, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 164 | queue->delivering = 0; |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 170 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 171 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 172 | const struct iovec *iov, |
| 173 | int iovcnt) |
| 174 | { |
| 175 | ssize_t ret = -1; |
| 176 | |
| 177 | queue->delivering = 1; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame^] | 178 | ret = queue->deliver(sender, flags, iov, iovcnt, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 179 | queue->delivering = 0; |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | ssize_t qemu_net_queue_send(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 185 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 186 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 187 | const uint8_t *data, |
| 188 | size_t size, |
| 189 | NetPacketSent *sent_cb) |
| 190 | { |
| 191 | ssize_t ret; |
| 192 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 193 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 194 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
| 195 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 198 | ret = qemu_net_queue_deliver(queue, sender, flags, data, size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 199 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 200 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | qemu_net_queue_flush(queue); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | ssize_t qemu_net_queue_send_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 210 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 211 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 212 | const struct iovec *iov, |
| 213 | int iovcnt, |
| 214 | NetPacketSent *sent_cb) |
| 215 | { |
| 216 | ssize_t ret; |
| 217 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 218 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 219 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
| 220 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 223 | ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 224 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 225 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | qemu_net_queue_flush(queue); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 234 | void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 235 | { |
| 236 | NetPacket *packet, *next; |
| 237 | |
| 238 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 239 | if (packet->sender == from) { |
| 240 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 241 | queue->nq_count--; |
Michael S. Tsirkin | 07d8084 | 2014-09-04 11:39:10 +0300 | [diff] [blame] | 242 | if (packet->sent_cb) { |
| 243 | packet->sent_cb(packet->sender, 0); |
| 244 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 245 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 250 | bool qemu_net_queue_flush(NetQueue *queue) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 251 | { |
| 252 | while (!QTAILQ_EMPTY(&queue->packets)) { |
| 253 | NetPacket *packet; |
| 254 | int ret; |
| 255 | |
| 256 | packet = QTAILQ_FIRST(&queue->packets); |
| 257 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 258 | queue->nq_count--; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 259 | |
| 260 | ret = qemu_net_queue_deliver(queue, |
| 261 | packet->sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 262 | packet->flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 263 | packet->data, |
| 264 | packet->size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 265 | if (ret == 0) { |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 266 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 267 | QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 268 | return false; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (packet->sent_cb) { |
| 272 | packet->sent_cb(packet->sender, ret); |
| 273 | } |
| 274 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 275 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 276 | } |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 277 | return true; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 278 | } |