2
0

vhost_net.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 "qapi/error.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/main-loop.h"
  26. #include <sys/socket.h>
  27. #include <net/if.h>
  28. #include <netinet/in.h>
  29. #include "standard-headers/linux/virtio_ring.h"
  30. #include "hw/virtio/vhost.h"
  31. #include "hw/virtio/virtio-bus.h"
  32. #include "linux-headers/linux/vhost.h"
  33. /* Features supported by host kernel. */
  34. static const int kernel_feature_bits[] = {
  35. VIRTIO_F_NOTIFY_ON_EMPTY,
  36. VIRTIO_RING_F_INDIRECT_DESC,
  37. VIRTIO_RING_F_EVENT_IDX,
  38. VIRTIO_NET_F_MRG_RXBUF,
  39. VIRTIO_F_VERSION_1,
  40. VIRTIO_NET_F_MTU,
  41. VIRTIO_F_IOMMU_PLATFORM,
  42. VIRTIO_F_RING_PACKED,
  43. VIRTIO_F_RING_RESET,
  44. VIRTIO_F_IN_ORDER,
  45. VIRTIO_F_NOTIFICATION_DATA,
  46. VIRTIO_NET_F_RSC_EXT,
  47. VIRTIO_NET_F_HASH_REPORT,
  48. VHOST_INVALID_FEATURE_BIT
  49. };
  50. /* Features supported by others. */
  51. static const int user_feature_bits[] = {
  52. VIRTIO_F_NOTIFY_ON_EMPTY,
  53. VIRTIO_F_NOTIFICATION_DATA,
  54. VIRTIO_RING_F_INDIRECT_DESC,
  55. VIRTIO_RING_F_EVENT_IDX,
  56. VIRTIO_F_ANY_LAYOUT,
  57. VIRTIO_F_VERSION_1,
  58. VIRTIO_NET_F_CSUM,
  59. VIRTIO_NET_F_GUEST_CSUM,
  60. VIRTIO_NET_F_GSO,
  61. VIRTIO_NET_F_GUEST_TSO4,
  62. VIRTIO_NET_F_GUEST_TSO6,
  63. VIRTIO_NET_F_GUEST_ECN,
  64. VIRTIO_NET_F_GUEST_UFO,
  65. VIRTIO_NET_F_HOST_TSO4,
  66. VIRTIO_NET_F_HOST_TSO6,
  67. VIRTIO_NET_F_HOST_ECN,
  68. VIRTIO_NET_F_HOST_UFO,
  69. VIRTIO_NET_F_MRG_RXBUF,
  70. VIRTIO_NET_F_MTU,
  71. VIRTIO_F_IOMMU_PLATFORM,
  72. VIRTIO_F_RING_PACKED,
  73. VIRTIO_F_RING_RESET,
  74. VIRTIO_F_IN_ORDER,
  75. VIRTIO_NET_F_RSS,
  76. VIRTIO_NET_F_RSC_EXT,
  77. VIRTIO_NET_F_HASH_REPORT,
  78. VIRTIO_NET_F_GUEST_USO4,
  79. VIRTIO_NET_F_GUEST_USO6,
  80. VIRTIO_NET_F_HOST_USO,
  81. /* This bit implies RARP isn't sent by QEMU out of band */
  82. VIRTIO_NET_F_GUEST_ANNOUNCE,
  83. VIRTIO_NET_F_MQ,
  84. VHOST_INVALID_FEATURE_BIT
  85. };
  86. static const int *vhost_net_get_feature_bits(struct vhost_net *net)
  87. {
  88. const int *feature_bits = 0;
  89. switch (net->nc->info->type) {
  90. case NET_CLIENT_DRIVER_TAP:
  91. feature_bits = kernel_feature_bits;
  92. break;
  93. case NET_CLIENT_DRIVER_VHOST_USER:
  94. feature_bits = user_feature_bits;
  95. break;
  96. #ifdef CONFIG_VHOST_NET_VDPA
  97. case NET_CLIENT_DRIVER_VHOST_VDPA:
  98. feature_bits = vdpa_feature_bits;
  99. break;
  100. #endif
  101. default:
  102. error_report("Feature bits not defined for this type: %d",
  103. net->nc->info->type);
  104. break;
  105. }
  106. return feature_bits;
  107. }
  108. uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
  109. {
  110. return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
  111. features);
  112. }
  113. int vhost_net_get_config(struct vhost_net *net, uint8_t *config,
  114. uint32_t config_len)
  115. {
  116. return vhost_dev_get_config(&net->dev, config, config_len, NULL);
  117. }
  118. int vhost_net_set_config(struct vhost_net *net, const uint8_t *data,
  119. uint32_t offset, uint32_t size, uint32_t flags)
  120. {
  121. return vhost_dev_set_config(&net->dev, data, offset, size, flags);
  122. }
  123. void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
  124. {
  125. net->dev.acked_features = net->dev.backend_features;
  126. vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
  127. }
  128. uint64_t vhost_net_get_max_queues(VHostNetState *net)
  129. {
  130. return net->dev.max_queues;
  131. }
  132. uint64_t vhost_net_get_acked_features(VHostNetState *net)
  133. {
  134. return net->dev.acked_features;
  135. }
  136. void vhost_net_save_acked_features(NetClientState *nc)
  137. {
  138. #ifdef CONFIG_VHOST_NET_USER
  139. if (nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  140. vhost_user_save_acked_features(nc);
  141. }
  142. #endif
  143. }
  144. static void vhost_net_disable_notifiers_nvhosts(VirtIODevice *dev,
  145. NetClientState *ncs, int data_queue_pairs, int nvhosts)
  146. {
  147. VirtIONet *n = VIRTIO_NET(dev);
  148. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  149. struct vhost_net *net;
  150. struct vhost_dev *hdev;
  151. int r, i, j;
  152. NetClientState *peer;
  153. /*
  154. * Batch all the host notifiers in a single transaction to avoid
  155. * quadratic time complexity in address_space_update_ioeventfds().
  156. */
  157. memory_region_transaction_begin();
  158. for (i = 0; i < nvhosts; i++) {
  159. if (i < data_queue_pairs) {
  160. peer = qemu_get_peer(ncs, i);
  161. } else {
  162. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  163. }
  164. net = get_vhost_net(peer);
  165. hdev = &net->dev;
  166. for (j = 0; j < hdev->nvqs; j++) {
  167. r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus),
  168. hdev->vq_index + j,
  169. false);
  170. if (r < 0) {
  171. error_report("vhost %d VQ %d notifier cleanup failed: %d",
  172. i, j, -r);
  173. }
  174. assert(r >= 0);
  175. }
  176. }
  177. /*
  178. * The transaction expects the ioeventfds to be open when it
  179. * commits. Do it now, before the cleanup loop.
  180. */
  181. memory_region_transaction_commit();
  182. for (i = 0; i < nvhosts; i++) {
  183. if (i < data_queue_pairs) {
  184. peer = qemu_get_peer(ncs, i);
  185. } else {
  186. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  187. }
  188. net = get_vhost_net(peer);
  189. hdev = &net->dev;
  190. for (j = 0; j < hdev->nvqs; j++) {
  191. virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus),
  192. hdev->vq_index + j);
  193. }
  194. virtio_device_release_ioeventfd(dev);
  195. }
  196. }
  197. static int vhost_net_enable_notifiers(VirtIODevice *dev,
  198. NetClientState *ncs, int data_queue_pairs, int cvq)
  199. {
  200. VirtIONet *n = VIRTIO_NET(dev);
  201. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  202. int nvhosts = data_queue_pairs + cvq;
  203. struct vhost_net *net;
  204. struct vhost_dev *hdev;
  205. int r, i, j, k;
  206. NetClientState *peer;
  207. /*
  208. * We will pass the notifiers to the kernel, make sure that QEMU
  209. * doesn't interfere.
  210. */
  211. for (i = 0; i < nvhosts; i++) {
  212. r = virtio_device_grab_ioeventfd(dev);
  213. if (r < 0) {
  214. error_report("vhost %d binding does not support host notifiers", i);
  215. for (k = 0; k < i; k++) {
  216. virtio_device_release_ioeventfd(dev);
  217. }
  218. return r;
  219. }
  220. }
  221. /*
  222. * Batch all the host notifiers in a single transaction to avoid
  223. * quadratic time complexity in address_space_update_ioeventfds().
  224. */
  225. memory_region_transaction_begin();
  226. for (i = 0; i < nvhosts; i++) {
  227. if (i < data_queue_pairs) {
  228. peer = qemu_get_peer(ncs, i);
  229. } else {
  230. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  231. }
  232. net = get_vhost_net(peer);
  233. hdev = &net->dev;
  234. for (j = 0; j < hdev->nvqs; j++) {
  235. r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus),
  236. hdev->vq_index + j,
  237. true);
  238. if (r < 0) {
  239. error_report("vhost %d VQ %d notifier binding failed: %d",
  240. i, j, -r);
  241. memory_region_transaction_commit();
  242. vhost_dev_disable_notifiers_nvqs(hdev, dev, j);
  243. goto fail_nvhosts;
  244. }
  245. }
  246. }
  247. memory_region_transaction_commit();
  248. return 0;
  249. fail_nvhosts:
  250. vhost_net_disable_notifiers_nvhosts(dev, ncs, data_queue_pairs, i);
  251. /*
  252. * This for loop starts from i+1, not i, because the i-th ioeventfd
  253. * has already been released in vhost_dev_disable_notifiers_nvqs().
  254. */
  255. for (k = i + 1; k < nvhosts; k++) {
  256. virtio_device_release_ioeventfd(dev);
  257. }
  258. return r;
  259. }
  260. /*
  261. * Stop processing guest IO notifications in qemu.
  262. * Start processing them in vhost in kernel.
  263. */
  264. static void vhost_net_disable_notifiers(VirtIODevice *dev,
  265. NetClientState *ncs, int data_queue_pairs, int cvq)
  266. {
  267. vhost_net_disable_notifiers_nvhosts(dev, ncs, data_queue_pairs,
  268. data_queue_pairs + cvq);
  269. }
  270. static int vhost_net_get_fd(NetClientState *backend)
  271. {
  272. switch (backend->info->type) {
  273. case NET_CLIENT_DRIVER_TAP:
  274. return tap_get_fd(backend);
  275. default:
  276. fprintf(stderr, "vhost-net requires tap backend\n");
  277. return -ENOSYS;
  278. }
  279. }
  280. struct vhost_net *vhost_net_init(VhostNetOptions *options)
  281. {
  282. int r;
  283. bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
  284. struct vhost_net *net = g_new0(struct vhost_net, 1);
  285. uint64_t features = 0;
  286. Error *local_err = NULL;
  287. if (!options->net_backend) {
  288. fprintf(stderr, "vhost-net requires net backend to be setup\n");
  289. goto fail;
  290. }
  291. net->nc = options->net_backend;
  292. net->dev.nvqs = options->nvqs;
  293. net->dev.max_queues = 1;
  294. net->dev.vqs = net->vqs;
  295. if (backend_kernel) {
  296. r = vhost_net_get_fd(options->net_backend);
  297. if (r < 0) {
  298. goto fail;
  299. }
  300. net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
  301. ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
  302. net->backend = r;
  303. net->dev.protocol_features = 0;
  304. } else {
  305. net->dev.backend_features = 0;
  306. net->dev.protocol_features = 0;
  307. net->backend = -1;
  308. /* vhost-user needs vq_index to initiate a specific queue pair */
  309. net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
  310. }
  311. r = vhost_dev_init(&net->dev, options->opaque,
  312. options->backend_type, options->busyloop_timeout,
  313. &local_err);
  314. if (r < 0) {
  315. error_report_err(local_err);
  316. goto fail;
  317. }
  318. if (backend_kernel) {
  319. if (!qemu_has_vnet_hdr_len(options->net_backend,
  320. sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
  321. net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
  322. }
  323. if (~net->dev.features & net->dev.backend_features) {
  324. fprintf(stderr, "vhost lacks feature mask 0x%" PRIx64
  325. " for backend\n",
  326. (uint64_t)(~net->dev.features & net->dev.backend_features));
  327. goto fail;
  328. }
  329. }
  330. /* Set sane init value. Override when guest acks. */
  331. #ifdef CONFIG_VHOST_NET_USER
  332. if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  333. features = vhost_user_get_acked_features(net->nc);
  334. if (~net->dev.features & features) {
  335. fprintf(stderr, "vhost lacks feature mask 0x%" PRIx64
  336. " for backend\n",
  337. (uint64_t)(~net->dev.features & features));
  338. goto fail;
  339. }
  340. }
  341. #endif
  342. vhost_net_ack_features(net, features);
  343. return net;
  344. fail:
  345. vhost_dev_cleanup(&net->dev);
  346. g_free(net);
  347. return NULL;
  348. }
  349. static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index,
  350. int vq_index_end)
  351. {
  352. net->dev.vq_index = vq_index;
  353. net->dev.vq_index_end = vq_index_end;
  354. }
  355. static int vhost_net_start_one(struct vhost_net *net,
  356. VirtIODevice *dev)
  357. {
  358. struct vhost_vring_file file = { };
  359. int r;
  360. if (net->nc->info->start) {
  361. r = net->nc->info->start(net->nc);
  362. if (r < 0) {
  363. return r;
  364. }
  365. }
  366. r = vhost_dev_start(&net->dev, dev, false);
  367. if (r < 0) {
  368. goto fail_start;
  369. }
  370. if (net->nc->info->poll) {
  371. net->nc->info->poll(net->nc, false);
  372. }
  373. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  374. qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
  375. file.fd = net->backend;
  376. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  377. if (!virtio_queue_enabled(dev, net->dev.vq_index +
  378. file.index)) {
  379. /* Queue might not be ready for start */
  380. continue;
  381. }
  382. r = vhost_net_set_backend(&net->dev, &file);
  383. if (r < 0) {
  384. r = -errno;
  385. goto fail;
  386. }
  387. }
  388. }
  389. if (net->nc->info->load) {
  390. r = net->nc->info->load(net->nc);
  391. if (r < 0) {
  392. goto fail;
  393. }
  394. }
  395. return 0;
  396. fail:
  397. file.fd = -1;
  398. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  399. while (file.index-- > 0) {
  400. if (!virtio_queue_enabled(dev, net->dev.vq_index +
  401. file.index)) {
  402. /* Queue might not be ready for start */
  403. continue;
  404. }
  405. int ret = vhost_net_set_backend(&net->dev, &file);
  406. assert(ret >= 0);
  407. }
  408. }
  409. if (net->nc->info->poll) {
  410. net->nc->info->poll(net->nc, true);
  411. }
  412. vhost_dev_stop(&net->dev, dev, false);
  413. fail_start:
  414. return r;
  415. }
  416. static void vhost_net_stop_one(struct vhost_net *net,
  417. VirtIODevice *dev)
  418. {
  419. struct vhost_vring_file file = { .fd = -1 };
  420. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  421. for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
  422. int r = vhost_net_set_backend(&net->dev, &file);
  423. assert(r >= 0);
  424. }
  425. }
  426. if (net->nc->info->poll) {
  427. net->nc->info->poll(net->nc, true);
  428. }
  429. vhost_dev_stop(&net->dev, dev, false);
  430. if (net->nc->info->stop) {
  431. net->nc->info->stop(net->nc);
  432. }
  433. }
  434. int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
  435. int data_queue_pairs, int cvq)
  436. {
  437. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  438. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  439. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  440. int total_notifiers = data_queue_pairs * 2 + cvq;
  441. VirtIONet *n = VIRTIO_NET(dev);
  442. int nvhosts = data_queue_pairs + cvq;
  443. struct vhost_net *net;
  444. int r, e, i, index_end = data_queue_pairs * 2;
  445. NetClientState *peer;
  446. if (cvq) {
  447. index_end += 1;
  448. }
  449. if (!k->set_guest_notifiers) {
  450. error_report("binding does not support guest notifiers");
  451. return -ENOSYS;
  452. }
  453. for (i = 0; i < nvhosts; i++) {
  454. if (i < data_queue_pairs) {
  455. peer = qemu_get_peer(ncs, i);
  456. } else { /* Control Virtqueue */
  457. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  458. }
  459. net = get_vhost_net(peer);
  460. vhost_net_set_vq_index(net, i * 2, index_end);
  461. /* Suppress the masking guest notifiers on vhost user
  462. * because vhost user doesn't interrupt masking/unmasking
  463. * properly.
  464. */
  465. if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  466. dev->use_guest_notifier_mask = false;
  467. }
  468. }
  469. r = vhost_net_enable_notifiers(dev, ncs, data_queue_pairs, cvq);
  470. if (r < 0) {
  471. error_report("Error enabling host notifiers: %d", -r);
  472. goto err;
  473. }
  474. r = k->set_guest_notifiers(qbus->parent, total_notifiers, true);
  475. if (r < 0) {
  476. error_report("Error binding guest notifier: %d", -r);
  477. goto err_host_notifiers;
  478. }
  479. for (i = 0; i < nvhosts; i++) {
  480. if (i < data_queue_pairs) {
  481. peer = qemu_get_peer(ncs, i);
  482. } else {
  483. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  484. }
  485. if (peer->vring_enable) {
  486. /* restore vring enable state */
  487. r = vhost_set_vring_enable(peer, peer->vring_enable);
  488. if (r < 0) {
  489. goto err_guest_notifiers;
  490. }
  491. }
  492. r = vhost_net_start_one(get_vhost_net(peer), dev);
  493. if (r < 0) {
  494. goto err_guest_notifiers;
  495. }
  496. }
  497. return 0;
  498. err_guest_notifiers:
  499. while (--i >= 0) {
  500. peer = qemu_get_peer(ncs, i < data_queue_pairs ?
  501. i : n->max_queue_pairs);
  502. vhost_net_stop_one(get_vhost_net(peer), dev);
  503. }
  504. e = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
  505. if (e < 0) {
  506. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
  507. fflush(stderr);
  508. }
  509. err_host_notifiers:
  510. vhost_net_disable_notifiers(dev, ncs, data_queue_pairs, cvq);
  511. err:
  512. return r;
  513. }
  514. void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
  515. int data_queue_pairs, int cvq)
  516. {
  517. BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
  518. VirtioBusState *vbus = VIRTIO_BUS(qbus);
  519. VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
  520. VirtIONet *n = VIRTIO_NET(dev);
  521. NetClientState *peer;
  522. int total_notifiers = data_queue_pairs * 2 + cvq;
  523. int nvhosts = data_queue_pairs + cvq;
  524. int i, r;
  525. for (i = 0; i < nvhosts; i++) {
  526. if (i < data_queue_pairs) {
  527. peer = qemu_get_peer(ncs, i);
  528. } else {
  529. peer = qemu_get_peer(ncs, n->max_queue_pairs);
  530. }
  531. vhost_net_stop_one(get_vhost_net(peer), dev);
  532. }
  533. r = k->set_guest_notifiers(qbus->parent, total_notifiers, false);
  534. if (r < 0) {
  535. fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
  536. fflush(stderr);
  537. }
  538. assert(r >= 0);
  539. vhost_net_disable_notifiers(dev, ncs, data_queue_pairs, cvq);
  540. }
  541. void vhost_net_cleanup(struct vhost_net *net)
  542. {
  543. vhost_dev_cleanup(&net->dev);
  544. }
  545. int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
  546. {
  547. const VhostOps *vhost_ops = net->dev.vhost_ops;
  548. assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  549. assert(vhost_ops->vhost_migration_done);
  550. return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
  551. }
  552. bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
  553. {
  554. return vhost_virtqueue_pending(&net->dev, idx);
  555. }
  556. void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
  557. int idx, bool mask)
  558. {
  559. vhost_virtqueue_mask(&net->dev, dev, idx, mask);
  560. }
  561. bool vhost_net_config_pending(VHostNetState *net)
  562. {
  563. return vhost_config_pending(&net->dev);
  564. }
  565. void vhost_net_config_mask(VHostNetState *net, VirtIODevice *dev, bool mask)
  566. {
  567. vhost_config_mask(&net->dev, dev, mask);
  568. }
  569. VHostNetState *get_vhost_net(NetClientState *nc)
  570. {
  571. VHostNetState *vhost_net = 0;
  572. if (!nc) {
  573. return 0;
  574. }
  575. switch (nc->info->type) {
  576. case NET_CLIENT_DRIVER_TAP:
  577. vhost_net = tap_get_vhost_net(nc);
  578. /*
  579. * tap_get_vhost_net() can return NULL if a tap net-device backend is
  580. * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
  581. * vhostforce or vhostfd options at all. Please see net_init_tap_one().
  582. * Hence, we omit the assertion here.
  583. */
  584. break;
  585. #ifdef CONFIG_VHOST_NET_USER
  586. case NET_CLIENT_DRIVER_VHOST_USER:
  587. vhost_net = vhost_user_get_vhost_net(nc);
  588. assert(vhost_net);
  589. break;
  590. #endif
  591. #ifdef CONFIG_VHOST_NET_VDPA
  592. case NET_CLIENT_DRIVER_VHOST_VDPA:
  593. vhost_net = vhost_vdpa_get_vhost_net(nc);
  594. assert(vhost_net);
  595. break;
  596. #endif
  597. default:
  598. break;
  599. }
  600. return vhost_net;
  601. }
  602. int vhost_set_vring_enable(NetClientState *nc, int enable)
  603. {
  604. VHostNetState *net = get_vhost_net(nc);
  605. const VhostOps *vhost_ops = net->dev.vhost_ops;
  606. /*
  607. * vhost-vdpa network devices need to enable dataplane virtqueues after
  608. * DRIVER_OK, so they can recover device state before starting dataplane.
  609. * Because of that, we don't enable virtqueues here and leave it to
  610. * net/vhost-vdpa.c.
  611. */
  612. if (nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
  613. return 0;
  614. }
  615. nc->vring_enable = enable;
  616. if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
  617. return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
  618. }
  619. return 0;
  620. }
  621. int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
  622. {
  623. const VhostOps *vhost_ops = net->dev.vhost_ops;
  624. if (!vhost_ops->vhost_net_set_mtu) {
  625. return 0;
  626. }
  627. return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
  628. }
  629. void vhost_net_virtqueue_reset(VirtIODevice *vdev, NetClientState *nc,
  630. int vq_index)
  631. {
  632. VHostNetState *net = get_vhost_net(nc->peer);
  633. const VhostOps *vhost_ops = net->dev.vhost_ops;
  634. struct vhost_vring_file file = { .fd = -1 };
  635. int idx;
  636. /* should only be called after backend is connected */
  637. assert(vhost_ops);
  638. idx = vhost_ops->vhost_get_vq_index(&net->dev, vq_index);
  639. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  640. file.index = idx;
  641. int r = vhost_net_set_backend(&net->dev, &file);
  642. assert(r >= 0);
  643. }
  644. vhost_virtqueue_stop(&net->dev,
  645. vdev,
  646. net->dev.vqs + idx,
  647. net->dev.vq_index + idx);
  648. }
  649. int vhost_net_virtqueue_restart(VirtIODevice *vdev, NetClientState *nc,
  650. int vq_index)
  651. {
  652. VHostNetState *net = get_vhost_net(nc->peer);
  653. const VhostOps *vhost_ops = net->dev.vhost_ops;
  654. struct vhost_vring_file file = { };
  655. int idx, r;
  656. if (!net->dev.started) {
  657. return -EBUSY;
  658. }
  659. /* should only be called after backend is connected */
  660. assert(vhost_ops);
  661. idx = vhost_ops->vhost_get_vq_index(&net->dev, vq_index);
  662. r = vhost_virtqueue_start(&net->dev,
  663. vdev,
  664. net->dev.vqs + idx,
  665. net->dev.vq_index + idx);
  666. if (r < 0) {
  667. goto err_start;
  668. }
  669. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  670. file.index = idx;
  671. file.fd = net->backend;
  672. r = vhost_net_set_backend(&net->dev, &file);
  673. if (r < 0) {
  674. r = -errno;
  675. goto err_start;
  676. }
  677. }
  678. return 0;
  679. err_start:
  680. error_report("Error when restarting the queue.");
  681. if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
  682. file.fd = VHOST_FILE_UNBIND;
  683. file.index = idx;
  684. int ret = vhost_net_set_backend(&net->dev, &file);
  685. assert(ret >= 0);
  686. }
  687. vhost_dev_stop(&net->dev, vdev, false);
  688. return r;
  689. }