blob: 6eaf5b63c0138a99ea5b1ca78e28dd168b503265 [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
Mark McLoughline1144d02009-10-23 17:52:16 +010024#include "net/queue.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/queue.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020026#include "net/net.h"
Mark McLoughlinf7105842009-10-08 19:58:31 +010027
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
42struct NetPacket {
43 QTAILQ_ENTRY(NetPacket) entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010044 NetClientState *sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +010045 unsigned flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +010046 int size;
47 NetPacketSent *sent_cb;
48 uint8_t data[0];
49};
50
51struct NetQueue {
Mark McLoughlinf7105842009-10-08 19:58:31 +010052 void *opaque;
53
54 QTAILQ_HEAD(packets, NetPacket) packets;
55
56 unsigned delivering : 1;
57};
58
Zhi Yong Wu86a77c32012-07-24 16:35:17 +010059NetQueue *qemu_new_net_queue(void *opaque)
Mark McLoughlinf7105842009-10-08 19:58:31 +010060{
61 NetQueue *queue;
62
Anthony Liguori7267c092011-08-20 22:09:37 -050063 queue = g_malloc0(sizeof(NetQueue));
Mark McLoughlinf7105842009-10-08 19:58:31 +010064
Mark McLoughlinf7105842009-10-08 19:58:31 +010065 queue->opaque = opaque;
66
67 QTAILQ_INIT(&queue->packets);
68
69 queue->delivering = 0;
70
71 return queue;
72}
73
74void 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 Liguori7267c092011-08-20 22:09:37 -050080 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +010081 }
82
Anthony Liguori7267c092011-08-20 22:09:37 -050083 g_free(queue);
Mark McLoughlinf7105842009-10-08 19:58:31 +010084}
85
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +010086static void qemu_net_queue_append(NetQueue *queue,
87 NetClientState *sender,
88 unsigned flags,
89 const uint8_t *buf,
90 size_t size,
91 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +010092{
93 NetPacket *packet;
94
Anthony Liguori7267c092011-08-20 22:09:37 -050095 packet = g_malloc(sizeof(NetPacket) + size);
Mark McLoughlinf7105842009-10-08 19:58:31 +010096 packet->sender = sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +010097 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +010098 packet->size = size;
99 packet->sent_cb = sent_cb;
100 memcpy(packet->data, buf, size);
101
102 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100103}
104
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100105static void qemu_net_queue_append_iov(NetQueue *queue,
106 NetClientState *sender,
107 unsigned flags,
108 const struct iovec *iov,
109 int iovcnt,
110 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100111{
112 NetPacket *packet;
113 size_t max_len = 0;
114 int i;
115
116 for (i = 0; i < iovcnt; i++) {
117 max_len += iov[i].iov_len;
118 }
119
Anthony Liguori7267c092011-08-20 22:09:37 -0500120 packet = g_malloc(sizeof(NetPacket) + max_len);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100121 packet->sender = sender;
122 packet->sent_cb = sent_cb;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100123 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100124 packet->size = 0;
125
126 for (i = 0; i < iovcnt; i++) {
127 size_t len = iov[i].iov_len;
128
129 memcpy(packet->data + packet->size, iov[i].iov_base, len);
130 packet->size += len;
131 }
132
133 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100134}
135
136static ssize_t qemu_net_queue_deliver(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100137 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100138 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100139 const uint8_t *data,
140 size_t size)
141{
142 ssize_t ret = -1;
143
144 queue->delivering = 1;
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100145 ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100146 queue->delivering = 0;
147
148 return ret;
149}
150
151static ssize_t qemu_net_queue_deliver_iov(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 struct iovec *iov,
155 int iovcnt)
156{
157 ssize_t ret = -1;
158
159 queue->delivering = 1;
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100160 ret = qemu_deliver_packet_iov(sender, flags, iov, iovcnt, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100161 queue->delivering = 0;
162
163 return ret;
164}
165
166ssize_t qemu_net_queue_send(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100167 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100168 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100169 const uint8_t *data,
170 size_t size,
171 NetPacketSent *sent_cb)
172{
173 ssize_t ret;
174
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100175 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100176 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
177 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100178 }
179
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100180 ret = qemu_net_queue_deliver(queue, sender, flags, data, size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000181 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100182 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100183 return 0;
184 }
185
186 qemu_net_queue_flush(queue);
187
188 return ret;
189}
190
191ssize_t qemu_net_queue_send_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100192 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100193 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100194 const struct iovec *iov,
195 int iovcnt,
196 NetPacketSent *sent_cb)
197{
198 ssize_t ret;
199
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100200 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100201 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
202 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100203 }
204
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100205 ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000206 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100207 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100208 return 0;
209 }
210
211 qemu_net_queue_flush(queue);
212
213 return ret;
214}
215
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100216void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100217{
218 NetPacket *packet, *next;
219
220 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
221 if (packet->sender == from) {
222 QTAILQ_REMOVE(&queue->packets, packet, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -0500223 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100224 }
225 }
226}
227
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200228bool qemu_net_queue_flush(NetQueue *queue)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100229{
230 while (!QTAILQ_EMPTY(&queue->packets)) {
231 NetPacket *packet;
232 int ret;
233
234 packet = QTAILQ_FIRST(&queue->packets);
235 QTAILQ_REMOVE(&queue->packets, packet, entry);
236
237 ret = qemu_net_queue_deliver(queue,
238 packet->sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100239 packet->flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100240 packet->data,
241 packet->size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000242 if (ret == 0) {
Mark McLoughlinf7105842009-10-08 19:58:31 +0100243 QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200244 return false;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100245 }
246
247 if (packet->sent_cb) {
248 packet->sent_cb(packet->sender, ret);
249 }
250
Anthony Liguori7267c092011-08-20 22:09:37 -0500251 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100252 }
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200253 return true;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100254}