Skip to content

Commit

Permalink
net: add qemu_{configure,create}_nic_device(), qemu_find_nic_info()
Browse files Browse the repository at this point in the history
Most code which directly accesses nd_table[] and nb_nics uses them for
one of two things. Either "I have created a NIC device and I'd like a
configuration for it", or "I will create a NIC device *if* there is a
configuration for it".  With some variants on the theme around whether
they actually *check* if the model specified in the configuration is
the right one.

Provide functions which perform both of those, allowing platforms to
be a little more consistent and as a step towards making nd_table[]
and nb_nics private to the net code.

One might argue that platforms ought to be consistent about whether
they create the unconfigured devices or not, but making significant
user-visible changes is explicitly *not* the intent right now.

The new functions leave the 'model' field of the NICInfo as NULL after
using it for the default NIC model, unlike the qemu_check_nic_model()
function which does set nd->model to match default_model explicitly.
This is acceptable because there is no code which consumes nd->model
except this NIC-matching code in net/net.c, and no reasonable excuse
for any code wanting to use nd->model in future.

Also export the qemu_find_nic_info() helper, as some platforms have
special cases they need to handle.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Reviewed-by: Paul Durrant <paul@xen.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
  • Loading branch information
dwmw2 committed Feb 2, 2024
1 parent c3709fd commit 93e9d73
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/net/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,47 @@ int qemu_show_nic_models(const char *arg, const char *const *models);
void qemu_check_nic_model(NICInfo *nd, const char *model);
int qemu_find_nic_model(NICInfo *nd, const char * const *models,
const char *default_model);
/**
* qemu_find_nic_info: Obtain NIC configuration information
* @typename: Name of device object type
* @match_default: Match NIC configurations with no model specified
* @alias: Additional model string to match (for user convenience and
* backward compatibility).
*
* Search for a NIC configuration matching the NIC model constraints.
*/
NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
const char *alias);
/**
* qemu_configure_nic_device: Apply NIC configuration to a given device
* @dev: Network device to be configured
* @match_default: Match NIC configurations with no model specified
* @alias: Additional model string to match
*
* Search for a NIC configuration for the provided device, using the
* additionally specified matching constraints. If found, apply the
* configuration using qdev_set_nic_properties() and return %true.
*
* This is used by platform code which creates the device anyway,
* regardless of whether there is a configuration for it. This tends
* to be platforms which ignore `--nodefaults` and create net devices
* anyway, for example because the Ethernet device on that board is
* always physically present.
*/
bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
const char *alias);

/**
* qemu_create_nic_device: Create a NIC device if a configuration exists for it
* @typename: Object typename of network device
* @match_default: Match NIC configurations with no model specified
* @alias: Additional model string to match
*
* Search for a NIC configuration for the provided device type. If found,
* create an object of the corresponding type and return it.
*/
DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
const char *alias);
void print_net_client(Monitor *mon, NetClientState *nc);
void net_socket_rs_init(SocketReadState *rs,
SocketReadStateFinalize *finalize,
Expand Down
51 changes: 51 additions & 0 deletions net/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,57 @@ static int net_init_nic(const Netdev *netdev, const char *name,
return idx;
}

NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
const char *alias)
{
NICInfo *nd;
int i;

for (i = 0; i < nb_nics; i++) {
nd = &nd_table[i];

if (!nd->used || nd->instantiated) {
continue;
}

if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename)
|| (alias && !g_strcmp0(nd->model, alias))) {
return nd;
}
}
return NULL;
}


/* "I have created a device. Please configure it if you can" */
bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
const char *alias)
{
NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)),
match_default, alias);

if (nd) {
qdev_set_nic_properties(dev, nd);
return true;
}
return false;
}

/* "Please create a device, if you have a configuration for it" */
DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
const char *alias)
{
NICInfo *nd = qemu_find_nic_info(typename, match_default, alias);
DeviceState *dev;

if (!nd) {
return NULL;
}

dev = qdev_new(typename);
qdev_set_nic_properties(dev, nd);
return dev;
}

static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
const Netdev *netdev,
Expand Down

0 comments on commit 93e9d73

Please sign in to comment.