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
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.
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]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * t_rcv.c and t_rcvv.c are very similar and contain common code.
30 * Any changes to either of them should be reviewed to see whether they
31 * are applicable to the other file.
38 #include <sys/stream.h>
39 #define _SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
48 _tx_rcvv(int fd
, struct t_iovec
*tiov
, unsigned int tiovcount
, int *flags
,
51 struct strbuf ctlbuf
, databuf
;
54 union T_primitives
*pptr
;
55 struct _ti_user
*tiptr
;
61 if ((tiptr
= _t_checkfd(fd
, 0, api_semantics
)) == NULL
)
63 sig_mutex_lock(&tiptr
->ti_lock
);
65 if (tiptr
->ti_servtype
== T_CLTS
) {
66 t_errno
= TNOTSUPPORT
;
67 sig_mutex_unlock(&tiptr
->ti_lock
);
71 assert(api_semantics
== TX_XTI_XNS5_API
);
73 if (tiovcount
== 0 || tiovcount
> T_IOV_MAX
) {
75 sig_mutex_unlock(&tiptr
->ti_lock
);
79 if (!(tiptr
->ti_state
== T_DATAXFER
||
80 tiptr
->ti_state
== T_OUTREL
)) {
82 sig_mutex_unlock(&tiptr
->ti_lock
);
87 * Check in lookbuf for stuff
89 if (tiptr
->ti_lookcnt
> 0) {
91 * Implied preference rules give priority to
92 * T_DISCON_IND over T_ORDREL_IND. Also certain errors like
93 * data received after T_ORDREL_IND or a duplicate T_ORDREL_IND
94 * after a T_ORDRELING have priority over TLOOK.
95 * This manifests in following code behavior.
97 * (1) If something in lookbuf then check
98 * the stream head also. This may result
99 * in retuning a TLOOK error but only if there are
100 * - message at stream head but look buffer
101 * has a T_DISCON_IND event.
102 * - no messages are on the stream head
104 * (2) If there are messages on the stream head and
105 * all of them are T_ORDREL_IND(i.e. no message in
106 * look buffer is T_DISCON_IND), there
107 * could be data on stream head to be picked up and
108 * we work on the stream head and not return TLOOK.
109 * We remove the event on the stream head and queue it.
113 retval
= ioctl(fd
, I_NREAD
, &msglen
);
114 } while (retval
< 0 && errno
== EINTR
);
119 sig_mutex_unlock(&tiptr
->ti_lock
);
126 * If any T_DISCON_IND event in look buffer
127 * list then return TLOOK. Else continue
128 * processing as what could be on the stream
129 * head might be a possible T_DISCON_IND (which
130 * would have priority over the T_ORDREL_INDs
131 * on the look buffer.)
133 struct _ti_lookbufs
*tlbs
;
135 tlbs
= &tiptr
->ti_lookbufs
;
137 /* LINTED pointer cast */
138 if (*((t_scalar_t
*)tlbs
->tl_lookcbuf
)
141 sig_mutex_unlock(&tiptr
->ti_lock
);
144 } while ((tlbs
= tlbs
->tl_next
) != NULL
);
146 } else { /* retval == 0 */
148 * Nothing on stream head so whatever in
149 * look buffer has nothing that might override
153 sig_mutex_unlock(&tiptr
->ti_lock
);
159 * Acquire ctlbuf for use in sending/receiving control part
162 if (_t_acquire_ctlbuf(tiptr
, &ctlbuf
, &didalloc
) < 0) {
164 sig_mutex_unlock(&tiptr
->ti_lock
);
169 nbytes
= _t_bytecount_upto_intmax(tiov
, tiovcount
);
171 if (nbytes
!= 0 && ((dataptr
= malloc(nbytes
)) == NULL
)) {
177 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
178 sig_mutex_unlock(&tiptr
->ti_lock
);
183 databuf
.maxlen
= (int)nbytes
;
185 databuf
.buf
= dataptr
;
190 * This is a call that may block indefinitely so we drop the
191 * lock and allow signals in MT case here and reacquire it.
192 * Error case should roll back state changes done above
193 * (happens to be no state change here)
195 sig_mutex_unlock(&tiptr
->ti_lock
);
196 if ((retval
= getmsg(fd
, &ctlbuf
, &databuf
, &flg
)) < 0) {
202 sig_mutex_lock(&tiptr
->ti_lock
);
206 sig_mutex_lock(&tiptr
->ti_lock
);
208 assert((retval
& MORECTL
) == 0); /* MORECTL should not be on */
210 if (databuf
.len
== -1) databuf
.len
= 0;
212 if (ctlbuf
.len
> 0) {
213 if (ctlbuf
.len
< (int)sizeof (t_scalar_t
)) {
219 /* LINTED pointer cast */
220 pptr
= (union T_primitives
*)ctlbuf
.buf
;
222 switch (pptr
->type
) {
225 *flags
|= T_EXPEDITED
;
227 tiptr
->ti_flags
|= EXPEDITED
;
231 * Uses the fact T_DATA_IND and T_EXDATA_IND
234 if ((ctlbuf
.len
< (int)sizeof (struct T_data_ind
)) ||
235 (tiptr
->ti_lookcnt
> 0)) {
237 * ti_lookcnt > 0 implies data
238 * received after T_DISCON_IND or
239 * T_ORDREL_IND hence error
246 if ((pptr
->data_ind
.MORE_flag
) || retval
)
248 if ((pptr
->data_ind
.MORE_flag
) && retval
)
249 tiptr
->ti_flags
|= MORE
;
251 * No real state change on T_RCV event (noop)
253 * We invoke the macro only for error logging
254 * part of its capabilities when in a bad state.
256 _T_TX_NEXTSTATE(T_RCV
, tiptr
,
257 "t_rcvv: invalid state event T_RCV");
261 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
262 _t_scatter(&databuf
, tiov
, tiovcount
);
264 sig_mutex_unlock(&tiptr
->ti_lock
);
265 return (databuf
.len
);
268 if (tiptr
->ti_lookcnt
> 0) {
270 * ti_lookcnt > 0 implies T_ORDREL_IND
271 * received after T_DISCON_IND or
272 * another T_ORDREL_IND hence error.
281 * Post event (T_ORDREL_IND/T_DISCON_IND) to
282 * the lookbuffer list.
285 if (_t_register_lookevent(tiptr
, databuf
.buf
,
287 ctlbuf
.buf
, ctlbuf
.len
) < 0) {
293 * We know that T_DISCON_IND is stored in
294 * last look buffer. If there is more data
295 * that follows, we try to append it to
296 * the same look buffer
298 if (retval
& MOREDATA
) {
299 ctlbuf
.maxlen
= 0; /* XXX why ? */
303 * XXX Will break (-ve maxlen) for
304 * transport provider with unbounded
305 * T_DISCON_IND data part (-1).
308 tiptr
->ti_rcvsize
- databuf
.len
;
312 tiptr
->ti_lookbufs
.tl_lookdbuf
+
313 tiptr
->ti_lookbufs
.tl_lookdlen
;
317 * Since MOREDATA was set, we assume
318 * that this getmsg will not block
322 retval
= getmsg(fd
, &ctlbuf
,
324 } while (retval
< 0 && errno
== EINTR
);
330 if (databuf
.len
== -1) databuf
.len
= 0;
332 /* MORECTL should not be on */
333 assert((retval
& MORECTL
) == 0);
336 * No support for unbounded data
343 tiptr
->ti_lookbufs
.tl_lookdlen
+=
358 } else { /* else for "if (ctlbuf.len > 0)" */
359 if (!retval
&& (tiptr
->ti_flags
& MORE
)) {
361 tiptr
->ti_flags
&= ~MORE
;
363 if (retval
& MOREDATA
)
367 * If inside an ETSDU, set expedited flag and turn
368 * of internal version when reach end of "ETIDU".
370 if (tiptr
->ti_flags
& EXPEDITED
) {
371 *flags
|= T_EXPEDITED
;
373 tiptr
->ti_flags
&= ~EXPEDITED
;
377 * No real state change on T_RCV events (It is a NOOP)
379 * We invoke the macro only for error logging
380 * part of its capabilities when in a bad state.
382 _T_TX_NEXTSTATE(T_RCV
, tiptr
,
383 "t_rcvv: state invalid T_RCV event");
387 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
388 _t_scatter(&databuf
, tiov
, tiovcount
);
390 sig_mutex_unlock(&tiptr
->ti_lock
);
391 return (databuf
.len
);
400 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
402 sig_mutex_unlock(&tiptr
->ti_lock
);