VSOCK: add sock_diag interface
Subsystems (6)
Stefan Hajnoczi
maintainercover VSOCK: add sock_diag interface
There is currently no way for userspace to query open AF_VSOCK sockets. This means ss(8), netstat(8), and other utilities cannot display AF_VSOCK sockets.
This patch series adds the netlink sock_diag interface for AF_VSOCK. Userspace programs sent a DUMP request including an sk_state bitmap to filter sockets based on their state (connected, listening, etc). The vsock_diag.ko module replies with information about matching sockets. This userspace ABI is defined in <linux/vm_sockets_diag.h>.
The final patch adds a test suite that exercises the basic cases.
Jorgen and Dexuan: I have only tested the virtio transport but this should also work for VMCI and Hyper-V. Please give it a shot if you have time.
Stefan Hajnoczi (5):
VSOCK: export socket tables for sock_diag interface
VSOCK: export __vsock_in_bound/connected_table()
VSOCK: use TCP state constants for sk_state
VSOCK: add sock_diag interface
VSOCK: add tools/vsock/vsock_diag_test
MAINTAINERS | 3 + net/vmw_vsock/Makefile | 3 + tools/vsock/Makefile | 9 + include/net/af_vsock.h | 10 +- include/uapi/linux/vm_sockets_diag.h | 33 ++ tools/vsock/control.h | 13 + tools/vsock/timeout.h | 14 + net/vmw_vsock/af_vsock.c | 62 ++- net/vmw_vsock/diag.c | 186 ++++++++ net/vmw_vsock/virtio_transport.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 22 +- net/vmw_vsock/vmci_transport.c | 34 +- net/vmw_vsock/vmci_transport_notify.c | 2 +- net/vmw_vsock/vmci_transport_notify_qstate.c | 2 +- tools/vsock/control.c | 219 +++++++++ tools/vsock/timeout.c | 64 +++ tools/vsock/vsock_diag_test.c | 681 +++++++++++++++++++++++++++ net/vmw_vsock/Kconfig | 10 + tools/vsock/.gitignore | 2 + 19 files changed, 1312 insertions(+), 59 deletions(-) create mode 100644 tools/vsock/Makefile create mode 100644 include/uapi/linux/vm_sockets_diag.h create mode 100644 tools/vsock/control.h create mode 100644 tools/vsock/timeout.h create mode 100644 net/vmw_vsock/diag.c create mode 100644 tools/vsock/control.c create mode 100644 tools/vsock/timeout.c create mode 100644 tools/vsock/vsock_diag_test.c create mode 100644 tools/vsock/.gitignore
Signature · 1 line
2.13.6
Stefan Hajnoczi
maintainerPATCH 1/5 VSOCK: export socket tables for sock_diag interface
The socket table symbols need to be exported from vsock.ko so that the vsock_diag.ko module will be able to traverse sockets.
--- include/net/af_vsock.h | 5 +++++ net/vmw_vsock/af_vsock.c | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-)
include/net/af_vsock.h +5 -0
@@ -27,6 +27,11 @@#define LAST_RESERVED_PORT 1023 +#define VSOCK_HASH_SIZE 251 +extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE +1 ]; +extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE ]; +extern spinlock_t vsock_table_lock; +#define vsock_sk (__sk) ((struct vsock_sock *)__sk)#define sk_vsock (__vsk) (&(__vsk)->sk)
net/vmw_vsock/af_vsock.c +6 -4
@@ -153,7 +153,6 @@ EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);* vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function * mods with VSOCK_HASH_SIZE to ensure this. */ -#define VSOCK_HASH_SIZE 251#define MAX_PORT_RETRIES 24#define VSOCK_HASH (addr) ((addr)->svm_port % VSOCK_HASH_SIZE)
@@ -168,9 +167,12 @@ EXPORT_SYMBOL_GPL(vm_sockets_get_local_cid);#define vsock_connected_sockets_vsk (vsk) \ vsock_connected_sockets(&(vsk)->remote_addr, &(vsk)->local_addr) -static struct list_head vsock_bind_table[VSOCK_HASH_SIZE +1 ]; -static struct list_head vsock_connected_table[VSOCK_HASH_SIZE ]; -static DEFINE_SPINLOCK (vsock_table_lock); +struct list_head vsock_bind_table[VSOCK_HASH_SIZE +1 ]; +EXPORT_SYMBOL_GPL (vsock_bind_table); +struct list_head vsock_connected_table[VSOCK_HASH_SIZE ]; +EXPORT_SYMBOL_GPL (vsock_connected_table); +DEFINE_SPINLOCK (vsock_table_lock); +EXPORT_SYMBOL_GPL (vsock_table_lock);/* Autobind this socket to the local address if necessary. */ static int vsock_auto_bind (struct vsock_sock *vsk)
Signature · 1 line
2.13.6
Stefan Hajnoczi
maintainerPATCH 2/5 VSOCK: export __vsock_in_bound/connected_table()
In reply to Stefan HajnocziThe vsock_diag.ko module will need to check socket table membership.
--- include/net/af_vsock.h | 2 ++ net/vmw_vsock/af_vsock.c | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
include/net/af_vsock.h +2 -0
@@ -187,6 +187,8 @@ void vsock_enqueue_accept(struct sock *listener, struct sock *connected);void vsock_insert_connected (struct vsock_sock *vsk);void vsock_remove_bound (struct vsock_sock *vsk);void vsock_remove_connected (struct vsock_sock *vsk); +bool __vsock_in_bound_table (struct vsock_sock *vsk); +bool __vsock_in_connected_table (struct vsock_sock *vsk);struct sock *vsock_find_bound_socket (struct sockaddr_vm *addr);struct sock *vsock_find_connected_socket (struct sockaddr_vm *src,struct sockaddr_vm *dst);
net/vmw_vsock/af_vsock.c +4 -2
@@ -250,15 +250,17 @@ static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,return NULL ; } -static bool __vsock_in_bound_table (struct vsock_sock *vsk) +bool __vsock_in_bound_table (struct vsock_sock *vsk) {return !list_empty (&vsk->bound_table); } +EXPORT_SYMBOL_GPL (__vsock_in_bound_table); -static bool __vsock_in_connected_table (struct vsock_sock *vsk) +bool __vsock_in_connected_table (struct vsock_sock *vsk) {return !list_empty (&vsk->connected_table); } +EXPORT_SYMBOL_GPL (__vsock_in_connected_table);static void vsock_insert_unbound (struct vsock_sock *vsk) {
Signature · 1 line
2.13.6
David Miller
maintainerRe: [PATCH 2/5] VSOCK: export __vsock_in_bound/connected_table()
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:40 -0400
Quoted 7 lines
net/vmw_vsock/af_vsock.c +1
-1@@ -250,15 +250,17 @@ static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,return NULL ; } -static bool __vsock_in_bound_table (struct vsock_sock *vsk) +bool __vsock_in_bound_table (struct vsock_sock *vsk) {
net/vmw_vsock/af_vsock.c static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, · line 263
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-1return !list_empty (&vsk->bound_table); } +EXPORT_SYMBOL_GPL (__vsock_in_bound_table); -static bool __vsock_in_connected_table (struct vsock_sock *vsk) +bool __vsock_in_connected_table (struct vsock_sock *vsk) {return !list_empty (&vsk->connected_table); } +EXPORT_SYMBOL_GPL (__vsock_in_connected_table);
Maybe you can just make these inline helpers in af_vsock.h?
Stefan Hajnoczi
maintainerRe: [PATCH 2/5] VSOCK: export __vsock_in_bound/connected_table()
Quoted 14 lines · David Miller
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:40 -0400Quoted 11 lines
net/vmw_vsock/af_vsock.c +2
-1@@ -250,15 +250,17 @@ static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src,return NULL ; } -static bool __vsock_in_bound_table (struct vsock_sock *vsk) +bool __vsock_in_bound_table (struct vsock_sock *vsk) {return !list_empty (&vsk->bound_table); } +EXPORT_SYMBOL_GPL (__vsock_in_bound_table);
Quoted 8 lines
net/vmw_vsock/af_vsock.c static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, · line 263
Quoted 6 lines
net/vmw_vsock/af_vsock.c +2
-1-static bool __vsock_in_connected_table (struct vsock_sock *vsk) +bool __vsock_in_connected_table (struct vsock_sock *vsk) {return !list_empty (&vsk->connected_table); } +EXPORT_SYMBOL_GPL (__vsock_in_connected_table);Maybe you can just make these inline helpers in
af_vsock.h?
Will fix in v2.
Stefan Hajnoczi
maintainerPATCH 3/5 VSOCK: use TCP state constants for sk_state
In reply to Stefan HajnocziThere are two state fields: socket->state and sock->sk_state. The socket->state field uses SS_UNCONNECTED, SS_CONNECTED, etc while the sock->sk_state typically uses values that match TCP state constants (TCP_CLOSE, TCP_ESTABLISHED). AF_VSOCK does not follow this convention and instead uses SS_* constants for both fields.
The sk_state field will be exposed to userspace through the vsock_diag interface for ss(8), netstat(8), and other programs.
This patch switches sk_state to TCP state constants so that the meaning of this field is consistent with other address families. Not just AF_INET and AF_INET6 use the TCP constants, AF_UNIX and others do too.
The following mapping was used to convert the code:
SS_FREE -> TCP_CLOSE
SS_UNCONNECTED -> TCP_CLOSE
SS_CONNECTING -> TCP_SYN_SENT
SS_CONNECTED -> TCP_ESTABLISHED
SS_DISCONNECTING -> TCP_CLOSING
VSOCK_SS_LISTEN -> TCP_LISTEN
In __vsock_create() the sk_state initialization was dropped because sock_init_data() already initializes sk_state to TCP_CLOSE.
--- include/net/af_vsock.h | 3 -- net/vmw_vsock/af_vsock.c | 46 ++++++++++++++++------------ net/vmw_vsock/virtio_transport.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 22 ++++++------- net/vmw_vsock/vmci_transport.c | 34 ++++++++++---------- net/vmw_vsock/vmci_transport_notify.c | 2 +- net/vmw_vsock/vmci_transport_notify_qstate.c | 2 +- 7 files changed, 58 insertions(+), 53 deletions(-)
include/net/af_vsock.h +0 -3
@@ -22,9 +22,6 @@#include "vsock_addr.h" -/* vsock-specific sock->sk_state constants */ -#define VSOCK_SS_LISTEN 255 -#define LAST_RESERVED_PORT 1023#define VSOCK_HASH_SIZE 251
net/vmw_vsock/af_vsock.c +27 -19
160 lines not on this page · raw message
net/vmw_vsock/virtio_transport.c +1 -1
@@ -414,7 +414,7 @@ static void virtio_vsock_event_fill(struct virtio_vsock *vsock)static void virtio_vsock_reset_sock (struct sock *sk) {lock_sock (sk); - sk->sk_state =SS_UNCONNECTED ; + sk->sk_state =TCP_CLOSE ; sk->sk_err =ECONNRESET ; sk->sk_error_report (sk);release_sock (sk);
net/vmw_vsock/virtio_transport_common.c +11 -11
@@ -708,7 +708,7 @@ static void virtio_transport_do_close(struct vsock_sock *vsk,sock_set_flag (sk,SOCK_DONE ); vsk->peer_shutdown =SHUTDOWN_MASK ;if (vsock_stream_has_data (vsk) <=0 ) - sk->sk_state =SS_DISCONNECTING ; + sk->sk_state =TCP_CLOSING ; sk->sk_state_change (sk); if (vsk->close_work_scheduled &&
@@ -748,8 +748,8 @@ static bool virtio_transport_close(struct vsock_sock *vsk) {struct sock *sk = &vsk->sk; -if (!(sk->sk_state ==SS_CONNECTED || - sk->sk_state ==SS_DISCONNECTING )) +if (!(sk->sk_state ==TCP_ESTABLISHED || + sk->sk_state ==TCP_CLOSING ))return true;/* Already received SHUTDOWN from peer, reply with RST */
@@ -801,7 +801,7 @@ virtio_transport_recv_connecting(struct sock *sk,switch (le16_to_cpu (pkt->hdr.op)) {case VIRTIO_VSOCK_OP_RESPONSE : - sk->sk_state =SS_CONNECTED ; + sk->sk_state =TCP_ESTABLISHED ; sk->sk_socket->state =SS_CONNECTED ;vsock_insert_connected (vsk); sk->sk_state_change (sk);
@@ -821,7 +821,7 @@ virtio_transport_recv_connecting(struct sock *sk, destroy:virtio_transport_reset (vsk, pkt); - sk->sk_state =SS_UNCONNECTED ; + sk->sk_state =TCP_CLOSE ; sk->sk_err = skerr; sk->sk_error_report (sk);return err;
@@ -857,7 +857,7 @@ virtio_transport_recv_connected(struct sock *sk, vsk->peer_shutdown |=SEND_SHUTDOWN ;if (vsk->peer_shutdown ==SHUTDOWN_MASK &&vsock_stream_has_data (vsk) <=0 ) - sk->sk_state =SS_DISCONNECTING ; + sk->sk_state =TCP_CLOSING ;if (le32_to_cpu (pkt->hdr.flags)) sk->sk_state_change (sk);break ;
@@ -928,7 +928,7 @@ virtio_transport_recv_listen(struct sock *sk, struct virtio_vsock_pkt *pkt)lock_sock_nested (child,SINGLE_DEPTH_NESTING ); - child->sk_state =SS_CONNECTED ; + child->sk_state =TCP_ESTABLISHED ; vchild =vsock_sk (child); vsock_addr_init(&vchild->local_addr, le64_to_cpu(pkt->hdr.dst_cid),
@@ -1016,18 +1016,18 @@ void virtio_transport_recv_pkt(struct virtio_vsock_pkt *pkt) sk->sk_write_space (sk);switch (sk->sk_state) { -case VSOCK_SS_LISTEN : +case TCP_LISTEN :virtio_transport_recv_listen (sk, pkt);virtio_transport_free_pkt (pkt);break ; -case SS_CONNECTING : +case TCP_SYN_SENT :virtio_transport_recv_connecting (sk, pkt);virtio_transport_free_pkt (pkt);break ; -case SS_CONNECTED : +case TCP_ESTABLISHED :virtio_transport_recv_connected (sk, pkt);break ; -case SS_DISCONNECTING : +case TCP_CLOSING :virtio_transport_recv_disconnecting (sk, pkt);virtio_transport_free_pkt (pkt);break ;
net/vmw_vsock/vmci_transport.c +17 -17
@@ -743,7 +743,7 @@ static int vmci_transport_recv_stream_cb(void *data, struct vmci_datagram *dg)/* The local context ID may be out of date, update it. */ vsk->local_addr.svm_cid = dst.svm_cid; -if (sk->sk_state ==SS_CONNECTED ) +if (sk->sk_state ==TCP_ESTABLISHED )vmci_trans (vsk)->notify_ops->handle_notify_pkt ( sk, pkt, true, &dst, &src, &bh_process_pkt);
@@ -801,7 +801,9 @@ static void vmci_transport_handle_detach(struct sock *sk)* left in our consume queue. */ if (vsock_stream_has_data (vsk) <=0 ) { -if (sk->sk_state ==SS_CONNECTING ) { + sk->sk_state =TCP_CLOSE ; + +if (sk->sk_state ==TCP_SYN_SENT ) {/* The peer may detach from a queue pair while * we are still in the connecting state, i.e., * if the peer VM is killed after attaching to
@@ -810,12 +812,10 @@ static void vmci_transport_handle_detach(struct sock *sk)* event like a reset. */ - sk->sk_state =SS_UNCONNECTED ; sk->sk_err =ECONNRESET ; sk->sk_error_report (sk);return ; } - sk->sk_state =SS_UNCONNECTED ; } sk->sk_state_change (sk); }
@@ -883,17 +883,17 @@ static void vmci_transport_recv_pkt_work(struct work_struct *work)vsock_sk (sk)->local_addr.svm_cid = pkt->dg.dst.context;switch (sk->sk_state) { -case VSOCK_SS_LISTEN : +case TCP_LISTEN :vmci_transport_recv_listen (sk, pkt);break ; -case SS_CONNECTING : +case TCP_SYN_SENT :/* Processing of pending connections for servers goes through * the listening socket, so see vmci_transport_recv_listen() * for that path. */ vmci_transport_recv_connecting_client (sk, pkt);break ; -case SS_CONNECTED : +case TCP_ESTABLISHED :vmci_transport_recv_connected (sk, pkt);break ;default :
@@ -942,7 +942,7 @@ static int vmci_transport_recv_listen(struct sock *sk,vsock_sk (pending)->local_addr.svm_cid = pkt->dg.dst.context;switch (pending->sk_state) { -case SS_CONNECTING : +case TCP_SYN_SENT : err =vmci_transport_recv_connecting_server (sk, pending, pkt);
@@ -1072,7 +1072,7 @@ static int vmci_transport_recv_listen(struct sock *sk,vsock_add_pending (sk, pending); sk->sk_ack_backlog++; - pending->sk_state =SS_CONNECTING ; + pending->sk_state =TCP_SYN_SENT ;vmci_trans (vpending)->produce_size =vmci_trans (vpending)->consume_size = qp_size;vmci_trans (vpending)->queue_pair_size = qp_size;
@@ -1197,11 +1197,11 @@ vmci_transport_recv_connecting_server(struct sock *listener,* the socket will be valid until it is removed from the queue. * * If we fail sending the attach below, we remove the socket from the -* connected list and move the socket to SS_UNCONNECTED before +* connected list and move the socket to TCP_CLOSE before * releasing the lock, so a pending slow path processing of an incoming * packet will not see the socket in the connected state in that case. */ - pending->sk_state =SS_CONNECTED ; + pending->sk_state =TCP_ESTABLISHED ;vsock_insert_connected (vpending);
@@ -1232,7 +1232,7 @@ vmci_transport_recv_connecting_server(struct sock *listener, destroy: pending->sk_err = skerr; - pending->sk_state =SS_UNCONNECTED ; + pending->sk_state =TCP_CLOSE ;/* As long as we drop our reference, all necessary cleanup will handle * when the cleanup function drops its reference and our destruct * implementation is called. Note that since the listen handler will
@@ -1270,7 +1270,7 @@ vmci_transport_recv_connecting_client(struct sock *sk,* accounting (it can already be found since it's in the bound * table). */ - sk->sk_state =SS_CONNECTED ; + sk->sk_state =TCP_ESTABLISHED ; sk->sk_socket->state =SS_CONNECTED ;vsock_insert_connected (vsk); sk->sk_state_change (sk);
@@ -1338,7 +1338,7 @@ vmci_transport_recv_connecting_client(struct sock *sk, destroy:vmci_transport_send_reset (sk, pkt); - sk->sk_state =SS_UNCONNECTED ; + sk->sk_state =TCP_CLOSE ; sk->sk_err = skerr; sk->sk_error_report (sk);return err;
@@ -1526,7 +1526,7 @@ static int vmci_transport_recv_connected(struct sock *sk,sock_set_flag (sk,SOCK_DONE ); vsk->peer_shutdown =SHUTDOWN_MASK ;if (vsock_stream_has_data (vsk) <=0 ) - sk->sk_state =SS_DISCONNECTING ; + sk->sk_state =TCP_CLOSING ; sk->sk_state_change (sk);break ;
@@ -1790,7 +1790,7 @@ static int vmci_transport_connect(struct vsock_sock *vsk) err =vmci_transport_send_conn_request ( sk,vmci_trans (vsk)->queue_pair_size);if (err <0 ) { - sk->sk_state =SS_UNCONNECTED ; + sk->sk_state =TCP_CLOSE ;return err; } }else {
@@ -1800,7 +1800,7 @@ static int vmci_transport_connect(struct vsock_sock *vsk) sk,vmci_trans (vsk)->queue_pair_size, supported_proto_versions);if (err <0 ) { - sk->sk_state =SS_UNCONNECTED ; + sk->sk_state =TCP_CLOSE ;return err; }
net/vmw_vsock/vmci_transport_notify.c +1 -1
@@ -355,7 +355,7 @@ vmci_transport_notify_pkt_poll_in(struct sock *sk,* queue. Ask for notifications when there is something to * read. */ -if (sk->sk_state ==SS_CONNECTED ) { +if (sk->sk_state ==TCP_ESTABLISHED ) {if (!send_waiting_read (sk,1 ))return -1 ;
net/vmw_vsock/vmci_transport_notify_qstate.c +1 -1
@@ -176,7 +176,7 @@ vmci_transport_notify_pkt_poll_in(struct sock *sk,* queue. Ask for notifications when there is something to * read. */ -if (sk->sk_state ==SS_CONNECTED ) +if (sk->sk_state ==TCP_ESTABLISHED )vsock_block_update_write_window (sk); *data_ready_now = false; }
Signature · 1 line
2.13.6
Stefan Hajnoczi
maintainerPATCH 4/5 VSOCK: add sock_diag interface
In reply to Stefan HajnocziThis patch adds the sock_diag interface for querying sockets from userspace. Tools like ss(8) and netstat(8) can use this interface to list open sockets.
The userspace ABI is defined in <linux/vm_sockets_diag.h> and includes netlink request and response structs. The request can query sockets based on their sk_state (e.g. listening sockets only) and the response contains socket information fields including the local/remote addresses, inode number, etc.
This patch does not dump VMCI pending sockets because I have only tested the virtio transport, which does not use pending sockets. Support can be added later by extending vsock_diag_dump() if needed by VMCI users.
--- MAINTAINERS | 2 + net/vmw_vsock/Makefile | 3 + include/uapi/linux/vm_sockets_diag.h | 33 +++++++ net/vmw_vsock/diag.c | 186 +++++++++++++++++++++++++++++++++++ net/vmw_vsock/Kconfig | 10 ++ 5 files changed, 234 insertions(+) create mode 100644 include/uapi/linux/vm_sockets_diag.h create mode 100644 net/vmw_vsock/diag.c
MAINTAINERS +2 -0
@@ -13975,6 +13975,8 @@ S: Maintained F: include/linux/virtio_vsock.h F: include/uapi/linux/virtio_vsock.h F: include/uapi/linux/vsockmon.h +F: include/uapi/linux/vm_sockets_diag.h +F: net/vmw_vsock/diag.c F: net/vmw_vsock/af_vsock_tap.c F: net/vmw_vsock/virtio_transport_common.c F: net/vmw_vsock/virtio_transport.c
net/vmw_vsock/Makefile +3 -0
@@ -1,10 +1,13 @@ obj-$(CONFIG_VSOCKETS) += vsock.o +obj-$(CONFIG_VSOCKETS_DIAG) += vsock_diag.o obj-$(CONFIG_VMWARE_VMCI_VSOCKETS) += vmw_vsock_vmci_transport.o obj-$(CONFIG_VIRTIO_VSOCKETS) += vmw_vsock_virtio_transport.o obj-$(CONFIG_VIRTIO_VSOCKETS_COMMON) += vmw_vsock_virtio_transport_common.o vsock-y += af_vsock.o af_vsock_tap.o vsock_addr.o +vsock_diag-y += diag.o + vmw_vsock_vmci_transport-y += vmci_transport.o vmci_transport_notify.o \ vmci_transport_notify_qstate.o
include/uapi/linux/vm_sockets_diag.h new file +33 -0
@@ -0,0 +1,33 @@ +/* AF_VSOCK sock_diag(7) interface for querying open sockets */ + +#ifndef _UAPI__VM_SOCKETS_DIAG_H__ +#define _UAPI__VM_SOCKETS_DIAG_H__ + +#include <linux/types.h> + +/* Request */ +struct vsock_diag_req { +__u8 sdiag_family;/* must be AF_VSOCK */ +__u8 sdiag_protocol;/* must be 0 */ +__u16 pad;/* must be 0 */ +__u32 vdiag_states;/* query bitmap (e.g. 1 << TCP_LISTEN) */ +__u32 vdiag_ino;/* must be 0 (reserved) */ +__u32 vdiag_show;/* must be 0 (reserved) */ +__u32 vdiag_cookie[2 ]; +}; + +/* Response */ +struct vsock_diag_msg { +__u8 vdiag_family;/* AF_VSOCK */ +__u8 vdiag_type;/* SOCK_STREAM or SOCK_DGRAM */ +__u8 vdiag_state;/* sk_state (e.g. TCP_LISTEN) */ +__u8 vdiag_shutdown;/* local RCV_SHUTDOWN | SEND_SHUTDOWN */ +__u32 vdiag_src_cid; +__u32 vdiag_src_port; +__u32 vdiag_dst_cid; +__u32 vdiag_dst_port; +__u32 vdiag_ino; +__u32 vdiag_cookie[2 ]; +}; + +#endif /* _UAPI__VM_SOCKETS_DIAG_H__ */
net/vmw_vsock/diag.c new file +186 -0
186 lines not on this page · raw message
net/vmw_vsock/Kconfig +10 -0
@@ -15,6 +15,16 @@ config VSOCKETS To compile this driver as a module, choose M here: the module will be called vsock. If unsure, say N. +config VSOCKETS_DIAG + tristate "Virtual Sockets monitoring interface" + depends on VSOCKETS + default y + help + Support for PF_VSOCK sockets monitoring interface used by the ss tool. + If unsure, say Y. + + Enable this module so userspace applications can query open sockets. + config VMWARE_VMCI_VSOCKETS tristate "VMware VMCI transport for Virtual Sockets" depends on VSOCKETS && VMWARE_VMCI
Signature · 1 line
2.13.6
David Miller
maintainerFrom: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:42 -0400
net/vmw_vsock/diag.c line 27
Quoted 6 lines
net/vmw_vsock/diag.c new file +6
-0+static int sk_diag_fill (struct sock *sk,struct sk_buff *skb, +u32 portid,u32 seq,u32 flags) +{ +struct nlmsghdr *nlh; +struct vsock_diag_msg *rep; +struct vsock_sock *vsk =vsock_sk (sk);
Please order local variables from longest to shortest line.
net/vmw_vsock/diag.c line 64
Quoted 9 lines
net/vmw_vsock/diag.c new file +9
-0+static int vsock_diag_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct vsock_diag_req *req; +unsigned int table; +unsigned int bucket; +unsigned int last_i; +unsigned int i; +struct vsock_sock *vsk; +struct net *net;
Likewise.
Stefan Hajnoczi
maintainerQuoted 12 lines · David Miller
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:42 -0400net/vmw_vsock/diag.c line 27
Quoted 6 lines
net/vmw_vsock/diag.c new file +6
-0+static int sk_diag_fill (struct sock *sk,struct sk_buff *skb, +u32 portid,u32 seq,u32 flags) +{ +struct nlmsghdr *nlh; +struct vsock_diag_msg *rep; +struct vsock_sock *vsk =vsock_sk (sk);Please order local variables from longest to shortest line.
Quoted 11 lines
net/vmw_vsock/diag.c line 64
Quoted 9 lines
net/vmw_vsock/diag.c new file +9
-0+static int vsock_diag_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ +struct vsock_diag_req *req; +unsigned int table; +unsigned int bucket; +unsigned int last_i; +unsigned int i; +struct vsock_sock *vsk; +struct net *net;Likewise.
Will fix in v2.
Stefan Hajnoczi
maintainerPATCH 5/5 VSOCK: add tools/vsock/vsock_diag_test
In reply to Stefan HajnocziThis patch adds tests for the vsock_diag.ko module.
To run the tests:
# qemu-system-x86_64 -M accel=kvm ... \
-device vhost-vsock-pci,peer-cid=3
(host)# ./vsock_diag_test --mode=server \
--control-port=1234 \
--peer-cid=3
(guest)# ./vsock_diag_test --mode=client \
--control-host=10.0.2.2 \
--control-port 1234 \
--peer-cid=2The control.h and timeout.h infrastructure can be used for additional AF_VSOCK tests in the future.
--- MAINTAINERS | 1 + tools/vsock/Makefile | 9 + tools/vsock/control.h | 13 + tools/vsock/timeout.h | 14 + tools/vsock/control.c | 219 ++++++++++++++ tools/vsock/timeout.c | 64 ++++ tools/vsock/vsock_diag_test.c | 681 ++++++++++++++++++++++++++++++++++++++++++ tools/vsock/.gitignore | 2 + 8 files changed, 1003 insertions(+) create mode 100644 tools/vsock/Makefile create mode 100644 tools/vsock/control.h create mode 100644 tools/vsock/timeout.h create mode 100644 tools/vsock/control.c create mode 100644 tools/vsock/timeout.c create mode 100644 tools/vsock/vsock_diag_test.c create mode 100644 tools/vsock/.gitignore
MAINTAINERS +1 -0
@@ -13983,6 +13983,7 @@ F: net/vmw_vsock/virtio_transport.c F: drivers/net/vsockmon.c F: drivers/vhost/vsock.c F: drivers/vhost/vsock.h +F: tools/vsock/ VIRTIO CONSOLE DRIVER M: Amit Shah <[email protected]>
tools/vsock/Makefile new file +9 -0
@@ -0,0 +1,9 @@ +all: test +test: vsock_diag_test +vsock_diag_test: vsock_diag_test.o timeout.o control.o + +CFLAGS += -g -O2 -Werror -Wall -I. -I../include/uapi -I../include -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -D_GNU_SOURCE +.PHONY: all test clean +clean: + ${RM} *.o *.d vsock_diag_test +-include *.d
tools/vsock/control.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef CONTROL_H +#define CONTROL_H + +#include <stdbool.h> + +void control_init (const char *control_host,const char *control_port, +bool server); +void control_cleanup (void ); +void control_writeln (const char *str); +char *control_readln (void ); +void control_expectln (const char *str); + +#endif /* CONTROL_H */
tools/vsock/timeout.h new file +14 -0
@@ -0,0 +1,14 @@ +#ifndef TIMEOUT_H +#define TIMEOUT_H + +enum { +/* Default timeout */ +TIMEOUT =10 /* seconds */ +}; + +void sigalrm (int signo); +void timeout_begin (unsigned int seconds); +void timeout_check (const char *operation); +void timeout_end (void ); + +#endif /* TIMEOUT_H */
tools/vsock/control.c new file +219 -0
219 lines not on this page · raw message
tools/vsock/timeout.c new file +64 -0
64 lines not on this page · raw message
tools/vsock/vsock_diag_test.c new file +681 -0
681 lines not on this page · raw message
tools/vsock/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +*.d +vsock_diag_test
Signature · 1 line
2.13.6
David Miller
maintainerRe: [PATCH 5/5] VSOCK: add tools/vsock/vsock_diag_test
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:43 -0400
Quoted 8 lines
MAINTAINERS | 1 + tools/vsock/Makefile | 9 + tools/vsock/control.h | 13 + tools/vsock/timeout.h | 14 + tools/vsock/control.c | 219 ++++++++++++++ tools/vsock/timeout.c | 64 ++++ tools/vsock/vsock_diag_test.c | 681 ++++++++++++++++++++++++++++++++++++++++++ tools/vsock/.gitignore | 2 +
Please don't create you own "special" directory for tests.
Tests belong under tools/testing/selftests/
If you put your tests in the proper place, and structure them properly (especially the Makefile rules), they will automatically be run by various automated build and test frameworks.
Stefan Hajnoczi
maintainerRe: [PATCH 5/5] VSOCK: add tools/vsock/vsock_diag_test
Quoted 12 lines · David Miller
From: Stefan Hajnoczi <[email protected]>
Date: Tue, 3 Oct 2017 11:39:43 -0400Quoted 8 lines
MAINTAINERS | 1 + tools/vsock/Makefile | 9 + tools/vsock/control.h | 13 + tools/vsock/timeout.h | 14 + tools/vsock/control.c | 219 ++++++++++++++ tools/vsock/timeout.c | 64 ++++ tools/vsock/vsock_diag_test.c | 681 ++++++++++++++++++++++++++++++++++++++++++ tools/vsock/.gitignore | 2 +
Quoted 7 lines
Please don't create you own "special" directory for tests.
Tests belong under
tools/testing/selftests/If you put your tests in the proper place, and structure them properly (especially the
Makefilerules), they will automatically be run by various automated build and test frameworks.
Cool, thanks for the hint. I wasn't aware of that.
Will fix in v2.
Stefan