blob: 218f985037cf6dc809104ebd958a3407960d1ce1 [file] [log] [blame]
aliguorifbe78f42008-12-17 19:13:11 +00001/*
2 * Virtio Network Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include "virtio.h"
15#include "net.h"
16#include "qemu-timer.h"
17#include "virtio-net.h"
18
Alex Williamson015cb162009-06-05 14:47:18 -060019#define VIRTIO_NET_VM_VERSION 10
aliguorib6503ed2009-02-05 22:36:28 +000020
Alex Williamson4ffb17f2009-06-05 14:47:23 -060021#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000022#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000023
aliguorifbe78f42008-12-17 19:13:11 +000024typedef struct VirtIONet
25{
26 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000027 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000028 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000029 VirtQueue *rx_vq;
30 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000031 VirtQueue *ctrl_vq;
aliguorifbe78f42008-12-17 19:13:11 +000032 VLANClientState *vc;
33 QEMUTimer *tx_timer;
34 int tx_timer_active;
Mark McLoughlin62433752009-06-18 18:21:36 +010035 struct {
36 VirtQueueElement elem;
37 ssize_t len;
38 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000039 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060040 uint8_t promisc;
41 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060042 uint8_t alluni;
43 uint8_t nomulti;
44 uint8_t nouni;
45 uint8_t nobcast;
aliguorib6503ed2009-02-05 22:36:28 +000046 struct {
47 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060048 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060049 uint8_t multi_overflow;
50 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000051 uint8_t *macs;
52 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000053 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000054} VirtIONet;
55
56/* TODO
57 * - we could suppress RX interrupt if we were so inclined.
58 */
59
60static VirtIONet *to_virtio_net(VirtIODevice *vdev)
61{
62 return (VirtIONet *)vdev;
63}
64
aliguori0f03eca2009-02-05 22:36:08 +000065static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000066{
67 VirtIONet *n = to_virtio_net(vdev);
68 struct virtio_net_config netcfg;
69
aliguori554c97d2009-01-08 19:46:33 +000070 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000071 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000072 memcpy(config, &netcfg, sizeof(netcfg));
73}
74
aliguori0f03eca2009-02-05 22:36:08 +000075static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
76{
77 VirtIONet *n = to_virtio_net(vdev);
78 struct virtio_net_config netcfg;
79
80 memcpy(&netcfg, config, sizeof(netcfg));
81
aliguori79674062009-02-05 22:36:12 +000082 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
83 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000084 qemu_format_nic_info_str(n->vc, n->mac);
85 }
86}
87
aliguori554c97d2009-01-08 19:46:33 +000088static void virtio_net_set_link_status(VLANClientState *vc)
89{
90 VirtIONet *n = vc->opaque;
91 uint16_t old_status = n->status;
92
93 if (vc->link_down)
94 n->status &= ~VIRTIO_NET_S_LINK_UP;
95 else
96 n->status |= VIRTIO_NET_S_LINK_UP;
97
98 if (n->status != old_status)
99 virtio_notify_config(&n->vdev);
100}
101
aliguori002437c2009-02-05 22:36:20 +0000102static void virtio_net_reset(VirtIODevice *vdev)
103{
104 VirtIONet *n = to_virtio_net(vdev);
105
106 /* Reset back to compatibility mode */
107 n->promisc = 1;
108 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600109 n->alluni = 0;
110 n->nomulti = 0;
111 n->nouni = 0;
112 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000113
aliguorif21c0ed2009-02-05 22:36:32 +0000114 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000115 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600116 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600117 n->mac_table.multi_overflow = 0;
118 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000119 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000120 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000121}
122
aliguorifbe78f42008-12-17 19:13:11 +0000123static uint32_t virtio_net_get_features(VirtIODevice *vdev)
124{
aliguori3d11d362009-02-05 22:36:16 +0000125 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
Mark McLoughline16044e2009-06-18 11:31:07 +0100126 (1 << VIRTIO_NET_F_MRG_RXBUF) |
aliguori3d11d362009-02-05 22:36:16 +0000127 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000128 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000129 (1 << VIRTIO_NET_F_CTRL_RX) |
Alex Williamson015cb162009-06-05 14:47:18 -0600130 (1 << VIRTIO_NET_F_CTRL_VLAN) |
131 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
aliguorifbe78f42008-12-17 19:13:11 +0000132
133 return features;
134}
135
aliguori8eca6b12009-04-05 17:40:08 +0000136static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
137{
138 uint32_t features = 0;
139
140 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
141 * but also these: */
142 features |= (1 << VIRTIO_NET_F_MAC);
143 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
144 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
145 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
146 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
147
148 return features & virtio_net_get_features(vdev);
149}
150
aliguorifbe78f42008-12-17 19:13:11 +0000151static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
152{
153 VirtIONet *n = to_virtio_net(vdev);
154
155 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
156}
157
aliguori002437c2009-02-05 22:36:20 +0000158static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
159 VirtQueueElement *elem)
160{
161 uint8_t on;
162
163 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
164 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
165 exit(1);
166 }
167
168 on = ldub_p(elem->out_sg[1].iov_base);
169
170 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
171 n->promisc = on;
172 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
173 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600174 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
175 n->alluni = on;
176 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
177 n->nomulti = on;
178 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
179 n->nouni = on;
180 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
181 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000182 else
183 return VIRTIO_NET_ERR;
184
185 return VIRTIO_NET_OK;
186}
187
aliguorib6503ed2009-02-05 22:36:28 +0000188static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
189 VirtQueueElement *elem)
190{
191 struct virtio_net_ctrl_mac mac_data;
192
193 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
194 elem->out_sg[1].iov_len < sizeof(mac_data) ||
195 elem->out_sg[2].iov_len < sizeof(mac_data))
196 return VIRTIO_NET_ERR;
197
198 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600199 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600200 n->mac_table.uni_overflow = 0;
201 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000202 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
203
204 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
205
206 if (sizeof(mac_data.entries) +
207 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
208 return VIRTIO_NET_ERR;
209
210 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
211 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
212 mac_data.entries * ETH_ALEN);
213 n->mac_table.in_use += mac_data.entries;
214 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600215 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000216 }
217
Alex Williamson2d9aba32009-06-05 14:47:13 -0600218 n->mac_table.first_multi = n->mac_table.in_use;
219
aliguorib6503ed2009-02-05 22:36:28 +0000220 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
221
222 if (sizeof(mac_data.entries) +
223 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
224 return VIRTIO_NET_ERR;
225
226 if (mac_data.entries) {
227 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
228 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
229 elem->out_sg[2].iov_base + sizeof(mac_data),
230 mac_data.entries * ETH_ALEN);
231 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600232 } else {
233 n->mac_table.multi_overflow = 1;
234 }
aliguorib6503ed2009-02-05 22:36:28 +0000235 }
236
237 return VIRTIO_NET_OK;
238}
239
aliguorif21c0ed2009-02-05 22:36:32 +0000240static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
241 VirtQueueElement *elem)
242{
243 uint16_t vid;
244
245 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
246 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
247 return VIRTIO_NET_ERR;
248 }
249
250 vid = lduw_le_p(elem->out_sg[1].iov_base);
251
252 if (vid >= MAX_VLAN)
253 return VIRTIO_NET_ERR;
254
255 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
256 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
257 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
258 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
259 else
260 return VIRTIO_NET_ERR;
261
262 return VIRTIO_NET_OK;
263}
264
aliguori3d11d362009-02-05 22:36:16 +0000265static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
266{
aliguori002437c2009-02-05 22:36:20 +0000267 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000268 struct virtio_net_ctrl_hdr ctrl;
269 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
270 VirtQueueElement elem;
271
272 while (virtqueue_pop(vq, &elem)) {
273 if ((elem.in_num < 1) || (elem.out_num < 1)) {
274 fprintf(stderr, "virtio-net ctrl missing headers\n");
275 exit(1);
276 }
277
278 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000279 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000280 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
281 exit(1);
282 }
283
284 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
285 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
286
aliguori002437c2009-02-05 22:36:20 +0000287 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
288 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000289 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
290 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000291 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
292 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000293
aliguori3d11d362009-02-05 22:36:16 +0000294 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
295
296 virtqueue_push(vq, &elem, sizeof(status));
297 virtio_notify(vdev, vq);
298 }
299}
300
aliguorifbe78f42008-12-17 19:13:11 +0000301/* RX */
302
303static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
304{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100305 VirtIONet *n = to_virtio_net(vdev);
306
307 qemu_flush_queued_packets(n->vc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400308
309 /* We now have RX buffers, signal to the IO thread to break out of the
310 * select to re-poll the tap file descriptor */
311 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000312}
313
314static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
315{
316 if (!virtio_queue_ready(n->rx_vq) ||
317 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
318 return 0;
319
320 if (virtio_queue_empty(n->rx_vq) ||
321 (n->mergeable_rx_bufs &&
322 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
323 virtio_queue_set_notification(n->rx_vq, 1);
324 return 0;
325 }
326
327 virtio_queue_set_notification(n->rx_vq, 0);
328 return 1;
329}
330
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100331static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000332{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100333 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000334
335 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
336}
337
338static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
339{
340 int offset, i;
341
342 offset = i = 0;
343 while (offset < count && i < iovcnt) {
344 int len = MIN(iov[i].iov_len, count - offset);
345 memcpy(iov[i].iov_base, buf + offset, len);
346 offset += len;
347 i++;
348 }
349
350 return offset;
351}
352
353static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000354 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000355{
blueswir13f4cb3d2009-04-13 16:31:01 +0000356 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000357 int offset = 0;
358
359 hdr->flags = 0;
360 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
361
362 /* We only ever receive a struct virtio_net_hdr from the tapfd,
363 * but we may be passing along a larger header to the guest.
364 */
365 iov[0].iov_base += hdr_len;
366 iov[0].iov_len -= hdr_len;
367
368 return offset;
369}
370
aliguori3831ab22009-02-05 22:36:24 +0000371static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
372{
373 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000374 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000375 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000376 int i;
aliguori3831ab22009-02-05 22:36:24 +0000377
378 if (n->promisc)
379 return 1;
380
aliguorif21c0ed2009-02-05 22:36:32 +0000381 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
382 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
383 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
384 return 0;
385 }
386
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600387 if (ptr[0] & 1) { // multicast
388 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600389 return !n->nobcast;
390 } else if (n->nomulti) {
391 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600392 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600393 return 1;
394 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600395
396 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
397 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
398 return 1;
399 }
400 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600401 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600402 if (n->nouni) {
403 return 0;
404 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600405 return 1;
406 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600407 return 1;
408 }
aliguori3831ab22009-02-05 22:36:24 +0000409
Alex Williamson2d9aba32009-06-05 14:47:13 -0600410 for (i = 0; i < n->mac_table.first_multi; i++) {
411 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
412 return 1;
413 }
414 }
aliguorib6503ed2009-02-05 22:36:28 +0000415 }
416
aliguori3831ab22009-02-05 22:36:24 +0000417 return 0;
418}
419
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100420static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000421{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100422 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000423 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000424 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000425
426 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100427 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000428
aliguori3831ab22009-02-05 22:36:24 +0000429 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100430 return size;
aliguori3831ab22009-02-05 22:36:24 +0000431
aliguorifbe78f42008-12-17 19:13:11 +0000432 /* hdr_len refers to the header we supply to the guest */
433 hdr_len = n->mergeable_rx_bufs ?
434 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
435
436 offset = i = 0;
437
438 while (offset < size) {
439 VirtQueueElement elem;
440 int len, total;
441 struct iovec sg[VIRTQUEUE_MAX_SIZE];
442
443 len = total = 0;
444
445 if ((i != 0 && !n->mergeable_rx_bufs) ||
446 virtqueue_pop(n->rx_vq, &elem) == 0) {
447 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100448 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000449 fprintf(stderr, "virtio-net truncating packet\n");
450 exit(1);
451 }
452
453 if (elem.in_num < 1) {
454 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
455 exit(1);
456 }
457
458 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
459 fprintf(stderr, "virtio-net header not in first element\n");
460 exit(1);
461 }
462
463 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
464
465 if (i == 0) {
466 if (n->mergeable_rx_bufs)
467 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
468
469 offset += receive_header(n, sg, elem.in_num,
470 buf + offset, size - offset, hdr_len);
471 total += hdr_len;
472 }
473
474 /* copy in packet. ugh */
475 len = iov_fill(sg, elem.in_num,
476 buf + offset, size - offset);
477 total += len;
478
479 /* signal other side */
480 virtqueue_fill(n->rx_vq, &elem, total, i++);
481
482 offset += len;
483 }
484
485 if (mhdr)
486 mhdr->num_buffers = i;
487
488 virtqueue_flush(n->rx_vq, i);
489 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100490
491 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000492}
493
Mark McLoughlin62433752009-06-18 18:21:36 +0100494static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
495
496static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
497{
498 VirtIONet *n = vc->opaque;
499
500 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
501 virtio_notify(&n->vdev, n->tx_vq);
502
503 n->async_tx.elem.out_num = n->async_tx.len = 0;
504
505 virtio_queue_set_notification(n->tx_vq, 1);
506 virtio_net_flush_tx(n, n->tx_vq);
507}
508
aliguorifbe78f42008-12-17 19:13:11 +0000509/* TX */
510static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
511{
512 VirtQueueElement elem;
513 int has_vnet_hdr = 0;
514
515 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
516 return;
517
Mark McLoughlin62433752009-06-18 18:21:36 +0100518 if (n->async_tx.elem.out_num) {
519 virtio_queue_set_notification(n->tx_vq, 0);
520 return;
521 }
522
aliguorifbe78f42008-12-17 19:13:11 +0000523 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100524 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000525 unsigned int out_num = elem.out_num;
526 struct iovec *out_sg = &elem.out_sg[0];
527 unsigned hdr_len;
528
529 /* hdr_len refers to the header received from the guest */
530 hdr_len = n->mergeable_rx_bufs ?
531 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
532 sizeof(struct virtio_net_hdr);
533
534 if (out_num < 1 || out_sg->iov_len != hdr_len) {
535 fprintf(stderr, "virtio-net header not in first element\n");
536 exit(1);
537 }
538
539 /* ignore the header if GSO is not supported */
540 if (!has_vnet_hdr) {
541 out_num--;
542 out_sg++;
543 len += hdr_len;
544 } else if (n->mergeable_rx_bufs) {
545 /* tapfd expects a struct virtio_net_hdr */
546 hdr_len -= sizeof(struct virtio_net_hdr);
547 out_sg->iov_len -= hdr_len;
548 len += hdr_len;
549 }
550
Mark McLoughlin62433752009-06-18 18:21:36 +0100551 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
552 virtio_net_tx_complete);
553 if (ret == 0) {
554 virtio_queue_set_notification(n->tx_vq, 0);
555 n->async_tx.elem = elem;
556 n->async_tx.len = len;
557 return;
558 }
559
560 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000561
562 virtqueue_push(vq, &elem, len);
563 virtio_notify(&n->vdev, vq);
564 }
565}
566
567static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
568{
569 VirtIONet *n = to_virtio_net(vdev);
570
571 if (n->tx_timer_active) {
572 virtio_queue_set_notification(vq, 1);
573 qemu_del_timer(n->tx_timer);
574 n->tx_timer_active = 0;
575 virtio_net_flush_tx(n, vq);
576 } else {
577 qemu_mod_timer(n->tx_timer,
578 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
579 n->tx_timer_active = 1;
580 virtio_queue_set_notification(vq, 0);
581 }
582}
583
584static void virtio_net_tx_timer(void *opaque)
585{
586 VirtIONet *n = opaque;
587
588 n->tx_timer_active = 0;
589
590 /* Just in case the driver is not ready on more */
591 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
592 return;
593
594 virtio_queue_set_notification(n->tx_vq, 1);
595 virtio_net_flush_tx(n, n->tx_vq);
596}
597
598static void virtio_net_save(QEMUFile *f, void *opaque)
599{
600 VirtIONet *n = opaque;
601
602 virtio_save(&n->vdev, f);
603
aliguori79674062009-02-05 22:36:12 +0000604 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000605 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000606 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000607 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600608 qemu_put_byte(f, n->promisc);
609 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000610 qemu_put_be32(f, n->mac_table.in_use);
611 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000612 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Alex Williamson6c042c12009-06-05 14:46:52 -0600613 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600614 qemu_put_byte(f, n->mac_table.multi_overflow);
615 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600616 qemu_put_byte(f, n->alluni);
617 qemu_put_byte(f, n->nomulti);
618 qemu_put_byte(f, n->nouni);
619 qemu_put_byte(f, n->nobcast);
aliguorifbe78f42008-12-17 19:13:11 +0000620}
621
622static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
623{
624 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600625 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000626
aliguori9d6271b2009-02-05 22:36:04 +0000627 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000628 return -EINVAL;
629
630 virtio_load(&n->vdev, f);
631
aliguori79674062009-02-05 22:36:12 +0000632 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000633 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000634 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000635
aliguori9d6271b2009-02-05 22:36:04 +0000636 if (version_id >= 3)
637 n->status = qemu_get_be16(f);
638
aliguori002437c2009-02-05 22:36:20 +0000639 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600640 if (version_id < 8) {
641 n->promisc = qemu_get_be32(f);
642 n->allmulti = qemu_get_be32(f);
643 } else {
644 n->promisc = qemu_get_byte(f);
645 n->allmulti = qemu_get_byte(f);
646 }
aliguori002437c2009-02-05 22:36:20 +0000647 }
648
aliguorib6503ed2009-02-05 22:36:28 +0000649 if (version_id >= 5) {
650 n->mac_table.in_use = qemu_get_be32(f);
651 /* MAC_TABLE_ENTRIES may be different from the saved image */
652 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
653 qemu_get_buffer(f, n->mac_table.macs,
654 n->mac_table.in_use * ETH_ALEN);
655 } else if (n->mac_table.in_use) {
656 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600657 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000658 n->mac_table.in_use = 0;
659 }
660 }
661
aliguorif21c0ed2009-02-05 22:36:32 +0000662 if (version_id >= 6)
663 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
664
Alex Williamson6c042c12009-06-05 14:46:52 -0600665 if (version_id >= 7 && qemu_get_be32(f)) {
666 fprintf(stderr,
667 "virtio-net: saved image requires vnet header support\n");
668 exit(1);
669 }
670
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600671 if (version_id >= 9) {
672 n->mac_table.multi_overflow = qemu_get_byte(f);
673 n->mac_table.uni_overflow = qemu_get_byte(f);
674 }
675
Alex Williamson015cb162009-06-05 14:47:18 -0600676 if (version_id >= 10) {
677 n->alluni = qemu_get_byte(f);
678 n->nomulti = qemu_get_byte(f);
679 n->nouni = qemu_get_byte(f);
680 n->nobcast = qemu_get_byte(f);
681 }
682
Alex Williamson2d9aba32009-06-05 14:47:13 -0600683 /* Find the first multicast entry in the saved MAC filter */
684 for (i = 0; i < n->mac_table.in_use; i++) {
685 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
686 break;
687 }
688 }
689 n->mac_table.first_multi = i;
690
aliguorifbe78f42008-12-17 19:13:11 +0000691 if (n->tx_timer_active) {
692 qemu_mod_timer(n->tx_timer,
693 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
694 }
695
696 return 0;
697}
698
aliguorib946a152009-04-17 17:11:08 +0000699static void virtio_net_cleanup(VLANClientState *vc)
700{
701 VirtIONet *n = vc->opaque;
702
Mark McLoughlin62433752009-06-18 18:21:36 +0100703 qemu_purge_queued_packets(vc);
704
aliguorib946a152009-04-17 17:11:08 +0000705 unregister_savevm("virtio-net", n);
706
707 qemu_free(n->mac_table.macs);
708 qemu_free(n->vlans);
709
710 qemu_del_timer(n->tx_timer);
711 qemu_free_timer(n->tx_timer);
712
713 virtio_cleanup(&n->vdev);
714}
715
Paul Brook53c25ce2009-05-18 14:51:59 +0100716VirtIODevice *virtio_net_init(DeviceState *dev)
aliguorifbe78f42008-12-17 19:13:11 +0000717{
718 VirtIONet *n;
719 static int virtio_net_id;
720
Paul Brook53c25ce2009-05-18 14:51:59 +0100721 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
722 sizeof(struct virtio_net_config),
723 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000724
aliguori0f03eca2009-02-05 22:36:08 +0000725 n->vdev.get_config = virtio_net_get_config;
726 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000727 n->vdev.get_features = virtio_net_get_features;
728 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000729 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000730 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000731 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
732 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600733 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Paul Brook53c25ce2009-05-18 14:51:59 +0100734 qdev_get_macaddr(dev, n->mac);
aliguori554c97d2009-01-08 19:46:33 +0000735 n->status = VIRTIO_NET_S_LINK_UP;
Paul Brook53c25ce2009-05-18 14:51:59 +0100736 n->vc = qdev_get_vlan_client(dev,
aliguorib946a152009-04-17 17:11:08 +0000737 virtio_net_can_receive,
Mark McLoughlin463af532009-05-18 12:55:27 +0100738 virtio_net_receive, NULL,
aliguorib946a152009-04-17 17:11:08 +0000739 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000740 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000741
aliguori96d5e202009-01-07 17:47:15 +0000742 qemu_format_nic_info_str(n->vc, n->mac);
743
aliguorifbe78f42008-12-17 19:13:11 +0000744 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
745 n->tx_timer_active = 0;
746 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000747 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000748
aliguorib6503ed2009-02-05 22:36:28 +0000749 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000750
aliguorif21c0ed2009-02-05 22:36:32 +0000751 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
Michael S. Tsirkinffe63702009-06-21 19:51:18 +0300752 if (dev->nd->nvectors == NIC_NVECTORS_UNSPECIFIED)
753 n->vdev.nvectors = 3;
754 else
755 n->vdev.nvectors = dev->nd->nvectors;
aliguorif21c0ed2009-02-05 22:36:32 +0000756
aliguori9d6271b2009-02-05 22:36:04 +0000757 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000758 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100759
Paul Brook53c25ce2009-05-18 14:51:59 +0100760 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100761}