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
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#endif /* SOCK_TEST */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_acceptq_dequeue_locked(struct sonode *so, boolean_t dontblock,
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson while ((nso = list_remove_head(&so->so_acceptq_list)) == NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * No need to check so_error here, because it is not
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * possible for a listening socket to be reset or otherwise
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * disconnected.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * So now we just need check if it's ok to wait.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * int so_acceptq_dequeue(struct sonode *, boolean_t, struct sonode **)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Pulls a connection off of the accept queue.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Arguments:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so - listening socket
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * dontblock - indicate whether it's ok to sleep if there are no
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * connections on the queue
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * nsop - Value-return argument
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Return values:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * 0 when a connection is successfully dequeued, in which case nsop
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * is set to point to the new connection. Upon failure a non-zero
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * value is returned, and the value of nsop is set to NULL.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_acceptq_dequeue() may return prematurly if the socket is falling
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * back to TPI.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_acceptq_dequeue(struct sonode *so, boolean_t dontblock,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = so_acceptq_dequeue_locked(so, dontblock, nsop);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Perssonso_acceptq_flush_impl(struct sonode *so, list_t *list, boolean_t doclose)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson while ((nso = list_remove_head(list)) != NULL) {
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Only used for fallback - not possible when filters
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * are present.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Since the socket is on the accept queue, there can
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * only be one reference. We drop the reference and
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * just blow off the socket.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* drop the proto ref */
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * void so_acceptq_flush(struct sonode *so)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Removes all pending connections from a listening socket, and
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * frees the associated resources.
2320a8c1cca132d99d97f0f0025b5b4b4bfaf5b6Anders Persson * so - listening socket
2320a8c1cca132d99d97f0f0025b5b4b4bfaf5b6Anders Persson * doclose - make a close downcall for each socket on the accept queue
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Return values:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The caller has to ensure that no calls to so_acceptq_enqueue() or
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_acceptq_dequeue() occur while the accept queue is being flushed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * So either the socket needs to be in a state where no operations
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * would come in, or so_lock needs to be obtained.
2320a8c1cca132d99d97f0f0025b5b4b4bfaf5b6Anders Perssonso_acceptq_flush(struct sonode *so, boolean_t doclose)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so_acceptq_flush_impl(so, &so->so_acceptq_list, doclose);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson so_acceptq_flush_impl(so, &so->so_acceptq_defer, doclose);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_wait_connected_locked(struct sonode *so, boolean_t nonblock,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The protocol has notified us that a connection attempt is being
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * made, so before we wait for a notification to arrive we must
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * clear out any errors associated with earlier connection attempts.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_error != 0 && SOCK_CONNID_LT(so->so_proto_connid, id))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning while (SOCK_CONNID_LT(so->so_proto_connid, id)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (cv_wait_sig_swap(&so->so_state_cv, &so->so_lock) == 0)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Under normal circumstances, so_error should contain an error
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * in case the connect failed. However, it is possible for another
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * thread to come in a consume the error, so generate a sensible
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * error in that case.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * int so_wait_connected(struct sonode *so, boolean_t nonblock,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * sock_connid_t id)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Wait until the socket is connected or an error has occured.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Arguments:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so - socket
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * nonblock - indicate whether it's ok to sleep if the connection has
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * not yet been established
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * gen - generation number that was returned by the protocol
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * when the operation was started
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * 0 if the connection attempt was successful, or an error indicating why
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the connection attempt failed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_wait_connected(struct sonode *so, boolean_t nonblock, sock_connid_t id)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = so_wait_connected_locked(so, nonblock, id);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_snd_wait_qnotfull_locked(struct sonode *so, boolean_t dontblock)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Zero means disable timeout.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
d3d50737e566cade9a08d73d2af95105ac7cd960Rafael Vanoni &so->so_lock, so->so_sndtimeo, TR_CLOCK_TICK);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * int so_wait_sendbuf(struct sonode *so, boolean_t dontblock)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Wait for the transport to notify us about send buffers becoming
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * available.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_snd_wait_qnotfull(struct sonode *so, boolean_t dontblock)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson error = so_snd_wait_qnotfull_locked(so, dontblock);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* wake up everyone waiting for buffers */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Change the process/process group to which SIGIO is sent.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Permissions check by sending signal 0.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Note that when kill fails it does a
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * set_errno causing the system call to fail.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Generate a SIGIO, for 'writable' events include siginfo structure,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * for read events just send the signal.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(event & (SOCKETSIG_WRITE | SOCKETSIG_READ | SOCKETSIG_URG));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_pgrp == 0 || (!(so->so_state & SS_ASYNC) &&
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning dprint(3, ("sending sig %d to %d\n", event, so->so_pgrp));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * XXX This unfortunately still generates
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * a signal when a fd is closed but
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the proc is active.
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * Even if the thread started in another zone, we're receiving
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * on behalf of this socket's zone, so find the proc using the
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * socket's zone ID.
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald proc = prfind_zone(so->so_pgrp, so->so_zoneid);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Send to process group. Hold pidlock across
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * calls to socket_sigproc().
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * Even if the thread started in another zone, we're receiving
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * on behalf of this socket's zone, so find the pgrp using the
d690b62cf13675007342e6bb43d4816ec57a46ddDan McDonald * socket's zone ID.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* Copy userdata into a new mblk_t */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocopyinuio(uio_t *uiop, ssize_t iosize, size_t wroff, ssize_t maxblk,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (iosize == INFPSZ || iosize > uiop->uio_resid)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Nothing to do in these cases, so we're done */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (iosize < 0 || maxblk < 0 || (maxblk == 0 && iosize > 0))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We will enter the loop below if iosize is 0; it will allocate an
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * empty message block and call uiomove(9F) which will just return.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We could avoid that with an extra check but would only slow
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * down the much more likely case where iosize is larger than 0.
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark mp = allocb(wroff + blocksize + tail_len, BPRI_MED);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* uiomove(9F) either returns 0 or EFAULT */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if ((*errorp = uiomove(mp->b_rptr, (size_t)blocksize,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } while (iosize > 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocopyoutuio(mblk_t *mp, struct uio *uiop, ssize_t max_read, int *errorp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * max_read is the offset of the oobmark and read can not go pass
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the oobmark.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (max_read == INFPSZ || max_read > uiop->uio_resid)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = uiomove(mp->b_rptr, n, UIO_READ, uiop);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning while (mp != NULL && (mp->b_rptr >= mp->b_wptr)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * get rid of zero length mblks
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_prepend_msg(struct sonode *so, mblk_t *mp, mblk_t *last_tail)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(!(DB_FLAGS(so->so_rcv_q_head) & DBLK_UIOA));
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * Move a mblk chain (mp_head, mp_last_head) to the sonode's rcv queue so it
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * can be processed by so_dequeue_msg().
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Perssonso_process_new_message(struct sonode *so, mblk_t *mp_head, mblk_t *mp_last_head)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson (mp_head = sof_filter_data_in_proc(so, mp_head,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(so->so_rcv_q_last_head->b_prev != NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning boolean_t flag_equal = ((DB_FLAGS(mp_head) & DBLK_UIOA) ==
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (DB_FLAGS(so->so_rcv_q_last_head) & DBLK_UIOA));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning DB_TYPE(so->so_rcv_q_last_head) == M_DATA && flag_equal) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if (flag_equal && (DB_FLAGS(mp_head) & DBLK_UIOA)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Append to last_head if more than one mblks, and both
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * mp_head and last_head are I/OAT mblks.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_next = mp_head->b_next;
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * Check flow control on a given sonode. Must have so_lock held, and
a215d4eb400e2ff52f7a17e0781964c37aabfc04Kacheong Poon * this function will release the hold. Return true if flow control
a215d4eb400e2ff52f7a17e0781964c37aabfc04Kacheong Poon * is cleared.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson if (so->so_flowctrld && (so->so_rcv_queued < so->so_rcvlowat &&
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * Open up flow control. SCTP does not have any downcalls, and
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * it will clr flow ctrl in sosctp_recvmsg().
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* filters can start injecting data */
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson sof_sonode_notify_filters(so, SOF_EV_INJECT_DATA_IN_OK, 0);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_dequeue_msg(struct sonode *so, mblk_t **mctlp, struct uio *uiop,
8591a19ae7627d4f6ddb6168875db0d053972cbcAnders Persson /* Check whether the caller is OK to read past the mark */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * First move messages from the dump area to processing area
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* nothing to uioamove */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if (sodp->sod_uioa.uioa_state & UIOA_INIT) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * try to uioamove() the data that
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * has already queued.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We can release the lock as there can only be one reader
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson so_process_new_message(so, new_msg_head, new_msg_last_head);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * First process PROTO or PCPROTO blocks, if any.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Now process DATA blocks, if any. Note that for sodirect
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * enabled socket, uio_resid can be 0.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (sodp != NULL && (DB_FLAGS(mp) & DBLK_UIOA)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* mark this mblk as processed */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Can not read beyond the oobmark
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * so_check_flow_control() will drop
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * so->so_lock.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (mp != NULL) { /* more data blocks in msg */
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * so_check_flow_control() will drop
5795faa497d29c8b3efac35c426174aaf539605eRao Shoaib * so->so_lock.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if (partial_read && !somsghasdata(mp)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Avoid queuing a zero-length tail part of
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * a message. partial_read == 1 indicates that
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * we read some of the message.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * There should only be non data mblks
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * In case we
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * cannot copy
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * control data
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * free the remaining
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * putback mp
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* fast check so_rcv_head if there is more data */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (partial_read && !(so->so_state & SS_RCVATMARK) &&
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning !(flags & MSG_PEEK) && so->so_rcv_head != NULL) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if (!partial_read) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * No pending data. Return right away for nonblocking
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * socket, otherwise sleep waiting for data.
2caa659d84568b9b606156c014d2bd22273359d4Mike Cheng if (!(so->so_state & SS_CANTRCVMORE) && uiop->uio_resid > 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Zero means disable timeout.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (reset_atmark && partial_read && !(flags & MSG_PEEK)) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We are passed the mark, update state
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * 4.3BSD and 4.4BSD clears the mark when peeking across it.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The draft Posix socket spec states that the mark should
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * not be cleared when peeking. We follow the latter.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * Enqueue data from the protocol on the socket's rcv queue.
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * We try to hook new M_DATA mblks onto an existing chain, however,
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * that cannot be done if the existing chain has already been
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * processed by I/OAT. Non-M_DATA mblks are just linked together via
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * b_next. In all cases the b_prev of the enqueued mblk is set to
e4b767e87eacd08854eda4dbb32906316ba2db8bAnders Persson * point to the last mblk in its b_cont chain.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_enqueue_msg(struct sonode *so, mblk_t *mp, size_t msg_size)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (DB_FLAGS(so->so_rcv_last_head) & DBLK_UIOA))) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Added to the end */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Start a new end */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Return B_TRUE if there is data in the message, B_FALSE otherwise.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Flush the read side of sockfs.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The caller must be sure that a reader is not already active when the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * buffer is being flushed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|SS_RCVATMARK);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Free messages sitting in the recv queues
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", (void *)so, (void *)msg,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * There is never any oob data with addresses or control since
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the T_EXDATA_IND does not carry any options.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Since recv* can not return ENOBUFS we can not use dupmsg.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Instead we revert to the consolidation private
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * allocb_wait plus bcopy.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Update the state indicating that the data has been consumed.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Keep SS_OOBPEND set until data is consumed past the mark.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Allocate and initializ sonode
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_sonode_create(struct sockparams *sp, int family, int type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning int protocol, int version, int sflags, int *errorp, struct cred *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Choose the right set of sonodeops based on the upcall and
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * down call version that the protocol has provided
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (SOCK_UC_VERSION != sp->sp_smod_info->smod_uc_version ||
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning SOCK_DC_VERSION != sp->sp_smod_info->smod_dc_version) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cmn_err(CE_CONT, "protocol and socket module version mismatch");
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning kmflags = (sflags & SOCKET_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sonode_init(so, sp, family, type, protocol, &so_sonodeops);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * set the default values to be INFPSZ
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * if a protocol desires it can change the value later
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_proto_props.sopp_rxhiwat = SOCKET_RECVHIWATER;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_proto_props.sopp_rxlowat = SOCKET_RECVLOWATER;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_init_common(struct sonode *so, struct sonode *pso, int flags, cred_t *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We have a passive open, so inherit basic state from
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the parent (listener).
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * No need to grab the new sonode's lock, since there is no
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * one that can have a reference to it.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_state |= SS_ISCONNECTED | (pso->so_state & SS_ASYNC);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Make note of the socket level options. TCP and IP level
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * options are already inherited. We could do all this after
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * accept is successful but doing it here simplifies code and
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * no harm done for error case.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_options = pso->so_options & (SO_DEBUG|SO_REUSEADDR|
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning SO_KEEPALIVE|SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * If the parent has any filters, try to inherit them.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson (error = sof_sonode_inherit_filters(so, pso)) != 0)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Attach automatic filters, if there are any.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson (error = sof_sonode_autoattach_filters(so, cr)) != 0)
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* OK to attach filters */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Based on the version number select the right upcalls to
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * pass down. Currently we only have one version so choose
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* active open, so create a lower handle */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning sp->sp_smod_info->smod_proto_create_func(so->so_family,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_type, so->so_protocol, &so->so_downcalls,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * To be safe; if a lower handle cannot be created, and
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the proto does not give a reason why, assume there
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * was a lack of memory.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (*so->so_downcalls->sd_activate)(so->so_proto_handle,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning (sock_upper_handle_t)so, upcalls_to_use, 0, cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Wildcard */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * FIXME No need for this, the protocol can deal with it in
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * sd_create(). Should update ICMP.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so->so_protocol != so->so_sockparams->sp_protocol) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Issue SO_PROTOTYPE setsockopt.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = socket_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning &protocol, (t_uscalar_t)sizeof (protocol), cr);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Setsockopt often fails with ENOPROTOOPT but
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * socket() should fail with
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* put an extra reference on the socket for the protocol */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * int socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * struct cred *cr, int32_t *rvalp)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Handle ioctls that manipulate basic socket state; non-blocking,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * async, etc.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * < 0 - ioctl was not handle
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * >= 0 - ioctl was handled, if > 0, then it is an errno
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Assumes the standard receive buffer is used to obtain info for
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning/* ARGSUSED */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
bfcb55b84554e024ad218fb452e863d9f2acd644Rao Shoaib * SIOCSQPTR is valid only when helper stream is created
bfcb55b84554e024ad218fb452e863d9f2acd644Rao Shoaib * by the protocol.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so_copyin((void *)arg, &value, sizeof (int32_t),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Turn on SIGIO */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Turn off SIGIO */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so_copyin((void *)arg, &pid, sizeof (pid_t),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Only protocols that support urgent data can handle ATMARK.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * If the protocol is maintaining its own buffer, then the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * request must be passed down.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so_copyout(&retval, (void *)arg, sizeof (int),
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * If the protocol is maintaining its own buffer, then the
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * request must be passed down.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning } else if ((so->so_state & SS_ISCONNECTED) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Handle the I_NREAD STREAM ioctl.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Perssonso_strioc_nread(struct sonode *so, intptr_t arg, int mode, int32_t *rvalp)
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Wait for reader to get out of the way. */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * If reader is waiting for data, then there should be nothing
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * on the rcv queue.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Do a timed sleep, in case the reader goes to sleep. */
decd6ccd1e5609a8762c74262d82ce6523c6b19cAnders Persson (void) cv_reltimedwait(&so->so_read_cv, &so->so_lock, wakeup,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Since we are holding so_lock no new reader will come in, and the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * protocol will not be able to enqueue data. So it's safe to walk
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * both rcv queues.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * In case the processing list was empty, get the size of the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * next msg in line.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson for (mp = so->so_rcv_head; mp != NULL; mp = mp->b_next)
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Drop down from size_t to the "int" required by the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * interface. Cap at INT_MAX.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson if (so_copyout(&retval, (void *)arg, sizeof (retval),
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Process STREAM ioctls.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * < 0 - ioctl was not handle
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * >= 0 - ioctl was handled, if > 0, then it is an errno
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_strioc_common(struct sonode *so, int cmd, intptr_t arg, int mode,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Only STREAM iotcls are handled here */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * We return an error for I_CANPUT so that isastream(3C) will
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * not report the socket as being a STREAM.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Avoid doing a fallback for I_NREAD. */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson return (so_strioc_nread(so, arg, mode, rvalp));
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Avoid doing a fallback for I_LOOK. */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (so_copyout("sockmod", (void *)arg, strlen("sockmod") + 1,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Try to fall back to TPI, and if successful, reissue the ioctl.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson if ((retval = so_tpi_fallback(so, cr)) == 0) {
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Reissue the ioctl */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
2c632ad51ffa982737943af0e3c0abfdbe65f571Anders Persson * This is called for all socket types to verify that the buffer size is large
2c632ad51ffa982737943af0e3c0abfdbe65f571Anders Persson * enough for the option, and if we can, handle the request as well. Most
2c632ad51ffa982737943af0e3c0abfdbe65f571Anders Persson * options will be forwarded to the protocol.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningsocket_getopt_common(struct sonode *so, int level, int option_name,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning#endif /* notyet */
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning if ((flags & _SOGETSOCKOPT_XPG4_2) && so->so_xpg_rcvbuf != 0) {
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * XXX If SO_RCVBUF has been set and this is an
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * XPG 4.2 application then do not ask the transport
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * since the transport might adjust the value and not
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * return exactly what was set by the application.
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * For non-XPG 4.2 application we return the value
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * that the transport is actually using.
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * If the option has not been set then get a default
a5adac4d8d51bc60d7d7f5763966ce1290dc1277Yu Xiangning * value from the transport.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (optlen < (t_uscalar_t)sizeof (struct linger))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (optlen < (t_uscalar_t)sizeof (struct so_snd_bufinfo))
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ((struct so_snd_bufinfo *)(optval))->sbi_wroff =
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ((struct so_snd_bufinfo *)(optval))->sbi_maxblk =
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ((struct so_snd_bufinfo *)(optval))->sbi_maxpsz =
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ((struct so_snd_bufinfo *)(optval))->sbi_tail =
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Avoid zero-copy if there is a filter with a data_out
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * callback. We could let the operation succeed, but then
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * the filter would have to copy the data anyway.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Unknown Option */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return (-1);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (cv_wait_sig(&so->so_copy_cv, &so->so_lock) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Verify that the length stored in so_rcv_queued and the length of data blocks
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * queued is same.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning return ((len == so->so_rcv_queued) ? B_TRUE : B_FALSE);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning ASSERT(sp != NULL && sp->sp_smod_info != NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_start_fallback()
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Block new socket operations from coming in, and wait for active operations
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * to complete. Threads that are sleeping will be woken up so they can get
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * out of the way.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The caller must be a reader on so_fallback_rwlock.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Poke all threads that might be sleeping. Any operation that comes
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * in after the cv_broadcast will observe the fallback pending flag
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * which cause the call to return where it would normally sleep.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_broadcast(&so->so_state_cv); /* threads in connect() */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_broadcast(&so->so_rcv_cv); /* threads in recvmsg() */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_broadcast(&so->so_snd_cv); /* threads in sendmsg() */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning cv_broadcast(&so->so_acceptq_cv); /* threads in accept() */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The main reason for the rw_tryupgrade call is to provide
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * observability during the fallback process. We want to
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * be able to see if there are pending operations.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning if (rw_tryupgrade(&so->so_fallback_rwlock) == 0) {
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * It is safe to drop and reaquire the fallback lock, because
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * we are guaranteed that another fallback cannot take place.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning DTRACE_PROBE1(pending__ops__wait, (struct sonode *), so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning DTRACE_PROBE1(pending__ops__complete, (struct sonode *), so);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_end_fallback()
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Allow socket opertions back in.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * The caller must be a writer on so_fallback_rwlock.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson so->so_state &= ~(SS_FALLBACK_PENDING|SS_FALLBACK_DRAIN);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_quiesced_cb()
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Callback passed to the protocol during fallback. It is called once
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the endpoint is quiescent.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * No requests from the user, no notifications from the protocol, so it
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * is safe to synchronize the state. Data can also be moved without
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * risk for reordering.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * We do not need to hold so_lock, since there can be only one thread
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * operating on the sonode.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Perssonso_quiesced_cb(sock_upper_handle_t sock_handle, sock_quiesce_arg_t *arg,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sockaddr *faddr, socklen_t faddrlen, short opts)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning struct sonode *so = (struct sonode *)sock_handle;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson sotpi_update_state(so, tcap, laddr, laddrlen, faddr, faddrlen,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Some protocols do not quiece the data path during fallback. Once
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * we set the SS_FALLBACK_DRAIN flag any attempt to queue data will
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * fail and the protocol is responsible for saving the data for later
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * delivery (i.e., once the fallback has completed).
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning so->so_rcv_q_last_head->b_next = so->so_rcv_head;
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Clear any OOB state having to do with pending data. The TPI
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * code path will set the appropriate oob state when we move the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * oob data to the STREAM head. We leave SS_HADOOBDATA since the oob
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * data has already been consumed.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson ASSERT(so->so_oobmsg != NULL || so->so_oobmark <= so->so_rcv_queued);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Move data to the STREAM head.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Send T_EXDATA_IND if we are at the oob mark.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* process current mp next time around */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* we have consumed the oob mark */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Check if the OOB mark is within the current
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * mblk chain. In that case we have to split it up.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * It is assumed that the OOB mark does
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * not land within a mblk.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Queue data on the STREAM head.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Check if the oob byte is at the end of the data stream, or if the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * oob byte has not yet arrived. In the latter case we have to send a
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * SIGURG and a mark indicator to the STREAM head. The mark indicator
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * is needed to guarantee correct behavior for SIOCATMARK. See block
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * comment in socktpi.h for more details.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Send up the signal */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Send up the mark indicator */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson mp->b_flag = atmark ? MSGMARKNEXT : MSGNOTMARKNEXT;
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Do an integrity check of the sonode. This should be done if a
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * fallback fails after sonode has initially been converted to use
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * TPI and subsequently have to be reverted.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Failure to pass the integrity check will panic the system.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Perssonso_integrity_check(struct sonode *cur, struct sonode *orig)
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * For so_state we can only VERIFY the state flags in CHECK_STATE.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * The other state flags might be affected by a notification from the
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson#define CHECK_STATE (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_NDELAY|SS_NONBLOCK| \
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson SS_ASYNC|SS_ACCEPTCONN|SS_SAVEDEOR|SS_RCVATMARK|SS_OOBPEND| \
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson SS_HAVEOOBDATA|SS_HADOOBDATA|SS_SENTLASTREADSIG|SS_SENTLASTWRITESIG)
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY((cur->so_state & (orig->so_state & CHECK_STATE)) ==
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Cannot VERIFY so_proto_connid; proto can update it */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_sockparams == orig->so_sockparams);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* an error might have been recorded, but it can not be lost */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_error != 0 || orig->so_error == 0);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_protocol == orig->so_protocol);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* New conns might have arrived, but none should have been lost */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_acceptq_len >= orig->so_acceptq_len);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* New OOB migth have arrived, but mark should not have been lost */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Cannot VERIFY so_oobmsg; the proto might have sent up a new one */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_peercred == orig->so_peercred);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* New data migth have arrived, but none should have been lost */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_rcv_queued >= orig->so_rcv_queued);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_rcv_q_head == orig->so_rcv_q_head);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_rcv_head == orig->so_rcv_head);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_proto_handle == orig->so_proto_handle);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson VERIFY(cur->so_downcalls == orig->so_downcalls);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* Cannot VERIFY so_proto_props; they can be updated by proto */
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * so_tpi_fallback()
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * This is the fallback initation routine; things start here.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Basic strategy:
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * o Block new socket operations from coming in
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * o Allocate/initate info needed by TPI
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * o Quiesce the connection, at which point we sync
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * state and move data
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * o Change operations (sonodeops) associated with the socket
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * o Unblock threads waiting for the fallback to finish
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangningso_tpi_fallback(struct sonode *so, struct cred *cr)
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning fbfunc = sp->sp_smod_info->smod_proto_fallback_func;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Cannot fallback if the socket has active filters
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson devpath = sp->sp_smod_info->smod_fallback_devpath_v4;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson devpath = sp->sp_smod_info->smod_fallback_devpath_v6;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * Fallback can only happen if the socket module has a TPI device
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * and fallback function.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Initiate fallback; upon success we know that no new requests
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * will come in from the user.
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Make a copy of the sonode in case we need to make an integrity
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * check later on.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning newsp = sockparams_hold_ephemeral_bydev(so->so_family, so->so_type,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning /* Turn sonode into a TPI socket */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson error = sotpi_convert_sonode(so, newsp, &direct, &q, cr);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * When it comes to urgent data we have two cases to deal with;
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * (1) The oob byte has already arrived, or (2) the protocol has
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * notified that oob data is pending, but it has not yet arrived.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * For (1) all we need to do is send a T_EXDATA_IND to indicate were
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * in the byte stream the oob byte is. For (2) we have to send a
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * SIGURG (M_PCSIG), followed by a zero-length mblk indicating whether
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * the oob byte will be the next byte from the protocol.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * So in the worst case we need two mblks, one for the signal, another
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * for mark indication. In that case we use the exdata_mp for the sig.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson arg.soqa_exdata_mp = allocb_wait(sizeof (struct T_exdata_ind),
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson arg.soqa_urgmark_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Now tell the protocol to start using TPI. so_quiesced_cb be
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * called once it's safe to synchronize state.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning DTRACE_PROBE1(proto__fallback__begin, struct sonode *, so);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson error = (*fbfunc)(so->so_proto_handle, q, direct, so_quiesced_cb,
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning DTRACE_PROBE1(proto__fallback__end, struct sonode *, so);
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson /* protocol was unable to do a fallback, revert the sonode */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Walk the accept queue and notify the proto that they should
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * fall back to TPI. The protocol will send up the T_CONN_IND.
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson arg.soqa_urgmark_mp = allocb_wait(0, BPRI_MED,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson DTRACE_PROBE1(proto__fallback__begin, struct sonode *, nso);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson rval = (*fbfunc)(nso->so_proto_handle, NULL, direct,
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson DTRACE_PROBE1(proto__fallback__end, struct sonode *, nso);
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson /* Abort the connection */
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson "Failed to convert socket in accept queue to TPI. "
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * Now flush the acceptq, this will destroy all sockets. They will
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson * be recreated in sotpi_accept().
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Swap the sonode ops. Socket opertations that come in once this
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * is done will proceed without blocking.
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * Wake up any threads stuck in poll. This is needed since the poll
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * head changes when the fallback happens (moves from the sonode to
0f1702c5201310f0529cd5abb77652e5e9b241b6Yu Xiangning * the STREAMS head).
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * When this non-STREAM socket was created we placed an extra ref on
3e95bd4ab92abca814bd28e854607d1975c7dc88Anders Persson * the associated vnode to support asynchronous close. Drop that ref
4117443759eb8485e3cfd93459f86a41ea241d20Anders Persson "Failed to convert socket to TPI (err=%d). Pid = %d\n",