Skip to content

Commit 1a200b5

Browse files
committed
Merge branch 'net-virtio-vsock'
2 parents cd35601 + 7e699d2 commit 1a200b5

File tree

2 files changed

+131
-16
lines changed

2 files changed

+131
-16
lines changed

net/vmw_vsock/virtio_transport_common.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,18 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
241241
}
242242

243243
static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
244-
struct sk_buff *skb)
244+
u32 len)
245245
{
246-
if (vvs->rx_bytes + skb->len > vvs->buf_alloc)
246+
if (vvs->rx_bytes + len > vvs->buf_alloc)
247247
return false;
248248

249-
vvs->rx_bytes += skb->len;
249+
vvs->rx_bytes += len;
250250
return true;
251251
}
252252

253253
static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
254-
struct sk_buff *skb)
254+
u32 len)
255255
{
256-
int len;
257-
258-
len = skb_headroom(skb) - sizeof(struct virtio_vsock_hdr) - skb->len;
259256
vvs->rx_bytes -= len;
260257
vvs->fwd_cnt += len;
261258
}
@@ -367,7 +364,7 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
367364

368365
spin_lock_bh(&vvs->rx_lock);
369366
while (total < len && !skb_queue_empty(&vvs->rx_queue)) {
370-
skb = __skb_dequeue(&vvs->rx_queue);
367+
skb = skb_peek(&vvs->rx_queue);
371368

372369
bytes = len - total;
373370
if (bytes > skb->len)
@@ -388,10 +385,11 @@ virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
388385
skb_pull(skb, bytes);
389386

390387
if (skb->len == 0) {
391-
virtio_transport_dec_rx_pkt(vvs, skb);
388+
u32 pkt_len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
389+
390+
virtio_transport_dec_rx_pkt(vvs, pkt_len);
391+
__skb_unlink(skb, &vvs->rx_queue);
392392
consume_skb(skb);
393-
} else {
394-
__skb_queue_head(&vvs->rx_queue, skb);
395393
}
396394
}
397395

@@ -437,17 +435,17 @@ static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
437435

438436
while (!msg_ready) {
439437
struct virtio_vsock_hdr *hdr;
438+
size_t pkt_len;
440439

441440
skb = __skb_dequeue(&vvs->rx_queue);
442441
if (!skb)
443442
break;
444443
hdr = virtio_vsock_hdr(skb);
444+
pkt_len = (size_t)le32_to_cpu(hdr->len);
445445

446446
if (dequeued_len >= 0) {
447-
size_t pkt_len;
448447
size_t bytes_to_copy;
449448

450-
pkt_len = (size_t)le32_to_cpu(hdr->len);
451449
bytes_to_copy = min(user_buf_len, pkt_len);
452450

453451
if (bytes_to_copy) {
@@ -466,7 +464,6 @@ static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
466464
dequeued_len = err;
467465
} else {
468466
user_buf_len -= bytes_to_copy;
469-
skb_pull(skb, bytes_to_copy);
470467
}
471468

472469
spin_lock_bh(&vvs->rx_lock);
@@ -484,7 +481,7 @@ static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
484481
msg->msg_flags |= MSG_EOR;
485482
}
486483

487-
virtio_transport_dec_rx_pkt(vvs, skb);
484+
virtio_transport_dec_rx_pkt(vvs, pkt_len);
488485
kfree_skb(skb);
489486
}
490487

@@ -1040,7 +1037,7 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk,
10401037

10411038
spin_lock_bh(&vvs->rx_lock);
10421039

1043-
can_enqueue = virtio_transport_inc_rx_pkt(vvs, skb);
1040+
can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
10441041
if (!can_enqueue) {
10451042
free_pkt = true;
10461043
goto out;

tools/testing/vsock/vsock_test.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,114 @@ static void test_stream_poll_rcvlowat_client(const struct test_opts *opts)
860860
close(fd);
861861
}
862862

