vhost_net.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = g_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. g_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. net->dev.nvqs = 2;
  128. net->dev.vqs = net->vqs;
  129. r = vhost_dev_enable_notifiers(&net->dev, dev);
  130. if (r < 0) {
  131. goto fail_notifiers;
  132. }
  133. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  134. tap_set_vnet_hdr_len(net->vc,
  135. sizeof(struct virtio_net_hdr_mrg_rxbuf));
  136. }
  137. r = vhost_dev_start(&net->dev, dev);
  138. if (r < 0) {
  139. goto fail_start;
  140. }
  141. net->vc->info->poll(net->vc, false);
  142. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  143. file.fd = net->backend;
  144. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  145. r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  146. if (r < 0) {
  147. r = -errno;
  148. goto fail;
  149. }
  150. }
  151. return 0;
  152. fail:
  153. file.fd = -1;
  154. while (file.index-- > 0) {
  155. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  156. assert(r >= 0);
  157. }
  158. net->vc->info->poll(net->vc, true);
  159. vhost_dev_stop(&net->dev, dev);
  160. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  161. tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
  162. }
  163. fail_start:
  164. vhost_dev_disable_notifiers(&net->dev, dev);
  165. fail_notifiers:
  166. return r;
  167. }
  168. void vhost_net_stop(struct vhost_net *net,
  169. VirtIODevice *dev)
  170. {
  171. struct vhost_vring_file file = { .fd = -1 };
  172. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  173. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  174. assert(r >= 0);
  175. }
  176. net->vc->info->poll(net->vc, true);
  177. vhost_dev_stop(&net->dev, 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. vhost_dev_disable_notifiers(&net->dev, dev);
  182. }
  183. void vhost_net_cleanup(struct vhost_net *net)
  184. {
  185. vhost_dev_cleanup(&net->dev);
  186. if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  187. tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
  188. }
  189. g_free(net);
  190. }
  191. #else
  192. struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
  193. bool force)
  194. {
  195. error_report("vhost-net support is not compiled in");
  196. return NULL;
  197. }
  198. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  199. {
  200. return false;
  201. }
  202. int vhost_net_start(struct vhost_net *net,
  203. VirtIODevice *dev)
  204. {
  205. return -ENOSYS;
  206. }
  207. void vhost_net_stop(struct vhost_net *net,
  208. VirtIODevice *dev)
  209. {
  210. }
  211. void vhost_net_cleanup(struct vhost_net *net)
  212. {
  213. }
  214. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  215. {
  216. return features;
  217. }
  218. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  219. {
  220. }
  221. #endif