vhost_net.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "hw/virtio/virtio-net.h"
  18. #include "net/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 "hw/virtio/vhost.h"
  34. #include "hw/virtio/virtio-bus.h"
  35. struct vhost_net {
  36. struct vhost_dev dev;
  37. struct vhost_virtqueue vqs[2];
  38. int backend;
  39. NetClientState *nc;
  40. };
  41. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  42. {
  43. /* Clear features not supported by host kernel. */
  44. if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
  45. features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
  46. }
  47. if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
  48. features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
  49. }
  50. if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
  51. features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
  52. }
  53. if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
  54. features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
  55. }
  56. return features;
  57. }
  58. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  59. {
  60. net->dev.acked_features = net->dev.backend_features;
  61. if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
  62. net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
  63. }
  64. if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
  65. net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
  66. }
  67. if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
  68. net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
  69. }
  70. if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
  71. net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
  72. }
  73. }
  74. static int vhost_net_get_fd(NetClientState *backend)
  75. {
  76. switch (backend->info->type) {
  77. case NET_CLIENT_OPTIONS_KIND_TAP:
  78. return tap_get_fd(backend);
  79. default:
  80. fprintf(stderr, "vhost-net requires tap backend\n");
  81. return -EBADFD;
  82. }
  83. }
  84. struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
  85. bool force)
  86. {
  87. int r;
  88. struct vhost_net *net = g_malloc(sizeof *net);
  89. if (!backend) {
  90. fprintf(stderr, "vhost-net requires backend to be setup\n");
  91. goto fail;
  92. }
  93. r = vhost_net_get_fd(backend);
  94. if (r < 0) {
  95. goto fail;
  96. }
  97. net->nc = backend;
  98. net->dev.backend_features = qemu_has_vnet_hdr(backend) ? 0 :
  99. (1 << VHOST_NET_F_VIRTIO_NET_HDR);
  100. net->backend = r;
  101. net->dev.nvqs = 2;
  102. net->dev.vqs = net->vqs;
  103. r = vhost_dev_init(&net->dev, devfd, "/dev/vhost-net", force);
  104. if (r < 0) {
  105. goto fail;
  106. }
  107. if (!qemu_has_vnet_hdr_len(backend,
  108. sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
  109. net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
  110. }
  111. if (~net->dev.features & net->dev.backend_features) {
  112. fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
  113. (uint64_t)(~net->dev.features & net->dev.backend_features));
  114. vhost_dev_cleanup(&net->dev);
  115. goto fail;
  116. }
  117. /* Set sane init value. Override when guest acks. */
  118. vhost_net_ack_features(net, 0);
  119. return net;
  120. fail:
  121. g_free(net);
  122. return NULL;
  123. }
  124. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  125. {
  126. return vhost_dev_query(&net->dev, dev);
  127. }
  128. static int vhost_net_start_one(struct vhost_net *net,
  129. VirtIODevice *dev,
  130. int vq_index)
  131. {
  132. struct vhost_vring_file file = { };
  133. int r;
  134. if (net->dev.started) {
  135. return 0;
  136. }
  137. net->dev.nvqs = 2;
  138. net->dev.vqs = net->vqs;
  139. net->dev.vq_index = vq_index;
  140. r = vhost_dev_enable_notifiers(&net->dev, dev);
  141. if (r < 0) {
  142. goto fail_notifiers;
  143. }
  144. r = vhost_dev_start(&net->dev, dev);
  145. if (r < 0) {
  146. goto fail_start;
  147. }
  148. net->nc->info->poll(net->nc, false);
  149. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  150. file.fd = net->backend;
  151. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  152. r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  153. if (r < 0) {
  154. r = -errno;
  155. goto fail;
  156. }
  157. }
  158. return 0;
  159. fail:
  160. file.fd = -1;
  161. while (file.index-- > 0) {
  162. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  163. assert(r >= 0);
  164. }
  165. net->nc->info->poll(net->nc, true);
  166. vhost_dev_stop(&net->dev, dev);
  167. fail_start:
  168. vhost_dev_disable_notifiers(&net->dev, dev);
  169. fail_notifiers:
  170. return r;
  171. }
  172. static void vhost_net_stop_one(struct vhost_net *net,
  173. VirtIODevice *dev)
  174. {
  175. struct vhost_vring_file file = { .fd = -1 };
  176. if (!net->dev.started) {
  177. return;
  178. }
  179. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  180. int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
  181. assert(r >= 0);
  182. }
  183. net->nc->info->poll(net->nc, true);
  184. vhost_dev_stop(&net->dev, dev);
  185. vhost_dev_disable_notifiers(&net->dev, dev);
  186. }
  187. int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  188. int total_queues)
  189. {
  190. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  191. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  192. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  193. int r, i = 0;
  194. if (!k->set_guest_notifiers) {
  195. error_report("binding does not support guest notifiers");
  196. r = -ENOSYS;
  197. goto err;
  198. }
  199. for (i = 0; i < total_queues; i++) {
  200. r = vhost_net_start_one(tap_get_vhost_net(ncs[i].peer), dev, i * 2);
  201. if (r < 0) {
  202. goto err;
  203. }
  204. }
  205. r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
  206. if (r < 0) {
  207. error_report("Error binding guest notifier: %d", -r);
  208. goto err;
  209. }
  210. return 0;
  211. err:
  212. while (--i >= 0) {
  213. vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
  214. }
  215. return r;
  216. }
  217. void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
  218. int total_queues)
  219. {
  220. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  221. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  222. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  223. int i, r;
  224. r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
  225. if (r < 0) {
  226. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
  227. fflush(stderr);
  228. }
  229. assert(r >= 0);
  230. for (i = 0; i < total_queues; i++) {
  231. vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
  232. }
  233. }
  234. void vhost_net_cleanup(struct vhost_net *net)
  235. {
  236. vhost_dev_cleanup(&net->dev);
  237. g_free(net);
  238. }
  239. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  240. {
  241. return vhost_virtqueue_pending(&net->dev, idx);
  242. }
  243. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  244. int idx, bool mask)
  245. {
  246. vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  247. }
  248. #else
  249. struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
  250. bool force)
  251. {
  252. error_report("vhost-net support is not compiled in");
  253. return NULL;
  254. }
  255. bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
  256. {
  257. return false;
  258. }
  259. int vhost_net_start(VirtIODevice *dev,
  260. NetClientState *ncs,
  261. int total_queues)
  262. {
  263. return -ENOSYS;
  264. }
  265. void vhost_net_stop(VirtIODevice *dev,
  266. NetClientState *ncs,
  267. int total_queues)
  268. {
  269. }
  270. void vhost_net_cleanup(struct vhost_net *net)
  271. {
  272. }
  273. unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
  274. {
  275. return features;
  276. }
  277. void vhost_net_ack_features(struct vhost_net *net, unsigned features)
  278. {
  279. }
  280. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  281. {
  282. return false;
  283. }
  284. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  285. int idx, bool mask)
  286. {
  287. }
  288. #endif