~kris/9p

9hist

ref: f2df868ea8f5bf58dfd8c5776b770098e69ee0f9 9hist/port/sturp.c -rw-r--r-- 13.8 KiB
f2df868e — David du Colombier Plan 9 from Bell Labs 1990-03-02 36 years ago
                                                                                
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#include "syslibc.h"
#include "lock.h"
#include "chan.h"
#include "proc.h"
#include "user.h"
#include "errno.h"
#include "lint.h"
#include "mem.h"
#include "mempool.h"
#include "stream.h"
#include "dkparam.h"
#include "misc.h"

enum {
	Nurp=		32,
	MSrexmit=	1000,
};

typedef struct Urp	Urp;

/*
 * URP status
 */
struct urpstat {
	ulong	input;		/* bytes read from urp */
	ulong	output;		/* bytes output to urp */
	ulong	rxmit;		/* retransmit rejected urp msg */
	ulong	rjtrs;		/* reject, trailer size */
	ulong	rjpks;		/* reject, packet size */
	ulong	rjseq;		/* reject, sequence number */
	ulong	levelb;		/* unknown level b */
	ulong	enqsx;		/* enqs sent */
	ulong	enqsr;		/* enqs rcved */
} urpstat;

struct Urp {
	Qlock;
	short	state;		/* flags */

	/* input */

	ulong	iwindow;	/* input window */
	uchar	iseq;		/* last good input sequence number */
	uchar	lastecho;	/* last echo/rej sent */
	uchar	trbuf[3];	/* trailer being collected */
	short	trx;		/* # bytes in trailer being collected */

	/* output */

	Queue	*rq;
	Queue	*wq;
	int	XW;		/* blocks per xmit window */
	int	maxblock;	/* max block size */
	int	timeout;	/* a timeout has occurred */
	int	WS;		/* start of current window */
	int	WACK;		/* first non-acknowledged message */
	int	WNX;		/* next message to be sent */
	Rendez	r;		/* process waiting in urpoput */
	Rendez	kr;		/* process waiting in urpoput */
	Block	*xb[8];		/* the xmit window buffer */
	uchar	timer;		/* timeout for xmit */
};
#define WINDOW(u) ((u->WS + u->XW - u->WNX)%8)
#define BETWEEN(x, s, n) (s<=n ? x>=s && x<n : x<n || x>=s)
#define UNACKED(x, u) (BETWEEN(x, u->WACK, u->WNX)

/*
 *  Protocol control bytes
 */
#define	SEQ	0010		/* sequence number, ends trailers */
#undef	ECHO
#define	ECHO	0020		/* echos, data given to next queue */
#define	REJ	0030		/* rejections, transmission error */
#define	ACK	0040		/* acknowledgments */
#define	BOT	0050		/* beginning of trailer */
#define	BOTM	0051		/* beginning of trailer, more data follows */
#define	BOTS	0052		/* seq update algorithm on this trailer */
#define	SOU	0053		/* start of unsequenced trailer */
#define	EOU	0054		/* end of unsequenced trailer */
#define	ENQ	0055		/* xmitter requests flow/error status */
#define	CHECK	0056		/* xmitter requests error status */
#define	INITREQ 0057		/* request initialization */
#define	INIT0	0060		/* disable trailer processing */
#define	INIT1	0061		/* enable trailer procesing */
#define	AINIT	0062		/* response to INIT0/INIT1 */
#undef	DELAY
#define	DELAY	0100		/* real-time printing delay */
#define	BREAK	0110		/* Send/receive break (new style) */

#define	REXMITING	0001
#define	REXMIT		0002
#define	INITING 	0004

Urp	urp[Nurp];

/*
 *  predeclared
 */
static void	urpiput(Queue*, Block*, Rendez*);
static void	urpoput(Queue*, Block*, Rendez*);
static void	urpopen(Queue*, Stream*);
static void	urpclose(Queue *);
static void	rcvack(Urp*, int);
static void	flushinput(Urp*);
static void	sendctl(Queue*, int);
static void	queuectl(Urp*, int);
static void	initoutput(Urp*, int);
static void	initinput(Urp*, int);

