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
|
/* $NetBSD: virtif_user.c,v 1.6 2019/03/26 08:56:17 bad Exp $ */
/*
* Copyright (c) 2013 Antti Kantee. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifdef __KERNEL_RCSID
__KERNEL_RCSID(0, "$NetBSD: virtif_user.c,v 1.6 2019/03/26 08:56:17 bad Exp $");
#endif
#ifndef _KERNEL
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __linux__
#include <net/if.h>
#include <linux/if_tun.h>
#endif
#include <rump/rumpuser_component.h>
#include "if_virt.h"
#include "virtif_user.h"
#if VIFHYPER_REVISION != 20140313
#error VIFHYPER_REVISION mismatch
#endif
struct virtif_user {
struct virtif_sc *viu_virtifsc;
int viu_devnum;
int viu_fd;
int viu_pipe[2];
pthread_t viu_rcvthr;
int viu_dying;
char viu_rcvbuf[9018]; /* jumbo frame max len */
};
static int
opentapdev(int devnum)
{
int fd = -1;
#if defined(__NetBSD__) || defined(__DragonFly__)
char tapdev[64];
snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", devnum);
fd = open(tapdev, O_RDWR);
if (fd == -1) {
fprintf(stderr, "rumpcomp_virtif_create: can't open %s: "
"%s\n", tapdev, strerror(errno));
}
#elif defined(__linux__)
struct ifreq ifr;
char devname[16];
fd = open("/dev/net/tun", O_RDWR);
if (fd == -1) {
fprintf(stderr, "rumpcomp_virtif_create: can't open %s: "
"%s\n", "/dev/net/tun", strerror(errno));
return -1;
}
snprintf(devname, sizeof(devname), "tun%d", devnum);
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name)-1);
if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
close(fd);
fd = -1;
}
#else
fprintf(stderr, "virtif not supported on this platform\n");
#endif
return fd;
}
static void
closetapdev(struct virtif_user *viu)
{
close(viu->viu_fd);
}
static void *
rcvthread(void *aaargh)
{
struct virtif_user *viu = aaargh;
struct pollfd pfd[2];
struct iovec iov;
ssize_t nn = 0;
int prv;
rumpuser_component_kthread();
pfd[0].fd = viu->viu_fd;
pfd[0].events = POLLIN;
pfd[1].fd = viu->viu_pipe[0];
pfd[1].events = POLLIN;
while (!viu->viu_dying) {
prv = poll(pfd, 2, -1);
if (prv == 0)
continue;
if (prv == -1) {
/* XXX */
fprintf(stderr, "virt%d: poll error: %d\n",
viu->viu_devnum, errno);
sleep(1);
continue;
}
if (pfd[1].revents & POLLIN)
continue;
nn = read(viu->viu_fd,
viu->viu_rcvbuf, sizeof(viu->viu_rcvbuf));
if (nn == -1 && errno == EAGAIN)
continue;
if (nn < 1) {
/* XXX */
fprintf(stderr, "virt%d: receive failed\n",
viu->viu_devnum);
sleep(1);
continue;
}
iov.iov_base = viu->viu_rcvbuf;
iov.iov_len = nn;
rumpuser_component_schedule(NULL);
VIF_DELIVERPKT(viu->viu_virtifsc, &iov, 1);
rumpuser_component_unschedule();
}
assert(viu->viu_dying);
rumpuser_component_kthread_release();
return NULL;
}
int
VIFHYPER_CREATE(const char *devstr, struct virtif_sc *vif_sc, uint8_t *enaddr,
struct virtif_user **viup)
{
struct virtif_user *viu = NULL;
void *cookie;
int devnum;
int rv;
cookie = rumpuser_component_unschedule();
/*
* Since this interface doesn't do LINKSTR, we know devstr to be
* well-formatted.
*/
devnum = atoi(devstr);
viu = calloc(1, sizeof(*viu));
if (viu == NULL) {
rv = errno;
goto oerr1;
}
viu->viu_virtifsc = vif_sc;
viu->viu_fd = opentapdev(devnum);
if (viu->viu_fd == -1) {
rv = errno;
goto oerr2;
}
viu->viu_devnum = devnum;
if (pipe(viu->viu_pipe) == -1) {
rv = errno;
goto oerr3;
}
if ((rv = pthread_create(&viu->viu_rcvthr, NULL, rcvthread, viu)) != 0)
goto oerr4;
rumpuser_component_schedule(cookie);
*viup = viu;
return 0;
oerr4:
close(viu->viu_pipe[0]);
close(viu->viu_pipe[1]);
oerr3:
closetapdev(viu);
oerr2:
free(viu);
oerr1:
rumpuser_component_schedule(cookie);
return rumpuser_component_errtrans(rv);
}
void
VIFHYPER_SEND(struct virtif_user *viu,
struct iovec *iov, size_t iovlen)
{
void *cookie = rumpuser_component_unschedule();
ssize_t idontcare __attribute__((__unused__));
/*
* no need to check for return value; packets may be dropped
*
* ... sorry, I spoke too soon. We need to check it because
* apparently gcc reinvented const poisoning and it's very
* hard to say "thanks, I know I'm not using the result,
* but please STFU and let's get on with something useful".
* So let's trick gcc into letting us share the compiler
* experience.
*/
idontcare = writev(viu->viu_fd, iov, iovlen);
rumpuser_component_schedule(cookie);
}
int
VIFHYPER_DYING(struct virtif_user *viu)
{
void *cookie = rumpuser_component_unschedule();
viu->viu_dying = 1;
if (write(viu->viu_pipe[1],
&viu->viu_dying, sizeof(viu->viu_dying)) == -1) {
/*
* this is here mostly to avoid a compiler warning
* about ignoring the return value of write()
*/
fprintf(stderr, "%s: failed to signal thread\n",
VIF_STRING(VIFHYPER_DYING));
}
rumpuser_component_schedule(cookie);
return 0;
}
void
VIFHYPER_DESTROY(struct virtif_user *viu)
{
void *cookie = rumpuser_component_unschedule();
pthread_join(viu->viu_rcvthr, NULL);
closetapdev(viu);
close(viu->viu_pipe[0]);
close(viu->viu_pipe[1]);
free(viu);
rumpuser_component_schedule(cookie);
}
#endif
|