2
0

vhost-user.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * vhost-user.c
  3. *
  4. * Copyright (c) 2013 Virtual Open Systems Sarl.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "clients.h"
  12. #include "net/vhost_net.h"
  13. #include "net/vhost-user.h"
  14. #include "sysemu/char.h"
  15. #include "qemu/config-file.h"
  16. #include "qemu/error-report.h"
  17. #include "qmp-commands.h"
  18. #include "trace.h"
  19. typedef struct VhostUserState {
  20. NetClientState nc;
  21. CharBackend chr; /* only queue index 0 */
  22. VHostNetState *vhost_net;
  23. guint watch;
  24. uint64_t acked_features;
  25. bool started;
  26. } VhostUserState;
  27. VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
  28. {
  29. VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  30. assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  31. return s->vhost_net;
  32. }
  33. uint64_t vhost_user_get_acked_features(NetClientState *nc)
  34. {
  35. VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  36. assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  37. return s->acked_features;
  38. }
  39. static void vhost_user_stop(int queues, NetClientState *ncs[])
  40. {
  41. VhostUserState *s;
  42. int i;
  43. for (i = 0; i < queues; i++) {
  44. assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  45. s = DO_UPCAST(VhostUserState, nc, ncs[i]);
  46. if (s->vhost_net) {
  47. /* save acked features */
  48. uint64_t features = vhost_net_get_acked_features(s->vhost_net);
  49. if (features) {
  50. s->acked_features = features;
  51. }
  52. vhost_net_cleanup(s->vhost_net);
  53. }
  54. }
  55. }
  56. static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
  57. {
  58. VhostNetOptions options;
  59. struct vhost_net *net = NULL;
  60. VhostUserState *s;
  61. int max_queues;
  62. int i;
  63. options.backend_type = VHOST_BACKEND_TYPE_USER;
  64. for (i = 0; i < queues; i++) {
  65. assert(ncs[i]->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  66. s = DO_UPCAST(VhostUserState, nc, ncs[i]);
  67. options.net_backend = ncs[i];
  68. options.opaque = be;
  69. options.busyloop_timeout = 0;
  70. net = vhost_net_init(&options);
  71. if (!net) {
  72. error_report("failed to init vhost_net for queue %d", i);
  73. goto err;
  74. }
  75. if (i == 0) {
  76. max_queues = vhost_net_get_max_queues(net);
  77. if (queues > max_queues) {
  78. error_report("you are asking more queues than supported: %d",
  79. max_queues);
  80. goto err;
  81. }
  82. }
  83. if (s->vhost_net) {
  84. vhost_net_cleanup(s->vhost_net);
  85. g_free(s->vhost_net);
  86. }
  87. s->vhost_net = net;
  88. }
  89. return 0;
  90. err:
  91. if (net) {
  92. vhost_net_cleanup(net);
  93. }
  94. vhost_user_stop(i, ncs);
  95. return -1;
  96. }
  97. static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
  98. size_t size)
  99. {
  100. /* In case of RARP (message size is 60) notify backup to send a fake RARP.
  101. This fake RARP will be sent by backend only for guest
  102. without GUEST_ANNOUNCE capability.
  103. */
  104. if (size == 60) {
  105. VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  106. int r;
  107. static int display_rarp_failure = 1;
  108. char mac_addr[6];
  109. /* extract guest mac address from the RARP message */
  110. memcpy(mac_addr, &buf[6], 6);
  111. r = vhost_net_notify_migration_done(s->vhost_net, mac_addr);
  112. if ((r != 0) && (display_rarp_failure)) {
  113. fprintf(stderr,
  114. "Vhost user backend fails to broadcast fake RARP\n");
  115. fflush(stderr);
  116. display_rarp_failure = 0;
  117. }
  118. }
  119. return size;
  120. }
  121. static void vhost_user_cleanup(NetClientState *nc)
  122. {
  123. VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
  124. if (s->vhost_net) {
  125. vhost_net_cleanup(s->vhost_net);
  126. g_free(s->vhost_net);
  127. s->vhost_net = NULL;
  128. }
  129. if (nc->queue_index == 0) {
  130. Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
  131. qemu_chr_fe_deinit(&s->chr);
  132. qemu_chr_delete(chr);
  133. }
  134. qemu_purge_queued_packets(nc);
  135. }
  136. static bool vhost_user_has_vnet_hdr(NetClientState *nc)
  137. {
  138. assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  139. return true;
  140. }
  141. static bool vhost_user_has_ufo(NetClientState *nc)
  142. {
  143. assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_USER);
  144. return true;
  145. }
  146. static NetClientInfo net_vhost_user_info = {
  147. .type = NET_CLIENT_DRIVER_VHOST_USER,
  148. .size = sizeof(VhostUserState),
  149. .receive = vhost_user_receive,
  150. .cleanup = vhost_user_cleanup,
  151. .has_vnet_hdr = vhost_user_has_vnet_hdr,
  152. .has_ufo = vhost_user_has_ufo,
  153. };
  154. static gboolean net_vhost_user_watch(GIOChannel *chan, GIOCondition cond,
  155. void *opaque)
  156. {
  157. VhostUserState *s = opaque;
  158. qemu_chr_fe_disconnect(&s->chr);
  159. return TRUE;
  160. }
  161. static void net_vhost_user_event(void *opaque, int event);
  162. static void chr_closed_bh(void *opaque)
  163. {
  164. const char *name = opaque;
  165. NetClientState *ncs[MAX_QUEUE_NUM];
  166. VhostUserState *s;
  167. Error *err = NULL;
  168. int queues;
  169. queues = qemu_find_net_clients_except(name, ncs,
  170. NET_CLIENT_DRIVER_NIC,
  171. MAX_QUEUE_NUM);
  172. assert(queues < MAX_QUEUE_NUM);
  173. s = DO_UPCAST(VhostUserState, nc, ncs[0]);
  174. qmp_set_link(name, false, &err);
  175. vhost_user_stop(queues, ncs);
  176. qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, net_vhost_user_event,
  177. opaque, NULL, true);
  178. if (err) {
  179. error_report_err(err);
  180. }
  181. }
  182. static void net_vhost_user_event(void *opaque, int event)
  183. {
  184. const char *name = opaque;
  185. NetClientState *ncs[MAX_QUEUE_NUM];
  186. VhostUserState *s;
  187. Chardev *chr;
  188. Error *err = NULL;
  189. int queues;
  190. queues = qemu_find_net_clients_except(name, ncs,
  191. NET_CLIENT_DRIVER_NIC,
  192. MAX_QUEUE_NUM);
  193. assert(queues < MAX_QUEUE_NUM);
  194. s = DO_UPCAST(VhostUserState, nc, ncs[0]);
  195. chr = qemu_chr_fe_get_driver(&s->chr);
  196. trace_vhost_user_event(chr->label, event);
  197. switch (event) {
  198. case CHR_EVENT_OPENED:
  199. if (vhost_user_start(queues, ncs, &s->chr) < 0) {
  200. qemu_chr_fe_disconnect(&s->chr);
  201. return;
  202. }
  203. s->watch = qemu_chr_fe_add_watch(&s->chr, G_IO_HUP,
  204. net_vhost_user_watch, s);
  205. qmp_set_link(name, true, &err);
  206. s->started = true;
  207. break;
  208. case CHR_EVENT_CLOSED:
  209. /* a close event may happen during a read/write, but vhost
  210. * code assumes the vhost_dev remains setup, so delay the
  211. * stop & clear to idle.
  212. * FIXME: better handle failure in vhost code, remove bh
  213. */
  214. if (s->watch) {
  215. AioContext *ctx = qemu_get_current_aio_context();
  216. g_source_remove(s->watch);
  217. s->watch = 0;
  218. qemu_chr_fe_set_handlers(&s->chr, NULL, NULL, NULL,
  219. NULL, NULL, false);
  220. aio_bh_schedule_oneshot(ctx, chr_closed_bh, opaque);
  221. }
  222. break;
  223. }
  224. if (err) {
  225. error_report_err(err);
  226. }
  227. }
  228. static int net_vhost_user_init(NetClientState *peer, const char *device,
  229. const char *name, Chardev *chr,
  230. int queues)
  231. {
  232. Error *err = NULL;
  233. NetClientState *nc, *nc0 = NULL;
  234. VhostUserState *s;
  235. int i;
  236. assert(name);
  237. assert(queues > 0);
  238. for (i = 0; i < queues; i++) {
  239. nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
  240. snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
  241. i, chr->label);
  242. nc->queue_index = i;
  243. if (!nc0) {
  244. nc0 = nc;
  245. s = DO_UPCAST(VhostUserState, nc, nc);
  246. if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
  247. error_report_err(err);
  248. return -1;
  249. }
  250. }
  251. }
  252. s = DO_UPCAST(VhostUserState, nc, nc0);
  253. do {
  254. if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
  255. error_report_err(err);
  256. return -1;
  257. }
  258. qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
  259. net_vhost_user_event, nc0->name, NULL, true);
  260. } while (!s->started);
  261. assert(s->vhost_net);
  262. return 0;
  263. }
  264. static Chardev *net_vhost_claim_chardev(
  265. const NetdevVhostUserOptions *opts, Error **errp)
  266. {
  267. Chardev *chr = qemu_chr_find(opts->chardev);
  268. if (chr == NULL) {
  269. error_setg(errp, "chardev \"%s\" not found", opts->chardev);
  270. return NULL;
  271. }
  272. if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
  273. error_setg(errp, "chardev \"%s\" is not reconnectable",
  274. opts->chardev);
  275. return NULL;
  276. }
  277. if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
  278. error_setg(errp, "chardev \"%s\" does not support FD passing",
  279. opts->chardev);
  280. return NULL;
  281. }
  282. return chr;
  283. }
  284. static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp)
  285. {
  286. const char *name = opaque;
  287. const char *driver, *netdev;
  288. driver = qemu_opt_get(opts, "driver");
  289. netdev = qemu_opt_get(opts, "netdev");
  290. if (!driver || !netdev) {
  291. return 0;
  292. }
  293. if (strcmp(netdev, name) == 0 &&
  294. !g_str_has_prefix(driver, "virtio-net-")) {
  295. error_setg(errp, "vhost-user requires frontend driver virtio-net-*");
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. int net_init_vhost_user(const Netdev *netdev, const char *name,
  301. NetClientState *peer, Error **errp)
  302. {
  303. int queues;
  304. const NetdevVhostUserOptions *vhost_user_opts;
  305. Chardev *chr;
  306. assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
  307. vhost_user_opts = &netdev->u.vhost_user;
  308. chr = net_vhost_claim_chardev(vhost_user_opts, errp);
  309. if (!chr) {
  310. return -1;
  311. }
  312. /* verify net frontend */
  313. if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net,
  314. (char *)name, errp)) {
  315. return -1;
  316. }
  317. queues = vhost_user_opts->has_queues ? vhost_user_opts->queues : 1;
  318. if (queues < 1 || queues > MAX_QUEUE_NUM) {
  319. error_setg(errp,
  320. "vhost-user number of queues must be in range [1, %d]",
  321. MAX_QUEUE_NUM);
  322. return -1;
  323. }
  324. return net_vhost_user_init(peer, "vhost_user", name, chr, queues);
  325. }