aboutsummaryrefslogtreecommitdiff
path: root/lib/libvirtio/virtio-net.c
blob: fc620a201b27199f65a8344528771922864057fe (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/******************************************************************************
 * Copyright (c) 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

/*
 * This is the implementation for the Virtio network device driver. Details
 * about the virtio-net interface can be found in Rusty Russel's "Virtio PCI
 * Card Specification v0.8.10", appendix C, which can be found here:
 *
 *        http://ozlabs.org/~rusty/virtio-spec/virtio-spec.pdf
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <helpers.h>
#include <cache.h>
#include <byteorder.h>
#include "virtio.h"
#include "virtio-net.h"
#include "virtio-internal.h"

#undef DEBUG
//#define DEBUG
#ifdef DEBUG
# define dprintf(fmt...) do { printf(fmt); } while(0)
#else
# define dprintf(fmt...)
#endif

#define sync()  asm volatile (" sync \n" ::: "memory")

#define DRIVER_FEATURE_SUPPORT  (VIRTIO_NET_F_MAC | VIRTIO_F_VERSION_1)

struct virtio_device virtiodev;
static struct vqs vq_rx;     /* Information about receive virtqueues */
static struct vqs vq_tx;     /* Information about transmit virtqueues */

/* See Virtio Spec, appendix C, "Device Operation" */
struct virtio_net_hdr {
	uint8_t  flags;
	uint8_t  gso_type;
	uint16_t  hdr_len;
	uint16_t  gso_size;
	uint16_t  csum_start;
	uint16_t  csum_offset;
	// uint16_t  num_buffers;	/* Only if VIRTIO_NET_F_MRG_RXBUF */
};

static unsigned int net_hdr_size;

struct virtio_net_hdr_v1 {
	uint8_t  flags;
	uint8_t  gso_type;
	le16  hdr_len;
	le16  gso_size;
	le16  csum_start;
	le16  csum_offset;
	le16  num_buffers;
};

static uint16_t last_rx_idx;	/* Last index in RX "used" ring */

/**
 * Module init for virtio via PCI.
 * Checks whether we're reponsible for the given device and set up
 * the virtqueue configuration.
 */
static int virtionet_init_pci(struct virtio_device *dev)
{
	dprintf("virtionet: doing virtionet_init_pci!\n");

	if (!dev)
		return -1;

	/* make a copy of the device structure */
	memcpy(&virtiodev, dev, sizeof(struct virtio_device));

	/* Reset device */
	virtio_reset_device(&virtiodev);

	/* The queue information can be retrieved via the virtio header that
	 * can be found in the I/O BAR. First queue is the receive queue,
	 * second the transmit queue, and the forth is the control queue for
	 * networking options.
	 * We are only interested in the receive and transmit queue here. */
	if (virtio_queue_init_vq(dev, &vq_rx, VQ_RX) ||
	    virtio_queue_init_vq(dev, &vq_tx, VQ_TX)) {
		virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER
				  |VIRTIO_STAT_FAILED);
		return -1;
	}

	/* Acknowledge device. */
	virtio_set_status(&virtiodev, VIRTIO_STAT_ACKNOWLEDGE);

	return 0;
}

/**
 * Initialize the virtio-net device.
 * See the Virtio Spec, chapter 2.2.1 and Appendix C "Device Initialization"
 * for details.
 */
