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 "qemu/osdep.h"
25 #include "net/queue.h"
26 #include "qemu/queue.h"
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
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
39 * If a sent callback isn't provided, we just drop the packet to avoid
44 QTAILQ_ENTRY(NetPacket
) entry
;
45 NetClientState
*sender
;
48 NetPacketSent
*sent_cb
;
56 NetQueueDeliverFunc
*deliver
;
58 QTAILQ_HEAD(, NetPacket
) packets
;
60 unsigned delivering
: 1;
63 NetQueue
*qemu_new_net_queue(NetQueueDeliverFunc
*deliver
, void *opaque
)
67 queue
= g_new0(NetQueue
, 1);
69 queue
->opaque
= opaque
;
70 queue
->nq_maxlen
= 10000;
72 queue
->deliver
= deliver
;
74 QTAILQ_INIT(&queue
->packets
);
76 queue
->delivering
= 0;
81 void qemu_del_net_queue(NetQueue
*queue
)
83 NetPacket
*packet
, *next
;
85 QTAILQ_FOREACH_SAFE(packet
, &queue
->packets
, entry
, next
) {
86 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
93 static void qemu_net_queue_append(NetQueue
*queue
,
94 NetClientState
*sender
,
98 NetPacketSent
*sent_cb
)
102 if (queue
->nq_count
>= queue
->nq_maxlen
&& !sent_cb
) {
103 return; /* drop if queue full and no callback */
105 packet
= g_malloc(sizeof(NetPacket
) + size
);
106 packet
->sender
= sender
;
107 packet
->flags
= flags
;
109 packet
->sent_cb
= sent_cb
;
110 memcpy(packet
->data
, buf
, size
);
113 QTAILQ_INSERT_TAIL(&queue
->packets
, packet
, entry
);
116 void qemu_net_queue_append_iov(NetQueue
*queue
,
117 NetClientState
*sender
,
119 const struct iovec
*iov
,
121 NetPacketSent
*sent_cb
)
127 if (queue
->nq_count
>= queue
->nq_maxlen
&& !sent_cb
) {
128 return; /* drop if queue full and no callback */
130 for (i
= 0; i
< iovcnt
; i
++) {
131 max_len
+= iov
[i
].iov_len
;
134 packet
= g_malloc(sizeof(NetPacket
) + max_len
);
135 packet
->sender
= sender
;
136 packet
->sent_cb
= sent_cb
;
137 packet
->flags
= flags
;
140 for (i
= 0; i
< iovcnt
; i
++) {
141 size_t len
= iov
[i
].iov_len
;
143 memcpy(packet
->data
+ packet
->size
, iov
[i
].iov_base
, len
);
148 QTAILQ_INSERT_TAIL(&queue
->packets
, packet
, entry
);
151 static ssize_t
qemu_net_queue_deliver(NetQueue
*queue
,
152 NetClientState
*sender
,
159 .iov_base
= (void *)data
,
163 queue
->delivering
= 1;
164 ret
= queue
->deliver(sender
, flags
, &iov
, 1, queue
->opaque
);
165 queue
->delivering
= 0;
170 static ssize_t
qemu_net_queue_deliver_iov(NetQueue
*queue
,
171 NetClientState
*sender
,
173 const struct iovec
*iov
,
178 queue
->delivering
= 1;
179 ret
= queue
->deliver(sender
, flags
, iov
, iovcnt
, queue
->opaque
);
180 queue
->delivering
= 0;
185 ssize_t
qemu_net_queue_send(NetQueue
*queue
,
186 NetClientState
*sender
,
190 NetPacketSent
*sent_cb
)
194 if (queue
->delivering
|| !qemu_can_send_packet(sender
)) {
195 qemu_net_queue_append(queue
, sender
, flags
, data
, size
, sent_cb
);
199 ret
= qemu_net_queue_deliver(queue
, sender
, flags
, data
, size
);
201 qemu_net_queue_append(queue
, sender
, flags
, data
, size
, sent_cb
);
205 qemu_net_queue_flush(queue
);
210 ssize_t
qemu_net_queue_send_iov(NetQueue
*queue
,
211 NetClientState
*sender
,
213 const struct iovec
*iov
,
215 NetPacketSent
*sent_cb
)
219 if (queue
->delivering
|| !qemu_can_send_packet(sender
)) {
220 qemu_net_queue_append_iov(queue
, sender
, flags
, iov
, iovcnt
, sent_cb
);
224 ret
= qemu_net_queue_deliver_iov(queue
, sender
, flags
, iov
, iovcnt
);
226 qemu_net_queue_append_iov(queue
, sender
, flags
, iov
, iovcnt
, sent_cb
);
230 qemu_net_queue_flush(queue
);
235 void qemu_net_queue_purge(NetQueue
*queue
, NetClientState
*from
)
237 NetPacket
*packet
, *next
;
239 QTAILQ_FOREACH_SAFE(packet
, &queue
->packets
, entry
, next
) {
240 if (packet
->sender
== from
) {
241 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
243 if (packet
->sent_cb
) {
244 packet
->sent_cb(packet
->sender
, 0);
251 bool qemu_net_queue_flush(NetQueue
*queue
)
253 if (queue
->delivering
)
256 while (!QTAILQ_EMPTY(&queue
->packets
)) {
260 packet
= QTAILQ_FIRST(&queue
->packets
);
261 QTAILQ_REMOVE(&queue
->packets
, packet
, entry
);
264 ret
= qemu_net_queue_deliver(queue
,
271 QTAILQ_INSERT_HEAD(&queue
->packets
, packet
, entry
);
275 if (packet
->sent_cb
) {
276 packet
->sent_cb(packet
->sender
, ret
);