public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob 260dda7887e13a9ee5b19b88199ab77f05abab53 12632 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
 
// SPDX-License-Identifier: GPL-2.0-or-later
/* tcp_vu.c - TCP L2 vhost-user management functions
 *
 * Copyright Red Hat
 * Author: Laurent Vivier <lvivier@redhat.com>
 */

#include <errno.h>
#include <stddef.h>
#include <stdint.h>

#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <sys/socket.h>

#include <linux/virtio_net.h>

#include "util.h"
#include "ip.h"
#include "passt.h"
#include "siphash.h"
#include "inany.h"
#include "vhost_user.h"
#include "tcp.h"
#include "pcap.h"
#include "flow.h"
#include "tcp_conn.h"
#include "flow_table.h"
#include "tcp_vu.h"
#include "tap.h"
#include "tcp_internal.h"
#include "checksum.h"
#include "vu_common.h"
#include <time.h>

static struct iovec iov_vu[VIRTQUEUE_MAX_SIZE + 1];
static struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];

/**
 * tcp_vu_hdrlen() - return the size of the header in level 2 frame (TCP)
 * @v6:		Set for IPv6 packet
 *
 * Return: Return the size of the header
 */
static size_t tcp_vu_hdrlen(bool v6)
{
	size_t hdrlen;

	hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
		 sizeof(struct ethhdr) + sizeof(struct tcphdr);

	if (v6)
		hdrlen += sizeof(struct ipv6hdr);
	else
		hdrlen += sizeof(struct iphdr);

	return hdrlen;
}

/**
 * tcp_vu_update_check() - Calculate TCP checksum
 * @tapside:	Address information for one side of the flow
 * @iov:	Pointer to the array of IO vectors
 * @iov_used:	Length of the array
 */
static void tcp_vu_update_check(const struct flowside *tapside,
			        struct iovec *iov, int iov_used)
{
	char *base = iov[0].iov_base;

	if (inany_v4(&tapside->oaddr)) {
		const struct iphdr *iph = vu_ip(base);

		tcp_update_check_tcp4(iph, iov, iov_used,
				      (char *)vu_payloadv4(base) - base);
	} else {
		const struct ipv6hdr *ip6h = vu_ip(base);

		tcp_update_check_tcp6(ip6h, iov, iov_used,
				      (char *)vu_payloadv6(base) - base);
	}
}

/**
 * tcp_vu_send_flag() - Send segment with flags to vhost-user (no payload)
 * @c:		Execution context
 * @conn:	Connection pointer
 * @flags:	TCP flags: if not set, send segment only if ACK is due
 *
 * Return: negative error code on connection reset, 0 otherwise
 */
