xref: /illumos-gate/usr/src/cmd/vntsd/vntsd.h (revision 28b1e50e)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo /*
22*28b1e50eSSriharsha Basavapatna  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
231ae08745Sheppo  * Use is subject to license terms.
241ae08745Sheppo  */
261ae08745Sheppo /*
271ae08745Sheppo  * vntsd uses configuration information provided by vcc to export access
281ae08745Sheppo  * to Ldom console access over regular TCP sockets. When it starts, it opens
291ae08745Sheppo  * the vcc driver control port and obtains the list of ports that have been
301ae08745Sheppo  * created by the vcc driver as well as TCP port number and group associated
311ae08745Sheppo  * with each port.
321ae08745Sheppo  * vntsd consists of multiple components as the follows:
331ae08745Sheppo  *
341ae08745Sheppo  * vntsd.c
351ae08745Sheppo  * This module initializes vnts daemon, process user options such as instance
361ae08745Sheppo  * number, ip address and etc., and provides main thread to poll any console
371ae08745Sheppo  * port change.
381ae08745Sheppo  *
391ae08745Sheppo  * vntsdvcc.c
401ae08745Sheppo  * This module provides vcc driver interface. It opens vcc driver control
411ae08745Sheppo  * ports, read initial configuration, and provides interface to read, write and
421ae08745Sheppo  * ioctl virtual console ports. This module creates a listen thread for each
431ae08745Sheppo  * console group. It further dynamically adds and removes virtual consoles
441ae08745Sheppo  * and groups following instructions of the vcc driver. This module
451ae08745Sheppo  * is executed in the same thread as vntsd.c which is blocked on vcc control
461ae08745Sheppo  * poll interface.
471ae08745Sheppo  *
481ae08745Sheppo  * listen.c
491ae08745Sheppo  * This is a group listen thread. Each group's tcp-port has a listen thread
501ae08745Sheppo  * associated with it. The thread is created when a console is associated with
511ae08745Sheppo  * a new group and is removed when all consoles in the group are removed.
521ae08745Sheppo  *
531ae08745Sheppo  * console.c
541ae08745Sheppo  * This is a console selection thread. The thread is created when a client
551ae08745Sheppo  * connects to a group TCP port and exited when client disconnects. If there is
561ae08745Sheppo  * only one console in the group, the client is connected to that console. If
571ae08745Sheppo  * there are multiple consoles in the group, the client is asked to select a
581ae08745Sheppo  * console. After determining which console to connect to, this thread
591ae08745Sheppo  * a write thread if the cient is a writer and it self read in client input.
601ae08745Sheppo  *
611ae08745Sheppo  * read.c
621ae08745Sheppo  * it reads input from a TCP client, processes
631ae08745Sheppo  * special daemon and telent commands and write to vcc driver if the client
641ae08745Sheppo  * is a writer. The client is a writer if the client is the first one connects
651ae08745Sheppo  * to the console. Read thread print out an error message if a reader attempt
661ae08745Sheppo  * to input to vcc. Read thread exits if console is deleted, client
671ae08745Sheppo  * disconnects, or there is a fatal error.
681ae08745Sheppo  *
691ae08745Sheppo  * Write.c
701ae08745Sheppo  * Write thread is creaed when first client connects to a console. It reads
711ae08745Sheppo  * from vcc and writes to all clients that connect to the same console.
721ae08745Sheppo  * Write thread exits when all clients disconnect from the console.
731ae08745Sheppo  *
741ae08745Sheppo  * cmd.c
751ae08745Sheppo  * This is a supporting module for handling special daemon and telnet commands.
761ae08745Sheppo  *
771ae08745Sheppo  * common.c
781ae08745Sheppo  * supporting modules shared by threads modules.
791ae08745Sheppo  *
801ae08745Sheppo  * queue.c
811ae08745Sheppo  * This is a moudle supporting queue operations. Vntsd organizes its data
821ae08745Sheppo  * in multiple queues <see data structure below>.
831ae08745Sheppo  *
841ae08745Sheppo  * vntsd.xml
851ae08745Sheppo  * This is a manifest to support SMF interfaces.
861ae08745Sheppo  *
871ae08745Sheppo  * Data structures
881ae08745Sheppo  * each group has a vntsd_group_t structure, which contains a queue of
891ae08745Sheppo  * all console in that group.
901ae08745Sheppo  * each console has a vntsd_cons_t structure, which contains a queue of
911ae08745Sheppo  * all clients that connected to the console.
921ae08745Sheppo  *
931ae08745Sheppo  *     +----------+   +----------+   +----------+
941ae08745Sheppo  *     |  group	  |-->|  group   |-->|   group  |-->....
951ae08745Sheppo  *     +----------+   +----------+   +----------+
961ae08745Sheppo  *          |
971ae08745Sheppo  *          |<-----------------------------------------+
981ae08745Sheppo  *          |<------------------------+                |
991ae08745Sheppo  *          |<--------+               |                |
1001ae08745Sheppo  *          |         |               |                |
1011ae08745Sheppo  *          |      +----------+     +----------+     +----------+
1021ae08745Sheppo  *          +----->| console  |---->| console  |---->| lconsole |---> ....
1031ae08745Sheppo  *                 +----------+     +----------+     +----------+
1041ae08745Sheppo  *                     |  |
1051ae08745Sheppo  *		       |  |     +----------+      +----------+
1061ae08745Sheppo  *		       |  +---->|  client  |----->|   client |----->......
1071ae08745Sheppo  *		       |	+----------+      +----------+
1081ae08745Sheppo  *		       |	     |                 |
1091ae08745Sheppo  *		       |<------------+                 |
1101ae08745Sheppo  *		       |<------------------------------+
1111ae08745Sheppo  *
1121ae08745Sheppo  * Locks
1131ae08745Sheppo  *  Each vntsd has one lock to protect the group queue
1141ae08745Sheppo  *  Each group has one lock to protect the console queue,  the queue for
1151ae08745Sheppo  *  clients without a console connection and status.
1161ae08745Sheppo  *  Each console has one lock to protect client queue and status.
1171ae08745Sheppo  *  Each client has one lock to protect the state of the client. The client
1181ae08745Sheppo  *  states are:
1191ae08745Sheppo  *
1201ae08745Sheppo  *  VCC_CLIENT_READER
1211ae08745Sheppo  *	A client is connected to a console as either a writer or a reader.
1221ae08745Sheppo  *	if this client is the first one connects the console, the client is
1231ae08745Sheppo  *	a writer, otherwise the client is a reader. A writer' write thread
1241ae08745Sheppo  *	reads from vcc and send output to all readers connected to the
1251ae08745Sheppo  *	same console. a reader's write thread is blocked until a reader becomes
1261ae08745Sheppo  *	a writer.
1271ae08745Sheppo  *
1281ae08745Sheppo  *	When a client selected a console, the client becomes a reader if
1291ae08745Sheppo  *	there is another client connected to the console before the client.
1301ae08745Sheppo  *	A client will be a writer if
1311ae08745Sheppo  *	1. client is the first one connected to the console or
1321ae08745Sheppo  *	2. client has entered a ~w daemon command or
1331ae08745Sheppo  *	3. all clients connected to the console before the client have
1341ae08745Sheppo  *	   disconnected from the console.
1351ae08745Sheppo  *
1361ae08745Sheppo  *  VCC_CLIENT_MOVE_CONS_FORWARD
1381ae08745Sheppo  *	A client is disconnecting from one console and move to the next or
1391ae08745Sheppo  *	previous console in the group queue.
1401ae08745Sheppo  *	A client is in one of these state if
1411ae08745Sheppo  *	1. the client has entered the daemon command and
1421ae08745Sheppo  *	2. the vntsd is in process of switching the client from one
1431ae08745Sheppo  *	   console to another.
1441ae08745Sheppo  *
1461ae08745Sheppo  *	vntsd is in processing of a client's daemon command or the client is
1471ae08745Sheppo  *	in selecting console.
1481ae08745Sheppo  *	A client is in this state if
1491ae08745Sheppo  *	1. the client has not selected a console or
1501ae08745Sheppo  *	2. the vntsd is processing a client's daemon command.
1511ae08745Sheppo  *
1521ae08745Sheppo  *  VCC_CLIENT_ACQUIRE_WRITER
1531ae08745Sheppo  *	A reader forces to become a writer via vntsd special command.
1541ae08745Sheppo  *	A client is in this state if
1551ae08745Sheppo  *	1. the client is a reader and
1561ae08745Sheppo  *	2. client has entered a daemon command to become a writer.
1571ae08745Sheppo  *
1581ae08745Sheppo  *  VCC_CLIENT_CONS_DELETED
1591ae08745Sheppo  *	The console that the client is connected to is being deleted and
1601ae08745Sheppo  *	waiting for the client to disconnect.
1611ae08745Sheppo  *	A client is in this state if
1621ae08745Sheppo  *	1. the console a client is connected to is being removed and
1631ae08745Sheppo  *	2. the vntsd is in process of disconnecting the client from the console.
1641ae08745Sheppo  *
1651ae08745Sheppo  */
1671ae08745Sheppo #ifndef _VNTSD_H
1681ae08745Sheppo #define	_VNTSD_H
1701ae08745Sheppo #ifdef __cplusplus
1711ae08745Sheppo extern "C" {
1721ae08745Sheppo #endif
1741ae08745Sheppo #include	<sys/shm.h>
1751ae08745Sheppo #include	<strings.h>
1761ae08745Sheppo #include	<assert.h>
1771ae08745Sheppo #include	<sys/wait.h>
1781ae08745Sheppo #include	<sys/stat.h>
1791ae08745Sheppo #include	<fcntl.h>
1801ae08745Sheppo #include	<stropts.h>
1811ae08745Sheppo #include	<errno.h>
1821ae08745Sheppo #include	<sys/param.h>
1831ae08745Sheppo #include	"../../uts/sun4v/sys/vcc.h"
1851ae08745Sheppo #define	DEBUG
1871ae08745Sheppo /* vntsd limits */
1881ae08745Sheppo #define	    VNTSD_MAX_BUF_SIZE		128
1891ae08745Sheppo #define	    VNTSD_LINE_LEN		100
1901ae08745Sheppo #define	    VNTSD_MAX_SOCKETS		5
1911ae08745Sheppo #define	    VNTSD_EOL_LEN		2
1931ae08745Sheppo /* secons before re-send signal for cv_wait */
1941ae08745Sheppo #define	    VNTSD_CV_WAIT_DELTIME	10
1961ae08745Sheppo #define	    VCC_PATH_PREFIX     \
1971ae08745Sheppo 		"/devices/virtual-devices@100/channel-devices@200/"
1981ae08745Sheppo #define	    VCC_DEVICE_PATH			"/devices%s"
1991ae08745Sheppo #define	    VCC_DEVICE_CTL_PATH VCC_PATH_PREFIX "%s:ctl"
2011ae08745Sheppo /* common messages */
2021ae08745Sheppo #define	    VNTSD_NO_WRITE_ACCESS_MSG	"You do not have write access"
2041ae08745Sheppo /* vntsd options */
2051ae08745Sheppo #define	    VNTSD_OPT_DAEMON_OFF	0x1
206*28b1e50eSSriharsha Basavapatna #define	    VNTSD_OPT_AUTH_CHECK	0x2	/* Enable auth checking */
2084d39be2bSsg /*
2094d39be2bSsg  * group states
2104d39be2bSsg  * When a console is removed or vntsd is exiting, main thread
2114d39be2bSsg  * notifies listen, read and write thread to exit.
2124d39be2bSsg  * After those threads exit, main thread clears up group structurre.
2134d39be2bSsg  *
2144d39be2bSsg  * VNTSD_GROUP_SIG_WAIT
2154d39be2bSsg  * The main thread is waiting for listen thread to exit.
2164d39be2bSsg  * VNTSD_GROUP_CLEAN_CONS
2174d39be2bSsg  * There are console(s) in the group that are being removed.
2184d39be2bSsg  * This is a transition state where the corresponding vcc port has been
2194d39be2bSsg  * removed, but vntsd has not done its clean up yet.
2204d39be2bSsg  * VNTSD_GROUP_IN_CLEANUP
2214d39be2bSsg  * vntsd main thread has started cleaning up the group.
2224d39be2bSsg  */
2244d39be2bSsg #define	    VNTSD_GROUP_SIG_WAIT	0x1
2254d39be2bSsg #define	    VNTSD_GROUP_CLEAN_CONS	0x2
2264d39be2bSsg #define	    VNTSD_GROUP_IN_CLEANUP	0x4
2324d39be2bSsg /*
2334d39be2bSsg  * console states
2344d39be2bSsg  * There are two states when a console is removed
2354d39be2bSsg  * VNTSD_CONS_DELETED
2364d39be2bSsg  * the console is being deleted
2374d39be2bSsg  * VNTSD_CONS_SIG_WAIT
2384d39be2bSsg  * console is waiting for all clients to exit.
2394d39be2bSsg  */
2411ae08745Sheppo #define	    VNTSD_CONS_DELETED		0x1	/* deleted */
2424d39be2bSsg #define	    VNTSD_CONS_SIG_WAIT		0x2	/* waiting for signal */
2451ae08745Sheppo #define	    VNTSD_CLIENT_IO_ERR		    0x1	    /* reader */
2461ae08745Sheppo #define	    VNTSD_CLIENT_DISABLE_DAEMON_CMD 0x2	    /* disable daemon cmd */
2471ae08745Sheppo #define	    VNTSD_CLIENT_TIMEOUT	    0x4	    /* timeout */
2481ae08745Sheppo #define	    VNTSD_CLIENT_CONS_DELETED	    0x8	    /* console deleted */
2501ae08745Sheppo /* generic que structure */
2511ae08745Sheppo typedef	struct vntsd_que {
2521ae08745Sheppo 	void			*handle;	/* element in queue */
2531ae08745Sheppo 	struct vntsd_que	*nextp;		/* next queue element */
2541ae08745Sheppo 	struct vntsd_que	*prevp;		/* previous queue element */
2551ae08745Sheppo } vntsd_que_t;
2571ae08745Sheppo struct vntsd_cons;
2581ae08745Sheppo struct vntsd_group;
2591ae08745Sheppo struct vntsd;
2611ae08745Sheppo /* client structure  */
2621ae08745Sheppo typedef struct vntsd_client {
2631ae08745Sheppo 	mutex_t	    lock;	    /* protect the client */
2641ae08745Sheppo 	uint_t	    status;	    /* client's state */
2661ae08745Sheppo 	int	    sockfd;	    /* connection socket */
2671ae08745Sheppo 	thread_t    cons_tid;	    /* console thread */
2691ae08745Sheppo 	struct vntsd_cons    *cons; /* back link to console configuration */
271d10e4ef2Snarayan 	char	    prev_char;	    /* previous char read by this client */
2731ae08745Sheppo } vntsd_client_t;
2751ae08745Sheppo /* console structure */
2761ae08745Sheppo typedef struct vntsd_cons {
2771ae08745Sheppo 	mutex_t		lock;			    /* protect console port */
2781ae08745Sheppo 	cond_t		cvp;			    /* sync between threads */
2801ae08745Sheppo 	vntsd_que_t	*clientpq;		    /* client que */
2811ae08745Sheppo 	uint_t		status;			    /* client's state */
2821ae08745Sheppo 	int		vcc_fd;			    /* vcc console port */
2831ae08745Sheppo 	thread_t	wr_tid;			    /* write thread */
2851ae08745Sheppo 	uint_t		cons_no;		    /* console port number  */
2861ae08745Sheppo 	char		domain_name[MAXPATHLEN];    /* domain name */
2871ae08745Sheppo 	char		dev_name[MAXPATHLEN];
2891ae08745Sheppo 	struct vntsd_group   *group;		    /* back link to group */
2901ae08745Sheppo } vntsd_cons_t;
2921ae08745Sheppo /* group structure  */
2931ae08745Sheppo typedef struct vntsd_group {
2941ae08745Sheppo 	mutex_t	    lock;		    /* protect group */
2951ae08745Sheppo 	cond_t	    cvp;		    /* sync remove group */
2971ae08745Sheppo 	uint_t	    status;		    /* group status */
2981ae08745Sheppo 	char	    group_name[MAXPATHLEN];
2991ae08745Sheppo 	uint64_t    tcp_port;		    /* telnet port */
3011ae08745Sheppo 	thread_t    listen_tid;		    /* listen thread */
3021ae08745Sheppo 	int	    sockfd;		    /* listen socket */
3041ae08745Sheppo 	vntsd_que_t *conspq;		    /* console queue */
3051ae08745Sheppo 	uint_t	    num_cons;		    /* num console */
3071ae08745Sheppo 	/* clients have no console connection */
3081ae08745Sheppo 	vntsd_que_t *no_cons_clientpq;
3091ae08745Sheppo 	struct vntsd   *vntsd;
3111ae08745Sheppo } vntsd_group_t;
3131ae08745Sheppo /* daemon structure */
3141ae08745Sheppo typedef struct vntsd {
3161ae08745Sheppo 	mutex_t		lock;			/* protect vntsd */
3171ae08745Sheppo 	mutex_t		tmo_lock;		/* protect tmo queue */
3191ae08745Sheppo 	int		instance;		/* vcc instance */
3201ae08745Sheppo 	struct in_addr  ip_addr;		/* ip address to listen */
3211ae08745Sheppo 	uint64_t	options;		/* daemon options */
3221ae08745Sheppo 	int		timeout;		/* connection timeout */
3241ae08745Sheppo 	char		*devinst;		/* device name */
3251ae08745Sheppo 	int		ctrl_fd;		/* vcc ctrl port */
3271ae08745Sheppo 	vntsd_que_t	*grouppq;		/* group queue */
3281ae08745Sheppo 	uint_t		num_grps;		/* num groups */
3301ae08745Sheppo 	vntsd_que_t	*tmoq;			/* timeout queue */
3311ae08745Sheppo 	thread_t	tid;			/* main thread id */
3331ae08745Sheppo } vntsd_t;
3351ae08745Sheppo /* handle for creating thread */
3361ae08745Sheppo typedef	struct vntsd_thr_arg {
3371ae08745Sheppo 	void	*handle;
3381ae08745Sheppo 	void	*arg;
3391ae08745Sheppo } vntsd_thr_arg_t;
3411ae08745Sheppo /* timeout structure */
3421ae08745Sheppo typedef struct vntsd_timeout {
3431ae08745Sheppo 	thread_t	tid;		    /* thread tid */
3441ae08745Sheppo 	uint_t		minutes;	    /* idle minutes */
3451ae08745Sheppo 	vntsd_client_t	*clientp;	    /* client */
3461ae08745Sheppo } vntsd_timeout_t;
3481ae08745Sheppo /* vntsd status and error  definitions */
3491ae08745Sheppo typedef enum {
3511ae08745Sheppo 	/* status */
3521ae08745Sheppo 	VNTSD_SUCCESS = 0,		/* success */
3531ae08745Sheppo 	VNTSD_STATUS_CONTINUE,		/* continue to execute */
3541ae08745Sheppo 	VNTSD_STATUS_EXIT_SIG,		/* exit siginal */
3551ae08745Sheppo 	VNTSD_STATUS_SIG,		/* known signal */
3561ae08745Sheppo 	VNTSD_STATUS_NO_HOST_NAME,	/* no host name set */
3571ae08745Sheppo 	VNTSD_STATUS_CLIENT_QUIT,	/* client disconnected from group */