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