virtio: Remove malloc failure checks (Jan Kiszka)
[sniper_test.git] / hw / virtio-net.c
blobe83d1019c1867a96e1daa4fa8a8c0bbee2bbfbbb
1 /*
2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "virtio.h"
15 #include "net.h"
16 #include "qemu-timer.h"
17 #include "virtio-net.h"
19 #define VIRTIO_NET_VM_VERSION 6
21 #define MAC_TABLE_ENTRIES 32
22 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
24 typedef struct VirtIONet
26 VirtIODevice vdev;
27 uint8_t mac[ETH_ALEN];
28 uint16_t status;
29 VirtQueue *rx_vq;
30 VirtQueue *tx_vq;
31 VirtQueue *ctrl_vq;
32 VLANClientState *vc;
33 QEMUTimer *tx_timer;
34 int tx_timer_active;
35 int mergeable_rx_bufs;
36 int promisc;
37 int allmulti;
38 struct {
39 int in_use;
40 uint8_t *macs;
41 } mac_table;
42 uint32_t *vlans;
43 } VirtIONet;
45 /* TODO
46 * - we could suppress RX interrupt if we were so inclined.
49 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
51 return (VirtIONet *)vdev;
54 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
56 VirtIONet *n = to_virtio_net(vdev);
57 struct virtio_net_config netcfg;
59 netcfg.status = n->status;
60 memcpy(netcfg.mac, n->mac, ETH_ALEN);
61 memcpy(config, &netcfg, sizeof(netcfg));
64 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
66 VirtIONet *n = to_virtio_net(vdev);
67 struct virtio_net_config netcfg;
69 memcpy(&netcfg, config, sizeof(netcfg));
71 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
72 memcpy(n->mac, netcfg.mac, ETH_ALEN);
73 qemu_format_nic_info_str(n->vc, n->mac);
77 static void virtio_net_set_link_status(VLANClientState *vc)
79 VirtIONet *n = vc->opaque;
80 uint16_t old_status = n->status;
82 if (vc->link_down)
83 n->status &= ~VIRTIO_NET_S_LINK_UP;
84 else
85 n->status |= VIRTIO_NET_S_LINK_UP;
87 if (n->status != old_status)
88 virtio_notify_config(&n->vdev);
91 static void virtio_net_reset(VirtIODevice *vdev)
93 VirtIONet *n = to_virtio_net(vdev);
95 /* Reset back to compatibility mode */
96 n->promisc = 1;
97 n->allmulti = 0;
99 /* Flush any MAC and VLAN filter table state */
100 n->mac_table.in_use = 0;
101 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
102 memset(n->vlans, 0, MAX_VLAN >> 3);
105 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
107 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
108 (1 << VIRTIO_NET_F_STATUS) |
109 (1 << VIRTIO_NET_F_CTRL_VQ) |
110 (1 << VIRTIO_NET_F_CTRL_RX) |
111 (1 << VIRTIO_NET_F_CTRL_VLAN);
113 return features;
116 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
118 VirtIONet *n = to_virtio_net(vdev);
120 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
123 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
124 VirtQueueElement *elem)
126 uint8_t on;
128 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
129 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
130 exit(1);
133 on = ldub_p(elem->out_sg[1].iov_base);
135 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
136 n->promisc = on;
137 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
138 n->allmulti = on;
139 else
140 return VIRTIO_NET_ERR;
142 return VIRTIO_NET_OK;
145 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
146 VirtQueueElement *elem)
148 struct virtio_net_ctrl_mac mac_data;
150 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
151 elem->out_sg[1].iov_len < sizeof(mac_data) ||
152 elem->out_sg[2].iov_len < sizeof(mac_data))
153 return VIRTIO_NET_ERR;
155 n->mac_table.in_use = 0;
156 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
158 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
160 if (sizeof(mac_data.entries) +
161 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
162 return VIRTIO_NET_ERR;
164 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
165 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
166 mac_data.entries * ETH_ALEN);
167 n->mac_table.in_use += mac_data.entries;
168 } else {
169 n->promisc = 1;
170 return VIRTIO_NET_OK;
173 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
175 if (sizeof(mac_data.entries) +
176 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
177 return VIRTIO_NET_ERR;
179 if (mac_data.entries) {
180 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
181 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
182 elem->out_sg[2].iov_base + sizeof(mac_data),
183 mac_data.entries * ETH_ALEN);
184 n->mac_table.in_use += mac_data.entries;
185 } else
186 n->allmulti = 1;
189 return VIRTIO_NET_OK;
192 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
193 VirtQueueElement *elem)
195 uint16_t vid;
197 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
198 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
199 return VIRTIO_NET_ERR;
202 vid = lduw_le_p(elem->out_sg[1].iov_base);
204 if (vid >= MAX_VLAN)
205 return VIRTIO_NET_ERR;
207 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
208 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
209 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
210 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
211 else
212 return VIRTIO_NET_ERR;
214 return VIRTIO_NET_OK;
217 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
219 VirtIONet *n = to_virtio_net(vdev);
220 struct virtio_net_ctrl_hdr ctrl;
221 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
222 VirtQueueElement elem;
224 while (virtqueue_pop(vq, &elem)) {
225 if ((elem.in_num < 1) || (elem.out_num < 1)) {
226 fprintf(stderr, "virtio-net ctrl missing headers\n");
227 exit(1);
230 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
231 elem.out_sg[elem.in_num - 1].iov_len < sizeof(status)) {
232 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
233 exit(1);
236 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
237 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
239 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
240 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
241 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
242 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
243 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
244 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
246 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
248 virtqueue_push(vq, &elem, sizeof(status));
249 virtio_notify(vdev, vq);
253 /* RX */
255 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
259 static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
261 if (!virtio_queue_ready(n->rx_vq) ||
262 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
263 return 0;
265 if (virtio_queue_empty(n->rx_vq) ||
266 (n->mergeable_rx_bufs &&
267 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
268 virtio_queue_set_notification(n->rx_vq, 1);
269 return 0;
272 virtio_queue_set_notification(n->rx_vq, 0);
273 return 1;
276 static int virtio_net_can_receive(void *opaque)
278 VirtIONet *n = opaque;
280 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
283 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
285 int offset, i;
287 offset = i = 0;
288 while (offset < count && i < iovcnt) {
289 int len = MIN(iov[i].iov_len, count - offset);
290 memcpy(iov[i].iov_base, buf + offset, len);
291 offset += len;
292 i++;
295 return offset;
298 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
299 const void *buf, size_t size, size_t hdr_len)
301 struct virtio_net_hdr *hdr = iov[0].iov_base;
302 int offset = 0;
304 hdr->flags = 0;
305 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
307 /* We only ever receive a struct virtio_net_hdr from the tapfd,
308 * but we may be passing along a larger header to the guest.
310 iov[0].iov_base += hdr_len;
311 iov[0].iov_len -= hdr_len;
313 return offset;
316 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
318 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
319 static const uint8_t vlan[] = {0x81, 0x00};
320 uint8_t *ptr = (uint8_t *)buf;
321 int i;
323 if (n->promisc)
324 return 1;
326 #ifdef TAP_VNET_HDR
327 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
328 ptr += sizeof(struct virtio_net_hdr);
329 #endif
331 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
332 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
333 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
334 return 0;
337 if ((ptr[0] & 1) && n->allmulti)
338 return 1;
340 if (!memcmp(ptr, bcast, sizeof(bcast)))
341 return 1;
343 if (!memcmp(ptr, n->mac, ETH_ALEN))
344 return 1;
346 for (i = 0; i < n->mac_table.in_use; i++) {
347 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
348 return 1;
351 return 0;
354 static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
356 VirtIONet *n = opaque;
357 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
358 size_t hdr_len, offset, i;
360 if (!do_virtio_net_can_receive(n, size))
361 return;
363 if (!receive_filter(n, buf, size))
364 return;
366 /* hdr_len refers to the header we supply to the guest */
367 hdr_len = n->mergeable_rx_bufs ?
368 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
370 offset = i = 0;
372 while (offset < size) {
373 VirtQueueElement elem;
374 int len, total;
375 struct iovec sg[VIRTQUEUE_MAX_SIZE];
377 len = total = 0;
379 if ((i != 0 && !n->mergeable_rx_bufs) ||
380 virtqueue_pop(n->rx_vq, &elem) == 0) {
381 if (i == 0)
382 return;
383 fprintf(stderr, "virtio-net truncating packet\n");
384 exit(1);
387 if (elem.in_num < 1) {
388 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
389 exit(1);
392 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
393 fprintf(stderr, "virtio-net header not in first element\n");
394 exit(1);
397 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
399 if (i == 0) {
400 if (n->mergeable_rx_bufs)
401 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
403 offset += receive_header(n, sg, elem.in_num,
404 buf + offset, size - offset, hdr_len);
405 total += hdr_len;
408 /* copy in packet. ugh */
409 len = iov_fill(sg, elem.in_num,
410 buf + offset, size - offset);
411 total += len;
413 /* signal other side */
414 virtqueue_fill(n->rx_vq, &elem, total, i++);
416 offset += len;
419 if (mhdr)
420 mhdr->num_buffers = i;
422 virtqueue_flush(n->rx_vq, i);
423 virtio_notify(&n->vdev, n->rx_vq);
426 /* TX */
427 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
429 VirtQueueElement elem;
430 int has_vnet_hdr = 0;
432 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
433 return;
435 while (virtqueue_pop(vq, &elem)) {
436 ssize_t len = 0;
437 unsigned int out_num = elem.out_num;
438 struct iovec *out_sg = &elem.out_sg[0];
439 unsigned hdr_len;
441 /* hdr_len refers to the header received from the guest */
442 hdr_len = n->mergeable_rx_bufs ?
443 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
444 sizeof(struct virtio_net_hdr);
446 if (out_num < 1 || out_sg->iov_len != hdr_len) {
447 fprintf(stderr, "virtio-net header not in first element\n");
448 exit(1);
451 /* ignore the header if GSO is not supported */
452 if (!has_vnet_hdr) {
453 out_num--;
454 out_sg++;
455 len += hdr_len;
456 } else if (n->mergeable_rx_bufs) {
457 /* tapfd expects a struct virtio_net_hdr */
458 hdr_len -= sizeof(struct virtio_net_hdr);
459 out_sg->iov_len -= hdr_len;
460 len += hdr_len;
463 len += qemu_sendv_packet(n->vc, out_sg, out_num);
465 virtqueue_push(vq, &elem, len);
466 virtio_notify(&n->vdev, vq);
470 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
472 VirtIONet *n = to_virtio_net(vdev);
474 if (n->tx_timer_active) {
475 virtio_queue_set_notification(vq, 1);
476 qemu_del_timer(n->tx_timer);
477 n->tx_timer_active = 0;
478 virtio_net_flush_tx(n, vq);
479 } else {
480 qemu_mod_timer(n->tx_timer,
481 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
482 n->tx_timer_active = 1;
483 virtio_queue_set_notification(vq, 0);
487 static void virtio_net_tx_timer(void *opaque)
489 VirtIONet *n = opaque;
491 n->tx_timer_active = 0;
493 /* Just in case the driver is not ready on more */
494 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
495 return;
497 virtio_queue_set_notification(n->tx_vq, 1);
498 virtio_net_flush_tx(n, n->tx_vq);
501 static void virtio_net_save(QEMUFile *f, void *opaque)
503 VirtIONet *n = opaque;
505 virtio_save(&n->vdev, f);
507 qemu_put_buffer(f, n->mac, ETH_ALEN);
508 qemu_put_be32(f, n->tx_timer_active);
509 qemu_put_be32(f, n->mergeable_rx_bufs);
510 qemu_put_be16(f, n->status);
511 qemu_put_be32(f, n->promisc);
512 qemu_put_be32(f, n->allmulti);
513 qemu_put_be32(f, n->mac_table.in_use);
514 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
515 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
518 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
520 VirtIONet *n = opaque;
522 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
523 return -EINVAL;
525 virtio_load(&n->vdev, f);
527 qemu_get_buffer(f, n->mac, ETH_ALEN);
528 n->tx_timer_active = qemu_get_be32(f);
529 n->mergeable_rx_bufs = qemu_get_be32(f);
531 if (version_id >= 3)
532 n->status = qemu_get_be16(f);
534 if (version_id >= 4) {
535 n->promisc = qemu_get_be32(f);
536 n->allmulti = qemu_get_be32(f);
539 if (version_id >= 5) {
540 n->mac_table.in_use = qemu_get_be32(f);
541 /* MAC_TABLE_ENTRIES may be different from the saved image */
542 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
543 qemu_get_buffer(f, n->mac_table.macs,
544 n->mac_table.in_use * ETH_ALEN);
545 } else if (n->mac_table.in_use) {
546 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
547 n->promisc = 1;
548 n->mac_table.in_use = 0;
552 if (version_id >= 6)
553 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
555 if (n->tx_timer_active) {
556 qemu_mod_timer(n->tx_timer,
557 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
560 return 0;
563 PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
565 VirtIONet *n;
566 static int virtio_net_id;
568 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
569 PCI_VENDOR_ID_REDHAT_QUMRANET,
570 PCI_DEVICE_ID_VIRTIO_NET,
571 PCI_VENDOR_ID_REDHAT_QUMRANET,
572 VIRTIO_ID_NET,
573 PCI_CLASS_NETWORK_ETHERNET, 0x00,
574 sizeof(struct virtio_net_config),
575 sizeof(VirtIONet));
576 if (!n)
577 return NULL;
579 n->vdev.get_config = virtio_net_get_config;
580 n->vdev.set_config = virtio_net_set_config;
581 n->vdev.get_features = virtio_net_get_features;
582 n->vdev.set_features = virtio_net_set_features;
583 n->vdev.reset = virtio_net_reset;
584 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
585 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
586 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
587 memcpy(n->mac, nd->macaddr, ETH_ALEN);
588 n->status = VIRTIO_NET_S_LINK_UP;
589 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
590 virtio_net_receive, virtio_net_can_receive, n);
591 n->vc->link_status_changed = virtio_net_set_link_status;
593 qemu_format_nic_info_str(n->vc, n->mac);
595 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
596 n->tx_timer_active = 0;
597 n->mergeable_rx_bufs = 0;
598 n->promisc = 1; /* for compatibility */
600 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
602 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
604 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
605 virtio_net_save, virtio_net_load, n);
606 return (PCIDevice *)n;