root / hw / virtio-net.c @ b4b784fe
History | View | Annotate | Download (25.4 kB)
1 | fbe78f4f | aliguori | /*
|
---|---|---|---|
2 | fbe78f4f | aliguori | * Virtio Network Device
|
3 | fbe78f4f | aliguori | *
|
4 | fbe78f4f | aliguori | * Copyright IBM, Corp. 2007
|
5 | fbe78f4f | aliguori | *
|
6 | fbe78f4f | aliguori | * Authors:
|
7 | fbe78f4f | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | fbe78f4f | aliguori | *
|
9 | fbe78f4f | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | fbe78f4f | aliguori | * the COPYING file in the top-level directory.
|
11 | fbe78f4f | aliguori | *
|
12 | fbe78f4f | aliguori | */
|
13 | fbe78f4f | aliguori | |
14 | fbe78f4f | aliguori | #include "virtio.h" |
15 | fbe78f4f | aliguori | #include "net.h" |
16 | 7200ac3c | Mark McLoughlin | #include "net/checksum.h" |
17 | a8ed73f7 | Mark McLoughlin | #include "net/tap.h" |
18 | fbe78f4f | aliguori | #include "qemu-timer.h" |
19 | fbe78f4f | aliguori | #include "virtio-net.h" |
20 | fbe78f4f | aliguori | |
21 | 0ce0e8f4 | Mark McLoughlin | #define VIRTIO_NET_VM_VERSION 11 |
22 | b6503ed9 | aliguori | |
23 | 4ffb17f5 | Alex Williamson | #define MAC_TABLE_ENTRIES 64 |
24 | f21c0ed9 | aliguori | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
25 | 9d6271b8 | aliguori | |
26 | fbe78f4f | aliguori | typedef struct VirtIONet |
27 | fbe78f4f | aliguori | { |
28 | fbe78f4f | aliguori | VirtIODevice vdev; |
29 | 79674068 | aliguori | uint8_t mac[ETH_ALEN]; |
30 | 554c97dd | aliguori | uint16_t status; |
31 | fbe78f4f | aliguori | VirtQueue *rx_vq; |
32 | fbe78f4f | aliguori | VirtQueue *tx_vq; |
33 | 3d11d36c | aliguori | VirtQueue *ctrl_vq; |
34 | eb6b6c12 | Mark McLoughlin | NICState *nic; |
35 | fbe78f4f | aliguori | QEMUTimer *tx_timer; |
36 | fbe78f4f | aliguori | int tx_timer_active;
|
37 | 3a330134 | Mark McLoughlin | uint32_t has_vnet_hdr; |
38 | 0ce0e8f4 | Mark McLoughlin | uint8_t has_ufo; |
39 | 6243375f | Mark McLoughlin | struct {
|
40 | 6243375f | Mark McLoughlin | VirtQueueElement elem; |
41 | 6243375f | Mark McLoughlin | ssize_t len; |
42 | 6243375f | Mark McLoughlin | } async_tx; |
43 | fbe78f4f | aliguori | int mergeable_rx_bufs;
|
44 | f10c592e | Alex Williamson | uint8_t promisc; |
45 | f10c592e | Alex Williamson | uint8_t allmulti; |
46 | 015cb166 | Alex Williamson | uint8_t alluni; |
47 | 015cb166 | Alex Williamson | uint8_t nomulti; |
48 | 015cb166 | Alex Williamson | uint8_t nouni; |
49 | 015cb166 | Alex Williamson | uint8_t nobcast; |
50 | b6503ed9 | aliguori | struct {
|
51 | b6503ed9 | aliguori | int in_use;
|
52 | 2d9aba39 | Alex Williamson | int first_multi;
|
53 | 8fd2a2f1 | Alex Williamson | uint8_t multi_overflow; |
54 | 8fd2a2f1 | Alex Williamson | uint8_t uni_overflow; |
55 | b6503ed9 | aliguori | uint8_t *macs; |
56 | b6503ed9 | aliguori | } mac_table; |
57 | f21c0ed9 | aliguori | uint32_t *vlans; |
58 | fbe78f4f | aliguori | } VirtIONet; |
59 | fbe78f4f | aliguori | |
60 | fbe78f4f | aliguori | /* TODO
|
61 | fbe78f4f | aliguori | * - we could suppress RX interrupt if we were so inclined.
|
62 | fbe78f4f | aliguori | */
|
63 | fbe78f4f | aliguori | |
64 | fbe78f4f | aliguori | static VirtIONet *to_virtio_net(VirtIODevice *vdev)
|
65 | fbe78f4f | aliguori | { |
66 | fbe78f4f | aliguori | return (VirtIONet *)vdev;
|
67 | fbe78f4f | aliguori | } |
68 | fbe78f4f | aliguori | |
69 | 0f03eca6 | aliguori | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
70 | fbe78f4f | aliguori | { |
71 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
72 | fbe78f4f | aliguori | struct virtio_net_config netcfg;
|
73 | fbe78f4f | aliguori | |
74 | 554c97dd | aliguori | netcfg.status = n->status; |
75 | 79674068 | aliguori | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
76 | fbe78f4f | aliguori | memcpy(config, &netcfg, sizeof(netcfg));
|
77 | fbe78f4f | aliguori | } |
78 | fbe78f4f | aliguori | |
79 | 0f03eca6 | aliguori | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
80 | 0f03eca6 | aliguori | { |
81 | 0f03eca6 | aliguori | VirtIONet *n = to_virtio_net(vdev); |
82 | 0f03eca6 | aliguori | struct virtio_net_config netcfg;
|
83 | 0f03eca6 | aliguori | |
84 | 0f03eca6 | aliguori | memcpy(&netcfg, config, sizeof(netcfg));
|
85 | 0f03eca6 | aliguori | |
86 | 79674068 | aliguori | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
|
87 | 79674068 | aliguori | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
88 | eb6b6c12 | Mark McLoughlin | qemu_format_nic_info_str(&n->nic->nc, n->mac); |
89 | 0f03eca6 | aliguori | } |
90 | 0f03eca6 | aliguori | } |
91 | 0f03eca6 | aliguori | |
92 | eb6b6c12 | Mark McLoughlin | static void virtio_net_set_link_status(VLANClientState *nc) |
93 | 554c97dd | aliguori | { |
94 | eb6b6c12 | Mark McLoughlin | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
95 | 554c97dd | aliguori | uint16_t old_status = n->status; |
96 | 554c97dd | aliguori | |
97 | eb6b6c12 | Mark McLoughlin | if (nc->link_down)
|
98 | 554c97dd | aliguori | n->status &= ~VIRTIO_NET_S_LINK_UP; |
99 | 554c97dd | aliguori | else
|
100 | 554c97dd | aliguori | n->status |= VIRTIO_NET_S_LINK_UP; |
101 | 554c97dd | aliguori | |
102 | 554c97dd | aliguori | if (n->status != old_status)
|
103 | 554c97dd | aliguori | virtio_notify_config(&n->vdev); |
104 | 554c97dd | aliguori | } |
105 | 554c97dd | aliguori | |
106 | 002437cd | aliguori | static void virtio_net_reset(VirtIODevice *vdev) |
107 | 002437cd | aliguori | { |
108 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
109 | 002437cd | aliguori | |
110 | 002437cd | aliguori | /* Reset back to compatibility mode */
|
111 | 002437cd | aliguori | n->promisc = 1;
|
112 | 002437cd | aliguori | n->allmulti = 0;
|
113 | 015cb166 | Alex Williamson | n->alluni = 0;
|
114 | 015cb166 | Alex Williamson | n->nomulti = 0;
|
115 | 015cb166 | Alex Williamson | n->nouni = 0;
|
116 | 015cb166 | Alex Williamson | n->nobcast = 0;
|
117 | b6503ed9 | aliguori | |
118 | f21c0ed9 | aliguori | /* Flush any MAC and VLAN filter table state */
|
119 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
120 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = 0;
|
121 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 0;
|
122 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 0;
|
123 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
124 | f21c0ed9 | aliguori | memset(n->vlans, 0, MAX_VLAN >> 3); |
125 | 002437cd | aliguori | } |
126 | 002437cd | aliguori | |
127 | 3a330134 | Mark McLoughlin | static int peer_has_vnet_hdr(VirtIONet *n) |
128 | 3a330134 | Mark McLoughlin | { |
129 | eb6b6c12 | Mark McLoughlin | if (!n->nic->nc.peer)
|
130 | 3a330134 | Mark McLoughlin | return 0; |
131 | 3a330134 | Mark McLoughlin | |
132 | 665a3b07 | Mark McLoughlin | if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
|
133 | 3a330134 | Mark McLoughlin | return 0; |
134 | 3a330134 | Mark McLoughlin | |
135 | eb6b6c12 | Mark McLoughlin | n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer); |
136 | 3a330134 | Mark McLoughlin | |
137 | 3a330134 | Mark McLoughlin | return n->has_vnet_hdr;
|
138 | 3a330134 | Mark McLoughlin | } |
139 | 3a330134 | Mark McLoughlin | |
140 | 0ce0e8f4 | Mark McLoughlin | static int peer_has_ufo(VirtIONet *n) |
141 | 0ce0e8f4 | Mark McLoughlin | { |
142 | 0ce0e8f4 | Mark McLoughlin | if (!peer_has_vnet_hdr(n))
|
143 | 0ce0e8f4 | Mark McLoughlin | return 0; |
144 | 0ce0e8f4 | Mark McLoughlin | |
145 | eb6b6c12 | Mark McLoughlin | n->has_ufo = tap_has_ufo(n->nic->nc.peer); |
146 | 0ce0e8f4 | Mark McLoughlin | |
147 | 0ce0e8f4 | Mark McLoughlin | return n->has_ufo;
|
148 | 0ce0e8f4 | Mark McLoughlin | } |
149 | 0ce0e8f4 | Mark McLoughlin | |
150 | fbe78f4f | aliguori | static uint32_t virtio_net_get_features(VirtIODevice *vdev)
|
151 | fbe78f4f | aliguori | { |
152 | 3a330134 | Mark McLoughlin | VirtIONet *n = to_virtio_net(vdev); |
153 | 3d11d36c | aliguori | uint32_t features = (1 << VIRTIO_NET_F_MAC) |
|
154 | e16044ef | Mark McLoughlin | (1 << VIRTIO_NET_F_MRG_RXBUF) |
|
155 | 3d11d36c | aliguori | (1 << VIRTIO_NET_F_STATUS) |
|
156 | b6503ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_VQ) |
|
157 | f21c0ed9 | aliguori | (1 << VIRTIO_NET_F_CTRL_RX) |
|
158 | 015cb166 | Alex Williamson | (1 << VIRTIO_NET_F_CTRL_VLAN) |
|
159 | 015cb166 | Alex Williamson | (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
|
160 | fbe78f4f | aliguori | |
161 | 3a330134 | Mark McLoughlin | if (peer_has_vnet_hdr(n)) {
|
162 | eb6b6c12 | Mark McLoughlin | tap_using_vnet_hdr(n->nic->nc.peer, 1);
|
163 | 3a330134 | Mark McLoughlin | |
164 | 3a330134 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_CSUM);
|
165 | 3a330134 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_HOST_TSO4);
|
166 | 3a330134 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_HOST_TSO6);
|
167 | 3a330134 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_HOST_ECN);
|
168 | f5436dd9 | Mark McLoughlin | |
169 | f5436dd9 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
|
170 | f5436dd9 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
|
171 | f5436dd9 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
|
172 | f5436dd9 | Mark McLoughlin | features |= (1 << VIRTIO_NET_F_GUEST_ECN);
|
173 | 6c9f58ba | Sridhar Samudrala | |
174 | 0ce0e8f4 | Mark McLoughlin | if (peer_has_ufo(n)) {
|
175 | 6c9f58ba | Sridhar Samudrala | features |= (1 << VIRTIO_NET_F_GUEST_UFO);
|
176 | 6c9f58ba | Sridhar Samudrala | features |= (1 << VIRTIO_NET_F_HOST_UFO);
|
177 | 6c9f58ba | Sridhar Samudrala | } |
178 | 3a330134 | Mark McLoughlin | } |
179 | 3a330134 | Mark McLoughlin | |
180 | fbe78f4f | aliguori | return features;
|
181 | fbe78f4f | aliguori | } |
182 | fbe78f4f | aliguori | |
183 | 8eca6b1b | aliguori | static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
|
184 | 8eca6b1b | aliguori | { |
185 | 8eca6b1b | aliguori | uint32_t features = 0;
|
186 | 8eca6b1b | aliguori | |
187 | 8eca6b1b | aliguori | /* Linux kernel 2.6.25. It understood MAC (as everyone must),
|
188 | 8eca6b1b | aliguori | * but also these: */
|
189 | 8eca6b1b | aliguori | features |= (1 << VIRTIO_NET_F_MAC);
|
190 | 184bd048 | Dustin Kirkland | features |= (1 << VIRTIO_NET_F_CSUM);
|
191 | 184bd048 | Dustin Kirkland | features |= (1 << VIRTIO_NET_F_HOST_TSO4);
|
192 | 184bd048 | Dustin Kirkland | features |= (1 << VIRTIO_NET_F_HOST_TSO6);
|
193 | 184bd048 | Dustin Kirkland | features |= (1 << VIRTIO_NET_F_HOST_ECN);
|
194 | 8eca6b1b | aliguori | |
195 | 8eca6b1b | aliguori | return features & virtio_net_get_features(vdev);
|
196 | 8eca6b1b | aliguori | } |
197 | 8eca6b1b | aliguori | |
198 | fbe78f4f | aliguori | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
199 | fbe78f4f | aliguori | { |
200 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
201 | fbe78f4f | aliguori | |
202 | fbe78f4f | aliguori | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
|
203 | f5436dd9 | Mark McLoughlin | |
204 | f5436dd9 | Mark McLoughlin | if (n->has_vnet_hdr) {
|
205 | eb6b6c12 | Mark McLoughlin | tap_set_offload(n->nic->nc.peer, |
206 | f5436dd9 | Mark McLoughlin | (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
|
207 | f5436dd9 | Mark McLoughlin | (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
|
208 | f5436dd9 | Mark McLoughlin | (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
|
209 | 6c9f58ba | Sridhar Samudrala | (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
|
210 | 6c9f58ba | Sridhar Samudrala | (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
|
211 | f5436dd9 | Mark McLoughlin | } |
212 | fbe78f4f | aliguori | } |
213 | fbe78f4f | aliguori | |
214 | 002437cd | aliguori | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
215 | 002437cd | aliguori | VirtQueueElement *elem) |
216 | 002437cd | aliguori | { |
217 | 002437cd | aliguori | uint8_t on; |
218 | 002437cd | aliguori | |
219 | 002437cd | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { |
220 | 002437cd | aliguori | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
|
221 | 002437cd | aliguori | exit(1);
|
222 | 002437cd | aliguori | } |
223 | 002437cd | aliguori | |
224 | 002437cd | aliguori | on = ldub_p(elem->out_sg[1].iov_base);
|
225 | 002437cd | aliguori | |
226 | 002437cd | aliguori | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
|
227 | 002437cd | aliguori | n->promisc = on; |
228 | 002437cd | aliguori | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) |
229 | 002437cd | aliguori | n->allmulti = on; |
230 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI) |
231 | 015cb166 | Alex Williamson | n->alluni = on; |
232 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI) |
233 | 015cb166 | Alex Williamson | n->nomulti = on; |
234 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI) |
235 | 015cb166 | Alex Williamson | n->nouni = on; |
236 | 015cb166 | Alex Williamson | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST) |
237 | 015cb166 | Alex Williamson | n->nobcast = on; |
238 | 002437cd | aliguori | else
|
239 | 002437cd | aliguori | return VIRTIO_NET_ERR;
|
240 | 002437cd | aliguori | |
241 | 002437cd | aliguori | return VIRTIO_NET_OK;
|
242 | 002437cd | aliguori | } |
243 | 002437cd | aliguori | |
244 | b6503ed9 | aliguori | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
245 | b6503ed9 | aliguori | VirtQueueElement *elem) |
246 | b6503ed9 | aliguori | { |
247 | b6503ed9 | aliguori | struct virtio_net_ctrl_mac mac_data;
|
248 | b6503ed9 | aliguori | |
249 | b6503ed9 | aliguori | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || |
250 | b6503ed9 | aliguori | elem->out_sg[1].iov_len < sizeof(mac_data) || |
251 | b6503ed9 | aliguori | elem->out_sg[2].iov_len < sizeof(mac_data)) |
252 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
253 | b6503ed9 | aliguori | |
254 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
255 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = 0;
|
256 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 0;
|
257 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 0;
|
258 | b6503ed9 | aliguori | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
|
259 | b6503ed9 | aliguori | |
260 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
|
261 | b6503ed9 | aliguori | |
262 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
263 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
|
264 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
265 | b6503ed9 | aliguori | |
266 | b6503ed9 | aliguori | if (mac_data.entries <= MAC_TABLE_ENTRIES) {
|
267 | b6503ed9 | aliguori | memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data), |
268 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
269 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
270 | b6503ed9 | aliguori | } else {
|
271 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = 1;
|
272 | b6503ed9 | aliguori | } |
273 | b6503ed9 | aliguori | |
274 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = n->mac_table.in_use; |
275 | 2d9aba39 | Alex Williamson | |
276 | b6503ed9 | aliguori | mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
|
277 | b6503ed9 | aliguori | |
278 | b6503ed9 | aliguori | if (sizeof(mac_data.entries) + |
279 | b6503ed9 | aliguori | (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
|
280 | b6503ed9 | aliguori | return VIRTIO_NET_ERR;
|
281 | b6503ed9 | aliguori | |
282 | b6503ed9 | aliguori | if (mac_data.entries) {
|
283 | b6503ed9 | aliguori | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
|
284 | b6503ed9 | aliguori | memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), |
285 | b6503ed9 | aliguori | elem->out_sg[2].iov_base + sizeof(mac_data), |
286 | b6503ed9 | aliguori | mac_data.entries * ETH_ALEN); |
287 | b6503ed9 | aliguori | n->mac_table.in_use += mac_data.entries; |
288 | 8fd2a2f1 | Alex Williamson | } else {
|
289 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = 1;
|
290 | 8fd2a2f1 | Alex Williamson | } |
291 | b6503ed9 | aliguori | } |
292 | b6503ed9 | aliguori | |
293 | b6503ed9 | aliguori | return VIRTIO_NET_OK;
|
294 | b6503ed9 | aliguori | } |
295 | b6503ed9 | aliguori | |
296 | f21c0ed9 | aliguori | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
297 | f21c0ed9 | aliguori | VirtQueueElement *elem) |
298 | f21c0ed9 | aliguori | { |
299 | f21c0ed9 | aliguori | uint16_t vid; |
300 | f21c0ed9 | aliguori | |
301 | f21c0ed9 | aliguori | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { |
302 | f21c0ed9 | aliguori | fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
|
303 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
304 | f21c0ed9 | aliguori | } |
305 | f21c0ed9 | aliguori | |
306 | f21c0ed9 | aliguori | vid = lduw_le_p(elem->out_sg[1].iov_base);
|
307 | f21c0ed9 | aliguori | |
308 | f21c0ed9 | aliguori | if (vid >= MAX_VLAN)
|
309 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
310 | f21c0ed9 | aliguori | |
311 | f21c0ed9 | aliguori | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
|
312 | f21c0ed9 | aliguori | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); |
313 | f21c0ed9 | aliguori | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) |
314 | f21c0ed9 | aliguori | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); |
315 | f21c0ed9 | aliguori | else
|
316 | f21c0ed9 | aliguori | return VIRTIO_NET_ERR;
|
317 | f21c0ed9 | aliguori | |
318 | f21c0ed9 | aliguori | return VIRTIO_NET_OK;
|
319 | f21c0ed9 | aliguori | } |
320 | f21c0ed9 | aliguori | |
321 | 3d11d36c | aliguori | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
322 | 3d11d36c | aliguori | { |
323 | 002437cd | aliguori | VirtIONet *n = to_virtio_net(vdev); |
324 | 3d11d36c | aliguori | struct virtio_net_ctrl_hdr ctrl;
|
325 | 3d11d36c | aliguori | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
326 | 3d11d36c | aliguori | VirtQueueElement elem; |
327 | 3d11d36c | aliguori | |
328 | 3d11d36c | aliguori | while (virtqueue_pop(vq, &elem)) {
|
329 | 3d11d36c | aliguori | if ((elem.in_num < 1) || (elem.out_num < 1)) { |
330 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl missing headers\n");
|
331 | 3d11d36c | aliguori | exit(1);
|
332 | 3d11d36c | aliguori | } |
333 | 3d11d36c | aliguori | |
334 | 3d11d36c | aliguori | if (elem.out_sg[0].iov_len < sizeof(ctrl) || |
335 | c6bb9a32 | aliguori | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
336 | 3d11d36c | aliguori | fprintf(stderr, "virtio-net ctrl header not in correct element\n");
|
337 | 3d11d36c | aliguori | exit(1);
|
338 | 3d11d36c | aliguori | } |
339 | 3d11d36c | aliguori | |
340 | 3d11d36c | aliguori | ctrl.class = ldub_p(elem.out_sg[0].iov_base);
|
341 | 3d11d36c | aliguori | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); |
342 | 3d11d36c | aliguori | |
343 | 002437cd | aliguori | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
|
344 | 002437cd | aliguori | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); |
345 | b6503ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
346 | b6503ed9 | aliguori | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); |
347 | f21c0ed9 | aliguori | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
348 | f21c0ed9 | aliguori | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); |
349 | 002437cd | aliguori | |
350 | 3d11d36c | aliguori | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
|
351 | 3d11d36c | aliguori | |
352 | 3d11d36c | aliguori | virtqueue_push(vq, &elem, sizeof(status));
|
353 | 3d11d36c | aliguori | virtio_notify(vdev, vq); |
354 | 3d11d36c | aliguori | } |
355 | 3d11d36c | aliguori | } |
356 | 3d11d36c | aliguori | |
357 | fbe78f4f | aliguori | /* RX */
|
358 | fbe78f4f | aliguori | |
359 | fbe78f4f | aliguori | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) |
360 | fbe78f4f | aliguori | { |
361 | 8aeff62d | Mark McLoughlin | VirtIONet *n = to_virtio_net(vdev); |
362 | 8aeff62d | Mark McLoughlin | |
363 | eb6b6c12 | Mark McLoughlin | qemu_flush_queued_packets(&n->nic->nc); |
364 | a61d1f67 | Glauber Costa | |
365 | a61d1f67 | Glauber Costa | /* We now have RX buffers, signal to the IO thread to break out of the
|
366 | a61d1f67 | Glauber Costa | * select to re-poll the tap file descriptor */
|
367 | a61d1f67 | Glauber Costa | qemu_notify_event(); |
368 | fbe78f4f | aliguori | } |
369 | fbe78f4f | aliguori | |
370 | eb6b6c12 | Mark McLoughlin | static int virtio_net_can_receive(VLANClientState *nc) |
371 | fbe78f4f | aliguori | { |
372 | eb6b6c12 | Mark McLoughlin | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
373 | cdd5cc12 | Mark McLoughlin | |
374 | fbe78f4f | aliguori | if (!virtio_queue_ready(n->rx_vq) ||
|
375 | fbe78f4f | aliguori | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) |
376 | fbe78f4f | aliguori | return 0; |
377 | fbe78f4f | aliguori | |
378 | cdd5cc12 | Mark McLoughlin | return 1; |
379 | cdd5cc12 | Mark McLoughlin | } |
380 | cdd5cc12 | Mark McLoughlin | |
381 | cdd5cc12 | Mark McLoughlin | static int virtio_net_has_buffers(VirtIONet *n, int bufsize) |
382 | cdd5cc12 | Mark McLoughlin | { |
383 | fbe78f4f | aliguori | if (virtio_queue_empty(n->rx_vq) ||
|
384 | fbe78f4f | aliguori | (n->mergeable_rx_bufs && |
385 | fbe78f4f | aliguori | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
|
386 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 1);
|
387 | fbe78f4f | aliguori | return 0; |
388 | fbe78f4f | aliguori | } |
389 | fbe78f4f | aliguori | |
390 | fbe78f4f | aliguori | virtio_queue_set_notification(n->rx_vq, 0);
|
391 | fbe78f4f | aliguori | return 1; |
392 | fbe78f4f | aliguori | } |
393 | fbe78f4f | aliguori | |
394 | 1d41b0c1 | Anthony Liguori | /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
|
395 | 1d41b0c1 | Anthony Liguori | * it never finds out that the packets don't have valid checksums. This
|
396 | 1d41b0c1 | Anthony Liguori | * causes dhclient to get upset. Fedora's carried a patch for ages to
|
397 | 1d41b0c1 | Anthony Liguori | * fix this with Xen but it hasn't appeared in an upstream release of
|
398 | 1d41b0c1 | Anthony Liguori | * dhclient yet.
|
399 | 1d41b0c1 | Anthony Liguori | *
|
400 | 1d41b0c1 | Anthony Liguori | * To avoid breaking existing guests, we catch udp packets and add
|
401 | 1d41b0c1 | Anthony Liguori | * checksums. This is terrible but it's better than hacking the guest
|
402 | 1d41b0c1 | Anthony Liguori | * kernels.
|
403 | 1d41b0c1 | Anthony Liguori | *
|
404 | 1d41b0c1 | Anthony Liguori | * N.B. if we introduce a zero-copy API, this operation is no longer free so
|
405 | 1d41b0c1 | Anthony Liguori | * we should provide a mechanism to disable it to avoid polluting the host
|
406 | 1d41b0c1 | Anthony Liguori | * cache.
|
407 | 1d41b0c1 | Anthony Liguori | */
|
408 | 1d41b0c1 | Anthony Liguori | static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, |
409 | 1d41b0c1 | Anthony Liguori | const uint8_t *buf, size_t size)
|
410 | 1d41b0c1 | Anthony Liguori | { |
411 | 1d41b0c1 | Anthony Liguori | if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ |
412 | 1d41b0c1 | Anthony Liguori | (size > 27 && size < 1500) && /* normal sized MTU */ |
413 | 1d41b0c1 | Anthony Liguori | (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ |
414 | 1d41b0c1 | Anthony Liguori | (buf[23] == 17) && /* ip.protocol == UDP */ |
415 | 1d41b0c1 | Anthony Liguori | (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ |
416 | 1d41b0c1 | Anthony Liguori | /* FIXME this cast is evil */
|
417 | 1d41b0c1 | Anthony Liguori | net_checksum_calculate((uint8_t *)buf, size); |
418 | 1d41b0c1 | Anthony Liguori | hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; |
419 | 1d41b0c1 | Anthony Liguori | } |
420 | 1d41b0c1 | Anthony Liguori | } |
421 | 1d41b0c1 | Anthony Liguori | |
422 | fbe78f4f | aliguori | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) |
423 | fbe78f4f | aliguori | { |
424 | fbe78f4f | aliguori | int offset, i;
|
425 | fbe78f4f | aliguori | |
426 | fbe78f4f | aliguori | offset = i = 0;
|
427 | fbe78f4f | aliguori | while (offset < count && i < iovcnt) {
|
428 | fbe78f4f | aliguori | int len = MIN(iov[i].iov_len, count - offset);
|
429 | fbe78f4f | aliguori | memcpy(iov[i].iov_base, buf + offset, len); |
430 | fbe78f4f | aliguori | offset += len; |
431 | fbe78f4f | aliguori | i++; |
432 | fbe78f4f | aliguori | } |
433 | fbe78f4f | aliguori | |
434 | fbe78f4f | aliguori | return offset;
|
435 | fbe78f4f | aliguori | } |
436 | fbe78f4f | aliguori | |
437 | fbe78f4f | aliguori | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, |
438 | 4689f4b3 | aliguori | const void *buf, size_t size, size_t hdr_len) |
439 | fbe78f4f | aliguori | { |
440 | 3f4cb3d3 | blueswir1 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
441 | fbe78f4f | aliguori | int offset = 0; |
442 | fbe78f4f | aliguori | |
443 | fbe78f4f | aliguori | hdr->flags = 0;
|
444 | fbe78f4f | aliguori | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
445 | fbe78f4f | aliguori | |
446 | 3a330134 | Mark McLoughlin | if (n->has_vnet_hdr) {
|
447 | 3a330134 | Mark McLoughlin | memcpy(hdr, buf, sizeof(*hdr));
|
448 | 3a330134 | Mark McLoughlin | offset = sizeof(*hdr);
|
449 | 1d41b0c1 | Anthony Liguori | work_around_broken_dhclient(hdr, buf + offset, size - offset); |
450 | 3a330134 | Mark McLoughlin | } |
451 | 3a330134 | Mark McLoughlin | |
452 | fbe78f4f | aliguori | /* We only ever receive a struct virtio_net_hdr from the tapfd,
|
453 | fbe78f4f | aliguori | * but we may be passing along a larger header to the guest.
|
454 | fbe78f4f | aliguori | */
|
455 | fbe78f4f | aliguori | iov[0].iov_base += hdr_len;
|
456 | fbe78f4f | aliguori | iov[0].iov_len -= hdr_len;
|
457 | fbe78f4f | aliguori | |
458 | fbe78f4f | aliguori | return offset;
|
459 | fbe78f4f | aliguori | } |
460 | fbe78f4f | aliguori | |
461 | 3831ab20 | aliguori | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
462 | 3831ab20 | aliguori | { |
463 | 3831ab20 | aliguori | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
464 | f21c0ed9 | aliguori | static const uint8_t vlan[] = {0x81, 0x00}; |
465 | 3831ab20 | aliguori | uint8_t *ptr = (uint8_t *)buf; |
466 | b6503ed9 | aliguori | int i;
|
467 | 3831ab20 | aliguori | |
468 | 3831ab20 | aliguori | if (n->promisc)
|
469 | 3831ab20 | aliguori | return 1; |
470 | 3831ab20 | aliguori | |
471 | 3a330134 | Mark McLoughlin | if (n->has_vnet_hdr) {
|
472 | 3a330134 | Mark McLoughlin | ptr += sizeof(struct virtio_net_hdr); |
473 | 3a330134 | Mark McLoughlin | } |
474 | 3a330134 | Mark McLoughlin | |
475 | f21c0ed9 | aliguori | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
476 | f21c0ed9 | aliguori | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; |
477 | f21c0ed9 | aliguori | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) |
478 | f21c0ed9 | aliguori | return 0; |
479 | f21c0ed9 | aliguori | } |
480 | f21c0ed9 | aliguori | |
481 | bbe2f399 | Alex Williamson | if (ptr[0] & 1) { // multicast |
482 | bbe2f399 | Alex Williamson | if (!memcmp(ptr, bcast, sizeof(bcast))) { |
483 | 015cb166 | Alex Williamson | return !n->nobcast;
|
484 | 015cb166 | Alex Williamson | } else if (n->nomulti) { |
485 | 015cb166 | Alex Williamson | return 0; |
486 | 8fd2a2f1 | Alex Williamson | } else if (n->allmulti || n->mac_table.multi_overflow) { |
487 | bbe2f399 | Alex Williamson | return 1; |
488 | bbe2f399 | Alex Williamson | } |
489 | 2d9aba39 | Alex Williamson | |
490 | 2d9aba39 | Alex Williamson | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
|
491 | 2d9aba39 | Alex Williamson | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
|
492 | 2d9aba39 | Alex Williamson | return 1; |
493 | 2d9aba39 | Alex Williamson | } |
494 | 2d9aba39 | Alex Williamson | } |
495 | bbe2f399 | Alex Williamson | } else { // unicast |
496 | 015cb166 | Alex Williamson | if (n->nouni) {
|
497 | 015cb166 | Alex Williamson | return 0; |
498 | 015cb166 | Alex Williamson | } else if (n->alluni || n->mac_table.uni_overflow) { |
499 | 8fd2a2f1 | Alex Williamson | return 1; |
500 | 8fd2a2f1 | Alex Williamson | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { |
501 | bbe2f399 | Alex Williamson | return 1; |
502 | bbe2f399 | Alex Williamson | } |
503 | 3831ab20 | aliguori | |
504 | 2d9aba39 | Alex Williamson | for (i = 0; i < n->mac_table.first_multi; i++) { |
505 | 2d9aba39 | Alex Williamson | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
|
506 | 2d9aba39 | Alex Williamson | return 1; |
507 | 2d9aba39 | Alex Williamson | } |
508 | 2d9aba39 | Alex Williamson | } |
509 | b6503ed9 | aliguori | } |
510 | b6503ed9 | aliguori | |
511 | 3831ab20 | aliguori | return 0; |
512 | 3831ab20 | aliguori | } |
513 | 3831ab20 | aliguori | |
514 | eb6b6c12 | Mark McLoughlin | static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
515 | fbe78f4f | aliguori | { |
516 | eb6b6c12 | Mark McLoughlin | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
517 | fbe78f4f | aliguori | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
518 | 4689f4b3 | aliguori | size_t hdr_len, offset, i; |
519 | fbe78f4f | aliguori | |
520 | eb6b6c12 | Mark McLoughlin | if (!virtio_net_can_receive(&n->nic->nc))
|
521 | cdd5cc12 | Mark McLoughlin | return -1; |
522 | cdd5cc12 | Mark McLoughlin | |
523 | cdd5cc12 | Mark McLoughlin | if (!virtio_net_has_buffers(n, size))
|
524 | 8aeff62d | Mark McLoughlin | return 0; |
525 | fbe78f4f | aliguori | |
526 | 3831ab20 | aliguori | if (!receive_filter(n, buf, size))
|
527 | 4f1c942b | Mark McLoughlin | return size;
|
528 | 3831ab20 | aliguori | |
529 | fbe78f4f | aliguori | /* hdr_len refers to the header we supply to the guest */
|
530 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
531 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); |
532 | fbe78f4f | aliguori | |
533 | fbe78f4f | aliguori | offset = i = 0;
|
534 | fbe78f4f | aliguori | |
535 | fbe78f4f | aliguori | while (offset < size) {
|
536 | fbe78f4f | aliguori | VirtQueueElement elem; |
537 | fbe78f4f | aliguori | int len, total;
|
538 | fbe78f4f | aliguori | struct iovec sg[VIRTQUEUE_MAX_SIZE];
|
539 | fbe78f4f | aliguori | |
540 | fbe78f4f | aliguori | len = total = 0;
|
541 | fbe78f4f | aliguori | |
542 | fbe78f4f | aliguori | if ((i != 0 && !n->mergeable_rx_bufs) || |
543 | fbe78f4f | aliguori | virtqueue_pop(n->rx_vq, &elem) == 0) {
|
544 | fbe78f4f | aliguori | if (i == 0) |
545 | 4f1c942b | Mark McLoughlin | return -1; |
546 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net truncating packet\n");
|
547 | fbe78f4f | aliguori | exit(1);
|
548 | fbe78f4f | aliguori | } |
549 | fbe78f4f | aliguori | |
550 | fbe78f4f | aliguori | if (elem.in_num < 1) { |
551 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
|
552 | fbe78f4f | aliguori | exit(1);
|
553 | fbe78f4f | aliguori | } |
554 | fbe78f4f | aliguori | |
555 | fbe78f4f | aliguori | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { |
556 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
557 | fbe78f4f | aliguori | exit(1);
|
558 | fbe78f4f | aliguori | } |
559 | fbe78f4f | aliguori | |
560 | fbe78f4f | aliguori | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); |
561 | fbe78f4f | aliguori | |
562 | fbe78f4f | aliguori | if (i == 0) { |
563 | fbe78f4f | aliguori | if (n->mergeable_rx_bufs)
|
564 | fbe78f4f | aliguori | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; |
565 | fbe78f4f | aliguori | |
566 | fbe78f4f | aliguori | offset += receive_header(n, sg, elem.in_num, |
567 | fbe78f4f | aliguori | buf + offset, size - offset, hdr_len); |
568 | fbe78f4f | aliguori | total += hdr_len; |
569 | fbe78f4f | aliguori | } |
570 | fbe78f4f | aliguori | |
571 | fbe78f4f | aliguori | /* copy in packet. ugh */
|
572 | fbe78f4f | aliguori | len = iov_fill(sg, elem.in_num, |
573 | fbe78f4f | aliguori | buf + offset, size - offset); |
574 | fbe78f4f | aliguori | total += len; |
575 | fbe78f4f | aliguori | |
576 | fbe78f4f | aliguori | /* signal other side */
|
577 | fbe78f4f | aliguori | virtqueue_fill(n->rx_vq, &elem, total, i++); |
578 | fbe78f4f | aliguori | |
579 | fbe78f4f | aliguori | offset += len; |
580 | fbe78f4f | aliguori | } |
581 | fbe78f4f | aliguori | |
582 | fbe78f4f | aliguori | if (mhdr)
|
583 | fbe78f4f | aliguori | mhdr->num_buffers = i; |
584 | fbe78f4f | aliguori | |
585 | fbe78f4f | aliguori | virtqueue_flush(n->rx_vq, i); |
586 | fbe78f4f | aliguori | virtio_notify(&n->vdev, n->rx_vq); |
587 | 4f1c942b | Mark McLoughlin | |
588 | 4f1c942b | Mark McLoughlin | return size;
|
589 | fbe78f4f | aliguori | } |
590 | fbe78f4f | aliguori | |
591 | 6243375f | Mark McLoughlin | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq); |
592 | 6243375f | Mark McLoughlin | |
593 | eb6b6c12 | Mark McLoughlin | static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len) |
594 | 6243375f | Mark McLoughlin | { |
595 | eb6b6c12 | Mark McLoughlin | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
596 | 6243375f | Mark McLoughlin | |
597 | 6243375f | Mark McLoughlin | virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len); |
598 | 6243375f | Mark McLoughlin | virtio_notify(&n->vdev, n->tx_vq); |
599 | 6243375f | Mark McLoughlin | |
600 | 6243375f | Mark McLoughlin | n->async_tx.elem.out_num = n->async_tx.len = 0;
|
601 | 6243375f | Mark McLoughlin | |
602 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 1);
|
603 | 6243375f | Mark McLoughlin | virtio_net_flush_tx(n, n->tx_vq); |
604 | 6243375f | Mark McLoughlin | } |
605 | 6243375f | Mark McLoughlin | |
606 | fbe78f4f | aliguori | /* TX */
|
607 | fbe78f4f | aliguori | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) |
608 | fbe78f4f | aliguori | { |
609 | fbe78f4f | aliguori | VirtQueueElement elem; |
610 | fbe78f4f | aliguori | |
611 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
612 | fbe78f4f | aliguori | return;
|
613 | fbe78f4f | aliguori | |
614 | 6243375f | Mark McLoughlin | if (n->async_tx.elem.out_num) {
|
615 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 0);
|
616 | 6243375f | Mark McLoughlin | return;
|
617 | 6243375f | Mark McLoughlin | } |
618 | 6243375f | Mark McLoughlin | |
619 | fbe78f4f | aliguori | while (virtqueue_pop(vq, &elem)) {
|
620 | 6243375f | Mark McLoughlin | ssize_t ret, len = 0;
|
621 | fbe78f4f | aliguori | unsigned int out_num = elem.out_num; |
622 | fbe78f4f | aliguori | struct iovec *out_sg = &elem.out_sg[0]; |
623 | fbe78f4f | aliguori | unsigned hdr_len;
|
624 | fbe78f4f | aliguori | |
625 | fbe78f4f | aliguori | /* hdr_len refers to the header received from the guest */
|
626 | fbe78f4f | aliguori | hdr_len = n->mergeable_rx_bufs ? |
627 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr_mrg_rxbuf) : |
628 | fbe78f4f | aliguori | sizeof(struct virtio_net_hdr); |
629 | fbe78f4f | aliguori | |
630 | fbe78f4f | aliguori | if (out_num < 1 || out_sg->iov_len != hdr_len) { |
631 | fbe78f4f | aliguori | fprintf(stderr, "virtio-net header not in first element\n");
|
632 | fbe78f4f | aliguori | exit(1);
|
633 | fbe78f4f | aliguori | } |
634 | fbe78f4f | aliguori | |
635 | fbe78f4f | aliguori | /* ignore the header if GSO is not supported */
|
636 | 3a330134 | Mark McLoughlin | if (!n->has_vnet_hdr) {
|
637 | fbe78f4f | aliguori | out_num--; |
638 | fbe78f4f | aliguori | out_sg++; |
639 | fbe78f4f | aliguori | len += hdr_len; |
640 | fbe78f4f | aliguori | } else if (n->mergeable_rx_bufs) { |
641 | fbe78f4f | aliguori | /* tapfd expects a struct virtio_net_hdr */
|
642 | fbe78f4f | aliguori | hdr_len -= sizeof(struct virtio_net_hdr); |
643 | fbe78f4f | aliguori | out_sg->iov_len -= hdr_len; |
644 | fbe78f4f | aliguori | len += hdr_len; |
645 | fbe78f4f | aliguori | } |
646 | fbe78f4f | aliguori | |
647 | eb6b6c12 | Mark McLoughlin | ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num, |
648 | 6243375f | Mark McLoughlin | virtio_net_tx_complete); |
649 | 6243375f | Mark McLoughlin | if (ret == 0) { |
650 | 6243375f | Mark McLoughlin | virtio_queue_set_notification(n->tx_vq, 0);
|
651 | 6243375f | Mark McLoughlin | n->async_tx.elem = elem; |
652 | 6243375f | Mark McLoughlin | n->async_tx.len = len; |
653 | 6243375f | Mark McLoughlin | return;
|
654 | 6243375f | Mark McLoughlin | } |
655 | 6243375f | Mark McLoughlin | |
656 | 6243375f | Mark McLoughlin | len += ret; |
657 | fbe78f4f | aliguori | |
658 | fbe78f4f | aliguori | virtqueue_push(vq, &elem, len); |
659 | fbe78f4f | aliguori | virtio_notify(&n->vdev, vq); |
660 | fbe78f4f | aliguori | } |
661 | fbe78f4f | aliguori | } |
662 | fbe78f4f | aliguori | |
663 | fbe78f4f | aliguori | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) |
664 | fbe78f4f | aliguori | { |
665 | fbe78f4f | aliguori | VirtIONet *n = to_virtio_net(vdev); |
666 | fbe78f4f | aliguori | |
667 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
668 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 1);
|
669 | fbe78f4f | aliguori | qemu_del_timer(n->tx_timer); |
670 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
671 | fbe78f4f | aliguori | virtio_net_flush_tx(n, vq); |
672 | fbe78f4f | aliguori | } else {
|
673 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
674 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
675 | fbe78f4f | aliguori | n->tx_timer_active = 1;
|
676 | fbe78f4f | aliguori | virtio_queue_set_notification(vq, 0);
|
677 | fbe78f4f | aliguori | } |
678 | fbe78f4f | aliguori | } |
679 | fbe78f4f | aliguori | |
680 | fbe78f4f | aliguori | static void virtio_net_tx_timer(void *opaque) |
681 | fbe78f4f | aliguori | { |
682 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
683 | fbe78f4f | aliguori | |
684 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
685 | fbe78f4f | aliguori | |
686 | fbe78f4f | aliguori | /* Just in case the driver is not ready on more */
|
687 | fbe78f4f | aliguori | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
|
688 | fbe78f4f | aliguori | return;
|
689 | fbe78f4f | aliguori | |
690 | fbe78f4f | aliguori | virtio_queue_set_notification(n->tx_vq, 1);
|
691 | fbe78f4f | aliguori | virtio_net_flush_tx(n, n->tx_vq); |
692 | fbe78f4f | aliguori | } |
693 | fbe78f4f | aliguori | |
694 | fbe78f4f | aliguori | static void virtio_net_save(QEMUFile *f, void *opaque) |
695 | fbe78f4f | aliguori | { |
696 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
697 | fbe78f4f | aliguori | |
698 | fbe78f4f | aliguori | virtio_save(&n->vdev, f); |
699 | fbe78f4f | aliguori | |
700 | 79674068 | aliguori | qemu_put_buffer(f, n->mac, ETH_ALEN); |
701 | fbe78f4f | aliguori | qemu_put_be32(f, n->tx_timer_active); |
702 | e46cb38f | aliguori | qemu_put_be32(f, n->mergeable_rx_bufs); |
703 | 9d6271b8 | aliguori | qemu_put_be16(f, n->status); |
704 | f10c592e | Alex Williamson | qemu_put_byte(f, n->promisc); |
705 | f10c592e | Alex Williamson | qemu_put_byte(f, n->allmulti); |
706 | b6503ed9 | aliguori | qemu_put_be32(f, n->mac_table.in_use); |
707 | b6503ed9 | aliguori | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); |
708 | f21c0ed9 | aliguori | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
709 | 3a330134 | Mark McLoughlin | qemu_put_be32(f, n->has_vnet_hdr); |
710 | 8fd2a2f1 | Alex Williamson | qemu_put_byte(f, n->mac_table.multi_overflow); |
711 | 8fd2a2f1 | Alex Williamson | qemu_put_byte(f, n->mac_table.uni_overflow); |
712 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->alluni); |
713 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nomulti); |
714 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nouni); |
715 | 015cb166 | Alex Williamson | qemu_put_byte(f, n->nobcast); |
716 | 0ce0e8f4 | Mark McLoughlin | qemu_put_byte(f, n->has_ufo); |
717 | fbe78f4f | aliguori | } |
718 | fbe78f4f | aliguori | |
719 | fbe78f4f | aliguori | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) |
720 | fbe78f4f | aliguori | { |
721 | fbe78f4f | aliguori | VirtIONet *n = opaque; |
722 | 2d9aba39 | Alex Williamson | int i;
|
723 | fbe78f4f | aliguori | |
724 | 9d6271b8 | aliguori | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
725 | fbe78f4f | aliguori | return -EINVAL;
|
726 | fbe78f4f | aliguori | |
727 | fbe78f4f | aliguori | virtio_load(&n->vdev, f); |
728 | fbe78f4f | aliguori | |
729 | 79674068 | aliguori | qemu_get_buffer(f, n->mac, ETH_ALEN); |
730 | fbe78f4f | aliguori | n->tx_timer_active = qemu_get_be32(f); |
731 | e46cb38f | aliguori | n->mergeable_rx_bufs = qemu_get_be32(f); |
732 | fbe78f4f | aliguori | |
733 | 9d6271b8 | aliguori | if (version_id >= 3) |
734 | 9d6271b8 | aliguori | n->status = qemu_get_be16(f); |
735 | 9d6271b8 | aliguori | |
736 | 002437cd | aliguori | if (version_id >= 4) { |
737 | f10c592e | Alex Williamson | if (version_id < 8) { |
738 | f10c592e | Alex Williamson | n->promisc = qemu_get_be32(f); |
739 | f10c592e | Alex Williamson | n->allmulti = qemu_get_be32(f); |
740 | f10c592e | Alex Williamson | } else {
|
741 | f10c592e | Alex Williamson | n->promisc = qemu_get_byte(f); |
742 | f10c592e | Alex Williamson | n->allmulti = qemu_get_byte(f); |
743 | f10c592e | Alex Williamson | } |
744 | 002437cd | aliguori | } |
745 | 002437cd | aliguori | |
746 | b6503ed9 | aliguori | if (version_id >= 5) { |
747 | b6503ed9 | aliguori | n->mac_table.in_use = qemu_get_be32(f); |
748 | b6503ed9 | aliguori | /* MAC_TABLE_ENTRIES may be different from the saved image */
|
749 | b6503ed9 | aliguori | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
|
750 | b6503ed9 | aliguori | qemu_get_buffer(f, n->mac_table.macs, |
751 | b6503ed9 | aliguori | n->mac_table.in_use * ETH_ALEN); |
752 | b6503ed9 | aliguori | } else if (n->mac_table.in_use) { |
753 | b6503ed9 | aliguori | qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); |
754 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
|
755 | b6503ed9 | aliguori | n->mac_table.in_use = 0;
|
756 | b6503ed9 | aliguori | } |
757 | b6503ed9 | aliguori | } |
758 | b6503ed9 | aliguori | |
759 | f21c0ed9 | aliguori | if (version_id >= 6) |
760 | f21c0ed9 | aliguori | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
|
761 | f21c0ed9 | aliguori | |
762 | 3a330134 | Mark McLoughlin | if (version_id >= 7) { |
763 | 3a330134 | Mark McLoughlin | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
|
764 | 3a330134 | Mark McLoughlin | qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
|
765 | 3a330134 | Mark McLoughlin | return -1; |
766 | 3a330134 | Mark McLoughlin | } |
767 | 3a330134 | Mark McLoughlin | |
768 | 3a330134 | Mark McLoughlin | if (n->has_vnet_hdr) {
|
769 | eb6b6c12 | Mark McLoughlin | tap_using_vnet_hdr(n->nic->nc.peer, 1);
|
770 | eb6b6c12 | Mark McLoughlin | tap_set_offload(n->nic->nc.peer, |
771 | f5436dd9 | Mark McLoughlin | (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
|
772 | f5436dd9 | Mark McLoughlin | (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
|
773 | f5436dd9 | Mark McLoughlin | (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
|
774 | 6c9f58ba | Sridhar Samudrala | (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
|
775 | 6c9f58ba | Sridhar Samudrala | (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
|
776 | 3a330134 | Mark McLoughlin | } |
777 | 6c042c16 | Alex Williamson | } |
778 | 6c042c16 | Alex Williamson | |
779 | 8fd2a2f1 | Alex Williamson | if (version_id >= 9) { |
780 | 8fd2a2f1 | Alex Williamson | n->mac_table.multi_overflow = qemu_get_byte(f); |
781 | 8fd2a2f1 | Alex Williamson | n->mac_table.uni_overflow = qemu_get_byte(f); |
782 | 8fd2a2f1 | Alex Williamson | } |
783 | 8fd2a2f1 | Alex Williamson | |
784 | 015cb166 | Alex Williamson | if (version_id >= 10) { |
785 | 015cb166 | Alex Williamson | n->alluni = qemu_get_byte(f); |
786 | 015cb166 | Alex Williamson | n->nomulti = qemu_get_byte(f); |
787 | 015cb166 | Alex Williamson | n->nouni = qemu_get_byte(f); |
788 | 015cb166 | Alex Williamson | n->nobcast = qemu_get_byte(f); |
789 | 015cb166 | Alex Williamson | } |
790 | 015cb166 | Alex Williamson | |
791 | 0ce0e8f4 | Mark McLoughlin | if (version_id >= 11) { |
792 | 0ce0e8f4 | Mark McLoughlin | if (qemu_get_byte(f) && !peer_has_ufo(n)) {
|
793 | 0ce0e8f4 | Mark McLoughlin | qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
|
794 | 0ce0e8f4 | Mark McLoughlin | return -1; |
795 | 0ce0e8f4 | Mark McLoughlin | } |
796 | 0ce0e8f4 | Mark McLoughlin | } |
797 | 0ce0e8f4 | Mark McLoughlin | |
798 | 2d9aba39 | Alex Williamson | /* Find the first multicast entry in the saved MAC filter */
|
799 | 2d9aba39 | Alex Williamson | for (i = 0; i < n->mac_table.in_use; i++) { |
800 | 2d9aba39 | Alex Williamson | if (n->mac_table.macs[i * ETH_ALEN] & 1) { |
801 | 2d9aba39 | Alex Williamson | break;
|
802 | 2d9aba39 | Alex Williamson | } |
803 | 2d9aba39 | Alex Williamson | } |
804 | 2d9aba39 | Alex Williamson | n->mac_table.first_multi = i; |
805 | 2d9aba39 | Alex Williamson | |
806 | fbe78f4f | aliguori | if (n->tx_timer_active) {
|
807 | fbe78f4f | aliguori | qemu_mod_timer(n->tx_timer, |
808 | fbe78f4f | aliguori | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); |
809 | fbe78f4f | aliguori | } |
810 | fbe78f4f | aliguori | |
811 | fbe78f4f | aliguori | return 0; |
812 | fbe78f4f | aliguori | } |
813 | fbe78f4f | aliguori | |
814 | eb6b6c12 | Mark McLoughlin | static void virtio_net_cleanup(VLANClientState *nc) |
815 | b946a153 | aliguori | { |
816 | eb6b6c12 | Mark McLoughlin | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
817 | b946a153 | aliguori | |
818 | eb6b6c12 | Mark McLoughlin | n->nic = NULL;
|
819 | b946a153 | aliguori | } |
820 | b946a153 | aliguori | |
821 | eb6b6c12 | Mark McLoughlin | static NetClientInfo net_virtio_info = {
|
822 | eb6b6c12 | Mark McLoughlin | .type = NET_CLIENT_TYPE_NIC, |
823 | eb6b6c12 | Mark McLoughlin | .size = sizeof(NICState),
|
824 | eb6b6c12 | Mark McLoughlin | .can_receive = virtio_net_can_receive, |
825 | eb6b6c12 | Mark McLoughlin | .receive = virtio_net_receive, |
826 | eb6b6c12 | Mark McLoughlin | .cleanup = virtio_net_cleanup, |
827 | eb6b6c12 | Mark McLoughlin | .link_status_changed = virtio_net_set_link_status, |
828 | eb6b6c12 | Mark McLoughlin | }; |
829 | eb6b6c12 | Mark McLoughlin | |
830 | 97b15621 | Gerd Hoffmann | VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf) |
831 | fbe78f4f | aliguori | { |
832 | fbe78f4f | aliguori | VirtIONet *n; |
833 | fbe78f4f | aliguori | static int virtio_net_id; |
834 | fbe78f4f | aliguori | |
835 | 53c25cea | Paul Brook | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
|
836 | 53c25cea | Paul Brook | sizeof(struct virtio_net_config), |
837 | 53c25cea | Paul Brook | sizeof(VirtIONet));
|
838 | fbe78f4f | aliguori | |
839 | 0f03eca6 | aliguori | n->vdev.get_config = virtio_net_get_config; |
840 | 0f03eca6 | aliguori | n->vdev.set_config = virtio_net_set_config; |
841 | fbe78f4f | aliguori | n->vdev.get_features = virtio_net_get_features; |
842 | fbe78f4f | aliguori | n->vdev.set_features = virtio_net_set_features; |
843 | 8eca6b1b | aliguori | n->vdev.bad_features = virtio_net_bad_features; |
844 | 002437cd | aliguori | n->vdev.reset = virtio_net_reset; |
845 | fbe78f4f | aliguori | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
|
846 | fbe78f4f | aliguori | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
|
847 | 4ffb17f5 | Alex Williamson | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
|
848 | 97b15621 | Gerd Hoffmann | qemu_macaddr_default_if_unset(&conf->macaddr); |
849 | 3cbe04c4 | Mark McLoughlin | memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac)); |
850 | 554c97dd | aliguori | n->status = VIRTIO_NET_S_LINK_UP; |
851 | fbe78f4f | aliguori | |
852 | eb6b6c12 | Mark McLoughlin | n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n); |
853 | eb6b6c12 | Mark McLoughlin | |
854 | eb6b6c12 | Mark McLoughlin | qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a); |
855 | 96d5e201 | aliguori | |
856 | fbe78f4f | aliguori | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
857 | fbe78f4f | aliguori | n->tx_timer_active = 0;
|
858 | fbe78f4f | aliguori | n->mergeable_rx_bufs = 0;
|
859 | 002437cd | aliguori | n->promisc = 1; /* for compatibility */ |
860 | fbe78f4f | aliguori | |
861 | b6503ed9 | aliguori | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
862 | b6503ed9 | aliguori | |
863 | f21c0ed9 | aliguori | n->vlans = qemu_mallocz(MAX_VLAN >> 3);
|
864 | f21c0ed9 | aliguori | |
865 | 9d6271b8 | aliguori | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
|
866 | fbe78f4f | aliguori | virtio_net_save, virtio_net_load, n); |
867 | cf21e106 | Paul Brook | |
868 | 53c25cea | Paul Brook | return &n->vdev;
|
869 | cf21e106 | Paul Brook | } |
870 | 97b15621 | Gerd Hoffmann | |
871 | 97b15621 | Gerd Hoffmann | void virtio_net_exit(VirtIODevice *vdev)
|
872 | 97b15621 | Gerd Hoffmann | { |
873 | 97b15621 | Gerd Hoffmann | VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev); |
874 | 97b15621 | Gerd Hoffmann | |
875 | eb6b6c12 | Mark McLoughlin | qemu_purge_queued_packets(&n->nic->nc); |
876 | 97b15621 | Gerd Hoffmann | |
877 | 97b15621 | Gerd Hoffmann | unregister_savevm("virtio-net", n);
|
878 | 97b15621 | Gerd Hoffmann | |
879 | 97b15621 | Gerd Hoffmann | qemu_free(n->mac_table.macs); |
880 | 97b15621 | Gerd Hoffmann | qemu_free(n->vlans); |
881 | 97b15621 | Gerd Hoffmann | |
882 | 97b15621 | Gerd Hoffmann | qemu_del_timer(n->tx_timer); |
883 | 97b15621 | Gerd Hoffmann | qemu_free_timer(n->tx_timer); |
884 | 97b15621 | Gerd Hoffmann | |
885 | 97b15621 | Gerd Hoffmann | virtio_cleanup(&n->vdev); |
886 | eb6b6c12 | Mark McLoughlin | qemu_del_vlan_client(&n->nic->nc); |
887 | 97b15621 | Gerd Hoffmann | } |