int tcp_vu_send_flag(const struct ctx *c, struct tcp_tap_conn *conn, int flags)
{
	struct vu_dev *vdev = c->vdev;
	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
	const struct flowside *tapside = TAPFLOW(conn);
	size_t l2len, l4len, optlen, hdrlen;
	struct vu_virtq_element flags_elem[2];
	struct tcp_flags_t *tcp_flags;
	struct ipv6hdr *ip6h = NULL;
	struct iovec flags_iov[2];
	struct iphdr *iph = NULL;
	struct ethhdr *eh;
	uint32_t seq;
	int elem_cnt;
	int nb_ack;
	int ret;

	hdrlen = tcp_vu_hdrlen(CONN_V6(conn));

	vu_set_element(&flags_elem[0], NULL, &flags_iov[0]);

	elem_cnt = vu_collect(vdev, vq, &flags_elem[0], 1,
			      hdrlen + sizeof(struct tcp_syn_opts), NULL);
	if (elem_cnt != 1)
		return -1;

	vu_set_vnethdr(vdev, flags_elem[0].in_sg[0].iov_base, 1);

	eh = vu_eth(flags_elem[0].in_sg[0].iov_base);

	memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
	memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));

	if (CONN_V4(conn)) {
		eh->h_proto = htons(ETH_P_IP);

		iph = vu_ip(flags_elem[0].in_sg[0].iov_base);
		*iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);

		tcp_flags = vu_payloadv4(flags_elem[0].in_sg[0].iov_base);
	} else {
		eh->h_proto = htons(ETH_P_IPV6);

		ip6h = vu_ip(flags_elem[0].in_sg[0].iov_base);
		*ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);
		tcp_flags = vu_payloadv6(flags_elem[0].in_sg[0].iov_base);
	}

	memset(&tcp_flags->th, 0, sizeof(tcp_flags->th));
	tcp_flags->th.doff = offsetof(struct tcp_flags_t, opts) / 4;
	tcp_flags->th.ack = 1;

	seq = conn->seq_to_tap;
	ret = tcp_prepare_flags(c, conn, flags, &tcp_flags->th,
				&tcp_flags->opts, &optlen);
	if (ret <= 0) {
		vu_queue_rewind(vq, 1);
		return ret;
	}

	if (CONN_V4(conn)) {
		l4len = tcp_fill_headers4(conn, NULL, iph,
					  (struct tcp_payload_t *)tcp_flags, optlen,
					  NULL, seq, true);
		l2len = sizeof(*iph);
	} else {
		l4len = tcp_fill_headers6(conn, NULL, ip6h,
					  (struct tcp_payload_t *)tcp_flags,
					  optlen, seq, true);
		l2len = sizeof(*ip6h);
	}

	l2len += l4len + sizeof(struct ethhdr);

	flags_elem[0].in_sg[0].iov_len = l2len +
				   sizeof(struct virtio_net_hdr_mrg_rxbuf);
	if (*c->pcap) {
		tcp_vu_update_check(tapside, &flags_elem[0].in_sg[0], 1);
		pcap_iov(&flags_elem[0].in_sg[0], 1,
			 sizeof(struct virtio_net_hdr_mrg_rxbuf));
	}
	nb_ack = 1;

	if (flags & DUP_ACK) {
		vu_set_element(&flags_elem[1], NULL, &flags_iov[1]);

		elem_cnt = vu_collect(vdev, vq, &flags_elem[1], 1,
				      flags_elem[0].in_sg[0].iov_len, NULL);
		if (elem_cnt == 1) {
			memcpy(flags_elem[1].in_sg[0].iov_base,
			       flags_elem[0].in_sg[0].iov_base,
			       flags_elem[0].in_sg[0].iov_len);
			nb_ack++;

			if (*c->pcap)
				pcap_iov(&flags_elem[1].in_sg[0], 1, 0);
		}
	}

	vu_flush(vdev, vq, flags_elem, nb_ack);

	return 0;
}

/** tcp_vu_sock_recv() - Receive datastream from socket into vhost-user buffers
 * @c:		Execution context
 * @conn:	Connection pointer
 * @v6:		Set for IPv6 connections
 * @fillsize:	Number of bytes we can receive
 * @iov_cnt:	number of iov (output)
 *
 * Return: Number of iov entries used to store the data
 */
static ssize_t tcp_vu_sock_recv(const struct ctx *c,
				const struct tcp_tap_conn *conn, bool v6,
				size_t fillsize, int *iov_cnt)
{
	struct vu_dev *vdev = c->vdev;
	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
	struct msghdr mh_sock = { 0 };
	uint16_t mss = MSS_GET(conn);
	int s = conn->sock;
	size_t hdrlen;
	int elem_cnt;
	ssize_t ret;

	*iov_cnt = 0;

	hdrlen = tcp_vu_hdrlen(v6);

	vu_init_elem(elem, &iov_vu[1], VIRTQUEUE_MAX_SIZE);

	elem_cnt = 0;

	while (fillsize > 0 && elem_cnt < VIRTQUEUE_MAX_SIZE) {
		struct iovec *iov;
		size_t frame_size;
		int cnt;

		if (mss > fillsize)
			mss = fillsize;

		cnt = vu_collect(vdev, vq, &elem[elem_cnt],
				 VIRTQUEUE_MAX_SIZE - elem_cnt,
				 mss + hdrlen, &frame_size);
		if (cnt == 0)
			break;

		frame_size -= hdrlen;
		iov = &elem[elem_cnt].in_sg[0];
		iov->iov_base = (char *)iov->iov_base + hdrlen;
		iov->iov_len -= hdrlen;

		fillsize -= frame_size;
		elem_cnt += cnt;

		/* All the frames must have the same size (except the last one),
		 * otherwise we will no able to scan the iov array
		 * to find iov entries with headers
		 * (headers are spread every frame_size in the the array
		 */
		if (frame_size < mss)
			break;
	}

	if (peek_offset_cap) {
		mh_sock.msg_iov = iov_vu + 1;
		mh_sock.msg_iovlen = elem_cnt;
	} else {
		mh_sock.msg_iov = iov_vu;
		mh_sock.msg_iovlen = elem_cnt + 1;
	}

	do
		ret = recvmsg(s, &mh_sock, MSG_PEEK);
	while (ret < 0 && errno == EINTR);

	*iov_cnt = elem_cnt;

	return ret;
}

