Print this page
5026 intra-node/inter-zone networking doesn't always deliver SIGPOLL
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/sockfs/sockcommon_subr.c
+++ new/usr/src/uts/common/fs/sockfs/sockcommon_subr.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 +/*
26 + * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
27 + */
25 28
26 29 #include <sys/types.h>
27 30 #include <sys/param.h>
28 31 #include <sys/signal.h>
29 32 #include <sys/cmn_err.h>
30 33
31 34 #include <sys/stropts.h>
32 35 #include <sys/socket.h>
33 36 #include <sys/socketvar.h>
34 37 #include <sys/sockio.h>
35 38 #include <sys/strsubr.h>
36 39 #include <sys/strsun.h>
37 40 #include <sys/atomic.h>
38 41 #include <sys/tihdr.h>
39 42
40 43 #include <fs/sockfs/sockcommon.h>
41 44 #include <fs/sockfs/sockfilter_impl.h>
42 45 #include <fs/sockfs/socktpi.h>
43 46 #include <fs/sockfs/sodirect.h>
44 47 #include <sys/ddi.h>
45 48 #include <inet/ip.h>
46 49 #include <sys/time.h>
47 50 #include <sys/cmn_err.h>
48 51
49 52 #ifdef SOCK_TEST
50 53 extern int do_useracc;
51 54 extern clock_t sock_test_timelimit;
52 55 #endif /* SOCK_TEST */
53 56
54 57 #define MBLK_PULL_LEN 64
55 58 uint32_t so_mblk_pull_len = MBLK_PULL_LEN;
56 59
57 60 #ifdef DEBUG
58 61 boolean_t so_debug_length = B_FALSE;
59 62 static boolean_t so_check_length(sonode_t *so);
60 63 #endif
61 64
62 65 static int
63 66 so_acceptq_dequeue_locked(struct sonode *so, boolean_t dontblock,
64 67 struct sonode **nsop)
65 68 {
66 69 struct sonode *nso = NULL;
67 70
68 71 *nsop = NULL;
69 72 ASSERT(MUTEX_HELD(&so->so_acceptq_lock));
70 73 while ((nso = list_remove_head(&so->so_acceptq_list)) == NULL) {
71 74 /*
72 75 * No need to check so_error here, because it is not
73 76 * possible for a listening socket to be reset or otherwise
74 77 * disconnected.
75 78 *
76 79 * So now we just need check if it's ok to wait.
77 80 */
78 81 if (dontblock)
79 82 return (EWOULDBLOCK);
80 83 if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
81 84 return (EINTR);
82 85
83 86 if (cv_wait_sig_swap(&so->so_acceptq_cv,
84 87 &so->so_acceptq_lock) == 0)
85 88 return (EINTR);
86 89 }
87 90
88 91 ASSERT(nso != NULL);
89 92 ASSERT(so->so_acceptq_len > 0);
90 93 so->so_acceptq_len--;
91 94 nso->so_listener = NULL;
92 95
93 96 *nsop = nso;
94 97
95 98 return (0);
96 99 }
97 100
98 101 /*
99 102 * int so_acceptq_dequeue(struct sonode *, boolean_t, struct sonode **)
100 103 *
101 104 * Pulls a connection off of the accept queue.
102 105 *
103 106 * Arguments:
104 107 * so - listening socket
105 108 * dontblock - indicate whether it's ok to sleep if there are no
106 109 * connections on the queue
107 110 * nsop - Value-return argument
108 111 *
109 112 * Return values:
110 113 * 0 when a connection is successfully dequeued, in which case nsop
111 114 * is set to point to the new connection. Upon failure a non-zero
112 115 * value is returned, and the value of nsop is set to NULL.
113 116 *
114 117 * Note:
115 118 * so_acceptq_dequeue() may return prematurly if the socket is falling
116 119 * back to TPI.
117 120 */
118 121 int
119 122 so_acceptq_dequeue(struct sonode *so, boolean_t dontblock,
120 123 struct sonode **nsop)
121 124 {
122 125 int error;
123 126
124 127 mutex_enter(&so->so_acceptq_lock);
125 128 error = so_acceptq_dequeue_locked(so, dontblock, nsop);
126 129 mutex_exit(&so->so_acceptq_lock);
127 130
128 131 return (error);
129 132 }
130 133
131 134 static void
132 135 so_acceptq_flush_impl(struct sonode *so, list_t *list, boolean_t doclose)
133 136 {
134 137 struct sonode *nso;
135 138
136 139 while ((nso = list_remove_head(list)) != NULL) {
137 140 nso->so_listener = NULL;
138 141 if (doclose) {
139 142 (void) socket_close(nso, 0, CRED());
140 143 } else {
141 144 /*
142 145 * Only used for fallback - not possible when filters
143 146 * are present.
144 147 */
145 148 ASSERT(so->so_filter_active == 0);
146 149 /*
147 150 * Since the socket is on the accept queue, there can
148 151 * only be one reference. We drop the reference and
149 152 * just blow off the socket.
150 153 */
151 154 ASSERT(nso->so_count == 1);
152 155 nso->so_count--;
153 156 /* drop the proto ref */
154 157 VN_RELE(SOTOV(nso));
155 158 }
156 159 socket_destroy(nso);
157 160 }
158 161 }
159 162 /*
160 163 * void so_acceptq_flush(struct sonode *so)
161 164 *
162 165 * Removes all pending connections from a listening socket, and
163 166 * frees the associated resources.
164 167 *
165 168 * Arguments
166 169 * so - listening socket
167 170 * doclose - make a close downcall for each socket on the accept queue
168 171 *
169 172 * Return values:
170 173 * None.
171 174 *
172 175 * Note:
173 176 * The caller has to ensure that no calls to so_acceptq_enqueue() or
174 177 * so_acceptq_dequeue() occur while the accept queue is being flushed.
175 178 * So either the socket needs to be in a state where no operations
176 179 * would come in, or so_lock needs to be obtained.
177 180 */
178 181 void
179 182 so_acceptq_flush(struct sonode *so, boolean_t doclose)
180 183 {
181 184 so_acceptq_flush_impl(so, &so->so_acceptq_list, doclose);
182 185 so_acceptq_flush_impl(so, &so->so_acceptq_defer, doclose);
183 186
184 187 so->so_acceptq_len = 0;
185 188 }
186 189
187 190 int
188 191 so_wait_connected_locked(struct sonode *so, boolean_t nonblock,
189 192 sock_connid_t id)
190 193 {
191 194 ASSERT(MUTEX_HELD(&so->so_lock));
192 195
193 196 /*
194 197 * The protocol has notified us that a connection attempt is being
195 198 * made, so before we wait for a notification to arrive we must
196 199 * clear out any errors associated with earlier connection attempts.
197 200 */
198 201 if (so->so_error != 0 && SOCK_CONNID_LT(so->so_proto_connid, id))
199 202 so->so_error = 0;
200 203
201 204 while (SOCK_CONNID_LT(so->so_proto_connid, id)) {
202 205 if (nonblock)
203 206 return (EINPROGRESS);
204 207
205 208 if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
206 209 return (EINTR);
207 210
208 211 if (cv_wait_sig_swap(&so->so_state_cv, &so->so_lock) == 0)
209 212 return (EINTR);
210 213 }
211 214
212 215 if (so->so_error != 0)
213 216 return (sogeterr(so, B_TRUE));
214 217 /*
215 218 * Under normal circumstances, so_error should contain an error
216 219 * in case the connect failed. However, it is possible for another
217 220 * thread to come in a consume the error, so generate a sensible
218 221 * error in that case.
219 222 */
220 223 if ((so->so_state & SS_ISCONNECTED) == 0)
221 224 return (ECONNREFUSED);
222 225
223 226 return (0);
224 227 }
225 228
226 229 /*
227 230 * int so_wait_connected(struct sonode *so, boolean_t nonblock,
228 231 * sock_connid_t id)
229 232 *
230 233 * Wait until the socket is connected or an error has occured.
231 234 *
232 235 * Arguments:
233 236 * so - socket
234 237 * nonblock - indicate whether it's ok to sleep if the connection has
235 238 * not yet been established
236 239 * gen - generation number that was returned by the protocol
237 240 * when the operation was started
238 241 *
239 242 * Returns:
240 243 * 0 if the connection attempt was successful, or an error indicating why
241 244 * the connection attempt failed.
242 245 */
243 246 int
244 247 so_wait_connected(struct sonode *so, boolean_t nonblock, sock_connid_t id)
245 248 {
246 249 int error;
247 250
248 251 mutex_enter(&so->so_lock);
249 252 error = so_wait_connected_locked(so, nonblock, id);
250 253 mutex_exit(&so->so_lock);
251 254
252 255 return (error);
253 256 }
254 257
255 258 int
256 259 so_snd_wait_qnotfull_locked(struct sonode *so, boolean_t dontblock)
257 260 {
258 261 int error;
259 262
260 263 ASSERT(MUTEX_HELD(&so->so_lock));
261 264 while (SO_SND_FLOWCTRLD(so)) {
262 265 if (so->so_state & SS_CANTSENDMORE)
263 266 return (EPIPE);
264 267 if (dontblock)
265 268 return (EWOULDBLOCK);
266 269
267 270 if (so->so_state & (SS_CLOSING | SS_FALLBACK_PENDING))
268 271 return (EINTR);
269 272
270 273 if (so->so_sndtimeo == 0) {
271 274 /*
272 275 * Zero means disable timeout.
273 276 */
274 277 error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
275 278 } else {
276 279 error = cv_reltimedwait_sig(&so->so_snd_cv,
277 280 &so->so_lock, so->so_sndtimeo, TR_CLOCK_TICK);
278 281 }
279 282 if (error == 0)
280 283 return (EINTR);
281 284 else if (error == -1)
282 285 return (EAGAIN);
283 286 }
284 287 return (0);
285 288 }
286 289
287 290 /*
288 291 * int so_wait_sendbuf(struct sonode *so, boolean_t dontblock)
289 292 *
290 293 * Wait for the transport to notify us about send buffers becoming
291 294 * available.
292 295 */
293 296 int
294 297 so_snd_wait_qnotfull(struct sonode *so, boolean_t dontblock)
295 298 {
296 299 int error = 0;
297 300
298 301 mutex_enter(&so->so_lock);
299 302 so->so_snd_wakeup = B_TRUE;
300 303 error = so_snd_wait_qnotfull_locked(so, dontblock);
301 304 so->so_snd_wakeup = B_FALSE;
302 305 mutex_exit(&so->so_lock);
303 306
304 307 return (error);
305 308 }
306 309
307 310 void
308 311 so_snd_qfull(struct sonode *so)
309 312 {
310 313 mutex_enter(&so->so_lock);
311 314 so->so_snd_qfull = B_TRUE;
312 315 mutex_exit(&so->so_lock);
313 316 }
314 317
315 318 void
316 319 so_snd_qnotfull(struct sonode *so)
317 320 {
318 321 mutex_enter(&so->so_lock);
319 322 so->so_snd_qfull = B_FALSE;
320 323 /* wake up everyone waiting for buffers */
321 324 cv_broadcast(&so->so_snd_cv);
322 325 mutex_exit(&so->so_lock);
323 326 }
324 327
325 328 /*
326 329 * Change the process/process group to which SIGIO is sent.
327 330 */
328 331 int
329 332 socket_chgpgrp(struct sonode *so, pid_t pid)
330 333 {
331 334 int error;
332 335
333 336 ASSERT(MUTEX_HELD(&so->so_lock));
334 337 if (pid != 0) {
335 338 /*
336 339 * Permissions check by sending signal 0.
337 340 * Note that when kill fails it does a
338 341 * set_errno causing the system call to fail.
339 342 */
340 343 error = kill(pid, 0);
341 344 if (error != 0) {
342 345 return (error);
343 346 }
344 347 }
345 348 so->so_pgrp = pid;
346 349 return (0);
347 350 }
348 351
349 352
350 353 /*
351 354 * Generate a SIGIO, for 'writable' events include siginfo structure,
352 355 * for read events just send the signal.
353 356 */
354 357 /*ARGSUSED*/
355 358 static void
356 359 socket_sigproc(proc_t *proc, int event)
357 360 {
358 361 k_siginfo_t info;
359 362
360 363 ASSERT(event & (SOCKETSIG_WRITE | SOCKETSIG_READ | SOCKETSIG_URG));
361 364
362 365 if (event & SOCKETSIG_WRITE) {
363 366 info.si_signo = SIGPOLL;
364 367 info.si_code = POLL_OUT;
365 368 info.si_errno = 0;
366 369 info.si_fd = 0;
367 370 info.si_band = 0;
368 371 sigaddq(proc, NULL, &info, KM_NOSLEEP);
369 372 }
370 373 if (event & SOCKETSIG_READ) {
371 374 sigtoproc(proc, NULL, SIGPOLL);
372 375 }
373 376 if (event & SOCKETSIG_URG) {
374 377 sigtoproc(proc, NULL, SIGURG);
375 378 }
376 379 }
377 380
378 381 void
379 382 socket_sendsig(struct sonode *so, int event)
380 383 {
381 384 proc_t *proc;
382 385
383 386 ASSERT(MUTEX_HELD(&so->so_lock));
384 387
385 388 if (so->so_pgrp == 0 || (!(so->so_state & SS_ASYNC) &&
386 389 event != SOCKETSIG_URG)) {
387 390 return;
388 391 }
|
↓ open down ↓ |
354 lines elided |
↑ open up ↑ |
389 392
390 393 dprint(3, ("sending sig %d to %d\n", event, so->so_pgrp));
391 394
392 395 if (so->so_pgrp > 0) {
393 396 /*
394 397 * XXX This unfortunately still generates
395 398 * a signal when a fd is closed but
396 399 * the proc is active.
397 400 */
398 401 mutex_enter(&pidlock);
399 - proc = prfind(so->so_pgrp);
402 + /*
403 + * Even if the thread started in another zone, we're receiving
404 + * on behalf of this socket's zone, so find the proc using the
405 + * socket's zone ID.
406 + */
407 + proc = prfind_zone(so->so_pgrp, so->so_zoneid);
400 408 if (proc == NULL) {
401 409 mutex_exit(&pidlock);
402 410 return;
403 411 }
404 412 mutex_enter(&proc->p_lock);
405 413 mutex_exit(&pidlock);
406 414 socket_sigproc(proc, event);
407 415 mutex_exit(&proc->p_lock);
408 416 } else {
409 417 /*
410 418 * Send to process group. Hold pidlock across
411 419 * calls to socket_sigproc().
412 420 */
413 421 pid_t pgrp = -so->so_pgrp;
414 422
415 423 mutex_enter(&pidlock);
416 - proc = pgfind(pgrp);
424 + /*
425 + * Even if the thread started in another zone, we're receiving
426 + * on behalf of this socket's zone, so find the pgrp using the
427 + * socket's zone ID.
428 + */
429 + proc = pgfind_zone(pgrp, so->so_zoneid);
417 430 while (proc != NULL) {
418 431 mutex_enter(&proc->p_lock);
419 432 socket_sigproc(proc, event);
420 433 mutex_exit(&proc->p_lock);
421 434 proc = proc->p_pglink;
422 435 }
423 436 mutex_exit(&pidlock);
424 437 }
425 438 }
426 439
427 440 #define MIN(a, b) ((a) < (b) ? (a) : (b))
428 441 /* Copy userdata into a new mblk_t */
429 442 mblk_t *
430 443 socopyinuio(uio_t *uiop, ssize_t iosize, size_t wroff, ssize_t maxblk,
431 444 size_t tail_len, int *errorp)
432 445 {
433 446 mblk_t *head = NULL, **tail = &head;
434 447
435 448 ASSERT(iosize == INFPSZ || iosize > 0);
436 449
437 450 if (iosize == INFPSZ || iosize > uiop->uio_resid)
438 451 iosize = uiop->uio_resid;
439 452
440 453 if (maxblk == INFPSZ)
441 454 maxblk = iosize;
442 455
443 456 /* Nothing to do in these cases, so we're done */
444 457 if (iosize < 0 || maxblk < 0 || (maxblk == 0 && iosize > 0))
445 458 goto done;
446 459
447 460 /*
448 461 * We will enter the loop below if iosize is 0; it will allocate an
449 462 * empty message block and call uiomove(9F) which will just return.
450 463 * We could avoid that with an extra check but would only slow
451 464 * down the much more likely case where iosize is larger than 0.
452 465 */
453 466 do {
454 467 ssize_t blocksize;
455 468 mblk_t *mp;
456 469
457 470 blocksize = MIN(iosize, maxblk);
458 471 ASSERT(blocksize >= 0);
459 472 mp = allocb(wroff + blocksize + tail_len, BPRI_MED);
460 473 if (mp == NULL) {
461 474 *errorp = ENOMEM;
462 475 return (head);
463 476 }
464 477 mp->b_rptr += wroff;
465 478 mp->b_wptr = mp->b_rptr + blocksize;
466 479
467 480 *tail = mp;
468 481 tail = &mp->b_cont;
469 482
470 483 /* uiomove(9F) either returns 0 or EFAULT */
471 484 if ((*errorp = uiomove(mp->b_rptr, (size_t)blocksize,
472 485 UIO_WRITE, uiop)) != 0) {
473 486 ASSERT(*errorp != ENOMEM);
474 487 freemsg(head);
475 488 return (NULL);
476 489 }
477 490
478 491 iosize -= blocksize;
479 492 } while (iosize > 0);
480 493
481 494 done:
482 495 *errorp = 0;
483 496 return (head);
484 497 }
485 498
486 499 mblk_t *
487 500 socopyoutuio(mblk_t *mp, struct uio *uiop, ssize_t max_read, int *errorp)
488 501 {
489 502 int error;
490 503 ptrdiff_t n;
491 504 mblk_t *nmp;
492 505
493 506 ASSERT(mp->b_wptr >= mp->b_rptr);
494 507
495 508 /*
496 509 * max_read is the offset of the oobmark and read can not go pass
497 510 * the oobmark.
498 511 */
499 512 if (max_read == INFPSZ || max_read > uiop->uio_resid)
500 513 max_read = uiop->uio_resid;
501 514
502 515 do {
503 516 if ((n = MIN(max_read, MBLKL(mp))) != 0) {
504 517 ASSERT(n > 0);
505 518
506 519 error = uiomove(mp->b_rptr, n, UIO_READ, uiop);
507 520 if (error != 0) {
508 521 freemsg(mp);
509 522 *errorp = error;
510 523 return (NULL);
511 524 }
512 525 }
513 526
514 527 mp->b_rptr += n;
515 528 max_read -= n;
516 529 while (mp != NULL && (mp->b_rptr >= mp->b_wptr)) {
517 530 /*
518 531 * get rid of zero length mblks
519 532 */
520 533 nmp = mp;
521 534 mp = mp->b_cont;
522 535 freeb(nmp);
523 536 }
524 537 } while (mp != NULL && max_read > 0);
525 538
526 539 *errorp = 0;
527 540 return (mp);
528 541 }
529 542
530 543 static void
531 544 so_prepend_msg(struct sonode *so, mblk_t *mp, mblk_t *last_tail)
532 545 {
533 546 ASSERT(last_tail != NULL);
534 547 mp->b_next = so->so_rcv_q_head;
535 548 mp->b_prev = last_tail;
536 549 ASSERT(!(DB_FLAGS(mp) & DBLK_UIOA));
537 550
538 551 if (so->so_rcv_q_head == NULL) {
539 552 ASSERT(so->so_rcv_q_last_head == NULL);
540 553 so->so_rcv_q_last_head = mp;
541 554 #ifdef DEBUG
542 555 } else {
543 556 ASSERT(!(DB_FLAGS(so->so_rcv_q_head) & DBLK_UIOA));
544 557 #endif
545 558 }
546 559 so->so_rcv_q_head = mp;
547 560
548 561 #ifdef DEBUG
549 562 if (so_debug_length) {
550 563 mutex_enter(&so->so_lock);
551 564 ASSERT(so_check_length(so));
552 565 mutex_exit(&so->so_lock);
553 566 }
554 567 #endif
555 568 }
556 569
557 570 /*
558 571 * Move a mblk chain (mp_head, mp_last_head) to the sonode's rcv queue so it
559 572 * can be processed by so_dequeue_msg().
560 573 */
561 574 void
562 575 so_process_new_message(struct sonode *so, mblk_t *mp_head, mblk_t *mp_last_head)
563 576 {
564 577 if (so->so_filter_active > 0 &&
565 578 (mp_head = sof_filter_data_in_proc(so, mp_head,
566 579 &mp_last_head)) == NULL)
567 580 return;
568 581
569 582 ASSERT(mp_head->b_prev != NULL);
570 583 if (so->so_rcv_q_head == NULL) {
571 584 so->so_rcv_q_head = mp_head;
572 585 so->so_rcv_q_last_head = mp_last_head;
573 586 ASSERT(so->so_rcv_q_last_head->b_prev != NULL);
574 587 } else {
575 588 boolean_t flag_equal = ((DB_FLAGS(mp_head) & DBLK_UIOA) ==
576 589 (DB_FLAGS(so->so_rcv_q_last_head) & DBLK_UIOA));
577 590
578 591 if (mp_head->b_next == NULL &&
579 592 DB_TYPE(mp_head) == M_DATA &&
580 593 DB_TYPE(so->so_rcv_q_last_head) == M_DATA && flag_equal) {
581 594 so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
582 595 so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
583 596 mp_head->b_prev = NULL;
584 597 } else if (flag_equal && (DB_FLAGS(mp_head) & DBLK_UIOA)) {
585 598 /*
586 599 * Append to last_head if more than one mblks, and both
587 600 * mp_head and last_head are I/OAT mblks.
588 601 */
589 602 ASSERT(mp_head->b_next != NULL);
590 603 so->so_rcv_q_last_head->b_prev->b_cont = mp_head;
591 604 so->so_rcv_q_last_head->b_prev = mp_head->b_prev;
592 605 mp_head->b_prev = NULL;
593 606
594 607 so->so_rcv_q_last_head->b_next = mp_head->b_next;
595 608 mp_head->b_next = NULL;
596 609 so->so_rcv_q_last_head = mp_last_head;
597 610 } else {
598 611 #ifdef DEBUG
599 612 {
600 613 mblk_t *tmp_mblk;
601 614 tmp_mblk = mp_head;
602 615 while (tmp_mblk != NULL) {
603 616 ASSERT(tmp_mblk->b_prev != NULL);
604 617 tmp_mblk = tmp_mblk->b_next;
605 618 }
606 619 }
607 620 #endif
608 621 so->so_rcv_q_last_head->b_next = mp_head;
609 622 so->so_rcv_q_last_head = mp_last_head;
610 623 }
611 624 }
612 625 }
613 626
614 627 /*
615 628 * Check flow control on a given sonode. Must have so_lock held, and
616 629 * this function will release the hold. Return true if flow control
617 630 * is cleared.
618 631 */
619 632 boolean_t
620 633 so_check_flow_control(struct sonode *so)
621 634 {
622 635 ASSERT(MUTEX_HELD(&so->so_lock));
623 636
624 637 if (so->so_flowctrld && (so->so_rcv_queued < so->so_rcvlowat &&
625 638 !(so->so_state & SS_FIL_RCV_FLOWCTRL))) {
626 639 so->so_flowctrld = B_FALSE;
627 640 mutex_exit(&so->so_lock);
628 641 /*
629 642 * Open up flow control. SCTP does not have any downcalls, and
630 643 * it will clr flow ctrl in sosctp_recvmsg().
631 644 */
632 645 if (so->so_downcalls != NULL &&
633 646 so->so_downcalls->sd_clr_flowctrl != NULL) {
634 647 (*so->so_downcalls->sd_clr_flowctrl)
635 648 (so->so_proto_handle);
636 649 }
637 650 /* filters can start injecting data */
638 651 sof_sonode_notify_filters(so, SOF_EV_INJECT_DATA_IN_OK, 0);
639 652 return (B_TRUE);
640 653 } else {
641 654 mutex_exit(&so->so_lock);
642 655 return (B_FALSE);
643 656 }
644 657 }
645 658
646 659 int
647 660 so_dequeue_msg(struct sonode *so, mblk_t **mctlp, struct uio *uiop,
648 661 rval_t *rvalp, int flags)
649 662 {
650 663 mblk_t *mp, *nmp;
651 664 mblk_t *savemp, *savemptail;
652 665 mblk_t *new_msg_head;
653 666 mblk_t *new_msg_last_head;
654 667 mblk_t *last_tail;
655 668 boolean_t partial_read;
656 669 boolean_t reset_atmark = B_FALSE;
657 670 int more = 0;
658 671 int error;
659 672 ssize_t oobmark;
660 673 sodirect_t *sodp = so->so_direct;
661 674
662 675 partial_read = B_FALSE;
663 676 *mctlp = NULL;
664 677 again:
665 678 mutex_enter(&so->so_lock);
666 679 again1:
667 680 #ifdef DEBUG
668 681 if (so_debug_length) {
669 682 ASSERT(so_check_length(so));
670 683 }
671 684 #endif
672 685 if (so->so_state & SS_RCVATMARK) {
673 686 /* Check whether the caller is OK to read past the mark */
674 687 if (flags & MSG_NOMARK) {
675 688 mutex_exit(&so->so_lock);
676 689 return (EWOULDBLOCK);
677 690 }
678 691 reset_atmark = B_TRUE;
679 692 }
680 693 /*
681 694 * First move messages from the dump area to processing area
682 695 */
683 696 if (sodp != NULL) {
684 697 if (sodp->sod_enabled) {
685 698 if (sodp->sod_uioa.uioa_state & UIOA_ALLOC) {
686 699 /* nothing to uioamove */
687 700 sodp = NULL;
688 701 } else if (sodp->sod_uioa.uioa_state & UIOA_INIT) {
689 702 sodp->sod_uioa.uioa_state &= UIOA_CLR;
690 703 sodp->sod_uioa.uioa_state |= UIOA_ENABLED;
691 704 /*
692 705 * try to uioamove() the data that
693 706 * has already queued.
694 707 */
695 708 sod_uioa_so_init(so, sodp, uiop);
696 709 }
697 710 } else {
698 711 sodp = NULL;
699 712 }
700 713 }
701 714 new_msg_head = so->so_rcv_head;
702 715 new_msg_last_head = so->so_rcv_last_head;
703 716 so->so_rcv_head = NULL;
704 717 so->so_rcv_last_head = NULL;
705 718 oobmark = so->so_oobmark;
706 719 /*
707 720 * We can release the lock as there can only be one reader
708 721 */
709 722 mutex_exit(&so->so_lock);
710 723
711 724 if (new_msg_head != NULL) {
712 725 so_process_new_message(so, new_msg_head, new_msg_last_head);
713 726 }
714 727 savemp = savemptail = NULL;
715 728 rvalp->r_vals = 0;
716 729 error = 0;
717 730 mp = so->so_rcv_q_head;
718 731
719 732 if (mp != NULL &&
720 733 (so->so_rcv_timer_tid == 0 ||
721 734 so->so_rcv_queued >= so->so_rcv_thresh)) {
722 735 partial_read = B_FALSE;
723 736
724 737 if (flags & MSG_PEEK) {
725 738 if ((nmp = dupmsg(mp)) == NULL &&
726 739 (nmp = copymsg(mp)) == NULL) {
727 740 size_t size = msgsize(mp);
728 741
729 742 error = strwaitbuf(size, BPRI_HI);
730 743 if (error) {
731 744 return (error);
732 745 }
733 746 goto again;
734 747 }
735 748 mp = nmp;
736 749 } else {
737 750 ASSERT(mp->b_prev != NULL);
738 751 last_tail = mp->b_prev;
739 752 mp->b_prev = NULL;
740 753 so->so_rcv_q_head = mp->b_next;
741 754 if (so->so_rcv_q_head == NULL) {
742 755 so->so_rcv_q_last_head = NULL;
743 756 }
744 757 mp->b_next = NULL;
745 758 }
746 759
747 760 ASSERT(mctlp != NULL);
748 761 /*
749 762 * First process PROTO or PCPROTO blocks, if any.
750 763 */
751 764 if (DB_TYPE(mp) != M_DATA) {
752 765 *mctlp = mp;
753 766 savemp = mp;
754 767 savemptail = mp;
755 768 ASSERT(DB_TYPE(mp) == M_PROTO ||
756 769 DB_TYPE(mp) == M_PCPROTO);
757 770 while (mp->b_cont != NULL &&
758 771 DB_TYPE(mp->b_cont) != M_DATA) {
759 772 ASSERT(DB_TYPE(mp->b_cont) == M_PROTO ||
760 773 DB_TYPE(mp->b_cont) == M_PCPROTO);
761 774 mp = mp->b_cont;
762 775 savemptail = mp;
763 776 }
764 777 mp = savemptail->b_cont;
765 778 savemptail->b_cont = NULL;
766 779 }
767 780
768 781 ASSERT(DB_TYPE(mp) == M_DATA);
769 782 /*
770 783 * Now process DATA blocks, if any. Note that for sodirect
771 784 * enabled socket, uio_resid can be 0.
772 785 */
773 786 if (uiop->uio_resid >= 0) {
774 787 ssize_t copied = 0;
775 788
776 789 if (sodp != NULL && (DB_FLAGS(mp) & DBLK_UIOA)) {
777 790 mutex_enter(&so->so_lock);
778 791 ASSERT(uiop == (uio_t *)&sodp->sod_uioa);
779 792 copied = sod_uioa_mblk(so, mp);
780 793 if (copied > 0)
781 794 partial_read = B_TRUE;
782 795 mutex_exit(&so->so_lock);
783 796 /* mark this mblk as processed */
784 797 mp = NULL;
785 798 } else {
786 799 ssize_t oldresid = uiop->uio_resid;
787 800
788 801 if (MBLKL(mp) < so_mblk_pull_len) {
789 802 if (pullupmsg(mp, -1) == 1) {
790 803 last_tail = mp;
791 804 }
792 805 }
793 806 /*
794 807 * Can not read beyond the oobmark
795 808 */
796 809 mp = socopyoutuio(mp, uiop,
797 810 oobmark == 0 ? INFPSZ : oobmark, &error);
798 811 if (error != 0) {
799 812 freemsg(*mctlp);
800 813 *mctlp = NULL;
801 814 more = 0;
802 815 goto done;
803 816 }
804 817 ASSERT(oldresid >= uiop->uio_resid);
805 818 copied = oldresid - uiop->uio_resid;
806 819 if (oldresid > uiop->uio_resid)
807 820 partial_read = B_TRUE;
808 821 }
809 822 ASSERT(copied >= 0);
810 823 if (copied > 0 && !(flags & MSG_PEEK)) {
811 824 mutex_enter(&so->so_lock);
812 825 so->so_rcv_queued -= copied;
813 826 ASSERT(so->so_oobmark >= 0);
814 827 if (so->so_oobmark > 0) {
815 828 so->so_oobmark -= copied;
816 829 ASSERT(so->so_oobmark >= 0);
817 830 if (so->so_oobmark == 0) {
818 831 ASSERT(so->so_state &
819 832 SS_OOBPEND);
820 833 so->so_oobmark = 0;
821 834 so->so_state |= SS_RCVATMARK;
822 835 }
823 836 }
824 837 /*
825 838 * so_check_flow_control() will drop
826 839 * so->so_lock.
827 840 */
828 841 rvalp->r_val2 = so_check_flow_control(so);
829 842 }
830 843 }
831 844 if (mp != NULL) { /* more data blocks in msg */
832 845 more |= MOREDATA;
833 846 if ((flags & (MSG_PEEK|MSG_TRUNC))) {
834 847 if (flags & MSG_PEEK) {
835 848 freemsg(mp);
836 849 } else {
837 850 unsigned int msize = msgdsize(mp);
838 851
839 852 freemsg(mp);
840 853 mutex_enter(&so->so_lock);
841 854 so->so_rcv_queued -= msize;
842 855 /*
843 856 * so_check_flow_control() will drop
844 857 * so->so_lock.
845 858 */
846 859 rvalp->r_val2 =
847 860 so_check_flow_control(so);
848 861 }
849 862 } else if (partial_read && !somsghasdata(mp)) {
850 863 /*
851 864 * Avoid queuing a zero-length tail part of
852 865 * a message. partial_read == 1 indicates that
853 866 * we read some of the message.
854 867 */
855 868 freemsg(mp);
856 869 more &= ~MOREDATA;
857 870 } else {
858 871 if (savemp != NULL &&
859 872 (flags & MSG_DUPCTRL)) {
860 873 mblk_t *nmp;
861 874 /*
862 875 * There should only be non data mblks
863 876 */
864 877 ASSERT(DB_TYPE(savemp) != M_DATA &&
865 878 DB_TYPE(savemptail) != M_DATA);
866 879 try_again:
867 880 if ((nmp = dupmsg(savemp)) == NULL &&
868 881 (nmp = copymsg(savemp)) == NULL) {
869 882
870 883 size_t size = msgsize(savemp);
871 884
872 885 error = strwaitbuf(size,
873 886 BPRI_HI);
874 887 if (error != 0) {
875 888 /*
876 889 * In case we
877 890 * cannot copy
878 891 * control data
879 892 * free the remaining
880 893 * data.
881 894 */
882 895 freemsg(mp);
883 896 goto done;
884 897 }
885 898 goto try_again;
886 899 }
887 900
888 901 ASSERT(nmp != NULL);
889 902 ASSERT(DB_TYPE(nmp) != M_DATA);
890 903 savemptail->b_cont = mp;
891 904 *mctlp = nmp;
892 905 mp = savemp;
893 906 }
894 907 /*
895 908 * putback mp
896 909 */
897 910 so_prepend_msg(so, mp, last_tail);
898 911 }
899 912 }
900 913
901 914 /* fast check so_rcv_head if there is more data */
902 915 if (partial_read && !(so->so_state & SS_RCVATMARK) &&
903 916 *mctlp == NULL && uiop->uio_resid > 0 &&
904 917 !(flags & MSG_PEEK) && so->so_rcv_head != NULL) {
905 918 goto again;
906 919 }
907 920 } else if (!partial_read) {
908 921 mutex_enter(&so->so_lock);
909 922 if (so->so_error != 0) {
910 923 error = sogeterr(so, !(flags & MSG_PEEK));
911 924 mutex_exit(&so->so_lock);
912 925 return (error);
913 926 }
914 927 /*
915 928 * No pending data. Return right away for nonblocking
916 929 * socket, otherwise sleep waiting for data.
917 930 */
918 931 if (!(so->so_state & SS_CANTRCVMORE) && uiop->uio_resid > 0) {
919 932 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
920 933 (flags & MSG_DONTWAIT)) {
921 934 error = EWOULDBLOCK;
922 935 } else {
923 936 if (so->so_state & (SS_CLOSING |
924 937 SS_FALLBACK_PENDING)) {
925 938 mutex_exit(&so->so_lock);
926 939 error = EINTR;
927 940 goto done;
928 941 }
929 942
930 943 if (so->so_rcv_head != NULL) {
931 944 goto again1;
932 945 }
933 946 so->so_rcv_wakeup = B_TRUE;
934 947 so->so_rcv_wanted = uiop->uio_resid;
935 948 if (so->so_rcvtimeo == 0) {
936 949 /*
937 950 * Zero means disable timeout.
938 951 */
939 952 error = cv_wait_sig(&so->so_rcv_cv,
940 953 &so->so_lock);
941 954 } else {
942 955 error = cv_reltimedwait_sig(
943 956 &so->so_rcv_cv, &so->so_lock,
944 957 so->so_rcvtimeo, TR_CLOCK_TICK);
945 958 }
946 959 so->so_rcv_wakeup = B_FALSE;
947 960 so->so_rcv_wanted = 0;
948 961
949 962 if (error == 0) {
950 963 error = EINTR;
951 964 } else if (error == -1) {
952 965 error = EAGAIN;
953 966 } else {
954 967 goto again1;
955 968 }
956 969 }
957 970 }
958 971 mutex_exit(&so->so_lock);
959 972 }
960 973 if (reset_atmark && partial_read && !(flags & MSG_PEEK)) {
961 974 /*
962 975 * We are passed the mark, update state
963 976 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
964 977 * The draft Posix socket spec states that the mark should
965 978 * not be cleared when peeking. We follow the latter.
966 979 */
967 980 mutex_enter(&so->so_lock);
968 981 ASSERT(so_verify_oobstate(so));
969 982 so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
970 983 freemsg(so->so_oobmsg);
971 984 so->so_oobmsg = NULL;
972 985 ASSERT(so_verify_oobstate(so));
973 986 mutex_exit(&so->so_lock);
974 987 }
975 988 ASSERT(so->so_rcv_wakeup == B_FALSE);
976 989 done:
977 990 if (sodp != NULL) {
978 991 mutex_enter(&so->so_lock);
979 992 if (sodp->sod_enabled &&
980 993 (sodp->sod_uioa.uioa_state & UIOA_ENABLED)) {
981 994 SOD_UIOAFINI(sodp);
982 995 if (sodp->sod_uioa.uioa_mbytes > 0) {
983 996 ASSERT(so->so_rcv_q_head != NULL ||
984 997 so->so_rcv_head != NULL);
985 998 so->so_rcv_queued -= sod_uioa_mblk(so, NULL);
986 999 if (error == EWOULDBLOCK)
987 1000 error = 0;
988 1001 }
989 1002 }
990 1003 mutex_exit(&so->so_lock);
991 1004 }
992 1005 #ifdef DEBUG
993 1006 if (so_debug_length) {
994 1007 mutex_enter(&so->so_lock);
995 1008 ASSERT(so_check_length(so));
996 1009 mutex_exit(&so->so_lock);
997 1010 }
998 1011 #endif
999 1012 rvalp->r_val1 = more;
1000 1013 ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1001 1014 return (error);
1002 1015 }
1003 1016
1004 1017 /*
1005 1018 * Enqueue data from the protocol on the socket's rcv queue.
1006 1019 *
1007 1020 * We try to hook new M_DATA mblks onto an existing chain, however,
1008 1021 * that cannot be done if the existing chain has already been
1009 1022 * processed by I/OAT. Non-M_DATA mblks are just linked together via
1010 1023 * b_next. In all cases the b_prev of the enqueued mblk is set to
1011 1024 * point to the last mblk in its b_cont chain.
1012 1025 */
1013 1026 void
1014 1027 so_enqueue_msg(struct sonode *so, mblk_t *mp, size_t msg_size)
1015 1028 {
1016 1029 ASSERT(MUTEX_HELD(&so->so_lock));
1017 1030
1018 1031 #ifdef DEBUG
1019 1032 if (so_debug_length) {
1020 1033 ASSERT(so_check_length(so));
1021 1034 }
1022 1035 #endif
1023 1036 so->so_rcv_queued += msg_size;
1024 1037
1025 1038 if (so->so_rcv_head == NULL) {
1026 1039 ASSERT(so->so_rcv_last_head == NULL);
1027 1040 so->so_rcv_head = mp;
1028 1041 so->so_rcv_last_head = mp;
1029 1042 } else if ((DB_TYPE(mp) == M_DATA &&
1030 1043 DB_TYPE(so->so_rcv_last_head) == M_DATA) &&
1031 1044 ((DB_FLAGS(mp) & DBLK_UIOA) ==
1032 1045 (DB_FLAGS(so->so_rcv_last_head) & DBLK_UIOA))) {
1033 1046 /* Added to the end */
1034 1047 ASSERT(so->so_rcv_last_head != NULL);
1035 1048 ASSERT(so->so_rcv_last_head->b_prev != NULL);
1036 1049 so->so_rcv_last_head->b_prev->b_cont = mp;
1037 1050 } else {
1038 1051 /* Start a new end */
1039 1052 so->so_rcv_last_head->b_next = mp;
1040 1053 so->so_rcv_last_head = mp;
1041 1054 }
1042 1055 while (mp->b_cont != NULL)
1043 1056 mp = mp->b_cont;
1044 1057
1045 1058 so->so_rcv_last_head->b_prev = mp;
1046 1059 #ifdef DEBUG
1047 1060 if (so_debug_length) {
1048 1061 ASSERT(so_check_length(so));
1049 1062 }
1050 1063 #endif
1051 1064 }
1052 1065
1053 1066 /*
1054 1067 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
1055 1068 */
1056 1069 boolean_t
1057 1070 somsghasdata(mblk_t *mp)
1058 1071 {
1059 1072 for (; mp; mp = mp->b_cont)
1060 1073 if (mp->b_datap->db_type == M_DATA) {
1061 1074 ASSERT(mp->b_wptr >= mp->b_rptr);
1062 1075 if (mp->b_wptr > mp->b_rptr)
1063 1076 return (B_TRUE);
1064 1077 }
1065 1078 return (B_FALSE);
1066 1079 }
1067 1080
1068 1081 /*
1069 1082 * Flush the read side of sockfs.
1070 1083 *
1071 1084 * The caller must be sure that a reader is not already active when the
1072 1085 * buffer is being flushed.
1073 1086 */
1074 1087 void
1075 1088 so_rcv_flush(struct sonode *so)
1076 1089 {
1077 1090 mblk_t *mp;
1078 1091
1079 1092 ASSERT(MUTEX_HELD(&so->so_lock));
1080 1093
1081 1094 if (so->so_oobmsg != NULL) {
1082 1095 freemsg(so->so_oobmsg);
1083 1096 so->so_oobmsg = NULL;
1084 1097 so->so_oobmark = 0;
1085 1098 so->so_state &=
1086 1099 ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|SS_RCVATMARK);
1087 1100 }
1088 1101
1089 1102 /*
1090 1103 * Free messages sitting in the recv queues
1091 1104 */
1092 1105 while (so->so_rcv_q_head != NULL) {
1093 1106 mp = so->so_rcv_q_head;
1094 1107 so->so_rcv_q_head = mp->b_next;
1095 1108 mp->b_next = mp->b_prev = NULL;
1096 1109 freemsg(mp);
1097 1110 }
1098 1111 while (so->so_rcv_head != NULL) {
1099 1112 mp = so->so_rcv_head;
1100 1113 so->so_rcv_head = mp->b_next;
1101 1114 mp->b_next = mp->b_prev = NULL;
1102 1115 freemsg(mp);
1103 1116 }
1104 1117 so->so_rcv_queued = 0;
1105 1118 so->so_rcv_q_head = NULL;
1106 1119 so->so_rcv_q_last_head = NULL;
1107 1120 so->so_rcv_head = NULL;
1108 1121 so->so_rcv_last_head = NULL;
1109 1122 }
1110 1123
1111 1124 /*
1112 1125 * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
1113 1126 */
1114 1127 int
1115 1128 sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags,
1116 1129 boolean_t oob_inline)
1117 1130 {
1118 1131 mblk_t *mp, *nmp;
1119 1132 int error;
1120 1133
1121 1134 dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", (void *)so, (void *)msg,
1122 1135 flags));
1123 1136
1124 1137 if (msg != NULL) {
1125 1138 /*
1126 1139 * There is never any oob data with addresses or control since
1127 1140 * the T_EXDATA_IND does not carry any options.
1128 1141 */
1129 1142 msg->msg_controllen = 0;
1130 1143 msg->msg_namelen = 0;
1131 1144 msg->msg_flags = 0;
1132 1145 }
1133 1146
1134 1147 mutex_enter(&so->so_lock);
1135 1148 ASSERT(so_verify_oobstate(so));
1136 1149 if (oob_inline ||
1137 1150 (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
1138 1151 dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
1139 1152 mutex_exit(&so->so_lock);
1140 1153 return (EINVAL);
1141 1154 }
1142 1155 if (!(so->so_state & SS_HAVEOOBDATA)) {
1143 1156 dprintso(so, 1, ("sorecvoob: no data yet\n"));
1144 1157 mutex_exit(&so->so_lock);
1145 1158 return (EWOULDBLOCK);
1146 1159 }
1147 1160 ASSERT(so->so_oobmsg != NULL);
1148 1161 mp = so->so_oobmsg;
1149 1162 if (flags & MSG_PEEK) {
1150 1163 /*
1151 1164 * Since recv* can not return ENOBUFS we can not use dupmsg.
1152 1165 * Instead we revert to the consolidation private
1153 1166 * allocb_wait plus bcopy.
1154 1167 */
1155 1168 mblk_t *mp1;
1156 1169
1157 1170 mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
1158 1171 ASSERT(mp1);
1159 1172
1160 1173 while (mp != NULL) {
1161 1174 ssize_t size;
1162 1175
1163 1176 size = MBLKL(mp);
1164 1177 bcopy(mp->b_rptr, mp1->b_wptr, size);
1165 1178 mp1->b_wptr += size;
1166 1179 ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim);
1167 1180 mp = mp->b_cont;
1168 1181 }
1169 1182 mp = mp1;
1170 1183 } else {
1171 1184 /*
1172 1185 * Update the state indicating that the data has been consumed.
1173 1186 * Keep SS_OOBPEND set until data is consumed past the mark.
1174 1187 */
1175 1188 so->so_oobmsg = NULL;
1176 1189 so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA;
1177 1190 }
1178 1191 ASSERT(so_verify_oobstate(so));
1179 1192 mutex_exit(&so->so_lock);
1180 1193
1181 1194 error = 0;
1182 1195 nmp = mp;
1183 1196 while (nmp != NULL && uiop->uio_resid > 0) {
1184 1197 ssize_t n = MBLKL(nmp);
1185 1198
1186 1199 n = MIN(n, uiop->uio_resid);
1187 1200 if (n > 0)
1188 1201 error = uiomove(nmp->b_rptr, n,
1189 1202 UIO_READ, uiop);
1190 1203 if (error)
1191 1204 break;
1192 1205 nmp = nmp->b_cont;
1193 1206 }
1194 1207 ASSERT(mp->b_next == NULL && mp->b_prev == NULL);
1195 1208 freemsg(mp);
1196 1209 return (error);
1197 1210 }
1198 1211
1199 1212 /*
1200 1213 * Allocate and initializ sonode
1201 1214 */
1202 1215 /* ARGSUSED */
1203 1216 struct sonode *
1204 1217 socket_sonode_create(struct sockparams *sp, int family, int type,
1205 1218 int protocol, int version, int sflags, int *errorp, struct cred *cr)
1206 1219 {
1207 1220 sonode_t *so;
1208 1221 int kmflags;
1209 1222
1210 1223 /*
1211 1224 * Choose the right set of sonodeops based on the upcall and
1212 1225 * down call version that the protocol has provided
1213 1226 */
1214 1227 if (SOCK_UC_VERSION != sp->sp_smod_info->smod_uc_version ||
1215 1228 SOCK_DC_VERSION != sp->sp_smod_info->smod_dc_version) {
1216 1229 /*
1217 1230 * mismatch
1218 1231 */
1219 1232 #ifdef DEBUG
1220 1233 cmn_err(CE_CONT, "protocol and socket module version mismatch");
1221 1234 #endif
1222 1235 *errorp = EINVAL;
1223 1236 return (NULL);
1224 1237 }
1225 1238
1226 1239 kmflags = (sflags & SOCKET_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
1227 1240
1228 1241 so = kmem_cache_alloc(socket_cache, kmflags);
1229 1242 if (so == NULL) {
1230 1243 *errorp = ENOMEM;
1231 1244 return (NULL);
1232 1245 }
1233 1246
1234 1247 sonode_init(so, sp, family, type, protocol, &so_sonodeops);
1235 1248
1236 1249 if (version == SOV_DEFAULT)
1237 1250 version = so_default_version;
1238 1251
1239 1252 so->so_version = (short)version;
1240 1253
1241 1254 /*
1242 1255 * set the default values to be INFPSZ
1243 1256 * if a protocol desires it can change the value later
1244 1257 */
1245 1258 so->so_proto_props.sopp_rxhiwat = SOCKET_RECVHIWATER;
1246 1259 so->so_proto_props.sopp_rxlowat = SOCKET_RECVLOWATER;
1247 1260 so->so_proto_props.sopp_maxpsz = INFPSZ;
1248 1261 so->so_proto_props.sopp_maxblk = INFPSZ;
1249 1262
1250 1263 return (so);
1251 1264 }
1252 1265
1253 1266 int
1254 1267 socket_init_common(struct sonode *so, struct sonode *pso, int flags, cred_t *cr)
1255 1268 {
1256 1269 int error = 0;
1257 1270
1258 1271 if (pso != NULL) {
1259 1272 /*
1260 1273 * We have a passive open, so inherit basic state from
1261 1274 * the parent (listener).
1262 1275 *
1263 1276 * No need to grab the new sonode's lock, since there is no
1264 1277 * one that can have a reference to it.
1265 1278 */
1266 1279 mutex_enter(&pso->so_lock);
1267 1280
1268 1281 so->so_state |= SS_ISCONNECTED | (pso->so_state & SS_ASYNC);
1269 1282 so->so_pgrp = pso->so_pgrp;
1270 1283 so->so_rcvtimeo = pso->so_rcvtimeo;
1271 1284 so->so_sndtimeo = pso->so_sndtimeo;
1272 1285 so->so_xpg_rcvbuf = pso->so_xpg_rcvbuf;
1273 1286 /*
1274 1287 * Make note of the socket level options. TCP and IP level
1275 1288 * options are already inherited. We could do all this after
1276 1289 * accept is successful but doing it here simplifies code and
1277 1290 * no harm done for error case.
1278 1291 */
1279 1292 so->so_options = pso->so_options & (SO_DEBUG|SO_REUSEADDR|
1280 1293 SO_KEEPALIVE|SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
1281 1294 SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
1282 1295 so->so_proto_props = pso->so_proto_props;
1283 1296 so->so_mode = pso->so_mode;
1284 1297 so->so_pollev = pso->so_pollev & SO_POLLEV_ALWAYS;
1285 1298
1286 1299 mutex_exit(&pso->so_lock);
1287 1300
1288 1301 /*
1289 1302 * If the parent has any filters, try to inherit them.
1290 1303 */
1291 1304 if (pso->so_filter_active > 0 &&
1292 1305 (error = sof_sonode_inherit_filters(so, pso)) != 0)
1293 1306 return (error);
1294 1307
1295 1308 } else {
1296 1309 struct sockparams *sp = so->so_sockparams;
1297 1310 sock_upcalls_t *upcalls_to_use;
1298 1311
1299 1312 /*
1300 1313 * Attach automatic filters, if there are any.
1301 1314 */
1302 1315 if (!list_is_empty(&sp->sp_auto_filters) &&
1303 1316 (error = sof_sonode_autoattach_filters(so, cr)) != 0)
1304 1317 return (error);
1305 1318
1306 1319 /* OK to attach filters */
1307 1320 so->so_state |= SS_FILOP_OK;
1308 1321
1309 1322 /*
1310 1323 * Based on the version number select the right upcalls to
1311 1324 * pass down. Currently we only have one version so choose
1312 1325 * default
1313 1326 */
1314 1327 upcalls_to_use = &so_upcalls;
1315 1328
1316 1329 /* active open, so create a lower handle */
1317 1330 so->so_proto_handle =
1318 1331 sp->sp_smod_info->smod_proto_create_func(so->so_family,
1319 1332 so->so_type, so->so_protocol, &so->so_downcalls,
1320 1333 &so->so_mode, &error, flags, cr);
1321 1334
1322 1335 if (so->so_proto_handle == NULL) {
1323 1336 ASSERT(error != 0);
1324 1337 /*
1325 1338 * To be safe; if a lower handle cannot be created, and
1326 1339 * the proto does not give a reason why, assume there
1327 1340 * was a lack of memory.
1328 1341 */
1329 1342 return ((error == 0) ? ENOMEM : error);
1330 1343 }
1331 1344 ASSERT(so->so_downcalls != NULL);
1332 1345 ASSERT(so->so_downcalls->sd_send != NULL ||
1333 1346 so->so_downcalls->sd_send_uio != NULL);
1334 1347 if (so->so_downcalls->sd_recv_uio != NULL) {
1335 1348 ASSERT(so->so_downcalls->sd_poll != NULL);
1336 1349 so->so_pollev |= SO_POLLEV_ALWAYS;
1337 1350 }
1338 1351
1339 1352 (*so->so_downcalls->sd_activate)(so->so_proto_handle,
1340 1353 (sock_upper_handle_t)so, upcalls_to_use, 0, cr);
1341 1354
1342 1355 /* Wildcard */
1343 1356
1344 1357 /*
1345 1358 * FIXME No need for this, the protocol can deal with it in
1346 1359 * sd_create(). Should update ICMP.
1347 1360 */
1348 1361 if (so->so_protocol != so->so_sockparams->sp_protocol) {
1349 1362 int protocol = so->so_protocol;
1350 1363 int error;
1351 1364 /*
1352 1365 * Issue SO_PROTOTYPE setsockopt.
1353 1366 */
1354 1367 error = socket_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
1355 1368 &protocol, (t_uscalar_t)sizeof (protocol), cr);
1356 1369 if (error) {
1357 1370 (void) (*so->so_downcalls->sd_close)
1358 1371 (so->so_proto_handle, 0, cr);
1359 1372
1360 1373 mutex_enter(&so->so_lock);
1361 1374 so_rcv_flush(so);
1362 1375 mutex_exit(&so->so_lock);
1363 1376 /*
1364 1377 * Setsockopt often fails with ENOPROTOOPT but
1365 1378 * socket() should fail with
1366 1379 * EPROTONOSUPPORT/EPROTOTYPE.
1367 1380 */
1368 1381 return (EPROTONOSUPPORT);
1369 1382 }
1370 1383 }
1371 1384 }
1372 1385
1373 1386 if (uioasync.enabled)
1374 1387 sod_sock_init(so);
1375 1388
1376 1389 /* put an extra reference on the socket for the protocol */
1377 1390 VN_HOLD(SOTOV(so));
1378 1391
1379 1392 return (0);
1380 1393 }
1381 1394
1382 1395 /*
1383 1396 * int socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1384 1397 * struct cred *cr, int32_t *rvalp)
1385 1398 *
1386 1399 * Handle ioctls that manipulate basic socket state; non-blocking,
1387 1400 * async, etc.
1388 1401 *
1389 1402 * Returns:
1390 1403 * < 0 - ioctl was not handle
1391 1404 * >= 0 - ioctl was handled, if > 0, then it is an errno
1392 1405 *
1393 1406 * Notes:
1394 1407 * Assumes the standard receive buffer is used to obtain info for
1395 1408 * NREAD.
1396 1409 */
1397 1410 /* ARGSUSED */
1398 1411 int
1399 1412 socket_ioctl_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1400 1413 struct cred *cr, int32_t *rvalp)
1401 1414 {
1402 1415 switch (cmd) {
1403 1416 case SIOCSQPTR:
1404 1417 /*
1405 1418 * SIOCSQPTR is valid only when helper stream is created
1406 1419 * by the protocol.
1407 1420 */
1408 1421
1409 1422 return (EOPNOTSUPP);
1410 1423 case FIONBIO: {
1411 1424 int32_t value;
1412 1425
1413 1426 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1414 1427 (mode & (int)FKIOCTL)))
1415 1428 return (EFAULT);
1416 1429
1417 1430 mutex_enter(&so->so_lock);
1418 1431 if (value) {
1419 1432 so->so_state |= SS_NDELAY;
1420 1433 } else {
1421 1434 so->so_state &= ~SS_NDELAY;
1422 1435 }
1423 1436 mutex_exit(&so->so_lock);
1424 1437 return (0);
1425 1438 }
1426 1439 case FIOASYNC: {
1427 1440 int32_t value;
1428 1441
1429 1442 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1430 1443 (mode & (int)FKIOCTL)))
1431 1444 return (EFAULT);
1432 1445
1433 1446 mutex_enter(&so->so_lock);
1434 1447
1435 1448 if (value) {
1436 1449 /* Turn on SIGIO */
1437 1450 so->so_state |= SS_ASYNC;
1438 1451 } else {
1439 1452 /* Turn off SIGIO */
1440 1453 so->so_state &= ~SS_ASYNC;
1441 1454 }
1442 1455 mutex_exit(&so->so_lock);
1443 1456
1444 1457 return (0);
1445 1458 }
1446 1459
1447 1460 case SIOCSPGRP:
1448 1461 case FIOSETOWN: {
1449 1462 int error;
1450 1463 pid_t pid;
1451 1464
1452 1465 if (so_copyin((void *)arg, &pid, sizeof (pid_t),
1453 1466 (mode & (int)FKIOCTL)))
1454 1467 return (EFAULT);
1455 1468
1456 1469 mutex_enter(&so->so_lock);
1457 1470 error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
1458 1471 mutex_exit(&so->so_lock);
1459 1472 return (error);
1460 1473 }
1461 1474 case SIOCGPGRP:
1462 1475 case FIOGETOWN:
1463 1476 if (so_copyout(&so->so_pgrp, (void *)arg,
1464 1477 sizeof (pid_t), (mode & (int)FKIOCTL)))
1465 1478 return (EFAULT);
1466 1479
1467 1480 return (0);
1468 1481 case SIOCATMARK: {
1469 1482 int retval;
1470 1483
1471 1484 /*
1472 1485 * Only protocols that support urgent data can handle ATMARK.
1473 1486 */
1474 1487 if ((so->so_mode & SM_EXDATA) == 0)
1475 1488 return (EINVAL);
1476 1489
1477 1490 /*
1478 1491 * If the protocol is maintaining its own buffer, then the
1479 1492 * request must be passed down.
1480 1493 */
1481 1494 if (so->so_downcalls->sd_recv_uio != NULL)
1482 1495 return (-1);
1483 1496
1484 1497 retval = (so->so_state & SS_RCVATMARK) != 0;
1485 1498
1486 1499 if (so_copyout(&retval, (void *)arg, sizeof (int),
1487 1500 (mode & (int)FKIOCTL))) {
1488 1501 return (EFAULT);
1489 1502 }
1490 1503 return (0);
1491 1504 }
1492 1505
1493 1506 case FIONREAD: {
1494 1507 int retval;
1495 1508
1496 1509 /*
1497 1510 * If the protocol is maintaining its own buffer, then the
1498 1511 * request must be passed down.
1499 1512 */
1500 1513 if (so->so_downcalls->sd_recv_uio != NULL)
1501 1514 return (-1);
1502 1515
1503 1516 retval = MIN(so->so_rcv_queued, INT_MAX);
1504 1517
1505 1518 if (so_copyout(&retval, (void *)arg,
1506 1519 sizeof (retval), (mode & (int)FKIOCTL))) {
1507 1520 return (EFAULT);
1508 1521 }
1509 1522 return (0);
1510 1523 }
1511 1524
1512 1525 case _I_GETPEERCRED: {
1513 1526 int error = 0;
1514 1527
1515 1528 if ((mode & FKIOCTL) == 0)
1516 1529 return (EINVAL);
1517 1530
1518 1531 mutex_enter(&so->so_lock);
1519 1532 if ((so->so_mode & SM_CONNREQUIRED) == 0) {
1520 1533 error = ENOTSUP;
1521 1534 } else if ((so->so_state & SS_ISCONNECTED) == 0) {
1522 1535 error = ENOTCONN;
1523 1536 } else if (so->so_peercred != NULL) {
1524 1537 k_peercred_t *kp = (k_peercred_t *)arg;
1525 1538 kp->pc_cr = so->so_peercred;
1526 1539 kp->pc_cpid = so->so_cpid;
1527 1540 crhold(so->so_peercred);
1528 1541 } else {
1529 1542 error = EINVAL;
1530 1543 }
1531 1544 mutex_exit(&so->so_lock);
1532 1545 return (error);
1533 1546 }
1534 1547 default:
1535 1548 return (-1);
1536 1549 }
1537 1550 }
1538 1551
1539 1552 /*
1540 1553 * Handle the I_NREAD STREAM ioctl.
1541 1554 */
1542 1555 static int
1543 1556 so_strioc_nread(struct sonode *so, intptr_t arg, int mode, int32_t *rvalp)
1544 1557 {
1545 1558 size_t size = 0;
1546 1559 int retval;
1547 1560 int count = 0;
1548 1561 mblk_t *mp;
1549 1562 clock_t wakeup = drv_usectohz(10);
1550 1563
1551 1564 if (so->so_downcalls == NULL ||
1552 1565 so->so_downcalls->sd_recv_uio != NULL)
1553 1566 return (EINVAL);
1554 1567
1555 1568 mutex_enter(&so->so_lock);
1556 1569 /* Wait for reader to get out of the way. */
1557 1570 while (so->so_flag & SOREADLOCKED) {
1558 1571 /*
1559 1572 * If reader is waiting for data, then there should be nothing
1560 1573 * on the rcv queue.
1561 1574 */
1562 1575 if (so->so_rcv_wakeup)
1563 1576 goto out;
1564 1577
1565 1578 /* Do a timed sleep, in case the reader goes to sleep. */
1566 1579 (void) cv_reltimedwait(&so->so_read_cv, &so->so_lock, wakeup,
1567 1580 TR_CLOCK_TICK);
1568 1581 }
1569 1582
1570 1583 /*
1571 1584 * Since we are holding so_lock no new reader will come in, and the
1572 1585 * protocol will not be able to enqueue data. So it's safe to walk
1573 1586 * both rcv queues.
1574 1587 */
1575 1588 mp = so->so_rcv_q_head;
1576 1589 if (mp != NULL) {
1577 1590 size = msgdsize(so->so_rcv_q_head);
1578 1591 for (; mp != NULL; mp = mp->b_next)
1579 1592 count++;
1580 1593 } else {
1581 1594 /*
1582 1595 * In case the processing list was empty, get the size of the
1583 1596 * next msg in line.
1584 1597 */
1585 1598 size = msgdsize(so->so_rcv_head);
1586 1599 }
1587 1600
1588 1601 for (mp = so->so_rcv_head; mp != NULL; mp = mp->b_next)
1589 1602 count++;
1590 1603 out:
1591 1604 mutex_exit(&so->so_lock);
1592 1605
1593 1606 /*
1594 1607 * Drop down from size_t to the "int" required by the
1595 1608 * interface. Cap at INT_MAX.
1596 1609 */
1597 1610 retval = MIN(size, INT_MAX);
1598 1611 if (so_copyout(&retval, (void *)arg, sizeof (retval),
1599 1612 (mode & (int)FKIOCTL))) {
1600 1613 return (EFAULT);
1601 1614 } else {
1602 1615 *rvalp = count;
1603 1616 return (0);
1604 1617 }
1605 1618 }
1606 1619
1607 1620 /*
1608 1621 * Process STREAM ioctls.
1609 1622 *
1610 1623 * Returns:
1611 1624 * < 0 - ioctl was not handle
1612 1625 * >= 0 - ioctl was handled, if > 0, then it is an errno
1613 1626 */
1614 1627 int
1615 1628 socket_strioc_common(struct sonode *so, int cmd, intptr_t arg, int mode,
1616 1629 struct cred *cr, int32_t *rvalp)
1617 1630 {
1618 1631 int retval;
1619 1632
1620 1633 /* Only STREAM iotcls are handled here */
1621 1634 if ((cmd & 0xffffff00U) != STR)
1622 1635 return (-1);
1623 1636
1624 1637 switch (cmd) {
1625 1638 case I_CANPUT:
1626 1639 /*
1627 1640 * We return an error for I_CANPUT so that isastream(3C) will
1628 1641 * not report the socket as being a STREAM.
1629 1642 */
1630 1643 return (EOPNOTSUPP);
1631 1644 case I_NREAD:
1632 1645 /* Avoid doing a fallback for I_NREAD. */
1633 1646 return (so_strioc_nread(so, arg, mode, rvalp));
1634 1647 case I_LOOK:
1635 1648 /* Avoid doing a fallback for I_LOOK. */
1636 1649 if (so_copyout("sockmod", (void *)arg, strlen("sockmod") + 1,
1637 1650 (mode & (int)FKIOCTL))) {
1638 1651 return (EFAULT);
1639 1652 }
1640 1653 return (0);
1641 1654 default:
1642 1655 break;
1643 1656 }
1644 1657
1645 1658 /*
1646 1659 * Try to fall back to TPI, and if successful, reissue the ioctl.
1647 1660 */
1648 1661 if ((retval = so_tpi_fallback(so, cr)) == 0) {
1649 1662 /* Reissue the ioctl */
1650 1663 ASSERT(so->so_rcv_q_head == NULL);
1651 1664 return (SOP_IOCTL(so, cmd, arg, mode, cr, rvalp));
1652 1665 } else {
1653 1666 return (retval);
1654 1667 }
1655 1668 }
1656 1669
1657 1670 /*
1658 1671 * This is called for all socket types to verify that the buffer size is large
1659 1672 * enough for the option, and if we can, handle the request as well. Most
1660 1673 * options will be forwarded to the protocol.
1661 1674 */
1662 1675 int
1663 1676 socket_getopt_common(struct sonode *so, int level, int option_name,
1664 1677 void *optval, socklen_t *optlenp, int flags)
1665 1678 {
1666 1679 if (level != SOL_SOCKET)
1667 1680 return (-1);
1668 1681
1669 1682 switch (option_name) {
1670 1683 case SO_ERROR:
1671 1684 case SO_DOMAIN:
1672 1685 case SO_TYPE:
1673 1686 case SO_ACCEPTCONN: {
1674 1687 int32_t value;
1675 1688 socklen_t optlen = *optlenp;
1676 1689
1677 1690 if (optlen < (t_uscalar_t)sizeof (int32_t)) {
1678 1691 return (EINVAL);
1679 1692 }
1680 1693
1681 1694 switch (option_name) {
1682 1695 case SO_ERROR:
1683 1696 mutex_enter(&so->so_lock);
1684 1697 value = sogeterr(so, B_TRUE);
1685 1698 mutex_exit(&so->so_lock);
1686 1699 break;
1687 1700 case SO_DOMAIN:
1688 1701 value = so->so_family;
1689 1702 break;
1690 1703 case SO_TYPE:
1691 1704 value = so->so_type;
1692 1705 break;
1693 1706 case SO_ACCEPTCONN:
1694 1707 if (so->so_state & SS_ACCEPTCONN)
1695 1708 value = SO_ACCEPTCONN;
1696 1709 else
1697 1710 value = 0;
1698 1711 break;
1699 1712 }
1700 1713
1701 1714 bcopy(&value, optval, sizeof (value));
1702 1715 *optlenp = sizeof (value);
1703 1716
1704 1717 return (0);
1705 1718 }
1706 1719 case SO_SNDTIMEO:
1707 1720 case SO_RCVTIMEO: {
1708 1721 clock_t value;
1709 1722 socklen_t optlen = *optlenp;
1710 1723
1711 1724 if (get_udatamodel() == DATAMODEL_NONE ||
1712 1725 get_udatamodel() == DATAMODEL_NATIVE) {
1713 1726 if (optlen < sizeof (struct timeval))
1714 1727 return (EINVAL);
1715 1728 } else {
1716 1729 if (optlen < sizeof (struct timeval32))
1717 1730 return (EINVAL);
1718 1731 }
1719 1732 if (option_name == SO_RCVTIMEO)
1720 1733 value = drv_hztousec(so->so_rcvtimeo);
1721 1734 else
1722 1735 value = drv_hztousec(so->so_sndtimeo);
1723 1736
1724 1737 if (get_udatamodel() == DATAMODEL_NONE ||
1725 1738 get_udatamodel() == DATAMODEL_NATIVE) {
1726 1739 ((struct timeval *)(optval))->tv_sec =
1727 1740 value / (1000 * 1000);
1728 1741 ((struct timeval *)(optval))->tv_usec =
1729 1742 value % (1000 * 1000);
1730 1743 *optlenp = sizeof (struct timeval);
1731 1744 } else {
1732 1745 ((struct timeval32 *)(optval))->tv_sec =
1733 1746 value / (1000 * 1000);
1734 1747 ((struct timeval32 *)(optval))->tv_usec =
1735 1748 value % (1000 * 1000);
1736 1749 *optlenp = sizeof (struct timeval32);
1737 1750 }
1738 1751 return (0);
1739 1752 }
1740 1753 case SO_DEBUG:
1741 1754 case SO_REUSEADDR:
1742 1755 case SO_KEEPALIVE:
1743 1756 case SO_DONTROUTE:
1744 1757 case SO_BROADCAST:
1745 1758 case SO_USELOOPBACK:
1746 1759 case SO_OOBINLINE:
1747 1760 case SO_SNDBUF:
1748 1761 #ifdef notyet
1749 1762 case SO_SNDLOWAT:
1750 1763 case SO_RCVLOWAT:
1751 1764 #endif /* notyet */
1752 1765 case SO_DGRAM_ERRIND: {
1753 1766 socklen_t optlen = *optlenp;
1754 1767
1755 1768 if (optlen < (t_uscalar_t)sizeof (int32_t))
1756 1769 return (EINVAL);
1757 1770 break;
1758 1771 }
1759 1772 case SO_RCVBUF: {
1760 1773 socklen_t optlen = *optlenp;
1761 1774
1762 1775 if (optlen < (t_uscalar_t)sizeof (int32_t))
1763 1776 return (EINVAL);
1764 1777
1765 1778 if ((flags & _SOGETSOCKOPT_XPG4_2) && so->so_xpg_rcvbuf != 0) {
1766 1779 /*
1767 1780 * XXX If SO_RCVBUF has been set and this is an
1768 1781 * XPG 4.2 application then do not ask the transport
1769 1782 * since the transport might adjust the value and not
1770 1783 * return exactly what was set by the application.
1771 1784 * For non-XPG 4.2 application we return the value
1772 1785 * that the transport is actually using.
1773 1786 */
1774 1787 *(int32_t *)optval = so->so_xpg_rcvbuf;
1775 1788 *optlenp = sizeof (so->so_xpg_rcvbuf);
1776 1789 return (0);
1777 1790 }
1778 1791 /*
1779 1792 * If the option has not been set then get a default
1780 1793 * value from the transport.
1781 1794 */
1782 1795 break;
1783 1796 }
1784 1797 case SO_LINGER: {
1785 1798 socklen_t optlen = *optlenp;
1786 1799
1787 1800 if (optlen < (t_uscalar_t)sizeof (struct linger))
1788 1801 return (EINVAL);
1789 1802 break;
1790 1803 }
1791 1804 case SO_SND_BUFINFO: {
1792 1805 socklen_t optlen = *optlenp;
1793 1806
1794 1807 if (optlen < (t_uscalar_t)sizeof (struct so_snd_bufinfo))
1795 1808 return (EINVAL);
1796 1809 ((struct so_snd_bufinfo *)(optval))->sbi_wroff =
1797 1810 (so->so_proto_props).sopp_wroff;
1798 1811 ((struct so_snd_bufinfo *)(optval))->sbi_maxblk =
1799 1812 (so->so_proto_props).sopp_maxblk;
1800 1813 ((struct so_snd_bufinfo *)(optval))->sbi_maxpsz =
1801 1814 (so->so_proto_props).sopp_maxpsz;
1802 1815 ((struct so_snd_bufinfo *)(optval))->sbi_tail =
1803 1816 (so->so_proto_props).sopp_tail;
1804 1817 *optlenp = sizeof (struct so_snd_bufinfo);
1805 1818 return (0);
1806 1819 }
1807 1820 case SO_SND_COPYAVOID: {
1808 1821 sof_instance_t *inst;
1809 1822
1810 1823 /*
1811 1824 * Avoid zero-copy if there is a filter with a data_out
1812 1825 * callback. We could let the operation succeed, but then
1813 1826 * the filter would have to copy the data anyway.
1814 1827 */
1815 1828 for (inst = so->so_filter_top; inst != NULL;
1816 1829 inst = inst->sofi_next) {
1817 1830 if (SOF_INTERESTED(inst, data_out))
1818 1831 return (EOPNOTSUPP);
1819 1832 }
1820 1833 break;
1821 1834 }
1822 1835
1823 1836 default:
1824 1837 break;
1825 1838 }
1826 1839
1827 1840 /* Unknown Option */
1828 1841 return (-1);
1829 1842 }
1830 1843
1831 1844 void
1832 1845 socket_sonode_destroy(struct sonode *so)
1833 1846 {
1834 1847 sonode_fini(so);
1835 1848 kmem_cache_free(socket_cache, so);
1836 1849 }
1837 1850
1838 1851 int
1839 1852 so_zcopy_wait(struct sonode *so)
1840 1853 {
1841 1854 int error = 0;
1842 1855
1843 1856 mutex_enter(&so->so_lock);
1844 1857 while (!(so->so_copyflag & STZCNOTIFY)) {
1845 1858 if (so->so_state & SS_CLOSING) {
1846 1859 mutex_exit(&so->so_lock);
1847 1860 return (EINTR);
1848 1861 }
1849 1862 if (cv_wait_sig(&so->so_copy_cv, &so->so_lock) == 0) {
1850 1863 error = EINTR;
1851 1864 break;
1852 1865 }
1853 1866 }
1854 1867 so->so_copyflag &= ~STZCNOTIFY;
1855 1868 mutex_exit(&so->so_lock);
1856 1869 return (error);
1857 1870 }
1858 1871
1859 1872 void
1860 1873 so_timer_callback(void *arg)
1861 1874 {
1862 1875 struct sonode *so = (struct sonode *)arg;
1863 1876
1864 1877 mutex_enter(&so->so_lock);
1865 1878
1866 1879 so->so_rcv_timer_tid = 0;
1867 1880 if (so->so_rcv_queued > 0) {
1868 1881 so_notify_data(so, so->so_rcv_queued);
1869 1882 } else {
1870 1883 mutex_exit(&so->so_lock);
1871 1884 }
1872 1885 }
1873 1886
1874 1887 #ifdef DEBUG
1875 1888 /*
1876 1889 * Verify that the length stored in so_rcv_queued and the length of data blocks
1877 1890 * queued is same.
1878 1891 */
1879 1892 static boolean_t
1880 1893 so_check_length(sonode_t *so)
1881 1894 {
1882 1895 mblk_t *mp = so->so_rcv_q_head;
1883 1896 int len = 0;
1884 1897
1885 1898 ASSERT(MUTEX_HELD(&so->so_lock));
1886 1899
1887 1900 if (mp != NULL) {
1888 1901 len = msgdsize(mp);
1889 1902 while ((mp = mp->b_next) != NULL)
1890 1903 len += msgdsize(mp);
1891 1904 }
1892 1905 mp = so->so_rcv_head;
1893 1906 if (mp != NULL) {
1894 1907 len += msgdsize(mp);
1895 1908 while ((mp = mp->b_next) != NULL)
1896 1909 len += msgdsize(mp);
1897 1910 }
1898 1911 return ((len == so->so_rcv_queued) ? B_TRUE : B_FALSE);
1899 1912 }
1900 1913 #endif
1901 1914
1902 1915 int
1903 1916 so_get_mod_version(struct sockparams *sp)
1904 1917 {
1905 1918 ASSERT(sp != NULL && sp->sp_smod_info != NULL);
1906 1919 return (sp->sp_smod_info->smod_version);
1907 1920 }
1908 1921
1909 1922 /*
1910 1923 * so_start_fallback()
1911 1924 *
1912 1925 * Block new socket operations from coming in, and wait for active operations
1913 1926 * to complete. Threads that are sleeping will be woken up so they can get
1914 1927 * out of the way.
1915 1928 *
1916 1929 * The caller must be a reader on so_fallback_rwlock.
1917 1930 */
1918 1931 static boolean_t
1919 1932 so_start_fallback(struct sonode *so)
1920 1933 {
1921 1934 ASSERT(RW_READ_HELD(&so->so_fallback_rwlock));
1922 1935
1923 1936 mutex_enter(&so->so_lock);
1924 1937 if (so->so_state & SS_FALLBACK_PENDING) {
1925 1938 mutex_exit(&so->so_lock);
1926 1939 return (B_FALSE);
1927 1940 }
1928 1941 so->so_state |= SS_FALLBACK_PENDING;
1929 1942 /*
1930 1943 * Poke all threads that might be sleeping. Any operation that comes
1931 1944 * in after the cv_broadcast will observe the fallback pending flag
1932 1945 * which cause the call to return where it would normally sleep.
1933 1946 */
1934 1947 cv_broadcast(&so->so_state_cv); /* threads in connect() */
1935 1948 cv_broadcast(&so->so_rcv_cv); /* threads in recvmsg() */
1936 1949 cv_broadcast(&so->so_snd_cv); /* threads in sendmsg() */
1937 1950 mutex_enter(&so->so_acceptq_lock);
1938 1951 cv_broadcast(&so->so_acceptq_cv); /* threads in accept() */
1939 1952 mutex_exit(&so->so_acceptq_lock);
1940 1953 mutex_exit(&so->so_lock);
1941 1954
1942 1955 /*
1943 1956 * The main reason for the rw_tryupgrade call is to provide
1944 1957 * observability during the fallback process. We want to
1945 1958 * be able to see if there are pending operations.
1946 1959 */
1947 1960 if (rw_tryupgrade(&so->so_fallback_rwlock) == 0) {
1948 1961 /*
1949 1962 * It is safe to drop and reaquire the fallback lock, because
1950 1963 * we are guaranteed that another fallback cannot take place.
1951 1964 */
1952 1965 rw_exit(&so->so_fallback_rwlock);
1953 1966 DTRACE_PROBE1(pending__ops__wait, (struct sonode *), so);
1954 1967 rw_enter(&so->so_fallback_rwlock, RW_WRITER);
1955 1968 DTRACE_PROBE1(pending__ops__complete, (struct sonode *), so);
1956 1969 }
1957 1970
1958 1971 return (B_TRUE);
1959 1972 }
1960 1973
1961 1974 /*
1962 1975 * so_end_fallback()
1963 1976 *
1964 1977 * Allow socket opertions back in.
1965 1978 *
1966 1979 * The caller must be a writer on so_fallback_rwlock.
1967 1980 */
1968 1981 static void
1969 1982 so_end_fallback(struct sonode *so)
1970 1983 {
1971 1984 ASSERT(RW_ISWRITER(&so->so_fallback_rwlock));
1972 1985
1973 1986 mutex_enter(&so->so_lock);
1974 1987 so->so_state &= ~(SS_FALLBACK_PENDING|SS_FALLBACK_DRAIN);
1975 1988 mutex_exit(&so->so_lock);
1976 1989
1977 1990 rw_downgrade(&so->so_fallback_rwlock);
1978 1991 }
1979 1992
1980 1993 /*
1981 1994 * so_quiesced_cb()
1982 1995 *
1983 1996 * Callback passed to the protocol during fallback. It is called once
1984 1997 * the endpoint is quiescent.
1985 1998 *
1986 1999 * No requests from the user, no notifications from the protocol, so it
1987 2000 * is safe to synchronize the state. Data can also be moved without
1988 2001 * risk for reordering.
1989 2002 *
1990 2003 * We do not need to hold so_lock, since there can be only one thread
1991 2004 * operating on the sonode.
1992 2005 */
1993 2006 static mblk_t *
1994 2007 so_quiesced_cb(sock_upper_handle_t sock_handle, sock_quiesce_arg_t *arg,
1995 2008 struct T_capability_ack *tcap,
1996 2009 struct sockaddr *laddr, socklen_t laddrlen,
1997 2010 struct sockaddr *faddr, socklen_t faddrlen, short opts)
1998 2011 {
1999 2012 struct sonode *so = (struct sonode *)sock_handle;
2000 2013 boolean_t atmark;
2001 2014 mblk_t *retmp = NULL, **tailmpp = &retmp;
2002 2015
2003 2016 if (tcap != NULL)
2004 2017 sotpi_update_state(so, tcap, laddr, laddrlen, faddr, faddrlen,
2005 2018 opts);
2006 2019
2007 2020 /*
2008 2021 * Some protocols do not quiece the data path during fallback. Once
2009 2022 * we set the SS_FALLBACK_DRAIN flag any attempt to queue data will
2010 2023 * fail and the protocol is responsible for saving the data for later
2011 2024 * delivery (i.e., once the fallback has completed).
2012 2025 */
2013 2026 mutex_enter(&so->so_lock);
2014 2027 so->so_state |= SS_FALLBACK_DRAIN;
2015 2028 SOCKET_TIMER_CANCEL(so);
2016 2029 mutex_exit(&so->so_lock);
2017 2030
2018 2031 if (so->so_rcv_head != NULL) {
2019 2032 if (so->so_rcv_q_last_head == NULL)
2020 2033 so->so_rcv_q_head = so->so_rcv_head;
2021 2034 else
2022 2035 so->so_rcv_q_last_head->b_next = so->so_rcv_head;
2023 2036 so->so_rcv_q_last_head = so->so_rcv_last_head;
2024 2037 }
2025 2038
2026 2039 atmark = (so->so_state & SS_RCVATMARK) != 0;
2027 2040 /*
2028 2041 * Clear any OOB state having to do with pending data. The TPI
2029 2042 * code path will set the appropriate oob state when we move the
2030 2043 * oob data to the STREAM head. We leave SS_HADOOBDATA since the oob
2031 2044 * data has already been consumed.
2032 2045 */
2033 2046 so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA);
2034 2047
2035 2048 ASSERT(so->so_oobmsg != NULL || so->so_oobmark <= so->so_rcv_queued);
2036 2049
2037 2050 /*
2038 2051 * Move data to the STREAM head.
2039 2052 */
2040 2053 while (so->so_rcv_q_head != NULL) {
2041 2054 mblk_t *mp = so->so_rcv_q_head;
2042 2055 size_t mlen = msgdsize(mp);
2043 2056
2044 2057 so->so_rcv_q_head = mp->b_next;
2045 2058 mp->b_next = NULL;
2046 2059 mp->b_prev = NULL;
2047 2060
2048 2061 /*
2049 2062 * Send T_EXDATA_IND if we are at the oob mark.
2050 2063 */
2051 2064 if (atmark) {
2052 2065 struct T_exdata_ind *tei;
2053 2066 mblk_t *mp1 = arg->soqa_exdata_mp;
2054 2067
2055 2068 arg->soqa_exdata_mp = NULL;
2056 2069 ASSERT(mp1 != NULL);
2057 2070 mp1->b_datap->db_type = M_PROTO;
2058 2071 tei = (struct T_exdata_ind *)mp1->b_rptr;
2059 2072 tei->PRIM_type = T_EXDATA_IND;
2060 2073 tei->MORE_flag = 0;
2061 2074 mp1->b_wptr = (uchar_t *)&tei[1];
2062 2075
2063 2076 if (IS_SO_OOB_INLINE(so)) {
2064 2077 mp1->b_cont = mp;
2065 2078 } else {
2066 2079 ASSERT(so->so_oobmsg != NULL);
2067 2080 mp1->b_cont = so->so_oobmsg;
2068 2081 so->so_oobmsg = NULL;
2069 2082
2070 2083 /* process current mp next time around */
2071 2084 mp->b_next = so->so_rcv_q_head;
2072 2085 so->so_rcv_q_head = mp;
2073 2086 mlen = 0;
2074 2087 }
2075 2088 mp = mp1;
2076 2089
2077 2090 /* we have consumed the oob mark */
2078 2091 atmark = B_FALSE;
2079 2092 } else if (so->so_oobmark > 0) {
2080 2093 /*
2081 2094 * Check if the OOB mark is within the current
2082 2095 * mblk chain. In that case we have to split it up.
2083 2096 */
2084 2097 if (so->so_oobmark < mlen) {
2085 2098 mblk_t *urg_mp = mp;
2086 2099
2087 2100 atmark = B_TRUE;
2088 2101 mp = NULL;
2089 2102 mlen = so->so_oobmark;
2090 2103
2091 2104 /*
2092 2105 * It is assumed that the OOB mark does
2093 2106 * not land within a mblk.
2094 2107 */
2095 2108 do {
2096 2109 so->so_oobmark -= MBLKL(urg_mp);
2097 2110 mp = urg_mp;
2098 2111 urg_mp = urg_mp->b_cont;
2099 2112 } while (so->so_oobmark > 0);
2100 2113 mp->b_cont = NULL;
2101 2114 if (urg_mp != NULL) {
2102 2115 urg_mp->b_next = so->so_rcv_q_head;
2103 2116 so->so_rcv_q_head = urg_mp;
2104 2117 }
2105 2118 } else {
2106 2119 so->so_oobmark -= mlen;
2107 2120 if (so->so_oobmark == 0)
2108 2121 atmark = B_TRUE;
2109 2122 }
2110 2123 }
2111 2124
2112 2125 /*
2113 2126 * Queue data on the STREAM head.
2114 2127 */
2115 2128 so->so_rcv_queued -= mlen;
2116 2129 *tailmpp = mp;
2117 2130 tailmpp = &mp->b_next;
2118 2131 }
2119 2132 so->so_rcv_head = NULL;
2120 2133 so->so_rcv_last_head = NULL;
2121 2134 so->so_rcv_q_head = NULL;
2122 2135 so->so_rcv_q_last_head = NULL;
2123 2136
2124 2137 /*
2125 2138 * Check if the oob byte is at the end of the data stream, or if the
2126 2139 * oob byte has not yet arrived. In the latter case we have to send a
2127 2140 * SIGURG and a mark indicator to the STREAM head. The mark indicator
2128 2141 * is needed to guarantee correct behavior for SIOCATMARK. See block
2129 2142 * comment in socktpi.h for more details.
2130 2143 */
2131 2144 if (atmark || so->so_oobmark > 0) {
2132 2145 mblk_t *mp;
2133 2146
2134 2147 if (atmark && so->so_oobmsg != NULL) {
2135 2148 struct T_exdata_ind *tei;
2136 2149
2137 2150 mp = arg->soqa_exdata_mp;
2138 2151 arg->soqa_exdata_mp = NULL;
2139 2152 ASSERT(mp != NULL);
2140 2153 mp->b_datap->db_type = M_PROTO;
2141 2154 tei = (struct T_exdata_ind *)mp->b_rptr;
2142 2155 tei->PRIM_type = T_EXDATA_IND;
2143 2156 tei->MORE_flag = 0;
2144 2157 mp->b_wptr = (uchar_t *)&tei[1];
2145 2158
2146 2159 mp->b_cont = so->so_oobmsg;
2147 2160 so->so_oobmsg = NULL;
2148 2161
2149 2162 *tailmpp = mp;
2150 2163 tailmpp = &mp->b_next;
2151 2164 } else {
2152 2165 /* Send up the signal */
2153 2166 mp = arg->soqa_exdata_mp;
2154 2167 arg->soqa_exdata_mp = NULL;
2155 2168 ASSERT(mp != NULL);
2156 2169 DB_TYPE(mp) = M_PCSIG;
2157 2170 *mp->b_wptr++ = (uchar_t)SIGURG;
2158 2171 *tailmpp = mp;
2159 2172 tailmpp = &mp->b_next;
2160 2173
2161 2174 /* Send up the mark indicator */
2162 2175 mp = arg->soqa_urgmark_mp;
2163 2176 arg->soqa_urgmark_mp = NULL;
2164 2177 mp->b_flag = atmark ? MSGMARKNEXT : MSGNOTMARKNEXT;
2165 2178 *tailmpp = mp;
2166 2179 tailmpp = &mp->b_next;
2167 2180
2168 2181 so->so_oobmark = 0;
2169 2182 }
2170 2183 }
2171 2184 ASSERT(so->so_oobmark == 0);
2172 2185 ASSERT(so->so_rcv_queued == 0);
2173 2186
2174 2187 return (retmp);
2175 2188 }
2176 2189
2177 2190 #ifdef DEBUG
2178 2191 /*
2179 2192 * Do an integrity check of the sonode. This should be done if a
2180 2193 * fallback fails after sonode has initially been converted to use
2181 2194 * TPI and subsequently have to be reverted.
2182 2195 *
2183 2196 * Failure to pass the integrity check will panic the system.
2184 2197 */
2185 2198 void
2186 2199 so_integrity_check(struct sonode *cur, struct sonode *orig)
2187 2200 {
2188 2201 VERIFY(cur->so_vnode == orig->so_vnode);
2189 2202 VERIFY(cur->so_ops == orig->so_ops);
2190 2203 /*
2191 2204 * For so_state we can only VERIFY the state flags in CHECK_STATE.
2192 2205 * The other state flags might be affected by a notification from the
2193 2206 * protocol.
2194 2207 */
2195 2208 #define CHECK_STATE (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_NDELAY|SS_NONBLOCK| \
2196 2209 SS_ASYNC|SS_ACCEPTCONN|SS_SAVEDEOR|SS_RCVATMARK|SS_OOBPEND| \
2197 2210 SS_HAVEOOBDATA|SS_HADOOBDATA|SS_SENTLASTREADSIG|SS_SENTLASTWRITESIG)
2198 2211 VERIFY((cur->so_state & (orig->so_state & CHECK_STATE)) ==
2199 2212 (orig->so_state & CHECK_STATE));
2200 2213 VERIFY(cur->so_mode == orig->so_mode);
2201 2214 VERIFY(cur->so_flag == orig->so_flag);
2202 2215 VERIFY(cur->so_count == orig->so_count);
2203 2216 /* Cannot VERIFY so_proto_connid; proto can update it */
2204 2217 VERIFY(cur->so_sockparams == orig->so_sockparams);
2205 2218 /* an error might have been recorded, but it can not be lost */
2206 2219 VERIFY(cur->so_error != 0 || orig->so_error == 0);
2207 2220 VERIFY(cur->so_family == orig->so_family);
2208 2221 VERIFY(cur->so_type == orig->so_type);
2209 2222 VERIFY(cur->so_protocol == orig->so_protocol);
2210 2223 VERIFY(cur->so_version == orig->so_version);
2211 2224 /* New conns might have arrived, but none should have been lost */
2212 2225 VERIFY(cur->so_acceptq_len >= orig->so_acceptq_len);
2213 2226 VERIFY(list_head(&cur->so_acceptq_list) ==
2214 2227 list_head(&orig->so_acceptq_list));
2215 2228 VERIFY(cur->so_backlog == orig->so_backlog);
2216 2229 /* New OOB migth have arrived, but mark should not have been lost */
2217 2230 VERIFY(cur->so_oobmark >= orig->so_oobmark);
2218 2231 /* Cannot VERIFY so_oobmsg; the proto might have sent up a new one */
2219 2232 VERIFY(cur->so_pgrp == orig->so_pgrp);
2220 2233 VERIFY(cur->so_peercred == orig->so_peercred);
2221 2234 VERIFY(cur->so_cpid == orig->so_cpid);
2222 2235 VERIFY(cur->so_zoneid == orig->so_zoneid);
2223 2236 /* New data migth have arrived, but none should have been lost */
2224 2237 VERIFY(cur->so_rcv_queued >= orig->so_rcv_queued);
2225 2238 VERIFY(cur->so_rcv_q_head == orig->so_rcv_q_head);
2226 2239 VERIFY(cur->so_rcv_head == orig->so_rcv_head);
2227 2240 VERIFY(cur->so_proto_handle == orig->so_proto_handle);
2228 2241 VERIFY(cur->so_downcalls == orig->so_downcalls);
2229 2242 /* Cannot VERIFY so_proto_props; they can be updated by proto */
2230 2243 }
2231 2244 #endif
2232 2245
2233 2246 /*
2234 2247 * so_tpi_fallback()
2235 2248 *
2236 2249 * This is the fallback initation routine; things start here.
2237 2250 *
2238 2251 * Basic strategy:
2239 2252 * o Block new socket operations from coming in
2240 2253 * o Allocate/initate info needed by TPI
2241 2254 * o Quiesce the connection, at which point we sync
2242 2255 * state and move data
2243 2256 * o Change operations (sonodeops) associated with the socket
2244 2257 * o Unblock threads waiting for the fallback to finish
2245 2258 */
2246 2259 int
2247 2260 so_tpi_fallback(struct sonode *so, struct cred *cr)
2248 2261 {
2249 2262 int error;
2250 2263 queue_t *q;
2251 2264 struct sockparams *sp;
2252 2265 struct sockparams *newsp = NULL;
2253 2266 so_proto_fallback_func_t fbfunc;
2254 2267 const char *devpath;
2255 2268 boolean_t direct;
2256 2269 struct sonode *nso;
2257 2270 sock_quiesce_arg_t arg = { NULL, NULL };
2258 2271 #ifdef DEBUG
2259 2272 struct sonode origso;
2260 2273 #endif
2261 2274 error = 0;
2262 2275 sp = so->so_sockparams;
2263 2276 fbfunc = sp->sp_smod_info->smod_proto_fallback_func;
2264 2277
2265 2278 /*
2266 2279 * Cannot fallback if the socket has active filters
2267 2280 */
2268 2281 if (so->so_filter_active > 0)
2269 2282 return (EINVAL);
2270 2283
2271 2284 switch (so->so_family) {
2272 2285 case AF_INET:
2273 2286 devpath = sp->sp_smod_info->smod_fallback_devpath_v4;
2274 2287 break;
2275 2288 case AF_INET6:
2276 2289 devpath = sp->sp_smod_info->smod_fallback_devpath_v6;
2277 2290 break;
2278 2291 default:
2279 2292 return (EINVAL);
2280 2293 }
2281 2294
2282 2295 /*
2283 2296 * Fallback can only happen if the socket module has a TPI device
2284 2297 * and fallback function.
2285 2298 */
2286 2299 if (devpath == NULL || fbfunc == NULL)
2287 2300 return (EINVAL);
2288 2301
2289 2302 /*
2290 2303 * Initiate fallback; upon success we know that no new requests
2291 2304 * will come in from the user.
2292 2305 */
2293 2306 if (!so_start_fallback(so))
2294 2307 return (EAGAIN);
2295 2308 #ifdef DEBUG
2296 2309 /*
2297 2310 * Make a copy of the sonode in case we need to make an integrity
2298 2311 * check later on.
2299 2312 */
2300 2313 bcopy(so, &origso, sizeof (*so));
2301 2314 #endif
2302 2315
2303 2316 sp->sp_stats.sps_nfallback.value.ui64++;
2304 2317
2305 2318 newsp = sockparams_hold_ephemeral_bydev(so->so_family, so->so_type,
2306 2319 so->so_protocol, devpath, KM_SLEEP, &error);
2307 2320 if (error != 0)
2308 2321 goto out;
2309 2322
2310 2323 if (so->so_direct != NULL) {
2311 2324 sodirect_t *sodp = so->so_direct;
2312 2325 mutex_enter(&so->so_lock);
2313 2326
2314 2327 so->so_direct->sod_enabled = B_FALSE;
2315 2328 so->so_state &= ~SS_SODIRECT;
2316 2329 ASSERT(sodp->sod_uioafh == NULL);
2317 2330 mutex_exit(&so->so_lock);
2318 2331 }
2319 2332
2320 2333 /* Turn sonode into a TPI socket */
2321 2334 error = sotpi_convert_sonode(so, newsp, &direct, &q, cr);
2322 2335 if (error != 0)
2323 2336 goto out;
2324 2337 /*
2325 2338 * When it comes to urgent data we have two cases to deal with;
2326 2339 * (1) The oob byte has already arrived, or (2) the protocol has
2327 2340 * notified that oob data is pending, but it has not yet arrived.
2328 2341 *
2329 2342 * For (1) all we need to do is send a T_EXDATA_IND to indicate were
2330 2343 * in the byte stream the oob byte is. For (2) we have to send a
2331 2344 * SIGURG (M_PCSIG), followed by a zero-length mblk indicating whether
2332 2345 * the oob byte will be the next byte from the protocol.
2333 2346 *
2334 2347 * So in the worst case we need two mblks, one for the signal, another
2335 2348 * for mark indication. In that case we use the exdata_mp for the sig.
2336 2349 */
2337 2350 arg.soqa_exdata_mp = allocb_wait(sizeof (struct T_exdata_ind),
2338 2351 BPRI_MED, STR_NOSIG, NULL);
2339 2352 arg.soqa_urgmark_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
2340 2353
2341 2354 /*
2342 2355 * Now tell the protocol to start using TPI. so_quiesced_cb be
2343 2356 * called once it's safe to synchronize state.
2344 2357 */
2345 2358 DTRACE_PROBE1(proto__fallback__begin, struct sonode *, so);
2346 2359 error = (*fbfunc)(so->so_proto_handle, q, direct, so_quiesced_cb,
2347 2360 &arg);
2348 2361 DTRACE_PROBE1(proto__fallback__end, struct sonode *, so);
2349 2362
2350 2363 if (error != 0) {
2351 2364 /* protocol was unable to do a fallback, revert the sonode */
2352 2365 sotpi_revert_sonode(so, cr);
2353 2366 goto out;
2354 2367 }
2355 2368
2356 2369 /*
2357 2370 * Walk the accept queue and notify the proto that they should
2358 2371 * fall back to TPI. The protocol will send up the T_CONN_IND.
2359 2372 */
2360 2373 nso = list_head(&so->so_acceptq_list);
2361 2374 while (nso != NULL) {
2362 2375 int rval;
2363 2376 struct sonode *next;
2364 2377
2365 2378 if (arg.soqa_exdata_mp == NULL) {
2366 2379 arg.soqa_exdata_mp =
2367 2380 allocb_wait(sizeof (struct T_exdata_ind),
2368 2381 BPRI_MED, STR_NOSIG, NULL);
2369 2382 }
2370 2383 if (arg.soqa_urgmark_mp == NULL) {
2371 2384 arg.soqa_urgmark_mp = allocb_wait(0, BPRI_MED,
2372 2385 STR_NOSIG, NULL);
2373 2386 }
2374 2387
2375 2388 DTRACE_PROBE1(proto__fallback__begin, struct sonode *, nso);
2376 2389 rval = (*fbfunc)(nso->so_proto_handle, NULL, direct,
2377 2390 so_quiesced_cb, &arg);
2378 2391 DTRACE_PROBE1(proto__fallback__end, struct sonode *, nso);
2379 2392 if (rval != 0) {
2380 2393 /* Abort the connection */
2381 2394 zcmn_err(getzoneid(), CE_WARN,
2382 2395 "Failed to convert socket in accept queue to TPI. "
2383 2396 "Pid = %d\n", curproc->p_pid);
2384 2397 next = list_next(&so->so_acceptq_list, nso);
2385 2398 list_remove(&so->so_acceptq_list, nso);
2386 2399 so->so_acceptq_len--;
2387 2400
2388 2401 (void) socket_close(nso, 0, CRED());
2389 2402 socket_destroy(nso);
2390 2403 nso = next;
2391 2404 } else {
2392 2405 nso = list_next(&so->so_acceptq_list, nso);
2393 2406 }
2394 2407 }
2395 2408
2396 2409 /*
2397 2410 * Now flush the acceptq, this will destroy all sockets. They will
2398 2411 * be recreated in sotpi_accept().
2399 2412 */
2400 2413 so_acceptq_flush(so, B_FALSE);
2401 2414
2402 2415 mutex_enter(&so->so_lock);
2403 2416 so->so_state |= SS_FALLBACK_COMP;
2404 2417 mutex_exit(&so->so_lock);
2405 2418
2406 2419 /*
2407 2420 * Swap the sonode ops. Socket opertations that come in once this
2408 2421 * is done will proceed without blocking.
2409 2422 */
2410 2423 so->so_ops = &sotpi_sonodeops;
2411 2424
2412 2425 /*
2413 2426 * Wake up any threads stuck in poll. This is needed since the poll
2414 2427 * head changes when the fallback happens (moves from the sonode to
2415 2428 * the STREAMS head).
2416 2429 */
2417 2430 pollwakeup(&so->so_poll_list, POLLERR);
2418 2431
2419 2432 /*
2420 2433 * When this non-STREAM socket was created we placed an extra ref on
2421 2434 * the associated vnode to support asynchronous close. Drop that ref
2422 2435 * here.
2423 2436 */
2424 2437 ASSERT(SOTOV(so)->v_count >= 2);
2425 2438 VN_RELE(SOTOV(so));
2426 2439 out:
2427 2440 so_end_fallback(so);
2428 2441
2429 2442 if (error != 0) {
2430 2443 #ifdef DEBUG
2431 2444 so_integrity_check(so, &origso);
2432 2445 #endif
2433 2446 zcmn_err(getzoneid(), CE_WARN,
2434 2447 "Failed to convert socket to TPI (err=%d). Pid = %d\n",
2435 2448 error, curproc->p_pid);
2436 2449 if (newsp != NULL)
2437 2450 SOCKPARAMS_DEC_REF(newsp);
2438 2451 }
2439 2452 if (arg.soqa_exdata_mp != NULL)
2440 2453 freemsg(arg.soqa_exdata_mp);
2441 2454 if (arg.soqa_urgmark_mp != NULL)
2442 2455 freemsg(arg.soqa_urgmark_mp);
2443 2456
2444 2457 return (error);
2445 2458 }
|
↓ open down ↓ |
2019 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX