xref: /onnv-gate/usr/src/uts/common/io/sock_conf.c (revision 12643:044ff822d212)
18348SEric.Yu@Sun.COM /*
28348SEric.Yu@Sun.COM  * CDDL HEADER START
38348SEric.Yu@Sun.COM  *
48348SEric.Yu@Sun.COM  * The contents of this file are subject to the terms of the
58348SEric.Yu@Sun.COM  * Common Development and Distribution License (the "License").
68348SEric.Yu@Sun.COM  * You may not use this file except in compliance with the License.
78348SEric.Yu@Sun.COM  *
88348SEric.Yu@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98348SEric.Yu@Sun.COM  * or http://www.opensolaris.org/os/licensing.
108348SEric.Yu@Sun.COM  * See the License for the specific language governing permissions
118348SEric.Yu@Sun.COM  * and limitations under the License.
128348SEric.Yu@Sun.COM  *
138348SEric.Yu@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
148348SEric.Yu@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158348SEric.Yu@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
168348SEric.Yu@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
178348SEric.Yu@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
188348SEric.Yu@Sun.COM  *
198348SEric.Yu@Sun.COM  * CDDL HEADER END
208348SEric.Yu@Sun.COM  */
218348SEric.Yu@Sun.COM /*
22*12643SAnders.Persson@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
238348SEric.Yu@Sun.COM  */
248348SEric.Yu@Sun.COM 
258348SEric.Yu@Sun.COM #include <sys/sysmacros.h>
268348SEric.Yu@Sun.COM #include <sys/atomic.h>
278348SEric.Yu@Sun.COM #include <sys/strsubr.h>
288348SEric.Yu@Sun.COM #include <sys/socket.h>
298348SEric.Yu@Sun.COM #include <sys/socketvar.h>
308348SEric.Yu@Sun.COM #include <sys/cmn_err.h>
318348SEric.Yu@Sun.COM #include <sys/modctl.h>
328348SEric.Yu@Sun.COM #include <sys/sdt.h>
338348SEric.Yu@Sun.COM 
348348SEric.Yu@Sun.COM list_t smod_list;
358348SEric.Yu@Sun.COM kmutex_t smod_list_lock;
368348SEric.Yu@Sun.COM 
378348SEric.Yu@Sun.COM so_create_func_t sock_comm_create_function;
388348SEric.Yu@Sun.COM so_destroy_func_t sock_comm_destroy_function;
398348SEric.Yu@Sun.COM 
408348SEric.Yu@Sun.COM static smod_info_t *smod_create(const char *);
418348SEric.Yu@Sun.COM static void smod_destroy(smod_info_t *);
428348SEric.Yu@Sun.COM 
438348SEric.Yu@Sun.COM extern void smod_add(smod_info_t *);
448348SEric.Yu@Sun.COM 
458348SEric.Yu@Sun.COM void
smod_init(void)468348SEric.Yu@Sun.COM smod_init(void)
478348SEric.Yu@Sun.COM {
488348SEric.Yu@Sun.COM 	list_create(&smod_list, sizeof (smod_info_t),
498348SEric.Yu@Sun.COM 	    offsetof(smod_info_t, smod_node));
508348SEric.Yu@Sun.COM 	mutex_init(&smod_list_lock, NULL, MUTEX_DEFAULT, NULL);
518348SEric.Yu@Sun.COM }
528348SEric.Yu@Sun.COM 
538348SEric.Yu@Sun.COM static smod_info_t *
smod_find(const char * modname)548348SEric.Yu@Sun.COM smod_find(const char *modname)
558348SEric.Yu@Sun.COM {
568348SEric.Yu@Sun.COM 	smod_info_t *smodp;
578348SEric.Yu@Sun.COM 
588348SEric.Yu@Sun.COM 	ASSERT(MUTEX_HELD(&smod_list_lock));
598348SEric.Yu@Sun.COM 
608348SEric.Yu@Sun.COM 	for (smodp = list_head(&smod_list); smodp != NULL;
618348SEric.Yu@Sun.COM 	    smodp = list_next(&smod_list, smodp))
628348SEric.Yu@Sun.COM 		if (strcmp(smodp->smod_name, modname) == 0)
638348SEric.Yu@Sun.COM 			return (smodp);
648348SEric.Yu@Sun.COM 	return (NULL);
658348SEric.Yu@Sun.COM }
668348SEric.Yu@Sun.COM 
678348SEric.Yu@Sun.COM /*
688348SEric.Yu@Sun.COM  * Register the socket module.
698348SEric.Yu@Sun.COM  */
708348SEric.Yu@Sun.COM int
smod_register(const smod_reg_t * reg)718348SEric.Yu@Sun.COM smod_register(const smod_reg_t *reg)
728348SEric.Yu@Sun.COM {
738348SEric.Yu@Sun.COM 	smod_info_t	*smodp;
748348SEric.Yu@Sun.COM 
758348SEric.Yu@Sun.COM 	/*
768348SEric.Yu@Sun.COM 	 * Make sure the socket module does not depend on capabilities
778348SEric.Yu@Sun.COM 	 * not available on the system.
788348SEric.Yu@Sun.COM 	 */
798348SEric.Yu@Sun.COM 	if (reg->smod_version != SOCKMOD_VERSION ||
808348SEric.Yu@Sun.COM 	    reg->smod_dc_version != SOCK_DC_VERSION ||
818348SEric.Yu@Sun.COM 	    reg->smod_uc_version != SOCK_UC_VERSION) {
828348SEric.Yu@Sun.COM 		cmn_err(CE_WARN,
838348SEric.Yu@Sun.COM 		    "Failed to register socket module %s: version mismatch",
848348SEric.Yu@Sun.COM 		    reg->smod_name);
858348SEric.Yu@Sun.COM 		return (EINVAL);
868348SEric.Yu@Sun.COM 	}
878348SEric.Yu@Sun.COM 
888348SEric.Yu@Sun.COM #ifdef DEBUG
898348SEric.Yu@Sun.COM 	mutex_enter(&smod_list_lock);
908348SEric.Yu@Sun.COM 	if ((smodp = smod_find(reg->smod_name)) != NULL) {
918348SEric.Yu@Sun.COM 		mutex_exit(&smod_list_lock);
928348SEric.Yu@Sun.COM 		return (EEXIST);
938348SEric.Yu@Sun.COM 	}
948348SEric.Yu@Sun.COM 	mutex_exit(&smod_list_lock);
958348SEric.Yu@Sun.COM #endif
968348SEric.Yu@Sun.COM 
978348SEric.Yu@Sun.COM 	smodp = smod_create(reg->smod_name);
988348SEric.Yu@Sun.COM 	smodp->smod_version = reg->smod_version;
998348SEric.Yu@Sun.COM 	if (strcmp(smodp->smod_name, SOTPI_SMOD_NAME) == 0 ||
1008348SEric.Yu@Sun.COM 	    strcmp(smodp->smod_name, "socksctp") == 0 ||
1018348SEric.Yu@Sun.COM 	    strcmp(smodp->smod_name, "socksdp") == 0) {
1028348SEric.Yu@Sun.COM 		ASSERT(smodp->smod_proto_create_func == NULL);
1038348SEric.Yu@Sun.COM 		ASSERT(reg->__smod_priv != NULL);
1048348SEric.Yu@Sun.COM 		smodp->smod_sock_create_func =
1058348SEric.Yu@Sun.COM 		    reg->__smod_priv->smodp_sock_create_func;
1068348SEric.Yu@Sun.COM 		smodp->smod_sock_destroy_func =
1078348SEric.Yu@Sun.COM 		    reg->__smod_priv->smodp_sock_destroy_func;
1088348SEric.Yu@Sun.COM 		smodp->smod_proto_create_func = NULL;
1098348SEric.Yu@Sun.COM 	} else {
1108348SEric.Yu@Sun.COM 		if (reg->smod_proto_create_func == NULL ||
1118348SEric.Yu@Sun.COM 		    (reg->__smod_priv != NULL &&
1128348SEric.Yu@Sun.COM 		    (reg->__smod_priv->smodp_sock_create_func != NULL ||
1138348SEric.Yu@Sun.COM 		    reg->__smod_priv->smodp_sock_destroy_func != NULL))) {
1148348SEric.Yu@Sun.COM #ifdef DEBUG
1158348SEric.Yu@Sun.COM 			cmn_err(CE_CONT, "smod_register of %s failed",
1168348SEric.Yu@Sun.COM 			    smodp->smod_name);
1178348SEric.Yu@Sun.COM #endif
1188348SEric.Yu@Sun.COM 			smod_destroy(smodp);
1198348SEric.Yu@Sun.COM 			return (EINVAL);
1208348SEric.Yu@Sun.COM 		}
1218348SEric.Yu@Sun.COM 		smodp->smod_proto_create_func = reg->smod_proto_create_func;
1228348SEric.Yu@Sun.COM 		smodp->smod_sock_create_func = sock_comm_create_function;
1238348SEric.Yu@Sun.COM 		smodp->smod_sock_destroy_func = sock_comm_destroy_function;
1248348SEric.Yu@Sun.COM 		smodp->smod_uc_version = reg->smod_uc_version;
1258348SEric.Yu@Sun.COM 		smodp->smod_dc_version = reg->smod_dc_version;
1268348SEric.Yu@Sun.COM 		if (reg->__smod_priv != NULL) {
1278348SEric.Yu@Sun.COM 			smodp->smod_proto_fallback_func =
1288348SEric.Yu@Sun.COM 			    reg->__smod_priv->smodp_proto_fallback_func;
129*12643SAnders.Persson@Sun.COM 			smodp->smod_fallback_devpath_v4 =
130*12643SAnders.Persson@Sun.COM 			    reg->__smod_priv->smodp_fallback_devpath_v4;
131*12643SAnders.Persson@Sun.COM 			smodp->smod_fallback_devpath_v6 =
132*12643SAnders.Persson@Sun.COM 			    reg->__smod_priv->smodp_fallback_devpath_v6;
1338348SEric.Yu@Sun.COM 		}
1348348SEric.Yu@Sun.COM 	}
1358348SEric.Yu@Sun.COM 	smod_add(smodp);
1368348SEric.Yu@Sun.COM 	return (0);
1378348SEric.Yu@Sun.COM }
1388348SEric.Yu@Sun.COM 
1398348SEric.Yu@Sun.COM /*
1408348SEric.Yu@Sun.COM  * Unregister the socket module
1418348SEric.Yu@Sun.COM  */
1428348SEric.Yu@Sun.COM int
smod_unregister(const char * mod_name)1438348SEric.Yu@Sun.COM smod_unregister(const char *mod_name)
1448348SEric.Yu@Sun.COM {
1458348SEric.Yu@Sun.COM 	smod_info_t 	*smodp;
1468348SEric.Yu@Sun.COM 
1478348SEric.Yu@Sun.COM 	mutex_enter(&smod_list_lock);
1488348SEric.Yu@Sun.COM 	if ((smodp = smod_find(mod_name)) != NULL) {
1498348SEric.Yu@Sun.COM 		if (smodp->smod_refcnt != 0) {
1508348SEric.Yu@Sun.COM 			mutex_exit(&smod_list_lock);
1518348SEric.Yu@Sun.COM 			return (EBUSY);
1528348SEric.Yu@Sun.COM 		} else {
1538348SEric.Yu@Sun.COM 			/*
1548348SEric.Yu@Sun.COM 			 * Delete the entry from the socket module list.
1558348SEric.Yu@Sun.COM 			 */
1568348SEric.Yu@Sun.COM 			list_remove(&smod_list, smodp);
1578348SEric.Yu@Sun.COM 			mutex_exit(&smod_list_lock);
1588348SEric.Yu@Sun.COM 
1598348SEric.Yu@Sun.COM 			smod_destroy(smodp);
1608348SEric.Yu@Sun.COM 			return (0);
1618348SEric.Yu@Sun.COM 		}
1628348SEric.Yu@Sun.COM 	}
1638348SEric.Yu@Sun.COM 	mutex_exit(&smod_list_lock);
1648348SEric.Yu@Sun.COM 
1658348SEric.Yu@Sun.COM 	return (ENXIO);
1668348SEric.Yu@Sun.COM }
1678348SEric.Yu@Sun.COM 
1688348SEric.Yu@Sun.COM /*
1698348SEric.Yu@Sun.COM  * Initialize the socket module entry.
1708348SEric.Yu@Sun.COM  */
1718348SEric.Yu@Sun.COM static smod_info_t *
smod_create(const char * modname)1728348SEric.Yu@Sun.COM smod_create(const char *modname)
1738348SEric.Yu@Sun.COM {
1748348SEric.Yu@Sun.COM 	smod_info_t *smodp;
1758348SEric.Yu@Sun.COM 	int len;
1768348SEric.Yu@Sun.COM 
1778348SEric.Yu@Sun.COM 	smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
1788348SEric.Yu@Sun.COM 	len = strlen(modname) + 1;
1798348SEric.Yu@Sun.COM 	smodp->smod_name = kmem_alloc(len, KM_SLEEP);
1808348SEric.Yu@Sun.COM 	bcopy(modname, smodp->smod_name, len);
1818348SEric.Yu@Sun.COM 	smodp->smod_name[len - 1] = '\0';
1828348SEric.Yu@Sun.COM 	return (smodp);
1838348SEric.Yu@Sun.COM }
1848348SEric.Yu@Sun.COM 
1858348SEric.Yu@Sun.COM /*
1868348SEric.Yu@Sun.COM  * Clean up the socket module part of the sockparams entry.
1878348SEric.Yu@Sun.COM  */
1888348SEric.Yu@Sun.COM static void
smod_destroy(smod_info_t * smodp)1898348SEric.Yu@Sun.COM smod_destroy(smod_info_t *smodp)
1908348SEric.Yu@Sun.COM {
1918348SEric.Yu@Sun.COM 	ASSERT(smodp->smod_name != NULL);
1928348SEric.Yu@Sun.COM 	ASSERT(smodp->smod_refcnt == 0);
1938348SEric.Yu@Sun.COM 	ASSERT(!list_link_active(&smodp->smod_node));
1948348SEric.Yu@Sun.COM 	ASSERT(strcmp(smodp->smod_name, "socktpi") != 0);
1958348SEric.Yu@Sun.COM 
1968348SEric.Yu@Sun.COM 	kmem_free(smodp->smod_name, strlen(smodp->smod_name) + 1);
1978348SEric.Yu@Sun.COM 	smodp->smod_name = NULL;
1988348SEric.Yu@Sun.COM 	smodp->smod_proto_create_func = NULL;
1998348SEric.Yu@Sun.COM 	smodp->smod_sock_create_func = NULL;
2008348SEric.Yu@Sun.COM 	smodp->smod_sock_destroy_func = NULL;
2018348SEric.Yu@Sun.COM 	kmem_free(smodp, sizeof (*smodp));
2028348SEric.Yu@Sun.COM }
2038348SEric.Yu@Sun.COM 
2048348SEric.Yu@Sun.COM /*
2058348SEric.Yu@Sun.COM  * Add an entry at the front of the socket module list.
2068348SEric.Yu@Sun.COM  */
2078348SEric.Yu@Sun.COM void
smod_add(smod_info_t * smodp)2088348SEric.Yu@Sun.COM smod_add(smod_info_t *smodp)
2098348SEric.Yu@Sun.COM {
2108348SEric.Yu@Sun.COM 	ASSERT(smodp != NULL);
2118348SEric.Yu@Sun.COM 	mutex_enter(&smod_list_lock);
2128348SEric.Yu@Sun.COM 	list_insert_head(&smod_list, smodp);
2138348SEric.Yu@Sun.COM 	mutex_exit(&smod_list_lock);
2148348SEric.Yu@Sun.COM }
2158348SEric.Yu@Sun.COM 
2168348SEric.Yu@Sun.COM /*
2178348SEric.Yu@Sun.COM  * Lookup the socket module table by the socket module name.
2188348SEric.Yu@Sun.COM  * If there is an existing entry, then increase the reference count.
2198348SEric.Yu@Sun.COM  * Otherwise we load the module and in the module register function create
2208348SEric.Yu@Sun.COM  * a new entry and add it to the end of the socket module table.
2218348SEric.Yu@Sun.COM  */
2228348SEric.Yu@Sun.COM smod_info_t *
smod_lookup_byname(const char * modname)2238348SEric.Yu@Sun.COM smod_lookup_byname(const char *modname)
2248348SEric.Yu@Sun.COM {
2258348SEric.Yu@Sun.COM 	smod_info_t *smodp;
2268348SEric.Yu@Sun.COM 	int error;
2278348SEric.Yu@Sun.COM 
2288348SEric.Yu@Sun.COM again:
2298348SEric.Yu@Sun.COM 	/*
2308348SEric.Yu@Sun.COM 	 * If find an entry, increase the reference count and
2318348SEric.Yu@Sun.COM 	 * return the entry pointer.
2328348SEric.Yu@Sun.COM 	 */
2338348SEric.Yu@Sun.COM 	mutex_enter(&smod_list_lock);
2348348SEric.Yu@Sun.COM 	if ((smodp = smod_find(modname)) != NULL) {
2358348SEric.Yu@Sun.COM 		SMOD_INC_REF(smodp);
2368348SEric.Yu@Sun.COM 		mutex_exit(&smod_list_lock);
2378348SEric.Yu@Sun.COM 		return (smodp);
2388348SEric.Yu@Sun.COM 	}
2398348SEric.Yu@Sun.COM 	mutex_exit(&smod_list_lock);
2408348SEric.Yu@Sun.COM 
2418348SEric.Yu@Sun.COM 	/*
2428348SEric.Yu@Sun.COM 	 * We have a sockmod, and it is not loaded.
2438348SEric.Yu@Sun.COM 	 * Load the module into the kernel, modload() will
2448348SEric.Yu@Sun.COM 	 * take care of the multiple threads.
2458348SEric.Yu@Sun.COM 	 */
2468348SEric.Yu@Sun.COM 	DTRACE_PROBE1(load__socket__module, char *, modname);
2478348SEric.Yu@Sun.COM 	error = modload(SOCKMOD_PATH, modname);
2488348SEric.Yu@Sun.COM 	if (error == -1) {
2498348SEric.Yu@Sun.COM 		cmn_err(CE_CONT, "modload of %s/%s failed",
2508348SEric.Yu@Sun.COM 		    SOCKMOD_PATH, modname);
2518348SEric.Yu@Sun.COM 		return (NULL);
2528348SEric.Yu@Sun.COM 	}
2538348SEric.Yu@Sun.COM 	goto again;
2548348SEric.Yu@Sun.COM }
255