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