2 * Copyright (c) 2003-2008 Fabrice Bellard
3 * Copyright (c) 2009 Red Hat, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include "net/queue.h"
25 #include "qemu/queue.h"
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
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
38 * If a sent callback isn't provided, we just drop the packet to avoid
43 QTAILQ_ENTRY(NetPacket
) entry
;
44 NetClientState
*sender
;
47 NetPacketSent
*sent_cb
;
55 NetQueueDeliverFunc
*deliver
;
57 QTAILQ_HEAD(packets
, NetPacket
) packets
;
59 unsigned delivering
: 1;
62 NetQueue
*qemu_new_net_queue(NetQueueDeliverFunc
*deliver
, void *opaque
)
66 queue
= g_new0(NetQueue
, 1);
68 queue
->opaque
= opaque
;
69 queue
->nq_maxlen
= 10000;
71 queue
->deliver
= deliver
;
73 QTAILQ_INIT(&queue
->packets
);
75 queue
->delivering
= 0;
80 void qemu_del_net_queue(NetQueue
*queue
)
82 NetPacket
*packet
, *next
;
84 QTAILQ_FOREACH_SAFE(packet
, &queue
->packets
, entry
, next
) {
85 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
92 static void qemu_net_queue_append(NetQueue
*queue
,
93 NetClientState
*sender
,
97 NetPacketSent
*sent_cb
)
101 if (queue
->nq_count
>= queue
->nq_maxlen
&& !sent_cb
) {
102 return; /* drop if queue full and no callback */
104 packet
= g_malloc(sizeof(NetPacket
) + size
);
105 packet
->sender
= sender
;
106 packet
->flags
= flags
;
108 packet
->sent_cb
= sent_cb
;
109 memcpy(packet
->data
, buf
, size
);
112 QTAILQ_INSERT_TAIL(&queue
->packets
, packet
, entry
);
115 void qemu_net_queue_append_iov(NetQueue
*queue
,
116 NetClientState
*sender
,
118 const struct iovec
*iov
,
120 NetPacketSent
*sent_cb
)
126 if (queue
->nq_count
>= queue
->nq_maxlen
&& !sent_cb
) {
127 return; /* drop if queue full and no callback */
129 for (i
= 0; i
< iovcnt
; i
++) {
130 max_len
+= iov
[i
].iov_len
;
133 packet
= g_malloc(sizeof(NetPacket
) + max_len
);
134 packet
->sender
= sender
;
135 packet
->sent_cb
= sent_cb
;
136 packet
->flags
= flags
;
139 for (i
= 0; i
< iovcnt
; i
++) {
140 size_t len
= iov
[i
].iov_len
;
142 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
147 QTAILQ_INSERT_TAIL(&queue
->packets
, packet
, entry
);
150 static ssize_t
qemu_net_queue_deliver(NetQueue
*queue
,
151 NetClientState
*sender
,
158 .iov_base
= (void *)data
,
162 queue
->delivering
= 1;
163 ret
= queue
->deliver(sender
, flags
, &iov
, 1, queue
->opaque
);
164 queue
->delivering
= 0;
169 static ssize_t
qemu_net_queue_deliver_iov(NetQueue
*queue
,
170 NetClientState
*sender
,
172 const struct iovec
*iov
,
177 queue
->delivering
= 1;
178 ret
= queue
->deliver(sender
, flags
, iov
, iovcnt
, queue
->opaque
);
179 queue
->delivering
= 0;
184 ssize_t
qemu_net_queue_send(NetQueue
*queue
,
185 NetClientState
*sender
,
189 NetPacketSent
*sent_cb
)
193 if (queue
->delivering
|| !qemu_can_send_packet(sender
)) {
194 qemu_net_queue_append(queue
, sender
, flags
, data
, size
, sent_cb
);
198 ret
= qemu_net_queue_deliver(queue
, sender
, flags
, data
, size
);
200 qemu_net_queue_append(queue
, sender
, flags
, data
, size
, sent_cb
);
204 qemu_net_queue_flush(queue
);
209 ssize_t
qemu_net_queue_send_iov(NetQueue
*queue
,
210 NetClientState
*sender
,
212 const struct iovec
*iov
,
214 NetPacketSent
*sent_cb
)
218 if (queue
->delivering
|| !qemu_can_send_packet(sender
)) {
219 qemu_net_queue_append_iov(queue
, sender
, flags
, iov
, iovcnt
, sent_cb
);
223 ret
= qemu_net_queue_deliver_iov(queue
, sender
, flags
, iov
, iovcnt
);
225 qemu_net_queue_append_iov(queue
, sender
, flags
, iov
, iovcnt
, sent_cb
);
229 qemu_net_queue_flush(queue
);
234 void qemu_net_queue_purge(NetQueue
*queue
, NetClientState
*from
)
236 NetPacket
*packet
, *next
;
238 QTAILQ_FOREACH_SAFE(packet
, &queue
->packets
, entry
, next
) {
239 if (packet
->sender
== from
) {
240 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
242 if (packet
->sent_cb
) {
243 packet
->sent_cb(packet
->sender
, 0);
250 bool qemu_net_queue_flush(NetQueue
*queue
)
252 while (!QTAILQ_EMPTY(&queue
->packets
)) {
256 packet
= QTAILQ_FIRST(&queue
->packets
);
257 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
260 ret
= qemu_net_queue_deliver(queue
,
267 QTAILQ_INSERT_HEAD(&queue
->packets
, packet
, entry
);
271 if (packet
->sent_cb
) {
272 packet
->sent_cb(packet
->sender
, ret
);