2
0

vhost_net.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "qemu/osdep.h"
  16. #include "net/net.h"
  17. #include "net/tap.h"
  18. #include "net/vhost-user.h"
  19. #include "standard-headers/linux/vhost_types.h"
  20. #include "hw/virtio/virtio-net.h"
  21. #include "net/vhost_net.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/main-loop.h"
  24. #include <sys/socket.h>
  25. #include <net/if.h>
  26. #include <netinet/in.h>
  27. #include "standard-headers/linux/virtio_ring.h"
  28. #include "hw/virtio/vhost.h"
  29. #include "hw/virtio/virtio-bus.h"
  30. struct vhost_net {
  31. struct vhost_dev dev;
  32. struct vhost_virtqueue vqs[2];
  33. int backend;
  34. NetClientState *nc;
  35. };
  36. /* Features supported by host kernel. */
  37. static const int kernel_feature_bits[] = {
  38. VIRTIO_F_NOTIFY_ON_EMPTY,
  39. VIRTIO_RING_F_INDIRECT_DESC,
  40. VIRTIO_RING_F_EVENT_IDX,
  41. VIRTIO_NET_F_MRG_RXBUF,
  42. VIRTIO_F_VERSION_1,
  43. VIRTIO_NET_F_MTU,
  44. VIRTIO_F_IOMMU_PLATFORM,
  45. VIRTIO_F_RING_PACKED,
  46. VHOST_INVALID_FEATURE_BIT
  47. };
  48. /* Features supported by others. */
  49. static const int user_feature_bits[] = {
  50. VIRTIO_F_NOTIFY_ON_EMPTY,
  51. VIRTIO_RING_F_INDIRECT_DESC,
  52. VIRTIO_RING_F_EVENT_IDX,
  53. VIRTIO_F_ANY_LAYOUT,
  54. VIRTIO_F_VERSION_1,
  55. VIRTIO_NET_F_CSUM,
  56. VIRTIO_NET_F_GUEST_CSUM,
  57. VIRTIO_NET_F_GSO,
  58. VIRTIO_NET_F_GUEST_TSO4,
  59. VIRTIO_NET_F_GUEST_TSO6,
  60. VIRTIO_NET_F_GUEST_ECN,
  61. VIRTIO_NET_F_GUEST_UFO,
  62. VIRTIO_NET_F_HOST_TSO4,
  63. VIRTIO_NET_F_HOST_TSO6,
  64. VIRTIO_NET_F_HOST_ECN,
  65. VIRTIO_NET_F_HOST_UFO,
  66. VIRTIO_NET_F_MRG_RXBUF,
  67. VIRTIO_NET_F_MTU,
  68. VIRTIO_F_IOMMU_PLATFORM,
  69. VIRTIO_F_RING_PACKED,
  70. /* This bit implies RARP isn't sent by QEMU out of band */
  71. VIRTIO_NET_F_GUEST_ANNOUNCE,
  72. VIRTIO_NET_F_MQ,
  73. VHOST_INVALID_FEATURE_BIT
  74. };
  75. static const int *vhost_net_get_feature_bits(struct vhost_net *net)
  76. {
  77. const int *feature_bits = 0;
  78. switch (net->nc->info->type) {
  79. case NET_CLIENT_DRIVER_TAP:
  80. feature_bits = kernel_feature_bits;
  81. break;
  82. case NET_CLIENT_DRIVER_VHOST_USER:
  83. feature_bits = user_feature_bits;
  84. break;
  85. default:
  86. error_report("Feature bits not defined for this type: %d",
  87. net->nc->info->type);
  88. break;
  89. }
  90. return feature_bits;
  91. }
  92. uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
  93. {
  94. return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
  95. features);
  96. }
  97. void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
  98. {
  99. net->dev.acked_features = net->dev.backend_features;
  100. vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
  101. }
  102. uint64_t vhost_net_get_max_queues(VHostNetState *net)
  103. {
  104. return net->dev.max_queues;
  105. }
  106. uint64_t vhost_net_get_acked_features(VHostNetState *net)
  107. {
  108. return net->dev.acked_features;
  109. }
  110. static int vhost_net_get_fd(NetClientState *backend)
  111. {
  112. switch (backend->info->type) {
  113. case NET_CLIENT_DRIVER_TAP:
  114. return tap_get_fd(backend);
  115. default:
  116. fprintf(stderr, "vhost-net requires tap backend\n");
  117. return -ENOSYS;
  118. }
  119. }
  120. struct vhost_net *vhost_net_init(VhostNetOptions *options)
  121. {
  122. int r;
  123. bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
  124. struct vhost_net *net = g_new0(struct vhost_net, 1);
  125. uint64_t features = 0;
  126. if (!options->net_backend) {
  127. fprintf(stderr, "vhost-net requires net backend to be setup\n");
  128. goto fail;
  129. }
  130. net->nc = options->net_backend;
  131. net->dev.max_queues = 1;
  132. net->dev.nvqs = 2;
  133. net->dev.vqs = net->vqs;
  134. if (backend_kernel) {
  135. r = vhost_net_get_fd(options->net_backend);
  136. if (r < 0) {
  137. goto fail;
  138. }
  139. net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
  140. ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
  141. net->backend = r;
  142. net->dev.protocol_features = 0;
  143. } else {
  144. net->dev.backend_features = 0;
  145. net->dev.protocol_features = 0;
  146. net->backend = -1;
  147. /* vhost-user needs vq_index to initiate a specific queue pair */
  148. net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
  149. }
  150. r = vhost_dev_init(&net->dev, options->opaque,
  151. options->backend_type, options->busyloop_timeout);
  152. if (r < 0) {
  153. goto fail;
  154. }
  155. if (backend_kernel) {
  156. if (!qemu_has_vnet_hdr_len(options->net_backend,
  157. sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
  158. net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
  159. }
  160. if (~net->dev.features & net->dev.backend_features) {
  161. fprintf(stderr, "vhost lacks feature mask %" PRIu64
  162. " for backend\n",
  163. (uint64_t)(~net->dev.features & net->dev.backend_features));
  164. goto fail;
  165. }
  166. }
  167. /* Set sane init value. Override when guest acks. */
  168. #ifdef CONFIG_VHOST_NET_USER
  169. if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  170. features = vhost_user_get_acked_features(net->nc);
  171. if (~net->dev.features & features) {
  172. fprintf(stderr, "vhost lacks feature mask %" PRIu64
  173. " for backend\n",
  174. (uint64_t)(~net->dev.features & features));
  175. goto fail;
  176. }
  177. }
  178. #endif
  179. vhost_net_ack_features(net, features);
  180. return net;
  181. fail:
  182. vhost_dev_cleanup(&net->dev);
  183. g_free(net);
  184. return NULL;
  185. }
  186. static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
  187. {
  188. net->dev.vq_index = vq_index;
  189. }
  190. static int vhost_net_start_one(struct vhost_net *net,
  191. VirtIODevice *dev)
  192. {
  193. struct vhost_vring_file file = { };
  194. int r;
  195. net->dev.nvqs = 2;
  196. net->dev.vqs = net->vqs;
  197. r = vhost_dev_enable_notifiers(&net->dev, dev);
  198. if (r < 0) {
  199. goto fail_notifiers;
  200. }
  201. r = vhost_dev_start(&net->dev, dev);
  202. if (r < 0) {
  203. goto fail_start;
  204. }
  205. if (net->nc->info->poll) {
  206. net->nc->info->poll(net->nc, false);
  207. }
  208. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  209. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  210. file.fd = net->backend;
  211. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  212. if (!virtio_queue_enabled(dev, net->dev.vq_index +
  213. file.index)) {
  214. /* Queue might not be ready for start */
  215. continue;
  216. }
  217. r = vhost_net_set_backend(&net->dev, &file);
  218. if (r < 0) {
  219. r = -errno;
  220. goto fail;
  221. }
  222. }
  223. }
  224. return 0;
  225. fail:
  226. file.fd = -1;
  227. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  228. while (file.index-- > 0) {
  229. if (!virtio_queue_enabled(dev, net->dev.vq_index +
  230. file.index)) {
  231. /* Queue might not be ready for start */
  232. continue;
  233. }
  234. int r = vhost_net_set_backend(&net->dev, &file);
  235. assert(r >= 0);
  236. }
  237. }
  238. if (net->nc->info->poll) {
  239. net->nc->info->poll(net->nc, true);
  240. }
  241. vhost_dev_stop(&net->dev, dev);
  242. fail_start:
  243. vhost_dev_disable_notifiers(&net->dev, dev);
  244. fail_notifiers:
  245. return r;
  246. }
  247. static void vhost_net_stop_one(struct vhost_net *net,
  248. VirtIODevice *dev)
  249. {
  250. struct vhost_vring_file file = { .fd = -1 };
  251. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  252. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  253. int r = vhost_net_set_backend(&net->dev, &file);
  254. assert(r >= 0);
  255. }
  256. }
  257. if (net->nc->info->poll) {
  258. net->nc->info->poll(net->nc, true);
  259. }
  260. vhost_dev_stop(&net->dev, dev);
  261. vhost_dev_disable_notifiers(&net->dev, dev);
  262. }
  263. int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  264. int total_queues)
  265. {
  266. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  267. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  268. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  269. int r, e, i;
  270. if (!k->set_guest_notifiers) {
  271. error_report("binding does not support guest notifiers");
  272. return -ENOSYS;
  273. }
  274. for (i = 0; i < total_queues; i++) {
  275. struct vhost_net *net;
  276. net = get_vhost_net(ncs[i].peer);
  277. vhost_net_set_vq_index(net, i * 2);
  278. /* Suppress the masking guest notifiers on vhost user
  279. * because vhost user doesn't interrupt masking/unmasking
  280. * properly.
  281. */
  282. if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  283. dev->use_guest_notifier_mask = false;
  284. }
  285. }
  286. r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
  287. if (r < 0) {
  288. error_report("Error binding guest notifier: %d", -r);
  289. goto err;
  290. }
  291. for (i = 0; i < total_queues; i++) {
  292. r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
  293. if (r < 0) {
  294. goto err_start;
  295. }
  296. if (ncs[i].peer->vring_enable) {
  297. /* restore vring enable state */
  298. r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
  299. if (r < 0) {
  300. goto err_start;
  301. }
  302. }
  303. }
  304. return 0;
  305. err_start:
  306. while (--i >= 0) {
  307. vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
  308. }
  309. e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
  310. if (e < 0) {
  311. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
  312. fflush(stderr);
  313. }
  314. err:
  315. return r;
  316. }
  317. void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
  318. int total_queues)
  319. {
  320. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  321. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  322. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  323. int i, r;
  324. for (i = 0; i < total_queues; i++) {
  325. vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
  326. }
  327. r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
  328. if (r < 0) {
  329. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
  330. fflush(stderr);
  331. }
  332. assert(r >= 0);
  333. }
  334. void vhost_net_cleanup(struct vhost_net *net)
  335. {
  336. vhost_dev_cleanup(&net->dev);
  337. }
  338. int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
  339. {
  340. const VhostOps *vhost_ops = net->dev.vhost_ops;
  341. assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  342. assert(vhost_ops->vhost_migration_done);
  343. return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
  344. }
  345. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  346. {
  347. return vhost_virtqueue_pending(&net->dev, idx);
  348. }
  349. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  350. int idx, bool mask)
  351. {
  352. vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  353. }
  354. VHostNetState *get_vhost_net(NetClientState *nc)
  355. {
  356. VHostNetState *vhost_net = 0;
  357. if (!nc) {
  358. return 0;
  359. }
  360. switch (nc->info->type) {
  361. case NET_CLIENT_DRIVER_TAP:
  362. vhost_net = tap_get_vhost_net(nc);
  363. break;
  364. #ifdef CONFIG_VHOST_NET_USER
  365. case NET_CLIENT_DRIVER_VHOST_USER:
  366. vhost_net = vhost_user_get_vhost_net(nc);
  367. assert(vhost_net);
  368. break;
  369. #endif
  370. default:
  371. break;
  372. }
  373. return vhost_net;
  374. }
  375. int vhost_set_vring_enable(NetClientState *nc, int enable)
  376. {
  377. VHostNetState *net = get_vhost_net(nc);
  378. const VhostOps *vhost_ops = net->dev.vhost_ops;
  379. nc->vring_enable = enable;
  380. if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
  381. return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
  382. }
  383. return 0;
  384. }
  385. int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
  386. {
  387. const VhostOps *vhost_ops = net->dev.vhost_ops;
  388. if (!vhost_ops->vhost_net_set_mtu) {
  389. return 0;
  390. }
  391. return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
  392. }