sockcommon.c revision 6a571a2dc9fd3dada6de18b4c74cc8dcfca99eda
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * CDDL HEADER START
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The contents of this file are subject to the terms of the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Common Development and Distribution License (the "License").
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * You may not use this file except in compliance with the License.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * See the License for the specific language governing permissions
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * and limitations under the License.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * When distributing Covered Code, include this CDDL HEADER in each
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * If applicable, add the following below this CDDL HEADER, with the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * fields enclosed by brackets "[]" replaced with your own identifying
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * information: Portions Copyright [yyyy] [name of copyright owner]
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * CDDL HEADER END
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Use is subject to license terms.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningextern int xnet_skip_checks, xnet_check_print, xnet_truncate_print;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Common socket access functions.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Instead of accessing the sonode switch directly (i.e., SOP_xxx()),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the socket_xxx() function should be used.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Try to create a new sonode of the requested <family, type, protocol>.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_create(int family, int type, int protocol, char *devpath, char *mod,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int flags, int version, struct cred *cr, int *errorp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Look for a sockparams entry that match the given criteria.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * solookup() returns with the entry held.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning *errorp = solookup(family, type, protocol, &sp);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int kmflags = (flags == SOCKET_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * There is no matching sockparams entry. An ephemeral entry is
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * created if the caller specifies a device or a socket module.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp = sockparams_hold_ephemeral_bydev(family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp = sockparams_hold_ephemeral_bymod(family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(flags == SOCKET_SLEEP || flags == SOCKET_NOSLEEP);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = sp->sp_smod_info->smod_sock_create_func(sp, family, type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((*errorp = SOP_INIT(so, NULL, cr, flags)) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Cannot fail, only bumps so_count */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, cr, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_newconn(struct sonode *parent, sock_lower_handle_t lh,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so = sp->sp_smod_info->smod_sock_create_func(sp, parent->so_family,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning parent->so_type, parent->so_protocol, parent->so_version, flags,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * This function may be called in interrupt context, and CRED()
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * will be NULL. In this case, pass in kcred.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((*errorp = SOP_INIT(so, parent, cr, flags)) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Cannot fail, only bumps so_count */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, cr, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Bind local endpoint.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_BIND(so, name, namelen, flags, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Turn socket into a listen socket.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_listen(struct sonode *so, int backlog, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Use the same qlimit as in BSD. BSD checks the qlimit
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * before queuing the next connection implying that a
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * listen(sock, 0) allows one connection to be queued.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * BSD also uses 1.5 times the requested backlog.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * XNS Issue 4 required a strict interpretation of the backlog.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * This has been waived subsequently for Issue 4 and the change
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * incorporated in XNS Issue 5. So we aren't required to do
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * anything special for XPG apps.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Accept incoming connection.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_accept(struct sonode *lso, int fflag, cred_t *cr, struct sonode **nsop)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Active open.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_connect(struct sonode *so, const struct sockaddr *name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socklen_t namelen, int fflag, int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Handle a connect to a name parameter of type AF_UNSPEC like a
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * connect to a null address. This is the portable method to
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * unconnect a socket.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = SOP_CONNECT(so, name, namelen, fflag, flags, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (error == EHOSTUNREACH && flags & _SOCONNECT_XPG4_2) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * X/Open specification contains a requirement that
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * ENETUNREACH be returned but does not require
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * EHOSTUNREACH. In order to keep the test suite
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * happy we mess with the errno here.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get address of remote node.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getpeername(struct sonode *so, struct sockaddr *addr,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning socklen_t *addrlen, boolean_t accept, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETPEERNAME(so, addr, addrlen, accept, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get local address.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getsockname(struct sonode *so, struct sockaddr *addr,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETSOCKNAME(so, addr, addrlen, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Called from shutdown().
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_shutdown(struct sonode *so, int how, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Get socket options.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getsockopt(struct sonode *so, int level, int option_name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning void *optval, socklen_t *optlenp, int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_GETSOCKOPT(so, level, option_name, optval,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Set socket options
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_setsockopt(struct sonode *so, int level, int option_name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning const void *optval, t_uscalar_t optlen, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Caller allocates aligned optval, or passes null */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* If optval is null optlen is 0, and vice-versa */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_SETSOCKOPT(so, level, option_name, optval, optlen, cr));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Do not bypass the cache if we are doing a local (AF_UNIX) write.
34dfe6834cf56b6816de4f0528362e98e322c5a1shenjian /* EAGAIN is EWOULDBLOCK */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* We did a partial send */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Do not bypass the cache when reading data, as the application
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * is likely to access the data shortly.
34dfe6834cf56b6816de4f0528362e98e322c5a1shenjian /* EAGAIN is EWOULDBLOCK */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* We did a partial read */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_poll(struct sonode *so, short events, int anyyet, short *reventsp,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (SOP_POLL(so, events, anyyet, reventsp, phpp));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_close(struct sonode *so, int flag, struct cred *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (VOP_CLOSE(SOTOV(so), flag, 1, 0, cr, NULL));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_close_internal(struct sonode *so, int flag, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_destroy_internal(struct sonode *so, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * TODO Once the common vnode ops is available, then the vnops argument
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * should be removed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_constructor(void *buf, void *cdrarg, int kmflags)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_timer_interval = SOCKET_NO_RCVTIMER;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_init(&so->so_lock, NULL, MUTEX_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mutex_init(&so->so_acceptq_lock, NULL, MUTEX_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning rw_init(&so->so_fallback_rwlock, NULL, RW_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_state_cv, NULL, CV_DEFAULT, NULL);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_init(&so->so_single_cv, NULL, CV_DEFAULT, NULL);
6a571a2dc9fd3dada6de18b4c74cc8dcfca99edaAnders Persson cv_init(&so->so_read_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_acceptq_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_snd_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_rcv_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_copy_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_init(&so->so_closing_cv, NULL, CV_DEFAULT, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_acceptq_tail == &so->so_acceptq_head);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsonode_init(struct sonode *so, struct sockparams *sp, int family,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_acceptq_tail == &so->so_acceptq_head);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&so->so_poll_list, sizeof (so->so_poll_list));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&so->so_proto_props, sizeof (struct sock_proto_props));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning bzero(&(so->so_ksock_callbacks), sizeof (ksocket_callbacks_t));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_max_addr_len = sizeof (struct sockaddr_storage);