Debian Codegraph @

common/dev_vtty.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * Virtual console TTY.
6 *
7 * "Interactive" part idea by Mtve.
8 * TCP console added by Mtve.
9 * Serial console by Peter Ross ([email protected])
10 */
11
12 #include "dynamips_common.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/tcp.h>
21 #include <termios.h>
22 #include <netdb.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <assert.h>
26
27 #include <arpa/telnet.h>
28 #include <arpa/inet.h>
29
30 #include "utils.h"
31 #include "cpu.h"
32 #include "vm.h"
33 #include "dynamips.h"
34 #include "mips64_exec.h"
35 #include "ppc32_exec.h"
36 #include "device.h"
37 #include "memory.h"
38 #include "dev_vtty.h"
39
40 #ifdef USE_UNSTABLE
41 #include "tcb.h"
42 #endif
43
44 #ifndef SOL_TCP
45 #define SOL_TCP 6
46 #endif
47
48 /* VTTY list */
49 static pthread_mutex_t vtty_list_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static vtty_t *vtty_list = NULL;
51 static pthread_t vtty_thread;
52
53 #define VTTY_LIST_LOCK() pthread_mutex_lock(&vtty_list_mutex);
54 #define VTTY_LIST_UNLOCK() pthread_mutex_unlock(&vtty_list_mutex);
55
56 static struct termios tios,tios_orig;
57
58 static int ctrl_code_ok = 1;
59 static int telnet_message_ok = 1;
60
61 /* Allow the user to disable the CTRL code for the monitor interface */
62 void vtty_set_ctrlhandler(int n)
63 {
64 ctrl_code_ok = n;
65 }
66 /* Allow the user to disable the telnet message for AUX and CONSOLE */
67 void vtty_set_telnetmsg(int n)
68 {
69 telnet_message_ok = n;
70 }
71
72 /* Send Telnet command: WILL TELOPT_ECHO */
73 static void vtty_telnet_will_echo(int fd)
74 {
75 u_char cmd[] = { IAC, WILL, TELOPT_ECHO };
76 write(fd,cmd,sizeof(cmd));
77 }
78
79 /* Send Telnet command: Suppress Go-Ahead */
80 static void vtty_telnet_will_suppress_go_ahead(int fd)
81 {
82 u_char cmd[] = { IAC, WILL, TELOPT_SGA };
83 write(fd,cmd,sizeof(cmd));
84 }
85
86 /* Send Telnet command: Don't use linemode */
87 static void vtty_telnet_dont_linemode(int fd)
88 {
89 u_char cmd[] = { IAC, DONT, TELOPT_LINEMODE };
90 write(fd,cmd,sizeof(cmd));
91 }
92
93 /* Send Telnet command: does the client support terminal type message? */
94 static void vtty_telnet_do_ttype(int fd)
95 {
96 u_char cmd[] = { IAC, DO, TELOPT_TTYPE };
97 write(fd,cmd,sizeof(cmd));
98 }
99
100 /* Restore TTY original settings */
101 static void vtty_term_reset(void)
102 {
103 tcsetattr(STDIN_FILENO,TCSANOW,&tios_orig);
104 }
105
106 /* Initialize real TTY */
107 static void vtty_term_init(void)
108 {
109 tcgetattr(STDIN_FILENO,&tios);
110
111 memcpy(&tios_orig,&tios,sizeof(struct termios));
112 atexit(vtty_term_reset);
113
114 tios.c_cc[VTIME] = 0;
115 tios.c_cc[VMIN] = 1;
116
117 /* Disable Ctrl-C, Ctrl-S, Ctrl-Q and Ctrl-Z */
118 tios.c_cc[VINTR] = 0;
119 tios.c_cc[VSTART] = 0;
120 tios.c_cc[VSTOP] = 0;
121 tios.c_cc[VSUSP] = 0;
122
123 tios.c_lflag &= ~(ICANON|ECHO);
124 tios.c_iflag &= ~ICRNL;
125 tcsetattr(STDIN_FILENO, TCSANOW, &tios);
126 tcflush(STDIN_FILENO,TCIFLUSH);
127 }
128
129 #if HAS_RFC2553
130 /* Wait for a TCP connection */
131 static int vtty_tcp_conn_wait(vtty_t *vtty)
132 {
133 struct addrinfo hints,*res,*res0;
134 char port_str[20],*addr,*proto;
135 int i, nsock;
136 int one = 1;
137
138 for(i=0;i<VTTY_MAX_FD;i++)
139 vtty->fd_array[i] = -1;
140
141 memset(&hints,0,sizeof(hints));
142 hints.ai_family = PF_UNSPEC;
143 hints.ai_socktype = SOCK_STREAM;
144 hints.ai_flags = AI_PASSIVE;
145
146 snprintf(port_str,sizeof(port_str),"%d",vtty->tcp_port);
147
148 /* Try to use the console binding address first, then fallback to the global binding address */
149 addr = (console_binding_addr && strlen(console_binding_addr)) ? console_binding_addr : NULL;
150 if (addr == NULL)
151 addr = (binding_addr && strlen(binding_addr)) ? binding_addr : "127.0.0.1";
152
153 if (getaddrinfo(addr,port_str,&hints,&res0) != 0) {
154 perror("vtty_tcp_waitcon: getaddrinfo");
155 return(-1);
156 }
157
158 nsock = 0;
159 for (res=res0;(res && (nsock < VTTY_MAX_FD));res=res->ai_next)
160 {
161 if ((res->ai_family != PF_INET) && (res->ai_family != PF_INET6))
162 continue;
163
164 vtty->fd_array[nsock] = socket(res->ai_family,res->ai_socktype,
165 res->ai_protocol);
166
167 if (vtty->fd_array[nsock] < 0)
168 continue;
169
170 if (setsockopt(vtty->fd_array[nsock],SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one)) < 0)
171 perror("vtty_tcp_waitcon: setsockopt(SO_REUSEADDR)");
172
173 if (setsockopt(vtty->fd_array[nsock],SOL_SOCKET,SO_KEEPALIVE,&one,sizeof(one)) < 0)
174 perror("vtty_tcp_waitcon: setsockopt(SO_KEEPALIVE)");
175
176 // Send telnet packets asap. Dont wait to fill packets up
177 if (setsockopt(vtty->fd_array[nsock],SOL_TCP,TCP_NODELAY, &one,sizeof(one)) < 0)
178 perror("vtty_tcp_waitcon: setsockopt(TCP_NODELAY)");
179
180 if ((bind(vtty->fd_array[nsock],res->ai_addr,res->ai_addrlen) < 0) ||
181 (listen(vtty->fd_array[nsock],1) < 0))
182 {
183 close(vtty->fd_array[nsock]);
184 vtty->fd_array[nsock] = -1;
185 continue;
186 }
187
188 proto = (res->ai_family == PF_INET6) ? "IPv6" : "IPv4";
189 vm_log(vtty->vm,"VTTY","%s: waiting connection on tcp port %d for protocol %s (FD %d)\n",
190 vtty->name,vtty->tcp_port,proto,vtty->fd_array[nsock]);
191
192 nsock++;
193 }
194
195 freeaddrinfo(res0);
196 return(nsock);
197 }
198 #else
199 /* Wait for a TCP connection */
200 static int vtty_tcp_conn_wait(vtty_t *vtty)
201 {
202 struct sockaddr_in serv;
203 int i;
204 int one = 1;
205
206 for(i=0;i<VTTY_MAX_FD;i++)
207 vtty->fd_array[i] = -1;
208
209 if ((vtty->fd_array[0] = socket(PF_INET,SOCK_STREAM,0)) < 0) {
210 perror("vtty_tcp_waitcon: socket");
211 return(-1);
212 }
213
214 if (setsockopt(vtty->fd_array[0],SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one)) < 0) {
215 perror("vtty_tcp_waitcon: setsockopt(SO_REUSEADDR)");
216 goto error;
217 }
218
219 if (setsockopt(vtty->fd_array[0],SOL_SOCKET,SO_KEEPALIVE,&one,sizeof(one)) < 0) {
220 perror("vtty_tcp_waitcon: setsockopt(SO_KEEPALIVE)");
221 goto error;
222 }
223
224 // Send telnet packets asap. Dont wait to fill packets up
225 if (setsockopt(vtty->fd_array[0],SOL_TCP,TCP_NODELAY,&one,sizeof(one)) < 0)
226 {
227 perror("vtty_tcp_waitcon: setsockopt(TCP_NODELAY)");
228 goto error;
229 }
230
231 memset(&serv,0,sizeof(serv));
232 serv.sin_family = AF_INET;
233 serv.sin_addr.s_addr = htonl(INADDR_ANY);
234 serv.sin_port = htons(vtty->tcp_port);
235
236 if (bind(vtty->fd_array[0],(struct sockaddr *)&serv,sizeof(serv)) < 0) {
237 perror("vtty_tcp_waitcon: bind");
238 goto error;
239 }
240
241 if (listen(vtty->fd_array[0],1) < 0) {
242 perror("vtty_tcp_waitcon: listen");
243 goto error;
244 }
245
246 vm_log(vtty->vm,"VTTY","%s: waiting connection on tcp port %d (FD %d)\n",
247 vtty->name,vtty->tcp_port,vtty->fd_array[0]);
248
249 return(1);
250
251 error:
252 close(vtty->fd_array[0]);
253 vtty->fd_array[0] = -1;
254 return(-1);
255 }
256 #endif
257
258 /* Accept a TCP connection */
259 static int vtty_tcp_conn_accept(vtty_t *vtty, int nsock)
260 {
261 int fd,*fd_slot;
262 u_int i;
263
264 if (fd_pool_get_free_slot(&vtty->fd_pool,&fd_slot) < 0) {
265 vm_error(vtty->vm,"unable to create a new VTTY TCP connection\n");
266 return(-1);
267 }
268
269 if ((fd = accept(vtty->fd_array[nsock],NULL,NULL)) < 0) {
270 vm_error(vtty->vm,"vtty_tcp_conn_accept: accept on port %d failed %s\n",
271 vtty->tcp_port,strerror(errno));
272 return(-1);
273 }
274
275 /* Register the new FD */
276 *fd_slot = fd;
277
278 vm_log(vtty->vm,"VTTY","%s is now connected (accept_fd=%d,conn_fd=%d)\n",
279 vtty->name,vtty->fd_array[nsock],fd);
280
281 /* Adapt Telnet settings */
282 if (vtty->terminal_support) {
283 vtty_telnet_do_ttype(fd);
284 vtty_telnet_will_echo(fd);
285 vtty_telnet_will_suppress_go_ahead(fd);
286 vtty_telnet_dont_linemode(fd);
287 vtty->input_state = VTTY_INPUT_TEXT;
288 }
289
290 if (telnet_message_ok == 1) {
291 fd_printf(fd,0,
292 "Connected to Dynamips VM \"%s\" (ID %u, type %s) - %s\r\n"
293 "Press ENTER to get the prompt.\r\n",
294 vtty->vm->name, vtty->vm->instance_id, vm_get_type(vtty->vm),
295 vtty->name);
296 /* replay old text */
297 for (i = vtty->replay_ptr; i < VTTY_BUFFER_SIZE; i++) {
298 if (vtty->replay_buffer[i] != 0) {
299 send(fd,&vtty->replay_buffer[i],VTTY_BUFFER_SIZE-i,0);
300 break;
301 }
302 }
303 for (i = 0; i < vtty->replay_ptr; i++) {
304 if (vtty->replay_buffer[i] != 0) {
305 send(fd,&vtty->replay_buffer[i],vtty->replay_ptr-i,0);
306 break;
307 }
308 }
309 /* warn if not running */
310 if (vtty->vm->status != VM_STATUS_RUNNING)
311 fd_printf(fd,0,"\r\n!!! WARNING - VM is not running, will be unresponsive (status=%d) !!!\r\n",vtty->vm->status);
312 vtty_flush(vtty);
313 }
314 return(0);
315 }
316
317 /*
318 * Parse serial interface descriptor string, return 0 if success
319 * string takes the form "device:baudrate:databits:parity:stopbits:hwflow"
320 * device is mandatory, other options are optional (default=9600,8,N,1,0).
321 */
322 int vtty_parse_serial_option(vtty_serial_option_t *option, char *optarg)
323 {
324 char *array[6];
325 int count;
326
327 if ((count = m_strtok(optarg, ':', array, 6)) < 1) {
328 fprintf(stderr,"vtty_parse_serial_option: invalid string\n");
329 return(-1);
330 }
331
332 if (!(option->device = strdup(array[0]))) {
333 fprintf(stderr,"vtty_parse_serial_option: unable to copy string\n");
334 return(-1);
335 }
336
337 option->baudrate = (count>1) ? atoi(array[1]) : 9600;
338 option->databits = (count>2) ? atoi(array[2]) : 8;
339
340 if (count > 3) {
341 switch(*array[3]) {
342 case 'o':
343 case 'O':
344 option->parity = 1; /* odd */
345 case 'e':
346 case 'E':
347 option->parity = 2; /* even */
348 default:
349 option->parity = 0; /* none */
350 }
351 } else {
352 option->parity = 0;
353 }
354
355 option->stopbits = (count>4) ? atoi(array[4]) : 1;
356 option->hwflow = (count>5) ? atoi(array[5]) : 0;
357 return(0);
358 }
359
360 #if defined(__CYGWIN__) || defined(SUNOS)
361 void cfmakeraw(struct termios *termios_p) {
362 termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|
363 INLCR|IGNCR|ICRNL|IXON);
364 termios_p->c_oflag &= ~OPOST;
365 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
366 termios_p->c_cflag &= ~(CSIZE|PARENB);
367 termios_p->c_cflag |= CS8;
368 }
369 #endif
370
371 /*
372 * Setup serial port, return 0 if success.
373 */
374 static int vtty_serial_setup(vtty_t *vtty, const vtty_serial_option_t *option)
375 {
376 struct termios tio;
377 int tio_baudrate;
378
379 if (tcgetattr(vtty->fd_array[0], &tio) != 0) {
380 fprintf(stderr, "error: tcgetattr failed\n");
381 return(-1);
382 }
383
384 cfmakeraw(&tio);
385
386 tio.c_cflag = 0
387 |CLOCAL // ignore modem control lines
388 ;
389
390 tio.c_cflag &= ~CREAD;
391 tio.c_cflag |= CREAD;
392
393 switch(option->baudrate) {
394 case 50 : tio_baudrate = B50; break;
395 case 75 : tio_baudrate = B75; break;
396 case 110 : tio_baudrate = B110; break;
397 case 134 : tio_baudrate = B134; break;
398 case 150 : tio_baudrate = B150; break;
399 case 200 : tio_baudrate = B200; break;
400 case 300 : tio_baudrate = B300; break;
401 case 600 : tio_baudrate = B600; break;
402 case 1200 : tio_baudrate = B1200; break;
403 case 1800 : tio_baudrate = B1800; break;
404 case 2400 : tio_baudrate = B2400; break;
405 case 4800 : tio_baudrate = B4800; break;
406 case 9600 : tio_baudrate = B9600; break;
407 case 19200 : tio_baudrate = B19200; break;
408 case 38400 : tio_baudrate = B38400; break;
409 case 57600 : tio_baudrate = B57600; break;
410 #if defined(B76800)
411 case 76800 : tio_baudrate = B76800; break;
412 #endif
413 case 115200 : tio_baudrate = B115200; break;
414 #if defined(B230400)
415 case 230400 : tio_baudrate = B230400; break;
416 #endif
417 default:
418 fprintf(stderr, "error: unsupported baudrate\n");
419 return(-1);
420 }
421
422 cfsetospeed(&tio, tio_baudrate);
423 cfsetispeed(&tio, tio_baudrate);
424
425 tio.c_cflag &= ~CSIZE; /* clear size flag */
426 switch(option->databits) {
427 case 5 : tio.c_cflag |= CS5; break;
428 case 6 : tio.c_cflag |= CS6; break;
429 case 7 : tio.c_cflag |= CS7; break;
430 case 8 : tio.c_cflag |= CS8; break;
431 default :
432 fprintf(stderr, "error: unsupported databits\n");
433 return(-1);
434 }
435
436 tio.c_iflag &= ~INPCK; /* clear parity flag */
437 tio.c_cflag &= ~(PARENB|PARODD);
438 switch(option->parity) {
439 case 0 : break;
440 case 2 : tio.c_iflag|=INPCK; tio.c_cflag|=PARENB; break; /* even */
441 case 1 : tio.c_iflag|=INPCK; tio.c_cflag|=PARENB|PARODD; break; /* odd */
442 default:
443 fprintf(stderr, "error: unsupported parity\n");
444 return(-1);
445 }
446
447 tio.c_cflag &= ~CSTOPB; /* clear stop flag */
448 switch(option->stopbits) {
449 case 1 : break;
450 case 2 : tio.c_cflag |= CSTOPB; break;
451 default :
452 fprintf(stderr, "error: unsupported stopbits\n");
453 return(-1);
454 }
455
456 #if defined(CRTSCTS)
457 tio.c_cflag &= ~CRTSCTS;
458 #endif
459 #if defined(CNEW_RTSCTS)
460 tio.c_cflag &= ~CNEW_RTSCTS;
461 #endif
462 if (option->hwflow) {
463 #if defined(CRTSCTS)
464 tio.c_cflag |= CRTSCTS;
465 #else
466 tio.c_cflag |= CNEW_RTSCTS;
467 #endif
468 }
469
470 tio.c_cc[VTIME] = 0;
471 tio.c_cc[VMIN] = 1; /* block read() until one character is available */
472
473 #if 0
474 /* not neccessary unless O_NONBLOCK used */
475 if (fcntl(vtty->fd_array[0], F_SETFL, 0) != 0) { /* enable blocking mode */
476 fprintf(stderr, "error: fnctl F_SETFL failed\n");
477 return(-1);
478 }
479 #endif
480
481 if (tcflush(vtty->fd_array[0], TCIOFLUSH) != 0) {
482 fprintf(stderr, "error: tcflush failed\n");
483 return(-1);
484 }
485
486 if (tcsetattr(vtty->fd_array[0], TCSANOW, &tio) != 0 ) {
487 fprintf(stderr, "error: tcsetattr failed\n");
488 return(-1);
489 }
490
491 return(0);
492 }
493
494 /* Create a virtual tty */
495 vtty_t *vtty_create(vm_instance_t *vm,char *name,int type,int tcp_port,
496 const vtty_serial_option_t *option)
497 {
498 vtty_t *vtty;
499 int i;
500
501 if (!(vtty = malloc(sizeof(*vtty)))) {
502 fprintf(stderr,"VTTY: unable to create new virtual tty.\n");
503 return NULL;
504 }
505 memset(vtty,0,sizeof(*vtty));
506 vtty->name = name;
507 vtty->type = type;
508 vtty->vm = vm;
509 vtty->fd_count = 0;
510 pthread_mutex_init(&vtty->lock,NULL);
511 vtty->terminal_support = 1;
512 vtty->input_state = VTTY_INPUT_TEXT;
513 fd_pool_init(&vtty->fd_pool);
514 for(i=0;i<VTTY_MAX_FD;i++)
515 vtty->fd_array[i] = -1;
516
517 switch (vtty->type) {
518 case VTTY_TYPE_NONE:
519 break;
520
521 case VTTY_TYPE_TERM:
522 vtty_term_init();
523 vtty->fd_array[0] = STDIN_FILENO;
524 break;
525
526 case VTTY_TYPE_TCP:
527 vtty->tcp_port = tcp_port;
528 vtty->fd_count = vtty_tcp_conn_wait(vtty);
529 break;
530
531 case VTTY_TYPE_SERIAL:
532 vtty->fd_array[0] = open(option->device, O_RDWR);
533 if (vtty->fd_array[0] < 0) {
534 fprintf(stderr,"VTTY: open failed\n");
535 free(vtty);
536 return NULL;
537 }
538 if (vtty_serial_setup(vtty,option)) {
539 fprintf(stderr,"VTTY: setup failed\n");
540 close(vtty->fd_array[0]);
541 free(vtty);
542 return NULL;
543 }
544 vtty->terminal_support = 0;
545 break;
546
547 default:
548 fprintf(stderr,"tty_create: bad vtty type %d\n",vtty->type);
549 return NULL;
550 }
551
552 /* Add this new VTTY to the list */
553 VTTY_LIST_LOCK();
554 vtty->next = vtty_list;
555 vtty->pprev = &vtty_list;
556
557 if (vtty_list != NULL)
558 vtty_list->pprev = &vtty->next;
559
560 vtty_list = vtty;
561 VTTY_LIST_UNLOCK();
562 return vtty;
563 }
564
565 /* Delete a virtual tty */
566 void vtty_delete(vtty_t *vtty)
567 {
568 int i;
569
570 if (vtty != NULL) {
571 if (vtty->pprev != NULL) {
572 VTTY_LIST_LOCK();
573 if (vtty->next)
574 vtty->next->pprev = vtty->pprev;
575 *(vtty->pprev) = vtty->next;
576 VTTY_LIST_UNLOCK();
577 }
578
579 switch(vtty->type) {
580 case VTTY_TYPE_TCP:
581
582 for(i=0;i<vtty->fd_count;i++)
583 if (vtty->fd_array[i] != -1) {
584 vm_log(vtty->vm,"VTTY","%s: closing FD %d\n",vtty->name,vtty->fd_array[i]);
585 close(vtty->fd_array[i]);
586 }
587
588 fd_pool_free(&vtty->fd_pool);
589 vtty->fd_count = 0;
590 break;
591
592 default:
593
594 /* We don't close FD 0 since it is stdin */
595 if (vtty->fd_array[0] > 0) {
596 vm_log(vtty->vm,"VTTY","%s: closing FD %d\n",vtty->name,vtty->fd_array[0]);
597 close(vtty->fd_array[0]);
598 }
599 }
600 free(vtty);
601 }
602 }
603
604 /* Store a character in the FIFO buffer */
605 static int vtty_store(vtty_t *vtty,u_char c)
606 {
607 u_int nwptr;
608
609 VTTY_LOCK(vtty);
610 nwptr = vtty->write_ptr + 1;
611 if (nwptr == VTTY_BUFFER_SIZE)
612 nwptr = 0;
613
614 if (nwptr == vtty->read_ptr) {
615 VTTY_UNLOCK(vtty);
616 return(-1);
617 }
618
619 vtty->buffer[vtty->write_ptr] = c;
620 vtty->write_ptr = nwptr;
621 VTTY_UNLOCK(vtty);
622 return(0);
623 }
624
625 /* Store arbritary data in the FIFO buffer */
626 int vtty_store_data(vtty_t *vtty,char *data, int len)
627 {
628 int bytes;
629
630 if (!vtty || !data || len < 0)
631 return(-1); // invalid argument
632
633 for (bytes = 0; bytes < len; bytes++) {
634 if (vtty_store(vtty,data[bytes]) == -1)
635 break;
636 }
637
638 vtty->input_pending = TRUE;
639 return(bytes);
640 }
641
642 /* Store CTRL+C in buffer */
643 int vtty_store_ctrlc(vtty_t *vtty)
644 {
645 if (vtty)
646 vtty_store(vtty,0x03);
647 return(0);
648 }
649
650 /*
651 * Read a character from the terminal.
652 */
653 static int vtty_term_read(vtty_t *vtty)
654 {
655 u_char c;
656
657 if (read(vtty->fd_array[0],&c,1) == 1)
658 return(c);
659
660 perror("read from vtty failed");
661 return(-1);
662 }
663
664 /*
665 * Read a character from the TCP connection.
666 */
667 static int vtty_tcp_read(vtty_t *vtty,int *fd_slot)
668 {
669 int fd = *fd_slot;
670 u_char c;
671
672 if (read(fd,&c,1) == 1)
673 return(c);
674
675 /* problem with the connection */
676 shutdown(fd,2);
677 close(fd);
678 *fd_slot = -1;
679
680 /* Shouldn't happen... */
681 return(-1);
682 }
683
684 /*
685 * Read a character from the virtual TTY.
686 *
687 * If the VTTY is a TCP connection, restart it in case of error.
688 */
689 static int vtty_read(vtty_t *vtty,int *fd_slot)
690 {
691 switch(vtty->type) {
692 case VTTY_TYPE_TERM:
693 case VTTY_TYPE_SERIAL:
694 return(vtty_term_read(vtty));
695 case VTTY_TYPE_TCP:
696 return(vtty_tcp_read(vtty,fd_slot));
697 default:
698 fprintf(stderr,"vtty_read: bad vtty type %d\n",vtty->type);
699 return(-1);
700 }
701
702 /* NOTREACHED */
703 return(-1);
704 }
705
706 /* Remote control for MIPS64 processors */
707 static int remote_control_mips64(vtty_t *vtty,char c,cpu_mips_t *cpu)
708 {
709 switch(c) {
710 /* Show information about JIT compiled pages */
711 case 'b':
712 printf("\nCPU0: %u JIT compiled pages [Exec Area Pages: %lu/%lu]\n",
713 cpu->compiled_pages,
714 (u_long)cpu->exec_page_alloc,
715 (u_long)cpu->exec_page_count);
716 break;
717
718 /* Non-JIT mode statistics */
719 case 'j':
720 mips64_dump_stats(cpu);
721 break;
722
723 default:
724 return(FALSE);
725 }
726
727 return(TRUE);
728 }
729
730 /* Remote control for PPC32 processors */
731 static int remote_control_ppc32(vtty_t *vtty,char c,cpu_ppc_t *cpu)
732 {
733 switch(c) {
734 /* Show information about JIT compiled pages */
735 case 'b':
736 printf("\nCPU0: %u JIT compiled pages [Exec Area Pages: %lu/%lu]\n",
737 cpu->compiled_pages,
738 (u_long)cpu->exec_page_alloc,
739 (u_long)cpu->exec_page_count);
740 break;
741
742 /* Non-JIT mode statistics */
743 case 'j':
744 ppc32_dump_stats(cpu);
745 break;
746
747 default:
748 return(FALSE);
749 }
750
751 return(TRUE);
752 }
753
754 /* Process remote control char */
755 static void remote_control(vtty_t *vtty,u_char c)
756 {
757 vm_instance_t *vm = vtty->vm;
758 cpu_gen_t *cpu0;
759
760 cpu0 = vm->boot_cpu;
761
762 /* Specific commands for the different CPU models */
763 if (cpu0) {
764 switch(cpu0->type) {
765 case CPU_TYPE_MIPS64:
766 if (remote_control_mips64(vtty,c,CPU_MIPS64(cpu0)))
767 return;
768 break;
769 case CPU_TYPE_PPC32:
770 if (remote_control_ppc32(vtty,c,CPU_PPC32(cpu0)))
771 return;
772 break;
773 }
774 }
775
776 switch(c) {
777 /* Show the object list */
778 case 'o':
779 vm_object_dump(vm);
780 break;
781
782 /* Stop the MIPS VM */
783 case 'q':
784 vm->status = VM_STATUS_SHUTDOWN;
785 break;
786
787 /* Reboot the C7200 */
788 case 'k':
789 #if 0
790 if (vm->type == VM_TYPE_C7200)
791 c7200_boot_ios(VM_C7200(vm));
792 #endif
793 break;
794
795 /* Show the device list */
796 case 'd':
797 dev_show_list(vm);
798 pci_dev_show_list(vm->pci_bus[0]);
799 pci_dev_show_list(vm->pci_bus[1]);
800 break;
801
802 /* Show info about Port Adapters or Network Modules */
803 case 'p':
804 vm_slot_show_all_info(vm);
805 break;
806
807 /* Dump the MIPS registers */
808 case 'r':
809 if (cpu0) cpu0->reg_dump(cpu0);
810 break;
811
812 /* Dump the latest memory accesses */
813 case 'm':
814 if (cpu0) memlog_dump(cpu0);
815 break;
816
817 /* Suspend CPU emulation */
818 case 's':
819 vm_suspend(vm);
820 break;
821
822 /* Resume CPU emulation */
823 case 'u':
824 vm_resume(vm);
825 break;
826
827 /* Dump the MMU information */
828 case 't':
829 if (cpu0) cpu0->mmu_dump(cpu0);
830 break;
831
832 /* Dump the MMU information (raw mode) */
833 case 'z':
834 if (cpu0) cpu0->mmu_raw_dump(cpu0);
835 break;
836
837 /* Memory translation cache statistics */
838 case 'l':
839 if (cpu0) cpu0->mts_show_stats(cpu0);
840 break;
841
842 /* Extract the configuration from the NVRAM */
843 case 'c':
844 vm_ios_save_config(vm);
845 break;
846
847 /* Determine an idle pointer counter */
848 case 'i':
849 if (cpu0)
850 cpu0->get_idling_pc(cpu0);
851 break;
852
853 /* Experimentations / Tests */
854 case 'x':
855
856 #if 0
857 if (cpu0) {
858 /* IRQ triggering */
859 vm_set_irq(vm,6);
860 //CPU_MIPS64(cpu0)->irq_disable = TRUE;
861 }
862 #endif
863 #ifdef USE_UNSTABLE
864 tsg_show_stats();
865 #endif
866 break;
867
868 case 'y':
869 if (cpu0) {
870 /* IRQ clearing */
871 vm_clear_irq(vm,6);
872 }
873 break;
874
875 /* Twice Ctrl + ']' (0x1d, 29), or Alt-Gr + '*' (0xb3, 179) */
876 case 0x1d:
877 case 0xb3:
878 vtty_store(vtty,c);
879 break;
880
881 default:
882 printf("\n\nInstance %s (ID %d)\n\n",vm->name,vm->instance_id);
883
884 printf("o - Show the VM object list\n"
885 "d - Show the device list\n"
886 "r - Dump CPU registers\n"
887 "t - Dump MMU information\n"
888 "z - Dump MMU information (raw mode)\n"
889 "m - Dump the latest memory accesses\n"
890 "s - Suspend CPU emulation\n"
891 "u - Resume CPU emulation\n"
892 "q - Quit the emulator\n"
893 "k - Reboot the virtual machine\n"
894 "b - Show info about JIT compiled pages\n"
895 "l - MTS cache statistics\n"
896 "c - Write IOS configuration to disk\n"
897 "j - Non-JIT mode statistics\n"
898 "i - Determine an idling pointer counter\n"
899 "x - Experimentations (can crash the box!)\n"
900 "^] - Send ^]\n"
901 "Other - This help\n");
902 }
903 }
904
905
906 /* Read a character (until one is available) and store it in buffer */
907 static void vtty_read_and_store(vtty_t *vtty,int *fd_slot)
908 {
909 int c;
910
911 /* wait until we get a character input */
912 c = vtty_read(vtty,fd_slot);
913
914 /* if read error, do nothing */
915 if (c < 0) return;
916
917 /* If something was read, make sure the handler is informed */
918 vtty->input_pending = TRUE;
919
920 if (!vtty->terminal_support) {
921 vtty_store(vtty,c);
922 return;
923 }
924
925 switch(vtty->input_state) {
926 case VTTY_INPUT_TEXT :
927 switch(c) {
928 case 0x1b:
929 vtty->input_state = VTTY_INPUT_VT1;
930 return;
931
932 /* Ctrl + ']' (0x1d, 29), or Alt-Gr + '*' (0xb3, 179) */
933 case 0x1d:
934 case 0xb3:
935 if (ctrl_code_ok == 1) {
936 vtty->input_state = VTTY_INPUT_REMOTE;
937 } else {
938 vtty_store(vtty,c);
939 }
940 return;
941 case IAC :
942 vtty->input_state = VTTY_INPUT_TELNET;
943 return;
944 case 0: /* NULL - Must be ignored - generated by Linux telnet */
945 case 10: /* LF (Line Feed) - Must be ignored on Windows platform */
946 return;
947 default:
948 /* Store a standard character */
949 vtty_store(vtty,c);
950 return;
951 }
952
953 case VTTY_INPUT_VT1 :
954 switch(c) {
955 case 0x5b:
956 vtty->input_state = VTTY_INPUT_VT2;
957 return;
958 default:
959 vtty_store(vtty,0x1b);
960 vtty_store(vtty,c);
961 }
962 vtty->input_state = VTTY_INPUT_TEXT;
963 return;
964
965 case VTTY_INPUT_VT2 :
966 switch(c) {
967 case 0x41: /* Up Arrow */
968 vtty_store(vtty,16);
969 break;
970 case 0x42: /* Down Arrow */
971 vtty_store(vtty,14);
972 break;
973 case 0x43: /* Right Arrow */
974 vtty_store(vtty,6);
975 break;
976 case 0x44: /* Left Arrow */
977 vtty_store(vtty,2);
978 break;
979 default:
980 vtty_store(vtty,0x5b);
981 vtty_store(vtty,0x1b);
982 vtty_store(vtty,c);
983 break;
984 }
985 vtty->input_state = VTTY_INPUT_TEXT;
986 return;
987
988 case VTTY_INPUT_REMOTE :
989 remote_control(vtty, c);
990 vtty->input_state = VTTY_INPUT_TEXT;
991 return;
992
993 case VTTY_INPUT_TELNET :
994 vtty->telnet_cmd = c;
995 switch(c) {
996 case WILL:
997 case WONT:
998 case DO:
999 case DONT:
1000 vtty->input_state = VTTY_INPUT_TELNET_IYOU;
1001 return;
1002 case SB :
1003 vtty->telnet_cmd = c;
1004 vtty->input_state = VTTY_INPUT_TELNET_SB1;
1005 return;
1006 case SE:
1007 break;
1008 case IAC :
1009 vtty_store(vtty, IAC);
1010 break;
1011 }
1012 vtty->input_state = VTTY_INPUT_TEXT;
1013 return;
1014
1015 case VTTY_INPUT_TELNET_IYOU :
1016 vtty->telnet_opt = c;
1017 /* if telnet client can support ttype, ask it to send ttype string */
1018 if ((vtty->telnet_cmd == WILL) &&
1019 (vtty->telnet_opt == TELOPT_TTYPE))
1020 {
1021 vtty_put_char(vtty, IAC);
1022 vtty_put_char(vtty, SB);
1023 vtty_put_char(vtty, TELOPT_TTYPE);
1024 vtty_put_char(vtty, TELQUAL_SEND);
1025 vtty_put_char(vtty, IAC);
1026 vtty_put_char(vtty, SE);
1027 }
1028 vtty->input_state = VTTY_INPUT_TEXT;
1029 return;
1030
1031 case VTTY_INPUT_TELNET_SB1 :
1032 vtty->telnet_opt = c;
1033 vtty->input_state = VTTY_INPUT_TELNET_SB2;
1034 return;
1035
1036 case VTTY_INPUT_TELNET_SB2 :
1037 vtty->telnet_qual = c;
1038 if ((vtty->telnet_opt == TELOPT_TTYPE) &&
1039 (vtty->telnet_qual == TELQUAL_IS))
1040 vtty->input_state = VTTY_INPUT_TELNET_SB_TTYPE;
1041 else
1042 vtty->input_state = VTTY_INPUT_TELNET_NEXT;
1043 return;
1044
1045 case VTTY_INPUT_TELNET_SB_TTYPE :
1046 /* parse ttype string: first char is sufficient */
1047 /* if client is xterm or vt, set the title bar */
1048 if ((c == 'x') || (c == 'X') || (c == 'v') || (c == 'V')) {
1049 fd_printf(*fd_slot,0,"\033]0;%s\07", vtty->vm->name);
1050 }
1051 vtty->input_state = VTTY_INPUT_TELNET_NEXT;
1052 return;
1053
1054 case VTTY_INPUT_TELNET_NEXT :
1055 /* ignore all chars until next IAC */
1056 if (c == IAC)
1057 vtty->input_state = VTTY_INPUT_TELNET;
1058 return;
1059 }
1060 }
1061
1062 /* Read a character from the buffer (-1 if the buffer is empty) */
1063 int vtty_get_char(vtty_t *vtty)
1064 {
1065 u_char c;
1066
1067 VTTY_LOCK(vtty);
1068
1069 if (vtty->read_ptr == vtty->write_ptr) {
1070 VTTY_UNLOCK(vtty);
1071 return(-1);
1072 }
1073
1074 c = vtty->buffer[vtty->read_ptr++];
1075
1076 if (vtty->read_ptr == VTTY_BUFFER_SIZE)
1077 vtty->read_ptr = 0;
1078
1079 VTTY_UNLOCK(vtty);
1080 return(c);
1081 }
1082
1083 /* Returns TRUE if a character is available in buffer */
1084 int vtty_is_char_avail(vtty_t *vtty)
1085 {
1086 int res;
1087
1088 VTTY_LOCK(vtty);
1089 res = (vtty->read_ptr != vtty->write_ptr);
1090 VTTY_UNLOCK(vtty);
1091 return(res);
1092 }
1093
1094 /* Put char to vtty */
1095 void vtty_put_char(vtty_t *vtty, char ch)
1096 {
1097 switch(vtty->type) {
1098 case VTTY_TYPE_NONE:
1099 break;
1100
1101 case VTTY_TYPE_TERM:
1102 case VTTY_TYPE_SERIAL:
1103 if (write(vtty->fd_array[0],&ch,1) != 1) {
1104 vm_log(vtty->vm,"VTTY","%s: put char 0x%x failed (%s)\n",
1105 vtty->name,(int)ch,strerror(errno));
1106 }
1107 break;
1108
1109 case VTTY_TYPE_TCP:
1110 fd_pool_send(&vtty->fd_pool,&ch,1,0);
1111 break;
1112
1113 default:
1114 vm_error(vtty->vm,"vtty_put_char: bad vtty type %d\n",vtty->type);
1115 exit(1);
1116 }
1117
1118 /* store char for replay */
1119 vtty->replay_buffer[vtty->replay_ptr] = ch;
1120
1121 ++vtty->replay_ptr;
1122 if (vtty->replay_ptr == VTTY_BUFFER_SIZE)
1123 vtty->replay_ptr = 0;
1124 }
1125
1126 /* Put a buffer to vtty */
1127 void vtty_put_buffer(vtty_t *vtty,char *buf,size_t len)
1128 {
1129 size_t i;
1130
1131 for(i=0;i<len;i++)
1132 vtty_put_char(vtty,buf[i]);
1133
1134 vtty_flush(vtty);
1135 }
1136
1137 /* Flush VTTY output */
1138 void vtty_flush(vtty_t *vtty)
1139 {
1140 switch(vtty->type) {
1141 case VTTY_TYPE_TERM:
1142 case VTTY_TYPE_SERIAL:
1143 if (vtty->fd_array[0] != -1)
1144 fsync(vtty->fd_array[0]);
1145 break;
1146 }
1147 }
1148
1149 /* VTTY TCP input */
1150 static void vtty_tcp_input(int *fd_slot,void *opt)
1151 {
1152 vtty_read_and_store((vtty_t *)opt,fd_slot);
1153 }
1154
1155 /* VTTY thread */
1156 static void *vtty_thread_main(void *arg)
1157 {
1158 vtty_t *vtty;
1159 struct timeval tv;
1160 int fd_max,fd_tcp,res;
1161 fd_set rfds;
1162 int i;
1163
1164 for(;;) {
1165 VTTY_LIST_LOCK();
1166
1167 /* Build the FD set */
1168 FD_ZERO(&rfds);
1169 fd_max = -1;
1170 for(vtty=vtty_list;vtty;vtty=vtty->next) {
1171
1172 switch(vtty->type) {
1173 case VTTY_TYPE_TCP:
1174
1175 for(i=0;i<vtty->fd_count;i++)
1176 if (vtty->fd_array[i] != -1) {
1177 FD_SET(vtty->fd_array[i],&rfds);
1178 if (vtty->fd_array[i] > fd_max)
1179 fd_max = vtty->fd_array[i];
1180 }
1181
1182 fd_tcp = fd_pool_set_fds(&vtty->fd_pool,&rfds);
1183 fd_max = m_max(fd_tcp,fd_max);
1184 break;
1185
1186 default:
1187 if (vtty->fd_array[0] != -1) {
1188 FD_SET(vtty->fd_array[0],&rfds);
1189 fd_max = m_max(vtty->fd_array[0],fd_max);
1190 }
1191 }
1192
1193 }
1194 VTTY_LIST_UNLOCK();
1195
1196 /* Wait for incoming data */
1197 tv.tv_sec = 0;
1198 tv.tv_usec = 50 * 1000; /* 50 ms */
1199 res = select(fd_max+1,&rfds,NULL,NULL,&tv);
1200
1201 if (res == -1) {
1202 if (errno != EINTR) {
1203 perror("vtty_thread: select");
1204 }
1205 continue;
1206 }
1207
1208 /* Examine active FDs and call user handlers */
1209 VTTY_LIST_LOCK();
1210 for(vtty=vtty_list;vtty;vtty=vtty->next) {
1211
1212 switch(vtty->type) {
1213 case VTTY_TYPE_TCP:
1214
1215 /* check incoming connection */
1216 for(i=0;i<vtty->fd_count;i++) {
1217
1218 if (vtty->fd_array[i] == -1)
1219 continue;
1220
1221 if (!FD_ISSET(vtty->fd_array[i],&rfds))
1222 continue;
1223
1224 vtty_tcp_conn_accept(vtty, i);
1225 }
1226
1227 /* check established connection */
1228 fd_pool_check_input(&vtty->fd_pool,&rfds,vtty_tcp_input,vtty);
1229 break;
1230
1231 /* Term, Serial */
1232 default:
1233 if (vtty->fd_array[0] != -1 && FD_ISSET(vtty->fd_array[0],&rfds)) {
1234 vtty_read_and_store(vtty,&vtty->fd_array[0]);
1235 vtty->input_pending = TRUE;
1236 }
1237 }
1238
1239 if (vtty->input_pending) {
1240 if (vtty->read_notifier != NULL)
1241 vtty->read_notifier(vtty);
1242
1243 vtty->input_pending = FALSE;
1244 }
1245
1246 /* Flush any pending output */
1247 if (!vtty->managed_flush)
1248 vtty_flush(vtty);
1249 }
1250 VTTY_LIST_UNLOCK();
1251 }
1252
1253 return NULL;
1254 }
1255
1256 /* Initialize the VTTY thread */
1257 int vtty_init(void)
1258 {
1259 if (pthread_create(&vtty_thread,NULL,vtty_thread_main,NULL)) {
1260 perror("vtty: pthread_create");
1261 return(-1);
1262 }
1263
1264 return(0);
1265 }