]>
Commit | Line | Data |
---|---|---|
b8476205 YC |
1 | /* |
2 | * Terminal 3270 implementation | |
3 | * | |
4 | * Copyright 2017 IBM Corp. | |
5 | * | |
6 | * Authors: Yang Chen <[email protected]> | |
7 | * Jing Liu <[email protected]> | |
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" |
4d43a603 | 16 | #include "chardev/char-fe.h" |
b8476205 YC |
17 | #include "hw/s390x/3270-ccw.h" |
18 | ||
2dc95b4c JL |
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 | ||
b8476205 YC |
27 | typedef struct Terminal3270 { |
28 | EmulatedCcw3270Device cdev; | |
2dc95b4c JL |
29 | CharBackend chr; |
30 | uint8_t inv[INPUT_BUFFER_SIZE]; | |
31 | uint8_t outv[OUTPUT_BUFFER_SIZE]; | |
32 | int in_len; | |
2dc95b4c | 33 | bool handshake_done; |
2c716ba1 | 34 | GSource *timer_src; |
b8476205 YC |
35 | } Terminal3270; |
36 | ||
37 | #define TYPE_TERMINAL_3270 "x-terminal3270" | |
2dc95b4c JL |
38 | #define TERMINAL_3270(obj) \ |
39 | OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270) | |
40 | ||
41 | static int terminal_can_read(void *opaque) | |
42 | { | |
43 | Terminal3270 *t = opaque; | |
44 | ||
45 | return INPUT_BUFFER_SIZE - t->in_len; | |
46 | } | |
47 | ||
2c716ba1 PX |
48 | static void terminal_timer_cancel(Terminal3270 *t) |
49 | { | |
50 | if (t->timer_src) { | |
51 | g_source_destroy(t->timer_src); | |
52 | g_source_unref(t->timer_src); | |
53 | t->timer_src = NULL; | |
54 | } | |
55 | } | |
56 | ||
2dc95b4c JL |
57 | /* |
58 | * Protocol handshake done, | |
59 | * signal guest by an unsolicited DE irq. | |
60 | */ | |
61 | static void TN3270_handshake_done(Terminal3270 *t) | |
62 | { | |
63 | CcwDevice *ccw_dev = CCW_DEVICE(t); | |
64 | SubchDev *sch = ccw_dev->sch; | |
65 | ||
66 | t->handshake_done = true; | |
67 | sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END; | |
68 | css_conditional_io_interrupt(sch); | |
69 | } | |
70 | ||
e65a2720 JL |
71 | /* |
72 | * Called when the interval is timeout to detect | |
73 | * if the client is still alive by Timing Mark. | |
74 | */ | |
75 | static gboolean send_timing_mark_cb(gpointer opaque) | |
76 | { | |
77 | Terminal3270 *t = opaque; | |
78 | const uint8_t timing[] = {0xff, 0xfd, 0x06}; | |
79 | ||
80 | qemu_chr_fe_write_all(&t->chr, timing, sizeof(timing)); | |
81 | return true; | |
82 | } | |
83 | ||
2dc95b4c JL |
84 | /* |
85 | * Receive inbound data from socket. | |
86 | * For data given to guest, drop the data boundary IAC, IAC_EOR. | |
87 | * TODO: | |
88 | * Using "Reset" key on x3270 may result multiple commands in one packet. | |
89 | * This usually happens when the user meets a poor traffic of the network. | |
90 | * As of now, for such case, we simply terminate the connection, | |
91 | * and we should come back here later with a better solution. | |
92 | */ | |
93 | static void terminal_read(void *opaque, const uint8_t *buf, int size) | |
94 | { | |
95 | Terminal3270 *t = opaque; | |
96 | CcwDevice *ccw_dev = CCW_DEVICE(t); | |
97 | SubchDev *sch = ccw_dev->sch; | |
98 | int end; | |
99 | ||
100 | assert(size <= (INPUT_BUFFER_SIZE - t->in_len)); | |
101 | ||
2c716ba1 PX |
102 | terminal_timer_cancel(t); |
103 | t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000, | |
104 | 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 | |
4996241a JL |
145 | static void chr_event(void *opaque, int event) |
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 | */ | |
2c716ba1 PX |
163 | t->timer_src = qemu_chr_timeout_add_ms(t->chr.chr, 600 * 1000, |
164 | send_timing_mark_cb, t); | |
4996241a JL |
165 | break; |
166 | case CHR_EVENT_CLOSED: | |
167 | sch->curr_status.scsw.dstat = SCSW_DSTAT_DEVICE_END; | |
168 | css_conditional_io_interrupt(sch); | |
169 | break; | |
170 | } | |
171 | } | |
172 | ||
b8476205 YC |
173 | static void terminal_init(EmulatedCcw3270Device *dev, Error **errp) |
174 | { | |
2dc95b4c | 175 | Terminal3270 *t = TERMINAL_3270(dev); |
b8476205 YC |
176 | static bool terminal_available; |
177 | ||
178 | if (terminal_available) { | |
179 | error_setg(errp, "Multiple 3270 terminals are not supported."); | |
180 | return; | |
181 | } | |
182 | terminal_available = true; | |
2dc95b4c | 183 | qemu_chr_fe_set_handlers(&t->chr, terminal_can_read, |
81517ba3 | 184 | terminal_read, chr_event, NULL, t, NULL, true); |
2dc95b4c JL |
185 | } |
186 | ||
1baa2eb0 HP |
187 | static inline CcwDataStream *get_cds(Terminal3270 *t) |
188 | { | |
189 | return &(CCW_DEVICE(&t->cdev)->sch->cds); | |
190 | } | |
191 | ||
192 | static int read_payload_3270(EmulatedCcw3270Device *dev) | |
2dc95b4c JL |
193 | { |
194 | Terminal3270 *t = TERMINAL_3270(dev); | |
195 | int len; | |
196 | ||
1baa2eb0 HP |
197 | len = MIN(ccw_dstream_avail(get_cds(t)), t->in_len); |
198 | ccw_dstream_write_buf(get_cds(t), t->inv, len); | |
2dc95b4c JL |
199 | t->in_len -= len; |
200 | ||
201 | return len; | |
202 | } | |
203 | ||
204 | /* TN3270 uses binary transmission, which needs escape IAC to IAC IAC */ | |
205 | static int insert_IAC_escape_char(uint8_t *outv, int out_len) | |
206 | { | |
207 | int IAC_num = 0, new_out_len, i, j; | |
208 | ||
209 | for (i = 0; i < out_len; i++) { | |
210 | if (outv[i] == IAC) { | |
211 | IAC_num++; | |
212 | } | |
213 | } | |
214 | if (IAC_num == 0) { | |
215 | return out_len; | |
216 | } | |
217 | new_out_len = out_len + IAC_num; | |
218 | for (i = out_len - 1, j = new_out_len - 1; j > i && i >= 0; i--, j--) { | |
219 | outv[j] = outv[i]; | |
220 | if (outv[i] == IAC) { | |
221 | outv[--j] = IAC; | |
222 | } | |
223 | } | |
224 | return new_out_len; | |
b8476205 YC |
225 | } |
226 | ||
2dc95b4c JL |
227 | /* |
228 | * Write 3270 outbound to socket. | |
229 | * Return the count of 3270 data field if succeeded, zero if failed. | |
230 | */ | |
1baa2eb0 | 231 | static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd) |
2dc95b4c JL |
232 | { |
233 | Terminal3270 *t = TERMINAL_3270(dev); | |
234 | int retval = 0; | |
1baa2eb0 | 235 | int count = ccw_dstream_avail(get_cds(t)); |
17ec9921 HP |
236 | int bound = (OUTPUT_BUFFER_SIZE - 3) / 2; |
237 | int len = MIN(count, bound); | |
238 | int out_len = 0; | |
2dc95b4c JL |
239 | |
240 | if (!t->handshake_done) { | |
e65a2720 JL |
241 | if (!(t->outv[0] == IAC && t->outv[1] != IAC)) { |
242 | /* | |
243 | * Before having finished 3270 negotiation, | |
244 | * sending outbound data except protocol options is prohibited. | |
245 | */ | |
246 | return 0; | |
247 | } | |
2dc95b4c | 248 | } |
30650701 | 249 | if (!qemu_chr_fe_backend_connected(&t->chr)) { |
2dc95b4c JL |
250 | /* We just say we consumed all data if there's no backend. */ |
251 | return count; | |
252 | } | |
2dc95b4c | 253 | |
17ec9921 HP |
254 | t->outv[out_len++] = cmd; |
255 | do { | |
256 | ccw_dstream_read_buf(get_cds(t), &t->outv[out_len], len); | |
257 | count = ccw_dstream_avail(get_cds(t)); | |
258 | out_len += len; | |
2dc95b4c | 259 | |
17ec9921 HP |
260 | out_len = insert_IAC_escape_char(t->outv, out_len); |
261 | if (!count) { | |
262 | t->outv[out_len++] = IAC; | |
263 | t->outv[out_len++] = IAC_EOR; | |
264 | } | |
265 | retval = qemu_chr_fe_write_all(&t->chr, t->outv, out_len); | |
266 | len = MIN(count, bound); | |
267 | out_len = 0; | |
268 | } while (len && retval >= 0); | |
269 | return (retval <= 0) ? 0 : get_cds(t)->count; | |
2dc95b4c JL |
270 | } |
271 | ||
272 | static Property terminal_properties[] = { | |
273 | DEFINE_PROP_CHR("chardev", Terminal3270, chr), | |
274 | DEFINE_PROP_END_OF_LIST(), | |
275 | }; | |
276 | ||
9e8b3009 JL |
277 | static const VMStateDescription terminal3270_vmstate = { |
278 | .name = TYPE_TERMINAL_3270, | |
279 | .unmigratable = 1, | |
280 | }; | |
281 | ||
b8476205 YC |
282 | static void terminal_class_init(ObjectClass *klass, void *data) |
283 | { | |
2dc95b4c | 284 | DeviceClass *dc = DEVICE_CLASS(klass); |
b8476205 YC |
285 | EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass); |
286 | ||
2dc95b4c | 287 | dc->props = terminal_properties; |
9e8b3009 | 288 | dc->vmsd = &terminal3270_vmstate; |
b8476205 | 289 | ck->init = terminal_init; |
2dc95b4c JL |
290 | ck->read_payload_3270 = read_payload_3270; |
291 | ck->write_payload_3270 = write_payload_3270; | |
b8476205 YC |
292 | } |
293 | ||
294 | static const TypeInfo ccw_terminal_info = { | |
295 | .name = TYPE_TERMINAL_3270, | |
296 | .parent = TYPE_EMULATED_CCW_3270, | |
297 | .instance_size = sizeof(Terminal3270), | |
298 | .class_init = terminal_class_init, | |
299 | .class_size = sizeof(EmulatedCcw3270Class), | |
300 | }; | |
301 | ||
302 | static void register_types(void) | |
303 | { | |
304 | type_register_static(&ccw_terminal_info); | |
305 | } | |
306 | ||
307 | type_init(register_types) |