Commit 0df0ff6de70393680cea81ad696d9d74b75f88da

Authored by Mark McLoughlin
Committed by Anthony Liguori
1 parent 4a77b25e

net: add '-net tap,sndbuf=nbytes'

2.6.30 adds a new TUNSETSNDBUF ioctl() which allows a send buffer limit
for the tap device to be specified. When this limit is reached, a tap
write() will return EAGAIN and poll() will indicate the fd isn't
writable.

This allows people to tune their setups so as to avoid e.g. UDP packet
loss when the sending application in the guest out-runs the NIC in the
host.

There is no obviously sensible default setting - a suitable value
depends mostly on the capabilities of the physical NIC through which the
packets are being sent.

Also, note that when using a bridge with netfilter enabled, we currently
never get EAGAIN because netfilter causes the packet to be immediately
orphaned. Set /proc/sys/net/bridge/bridge nf-call-iptables to zero to
disable this behaviour.

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 2 changed files with 30 additions and 5 deletions
... ... @@ -1162,6 +1162,18 @@ static void tap_send(void *opaque)
1162 1162 } while (size > 0);
1163 1163 }
1164 1164  
  1165 +static void tap_set_sndbuf(TAPState *s, int sndbuf, Monitor *mon)
  1166 +{
  1167 +#ifdef TUNSETSNDBUF
  1168 + if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1) {
  1169 + config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
  1170 + strerror(errno));
  1171 + }
  1172 +#else
  1173 + config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
  1174 +#endif
  1175 +}
  1176 +
1165 1177 static void tap_cleanup(VLANClientState *vc)
1166 1178 {
1167 1179 TAPState *s = vc->opaque;
... ... @@ -2141,9 +2153,6 @@ void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2141 2153  
2142 2154 int net_client_init(Monitor *mon, const char *device, const char *p)
2143 2155 {
2144   - static const char * const fd_params[] = {
2145   - "vlan", "name", "fd", NULL
2146   - };
2147 2156 char buf[1024];
2148 2157 int vlan_id, ret;
2149 2158 VLANState *vlan;
... ... @@ -2298,6 +2307,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2298 2307 int fd;
2299 2308 vlan->nb_host_devs++;
2300 2309 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
  2310 + static const char * const fd_params[] = {
  2311 + "vlan", "name", "fd", "sndbuf", NULL
  2312 + };
2301 2313 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2302 2314 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2303 2315 ret = -1;
... ... @@ -2308,7 +2320,7 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2308 2320 s = net_tap_fd_init(vlan, device, name, fd);
2309 2321 } else {
2310 2322 static const char * const tap_params[] = {
2311   - "vlan", "name", "ifname", "script", "downscript", NULL
  2323 + "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
2312 2324 };
2313 2325 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2314 2326 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
... ... @@ -2327,6 +2339,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2327 2339 s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2328 2340 }
2329 2341 if (s != NULL) {
  2342 + if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
  2343 + tap_set_sndbuf(s, atoi(buf), mon);
  2344 + }
2330 2345 ret = 0;
2331 2346 } else {
2332 2347 ret = -1;
... ... @@ -2336,6 +2351,9 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
2336 2351 if (!strcmp(device, "socket")) {
2337 2352 char chkbuf[64];
2338 2353 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
  2354 + static const char * const fd_params[] = {
  2355 + "vlan", "name", "fd", NULL
  2356 + };
2339 2357 int fd;
2340 2358 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2341 2359 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
... ...
qemu-options.hx
... ... @@ -747,12 +747,19 @@ DEF(&quot;net&quot;, HAS_ARG, QEMU_OPTION_net,
747 747 "-net tap[,vlan=n][,name=str],ifname=name\n"
748 748 " connect the host TAP network interface to VLAN 'n'\n"
749 749 #else
750   - "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
  750 + "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]"
  751 +#ifdef TUNSETSNDBUF
  752 + "[,sndbuf=nbytes]"
  753 +#endif
  754 + "\n"
751 755 " connect the host TAP network interface to VLAN 'n' and use the\n"
752 756 " network scripts 'file' (default=%s)\n"
753 757 " and 'dfile' (default=%s);\n"
754 758 " use '[down]script=no' to disable script execution;\n"
755 759 " use 'fd=h' to connect to an already opened TAP interface\n"
  760 +#ifdef TUNSETSNDBUF
  761 + " use 'sndbuf=nbytes' to limit the size of the send buffer\n"
  762 +#endif
756 763 #endif
757 764 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
758 765 " connect the vlan 'n' to another VLAN using a socket connection\n"
... ...