summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/pckt.c
blob: 410bd31e3bd3e502acac086c93ec3a9e87990439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/


/*
 * Description: The pckt module packetizes messages on
 *		its read queue by pre-fixing an M_PROTO
 *		message type to certain incoming messages.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/debug.h>

/*
 * This is the loadable module wrapper.
 */
#include <sys/conf.h>
#include <sys/modctl.h>

static struct streamtab pcktinfo;

/*
 * Per queue instances are single-threaded since the q_ptr
 * field of queues need to be shared among threads.
 */
static struct fmodsw fsw = {
	"pckt",
	&pcktinfo,
	D_NEW | D_MTPERQ | D_MP
};

/*
 * Module linkage information for the kernel.
 */

static struct modlstrmod modlstrmod = {
	&mod_strmodops,
	"pckt module",
	&fsw
};

static struct modlinkage modlinkage = {
	MODREV_1, &modlstrmod, NULL
};


int
_init(void)
{
	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	return (mod_remove(&modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static int	pcktopen(queue_t *, dev_t *, int, int, cred_t *);
static int	pcktclose(queue_t *, int, cred_t *);
static int	pcktrput(queue_t *, mblk_t *);
static int	pcktrsrv(queue_t *);
static int	pcktwput(queue_t *, mblk_t *);
static mblk_t	*add_ctl_info(queue_t *, mblk_t *);
static void	add_ctl_wkup(void *);


/*
 * Stream module data structure definitions.
 * Sits over the ptm module generally.
 *
 * Read side flow control strategy: Since we may be putting messages on
 * the read q due to allocb failures, these failures must get
 * reflected fairly quickly to the module below us.
 * No sense in piling on messages in times of memory shortage.
 * Further, for the case of upper level flow control, there is no
 * compelling reason to have more buffering in this module.
 * Thus use a hi-water mark of one.
 * This module imposes no max packet size, there is no inherent reason
 * in the code to do so.
 */
static struct module_info pcktiinfo = {
	0x9898,					/* module id number */
	"pckt",					/* module name */
	0,					/* minimum packet size */
	INFPSZ,					/* maximum packet size */
	1,					/* hi-water mark */
	0					/* lo-water mark */
};

/*
 * Write side flow control strategy: There is no write service procedure.
 * The write put function is pass thru, thus there is no reason to have any
 * limits on the maximum packet size.
 */
static struct module_info pcktoinfo = {
	0x9898,					/* module id number */
	"pckt",					/* module name */
	0,					/* minimum packet size */
	INFPSZ,					/* maximum packet size */
	0,					/* hi-water mark */
	0					/* lo-water mark */
};

static struct qinit pcktrinit = {
	pcktrput,
	pcktrsrv,
	pcktopen,
	pcktclose,
	NULL,
	&pcktiinfo,
	NULL
};

static struct qinit pcktwinit = {
	pcktwput,
	NULL,
	NULL,
	NULL,
	NULL,
	&pcktoinfo,
	NULL
};

static struct streamtab pcktinfo = {
	&pcktrinit,
	&pcktwinit,
	NULL,
	NULL
};


/*
 * Per-instance state struct for the pckt module.
 */
struct pckt_info {
	queue_t		*pi_qptr;		/* back pointer to q */
	bufcall_id_t	pi_bufcall_id;
#ifdef _MULTI_DATAMODEL
	model_t		model;
#endif /* _MULTI_DATAMODEL */
};

/*
 * Dummy qbufcall callback routine used by open and close.
 * The framework will wake up qwait_sig when we return from
 * this routine (as part of leaving the perimeters.)
 * (The framework enters the perimeters before calling the qbufcall() callback
 * and leaves the perimeters after the callback routine has executed. The
 * framework performs an implicit wakeup of any thread in qwait/qwait_sig
 * when it leaves the perimeter. See qwait(9E).)
 */
/* ARGSUSED */
static void
dummy_callback(void *arg)
{}

/*
 * pcktopen - open routine gets called when the
 *	    module gets pushed onto the stream.
 */
/*ARGSUSED*/
static int
pcktopen(
	queue_t *q,		/* pointer to the read side queue */
	dev_t   *devp,		/* pointer to stream tail's dev */
	int	oflag,		/* the user open(2) supplied flags */
	int	sflag,		/* open state flag */
	cred_t  *credp)		/* credentials */
{
	struct pckt_info	*pip;
	mblk_t			*mop; /* ptr to a setopts msg block */
	struct stroptions	*sop;

	if (sflag != MODOPEN)
		return (EINVAL);

	if (q->q_ptr != NULL) {
		/* It's already attached. */
		return (0);
	}

	/*
	 * Allocate state structure.
	 */
	pip = kmem_zalloc(sizeof (*pip), KM_SLEEP);

#ifdef _MULTI_DATAMODEL
	pip->model = ddi_model_convert_from(get_udatamodel());
#endif /* _MULTI_DATAMODEL */

	/*
	 * Cross-link.
	 */
	pip->pi_qptr = q;
	q->q_ptr = pip;
	WR(q)->q_ptr = pip;

	qprocson(q);

	/*
	 * Initialize an M_SETOPTS message to set up hi/lo water marks on
	 * stream head read queue.
	 */

	while ((mop = allocb(sizeof (struct stroptions), BPRI_MED)) == NULL) {
		bufcall_id_t id = qbufcall(q, sizeof (struct stroptions),
		    BPRI_MED, dummy_callback, NULL);
		if (!qwait_sig(q)) {
			qunbufcall(q, id);
			kmem_free(pip, sizeof (*pip));
			qprocsoff(q);
			return (EINTR);
		}
		qunbufcall(q, id);
	}


	/*
	 * XXX: Should this module really control the hi/low water marks?
	 * Is there any reason in this code to do so?
	 */
	mop->b_datap->db_type = M_SETOPTS;
	mop->b_wptr += sizeof (struct stroptions);
	sop = (struct stroptions *)mop->b_rptr;
	sop->so_flags = SO_HIWAT | SO_LOWAT;
	sop->so_hiwat = 512;
	sop->so_lowat = 256;

	/*
	 * Commit to the open and send the M_SETOPTS off to the stream head.
	 */
	putnext(q, mop);

	return (0);
}


/*
 * pcktclose - This routine gets called when the module
 *	gets popped off of the stream.
 */

/*ARGSUSED*/
static int
pcktclose(
	queue_t *q,	/* Pointer to the read queue */
	int	flag,
	cred_t  *credp)
{
	struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;

	qprocsoff(q);
	/*
	 * Cancel outstanding qbufcall
	 */
	if (pip->pi_bufcall_id) {
		qunbufcall(q, pip->pi_bufcall_id);
		pip->pi_bufcall_id = 0;
	}
	/*
	 * Do not worry about msgs queued on the q, the framework
	 * will free them up.
	 */
	kmem_free(q->q_ptr, sizeof (struct pckt_info));
	q->q_ptr = WR(q)->q_ptr = NULL;
	return (0);
}

/*
 * pcktrput - Module read queue put procedure.
 *	This is called from the module or
 *	driver downstream.
 */
static int
pcktrput(
	queue_t *q,	/* Pointer to the read queue */
	mblk_t *mp)	/* Pointer to the current message block */
{
	mblk_t		*pckt_msgp;


	switch (mp->b_datap->db_type) {
	case M_FLUSH:
		/*
		 * The PTS driver swaps the FLUSHR and FLUSHW flags
		 * we need to swap them back to reflect the actual
		 * slave side FLUSH mode.
		 */
		if ((*mp->b_rptr & FLUSHRW) != FLUSHRW)
			if ((*mp->b_rptr & FLUSHRW) == FLUSHR)
				*mp->b_rptr = FLUSHW;
			else if ((*mp->b_rptr & FLUSHRW) == FLUSHW)
				*mp->b_rptr = FLUSHR;

		pckt_msgp = copymsg(mp);
		if (*mp->b_rptr & FLUSHW) {
			/*
			 * In the packet model we are not allowing
			 * flushes of the master's stream head read
			 * side queue. This is because all packet
			 * state information is stored there and
			 * a flush could destroy this data before
			 * it is read.
			 */
			*mp->b_rptr = FLUSHW;
			putnext(q, mp);
		} else {
			/*
			 * Free messages that only flush the
			 * master's read queue.
			 */
			freemsg(mp);
		}

		if (pckt_msgp == NULL)
			break;

		mp = pckt_msgp;
		/*
		 * Prefix M_PROTO and putnext.
		 */
		goto prefix_head;

	case M_DATA:
	case M_IOCTL:
	case M_PROTO:
		/*
		 * For non-priority messages, follow flow-control rules.
		 * Also, if there are messages on the q already, keep
		 * queueing them since they need to be processed in order.
		 */
		if (!canputnext(q) || (qsize(q) > 0)) {
			(void) putq(q, mp);
			break;
		}
		/* FALLTHROUGH */

	/*
	 * For high priority messages, skip flow control checks.
	 */
	case M_PCPROTO:
	case M_READ:
	case M_STOP:
	case M_START:
	case M_STARTI:
	case M_STOPI:
prefix_head:
		/*
		 * Prefix an M_PROTO header to message and pass upstream.
		 */
		if ((mp = add_ctl_info(q, mp)) != NULL)
			putnext(q, mp);
		break;

	default:
		/*
		 * For data messages, queue them back on the queue if
		 * there are messages on the queue already. This is
		 * done to preserve the order of messages.
		 * For high priority messages or for no messages on the
		 * q, simply putnext() and pass it on.
		 */
		if ((datamsg(mp->b_datap->db_type)) && (qsize(q) > 0))
			(void) putq(q, mp);
		else
			putnext(q, mp);
		break;
	}
	return (0);
}

/*
 * pcktrsrv - module read service procedure
 * This function deals with messages left in the queue due to
 *	(a) not enough memory to allocate the header M_PROTO message
 *	(b) flow control reasons
 * The function will attempt to get the messages off the queue and
 * process them.
 */
static int
pcktrsrv(queue_t *q)
{
	mblk_t *mp;

	while ((mp = getq(q)) != NULL) {
		if (!canputnext(q)) {
			/*
			 * For high priority messages, make sure there is no
			 * infinite loop. Disable the queue for this case.
			 * High priority messages get here only for buffer
			 * allocation failures. Thus the bufcall callout
			 * will reenable the q.
			 * XXX bug alert - nooenable will *not* prevent
			 * putbq of a hipri messages frm enabling the queue.
			 */
			if (!datamsg(mp->b_datap->db_type))
				noenable(q);
			(void) putbq(q, mp);
			return (0);
		}

		/*
		 * M_FLUSH msgs may also be here if there was a memory
		 * failure.
		 */
		switch (mp->b_datap->db_type) {
		case M_FLUSH:
		case M_PROTO:
		case M_PCPROTO:
		case M_STOP:
		case M_START:
		case M_IOCTL:
		case M_DATA:
		case M_READ:
		case M_STARTI:
		case M_STOPI:
			/*
			 * Prefix an M_PROTO header to msg and pass upstream.
			 */
			if ((mp = add_ctl_info(q, mp)) == NULL) {
				/*
				 * Running into memory or flow ctl problems.
				 */
				return (0);
			}
			/* FALL THROUGH */

		default:
			putnext(q, mp);
			break;
		}
	}
	return (0);
}

/*
 * pcktwput - Module write queue put procedure.
 *	All messages are send downstream unchanged
 */

static int
pcktwput(
	queue_t *q,	/* Pointer to the read queue */
	mblk_t *mp)	/* Pointer to current message block */
{
	putnext(q, mp);
	return (0);
}

#ifdef _MULTI_DATAMODEL
/*
 * reallocb - copy the data block from the given message block into a new block.
 * This function is used in case data block had another message block
 * pointing to it (and hence we just copy this one data block).
 *
 * Returns new message block if successful. On failure it returns NULL.
 * It also tries to do a qbufcall and if that also fails,
 * it frees the message block.
 */
static mblk_t *
pckt_reallocb(
	queue_t *q,	/* Pointer to the read queue */
	mblk_t *mp)	/* Pointer to the message block to be changed */
{
	mblk_t	*nmp;

	ASSERT(mp->b_datap->db_ref >= 1);

	/*
	 * No reallocation is needed if there is only one reference
	 * to this data block.
	 */
	if (mp->b_datap->db_ref == 1)
		return (mp);

	if ((nmp = copyb(mp)) == NULL) {
		struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;

		noenable(q);
		if (pip->pi_bufcall_id = qbufcall(q, mp->b_wptr - mp->b_rptr,
		    BPRI_MED, add_ctl_wkup, q)) {
			/*
			 * Put the message back onto the q.
			 */
			(void) putq(q, mp);
		} else {
			/*
			 * Things are pretty bad and serious if bufcall fails!
			 * Drop the message in this case.
			 */
			freemsg(mp);
		}
		return ((mblk_t *)0);
	}

	nmp->b_cont = mp->b_cont;
	freeb(mp);
	return (nmp);
}
#endif /* _MULTI_DATAMODEL */

/*
 * add_ctl_info: add message control information to in coming
 *	message.
 */
static mblk_t *
add_ctl_info(
	queue_t *q,		/* pointer to the read queue */
	mblk_t	*mp)		/* pointer to the raw data input message */
{
	struct pckt_info	*pip = (struct pckt_info *)q->q_ptr;
	mblk_t	*bp;		/* pointer to the unmodified message block */

	/*
	 * Waiting on space for previous message?
	 */
	if (pip->pi_bufcall_id) {
		/*
		 * Chain this message on to q for later processing.
		 */
		(void) putq(q, mp);
		return (NULL);
	}

	/*
	 * Need to add the message block header as
	 * an M_PROTO type message.
	 */
	if ((bp = allocb(sizeof (char), BPRI_MED)) == (mblk_t *)NULL) {

		/*
		 * There are two reasons to disable the q:
		 * (1) Flow control reasons should not wake up the q.
		 * (2) High priority messages will wakeup the q
		 *	immediately. Disallow this.
		 */
		noenable(q);
		if (pip->pi_bufcall_id = qbufcall(q, sizeof (char), BPRI_MED,
		    add_ctl_wkup, q)) {
			/*
			 * Add the message to the q.
			 */
			(void) putq(q, mp);
		} else {
			/*
			 * Things are pretty bad and serious if bufcall fails!
			 * Drop the message in this case.
			 */
			freemsg(mp);
		}

		return (NULL);
	}

	/*
	 * Copy the message type information to this message.
	 */
	bp->b_datap->db_type = M_PROTO;
	*(unsigned char *)bp->b_rptr = mp->b_datap->db_type;
	bp->b_wptr++;

#ifdef _MULTI_DATAMODEL
	/*
	 * Check the datamodel and if the calling program is
	 * an ILP32 application then we covert the M_IOCTLs and M_READs
	 * into the native ILP32 format before passing them upstream
	 * to user mode.
	 */
	switch (pip->model) {
	case DDI_MODEL_ILP32:
		switch (mp->b_datap->db_type) {
			/*
			 * This structure must have the same shape as
			 * the * ILP32 compilation of `struct iocblk'
			 * from <sys/stream.h>.
			 */
			struct iocblk32 {
				int32_t		ioc_cmd;
				caddr32_t	ioc_cr;
				uint32_t	ioc_id;
				int32_t		ioc_count;
				int32_t		ioc_error;
				int32_t		ioc_rval;
				int32_t		ioc_fill1;
				uint32_t	ioc_flag;
				int32_t		ioc_filler[2];
			} niocblk_32;
			struct iocblk		*iocblk_64;

		case M_IOCTL:
			if ((mp = pckt_reallocb(q, mp)) == (mblk_t *)0)
				return ((mblk_t *)0);

			bzero(&niocblk_32, sizeof (niocblk_32));
			iocblk_64 = (struct iocblk *)mp->b_rptr;

			/* Leave the pointer to cred_t structure as it is. */
			niocblk_32.ioc_cmd = iocblk_64->ioc_cmd;
			niocblk_32.ioc_cr = (caddr32_t)(uintptr_t)
			    iocblk_64->ioc_cr;
			niocblk_32.ioc_id = iocblk_64->ioc_id;
			niocblk_32.ioc_count = iocblk_64->ioc_count;
			niocblk_32.ioc_error = iocblk_64->ioc_error;
			niocblk_32.ioc_rval = iocblk_64->ioc_rval;
			niocblk_32.ioc_flag = iocblk_64->ioc_flag;

			/* Copy the iocblk structure for ILP32 back */
			*(struct iocblk32 *)mp->b_rptr = niocblk_32;
			mp->b_wptr = mp->b_rptr + sizeof (struct iocblk32);
			break;

		case M_READ:
			if ((mp = pckt_reallocb(q, mp)) == (mblk_t *)0)
				return ((mblk_t *)0);

			/* change the size_t to size32_t for ILP32 */
			*(size32_t *)mp->b_rptr = *(size_t *)mp->b_rptr;
			mp->b_wptr = mp->b_rptr + sizeof (size32_t);
			break;
		}
		break;

	case DATAMODEL_NONE:
		break;
	}
#endif /* _MULTI_DATAMODEL */

	/*
	 * Now change the orginal message type to M_DATA and tie them up.
	 */
	mp->b_datap->db_type = M_DATA;
	bp->b_cont = mp;

	return (bp);
}

static void
add_ctl_wkup(void *arg)
{
	queue_t *q = arg;	/* ptr to the read queue */
	struct pckt_info *pip = (struct pckt_info *)q->q_ptr;

	pip->pi_bufcall_id = 0;
	/*
	 * Allow enabling of the q to allow the service
	 * function to do its job.
	 *
	 * Also, qenable() to schedule the q immediately.
	 * This is to ensure timely processing of high priority
	 * messages if they are on the q.
	 */
	enableok(q);
	qenable(q);
}