Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / net / vmnet-host.c
blob49fb25c224e41bee1ae5ae55f64b493fed5a6154
1 /*
2 * vmnet-host.c
4 * Copyright(c) 2022 Vladislav Yaroshchuk <vladislav.yaroshchuk@jetbrains.com>
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.
9 */
11 #include "qemu/osdep.h"
12 #include "qemu/uuid.h"
13 #include "qapi/qapi-types-net.h"
14 #include "qapi/error.h"
15 #include "clients.h"
16 #include "vmnet_int.h"
18 #include <vmnet/vmnet.h>
21 static bool validate_options(const Netdev *netdev, Error **errp)
23 const NetdevVmnetHostOptions *options = &(netdev->u.vmnet_host);
24 QemuUUID net_uuid;
26 if (options->net_uuid &&
27 qemu_uuid_parse(options->net_uuid, &net_uuid) < 0) {
28 error_setg(errp, "Invalid UUID provided in 'net-uuid'");
29 return false;
32 if ((options->start_address ||
33 options->end_address ||
34 options->subnet_mask) &&
35 !(options->start_address &&
36 options->end_address &&
37 options->subnet_mask)) {
38 error_setg(errp,
39 "'start-address', 'end-address', 'subnet-mask' "
40 "should be provided together");
41 return false;
44 return true;
47 static xpc_object_t build_if_desc(const Netdev *netdev)
49 const NetdevVmnetHostOptions *options = &(netdev->u.vmnet_host);
50 xpc_object_t if_desc = xpc_dictionary_create(NULL, NULL, 0);
52 xpc_dictionary_set_uint64(if_desc,
53 vmnet_operation_mode_key,
54 VMNET_HOST_MODE);
56 xpc_dictionary_set_bool(if_desc,
57 vmnet_enable_isolation_key,
58 options->isolated);
60 QemuUUID net_uuid;
61 if (options->net_uuid) {
62 qemu_uuid_parse(options->net_uuid, &net_uuid);
63 xpc_dictionary_set_uuid(if_desc,
64 vmnet_network_identifier_key,
65 net_uuid.data);
68 if (options->start_address) {
69 xpc_dictionary_set_string(if_desc,
70 vmnet_start_address_key,
71 options->start_address);
72 xpc_dictionary_set_string(if_desc,
73 vmnet_end_address_key,
74 options->end_address);
75 xpc_dictionary_set_string(if_desc,
76 vmnet_subnet_mask_key,
77 options->subnet_mask);
80 return if_desc;
83 static NetClientInfo net_vmnet_host_info = {
84 .type = NET_CLIENT_DRIVER_VMNET_HOST,
85 .size = sizeof(VmnetState),
86 .receive = vmnet_receive_common,
87 .cleanup = vmnet_cleanup_common,
90 int net_init_vmnet_host(const Netdev *netdev, const char *name,
91 NetClientState *peer, Error **errp)
93 NetClientState *nc = qemu_new_net_client(&net_vmnet_host_info,
94 peer, "vmnet-host", name);
95 xpc_object_t if_desc;
96 int result = -1;
98 if (!validate_options(netdev, errp)) {
99 return result;
102 if_desc = build_if_desc(netdev);
103 result = vmnet_if_create(nc, if_desc, errp);
104 xpc_release(if_desc);
105 return result;