Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / net / vmnet-bridged.m
bloba04a14fa11e4d0ffd3add5b3c3031d7f3f1edbfa
1 /*
2  * vmnet-bridged.m
3  *
4  * Copyright(c) 2022 Vladislav Yaroshchuk <vladislav.yaroshchuk@jetbrains.com>
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  */
11 #include "qemu/osdep.h"
12 #include "qapi/qapi-types-net.h"
13 #include "qapi/error.h"
14 #include "clients.h"
15 #include "vmnet_int.h"
17 #include <vmnet/vmnet.h>
20 static bool validate_ifname(const char *ifname)
22     xpc_object_t shared_if_list = vmnet_copy_shared_interface_list();
23     bool match = false;
24     if (!xpc_array_get_count(shared_if_list)) {
25         goto done;
26     }
28     match = !xpc_array_apply(
29         shared_if_list,
30         ^bool(size_t index, xpc_object_t value) {
31             return strcmp(xpc_string_get_string_ptr(value), ifname) != 0;
32         });
34 done:
35     xpc_release(shared_if_list);
36     return match;
40 static char* get_valid_ifnames(void)
42     xpc_object_t shared_if_list = vmnet_copy_shared_interface_list();
43     __block char *if_list = NULL;
44     __block char *if_list_prev = NULL;
46     if (!xpc_array_get_count(shared_if_list)) {
47         goto done;
48     }
50     xpc_array_apply(
51         shared_if_list,
52         ^bool(size_t index, xpc_object_t value) {
53             /* build list of strings like "en0 en1 en2 " */
54             if_list = g_strconcat(xpc_string_get_string_ptr(value),
55                                   " ",
56                                   if_list_prev,
57                                   NULL);
58             g_free(if_list_prev);
59             if_list_prev = if_list;
60             return true;
61         });
63 done:
64     xpc_release(shared_if_list);
65     return if_list;
69 static bool validate_options(const Netdev *netdev, Error **errp)
71     const NetdevVmnetBridgedOptions *options = &(netdev->u.vmnet_bridged);
72     char* if_list;
74     if (!validate_ifname(options->ifname)) {
75         if_list = get_valid_ifnames();
76         if (if_list) {
77             error_setg(errp,
78                        "unsupported ifname '%s', expected one of [ %s]",
79                        options->ifname,
80                        if_list);
81             g_free(if_list);
82         } else {
83             error_setg(errp,
84                        "unsupported ifname '%s', no supported "
85                        "interfaces available",
86                        options->ifname);
87         }
88         return false;
89     }
91     return true;
95 static xpc_object_t build_if_desc(const Netdev *netdev)
97     const NetdevVmnetBridgedOptions *options = &(netdev->u.vmnet_bridged);
98     xpc_object_t if_desc = xpc_dictionary_create(NULL, NULL, 0);
100     xpc_dictionary_set_uint64(if_desc,
101                               vmnet_operation_mode_key,
102                               VMNET_BRIDGED_MODE
103     );
105     xpc_dictionary_set_string(if_desc,
106                               vmnet_shared_interface_name_key,
107                               options->ifname);
109     xpc_dictionary_set_bool(if_desc,
110                             vmnet_enable_isolation_key,
111                             options->isolated);
113     return if_desc;
117 static NetClientInfo net_vmnet_bridged_info = {
118     .type = NET_CLIENT_DRIVER_VMNET_BRIDGED,
119     .size = sizeof(VmnetState),
120     .receive = vmnet_receive_common,
121     .cleanup = vmnet_cleanup_common,
125 int net_init_vmnet_bridged(const Netdev *netdev, const char *name,
126                            NetClientState *peer, Error **errp)
128     NetClientState *nc = qemu_new_net_client(&net_vmnet_bridged_info,
129                                              peer, "vmnet-bridged", name);
130     xpc_object_t if_desc;
131     int result = -1;
133     if (!validate_options(netdev, errp)) {
134         return result;
135     }
137     if_desc = build_if_desc(netdev);
138     result = vmnet_if_create(nc, if_desc, errp);
139     xpc_release(if_desc);
140     return result;