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 | |
Peter Maydell | 2744d92 | 2016-01-29 17:50:00 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Mark McLoughlin | e1144d0 | 2009-10-23 17:52:16 +0100 | [diff] [blame] | 25 | #include "net/queue.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/queue.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 27 | #include "net/net.h" |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 28 | |
| 29 | /* The delivery handler may only return zero if it will call |
| 30 | * qemu_net_queue_flush() when it determines that it is once again able |
| 31 | * to deliver packets. It must also call qemu_net_queue_purge() in its |
| 32 | * cleanup path. |
| 33 | * |
| 34 | * If a sent callback is provided to send(), the caller must handle a |
| 35 | * zero return from the delivery handler by not sending any more packets |
| 36 | * until we have invoked the callback. Only in that case will we queue |
| 37 | * the packet. |
| 38 | * |
| 39 | * If a sent callback isn't provided, we just drop the packet to avoid |
| 40 | * unbounded queueing. |
| 41 | */ |
| 42 | |
| 43 | struct NetPacket { |
| 44 | QTAILQ_ENTRY(NetPacket) entry; |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 45 | NetClientState *sender; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 46 | unsigned flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 47 | int size; |
| 48 | NetPacketSent *sent_cb; |
| 49 | uint8_t data[0]; |
| 50 | }; |
| 51 | |
| 52 | struct NetQueue { |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 53 | void *opaque; |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 54 | uint32_t nq_maxlen; |
| 55 | uint32_t nq_count; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 56 | NetQueueDeliverFunc *deliver; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 57 | |
| 58 | QTAILQ_HEAD(packets, NetPacket) packets; |
| 59 | |
| 60 | unsigned delivering : 1; |
| 61 | }; |
| 62 | |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 63 | NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 64 | { |
| 65 | NetQueue *queue; |
| 66 | |
Markus Armbruster | 58889fe | 2014-12-04 14:28:17 +0100 | [diff] [blame] | 67 | queue = g_new0(NetQueue, 1); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 68 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 69 | queue->opaque = opaque; |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 70 | queue->nq_maxlen = 10000; |
| 71 | queue->nq_count = 0; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 72 | queue->deliver = deliver; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 73 | |
| 74 | QTAILQ_INIT(&queue->packets); |
| 75 | |
| 76 | queue->delivering = 0; |
| 77 | |
| 78 | return queue; |
| 79 | } |
| 80 | |
| 81 | void qemu_del_net_queue(NetQueue *queue) |
| 82 | { |
| 83 | NetPacket *packet, *next; |
| 84 | |
| 85 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 86 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 87 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 90 | g_free(queue); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 93 | static void qemu_net_queue_append(NetQueue *queue, |
| 94 | NetClientState *sender, |
| 95 | unsigned flags, |
| 96 | const uint8_t *buf, |
| 97 | size_t size, |
| 98 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 99 | { |
| 100 | NetPacket *packet; |
| 101 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 102 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 103 | return; /* drop if queue full and no callback */ |
| 104 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 105 | packet = g_malloc(sizeof(NetPacket) + size); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 106 | packet->sender = sender; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 107 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 108 | packet->size = size; |
| 109 | packet->sent_cb = sent_cb; |
| 110 | memcpy(packet->data, buf, size); |
| 111 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 112 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 113 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Yang Hongyang | b68c7f7 | 2015-10-07 11:52:20 +0800 | [diff] [blame] | 116 | void qemu_net_queue_append_iov(NetQueue *queue, |
| 117 | NetClientState *sender, |
| 118 | unsigned flags, |
| 119 | const struct iovec *iov, |
| 120 | int iovcnt, |
| 121 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 122 | { |
| 123 | NetPacket *packet; |
| 124 | size_t max_len = 0; |
| 125 | int i; |
| 126 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 127 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 128 | return; /* drop if queue full and no callback */ |
| 129 | } |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 130 | for (i = 0; i < iovcnt; i++) { |
| 131 | max_len += iov[i].iov_len; |
| 132 | } |
| 133 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 134 | packet = g_malloc(sizeof(NetPacket) + max_len); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 135 | packet->sender = sender; |
| 136 | packet->sent_cb = sent_cb; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 137 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 138 | packet->size = 0; |
| 139 | |
| 140 | for (i = 0; i < iovcnt; i++) { |
| 141 | size_t len = iov[i].iov_len; |
| 142 | |
| 143 | memcpy(packet->data + packet->size, iov[i].iov_base, len); |
| 144 | packet->size += len; |
| 145 | } |
| 146 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 147 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 148 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static ssize_t qemu_net_queue_deliver(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 152 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 153 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 154 | const uint8_t *data, |
| 155 | size_t size) |
| 156 | { |
| 157 | ssize_t ret = -1; |
Yang Hongyang | fefe2a7 | 2015-10-07 11:52:16 +0800 | [diff] [blame] | 158 | struct iovec iov = { |
| 159 | .iov_base = (void *)data, |
| 160 | .iov_len = size |
| 161 | }; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 162 | |
| 163 | queue->delivering = 1; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 164 | ret = queue->deliver(sender, flags, &iov, 1, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 165 | queue->delivering = 0; |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 171 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 172 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 173 | const struct iovec *iov, |
| 174 | int iovcnt) |
| 175 | { |
| 176 | ssize_t ret = -1; |
| 177 | |
| 178 | queue->delivering = 1; |
Yang Hongyang | 3e033a4 | 2015-10-07 11:52:17 +0800 | [diff] [blame] | 179 | ret = queue->deliver(sender, flags, iov, iovcnt, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 180 | queue->delivering = 0; |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | ssize_t qemu_net_queue_send(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 186 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 187 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 188 | const uint8_t *data, |
| 189 | size_t size, |
| 190 | NetPacketSent *sent_cb) |
| 191 | { |
| 192 | ssize_t ret; |
| 193 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 194 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 195 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
| 196 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 199 | ret = qemu_net_queue_deliver(queue, sender, flags, data, size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 200 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 201 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | qemu_net_queue_flush(queue); |
| 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | ssize_t qemu_net_queue_send_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 211 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 212 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 213 | const struct iovec *iov, |
| 214 | int iovcnt, |
| 215 | NetPacketSent *sent_cb) |
| 216 | { |
| 217 | ssize_t ret; |
| 218 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 219 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 220 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
| 221 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 224 | ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 225 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 226 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | qemu_net_queue_flush(queue); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 235 | void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 236 | { |
| 237 | NetPacket *packet, *next; |
| 238 | |
| 239 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 240 | if (packet->sender == from) { |
| 241 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 242 | queue->nq_count--; |
Michael S. Tsirkin | 07d8084 | 2014-09-04 11:39:10 +0300 | [diff] [blame] | 243 | if (packet->sent_cb) { |
| 244 | packet->sent_cb(packet->sender, 0); |
| 245 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 246 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 251 | bool qemu_net_queue_flush(NetQueue *queue) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 252 | { |
| 253 | while (!QTAILQ_EMPTY(&queue->packets)) { |
| 254 | NetPacket *packet; |
| 255 | int ret; |
| 256 | |
| 257 | packet = QTAILQ_FIRST(&queue->packets); |
| 258 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 259 | queue->nq_count--; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 260 | |
| 261 | ret = qemu_net_queue_deliver(queue, |
| 262 | packet->sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 263 | packet->flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 264 | packet->data, |
| 265 | packet->size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 266 | if (ret == 0) { |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 267 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 268 | QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 269 | return false; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (packet->sent_cb) { |
| 273 | packet->sent_cb(packet->sender, ret); |
| 274 | } |
| 275 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 276 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 277 | } |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 278 | return true; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 279 | } |