aboutgitcodebugslistschat
path: root/qemu/0001-net-Allow-also-UNIX-domain-sockets-to-be-used-as-net.patch
blob: 9e71f88b5a23080edeada17f6c9219c8812c27ff (plain) (blame)
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
From 83c3f76b8fe6b4a6bb45dcf5cfad65ec6f98a10e Mon Sep 17 00:00:00 2001
From: Stefano Brivio <sbrivio@redhat.com>
Date: Wed, 26 Jan 2022 16:45:15 +0100
Subject: [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev
 socket

It has lower overhead compared to TCP, doesn't need a free port
and the adaptation is trivial.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
SPDX-FileCopyrightText: 2020-2022 Red Hat GmbH <sbrivio@redhat.com>
SPDX-License-Identifier: AGPL-3.0-or-later

 net/socket.c | 106 ++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 87 insertions(+), 19 deletions(-)

diff --git a/net/socket.c b/net/socket.c
index 2e5f3ac923..b901e22836 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -511,26 +511,59 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
-    int fd, ret;
+    struct sockaddr_storage saddr;
+    struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
+    struct sockaddr_un *saddr_un = (struct sockaddr_un *)&saddr;
+    size_t saddr_size;
+    int fd, ret, pf;
+
+#ifndef WIN32
+    if (strchr(host_str, ':')) {
+#endif
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
+            return -1;
 
-    if (parse_host_port(&saddr, host_str, errp) < 0) {
-        return -1;
-    }
+        pf = PF_INET;
+        saddr_size = sizeof(*saddr_in);
+#ifndef WIN32
+    } else {
+        struct stat sb;
+
+        if (stat(host_str, &sb) == -1) {
+            error_setg_errno(errp, errno, "can't stat socket path");
+            return -1;
+        }
+
+        if ((sb.st_mode & S_IFMT) != S_IFSOCK) {
+            error_setg_errno(errp, errno, "path provided is not a socket");
+            return -1;
+        }
 
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+        saddr_un->sun_family = PF_UNIX;
+        strncpy(saddr_un->sun_path, host_str, sizeof(saddr_un->sun_path));
+
+        pf = PF_UNIX;
+        saddr_size = sizeof(*saddr_un);
+    }
+#endif /* !WIN32 */
+    fd = qemu_socket(pf, SOCK_STREAM, 0);
     if (fd < 0) {
         error_setg_errno(errp, errno, "can't create stream socket");
         return -1;
     }
     qemu_set_nonblock(fd);
 
-    socket_set_fast_reuse(fd);
+    if (pf == PF_INET)
+        socket_set_fast_reuse(fd);
 
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+    ret = bind(fd, (struct sockaddr *)&saddr, saddr_size);
     if (ret < 0) {
-        error_setg_errno(errp, errno, "can't bind ip=%s to socket",
-                         inet_ntoa(saddr.sin_addr));
+        if (pf == PF_INET)
+            error_setg_errno(errp, errno, "can't bind ip=%s to socket",
+                             inet_ntoa(saddr_in->sin_addr));
+        else if (pf == PF_UNIX)
+            error_setg_errno(errp, errno, "can't create socket with path: %s",
+                             host_str);
         closesocket(fd);
         return -1;
     }
@@ -559,14 +592,43 @@ static int net_socket_connect_init(NetClientState *peer,
                                    Error **errp)
 {
     NetSocketState *s;
-    int fd, connected, ret;
-    struct sockaddr_in saddr;
+    int fd, connected, ret, pf;
+    struct sockaddr_storage saddr;
+    size_t saddr_size;
+    struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
+#ifndef WIN32
+    struct sockaddr_un *saddr_un = (struct sockaddr_un *)&saddr;
+
+    if (strchr(host_str, ':')) {
+#endif
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
+            return -1;
 
-    if (parse_host_port(&saddr, host_str, errp) < 0) {
-        return -1;
+        pf = PF_INET;
+        saddr_size = sizeof(*saddr_in);
+#ifndef WIN32
+    } else {
+        struct stat sb;
+
+        if (stat(host_str, &sb) == -1) {
+            error_setg_errno(errp, errno, "can't stat socket path");
+            return -1;
+        }
+
+        if ((sb.st_mode & S_IFMT) != S_IFSOCK) {
+            error_setg_errno(errp, errno, "provided path is not a socket");
+            return -1;
+        }
+
+        saddr_un->sun_family = PF_UNIX;
+        strncpy(saddr_un->sun_path, host_str, sizeof(saddr_un->sun_path));
+
+        pf = PF_UNIX;
+        saddr_size = sizeof(*saddr_un);
     }
+#endif /* !WIN32 */
 
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+    fd = qemu_socket(pf, SOCK_STREAM, 0);
     if (fd < 0) {
         error_setg_errno(errp, errno, "can't create stream socket");
         return -1;
@@ -575,7 +637,7 @@ static int net_socket_connect_init(NetClientState *peer,
 
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = connect(fd, (struct sockaddr *)&saddr, saddr_size);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
@@ -597,9 +659,15 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
 
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    if (pf == PF_INET) {
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "socket: connect to %s:%d",
+                 inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port));
+    } else if (pf == PF_UNIX) {
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "socket: connect to %s", saddr_un->sun_path);
+    }
+
     return 0;
 }
 
-- 
2.28.0