17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2134709573Sraf 
227c478bd9Sstevel@tonic-gate /*
23df2381bfSpraks  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*f23ed011SPatrick Mooney  * Copyright 2022 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef	_SYS_PORT_KERNEL_H
297c478bd9Sstevel@tonic-gate #define	_SYS_PORT_KERNEL_H
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
327c478bd9Sstevel@tonic-gate #include <sys/list.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
357c478bd9Sstevel@tonic-gate extern "C" {
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Note:
407c478bd9Sstevel@tonic-gate  * The contents of this file are private to the implementation of the
417c478bd9Sstevel@tonic-gate  * Solaris system and event ports subsystem and are subject to change
427c478bd9Sstevel@tonic-gate  * at any time without notice.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef _KERNEL
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * The port_kevent_t struct represents the kernel internal port event.
497c478bd9Sstevel@tonic-gate  * Every event is associated to a port (portkev_port).
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate typedef	struct	port_kevent {
5261b4b1efSpraks 	kmutex_t	portkev_lock;	/* used by PORT_SOURCE_FD source */
537c478bd9Sstevel@tonic-gate 	int	portkev_source;		/* event: source */
54*f23ed011SPatrick Mooney 	int	portkev_events;		/* event: data */
557c478bd9Sstevel@tonic-gate 	int	portkev_flags;		/* internal flags */
567c478bd9Sstevel@tonic-gate 	pid_t	portkev_pid;		/* pid of process using this struct */
577c478bd9Sstevel@tonic-gate 	long	portkev_object;		/* event: object */
587c478bd9Sstevel@tonic-gate 	void	*portkev_user;		/* event: user-defined value */
597c478bd9Sstevel@tonic-gate 	int	(*portkev_callback)(void *, int *, pid_t, int, void *);
607c478bd9Sstevel@tonic-gate 	void	*portkev_arg;		/* event source callback arg */
617c478bd9Sstevel@tonic-gate 	struct	port *portkev_port;	/* associated port */
627c478bd9Sstevel@tonic-gate 	list_node_t portkev_node;	/* pointer to neighbor events */
637c478bd9Sstevel@tonic-gate } port_kevent_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* portkev_flags */
667c478bd9Sstevel@tonic-gate #define	PORT_KEV_PRIVATE	0x01	/* subsystem private, don't free */
677c478bd9Sstevel@tonic-gate #define	PORT_KEV_CACHED		0x02	/* port local cached, don't free */
687c478bd9Sstevel@tonic-gate #define	PORT_KEV_SCACHED	0x04	/* source local cached, don't free */
697c478bd9Sstevel@tonic-gate #define	PORT_KEV_VALID		0x08	/* event associated and enabled */
707c478bd9Sstevel@tonic-gate #define	PORT_KEV_DONEQ		0x10	/* event is in done queue */
717c478bd9Sstevel@tonic-gate #define	PORT_KEV_FREE		0x20	/* free event and don't copyout it */
727c478bd9Sstevel@tonic-gate #define	PORT_KEV_NOSHARE	0x40	/* non-shareable across processes */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* flags : port_alloc_event() */
757c478bd9Sstevel@tonic-gate #define	PORT_ALLOC_DEFAULT	0
767c478bd9Sstevel@tonic-gate #define	PORT_ALLOC_PRIVATE	PORT_KEV_PRIVATE
777c478bd9Sstevel@tonic-gate #define	PORT_ALLOC_CACHED	PORT_KEV_CACHED
787c478bd9Sstevel@tonic-gate #define	PORT_ALLOC_SCACHED	PORT_KEV_SCACHED
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* flags : callback function */
817c478bd9Sstevel@tonic-gate #define	PORT_CALLBACK_DEFAULT	0	/* free resources, event delivery */
827c478bd9Sstevel@tonic-gate #define	PORT_CALLBACK_CLOSE	1	/* free resources, don't copyout */
837c478bd9Sstevel@tonic-gate #define	PORT_CALLBACK_DISSOCIATE 2	/* dissociate object */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	PORT_DEFAULT_PORTS	0x02000
867c478bd9Sstevel@tonic-gate #define	PORT_MAX_PORTS		0x10000
877c478bd9Sstevel@tonic-gate #define	PORT_DEFAULT_EVENTS	0x10000	/* default # of events per port */
887c478bd9Sstevel@tonic-gate #define	PORT_MAX_EVENTS		UINT_MAX/2 /* max. # of events per port */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * port_source_t represents a source associated with a port.
927c478bd9Sstevel@tonic-gate  * The portsrc_close() function is required to notify the source when
937c478bd9Sstevel@tonic-gate  * a port is closed.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate typedef struct port_source {
967c478bd9Sstevel@tonic-gate 	int	portsrc_source;
977c478bd9Sstevel@tonic-gate 	int	portsrc_cnt;		/* # of associations */
987c478bd9Sstevel@tonic-gate 	void	(*portsrc_close)(void *, int, pid_t, int);
997c478bd9Sstevel@tonic-gate 	void	*portsrc_closearg;	/* callback arg */
100df2381bfSpraks 	void	*portsrc_data;		/* Private data of source */
1017c478bd9Sstevel@tonic-gate 	struct port_source *portsrc_next;
1027c478bd9Sstevel@tonic-gate 	struct port_source *portsrc_prev;
1037c478bd9Sstevel@tonic-gate } port_source_t;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
107df2381bfSpraks  * PORT_SOURCE_FILE cache structure.
108df2381bfSpraks  */
109df2381bfSpraks #define	PORTFOP_HASHSIZE	256	/* cache space for fop events */
110df2381bfSpraks 
111df2381bfSpraks /*
112df2381bfSpraks  * One cache for each port that uses PORT_SOURCE_FILE.
113df2381bfSpraks  */
114df2381bfSpraks typedef struct portfop_cache {
115df2381bfSpraks 	kmutex_t	pfc_lock;	/* lock to protect cache */
116df2381bfSpraks 	kcondvar_t	pfc_lclosecv;	/* last close cv */
117df2381bfSpraks 	int		pfc_objcount;	/* track how many file obj are hashed */
118df2381bfSpraks 	struct portfop	*pfc_hash[PORTFOP_HASHSIZE]; /* hash table */
119df2381bfSpraks } portfop_cache_t;
120df2381bfSpraks 
121df2381bfSpraks /*
122df2381bfSpraks  * PORT_SOURCE_FD cache per port.
1237c478bd9Sstevel@tonic-gate  * One cache for each port that uses PORT_SOURCE_FD.
124*f23ed011SPatrick Mooney  *
125*f23ed011SPatrick Mooney  * The types and offsets of pc_lock and pc_flag must exactly match their sibling
126*f23ed011SPatrick Mooney  * fields in pollcache_t, as they are accessed as if the port_fdcache_t _was_ a
127*f23ed011SPatrick Mooney  * pollcache via t_pollcache. (See: pollrelock() and fs_reject_epoll())
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate typedef struct port_fdcache {
1307c478bd9Sstevel@tonic-gate 	kmutex_t	pc_lock;	/* lock to protect portcache */
13161b4b1efSpraks 	kcondvar_t	pc_lclosecv;
1327c478bd9Sstevel@tonic-gate 	struct portfd	**pc_hash;	/* points to a hash table of ptrs */
1337c478bd9Sstevel@tonic-gate 	int		pc_hashsize;	/* the size of current hash table */
1347c478bd9Sstevel@tonic-gate 	int		pc_fdcount;	/* track how many fd's are hashed */
135*f23ed011SPatrick Mooney 	uintptr_t	_pc_pad;	/* pad to properly offset pc_flag */
136*f23ed011SPatrick Mooney 	int		pc_flag;	/* pollcache flags (compat) */
1377c478bd9Sstevel@tonic-gate } port_fdcache_t;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Structure of port_ksource_tab[] table.
1417c478bd9Sstevel@tonic-gate  * The port_ksource_tab[] is required to allow kernel sources to become
1427c478bd9Sstevel@tonic-gate  * associated with a port at the time of port creation. This feature is
1437c478bd9Sstevel@tonic-gate  * required to avoid performance degradation in sub-systems, specially when
1447c478bd9Sstevel@tonic-gate  * they should need to check the association on every event activity.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate typedef	struct	port_ksource {
1477c478bd9Sstevel@tonic-gate 	int	pks_source;
1487c478bd9Sstevel@tonic-gate 	void	(*pks_close)(void *, int, pid_t, int);
1497c478bd9Sstevel@tonic-gate 	void	*pks_closearg;
1507c478bd9Sstevel@tonic-gate 	void	*pks_portsrc;
1517c478bd9Sstevel@tonic-gate } port_ksource_t;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* event port and source management */
1547c478bd9Sstevel@tonic-gate int	port_associate_ksource(int, int, struct port_source **,
1557c478bd9Sstevel@tonic-gate     void (*)(void *, int, pid_t, int), void *arg,
1567c478bd9Sstevel@tonic-gate     int (*)(port_kevent_t *, int, int, uintptr_t, void *));
1577c478bd9Sstevel@tonic-gate int	port_dissociate_ksource(int, int, struct port_source *);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /* event management */
1607c478bd9Sstevel@tonic-gate int	port_alloc_event(int, int, int, port_kevent_t **);
16111dc39ddSpraks int	port_pollwkup(struct port *);
16211dc39ddSpraks void	port_pollwkdone(struct port *);
16334709573Sraf void	port_send_event(port_kevent_t *);
1647c478bd9Sstevel@tonic-gate void	port_free_event(port_kevent_t *);
1657c478bd9Sstevel@tonic-gate void	port_init_event(port_kevent_t *, uintptr_t, void *,
1667c478bd9Sstevel@tonic-gate     int (*)(void *, int *, pid_t, int, void *), void *);
1677c478bd9Sstevel@tonic-gate int	port_dup_event(port_kevent_t *, port_kevent_t **, int);
1687c478bd9Sstevel@tonic-gate int	port_associate_fd(struct port *, int, uintptr_t, int, void *);
1697c478bd9Sstevel@tonic-gate int	port_dissociate_fd(struct port *, uintptr_t);
170df2381bfSpraks int	port_associate_fop(struct port *, int, uintptr_t, int, void *);
171df2381bfSpraks int	port_dissociate_fop(struct port *, uintptr_t);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /* misc functions */
1747c478bd9Sstevel@tonic-gate void	port_free_event_local(port_kevent_t *, int counter);
1757c478bd9Sstevel@tonic-gate int	port_alloc_event_local(struct port *, int, int, port_kevent_t **);
1767c478bd9Sstevel@tonic-gate void	port_close_pfd(struct portfd *);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate #endif
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #endif	/* _SYS_PORT_KERNEL_H */
185