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