Blame


1 056fe1ba 2003-11-23 devnull /*
2 056fe1ba 2003-11-23 devnull * Multiplexed Venti client. It would be nice if we
3 056fe1ba 2003-11-23 devnull * could turn this into a generic library routine rather
4 056fe1ba 2003-11-23 devnull * than keep it Venti specific. A user-level 9P client
5 056fe1ba 2003-11-23 devnull * could use something like this too.
6 a09e80f9 2004-05-23 devnull *
7 a09e80f9 2004-05-23 devnull * (Actually it does - this should be replaced with libmux,
8 a09e80f9 2004-05-23 devnull * which should be renamed librpcmux.)
9 056fe1ba 2003-11-23 devnull *
10 056fe1ba 2003-11-23 devnull * This is a little more complicated than it might be
11 056fe1ba 2003-11-23 devnull * because we want it to work well within and without libthread.
12 056fe1ba 2003-11-23 devnull *
13 056fe1ba 2003-11-23 devnull * The mux code is inspired by tra's, which is inspired by the Plan 9 kernel.
14 056fe1ba 2003-11-23 devnull */
15 056fe1ba 2003-11-23 devnull
16 056fe1ba 2003-11-23 devnull #include <u.h>
17 056fe1ba 2003-11-23 devnull #include <libc.h>
18 056fe1ba 2003-11-23 devnull #include <venti.h>
19 056fe1ba 2003-11-23 devnull
20 056fe1ba 2003-11-23 devnull typedef struct Rwait Rwait;
21 056fe1ba 2003-11-23 devnull struct Rwait
22 056fe1ba 2003-11-23 devnull {
23 056fe1ba 2003-11-23 devnull Rendez r;
24 056fe1ba 2003-11-23 devnull Packet *p;
25 056fe1ba 2003-11-23 devnull int done;
26 056fe1ba 2003-11-23 devnull int sleeping;
27 056fe1ba 2003-11-23 devnull };
28 056fe1ba 2003-11-23 devnull
29 056fe1ba 2003-11-23 devnull static int gettag(VtConn*, Rwait*);
30 056fe1ba 2003-11-23 devnull static void puttag(VtConn*, Rwait*, int);
31 056fe1ba 2003-11-23 devnull static void muxrpc(VtConn*, Packet*);
32 056fe1ba 2003-11-23 devnull
33 056fe1ba 2003-11-23 devnull Packet*
34 7252036f 2005-11-02 devnull _vtrpc(VtConn *z, Packet *p, VtFcall *tx)
35 056fe1ba 2003-11-23 devnull {
36 056fe1ba 2003-11-23 devnull int i;
37 056fe1ba 2003-11-23 devnull uchar tag, buf[2], *top;
38 5ddc97fc 2005-02-14 devnull Rwait *r, *rr;
39 056fe1ba 2003-11-23 devnull
40 056fe1ba 2003-11-23 devnull /* must malloc because stack could be private */
41 056fe1ba 2003-11-23 devnull r = vtmallocz(sizeof(Rwait));
42 056fe1ba 2003-11-23 devnull
43 056fe1ba 2003-11-23 devnull qlock(&z->lk);
44 056fe1ba 2003-11-23 devnull r->r.l = &z->lk;
45 056fe1ba 2003-11-23 devnull tag = gettag(z, r);
46 7252036f 2005-11-02 devnull if(tx){
47 7252036f 2005-11-02 devnull /* vtfcallrpc can't print packet because it doesn't have tag */
48 7252036f 2005-11-02 devnull tx->tag = tag;
49 7252036f 2005-11-02 devnull if(chattyventi)
50 7252036f 2005-11-02 devnull fprint(2, "%s -> %F\n", argv0, tx);
51 7252036f 2005-11-02 devnull }
52 056fe1ba 2003-11-23 devnull
53 056fe1ba 2003-11-23 devnull /* slam tag into packet */
54 056fe1ba 2003-11-23 devnull top = packetpeek(p, buf, 0, 2);
55 056fe1ba 2003-11-23 devnull if(top == nil){
56 056fe1ba 2003-11-23 devnull packetfree(p);
57 056fe1ba 2003-11-23 devnull return nil;
58 056fe1ba 2003-11-23 devnull }
59 056fe1ba 2003-11-23 devnull if(top == buf){
60 056fe1ba 2003-11-23 devnull werrstr("first two bytes must be in same packet fragment");
61 056fe1ba 2003-11-23 devnull packetfree(p);
62 6fc7da3c 2006-10-19 devnull vtfree(r);
63 056fe1ba 2003-11-23 devnull return nil;
64 056fe1ba 2003-11-23 devnull }
65 056fe1ba 2003-11-23 devnull top[1] = tag;
66 056fe1ba 2003-11-23 devnull qunlock(&z->lk);
67 6fc7da3c 2006-10-19 devnull if(vtsend(z, p) < 0){
68 6fc7da3c 2006-10-19 devnull vtfree(r);
69 056fe1ba 2003-11-23 devnull return nil;
70 6fc7da3c 2006-10-19 devnull }
71 056fe1ba 2003-11-23 devnull
72 056fe1ba 2003-11-23 devnull qlock(&z->lk);
73 056fe1ba 2003-11-23 devnull /* wait for the muxer to give us our packet */
74 056fe1ba 2003-11-23 devnull r->sleeping = 1;
75 056fe1ba 2003-11-23 devnull z->nsleep++;
76 056fe1ba 2003-11-23 devnull while(z->muxer && !r->done)
77 056fe1ba 2003-11-23 devnull rsleep(&r->r);
78 056fe1ba 2003-11-23 devnull z->nsleep--;
79 056fe1ba 2003-11-23 devnull r->sleeping = 0;
80 056fe1ba 2003-11-23 devnull
81 056fe1ba 2003-11-23 devnull /* if not done, there's no muxer: start muxing */
82 056fe1ba 2003-11-23 devnull if(!r->done){
83 056fe1ba 2003-11-23 devnull if(z->muxer)
84 056fe1ba 2003-11-23 devnull abort();
85 056fe1ba 2003-11-23 devnull z->muxer = 1;
86 056fe1ba 2003-11-23 devnull while(!r->done){
87 056fe1ba 2003-11-23 devnull qunlock(&z->lk);
88 056fe1ba 2003-11-23 devnull if((p = vtrecv(z)) == nil){
89 2e965b33 2004-05-05 devnull werrstr("unexpected eof on venti connection");
90 056fe1ba 2003-11-23 devnull z->muxer = 0;
91 6fc7da3c 2006-10-19 devnull vtfree(r);
92 056fe1ba 2003-11-23 devnull return nil;
93 056fe1ba 2003-11-23 devnull }
94 056fe1ba 2003-11-23 devnull qlock(&z->lk);
95 056fe1ba 2003-11-23 devnull muxrpc(z, p);
96 056fe1ba 2003-11-23 devnull }
97 056fe1ba 2003-11-23 devnull z->muxer = 0;
98 5ddc97fc 2005-02-14 devnull /* if there is anyone else sleeping, wake first unfinished to mux */
99 5ddc97fc 2005-02-14 devnull if(z->nsleep)
100 5ddc97fc 2005-02-14 devnull for(i=0; i<256; i++){
101 5ddc97fc 2005-02-14 devnull rr = z->wait[i];
102 5ddc97fc 2005-02-14 devnull if(rr && rr->sleeping && !rr->done){
103 5ddc97fc 2005-02-14 devnull rwakeup(&rr->r);
104 5ddc97fc 2005-02-14 devnull break;
105 5ddc97fc 2005-02-14 devnull }
106 5ddc97fc 2005-02-14 devnull }
107 056fe1ba 2003-11-23 devnull }
108 056fe1ba 2003-11-23 devnull
109 056fe1ba 2003-11-23 devnull p = r->p;
110 056fe1ba 2003-11-23 devnull puttag(z, r, tag);
111 056fe1ba 2003-11-23 devnull vtfree(r);
112 056fe1ba 2003-11-23 devnull qunlock(&z->lk);
113 056fe1ba 2003-11-23 devnull return p;
114 056fe1ba 2003-11-23 devnull }
115 056fe1ba 2003-11-23 devnull
116 7252036f 2005-11-02 devnull Packet*
117 7252036f 2005-11-02 devnull vtrpc(VtConn *z, Packet *p)
118 7252036f 2005-11-02 devnull {
119 7252036f 2005-11-02 devnull return _vtrpc(z, p, nil);
120 7252036f 2005-11-02 devnull }
121 7252036f 2005-11-02 devnull
122 056fe1ba 2003-11-23 devnull static int
123 056fe1ba 2003-11-23 devnull gettag(VtConn *z, Rwait *r)
124 056fe1ba 2003-11-23 devnull {
125 056fe1ba 2003-11-23 devnull int i;
126 056fe1ba 2003-11-23 devnull
127 056fe1ba 2003-11-23 devnull Again:
128 056fe1ba 2003-11-23 devnull while(z->ntag == 256)
129 056fe1ba 2003-11-23 devnull rsleep(&z->tagrend);
130 056fe1ba 2003-11-23 devnull for(i=0; i<256; i++)
131 056fe1ba 2003-11-23 devnull if(z->wait[i] == 0){
132 056fe1ba 2003-11-23 devnull z->ntag++;
133 056fe1ba 2003-11-23 devnull z->wait[i] = r;
134 056fe1ba 2003-11-23 devnull return i;
135 056fe1ba 2003-11-23 devnull }
136 056fe1ba 2003-11-23 devnull fprint(2, "libventi: ntag botch\n");
137 056fe1ba 2003-11-23 devnull goto Again;
138 056fe1ba 2003-11-23 devnull }
139 056fe1ba 2003-11-23 devnull
140 056fe1ba 2003-11-23 devnull static void
141 056fe1ba 2003-11-23 devnull puttag(VtConn *z, Rwait *r, int tag)
142 056fe1ba 2003-11-23 devnull {
143 056fe1ba 2003-11-23 devnull assert(z->wait[tag] == r);
144 056fe1ba 2003-11-23 devnull z->wait[tag] = nil;
145 056fe1ba 2003-11-23 devnull z->ntag--;
146 056fe1ba 2003-11-23 devnull rwakeup(&z->tagrend);
147 056fe1ba 2003-11-23 devnull }
148 056fe1ba 2003-11-23 devnull
149 056fe1ba 2003-11-23 devnull static void
150 056fe1ba 2003-11-23 devnull muxrpc(VtConn *z, Packet *p)
151 056fe1ba 2003-11-23 devnull {
152 056fe1ba 2003-11-23 devnull uchar tag, buf[2], *top;
153 056fe1ba 2003-11-23 devnull Rwait *r;
154 056fe1ba 2003-11-23 devnull
155 056fe1ba 2003-11-23 devnull if((top = packetpeek(p, buf, 0, 2)) == nil){
156 056fe1ba 2003-11-23 devnull fprint(2, "libventi: short packet in vtrpc\n");
157 056fe1ba 2003-11-23 devnull packetfree(p);
158 056fe1ba 2003-11-23 devnull return;
159 056fe1ba 2003-11-23 devnull }
160 056fe1ba 2003-11-23 devnull
161 056fe1ba 2003-11-23 devnull tag = top[1];
162 056fe1ba 2003-11-23 devnull if((r = z->wait[tag]) == nil){
163 056fe1ba 2003-11-23 devnull fprint(2, "libventi: unexpected packet tag %d in vtrpc\n", tag);
164 056fe1ba 2003-11-23 devnull abort();
165 056fe1ba 2003-11-23 devnull packetfree(p);
166 056fe1ba 2003-11-23 devnull return;
167 056fe1ba 2003-11-23 devnull }
168 056fe1ba 2003-11-23 devnull
169 056fe1ba 2003-11-23 devnull r->p = p;
170 056fe1ba 2003-11-23 devnull r->done = 1;
171 056fe1ba 2003-11-23 devnull rwakeup(&r->r);
172 056fe1ba 2003-11-23 devnull }
173 056fe1ba 2003-11-23 devnull