Commit 10ae5a7a98f7c1d901fbb6658324e9c4c4fec8cf

Authored by Jan Kiszka
Committed by Mark McLoughlin
1 parent c8decae2

net: Improve parameter error reporting

As host network devices can also be instantiated via the monitor, errors
should then be reported to the related monitor instead of stderr. This
requires larger refactoring, so this patch starts small with introducing
a helper to catch both cases and convert net_client_init as well as
net_slirp_redir.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Mark McLoughlin <markmc@redhat.com>
hw/pci-hotplug.c
... ... @@ -33,11 +33,12 @@
33 33 #include "virtio-blk.h"
34 34  
35 35 #if defined(TARGET_I386) || defined(TARGET_X86_64)
36   -static PCIDevice *qemu_pci_hot_add_nic(PCIBus *pci_bus, const char *opts)
  36 +static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, PCIBus *pci_bus,
  37 + const char *opts)
37 38 {
38 39 int ret;
39 40  
40   - ret = net_client_init("nic", opts);
  41 + ret = net_client_init(mon, "nic", opts);
41 42 if (ret < 0)
42 43 return NULL;
43 44 return pci_nic_init(pci_bus, &nd_table[ret], -1, "rtl8139");
... ... @@ -149,7 +150,7 @@ void pci_device_hot_add(Monitor *mon, const char *pci_addr, const char *type,
149 150 }
150 151  
151 152 if (strcmp(type, "nic") == 0)
152   - dev = qemu_pci_hot_add_nic(pci_bus, opts);
  153 + dev = qemu_pci_hot_add_nic(mon, pci_bus, opts);
153 154 else if (strcmp(type, "storage") == 0)
154 155 dev = qemu_pci_hot_add_storage(mon, pci_bus, opts);
155 156 else
... ...
... ... @@ -532,6 +532,21 @@ ssize_t qemu_sendv_packet(VLANClientState *sender, const struct iovec *iov,
532 532 return max_len;
533 533 }
534 534  
  535 +static void config_error(Monitor *mon, const char *fmt, ...)
  536 +{
  537 + va_list ap;
  538 +
  539 + va_start(ap, fmt);
  540 + if (mon) {
  541 + monitor_vprintf(mon, fmt, ap);
  542 + } else {
  543 + fprintf(stderr, "qemu: ");
  544 + vfprintf(stderr, fmt, ap);
  545 + exit(1);
  546 + }
  547 + va_end(ap);
  548 +}
  549 +
535 550 #if defined(CONFIG_SLIRP)
536 551  
537 552 /* slirp network adapter */
... ... @@ -674,7 +689,7 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
674 689 {
675 690 int is_udp;
676 691 char buf[256], *r;
677   - const char *p, *errmsg;
  692 + const char *p;
678 693 struct in_addr guest_addr;
679 694 int host_port, guest_port;
680 695  
... ... @@ -723,20 +738,12 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
723 738 goto fail_syntax;
724 739  
725 740 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
726   - errmsg = "could not set up redirection\n";
727   - goto fail;
  741 + config_error(mon, "could not set up redirection '%s'\n", redir_str);
728 742 }
729 743 return;
730 744  
731 745 fail_syntax:
732   - errmsg = "invalid redirection format\n";
733   - fail:
734   - if (mon) {
735   - monitor_printf(mon, "%s", errmsg);
736   - } else {
737   - fprintf(stderr, "qemu: %s", errmsg);
738   - exit(1);
739   - }
  746 + config_error(mon, "invalid redirection format '%s'\n", redir_str);
740 747 }
741 748  
742 749 #ifndef _WIN32
... ... @@ -1784,7 +1791,7 @@ static void net_dump_cleanup(VLANClientState *vc)
1784 1791 qemu_free(s);
1785 1792 }
1786 1793  
1787   -static int net_dump_init(VLANState *vlan, const char *device,
  1794 +static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
1788 1795 const char *name, const char *filename, int len)
1789 1796 {
1790 1797 struct pcap_file_hdr hdr;
... ... @@ -1794,7 +1801,7 @@ static int net_dump_init(VLANState *vlan, const char *device,
1794 1801  
1795 1802 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
1796 1803 if (s->fd < 0) {
1797   - fprintf(stderr, "-net dump: can't open %s\n", filename);
  1804 + config_error(mon, "-net dump: can't open %s\n", filename);
1798 1805 return -1;
1799 1806 }
1800 1807  
... ... @@ -1809,7 +1816,7 @@ static int net_dump_init(VLANState *vlan, const char *device,
1809 1816 hdr.linktype = 1;
1810 1817  
1811 1818 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
1812   - perror("-net dump write error");
  1819 + config_error(mon, "-net dump write error: %s\n", strerror(errno));
1813 1820 close(s->fd);
1814 1821 qemu_free(s);
1815 1822 return -1;
... ... @@ -1884,7 +1891,7 @@ void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1884 1891 exit(exit_status);
1885 1892 }
1886 1893  
1887   -int net_client_init(const char *device, const char *p)
  1894 +int net_client_init(Monitor *mon, const char *device, const char *p)
1888 1895 {
1889 1896 static const char * const fd_params[] = {
1890 1897 "vlan", "name", "fd", NULL
... ... @@ -1901,7 +1908,7 @@ int net_client_init(const char *device, const char *p)
1901 1908 vlan = qemu_find_vlan(vlan_id);
1902 1909  
1903 1910 if (get_param_value(buf, sizeof(buf), "name", p)) {
1904   - name = strdup(buf);
  1911 + name = qemu_strdup(buf);
1905 1912 }
1906 1913 if (!strcmp(device, "nic")) {
1907 1914 static const char * const nic_params[] = {
... ... @@ -1912,12 +1919,12 @@ int net_client_init(const char *device, const char *p)
1912 1919 int idx = nic_get_free_idx();
1913 1920  
1914 1921 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
1915   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1916   - buf, p);
1917   - return -1;
  1922 + config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
  1923 + ret = -1;
  1924 + goto out;
1918 1925 }
1919 1926 if (idx == -1 || nb_nics >= MAX_NICS) {
1920   - fprintf(stderr, "Too Many NICs\n");
  1927 + config_error(mon, "Too Many NICs\n");
1921 1928 ret = -1;
1922 1929 goto out;
1923 1930 }
... ... @@ -1932,7 +1939,7 @@ int net_client_init(const char *device, const char *p)
1932 1939  
1933 1940 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1934 1941 if (parse_macaddr(macaddr, buf) < 0) {
1935   - fprintf(stderr, "invalid syntax for ethernet address\n");
  1942 + config_error(mon, "invalid syntax for ethernet address\n");
1936 1943 ret = -1;
1937 1944 goto out;
1938 1945 }
... ... @@ -1950,8 +1957,9 @@ int net_client_init(const char *device, const char *p)
1950 1957 } else
1951 1958 if (!strcmp(device, "none")) {
1952 1959 if (*p != '\0') {
1953   - fprintf(stderr, "qemu: 'none' takes no parameters\n");
1954   - return -1;
  1960 + config_error(mon, "'none' takes no parameters\n");
  1961 + ret = -1;
  1962 + goto out;
1955 1963 }
1956 1964 /* does nothing. It is needed to signal that no network cards
1957 1965 are wanted */
... ... @@ -1963,9 +1971,9 @@ int net_client_init(const char *device, const char *p)
1963 1971 "vlan", "name", "hostname", "restrict", "ip", NULL
1964 1972 };
1965 1973 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
1966   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1967   - buf, p);
1968   - return -1;
  1974 + config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
  1975 + ret = -1;
  1976 + goto out;
1969 1977 }
1970 1978 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1971 1979 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
... ... @@ -1986,7 +1994,7 @@ int net_client_init(const char *device, const char *p)
1986 1994 port = strtol(p, &devname, 10);
1987 1995 devname++;
1988 1996 if (port < 1 || port > 65535) {
1989   - fprintf(stderr, "vmchannel wrong port number\n");
  1997 + config_error(mon, "vmchannel wrong port number\n");
1990 1998 ret = -1;
1991 1999 goto out;
1992 2000 }
... ... @@ -1994,8 +2002,8 @@ int net_client_init(const char *device, const char *p)
1994 2002 snprintf(name, 20, "vmchannel%ld", port);
1995 2003 vmc->hd = qemu_chr_open(name, devname, NULL);
1996 2004 if (!vmc->hd) {
1997   - fprintf(stderr, "qemu: could not open vmchannel device"
1998   - "'%s'\n", devname);
  2005 + config_error(mon, "could not open vmchannel device '%s'\n",
  2006 + devname);
1999 2007 ret = -1;
2000 2008 goto out;
2001 2009 }
... ... @@ -2014,12 +2022,12 @@ int net_client_init(const char *device, const char *p)
2014 2022 char ifname[64];
2015 2023  
2016 2024 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
2017   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2018   - buf, p);
2019   - return -1;
  2025 + config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
  2026 + ret = -1;
  2027 + goto out;