static int virtionet_init(net_driver_t *driver)
{
	int i;
	int status = VIRTIO_STAT_ACKNOWLEDGE | VIRTIO_STAT_DRIVER;

	dprintf("virtionet_init(%02x:%02x:%02x:%02x:%02x:%02x)\n",
		driver->mac_addr[0], driver->mac_addr[1],
		driver->mac_addr[2], driver->mac_addr[3],
		driver->mac_addr[4], driver->mac_addr[5]);

	if (driver->running != 0)
		return 0;

	/* Tell HV that we know how to drive the device. */
	virtio_set_status(&virtiodev, status);

	/* Device specific setup */
	if (virtiodev.is_modern) {
		if (virtio_negotiate_guest_features(&virtiodev, DRIVER_FEATURE_SUPPORT))
			goto dev_error;
		net_hdr_size = sizeof(struct virtio_net_hdr_v1);
		virtio_get_status(&virtiodev, &status);
	} else {
		net_hdr_size = sizeof(struct virtio_net_hdr);
		virtio_set_guest_features(&virtiodev,  0);
	}

	/* Allocate memory for one transmit an multiple receive buffers */
	vq_rx.buf_mem = SLOF_alloc_mem((BUFFER_ENTRY_SIZE+net_hdr_size)
				   * RX_QUEUE_SIZE);
	if (!vq_rx.buf_mem) {
		printf("virtionet: Failed to allocate buffers!\n");
		goto dev_error;
	}

	/* Prepare receive buffer queue */
	for (i = 0; i < RX_QUEUE_SIZE; i++) {
		uint64_t addr = (uint64_t)vq_rx.buf_mem
			+ i * (BUFFER_ENTRY_SIZE+net_hdr_size);
		uint32_t id = i*2;
		/* Descriptor for net_hdr: */
		virtio_fill_desc(&vq_rx.desc[id], virtiodev.is_modern, addr, net_hdr_size,
				 VRING_DESC_F_NEXT | VRING_DESC_F_WRITE, id + 1);

		/* Descriptor for data: */
		virtio_fill_desc(&vq_rx.desc[id+1], virtiodev.is_modern, addr + net_hdr_size,
				 BUFFER_ENTRY_SIZE, VRING_DESC_F_WRITE, 0);

		vq_rx.avail->ring[i] = virtio_cpu_to_modern16(&virtiodev, id);
	}
	sync();

	vq_rx.avail->flags = virtio_cpu_to_modern16(&virtiodev, VRING_AVAIL_F_NO_INTERRUPT);
	vq_rx.avail->idx = virtio_cpu_to_modern16(&virtiodev, RX_QUEUE_SIZE);

	last_rx_idx = virtio_modern16_to_cpu(&virtiodev, vq_rx.used->idx);

	vq_tx.avail->flags = virtio_cpu_to_modern16(&virtiodev, VRING_AVAIL_F_NO_INTERRUPT);
	vq_tx.avail->idx = 0;

	/* Tell HV that setup succeeded */
	status |= VIRTIO_STAT_DRIVER_OK;
	virtio_set_status(&virtiodev, status);

	/* Tell HV that RX queues are ready */
	virtio_queue_notify(&virtiodev, VQ_RX);

	driver->running = 1;
	for(i = 0; i < (int)sizeof(driver->mac_addr); i++) {
		driver->mac_addr[i] = virtio_get_config(&virtiodev, i, 1);
	}
	return 0;

dev_error:
	status |= VIRTIO_STAT_FAILED;
	virtio_set_status(&virtiodev, status);
	return -1;
}


/**
 * Shutdown driver.
 * We've got to make sure that the hosts stops all transfers since the buffers
 * in our main memory will become invalid after this module has been terminated.
 */
static int virtionet_term(net_driver_t *driver)
{
	dprintf("virtionet_term()\n");

	if (driver->running == 0)
		return 0;

	/* Quiesce device */
	virtio_set_status(&virtiodev, VIRTIO_STAT_FAILED);

	/* Reset device */
	virtio_reset_device(&virtiodev);

	driver->running = 0;

	return 0;
}


/**
 * Transmit a packet
 */
