public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 38d5d8eb4fa3dc935da578533d17779f86165a35 8410 bytes (raw)

  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
 
// SPDX-License-Identifier: GPL-2.0-or-later
/* Copyright Red Hat
 * Author: Laurent Vivier <lvivier@redhat.com>
 *
 * common_vu.c - vhost-user common UDP and TCP functions
 */

#include <unistd.h>
#include <sys/uio.h>
#include <sys/eventfd.h>
#include <linux/virtio_net.h>

#include "util.h"
#include "passt.h"
#include "tap.h"
#include "vhost_user.h"
#include "pcap.h"
#include "vu_common.h"

/**
 * vu_init_elem() - initialize an array of virtqueue element with 1 iov in each
 * @elem:	Array of virtqueue element to initialize
 * @iov:	Array of iovec to assign to virtqueue element
 * @elem_cnt:	Number of virtqueue element
 */
void vu_init_elem(struct vu_virtq_element *elem, struct iovec *iov, int elem_cnt)
{
	int i;

	for (i = 0; i < elem_cnt; i++) {
		elem[i].out_num = 0;
		elem[i].out_sg = NULL;
		elem[i].in_num = 1;
		elem[i].in_sg = &iov[i];
	}
}

/**
 * vu_collect_one_frame() - collect virtio buffers from a given virtqueue for
 *			    one frame
 * @vdev:		vhost-user device
 * @vq:			virtqueue to collect from
 * @elem:		Array of virtqueue element
 * 			each element must be initialized with one iovec entry
 * 			in the in_sg array.
 * @max_elem:		Number of virtqueue element in the array
 * @size:		Maximum size of the data in the frame
 * @hdrlen:		Size of the frame header
 */
int vu_collect_one_frame(struct vu_dev *vdev, struct vu_virtq *vq,
			 struct vu_virtq_element *elem, int max_elem,
			 size_t size, size_t hdrlen)
{
	return vu_collect(vdev, vq, elem, max_elem, size, hdrlen, size);
}

/**
 * vu_collect() - collect virtio buffers from a given virtqueue
 * @vdev:		vhost-user device
 * @vq:			virtqueue to collect from
 * @elem:		Array of virtqueue element
 * 			each element must be initialized with one iovec entry
 * 			in the in_sg array.
 * @max_elem:		Number of virtqueue element in the array
 * @max_frame_size:	Maximum size of the data in the frame
 * @hdrlen:		Size of the frame header
 * @size:		Total size of the buffers we need to collect
 * 			(if size > max_frame_size, we collect several frame)
 */
int vu_collect(struct vu_dev *vdev, struct vu_virtq *vq,
	       struct vu_virtq_element *elem, int max_elem,
	       size_t max_frame_size, size_t hdrlen, size_t size)
{
	size_t frame_size = 0;
	int elem_cnt = 0;

	/* header is at least virtio_net_hdr_mrg_rxbuf */
	hdrlen += sizeof(struct virtio_net_hdr_mrg_rxbuf);

	while (size > 0 && elem_cnt < max_elem) {
		struct iovec *iov;
		int ret;

		ret = vu_queue_pop(vdev, vq, &elem[elem_cnt]);
		if (ret < 0)
			break;

		if (elem[elem_cnt].in_num < 1) {
			warn("virtio-net receive queue contains no in buffers");
			vu_queue_detach_element(vq);
			break;
		}

		iov = &elem[elem_cnt].in_sg[0];

		ASSERT(iov->iov_len >= hdrlen);

		if (frame_size == 0) {
			/* this is the iovec that will contain frame headers */
			iov->iov_base = (char *)iov->iov_base + hdrlen;
			iov->iov_len -= hdrlen;
		}

		if (iov->iov_len > size)
			iov->iov_len = size;

		frame_size += iov->iov_len;

		if (frame_size >= max_frame_size) {
			/* if the frame size is greater than maximum size
			 * we need to start a new frame
			 */
			iov->iov_len -= frame_size - max_frame_size;
			frame_size = 0;
		} else if (!vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
			/* if we don't have feature F_MRG_RXBUF,
			 * the frame cannot be spread over several
			 * virtqueue element
			 */
			frame_size = 0;
		}

		size -= iov->iov_len;
		elem_cnt++;
	}

	return elem_cnt;
}

/**
 * vu_set_vnethdr() - set virtio-net headers in a given iovec
 * @vdev:		vhost-user device
 * @iov:		One iovec to initialize
 * @num_buffers:	Number of guest buffers of the frame
 * @hdrlen:		Size of the frame header
 */
void vu_set_vnethdr(const struct vu_dev *vdev, struct iovec *iov,
		    int num_buffers, size_t hdrlen)
{
	struct virtio_net_hdr_mrg_rxbuf *vnethdr;

	/* header is at least virtio_net_hdr_mrg_rxbuf */
	hdrlen += sizeof(struct virtio_net_hdr_mrg_rxbuf);

	/* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
	iov->iov_base = (char *)iov->iov_base - hdrlen;
	iov->iov_len += hdrlen;

	vnethdr = iov->iov_base;
	vnethdr->hdr = VU_HEADER;
	if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
		vnethdr->num_buffers = htole16(num_buffers);
}

/**
 * vu_flush() - flush all the collected buffers to the vhost-user interface
 * @vdev:	vhost-user device
 * @vq:		vhost-user virtqueue
 * @elem:	virtqueue element array to send back to the virqueue
 * @iov_used:	Length of the array
 */
void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq,
	      struct vu_virtq_element *elem, int elem_cnt)
{
	int i;

