blob: 16dddf0e71e2ad333e2623fa51bd95ae03205eee [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;
Yang Hongyang3e033a42015-10-07 11:52:17 +080055 NetQueueDeliverFunc *deliver;
Mark McLoughlinf7105842009-10-08 19:58:31 +010056
57 QTAILQ_HEAD(packets, NetPacket) packets;
58
59 unsigned delivering : 1;
60};
61
Yang Hongyang3e033a42015-10-07 11:52:17 +080062NetQueue *qemu_new_net_queue(NetQueueDeliverFunc *deliver, void *opaque)
Mark McLoughlinf7105842009-10-08 19:58:31 +010063{
64 NetQueue *queue;
65
Markus Armbruster58889fe2014-12-04 14:28:17 +010066 queue = g_new0(NetQueue, 1);
Mark McLoughlinf7105842009-10-08 19:58:31 +010067
Mark McLoughlinf7105842009-10-08 19:58:31 +010068 queue->opaque = opaque;
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +010069 queue->nq_maxlen = 10000;
70 queue->nq_count = 0;
Yang Hongyang3e033a42015-10-07 11:52:17 +080071 queue->deliver = deliver;
Mark McLoughlinf7105842009-10-08 19:58:31 +010072
73 QTAILQ_INIT(&queue->packets);
74
75 queue->delivering = 0;
76
77 return queue;
78}
79
80void qemu_del_net_queue(NetQueue *queue)
81{
82 NetPacket *packet, *next;
83
84 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
85 QTAILQ_REMOVE(&queue->packets, packet, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -050086 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +010087 }
88
Anthony Liguori7267c092011-08-20 22:09:37 -050089 g_free(queue);
Mark McLoughlinf7105842009-10-08 19:58:31 +010090}
91
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +010092static void qemu_net_queue_append(NetQueue *queue,
93 NetClientState *sender,
94 unsigned flags,
95 const uint8_t *buf,
96 size_t size,
97 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +010098{
99 NetPacket *packet;
100
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100101 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
102 return; /* drop if queue full and no callback */
103 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500104 packet = g_malloc(sizeof(NetPacket) + size);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100105 packet->sender = sender;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100106 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100107 packet->size = size;
108 packet->sent_cb = sent_cb;
109 memcpy(packet->data, buf, size);
110
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100111 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100112 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100113}
114
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100115static void qemu_net_queue_append_iov(NetQueue *queue,
116 NetClientState *sender,
117 unsigned flags,
118 const struct iovec *iov,
119 int iovcnt,
120 NetPacketSent *sent_cb)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100121{
122 NetPacket *packet;
123 size_t max_len = 0;
124 int i;
125
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100126 if (queue->nq_count >= queue->nq_maxlen && !sent_cb) {
127 return; /* drop if queue full and no callback */
128 }
Mark McLoughlinf7105842009-10-08 19:58:31 +0100129 for (i = 0; i < iovcnt; i++) {
130 max_len += iov[i].iov_len;
131 }
132
Anthony Liguori7267c092011-08-20 22:09:37 -0500133 packet = g_malloc(sizeof(NetPacket) + max_len);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100134 packet->sender = sender;
135 packet->sent_cb = sent_cb;
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100136 packet->flags = flags;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100137 packet->size = 0;
138
139 for (i = 0; i < iovcnt; i++) {
140 size_t len = iov[i].iov_len;
141
142 memcpy(packet->data + packet->size, iov[i].iov_base, len);
143 packet->size += len;
144 }
145
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100146 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100147 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100148}
149
150static ssize_t qemu_net_queue_deliver(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100151 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100152 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100153 const uint8_t *data,
154 size_t size)
155{
156 ssize_t ret = -1;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800157 struct iovec iov = {
158 .iov_base = (void *)data,
159 .iov_len = size
160 };
Mark McLoughlinf7105842009-10-08 19:58:31 +0100161
162 queue->delivering = 1;
Yang Hongyang3e033a42015-10-07 11:52:17 +0800163 ret = queue->deliver(sender, flags, &iov, 1, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100164 queue->delivering = 0;
165
166 return ret;
167}
168
169static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100170 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100171 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100172 const struct iovec *iov,
173 int iovcnt)
174{
175 ssize_t ret = -1;
176
177 queue->delivering = 1;
Yang Hongyang3e033a42015-10-07 11:52:17 +0800178 ret = queue->deliver(sender, flags, iov, iovcnt, queue->opaque);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100179 queue->delivering = 0;
180
181 return ret;
182}
183
184ssize_t qemu_net_queue_send(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100185 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100186 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100187 const uint8_t *data,
188 size_t size,
189 NetPacketSent *sent_cb)
190{
191 ssize_t ret;
192
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100193 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100194 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
195 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100196 }
197
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100198 ret = qemu_net_queue_deliver(queue, sender, flags, data, size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000199 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100200 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100201 return 0;
202 }
203
204 qemu_net_queue_flush(queue);
205
206 return ret;
207}
208
209ssize_t qemu_net_queue_send_iov(NetQueue *queue,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100210 NetClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100211 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100212 const struct iovec *iov,
213 int iovcnt,
214 NetPacketSent *sent_cb)
215{
216 ssize_t ret;
217
Zhi Yong Wu691a4f32012-07-24 16:35:18 +0100218 if (queue->delivering || !qemu_can_send_packet(sender)) {
Stefan Hajnoczi06b5f362012-08-20 13:35:23 +0100219 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
220 return 0;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100221 }
222
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100223 ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000224 if (ret == 0) {
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100225 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100226 return 0;
227 }
228
229 qemu_net_queue_flush(queue);
230
231 return ret;
232}
233
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100234void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100235{
236 NetPacket *packet, *next;
237
238 QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
239 if (packet->sender == from) {
240 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100241 queue->nq_count--;
Michael S. Tsirkin07d80842014-09-04 11:39:10 +0300242 if (packet->sent_cb) {
243 packet->sent_cb(packet->sender, 0);
244 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500245 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100246 }
247 }
248}
249
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200250bool qemu_net_queue_flush(NetQueue *queue)
Mark McLoughlinf7105842009-10-08 19:58:31 +0100251{
252 while (!QTAILQ_EMPTY(&queue->packets)) {
253 NetPacket *packet;
254 int ret;
255
256 packet = QTAILQ_FIRST(&queue->packets);
257 QTAILQ_REMOVE(&queue->packets, packet, entry);
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100258 queue->nq_count--;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100259
260 ret = qemu_net_queue_deliver(queue,
261 packet->sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100262 packet->flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100263 packet->data,
264 packet->size);
Mark McLoughlin839f3682009-10-27 18:16:37 +0000265 if (ret == 0) {
Luigi Rizzo7d91ddd2013-02-05 18:29:09 +0100266 queue->nq_count++;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100267 QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200268 return false;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100269 }
270
271 if (packet->sent_cb) {
272 packet->sent_cb(packet->sender, ret);
273 }
274
Anthony Liguori7267c092011-08-20 22:09:37 -0500275 g_free(packet);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100276 }
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200277 return true;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100278}