terminal3270.c 8.7 KB

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