terminal3270.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Terminal 3270 implementation
  3. *
  4. * Copyright 2017 IBM Corp.
  5. *
  6. * Authors: Yang Chen <bjcyang@linux.vnet.ibm.com>
  7. * Jing Liu <liujbjl@linux.vnet.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  10. * your option) any later version. See the COPYING file in the top-level
  11. * directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qemu/module.h"
  16. #include "chardev/char-fe.h"
  17. #include "hw/qdev-properties.h"
  18. #include "hw/s390x/3270-ccw.h"
  19. /* Enough spaces for different window sizes. */
  20. #define INPUT_BUFFER_SIZE 1000
  21. /*
  22. * 1 for header, 1024*2 for datastream, 2 for tail
  23. * Reserve enough spaces for telnet IAC escape.
  24. */
  25. #define OUTPUT_BUFFER_SIZE 2051
  26. typedef struct Terminal3270 {
  27. EmulatedCcw3270Device cdev;
  28. CharBackend chr;
  29. uint8_t inv[INPUT_BUFFER_SIZE];
  30. uint8_t outv[OUTPUT_BUFFER_SIZE];
  31. int in_len;
  32. bool handshake_done;
  33. guint timer_tag;
  34. } Terminal3270;
  35. #define TYPE_TERMINAL_3270 "x-terminal3270"
  36. #define TERMINAL_3270(obj) \
  37. OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270)
  38. static int terminal_can_read(void *opaque)
  39. {
  40. Terminal3270 *t = opaque;
  41. return INPUT_BUFFER_SIZE - t->in_len;
  42. }
  43. static void terminal_timer_cancel(Terminal3270 *t)
  44. {
  45. if (t->timer_tag) {
  46. g_source_remove(t->timer_tag);
  47. t->timer_tag = 0;
  48. }
  49. }
  50. /*
  51. * Protocol handshake done,
  52. * signal guest by an unsolicited DE irq.
  53. */
  54. static void TN3270_handshake_done(Terminal3270 *t)
  55. {
  56. CcwDevice *ccw_dev = CCW_DEVICE(t);
  57. SubchDev *sch = ccw_dev->sch;
  58. t->handshake_done = true;
  59. sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
  60. css_conditional_io_interrupt(sch);
  61. }
  62. /*
  63. * Called when the interval is timeout to detect
  64. * if the client is still alive by Timing Mark.
  65. */
  66. static gboolean send_timing_mark_cb(gpointer opaque)
  67. {
  68. Terminal3270 *t = opaque;
  69. const uint8_t timing[] = {0xff, 0xfd, 0x06};
  70. qemu_chr_fe_write_all(&t->chr, timing, sizeof(timing));
  71. return true;
  72. }
  73. /*
  74. * Receive inbound data from socket.
  75. * For data given to guest, drop the data boundary IAC, IAC_EOR.
  76. * TODO:
  77. * Using "Reset" key on x3270 may result multiple commands in one packet.
  78. * This usually happens when the user meets a poor traffic of the network.
  79. * As of now, for such case, we simply terminate the connection,
  80. * and we should come back here later with a better solution.
  81. */
  82. static void terminal_read(void *opaque, const uint8_t *buf, int size)
  83. {
  84. Terminal3270 *t = opaque;
  85. CcwDevice *ccw_dev = CCW_DEVICE(t);
  86. SubchDev *sch = ccw_dev->sch;
  87. int end;
  88. assert(size <= (INPUT_BUFFER_SIZE - t->in_len));
  89. terminal_timer_cancel(t);
  90. t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
  91. memcpy(&t->inv[t->in_len], buf, size);
  92. t->in_len += size;
  93. if (t->in_len < 2) {
  94. return;
  95. }
  96. if (!t->handshake_done) {
  97. /*
  98. * Receiving Terminal Type is the last step of handshake.
  99. * The data format: IAC SB Terminal-Type IS <terminal type> IAC SE
  100. * The code for Terminal-Type is 0x18, for IS is 0.
  101. * Simply check the data format and mark handshake_done.
  102. */
  103. if (t->in_len > 6 && t->inv[2] == 0x18 && t->inv[3] == 0x0 &&
  104. t->inv[t->in_len - 2] == IAC && t->inv[t->in_len - 1] == IAC_SE) {
  105. TN3270_handshake_done(t);
  106. t->in_len = 0;
  107. }
  108. return;
  109. }
  110. for (end = 0; end < t->in_len - 1; end++) {
  111. if (t->inv[end] == IAC && t->inv[end + 1] == IAC_EOR) {
  112. break;
  113. }
  114. }
  115. if (end == t->in_len - 2) {
  116. /* Data is valid for consuming. */
  117. t->in_len -= 2;
  118. sch->curr_status.scsw.dstat = SCSW_DSTAT_ATTENTION;
  119. css_conditional_io_interrupt(sch);
  120. } else if (end < t->in_len - 2) {
  121. /* "Reset" key is used. */
  122. qemu_chr_fe_disconnect(&t->chr);
  123. } else {
  124. /* Gathering data. */
  125. return;
  126. }
  127. }
  128. static void chr_event(void *opaque, int event)
  129. {
  130. Terminal3270 *t = opaque;
  131. CcwDevice *ccw_dev = CCW_DEVICE(t);
  132. SubchDev *sch = ccw_dev->sch;
  133. /* Ensure the initial status correct, always reset them. */
  134. t->in_len = 0;
  135. t->handshake_done = false;
  136. terminal_timer_cancel(t);
  137. switch (event) {
  138. case CHR_EVENT_OPENED:
  139. /*
  140. * 3270 does handshake firstly by the negotiate options in
  141. * char-socket.c. Once qemu receives the terminal-type of the
  142. * client, mark handshake done and trigger everything rolling again.
  143. */
  144. t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
  145. break;
  146. case CHR_EVENT_CLOSED:
  147. sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
  148. css_conditional_io_interrupt(sch);
  149. break;
  150. }
  151. }
  152. static void terminal_init(EmulatedCcw3270Device *dev, Error **errp)
  153. {
  154. Terminal3270 *t = TERMINAL_3270(dev);
  155. static bool terminal_available;
  156. if (terminal_available) {
  157. error_setg(errp, "Multiple 3270 terminals are not supported.");
  158. return;
  159. }
  160. terminal_available = true;
  161. qemu_chr_fe_set_handlers(&t->chr, terminal_can_read,
  162. terminal_read, chr_event, NULL, t, NULL, true);
  163. }
  164. static inline CcwDataStream *get_cds(Terminal3270 *t)
  165. {
  166. return &(CCW_DEVICE(&t->cdev)->sch->cds);
  167. }
  168. static int read_payload_3270(EmulatedCcw3270Device *dev)
  169. {
  170. Terminal3270 *t = TERMINAL_3270(dev);
  171. int len;
  172. len = MIN(ccw_dstream_avail(get_cds(t)), t->in_len);
  173. ccw_dstream_write_buf(get_cds(t), t->inv, len);
  174. t->in_len -= len;
  175. return len;
  176. }
  177. /* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */
  178. static int insert_IAC_escape_char(uint8_t *outv, int out_len)
  179. {
  180. int IAC_num = 0, new_out_len, i, j;
  181. for (i = 0; i < out_len; i++) {
  182. if (outv[i] == IAC) {
  183. IAC_num++;
  184. }
  185. }
  186. if (IAC_num == 0) {
  187. return out_len;
  188. }
  189. new_out_len = out_len + IAC_num;
  190. for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) {
  191. outv[j] = outv[i];
  192. if (outv[i] == IAC) {
  193. outv[--j] = IAC;
  194. }
  195. }
  196. return new_out_len;
  197. }
  198. /*
  199. * Write 3270 outbound to socket.
  200. * Return the count of 3270 data field if succeeded, zero if failed.
  201. */
  202. static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd)
  203. {
  204. Terminal3270 *t = TERMINAL_3270(dev);
  205. int retval = 0;
  206. int count = ccw_dstream_avail(get_cds(t));
  207. int bound = (OUTPUT_BUFFER_SIZE - 3) / 2;
  208. int len = MIN(count, bound);
  209. int out_len = 0;
  210. if (!t->handshake_done) {
  211. if (!(t->outv[0] == IAC && t->outv[1] != IAC)) {
  212. /*
  213. * Before having finished 3270 negotiation,
  214. * sending outbound data except protocol options is prohibited.
  215. */
  216. return 0;
  217. }
  218. }
  219. if (!qemu_chr_fe_backend_connected(&t->chr)) {
  220. /* We just say we consumed all data if there's no backend. */
  221. return count;
  222. }
  223. t->outv[out_len++] = cmd;
  224. do {
  225. ccw_dstream_read_buf(get_cds(t), &t->outv[out_len], len);
  226. count = ccw_dstream_avail(get_cds(t));
  227. out_len += len;
  228. out_len = insert_IAC_escape_char(t->outv, out_len);
  229. if (!count) {
  230. t->outv[out_len++] = IAC;
  231. t->outv[out_len++] = IAC_EOR;
  232. }
  233. retval = qemu_chr_fe_write_all(&t->chr, t->outv, out_len);
  234. len = MIN(count, bound);
  235. out_len = 0;
  236. } while (len && retval >= 0);
  237. return (retval <= 0) ? 0 : get_cds(t)->count;
  238. }
  239. static Property terminal_properties[] = {
  240. DEFINE_PROP_CHR("chardev", Terminal3270, chr),
  241. DEFINE_PROP_END_OF_LIST(),
  242. };
  243. static const VMStateDescription terminal3270_vmstate = {
  244. .name = TYPE_TERMINAL_3270,
  245. .unmigratable = 1,
  246. };
  247. static void terminal_class_init(ObjectClass *klass, void *data)
  248. {
  249. DeviceClass *dc = DEVICE_CLASS(klass);
  250. EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass);
  251. dc->props = terminal_properties;
  252. dc->vmsd = &terminal3270_vmstate;
  253. ck->init = terminal_init;
  254. ck->read_payload_3270 = read_payload_3270;
  255. ck->write_payload_3270 = write_payload_3270;
  256. }
  257. static const TypeInfo ccw_terminal_info = {
  258. .name = TYPE_TERMINAL_3270,
  259. .parent = TYPE_EMULATED_CCW_3270,
  260. .instance_size = sizeof(Terminal3270),
  261. .class_init = terminal_class_init,
  262. .class_size = sizeof(EmulatedCcw3270Class),
  263. };
  264. static void register_types(void)
  265. {
  266. type_register_static(&ccw_terminal_info);
  267. }
  268. type_init(register_types)