2020 2028 }
2021 2029 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2022   - fprintf(stderr, "tap: no interface name\n");
  2030 + config_error(mon, "tap: no interface name\n");
2023 2031 ret = -1;
2024 2032 goto out;
2025 2033 }
... ... @@ -2035,9 +2043,9 @@ int net_client_init(const char *device, const char *p)
2035 2043 vlan->nb_host_devs++;
2036 2044 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2037 2045 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2038   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2039   - chkbuf, p);
2040   - return -1;
  2046 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2047 + ret = -1;
  2048 + goto out;
2041 2049 }
2042 2050 fd = strtol(buf, NULL, 0);
2043 2051 fcntl(fd, F_SETFL, O_NONBLOCK);
... ... @@ -2048,9 +2056,9 @@ int net_client_init(const char *device, const char *p)
2048 2056 "vlan", "name", "ifname", "script", "downscript", NULL
2049 2057 };
2050 2058 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2051   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2052   - chkbuf, p);
2053   - return -1;
  2059 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2060 + ret = -1;
  2061 + goto out;
2054 2062 }
2055 2063 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2056 2064 ifname[0] = '\0';
... ... @@ -2070,9 +2078,9 @@ int net_client_init(const char *device, const char *p)
2070 2078 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2071 2079 int fd;
2072 2080 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2073   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2074   - chkbuf, p);
2075   - return -1;
  2081 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2082 + ret = -1;
  2083 + goto out;
