2
0

vhost_net.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/net.h"
  16. #include "net/tap.h"
  17. #include "virtio-net.h"
  18. #include "vhost_net.h"
  19. #include "qemu/error-report.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. net->dev.nvqs = 2;
  101. net->dev.vqs = net->vqs;
  102. r = vhost_dev_init(&net->dev, devfd, "/dev/vhost-net", force);
  103. if (r < 0) {
  104. goto fail;
  105. }
  106. if (!tap_has_vnet_hdr_len(backend,
  107. sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
  108. net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
  109. }
  110. if (~net->dev.features & net->dev.backend_features) {
  111. fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
  112. (uint64_t)(~net->dev.features & net->dev.backend_features));
  113. vhost_dev_cleanup(&net->dev);
  114. goto fail;
  115. }
  116. /* Set sane init value. Override when guest acks. */
  117. vhost_net_ack_features(net, 0);
  118. return net;
  119. fail:
  120. g_free(net);
  121. return NULL;
  122. }
  123. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  124. {
  125. return vhost_dev_query(&net->dev, dev);
  126. }
  127. static int vhost_net_start_one(struct vhost_net *net,
  128. VirtIODevice *dev,
  129. int vq_index)
  130. {
  131. struct vhost_vring_file file = { };
  132. int r;
  133. if (net->dev.started) {
  134. return 0;
  135. }
  136. net->dev.nvqs = 2;
  137. net->dev.vqs = net->vqs;
  138. net->dev.vq_index = vq_index;
  139. r = vhost_dev_enable_notifiers(&net->dev, dev);
  140. if (r < 0) {
  141. goto fail_notifiers;
  142. }
  143. r = vhost_dev_start(&net->dev, dev);
  144. if (r < 0) {
  145. goto fail_start;
  146. }
  147. net->nc->info->poll(net->nc, false);
  148. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  149. file.fd = net->backend;
  150. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  151. r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  152. if (r < 0) {
  153. r = -errno;
  154. goto fail;
  155. }
  156. }
  157. return 0;
  158. fail:
  159. file.fd = -1;
  160. while (file.index-- > 0) {
  161. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  162. assert(r >= 0);
  163. }
  164. net->nc->info->poll(net->nc, true);
  165. vhost_dev_stop(&net->dev, dev);
  166. fail_start:
  167. vhost_dev_disable_notifiers(&net->dev, dev);
  168. fail_notifiers:
  169. return r;
  170. }
  171. static void vhost_net_stop_one(struct vhost_net *net,
  172. VirtIODevice *dev)
  173. {
  174. struct vhost_vring_file file = { .fd = -1 };
  175. if (!net->dev.started) {
  176. return;
  177. }
  178. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  179. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  180. assert(r >= 0);
  181. }
  182. net->nc->info->poll(net->nc, true);
  183. vhost_dev_stop(&net->dev, dev);
  184. vhost_dev_disable_notifiers(&net->dev, dev);
  185. }
  186. int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  187. int total_queues)
  188. {
  189. int r, i = 0;
  190. if (!dev->binding->set_guest_notifiers) {
  191. error_report("binding does not support guest notifiers");
  192. r = -ENOSYS;
  193. goto err;
  194. }
  195. for (i = 0; i < total_queues; i++) {
  196. r = vhost_net_start_one(tap_get_vhost_net(ncs[i].peer), dev, i * 2);
  197. if (r < 0) {
  198. goto err;
  199. }
  200. }
  201. r = dev->binding->set_guest_notifiers(dev->binding_opaque,
  202. total_queues * 2,
  203. true);
  204. if (r < 0) {
  205. error_report("Error binding guest notifier: %d", -r);
  206. goto err;
  207. }
  208. return 0;
  209. err:
  210. while (--i >= 0) {
  211. vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
  212. }
  213. return r;
  214. }
  215. void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
  216. int total_queues)
  217. {
  218. int i, r;
  219. r = dev->binding->set_guest_notifiers(dev->binding_opaque,
  220. total_queues * 2,
  221. false);
  222. if (r < 0) {
  223. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
  224. fflush(stderr);
  225. }
  226. assert(r >= 0);
  227. for (i = 0; i < total_queues; i++) {
  228. vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
  229. }
  230. }
  231. void vhost_net_cleanup(struct vhost_net *net)
  232. {
  233. vhost_dev_cleanup(&net->dev);
  234. g_free(net);
  235. }
  236. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  237. {
  238. return vhost_virtqueue_pending(&net->dev, idx);
  239. }
  240. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  241. int idx, bool mask)
  242. {
  243. vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  244. }
  245. #else
  246. struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
  247. bool force)
  248. {
  249. error_report("vhost-net support is not compiled in");
  250. return NULL;
  251. }
  252. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  253. {
  254. return false;
  255. }
  256. int vhost_net_start(VirtIODevice *dev,
  257. NetClientState *ncs,
  258. int total_queues)
  259. {
  260. return -ENOSYS;
  261. }
  262. void vhost_net_stop(VirtIODevice *dev,
  263. NetClientState *ncs,
  264. int total_queues)
  265. {
  266. }
  267. void vhost_net_cleanup(struct vhost_net *net)
  268. {
  269. }
  270. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  271. {
  272. return features;
  273. }
  274. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  275. {
  276. }
  277. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  278. {
  279. return -ENOSYS;
  280. }
  281. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  282. int idx, bool mask)
  283. {
  284. }
  285. #endif