1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "net/queue.h"
25#include "qemu/queue.h"
26#include "net/net.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42struct NetPacket {
43 QTAILQ_ENTRY(NetPacket) entry;
44 NetClientState *sender;
45 unsigned flags;
46 int size;
47 NetPacketSent *sent_cb;
48 uint8_t data[0];
49};
50
51struct NetQueue {
52 void *opaque;
53
54 QTAILQ_HEAD(packets, NetPacket) packets;
55
56 unsigned delivering : 1;
57};
58
59NetQueue *qemu_new_net_queue(void *opaque)
60{
61 NetQueue *queue;
62
63 queue = g_malloc0(sizeof(NetQueue));
64
65 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);
80 g_free(packet);
81 }
82
83 g_free(queue);
84}
85
86static 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)
92{
93 NetPacket *packet;
94
95 packet = g_malloc(sizeof(NetPacket) + size);
96 packet->sender = sender;
97 packet->flags = flags;
98 packet->size = size;
99 packet->sent_cb = sent_cb;
100 memcpy(packet->data, buf, size);
101
102 QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
103}
104
105static 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)
111{
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
120 packet = g_malloc(sizeof(NetPacket) + max_len);
121 packet->sender = sender;
122 packet->sent_cb = sent_cb;
123 packet->flags = flags;
124 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);
134}
135
136static ssize_t qemu_net_queue_deliver(NetQueue *queue,
137 NetClientState *sender,
138 unsigned flags,
139 const uint8_t *data,
140 size_t size)
141{
142 ssize_t ret = -1;
143
144 queue->delivering = 1;
145 ret = qemu_deliver_packet(sender, flags, data, size, queue->opaque);
146 queue->delivering = 0;
147
148 return ret;
149}
150
151static ssize_t qemu_net_queue_deliver_iov(NetQueue *queue,
152 NetClientState *sender,
153 unsigned flags,
154 const struct iovec *iov,
155 int iovcnt)
156{
157 ssize_t ret = -1;
158
159 queue->delivering = 1;
160 ret = qemu_deliver_packet_iov(sender, flags, iov, iovcnt, queue->opaque);
161 queue->delivering = 0;
162
163 return ret;
164}
165
166ssize_t qemu_net_queue_send(NetQueue *queue,
167 NetClientState *sender,
168 unsigned flags,
169 const uint8_t *data,
170 size_t size,
171 NetPacketSent *sent_cb)
172{
173 ssize_t ret;
174
175 if (queue->delivering || !qemu_can_send_packet(sender)) {
176 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
177 return 0;
178 }
179
180 ret = qemu_net_queue_deliver(queue, sender, flags, data, size);
181 if (ret == 0) {
182 qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
183 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,
192 NetClientState *sender,
193 unsigned flags,
194 const struct iovec *iov,
195 int iovcnt,
196 NetPacketSent *sent_cb)
197{
198 ssize_t ret;
199
200 if (queue->delivering || !qemu_can_send_packet(sender)) {
201 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
202 return 0;
203 }
204
205 ret = qemu_net_queue_deliver_iov(queue, sender, flags, iov, iovcnt);
206 if (ret == 0) {
207 qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
208 return 0;
209 }
210
211 qemu_net_queue_flush(queue);
212
213 return ret;
214}
215
216void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
217{
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);
223 g_free(packet);
224 }
225 }
226}
227
228bool qemu_net_queue_flush(NetQueue *queue)
229{
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,
239 packet->flags,
240 packet->data,
241 packet->size);
242 if (ret == 0) {
243 QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
244 return false;
245 }
246
247 if (packet->sent_cb) {
248 packet->sent_cb(packet->sender, ret);
249 }
250
251 g_free(packet);
252 }
253 return true;
254}
255