/**
 * tcp_vu_prepare() - Prepare the packet header
 * @c:		Execution context
 * @conn:	Connection pointer
 * @first:	Pointer to the array of IO vectors
 * @dlen:	Packet data length
 * @check:	Checksum, if already known
 */
static void tcp_vu_prepare(const struct ctx *c,
			   struct tcp_tap_conn *conn, struct iovec *first,
			   size_t dlen, const uint16_t **check)
{
	const struct flowside *toside = TAPFLOW(conn);
	struct tcp_payload_t *payload;
	char *base = first->iov_base;
	struct ipv6hdr *ip6h = NULL;
	struct iphdr *iph = NULL;
	struct ethhdr *eh;

	/* we guess the first iovec provided by the guest can embed
	 * all the headers needed by L2 frame
	 */

	eh = vu_eth(base);

	memcpy(eh->h_dest, c->guest_mac, sizeof(eh->h_dest));
	memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));

	/* initialize header */

	if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) {
		ASSERT(first[0].iov_len >= sizeof(struct virtio_net_hdr_mrg_rxbuf) +
		       sizeof(struct ethhdr) + sizeof(struct iphdr) +
		       sizeof(struct tcphdr));

		eh->h_proto = htons(ETH_P_IP);

		iph = vu_ip(base);
		*iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_TCP);
		payload = vu_payloadv4(base);
	} else {
		ASSERT(first[0].iov_len >= sizeof(struct virtio_net_hdr_mrg_rxbuf) +
		       sizeof(struct ethhdr) + sizeof(struct ipv6hdr) +
		       sizeof(struct tcphdr));

		eh->h_proto = htons(ETH_P_IPV6);

		ip6h = vu_ip(base);
		*ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_TCP);

		payload = vu_payloadv6(base);
	}

	memset(&payload->th, 0, sizeof(payload->th));
	payload->th.doff = offsetof(struct tcp_payload_t, data) / 4;
	payload->th.ack = 1;

	if (inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)) {
		tcp_fill_headers4(conn, NULL, iph, payload, dlen,
				  *check, conn->seq_to_tap, true);
		*check = &iph->check;
	} else {
		tcp_fill_headers6(conn, NULL, ip6h, payload, dlen,
				  conn->seq_to_tap, true);
	}
}

/**
 * tcp_vu_data_from_sock() - Handle new data from socket, queue to vhost-user,
 *			     in window
 * @c:		Execution context
 * @conn:	Connection pointer
 *
 * Return: Negative on connection reset, 0 otherwise
 */
