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; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 55 | |
| 56 | QTAILQ_HEAD(packets, NetPacket) packets; |
| 57 | |
| 58 | unsigned delivering : 1; |
| 59 | }; |
| 60 | |
Zhi Yong Wu | 86a77c3 | 2012-07-24 16:35:17 +0100 | [diff] [blame] | 61 | NetQueue *qemu_new_net_queue(void *opaque) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 62 | { |
| 63 | NetQueue *queue; |
| 64 | |
Markus Armbruster | 58889fe | 2014-12-04 14:28:17 +0100 | [diff] [blame] | 65 | queue = g_new0(NetQueue, 1); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 66 | |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 67 | queue->opaque = opaque; |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 68 | queue->nq_maxlen = 10000; |
| 69 | queue->nq_count = 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 70 | |
| 71 | QTAILQ_INIT(&queue->packets); |
| 72 | |
| 73 | queue->delivering = 0; |
| 74 | |
| 75 | return queue; |
| 76 | } |
| 77 | |
| 78 | void qemu_del_net_queue(NetQueue *queue) |
| 79 | { |
| 80 | NetPacket *packet, *next; |
| 81 | |
| 82 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 83 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 84 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 87 | g_free(queue); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 90 | static void qemu_net_queue_append(NetQueue *queue, |
| 91 | NetClientState *sender, |
| 92 | unsigned flags, |
| 93 | const uint8_t *buf, |
| 94 | size_t size, |
| 95 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 96 | { |
| 97 | NetPacket *packet; |
| 98 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 99 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 100 | return; /* drop if queue full and no callback */ |
| 101 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 102 | packet = g_malloc(sizeof(NetPacket) + size); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 103 | packet->sender = sender; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 104 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 105 | packet->size = size; |
| 106 | packet->sent_cb = sent_cb; |
| 107 | memcpy(packet->data, buf, size); |
| 108 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 109 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 110 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 113 | static void qemu_net_queue_append_iov(NetQueue *queue, |
| 114 | NetClientState *sender, |
| 115 | unsigned flags, |
| 116 | const struct iovec *iov, |
| 117 | int iovcnt, |
| 118 | NetPacketSent *sent_cb) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 119 | { |
| 120 | NetPacket *packet; |
| 121 | size_t max_len = 0; |
| 122 | int i; |
| 123 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 124 | if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { |
| 125 | return; /* drop if queue full and no callback */ |
| 126 | } |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 127 | for (i = 0; i < iovcnt; i++) { |
| 128 | max_len += iov[i].iov_len; |
| 129 | } |
| 130 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 131 | packet = g_malloc(sizeof(NetPacket) + max_len); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 132 | packet->sender = sender; |
| 133 | packet->sent_cb = sent_cb; |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 134 | packet->flags = flags; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 135 | packet->size = 0; |
| 136 | |
| 137 | for (i = 0; i < iovcnt; i++) { |
| 138 | size_t len = iov[i].iov_len; |
| 139 | |
| 140 | memcpy(packet->data + packet->size, iov[i].iov_base, len); |
| 141 | packet->size += len; |
| 142 | } |
| 143 | |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 144 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 145 | QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static ssize_t qemu_net_queue_deliver(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 149 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 150 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 151 | const uint8_t *data, |
| 152 | size_t size) |
| 153 | { |
| 154 | ssize_t ret = -1; |
| 155 | |
| 156 | queue->delivering = 1; |
Zhi Yong Wu | 86a77c3 | 2012-07-24 16:35:17 +0100 | [diff] [blame] | 157 | ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 158 | queue->delivering = 0; |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 164 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 165 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 166 | const struct iovec *iov, |
| 167 | int iovcnt) |
| 168 | { |
| 169 | ssize_t ret = -1; |
| 170 | |
| 171 | queue->delivering = 1; |
Zhi Yong Wu | 86a77c3 | 2012-07-24 16:35:17 +0100 | [diff] [blame] | 172 | ret = qemu_deliver_packet_iov(sender, flags, iov, iovcnt, queue->opaque); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 173 | queue->delivering = 0; |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | ssize_t qemu_net_queue_send(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 179 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 180 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 181 | const uint8_t *data, |
| 182 | size_t size, |
| 183 | NetPacketSent *sent_cb) |
| 184 | { |
| 185 | ssize_t ret; |
| 186 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 187 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 188 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
| 189 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 192 | ret = qemu_net_queue_deliver(queue, sender, flags, data, size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 193 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 194 | qemu_net_queue_append(queue, sender, flags, data, size, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | qemu_net_queue_flush(queue); |
| 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | ssize_t qemu_net_queue_send_iov(NetQueue *queue, |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 204 | NetClientState *sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 205 | unsigned flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 206 | const struct iovec *iov, |
| 207 | int iovcnt, |
| 208 | NetPacketSent *sent_cb) |
| 209 | { |
| 210 | ssize_t ret; |
| 211 | |
Zhi Yong Wu | 691a4f3 | 2012-07-24 16:35:18 +0100 | [diff] [blame] | 212 | if (queue->delivering || !qemu_can_send_packet(sender)) { |
Stefan Hajnoczi | 06b5f36 | 2012-08-20 13:35:23 +0100 | [diff] [blame] | 213 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
| 214 | return 0; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 217 | ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 218 | if (ret == 0) { |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 219 | qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | qemu_net_queue_flush(queue); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 228 | void qemu_net_queue_purge(NetQueue *queue, NetClientState *from) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 229 | { |
| 230 | NetPacket *packet, *next; |
| 231 | |
| 232 | QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { |
| 233 | if (packet->sender == from) { |
| 234 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 235 | queue->nq_count--; |
Michael S. Tsirkin | 07d8084 | 2014-09-04 11:39:10 +0300 | [diff] [blame] | 236 | if (packet->sent_cb) { |
| 237 | packet->sent_cb(packet->sender, 0); |
| 238 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 239 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 244 | bool qemu_net_queue_flush(NetQueue *queue) |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 245 | { |
| 246 | while (!QTAILQ_EMPTY(&queue->packets)) { |
| 247 | NetPacket *packet; |
| 248 | int ret; |
| 249 | |
| 250 | packet = QTAILQ_FIRST(&queue->packets); |
| 251 | QTAILQ_REMOVE(&queue->packets, packet, entry); |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 252 | queue->nq_count--; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 253 | |
| 254 | ret = qemu_net_queue_deliver(queue, |
| 255 | packet->sender, |
Mark McLoughlin | c0b8e49 | 2009-10-22 17:43:40 +0100 | [diff] [blame] | 256 | packet->flags, |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 257 | packet->data, |
| 258 | packet->size); |
Mark McLoughlin | 839f368 | 2009-10-27 18:16:37 +0000 | [diff] [blame] | 259 | if (ret == 0) { |
Luigi Rizzo | 7d91ddd | 2013-02-05 18:29:09 +0100 | [diff] [blame] | 260 | queue->nq_count++; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 261 | QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 262 | return false; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (packet->sent_cb) { |
| 266 | packet->sent_cb(packet->sender, ret); |
| 267 | } |
| 268 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 269 | g_free(packet); |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 270 | } |
Paolo Bonzini | 987a9b4 | 2012-08-09 16:45:55 +0200 | [diff] [blame] | 271 | return true; |
Mark McLoughlin | f710584 | 2009-10-08 19:58:31 +0100 | [diff] [blame] | 272 | } |