s390x/3270: IDA support for 3270 via CcwDataStream
[qemu/ar7.git] / hw / char / terminal3270.c
blobc976a63cc22252348df27399230bef795c6b461b
1 /*
2 * Terminal 3270 implementation
4 * Copyright 2017 IBM Corp.
6 * Authors: Yang Chen <bjcyang@linux.vnet.ibm.com>
7 * Jing Liu <liujbjl@linux.vnet.ibm.com>
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "chardev/char-fe.h"
17 #include "hw/s390x/3270-ccw.h"
19 /* Enough spaces for different window sizes. */
20 #define INPUT_BUFFER_SIZE 1000
22 * 1 for header, 1024*2 for datastream, 2 for tail
23 * Reserve enough spaces for telnet IAC escape.
25 #define OUTPUT_BUFFER_SIZE 2051
27 typedef struct Terminal3270 {
28 EmulatedCcw3270Device cdev;
29 CharBackend chr;
30 uint8_t inv[INPUT_BUFFER_SIZE];
31 uint8_t outv[OUTPUT_BUFFER_SIZE];
32 int in_len;
33 int out_len;
34 bool handshake_done;
35 guint timer_tag;
36 } Terminal3270;
38 #define TYPE_TERMINAL_3270 "x-terminal3270"
39 #define TERMINAL_3270(obj) \
40 OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270)
42 static int terminal_can_read(void *opaque)
44 Terminal3270 *t = opaque;
46 return INPUT_BUFFER_SIZE - t->in_len;
50 * Protocol handshake done,
51 * signal guest by an unsolicited DE irq.
53 static void TN3270_handshake_done(Terminal3270 *t)
55 CcwDevice *ccw_dev = CCW_DEVICE(t);
56 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);
64 * Called when the interval is timeout to detect
65 * if the client is still alive by Timing Mark.
67 static gboolean send_timing_mark_cb(gpointer opaque)
69 Terminal3270 *t = opaque;
70 const uint8_t timing[] = {0xff, 0xfd, 0x06};
72 qemu_chr_fe_write_all(&t->chr, timing, sizeof(timing));
73 return true;
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.
85 static void terminal_read(void *opaque, const uint8_t *buf, int size)
87 Terminal3270 *t = opaque;
88 CcwDevice *ccw_dev = CCW_DEVICE(t);
89 SubchDev *sch = ccw_dev->sch;
90 int end;
92 assert(size <= (INPUT_BUFFER_SIZE - t->in_len));
94 if (t->timer_tag) {
95 g_source_remove(t->timer_tag);
96 t->timer_tag = 0;
98 t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
100 memcpy(&t->inv[t->in_len], buf, size);
101 t->in_len += size;
102 if (t->in_len < 2) {
103 return;
106 if (!t->handshake_done) {
108 * Receiving Terminal Type is the last step of handshake.
109 * The data format: IAC SB Terminal-Type IS <terminal type> IAC SE
110 * The code for Terminal-Type is 0x18, for IS is 0.
111 * Simply check the data format and mark handshake_done.
113 if (t->in_len > 6 && t->inv[2] == 0x18 && t->inv[3] == 0x0 &&
114 t->inv[t->in_len - 2] == IAC && t->inv[t->in_len - 1] == IAC_SE) {
115 TN3270_handshake_done(t);
116 t->in_len = 0;
118 return;
121 for (end = 0; end < t->in_len - 1; end++) {
122 if (t->inv[end] == IAC && t->inv[end + 1] == IAC_EOR) {
123 break;
126 if (end == t->in_len - 2) {
127 /* Data is valid for consuming. */
128 t->in_len -= 2;
129 sch->curr_status.scsw.dstat = SCSW_DSTAT_ATTENTION;
130 css_conditional_io_interrupt(sch);
131 } else if (end < t->in_len - 2) {
132 /* "Reset" key is used. */
133 qemu_chr_fe_disconnect(&t->chr);
134 } else {
135 /* Gathering data. */
136 return;
140 static void chr_event(void *opaque, int event)
142 Terminal3270 *t = opaque;
143 CcwDevice *ccw_dev = CCW_DEVICE(t);
144 SubchDev *sch = ccw_dev->sch;
146 /* Ensure the initial status correct, always reset them. */
147 t->in_len = 0;
148 t->out_len = 0;
149 t->handshake_done = false;
150 if (t->timer_tag) {
151 g_source_remove(t->timer_tag);
152 t->timer_tag = 0;
155 switch (event) {
156 case CHR_EVENT_OPENED:
158 * 3270 does handshake firstly by the negotiate options in
159 * char-socket.c. Once qemu receives the terminal-type of the
160 * client, mark handshake done and trigger everything rolling again.
162 t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t);
163 break;
164 case CHR_EVENT_CLOSED:
165 sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END;
166 css_conditional_io_interrupt(sch);
167 break;
171 static void terminal_init(EmulatedCcw3270Device *dev, Error **errp)
173 Terminal3270 *t = TERMINAL_3270(dev);
174 static bool terminal_available;
176 if (terminal_available) {
177 error_setg(errp, "Multiple 3270 terminals are not supported.");
178 return;
180 terminal_available = true;
181 qemu_chr_fe_set_handlers(&t->chr, terminal_can_read,
182 terminal_read, chr_event, NULL, t, NULL, true);
185 static inline CcwDataStream *get_cds(Terminal3270 *t)
187 return &(CCW_DEVICE(&t->cdev)->sch->cds);
190 static int read_payload_3270(EmulatedCcw3270Device *dev)
192 Terminal3270 *t = TERMINAL_3270(dev);
193 int len;
195 len = MIN(ccw_dstream_avail(get_cds(t)), t->in_len);
196 ccw_dstream_write_buf(get_cds(t), t->inv, len);
197 t->in_len -= len;
199 return len;
202 /* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */
203 static int insert_IAC_escape_char(uint8_t *outv, int out_len)
205 int IAC_num = 0, new_out_len, i, j;
207 for (i = 0; i < out_len; i++) {
208 if (outv[i] == IAC) {
209 IAC_num++;
212 if (IAC_num == 0) {
213 return out_len;
215 new_out_len = out_len + IAC_num;
216 for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) {
217 outv[j] = outv[i];
218 if (outv[i] == IAC) {
219 outv[--j] = IAC;
222 return new_out_len;
226 * Write 3270 outbound to socket.
227 * Return the count of 3270 data field if succeeded, zero if failed.
229 static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd)
231 Terminal3270 *t = TERMINAL_3270(dev);
232 int retval = 0;
233 int count = ccw_dstream_avail(get_cds(t));
235 assert(count <= (OUTPUT_BUFFER_SIZE - 3) / 2);
237 if (!t->handshake_done) {
238 if (!(t->outv[0] == IAC && t->outv[1] != IAC)) {
240 * Before having finished 3270 negotiation,
241 * sending outbound data except protocol options is prohibited.
243 return 0;
246 if (!qemu_chr_fe_backend_connected(&t->chr)) {
247 /* We just say we consumed all data if there's no backend. */
248 return count;
250 t->outv[0] = cmd;
251 ccw_dstream_read_buf(get_cds(t), &t->outv[1], count);
252 t->out_len = count + 1;
254 t->out_len = insert_IAC_escape_char(t->outv, t->out_len);
255 t->outv[t->out_len++] = IAC;
256 t->outv[t->out_len++] = IAC_EOR;
258 retval = qemu_chr_fe_write_all(&t->chr, t->outv, t->out_len);
259 return (retval <= 0) ? 0 : (retval - 3);
262 static Property terminal_properties[] = {
263 DEFINE_PROP_CHR("chardev", Terminal3270, chr),
264 DEFINE_PROP_END_OF_LIST(),
267 static const VMStateDescription terminal3270_vmstate = {
268 .name = TYPE_TERMINAL_3270,
269 .unmigratable = 1,
272 static void terminal_class_init(ObjectClass *klass, void *data)
274 DeviceClass *dc = DEVICE_CLASS(klass);
275 EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass);
277 dc->props = terminal_properties;
278 dc->vmsd = &terminal3270_vmstate;
279 ck->init = terminal_init;
280 ck->read_payload_3270 = read_payload_3270;
281 ck->write_payload_3270 = write_payload_3270;
284 static const TypeInfo ccw_terminal_info = {
285 .name = TYPE_TERMINAL_3270,
286 .parent = TYPE_EMULATED_CCW_3270,
287 .instance_size = sizeof(Terminal3270),
288 .class_init = terminal_class_init,
289 .class_size = sizeof(EmulatedCcw3270Class),
292 static void register_types(void)
294 type_register_static(&ccw_terminal_info);
297 type_init(register_types)