virtio-serial-bus.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 "iov.h"
  21. #include "monitor.h"
  22. #include "qemu-queue.h"
  23. #include "sysbus.h"
  24. #include "trace.h"
  25. #include "virtio-serial.h"
  26. /* The virtio-serial bus on top of which the ports will ride as devices */
  27. struct VirtIOSerialBus {
  28. BusState qbus;
  29. /* This is the parent device that provides the bus for ports. */
  30. VirtIOSerial *vser;
  31. /* The maximum number of ports that can ride on top of this bus */
  32. uint32_t max_nr_ports;
  33. };
  34. struct VirtIOSerial {
  35. VirtIODevice vdev;
  36. VirtQueue *c_ivq, *c_ovq;
  37. /* Arrays of ivqs and ovqs: one per port */
  38. VirtQueue **ivqs, **ovqs;
  39. VirtIOSerialBus bus;
  40. DeviceState *qdev;
  41. QTAILQ_HEAD(, VirtIOSerialPort) ports;
  42. /* bitmap for identifying active ports */
  43. uint32_t *ports_map;
  44. struct virtio_console_config config;
  45. };
  46. static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
  47. {
  48. VirtIOSerialPort *port;
  49. if (id == VIRTIO_CONSOLE_BAD_ID) {
  50. return NULL;
  51. }
  52. QTAILQ_FOREACH(port, &vser->ports, next) {
  53. if (port->id == id)
  54. return port;
  55. }
  56. return NULL;
  57. }
  58. static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
  59. {
  60. VirtIOSerialPort *port;
  61. QTAILQ_FOREACH(port, &vser->ports, next) {
  62. if (port->ivq == vq || port->ovq == vq)
  63. return port;
  64. }
  65. return NULL;
  66. }
  67. static bool use_multiport(VirtIOSerial *vser)
  68. {
  69. return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  70. }
  71. static size_t write_to_port(VirtIOSerialPort *port,
  72. const uint8_t *buf, size_t size)
  73. {
  74. VirtQueueElement elem;
  75. VirtQueue *vq;
  76. size_t offset;
  77. vq = port->ivq;
  78. if (!virtio_queue_ready(vq)) {
  79. return 0;
  80. }
  81. offset = 0;
  82. while (offset < size) {
  83. size_t len;
  84. if (!virtqueue_pop(vq, &elem)) {
  85. break;
  86. }
  87. len = iov_from_buf(elem.in_sg, elem.in_num, 0,
  88. buf + offset, size - offset);
  89. offset += len;
  90. virtqueue_push(vq, &elem, len);
  91. }
  92. virtio_notify(&port->vser->vdev, vq);
  93. return offset;
  94. }
  95. static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
  96. {
  97. VirtQueueElement elem;
  98. if (!virtio_queue_ready(vq)) {
  99. return;
  100. }
  101. while (virtqueue_pop(vq, &elem)) {
  102. virtqueue_push(vq, &elem, 0);
  103. }
  104. virtio_notify(vdev, vq);
  105. }
  106. static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
  107. VirtIODevice *vdev)
  108. {
  109. VirtIOSerialPortClass *vsc;
  110. assert(port);
  111. assert(virtio_queue_ready(vq));
  112. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  113. while (!port->throttled) {
  114. unsigned int i;
  115. /* Pop an elem only if we haven't left off a previous one mid-way */
  116. if (!port->elem.out_num) {
  117. if (!virtqueue_pop(vq, &port->elem)) {
  118. break;
  119. }
  120. port->iov_idx = 0;
  121. port->iov_offset = 0;
  122. }
  123. for (i = port->iov_idx; i < port->elem.out_num; i++) {
  124. size_t buf_size;
  125. ssize_t ret;
  126. buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
  127. ret = vsc->have_data(port,
  128. port->elem.out_sg[i].iov_base
  129. + port->iov_offset,
  130. buf_size);
  131. if (ret < 0 && ret != -EAGAIN) {
  132. /* We don't handle any other type of errors here */
  133. abort();
  134. }
  135. if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
  136. /*
  137. * this is a temporary check until chardevs can signal to
  138. * frontends that they are writable again. This prevents
  139. * the console from going into throttled mode (forever)
  140. * if virtio-console is connected to a pty without a
  141. * listener. Otherwise the guest spins forever.
  142. * We can revert this if
  143. * 1: chardevs can notify frondends
  144. * 2: the guest driver does not spin in these cases
  145. */
  146. if (!vsc->is_console) {
  147. virtio_serial_throttle_port(port, true);
  148. }
  149. port->iov_idx = i;
  150. if (ret > 0) {
  151. port->iov_offset += ret;
  152. }
  153. break;
  154. }
  155. port->iov_offset = 0;
  156. }
  157. if (port->throttled) {
  158. break;
  159. }
  160. virtqueue_push(vq, &port->elem, 0);
  161. port->elem.out_num = 0;
  162. }
  163. virtio_notify(vdev, vq);
  164. }
  165. static void flush_queued_data(VirtIOSerialPort *port)
  166. {
  167. assert(port);
  168. if (!virtio_queue_ready(port->ovq)) {
  169. return;
  170. }
  171. do_flush_queued_data(port, port->ovq, &port->vser->vdev);
  172. }
  173. static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
  174. {
  175. VirtQueueElement elem;
  176. VirtQueue *vq;
  177. struct virtio_console_control *cpkt;
  178. vq = port->vser->c_ivq;
  179. if (!virtio_queue_ready(vq)) {
  180. return 0;
  181. }
  182. if (!virtqueue_pop(vq, &elem)) {
  183. return 0;
  184. }
  185. cpkt = (struct virtio_console_control *)buf;
  186. stl_p(&cpkt->id, port->id);
  187. memcpy(elem.in_sg[0].iov_base, buf, len);
  188. virtqueue_push(vq, &elem, len);
  189. virtio_notify(&port->vser->vdev, vq);
  190. return len;
  191. }
  192. static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
  193. uint16_t value)
  194. {
  195. struct virtio_console_control cpkt;
  196. stw_p(&cpkt.event, event);
  197. stw_p(&cpkt.value, value);
  198. trace_virtio_serial_send_control_event(port->id, event, value);
  199. return send_control_msg(port, &cpkt, sizeof(cpkt));
  200. }
  201. /* Functions for use inside qemu to open and read from/write to ports */
  202. int virtio_serial_open(VirtIOSerialPort *port)
  203. {
  204. /* Don't allow opening an already-open port */
  205. if (port->host_connected) {
  206. return 0;
  207. }
  208. /* Send port open notification to the guest */
  209. port->host_connected = true;
  210. send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  211. return 0;
  212. }
  213. int virtio_serial_close(VirtIOSerialPort *port)
  214. {
  215. port->host_connected = false;
  216. /*
  217. * If there's any data the guest sent which the app didn't
  218. * consume, reset the throttling flag and discard the data.
  219. */
  220. port->throttled = false;
  221. discard_vq_data(port->ovq, &port->vser->vdev);
  222. send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
  223. return 0;
  224. }
  225. /* Individual ports/apps call this function to write to the guest. */
  226. ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  227. size_t size)
  228. {
  229. if (!port || !port->host_connected || !port->guest_connected) {
  230. return 0;
  231. }
  232. return write_to_port(port, buf, size);
  233. }
  234. /*
  235. * Readiness of the guest to accept data on a port.
  236. * Returns max. data the guest can receive
  237. */
  238. size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
  239. {
  240. VirtQueue *vq = port->ivq;
  241. if (!virtio_queue_ready(vq) ||
  242. !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
  243. virtio_queue_empty(vq)) {
  244. return 0;
  245. }
  246. if (use_multiport(port->vser) && !port->guest_connected) {
  247. return 0;
  248. }
  249. if (virtqueue_avail_bytes(vq, 4096, 0)) {
  250. return 4096;
  251. }
  252. if (virtqueue_avail_bytes(vq, 1, 0)) {
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. static void flush_queued_data_bh(void *opaque)
  258. {
  259. VirtIOSerialPort *port = opaque;
  260. flush_queued_data(port);
  261. }
  262. void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
  263. {
  264. if (!port) {
  265. return;
  266. }
  267. trace_virtio_serial_throttle_port(port->id, throttle);
  268. port->throttled = throttle;
  269. if (throttle) {
  270. return;
  271. }
  272. qemu_bh_schedule(port->bh);
  273. }
  274. /* Guest wants to notify us of some event */
  275. static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
  276. {
  277. struct VirtIOSerialPort *port;
  278. VirtIOSerialPortClass *vsc;
  279. struct virtio_console_control cpkt, *gcpkt;
  280. uint8_t *buffer;
  281. size_t buffer_len;
  282. gcpkt = buf;
  283. if (len < sizeof(cpkt)) {
  284. /* The guest sent an invalid control packet */
  285. return;
  286. }
  287. cpkt.event = lduw_p(&gcpkt->event);
  288. cpkt.value = lduw_p(&gcpkt->value);
  289. trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
  290. if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
  291. if (!cpkt.value) {
  292. error_report("virtio-serial-bus: Guest failure in adding device %s",
  293. vser->bus.qbus.name);
  294. return;
  295. }
  296. /*
  297. * The device is up, we can now tell the device about all the
  298. * ports we have here.
  299. */
  300. QTAILQ_FOREACH(port, &vser->ports, next) {
  301. send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
  302. }
  303. return;
  304. }
  305. port = find_port_by_id(vser, ldl_p(&gcpkt->id));
  306. if (!port) {
  307. error_report("virtio-serial-bus: Unexpected port id %u for device %s",
  308. ldl_p(&gcpkt->id), vser->bus.qbus.name);
  309. return;
  310. }
  311. trace_virtio_serial_handle_control_message_port(port->id);
  312. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  313. switch(cpkt.event) {
  314. case VIRTIO_CONSOLE_PORT_READY:
  315. if (!cpkt.value) {
  316. error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
  317. port->id, vser->bus.qbus.name);
  318. break;
  319. }
  320. /*
  321. * Now that we know the guest asked for the port name, we're
  322. * sure the guest has initialised whatever state is necessary
  323. * for this port. Now's a good time to let the guest know if
  324. * this port is a console port so that the guest can hook it
  325. * up to hvc.
  326. */
  327. if (vsc->is_console) {
  328. send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
  329. }
  330. if (port->name) {
  331. stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
  332. stw_p(&cpkt.value, 1);
  333. buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
  334. buffer = g_malloc(buffer_len);
  335. memcpy(buffer, &cpkt, sizeof(cpkt));
  336. memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
  337. buffer[buffer_len - 1] = 0;
  338. send_control_msg(port, buffer, buffer_len);
  339. g_free(buffer);
  340. }
  341. if (port->host_connected) {
  342. send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  343. }
  344. /*
  345. * When the guest has asked us for this information it means
  346. * the guest is all setup and has its virtqueues
  347. * initialised. If some app is interested in knowing about
  348. * this event, let it know.
  349. */
  350. if (vsc->guest_ready) {
  351. vsc->guest_ready(port);
  352. }
  353. break;
  354. case VIRTIO_CONSOLE_PORT_OPEN:
  355. port->guest_connected = cpkt.value;
  356. if (cpkt.value && vsc->guest_open) {
  357. /* Send the guest opened notification if an app is interested */
  358. vsc->guest_open(port);
  359. }
  360. if (!cpkt.value && vsc->guest_close) {
  361. /* Send the guest closed notification if an app is interested */
  362. vsc->guest_close(port);
  363. }
  364. break;
  365. }
  366. }
  367. static void control_in(VirtIODevice *vdev, VirtQueue *vq)
  368. {
  369. }
  370. static void control_out(VirtIODevice *vdev, VirtQueue *vq)
  371. {
  372. VirtQueueElement elem;
  373. VirtIOSerial *vser;
  374. uint8_t *buf;
  375. size_t len;
  376. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  377. len = 0;
  378. buf = NULL;
  379. while (virtqueue_pop(vq, &elem)) {
  380. size_t cur_len;
  381. cur_len = iov_size(elem.out_sg, elem.out_num);
  382. /*
  383. * Allocate a new buf only if we didn't have one previously or
  384. * if the size of the buf differs
  385. */
  386. if (cur_len > len) {
  387. g_free(buf);
  388. buf = g_malloc(cur_len);
  389. len = cur_len;
  390. }
  391. iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
  392. handle_control_message(vser, buf, cur_len);
  393. virtqueue_push(vq, &elem, 0);
  394. }
  395. g_free(buf);
  396. virtio_notify(vdev, vq);
  397. }
  398. /* Guest wrote something to some port. */
  399. static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
  400. {
  401. VirtIOSerial *vser;
  402. VirtIOSerialPort *port;
  403. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  404. port = find_port_by_vq(vser, vq);
  405. if (!port || !port->host_connected) {
  406. discard_vq_data(vq, vdev);
  407. return;
  408. }
  409. if (!port->throttled) {
  410. do_flush_queued_data(port, vq, vdev);
  411. return;
  412. }
  413. }
  414. static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
  415. {
  416. }
  417. static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
  418. {
  419. VirtIOSerial *vser;
  420. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  421. if (vser->bus.max_nr_ports > 1) {
  422. features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
  423. }
  424. return features;
  425. }
  426. /* Guest requested config info */
  427. static void get_config(VirtIODevice *vdev, uint8_t *config_data)
  428. {
  429. VirtIOSerial *vser;
  430. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  431. memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
  432. }
  433. static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
  434. {
  435. struct virtio_console_config config;
  436. memcpy(&config, config_data, sizeof(config));
  437. }
  438. static void guest_reset(VirtIOSerial *vser)
  439. {
  440. VirtIOSerialPort *port;
  441. VirtIOSerialPortClass *vsc;
  442. QTAILQ_FOREACH(port, &vser->ports, next) {
  443. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  444. if (port->guest_connected) {
  445. port->guest_connected = false;
  446. if (vsc->guest_close)
  447. vsc->guest_close(port);
  448. }
  449. }
  450. }
  451. static void set_status(VirtIODevice *vdev, uint8_t status)
  452. {
  453. VirtIOSerial *vser;
  454. VirtIOSerialPort *port;
  455. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  456. port = find_port_by_id(vser, 0);
  457. if (port && !use_multiport(port->vser)
  458. && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  459. /*
  460. * Non-multiport guests won't be able to tell us guest
  461. * open/close status. Such guests can only have a port at id
  462. * 0, so set guest_connected for such ports as soon as guest
  463. * is up.
  464. */
  465. port->guest_connected = true;
  466. }
  467. if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  468. guest_reset(vser);
  469. }
  470. }
  471. static void vser_reset(VirtIODevice *vdev)
  472. {
  473. VirtIOSerial *vser;
  474. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  475. guest_reset(vser);
  476. }
  477. static void virtio_serial_save(QEMUFile *f, void *opaque)
  478. {
  479. VirtIOSerial *s = opaque;
  480. VirtIOSerialPort *port;
  481. uint32_t nr_active_ports;
  482. unsigned int i, max_nr_ports;
  483. /* The virtio device */
  484. virtio_save(&s->vdev, f);
  485. /* The config space */
  486. qemu_put_be16s(f, &s->config.cols);
  487. qemu_put_be16s(f, &s->config.rows);
  488. qemu_put_be32s(f, &s->config.max_nr_ports);
  489. /* The ports map */
  490. max_nr_ports = tswap32(s->config.max_nr_ports);
  491. for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
  492. qemu_put_be32s(f, &s->ports_map[i]);
  493. }
  494. /* Ports */
  495. nr_active_ports = 0;
  496. QTAILQ_FOREACH(port, &s->ports, next) {
  497. nr_active_ports++;
  498. }
  499. qemu_put_be32s(f, &nr_active_ports);
  500. /*
  501. * Items in struct VirtIOSerialPort.
  502. */
  503. QTAILQ_FOREACH(port, &s->ports, next) {
  504. uint32_t elem_popped;
  505. qemu_put_be32s(f, &port->id);
  506. qemu_put_byte(f, port->guest_connected);
  507. qemu_put_byte(f, port->host_connected);
  508. elem_popped = 0;
  509. if (port->elem.out_num) {
  510. elem_popped = 1;
  511. }
  512. qemu_put_be32s(f, &elem_popped);
  513. if (elem_popped) {
  514. qemu_put_be32s(f, &port->iov_idx);
  515. qemu_put_be64s(f, &port->iov_offset);
  516. qemu_put_buffer(f, (unsigned char *)&port->elem,
  517. sizeof(port->elem));
  518. }
  519. }
  520. }
  521. static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
  522. {
  523. VirtIOSerial *s = opaque;
  524. VirtIOSerialPort *port;
  525. uint32_t max_nr_ports, nr_active_ports, ports_map;
  526. unsigned int i;
  527. int ret;
  528. if (version_id > 3) {
  529. return -EINVAL;
  530. }
  531. /* The virtio device */
  532. ret = virtio_load(&s->vdev, f);
  533. if (ret) {
  534. return ret;
  535. }
  536. if (version_id < 2) {
  537. return 0;
  538. }
  539. /* The config space */
  540. qemu_get_be16s(f, &s->config.cols);
  541. qemu_get_be16s(f, &s->config.rows);
  542. qemu_get_be32s(f, &max_nr_ports);
  543. tswap32s(&max_nr_ports);
  544. if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
  545. /* Source could have had more ports than us. Fail migration. */
  546. return -EINVAL;
  547. }
  548. for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
  549. qemu_get_be32s(f, &ports_map);
  550. if (ports_map != s->ports_map[i]) {
  551. /*
  552. * Ports active on source and destination don't
  553. * match. Fail migration.
  554. */
  555. return -EINVAL;
  556. }
  557. }
  558. qemu_get_be32s(f, &nr_active_ports);
  559. /* Items in struct VirtIOSerialPort */
  560. for (i = 0; i < nr_active_ports; i++) {
  561. uint32_t id;
  562. bool host_connected;
  563. id = qemu_get_be32(f);
  564. port = find_port_by_id(s, id);
  565. if (!port) {
  566. return -EINVAL;
  567. }
  568. port->guest_connected = qemu_get_byte(f);
  569. host_connected = qemu_get_byte(f);
  570. if (host_connected != port->host_connected) {
  571. /*
  572. * We have to let the guest know of the host connection
  573. * status change
  574. */
  575. send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
  576. port->host_connected);
  577. }
  578. if (version_id > 2) {
  579. uint32_t elem_popped;
  580. qemu_get_be32s(f, &elem_popped);
  581. if (elem_popped) {
  582. qemu_get_be32s(f, &port->iov_idx);
  583. qemu_get_be64s(f, &port->iov_offset);
  584. qemu_get_buffer(f, (unsigned char *)&port->elem,
  585. sizeof(port->elem));
  586. virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
  587. port->elem.in_num, 1);
  588. virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
  589. port->elem.out_num, 1);
  590. /*
  591. * Port was throttled on source machine. Let's
  592. * unthrottle it here so data starts flowing again.
  593. */
  594. virtio_serial_throttle_port(port, false);
  595. }
  596. }
  597. }
  598. return 0;
  599. }
  600. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
  601. static Property virtser_props[] = {
  602. DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
  603. DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
  604. DEFINE_PROP_END_OF_LIST()
  605. };
  606. #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
  607. #define VIRTIO_SERIAL_BUS(obj) \
  608. OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
  609. static void virtser_bus_class_init(ObjectClass *klass, void *data)
  610. {
  611. BusClass *k = BUS_CLASS(klass);
  612. k->print_dev = virtser_bus_dev_print;
  613. }
  614. static const TypeInfo virtser_bus_info = {
  615. .name = TYPE_VIRTIO_SERIAL_BUS,
  616. .parent = TYPE_BUS,
  617. .instance_size = sizeof(VirtIOSerialBus),
  618. .class_init = virtser_bus_class_init,
  619. };
  620. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
  621. {
  622. VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
  623. monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
  624. indent, "", port->id,
  625. port->guest_connected ? "on" : "off",
  626. port->host_connected ? "on" : "off",
  627. port->throttled ? "on" : "off");
  628. }
  629. /* This function is only used if a port id is not provided by the user */
  630. static uint32_t find_free_port_id(VirtIOSerial *vser)
  631. {
  632. unsigned int i, max_nr_ports;
  633. max_nr_ports = tswap32(vser->config.max_nr_ports);
  634. for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
  635. uint32_t map, bit;
  636. map = vser->ports_map[i];
  637. bit = ffs(~map);
  638. if (bit) {
  639. return (bit - 1) + i * 32;
  640. }
  641. }
  642. return VIRTIO_CONSOLE_BAD_ID;
  643. }
  644. static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
  645. {
  646. unsigned int i;
  647. i = port_id / 32;
  648. vser->ports_map[i] |= 1U << (port_id % 32);
  649. }
  650. static void add_port(VirtIOSerial *vser, uint32_t port_id)
  651. {
  652. mark_port_added(vser, port_id);
  653. send_control_event(find_port_by_id(vser, port_id),
  654. VIRTIO_CONSOLE_PORT_ADD, 1);
  655. }
  656. static void remove_port(VirtIOSerial *vser, uint32_t port_id)
  657. {
  658. VirtIOSerialPort *port;
  659. unsigned int i;
  660. i = port_id / 32;
  661. vser->ports_map[i] &= ~(1U << (port_id % 32));
  662. port = find_port_by_id(vser, port_id);
  663. /* Flush out any unconsumed buffers first */
  664. discard_vq_data(port->ovq, &port->vser->vdev);
  665. send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
  666. }
  667. static int virtser_port_qdev_init(DeviceState *qdev)
  668. {
  669. VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
  670. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  671. VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
  672. int ret, max_nr_ports;
  673. bool plugging_port0;
  674. port->vser = bus->vser;
  675. port->bh = qemu_bh_new(flush_queued_data_bh, port);
  676. assert(vsc->have_data);
  677. /*
  678. * Is the first console port we're seeing? If so, put it up at
  679. * location 0. This is done for backward compatibility (old
  680. * kernel, new qemu).
  681. */
  682. plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
  683. if (find_port_by_id(port->vser, port->id)) {
  684. error_report("virtio-serial-bus: A port already exists at id %u",
  685. port->id);
  686. return -1;
  687. }
  688. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  689. if (plugging_port0) {
  690. port->id = 0;
  691. } else {
  692. port->id = find_free_port_id(port->vser);
  693. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  694. error_report("virtio-serial-bus: Maximum port limit for this device reached");
  695. return -1;
  696. }
  697. }
  698. }
  699. max_nr_ports = tswap32(port->vser->config.max_nr_ports);
  700. if (port->id >= max_nr_ports) {
  701. error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
  702. max_nr_ports - 1);
  703. return -1;
  704. }
  705. ret = vsc->init(port);
  706. if (ret) {
  707. return ret;
  708. }
  709. port->elem.out_num = 0;
  710. QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
  711. port->ivq = port->vser->ivqs[port->id];
  712. port->ovq = port->vser->ovqs[port->id];
  713. add_port(port->vser, port->id);
  714. /* Send an update to the guest about this new port added */
  715. virtio_notify_config(&port->vser->vdev);
  716. return ret;
  717. }
  718. static int virtser_port_qdev_exit(DeviceState *qdev)
  719. {
  720. VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
  721. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  722. VirtIOSerial *vser = port->vser;
  723. qemu_bh_delete(port->bh);
  724. remove_port(port->vser, port->id);
  725. QTAILQ_REMOVE(&vser->ports, port, next);
  726. if (vsc->exit) {
  727. vsc->exit(port);
  728. }
  729. return 0;
  730. }
  731. VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
  732. {
  733. VirtIOSerial *vser;
  734. VirtIODevice *vdev;
  735. uint32_t i, max_supported_ports;
  736. if (!conf->max_virtserial_ports)
  737. return NULL;
  738. /* Each port takes 2 queues, and one pair is for the control queue */
  739. max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
  740. if (conf->max_virtserial_ports > max_supported_ports) {
  741. error_report("maximum ports supported: %u", max_supported_ports);
  742. return NULL;
  743. }
  744. vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
  745. sizeof(struct virtio_console_config),
  746. sizeof(VirtIOSerial));
  747. vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  748. /* Spawn a new virtio-serial bus on which the ports will ride as devices */
  749. qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
  750. vser->bus.qbus.allow_hotplug = 1;
  751. vser->bus.vser = vser;
  752. QTAILQ_INIT(&vser->ports);
  753. vser->bus.max_nr_ports = conf->max_virtserial_ports;
  754. vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
  755. vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
  756. /* Add a queue for host to guest transfers for port 0 (backward compat) */
  757. vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
  758. /* Add a queue for guest to host transfers for port 0 (backward compat) */
  759. vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
  760. /* TODO: host to guest notifications can get dropped
  761. * if the queue fills up. Implement queueing in host,
  762. * this might also make it possible to reduce the control
  763. * queue size: as guest preposts buffers there,
  764. * this will save 4Kbyte of guest memory per entry. */
  765. /* control queue: host to guest */
  766. vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
  767. /* control queue: guest to host */
  768. vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
  769. for (i = 1; i < vser->bus.max_nr_ports; i++) {
  770. /* Add a per-port queue for host to guest transfers */
  771. vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
  772. /* Add a per-per queue for guest to host transfers */
  773. vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
  774. }
  775. vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
  776. vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
  777. * sizeof(vser->ports_map[0]));
  778. /*
  779. * Reserve location 0 for a console port for backward compat
  780. * (old kernel, new qemu)
  781. */
  782. mark_port_added(vser, 0);
  783. vser->vdev.get_features = get_features;
  784. vser->vdev.get_config = get_config;
  785. vser->vdev.set_config = set_config;
  786. vser->vdev.set_status = set_status;
  787. vser->vdev.reset = vser_reset;
  788. vser->qdev = dev;
  789. /*
  790. * Register for the savevm section with the virtio-console name
  791. * to preserve backward compat
  792. */
  793. register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
  794. virtio_serial_load, vser);
  795. return vdev;
  796. }
  797. void virtio_serial_exit(VirtIODevice *vdev)
  798. {
  799. VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
  800. unregister_savevm(vser->qdev, "virtio-console", vser);
  801. g_free(vser->ivqs);
  802. g_free(vser->ovqs);
  803. g_free(vser->ports_map);
  804. virtio_cleanup(vdev);
  805. }
  806. static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
  807. {
  808. DeviceClass *k = DEVICE_CLASS(klass);
  809. k->init = virtser_port_qdev_init;
  810. k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
  811. k->exit = virtser_port_qdev_exit;
  812. k->unplug = qdev_simple_unplug_cb;
  813. k->props = virtser_props;
  814. }
  815. static TypeInfo virtio_serial_port_type_info = {
  816. .name = TYPE_VIRTIO_SERIAL_PORT,
  817. .parent = TYPE_DEVICE,
  818. .instance_size = sizeof(VirtIOSerialPort),
  819. .abstract = true,
  820. .class_size = sizeof(VirtIOSerialPortClass),
  821. .class_init = virtio_serial_port_class_init,
  822. };
  823. static void virtio_serial_register_types(void)
  824. {
  825. type_register_static(&virtser_bus_info);
  826. type_register_static(&virtio_serial_port_type_info);
  827. }
  828. type_init(virtio_serial_register_types)