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