Skip to content

Commit

Permalink
qapi net: Elide redundant has_FOO in generated C
Browse files Browse the repository at this point in the history
The has_FOO for pointer-valued FOO are redundant, except for arrays.
They are also a nuisance to work with.  Recent commit "qapi: Start to
elide redundant has_FOO in generated C" provided the means to elide
them step by step.  This is the step for qapi/net.json.

Said commit explains the transformation in more detail.  The invariant
violations mentioned there do not occur here.

Cc: Jason Wang <[email protected]>
Signed-off-by: Markus Armbruster <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
[Fixes for MacOS squashed in]
  • Loading branch information
Markus Armbruster committed Dec 14, 2022
1 parent 9492718 commit 7480874
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 82 deletions.
3 changes: 1 addition & 2 deletions hw/net/virtio-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ static void rxfilter_notify(NetClientState *nc)

if (nc->rxfilter_notify_enabled) {
char *path = object_get_canonical_path(OBJECT(n->qdev));
qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
n->netclient_name, path);
qapi_event_send_nic_rx_filter_changed(n->netclient_name, path);
g_free(path);

/* disable event notification to avoid events flooding */
Expand Down
1 change: 0 additions & 1 deletion monitor/hmp-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,6 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict)
params->interfaces = strList_from_comma_list(interfaces_str);
params->has_interfaces = params->interfaces != NULL;
params->id = g_strdup(id);
params->has_id = !!params->id;
qmp_announce_self(params, NULL);
qapi_free_AnnounceParameters(params);
}
Expand Down
8 changes: 4 additions & 4 deletions net/announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void qemu_announce_timer_del(AnnounceTimer *timer, bool free_named)
}
qapi_free_strList(timer->params.interfaces);
timer->params.interfaces = NULL;
if (free_named && timer->params.has_id) {
if (free_named && timer->params.id) {
AnnounceTimer *list_timer;
/*
* Sanity check: There should only be one timer on the list with
Expand Down Expand Up @@ -157,7 +157,7 @@ static void qemu_announce_self_iter(NICState *nic, void *opaque)
skip = false;
}

trace_qemu_announce_self_iter(timer->params.has_id ? timer->params.id : "_",
trace_qemu_announce_self_iter(timer->params.id ?: "_",
nic->ncs->name,
qemu_ether_ntoa(&nic->conf->macaddr), skip);

Expand Down Expand Up @@ -199,9 +199,9 @@ void qemu_announce_self(AnnounceTimer *timer, AnnounceParameters *params)
void qmp_announce_self(AnnounceParameters *params, Error **errp)
{
AnnounceTimer *named_timer;
if (!params->has_id) {

if (!params->id) {
params->id = g_strdup("");
params->has_id = true;
}

named_timer = g_datalist_get_data(&named_timers, params->id);
Expand Down
2 changes: 1 addition & 1 deletion net/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ int net_init_hubport(const Netdev *netdev, const char *name,
assert(!peer);
hubport = &netdev->u.hubport;

if (hubport->has_netdev) {
if (hubport->netdev) {
hubpeer = qemu_find_netdev(hubport->netdev);
if (!hubpeer) {
error_setg(errp, "netdev '%s' not found", hubport->netdev);
Expand Down
2 changes: 1 addition & 1 deletion net/l2tpv3.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ int net_init_l2tpv3(const Netdev *netdev,

if (l2tpv3->has_udp && l2tpv3->udp) {
s->udp = true;
if (!(l2tpv3->has_srcport && l2tpv3->has_dstport)) {
if (!(l2tpv3->srcport && l2tpv3->dstport)) {
error_setg(errp, "need both src and dst port for udp");
goto outerr;
} else {
Expand Down
25 changes: 12 additions & 13 deletions net/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ static int net_init_nic(const Netdev *netdev, const char *name,

memset(nd, 0, sizeof(*nd));

if (nic->has_netdev) {
if (nic->netdev) {
nd->netdev = qemu_find_netdev(nic->netdev);
if (!nd->netdev) {
error_setg(errp, "netdev '%s' not found", nic->netdev);
Expand All @@ -975,19 +975,19 @@ static int net_init_nic(const Netdev *netdev, const char *name,
nd->netdev = peer;
}
nd->name = g_strdup(name);
if (nic->has_model) {
if (nic->model) {
nd->model = g_strdup(nic->model);
}
if (nic->has_addr) {
if (nic->addr) {
nd->devaddr = g_strdup(nic->addr);
}

if (nic->has_macaddr &&
if (nic->macaddr &&
net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
error_setg(errp, "invalid syntax for ethernet address");
return -1;
}
if (nic->has_macaddr &&
if (nic->macaddr &&
is_multicast_ether_addr(nd->macaddr.a)) {
error_setg(errp,
"NIC cannot have multicast MAC address (odd 1st byte)");
Expand Down Expand Up @@ -1081,7 +1081,7 @@ static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)

/* Do not add to a hub if it's a nic with a netdev= parameter. */
if (netdev->type != NET_CLIENT_DRIVER_NIC ||
!netdev->u.nic.has_netdev) {
!netdev->u.nic.netdev) {
peer = net_hub_add_port(0, NULL, NULL);
}
}
Expand Down Expand Up @@ -1295,22 +1295,21 @@ void print_net_client(Monitor *mon, NetClientState *nc)
}
}

RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
Error **errp)
RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
{
NetClientState *nc;
RxFilterInfoList *filter_list = NULL, **tail = &filter_list;

QTAILQ_FOREACH(nc, &net_clients, next) {
RxFilterInfo *info;

if (has_name && strcmp(nc->name, name) != 0) {
if (name && strcmp(nc->name, name) != 0) {
continue;
}

/* only query rx-filter information of NIC */
if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
if (has_name) {
if (name) {
error_setg(errp, "net client(%s) isn't a NIC", name);
assert(!filter_list);
return NULL;
Expand All @@ -1327,19 +1326,19 @@ RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
if (nc->info->query_rx_filter) {
info = nc->info->query_rx_filter(nc);
QAPI_LIST_APPEND(tail, info);
} else if (has_name) {
} else if (name) {
error_setg(errp, "net client(%s) doesn't support"
" rx-filter querying", name);
assert(!filter_list);
return NULL;
}

if (has_name) {
if (name) {
break;
}
}

if (filter_list == NULL && has_name) {
if (filter_list == NULL && name) {
error_setg(errp, "invalid net client name: %s", name);
}

Expand Down
4 changes: 2 additions & 2 deletions net/slirp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,8 +1153,8 @@ int net_init_slirp(const Netdev *netdev, const char *name,
ipv6 = 0;
}

vnet = user->has_net ? g_strdup(user->net) :
user->has_ip ? g_strdup_printf("%s/24", user->ip) :
vnet = user->net ? g_strdup(user->net) :
user->ip ? g_strdup_printf("%s/24", user->ip) :
NULL;

dnssearch = slirp_dnssearch(user->dnssearch);
Expand Down
18 changes: 9 additions & 9 deletions net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,19 +705,19 @@ int net_init_socket(const Netdev *netdev, const char *name,
assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
sock = &netdev->u.socket;

if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
sock->has_udp != 1) {
if (!!sock->fd + !!sock->listen + !!sock->connect + !!sock->mcast +
!!sock->udp != 1) {
error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
" is required");
return -1;
}

if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
if (sock->localaddr && !sock->mcast && !sock->udp) {
error_setg(errp, "localaddr= is only valid with mcast= or udp=");
return -1;
}

if (sock->has_fd) {
if (sock->fd) {
int fd, ret;

fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
Expand All @@ -737,23 +737,23 @@ int net_init_socket(const Netdev *netdev, const char *name,
return 0;
}

if (sock->has_listen) {
if (sock->listen) {
if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
< 0) {
return -1;
}
return 0;
}

if (sock->has_connect) {
if (sock->connect) {
if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
< 0) {
return -1;
}
return 0;
}

if (sock->has_mcast) {
if (sock->mcast) {
/* if sock->localaddr is missing, it has been initialized to "all bits
* zero" */
if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
Expand All @@ -763,8 +763,8 @@ int net_init_socket(const Netdev *netdev, const char *name,
return 0;
}

assert(sock->has_udp);
if (!sock->has_localaddr) {
assert(sock->udp);
if (!sock->localaddr) {
error_setg(errp, "localaddr= is mandatory with udp=");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion net/tap-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
assert(netdev->type == NET_CLIENT_DRIVER_TAP);
tap = &netdev->u.tap;

if (!tap->has_ifname) {
if (!tap->ifname) {
error_report("tap: no interface name");
return -1;
}
Expand Down
51 changes: 25 additions & 26 deletions net/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ int net_init_bridge(const Netdev *netdev, const char *name,

assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
bridge = &netdev->u.bridge;
helper = bridge->has_helper ? bridge->helper : NULL;
br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
helper = bridge->helper;
br = bridge->br ?: DEFAULT_BRIDGE_INTERFACE;

fd = net_bridge_run_helper(helper, br, errp);
if (fd == -1) {
Expand Down Expand Up @@ -688,9 +688,9 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
goto failed;
}

if (tap->has_fd || tap->has_fds) {
if (tap->fd || tap->fds) {
qemu_set_info_str(&s->nc, "fd=%d", fd);
} else if (tap->has_helper) {
} else if (tap->helper) {
qemu_set_info_str(&s->nc, "helper=%s", tap->helper);
} else {
qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname,
Expand Down Expand Up @@ -812,21 +812,21 @@ int net_init_tap(const Netdev *netdev, const char *name,
assert(netdev->type == NET_CLIENT_DRIVER_TAP);
tap = &netdev->u.tap;
queues = tap->has_queues ? tap->queues : 1;
vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
script = tap->has_script ? tap->script : NULL;
downscript = tap->has_downscript ? tap->downscript : NULL;
vhostfdname = tap->vhostfd;
script = tap->script;
downscript = tap->downscript;

/* QEMU hubs do not support multiqueue tap, in this case peer is set.
* For -netdev, peer is always NULL. */
if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
if (peer && (tap->has_queues || tap->fds || tap->vhostfds)) {
error_setg(errp, "Multiqueue tap cannot be used with hubs");
return -1;
}

if (tap->has_fd) {
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
tap->has_fds || tap->has_vhostfds) {
if (tap->fd) {
if (tap->ifname || tap->script || tap->downscript ||
tap->has_vnet_hdr || tap->helper || tap->has_queues ||
tap->fds || tap->vhostfds) {
error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
"helper=, queues=, fds=, and vhostfds= "
"are invalid with fd=");
Expand Down Expand Up @@ -859,14 +859,14 @@ int net_init_tap(const Netdev *netdev, const char *name,
close(fd);
return -1;
}
} else if (tap->has_fds) {
} else if (tap->fds) {
char **fds;
char **vhost_fds;
int nfds = 0, nvhosts = 0;

if (tap->has_ifname || tap->has_script || tap->has_downscript ||
tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
tap->has_vhostfd) {
if (tap->ifname || tap->script || tap->downscript ||
tap->has_vnet_hdr || tap->helper || tap->has_queues ||
tap->vhostfd) {
error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
"helper=, queues=, and vhostfd= "
"are invalid with fds=");
Expand All @@ -877,7 +877,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
vhost_fds = g_new0(char *, MAX_TAP_QUEUES);

nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
if (tap->has_vhostfds) {
if (tap->vhostfds) {
nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
if (nfds != nvhosts) {
error_setg(errp, "The number of fds passed does not match "
Expand Down Expand Up @@ -916,7 +916,7 @@ int net_init_tap(const Netdev *netdev, const char *name,

net_init_tap_one(tap, peer, "tap", name, ifname,
script, downscript,
tap->has_vhostfds ? vhost_fds[i] : NULL,
tap->vhostfds ? vhost_fds[i] : NULL,
vnet_hdr, fd, &err);
if (err) {
error_propagate(errp, err);
Expand All @@ -935,17 +935,16 @@ int net_init_tap(const Netdev *netdev, const char *name,
g_free(fds);
g_free(vhost_fds);
return ret;
} else if (tap->has_helper) {
if (tap->has_ifname || tap->has_script || tap->has_downscript ||
tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
} else if (tap->helper) {
if (tap->ifname || tap->script || tap->downscript ||
tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) {
error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
"queues=, and vhostfds= are invalid with helper=");
return -1;
}

fd = net_bridge_run_helper(tap->helper,
tap->has_br ?
tap->br : DEFAULT_BRIDGE_INTERFACE,
tap->br ?: DEFAULT_BRIDGE_INTERFACE,
errp);
if (fd == -1) {
return -1;
Expand All @@ -972,7 +971,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
} else {
g_autofree char *default_script = NULL;
g_autofree char *default_downscript = NULL;
if (tap->has_vhostfds) {
if (tap->vhostfds) {
error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
return -1;
}
Expand All @@ -985,7 +984,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
}

if (tap->has_ifname) {
if (tap->ifname) {
pstrcpy(ifname, sizeof ifname, tap->ifname);
} else {
ifname[0] = '\0';
Expand All @@ -998,7 +997,7 @@ int net_init_tap(const Netdev *netdev, const char *name,
return -1;
}

if (queues > 1 && i == 0 && !tap->has_ifname) {
if (queues > 1 && i == 0 && !tap->ifname) {
if (tap_fd_get_ifname(fd, ifname)) {
error_setg(errp, "Fail to get ifname");
close(fd);
Expand Down
Loading

0 comments on commit 7480874

Please sign in to comment.