vhost-user.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. /*
  2. * vhost-user
  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 "qapi/error.h"
  12. #include "hw/virtio/vhost.h"
  13. #include "hw/virtio/vhost-user.h"
  14. #include "hw/virtio/vhost-backend.h"
  15. #include "hw/virtio/virtio-net.h"
  16. #include "chardev/char-fe.h"
  17. #include "sysemu/kvm.h"
  18. #include "qemu/error-report.h"
  19. #include "qemu/sockets.h"
  20. #include "sysemu/cryptodev.h"
  21. #include "migration/migration.h"
  22. #include "migration/postcopy-ram.h"
  23. #include "trace.h"
  24. #include <sys/ioctl.h>
  25. #include <sys/socket.h>
  26. #include <sys/un.h>
  27. #include <linux/vhost.h>
  28. #include <linux/userfaultfd.h>
  29. #define VHOST_MEMORY_MAX_NREGIONS 8
  30. #define VHOST_USER_F_PROTOCOL_FEATURES 30
  31. #define VHOST_USER_SLAVE_MAX_FDS 8
  32. /*
  33. * Maximum size of virtio device config space
  34. */
  35. #define VHOST_USER_MAX_CONFIG_SIZE 256
  36. enum VhostUserProtocolFeature {
  37. VHOST_USER_PROTOCOL_F_MQ = 0,
  38. VHOST_USER_PROTOCOL_F_LOG_SHMFD = 1,
  39. VHOST_USER_PROTOCOL_F_RARP = 2,
  40. VHOST_USER_PROTOCOL_F_REPLY_ACK = 3,
  41. VHOST_USER_PROTOCOL_F_NET_MTU = 4,
  42. VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5,
  43. VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,
  44. VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
  45. VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
  46. VHOST_USER_PROTOCOL_F_CONFIG = 9,
  47. VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
  48. VHOST_USER_PROTOCOL_F_MAX
  49. };
  50. #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
  51. typedef enum VhostUserRequest {
  52. VHOST_USER_NONE = 0,
  53. VHOST_USER_GET_FEATURES = 1,
  54. VHOST_USER_SET_FEATURES = 2,
  55. VHOST_USER_SET_OWNER = 3,
  56. VHOST_USER_RESET_OWNER = 4,
  57. VHOST_USER_SET_MEM_TABLE = 5,
  58. VHOST_USER_SET_LOG_BASE = 6,
  59. VHOST_USER_SET_LOG_FD = 7,
  60. VHOST_USER_SET_VRING_NUM = 8,
  61. VHOST_USER_SET_VRING_ADDR = 9,
  62. VHOST_USER_SET_VRING_BASE = 10,
  63. VHOST_USER_GET_VRING_BASE = 11,
  64. VHOST_USER_SET_VRING_KICK = 12,
  65. VHOST_USER_SET_VRING_CALL = 13,
  66. VHOST_USER_SET_VRING_ERR = 14,
  67. VHOST_USER_GET_PROTOCOL_FEATURES = 15,
  68. VHOST_USER_SET_PROTOCOL_FEATURES = 16,
  69. VHOST_USER_GET_QUEUE_NUM = 17,
  70. VHOST_USER_SET_VRING_ENABLE = 18,
  71. VHOST_USER_SEND_RARP = 19,
  72. VHOST_USER_NET_SET_MTU = 20,
  73. VHOST_USER_SET_SLAVE_REQ_FD = 21,
  74. VHOST_USER_IOTLB_MSG = 22,
  75. VHOST_USER_SET_VRING_ENDIAN = 23,
  76. VHOST_USER_GET_CONFIG = 24,
  77. VHOST_USER_SET_CONFIG = 25,
  78. VHOST_USER_CREATE_CRYPTO_SESSION = 26,
  79. VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
  80. VHOST_USER_POSTCOPY_ADVISE = 28,
  81. VHOST_USER_POSTCOPY_LISTEN = 29,
  82. VHOST_USER_POSTCOPY_END = 30,
  83. VHOST_USER_MAX
  84. } VhostUserRequest;
  85. typedef enum VhostUserSlaveRequest {
  86. VHOST_USER_SLAVE_NONE = 0,
  87. VHOST_USER_SLAVE_IOTLB_MSG = 1,
  88. VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
  89. VHOST_USER_SLAVE_MAX
  90. } VhostUserSlaveRequest;
  91. typedef struct VhostUserMemoryRegion {
  92. uint64_t guest_phys_addr;
  93. uint64_t memory_size;
  94. uint64_t userspace_addr;
  95. uint64_t mmap_offset;
  96. } VhostUserMemoryRegion;
  97. typedef struct VhostUserMemory {
  98. uint32_t nregions;
  99. uint32_t padding;
  100. VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
  101. } VhostUserMemory;
  102. typedef struct VhostUserLog {
  103. uint64_t mmap_size;
  104. uint64_t mmap_offset;
  105. } VhostUserLog;
  106. typedef struct VhostUserConfig {
  107. uint32_t offset;
  108. uint32_t size;
  109. uint32_t flags;
  110. uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
  111. } VhostUserConfig;
  112. #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN 512
  113. #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN 64
  114. typedef struct VhostUserCryptoSession {
  115. /* session id for success, -1 on errors */
  116. int64_t session_id;
  117. CryptoDevBackendSymSessionInfo session_setup_data;
  118. uint8_t key[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN];
  119. uint8_t auth_key[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN];
  120. } VhostUserCryptoSession;
  121. static VhostUserConfig c __attribute__ ((unused));
  122. #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
  123. + sizeof(c.size) \
  124. + sizeof(c.flags))
  125. typedef struct {
  126. VhostUserRequest request;
  127. #define VHOST_USER_VERSION_MASK (0x3)
  128. #define VHOST_USER_REPLY_MASK (0x1<<2)
  129. #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
  130. uint32_t flags;
  131. uint32_t size; /* the following payload size */
  132. } QEMU_PACKED VhostUserHeader;
  133. typedef union {
  134. #define VHOST_USER_VRING_IDX_MASK (0xff)
  135. #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
  136. uint64_t u64;
  137. struct vhost_vring_state state;
  138. struct vhost_vring_addr addr;
  139. VhostUserMemory memory;
  140. VhostUserLog log;
  141. struct vhost_iotlb_msg iotlb;
  142. VhostUserConfig config;
  143. VhostUserCryptoSession session;
  144. } VhostUserPayload;
  145. typedef struct VhostUserMsg {
  146. VhostUserHeader hdr;
  147. VhostUserPayload payload;
  148. } QEMU_PACKED VhostUserMsg;
  149. static VhostUserMsg m __attribute__ ((unused));
  150. #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
  151. #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
  152. /* The version of the protocol we support */
  153. #define VHOST_USER_VERSION (0x1)
  154. struct vhost_user {
  155. struct vhost_dev *dev;
  156. /* Shared between vhost devs of the same virtio device */
  157. VhostUserState *user;
  158. int slave_fd;
  159. NotifierWithReturn postcopy_notifier;
  160. struct PostCopyFD postcopy_fd;
  161. uint64_t postcopy_client_bases[VHOST_MEMORY_MAX_NREGIONS];
  162. /* Length of the region_rb and region_rb_offset arrays */
  163. size_t region_rb_len;
  164. /* RAMBlock associated with a given region */
  165. RAMBlock **region_rb;
  166. /* The offset from the start of the RAMBlock to the start of the
  167. * vhost region.
  168. */
  169. ram_addr_t *region_rb_offset;
  170. /* True once we've entered postcopy_listen */
  171. bool postcopy_listen;
  172. };
  173. static bool ioeventfd_enabled(void)
  174. {
  175. return kvm_enabled() && kvm_eventfds_enabled();
  176. }
  177. static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
  178. {
  179. struct vhost_user *u = dev->opaque;
  180. CharBackend *chr = u->user->chr;
  181. uint8_t *p = (uint8_t *) msg;
  182. int r, size = VHOST_USER_HDR_SIZE;
  183. r = qemu_chr_fe_read_all(chr, p, size);
  184. if (r != size) {
  185. error_report("Failed to read msg header. Read %d instead of %d."
  186. " Original request %d.", r, size, msg->hdr.request);
  187. goto fail;
  188. }
  189. /* validate received flags */
  190. if (msg->hdr.flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) {
  191. error_report("Failed to read msg header."
  192. " Flags 0x%x instead of 0x%x.", msg->hdr.flags,
  193. VHOST_USER_REPLY_MASK | VHOST_USER_VERSION);
  194. goto fail;
  195. }
  196. /* validate message size is sane */
  197. if (msg->hdr.size > VHOST_USER_PAYLOAD_SIZE) {
  198. error_report("Failed to read msg header."
  199. " Size %d exceeds the maximum %zu.", msg->hdr.size,
  200. VHOST_USER_PAYLOAD_SIZE);
  201. goto fail;
  202. }
  203. if (msg->hdr.size) {
  204. p += VHOST_USER_HDR_SIZE;
  205. size = msg->hdr.size;
  206. r = qemu_chr_fe_read_all(chr, p, size);
  207. if (r != size) {
  208. error_report("Failed to read msg payload."
  209. " Read %d instead of %d.", r, msg->hdr.size);
  210. goto fail;
  211. }
  212. }
  213. return 0;
  214. fail:
  215. return -1;
  216. }
  217. static int process_message_reply(struct vhost_dev *dev,
  218. const VhostUserMsg *msg)
  219. {
  220. VhostUserMsg msg_reply;
  221. if ((msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
  222. return 0;
  223. }
  224. if (vhost_user_read(dev, &msg_reply) < 0) {
  225. return -1;
  226. }
  227. if (msg_reply.hdr.request != msg->hdr.request) {
  228. error_report("Received unexpected msg type."
  229. "Expected %d received %d",
  230. msg->hdr.request, msg_reply.hdr.request);
  231. return -1;
  232. }
  233. return msg_reply.payload.u64 ? -1 : 0;
  234. }
  235. static bool vhost_user_one_time_request(VhostUserRequest request)
  236. {
  237. switch (request) {
  238. case VHOST_USER_SET_OWNER:
  239. case VHOST_USER_RESET_OWNER:
  240. case VHOST_USER_SET_MEM_TABLE:
  241. case VHOST_USER_GET_QUEUE_NUM:
  242. case VHOST_USER_NET_SET_MTU:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. /* most non-init callers ignore the error */
  249. static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
  250. int *fds, int fd_num)
  251. {
  252. struct vhost_user *u = dev->opaque;
  253. CharBackend *chr = u->user->chr;
  254. int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size;
  255. /*
  256. * For non-vring specific requests, like VHOST_USER_SET_MEM_TABLE,
  257. * we just need send it once in the first time. For later such
  258. * request, we just ignore it.
  259. */
  260. if (vhost_user_one_time_request(msg->hdr.request) && dev->vq_index != 0) {
  261. msg->hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
  262. return 0;
  263. }
  264. if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) {
  265. error_report("Failed to set msg fds.");
  266. return -1;
  267. }
  268. ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size);
  269. if (ret != size) {
  270. error_report("Failed to write msg."
  271. " Wrote %d instead of %d.", ret, size);
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
  277. struct vhost_log *log)
  278. {
  279. int fds[VHOST_MEMORY_MAX_NREGIONS];
  280. size_t fd_num = 0;
  281. bool shmfd = virtio_has_feature(dev->protocol_features,
  282. VHOST_USER_PROTOCOL_F_LOG_SHMFD);
  283. VhostUserMsg msg = {
  284. .hdr.request = VHOST_USER_SET_LOG_BASE,
  285. .hdr.flags = VHOST_USER_VERSION,
  286. .payload.log.mmap_size = log->size * sizeof(*(log->log)),
  287. .payload.log.mmap_offset = 0,
  288. .hdr.size = sizeof(msg.payload.log),
  289. };
  290. if (shmfd && log->fd != -1) {
  291. fds[fd_num++] = log->fd;
  292. }
  293. if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
  294. return -1;
  295. }
  296. if (shmfd) {
  297. msg.hdr.size = 0;
  298. if (vhost_user_read(dev, &msg) < 0) {
  299. return -1;
  300. }
  301. if (msg.hdr.request != VHOST_USER_SET_LOG_BASE) {
  302. error_report("Received unexpected msg type. "
  303. "Expected %d received %d",
  304. VHOST_USER_SET_LOG_BASE, msg.hdr.request);
  305. return -1;
  306. }
  307. }
  308. return 0;
  309. }
  310. static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
  311. struct vhost_memory *mem)
  312. {
  313. struct vhost_user *u = dev->opaque;
  314. int fds[VHOST_MEMORY_MAX_NREGIONS];
  315. int i, fd;
  316. size_t fd_num = 0;
  317. bool reply_supported = virtio_has_feature(dev->protocol_features,
  318. VHOST_USER_PROTOCOL_F_REPLY_ACK);
  319. VhostUserMsg msg_reply;
  320. int region_i, msg_i;
  321. VhostUserMsg msg = {
  322. .hdr.request = VHOST_USER_SET_MEM_TABLE,
  323. .hdr.flags = VHOST_USER_VERSION,
  324. };
  325. if (reply_supported) {
  326. msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
  327. }
  328. if (u->region_rb_len < dev->mem->nregions) {
  329. u->region_rb = g_renew(RAMBlock*, u->region_rb, dev->mem->nregions);
  330. u->region_rb_offset = g_renew(ram_addr_t, u->region_rb_offset,
  331. dev->mem->nregions);
  332. memset(&(u->region_rb[u->region_rb_len]), '\0',
  333. sizeof(RAMBlock *) * (dev->mem->nregions - u->region_rb_len));
  334. memset(&(u->region_rb_offset[u->region_rb_len]), '\0',
  335. sizeof(ram_addr_t) * (dev->mem->nregions - u->region_rb_len));
  336. u->region_rb_len = dev->mem->nregions;
  337. }
  338. for (i = 0; i < dev->mem->nregions; ++i) {
  339. struct vhost_memory_region *reg = dev->mem->regions + i;
  340. ram_addr_t offset;
  341. MemoryRegion *mr;
  342. assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
  343. mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
  344. &offset);
  345. fd = memory_region_get_fd(mr);
  346. if (fd > 0) {
  347. trace_vhost_user_set_mem_table_withfd(fd_num, mr->name,
  348. reg->memory_size,
  349. reg->guest_phys_addr,
  350. reg->userspace_addr, offset);
  351. u->region_rb_offset[i] = offset;
  352. u->region_rb[i] = mr->ram_block;
  353. msg.payload.memory.regions[fd_num].userspace_addr =
  354. reg->userspace_addr;
  355. msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
  356. msg.payload.memory.regions[fd_num].guest_phys_addr =
  357. reg->guest_phys_addr;
  358. msg.payload.memory.regions[fd_num].mmap_offset = offset;
  359. assert(fd_num < VHOST_MEMORY_MAX_NREGIONS);
  360. fds[fd_num++] = fd;
  361. } else {
  362. u->region_rb_offset[i] = 0;
  363. u->region_rb[i] = NULL;
  364. }
  365. }
  366. msg.payload.memory.nregions = fd_num;
  367. if (!fd_num) {
  368. error_report("Failed initializing vhost-user memory map, "
  369. "consider using -object memory-backend-file share=on");
  370. return -1;
  371. }
  372. msg.hdr.size = sizeof(msg.payload.memory.nregions);
  373. msg.hdr.size += sizeof(msg.payload.memory.padding);
  374. msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion);
  375. if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
  376. return -1;
  377. }
  378. if (vhost_user_read(dev, &msg_reply) < 0) {
  379. return -1;
  380. }
  381. if (msg_reply.hdr.request != VHOST_USER_SET_MEM_TABLE) {
  382. error_report("%s: Received unexpected msg type."
  383. "Expected %d received %d", __func__,
  384. VHOST_USER_SET_MEM_TABLE, msg_reply.hdr.request);
  385. return -1;
  386. }
  387. /* We're using the same structure, just reusing one of the
  388. * fields, so it should be the same size.
  389. */
  390. if (msg_reply.hdr.size != msg.hdr.size) {
  391. error_report("%s: Unexpected size for postcopy reply "
  392. "%d vs %d", __func__, msg_reply.hdr.size, msg.hdr.size);
  393. return -1;
  394. }
  395. memset(u->postcopy_client_bases, 0,
  396. sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS);
  397. /* They're in the same order as the regions that were sent
  398. * but some of the regions were skipped (above) if they
  399. * didn't have fd's
  400. */
  401. for (msg_i = 0, region_i = 0;
  402. region_i < dev->mem->nregions;
  403. region_i++) {
  404. if (msg_i < fd_num &&
  405. msg_reply.payload.memory.regions[msg_i].guest_phys_addr ==
  406. dev->mem->regions[region_i].guest_phys_addr) {
  407. u->postcopy_client_bases[region_i] =
  408. msg_reply.payload.memory.regions[msg_i].userspace_addr;
  409. trace_vhost_user_set_mem_table_postcopy(
  410. msg_reply.payload.memory.regions[msg_i].userspace_addr,
  411. msg.payload.memory.regions[msg_i].userspace_addr,
  412. msg_i, region_i);
  413. msg_i++;
  414. }
  415. }
  416. if (msg_i != fd_num) {
  417. error_report("%s: postcopy reply not fully consumed "
  418. "%d vs %zd",
  419. __func__, msg_i, fd_num);
  420. return -1;
  421. }
  422. /* Now we've registered this with the postcopy code, we ack to the client,
  423. * because now we're in the position to be able to deal with any faults
  424. * it generates.
  425. */
  426. /* TODO: Use this for failure cases as well with a bad value */
  427. msg.hdr.size = sizeof(msg.payload.u64);
  428. msg.payload.u64 = 0; /* OK */
  429. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  430. return -1;
  431. }
  432. if (reply_supported) {
  433. return process_message_reply(dev, &msg);
  434. }
  435. return 0;
  436. }
  437. static int vhost_user_set_mem_table(struct vhost_dev *dev,
  438. struct vhost_memory *mem)
  439. {
  440. struct vhost_user *u = dev->opaque;
  441. int fds[VHOST_MEMORY_MAX_NREGIONS];
  442. int i, fd;
  443. size_t fd_num = 0;
  444. bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler;
  445. bool reply_supported = virtio_has_feature(dev->protocol_features,
  446. VHOST_USER_PROTOCOL_F_REPLY_ACK) &&
  447. !do_postcopy;
  448. if (do_postcopy) {
  449. /* Postcopy has enough differences that it's best done in it's own
  450. * version
  451. */
  452. return vhost_user_set_mem_table_postcopy(dev, mem);
  453. }
  454. VhostUserMsg msg = {
  455. .hdr.request = VHOST_USER_SET_MEM_TABLE,
  456. .hdr.flags = VHOST_USER_VERSION,
  457. };
  458. if (reply_supported) {
  459. msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
  460. }
  461. for (i = 0; i < dev->mem->nregions; ++i) {
  462. struct vhost_memory_region *reg = dev->mem->regions + i;
  463. ram_addr_t offset;
  464. MemoryRegion *mr;
  465. assert((uintptr_t)reg->userspace_addr == reg->userspace_addr);
  466. mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr,
  467. &offset);
  468. fd = memory_region_get_fd(mr);
  469. if (fd > 0) {
  470. if (fd_num == VHOST_MEMORY_MAX_NREGIONS) {
  471. error_report("Failed preparing vhost-user memory table msg");
  472. return -1;
  473. }
  474. msg.payload.memory.regions[fd_num].userspace_addr =
  475. reg->userspace_addr;
  476. msg.payload.memory.regions[fd_num].memory_size = reg->memory_size;
  477. msg.payload.memory.regions[fd_num].guest_phys_addr =
  478. reg->guest_phys_addr;
  479. msg.payload.memory.regions[fd_num].mmap_offset = offset;
  480. fds[fd_num++] = fd;
  481. }
  482. }
  483. msg.payload.memory.nregions = fd_num;
  484. if (!fd_num) {
  485. error_report("Failed initializing vhost-user memory map, "
  486. "consider using -object memory-backend-file share=on");
  487. return -1;
  488. }
  489. msg.hdr.size = sizeof(msg.payload.memory.nregions);
  490. msg.hdr.size += sizeof(msg.payload.memory.padding);
  491. msg.hdr.size += fd_num * sizeof(VhostUserMemoryRegion);
  492. if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
  493. return -1;
  494. }
  495. if (reply_supported) {
  496. return process_message_reply(dev, &msg);
  497. }
  498. return 0;
  499. }
  500. static int vhost_user_set_vring_addr(struct vhost_dev *dev,
  501. struct vhost_vring_addr *addr)
  502. {
  503. VhostUserMsg msg = {
  504. .hdr.request = VHOST_USER_SET_VRING_ADDR,
  505. .hdr.flags = VHOST_USER_VERSION,
  506. .payload.addr = *addr,
  507. .hdr.size = sizeof(msg.payload.addr),
  508. };
  509. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  510. return -1;
  511. }
  512. return 0;
  513. }
  514. static int vhost_user_set_vring_endian(struct vhost_dev *dev,
  515. struct vhost_vring_state *ring)
  516. {
  517. bool cross_endian = virtio_has_feature(dev->protocol_features,
  518. VHOST_USER_PROTOCOL_F_CROSS_ENDIAN);
  519. VhostUserMsg msg = {
  520. .hdr.request = VHOST_USER_SET_VRING_ENDIAN,
  521. .hdr.flags = VHOST_USER_VERSION,
  522. .payload.state = *ring,
  523. .hdr.size = sizeof(msg.payload.state),
  524. };
  525. if (!cross_endian) {
  526. error_report("vhost-user trying to send unhandled ioctl");
  527. return -1;
  528. }
  529. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. static int vhost_set_vring(struct vhost_dev *dev,
  535. unsigned long int request,
  536. struct vhost_vring_state *ring)
  537. {
  538. VhostUserMsg msg = {
  539. .hdr.request = request,
  540. .hdr.flags = VHOST_USER_VERSION,
  541. .payload.state = *ring,
  542. .hdr.size = sizeof(msg.payload.state),
  543. };
  544. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  545. return -1;
  546. }
  547. return 0;
  548. }
  549. static int vhost_user_set_vring_num(struct vhost_dev *dev,
  550. struct vhost_vring_state *ring)
  551. {
  552. return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
  553. }
  554. static int vhost_user_set_vring_base(struct vhost_dev *dev,
  555. struct vhost_vring_state *ring)
  556. {
  557. return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
  558. }
  559. static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
  560. {
  561. int i;
  562. if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
  563. return -1;
  564. }
  565. for (i = 0; i < dev->nvqs; ++i) {
  566. struct vhost_vring_state state = {
  567. .index = dev->vq_index + i,
  568. .num = enable,
  569. };
  570. vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state);
  571. }
  572. return 0;
  573. }
  574. static int vhost_user_get_vring_base(struct vhost_dev *dev,
  575. struct vhost_vring_state *ring)
  576. {
  577. VhostUserMsg msg = {
  578. .hdr.request = VHOST_USER_GET_VRING_BASE,
  579. .hdr.flags = VHOST_USER_VERSION,
  580. .payload.state = *ring,
  581. .hdr.size = sizeof(msg.payload.state),
  582. };
  583. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  584. return -1;
  585. }
  586. if (vhost_user_read(dev, &msg) < 0) {
  587. return -1;
  588. }
  589. if (msg.hdr.request != VHOST_USER_GET_VRING_BASE) {
  590. error_report("Received unexpected msg type. Expected %d received %d",
  591. VHOST_USER_GET_VRING_BASE, msg.hdr.request);
  592. return -1;
  593. }
  594. if (msg.hdr.size != sizeof(msg.payload.state)) {
  595. error_report("Received bad msg size.");
  596. return -1;
  597. }
  598. *ring = msg.payload.state;
  599. return 0;
  600. }
  601. static int vhost_set_vring_file(struct vhost_dev *dev,
  602. VhostUserRequest request,
  603. struct vhost_vring_file *file)
  604. {
  605. int fds[VHOST_MEMORY_MAX_NREGIONS];
  606. size_t fd_num = 0;
  607. VhostUserMsg msg = {
  608. .hdr.request = request,
  609. .hdr.flags = VHOST_USER_VERSION,
  610. .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
  611. .hdr.size = sizeof(msg.payload.u64),
  612. };
  613. if (ioeventfd_enabled() && file->fd > 0) {
  614. fds[fd_num++] = file->fd;
  615. } else {
  616. msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
  617. }
  618. if (vhost_user_write(dev, &msg, fds, fd_num) < 0) {
  619. return -1;
  620. }
  621. return 0;
  622. }
  623. static int vhost_user_set_vring_kick(struct vhost_dev *dev,
  624. struct vhost_vring_file *file)
  625. {
  626. return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
  627. }
  628. static int vhost_user_set_vring_call(struct vhost_dev *dev,
  629. struct vhost_vring_file *file)
  630. {
  631. return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
  632. }
  633. static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64)
  634. {
  635. VhostUserMsg msg = {
  636. .hdr.request = request,
  637. .hdr.flags = VHOST_USER_VERSION,
  638. .payload.u64 = u64,
  639. .hdr.size = sizeof(msg.payload.u64),
  640. };
  641. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  642. return -1;
  643. }
  644. return 0;
  645. }
  646. static int vhost_user_set_features(struct vhost_dev *dev,
  647. uint64_t features)
  648. {
  649. return vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES, features);
  650. }
  651. static int vhost_user_set_protocol_features(struct vhost_dev *dev,
  652. uint64_t features)
  653. {
  654. return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features);
  655. }
  656. static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
  657. {
  658. VhostUserMsg msg = {
  659. .hdr.request = request,
  660. .hdr.flags = VHOST_USER_VERSION,
  661. };
  662. if (vhost_user_one_time_request(request) && dev->vq_index != 0) {
  663. return 0;
  664. }
  665. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  666. return -1;
  667. }
  668. if (vhost_user_read(dev, &msg) < 0) {
  669. return -1;
  670. }
  671. if (msg.hdr.request != request) {
  672. error_report("Received unexpected msg type. Expected %d received %d",
  673. request, msg.hdr.request);
  674. return -1;
  675. }
  676. if (msg.hdr.size != sizeof(msg.payload.u64)) {
  677. error_report("Received bad msg size.");
  678. return -1;
  679. }
  680. *u64 = msg.payload.u64;
  681. return 0;
  682. }
  683. static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
  684. {
  685. return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features);
  686. }
  687. static int vhost_user_set_owner(struct vhost_dev *dev)
  688. {
  689. VhostUserMsg msg = {
  690. .hdr.request = VHOST_USER_SET_OWNER,
  691. .hdr.flags = VHOST_USER_VERSION,
  692. };
  693. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  694. return -1;
  695. }
  696. return 0;
  697. }
  698. static int vhost_user_reset_device(struct vhost_dev *dev)
  699. {
  700. VhostUserMsg msg = {
  701. .hdr.request = VHOST_USER_RESET_OWNER,
  702. .hdr.flags = VHOST_USER_VERSION,
  703. };
  704. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  705. return -1;
  706. }
  707. return 0;
  708. }
  709. static int vhost_user_slave_handle_config_change(struct vhost_dev *dev)
  710. {
  711. int ret = -1;
  712. if (!dev->config_ops) {
  713. return -1;
  714. }
  715. if (dev->config_ops->vhost_dev_config_notifier) {
  716. ret = dev->config_ops->vhost_dev_config_notifier(dev);
  717. }
  718. return ret;
  719. }
  720. static void slave_read(void *opaque)
  721. {
  722. struct vhost_dev *dev = opaque;
  723. struct vhost_user *u = dev->opaque;
  724. VhostUserHeader hdr = { 0, };
  725. VhostUserPayload payload = { 0, };
  726. int size, ret = 0;
  727. struct iovec iov;
  728. struct msghdr msgh;
  729. int fd[VHOST_USER_SLAVE_MAX_FDS];
  730. char control[CMSG_SPACE(sizeof(fd))];
  731. struct cmsghdr *cmsg;
  732. int i, fdsize = 0;
  733. memset(&msgh, 0, sizeof(msgh));
  734. msgh.msg_iov = &iov;
  735. msgh.msg_iovlen = 1;
  736. msgh.msg_control = control;
  737. msgh.msg_controllen = sizeof(control);
  738. memset(fd, -1, sizeof(fd));
  739. /* Read header */
  740. iov.iov_base = &hdr;
  741. iov.iov_len = VHOST_USER_HDR_SIZE;
  742. size = recvmsg(u->slave_fd, &msgh, 0);
  743. if (size != VHOST_USER_HDR_SIZE) {
  744. error_report("Failed to read from slave.");
  745. goto err;
  746. }
  747. if (msgh.msg_flags & MSG_CTRUNC) {
  748. error_report("Truncated message.");
  749. goto err;
  750. }
  751. for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
  752. cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
  753. if (cmsg->cmsg_level == SOL_SOCKET &&
  754. cmsg->cmsg_type == SCM_RIGHTS) {
  755. fdsize = cmsg->cmsg_len - CMSG_LEN(0);
  756. memcpy(fd, CMSG_DATA(cmsg), fdsize);
  757. break;
  758. }
  759. }
  760. if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
  761. error_report("Failed to read msg header."
  762. " Size %d exceeds the maximum %zu.", hdr.size,
  763. VHOST_USER_PAYLOAD_SIZE);
  764. goto err;
  765. }
  766. /* Read payload */
  767. size = read(u->slave_fd, &payload, hdr.size);
  768. if (size != hdr.size) {
  769. error_report("Failed to read payload from slave.");
  770. goto err;
  771. }
  772. switch (hdr.request) {
  773. case VHOST_USER_SLAVE_IOTLB_MSG:
  774. ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb);
  775. break;
  776. case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG :
  777. ret = vhost_user_slave_handle_config_change(dev);
  778. break;
  779. default:
  780. error_report("Received unexpected msg type.");
  781. ret = -EINVAL;
  782. }
  783. /* Close the remaining file descriptors. */
  784. for (i = 0; i < fdsize; i++) {
  785. if (fd[i] != -1) {
  786. close(fd[i]);
  787. }
  788. }
  789. /*
  790. * REPLY_ACK feature handling. Other reply types has to be managed
  791. * directly in their request handlers.
  792. */
  793. if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
  794. struct iovec iovec[2];
  795. hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
  796. hdr.flags |= VHOST_USER_REPLY_MASK;
  797. payload.u64 = !!ret;
  798. hdr.size = sizeof(payload.u64);
  799. iovec[0].iov_base = &hdr;
  800. iovec[0].iov_len = VHOST_USER_HDR_SIZE;
  801. iovec[1].iov_base = &payload;
  802. iovec[1].iov_len = hdr.size;
  803. size = writev(u->slave_fd, iovec, ARRAY_SIZE(iovec));
  804. if (size != VHOST_USER_HDR_SIZE + hdr.size) {
  805. error_report("Failed to send msg reply to slave.");
  806. goto err;
  807. }
  808. }
  809. return;
  810. err:
  811. qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
  812. close(u->slave_fd);
  813. u->slave_fd = -1;
  814. for (i = 0; i < fdsize; i++) {
  815. if (fd[i] != -1) {
  816. close(fd[i]);
  817. }
  818. }
  819. return;
  820. }
  821. static int vhost_setup_slave_channel(struct vhost_dev *dev)
  822. {
  823. VhostUserMsg msg = {
  824. .hdr.request = VHOST_USER_SET_SLAVE_REQ_FD,
  825. .hdr.flags = VHOST_USER_VERSION,
  826. };
  827. struct vhost_user *u = dev->opaque;
  828. int sv[2], ret = 0;
  829. bool reply_supported = virtio_has_feature(dev->protocol_features,
  830. VHOST_USER_PROTOCOL_F_REPLY_ACK);
  831. if (!virtio_has_feature(dev->protocol_features,
  832. VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
  833. return 0;
  834. }
  835. if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
  836. error_report("socketpair() failed");
  837. return -1;
  838. }
  839. u->slave_fd = sv[0];
  840. qemu_set_fd_handler(u->slave_fd, slave_read, NULL, dev);
  841. if (reply_supported) {
  842. msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
  843. }
  844. ret = vhost_user_write(dev, &msg, &sv[1], 1);
  845. if (ret) {
  846. goto out;
  847. }
  848. if (reply_supported) {
  849. ret = process_message_reply(dev, &msg);
  850. }
  851. out:
  852. close(sv[1]);
  853. if (ret) {
  854. qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
  855. close(u->slave_fd);
  856. u->slave_fd = -1;
  857. }
  858. return ret;
  859. }
  860. /*
  861. * Called back from the postcopy fault thread when a fault is received on our
  862. * ufd.
  863. * TODO: This is Linux specific
  864. */
  865. static int vhost_user_postcopy_fault_handler(struct PostCopyFD *pcfd,
  866. void *ufd)
  867. {
  868. struct vhost_dev *dev = pcfd->data;
  869. struct vhost_user *u = dev->opaque;
  870. struct uffd_msg *msg = ufd;
  871. uint64_t faultaddr = msg->arg.pagefault.address;
  872. RAMBlock *rb = NULL;
  873. uint64_t rb_offset;
  874. int i;
  875. trace_vhost_user_postcopy_fault_handler(pcfd->idstr, faultaddr,
  876. dev->mem->nregions);
  877. for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
  878. trace_vhost_user_postcopy_fault_handler_loop(i,
  879. u->postcopy_client_bases[i], dev->mem->regions[i].memory_size);
  880. if (faultaddr >= u->postcopy_client_bases[i]) {
  881. /* Ofset of the fault address in the vhost region */
  882. uint64_t region_offset = faultaddr - u->postcopy_client_bases[i];
  883. if (region_offset < dev->mem->regions[i].memory_size) {
  884. rb_offset = region_offset + u->region_rb_offset[i];
  885. trace_vhost_user_postcopy_fault_handler_found(i,
  886. region_offset, rb_offset);
  887. rb = u->region_rb[i];
  888. return postcopy_request_shared_page(pcfd, rb, faultaddr,
  889. rb_offset);
  890. }
  891. }
  892. }
  893. error_report("%s: Failed to find region for fault %" PRIx64,
  894. __func__, faultaddr);
  895. return -1;
  896. }
  897. static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb,
  898. uint64_t offset)
  899. {
  900. struct vhost_dev *dev = pcfd->data;
  901. struct vhost_user *u = dev->opaque;
  902. int i;
  903. trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb), offset);
  904. if (!u) {
  905. return 0;
  906. }
  907. /* Translate the offset into an address in the clients address space */
  908. for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
  909. if (u->region_rb[i] == rb &&
  910. offset >= u->region_rb_offset[i] &&
  911. offset < (u->region_rb_offset[i] +
  912. dev->mem->regions[i].memory_size)) {
  913. uint64_t client_addr = (offset - u->region_rb_offset[i]) +
  914. u->postcopy_client_bases[i];
  915. trace_vhost_user_postcopy_waker_found(client_addr);
  916. return postcopy_wake_shared(pcfd, client_addr, rb);
  917. }
  918. }
  919. trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb), offset);
  920. return 0;
  921. }
  922. /*
  923. * Called at the start of an inbound postcopy on reception of the
  924. * 'advise' command.
  925. */
  926. static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp)
  927. {
  928. struct vhost_user *u = dev->opaque;
  929. CharBackend *chr = u->user->chr;
  930. int ufd;
  931. VhostUserMsg msg = {
  932. .hdr.request = VHOST_USER_POSTCOPY_ADVISE,
  933. .hdr.flags = VHOST_USER_VERSION,
  934. };
  935. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  936. error_setg(errp, "Failed to send postcopy_advise to vhost");
  937. return -1;
  938. }
  939. if (vhost_user_read(dev, &msg) < 0) {
  940. error_setg(errp, "Failed to get postcopy_advise reply from vhost");
  941. return -1;
  942. }
  943. if (msg.hdr.request != VHOST_USER_POSTCOPY_ADVISE) {
  944. error_setg(errp, "Unexpected msg type. Expected %d received %d",
  945. VHOST_USER_POSTCOPY_ADVISE, msg.hdr.request);
  946. return -1;
  947. }
  948. if (msg.hdr.size) {
  949. error_setg(errp, "Received bad msg size.");
  950. return -1;
  951. }
  952. ufd = qemu_chr_fe_get_msgfd(chr);
  953. if (ufd < 0) {
  954. error_setg(errp, "%s: Failed to get ufd", __func__);
  955. return -1;
  956. }
  957. qemu_set_nonblock(ufd);
  958. /* register ufd with userfault thread */
  959. u->postcopy_fd.fd = ufd;
  960. u->postcopy_fd.data = dev;
  961. u->postcopy_fd.handler = vhost_user_postcopy_fault_handler;
  962. u->postcopy_fd.waker = vhost_user_postcopy_waker;
  963. u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */
  964. postcopy_register_shared_ufd(&u->postcopy_fd);
  965. return 0;
  966. }
  967. /*
  968. * Called at the switch to postcopy on reception of the 'listen' command.
  969. */
  970. static int vhost_user_postcopy_listen(struct vhost_dev *dev, Error **errp)
  971. {
  972. struct vhost_user *u = dev->opaque;
  973. int ret;
  974. VhostUserMsg msg = {
  975. .hdr.request = VHOST_USER_POSTCOPY_LISTEN,
  976. .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
  977. };
  978. u->postcopy_listen = true;
  979. trace_vhost_user_postcopy_listen();
  980. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  981. error_setg(errp, "Failed to send postcopy_listen to vhost");
  982. return -1;
  983. }
  984. ret = process_message_reply(dev, &msg);
  985. if (ret) {
  986. error_setg(errp, "Failed to receive reply to postcopy_listen");
  987. return ret;
  988. }
  989. return 0;
  990. }
  991. /*
  992. * Called at the end of postcopy
  993. */
  994. static int vhost_user_postcopy_end(struct vhost_dev *dev, Error **errp)
  995. {
  996. VhostUserMsg msg = {
  997. .hdr.request = VHOST_USER_POSTCOPY_END,
  998. .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
  999. };
  1000. int ret;
  1001. struct vhost_user *u = dev->opaque;
  1002. trace_vhost_user_postcopy_end_entry();
  1003. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1004. error_setg(errp, "Failed to send postcopy_end to vhost");
  1005. return -1;
  1006. }
  1007. ret = process_message_reply(dev, &msg);
  1008. if (ret) {
  1009. error_setg(errp, "Failed to receive reply to postcopy_end");
  1010. return ret;
  1011. }
  1012. postcopy_unregister_shared_ufd(&u->postcopy_fd);
  1013. u->postcopy_fd.handler = NULL;
  1014. trace_vhost_user_postcopy_end_exit();
  1015. return 0;
  1016. }
  1017. static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier,
  1018. void *opaque)
  1019. {
  1020. struct PostcopyNotifyData *pnd = opaque;
  1021. struct vhost_user *u = container_of(notifier, struct vhost_user,
  1022. postcopy_notifier);
  1023. struct vhost_dev *dev = u->dev;
  1024. switch (pnd->reason) {
  1025. case POSTCOPY_NOTIFY_PROBE:
  1026. if (!virtio_has_feature(dev->protocol_features,
  1027. VHOST_USER_PROTOCOL_F_PAGEFAULT)) {
  1028. /* TODO: Get the device name into this error somehow */
  1029. error_setg(pnd->errp,
  1030. "vhost-user backend not capable of postcopy");
  1031. return -ENOENT;
  1032. }
  1033. break;
  1034. case POSTCOPY_NOTIFY_INBOUND_ADVISE:
  1035. return vhost_user_postcopy_advise(dev, pnd->errp);
  1036. case POSTCOPY_NOTIFY_INBOUND_LISTEN:
  1037. return vhost_user_postcopy_listen(dev, pnd->errp);
  1038. case POSTCOPY_NOTIFY_INBOUND_END:
  1039. return vhost_user_postcopy_end(dev, pnd->errp);
  1040. default:
  1041. /* We ignore notifications we don't know */
  1042. break;
  1043. }
  1044. return 0;
  1045. }
  1046. static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)
  1047. {
  1048. uint64_t features, protocol_features;
  1049. struct vhost_user *u;
  1050. int err;
  1051. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  1052. u = g_new0(struct vhost_user, 1);
  1053. u->user = opaque;
  1054. u->slave_fd = -1;
  1055. u->dev = dev;
  1056. dev->opaque = u;
  1057. err = vhost_user_get_features(dev, &features);
  1058. if (err < 0) {
  1059. return err;
  1060. }
  1061. if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) {
  1062. dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
  1063. err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES,
  1064. &protocol_features);
  1065. if (err < 0) {
  1066. return err;
  1067. }
  1068. dev->protocol_features =
  1069. protocol_features & VHOST_USER_PROTOCOL_FEATURE_MASK;
  1070. if (!dev->config_ops || !dev->config_ops->vhost_dev_config_notifier) {
  1071. /* Don't acknowledge CONFIG feature if device doesn't support it */
  1072. dev->protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
  1073. } else if (!(protocol_features &
  1074. (1ULL << VHOST_USER_PROTOCOL_F_CONFIG))) {
  1075. error_report("Device expects VHOST_USER_PROTOCOL_F_CONFIG "
  1076. "but backend does not support it.");
  1077. return -1;
  1078. }
  1079. err = vhost_user_set_protocol_features(dev, dev->protocol_features);
  1080. if (err < 0) {
  1081. return err;
  1082. }
  1083. /* query the max queues we support if backend supports Multiple Queue */
  1084. if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
  1085. err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
  1086. &dev->max_queues);
  1087. if (err < 0) {
  1088. return err;
  1089. }
  1090. }
  1091. if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) &&
  1092. !(virtio_has_feature(dev->protocol_features,
  1093. VHOST_USER_PROTOCOL_F_SLAVE_REQ) &&
  1094. virtio_has_feature(dev->protocol_features,
  1095. VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
  1096. error_report("IOMMU support requires reply-ack and "
  1097. "slave-req protocol features.");
  1098. return -1;
  1099. }
  1100. }
  1101. if (dev->migration_blocker == NULL &&
  1102. !virtio_has_feature(dev->protocol_features,
  1103. VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
  1104. error_setg(&dev->migration_blocker,
  1105. "Migration disabled: vhost-user backend lacks "
  1106. "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
  1107. }
  1108. err = vhost_setup_slave_channel(dev);
  1109. if (err < 0) {
  1110. return err;
  1111. }
  1112. u->postcopy_notifier.notify = vhost_user_postcopy_notifier;
  1113. postcopy_add_notifier(&u->postcopy_notifier);
  1114. return 0;
  1115. }
  1116. static int vhost_user_backend_cleanup(struct vhost_dev *dev)
  1117. {
  1118. struct vhost_user *u;
  1119. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  1120. u = dev->opaque;
  1121. if (u->postcopy_notifier.notify) {
  1122. postcopy_remove_notifier(&u->postcopy_notifier);
  1123. u->postcopy_notifier.notify = NULL;
  1124. }
  1125. if (u->slave_fd >= 0) {
  1126. qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
  1127. close(u->slave_fd);
  1128. u->slave_fd = -1;
  1129. }
  1130. g_free(u->region_rb);
  1131. u->region_rb = NULL;
  1132. g_free(u->region_rb_offset);
  1133. u->region_rb_offset = NULL;
  1134. u->region_rb_len = 0;
  1135. g_free(u);
  1136. dev->opaque = 0;
  1137. return 0;
  1138. }
  1139. static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
  1140. {
  1141. assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
  1142. return idx;
  1143. }
  1144. static int vhost_user_memslots_limit(struct vhost_dev *dev)
  1145. {
  1146. return VHOST_MEMORY_MAX_NREGIONS;
  1147. }
  1148. static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
  1149. {
  1150. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  1151. return virtio_has_feature(dev->protocol_features,
  1152. VHOST_USER_PROTOCOL_F_LOG_SHMFD);
  1153. }
  1154. static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
  1155. {
  1156. VhostUserMsg msg = { };
  1157. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  1158. /* If guest supports GUEST_ANNOUNCE do nothing */
  1159. if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) {
  1160. return 0;
  1161. }
  1162. /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
  1163. if (virtio_has_feature(dev->protocol_features,
  1164. VHOST_USER_PROTOCOL_F_RARP)) {
  1165. msg.hdr.request = VHOST_USER_SEND_RARP;
  1166. msg.hdr.flags = VHOST_USER_VERSION;
  1167. memcpy((char *)&msg.payload.u64, mac_addr, 6);
  1168. msg.hdr.size = sizeof(msg.payload.u64);
  1169. return vhost_user_write(dev, &msg, NULL, 0);
  1170. }
  1171. return -1;
  1172. }
  1173. static bool vhost_user_can_merge(struct vhost_dev *dev,
  1174. uint64_t start1, uint64_t size1,
  1175. uint64_t start2, uint64_t size2)
  1176. {
  1177. ram_addr_t offset;
  1178. int mfd, rfd;
  1179. MemoryRegion *mr;
  1180. mr = memory_region_from_host((void *)(uintptr_t)start1, &offset);
  1181. mfd = memory_region_get_fd(mr);
  1182. mr = memory_region_from_host((void *)(uintptr_t)start2, &offset);
  1183. rfd = memory_region_get_fd(mr);
  1184. return mfd == rfd;
  1185. }
  1186. static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu)
  1187. {
  1188. VhostUserMsg msg;
  1189. bool reply_supported = virtio_has_feature(dev->protocol_features,
  1190. VHOST_USER_PROTOCOL_F_REPLY_ACK);
  1191. if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) {
  1192. return 0;
  1193. }
  1194. msg.hdr.request = VHOST_USER_NET_SET_MTU;
  1195. msg.payload.u64 = mtu;
  1196. msg.hdr.size = sizeof(msg.payload.u64);
  1197. msg.hdr.flags = VHOST_USER_VERSION;
  1198. if (reply_supported) {
  1199. msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
  1200. }
  1201. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1202. return -1;
  1203. }
  1204. /* If reply_ack supported, slave has to ack specified MTU is valid */
  1205. if (reply_supported) {
  1206. return process_message_reply(dev, &msg);
  1207. }
  1208. return 0;
  1209. }
  1210. static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev,
  1211. struct vhost_iotlb_msg *imsg)
  1212. {
  1213. VhostUserMsg msg = {
  1214. .hdr.request = VHOST_USER_IOTLB_MSG,
  1215. .hdr.size = sizeof(msg.payload.iotlb),
  1216. .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
  1217. .payload.iotlb = *imsg,
  1218. };
  1219. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1220. return -EFAULT;
  1221. }
  1222. return process_message_reply(dev, &msg);
  1223. }
  1224. static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled)
  1225. {
  1226. /* No-op as the receive channel is not dedicated to IOTLB messages. */
  1227. }
  1228. static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config,
  1229. uint32_t config_len)
  1230. {
  1231. VhostUserMsg msg = {
  1232. .hdr.request = VHOST_USER_GET_CONFIG,
  1233. .hdr.flags = VHOST_USER_VERSION,
  1234. .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len,
  1235. };
  1236. if (!virtio_has_feature(dev->protocol_features,
  1237. VHOST_USER_PROTOCOL_F_CONFIG)) {
  1238. return -1;
  1239. }
  1240. if (config_len > VHOST_USER_MAX_CONFIG_SIZE) {
  1241. return -1;
  1242. }
  1243. msg.payload.config.offset = 0;
  1244. msg.payload.config.size = config_len;
  1245. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1246. return -1;
  1247. }
  1248. if (vhost_user_read(dev, &msg) < 0) {
  1249. return -1;
  1250. }
  1251. if (msg.hdr.request != VHOST_USER_GET_CONFIG) {
  1252. error_report("Received unexpected msg type. Expected %d received %d",
  1253. VHOST_USER_GET_CONFIG, msg.hdr.request);
  1254. return -1;
  1255. }
  1256. if (msg.hdr.size != VHOST_USER_CONFIG_HDR_SIZE + config_len) {
  1257. error_report("Received bad msg size.");
  1258. return -1;
  1259. }
  1260. memcpy(config, msg.payload.config.region, config_len);
  1261. return 0;
  1262. }
  1263. static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data,
  1264. uint32_t offset, uint32_t size, uint32_t flags)
  1265. {
  1266. uint8_t *p;
  1267. bool reply_supported = virtio_has_feature(dev->protocol_features,
  1268. VHOST_USER_PROTOCOL_F_REPLY_ACK);
  1269. VhostUserMsg msg = {
  1270. .hdr.request = VHOST_USER_SET_CONFIG,
  1271. .hdr.flags = VHOST_USER_VERSION,
  1272. .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size,
  1273. };
  1274. if (!virtio_has_feature(dev->protocol_features,
  1275. VHOST_USER_PROTOCOL_F_CONFIG)) {
  1276. return -1;
  1277. }
  1278. if (reply_supported) {
  1279. msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
  1280. }
  1281. if (size > VHOST_USER_MAX_CONFIG_SIZE) {
  1282. return -1;
  1283. }
  1284. msg.payload.config.offset = offset,
  1285. msg.payload.config.size = size,
  1286. msg.payload.config.flags = flags,
  1287. p = msg.payload.config.region;
  1288. memcpy(p, data, size);
  1289. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1290. return -1;
  1291. }
  1292. if (reply_supported) {
  1293. return process_message_reply(dev, &msg);
  1294. }
  1295. return 0;
  1296. }
  1297. static int vhost_user_crypto_create_session(struct vhost_dev *dev,
  1298. void *session_info,
  1299. uint64_t *session_id)
  1300. {
  1301. bool crypto_session = virtio_has_feature(dev->protocol_features,
  1302. VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
  1303. CryptoDevBackendSymSessionInfo *sess_info = session_info;
  1304. VhostUserMsg msg = {
  1305. .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION,
  1306. .hdr.flags = VHOST_USER_VERSION,
  1307. .hdr.size = sizeof(msg.payload.session),
  1308. };
  1309. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
  1310. if (!crypto_session) {
  1311. error_report("vhost-user trying to send unhandled ioctl");
  1312. return -1;
  1313. }
  1314. memcpy(&msg.payload.session.session_setup_data, sess_info,
  1315. sizeof(CryptoDevBackendSymSessionInfo));
  1316. if (sess_info->key_len) {
  1317. memcpy(&msg.payload.session.key, sess_info->cipher_key,
  1318. sess_info->key_len);
  1319. }
  1320. if (sess_info->auth_key_len > 0) {
  1321. memcpy(&msg.payload.session.auth_key, sess_info->auth_key,
  1322. sess_info->auth_key_len);
  1323. }
  1324. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1325. error_report("vhost_user_write() return -1, create session failed");
  1326. return -1;
  1327. }
  1328. if (vhost_user_read(dev, &msg) < 0) {
  1329. error_report("vhost_user_read() return -1, create session failed");
  1330. return -1;
  1331. }
  1332. if (msg.hdr.request != VHOST_USER_CREATE_CRYPTO_SESSION) {
  1333. error_report("Received unexpected msg type. Expected %d received %d",
  1334. VHOST_USER_CREATE_CRYPTO_SESSION, msg.hdr.request);
  1335. return -1;
  1336. }
  1337. if (msg.hdr.size != sizeof(msg.payload.session)) {
  1338. error_report("Received bad msg size.");
  1339. return -1;
  1340. }
  1341. if (msg.payload.session.session_id < 0) {
  1342. error_report("Bad session id: %" PRId64 "",
  1343. msg.payload.session.session_id);
  1344. return -1;
  1345. }
  1346. *session_id = msg.payload.session.session_id;
  1347. return 0;
  1348. }
  1349. static int
  1350. vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
  1351. {
  1352. bool crypto_session = virtio_has_feature(dev->protocol_features,
  1353. VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
  1354. VhostUserMsg msg = {
  1355. .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION,
  1356. .hdr.flags = VHOST_USER_VERSION,
  1357. .hdr.size = sizeof(msg.payload.u64),
  1358. };
  1359. msg.payload.u64 = session_id;
  1360. if (!crypto_session) {
  1361. error_report("vhost-user trying to send unhandled ioctl");
  1362. return -1;
  1363. }
  1364. if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
  1365. error_report("vhost_user_write() return -1, close session failed");
  1366. return -1;
  1367. }
  1368. return 0;
  1369. }
  1370. static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
  1371. MemoryRegionSection *section)
  1372. {
  1373. bool result;
  1374. result = memory_region_get_fd(section->mr) >= 0;
  1375. return result;
  1376. }
  1377. VhostUserState *vhost_user_init(void)
  1378. {
  1379. VhostUserState *user = g_new0(struct VhostUserState, 1);
  1380. return user;
  1381. }
  1382. void vhost_user_cleanup(VhostUserState *user)
  1383. {
  1384. }
  1385. const VhostOps user_ops = {
  1386. .backend_type = VHOST_BACKEND_TYPE_USER,
  1387. .vhost_backend_init = vhost_user_backend_init,
  1388. .vhost_backend_cleanup = vhost_user_backend_cleanup,
  1389. .vhost_backend_memslots_limit = vhost_user_memslots_limit,
  1390. .vhost_set_log_base = vhost_user_set_log_base,
  1391. .vhost_set_mem_table = vhost_user_set_mem_table,
  1392. .vhost_set_vring_addr = vhost_user_set_vring_addr,
  1393. .vhost_set_vring_endian = vhost_user_set_vring_endian,
  1394. .vhost_set_vring_num = vhost_user_set_vring_num,
  1395. .vhost_set_vring_base = vhost_user_set_vring_base,
  1396. .vhost_get_vring_base = vhost_user_get_vring_base,
  1397. .vhost_set_vring_kick = vhost_user_set_vring_kick,
  1398. .vhost_set_vring_call = vhost_user_set_vring_call,
  1399. .vhost_set_features = vhost_user_set_features,
  1400. .vhost_get_features = vhost_user_get_features,
  1401. .vhost_set_owner = vhost_user_set_owner,
  1402. .vhost_reset_device = vhost_user_reset_device,
  1403. .vhost_get_vq_index = vhost_user_get_vq_index,
  1404. .vhost_set_vring_enable = vhost_user_set_vring_enable,
  1405. .vhost_requires_shm_log = vhost_user_requires_shm_log,
  1406. .vhost_migration_done = vhost_user_migration_done,
  1407. .vhost_backend_can_merge = vhost_user_can_merge,
  1408. .vhost_net_set_mtu = vhost_user_net_set_mtu,
  1409. .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback,
  1410. .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
  1411. .vhost_get_config = vhost_user_get_config,
  1412. .vhost_set_config = vhost_user_set_config,
  1413. .vhost_crypto_create_session = vhost_user_crypto_create_session,
  1414. .vhost_crypto_close_session = vhost_user_crypto_close_session,
  1415. .vhost_backend_mem_section_filter = vhost_user_mem_section_filter,
  1416. };