863+
#define INV_BUF_TEST_DATA_LEN 512
864+
865+
static void test_inv_buf_client(const struct test_opts *opts, bool stream)
866+
{
867+
unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
868+
ssize_t ret;
869+
int fd;
870+
871+
if (stream)
872+
fd = vsock_stream_connect(opts->peer_cid, 1234);
873+
else
874+
fd = vsock_seqpacket_connect(opts->peer_cid, 1234);
875+
876+
if (fd < 0) {
877+
perror("connect");
878+
exit(EXIT_FAILURE);
879+
}
880+
881+
control_expectln("SENDDONE");
882+
883+
/* Use invalid buffer here. */
884+
ret = recv(fd, NULL, sizeof(data), 0);
885+
if (ret != -1) {
886+
fprintf(stderr, "expected recv(2) failure, got %zi\n", ret);
887+
exit(EXIT_FAILURE);
888+
}
889+
890+
if (errno != ENOMEM) {
891+
fprintf(stderr, "unexpected recv(2) errno %d\n", errno);
892+
exit(EXIT_FAILURE);
893+
}
894+
895+
ret = recv(fd, data, sizeof(data), MSG_DONTWAIT);
896+
897+
if (stream) {
898+
/* For SOCK_STREAM we must continue reading. */
899+
if (ret != sizeof(data)) {
900+
fprintf(stderr, "expected recv(2) success, got %zi\n", ret);
901+
exit(EXIT_FAILURE);
902+
}
903+
/* Don't check errno in case of success. */
904+
} else {
905+
/* For SOCK_SEQPACKET socket's queue must be empty. */
906+
if (ret != -1) {
907+
fprintf(stderr, "expected recv(2) failure, got %zi\n", ret);
908+
exit(EXIT_FAILURE);
909+
}
910+
911+
if (errno != EAGAIN) {
912+
fprintf(stderr, "unexpected recv(2) errno %d\n", errno);
913+
exit(EXIT_FAILURE);
914+
}
915+
}
916+
917+
control_writeln("DONE");
918+
919+
close(fd);
920+
}
921+
922+
static void test_inv_buf_server(const struct test_opts *opts, bool stream)
923+
{
924+
unsigned char data[INV_BUF_TEST_DATA_LEN] = {0};
925+
ssize_t res;
926+
int fd;
927+
928+
if (stream)
929+
fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, NULL);
930+
else
931+
fd = vsock_seqpacket_accept(VMADDR_CID_ANY, 1234, NULL);
932+
933+
if (fd < 0) {
934+
perror("accept");
935+
exit(EXIT_FAILURE);
936+
}
937+
938+
res = send(fd, data, sizeof(data), 0);
939+
if (res != sizeof(data)) {
940+
fprintf(stderr, "unexpected send(2) result %zi\n", res);
941+
exit(EXIT_FAILURE);
942+
}
943+
944+
control_writeln("SENDDONE");
945+
946+
control_expectln("DONE");
947+
948+
close(fd);
949+
}
950+
951+
static void test_stream_inv_buf_client(const struct test_opts *opts)
952+
{
953+
test_inv_buf_client(opts, true);
954+
}
955+
956+
static void test_stream_inv_buf_server(const struct test_opts *opts)
957+
{
958+
test_inv_buf_server(opts, true);
959+
}
960+
961+
static void test_seqpacket_inv_buf_client(const struct test_opts *opts)
962+
{
963+
test_inv_buf_client(opts, false);
964+
}
965+
966+
static void test_seqpacket_inv_buf_server(const struct test_opts *opts)
967+
{
968+
test_inv_buf_server(opts, false);
969+
}
970+
863971
static struct test_case test_cases[] = {
864972
{
865973
.name = "SOCK_STREAM connection reset",
@@ -920,6 +1028,16 @@ static struct test_case test_cases[] = {
9201028
.run_client = test_seqpacket_bigmsg_client,
9211029
.run_server = test_seqpacket_bigmsg_server,
9221030
},
1031+
{
1032+
.name = "SOCK_STREAM test invalid buffer",
1033+
.run_client = test_stream_inv_buf_client,
1034+
.run_server = test_stream_inv_buf_server,
1035+
},
1036+
{
1037+
.name = "SOCK_SEQPACKET test invalid buffer",
1038+
.run_client = test_seqpacket_inv_buf_client,
1039+
.run_server = test_seqpacket_inv_buf_server,
1040+
},
9231041
{},
9241042
};
9251043

0 commit comments

Comments
 (0)