Qinfo urpinfo = { urpiput, urpoput, urpopen, urpclose, "urp" };

int
urpopen(Queue *q, Stream *s)
{
	Urp *up;
	int i;

	/*
	 *  find a free urp structure
	 */
	for(up = urp; up < &urp[Nurp]; up++){
		qlock(up);
		if(up->state == 0)
			break;
		qunlock(up);
	}
	if(up == &urp[Nurp])
		error(0, Egreg);

	q->ptr = = q->other->ptr = up;
	up->rq = q;
	up->wq = WR(q);
	q->put = urpciput;
	qunlock(up);
	initinput(up, 0);
	initoutput(up, 0);
}

/*
 *  Shut it down.
 */
static int
allacked(void *a)
{
	Urp *up;

	up = (Urp *)a;
	return up->WACK == up->WNX;
}
urpclose(Queue *q)
{
	Block *bp;
	Urp *up;
	int i;

	up = (Urp *)q->ptr;
	up->state |= LCLOSE;

	/*
	 *  wait for output to get acked
	 */
	while(!urpdone(up))
		sleep(&up->r, urpdone, up);
	up->state = 0;
}

/*
 *  upstream control messages
 */
static void
urpctliput(Urp *up, Queue *q, Block *bp)
{
	switch(bp->type){
	case M_HANGUP:
		/*
		 *  ack all outstanding messages
		 */
		lock(up);
		up->state &= ~(INITING);
		up->state |= RCLOSE;
		unlock(up);
		if(up->WS<up->WNX)
			urprack(up, ECHO+((up->WNX-1)&07));
	}
	PUTNEXT(q, bp);
}

/*
 *  character mode input.  the last character in EVERY block is
 *  a control character.
 */
void
urpciput(Queue *q, Block *bp, Rendez *rp)
{
	Urp *up;
	int i, full;
	Block *nbp;

	up = (Urp *)q->ptr;
	if(bp->type != M_DATA){
		urpctliput(up, q, bp);
		return;
	}

	/*
	 *  get the control character
	 */
	if(bp->wptr > bp->rptr)
		ctl = *(--bp->wptr);
	else
		ctl = 0;

	/*
	 *  send the block upstream
	 */
	if(bp->wptr > bp->rptr)
		PUTNEXT(q, bp);
	else
		freeb(bp);

	/*
	 *  handle the control character
	 */
	switch(ctl){
	case 0:
		break;

	case ENQ:
		urpstat.enqsr++;
		queuectl(up, up->lastecho);
		queuectl(up, ACK+up->iseq);
		flushinput(up);
		break;

	case CHECK:
		queuectl(up, ACK+up->iseq);
		break;

	case AINIT:
		up->state &= ~INITING;
		flushinput(up);
		wakeup(&cp->kr);
		break;

	case INIT0:
	case INIT1:
		queuectl(up, AINIT);
		if(*bp->rptr == INIT1)
			q->put = urpbiput;
		initinput(up, 0);
		break;

	case INITREQ:
		initoutput(up, 0);
		break;

	case BREAK:
		break;

	case ACK+0: case ACK+1: case ACK+2: case ACK+3:
	case ACK+4: case ACK+5: case ACK+6: case ACK+7:
	case ECHO+0: case ECHO+1: case ECHO+2: case ECHO+3:
	case ECHO+4: case ECHO+5: case ECHO+6: case ECHO+7:
		rcvack(up, ctl);
		break;

	case SEQ+0: case SEQ+1: case SEQ+2: case SEQ+3:
	case SEQ+4: case SEQ+5: case SEQ+6: case SEQ+7:
		/*
		 *  acknowledge receipt
		 */
		ctl = ctl & 07;
		if(q->next->len < Streamhi){
			queuectl(up, ECHO+ctl);
			up->lastecho = ECHO+ctl;
			wakeup(&cp->kr);
		}
		up->iseq = ctl;
		break;
	}
}

/*
 *  block mode input.  the last character in EVERY block is a control character.
 */
