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
|
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"
#include "ip.h"
static void netdevbind(Ipifc *ifc, int argc, char **argv);
static void netdevunbind(Ipifc *ifc);
static void netdevbwrite(Ipifc *ifc, Block *bp, int version, uchar *ip);
static void netdevread(void *a);
typedef struct Netdevrock Netdevrock;
struct Netdevrock
{
Fs *f; /* file system we belong to */
Proc *readp; /* reading process */
Chan *mchan; /* Data channel */
};
Medium netdevmedium =
{
.name= "netdev",
.hsize= 0,
.mintu= 0,
.maxtu= 64000,
.maclen= 0,
.bind= netdevbind,
.unbind= netdevunbind,
.bwrite= netdevbwrite,
.unbindonclose= 0,
};
/*
* called to bind an IP ifc to a generic network device
* called with ifc qlock'd
*/
static void
netdevbind(Ipifc *ifc, int argc, char **argv)
{
Chan *mchan;
Netdevrock *er;
if(argc < 2)
error(Ebadarg);
mchan = namec(argv[2], Aopen, ORDWR, 0);
er = smalloc(sizeof(*er));
er->mchan = mchan;
er->f = ifc->conv->p->f;
ifc->arg = er;
kproc("netdevread", netdevread, ifc);
}
/*
* called with ifc wlock'd
*/
static void
netdevunbind(Ipifc *ifc)
{
Netdevrock *er = ifc->arg;
if(er->readp != nil)
postnote(er->readp, 1, "unbind", 0);
/* wait for readers to die */
while(er->readp != nil)
tsleep(&up->sleep, return0, 0, 300);
if(er->mchan != nil)
cclose(er->mchan);
free(er);
}
/*
* called by ipoput with a single block to write
*/
static void
netdevbwrite(Ipifc *ifc, Block *bp, int, uchar*)
{
Netdevrock *er = ifc->arg;
if(BLEN(bp) < ifc->mintu)
bp = adjustblock(bp, ifc->mintu);
devtab[er->mchan->type]->bwrite(er->mchan, bp, 0);
ifc->out++;
}
/*
* process to read from the device
*/
static void
netdevread(void *a)
{
Ipifc *ifc;
Block *bp;
Netdevrock *er;
char *argv[1];
ifc = a;
er = ifc->arg;
er->readp = up; /* hide identity under a rock for unbind */
if(waserror()){
er->readp = nil;
pexit("hangup", 1);
}
for(;;){
bp = devtab[er->mchan->type]->bread(er->mchan, ifc->maxtu, 0);
if(bp == nil){
/*
* get here if mchan is a pipe and other side hangs up
* clean up this interface & get out
ZZZ is this a good idea?
*/
poperror();
er->readp = nil;
argv[0] = "unbind";
if(!waserror())
ifc->conv->p->ctl(ifc->conv, argv, 1);
pexit("hangup", 1);
}
if(!canrlock(ifc)){
freeb(bp);
continue;
}
if(waserror()){
runlock(ifc);
nexterror();
}
ifc->in++;
if(ifc->lifc == nil)
freeb(bp);
else
ipiput4(er->f, ifc, bp);
runlock(ifc);
poperror();
}
}
void
netdevmediumlink(void)
{
addipmedium(&netdevmedium);
}
|