xref: /titanic_51/usr/src/lib/libnsl/nsl/t_snd.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3.1.2 */
32 
33 /*
34  * t_snd.c and t_sndv.c are very similar and contain common code.
35  * Any changes to either of them should be reviewed to see whether they
36  * are applicable to the other file.
37  */
38 #include "mt.h"
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <stropts.h>
43 #include <sys/stream.h>
44 #define	_SUN_TPI_VERSION 2
45 #include <sys/tihdr.h>
46 #include <sys/timod.h>
47 #include <xti.h>
48 #include <syslog.h>
49 #include "tx.h"
50 
51 
52 int
53 _tx_snd(int fd, char *buf, unsigned nbytes, int flags, int api_semantics)
54 {
55 	struct T_data_req datareq;
56 	struct strbuf ctlbuf, databuf;
57 	unsigned int bytes_sent, bytes_remaining, bytes_to_send;
58 	char *curptr;
59 	struct _ti_user *tiptr;
60 	int band;
61 	int retval, lookevent;
62 	int sv_errno;
63 	int doputmsg = 0;
64 	int32_t tsdu_limit;
65 
66 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
67 		return (-1);
68 	sig_mutex_lock(&tiptr->ti_lock);
69 
70 	if (tiptr->ti_servtype == T_CLTS) {
71 		t_errno = TNOTSUPPORT;
72 		sig_mutex_unlock(&tiptr->ti_lock);
73 		return (-1);
74 	}
75 
76 	if (_T_IS_XTI(api_semantics)) {
77 		/*
78 		 * User level state verification only done for XTI
79 		 * because doing for TLI may break existing applications
80 		 */
81 		if (! (tiptr->ti_state == T_DATAXFER ||
82 		    tiptr->ti_state == T_INREL)) {
83 			t_errno = TOUTSTATE;
84 			sig_mutex_unlock(&tiptr->ti_lock);
85 			return (-1);
86 		}
87 		/*
88 		 * XXX
89 		 * Is it OK to do this TBADFLAG check when XTI spec
90 		 * is being extended with new and interesting flags
91 		 * everyday ?
92 		 */
93 		if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
94 			t_errno = TBADFLAG;
95 			sig_mutex_unlock(&tiptr->ti_lock);
96 			return (-1);
97 		}
98 		if (flags & T_EXPEDITED)
99 			tsdu_limit = tiptr->ti_etsdusize;
100 		else {
101 			/* normal data */
102 			tsdu_limit = tiptr->ti_tsdusize;
103 		}
104 
105 		if ((tsdu_limit > 0) && /* limit meaningful and ... */
106 		    (nbytes > (uint32_t)tsdu_limit)) {
107 			t_errno = TBADDATA;
108 			sig_mutex_unlock(&tiptr->ti_lock);
109 			return (-1);
110 		}
111 
112 		/*
113 		 * Check for incoming disconnect or orderly release
114 		 * Did anyone say "performance" ? Didn't hear that.
115 		 */
116 		lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
117 		if (lookevent < 0) {
118 			sv_errno = errno;
119 			sig_mutex_unlock(&tiptr->ti_lock);
120 			errno = sv_errno;
121 			return (-1);
122 		}
123 		/*
124 		 * XNS 4 required the TLOOK error to be returned if there
125 		 * is any incoming T_ORDREL. XNS 5 does not require an
126 		 * error to be returned in such a case.
127 		 */
128 		if (lookevent == T_DISCONNECT ||
129 		    (api_semantics == TX_XTI_XNS4_API &&
130 		    lookevent == T_ORDREL)) {
131 			t_errno = TLOOK;
132 			sig_mutex_unlock(&tiptr->ti_lock);
133 			return (-1);
134 		}
135 	}
136 
137 	/* sending zero length data when not allowed */
138 	if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
139 		t_errno = TBADDATA;
140 		sig_mutex_unlock(&tiptr->ti_lock);
141 		return (-1);
142 	}
143 
144 	doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
145 
146 	if (doputmsg) {
147 		/*
148 		 * Initialize ctlbuf for use in sending/receiving control part
149 		 * of the message.
150 		 */
151 		ctlbuf.maxlen = (int)sizeof (struct T_data_req);
152 		ctlbuf.len = (int)sizeof (struct T_data_req);
153 		ctlbuf.buf = (char *)&datareq;
154 
155 		band = TI_NORMAL; /* band 0 */
156 		if (flags & T_EXPEDITED) {
157 			datareq.PRIM_type = T_EXDATA_REQ;
158 			if (! (tiptr->ti_prov_flag & EXPINLINE))
159 				band = TI_EXPEDITED; /* band > 0 */
160 		} else
161 			datareq.PRIM_type = T_DATA_REQ;
162 	}
163 
164 	bytes_remaining = nbytes;
165 	curptr = buf;
166 
167 	/*
168 	 * Calls to send data (write or putmsg) can potentially
169 	 * block, for MT case, we drop the lock and enable signals here
170 	 * and acquire it back
171 	 */
172 	sig_mutex_unlock(&tiptr->ti_lock);
173 	do {
174 		bytes_to_send = bytes_remaining;
175 		if (doputmsg) {
176 			/*
177 			 * transport provider supports TSDU concept
178 			 * (unlike TCP) or it is expedited data.
179 			 * In this case do the fragmentation
180 			 */
181 			if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
182 				datareq.MORE_flag = 1;
183 				bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
184 			} else {
185 				if (flags&T_MORE)
186 					datareq.MORE_flag = 1;
187 				else
188 					datareq.MORE_flag = 0;
189 			}
190 			databuf.maxlen = bytes_to_send;
191 			databuf.len = bytes_to_send;
192 			databuf.buf = curptr;
193 			retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
194 			if (retval == 0)
195 				bytes_sent = bytes_to_send;
196 		} else {
197 			/*
198 			 * transport provider does *not* support TSDU concept
199 			 * (e.g. TCP) and it is not expedited data. A
200 			 * perf. optimization is used. Note: the T_MORE
201 			 * flag is ignored here even if set by the user.
202 			 */
203 			retval = write(fd, curptr, bytes_to_send);
204 			if (retval >= 0) {
205 				/* Amount that was actually sent */
206 				bytes_sent = retval;
207 			}
208 		}
209 
210 		if (retval < 0) {
211 			if (nbytes == bytes_remaining) {
212 				/*
213 				 * Error on *first* putmsg/write attempt.
214 				 * Return appropriate error
215 				 */
216 				if (errno == EAGAIN)
217 					t_errno = TFLOW;
218 				else
219 					t_errno = TSYSERR;
220 				return (-1); /* return error */
221 			}
222 			/*
223 			 * Not the first putmsg/write
224 			 * [ partial completion of t_snd() case.
225 			 *
226 			 * Error on putmsg/write attempt but
227 			 * some data was transmitted so don't
228 			 * return error. Don't attempt to
229 			 * send more (break from loop) but
230 			 * return OK.
231 			 */
232 			break;
233 		}
234 		bytes_remaining = bytes_remaining - bytes_sent;
235 		curptr = curptr + bytes_sent;
236 	} while (bytes_remaining != 0);
237 
238 	_T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
239 	return (nbytes - bytes_remaining);
240 }
241