summaryrefslogtreecommitdiff
path: root/net/vnic.c
blob: 8813de0fb9248111d66b9e8108d002187c1a823a (plain)
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
/*
 * QEMU System Emulator
 * Solaris VNIC support
 *
 * Copyright (c) 2011 Joyent, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <assert.h>
#include <errno.h>
#include <libdlpi.h>
#include <string.h>
#include <stdio.h>
#include <stropts.h>
#include <unistd.h>

#include <net/if_dl.h>
#include <sys/ethernet.h>
#include <sys/dlpi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "net/vnic.h"
#include "net/vnic-dhcp.h"

#include "qemu-common.h"
#include "qemu-error.h"
#include "qemu-option.h"
#include "qemu-char.h"

/*
 * XXX We should determine a good way to get this buffer size. 64k feels like
 * such an arbitrary number...
 */
#define	VNIC_BUFFSIZE	65536

typedef struct VNICState {
	VLANClientState	vns_nc;
	int		vns_fd;
	unsigned int	vns_rpoll;
	unsigned int	vns_wpoll;
	uint8_t		vns_buf[VNIC_BUFFSIZE];
	uint_t		vns_sap;
	dlpi_handle_t	vns_hdl;
	VNICDHCPState	vns_ds;
} VNICState;

static void vnic_update_fd_handler(VNICState *);

static void
vnic_read_poll(VNICState *vsp, int enable)
{
	vsp->vns_rpoll = enable;
	vnic_update_fd_handler(vsp);
}

static void
vnic_write_poll(VNICState *vsp, int enable)
{
	vsp->vns_wpoll = enable;
	vnic_update_fd_handler(vsp);
}

static void
vnic_poll(VLANClientState *ncp, bool enable)
{
	VNICState *vsp = DO_UPCAST(VNICState, vns_nc, ncp);
	vnic_read_poll(vsp, 1);
	vnic_write_poll(vsp, 1);
}

static int
vnic_read_packet(VNICState *vsp, uint8_t *buf, int len)
{
	struct strbuf sbuf;
	int flags, ret;

	flags = 0;
	sbuf.maxlen = len;
	sbuf.buf = (char *)buf;

	do {
		ret = getmsg(vsp->vns_fd, NULL, &sbuf, &flags);
	} while (ret == -1 && errno == EINTR);

	if (ret == -1 && errno == EAGAIN) {
		vnic_write_poll(vsp, 1);
		return (0);
	}

	if (ret == -1) {
		return (-1);
	}

	return (sbuf.len);
}

static int
vnic_write_packet(VNICState *vsp, const uint8_t *buf, int len)
{
	struct strbuf sbuf;
	int flags, ret;

	flags = 0;
	sbuf.len = len;
	sbuf.buf = (char *)buf;

	do {
		ret = putmsg(vsp->vns_fd, NULL, &sbuf, flags);
	} while (ret == -1 && errno == EINTR);

	if (ret == -1 && errno == EAGAIN) {
		vnic_write_poll(vsp, 1);
		return (0);
	}

	if (ret == -1)
		return (-1);

	return (len);
}

static int
vnic_can_send(void *opaque)
{
	VNICState *vsp = opaque;
	return (qemu_can_send_packet(&vsp->vns_nc));
}

static void
vnic_send_completed(VLANClientState *nc, ssize_t len)
{
	VNICState *vsp = DO_UPCAST(VNICState, vns_nc, nc);
	vnic_read_poll(vsp, 1);
}

/* outside world -> VM */
static void
vnic_send(void *opaque)
{
	VNICState *vsp = opaque;
	int ret;

	do {
		ret = vnic_read_packet(vsp, vsp->vns_buf,
		    sizeof (vsp->vns_buf));
		if (ret <= 0)
			break;

		ret = qemu_send_packet_async(&vsp->vns_nc, vsp->vns_buf, ret,
		    vnic_send_completed);

		if (ret == 0)
			vnic_read_poll(vsp, 0);

	} while (ret > 0 && qemu_can_send_packet(&vsp->vns_nc));
}

static void
vnic_writable(void *opaque)
{
	VNICState *vsp = opaque;
	vnic_write_poll(vsp, 0);
	qemu_flush_queued_packets(&vsp->vns_nc);
}

