tap-linux.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2009 Red Hat, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "tap_int.h"
  27. #include "tap-linux.h"
  28. #include "net/tap.h"
  29. #include <net/if.h>
  30. #include <sys/ioctl.h>
  31. #include "sysemu/sysemu.h"
  32. #include "qapi/error.h"
  33. #include "qemu/error-report.h"
  34. #include "qemu/cutils.h"
  35. #define PATH_NET_TUN "/dev/net/tun"
  36. int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
  37. int vnet_hdr_required, int mq_required, Error **errp)
  38. {
  39. struct ifreq ifr;
  40. int fd, ret;
  41. int len = sizeof(struct virtio_net_hdr);
  42. unsigned int features;
  43. TFR(fd = open(PATH_NET_TUN, O_RDWR));
  44. if (fd < 0) {
  45. error_setg_errno(errp, errno, "could not open %s", PATH_NET_TUN);
  46. return -1;
  47. }
  48. memset(&ifr, 0, sizeof(ifr));
  49. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  50. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  51. error_report("warning: TUNGETFEATURES failed: %s", strerror(errno));
  52. features = 0;
  53. }
  54. if (features & IFF_ONE_QUEUE) {
  55. ifr.ifr_flags |= IFF_ONE_QUEUE;
  56. }
  57. if (*vnet_hdr) {
  58. if (features & IFF_VNET_HDR) {
  59. *vnet_hdr = 1;
  60. ifr.ifr_flags |= IFF_VNET_HDR;
  61. } else {
  62. *vnet_hdr = 0;
  63. }
  64. if (vnet_hdr_required && !*vnet_hdr) {
  65. error_setg(errp, "vnet_hdr=1 requested, but no kernel "
  66. "support for IFF_VNET_HDR available");
  67. close(fd);
  68. return -1;
  69. }
  70. /*
  71. * Make sure vnet header size has the default value: for a persistent
  72. * tap it might have been modified e.g. by another instance of qemu.
  73. * Ignore errors since old kernels do not support this ioctl: in this
  74. * case the header size implicitly has the correct value.
  75. */
  76. ioctl(fd, TUNSETVNETHDRSZ, &len);
  77. }
  78. if (mq_required) {
  79. if (!(features & IFF_MULTI_QUEUE)) {
  80. error_setg(errp, "multiqueue required, but no kernel "
  81. "support for IFF_MULTI_QUEUE available");
  82. close(fd);
  83. return -1;
  84. } else {
  85. ifr.ifr_flags |= IFF_MULTI_QUEUE;
  86. }
  87. }
  88. if (ifname[0] != '\0')
  89. pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
  90. else
  91. pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
  92. ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
  93. if (ret != 0) {
  94. if (ifname[0] != '\0') {
  95. error_setg_errno(errp, errno, "could not configure %s (%s)",
  96. PATH_NET_TUN, ifr.ifr_name);
  97. } else {
  98. error_setg_errno(errp, errno, "could not configure %s",
  99. PATH_NET_TUN);
  100. }
  101. close(fd);
  102. return -1;
  103. }
  104. pstrcpy(ifname, ifname_size, ifr.ifr_name);
  105. fcntl(fd, F_SETFL, O_NONBLOCK);
  106. return fd;
  107. }
  108. /* sndbuf implements a kind of flow control for tap.
  109. * Unfortunately when it's enabled, and packets are sent
  110. * to other guests on the same host, the receiver
  111. * can lock up the transmitter indefinitely.
  112. *
  113. * To avoid packet loss, sndbuf should be set to a value lower than the tx
  114. * queue capacity of any destination network interface.
  115. * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
  116. * a good value, given a 1500 byte MTU.
  117. */
  118. #define TAP_DEFAULT_SNDBUF 0
  119. void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
  120. {
  121. int sndbuf;
  122. sndbuf = !tap->has_sndbuf ? TAP_DEFAULT_SNDBUF :
  123. tap->sndbuf > INT_MAX ? INT_MAX :
  124. tap->sndbuf;
  125. if (!sndbuf) {
  126. sndbuf = INT_MAX;
  127. }
  128. if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && tap->has_sndbuf) {
  129. error_setg_errno(errp, errno, "TUNSETSNDBUF ioctl failed");
  130. }
  131. }
  132. int tap_probe_vnet_hdr(int fd)
  133. {
  134. struct ifreq ifr;
  135. if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
  136. error_report("TUNGETIFF ioctl() failed: %s", strerror(errno));
  137. return 0;
  138. }
  139. return ifr.ifr_flags & IFF_VNET_HDR;
  140. }
  141. int tap_probe_has_ufo(int fd)
  142. {
  143. unsigned offload;
  144. offload = TUN_F_CSUM | TUN_F_UFO;
  145. if (ioctl(fd, TUNSETOFFLOAD, offload) < 0)
  146. return 0;
  147. return 1;
  148. }
  149. /* Verify that we can assign given length */
  150. int tap_probe_vnet_hdr_len(int fd, int len)
  151. {
  152. int orig;
  153. if (ioctl(fd, TUNGETVNETHDRSZ, &orig) == -1) {
  154. return 0;
  155. }
  156. if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
  157. return 0;
  158. }
  159. /* Restore original length: we can't handle failure. */
  160. if (ioctl(fd, TUNSETVNETHDRSZ, &orig) == -1) {
  161. fprintf(stderr, "TUNGETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
  162. strerror(errno));
  163. abort();
  164. return -errno;
  165. }
  166. return 1;
  167. }
  168. void tap_fd_set_vnet_hdr_len(int fd, int len)
  169. {
  170. if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
  171. fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
  172. strerror(errno));
  173. abort();
  174. }
  175. }
  176. int tap_fd_set_vnet_le(int fd, int is_le)
  177. {
  178. int arg = is_le ? 1 : 0;
  179. if (!ioctl(fd, TUNSETVNETLE, &arg)) {
  180. return 0;
  181. }
  182. /* Check if our kernel supports TUNSETVNETLE */
  183. if (errno == EINVAL) {
  184. return -errno;
  185. }
  186. error_report("TUNSETVNETLE ioctl() failed: %s.", strerror(errno));
  187. abort();
  188. }
  189. int tap_fd_set_vnet_be(int fd, int is_be)
  190. {
  191. int arg = is_be ? 1 : 0;
  192. if (!ioctl(fd, TUNSETVNETBE, &arg)) {
  193. return 0;
  194. }
  195. /* Check if our kernel supports TUNSETVNETBE */
  196. if (errno == EINVAL) {
  197. return -errno;
  198. }
  199. error_report("TUNSETVNETBE ioctl() failed: %s.", strerror(errno));
  200. abort();
  201. }
  202. void tap_fd_set_offload(int fd, int csum, int tso4,
  203. int tso6, int ecn, int ufo)
  204. {
  205. unsigned int offload = 0;
  206. /* Check if our kernel supports TUNSETOFFLOAD */
  207. if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
  208. return;
  209. }
  210. if (csum) {
  211. offload |= TUN_F_CSUM;
  212. if (tso4)
  213. offload |= TUN_F_TSO4;
  214. if (tso6)
  215. offload |= TUN_F_TSO6;
  216. if ((tso4 || tso6) && ecn)
  217. offload |= TUN_F_TSO_ECN;
  218. if (ufo)
  219. offload |= TUN_F_UFO;
  220. }
  221. if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
  222. offload &= ~TUN_F_UFO;
  223. if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
  224. fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
  225. strerror(errno));
  226. }
  227. }
  228. }
  229. /* Enable a specific queue of tap. */
  230. int tap_fd_enable(int fd)
  231. {
  232. struct ifreq ifr;
  233. int ret;
  234. memset(&ifr, 0, sizeof(ifr));
  235. ifr.ifr_flags = IFF_ATTACH_QUEUE;
  236. ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
  237. if (ret != 0) {
  238. error_report("could not enable queue");
  239. }
  240. return ret;
  241. }
  242. /* Disable a specific queue of tap/ */
  243. int tap_fd_disable(int fd)
  244. {
  245. struct ifreq ifr;
  246. int ret;
  247. memset(&ifr, 0, sizeof(ifr));
  248. ifr.ifr_flags = IFF_DETACH_QUEUE;
  249. ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
  250. if (ret != 0) {
  251. error_report("could not disable queue");
  252. }
  253. return ret;
  254. }
  255. int tap_fd_get_ifname(int fd, char *ifname)
  256. {
  257. struct ifreq ifr;
  258. if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
  259. error_report("TUNGETIFF ioctl() failed: %s",
  260. strerror(errno));
  261. return -1;
  262. }
  263. pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name);
  264. return 0;
  265. }