]>
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; | |
d314f586 NN |
53 | |
54 | s->vhost_net = vhost_net_init(&options); | |
55 | ||
56 | return vhost_user_running(s) ? 0 : -1; | |
57 | } | |
58 | ||
59 | static void vhost_user_stop(VhostUserState *s) | |
60 | { | |
61 | if (vhost_user_running(s)) { | |
62 | vhost_net_cleanup(s->vhost_net); | |
63 | } | |
64 | ||
65 | s->vhost_net = 0; | |
66 | } | |
67 | ||
68 | static void vhost_user_cleanup(NetClientState *nc) | |
69 | { | |
70 | VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); | |
71 | ||
72 | vhost_user_stop(s); | |
73 | qemu_purge_queued_packets(nc); | |
74 | } | |
75 | ||
76 | static bool vhost_user_has_vnet_hdr(NetClientState *nc) | |
77 | { | |
78 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); | |
79 | ||
80 | return true; | |
81 | } | |
82 | ||
83 | static bool vhost_user_has_ufo(NetClientState *nc) | |
84 | { | |
85 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); | |
86 | ||
87 | return true; | |
88 | } | |
89 | ||
90 | static NetClientInfo net_vhost_user_info = { | |
03ce5744 | 91 | .type = NET_CLIENT_OPTIONS_KIND_VHOST_USER, |
d314f586 NN |
92 | .size = sizeof(VhostUserState), |
93 | .cleanup = vhost_user_cleanup, | |
94 | .has_vnet_hdr = vhost_user_has_vnet_hdr, | |
95 | .has_ufo = vhost_user_has_ufo, | |
96 | }; | |
97 | ||
98 | static void net_vhost_link_down(VhostUserState *s, bool link_down) | |
99 | { | |
100 | s->nc.link_down = link_down; | |
101 | ||
102 | if (s->nc.peer) { | |
103 | s->nc.peer->link_down = link_down; | |
104 | } | |
105 | ||
106 | if (s->nc.info->link_status_changed) { | |
107 | s->nc.info->link_status_changed(&s->nc); | |
108 | } | |
109 | ||
110 | if (s->nc.peer && s->nc.peer->info->link_status_changed) { | |
111 | s->nc.peer->info->link_status_changed(s->nc.peer); | |
112 | } | |
113 | } | |
114 | ||
115 | static void net_vhost_user_event(void *opaque, int event) | |
116 | { | |
117 | VhostUserState *s = opaque; | |
118 | ||
119 | switch (event) { | |
120 | case CHR_EVENT_OPENED: | |
121 | vhost_user_start(s); | |
122 | net_vhost_link_down(s, false); | |
830d70db | 123 | error_report("chardev \"%s\" went up", s->nc.info_str); |
d314f586 NN |
124 | break; |
125 | case CHR_EVENT_CLOSED: | |
126 | net_vhost_link_down(s, true); | |
127 | vhost_user_stop(s); | |
830d70db | 128 | error_report("chardev \"%s\" went down", s->nc.info_str); |
d314f586 NN |
129 | break; |
130 | } | |
131 | } | |
132 | ||
133 | static int net_vhost_user_init(NetClientState *peer, const char *device, | |
830d70db OC |
134 | const char *name, CharDriverState *chr, |
135 | uint32_t queues) | |
d314f586 NN |
136 | { |
137 | NetClientState *nc; | |
138 | VhostUserState *s; | |
830d70db | 139 | int i; |
d314f586 | 140 | |
830d70db OC |
141 | for (i = 0; i < queues; i++) { |
142 | nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name); | |
d314f586 | 143 | |
830d70db OC |
144 | snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s", |
145 | i, chr->label); | |
d314f586 | 146 | |
830d70db | 147 | s = DO_UPCAST(VhostUserState, nc, nc); |
d314f586 | 148 | |
830d70db OC |
149 | /* We don't provide a receive callback */ |
150 | s->nc.receive_disabled = 1; | |
151 | s->chr = chr; | |
152 | s->nc.queue_index = i; | |
d314f586 | 153 | |
830d70db OC |
154 | qemu_chr_add_handlers(s->chr, NULL, NULL, net_vhost_user_event, s); |
155 | } | |
d314f586 NN |
156 | return 0; |
157 | } | |
158 | ||
71df1d83 MA |
159 | static int net_vhost_chardev_opts(void *opaque, |
160 | const char *name, const char *value, | |
161 | Error **errp) | |
03ce5744 NN |
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 { | |
81904831 MA |
172 | error_setg(errp, |
173 | "vhost-user does not support a chardev with option %s=%s", | |
174 | name, value); | |
03ce5744 NN |
175 | return -1; |
176 | } | |
177 | return 0; | |
178 | } | |
179 | ||
81904831 MA |
180 | static CharDriverState *net_vhost_parse_chardev( |
181 | const NetdevVhostUserOptions *opts, Error **errp) | |
03ce5744 NN |
182 | { |
183 | CharDriverState *chr = qemu_chr_find(opts->chardev); | |
184 | VhostUserChardevProps props; | |
185 | ||
186 | if (chr == NULL) { | |
81904831 | 187 | error_setg(errp, "chardev \"%s\" not found", opts->chardev); |
03ce5744 NN |
188 | return NULL; |
189 | } | |
190 | ||
191 | /* inspect chardev opts */ | |
192 | memset(&props, 0, sizeof(props)); | |
81904831 | 193 | if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) { |
03ce5744 NN |
194 | return NULL; |
195 | } | |
196 | ||
197 | if (!props.is_socket || !props.is_unix) { | |
81904831 MA |
198 | error_setg(errp, "chardev \"%s\" is not a unix socket", |
199 | opts->chardev); | |
03ce5744 NN |
200 | return NULL; |
201 | } | |
202 | ||
203 | qemu_chr_fe_claim_no_fail(chr); | |
204 | ||
205 | return chr; | |
206 | } | |
207 | ||
28d0de7a | 208 | static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp) |
03ce5744 NN |
209 | { |
210 | const char *name = opaque; | |
211 | const char *driver, *netdev; | |
212 | const char virtio_name[] = "virtio-net-"; | |
213 | ||
214 | driver = qemu_opt_get(opts, "driver"); | |
215 | netdev = qemu_opt_get(opts, "netdev"); | |
216 | ||
217 | if (!driver || !netdev) { | |
218 | return 0; | |
219 | } | |
220 | ||
221 | if (strcmp(netdev, name) == 0 && | |
222 | strncmp(driver, virtio_name, strlen(virtio_name)) != 0) { | |
81904831 | 223 | error_setg(errp, "vhost-user requires frontend driver virtio-net-*"); |
03ce5744 NN |
224 | return -1; |
225 | } | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
d314f586 | 230 | int net_init_vhost_user(const NetClientOptions *opts, const char *name, |
a30ecde6 | 231 | NetClientState *peer, Error **errp) |
d314f586 | 232 | { |
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 | ||
81904831 | 240 | chr = net_vhost_parse_chardev(vhost_user_opts, errp); |
03ce5744 | 241 | if (!chr) { |
03ce5744 NN |
242 | return -1; |
243 | } | |
244 | ||
245 | /* verify net frontend */ | |
246 | if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net, | |
81904831 | 247 | (char *)name, errp)) { |
03ce5744 NN |
248 | return -1; |
249 | } | |
250 | ||
830d70db OC |
251 | /* number of queues for multiqueue */ |
252 | if (vhost_user_opts->has_queues) { | |
253 | queues = vhost_user_opts->queues; | |
254 | } else { | |
255 | queues = 1; | |
256 | } | |
03ce5744 | 257 | |
830d70db | 258 | return net_vhost_user_init(peer, "vhost_user", name, chr, queues); |
d314f586 | 259 | } |