public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob deb64902815304adc4a667fa8aeef36a7e52141d 6284 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
 
// SPDX-License-Identifier: GPL-2.0-or-later

#include <unistd.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <stdint.h>
#include <stddef.h>
#include <sys/uio.h>
#include <linux/virtio_net.h>

#include "checksum.h"
#include "util.h"
#include "ip.h"
#include "passt.h"
#include "pcap.h"
#include "log.h"
#include "vhost_user.h"
#include "udp_internal.h"
#include "udp_vu.h"

/* vhost-user */
static const struct virtio_net_hdr vu_header = {
	.flags = VIRTIO_NET_HDR_F_DATA_VALID,
	.gso_type = VIRTIO_NET_HDR_GSO_NONE,
};

static struct iovec     iov_vu		[VIRTQUEUE_MAX_SIZE];
static VuVirtqElement	elem		[VIRTQUEUE_MAX_SIZE];
static struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
static int in_sg_count;

void udp_vu_sock_handler(const struct ctx *c, union epoll_ref ref,
			 uint32_t events, const struct timespec *now)
{
	VuDev *vdev = (VuDev *)&c->vdev;
	VuVirtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
	bool has_mrg_rxbuf, v6 = ref.udp.v6;
	in_port_t dstport = ref.udp.port;
	size_t l2_hdrlen, vnet_hdrlen;
	struct msghdr msg;
	int i, virtqueue_max;

	if (c->no_udp || !(events & EPOLLIN))
		return;

	has_mrg_rxbuf = vu_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF);
	if (has_mrg_rxbuf) {
		vnet_hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
		virtqueue_max = VIRTQUEUE_MAX_SIZE;
	} else {
		vnet_hdrlen = sizeof(struct virtio_net_hdr);
		virtqueue_max = 1;
	}
	l2_hdrlen = vnet_hdrlen + sizeof(struct ethhdr) + sizeof(struct udphdr);

	if (v6) {
		l2_hdrlen += sizeof(struct ipv6hdr);

		udp6_localname.sin6_port = htons(dstport);
		msg.msg_name = &udp6_localname;
		msg.msg_namelen = sizeof(udp6_localname);
	} else {
		l2_hdrlen += sizeof(struct iphdr);

		udp4_localname.sin_port = htons(dstport);
		msg.msg_name = &udp4_localname;
		msg.msg_namelen = sizeof(udp4_localname);
	}

	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;

	for (i = 0; i < UDP_MAX_FRAMES; i++) {
		struct virtio_net_hdr_mrg_rxbuf *vh;
		size_t size, fillsize, remaining;
		int iov_cnt, iov_used;
		struct ethhdr *eh;
		ssize_t data_len;
		size_t l4len;
		char *base;

		fillsize = USHRT_MAX;
		iov_cnt = 0;
		in_sg_count = 0;
		while (fillsize && iov_cnt < virtqueue_max &&
				in_sg_count < ARRAY_SIZE(in_sg)) {
			int ret;

			elem[iov_cnt].out_num = 0;
			elem[iov_cnt].out_sg = NULL;
			elem[iov_cnt].in_num = ARRAY_SIZE(in_sg) - in_sg_count;
			elem[iov_cnt].in_sg = &in_sg[in_sg_count];
			ret = vu_queue_pop(vdev, vq, &elem[iov_cnt]);
			if (ret < 0)
				break;
			in_sg_count += elem[iov_cnt].in_num;

			if (elem[iov_cnt].in_num < 1) {
				err("virtio-net receive queue contains no in buffers");
				vu_queue_rewind(vdev, vq, iov_cnt);
				return;
			}
			ASSERT(elem[iov_cnt].in_num == 1);
			ASSERT(elem[iov_cnt].in_sg[0].iov_len >= l2_hdrlen);

			if (iov_cnt == 0) {
				base = elem[iov_cnt].in_sg[0].iov_base;
				size = elem[iov_cnt].in_sg[0].iov_len;

				/* keep space for the headers */
				iov_vu[0].iov_base = base + l2_hdrlen;
				iov_vu[0].iov_len = size - l2_hdrlen;
			} else {
				iov_vu[iov_cnt].iov_base = elem[iov_cnt].in_sg[0].iov_base;
				iov_vu[iov_cnt].iov_len = elem[iov_cnt].in_sg[0].iov_len;
			}

			if (iov_vu[iov_cnt].iov_len > fillsize)
				iov_vu[iov_cnt].iov_len = fillsize;

			fillsize -= iov_vu[iov_cnt].iov_len;

			iov_cnt++;
		}
		if (iov_cnt == 0)
			break;

		msg.msg_iov = iov_vu;
		msg.msg_iovlen = iov_cnt;

		data_len = recvmsg(ref.fd, &msg, 0);
		if (data_len < 0) {
			vu_queue_rewind(vdev, vq, iov_cnt);
			return;
		}

		/* restore original values */
		iov_vu[0].iov_base = base;
		iov_vu[0].iov_len = size;

		/* count the numbers of buffer filled by recvmsg() */
		iov_used = iov_count(iov_vu, iov_cnt, l2_hdrlen + data_len,
				     &remaining);
		ASSERT(iov_used <= iov_cnt);
		if (iov_used > 0) {
			ASSERT(iov_vu[iov_used - 1].iov_len >= remaining);
			iov_vu[iov_used - 1].iov_len = remaining;
			/* update size */
			if (iov_used - 1 == 0)
				size = iov_vu[0].iov_len;
		}

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

		/* vnet_header */
		vh = (struct virtio_net_hdr_mrg_rxbuf *)base;
		vh->hdr = vu_header;
		if (has_mrg_rxbuf)
			vh->num_buffers = htole16(iov_used);

		/* ethernet header */
		eh = (struct ethhdr *)(base + vnet_hdrlen);

		memcpy(eh->h_dest, c->mac_guest, sizeof(eh->h_dest));
		memcpy(eh->h_source, c->mac, sizeof(eh->h_source));

		/* initialize header */
		if (v6) {
			struct ipv6hdr *ip6h = (struct ipv6hdr *)(eh + 1);
			struct udp_payload_t *bp = (struct udp_payload_t *)(ip6h + 1);

			eh->h_proto = htons(ETH_P_IPV6);

			*ip6h = (struct ipv6hdr)L2_BUF_IP6_INIT(IPPROTO_UDP);

			l4len = udp_update_hdr6(c, ip6h, &udp6_localname, bp,
						dstport, data_len, now);
			if (*c->pcap) {
				uint32_t sum;

				sum = proto_ipv6_header_psum(l4len, IPPROTO_UDP,
							     &ip6h->saddr,
							     &ip6h->daddr);

				iov_vu[0].iov_base = &bp->uh;
				iov_vu[0].iov_len = size - l2_hdrlen +
						    sizeof(bp->uh);
				bp->uh.check = 0; /* by default, set to 0xffff */
				bp->uh.check = csum_iov(iov_vu, iov_used, sum);
			}
		} else {
			struct iphdr *iph = (struct iphdr *)(eh + 1);
			struct udp_payload_t *bp = (struct udp_payload_t *)(iph + 1);

			eh->h_proto = htons(ETH_P_IP);

			*iph = (struct iphdr)L2_BUF_IP4_INIT(IPPROTO_UDP);

			l4len = udp_update_hdr4(c, iph, &udp4_localname, bp,
						dstport, data_len, now);
			if (*c->pcap) {
				uint32_t sum;

				sum = proto_ipv4_header_psum(l4len, IPPROTO_UDP,
				/* cppcheck-suppress unknownEvaluationOrder */
				(struct in_addr){ .s_addr = iph->saddr },
				(struct in_addr){ .s_addr = iph->daddr });

				iov_vu[0].iov_base = &bp->uh;
				iov_vu[0].iov_len = size - l2_hdrlen +
						    sizeof(bp->uh);
				bp->uh.check = csum_iov(iov_vu, iov_used, sum);
			}
		}

		/* set iov for pcap logging */
		iov_vu[0].iov_base = base + vnet_hdrlen;
		iov_vu[0].iov_len = size - vnet_hdrlen;
		pcap_iov(iov_vu, iov_used);

		/* set iov_len for vu_queue_fill_by_index(); */
		iov_vu[0].iov_base = base;
		iov_vu[0].iov_len = size;

		/* send packets */
		for (i = 0; i < iov_used; i++)
			vu_queue_fill(vdev, vq, &elem[i], iov_vu[i].iov_len, i);

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

debug log:

solving deb649028153 ...
found deb649028153 in https://archives.passt.top/passt-dev/20240621145640.1914287-6-lvivier@redhat.com/

applying [1/1] https://archives.passt.top/passt-dev/20240621145640.1914287-6-lvivier@redhat.com/
diff --git a/udp_vu.c b/udp_vu.c
new file mode 100644
index 000000000000..deb649028153

Checking patch udp_vu.c...
Applied patch udp_vu.c cleanly.

index at:
100644 deb64902815304adc4a667fa8aeef36a7e52141d	udp_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).