2
0

vhost_net.c 6.6 KB

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