hw/net: ne2000: Remove the logic of padding short frames in the receive path
[qemu/kevin.git] / hw / xen / xen-backend.c
blob5b0fb76eaeb38106f35ceac2830b1375f434b81e
1 /*
2 * Copyright (c) 2018 Citrix Systems Inc.
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
8 #include "qemu/osdep.h"
9 #include "qemu/error-report.h"
10 #include "qapi/error.h"
11 #include "hw/xen/xen-backend.h"
12 #include "hw/xen/xen-bus.h"
14 typedef struct XenBackendImpl {
15 const char *type;
16 XenBackendDeviceCreate create;
17 XenBackendDeviceDestroy destroy;
18 } XenBackendImpl;
20 struct XenBackendInstance {
21 QLIST_ENTRY(XenBackendInstance) entry;
22 const XenBackendImpl *impl;
23 XenBus *xenbus;
24 char *name;
25 XenDevice *xendev;
28 static GHashTable *xen_backend_table_get(void)
30 static GHashTable *table;
32 if (table == NULL) {
33 table = g_hash_table_new(g_str_hash, g_str_equal);
36 return table;
39 static void xen_backend_table_add(XenBackendImpl *impl)
41 g_hash_table_insert(xen_backend_table_get(), (void *)impl->type, impl);
44 static const char **xen_backend_table_keys(unsigned int *count)
46 return (const char **)g_hash_table_get_keys_as_array(
47 xen_backend_table_get(), count);
50 static const XenBackendImpl *xen_backend_table_lookup(const char *type)
52 return g_hash_table_lookup(xen_backend_table_get(), type);
55 void xen_backend_register(const XenBackendInfo *info)
57 XenBackendImpl *impl = g_new0(XenBackendImpl, 1);
59 g_assert(info->type);
61 if (xen_backend_table_lookup(info->type)) {
62 error_report("attempt to register duplicate Xen backend type '%s'",
63 info->type);
64 abort();
67 if (!info->create) {
68 error_report("backend type '%s' has no creator", info->type);
69 abort();
72 impl->type = info->type;
73 impl->create = info->create;
74 impl->destroy = info->destroy;
76 xen_backend_table_add(impl);
79 const char **xen_backend_get_types(unsigned int *count)
81 return xen_backend_table_keys(count);
84 static QLIST_HEAD(, XenBackendInstance) backend_list;
86 static void xen_backend_list_add(XenBackendInstance *backend)
88 QLIST_INSERT_HEAD(&backend_list, backend, entry);
91 static XenBackendInstance *xen_backend_list_find(XenDevice *xendev)
93 XenBackendInstance *backend;
95 QLIST_FOREACH(backend, &backend_list, entry) {
96 if (backend->xendev == xendev) {
97 return backend;
101 return NULL;
104 static void xen_backend_list_remove(XenBackendInstance *backend)
106 QLIST_REMOVE(backend, entry);
109 void xen_backend_device_create(XenBus *xenbus, const char *type,
110 const char *name, QDict *opts, Error **errp)
112 ERRP_GUARD();
113 const XenBackendImpl *impl = xen_backend_table_lookup(type);
114 XenBackendInstance *backend;
116 if (!impl) {
117 return;
120 backend = g_new0(XenBackendInstance, 1);
121 backend->xenbus = xenbus;
122 backend->name = g_strdup(name);
124 impl->create(backend, opts, errp);
125 if (*errp) {
126 g_free(backend->name);
127 g_free(backend);
128 return;
131 backend->impl = impl;
132 xen_backend_list_add(backend);
135 XenBus *xen_backend_get_bus(XenBackendInstance *backend)
137 return backend->xenbus;
140 const char *xen_backend_get_name(XenBackendInstance *backend)
142 return backend->name;
145 void xen_backend_set_device(XenBackendInstance *backend,
146 XenDevice *xendev)
148 g_assert(!backend->xendev);
149 backend->xendev = xendev;
152 XenDevice *xen_backend_get_device(XenBackendInstance *backend)
154 return backend->xendev;
158 bool xen_backend_try_device_destroy(XenDevice *xendev, Error **errp)
160 XenBackendInstance *backend = xen_backend_list_find(xendev);
161 const XenBackendImpl *impl;
163 if (!backend) {
164 return false;
167 impl = backend->impl;
168 impl->destroy(backend, errp);
170 xen_backend_list_remove(backend);
171 g_free(backend->name);
172 g_free(backend);
174 return true;