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
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"
typedef struct Link Link;
typedef struct Loop Loop;
struct Link
{
Lock;
int ref;
long packets; /* total number of packets sent */
long bytes; /* total number of bytes sent */
int indrop; /* enable dropping on iq overflow */
long soverflows; /* packets dropped because iq overflowed */
long droprate; /* drop 1/droprate packets in tq */
long drops; /* packets deliberately dropped */
vlong delay0ns; /* nanosec of delay in the link */
long delaynns; /* nanosec of delay per byte */
Block *tq; /* transmission queue */
Block *tqtail;
vlong tout; /* time the last packet in tq is really out */
vlong tin; /* time the head packet in tq enters the remote side */
long limit; /* queue buffering limit */
Queue *oq; /* output queue from other side & packets in the link */
Queue *iq;
Timer ci; /* time to move packets from next packet from oq */
};
struct Loop
{
QLock;
int ref;
int minmtu; /* smallest block transmittable */
Loop *next;
ulong path;
Link link[2];
};
static struct
{
Lock;
ulong path;
} loopbackalloc;
enum
{
Qtopdir= 1, /* top level directory */
Qloopdir, /* loopback* directory */
Qportdir, /* directory each end of the loop */
Qctl,
Qstatus,
Qstats,
Qdata,
MaxQ,
Nloopbacks = 5,
Statelen = 23*1024, /* status buffer size */
Tmsize = 8,
Delayn = 10000, /* default delays in ns */
Delay0 = 2500000,
Loopqlim = 32*1024, /* default size of queues */
};
static Dirtab loopportdir[] =
{
"ctl", {Qctl}, 0, 0222,
"status", {Qstatus}, 0, 0444,
"stats", {Qstats}, 0, 0444,
"data", {Qdata}, 0, 0666,
};
static Dirtab loopdirs[MaxQ];
static Loop loopbacks[Nloopbacks];
#define TYPE(x) (((ulong)(x))&0xff)
#define ID(x) (((ulong)(x))>>8)
#define QID(x,y) ((((ulong)(x))<<8)|((ulong)(y)))
static void looper(Loop *lb);
static long loopoput(Loop *lb, Link *link, Block *bp);
static void ptime(uchar *p, vlong t);
static vlong gtime(uchar *p);
static void closelink(Link *link, int dofree);
static void pushlink(Link *link, vlong now);
static void freelb(Loop *lb);
static void linkintr(Ureg*, Timer *ci);
static void
loopbackinit(void)
{
int i;
for(i = 0; i < Nloopbacks; i++)
loopbacks[i].path = i;
/* invert directory tables for non-directory entries */
for(i=0; i<nelem(loopportdir); i++)
loopdirs[loopportdir[i].qid.path] = loopportdir[i];
}
static Chan*
loopbackattach(char *spec)
{
Loop *volatile lb;
Queue *q;
Chan *c;
int chan;
ulong dev;
dev = strtoul(spec, nil, 10);
if(dev >= Nloopbacks)
error(Enodev);
c = devattach(loopbackdevtab.dc, spec);
if(waserror()){
chanfree(c);
nexterror();
}
lb = &loopbacks[dev];
qlock(lb);
if(waserror()){
lb->ref--;
qunlock(lb);
nexterror();
}
lb->ref++;
if(lb->ref == 1){
for(chan = 0; chan < 2; chan++){
lb->link[chan].ci.mode = Trelative;
lb->link[chan].ci.a = &lb->link[chan];
lb->link[chan].ci.f = linkintr;
lb->link[chan].limit = Loopqlim;
q = qopen(lb->link[chan].limit, 0, 0, 0);
lb->link[chan].iq = q;
if(q == nil){
freelb(lb);
exhausted("memory");
}
q = qopen(lb->link[chan].limit, 0, 0, 0);
lb->link[chan].oq = q;
if(q == nil){
freelb(lb);
exhausted("memory");
}
lb->link[chan].indrop = 1;
lb->link[chan].delaynns = Delayn;
lb->link[chan].delay0ns = Delay0;
}
}
poperror();
qunlock(lb);
poperror();
mkqid(&c->qid, QID(0, Qtopdir), 0, QTDIR);
c->aux = lb;
c->dev = dev;
return c;
}
static int
loopbackgen(Chan *c, char*, Dirtab*, int, int i, Dir *dp)
{
Dirtab *tab;
int len, type;
Qid qid;
type = TYPE(c->qid.path);
if(i == DEVDOTDOT){
switch(type){
case Qtopdir:
case Qloopdir:
snprint(up->genbuf, sizeof(up->genbuf), "#λ%ld", c->dev);
mkqid(&qid, QID(0, Qtopdir), 0, QTDIR);
devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
break;
case Qportdir:
snprint(up->genbuf, sizeof(up->genbuf), "loopback%ld", c->dev);
mkqid(&qid, QID(0, Qloopdir), 0, QTDIR);
devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
break;
default:
panic("loopbackgen %llux", c->qid.path);
}
return 1;
}
switch(type){
case Qtopdir:
if(i != 0)
return -1;
snprint(up->genbuf, sizeof(up->genbuf), "loopback%ld", c->dev);
mkqid(&qid, QID(0, Qloopdir), 0, QTDIR);
devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
return 1;
case Qloopdir:
if(i >= 2)
return -1;
snprint(up->genbuf, sizeof(up->genbuf), "%d", i);
mkqid(&qid, QID(i, QID(0, Qportdir)), 0, QTDIR);
devdir(c, qid, up->genbuf, 0, eve, 0555, dp);
return 1;
case Qportdir:
if(i >= nelem(loopportdir))
return -1;
tab = &loopportdir[i];
mkqid(&qid, QID(ID(c->qid.path), tab->qid.path), 0, QTFILE);
devdir(c, qid, tab->name, tab->length, eve, tab->perm, dp);
return 1;
default:
/* non directory entries end up here; must be in lowest level */
if(c->qid.type & QTDIR)
panic("loopbackgen: unexpected directory");
if(i != 0)
return -1;
tab = &loopdirs[type];
if(tab == nil)
panic("loopbackgen: unknown type: %d", type);
len = tab->length;
devdir(c, c->qid, tab->name, len, eve, tab->perm, dp);
return 1;
}
}
static Walkqid*
loopbackwalk(Chan *c, Chan *nc, char **name, int nname)
{
Walkqid *wq;
Loop *lb;
wq = devwalk(c, nc, name, nname, nil, 0, loopbackgen);
if(wq != nil && wq->clone != nil && wq->clone != c){
lb = c->aux;
qlock(lb);
lb->ref++;
if((c->flag & COPEN) && TYPE(c->qid.path) == Qdata)
lb->link[ID(c->qid.path)].ref++;
qunlock(lb);
}
return wq;
}
static int
loopbackstat(Chan *c, uchar *db, int n)
{
return devstat(c, db, n, nil, 0, loopbackgen);
}
/*
* if the stream doesn't exist, create it
*/
static Chan*
loopbackopen(Chan *c, int omode)
{
Loop *lb;
if(c->qid.type & QTDIR){
if(omode != OREAD)
error(Ebadarg);
c->mode = omode;
c->flag |= COPEN;
c->offset = 0;
return c;
}
lb = c->aux;
qlock(lb);
if(TYPE(c->qid.path) == Qdata){
if(lb->link[ID(c->qid.path)].ref){
qunlock(lb);
error(Einuse);
}
lb->link[ID(c->qid.path)].ref++;
}
qunlock(lb);
c->mode = openmode(omode);
c->flag |= COPEN;
c->offset = 0;
c->iounit = qiomaxatomic;
return c;
}
static void
loopbackclose(Chan *c)
{
Loop *lb;
int ref, chan;
lb = c->aux;
qlock(lb);
/*
* closing either side hangs up the stream
*/
if((c->flag & COPEN) && TYPE(c->qid.path) == Qdata){
chan = ID(c->qid.path);
if(--lb->link[chan].ref == 0){
qhangup(lb->link[chan ^ 1].oq, nil);
looper(lb);
}
}
/*
* if both sides are closed, they are reusable
*/
if(lb->link[0].ref == 0 && lb->link[1].ref == 0){
for(chan = 0; chan < 2; chan++){
closelink(&lb->link[chan], 0);
qreopen(lb->link[chan].iq);
qreopen(lb->link[chan].oq);
qsetlimit(lb->link[chan].oq, lb->link[chan].limit);
qsetlimit(lb->link[chan].iq, lb->link[chan].limit);
}
}
ref = --lb->ref;
if(ref == 0)
freelb(lb);
qunlock(lb);
}
static void
freelb(Loop *lb)
{
int chan;
for(chan = 0; chan < 2; chan++)
closelink(&lb->link[chan], 1);
}
/*
* called with the Loop qlocked,
* so only pushlink can mess with the queues
*/
static void
closelink(Link *link, int dofree)
{
Queue *iq, *oq;
Block *bp;
ilock(link);
iq = link->iq;
oq = link->oq;
bp = link->tq;
link->tq = nil;
link->tqtail = nil;
link->tout = 0;
link->tin = 0;
timerdel(&link->ci);
iunlock(link);
if(iq != nil){
qclose(iq);
if(dofree){
ilock(link);
free(iq);
link->iq = nil;
iunlock(link);
}
}
if(oq != nil){
qclose(oq);
if(dofree){
ilock(link);
free(oq);
link->oq = nil;
iunlock(link);
}
}
freeblist(bp);
}
static long
loopbackread(Chan *c, void *va, long n, vlong offset)
{
Loop *lb;
Link *link;
char *buf;
long rv;
lb = c->aux;
switch(TYPE(c->qid.path)){
default:
error(Eperm);
return -1; /* not reached */
case Qtopdir:
case Qloopdir:
case Qportdir:
return devdirread(c, va, n, nil, 0, loopbackgen);
case Qdata:
return qread(lb->link[ID(c->qid.path)].iq, va, n);
case Qstatus:
link = &lb->link[ID(c->qid.path)];
buf = smalloc(Statelen);
rv = snprint(buf, Statelen, "delay %lld %ld\n", link->delay0ns, link->delaynns);
rv += snprint(buf+rv, Statelen-rv, "limit %ld\n", link->limit);
rv += snprint(buf+rv, Statelen-rv, "indrop %d\n", link->indrop);
snprint(buf+rv, Statelen-rv, "droprate %ld\n", link->droprate);
rv = readstr(offset, va, n, buf);
free(buf);
break;
case Qstats:
link = &lb->link[ID(c->qid.path)];
buf = smalloc(Statelen);
rv = snprint(buf, Statelen, "packets: %ld\n", link->packets);
rv += snprint(buf+rv, Statelen-rv, "bytes: %ld\n", link->bytes);
rv += snprint(buf+rv, Statelen-rv, "dropped: %ld\n", link->drops);
snprint(buf+rv, Statelen-rv, "soft overflows: %ld\n", link->soverflows);
rv = readstr(offset, va, n, buf);
free(buf);
break;
}
return rv;
}
static Block*
loopbackbread(Chan *c, long n, ulong offset)
{
Loop *lb;
lb = c->aux;
if(TYPE(c->qid.path) == Qdata)
return qbread(lb->link[ID(c->qid.path)].iq, n);
return devbread(c, n, offset);
}
static long
loopbackbwrite(Chan *c, Block *bp, ulong off)
{
Loop *lb;
lb = c->aux;
if(TYPE(c->qid.path) == Qdata)
return loopoput(lb, &lb->link[ID(c->qid.path) ^ 1], bp);
return devbwrite(c, bp, off);
}
static long
loopbackwrite(Chan *c, void *va, long n, vlong off)
{
Loop *lb;
Link *link;
Cmdbuf *volatile cb;
Block *volatile bp;
vlong d0, d0ns;
long dn, dnns;
switch(TYPE(c->qid.path)){
case Qdata:
bp = allocb(n);
if(waserror()){
freeb(bp);
nexterror();
}
memmove(bp->wp, va, n);
poperror();
bp->wp += n;
return loopbackbwrite(c, bp, off);
case Qctl:
lb = c->aux;
link = &lb->link[ID(c->qid.path)];
cb = parsecmd(va, n);
if(waserror()){
free(cb);
nexterror();
}
if(cb->nf < 1)
error("short control request");
if(strcmp(cb->f[0], "delay") == 0){
if(cb->nf != 3)
error("usage: delay latency bytedelay");
d0ns = strtoll(cb->f[1], nil, 10);
dnns = strtol(cb->f[2], nil, 10);
/*
* it takes about 20000 cycles on a pentium ii
* to run pushlink; perhaps this should be accounted.
*/
ilock(link);
link->delay0ns = d0ns;
link->delaynns = dnns;
iunlock(link);
}else if(strcmp(cb->f[0], "indrop") == 0){
if(cb->nf != 2)
error("usage: indrop [01]");
ilock(link);
link->indrop = strtol(cb->f[1], nil, 0) != 0;
iunlock(link);
}else if(strcmp(cb->f[0], "droprate") == 0){
if(cb->nf != 2)
error("usage: droprate ofn");
ilock(link);
link->droprate = strtol(cb->f[1], nil, 0);
iunlock(link);
}else if(strcmp(cb->f[0], "limit") == 0){
if(cb->nf != 2)
error("usage: limit maxqsize");
ilock(link);
link->limit = strtol(cb->f[1], nil, 0);
qsetlimit(link->oq, link->limit);
qsetlimit(link->iq, link->limit);
iunlock(link);
}else if(strcmp(cb->f[0], "reset") == 0){
if(cb->nf != 1)
error("usage: reset");
ilock(link);
link->packets = 0;
link->bytes = 0;
link->indrop = 0;
link->soverflows = 0;
link->drops = 0;
iunlock(link);
}else
error("unknown control request");
poperror();
free(cb);
break;
default:
error(Eperm);
}
return n;
}
static long
loopoput(Loop *lb, Link *link, Block *bp)
{
long n = BLEN(bp);
bp = padblock(bp, Tmsize);
if(BLEN(bp) < lb->minmtu)
bp = adjustblock(bp, lb->minmtu);
ptime(bp->rp, todget(nil));
link->packets++;
link->bytes += n;
qbwrite(link->oq, bp);
looper(lb);
return n;
}
static void
looper(Loop *lb)
{
vlong t;
int chan;
t = todget(nil);
for(chan = 0; chan < 2; chan++)
pushlink(&lb->link[chan], t);
}
static void
linkintr(Ureg*, Timer *ci)
{
Link *link;
link = ci->a;
pushlink(link, ci->ns);
}
/*
* move blocks between queues if they are ready.
* schedule an interrupt for the next interesting time.
*
* must be called with the link ilocked.
*/
static void
pushlink(Link *link, vlong now)
{
Block *bp;
vlong tout, tin;
/*
* put another block in the link queue
*/
ilock(link);
if(link->iq == nil || link->oq == nil){
iunlock(link);
return;
}
timerdel(&link->ci);
/*
* put more blocks into the xmit queue
* use the time the last packet was supposed to go out
* as the start time for the next packet, rather than
* the current time. this more closely models a network
* device which can queue multiple output packets.
*/
tout = link->tout;
if(!tout)
tout = now;
while(tout <= now){
bp = qget(link->oq);
if(bp == nil){
tout = 0;
break;
}
/*
* can't send the packet before it gets queued
*/
tin = gtime(bp->rp);
if(tin > tout)
tout = tin;
tout = tout + (BLEN(bp) - Tmsize) * link->delayn;
/*
* drop packets
*/
if(link->droprate && nrand(link->droprate) == 0)
link->drops++;
else{
ptime(bp->rp, tout + link->delay0ns);
if(link->tq == nil)
link->tq = bp;
else
link->tqtail->next = bp;
link->tqtail = bp;
}
}
/*
* record the next time a packet can be sent,
* but don't schedule an interrupt if none is waiting
*/
link->tout = tout;
if(!qcanread(link->oq))
tout = 0;
/*
* put more blocks into the receive queue
*/
tin = 0;
while(bp = link->tq){
tin = gtime(bp->rp);
if(tin > now)
break;
bp->rp += Tmsize;
link->tq = bp->next;
bp->next = nil;
if(!link->indrop)
qpassnolim(link->iq, bp);
else if(qpass(link->iq, bp) < 0)
link->soverflows++;
tin = 0;
}
if(bp == nil && qisclosed(link->oq) && !qcanread(link->oq) && !qisclosed(link->iq))
qhangup(link->iq, nil);
link->tin = tin;
if(!tin || tin > tout && tout)
tin = tout;
link->ci.ns = tin - now;
if(tin){
if(tin < now)
panic("loopback unfinished business");
timeradd(&link->ci);
}
iunlock(link);
}
static void
ptime(uchar *p, vlong t)
{
ulong tt;
tt = t >> 32;
p[0] = tt >> 24;
p[1] = tt >> 16;
p[2] = tt >> 8;
p[3] = tt;
tt = t;
p[4] = tt >> 24;
p[5] = tt >> 16;
p[6] = tt >> 8;
p[7] = tt;
}
static vlong
gtime(uchar *p)
{
ulong t1, t2;
t1 = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
t2 = (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7];
return ((vlong)t1 << 32) | t2;
}
Dev loopbackdevtab = {
L'λ',
"loopback",
devreset,
loopbackinit,
devshutdown,
loopbackattach,
loopbackwalk,
loopbackstat,
loopbackopen,
devcreate,
loopbackclose,
loopbackread,
loopbackbread,
loopbackwrite,
loopbackbwrite,
devremove,
devwstat,
};
|