net/tap: net_init_tap(): refactor parameter checking
Move checks to the top of the function to simplify further refactoring. Merge duplicated checks. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Ben Chaney <bchaney@akamai.com> Signed-off-by: Jason Wang <jasowang@redhat.com>
Vladimir Sementsov-Ogievskiy committed
Mar 18, 2026 at 14:31 UTC
24f458abe76db8a3b726343780e6f3f2fec0fe90
1 file changed
+23
-30
net/tap.c
+23
-30
@@ -841,16 +841,30 @@ int net_init_tap(const Netdev *netdev, const char *name,
841
return -1;
842
}
843
844
- if (tap->fd) {
845
- if (tap->ifname || tap->script || tap->downscript ||
846
- tap->has_vnet_hdr || tap->helper || tap->has_queues ||
847
- tap->fds || tap->vhostfds) {
848
- error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
849
- "helper=, queues=, fds=, and vhostfds= "
850
- "are invalid with fd=");
851
- return -1;
852
- }
844
+ if (tap->has_queues + !!tap->helper + !!tap->fds + !!tap->fd > 1) {
845
+ error_setg(errp, "queues=, helper=, fds= and fd= are mutual exclusive");
846
+ return -1;
847
+ }
848
849
+ if ((tap->fd || tap->fds || tap->helper) &&
850
+ (tap->ifname || tap->script || tap->downscript ||
851
+ tap->has_vnet_hdr)) {
852
+ error_setg(errp, "ifname=, script=, downscript=, vnet_hdr= "
853
+ "are invalid with fd=/fds=/helper=");
854
+ return -1;
855
+ }
856
+
857
+ if (tap->vhostfds && !tap->fds) {
858
+ error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
859
+ return -1;
860
+ }
861
+
862
+ if (tap->vhostfd && tap->fds) {
863
+ error_setg(errp, "vhostfd= is invalid with fds=");
864
+ return -1;
865
+ }
866
+
867
+ if (tap->fd) {
868
fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
869
if (fd == -1) {
870
return -1;
@@ -878,15 +892,6 @@ int net_init_tap(const Netdev *netdev, const char *name,
892
char **vhost_fds;
893
int nfds = 0, nvhosts = 0;
894
881
- if (tap->ifname || tap->script || tap->downscript ||
882
- tap->has_vnet_hdr || tap->helper || tap->has_queues ||
883
- tap->vhostfd) {
884
- error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
885
- "helper=, queues=, and vhostfd= "
886
- "are invalid with fds=");
887
- return -1;
888
- }
889
-
895
fds = g_new0(char *, MAX_TAP_QUEUES);
896
vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
897
@@ -946,13 +951,6 @@ free_fail:
951
g_free(vhost_fds);
952
return ret;
953
} else if (tap->helper) {
949
- if (tap->ifname || tap->script || tap->downscript ||
950
- tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) {
951
- error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
952
- "queues=, and vhostfds= are invalid with helper=");
953
- return -1;
954
- }
955
-
954
fd = net_bridge_run_helper(tap->helper,
955
tap->br ?: DEFAULT_BRIDGE_INTERFACE,
956
errp);
@@ -981,11 +979,6 @@ free_fail:
979
g_autofree char *downscript =
980
tap_parse_script(tap->downscript, DEFAULT_NETWORK_DOWN_SCRIPT);
981
984
- if (tap->vhostfds) {
985
- error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
986
- return -1;
987
- }
988
-
982
if (tap->ifname) {
983
pstrcpy(ifname, sizeof ifname, tap->ifname);
984
} else {