xref: /openbmc/qemu/include/hw/virtio/virtio-net.h (revision 0b2ff2ce)
1 /*
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 #ifndef _QEMU_VIRTIO_NET_H
15 #define _QEMU_VIRTIO_NET_H
16 
17 #include "standard-headers/linux/virtio_net.h"
18 #include "hw/virtio/virtio.h"
19 
20 #define TYPE_VIRTIO_NET "virtio-net-device"
21 #define VIRTIO_NET(obj) \
22         OBJECT_CHECK(VirtIONet, (obj), TYPE_VIRTIO_NET)
23 
24 #define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS 2 /* Control channel offload
25                                          * configuration support */
26 
27 #define TX_TIMER_INTERVAL 150000 /* 150 us */
28 
29 /* Limit the number of packets that can be sent via a single flush
30  * of the TX queue.  This gives us a guaranteed exit condition and
31  * ensures fairness in the io path.  256 conveniently matches the
32  * length of the TX queue and shows a good balance of performance
33  * and latency. */
34 #define TX_BURST 256
35 
36 typedef struct virtio_net_conf
37 {
38     uint32_t txtimer;
39     int32_t txburst;
40     char *tx;
41 } virtio_net_conf;
42 
43 /* Maximum packet size we can receive from tap device: header + 64k */
44 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 << 10))
45 
46 typedef struct VirtIONetQueue {
47     VirtQueue *rx_vq;
48     VirtQueue *tx_vq;
49     QEMUTimer *tx_timer;
50     QEMUBH *tx_bh;
51     int tx_waiting;
52     struct {
53         VirtQueueElement elem;
54         ssize_t len;
55     } async_tx;
56     struct VirtIONet *n;
57 } VirtIONetQueue;
58 
59 typedef struct VirtIONet {
60     VirtIODevice parent_obj;
61     uint8_t mac[ETH_ALEN];
62     uint16_t status;
63     VirtIONetQueue *vqs;
64     VirtQueue *ctrl_vq;
65     NICState *nic;
66     uint32_t tx_timeout;
67     int32_t tx_burst;
68     uint32_t has_vnet_hdr;
69     size_t host_hdr_len;
70     size_t guest_hdr_len;
71     uint32_t host_features;
72     uint8_t has_ufo;
73     int mergeable_rx_bufs;
74     uint8_t promisc;
75     uint8_t allmulti;
76     uint8_t alluni;
77     uint8_t nomulti;
78     uint8_t nouni;
79     uint8_t nobcast;
80     uint8_t vhost_started;
81     struct {
82         uint32_t in_use;
83         uint32_t first_multi;
84         uint8_t multi_overflow;
85         uint8_t uni_overflow;
86         uint8_t *macs;
87     } mac_table;
88     uint32_t *vlans;
89     virtio_net_conf net_conf;
90     NICConf nic_conf;
91     DeviceState *qdev;
92     int multiqueue;
93     uint16_t max_queues;
94     uint16_t curr_queues;
95     size_t config_size;
96     char *netclient_name;
97     char *netclient_type;
98     uint64_t curr_guest_offloads;
99     QEMUTimer *announce_timer;
100     int announce_counter;
101 } VirtIONet;
102 
103 /*
104  * Control network offloads
105  *
106  * Dynamic offloads are available with the
107  * VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit.
108  */
109 #define VIRTIO_NET_CTRL_GUEST_OFFLOADS   5
110  #define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET        0
111 
112 #define DEFINE_VIRTIO_NET_FEATURES(_state, _field) \
113         DEFINE_PROP_BIT("any_layout", _state, _field, VIRTIO_F_ANY_LAYOUT, true), \
114         DEFINE_PROP_BIT("csum", _state, _field, VIRTIO_NET_F_CSUM, true), \
115         DEFINE_PROP_BIT("guest_csum", _state, _field, VIRTIO_NET_F_GUEST_CSUM, true), \
116         DEFINE_PROP_BIT("gso", _state, _field, VIRTIO_NET_F_GSO, true), \
117         DEFINE_PROP_BIT("guest_tso4", _state, _field, VIRTIO_NET_F_GUEST_TSO4, true), \
118         DEFINE_PROP_BIT("guest_tso6", _state, _field, VIRTIO_NET_F_GUEST_TSO6, true), \
119         DEFINE_PROP_BIT("guest_ecn", _state, _field, VIRTIO_NET_F_GUEST_ECN, true), \
120         DEFINE_PROP_BIT("guest_ufo", _state, _field, VIRTIO_NET_F_GUEST_UFO, true), \
121         DEFINE_PROP_BIT("guest_announce", _state, _field, VIRTIO_NET_F_GUEST_ANNOUNCE, true), \
122         DEFINE_PROP_BIT("host_tso4", _state, _field, VIRTIO_NET_F_HOST_TSO4, true), \
123         DEFINE_PROP_BIT("host_tso6", _state, _field, VIRTIO_NET_F_HOST_TSO6, true), \
124         DEFINE_PROP_BIT("host_ecn", _state, _field, VIRTIO_NET_F_HOST_ECN, true), \
125         DEFINE_PROP_BIT("host_ufo", _state, _field, VIRTIO_NET_F_HOST_UFO, true), \
126         DEFINE_PROP_BIT("mrg_rxbuf", _state, _field, VIRTIO_NET_F_MRG_RXBUF, true), \
127         DEFINE_PROP_BIT("status", _state, _field, VIRTIO_NET_F_STATUS, true), \
128         DEFINE_PROP_BIT("ctrl_vq", _state, _field, VIRTIO_NET_F_CTRL_VQ, true), \
129         DEFINE_PROP_BIT("ctrl_rx", _state, _field, VIRTIO_NET_F_CTRL_RX, true), \
130         DEFINE_PROP_BIT("ctrl_vlan", _state, _field, VIRTIO_NET_F_CTRL_VLAN, true), \
131         DEFINE_PROP_BIT("ctrl_rx_extra", _state, _field, VIRTIO_NET_F_CTRL_RX_EXTRA, true), \
132         DEFINE_PROP_BIT("ctrl_mac_addr", _state, _field, VIRTIO_NET_F_CTRL_MAC_ADDR, true), \
133         DEFINE_PROP_BIT("ctrl_guest_offloads", _state, _field, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true), \
134         DEFINE_PROP_BIT("mq", _state, _field, VIRTIO_NET_F_MQ, false)
135 
136 #define DEFINE_VIRTIO_NET_PROPERTIES(_state, _field)                           \
137     DEFINE_PROP_UINT32("x-txtimer", _state, _field.txtimer, TX_TIMER_INTERVAL),\
138     DEFINE_PROP_INT32("x-txburst", _state, _field.txburst, TX_BURST),          \
139     DEFINE_PROP_STRING("tx", _state, _field.tx)
140 
141 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
142                                    const char *type);
143 
144 #endif
145