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