/* VM -> outside world */
static ssize_t
vnic_receive(VLANClientState *ncp, const uint8_t *buf, size_t size)
{
	VNICState *vsp = DO_UPCAST(VNICState, vns_nc, ncp);

#if VNIC_DHCP_DEBUG
	debug_eth_frame(buf, size);
#endif

	if (vsp->vns_ds.vnds_enabled && is_dhcp_request(buf, size)) {
		int ret;

		// XXX: do we need to handle arp requests for the fake IP?
		ret = create_dhcp_response(buf, size, &vsp->vns_ds);
		if (!ret)
			return size;

		ret = qemu_send_packet_async(&vsp->vns_nc,
		    vsp->vns_ds.vnds_buf, ret, vnic_send_completed);
		if (ret == 0)
			vnic_read_poll(vsp, 0);

		return size;
	}

	return (vnic_write_packet(vsp, buf, size));
}

static void
vnic_cleanup(VLANClientState *ncp)
{
	VNICState *vsp;

	vsp = DO_UPCAST(VNICState, vns_nc, ncp);

	qemu_purge_queued_packets(ncp);

	dlpi_close(vsp->vns_hdl);
}

static void
vnic_update_fd_handler(VNICState *vsp)
{
	qemu_set_fd_handler2(vsp->vns_fd,
	    vsp->vns_rpoll ? vnic_can_send : NULL,
	    vsp->vns_rpoll ? vnic_send : NULL,
	    vsp->vns_wpoll ? vnic_writable : NULL,
	    vsp);
}

static NetClientInfo net_vnic_info = {
	.type = NET_CLIENT_TYPE_VNIC,
	.size = sizeof (VNICState),
	.receive = vnic_receive,
	.poll = vnic_poll,
	.cleanup = vnic_cleanup
};

#ifdef CONFIG_SUNOS_VNIC_KVM
static int
net_init_kvm(int vfd)
{
	int kfd;

	if ((kfd = open("/dev/kvm", O_RDWR)) < 0) {
		error_report("can't open /dev/kvm for vnic: %s\n",
		    strerror(errno));
		return (-1);
	}

	/* XXX We shouldn't be embedding the KVM_NET_QUEUE fd */
	if (ioctl(kfd, 0x2000ae21, vfd) < 0) {
		error_report("can't ioctl: %s\n", strerror(errno));
		return (-1);
	}

	(void) close(kfd);

	return (0);
}
#endif

int
net_init_vnic(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
{
	int fd, len;
	const char *ifname, *mac;
	uchar_t *macaddr;
	VLANClientState *ncp;
	VNICState *vsp;

	if ((ifname = qemu_opt_get(opts, "ifname")) == NULL) {
		error_report("missing ifname required for vnic\n");
		return (-1);
	}

	mac = qemu_opt_get(opts, "macaddr");

	if (mac != NULL) {
		macaddr = _link_aton(mac, &len);
		if (macaddr == NULL || len != ETHERADDRL) {
			error_report("invalid macaddr for vnic: %s\n", mac);
			return (-1);
		}
	}

	ncp = qemu_new_net_client(&net_vnic_info, vlan, NULL, "vnic", name);
	vsp = DO_UPCAST(VNICState, vns_nc, ncp);

	if (dlpi_open(ifname, &vsp->vns_hdl, DLPI_RAW) != DLPI_SUCCESS) {
		error_report("vnic: failed to open interface %s", ifname);
		return (-1);
	}

	if (dlpi_bind(vsp->vns_hdl, DLPI_ANY_SAP, &vsp->vns_sap) != DLPI_SUCCESS) {
		error_report("vnic: failed to bind interface %s", ifname);
		return (-1);
	}

	/*
	 * We only set the mac address of the vnic if the user passed in the
	 * option on the command line.
	 */
	if (mac != NULL) {
		if (dlpi_set_physaddr(vsp->vns_hdl, DL_CURR_PHYS_ADDR, macaddr,
		    ETHERADDRL) != DLPI_SUCCESS) {
			error_report("vnic: failed to set mac address\n");
			return (-1);
		}
	}

	if (dlpi_promiscon(vsp->vns_hdl, DL_PROMISC_SAP) != DLPI_SUCCESS) {
		error_report("vnic: failed to be promiscous with interface %s",
		    ifname);
		return (-1);
	}

	fd = dlpi_fd(vsp->vns_hdl);

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
		error_report("vnic: failed to set fd on interface %s to "
		    "non-blocking: %s\n", ifname, strerror(errno));
		return (-1);
	}

	vsp->vns_fd = fd;

	snprintf(vsp->vns_nc.info_str, sizeof (vsp->vns_nc.info_str), "ifname=%s",
	    qemu_opt_get(opts, "ifname"));

	if (vnic_dhcp_init(&vsp->vns_ds, opts) == 0)
		return (-1);

#ifdef CONFIG_SUNOS_VNIC_KVM
	net_init_kvm(fd);
#endif

	/* We have to manually intialize the polling for read */
	vnic_read_poll(vsp, 1);

	return (0);
}