char-udp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "chardev/char.h"
  26. #include "io/channel-socket.h"
  27. #include "qapi/error.h"
  28. #include "qemu/module.h"
  29. #include "qemu/option.h"
  30. #include "chardev/char-io.h"
  31. #include "qom/object.h"
  32. /***********************************************************/
  33. /* UDP Net console */
  34. struct UdpChardev {
  35. Chardev parent;
  36. QIOChannel *ioc;
  37. uint8_t buf[CHR_READ_BUF_LEN];
  38. int bufcnt;
  39. int bufptr;
  40. int max_size;
  41. };
  42. typedef struct UdpChardev UdpChardev;
  43. DECLARE_INSTANCE_CHECKER(UdpChardev, UDP_CHARDEV,
  44. TYPE_CHARDEV_UDP)
  45. /* Called with chr_write_lock held. */
  46. static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len)
  47. {
  48. UdpChardev *s = UDP_CHARDEV(chr);
  49. return qio_channel_write(
  50. s->ioc, (const char *)buf, len, NULL);
  51. }
  52. static void udp_chr_flush_buffer(UdpChardev *s)
  53. {
  54. Chardev *chr = CHARDEV(s);
  55. while (s->max_size > 0 && s->bufptr < s->bufcnt) {
  56. int n = MIN(s->max_size, s->bufcnt - s->bufptr);
  57. qemu_chr_be_write(chr, &s->buf[s->bufptr], n);
  58. s->bufptr += n;
  59. s->max_size = qemu_chr_be_can_write(chr);
  60. }
  61. }
  62. static int udp_chr_read_poll(void *opaque)
  63. {
  64. Chardev *chr = CHARDEV(opaque);
  65. UdpChardev *s = UDP_CHARDEV(opaque);
  66. s->max_size = qemu_chr_be_can_write(chr);
  67. /* If there were any stray characters in the queue process them
  68. * first
  69. */
  70. udp_chr_flush_buffer(s);
  71. return s->max_size;
  72. }
  73. static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
  74. {
  75. Chardev *chr = CHARDEV(opaque);
  76. UdpChardev *s = UDP_CHARDEV(opaque);
  77. ssize_t ret;
  78. if (s->max_size == 0) {
  79. return TRUE;
  80. }
  81. ret = qio_channel_read(
  82. s->ioc, (char *)s->buf, sizeof(s->buf), NULL);
  83. if (ret <= 0) {
  84. remove_fd_in_watch(chr);
  85. return FALSE;
  86. }
  87. s->bufcnt = ret;
  88. s->bufptr = 0;
  89. udp_chr_flush_buffer(s);
  90. return TRUE;
  91. }
  92. static void udp_chr_update_read_handler(Chardev *chr)
  93. {
  94. UdpChardev *s = UDP_CHARDEV(chr);
  95. remove_fd_in_watch(chr);
  96. if (s->ioc) {
  97. chr->gsource = io_add_watch_poll(chr, s->ioc,
  98. udp_chr_read_poll,
  99. udp_chr_read, chr,
  100. chr->gcontext);
  101. }
  102. }
  103. static void char_udp_finalize(Object *obj)
  104. {
  105. Chardev *chr = CHARDEV(obj);
  106. UdpChardev *s = UDP_CHARDEV(obj);
  107. remove_fd_in_watch(chr);
  108. if (s->ioc) {
  109. object_unref(OBJECT(s->ioc));
  110. }
  111. qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
  112. }
  113. static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
  114. Error **errp)
  115. {
  116. const char *host = qemu_opt_get(opts, "host");
  117. const char *port = qemu_opt_get(opts, "port");
  118. const char *localaddr = qemu_opt_get(opts, "localaddr");
  119. const char *localport = qemu_opt_get(opts, "localport");
  120. bool has_local = false;
  121. SocketAddressLegacy *addr;
  122. ChardevUdp *udp;
  123. backend->type = CHARDEV_BACKEND_KIND_UDP;
  124. if (host == NULL || strlen(host) == 0) {
  125. host = "localhost";
  126. }
  127. if (port == NULL || strlen(port) == 0) {
  128. error_setg(errp, "chardev: udp: remote port not specified");
  129. return;
  130. }
  131. if (localport == NULL || strlen(localport) == 0) {
  132. localport = "0";
  133. } else {
  134. has_local = true;
  135. }
  136. if (localaddr == NULL || strlen(localaddr) == 0) {
  137. localaddr = "";
  138. } else {
  139. has_local = true;
  140. }
  141. udp = backend->u.udp.data = g_new0(ChardevUdp, 1);
  142. qemu_chr_parse_common(opts, qapi_ChardevUdp_base(udp));
  143. addr = g_new0(SocketAddressLegacy, 1);
  144. addr->type = SOCKET_ADDRESS_TYPE_INET;
  145. addr->u.inet.data = g_new(InetSocketAddress, 1);
  146. *addr->u.inet.data = (InetSocketAddress) {
  147. .host = g_strdup(host),
  148. .port = g_strdup(port),
  149. .has_ipv4 = qemu_opt_get(opts, "ipv4"),
  150. .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
  151. .has_ipv6 = qemu_opt_get(opts, "ipv6"),
  152. .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
  153. };
  154. udp->remote = addr;
  155. if (has_local) {
  156. addr = g_new0(SocketAddressLegacy, 1);
  157. addr->type = SOCKET_ADDRESS_TYPE_INET;
  158. addr->u.inet.data = g_new(InetSocketAddress, 1);
  159. *addr->u.inet.data = (InetSocketAddress) {
  160. .host = g_strdup(localaddr),
  161. .port = g_strdup(localport),
  162. };
  163. udp->local = addr;
  164. }
  165. }
  166. static void qmp_chardev_open_udp(Chardev *chr,
  167. ChardevBackend *backend,
  168. bool *be_opened,
  169. Error **errp)
  170. {
  171. ChardevUdp *udp = backend->u.udp.data;
  172. SocketAddress *local_addr = socket_address_flatten(udp->local);
  173. SocketAddress *remote_addr = socket_address_flatten(udp->remote);
  174. QIOChannelSocket *sioc = qio_channel_socket_new();
  175. char *name;
  176. UdpChardev *s = UDP_CHARDEV(chr);
  177. int ret;
  178. ret = qio_channel_socket_dgram_sync(sioc, local_addr, remote_addr, errp);
  179. qapi_free_SocketAddress(local_addr);
  180. qapi_free_SocketAddress(remote_addr);
  181. if (ret < 0) {
  182. object_unref(OBJECT(sioc));
  183. return;
  184. }
  185. name = g_strdup_printf("chardev-udp-%s", chr->label);
  186. qio_channel_set_name(QIO_CHANNEL(sioc), name);
  187. g_free(name);
  188. s->ioc = QIO_CHANNEL(sioc);
  189. /* be isn't opened until we get a connection */
  190. *be_opened = false;
  191. }
  192. static void char_udp_class_init(ObjectClass *oc, void *data)
  193. {
  194. ChardevClass *cc = CHARDEV_CLASS(oc);
  195. cc->parse = qemu_chr_parse_udp;
  196. cc->open = qmp_chardev_open_udp;
  197. cc->chr_write = udp_chr_write;
  198. cc->chr_update_read_handler = udp_chr_update_read_handler;
  199. }
  200. static const TypeInfo char_udp_type_info = {
  201. .name = TYPE_CHARDEV_UDP,
  202. .parent = TYPE_CHARDEV,
  203. .instance_size = sizeof(UdpChardev),
  204. .instance_finalize = char_udp_finalize,
  205. .class_init = char_udp_class_init,
  206. };
  207. static void register_types(void)
  208. {
  209. type_register_static(&char_udp_type_info);
  210. }
  211. type_init(register_types);