]>
Commit | Line | Data |
---|---|---|
fbe78f4f AL |
1 | /* |
2 | * Virtio Network Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | ||
225 | static void virtio_net_receive(void *opaque, const uint8_t *buf, int size) | |
226 | { | |
227 | VirtIONet *n = opaque; | |
228 | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; | |
4689f4b3 | 229 | size_t hdr_len, offset, i; |
fbe78f4f AL |
230 | |
231 | if (!do_virtio_net_can_receive(n, size)) | |
232 | return; | |
233 | ||
234 | /* hdr_len refers to the header we supply to the guest */ | |
235 | hdr_len = n->mergeable_rx_bufs ? | |
236 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); | |
237 | ||
238 | offset = i = 0; | |
239 | ||
240 | while (offset < size) { | |
241 | VirtQueueElement elem; | |
242 | int len, total; | |
243 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; | |
244 | ||
245 | len = total = 0; | |
246 | ||
247 | if ((i != 0 && !n->mergeable_rx_bufs) || | |
248 | virtqueue_pop(n->rx_vq, &elem) == 0) { | |
249 | if (i == 0) | |
250 | return; | |
251 | fprintf(stderr, "virtio-net truncating packet\n"); | |
252 | exit(1); | |
253 | } | |
254 | ||
255 | if (elem.in_num < 1) { | |
256 | fprintf(stderr, "virtio-net receive queue contains no in buffers\n"); | |
257 | exit(1); | |
258 | } | |
259 | ||
260 | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { | |
261 | fprintf(stderr, "virtio-net header not in first element\n"); | |
262 | exit(1); | |
263 | } | |
264 | ||
265 | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); | |
266 | ||
267 | if (i == 0) { | |
268 | if (n->mergeable_rx_bufs) | |
269 | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; | |
270 | ||
271 | offset += receive_header(n, sg, elem.in_num, | |
272 | buf + offset, size - offset, hdr_len); | |
273 | total += hdr_len; | |
274 | } | |
275 | ||
276 | /* copy in packet. ugh */ | |
277 | len = iov_fill(sg, elem.in_num, | |
278 | buf + offset, size - offset); | |
279 | total += len; | |
280 | ||
281 | /* signal other side */ | |
282 | virtqueue_fill(n->rx_vq, &elem, total, i++); | |
283 | ||
284 | offset += len; | |
285 | } | |
286 | ||
287 | if (mhdr) | |
288 | mhdr->num_buffers = i; | |
289 | ||
290 | virtqueue_flush(n->rx_vq, i); | |
291 | virtio_notify(&n->vdev, n->rx_vq); | |
292 | } | |
293 | ||
294 | /* TX */ | |
295 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) | |
296 | { | |
297 | VirtQueueElement elem; | |
298 | int has_vnet_hdr = 0; | |
299 | ||
300 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
301 | return; | |
302 | ||
303 | while (virtqueue_pop(vq, &elem)) { | |
304 | ssize_t len = 0; | |
305 | unsigned int out_num = elem.out_num; | |
306 | struct iovec *out_sg = &elem.out_sg[0]; | |
307 | unsigned hdr_len; | |
308 | ||
309 | /* hdr_len refers to the header received from the guest */ | |
310 | hdr_len = n->mergeable_rx_bufs ? | |
311 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : | |
312 | sizeof(struct virtio_net_hdr); | |
313 | ||
314 | if (out_num < 1 || out_sg->iov_len != hdr_len) { | |
315 | fprintf(stderr, "virtio-net header not in first element\n"); | |
316 | exit(1); | |
317 | } | |
318 | ||
319 | /* ignore the header if GSO is not supported */ | |
320 | if (!has_vnet_hdr) { | |
321 | out_num--; | |
322 | out_sg++; | |
323 | len += hdr_len; | |
324 | } else if (n->mergeable_rx_bufs) { | |
325 | /* tapfd expects a struct virtio_net_hdr */ | |
326 | hdr_len -= sizeof(struct virtio_net_hdr); | |
327 | out_sg->iov_len -= hdr_len; | |
328 | len += hdr_len; | |
329 | } | |
330 | ||
331 | len += qemu_sendv_packet(n->vc, out_sg, out_num); | |
332 | ||
333 | virtqueue_push(vq, &elem, len); | |
334 | virtio_notify(&n->vdev, vq); | |
335 | } | |
336 | } | |
337 | ||
338 | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) | |
339 | { | |
340 | VirtIONet *n = to_virtio_net(vdev); | |
341 | ||
342 | if (n->tx_timer_active) { | |
343 | virtio_queue_set_notification(vq, 1); | |
344 | qemu_del_timer(n->tx_timer); | |
345 | n->tx_timer_active = 0; | |
346 | virtio_net_flush_tx(n, vq); | |
347 | } else { | |
348 | qemu_mod_timer(n->tx_timer, | |
349 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
350 | n->tx_timer_active = 1; | |
351 | virtio_queue_set_notification(vq, 0); | |
352 | } | |
353 | } | |
354 | ||
355 | static void virtio_net_tx_timer(void *opaque) | |
356 | { | |
357 | VirtIONet *n = opaque; | |
358 | ||
359 | n->tx_timer_active = 0; | |
360 | ||
361 | /* Just in case the driver is not ready on more */ | |
362 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
363 | return; | |
364 | ||
365 | virtio_queue_set_notification(n->tx_vq, 1); | |
366 | virtio_net_flush_tx(n, n->tx_vq); | |
367 | } | |
368 | ||
369 | static void virtio_net_save(QEMUFile *f, void *opaque) | |
370 | { | |
371 | VirtIONet *n = opaque; | |
372 | ||
373 | virtio_save(&n->vdev, f); | |
374 | ||
79674068 | 375 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 376 | qemu_put_be32(f, n->tx_timer_active); |
e46cb38f | 377 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 378 | qemu_put_be16(f, n->status); |
002437cd AL |
379 | qemu_put_be32(f, n->promisc); |
380 | qemu_put_be32(f, n->allmulti); | |
fbe78f4f AL |
381 | } |
382 | ||
383 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
384 | { | |
385 | VirtIONet *n = opaque; | |
386 | ||
9d6271b8 | 387 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
388 | return -EINVAL; |
389 | ||
390 | virtio_load(&n->vdev, f); | |
391 | ||
79674068 | 392 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 393 | n->tx_timer_active = qemu_get_be32(f); |
e46cb38f | 394 | n->mergeable_rx_bufs = qemu_get_be32(f); |
fbe78f4f | 395 | |
9d6271b8 AL |
396 | if (version_id >= 3) |
397 | n->status = qemu_get_be16(f); | |
398 | ||
002437cd AL |
399 | if (version_id >= 4) { |
400 | n->promisc = qemu_get_be32(f); | |
401 | n->allmulti = qemu_get_be32(f); | |
402 | } | |
403 | ||
fbe78f4f AL |
404 | if (n->tx_timer_active) { |
405 | qemu_mod_timer(n->tx_timer, | |
406 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
291c6ff9 | 412 | void virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn) |
fbe78f4f AL |
413 | { |
414 | VirtIONet *n; | |
415 | static int virtio_net_id; | |
416 | ||
a7c4996b AL |
417 | n = (VirtIONet *)virtio_init_pci(bus, "virtio-net", |
418 | PCI_VENDOR_ID_REDHAT_QUMRANET, | |
419 | PCI_DEVICE_ID_VIRTIO_NET, | |
99b3718e AL |
420 | PCI_VENDOR_ID_REDHAT_QUMRANET, |
421 | VIRTIO_ID_NET, | |
173a543b | 422 | PCI_CLASS_NETWORK_ETHERNET, 0x00, |
554c97dd AL |
423 | sizeof(struct virtio_net_config), |
424 | sizeof(VirtIONet)); | |
fbe78f4f | 425 | if (!n) |
291c6ff9 | 426 | return; |
fbe78f4f | 427 | |
0f03eca6 AL |
428 | n->vdev.get_config = virtio_net_get_config; |
429 | n->vdev.set_config = virtio_net_set_config; | |
fbe78f4f AL |
430 | n->vdev.get_features = virtio_net_get_features; |
431 | n->vdev.set_features = virtio_net_set_features; | |
002437cd | 432 | n->vdev.reset = virtio_net_reset; |
fbe78f4f AL |
433 | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
434 | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx); | |
3d11d36c | 435 | n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl); |
79674068 | 436 | memcpy(n->mac, nd->macaddr, ETH_ALEN); |
554c97dd | 437 | n->status = VIRTIO_NET_S_LINK_UP; |
7a9f6e4a | 438 | n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, |
bf38c1a0 | 439 | virtio_net_receive, virtio_net_can_receive, n); |
554c97dd | 440 | n->vc->link_status_changed = virtio_net_set_link_status; |
fbe78f4f | 441 | |
96d5e201 AL |
442 | qemu_format_nic_info_str(n->vc, n->mac); |
443 | ||
fbe78f4f AL |
444 | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
445 | n->tx_timer_active = 0; | |
446 | n->mergeable_rx_bufs = 0; | |
002437cd | 447 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 448 | |
9d6271b8 | 449 | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, |
fbe78f4f | 450 | virtio_net_save, virtio_net_load, n); |
fbe78f4f | 451 | } |