2
0

virtio-serial-bus.c 29 KB

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