]>
Commit | Line | Data |
---|---|---|
d314f586 NN |
1 | /* |
2 | * vhost-user.c | |
3 | * | |
4 | * Copyright (c) 2013 Virtual Open Systems Sarl. | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | * | |
9 | */ | |
10 | ||
11 | #include "clients.h" | |
12 | #include "net/vhost_net.h" | |
13 | #include "net/vhost-user.h" | |
14 | #include "sysemu/char.h" | |
03ce5744 | 15 | #include "qemu/config-file.h" |
d314f586 NN |
16 | #include "qemu/error-report.h" |
17 | ||
18 | typedef struct VhostUserState { | |
19 | NetClientState nc; | |
20 | CharDriverState *chr; | |
d314f586 NN |
21 | VHostNetState *vhost_net; |
22 | } VhostUserState; | |
23 | ||
03ce5744 NN |
24 | typedef struct VhostUserChardevProps { |
25 | bool is_socket; | |
26 | bool is_unix; | |
27 | bool is_server; | |
28 | } VhostUserChardevProps; | |
29 | ||
d314f586 NN |
30 | VHostNetState *vhost_user_get_vhost_net(NetClientState *nc) |
31 | { | |
32 | VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); | |
03ce5744 | 33 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); |
d314f586 NN |
34 | return s->vhost_net; |
35 | } | |
36 | ||
37 | static int vhost_user_running(VhostUserState *s) | |
38 | { | |
39 | return (s->vhost_net) ? 1 : 0; | |
40 | } | |
41 | ||
42 | static int vhost_user_start(VhostUserState *s) | |
43 | { | |
44 | VhostNetOptions options; | |
45 | ||
46 | if (vhost_user_running(s)) { | |
47 | return 0; | |
48 | } | |
49 | ||
50 | options.backend_type = VHOST_BACKEND_TYPE_USER; | |
51 | options.net_backend = &s->nc; | |
52 | options.opaque = s->chr; | |
24f938a6 | 53 | options.force = true; |
d314f586 NN |
54 | |
55 | s->vhost_net = vhost_net_init(&options); | |
56 | ||
57 | return vhost_user_running(s) ? 0 : -1; | |
58 | } | |
59 | ||
60 | static void vhost_user_stop(VhostUserState *s) | |
61 | { | |
62 | if (vhost_user_running(s)) { | |
63 | vhost_net_cleanup(s->vhost_net); | |
64 | } | |
65 | ||
66 | s->vhost_net = 0; | |
67 | } | |
68 | ||
69 | static void vhost_user_cleanup(NetClientState *nc) | |
70 | { | |
71 | VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); | |
72 | ||
73 | vhost_user_stop(s); | |
74 | qemu_purge_queued_packets(nc); | |
75 | } | |
76 | ||
77 | static bool vhost_user_has_vnet_hdr(NetClientState *nc) | |
78 | { | |
79 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); | |
80 | ||
81 | return true; | |
82 | } | |
83 | ||
84 | static bool vhost_user_has_ufo(NetClientState *nc) | |
85 | { | |
86 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); | |
87 | ||
88 | return true; | |
89 | } | |
90 | ||
91 | static NetClientInfo net_vhost_user_info = { | |
03ce5744 | 92 | .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER, |
d314f586 NN |
93 | .size = sizeof(VhostUserState), |
94 | .cleanup = vhost_user_cleanup, | |
95 | .has_vnet_hdr = vhost_user_has_vnet_hdr, | |
96 | .has_ufo = vhost_user_has_ufo, | |
97 | }; | |
98 | ||
99 | static void net_vhost_link_down(VhostUserState *s, bool link_down) | |
100 | { | |
101 | s->nc.link_down = link_down; | |
102 | ||
103 | if (s->nc.peer) { | |
104 | s->nc.peer->link_down = link_down; | |
105 | } | |
106 | ||
107 | if (s->nc.info->link_status_changed) { | |
108 | s->nc.info->link_status_changed(&s->nc); | |
109 | } | |
110 | ||
111 | if (s->nc.peer && s->nc.peer->info->link_status_changed) { | |
112 | s->nc.peer->info->link_status_changed(s->nc.peer); | |
113 | } | |
114 | } | |
115 | ||
116 | static void net_vhost_user_event(void *opaque, int event) | |
117 | { | |
118 | VhostUserState *s = opaque; | |
119 | ||
120 | switch (event) { | |
121 | case CHR_EVENT_OPENED: | |
122 | vhost_user_start(s); | |
123 | net_vhost_link_down(s, false); | |
830d70db | 124 | error_report("chardev \"%s\" went up", s->nc.info_str); |
d314f586 NN |
125 | break; |
126 | case CHR_EVENT_CLOSED: | |
127 | net_vhost_link_down(s, true); | |
128 | vhost_user_stop(s); | |
830d70db | 129 | error_report("chardev \"%s\" went down", s->nc.info_str); |
d314f586 NN |
130 | break; |
131 | } | |
132 | } | |
133 | ||
134 | static int net_vhost_user_init(NetClientState *peer, const char *device, | |
830d70db OC |
135 | const char *name, CharDriverState *chr, |
136 | uint32_t queues) | |
d314f586 NN |
137 | { |
138 | NetClientState *nc; | |
139 | VhostUserState *s; | |
830d70db | 140 | int i; |
d314f586 | 141 | |
830d70db OC |
142 | for (i = 0; i < queues; i++) { |
143 | nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name); | |
d314f586 | 144 | |
830d70db OC |
145 | snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s", |
146 | i, chr->label); | |
d314f586 | 147 | |
830d70db | 148 | s = DO_UPCAST(VhostUserState, nc, nc); |
d314f586 | 149 | |
830d70db OC |
150 | /* We don't provide a receive callback */ |
151 | s->nc.receive_disabled = 1; | |
152 | s->chr = chr; | |
153 | s->nc.queue_index = i; | |
d314f586 | 154 | |
830d70db OC |
155 | qemu_chr_add_handlers(s->chr, NULL, NULL, net_vhost_user_event, s); |
156 | } | |
d314f586 NN |
157 | return 0; |
158 | } | |
159 | ||
03ce5744 NN |
160 | static int net_vhost_chardev_opts(const char *name, const char *value, |
161 | void *opaque) | |
162 | { | |
163 | VhostUserChardevProps *props = opaque; | |
164 | ||
165 | if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) { | |
166 | props->is_socket = true; | |
167 | } else if (strcmp(name, "path") == 0) { | |
168 | props->is_unix = true; | |
169 | } else if (strcmp(name, "server") == 0) { | |
170 | props->is_server = true; | |
171 | } else { | |
172 | error_report("vhost-user does not support a chardev" | |
173 | " with the following option:\n %s = %s", | |
174 | name, value); | |
175 | return -1; | |
176 | } | |
177 | return 0; | |
178 | } | |
179 | ||
180 | static CharDriverState *net_vhost_parse_chardev(const NetdevVhostUserOptions *opts) | |
181 | { | |
182 | CharDriverState *chr = qemu_chr_find(opts->chardev); | |
183 | VhostUserChardevProps props; | |
184 | ||
185 | if (chr == NULL) { | |
186 | error_report("chardev \"%s\" not found", opts->chardev); | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | /* inspect chardev opts */ | |
191 | memset(&props, 0, sizeof(props)); | |
192 | if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, true) != 0) { | |
193 | return NULL; | |
194 | } | |
195 | ||
196 | if (!props.is_socket || !props.is_unix) { | |
197 | error_report("chardev \"%s\" is not a unix socket", | |
198 | opts->chardev); | |
199 | return NULL; | |
200 | } | |
201 | ||
202 | qemu_chr_fe_claim_no_fail(chr); | |
203 | ||
204 | return chr; | |
205 | } | |
206 | ||
207 | static int net_vhost_check_net(QemuOpts *opts, void *opaque) | |
208 | { | |
209 | const char *name = opaque; | |
210 | const char *driver, *netdev; | |
211 | const char virtio_name[] = "virtio-net-"; | |
212 | ||
213 | driver = qemu_opt_get(opts, "driver"); | |
214 | netdev = qemu_opt_get(opts, "netdev"); | |
215 | ||
216 | if (!driver || !netdev) { | |
217 | return 0; | |
218 | } | |
219 | ||
220 | if (strcmp(netdev, name) == 0 && | |
221 | strncmp(driver, virtio_name, strlen(virtio_name)) != 0) { | |
222 | error_report("vhost-user requires frontend driver virtio-net-*"); | |
223 | return -1; | |
224 | } | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
d314f586 | 229 | int net_init_vhost_user(const NetClientOptions *opts, const char *name, |
a30ecde6 | 230 | NetClientState *peer, Error **errp) |
d314f586 | 231 | { |
a30ecde6 | 232 | /* FIXME error_setg(errp, ...) on failure */ |
830d70db | 233 | uint32_t queues; |
03ce5744 NN |
234 | const NetdevVhostUserOptions *vhost_user_opts; |
235 | CharDriverState *chr; | |
03ce5744 NN |
236 | |
237 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_VHOST_USER); | |
238 | vhost_user_opts = opts->vhost_user; | |
239 | ||
240 | chr = net_vhost_parse_chardev(vhost_user_opts); | |
241 | if (!chr) { | |
242 | error_report("No suitable chardev found"); | |
243 | return -1; | |
244 | } | |
245 | ||
246 | /* verify net frontend */ | |
247 | if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net, | |
a4c7367f | 248 | (char *)name)) { |
03ce5744 NN |
249 | return -1; |
250 | } | |
251 | ||
830d70db OC |
252 | /* number of queues for multiqueue */ |
253 | if (vhost_user_opts->has_queues) { | |
254 | queues = vhost_user_opts->queues; | |
255 | } else { | |
256 | queues = 1; | |
257 | } | |
03ce5744 | 258 | |
830d70db | 259 | return net_vhost_user_init(peer, "vhost_user", name, chr, queues); |
d314f586 | 260 | } |