blob: cf8db3ae95fee4c12435aec19704a9660e26951b [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;
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010053 uint32_t nq_maxlen;
54 uint32_t nq_count;
Mark McLoughlinf7105842009-10-08 19:58:31 +010055
56 QTAILQ_HEAD(packets, NetPacket) packets;
57
58 unsigned delivering : 1;
59};
60
Zhi Yong Wu86a77c32012-07-24 16:35:17 +010061NetQueue *qemu_new_net_queue(void *opaque)
Mark McLoughlinf7105842009-10-08 19:58:31 +010062{
63 NetQueue *queue;
64
Markus Armbruster58889fe2014-12-04 14:28:17 +010065 queue = g_new0(NetQueue, 1);
Mark McLoughlinf7105842009-10-08 19:58:31 +010066
Mark McLoughlinf7105842009-10-08 19:58:31 +010067 queue->opaque = opaque;
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010068 queue->nq_maxlen = 10000;
69 queue->nq_count = 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +010070
71 QTAILQ_INIT(&queue->packets);
72
73 queue->delivering = 0;
74
75 return queue;
76}
77
78void qemu_del_net_queue(NetQueue *queue)
79{
80 NetPacket *packet, *next;
81
82 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
83 QTAILQ_REMOVE(&queue->packets, packet, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -050084 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +010085 }
86
Anthony Liguori7267c092011-08-20 22:09:37 -050087 g_free(queue);
Mark McLoughlinf7105842009-10-08 19:58:31 +010088}
89
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +010090static void qemu_net_queue_append(NetQueue *queue,
91 NetClientState *sender,
92 unsigned flags,
93 const uint8_t *buf,
94 size_t size,
95 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +010096{
97 NetPacket *packet;
98
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010099 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
100 return; /* drop if queue full and no callback */
101 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500102 packet = g_malloc(sizeof(NetPacket) + size);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100103 packet->sender = sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100104 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100105 packet->size = size;
106 packet->sent_cb = sent_cb;
107 memcpy(packet->data, buf, size);
108
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100109 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100110 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100111}
112
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100113static void qemu_net_queue_append_iov(NetQueue *queue,
114 NetClientState *sender,
115 unsigned flags,
116 const struct iovec *iov,
117 int iovcnt,
118 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100119{
120 NetPacket *packet;
121 size_t max_len = 0;
122 int i;
123
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100124 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
125 return; /* drop if queue full and no callback */
126 }
Mark McLoughlinf7105842009-10-08 19:58:31 +0100127 for (i = 0; i < iovcnt; i++) {
128 max_len += iov[i].iov_len;
129 }
130
Anthony Liguori7267c092011-08-20 22:09:37 -0500131 packet = g_malloc(sizeof(NetPacket) + max_len);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100132 packet->sender = sender;
133 packet->sent_cb = sent_cb;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100134 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100135 packet->size = 0;
136
137 for (i = 0; i < iovcnt; i++) {
138 size_t len = iov[i].iov_len;
139
140 memcpy(packet->data + packet->size, iov[i].iov_base, len);
141 packet->size += len;
142 }
143
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100144 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100145 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100146}
147
148static ssize_t qemu_net_queue_deliver(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100149 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100150 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100151 const uint8_t *data,
152 size_t size)
153{
154 ssize_t ret = -1;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800155 struct iovec iov = {
156 .iov_base = (void *)data,
157 .iov_len = size
158 };
Mark McLoughlinf7105842009-10-08 19:58:31 +0100159
160 queue->delivering = 1;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800161 ret = qemu_deliver_packet_iov(sender, flags, &iov, 1, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100162 queue->delivering = 0;
163
164 return ret;
165}
166
167static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100168 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100169 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100170 const struct iovec *iov,
171 int iovcnt)
172{
173 ssize_t ret = -1;
174
175 queue->delivering = 1;
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100176 ret = qemu_deliver_packet_iov(sender, flags, iov, iovcnt, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100177 queue->delivering = 0;
178
179 return ret;
180}
181
182ssize_t qemu_net_queue_send(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100183 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100184 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100185 const uint8_t *data,
186 size_t size,
187 NetPacketSent *sent_cb)
188{
189 ssize_t ret;
190
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100191 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100192 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
193 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100194 }
195
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100196 ret = qemu_net_queue_deliver(queue, sender, flags, data, size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000197 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100198 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100199 return 0;
200 }
201
202 qemu_net_queue_flush(queue);
203
204 return ret;
205}
206
207ssize_t qemu_net_queue_send_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100208 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100209 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100210 const struct iovec *iov,
211 int iovcnt,
212 NetPacketSent *sent_cb)
213{
214 ssize_t ret;
215
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100216 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100217 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
218 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100219 }
220
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100221 ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000222 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100223 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100224 return 0;
225 }
226
227 qemu_net_queue_flush(queue);
228
229 return ret;
230}
231
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100232void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100233{
234 NetPacket *packet, *next;
235
236 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
237 if (packet->sender == from) {
238 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100239 queue->nq_count--;
Michael S. Tsirkin07d80842014-09-04 11:39:10 +0300240 if (packet->sent_cb) {
241 packet->sent_cb(packet->sender, 0);
242 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500243 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100244 }
245 }
246}
247
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200248bool qemu_net_queue_flush(NetQueue *queue)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100249{
250 while (!QTAILQ_EMPTY(&queue->packets)) {
251 NetPacket *packet;
252 int ret;
253
254 packet = QTAILQ_FIRST(&queue->packets);
255 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100256 queue->nq_count--;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100257
258 ret = qemu_net_queue_deliver(queue,
259 packet->sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100260 packet->flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100261 packet->data,
262 packet->size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000263 if (ret == 0) {
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100264 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100265 QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200266 return false;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100267 }
268
269 if (packet->sent_cb) {
270 packet->sent_cb(packet->sender, ret);
271 }
272
Anthony Liguori7267c092011-08-20 22:09:37 -0500273 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100274 }
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200275 return true;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100276}