2
0

vhost_net.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * vhost-net support
  3. *
  4. * Copyright Red Hat, Inc. 2010
  5. *
  6. * Authors:
  7. * Michael S. Tsirkin <mst@redhat.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. #include "net.h"
  13. #include "net/tap.h"
  14. #include "virtio-net.h"
  15. #include "vhost_net.h"
  16. #include "qemu-error.h"
  17. #include "config.h"
  18. #ifdef CONFIG_VHOST_NET
  19. #include <linux/vhost.h>
  20. #include <sys/socket.h>
  21. #include <linux/kvm.h>
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include <linux/virtio_ring.h>
  25. #include <netpacket/packet.h>
  26. #include <net/ethernet.h>
  27. #include <net/if.h>
  28. #include <netinet/in.h>
  29. #include <stdio.h>
  30. #include "vhost.h"
  31. struct vhost_net {
  32. struct vhost_dev dev;
  33. struct vhost_virtqueue vqs[2];
  34. int backend;
  35. VLANClientState *vc;
  36. };
  37. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  38. {
  39. /* Clear features not supported by host kernel. */
  40. if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
  41. features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
  42. }
  43. if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
  44. features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
  45. }
  46. if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
  47. features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
  48. }
  49. if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
  50. features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
  51. }
  52. return features;
  53. }
  54. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  55. {
  56. net->dev.acked_features = net->dev.backend_features;
  57. if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
  58. net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
  59. }
  60. if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
  61. net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
  62. }
  63. if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
  64. net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
  65. }
  66. if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  67. net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
  68. }
  69. }
  70. static int vhost_net_get_fd(VLANClientState *backend)
  71. {
  72. switch (backend->info->type) {
  73. case NET_CLIENT_TYPE_TAP:
  74. return tap_get_fd(backend);
  75. default:
  76. fprintf(stderr, "vhost-net requires tap backend\n");
  77. return -EBADFD;
  78. }
  79. }
  80. struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
  81. bool force)
  82. {
  83. int r;
  84. struct vhost_net *net = qemu_malloc(sizeof *net);
  85. if (!backend) {
  86. fprintf(stderr, "vhost-net requires backend to be setup\n");
  87. goto fail;
  88. }
  89. r = vhost_net_get_fd(backend);
  90. if (r < 0) {
  91. goto fail;
  92. }
  93. net->vc = backend;
  94. net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
  95. (1 << VHOST_NET_F_VIRTIO_NET_HDR);
  96. net->backend = r;
  97. r = vhost_dev_init(&net->dev, devfd, force);
  98. if (r < 0) {
  99. goto fail;
  100. }
  101. if (!tap_has_vnet_hdr_len(backend,
  102. sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
  103. net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
  104. }
  105. if (~net->dev.features & net->dev.backend_features) {
  106. fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
  107. (uint64_t)(~net->dev.features & net->dev.backend_features));
  108. vhost_dev_cleanup(&net->dev);
  109. goto fail;
  110. }
  111. /* Set sane init value. Override when guest acks. */
  112. vhost_net_ack_features(net, 0);
  113. return net;
  114. fail:
  115. qemu_free(net);
  116. return NULL;
  117. }
  118. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  119. {
  120. return vhost_dev_query(&net->dev, dev);
  121. }
  122. int vhost_net_start(struct vhost_net *net,
  123. VirtIODevice *dev)
  124. {
  125. struct vhost_vring_file file = { };
  126. int r;
  127. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  128. tap_set_vnet_hdr_len(net->vc,
  129. sizeof(struct virtio_net_hdr_mrg_rxbuf));
  130. }
  131. net->dev.nvqs = 2;
  132. net->dev.vqs = net->vqs;
  133. r = vhost_dev_start(&net->dev, dev);
  134. if (r < 0) {
  135. return r;
  136. }
  137. net->vc->info->poll(net->vc, false);
  138. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  139. file.fd = net->backend;
  140. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  141. r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  142. if (r < 0) {
  143. r = -errno;
  144. goto fail;
  145. }
  146. }
  147. return 0;
  148. fail:
  149. file.fd = -1;
  150. while (file.index-- > 0) {
  151. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  152. assert(r >= 0);
  153. }
  154. net->vc->info->poll(net->vc, true);
  155. vhost_dev_stop(&net->dev, dev);
  156. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  157. tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
  158. }
  159. return r;
  160. }
  161. void vhost_net_stop(struct vhost_net *net,
  162. VirtIODevice *dev)
  163. {
  164. struct vhost_vring_file file = { .fd = -1 };
  165. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  166. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  167. assert(r >= 0);
  168. }
  169. net->vc->info->poll(net->vc, true);
  170. vhost_dev_stop(&net->dev, dev);
  171. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  172. tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
  173. }
  174. }
  175. void vhost_net_cleanup(struct vhost_net *net)
  176. {
  177. vhost_dev_cleanup(&net->dev);
  178. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  179. tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
  180. }
  181. qemu_free(net);
  182. }
  183. #else
  184. struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
  185. bool force)
  186. {
  187. error_report("vhost-net support is not compiled in");
  188. return NULL;
  189. }
  190. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  191. {
  192. return false;
  193. }
  194. int vhost_net_start(struct vhost_net *net,
  195. VirtIODevice *dev)
  196. {
  197. return -ENOSYS;
  198. }
  199. void vhost_net_stop(struct vhost_net *net,
  200. VirtIODevice *dev)
  201. {
  202. }
  203. void vhost_net_cleanup(struct vhost_net *net)
  204. {
  205. }
  206. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  207. {
  208. return features;
  209. }
  210. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  211. {
  212. }
  213. #endif