net: dgram: add unix socket
Signed-off-by: Laurent Vivier <lvivier@redhat.com> Reviewed-by: Stefano Brivio <sbrivio@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Acked-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Markus Armbruster <armbru@redhat.com> (QAPI schema) Signed-off-by: Jason Wang <jasowang@redhat.com>
This commit is contained in:
parent
8ecc7f40bc
commit
784e7a2531
55
net/dgram.c
55
net/dgram.c
@ -426,6 +426,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,
|
|||||||
SocketAddress *remote, *local;
|
SocketAddress *remote, *local;
|
||||||
struct sockaddr *dest_addr;
|
struct sockaddr *dest_addr;
|
||||||
struct sockaddr_in laddr_in, raddr_in;
|
struct sockaddr_in laddr_in, raddr_in;
|
||||||
|
struct sockaddr_un laddr_un, raddr_un;
|
||||||
socklen_t dest_len;
|
socklen_t dest_len;
|
||||||
|
|
||||||
assert(netdev->type == NET_CLIENT_DRIVER_DGRAM);
|
assert(netdev->type == NET_CLIENT_DRIVER_DGRAM);
|
||||||
@ -465,7 +466,8 @@ int net_init_dgram(const Netdev *netdev, const char *name,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (local->type != SOCKET_ADDRESS_TYPE_FD) {
|
if (local->type != SOCKET_ADDRESS_TYPE_FD) {
|
||||||
error_setg(errp, "type=inet requires remote parameter");
|
error_setg(errp,
|
||||||
|
"type=inet or type=unix requires remote parameter");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -508,6 +510,53 @@ int net_init_dgram(const Netdev *netdev, const char *name,
|
|||||||
dest_addr = g_malloc(dest_len);
|
dest_addr = g_malloc(dest_len);
|
||||||
memcpy(dest_addr, &raddr_in, dest_len);
|
memcpy(dest_addr, &raddr_in, dest_len);
|
||||||
break;
|
break;
|
||||||
|
case SOCKET_ADDRESS_TYPE_UNIX:
|
||||||
|
ret = unlink(local->u.q_unix.path);
|
||||||
|
if (ret < 0 && errno != ENOENT) {
|
||||||
|
error_setg_errno(errp, errno, "failed to unlink socket %s",
|
||||||
|
local->u.q_unix.path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
laddr_un.sun_family = PF_UNIX;
|
||||||
|
ret = snprintf(laddr_un.sun_path, sizeof(laddr_un.sun_path), "%s",
|
||||||
|
local->u.q_unix.path);
|
||||||
|
if (ret < 0 || ret >= sizeof(laddr_un.sun_path)) {
|
||||||
|
error_setg(errp, "UNIX socket path '%s' is too long",
|
||||||
|
local->u.q_unix.path);
|
||||||
|
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
||||||
|
sizeof(laddr_un.sun_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
raddr_un.sun_family = PF_UNIX;
|
||||||
|
ret = snprintf(raddr_un.sun_path, sizeof(raddr_un.sun_path), "%s",
|
||||||
|
remote->u.q_unix.path);
|
||||||
|
if (ret < 0 || ret >= sizeof(raddr_un.sun_path)) {
|
||||||
|
error_setg(errp, "UNIX socket path '%s' is too long",
|
||||||
|
remote->u.q_unix.path);
|
||||||
|
error_append_hint(errp, "Path must be less than %zu bytes\n",
|
||||||
|
sizeof(raddr_un.sun_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = qemu_socket(PF_UNIX, SOCK_DGRAM, 0);
|
||||||
|
if (fd < 0) {
|
||||||
|
error_setg_errno(errp, errno, "can't create datagram socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = bind(fd, (struct sockaddr *)&laddr_un, sizeof(laddr_un));
|
||||||
|
if (ret < 0) {
|
||||||
|
error_setg_errno(errp, errno, "can't bind unix=%s to socket",
|
||||||
|
laddr_un.sun_path);
|
||||||
|
closesocket(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
qemu_socket_set_nonblock(fd);
|
||||||
|
|
||||||
|
dest_len = sizeof(raddr_un);
|
||||||
|
dest_addr = g_malloc(dest_len);
|
||||||
|
memcpy(dest_addr, &raddr_un, dest_len);
|
||||||
|
break;
|
||||||
case SOCKET_ADDRESS_TYPE_FD:
|
case SOCKET_ADDRESS_TYPE_FD:
|
||||||
fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
|
fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
@ -546,6 +595,10 @@ int net_init_dgram(const Netdev *netdev, const char *name,
|
|||||||
inet_ntoa(raddr_in.sin_addr),
|
inet_ntoa(raddr_in.sin_addr),
|
||||||
ntohs(raddr_in.sin_port));
|
ntohs(raddr_in.sin_port));
|
||||||
break;
|
break;
|
||||||
|
case SOCKET_ADDRESS_TYPE_UNIX:
|
||||||
|
qemu_set_info_str(&s->nc, "udp=%s:%s",
|
||||||
|
laddr_un.sun_path, raddr_un.sun_path);
|
||||||
|
break;
|
||||||
case SOCKET_ADDRESS_TYPE_FD: {
|
case SOCKET_ADDRESS_TYPE_FD: {
|
||||||
SocketAddress *sa;
|
SocketAddress *sa;
|
||||||
SocketAddressType sa_type;
|
SocketAddressType sa_type;
|
||||||
|
@ -603,7 +603,7 @@
|
|||||||
# @remote: remote address
|
# @remote: remote address
|
||||||
# @local: local address
|
# @local: local address
|
||||||
#
|
#
|
||||||
# Only SocketAddress types 'inet' and 'fd' are supported.
|
# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
|
||||||
#
|
#
|
||||||
# If remote address is present and it's a multicast address, local address
|
# If remote address is present and it's a multicast address, local address
|
||||||
# is optional. Otherwise local address is required and remote address is
|
# is optional. Otherwise local address is required and remote address is
|
||||||
|
@ -2782,6 +2782,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
|
|||||||
" configure a network backend to connect to a multicast maddr and port\n"
|
" configure a network backend to connect to a multicast maddr and port\n"
|
||||||
" use ``local.host=addr`` to specify the host address to send packets from\n"
|
" use ``local.host=addr`` to specify the host address to send packets from\n"
|
||||||
"-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n"
|
"-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n"
|
||||||
|
"-netdev dgram,id=str,local.type=unix,local.path=path[,remote.type=unix,remote.path=path]\n"
|
||||||
"-netdev dgram,id=str,local.type=fd,local.str=file-descriptor\n"
|
"-netdev dgram,id=str,local.type=fd,local.str=file-descriptor\n"
|
||||||
" configure a network backend to connect to another network\n"
|
" configure a network backend to connect to another network\n"
|
||||||
" using an UDP tunnel\n"
|
" using an UDP tunnel\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user