blob: fb33856c188916a31e62bfd7a307a70d6fdc4c9e [file] [log] [blame]
Mark McLoughlinf7105842009-10-08 19:58:31 +01001/*
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 Maydell2744d922016-01-29 17:50:00 +000024#include "qemu/osdep.h"
Mark McLoughline1144d02009-10-23 17:52:16 +010025#include "net/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/queue.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020027#include "net/net.h"
Mark McLoughlinf7105842009-10-08 19:58:31 +010028
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
43struct NetPacket {
44 QTAILQ_ENTRY(NetPacket) entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010045 NetClientState *sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +010046 unsigned flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +010047 int size;
48 NetPacketSent *sent_cb;
Philippe Mathieu-Daudéf7795e42020-03-04 16:38:15 +010049 uint8_t data[];
Mark McLoughlinf7105842009-10-08 19:58:31 +010050};
51
52struct NetQueue {
Mark McLoughlinf7105842009-10-08 19:58:31 +010053 void *opaque;
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010054 uint32_t nq_maxlen;
55 uint32_t nq_count;
Yang Hongyang3e033a42015-10-07 11:52:17 +080056 NetQueueDeliverFunc *deliver;
Mark McLoughlinf7105842009-10-08 19:58:31 +010057
Paolo Bonzinib58deb32018-12-06 11:58:10 +010058 QTAILQ_HEAD(, NetPacket) packets;
Mark McLoughlinf7105842009-10-08 19:58:31 +010059
60 unsigned delivering : 1;
61};
62
Yang Hongyang3e033a42015-10-07 11:52:17 +080063NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque)
Mark McLoughlinf7105842009-10-08 19:58:31 +010064{
65 NetQueue *queue;
66
Markus Armbruster58889fe2014-12-04 14:28:17 +010067 queue = g_new0(NetQueue, 1);
Mark McLoughlinf7105842009-10-08 19:58:31 +010068
Mark McLoughlinf7105842009-10-08 19:58:31 +010069 queue->opaque = opaque;
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010070 queue->nq_maxlen = 10000;
71 queue->nq_count = 0;
Yang Hongyang3e033a42015-10-07 11:52:17 +080072 queue->deliver = deliver;
Mark McLoughlinf7105842009-10-08 19:58:31 +010073
74 QTAILQ_INIT(&queue->packets);
75
76 queue->delivering = 0;
77
78 return queue;
79}
80
81void 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 Liguori7267c092011-08-20 22:09:37 -050087 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +010088 }
89
Anthony Liguori7267c092011-08-20 22:09:37 -050090 g_free(queue);
Mark McLoughlinf7105842009-10-08 19:58:31 +010091}
92
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +010093static 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 McLoughlinf7105842009-10-08 19:58:31 +010099{
100 NetPacket *packet;
101
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100102 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
103 return; /* drop if queue full and no callback */
104 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500105 packet = g_malloc(sizeof(NetPacket) + size);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100106 packet->sender = sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100107 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100108 packet->size = size;
109 packet->sent_cb = sent_cb;
110 memcpy(packet->data, buf, size);
111
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100112 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100113 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100114}
115
Yang Hongyangb68c7f72015-10-07 11:52:20 +0800116void 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 McLoughlinf7105842009-10-08 19:58:31 +0100122{
123 NetPacket *packet;
124 size_t max_len = 0;
125 int i;
126
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100127 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
128 return; /* drop if queue full and no callback */
129 }
Mark McLoughlinf7105842009-10-08 19:58:31 +0100130 for (i = 0; i < iovcnt; i++) {
131 max_len += iov[i].iov_len;
132 }
133
Anthony Liguori7267c092011-08-20 22:09:37 -0500134 packet = g_malloc(sizeof(NetPacket) + max_len);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100135 packet->sender = sender;
136 packet->sent_cb = sent_cb;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100137 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100138 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 Rizzo7d91ddd2013-02-05 18:29:09 +0100147 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100148 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100149}
150
151static ssize_t qemu_net_queue_deliver(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100152 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100153 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100154 const uint8_t *data,
155 size_t size)
156{
157 ssize_t ret = -1;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800158 struct iovec iov = {
159 .iov_base = (void *)data,
160 .iov_len = size
161 };
Mark McLoughlinf7105842009-10-08 19:58:31 +0100162
163 queue->delivering = 1;
Yang Hongyang3e033a42015-10-07 11:52:17 +0800164 ret = queue->deliver(sender, flags, &iov, 1, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100165 queue->delivering = 0;
166
167 return ret;
168}
169
170static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100171 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100172 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100173 const struct iovec *iov,
174 int iovcnt)
175{
176 ssize_t ret = -1;
177
178 queue->delivering = 1;
Yang Hongyang3e033a42015-10-07 11:52:17 +0800179 ret = queue->deliver(sender, flags, iov, iovcnt, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100180 queue->delivering = 0;
181
182 return ret;
183}
184
Jason Wang705df542021-02-24 11:44:36 +0800185ssize_t qemu_net_queue_receive(NetQueue *queue,
186 const uint8_t *data,
187 size_t size)
188{
189 if (queue->delivering) {
190 return 0;
191 }
192
193 return qemu_net_queue_deliver(queue, NULL, 0, data, size);
194}
195
Mark McLoughlinf7105842009-10-08 19:58:31 +0100196ssize_t qemu_net_queue_send(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100197 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100198 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100199 const uint8_t *data,
200 size_t size,
201 NetPacketSent *sent_cb)
202{
203 ssize_t ret;
204
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100205 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100206 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
207 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100208 }
209
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100210 ret = qemu_net_queue_deliver(queue, sender, flags, data, size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000211 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100212 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100213 return 0;
214 }
215
216 qemu_net_queue_flush(queue);
217
218 return ret;
219}
220
221ssize_t qemu_net_queue_send_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100222 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100223 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100224 const struct iovec *iov,
225 int iovcnt,
226 NetPacketSent *sent_cb)
227{
228 ssize_t ret;
229
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100230 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100231 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
232 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100233 }
234
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100235 ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000236 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100237 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100238 return 0;
239 }
240
241 qemu_net_queue_flush(queue);
242
243 return ret;
244}
245
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100246void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100247{
248 NetPacket *packet, *next;
249
250 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
251 if (packet->sender == from) {
252 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100253 queue->nq_count--;
Michael S. Tsirkin07d80842014-09-04 11:39:10 +0300254 if (packet->sent_cb) {
255 packet->sent_cb(packet->sender, 0);
256 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500257 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100258 }
259 }
260}
261
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200262bool qemu_net_queue_flush(NetQueue *queue)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100263{
Jason Wang22dc8662020-07-22 16:57:46 +0800264 if (queue->delivering)
265 return false;
266
Mark McLoughlinf7105842009-10-08 19:58:31 +0100267 while (!QTAILQ_EMPTY(&queue->packets)) {
268 NetPacket *packet;
269 int ret;
270
271 packet = QTAILQ_FIRST(&queue->packets);
272 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100273 queue->nq_count--;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100274
275 ret = qemu_net_queue_deliver(queue,
276 packet->sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100277 packet->flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100278 packet->data,
279 packet->size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000280 if (ret == 0) {
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100281 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100282 QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200283 return false;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100284 }
285
286 if (packet->sent_cb) {
287 packet->sent_cb(packet->sender, ret);
288 }
289
Anthony Liguori7267c092011-08-20 22:09:37 -0500290 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100291 }
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200292 return true;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100293}