static int virtionet_xmit(char *buf, int len)
{
	int id, idx;
	static struct virtio_net_hdr_v1 nethdr_v1;
	static struct virtio_net_hdr nethdr_legacy;
	void *nethdr = &nethdr_legacy;

	if (len > BUFFER_ENTRY_SIZE) {
		printf("virtionet: Packet too big!\n");
		return 0;
	}

	dprintf("\nvirtionet_xmit(packet at %p, %d bytes)\n", buf, len);

	if (virtiodev.is_modern)
		nethdr = &nethdr_v1;

	memset(nethdr, 0, net_hdr_size);

	/* Determine descriptor index */
	idx = virtio_modern16_to_cpu(&virtiodev, vq_tx.avail->idx);
	id = (idx * 2) % vq_tx.size;

	/* Set up virtqueue descriptor for header */
	virtio_fill_desc(&vq_tx.desc[id], virtiodev.is_modern, (uint64_t)nethdr,
			 net_hdr_size, VRING_DESC_F_NEXT, id + 1);

	/* Set up virtqueue descriptor for data */
	virtio_fill_desc(&vq_tx.desc[id+1], virtiodev.is_modern, (uint64_t)buf, len, 0, 0);

	vq_tx.avail->ring[idx % vq_tx.size] = virtio_cpu_to_modern16(&virtiodev, id);
	sync();
	vq_tx.avail->idx = virtio_cpu_to_modern16(&virtiodev, idx + 1);
	sync();

	/* Tell HV that TX queue is ready */
	virtio_queue_notify(&virtiodev, VQ_TX);

	return len;
}


/**
 * Receive a packet
 */
static int virtionet_receive(char *buf, int maxlen)
{
	uint32_t len = 0;
	uint32_t id, idx;

	idx = virtio_modern16_to_cpu(&virtiodev, vq_rx.used->idx);

	if (last_rx_idx == idx) {
		/* Nothing received yet */
		return 0;
	}

	id = (virtio_modern32_to_cpu(&virtiodev, vq_rx.used->ring[last_rx_idx % vq_rx.size].id) + 1)
		% vq_rx.size;
	len = virtio_modern32_to_cpu(&virtiodev, vq_rx.used->ring[last_rx_idx % vq_rx.size].len)
		- net_hdr_size;
	dprintf("virtionet_receive() last_rx_idx=%i, vq_rx.used->idx=%i,"
		" id=%i len=%i\n", last_rx_idx, vq_rx.used->idx, id, len);

	if (len > (uint32_t)maxlen) {
		printf("virtio-net: Receive buffer not big enough!\n");
		len = maxlen;
	}

#if 0
	/* Dump packet */
	printf("\n");
	int i;
	for (i=0; i<64; i++) {
		printf(" %02x", *(uint8_t*)(vq_rx.desc[id].addr+i));
		if ((i%16)==15)
			printf("\n");
	}
	prinfk("\n");
#endif

	/* Copy data to destination buffer */
	memcpy(buf, (void *)virtio_modern64_to_cpu(&virtiodev, vq_rx.desc[id].addr), len);

	/* Move indices to next entries */
	last_rx_idx = last_rx_idx + 1;

	vq_rx.avail->ring[idx % vq_rx.size] = virtio_cpu_to_modern16(&virtiodev, id - 1);
	sync();
	vq_rx.avail->idx = virtio_cpu_to_modern16(&virtiodev, idx + 1);

	/* Tell HV that RX queue entry is ready */
	virtio_queue_notify(&virtiodev, VQ_RX);

	return len;
}

net_driver_t *virtionet_open(struct virtio_device *dev)
{
	net_driver_t *driver;

	driver = SLOF_alloc_mem(sizeof(*driver));
	if (!driver) {
		printf("Unable to allocate virtio-net driver\n");
		return NULL;
	}

	driver->running = 0;

	if (virtionet_init_pci(dev))
		goto FAIL;

	if (virtionet_init(driver))
		goto FAIL;

	return driver;

FAIL:	SLOF_free_mem(driver, sizeof(*driver));
	return NULL;
}

void virtionet_close(net_driver_t *driver)
{
	if (driver) {
		virtionet_term(driver);
		SLOF_free_mem(driver, sizeof(*driver));
	}
}

int virtionet_read(char *buf, int len)
{
	if (buf)
		return virtionet_receive(buf, len);
	return -1;
}

int virtionet_write(char *buf, int len)
{
	if (buf)
		return virtionet_xmit(buf, len);
	return -1;
}