xref: /illumos-gate/usr/src/uts/common/io/ppp/sppptun/sppptun_mod.c (revision 193974072f41a843678abf5f61979c748687e66b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * sppptun_mod.c - modload support for PPP multiplexing tunnel driver.
37c478bd9Sstevel@tonic-gate  *
4*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
67c478bd9Sstevel@tonic-gate  */
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #include <sys/types.h>
107c478bd9Sstevel@tonic-gate #include <sys/param.h>
117c478bd9Sstevel@tonic-gate #include <sys/stat.h>
127c478bd9Sstevel@tonic-gate #include <sys/systm.h>
137c478bd9Sstevel@tonic-gate #include <sys/socket.h>
147c478bd9Sstevel@tonic-gate #include <sys/stream.h>
157c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
167c478bd9Sstevel@tonic-gate #include <sys/time.h>
177c478bd9Sstevel@tonic-gate #include <sys/conf.h>
187c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
197c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
207c478bd9Sstevel@tonic-gate #include <net/sppptun.h>
217c478bd9Sstevel@tonic-gate #include <netinet/in.h>
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include "sppptun_mod.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Descriptions for flags values in cb_flags field:
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * D_MTQPAIR:
297c478bd9Sstevel@tonic-gate  *    An inner perimeter that spans the queue pair.
307c478bd9Sstevel@tonic-gate  * D_MTOUTPERIM:
317c478bd9Sstevel@tonic-gate  *    An outer perimeter that spans over all queues in the module.
327c478bd9Sstevel@tonic-gate  * D_MTOCEXCL:
337c478bd9Sstevel@tonic-gate  *    Open & close procedures are entered exclusively at outer perimeter.
347c478bd9Sstevel@tonic-gate  * D_MTPUTSHARED:
357c478bd9Sstevel@tonic-gate  *    Entry to put procedures are done with SHARED (reader) acess
367c478bd9Sstevel@tonic-gate  *    and not EXCLUSIVE (writer) access.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Therefore:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * 1. Open and close procedures are entered with EXCLUSIVE (writer)
417c478bd9Sstevel@tonic-gate  *    access at the inner perimeter, and with EXCLUSIVE (writer) access at
427c478bd9Sstevel@tonic-gate  *    the outer perimeter.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * 2. Put procedures are entered with SHARED (reader) access at the inner
457c478bd9Sstevel@tonic-gate  *    perimeter, and with SHARED (reader) access at the outer perimeter.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * 3. Service procedures are entered with EXCLUSIVE (writer) access at
487c478bd9Sstevel@tonic-gate  *    the inner perimeter, and with SHARED (reader) access at the
497c478bd9Sstevel@tonic-gate  *    outer perimeter.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * Do not attempt to modify these flags unless the entire corresponding
527c478bd9Sstevel@tonic-gate  * driver code is changed to accomodate the newly represented MT-STREAMS
537c478bd9Sstevel@tonic-gate  * flags. Doing so without making proper modifications to the driver code
547c478bd9Sstevel@tonic-gate  * will severely impact the intended driver behavior, and thus may affect
557c478bd9Sstevel@tonic-gate  * the system's stability and performance.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static int	tun_attach(dev_info_t *, ddi_attach_cmd_t);
597c478bd9Sstevel@tonic-gate static int	tun_detach(dev_info_t *, ddi_detach_cmd_t);
607c478bd9Sstevel@tonic-gate static int	tun_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static dev_info_t *tun_dev_info;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(sppptun_ops,			\
657c478bd9Sstevel@tonic-gate 	nulldev, nulldev,				\
667c478bd9Sstevel@tonic-gate 	tun_attach, tun_detach, nodev, tun_info,	\
677c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL | D_MTPUTSHARED, \
68*19397407SSherry Moore 	&sppptun_tab, ddi_quiesce_not_supported);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static struct fmodsw sppptun_fmodsw = {
797c478bd9Sstevel@tonic-gate 	PPP_TUN_NAME,
807c478bd9Sstevel@tonic-gate 	&sppptun_tab,
817c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_MTQPAIR | D_MTOUTPERIM | D_MTOCEXCL | D_MTPUTSHARED
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
857c478bd9Sstevel@tonic-gate 	&mod_driverops,
867c478bd9Sstevel@tonic-gate 	(char *)sppptun_driver_description,
877c478bd9Sstevel@tonic-gate 	&sppptun_ops
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
917c478bd9Sstevel@tonic-gate 	&mod_strmodops,
927c478bd9Sstevel@tonic-gate 	(char *)sppptun_module_description,
937c478bd9Sstevel@tonic-gate 	&sppptun_fmodsw
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
977c478bd9Sstevel@tonic-gate 	MODREV_1,
987c478bd9Sstevel@tonic-gate 	(void *)&modlstrmod,
997c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
1007c478bd9Sstevel@tonic-gate 	NULL
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
1047c478bd9Sstevel@tonic-gate _init(void)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	int retv;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	sppptun_init();
109002c70ffScarlsonj 	if ((retv = mod_install(&modlinkage)) == 0)
1107c478bd9Sstevel@tonic-gate 		sppptun_tcl_init();
1117c478bd9Sstevel@tonic-gate 	return (retv);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int
1157c478bd9Sstevel@tonic-gate _fini(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	int retv;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if ((retv = sppptun_tcl_fintest()) != 0)
1207c478bd9Sstevel@tonic-gate 		return (retv);
1217c478bd9Sstevel@tonic-gate 	retv = mod_remove(&modlinkage);
122002c70ffScarlsonj 	if (retv != 0)
1237c478bd9Sstevel@tonic-gate 		return (retv);
1247c478bd9Sstevel@tonic-gate 	sppptun_tcl_fini();
1257c478bd9Sstevel@tonic-gate 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate int
1297c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1307c478bd9Sstevel@tonic-gate {
131002c70ffScarlsonj 	return (mod_info(&modlinkage, modinfop));
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * tun_attach()
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * Description:
1387c478bd9Sstevel@tonic-gate  *    Attach a PPP tunnel driver to the system.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate static int
1417c478bd9Sstevel@tonic-gate tun_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1427c478bd9Sstevel@tonic-gate {
143002c70ffScarlsonj 	if (cmd != DDI_ATTACH)
1447c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1457c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, PPP_TUN_NAME, S_IFCHR, 0, DDI_PSEUDO,
1467c478bd9Sstevel@tonic-gate 	    CLONE_DEV) == DDI_FAILURE) {
1477c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
1487c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	tun_dev_info = dip;
1517c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * tun_detach()
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * Description:
1587c478bd9Sstevel@tonic-gate  *    Detach an interface to the system.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static int
1617c478bd9Sstevel@tonic-gate tun_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1627c478bd9Sstevel@tonic-gate {
163002c70ffScarlsonj 	if (cmd != DDI_DETACH)
1647c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1657c478bd9Sstevel@tonic-gate 	tun_dev_info = NULL;
1667c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
1677c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * tun_info()
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * Description:
1747c478bd9Sstevel@tonic-gate  *    Translate "dev_t" to a pointer to the associated "dev_info_t".
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate /* ARGSUSED */
1777c478bd9Sstevel@tonic-gate static int
1787c478bd9Sstevel@tonic-gate tun_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1797c478bd9Sstevel@tonic-gate 	void **result)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int	rc;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	switch (infocmd) {
1847c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1857c478bd9Sstevel@tonic-gate 		if (tun_dev_info == NULL) {
1867c478bd9Sstevel@tonic-gate 			rc = DDI_FAILURE;
1877c478bd9Sstevel@tonic-gate 		} else {
1887c478bd9Sstevel@tonic-gate 			*result = (void *)tun_dev_info;
1897c478bd9Sstevel@tonic-gate 			rc = DDI_SUCCESS;
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		break;
1927c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1937c478bd9Sstevel@tonic-gate 		*result = NULL;
1947c478bd9Sstevel@tonic-gate 		rc = DDI_SUCCESS;
1957c478bd9Sstevel@tonic-gate 		break;
1967c478bd9Sstevel@tonic-gate 	default:
1977c478bd9Sstevel@tonic-gate 		rc = DDI_FAILURE;
1987c478bd9Sstevel@tonic-gate 		break;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	return (rc);
2017c478bd9Sstevel@tonic-gate }
202