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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
240Sstevel@tonic-gate /* All Rights Reserved */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*132Srobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
280Sstevel@tonic-gate * Use is subject to license terms.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3.1.2 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * t_snd.c and t_sndv.c are very similar and contain common code.
350Sstevel@tonic-gate * Any changes to either of them should be reviewed to see whether they
360Sstevel@tonic-gate * are applicable to the other file.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate #include "mt.h"
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <stropts.h>
430Sstevel@tonic-gate #include <sys/stream.h>
440Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
450Sstevel@tonic-gate #include <sys/tihdr.h>
460Sstevel@tonic-gate #include <sys/timod.h>
470Sstevel@tonic-gate #include <xti.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include "tx.h"
500Sstevel@tonic-gate
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
_tx_snd(int fd,char * buf,unsigned nbytes,int flags,int api_semantics)530Sstevel@tonic-gate _tx_snd(int fd, char *buf, unsigned nbytes, int flags, int api_semantics)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate struct T_data_req datareq;
560Sstevel@tonic-gate struct strbuf ctlbuf, databuf;
570Sstevel@tonic-gate unsigned int bytes_sent, bytes_remaining, bytes_to_send;
580Sstevel@tonic-gate char *curptr;
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
66*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
670Sstevel@tonic-gate return (-1);
680Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
690Sstevel@tonic-gate
700Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
710Sstevel@tonic-gate t_errno = TNOTSUPPORT;
720Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
730Sstevel@tonic-gate return (-1);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * User level state verification only done for XTI
790Sstevel@tonic-gate * because doing for TLI may break existing applications
800Sstevel@tonic-gate */
810Sstevel@tonic-gate if (! (tiptr->ti_state == T_DATAXFER ||
820Sstevel@tonic-gate tiptr->ti_state == T_INREL)) {
830Sstevel@tonic-gate t_errno = TOUTSTATE;
840Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
850Sstevel@tonic-gate return (-1);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * XXX
890Sstevel@tonic-gate * Is it OK to do this TBADFLAG check when XTI spec
900Sstevel@tonic-gate * is being extended with new and interesting flags
910Sstevel@tonic-gate * everyday ?
920Sstevel@tonic-gate */
930Sstevel@tonic-gate if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
940Sstevel@tonic-gate t_errno = TBADFLAG;
950Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
960Sstevel@tonic-gate return (-1);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate if (flags & T_EXPEDITED)
990Sstevel@tonic-gate tsdu_limit = tiptr->ti_etsdusize;
1000Sstevel@tonic-gate else {
1010Sstevel@tonic-gate /* normal data */
1020Sstevel@tonic-gate tsdu_limit = tiptr->ti_tsdusize;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if ((tsdu_limit > 0) && /* limit meaningful and ... */
1060Sstevel@tonic-gate (nbytes > (uint32_t)tsdu_limit)) {
1070Sstevel@tonic-gate t_errno = TBADDATA;
1080Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1090Sstevel@tonic-gate return (-1);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Check for incoming disconnect or orderly release
1140Sstevel@tonic-gate * Did anyone say "performance" ? Didn't hear that.
1150Sstevel@tonic-gate */
1160Sstevel@tonic-gate lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
1170Sstevel@tonic-gate if (lookevent < 0) {
1180Sstevel@tonic-gate sv_errno = errno;
1190Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1200Sstevel@tonic-gate errno = sv_errno;
1210Sstevel@tonic-gate return (-1);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * XNS 4 required the TLOOK error to be returned if there
1250Sstevel@tonic-gate * is any incoming T_ORDREL. XNS 5 does not require an
1260Sstevel@tonic-gate * error to be returned in such a case.
1270Sstevel@tonic-gate */
1280Sstevel@tonic-gate if (lookevent == T_DISCONNECT ||
1290Sstevel@tonic-gate (api_semantics == TX_XTI_XNS4_API &&
1300Sstevel@tonic-gate lookevent == T_ORDREL)) {
1310Sstevel@tonic-gate t_errno = TLOOK;
1320Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1330Sstevel@tonic-gate return (-1);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /* sending zero length data when not allowed */
1380Sstevel@tonic-gate if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
1390Sstevel@tonic-gate t_errno = TBADDATA;
1400Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1410Sstevel@tonic-gate return (-1);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (doputmsg) {
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate * Initialize ctlbuf for use in sending/receiving control part
1490Sstevel@tonic-gate * of the message.
1500Sstevel@tonic-gate */
1510Sstevel@tonic-gate ctlbuf.maxlen = (int)sizeof (struct T_data_req);
1520Sstevel@tonic-gate ctlbuf.len = (int)sizeof (struct T_data_req);
1530Sstevel@tonic-gate ctlbuf.buf = (char *)&datareq;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate band = TI_NORMAL; /* band 0 */
1560Sstevel@tonic-gate if (flags & T_EXPEDITED) {
1570Sstevel@tonic-gate datareq.PRIM_type = T_EXDATA_REQ;
1580Sstevel@tonic-gate if (! (tiptr->ti_prov_flag & EXPINLINE))
1590Sstevel@tonic-gate band = TI_EXPEDITED; /* band > 0 */
1600Sstevel@tonic-gate } else
1610Sstevel@tonic-gate datareq.PRIM_type = T_DATA_REQ;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate bytes_remaining = nbytes;
1650Sstevel@tonic-gate curptr = buf;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Calls to send data (write or putmsg) can potentially
1690Sstevel@tonic-gate * block, for MT case, we drop the lock and enable signals here
1700Sstevel@tonic-gate * and acquire it back
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1730Sstevel@tonic-gate do {
1740Sstevel@tonic-gate bytes_to_send = bytes_remaining;
1750Sstevel@tonic-gate if (doputmsg) {
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * transport provider supports TSDU concept
1780Sstevel@tonic-gate * (unlike TCP) or it is expedited data.
1790Sstevel@tonic-gate * In this case do the fragmentation
1800Sstevel@tonic-gate */
1810Sstevel@tonic-gate if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
1820Sstevel@tonic-gate datareq.MORE_flag = 1;
1830Sstevel@tonic-gate bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
1840Sstevel@tonic-gate } else {
1850Sstevel@tonic-gate if (flags&T_MORE)
1860Sstevel@tonic-gate datareq.MORE_flag = 1;
1870Sstevel@tonic-gate else
1880Sstevel@tonic-gate datareq.MORE_flag = 0;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate databuf.maxlen = bytes_to_send;
1910Sstevel@tonic-gate databuf.len = bytes_to_send;
1920Sstevel@tonic-gate databuf.buf = curptr;
1930Sstevel@tonic-gate retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
1940Sstevel@tonic-gate if (retval == 0)
1950Sstevel@tonic-gate bytes_sent = bytes_to_send;
1960Sstevel@tonic-gate } else {
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate * transport provider does *not* support TSDU concept
1990Sstevel@tonic-gate * (e.g. TCP) and it is not expedited data. A
2000Sstevel@tonic-gate * perf. optimization is used. Note: the T_MORE
2010Sstevel@tonic-gate * flag is ignored here even if set by the user.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate retval = write(fd, curptr, bytes_to_send);
2040Sstevel@tonic-gate if (retval >= 0) {
2050Sstevel@tonic-gate /* Amount that was actually sent */
2060Sstevel@tonic-gate bytes_sent = retval;
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate if (retval < 0) {
2110Sstevel@tonic-gate if (nbytes == bytes_remaining) {
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * Error on *first* putmsg/write attempt.
2140Sstevel@tonic-gate * Return appropriate error
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate if (errno == EAGAIN)
2170Sstevel@tonic-gate t_errno = TFLOW;
2180Sstevel@tonic-gate else
2190Sstevel@tonic-gate t_errno = TSYSERR;
2200Sstevel@tonic-gate return (-1); /* return error */
2210Sstevel@tonic-gate }
222*132Srobinson /*
223*132Srobinson * Not the first putmsg/write
224*132Srobinson * [ partial completion of t_snd() case.
225*132Srobinson *
226*132Srobinson * Error on putmsg/write attempt but
227*132Srobinson * some data was transmitted so don't
228*132Srobinson * return error. Don't attempt to
229*132Srobinson * send more (break from loop) but
230*132Srobinson * return OK.
231*132Srobinson */
232*132Srobinson break;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate bytes_remaining = bytes_remaining - bytes_sent;
2350Sstevel@tonic-gate curptr = curptr + bytes_sent;
2360Sstevel@tonic-gate } while (bytes_remaining != 0);
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate _T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
2390Sstevel@tonic-gate return (nbytes - bytes_remaining);
2400Sstevel@tonic-gate }
241