void
urpbiput(Queue *q, Block *bp, Rendez *rp)
{
	Urp *up;
	int i, full;
	Block *nbp;

	up = (Urp *)q->ptr;
	if(bp->type != M_DATA){
		urpctliput(up, q, bp);
		return;
	}

	/*
	 *  get the control character
	 */
	if(bp->wptr > bp->rptr)
		ctl = *(--bp->wptr);
	else
		ctl = 0;

	/*
	 *  take care of any block count(trx)
	 */
	while(bp->wptr > bp->rptr && up->trx){
		switch (up->trx) {
		case 1:
		case 2:
			up->trbuf[up->trx++] = *bp->rptr++;
			continue;
		default:
			up->trx = 0;
		case 0:
			break;
		}
	}

	/*
	 *  queue the block
	 */
	if(bp->wptr > bp->rptr){
		if(q->len > up->iwindow){
			flushinput(up);
			freeb(bp);
			return;
		}
		putq(q, bp);
	} else
		freeb(bp);

	/*
	 *  handle the control character
	 */
	switch(ctl){
	case 0:
		break;
	case ENQ:
		urpstat.enqsr++;
		queuectl(up, up->lastecho);
		queuectl(up, ACK+up->iseq);
		flushinput(up);
		break;

	case CHECK:
		queuectl(WR(q)->next, ACK+up->iseq);
		break;

	case AINIT:
		up->state &= ~INITING;
		flushinput(up);
		wakeup(&cp->kr);
		break;

	case INIT0:
	case INIT1:
		queuectl(up, AINIT);
		if(*bp->rptr == INIT0)
			q->put = urpciput;
		initinput(up, 0);
		break;

	case INITREQ:
		initoutput(up, 0);
		break;

	case BREAK:
		break;

	case BOT:
	case BOTS:
	case BOTM:
		up->trx = 1;
		up->trbuf[0] = ctl;
		break;

	case REJ+0: case REJ+1: case REJ+2: case REJ+3:
	case REJ+4: case REJ+5: case REJ+6: case REJ+7:
		rcvack(up, ctl);
		break;
	
	case ACK+0: case ACK+1: case ACK+2: case ACK+3:
	case ACK+4: case ACK+5: case ACK+6: case ACK+7:
	case ECHO+0: case ECHO+1: case ECHO+2: case ECHO+3:
	case ECHO+4: case ECHO+5: case ECHO+6: case ECHO+7:
		rcvack(up, ctl);
		break;

	/*
	 *  this case is extremely ugliferous
	 */
	case SEQ+0: case SEQ+1: case SEQ+2: case SEQ+3:
	case SEQ+4: case SEQ+5: case SEQ+6: case SEQ+7:
		i = ctl & 07;
		if(up->trx != 3){
			urpstat.rjtrs++;
			flushinput(up);
			break;
		} else if(q->len != up->trbuf[1] + (up->trbuf[2]<<8)){
			urpstat.rjpks++;
			flushinput(up);
			break;
		} else if(i != ((up->iseq+1)&07))) {
			urpstat.rjseq++;
			flushinput(up);
			break;
		}

		/*
		 *  send data upstream
		 */
		if(q->first) {
			q->first->flags |= S_DELIM;
			while(bp = getq(q))
				PUTNEXT(q, nbp);
		} else {
			bp = allocb(0);
			bp->flag |= S_DELIM;
			PUTNEXT(q, bp);
		{
		up->trx = 0;

		/*
		 *  acknowledge receipt
		 */
		ctl = ctl & 07;
		if(q->next->len < Streamhi){
			queuectl(up, ECHO+ctl);
			up->lastecho = ECHO+ctl;
			wakeup(&cp->kr);
		}
		up->iseq = ctl;
		break;
	}
}

/*
 *  downstream control
 */
static void
urpctloput(Urp *up, Queue *q, Block *bp)
{
	int fields[2];
	int n;
	int inwin=0, outwin=0;

	switch(bp->type){
	case M_CTL:
		if(streamparse("init", bp)){
			switch(getfields(bp->rptr, fields, 2, ' ')){
			case 2:
				inwin = strtoul(fields[1], 0, 0);
			case 1:
				outwin = strtoul(fields[0], 0, 0);
			}
			initinput(up, inwin);
			initoutput(up, outwin);
			freeb(bp);
			return;
		}
	}
	PUTNEXT(q, bp);
}

/*
 * accept data from writer
 */
urpoput(Queue *q, Block *bp)
{
	Urp *up;

	up = (Urp *)q->ptr;

	if(bp->type != M_DATA){
		urpctloput(up, q, bp);
		return;
	}

	urpstat.output + =  bp->wptr - bp->rptr;

	for(;;){
	}
}

/*
 * wait for an AINIT
 */
void
urpopenwait(Urp *up, Queue *q)
{
	while((up->state&ISOPENING) && !(up->state&RCLOSE)) {
		proc->state = Waiting;
		up->proc = proc;
		putctl1(q->next, M_RDATA, INIT1);
		if((up->state&ISOPENING) == 0){
			up->proc = 0;
			if(proc->state == Waiting){
				proc->state = Running;
				return;
			}
		}
		sched();
		up->proc = 0;
	}
}

/*
 *  wait till transmission is complete
 */
void
urpxmitwait(Urp *up, Queue *q)
{
	Block *bp;
	int debug;

	debug = (q->flag&QDEBUG);

	up->timeout = 0;
	while(!EMPTY(q) || up->WS<up->WNX){
		/*
		 *  clean up if the channel closed
		 */
		if (up->state & RCLOSE) {
			while(bp = getq(q))
				freeb(bp);
			up->WACK = up->WS = up->WNX;
		}
		/*
		 * free acked blocks
		 */
		urpfreeacked(up);
		/*
		 * retransmit if requested
		 */
		if(up->state&REXMIT)
			urprexmit(up, q);
		/*
		 * fill up the window
		 */
		if(!EMPTY(q) && WINDOW(up))
			urpfillwindow(up, q);
		/*
		 * ask other end for its status
		 */
		if(up->timeout){
			urpstat.enqsx++;
			putctl1(q->next, M_RDATA, ENQ);
			up->timeout = 0;
		}
		lock(up->olock);
		if((up->state&RCLOSE) == 0 && up->WS! = up->WNX) {
			proc->state = Waiting;
			up->proc = proc;
			unlock(up->olock);
			sched();
		} else
			unlock(up->olock);
	}
	urpfreeacked(up);
	up->WS = up->WACK = up->WF = up->WNX = up->WNX&07;
}

/*
 * fill up the urp output window
 */
void
urpfillwindow(Urp *up, Queue *q)
{
	Block *bp, *xbp;
	int debug;

	debug = (q->flag&QDEBUG);

	/*
	 * now process any thing that fits in the flow control window
	 */
	while (WINDOW(up)) {
		bp = getq(q);
		if (bp  ==  NULL)
			break;
		/* force MAXBLOCK length segments */
		if (bp->rptr+up->maxblock < bp->wptr) {
			xbp = allocb(0);
			if (xbp == NULL) {
				putbq(q, bp);
				break;
			}
			xbp->rptr = bp->rptr;
			bp->rptr + =  up->maxblock;
			xbp->wptr = bp->rptr;
			putbq(q, bp);
			bp = xbp;
		}
		/*
		 *  put new block in the block array.  if something is already
		 *  there, it should be because we haven't gotten around to freeing
		 *  it yet.  If not, complain.
		 */
		if (up->xb[up->WNX&07]) {
			urpfreeacked(up);
			if (up->xb[up->WNX&07]) {
				uprint(up, "urpfillwindow: overlap");
				freeb(up->xb[up->WNX&07]);
			}
		}
		up->xb[up->WNX&07] = bp;
		up->WNX++;
		urpxmit(q, bp, up->WNX-1);
	}
}

/*
 *  Send out a message, with trailer.
 */
void
urpxmit(Queue *q, Block *bp, int seqno)
{
	Urp *up = (Urp *)q->ptr;
	int size;
	Block *xbp;
	int debug;

	if (bp == NULL) {
		print("null bp in urpxmit\n");
		return;
	}
	debug = (q->flag&QDEBUG);
	size = bp->wptr - bp->rptr;
	seqno & =  07;
	/* send ptr to block, if non-empty */
	if (size) {
		if ((xbp = allocb(0))  ==  NULL){
			print("can't xmit\n");
			return;
		}
		xbp->rptr = bp->rptr;
		xbp->wptr = bp->wptr;
		xbp->type = bp->type;
		PUTNEXT(q, xbp);
	}
	/* send trailer */
	if ((xbp = allocb(3))  ==  NULL){
		print("can't xmit2\n");
		return;
	}
	xbp->type = M_RDATA;
	*xbp->wptr++ = (bp->class&S_DELIM) ? BOT: BOTM;
	*xbp->wptr++ = size;
	*xbp->wptr++ = size >> 8;
	PUTNEXT(q, xbp);
	putctl1(q->next, M_RDATA, SEQ + seqno);
	up->timer = DKPTIME;
}

void
urprexmit(Urp *up, Queue *q)
{
	int i;

	for (i = up->WACK; i<up->WNX; i++) {
		urpxmit(q, up->xb[i&07], i);
		urpstat.rxmit++;
	}
	up->state & =  ~REXMIT;
}

/*
 *  receive an acknowledgement
 */
static void
rcvack(Urp *up, int msg)
{
	int seqno;
	int next;

	seqno = msg&07;
	next = (seqno+1) & 0x7

	lock(up);
	if(BETWEEN(seqno, up->WACK, up->WNX))
		up->WACK = next;

	switch(msg & 0370){
	case ECHO:
		up->timer = MSrexmit;	/* push off ENQ timeout */
		if(BETWEEN(seqno, up->WS, up->WNX)){
			up->WS = next;
			wakeup(&up->r);
		}
		break;
	case REJ:
	case ACK:
		if(up->WACK == next){
			up->state |= REXMIT;
			wakeup(&up->r);
		}
		break;
	}
	unlock(up);
}

/*
 * throw away any partially collected input
 */
static void
flushinput(Urp *up)
{
	Block *bp;

	while (bp = getq(up->rq))
		freeb(bp);
	up->trx = 0;
}

/*
 *  send a control character down stream
 */
static void
sendctl(Queue *q, int x)
{
	Block *bp;

	/*
	 *  send anything queued
	 */
	while(bp = getq(q))
		PUTNEXT(q, bp);

	/*
	 *  send the new byte
	 */
	bp = allocb(1);
	*bp->wptr++ = x;
	PUTNEXT(q, bp);
}

/*
 *  queue a control character to be sent down stream
 */
static void
queuectl(Urp *up, int x)
{
	Block *bp;
	Queue *q;

	q = up->wq;
	bp = allocb(1);
	*bp->wptr++ = x;
	putq(q, bp);
	wakeup(&up->r);
}

/*
 *  initialize output
 */
static void
initoutput(Urp *up, int window)
{
	/*
	 *  ack any outstanding blocks
	 */
	if(up->WS<up->WNX)
		urprack(up, ECHO+((up->WNX-1)&07));

	/*
	 *  set output window
	 */
	up->maxblock = window/4;
	if(up->maxblock < 64)
		up->maxblock = 64;
	if(up->maxblock > Streamhi/4)
		up->maxblock = Streamhi/4;
	up->XW = 4;

	/*
	 *  set sequence varialbles
	 */
	up->WS = 1;
	up->WACK = 1;
	up->WNX = 1;

	/*
	 *  tell the other side we've inited
	 */
	up->state |= ISOPENING;
	up->timer = MSrexmit;
	sendctl(q->next, INIT1);
}

/*
 *  initialize input
 */
static void
initinput(Urp *up, int window)
{
	/*
	 *  restart all sequence parameters
	 */
	up->trx = 0;
	up->iseq = 0;
	up->lastecho = ECHO+0;
	up->WF = 1;
	flushinput(up);
}