xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_sndv.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include "mt.h"
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <stropts.h>
35 #include <sys/stream.h>
36 #define	_SUN_TPI_VERSION 2
37 #include <sys/tihdr.h>
38 #include <sys/timod.h>
39 #include <xti.h>
40 #include <assert.h>
41 #include <syslog.h>
42 #include "tx.h"
43 
44 
45 /*
46  * t_snd.c and t_sndv.c are very similar and contain common code.
47  * Any changes to either of them should be reviewed to see whether they
48  * are applicable to the other file.
49  */
50 int
51 _tx_sndv(int fd, const struct t_iovec *tiov, unsigned int tiovcount,
52     int flags, int api_semantics)
53 {
54 	struct T_data_req datareq;
55 	struct strbuf ctlbuf, databuf;
56 	unsigned int bytes_sent, bytes_remaining, bytes_to_send, nbytes;
57 	char *curptr;
58 	struct iovec iov[T_IOV_MAX];
59 	int iovcount;
60 	char *dataptr;
61 	int first_time;
62 	struct _ti_user *tiptr;
63 	int band;
64 	int retval, lookevent;
65 	int sv_errno;
66 	int doputmsg = 0;
67 	int32_t tsdu_limit;
68 
69 	assert(api_semantics == TX_XTI_XNS5_API);
70 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
71 		return (-1);
72 	sig_mutex_lock(&tiptr->ti_lock);
73 
74 	if (tiptr->ti_servtype == T_CLTS) {
75 		t_errno = TNOTSUPPORT;
76 		sig_mutex_unlock(&tiptr->ti_lock);
77 		return (-1);
78 	}
79 
80 	if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
81 		t_errno = TBADDATA;
82 		sig_mutex_unlock(&tiptr->ti_lock);
83 		return (-1);
84 	}
85 
86 	if (!(tiptr->ti_state == T_DATAXFER ||
87 	    tiptr->ti_state == T_INREL)) {
88 		t_errno = TOUTSTATE;
89 		sig_mutex_unlock(&tiptr->ti_lock);
90 		return (-1);
91 	}
92 	/*
93 	 * XXX
94 	 * Is it OK to do this TBADFLAG check when XTI spec
95 	 * is being extended with new and interesting flags
96 	 * everyday ?
97 	 */
98 	if ((flags & ~(TX_ALL_VALID_FLAGS)) != 0) {
99 		t_errno = TBADFLAG;
100 		sig_mutex_unlock(&tiptr->ti_lock);
101 		return (-1);
102 	}
103 	if (flags & T_EXPEDITED)
104 		tsdu_limit = tiptr->ti_etsdusize;
105 	else {
106 		/* normal data */
107 		tsdu_limit = tiptr->ti_tsdusize;
108 	}
109 
110 	/*
111 	 * nbytes is the sum of the bytecounts in the tiov vector
112 	 * A value larger than INT_MAX is truncated to INT_MAX by
113 	 * _t_bytecount_upto_intmax()
114 	 */
115 	nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
116 
117 	if ((tsdu_limit > 0) && /* limit meaningful and ... */
118 	    (nbytes > (uint32_t)tsdu_limit)) {
119 		t_errno = TBADDATA;
120 		sig_mutex_unlock(&tiptr->ti_lock);
121 		return (-1);
122 	}
123 
124 	/*
125 	 * Check for incoming disconnect only. XNS Issue 5 makes it optional
126 	 * to check for incoming orderly release
127 	 */
128 	lookevent = _t_look_locked(fd, tiptr, 0, api_semantics);
129 	if (lookevent < 0) {
130 		sv_errno = errno;
131 		sig_mutex_unlock(&tiptr->ti_lock);
132 		errno = sv_errno;
133 		return (-1);
134 	}
135 	if (lookevent == T_DISCONNECT) {
136 		t_errno = TLOOK;
137 		sig_mutex_unlock(&tiptr->ti_lock);
138 		return (-1);
139 	}
140 
141 	/* sending zero length data when not allowed */
142 	if (nbytes == 0 && !(tiptr->ti_prov_flag & (SENDZERO|OLD_SENDZERO))) {
143 		t_errno = TBADDATA;
144 		sig_mutex_unlock(&tiptr->ti_lock);
145 		return (-1);
146 	}
147 
148 	doputmsg = (tiptr->ti_tsdusize != 0) || (flags & T_EXPEDITED);
149 
150 	if (doputmsg) {
151 		/*
152 		 * Initialize ctlbuf for use in sending/receiving control part
153 		 * of the message.
154 		 */
155 		ctlbuf.maxlen = (int)sizeof (struct T_data_req);
156 		ctlbuf.len = (int)sizeof (struct T_data_req);
157 		ctlbuf.buf = (char *)&datareq;
158 
159 		band = TI_NORMAL; /* band 0 */
160 		if (flags & T_EXPEDITED) {
161 			datareq.PRIM_type = T_EXDATA_REQ;
162 			if (!(tiptr->ti_prov_flag & EXPINLINE))
163 				band = TI_EXPEDITED; /* band > 0 */
164 		} else
165 			datareq.PRIM_type = T_DATA_REQ;
166 		/*
167 		 * Allocate a databuffer into which we will gather the
168 		 * input vector data, and make a call to putmsg(). We
169 		 * do this since we don't have the equivalent of a putmsgv()
170 		 */
171 		if (nbytes != 0) {
172 			if ((dataptr = malloc((size_t)nbytes)) == NULL) {
173 				sv_errno = errno;
174 				sig_mutex_unlock(&tiptr->ti_lock);
175 				errno = sv_errno;
176 				t_errno = TSYSERR;
177 				return (-1); /* error */
178 			}
179 			/*
180 			 * Gather the input buffers, into the single linear
181 			 * buffer allocated above, while taking care to see
182 			 * that no more than INT_MAX bytes will be copied.
183 			 */
184 			_t_gather(dataptr, tiov, tiovcount);
185 			curptr = dataptr; /* Initialize for subsequent use */
186 		} else {
187 			dataptr = NULL;
188 			curptr = NULL;
189 		}
190 	}
191 
192 	bytes_remaining = nbytes;
193 	/*
194 	 * Calls to send data (write or putmsg) can potentially
195 	 * block, for MT case, we drop the lock and enable signals here
196 	 * and acquire it back
197 	 */
198 	sig_mutex_unlock(&tiptr->ti_lock);
199 	first_time = 1;
200 	do {
201 		bytes_to_send = bytes_remaining;
202 		if (doputmsg) {
203 			/*
204 			 * transport provider supports TSDU concept
205 			 * (unlike TCP) or it is expedited data.
206 			 * In this case do the fragmentation
207 			 */
208 			if (bytes_to_send > (unsigned int)tiptr->ti_maxpsz) {
209 				datareq.MORE_flag = 1;
210 				bytes_to_send = (unsigned int)tiptr->ti_maxpsz;
211 			} else {
212 				if (flags&T_MORE)
213 					datareq.MORE_flag = 1;
214 				else
215 					datareq.MORE_flag = 0;
216 			}
217 			databuf.maxlen = bytes_to_send;
218 			databuf.len = bytes_to_send;
219 			databuf.buf = curptr;
220 			retval = putpmsg(fd, &ctlbuf, &databuf, band, MSG_BAND);
221 			if (retval == 0) {
222 				bytes_sent = bytes_to_send;
223 				curptr = curptr + bytes_sent;
224 			}
225 		} else {
226 			/*
227 			 * transport provider does *not* support TSDU concept
228 			 * (e.g. TCP) and it is not expedited data. A
229 			 * perf. optimization is used. Note: the T_MORE
230 			 * flag is ignored here even if set by the user.
231 			 */
232 			/*
233 			 * The first time, setup the tiovec for doing a writev
234 			 * call. We assume that T_IOV_MAX <= IOV_MAX.
235 			 * Since writev may return a partial count, we need
236 			 * the loop. After the first time, we just adjust
237 			 * the iov vector to not include the already
238 			 * written bytes.
239 			 */
240 			if (first_time) {
241 				first_time = 0;
242 				_t_copy_tiov_to_iov(tiov, tiovcount, iov,
243 				    &iovcount);
244 			} else {
245 				/*
246 				 * bytes_sent - value set below in the previous
247 				 * iteration of the loop is used now.
248 				 */
249 				_t_adjust_iov(bytes_sent, iov, &iovcount);
250 			}
251 			retval = (int)writev(fd, iov, iovcount);
252 			if (retval >= 0) {
253 				/* Amount that was actually sent */
254 				bytes_sent = retval;
255 			}
256 		}
257 
258 		if (retval < 0) {
259 			if (nbytes == bytes_remaining) {
260 				/*
261 				 * Error on *first* putmsg/write attempt.
262 				 * Return appropriate error
263 				 */
264 				if (errno == EAGAIN)
265 					t_errno = TFLOW;
266 				else
267 					t_errno = TSYSERR;
268 				if (dataptr)
269 					free(dataptr);
270 				return (-1); /* return error */
271 			}
272 			/*
273 			 * Not the first putmsg/write
274 			 * [ partial completion of t_snd() case.
275 			 *
276 			 * Error on putmsg/write attempt but
277 			 * some data was transmitted so don't
278 			 * return error. Don't attempt to
279 			 * send more (break from loop) but
280 			 * return OK.
281 			 */
282 			break;
283 		}
284 		bytes_remaining = bytes_remaining - bytes_sent;
285 	} while (bytes_remaining != 0);
286 
287 	if (dataptr != NULL)
288 		free(dataptr);
289 	_T_TX_NEXTSTATE(T_SND, tiptr, "t_snd: invalid state event T_SND");
290 	return (nbytes - bytes_remaining);
291 }
292