xref: /onnv-gate/usr/src/lib/libnsl/nsl/t_sndv.c (revision 132:e3f7eaf7dde4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*132Srobinson 
230Sstevel@tonic-gate /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include "mt.h"
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <stropts.h>
350Sstevel@tonic-gate #include <sys/stream.h>
360Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
370Sstevel@tonic-gate #include <sys/tihdr.h>
380Sstevel@tonic-gate #include <sys/timod.h>
390Sstevel@tonic-gate #include <xti.h>
400Sstevel@tonic-gate #include <assert.h>
410Sstevel@tonic-gate #include <syslog.h>
420Sstevel@tonic-gate #include "tx.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * t_snd.c and t_sndv.c are very similar and contain common code.
470Sstevel@tonic-gate  * Any changes to either of them should be reviewed to see whether they
480Sstevel@tonic-gate  * are applicable to the other file.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate int
510Sstevel@tonic-gate _tx_sndv(int fd, const struct t_iovec *tiov, unsigned int tiovcount,
520Sstevel@tonic-gate     int flags, int api_semantics)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	struct T_data_req datareq;
550Sstevel@tonic-gate 	struct strbuf ctlbuf, databuf;
560Sstevel@tonic-gate 	unsigned int bytes_sent, bytes_remaining, bytes_to_send, nbytes;
570Sstevel@tonic-gate 	char *curptr;
580Sstevel@tonic-gate 	struct iovec iov[T_IOV_MAX];
590Sstevel@tonic-gate 	int iovcount;
600Sstevel@tonic-gate 	char *dataptr;
610Sstevel@tonic-gate 	int first_time;
620Sstevel@tonic-gate 	struct _ti_user *tiptr;
630Sstevel@tonic-gate 	int band;
640Sstevel@tonic-gate 	int retval, lookevent;
650Sstevel@tonic-gate 	int sv_errno;
660Sstevel@tonic-gate 	int doputmsg = 0;
670Sstevel@tonic-gate 	int32_t tsdu_limit;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	assert(api_semantics == TX_XTI_XNS5_API);
70*132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
710Sstevel@tonic-gate 		return (-1);
720Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (tiptr->ti_servtype == T_CLTS) {
750Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
760Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
770Sstevel@tonic-gate 		return (-1);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
810Sstevel@tonic-gate 		t_errno = TBADDATA;
820Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
830Sstevel@tonic-gate 		return (-1);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
86*132Srobinson 	if (!(tiptr->ti_state == T_DATAXFER ||
870Sstevel@tonic-gate 	    tiptr->ti_state == T_INREL)) {
880Sstevel@tonic-gate 		t_errno = TOUTSTATE;
890Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
900Sstevel@tonic-gate 		return (-1);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * XXX
940Sstevel@tonic-gate 	 * Is it OK to do this TBADFLAG check when XTI spec
950Sstevel@tonic-gate 	 * is being extended with new and interesting flags
960Sstevel@tonic-gate 	 * everyday ?
970Sstevel@tonic-gate 	 */
980Sstevel@tonic-gate 	if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
990Sstevel@tonic-gate 		t_errno = TBADFLAG;
1000Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1010Sstevel@tonic-gate 		return (-1);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	if (flags & T_EXPEDITED)
1040Sstevel@tonic-gate 		tsdu_limit = tiptr->ti_etsdusize;
1050Sstevel@tonic-gate 	else {
1060Sstevel@tonic-gate 		/* normal data */
1070Sstevel@tonic-gate 		tsdu_limit = tiptr->ti_tsdusize;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * nbytes is the sum of the bytecounts in the tiov vector
1120Sstevel@tonic-gate 	 * A value larger than INT_MAX is truncated to INT_MAX by
1130Sstevel@tonic-gate 	 * _t_bytecount_upto_intmax()
1140Sstevel@tonic-gate 	 */
1150Sstevel@tonic-gate 	nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if ((tsdu_limit > 0) && /* limit meaningful and ... */
1180Sstevel@tonic-gate 	    (nbytes > (uint32_t)tsdu_limit)) {
1190Sstevel@tonic-gate 		t_errno = TBADDATA;
1200Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1210Sstevel@tonic-gate 		return (-1);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * Check for incoming disconnect only. XNS Issue 5 makes it optional
1260Sstevel@tonic-gate 	 * to check for incoming orderly release
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 	lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
1290Sstevel@tonic-gate 	if (lookevent < 0) {
1300Sstevel@tonic-gate 		sv_errno = errno;
1310Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1320Sstevel@tonic-gate 		errno = sv_errno;
1330Sstevel@tonic-gate 		return (-1);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	if (lookevent == T_DISCONNECT) {
1360Sstevel@tonic-gate 		t_errno = TLOOK;
1370Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1380Sstevel@tonic-gate 		return (-1);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/* sending zero length data when not allowed */
1420Sstevel@tonic-gate 	if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
1430Sstevel@tonic-gate 		t_errno = TBADDATA;
1440Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1450Sstevel@tonic-gate 		return (-1);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (doputmsg) {
1510Sstevel@tonic-gate 		/*
1520Sstevel@tonic-gate 		 * Initialize ctlbuf for use in sending/receiving control part
1530Sstevel@tonic-gate 		 * of the message.
1540Sstevel@tonic-gate 		 */
1550Sstevel@tonic-gate 		ctlbuf.maxlen = (int)sizeof (struct T_data_req);
1560Sstevel@tonic-gate 		ctlbuf.len = (int)sizeof (struct T_data_req);
1570Sstevel@tonic-gate 		ctlbuf.buf = (char *)&datareq;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		band = TI_NORMAL; /* band 0 */
1600Sstevel@tonic-gate 		if (flags & T_EXPEDITED) {
1610Sstevel@tonic-gate 			datareq.PRIM_type = T_EXDATA_REQ;
162*132Srobinson 			if (!(tiptr->ti_prov_flag & EXPINLINE))
1630Sstevel@tonic-gate 				band = TI_EXPEDITED; /* band > 0 */
1640Sstevel@tonic-gate 		} else
1650Sstevel@tonic-gate 			datareq.PRIM_type = T_DATA_REQ;
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * Allocate a databuffer into which we will gather the
1680Sstevel@tonic-gate 		 * input vector data, and make a call to putmsg(). We
1690Sstevel@tonic-gate 		 * do this since we don't have the equivalent of a putmsgv()
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		if (nbytes != 0) {
1720Sstevel@tonic-gate 			if ((dataptr = malloc((size_t)nbytes)) == NULL) {
1730Sstevel@tonic-gate 				sv_errno = errno;
1740Sstevel@tonic-gate 				sig_mutex_unlock(&tiptr->ti_lock);
1750Sstevel@tonic-gate 				errno = sv_errno;
1760Sstevel@tonic-gate 				t_errno = TSYSERR;
1770Sstevel@tonic-gate 				return (-1); /* error */
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			/*
1800Sstevel@tonic-gate 			 * Gather the input buffers, into the single linear
1810Sstevel@tonic-gate 			 * buffer allocated above, while taking care to see
1820Sstevel@tonic-gate 			 * that no more than INT_MAX bytes will be copied.
1830Sstevel@tonic-gate 			 */
1840Sstevel@tonic-gate 			_t_gather(dataptr, tiov, tiovcount);
1850Sstevel@tonic-gate 			curptr = dataptr; /* Initialize for subsequent use */
1860Sstevel@tonic-gate 		} else {
1870Sstevel@tonic-gate 			dataptr = NULL;
1880Sstevel@tonic-gate 			curptr = NULL;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	bytes_remaining = nbytes;
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * Calls to send data (write or putmsg) can potentially
1950Sstevel@tonic-gate 	 * block, for MT case, we drop the lock and enable signals here
1960Sstevel@tonic-gate 	 * and acquire it back
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
1990Sstevel@tonic-gate 	first_time = 1;
2000Sstevel@tonic-gate 	do {
2010Sstevel@tonic-gate 		bytes_to_send = bytes_remaining;
2020Sstevel@tonic-gate 		if (doputmsg) {
2030Sstevel@tonic-gate 			/*
2040Sstevel@tonic-gate 			 * transport provider supports TSDU concept
2050Sstevel@tonic-gate 			 * (unlike TCP) or it is expedited data.
2060Sstevel@tonic-gate 			 * In this case do the fragmentation
2070Sstevel@tonic-gate 			 */
2080Sstevel@tonic-gate 			if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
2090Sstevel@tonic-gate 				datareq.MORE_flag = 1;
2100Sstevel@tonic-gate 				bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
2110Sstevel@tonic-gate 			} else {
2120Sstevel@tonic-gate 				if (flags&T_MORE)
2130Sstevel@tonic-gate 					datareq.MORE_flag = 1;
2140Sstevel@tonic-gate 				else
2150Sstevel@tonic-gate 					datareq.MORE_flag = 0;
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 			databuf.maxlen = bytes_to_send;
2180Sstevel@tonic-gate 			databuf.len = bytes_to_send;
2190Sstevel@tonic-gate 			databuf.buf = curptr;
2200Sstevel@tonic-gate 			retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
2210Sstevel@tonic-gate 			if (retval == 0) {
2220Sstevel@tonic-gate 				bytes_sent = bytes_to_send;
2230Sstevel@tonic-gate 				curptr = curptr + bytes_sent;
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		} else {
2260Sstevel@tonic-gate 			/*
2270Sstevel@tonic-gate 			 * transport provider does *not* support TSDU concept
2280Sstevel@tonic-gate 			 * (e.g. TCP) and it is not expedited data. A
2290Sstevel@tonic-gate 			 * perf. optimization is used. Note: the T_MORE
2300Sstevel@tonic-gate 			 * flag is ignored here even if set by the user.
2310Sstevel@tonic-gate 			 */
2320Sstevel@tonic-gate 			/*
2330Sstevel@tonic-gate 			 * The first time, setup the tiovec for doing a writev
2340Sstevel@tonic-gate 			 * call. We assume that T_IOV_MAX <= IOV_MAX.
2350Sstevel@tonic-gate 			 * Since writev may return a partial count, we need
2360Sstevel@tonic-gate 			 * the loop. After the first time, we just adjust
2370Sstevel@tonic-gate 			 * the iov vector to not include the already
2380Sstevel@tonic-gate 			 * written bytes.
2390Sstevel@tonic-gate 			 */
2400Sstevel@tonic-gate 			if (first_time) {
2410Sstevel@tonic-gate 				first_time = 0;
2420Sstevel@tonic-gate 				_t_copy_tiov_to_iov(tiov, tiovcount, iov,
2430Sstevel@tonic-gate 				    &iovcount);
2440Sstevel@tonic-gate 			} else {
2450Sstevel@tonic-gate 				/*
2460Sstevel@tonic-gate 				 * bytes_sent - value set below in the previous
2470Sstevel@tonic-gate 				 * iteration of the loop is used now.
2480Sstevel@tonic-gate 				 */
2490Sstevel@tonic-gate 				_t_adjust_iov(bytes_sent, iov, &iovcount);
2500Sstevel@tonic-gate 			}
2510Sstevel@tonic-gate 			retval = (int)writev(fd, iov, iovcount);
2520Sstevel@tonic-gate 			if (retval >= 0) {
2530Sstevel@tonic-gate 				/* Amount that was actually sent */
2540Sstevel@tonic-gate 				bytes_sent = retval;
2550Sstevel@tonic-gate 			}
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		if (retval < 0) {
2590Sstevel@tonic-gate 			if (nbytes == bytes_remaining) {
2600Sstevel@tonic-gate 				/*
2610Sstevel@tonic-gate 				 * Error on *first* putmsg/write attempt.
2620Sstevel@tonic-gate 				 * Return appropriate error
2630Sstevel@tonic-gate 				 */
2640Sstevel@tonic-gate 				if (errno == EAGAIN)
2650Sstevel@tonic-gate 					t_errno = TFLOW;
2660Sstevel@tonic-gate 				else
2670Sstevel@tonic-gate 					t_errno = TSYSERR;
2680Sstevel@tonic-gate 				if (dataptr)
2690Sstevel@tonic-gate 					free(dataptr);
2700Sstevel@tonic-gate 				return (-1); /* return error */
2710Sstevel@tonic-gate 			}
272*132Srobinson 			/*
273*132Srobinson 			 * Not the first putmsg/write
274*132Srobinson 			 * [ partial completion of t_snd() case.
275*132Srobinson 			 *
276*132Srobinson 			 * Error on putmsg/write attempt but
277*132Srobinson 			 * some data was transmitted so don't
278*132Srobinson 			 * return error. Don't attempt to
279*132Srobinson 			 * send more (break from loop) but
280*132Srobinson 			 * return OK.
281*132Srobinson 			 */
282*132Srobinson 			break;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 		bytes_remaining = bytes_remaining - bytes_sent;
2850Sstevel@tonic-gate 	} while (bytes_remaining != 0);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (dataptr != NULL)
2880Sstevel@tonic-gate 		free(dataptr);
2890Sstevel@tonic-gate 	_T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
2900Sstevel@tonic-gate 	return (nbytes - bytes_remaining);
2910Sstevel@tonic-gate }
292