vhost_net.c 13 KB

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