vhost_net.c 13 KB

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