]>
Commit | Line | Data |
---|---|---|
fbe78f4f AL |
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 | #include "virtio.h" | |
15 | #include "net.h" | |
16 | #include "qemu-timer.h" | |
17 | #include "virtio-net.h" | |
18 | ||
002437cd | 19 | #define VIRTIO_NET_VM_VERSION 4 |
9d6271b8 | 20 | |
fbe78f4f AL |
21 | typedef struct VirtIONet |
22 | { | |
23 | VirtIODevice vdev; | |
79674068 | 24 | uint8_t mac[ETH_ALEN]; |
554c97dd | 25 | uint16_t status; |
fbe78f4f AL |
26 | VirtQueue *rx_vq; |
27 | VirtQueue *tx_vq; | |
3d11d36c | 28 | VirtQueue *ctrl_vq; |
fbe78f4f AL |
29 | VLANClientState *vc; |
30 | QEMUTimer *tx_timer; | |
31 | int tx_timer_active; | |
32 | int mergeable_rx_bufs; | |
002437cd AL |
33 | int promisc; |
34 | int allmulti; | |
fbe78f4f AL |
35 | } VirtIONet; |
36 | ||
37 | /* TODO | |
38 | * - we could suppress RX interrupt if we were so inclined. | |
39 | */ | |
40 | ||
41 | static VirtIONet *to_virtio_net(VirtIODevice *vdev) | |
42 | { | |
43 | return (VirtIONet *)vdev; | |
44 | } | |
45 | ||
0f03eca6 | 46 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
fbe78f4f AL |
47 | { |
48 | VirtIONet *n = to_virtio_net(vdev); | |
49 | struct virtio_net_config netcfg; | |
50 | ||
554c97dd | 51 | netcfg.status = n->status; |
79674068 | 52 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
fbe78f4f AL |
53 | memcpy(config, &netcfg, sizeof(netcfg)); |
54 | } | |
55 | ||
0f03eca6 AL |
56 | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
57 | { | |
58 | VirtIONet *n = to_virtio_net(vdev); | |
59 | struct virtio_net_config netcfg; | |
60 | ||
61 | memcpy(&netcfg, config, sizeof(netcfg)); | |
62 | ||
79674068 AL |
63 | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) { |
64 | memcpy(n->mac, netcfg.mac, ETH_ALEN); | |
0f03eca6 AL |
65 | qemu_format_nic_info_str(n->vc, n->mac); |
66 | } | |
67 | } | |
68 | ||
554c97dd AL |
69 | static void virtio_net_set_link_status(VLANClientState *vc) |
70 | { | |
71 | VirtIONet *n = vc->opaque; | |
72 | uint16_t old_status = n->status; | |
73 | ||
74 | if (vc->link_down) | |
75 | n->status &= ~VIRTIO_NET_S_LINK_UP; | |
76 | else | |
77 | n->status |= VIRTIO_NET_S_LINK_UP; | |
78 | ||
79 | if (n->status != old_status) | |
80 | virtio_notify_config(&n->vdev); | |
81 | } | |
82 | ||
002437cd AL |
83 | static void virtio_net_reset(VirtIODevice *vdev) |
84 | { | |
85 | VirtIONet *n = to_virtio_net(vdev); | |
86 | ||
87 | /* Reset back to compatibility mode */ | |
88 | n->promisc = 1; | |
89 | n->allmulti = 0; | |
90 | } | |
91 | ||
fbe78f4f AL |
92 | static uint32_t virtio_net_get_features(VirtIODevice *vdev) |
93 | { | |
3d11d36c AL |
94 | uint32_t features = (1 << VIRTIO_NET_F_MAC) | |
95 | (1 << VIRTIO_NET_F_STATUS) | | |
96 | (1 << VIRTIO_NET_F_CTRL_VQ); | |
fbe78f4f AL |
97 | |
98 | return features; | |
99 | } | |
100 | ||
101 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) | |
102 | { | |
103 | VirtIONet *n = to_virtio_net(vdev); | |
104 | ||
105 | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)); | |
106 | } | |
107 | ||
002437cd AL |
108 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
109 | VirtQueueElement *elem) | |
110 | { | |
111 | uint8_t on; | |
112 | ||
113 | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { | |
114 | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n"); | |
115 | exit(1); | |
116 | } | |
117 | ||
118 | on = ldub_p(elem->out_sg[1].iov_base); | |
119 | ||
120 | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC) | |
121 | n->promisc = on; | |
122 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) | |
123 | n->allmulti = on; | |
124 | else | |
125 | return VIRTIO_NET_ERR; | |
126 | ||
127 | return VIRTIO_NET_OK; | |
128 | } | |
129 | ||
3d11d36c AL |
130 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
131 | { | |
002437cd | 132 | VirtIONet *n = to_virtio_net(vdev); |
3d11d36c AL |
133 | struct virtio_net_ctrl_hdr ctrl; |
134 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; | |
135 | VirtQueueElement elem; | |
136 | ||
137 | while (virtqueue_pop(vq, &elem)) { | |
138 | if ((elem.in_num < 1) || (elem.out_num < 1)) { | |
139 | fprintf(stderr, "virtio-net ctrl missing headers\n"); | |
140 | exit(1); | |
141 | } | |
142 | ||
143 | if (elem.out_sg[0].iov_len < sizeof(ctrl) || | |
144 | elem.out_sg[elem.in_num - 1].iov_len < sizeof(status)) { | |
145 | fprintf(stderr, "virtio-net ctrl header not in correct element\n"); | |
146 | exit(1); | |
147 | } | |
148 | ||
149 | ctrl.class = ldub_p(elem.out_sg[0].iov_base); | |
150 | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); | |
151 | ||
002437cd AL |
152 | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE) |
153 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); | |
154 | ||
3d11d36c AL |
155 | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status); |
156 | ||
157 | virtqueue_push(vq, &elem, sizeof(status)); | |
158 | virtio_notify(vdev, vq); | |
159 | } | |
160 | } | |
161 | ||
fbe78f4f AL |
162 | /* RX */ |
163 | ||
164 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) | |
165 | { | |
166 | } | |
167 | ||
168 | static int do_virtio_net_can_receive(VirtIONet *n, int bufsize) | |
169 | { | |
170 | if (!virtio_queue_ready(n->rx_vq) || | |
171 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
172 | return 0; | |
173 | ||
174 | if (virtio_queue_empty(n->rx_vq) || | |
175 | (n->mergeable_rx_bufs && | |
176 | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) { | |
177 | virtio_queue_set_notification(n->rx_vq, 1); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | virtio_queue_set_notification(n->rx_vq, 0); | |
182 | return 1; | |
183 | } | |
184 | ||
185 | static int virtio_net_can_receive(void *opaque) | |
186 | { | |
187 | VirtIONet *n = opaque; | |
188 | ||
189 | return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE); | |
190 | } | |
191 | ||
192 | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) | |
193 | { | |
194 | int offset, i; | |
195 | ||
196 | offset = i = 0; | |
197 | while (offset < count && i < iovcnt) { | |
198 | int len = MIN(iov[i].iov_len, count - offset); | |
199 | memcpy(iov[i].iov_base, buf + offset, len); | |
200 | offset += len; | |
201 | i++; | |
202 | } | |
203 | ||
204 | return offset; | |
205 | } | |
206 | ||
207 | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, | |
4689f4b3 | 208 | const void *buf, size_t size, size_t hdr_len) |
fbe78f4f AL |
209 | { |
210 | struct virtio_net_hdr *hdr = iov[0].iov_base; | |
211 | int offset = 0; | |
212 | ||
213 | hdr->flags = 0; | |
214 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
215 | ||
216 | /* We only ever receive a struct virtio_net_hdr from the tapfd, | |
217 | * but we may be passing along a larger header to the guest. | |
218 | */ | |
219 | iov[0].iov_base += hdr_len; | |
220 | iov[0].iov_len -= hdr_len; | |
221 | ||
222 | return offset; | |
223 | } | |
224 | ||
3831ab20 AL |
225 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
226 | { | |
227 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
228 | uint8_t *ptr = (uint8_t *)buf; | |
229 | ||
230 | if (n->promisc) | |
231 | return 1; | |
232 | ||
233 | #ifdef TAP_VNET_HDR | |
234 | if (tap_has_vnet_hdr(n->vc->vlan->first_client)) | |
235 | ptr += sizeof(struct virtio_net_hdr); | |
236 | #endif | |
237 | ||
238 | if ((ptr[0] & 1) && n->allmulti) | |
239 | return 1; | |
240 | ||
241 | if (!memcmp(ptr, bcast, sizeof(bcast))) | |
242 | return 1; | |
243 | ||
244 | if (!memcmp(ptr, n->mac, ETH_ALEN)) | |
245 | return 1; | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
fbe78f4f AL |
250 | static void virtio_net_receive(void *opaque, const uint8_t *buf, int size) |
251 | { | |
252 | VirtIONet *n = opaque; | |
253 | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; | |
4689f4b3 | 254 | size_t hdr_len, offset, i; |
fbe78f4f AL |
255 | |
256 | if (!do_virtio_net_can_receive(n, size)) | |
257 | return; | |
258 | ||
3831ab20 AL |
259 | if (!receive_filter(n, buf, size)) |
260 | return; | |
261 | ||
fbe78f4f AL |
262 | /* hdr_len refers to the header we supply to the guest */ |
263 | hdr_len = n->mergeable_rx_bufs ? | |
264 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); | |
265 | ||
266 | offset = i = 0; | |
267 | ||
268 | while (offset < size) { | |
269 | VirtQueueElement elem; | |
270 | int len, total; | |
271 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; | |
272 | ||
273 | len = total = 0; | |
274 | ||
275 | if ((i != 0 && !n->mergeable_rx_bufs) || | |
276 | virtqueue_pop(n->rx_vq, &elem) == 0) { | |
277 | if (i == 0) | |
278 | return; | |
279 | fprintf(stderr, "virtio-net truncating packet\n"); | |
280 | exit(1); | |
281 | } | |
282 | ||
283 | if (elem.in_num < 1) { | |
284 | fprintf(stderr, "virtio-net receive queue contains no in buffers\n"); | |
285 | exit(1); | |
286 | } | |
287 | ||
288 | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { | |
289 | fprintf(stderr, "virtio-net header not in first element\n"); | |
290 | exit(1); | |
291 | } | |
292 | ||
293 | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); | |
294 | ||
295 | if (i == 0) { | |
296 | if (n->mergeable_rx_bufs) | |
297 | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; | |
298 | ||
299 | offset += receive_header(n, sg, elem.in_num, | |
300 | buf + offset, size - offset, hdr_len); | |
301 | total += hdr_len; | |
302 | } | |
303 | ||
304 | /* copy in packet. ugh */ | |
305 | len = iov_fill(sg, elem.in_num, | |
306 | buf + offset, size - offset); | |
307 | total += len; | |
308 | ||
309 | /* signal other side */ | |
310 | virtqueue_fill(n->rx_vq, &elem, total, i++); | |
311 | ||
312 | offset += len; | |
313 | } | |
314 | ||
315 | if (mhdr) | |
316 | mhdr->num_buffers = i; | |
317 | ||
318 | virtqueue_flush(n->rx_vq, i); | |
319 | virtio_notify(&n->vdev, n->rx_vq); | |
320 | } | |
321 | ||
322 | /* TX */ | |
323 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) | |
324 | { | |
325 | VirtQueueElement elem; | |
326 | int has_vnet_hdr = 0; | |
327 | ||
328 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
329 | return; | |
330 | ||
331 | while (virtqueue_pop(vq, &elem)) { | |
332 | ssize_t len = 0; | |
333 | unsigned int out_num = elem.out_num; | |
334 | struct iovec *out_sg = &elem.out_sg[0]; | |
335 | unsigned hdr_len; | |
336 | ||
337 | /* hdr_len refers to the header received from the guest */ | |
338 | hdr_len = n->mergeable_rx_bufs ? | |
339 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : | |
340 | sizeof(struct virtio_net_hdr); | |
341 | ||
342 | if (out_num < 1 || out_sg->iov_len != hdr_len) { | |
343 | fprintf(stderr, "virtio-net header not in first element\n"); | |
344 | exit(1); | |
345 | } | |
346 | ||
347 | /* ignore the header if GSO is not supported */ | |
348 | if (!has_vnet_hdr) { | |
349 | out_num--; | |
350 | out_sg++; | |
351 | len += hdr_len; | |
352 | } else if (n->mergeable_rx_bufs) { | |
353 | /* tapfd expects a struct virtio_net_hdr */ | |
354 | hdr_len -= sizeof(struct virtio_net_hdr); | |
355 | out_sg->iov_len -= hdr_len; | |
356 | len += hdr_len; | |
357 | } | |
358 | ||
359 | len += qemu_sendv_packet(n->vc, out_sg, out_num); | |
360 | ||
361 | virtqueue_push(vq, &elem, len); | |
362 | virtio_notify(&n->vdev, vq); | |
363 | } | |
364 | } | |
365 | ||
366 | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) | |
367 | { | |
368 | VirtIONet *n = to_virtio_net(vdev); | |
369 | ||
370 | if (n->tx_timer_active) { | |
371 | virtio_queue_set_notification(vq, 1); | |
372 | qemu_del_timer(n->tx_timer); | |
373 | n->tx_timer_active = 0; | |
374 | virtio_net_flush_tx(n, vq); | |
375 | } else { | |
376 | qemu_mod_timer(n->tx_timer, | |
377 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
378 | n->tx_timer_active = 1; | |
379 | virtio_queue_set_notification(vq, 0); | |
380 | } | |
381 | } | |
382 | ||
383 | static void virtio_net_tx_timer(void *opaque) | |
384 | { | |
385 | VirtIONet *n = opaque; | |
386 | ||
387 | n->tx_timer_active = 0; | |
388 | ||
389 | /* Just in case the driver is not ready on more */ | |
390 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
391 | return; | |
392 | ||
393 | virtio_queue_set_notification(n->tx_vq, 1); | |
394 | virtio_net_flush_tx(n, n->tx_vq); | |
395 | } | |
396 | ||
397 | static void virtio_net_save(QEMUFile *f, void *opaque) | |
398 | { | |
399 | VirtIONet *n = opaque; | |
400 | ||
401 | virtio_save(&n->vdev, f); | |
402 | ||
79674068 | 403 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 404 | qemu_put_be32(f, n->tx_timer_active); |
e46cb38f | 405 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 406 | qemu_put_be16(f, n->status); |
002437cd AL |
407 | qemu_put_be32(f, n->promisc); |
408 | qemu_put_be32(f, n->allmulti); | |
fbe78f4f AL |
409 | } |
410 | ||
411 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
412 | { | |
413 | VirtIONet *n = opaque; | |
414 | ||
9d6271b8 | 415 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
416 | return -EINVAL; |
417 | ||
418 | virtio_load(&n->vdev, f); | |
419 | ||
79674068 | 420 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 421 | n->tx_timer_active = qemu_get_be32(f); |
e46cb38f | 422 | n->mergeable_rx_bufs = qemu_get_be32(f); |
fbe78f4f | 423 | |
9d6271b8 AL |
424 | if (version_id >= 3) |
425 | n->status = qemu_get_be16(f); | |
426 | ||
002437cd AL |
427 | if (version_id >= 4) { |
428 | n->promisc = qemu_get_be32(f); | |
429 | n->allmulti = qemu_get_be32(f); | |
430 | } | |
431 | ||
fbe78f4f AL |
432 | if (n->tx_timer_active) { |
433 | qemu_mod_timer(n->tx_timer, | |
434 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
291c6ff9 | 440 | void virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn) |
fbe78f4f AL |
441 | { |
442 | VirtIONet *n; | |
443 | static int virtio_net_id; | |
444 | ||
a7c4996b AL |
445 | n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", |
446 | PCI_VENDOR_ID_REDHAT_QUMRANET, | |
447 | PCI_DEVICE_ID_VIRTIO_NET, | |
99b3718e AL |
448 | PCI_VENDOR_ID_REDHAT_QUMRANET, |
449 | VIRTIO_ID_NET, | |
173a543b | 450 | PCI_CLASS_NETWORK_ETHERNET, 0x00, |
554c97dd AL |
451 | sizeof(struct virtio_net_config), |
452 | sizeof(VirtIONet)); | |
fbe78f4f | 453 | if (!n) |
291c6ff9 | 454 | return; |
fbe78f4f | 455 | |
0f03eca6 AL |
456 | n->vdev.get_config = virtio_net_get_config; |
457 | n->vdev.set_config = virtio_net_set_config; | |
fbe78f4f AL |
458 | n->vdev.get_features = virtio_net_get_features; |
459 | n->vdev.set_features = virtio_net_set_features; | |
002437cd | 460 | n->vdev.reset = virtio_net_reset; |
fbe78f4f AL |
461 | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
462 | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx); | |
3d11d36c | 463 | n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl); |
79674068 | 464 | memcpy(n->mac, nd->macaddr, ETH_ALEN); |
554c97dd | 465 | n->status = VIRTIO_NET_S_LINK_UP; |
7a9f6e4a | 466 | n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, |
bf38c1a0 | 467 | virtio_net_receive, virtio_net_can_receive, n); |
554c97dd | 468 | n->vc->link_status_changed = virtio_net_set_link_status; |
fbe78f4f | 469 | |
96d5e201 AL |
470 | qemu_format_nic_info_str(n->vc, n->mac); |
471 | ||
fbe78f4f AL |
472 | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
473 | n->tx_timer_active = 0; | |
474 | n->mergeable_rx_bufs = 0; | |
002437cd | 475 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 476 | |
9d6271b8 | 477 | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, |
fbe78f4f | 478 | virtio_net_save, virtio_net_load, n); |
fbe78f4f | 479 | } |