virtio-serial-bus.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * A bus for connecting virtio serial and console ports
  3. *
  4. * Copyright (C) 2009, 2010 Red Hat, Inc.
  5. *
  6. * Author(s):
  7. * Amit Shah <amit.shah@redhat.com>
  8. *
  9. * Some earlier parts are:
  10. * Copyright IBM, Corp. 2008
  11. * authored by
  12. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. * Contributions after 2012-01-13 are licensed under the terms of the
  18. * GNU GPL, version 2 or (at your option) any later version.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/iov.h"
  23. #include "qemu/main-loop.h"
  24. #include "qemu/module.h"
  25. #include "migration/qemu-file-types.h"
  26. #include "monitor/monitor.h"
  27. #include "qemu/error-report.h"
  28. #include "qemu/queue.h"
  29. #include "hw/qdev-properties.h"
  30. #include "trace.h"
  31. #include "hw/virtio/virtio-serial.h"
  32. #include "hw/virtio/virtio-access.h"
  33. static struct VirtIOSerialDevices {
  34. QLIST_HEAD(, VirtIOSerial) devices;
  35. } vserdevices;
  36. static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
  37. {
  38. VirtIOSerialPort *port;
  39. if (id == VIRTIO_CONSOLE_BAD_ID) {
  40. return NULL;
  41. }
  42. QTAILQ_FOREACH(port, &vser->ports, next) {
  43. if (port->id == id)
  44. return port;
  45. }
  46. return NULL;
  47. }
  48. static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
  49. {
  50. VirtIOSerialPort *port;
  51. QTAILQ_FOREACH(port, &vser->ports, next) {
  52. if (port->ivq == vq || port->ovq == vq)
  53. return port;
  54. }
  55. return NULL;
  56. }
  57. static VirtIOSerialPort *find_port_by_name(char *name)
  58. {
  59. VirtIOSerial *vser;
  60. QLIST_FOREACH(vser, &vserdevices.devices, next) {
  61. VirtIOSerialPort *port;
  62. QTAILQ_FOREACH(port, &vser->ports, next) {
  63. if (port->name && !strcmp(port->name, name)) {
  64. return port;
  65. }
  66. }
  67. }
  68. return NULL;
  69. }
  70. static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
  71. {
  72. VirtIOSerialPort *port;
  73. QTAILQ_FOREACH(port, &vser->ports, next) {
  74. VirtIOSerialPortClass const *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  75. if (vsc->is_console && port->host_connected) {
  76. return port;
  77. }
  78. }
  79. return NULL;
  80. }
  81. static bool use_multiport(VirtIOSerial *vser)
  82. {
  83. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  84. return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
  85. }
  86. static size_t write_to_port(VirtIOSerialPort *port,
  87. const uint8_t *buf, size_t size)
  88. {
  89. VirtQueueElement *elem;
  90. VirtQueue *vq;
  91. size_t offset;
  92. vq = port->ivq;
  93. if (!virtio_queue_ready(vq)) {
  94. return 0;
  95. }
  96. offset = 0;
  97. while (offset < size) {
  98. size_t len;
  99. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  100. if (!elem) {
  101. break;
  102. }
  103. len = iov_from_buf(elem->in_sg, elem->in_num, 0,
  104. buf + offset, size - offset);
  105. offset += len;
  106. virtqueue_push(vq, elem, len);
  107. g_free(elem);
  108. }
  109. virtio_notify(VIRTIO_DEVICE(port->vser), vq);
  110. return offset;
  111. }
  112. static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
  113. {
  114. VirtQueueElement *elem;
  115. if (!virtio_queue_ready(vq)) {
  116. return;
  117. }
  118. for (;;) {
  119. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  120. if (!elem) {
  121. break;
  122. }
  123. virtqueue_push(vq, elem, 0);
  124. g_free(elem);
  125. }
  126. virtio_notify(vdev, vq);
  127. }
  128. static void discard_throttle_data(VirtIOSerialPort *port)
  129. {
  130. if (port->elem) {
  131. virtqueue_detach_element(port->ovq, port->elem, 0);
  132. g_free(port->elem);
  133. port->elem = NULL;
  134. }
  135. }
  136. static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
  137. VirtIODevice *vdev)
  138. {
  139. VirtIOSerialPortClass *vsc;
  140. assert(port);
  141. assert(virtio_queue_ready(vq));
  142. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  143. while (!port->throttled) {
  144. unsigned int i;
  145. /* Pop an elem only if we haven't left off a previous one mid-way */
  146. if (!port->elem) {
  147. port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  148. if (!port->elem) {
  149. break;
  150. }
  151. port->iov_idx = 0;
  152. port->iov_offset = 0;
  153. }
  154. for (i = port->iov_idx; i < port->elem->out_num; i++) {
  155. size_t buf_size;
  156. ssize_t ret;
  157. buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
  158. ret = vsc->have_data(port,
  159. port->elem->out_sg[i].iov_base
  160. + port->iov_offset,
  161. buf_size);
  162. if (!port->elem) { /* bail if we got disconnected */
  163. return;
  164. }
  165. if (port->throttled) {
  166. port->iov_idx = i;
  167. if (ret > 0) {
  168. port->iov_offset += ret;
  169. }
  170. break;
  171. }
  172. port->iov_offset = 0;
  173. }
  174. if (port->throttled) {
  175. break;
  176. }
  177. virtqueue_push(vq, port->elem, 0);
  178. g_free(port->elem);
  179. port->elem = NULL;
  180. }
  181. virtio_notify(vdev, vq);
  182. }
  183. static void flush_queued_data(VirtIOSerialPort *port)
  184. {
  185. assert(port);
  186. if (!virtio_queue_ready(port->ovq)) {
  187. return;
  188. }
  189. do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
  190. }
  191. static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
  192. {
  193. VirtQueueElement *elem;
  194. VirtQueue *vq;
  195. vq = vser->c_ivq;
  196. if (!virtio_queue_ready(vq)) {
  197. return 0;
  198. }
  199. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  200. if (!elem) {
  201. return 0;
  202. }
  203. /* TODO: detect a buffer that's too short, set NEEDS_RESET */
  204. iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
  205. virtqueue_push(vq, elem, len);
  206. virtio_notify(VIRTIO_DEVICE(vser), vq);
  207. g_free(elem);
  208. return len;
  209. }
  210. static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
  211. uint16_t event, uint16_t value)
  212. {
  213. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  214. struct virtio_console_control cpkt;
  215. virtio_stl_p(vdev, &cpkt.id, port_id);
  216. virtio_stw_p(vdev, &cpkt.event, event);
  217. virtio_stw_p(vdev, &cpkt.value, value);
  218. trace_virtio_serial_send_control_event(port_id, event, value);
  219. return send_control_msg(vser, &cpkt, sizeof(cpkt));
  220. }
  221. /* Functions for use inside qemu to open and read from/write to ports */
  222. int virtio_serial_open(VirtIOSerialPort *port)
  223. {
  224. /* Don't allow opening an already-open port */
  225. if (port->host_connected) {
  226. return 0;
  227. }
  228. /* Send port open notification to the guest */
  229. port->host_connected = true;
  230. send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
  231. return 0;
  232. }
  233. int virtio_serial_close(VirtIOSerialPort *port)
  234. {
  235. port->host_connected = false;
  236. /*
  237. * If there's any data the guest sent which the app didn't
  238. * consume, reset the throttling flag and discard the data.
  239. */
  240. port->throttled = false;
  241. discard_throttle_data(port);
  242. discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
  243. send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
  244. return 0;
  245. }
  246. /* Individual ports/apps call this function to write to the guest. */
  247. ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  248. size_t size)
  249. {
  250. if (!port || !port->host_connected || !port->guest_connected) {
  251. return 0;
  252. }
  253. return write_to_port(port, buf, size);
  254. }
  255. /*
  256. * Readiness of the guest to accept data on a port.
  257. * Returns max. data the guest can receive
  258. */
  259. size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
  260. {
  261. VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
  262. VirtQueue *vq = port->ivq;
  263. unsigned int bytes;
  264. if (!virtio_queue_ready(vq) ||
  265. !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
  266. virtio_queue_empty(vq)) {
  267. return 0;
  268. }
  269. if (use_multiport(port->vser) && !port->guest_connected) {
  270. return 0;
  271. }
  272. virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
  273. return bytes;
  274. }
  275. static void flush_queued_data_bh(void *opaque)
  276. {
  277. VirtIOSerialPort *port = opaque;
  278. flush_queued_data(port);
  279. }
  280. void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
  281. {
  282. if (!port) {
  283. return;
  284. }
  285. trace_virtio_serial_throttle_port(port->id, throttle);
  286. port->throttled = throttle;
  287. if (throttle) {
  288. return;
  289. }
  290. qemu_bh_schedule(port->bh);
  291. }
  292. /* Guest wants to notify us of some event */
  293. static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
  294. {
  295. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  296. struct VirtIOSerialPort *port;
  297. VirtIOSerialPortClass *vsc;
  298. struct virtio_console_control cpkt, *gcpkt;
  299. uint8_t *buffer;
  300. size_t buffer_len;
  301. gcpkt = buf;
  302. if (len < sizeof(cpkt)) {
  303. /* The guest sent an invalid control packet */
  304. return;
  305. }
  306. cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
  307. cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
  308. trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
  309. if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
  310. if (!cpkt.value) {
  311. error_report("virtio-serial-bus: Guest failure in adding device %s",
  312. vser->bus.qbus.name);
  313. return;
  314. }
  315. /*
  316. * The device is up, we can now tell the device about all the
  317. * ports we have here.
  318. */
  319. QTAILQ_FOREACH(port, &vser->ports, next) {
  320. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
  321. }
  322. return;
  323. }
  324. port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
  325. if (!port) {
  326. error_report("virtio-serial-bus: Unexpected port id %u for device %s",
  327. virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
  328. return;
  329. }
  330. trace_virtio_serial_handle_control_message_port(port->id);
  331. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  332. switch(cpkt.event) {
  333. case VIRTIO_CONSOLE_PORT_READY:
  334. if (!cpkt.value) {
  335. error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
  336. port->id, vser->bus.qbus.name);
  337. break;
  338. }
  339. /*
  340. * Now that we know the guest asked for the port name, we're
  341. * sure the guest has initialised whatever state is necessary
  342. * for this port. Now's a good time to let the guest know if
  343. * this port is a console port so that the guest can hook it
  344. * up to hvc.
  345. */
  346. if (vsc->is_console) {
  347. send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
  348. }
  349. if (port->name) {
  350. virtio_stl_p(vdev, &cpkt.id, port->id);
  351. virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
  352. virtio_stw_p(vdev, &cpkt.value, 1);
  353. buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
  354. buffer = g_malloc(buffer_len);
  355. memcpy(buffer, &cpkt, sizeof(cpkt));
  356. memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
  357. buffer[buffer_len - 1] = 0;
  358. send_control_msg(vser, buffer, buffer_len);
  359. g_free(buffer);
  360. }
  361. if (port->host_connected) {
  362. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
  363. }
  364. /*
  365. * When the guest has asked us for this information it means
  366. * the guest is all setup and has its virtqueues
  367. * initialised. If some app is interested in knowing about
  368. * this event, let it know.
  369. */
  370. if (vsc->guest_ready) {
  371. vsc->guest_ready(port);
  372. }
  373. break;
  374. case VIRTIO_CONSOLE_PORT_OPEN:
  375. port->guest_connected = cpkt.value;
  376. if (vsc->set_guest_connected) {
  377. /* Send the guest opened notification if an app is interested */
  378. vsc->set_guest_connected(port, cpkt.value);
  379. }
  380. break;
  381. }
  382. }
  383. static void control_in(VirtIODevice *vdev, VirtQueue *vq)
  384. {
  385. }
  386. static void control_out(VirtIODevice *vdev, VirtQueue *vq)
  387. {
  388. VirtQueueElement *elem;
  389. VirtIOSerial *vser;
  390. uint8_t *buf;
  391. size_t len;
  392. vser = VIRTIO_SERIAL(vdev);
  393. len = 0;
  394. buf = NULL;
  395. for (;;) {
  396. size_t cur_len;
  397. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  398. if (!elem) {
  399. break;
  400. }
  401. cur_len = iov_size(elem->out_sg, elem->out_num);
  402. /*
  403. * Allocate a new buf only if we didn't have one previously or
  404. * if the size of the buf differs
  405. */
  406. if (cur_len > len) {
  407. g_free(buf);
  408. buf = g_malloc(cur_len);
  409. len = cur_len;
  410. }
  411. iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
  412. handle_control_message(vser, buf, cur_len);
  413. virtqueue_push(vq, elem, 0);
  414. g_free(elem);
  415. }
  416. g_free(buf);
  417. virtio_notify(vdev, vq);
  418. }
  419. /* Guest wrote something to some port. */
  420. static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
  421. {
  422. VirtIOSerial *vser;
  423. VirtIOSerialPort *port;
  424. vser = VIRTIO_SERIAL(vdev);
  425. port = find_port_by_vq(vser, vq);
  426. if (!port || !port->host_connected) {
  427. discard_vq_data(vq, vdev);
  428. return;
  429. }
  430. if (!port->throttled) {
  431. do_flush_queued_data(port, vq, vdev);
  432. return;
  433. }
  434. }
  435. static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
  436. {
  437. /*
  438. * Users of virtio-serial would like to know when guest becomes
  439. * writable again -- i.e. if a vq had stuff queued up and the
  440. * guest wasn't reading at all, the host would not be able to
  441. * write to the vq anymore. Once the guest reads off something,
  442. * we can start queueing things up again. However, this call is
  443. * made for each buffer addition by the guest -- even though free
  444. * buffers existed prior to the current buffer addition. This is
  445. * done so as not to maintain previous state, which will need
  446. * additional live-migration-related changes.
  447. */
  448. VirtIOSerial *vser;
  449. VirtIOSerialPort *port;
  450. VirtIOSerialPortClass *vsc;
  451. vser = VIRTIO_SERIAL(vdev);
  452. port = find_port_by_vq(vser, vq);
  453. if (!port) {
  454. return;
  455. }
  456. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  457. /*
  458. * If guest_connected is false, this call is being made by the
  459. * early-boot queueing up of descriptors, which is just noise for
  460. * the host apps -- don't disturb them in that case.
  461. */
  462. if (port->guest_connected && port->host_connected && vsc->guest_writable) {
  463. vsc->guest_writable(port);
  464. }
  465. }
  466. static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
  467. Error **errp)
  468. {
  469. VirtIOSerial *vser;
  470. vser = VIRTIO_SERIAL(vdev);
  471. features |= vser->host_features;
  472. if (vser->bus.max_nr_ports > 1) {
  473. virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
  474. }
  475. return features;
  476. }
  477. /* Guest requested config info */
  478. static void get_config(VirtIODevice *vdev, uint8_t *config_data)
  479. {
  480. VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
  481. struct virtio_console_config *config =
  482. (struct virtio_console_config *)config_data;
  483. config->cols = 0;
  484. config->rows = 0;
  485. config->max_nr_ports = virtio_tswap32(vdev,
  486. vser->serial.max_virtserial_ports);
  487. }
  488. /* Guest sent new config info */
  489. static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
  490. {
  491. VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
  492. struct virtio_console_config *config =
  493. (struct virtio_console_config *)config_data;
  494. VirtIOSerialPort *port = find_first_connected_console(vser);
  495. VirtIOSerialPortClass *vsc;
  496. uint8_t emerg_wr_lo;
  497. if (!virtio_has_feature(vser->host_features,
  498. VIRTIO_CONSOLE_F_EMERG_WRITE) || !config->emerg_wr) {
  499. return;
  500. }
  501. emerg_wr_lo = le32_to_cpu(config->emerg_wr);
  502. /* Make sure we don't misdetect an emergency write when the guest
  503. * does a short config write after an emergency write. */
  504. config->emerg_wr = 0;
  505. if (!port) {
  506. return;
  507. }
  508. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  509. (void)vsc->have_data(port, &emerg_wr_lo, 1);
  510. }
  511. static void guest_reset(VirtIOSerial *vser)
  512. {
  513. VirtIOSerialPort *port;
  514. VirtIOSerialPortClass *vsc;
  515. QTAILQ_FOREACH(port, &vser->ports, next) {
  516. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  517. discard_throttle_data(port);
  518. if (port->guest_connected) {
  519. port->guest_connected = false;
  520. if (vsc->set_guest_connected) {
  521. vsc->set_guest_connected(port, false);
  522. }
  523. }
  524. }
  525. }
  526. static void set_status(VirtIODevice *vdev, uint8_t status)
  527. {
  528. VirtIOSerial *vser;
  529. VirtIOSerialPort *port;
  530. vser = VIRTIO_SERIAL(vdev);
  531. port = find_port_by_id(vser, 0);
  532. if (port && !use_multiport(port->vser)
  533. && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  534. /*
  535. * Non-multiport guests won't be able to tell us guest
  536. * open/close status. Such guests can only have a port at id
  537. * 0, so set guest_connected for such ports as soon as guest
  538. * is up.
  539. */
  540. port->guest_connected = true;
  541. }
  542. if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  543. guest_reset(vser);
  544. }
  545. QTAILQ_FOREACH(port, &vser->ports, next) {
  546. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  547. if (vsc->enable_backend) {
  548. vsc->enable_backend(port, vdev->vm_running);
  549. }
  550. }
  551. }
  552. static void vser_reset(VirtIODevice *vdev)
  553. {
  554. VirtIOSerial *vser;
  555. vser = VIRTIO_SERIAL(vdev);
  556. guest_reset(vser);
  557. }
  558. static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
  559. {
  560. VirtIOSerial *s = VIRTIO_SERIAL(vdev);
  561. VirtIOSerialPort *port;
  562. uint32_t nr_active_ports;
  563. unsigned int i, max_nr_ports;
  564. struct virtio_console_config config;
  565. /* The config space (ignored on the far end in current versions) */
  566. get_config(vdev, (uint8_t *)&config);
  567. qemu_put_be16(f, config.cols);
  568. qemu_put_be16(f, config.rows);
  569. qemu_put_be32(f, config.max_nr_ports);
  570. /* The ports map */
  571. max_nr_ports = s->serial.max_virtserial_ports;
  572. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  573. qemu_put_be32s(f, &s->ports_map[i]);
  574. }
  575. /* Ports */
  576. nr_active_ports = 0;
  577. QTAILQ_FOREACH(port, &s->ports, next) {
  578. nr_active_ports++;
  579. }
  580. qemu_put_be32s(f, &nr_active_ports);
  581. /*
  582. * Items in struct VirtIOSerialPort.
  583. */
  584. QTAILQ_FOREACH(port, &s->ports, next) {
  585. uint32_t elem_popped;
  586. qemu_put_be32s(f, &port->id);
  587. qemu_put_byte(f, port->guest_connected);
  588. qemu_put_byte(f, port->host_connected);
  589. elem_popped = 0;
  590. if (port->elem) {
  591. elem_popped = 1;
  592. }
  593. qemu_put_be32s(f, &elem_popped);
  594. if (elem_popped) {
  595. qemu_put_be32s(f, &port->iov_idx);
  596. qemu_put_be64s(f, &port->iov_offset);
  597. qemu_put_virtqueue_element(vdev, f, port->elem);
  598. }
  599. }
  600. }
  601. static void virtio_serial_post_load_timer_cb(void *opaque)
  602. {
  603. uint32_t i;
  604. VirtIOSerial *s = VIRTIO_SERIAL(opaque);
  605. VirtIOSerialPort *port;
  606. uint8_t host_connected;
  607. VirtIOSerialPortClass *vsc;
  608. if (!s->post_load) {
  609. return;
  610. }
  611. for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
  612. port = s->post_load->connected[i].port;
  613. host_connected = s->post_load->connected[i].host_connected;
  614. if (host_connected != port->host_connected) {
  615. /*
  616. * We have to let the guest know of the host connection
  617. * status change
  618. */
  619. send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
  620. port->host_connected);
  621. }
  622. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  623. if (vsc->set_guest_connected) {
  624. vsc->set_guest_connected(port, port->guest_connected);
  625. }
  626. }
  627. g_free(s->post_load->connected);
  628. timer_free(s->post_load->timer);
  629. g_free(s->post_load);
  630. s->post_load = NULL;
  631. }
  632. static int fetch_active_ports_list(QEMUFile *f,
  633. VirtIOSerial *s, uint32_t nr_active_ports)
  634. {
  635. VirtIODevice *vdev = VIRTIO_DEVICE(s);
  636. uint32_t i;
  637. s->post_load = g_malloc0(sizeof(*s->post_load));
  638. s->post_load->nr_active_ports = nr_active_ports;
  639. s->post_load->connected =
  640. g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
  641. s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  642. virtio_serial_post_load_timer_cb,
  643. s);
  644. /* Items in struct VirtIOSerialPort */
  645. for (i = 0; i < nr_active_ports; i++) {
  646. VirtIOSerialPort *port;
  647. uint32_t elem_popped;
  648. uint32_t id;
  649. id = qemu_get_be32(f);
  650. port = find_port_by_id(s, id);
  651. if (!port) {
  652. return -EINVAL;
  653. }
  654. port->guest_connected = qemu_get_byte(f);
  655. s->post_load->connected[i].port = port;
  656. s->post_load->connected[i].host_connected = qemu_get_byte(f);
  657. qemu_get_be32s(f, &elem_popped);
  658. if (elem_popped) {
  659. qemu_get_be32s(f, &port->iov_idx);
  660. qemu_get_be64s(f, &port->iov_offset);
  661. port->elem =
  662. qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
  663. /*
  664. * Port was throttled on source machine. Let's
  665. * unthrottle it here so data starts flowing again.
  666. */
  667. virtio_serial_throttle_port(port, false);
  668. }
  669. }
  670. timer_mod(s->post_load->timer, 1);
  671. return 0;
  672. }
  673. static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
  674. int version_id)
  675. {
  676. VirtIOSerial *s = VIRTIO_SERIAL(vdev);
  677. uint32_t max_nr_ports, nr_active_ports, ports_map;
  678. unsigned int i;
  679. int ret;
  680. uint32_t tmp;
  681. /* Unused */
  682. qemu_get_be16s(f, (uint16_t *) &tmp);
  683. qemu_get_be16s(f, (uint16_t *) &tmp);
  684. qemu_get_be32s(f, &tmp);
  685. max_nr_ports = s->serial.max_virtserial_ports;
  686. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  687. qemu_get_be32s(f, &ports_map);
  688. if (ports_map != s->ports_map[i]) {
  689. /*
  690. * Ports active on source and destination don't
  691. * match. Fail migration.
  692. */
  693. return -EINVAL;
  694. }
  695. }
  696. qemu_get_be32s(f, &nr_active_ports);
  697. if (nr_active_ports) {
  698. ret = fetch_active_ports_list(f, s, nr_active_ports);
  699. if (ret) {
  700. return ret;
  701. }
  702. }
  703. return 0;
  704. }
  705. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
  706. static const Property virtser_props[] = {
  707. DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
  708. DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
  709. };
  710. static void virtser_bus_class_init(ObjectClass *klass, void *data)
  711. {
  712. BusClass *k = BUS_CLASS(klass);
  713. k->print_dev = virtser_bus_dev_print;
  714. }
  715. static const TypeInfo virtser_bus_info = {
  716. .name = TYPE_VIRTIO_SERIAL_BUS,
  717. .parent = TYPE_BUS,
  718. .instance_size = sizeof(VirtIOSerialBus),
  719. .class_init = virtser_bus_class_init,
  720. };
  721. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
  722. {
  723. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
  724. monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
  725. indent, "", port->id,
  726. port->guest_connected ? "on" : "off",
  727. port->host_connected ? "on" : "off",
  728. port->throttled ? "on" : "off");
  729. }
  730. /* This function is only used if a port id is not provided by the user */
  731. static uint32_t find_free_port_id(VirtIOSerial *vser)
  732. {
  733. unsigned int i, max_nr_ports;
  734. max_nr_ports = vser->serial.max_virtserial_ports;
  735. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  736. uint32_t map, zeroes;
  737. map = vser->ports_map[i];
  738. zeroes = ctz32(~map);
  739. if (zeroes != 32) {
  740. return zeroes + i * 32;
  741. }
  742. }
  743. return VIRTIO_CONSOLE_BAD_ID;
  744. }
  745. static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
  746. {
  747. unsigned int i;
  748. i = port_id / 32;
  749. vser->ports_map[i] |= 1U << (port_id % 32);
  750. }
  751. static void add_port(VirtIOSerial *vser, uint32_t port_id)
  752. {
  753. mark_port_added(vser, port_id);
  754. send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
  755. }
  756. static void remove_port(VirtIOSerial *vser, uint32_t port_id)
  757. {
  758. VirtIOSerialPort *port;
  759. /*
  760. * Don't mark port 0 removed -- we explicitly reserve it for
  761. * backward compat with older guests, ensure a virtconsole device
  762. * unplug retains the reservation.
  763. */
  764. if (port_id) {
  765. unsigned int i;
  766. i = port_id / 32;
  767. vser->ports_map[i] &= ~(1U << (port_id % 32));
  768. }
  769. port = find_port_by_id(vser, port_id);
  770. /*
  771. * This function is only called from qdev's unplug callback; if we
  772. * get a NULL port here, we're in trouble.
  773. */
  774. assert(port);
  775. /* Flush out any unconsumed buffers first */
  776. discard_throttle_data(port);
  777. discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
  778. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
  779. }
  780. static void virtser_port_device_realize(DeviceState *dev, Error **errp)
  781. {
  782. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  783. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  784. VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
  785. int max_nr_ports;
  786. bool plugging_port0;
  787. Error *err = NULL;
  788. port->vser = bus->vser;
  789. assert(vsc->have_data);
  790. /*
  791. * Is the first console port we're seeing? If so, put it up at
  792. * location 0. This is done for backward compatibility (old
  793. * kernel, new qemu).
  794. */
  795. plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
  796. if (find_port_by_id(port->vser, port->id)) {
  797. error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
  798. port->id);
  799. return;
  800. }
  801. if (port->name != NULL && find_port_by_name(port->name)) {
  802. error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
  803. port->name);
  804. return;
  805. }
  806. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  807. if (plugging_port0) {
  808. port->id = 0;
  809. } else {
  810. port->id = find_free_port_id(port->vser);
  811. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  812. error_setg(errp, "virtio-serial-bus: Maximum port limit for "
  813. "this device reached");
  814. return;
  815. }
  816. }
  817. }
  818. max_nr_ports = port->vser->serial.max_virtserial_ports;
  819. if (port->id >= max_nr_ports) {
  820. error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
  821. "max. allowed: %u", max_nr_ports - 1);
  822. return;
  823. }
  824. vsc->realize(dev, &err);
  825. if (err != NULL) {
  826. error_propagate(errp, err);
  827. return;
  828. }
  829. port->bh = virtio_bh_new_guarded(dev, flush_queued_data_bh, port);
  830. port->elem = NULL;
  831. }
  832. static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
  833. DeviceState *dev, Error **errp)
  834. {
  835. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  836. QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
  837. port->ivq = port->vser->ivqs[port->id];
  838. port->ovq = port->vser->ovqs[port->id];
  839. add_port(port->vser, port->id);
  840. /* Send an update to the guest about this new port added */
  841. virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
  842. }
  843. static void virtser_port_device_unrealize(DeviceState *dev)
  844. {
  845. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  846. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
  847. VirtIOSerial *vser = port->vser;
  848. qemu_bh_delete(port->bh);
  849. remove_port(port->vser, port->id);
  850. QTAILQ_REMOVE(&vser->ports, port, next);
  851. if (vsc->unrealize) {
  852. vsc->unrealize(dev);
  853. }
  854. }
  855. static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
  856. {
  857. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  858. VirtIOSerial *vser = VIRTIO_SERIAL(dev);
  859. uint32_t i, max_supported_ports;
  860. size_t config_size = sizeof(struct virtio_console_config);
  861. if (!vser->serial.max_virtserial_ports) {
  862. error_setg(errp, "Maximum number of serial ports not specified");
  863. return;
  864. }
  865. /* Each port takes 2 queues, and one pair is for the control queue */
  866. max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
  867. if (vser->serial.max_virtserial_ports > max_supported_ports) {
  868. error_setg(errp, "maximum ports supported: %u", max_supported_ports);
  869. return;
  870. }
  871. if (!virtio_has_feature(vser->host_features,
  872. VIRTIO_CONSOLE_F_EMERG_WRITE)) {
  873. config_size = offsetof(struct virtio_console_config, emerg_wr);
  874. }
  875. virtio_init(vdev, VIRTIO_ID_CONSOLE, config_size);
  876. /* Spawn a new virtio-serial bus on which the ports will ride as devices */
  877. qbus_init(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
  878. dev, vdev->bus_name);
  879. qbus_set_hotplug_handler(BUS(&vser->bus), OBJECT(vser));
  880. vser->bus.vser = vser;
  881. QTAILQ_INIT(&vser->ports);
  882. vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
  883. vser->ivqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
  884. vser->ovqs = g_new(VirtQueue *, vser->serial.max_virtserial_ports);
  885. /* Add a queue for host to guest transfers for port 0 (backward compat) */
  886. vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
  887. /* Add a queue for guest to host transfers for port 0 (backward compat) */
  888. vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
  889. /* TODO: host to guest notifications can get dropped
  890. * if the queue fills up. Implement queueing in host,
  891. * this might also make it possible to reduce the control
  892. * queue size: as guest preposts buffers there,
  893. * this will save 4Kbyte of guest memory per entry. */
  894. /* control queue: host to guest */
  895. vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
  896. /* control queue: guest to host */
  897. vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
  898. for (i = 1; i < vser->bus.max_nr_ports; i++) {
  899. /* Add a per-port queue for host to guest transfers */
  900. vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
  901. /* Add a per-per queue for guest to host transfers */
  902. vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
  903. }
  904. vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
  905. * sizeof(vser->ports_map[0]));
  906. /*
  907. * Reserve location 0 for a console port for backward compat
  908. * (old kernel, new qemu)
  909. */
  910. mark_port_added(vser, 0);
  911. vser->post_load = NULL;
  912. QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
  913. }
  914. static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
  915. {
  916. DeviceClass *k = DEVICE_CLASS(klass);
  917. set_bit(DEVICE_CATEGORY_INPUT, k->categories);
  918. k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
  919. k->realize = virtser_port_device_realize;
  920. k->unrealize = virtser_port_device_unrealize;
  921. device_class_set_props(k, virtser_props);
  922. }
  923. static const TypeInfo virtio_serial_port_type_info = {
  924. .name = TYPE_VIRTIO_SERIAL_PORT,
  925. .parent = TYPE_DEVICE,
  926. .instance_size = sizeof(VirtIOSerialPort),
  927. .abstract = true,
  928. .class_size = sizeof(VirtIOSerialPortClass),
  929. .class_init = virtio_serial_port_class_init,
  930. };
  931. static void virtio_serial_device_unrealize(DeviceState *dev)
  932. {
  933. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  934. VirtIOSerial *vser = VIRTIO_SERIAL(dev);
  935. int i;
  936. QLIST_REMOVE(vser, next);
  937. virtio_delete_queue(vser->c_ivq);
  938. virtio_delete_queue(vser->c_ovq);
  939. for (i = 0; i < vser->bus.max_nr_ports; i++) {
  940. virtio_delete_queue(vser->ivqs[i]);
  941. virtio_delete_queue(vser->ovqs[i]);
  942. }
  943. g_free(vser->ivqs);
  944. g_free(vser->ovqs);
  945. g_free(vser->ports_map);
  946. if (vser->post_load) {
  947. g_free(vser->post_load->connected);
  948. timer_free(vser->post_load->timer);
  949. g_free(vser->post_load);
  950. }
  951. qbus_set_hotplug_handler(BUS(&vser->bus), NULL);
  952. virtio_cleanup(vdev);
  953. }
  954. /* Note: 'console' is used for backwards compatibility */
  955. static const VMStateDescription vmstate_virtio_console = {
  956. .name = "virtio-console",
  957. .minimum_version_id = 3,
  958. .version_id = 3,
  959. .fields = (const VMStateField[]) {
  960. VMSTATE_VIRTIO_DEVICE,
  961. VMSTATE_END_OF_LIST()
  962. },
  963. };
  964. static const Property virtio_serial_properties[] = {
  965. DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
  966. 31),
  967. DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
  968. VIRTIO_CONSOLE_F_EMERG_WRITE, true),
  969. };
  970. static void virtio_serial_class_init(ObjectClass *klass, void *data)
  971. {
  972. DeviceClass *dc = DEVICE_CLASS(klass);
  973. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  974. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  975. QLIST_INIT(&vserdevices.devices);
  976. device_class_set_props(dc, virtio_serial_properties);
  977. dc->vmsd = &vmstate_virtio_console;
  978. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  979. vdc->realize = virtio_serial_device_realize;
  980. vdc->unrealize = virtio_serial_device_unrealize;
  981. vdc->get_features = get_features;
  982. vdc->get_config = get_config;
  983. vdc->set_config = set_config;
  984. vdc->set_status = set_status;
  985. vdc->reset = vser_reset;
  986. vdc->save = virtio_serial_save_device;
  987. vdc->load = virtio_serial_load_device;
  988. hc->plug = virtser_port_device_plug;
  989. hc->unplug = qdev_simple_device_unplug_cb;
  990. }
  991. static const TypeInfo virtio_device_info = {
  992. .name = TYPE_VIRTIO_SERIAL,
  993. .parent = TYPE_VIRTIO_DEVICE,
  994. .instance_size = sizeof(VirtIOSerial),
  995. .class_init = virtio_serial_class_init,
  996. .interfaces = (InterfaceInfo[]) {
  997. { TYPE_HOTPLUG_HANDLER },
  998. { }
  999. }
  1000. };
  1001. static void virtio_serial_register_types(void)
  1002. {
  1003. type_register_static(&virtser_bus_info);
  1004. type_register_static(&virtio_serial_port_type_info);
  1005. type_register_static(&virtio_device_info);
  1006. }
  1007. type_init(virtio_serial_register_types)