int tcp_vu_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn)
{
	uint32_t wnd_scaled = conn->wnd_from_tap << conn->ws_from_tap;
	struct vu_dev *vdev = c->vdev;
	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
	const struct flowside *tapside = TAPFLOW(conn);
	uint16_t mss = MSS_GET(conn);
	size_t hdrlen, fillsize;
	int i, iov_cnt, iov_used;
	int v6 = CONN_V6(conn);
	uint32_t already_sent = 0;
	const uint16_t *check;
	struct iovec *first;
	int frame_size;
	int num_buffers;
	ssize_t len;

	if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) {
		flow_err(conn,
			 "Got packet, but RX virtqueue not usable yet");
		return 0;
	}

	already_sent = conn->seq_to_tap - conn->seq_ack_from_tap;

	if (SEQ_LT(already_sent, 0)) {
		/* RFC 761, section 2.1. */
		flow_trace(conn, "ACK sequence gap: ACK for %u, sent: %u",
			   conn->seq_ack_from_tap, conn->seq_to_tap);
		conn->seq_to_tap = conn->seq_ack_from_tap;
		already_sent = 0;
		if (tcp_set_peek_offset(conn->sock, 0)) {
			tcp_rst(c, conn);
			return -1;
		}
	}

	if (!wnd_scaled || already_sent >= wnd_scaled) {
		conn_flag(c, conn, STALLED);
		conn_flag(c, conn, ACK_FROM_TAP_DUE);
		return 0;
	}

	/* Set up buffer descriptors we'll fill completely and partially. */

	fillsize = wnd_scaled - already_sent;

	iov_vu[0].iov_base = tcp_buf_discard;
	iov_vu[0].iov_len = already_sent;

	/* collect the buffers from vhost-user and fill them with the
	 * data from the socket
	 */
	len = tcp_vu_sock_recv(c, conn, v6, fillsize, &iov_cnt);
	if (len < 0) {
		vu_queue_rewind(vq, iov_cnt);
		if (errno != EAGAIN && errno != EWOULDBLOCK) {
			tcp_rst(c, conn);
			return -errno;
		}
		return 0;
	}

	if (!len) {
		vu_queue_rewind(vq, iov_cnt);
		if ((conn->events & (SOCK_FIN_RCVD | TAP_FIN_SENT)) == SOCK_FIN_RCVD) {
			int ret = tcp_vu_send_flag(c, conn, FIN | ACK);
			if (ret) {
				tcp_rst(c, conn);
				return ret;
			}

			conn_event(c, conn, TAP_FIN_SENT);
		}

		return 0;
	}

	if (!peek_offset_cap)
		len -= already_sent;

	if (len <= 0) {
		vu_queue_rewind(vq, iov_cnt);
		conn_flag(c, conn, STALLED);
		return 0;
	}

	conn_flag(c, conn, ~STALLED);

	/* Likely, some new data was acked too. */
	tcp_update_seqack_wnd(c, conn, false, NULL);

	/* initialize headers */
	hdrlen = tcp_vu_hdrlen(v6);
	iov_used = 0;
	num_buffers = 0;
	check = NULL;
	frame_size = 0;

	/* iov_vu is an array of buffers and the buffer size can be
	 * smaller than the frame size we want to use but with
	 * num_buffer we can merge several virtio iov buffers in one packet
	 * we need only to set the packet headers in the first iov and
	 * num_buffer to the number of iov entries
	 */
	for (i = 0; i < iov_cnt && len; i++) {

		if (frame_size == 0)
			first = &iov_vu[i + 1];

		if (iov_vu[i + 1].iov_len > (size_t)len)
			iov_vu[i + 1].iov_len = len;

		len -= iov_vu[i + 1].iov_len;
		iov_used++;

		frame_size += iov_vu[i + 1].iov_len;
		num_buffers++;

		if (frame_size >= mss || len == 0 ||
		    i + 1 == iov_cnt || !vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
			if (i + 1 == iov_cnt)
				check = NULL;

			/* restore first iovec base: point to vnet header */
			first->iov_base = (char *)first->iov_base - hdrlen;
			first->iov_len += hdrlen;
			vu_set_vnethdr(vdev, first->iov_base, num_buffers);

			tcp_vu_prepare(c, conn, first, frame_size, &check);
			if (*c->pcap)  {
				tcp_vu_update_check(tapside, first, num_buffers);
				pcap_iov(first, num_buffers,
					 sizeof(struct virtio_net_hdr_mrg_rxbuf));
			}

			conn->seq_to_tap += frame_size;

			frame_size = 0;
			num_buffers = 0;
		}
	}

	/* release unused buffers */
	vu_queue_rewind(vq, iov_cnt - iov_used);

	/* send packets */
	vu_flush(vdev, vq, elem, iov_used);

	conn_flag(c, conn, ACK_FROM_TAP_DUE);

	return 0;
}

debug log:

solving 260dda7887e1 ...
found 260dda7887e1 in https://archives.passt.top/passt-dev/20241108103733.3554357-11-lvivier@redhat.com/
found cc6d4f0f262e in https://archives.passt.top/passt-dev/20241108103733.3554357-10-lvivier@redhat.com/
found 44fd538628a6 in https://archives.passt.top/passt-dev/20241108103733.3554357-8-lvivier@redhat.com/

applying [1/3] https://archives.passt.top/passt-dev/20241108103733.3554357-8-lvivier@redhat.com/
diff --git a/tcp_vu.c b/tcp_vu.c
new file mode 100644
index 000000000000..44fd538628a6


applying [2/3] https://archives.passt.top/passt-dev/20241108103733.3554357-10-lvivier@redhat.com/
diff --git a/tcp_vu.c b/tcp_vu.c
index 44fd538628a6..cc6d4f0f262e 100644


applying [3/3] https://archives.passt.top/passt-dev/20241108103733.3554357-11-lvivier@redhat.com/
diff --git a/tcp_vu.c b/tcp_vu.c
index cc6d4f0f262e..260dda7887e1 100644

Checking patch tcp_vu.c...
Applied patch tcp_vu.c cleanly.
Checking patch tcp_vu.c...
Applied patch tcp_vu.c cleanly.
Checking patch tcp_vu.c...
Applied patch tcp_vu.c cleanly.

index at:
100644 260dda7887e13a9ee5b19b88199ab77f05abab53	tcp_vu.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).