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" 15 #include "qapi/error.h" 16 #include "qemu/module.h" 17 #include "chardev/char-fe.h" 18 #include "hw/s390x/3270-ccw.h" 19 20 /* Enough spaces for different window sizes. */ 21 #define INPUT_BUFFER_SIZE 1000 22 /* 23 * 1 for header, 1024*2 for datastream, 2 for tail 24 * Reserve enough spaces for telnet IAC escape. 25 */ 26 #define OUTPUT_BUFFER_SIZE 2051 27 28 typedef 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 } Terminal3270; 37 38 #define TYPE_TERMINAL_3270 "x-terminal3270" 39 #define TERMINAL_3270(obj) \ 40 OBJECT_CHECK(Terminal3270, (obj), TYPE_TERMINAL_3270) 41 42 static int terminal_can_read(void *opaque) 43 { 44 Terminal3270 *t = opaque; 45 46 return INPUT_BUFFER_SIZE - t->in_len; 47 } 48 49 static void terminal_timer_cancel(Terminal3270 *t) 50 { 51 if (t->timer_tag) { 52 g_source_remove(t->timer_tag); 53 t->timer_tag = 0; 54 } 55 } 56 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 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 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 102 terminal_timer_cancel(t); 103 t->timer_tag = g_timeout_add_seconds(600, send_timing_mark_cb, t); 104 memcpy(&t->inv[t->in_len], buf, size); 105 t->in_len += size; 106 if (t->in_len < 2) { 107 return; 108 } 109 110 if (!t->handshake_done) { 111 /* 112 * Receiving Terminal Type is the last step of handshake. 113 * The data format: IAC SB Terminal-Type IS <terminal type> IAC SE 114 * The code for Terminal-Type is 0x18, for IS is 0. 115 * Simply check the data format and mark handshake_done. 116 */ 117 if (t->in_len > 6 && t->inv[2] == 0x18 && t->inv[3] == 0x0 && 118 t->inv[t->in_len - 2] == IAC && t->inv[t->in_len - 1] == IAC_SE) { 119 TN3270_handshake_done(t); 120 t->in_len = 0; 121 } 122 return; 123 } 124 125 for (end = 0; end < t->in_len - 1; end++) { 126 if (t->inv[end] == IAC && t->inv[end + 1] == IAC_EOR) { 127 break; 128 } 129 } 130 if (end == t->in_len - 2) { 131 /* Data is valid for consuming. */ 132 t->in_len -= 2; 133 sch->curr_status.scsw.dstat = SCSW_DSTAT_ATTENTION; 134 css_conditional_io_interrupt(sch); 135 } else if (end < t->in_len - 2) { 136 /* "Reset" key is used. */ 137 qemu_chr_fe_disconnect(&t->chr); 138 } else { 139 /* Gathering data. */ 140 return; 141 } 142 } 143 144 static void chr_event(void *opaque, int event) 145 { 146 Terminal3270 *t = opaque; 147 CcwDevice *ccw_dev = CCW_DEVICE(t); 148 SubchDev *sch = ccw_dev->sch; 149 150 /* Ensure the initial status correct, always reset them. */ 151 t->in_len = 0; 152 t->handshake_done = false; 153 terminal_timer_cancel(t); 154 155 switch (event) { 156 case CHR_EVENT_OPENED: 157 /* 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. 161 */ 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; 168 } 169 } 170 171 static void terminal_init(EmulatedCcw3270Device *dev, Error **errp) 172 { 173 Terminal3270 *t = TERMINAL_3270(dev); 174 static bool terminal_available; 175 176 if (terminal_available) { 177 error_setg(errp, "Multiple 3270 terminals are not supported."); 178 return; 179 } 180 terminal_available = true; 181 qemu_chr_fe_set_handlers(&t->chr, terminal_can_read, 182 terminal_read, chr_event, NULL, t, NULL, true); 183 } 184 185 static inline CcwDataStream *get_cds(Terminal3270 *t) 186 { 187 return &(CCW_DEVICE(&t->cdev)->sch->cds); 188 } 189 190 static int read_payload_3270(EmulatedCcw3270Device *dev) 191 { 192 Terminal3270 *t = TERMINAL_3270(dev); 193 int len; 194 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; 198 199 return len; 200 } 201 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) 204 { 205 int IAC_num = 0, new_out_len, i, j; 206 207 for (i = 0; i < out_len; i++) { 208 if (outv[i] == IAC) { 209 IAC_num++; 210 } 211 } 212 if (IAC_num == 0) { 213 return out_len; 214 } 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; 220 } 221 } 222 return new_out_len; 223 } 224 225 /* 226 * Write 3270 outbound to socket. 227 * Return the count of 3270 data field if succeeded, zero if failed. 228 */ 229 static int write_payload_3270(EmulatedCcw3270Device *dev, uint8_t cmd) 230 { 231 Terminal3270 *t = TERMINAL_3270(dev); 232 int retval = 0; 233 int count = ccw_dstream_avail(get_cds(t)); 234 int bound = (OUTPUT_BUFFER_SIZE - 3) / 2; 235 int len = MIN(count, bound); 236 int out_len = 0; 237 238 if (!t->handshake_done) { 239 if (!(t->outv[0] == IAC && t->outv[1] != IAC)) { 240 /* 241 * Before having finished 3270 negotiation, 242 * sending outbound data except protocol options is prohibited. 243 */ 244 return 0; 245 } 246 } 247 if (!qemu_chr_fe_backend_connected(&t->chr)) { 248 /* We just say we consumed all data if there's no backend. */ 249 return count; 250 } 251 252 t->outv[out_len++] = cmd; 253 do { 254 ccw_dstream_read_buf(get_cds(t), &t->outv[out_len], len); 255 count = ccw_dstream_avail(get_cds(t)); 256 out_len += len; 257 258 out_len = insert_IAC_escape_char(t->outv, out_len); 259 if (!count) { 260 t->outv[out_len++] = IAC; 261 t->outv[out_len++] = IAC_EOR; 262 } 263 retval = qemu_chr_fe_write_all(&t->chr, t->outv, out_len); 264 len = MIN(count, bound); 265 out_len = 0; 266 } while (len && retval >= 0); 267 return (retval <= 0) ? 0 : get_cds(t)->count; 268 } 269 270 static Property terminal_properties[] = { 271 DEFINE_PROP_CHR("chardev", Terminal3270, chr), 272 DEFINE_PROP_END_OF_LIST(), 273 }; 274 275 static const VMStateDescription terminal3270_vmstate = { 276 .name = TYPE_TERMINAL_3270, 277 .unmigratable = 1, 278 }; 279 280 static void terminal_class_init(ObjectClass *klass, void *data) 281 { 282 DeviceClass *dc = DEVICE_CLASS(klass); 283 EmulatedCcw3270Class *ck = EMULATED_CCW_3270_CLASS(klass); 284 285 dc->props = terminal_properties; 286 dc->vmsd = &terminal3270_vmstate; 287 ck->init = terminal_init; 288 ck->read_payload_3270 = read_payload_3270; 289 ck->write_payload_3270 = write_payload_3270; 290 } 291 292 static const TypeInfo ccw_terminal_info = { 293 .name = TYPE_TERMINAL_3270, 294 .parent = TYPE_EMULATED_CCW_3270, 295 .instance_size = sizeof(Terminal3270), 296 .class_init = terminal_class_init, 297 .class_size = sizeof(EmulatedCcw3270Class), 298 }; 299 300 static void register_types(void) 301 { 302 type_register_static(&ccw_terminal_info); 303 } 304 305 type_init(register_types) 306