2076 2084 }
2077 2085 fd = strtol(buf, NULL, 0);
2078 2086 ret = -1;
... ... @@ -2083,9 +2091,9 @@ int net_client_init(const char *device, const char *p)
2083 2091 "vlan", "name", "listen", NULL
2084 2092 };
2085 2093 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
2086   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2087   - chkbuf, p);
2088   - return -1;
  2094 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2095 + ret = -1;
  2096 + goto out;
2089 2097 }
2090 2098 ret = net_socket_listen_init(vlan, device, name, buf);
2091 2099 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
... ... @@ -2093,9 +2101,9 @@ int net_client_init(const char *device, const char *p)
2093 2101 "vlan", "name", "connect", NULL
2094 2102 };
2095 2103 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
2096   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2097   - chkbuf, p);
2098   - return -1;
  2104 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2105 + ret = -1;
  2106 + goto out;
2099 2107 }
2100 2108 ret = net_socket_connect_init(vlan, device, name, buf);
2101 2109 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
... ... @@ -2103,13 +2111,13 @@ int net_client_init(const char *device, const char *p)
2103 2111 "vlan", "name", "mcast", NULL
2104 2112 };
2105 2113 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
2106   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2107   - chkbuf, p);
2108   - return -1;
  2114 + config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
  2115 + ret = -1;
  2116 + goto out;
2109 2117 }
2110 2118 ret = net_socket_mcast_init(vlan, device, name, buf);
2111 2119 } else {
2112   - fprintf(stderr, "Unknown socket options: %s\n", p);
  2120 + config_error(mon, "Unknown socket options: %s\n", p);
2113 2121 ret = -1;
2114 2122 goto out;
2115 2123 }
... ... @@ -2124,9 +2132,9 @@ int net_client_init(const char *device, const char *p)
2124 2132 int vde_port, vde_mode;
2125 2133  
2126 2134 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
2127   - fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
2128   - buf, p);
2129   - return -1;
  2135 + config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
  2136 + ret = -1;
  2137 + goto out;
2130 2138 }
2131 2139 vlan->nb_host_devs++;
2132 2140 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
... ... @@ -2157,18 +2165,17 @@ int net_client_init(const char *device, const char *p)
2157 2165 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2158 2166 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2159 2167 }
2160   - ret = net_dump_init(vlan, device, name, buf, len);
  2168 + ret = net_dump_init(mon, vlan, device, name, buf, len);
2161 2169 } else {
2162   - fprintf(stderr, "Unknown network device: %s\n", device);
  2170 + config_error(mon, "Unknown network device: %s\n", device);
2163 2171 ret = -1;
2164 2172 goto out;
2165 2173 }
2166 2174 if (ret < 0) {
2167   - fprintf(stderr, "Could not initialize device '%s'\n", device);
  2175 + config_error(mon, "Could not initialize device '%s'\n", device);
2168 2176 }
2169 2177 out:
2170   - if (name)
2171   - free(name);
  2178 + qemu_free(name);
2172 2179 return ret;
2173 2180 }
2174 2181  
... ... @@ -2206,7 +2213,7 @@ void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2206 2213 monitor_printf(mon, "invalid host network device %s\n", device);
2207 2214 return;
2208 2215 }
2209   - if (net_client_init(device, opts ? opts : "") < 0) {
  2216 + if (net_client_init(mon, device, opts ? opts : "") < 0) {
2210 2217 monitor_printf(mon, "adding host network device %s failed\n", device);
2211 2218 }
2212 2219 }
... ... @@ -2252,7 +2259,7 @@ int net_client_parse(const char *str)
2252 2259 if (*p == ',')
2253 2260 p++;
2254 2261  
2255   - return net_client_init(device, p);
  2262 + return net_client_init(NULL, device, p);
2256 2263 }
2257 2264  
2258 2265 void do_info_network(Monitor *mon)
... ...
... ... @@ -108,7 +108,7 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
108 108 void net_checksum_calculate(uint8_t *data, int length);
109 109  
110 110 /* from net.c */
111   -int net_client_init(const char *device, const char *p);
  111 +int net_client_init(Monitor *mon, const char *device, const char *p);
112 112 void net_client_uninit(NICInfo *nd);
113 113 int net_client_parse(const char *str);
114 114 void net_slirp_smb(const char *exported_dir);
... ...
... ... @@ -2699,7 +2699,7 @@ static int usb_device_add(const char *devname, int is_hotplug)
2699 2699 } else if (strstart(devname, "net:", &p)) {
2700 2700 int nic = nb_nics;
2701 2701  
2702   - if (net_client_init("nic", p) < 0)
  2702 + if (net_client_init(NULL, "nic", p) < 0)
2703 2703 return -1;
2704 2704 nd_table[nic].model = "usb";
2705 2705 dev = usb_net_init(&nd_table[nic]);
... ...