hv_fcopy_uio_daemon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * An implementation of host to guest copy functionality for Linux.
  4. *
  5. * Copyright (C) 2023, Microsoft, Inc.
  6. *
  7. * Author : K. Y. Srinivasan <kys@microsoft.com>
  8. * Author : Saurabh Sengar <ssengar@microsoft.com>
  9. *
  10. */
  11. #include <dirent.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <getopt.h>
  15. #include <locale.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <syslog.h>
  23. #include <unistd.h>
  24. #include <wchar.h>
  25. #include <sys/stat.h>
  26. #include <linux/hyperv.h>
  27. #include <linux/limits.h>
  28. #include "vmbus_bufring.h"
  29. #define ICMSGTYPE_NEGOTIATE 0
  30. #define ICMSGTYPE_FCOPY 7
  31. #define WIN8_SRV_MAJOR 1
  32. #define WIN8_SRV_MINOR 1
  33. #define WIN8_SRV_VERSION (WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
  34. #define FCOPY_DEVICE_PATH(subdir) \
  35. "/sys/bus/vmbus/devices/eb765408-105f-49b6-b4aa-c123b64d17d4/" #subdir
  36. #define FCOPY_UIO_PATH FCOPY_DEVICE_PATH(uio)
  37. #define FCOPY_CHANNELS_PATH FCOPY_DEVICE_PATH(channels)
  38. #define FCOPY_VER_COUNT 1
  39. static const int fcopy_versions[] = {
  40. WIN8_SRV_VERSION
  41. };
  42. #define FW_VER_COUNT 1
  43. static const int fw_versions[] = {
  44. UTIL_FW_VERSION
  45. };
  46. static uint32_t get_ring_buffer_size(void)
  47. {
  48. char ring_path[PATH_MAX];
  49. DIR *dir;
  50. struct dirent *entry;
  51. struct stat st;
  52. uint32_t ring_size = 0;
  53. int retry_count = 0;
  54. /* Find the channel directory */
  55. dir = opendir(FCOPY_CHANNELS_PATH);
  56. if (!dir) {
  57. usleep(100 * 1000); /* Avoid race with kernel, wait 100ms and retry once */
  58. dir = opendir(FCOPY_CHANNELS_PATH);
  59. if (!dir) {
  60. syslog(LOG_ERR, "Failed to open channels directory: %s", strerror(errno));
  61. return 0;
  62. }
  63. }
  64. retry_once:
  65. while ((entry = readdir(dir)) != NULL) {
  66. if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 &&
  67. strcmp(entry->d_name, "..") != 0) {
  68. snprintf(ring_path, sizeof(ring_path), "%s/%s/ring",
  69. FCOPY_CHANNELS_PATH, entry->d_name);
  70. if (stat(ring_path, &st) == 0) {
  71. /*
  72. * stat returns size of Tx, Rx rings combined,
  73. * so take half of it for individual ring size.
  74. */
  75. ring_size = (uint32_t)st.st_size / 2;
  76. syslog(LOG_INFO, "Ring buffer size from %s: %u bytes",
  77. ring_path, ring_size);
  78. break;
  79. }
  80. }
  81. }
  82. if (!ring_size && retry_count == 0) {
  83. retry_count = 1;
  84. rewinddir(dir);
  85. usleep(100 * 1000); /* Wait 100ms and retry once */
  86. goto retry_once;
  87. }
  88. closedir(dir);
  89. if (!ring_size)
  90. syslog(LOG_ERR, "Could not determine ring size");
  91. return ring_size;
  92. }
  93. static unsigned char *desc;
  94. static int target_fd;
  95. static char target_fname[PATH_MAX];
  96. static unsigned long long filesize;
  97. static int hv_fcopy_create_file(char *file_name, char *path_name, __u32 flags)
  98. {
  99. int error = HV_E_FAIL;
  100. char *q, *p;
  101. filesize = 0;
  102. p = path_name;
  103. if (snprintf(target_fname, sizeof(target_fname), "%s/%s",
  104. path_name, file_name) >= sizeof(target_fname)) {
  105. syslog(LOG_ERR, "target file name is too long: %s/%s", path_name, file_name);
  106. goto done;
  107. }
  108. /*
  109. * Check to see if the path is already in place; if not,
  110. * create if required.
  111. */
  112. while ((q = strchr(p, '/')) != NULL) {
  113. if (q == p) {
  114. p++;
  115. continue;
  116. }
  117. *q = '\0';
  118. if (access(path_name, F_OK)) {
  119. if (flags & CREATE_PATH) {
  120. if (mkdir(path_name, 0755)) {
  121. syslog(LOG_ERR, "Failed to create %s",
  122. path_name);
  123. goto done;
  124. }
  125. } else {
  126. syslog(LOG_ERR, "Invalid path: %s", path_name);
  127. goto done;
  128. }
  129. }
  130. p = q + 1;
  131. *q = '/';
  132. }
  133. if (!access(target_fname, F_OK)) {
  134. syslog(LOG_INFO, "File: %s exists", target_fname);
  135. if (!(flags & OVER_WRITE)) {
  136. error = HV_ERROR_ALREADY_EXISTS;
  137. goto done;
  138. }
  139. }
  140. target_fd = open(target_fname,
  141. O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0744);
  142. if (target_fd == -1) {
  143. syslog(LOG_INFO, "Open Failed: %s", strerror(errno));
  144. goto done;
  145. }
  146. error = 0;
  147. done:
  148. if (error)
  149. target_fname[0] = '\0';
  150. return error;
  151. }
  152. /* copy the data into the file */
  153. static int hv_copy_data(struct hv_do_fcopy *cpmsg)
  154. {
  155. ssize_t len;
  156. int ret = 0;
  157. len = pwrite(target_fd, cpmsg->data, cpmsg->size, cpmsg->offset);
  158. filesize += cpmsg->size;
  159. if (len != cpmsg->size) {
  160. switch (errno) {
  161. case ENOSPC:
  162. ret = HV_ERROR_DISK_FULL;
  163. break;
  164. default:
  165. ret = HV_E_FAIL;
  166. break;
  167. }
  168. syslog(LOG_ERR, "pwrite failed to write %llu bytes: %ld (%s)",
  169. filesize, (long)len, strerror(errno));
  170. }
  171. return ret;
  172. }
  173. static int hv_copy_finished(void)
  174. {
  175. close(target_fd);
  176. target_fname[0] = '\0';
  177. return 0;
  178. }
  179. static void print_usage(char *argv[])
  180. {
  181. fprintf(stderr, "Usage: %s [options]\n"
  182. "Options are:\n"
  183. " -n, --no-daemon stay in foreground, don't daemonize\n"
  184. " -h, --help print this help\n", argv[0]);
  185. }
  186. static bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, unsigned char *buf,
  187. unsigned int buflen, const int *fw_version, int fw_vercnt,
  188. const int *srv_version, int srv_vercnt,
  189. int *nego_fw_version, int *nego_srv_version)
  190. {
  191. int icframe_major, icframe_minor;
  192. int icmsg_major, icmsg_minor;
  193. int fw_major, fw_minor;
  194. int srv_major, srv_minor;
  195. int i, j;
  196. bool found_match = false;
  197. struct icmsg_negotiate *negop;
  198. /* Check that there's enough space for icframe_vercnt, icmsg_vercnt */
  199. if (buflen < ICMSG_HDR + offsetof(struct icmsg_negotiate, reserved)) {
  200. syslog(LOG_ERR, "Invalid icmsg negotiate");
  201. return false;
  202. }
  203. icmsghdrp->icmsgsize = 0x10;
  204. negop = (struct icmsg_negotiate *)&buf[ICMSG_HDR];
  205. icframe_major = negop->icframe_vercnt;
  206. icframe_minor = 0;
  207. icmsg_major = negop->icmsg_vercnt;
  208. icmsg_minor = 0;
  209. /* Validate negop packet */
  210. if (icframe_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT ||
  211. icmsg_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT ||
  212. ICMSG_NEGOTIATE_PKT_SIZE(icframe_major, icmsg_major) > buflen) {
  213. syslog(LOG_ERR, "Invalid icmsg negotiate - icframe_major: %u, icmsg_major: %u\n",
  214. icframe_major, icmsg_major);
  215. goto fw_error;
  216. }
  217. /*
  218. * Select the framework version number we will
  219. * support.
  220. */
  221. for (i = 0; i < fw_vercnt; i++) {
  222. fw_major = (fw_version[i] >> 16);
  223. fw_minor = (fw_version[i] & 0xFFFF);
  224. for (j = 0; j < negop->icframe_vercnt; j++) {
  225. if (negop->icversion_data[j].major == fw_major &&
  226. negop->icversion_data[j].minor == fw_minor) {
  227. icframe_major = negop->icversion_data[j].major;
  228. icframe_minor = negop->icversion_data[j].minor;
  229. found_match = true;
  230. break;
  231. }
  232. }
  233. if (found_match)
  234. break;
  235. }
  236. if (!found_match)
  237. goto fw_error;
  238. found_match = false;
  239. for (i = 0; i < srv_vercnt; i++) {
  240. srv_major = (srv_version[i] >> 16);
  241. srv_minor = (srv_version[i] & 0xFFFF);
  242. for (j = negop->icframe_vercnt;
  243. (j < negop->icframe_vercnt + negop->icmsg_vercnt);
  244. j++) {
  245. if (negop->icversion_data[j].major == srv_major &&
  246. negop->icversion_data[j].minor == srv_minor) {
  247. icmsg_major = negop->icversion_data[j].major;
  248. icmsg_minor = negop->icversion_data[j].minor;
  249. found_match = true;
  250. break;
  251. }
  252. }
  253. if (found_match)
  254. break;
  255. }
  256. /*
  257. * Respond with the framework and service
  258. * version numbers we can support.
  259. */
  260. fw_error:
  261. if (!found_match) {
  262. negop->icframe_vercnt = 0;
  263. negop->icmsg_vercnt = 0;
  264. } else {
  265. negop->icframe_vercnt = 1;
  266. negop->icmsg_vercnt = 1;
  267. }
  268. if (nego_fw_version)
  269. *nego_fw_version = (icframe_major << 16) | icframe_minor;
  270. if (nego_srv_version)
  271. *nego_srv_version = (icmsg_major << 16) | icmsg_minor;
  272. negop->icversion_data[0].major = icframe_major;
  273. negop->icversion_data[0].minor = icframe_minor;
  274. negop->icversion_data[1].major = icmsg_major;
  275. negop->icversion_data[1].minor = icmsg_minor;
  276. return found_match;
  277. }
  278. static void wcstoutf8(char *dest, const __u16 *src, size_t dest_size)
  279. {
  280. size_t len = 0;
  281. while (len < dest_size && *src) {
  282. if (src[len] < 0x80)
  283. dest[len++] = (char)(*src++);
  284. else
  285. dest[len++] = 'X';
  286. }
  287. dest[len] = '\0';
  288. }
  289. static int hv_fcopy_start(struct hv_start_fcopy *smsg_in)
  290. {
  291. /*
  292. * file_name and path_name should have same length with appropriate
  293. * member of hv_start_fcopy.
  294. */
  295. char file_name[W_MAX_PATH], path_name[W_MAX_PATH];
  296. setlocale(LC_ALL, "en_US.utf8");
  297. wcstoutf8(file_name, smsg_in->file_name, W_MAX_PATH - 1);
  298. wcstoutf8(path_name, smsg_in->path_name, W_MAX_PATH - 1);
  299. return hv_fcopy_create_file(file_name, path_name, smsg_in->copy_flags);
  300. }
  301. static int hv_fcopy_send_data(struct hv_fcopy_hdr *fcopy_msg, int recvlen)
  302. {
  303. int operation = fcopy_msg->operation;
  304. /*
  305. * The strings sent from the host are encoded in
  306. * utf16; convert it to utf8 strings.
  307. * The host assures us that the utf16 strings will not exceed
  308. * the max lengths specified. We will however, reserve room
  309. * for the string terminating character - in the utf16s_utf8s()
  310. * function we limit the size of the buffer where the converted
  311. * string is placed to W_MAX_PATH -1 to guarantee
  312. * that the strings can be properly terminated!
  313. */
  314. switch (operation) {
  315. case START_FILE_COPY:
  316. return hv_fcopy_start((struct hv_start_fcopy *)fcopy_msg);
  317. case WRITE_TO_FILE:
  318. return hv_copy_data((struct hv_do_fcopy *)fcopy_msg);
  319. case COMPLETE_FCOPY:
  320. return hv_copy_finished();
  321. }
  322. return HV_E_FAIL;
  323. }
  324. /* process the packet recv from host */
  325. static int fcopy_pkt_process(struct vmbus_br *txbr)
  326. {
  327. int ret, offset, pktlen;
  328. int fcopy_srv_version;
  329. const struct vmbus_chanpkt_hdr *pkt;
  330. struct hv_fcopy_hdr *fcopy_msg;
  331. struct icmsg_hdr *icmsghdr;
  332. pkt = (const struct vmbus_chanpkt_hdr *)desc;
  333. offset = pkt->hlen << 3;
  334. pktlen = (pkt->tlen << 3) - offset;
  335. icmsghdr = (struct icmsg_hdr *)&desc[offset + sizeof(struct vmbuspipe_hdr)];
  336. icmsghdr->status = HV_E_FAIL;
  337. if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
  338. if (vmbus_prep_negotiate_resp(icmsghdr, desc + offset, pktlen, fw_versions,
  339. FW_VER_COUNT, fcopy_versions, FCOPY_VER_COUNT,
  340. NULL, &fcopy_srv_version)) {
  341. syslog(LOG_INFO, "FCopy IC version %d.%d",
  342. fcopy_srv_version >> 16, fcopy_srv_version & 0xFFFF);
  343. icmsghdr->status = 0;
  344. }
  345. } else if (icmsghdr->icmsgtype == ICMSGTYPE_FCOPY) {
  346. /* Ensure recvlen is big enough to contain hv_fcopy_hdr */
  347. if (pktlen < ICMSG_HDR + sizeof(struct hv_fcopy_hdr)) {
  348. syslog(LOG_ERR, "Invalid Fcopy hdr. Packet length too small: %u",
  349. pktlen);
  350. return -ENOBUFS;
  351. }
  352. fcopy_msg = (struct hv_fcopy_hdr *)&desc[offset + ICMSG_HDR];
  353. icmsghdr->status = hv_fcopy_send_data(fcopy_msg, pktlen);
  354. }
  355. icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
  356. ret = rte_vmbus_chan_send(txbr, 0x6, desc + offset, pktlen, 0);
  357. if (ret) {
  358. syslog(LOG_ERR, "Write to ringbuffer failed err: %d", ret);
  359. return ret;
  360. }
  361. return 0;
  362. }
  363. static void fcopy_get_first_folder(char *path, char *chan_no)
  364. {
  365. DIR *dir = opendir(path);
  366. struct dirent *entry;
  367. if (!dir) {
  368. syslog(LOG_ERR, "Failed to open directory (errno=%s).\n", strerror(errno));
  369. return;
  370. }
  371. while ((entry = readdir(dir)) != NULL) {
  372. if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 &&
  373. strcmp(entry->d_name, "..") != 0) {
  374. strcpy(chan_no, entry->d_name);
  375. break;
  376. }
  377. }
  378. closedir(dir);
  379. }
  380. int main(int argc, char *argv[])
  381. {
  382. int fcopy_fd = -1, tmp = 1;
  383. int daemonize = 1, long_index = 0, opt, ret = -EINVAL;
  384. struct vmbus_br txbr, rxbr;
  385. void *ring;
  386. uint32_t ring_size, len;
  387. char uio_name[NAME_MAX] = {0};
  388. char uio_dev_path[PATH_MAX] = {0};
  389. static struct option long_options[] = {
  390. {"help", no_argument, 0, 'h' },
  391. {"no-daemon", no_argument, 0, 'n' },
  392. {0, 0, 0, 0 }
  393. };
  394. while ((opt = getopt_long(argc, argv, "hn", long_options,
  395. &long_index)) != -1) {
  396. switch (opt) {
  397. case 'n':
  398. daemonize = 0;
  399. break;
  400. case 'h':
  401. default:
  402. print_usage(argv);
  403. goto exit;
  404. }
  405. }
  406. if (daemonize && daemon(1, 0)) {
  407. syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
  408. goto exit;
  409. }
  410. openlog("HV_UIO_FCOPY", 0, LOG_USER);
  411. syslog(LOG_INFO, "starting; pid is:%d", getpid());
  412. ring_size = get_ring_buffer_size();
  413. if (!ring_size) {
  414. ret = -ENODEV;
  415. goto exit;
  416. }
  417. desc = malloc(ring_size * sizeof(unsigned char));
  418. if (!desc) {
  419. syslog(LOG_ERR, "malloc failed for desc buffer");
  420. ret = -ENOMEM;
  421. goto exit;
  422. }
  423. fcopy_get_first_folder(FCOPY_UIO_PATH, uio_name);
  424. snprintf(uio_dev_path, sizeof(uio_dev_path), "/dev/%s", uio_name);
  425. fcopy_fd = open(uio_dev_path, O_RDWR);
  426. if (fcopy_fd < 0) {
  427. syslog(LOG_ERR, "open %s failed; error: %d %s",
  428. uio_dev_path, errno, strerror(errno));
  429. ret = fcopy_fd;
  430. goto free_desc;
  431. }
  432. ring = vmbus_uio_map(&fcopy_fd, ring_size);
  433. if (!ring) {
  434. ret = errno;
  435. syslog(LOG_ERR, "mmap ringbuffer failed; error: %d %s", ret, strerror(ret));
  436. goto close;
  437. }
  438. vmbus_br_setup(&txbr, ring, ring_size);
  439. vmbus_br_setup(&rxbr, (char *)ring + ring_size, ring_size);
  440. rxbr.vbr->imask = 0;
  441. while (1) {
  442. /*
  443. * In this loop we process fcopy messages after the
  444. * handshake is complete.
  445. */
  446. ret = pread(fcopy_fd, &tmp, sizeof(int), 0);
  447. if (ret < 0) {
  448. if (errno == EINTR || errno == EAGAIN)
  449. continue;
  450. syslog(LOG_ERR, "pread failed: %s", strerror(errno));
  451. goto close;
  452. }
  453. len = ring_size;
  454. ret = rte_vmbus_chan_recv_raw(&rxbr, desc, &len);
  455. if (unlikely(ret <= 0)) {
  456. /* This indicates a failure to communicate (or worse) */
  457. syslog(LOG_ERR, "VMBus channel recv error: %d", ret);
  458. } else {
  459. ret = fcopy_pkt_process(&txbr);
  460. if (ret < 0)
  461. goto close;
  462. /* Signal host */
  463. if ((write(fcopy_fd, &tmp, sizeof(int))) != sizeof(int)) {
  464. ret = errno;
  465. syslog(LOG_ERR, "Signal to host failed: %s\n", strerror(ret));
  466. goto close;
  467. }
  468. }
  469. }
  470. close:
  471. close(fcopy_fd);
  472. free_desc:
  473. free(desc);
  474. exit:
  475. return ret;
  476. }