terminal3270.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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, QEMUChrEvent 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. case CHR_EVENT_BREAK:
  151. case CHR_EVENT_MUX_IN:
  152. case CHR_EVENT_MUX_OUT:
  153. /* Ignore */
  154. break;
  155. }
  156. }
  157. static void terminal_init(EmulatedCcw3270Device *dev, Error **errp)
  158. {
  159. Terminal3270 *t = TERMINAL_3270(dev);
  160. static bool terminal_available;
  161. if (terminal_available) {
  162. error_setg(errp, "Multiple 3270 terminals are not supported.");
  163. return;
  164. }
  165. terminal_available = true;
  166. qemu_chr_fe_set_handlers(&t->chr, terminal_can_read,
  167. terminal_read, chr_event, NULL, t, NULL, true);
  168. }
  169. static inline CcwDataStream *get_cds(Terminal3270 *t)
  170. {
  171. return &(CCW_DEVICE(&t->cdev)->sch->cds);
  172. }
  173. static int read_payload_3270(EmulatedCcw3270Device *dev)
  174. {
  175. Terminal3270 *t = TERMINAL_3270(dev);
  176. int len;
  177. len = MIN(ccw_dstream_avail(get_cds(t)), t->in_len);
  178. ccw_dstream_write_buf(get_cds(t), t->inv, len);
  179. t->in_len -= len;
  180. return len;
  181. }
  182. /* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */
  183. static int insert_IAC_escape_char(uint8_t *outv, int out_len)
  184. {
  185. int IAC_num = 0, new_out_len, i, j;
  186. for (i = 0; i < out_len; i++) {
  187. if (outv[i] == IAC) {
  188. IAC_num++;
  189. }
  190. }
  191. if (IAC_num == 0) {
  192. return out_len;
  193. }
  194. new_out_len = out_len + IAC_num;
  195. for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) {
  196. outv[j] = outv[i];
  197. if (outv[i] == IAC) {
  198. outv[--j] = IAC;
  199. }
  200. }
  201. return new_out_len;
  202. }
  203. /*
  204. * Write 3270 outbound to socket.
  205. * Return the count of 3270 data field if succeeded, zero if failed.
  206. */
  207. static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd)
  208. {
  209. Terminal3270 *t = TERMINAL_3270(dev);
  210. int retval = 0;
  211. int count = ccw_dstream_avail(get_cds(t));
  212. int bound = (OUTPUT_BUFFER_SIZE - 3) / 2;
  213. int len = MIN(count, bound);
  214. int out_len = 0;
  215. if (!t->handshake_done) {
  216. if (!(t->outv[0] == IAC && t->outv[1] != IAC)) {
  217. /*
  218. * Before having finished 3270 negotiation,
  219. * sending outbound data except protocol options is prohibited.
  220. */
  221. return 0;
  222. }
  223. }
  224. if (!qemu_chr_fe_backend_connected(&t->chr)) {
  225. /* We just say we consumed all data if there's no backend. */
  226. return count;
  227. }
  228. t->outv[out_len++] = cmd;
  229. do {
  230. ccw_dstream_read_buf(get_cds(t), &t->outv[out_len], len);
  231. count = ccw_dstream_avail(get_cds(t));
  232. out_len += len;
  233. out_len = insert_IAC_escape_char(t->outv, out_len);
  234. if (!count) {
  235. t->outv[out_len++] = IAC;
  236. t->outv[out_len++] = IAC_EOR;
  237. }
  238. retval = qemu_chr_fe_write_all(&t->chr, t->outv, out_len);
  239. len = MIN(count, bound);
  240. out_len = 0;
  241. } while (len && retval >= 0);
  242. return (retval <= 0) ? 0 : get_cds(t)->count;
  243. }
  244. static Property terminal_properties[] = {
  245. DEFINE_PROP_CHR("chardev", Terminal3270, chr),
  246. DEFINE_PROP_END_OF_LIST(),
  247. };
  248. static const VMStateDescription terminal3270_vmstate = {
  249. .name = TYPE_TERMINAL_3270,
  250. .unmigratable = 1,
  251. };
  252. static void terminal_class_init(ObjectClass *klass, void *data)
  253. {
  254. DeviceClass *dc = DEVICE_CLASS(klass);
  255. EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass);
  256. device_class_set_props(dc, terminal_properties);
  257. dc->vmsd = &terminal3270_vmstate;
  258. ck->init = terminal_init;
  259. ck->read_payload_3270 = read_payload_3270;
  260. ck->write_payload_3270 = write_payload_3270;
  261. }
  262. static const TypeInfo ccw_terminal_info = {
  263. .name = TYPE_TERMINAL_3270,
  264. .parent = TYPE_EMULATED_CCW_3270,
  265. .instance_size = sizeof(Terminal3270),
  266. .class_init = terminal_class_init,
  267. .class_size = sizeof(EmulatedCcw3270Class),
  268. };
  269. static void register_types(void)
  270. {
  271. type_register_static(&ccw_terminal_info);
  272. }
  273. type_init(register_types)