	for (i = 0; i < elem_cnt; i++)
		vu_queue_fill(vq, &elem[i], elem[i].in_sg[0].iov_len, i);

	vu_queue_flush(vq, elem_cnt);
	vu_queue_notify(vdev, vq);
}

/**
 * vu_handle_tx() - Receive data from the TX virtqueue
 * @vdev:	vhost-user device
 * @index:	index of the virtqueue
 * @now:	Current timestamp
 */
static void vu_handle_tx(struct vu_dev *vdev, int index,
			 const struct timespec *now)
{
	struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
	struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
	struct vu_virtq *vq = &vdev->vq[index];
	int hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
	int out_sg_count;
	int count;

	if (!VHOST_USER_IS_QUEUE_TX(index)) {
		debug("vhost-user: index %d is not a TX queue", index);
		return;
	}

	tap_flush_pools();

	count = 0;
	out_sg_count = 0;
	while (count < VIRTQUEUE_MAX_SIZE) {
		int ret;

		elem[count].out_num = 1;
		elem[count].out_sg = &out_sg[out_sg_count];
		elem[count].in_num = 0;
		elem[count].in_sg = NULL;
		ret = vu_queue_pop(vdev, vq, &elem[count]);
		if (ret < 0)
			break;
		out_sg_count += elem[count].out_num;

		if (elem[count].out_num < 1) {
			debug("virtio-net header not in first element");
			break;
		}
		ASSERT(elem[count].out_num == 1);

		tap_add_packet(vdev->context,
			       elem[count].out_sg[0].iov_len - hdrlen,
			       (char *)elem[count].out_sg[0].iov_base + hdrlen);
		count++;
	}
	tap_handler(vdev->context, now);

	if (count) {
		int i;

		for (i = 0; i < count; i++)
			vu_queue_fill(vq, &elem[i], 0, i);
		vu_queue_flush(vq, count);
		vu_queue_notify(vdev, vq);
	}
}

/**
 * vu_kick_cb() - Called on a kick event to start to receive data
 * @vdev:	vhost-user device
 * @ref:	epoll reference information
 * @now:	Current timestamp
 */
void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref,
		const struct timespec *now)
{
	eventfd_t kick_data;
	ssize_t rc;
	int idx;

	for (idx = 0; idx < VHOST_USER_MAX_QUEUES; idx++) {
		if (vdev->vq[idx].kick_fd == ref.fd)
			break;
	}

	if (idx == VHOST_USER_MAX_QUEUES)
		return;

	rc = eventfd_read(ref.fd, &kick_data);
	if (rc == -1)
		die_perror("vhost-user kick eventfd_read()");

	debug("vhost-user: ot kick_data: %016"PRIx64" idx:%d",
	      kick_data, idx);
	if (VHOST_USER_IS_QUEUE_TX(idx))
		vu_handle_tx(vdev, idx, now);
}

/**
 * vu_send_single() - Send a buffer to the front-end using the RX virtqueue
 * @c:		execution context
 * @buf:	address of the buffer
 * @size:	size of the buffer
 *
 * Return: number of bytes sent, -1 if there is an error
 */
int vu_send_single(const struct ctx *c, const void *buf, size_t size)
{
	struct vu_dev *vdev = c->vdev;
	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
	struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
	struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
	size_t total;
	int elem_cnt, max_elem;
	int i;

	debug("vu_send_single size %zu", size);

	if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) {
		err("Got packet, but no available descriptors on RX virtq.");
		return 0;
	}

	if (vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
		max_elem = VIRTQUEUE_MAX_SIZE;
	else
		max_elem = 1;

	vu_init_elem(elem, in_sg, max_elem);

	elem_cnt = vu_collect_one_frame(vdev, vq, elem, max_elem, size,
					0);
	total = iov_size(in_sg, elem_cnt);
	if (total < size) {
		debug("vu_send_single: no space to send the data "
		      "elem_cnt %d iov_size %zd", elem_cnt, total);
		goto err;
	}

	vu_set_vnethdr(vdev, in_sg, elem_cnt, 0);

	/* copy data from the buffer to the iovec */
	iov_from_buf(in_sg, elem_cnt, sizeof(struct virtio_net_hdr_mrg_rxbuf),
		     buf, size);

	if (*c->pcap) {
		pcap_iov(in_sg, elem_cnt,
			 sizeof(struct virtio_net_hdr_mrg_rxbuf));
	}

	vu_flush(vdev, vq, elem, elem_cnt);

	debug("vhost-user sent %zu", total);

	return total;
err:
	for (i = 0; i < elem_cnt; i++)
		vu_queue_detach_element(vq);

	return 0;
}

debug log:

solving 38d5d8eb4fa3 ...
found 38d5d8eb4fa3 in https://archives.passt.top/passt-dev/20241007144054.41868-8-lvivier@redhat.com/

applying [1/1] https://archives.passt.top/passt-dev/20241007144054.41868-8-lvivier@redhat.com/
diff --git a/vu_common.c b/vu_common.c
new file mode 100644
index 000000000000..38d5d8eb4fa3

Checking patch vu_common.c...
Applied patch vu_common.c cleanly.

index at:
100644 38d5d8eb4fa3dc935da578533d17779f86165a35	vu_common.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).