tap-linux.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 "qapi/error.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/cutils.h"
  34. #define PATH_NET_TUN "/dev/net/tun"
  35. int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
  36. int vnet_hdr_required, int mq_required, Error **errp)
  37. {
  38. struct ifreq ifr;
  39. int fd, ret;
  40. int len = sizeof(struct virtio_net_hdr);
  41. unsigned int features;
  42. fd = RETRY_ON_EINTR(open(PATH_NET_TUN, O_RDWR));
  43. if (fd < 0) {
  44. error_setg_errno(errp, errno, "could not open %s", PATH_NET_TUN);
  45. return -1;
  46. }
  47. memset(&ifr, 0, sizeof(ifr));
  48. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  49. if (ioctl(fd, TUNGETFEATURES, &features) == -1) {
  50. warn_report("TUNGETFEATURES failed: %s", strerror(errno));
  51. features = 0;
  52. }
  53. if (features & IFF_ONE_QUEUE) {
  54. ifr.ifr_flags |= IFF_ONE_QUEUE;
  55. }
  56. if (*vnet_hdr) {
  57. if (features & IFF_VNET_HDR) {
  58. *vnet_hdr = 1;
  59. ifr.ifr_flags |= IFF_VNET_HDR;
  60. } else {
  61. *vnet_hdr = 0;
  62. }
  63. if (vnet_hdr_required && !*vnet_hdr) {
  64. error_setg(errp, "vnet_hdr=1 requested, but no kernel "
  65. "support for IFF_VNET_HDR available");
  66. close(fd);
  67. return -1;
  68. }
  69. /*
  70. * Make sure vnet header size has the default value: for a persistent
  71. * tap it might have been modified e.g. by another instance of qemu.
  72. * Ignore errors since old kernels do not support this ioctl: in this
  73. * case the header size implicitly has the correct value.
  74. */
  75. ioctl(fd, TUNSETVNETHDRSZ, &len);
  76. }
  77. if (mq_required) {
  78. if (!(features & IFF_MULTI_QUEUE)) {
  79. error_setg(errp, "multiqueue required, but no kernel "
  80. "support for IFF_MULTI_QUEUE available");
  81. close(fd);
  82. return -1;
  83. } else {
  84. ifr.ifr_flags |= IFF_MULTI_QUEUE;
  85. }
  86. }
  87. if (ifname[0] != '\0')
  88. pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
  89. else
  90. pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
  91. ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
  92. if (ret != 0) {
  93. if (ifname[0] != '\0') {
  94. error_setg_errno(errp, errno, "could not configure %s (%s)",
  95. PATH_NET_TUN, ifr.ifr_name);
  96. } else {
  97. error_setg_errno(errp, errno, "could not configure %s",
  98. PATH_NET_TUN);
  99. }
  100. close(fd);
  101. return -1;
  102. }
  103. pstrcpy(ifname, ifname_size, ifr.ifr_name);
  104. g_unix_set_fd_nonblocking(fd, true, NULL);
  105. return fd;
  106. }
  107. /* sndbuf implements a kind of flow control for tap.
  108. * Unfortunately when it's enabled, and packets are sent
  109. * to other guests on the same host, the receiver
  110. * can lock up the transmitter indefinitely.
  111. *
  112. * To avoid packet loss, sndbuf should be set to a value lower than the tx
  113. * queue capacity of any destination network interface.
  114. * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
  115. * a good value, given a 1500 byte MTU.
  116. */
  117. #define TAP_DEFAULT_SNDBUF 0
  118. void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
  119. {
  120. int sndbuf;
  121. sndbuf = !tap->has_sndbuf ? TAP_DEFAULT_SNDBUF :
  122. tap->sndbuf > INT_MAX ? INT_MAX :
  123. tap->sndbuf;
  124. if (!sndbuf) {
  125. sndbuf = INT_MAX;
  126. }
  127. if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && tap->has_sndbuf) {
  128. error_setg_errno(errp, errno, "TUNSETSNDBUF ioctl failed");
  129. }
  130. }
  131. int tap_probe_vnet_hdr(int fd, Error **errp)
  132. {
  133. struct ifreq ifr;
  134. memset(&ifr, 0, sizeof(ifr));
  135. if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
  136. /* TUNGETIFF is available since kernel v2.6.27 */
  137. error_setg_errno(errp, errno,
  138. "Unable to query TUNGETIFF on FD %d", fd);
  139. return -1;
  140. }
  141. return ifr.ifr_flags & IFF_VNET_HDR;
  142. }
  143. int tap_probe_has_ufo(int fd)
  144. {
  145. unsigned offload;
  146. offload = TUN_F_CSUM | TUN_F_UFO;
  147. if (ioctl(fd, TUNSETOFFLOAD, offload) < 0)
  148. return 0;
  149. return 1;
  150. }
  151. /* Verify that we can assign given length */
  152. int tap_probe_vnet_hdr_len(int fd, int len)
  153. {
  154. int orig;
  155. if (ioctl(fd, TUNGETVNETHDRSZ, &orig) == -1) {
  156. return 0;
  157. }
  158. if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
  159. return 0;
  160. }
  161. /* Restore original length: we can't handle failure. */
  162. if (ioctl(fd, TUNSETVNETHDRSZ, &orig) == -1) {
  163. fprintf(stderr, "TUNGETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
  164. strerror(errno));
  165. abort();
  166. return -errno;
  167. }
  168. return 1;
  169. }
  170. void tap_fd_set_vnet_hdr_len(int fd, int len)
  171. {
  172. if (ioctl(fd, TUNSETVNETHDRSZ, &len) == -1) {
  173. fprintf(stderr, "TUNSETVNETHDRSZ ioctl() failed: %s. Exiting.\n",
  174. strerror(errno));
  175. abort();
  176. }
  177. }
  178. int tap_fd_set_vnet_le(int fd, int is_le)
  179. {
  180. int arg = is_le ? 1 : 0;
  181. if (!ioctl(fd, TUNSETVNETLE, &arg)) {
  182. return 0;
  183. }
  184. /* Check if our kernel supports TUNSETVNETLE */
  185. if (errno == EINVAL) {
  186. return -errno;
  187. }
  188. error_report("TUNSETVNETLE ioctl() failed: %s.", strerror(errno));
  189. abort();
  190. }
  191. int tap_fd_set_vnet_be(int fd, int is_be)
  192. {
  193. int arg = is_be ? 1 : 0;
  194. if (!ioctl(fd, TUNSETVNETBE, &arg)) {
  195. return 0;
  196. }
  197. /* Check if our kernel supports TUNSETVNETBE */
  198. if (errno == EINVAL) {
  199. return -errno;
  200. }
  201. error_report("TUNSETVNETBE ioctl() failed: %s.", strerror(errno));
  202. abort();
  203. }
  204. void tap_fd_set_offload(int fd, int csum, int tso4,
  205. int tso6, int ecn, int ufo)
  206. {
  207. unsigned int offload = 0;
  208. /* Check if our kernel supports TUNSETOFFLOAD */
  209. if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
  210. return;
  211. }
  212. if (csum) {
  213. offload |= TUN_F_CSUM;
  214. if (tso4)
  215. offload |= TUN_F_TSO4;
  216. if (tso6)
  217. offload |= TUN_F_TSO6;
  218. if ((tso4 || tso6) && ecn)
  219. offload |= TUN_F_TSO_ECN;
  220. if (ufo)
  221. offload |= TUN_F_UFO;
  222. }
  223. if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
  224. offload &= ~TUN_F_UFO;
  225. if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
  226. fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
  227. strerror(errno));
  228. }
  229. }
  230. }
  231. /* Enable a specific queue of tap. */
  232. int tap_fd_enable(int fd)
  233. {
  234. struct ifreq ifr;
  235. int ret;
  236. memset(&ifr, 0, sizeof(ifr));
  237. ifr.ifr_flags = IFF_ATTACH_QUEUE;
  238. ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
  239. if (ret != 0) {
  240. error_report("could not enable queue");
  241. }
  242. return ret;
  243. }
  244. /* Disable a specific queue of tap/ */
  245. int tap_fd_disable(int fd)
  246. {
  247. struct ifreq ifr;
  248. int ret;
  249. memset(&ifr, 0, sizeof(ifr));
  250. ifr.ifr_flags = IFF_DETACH_QUEUE;
  251. ret = ioctl(fd, TUNSETQUEUE, (void *) &ifr);
  252. if (ret != 0) {
  253. error_report("could not disable queue");
  254. }
  255. return ret;
  256. }
  257. int tap_fd_get_ifname(int fd, char *ifname)
  258. {
  259. struct ifreq ifr;
  260. if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
  261. error_report("TUNGETIFF ioctl() failed: %s",
  262. strerror(errno));
  263. return -1;
  264. }
  265. pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name);
  266. return 0;
  267. }
  268. int tap_fd_set_steering_ebpf(int fd, int prog_fd)
  269. {
  270. if (ioctl(fd, TUNSETSTEERINGEBPF, (void *) &prog_fd) != 0) {
  271. error_report("Issue while setting TUNSETSTEERINGEBPF:"
  272. " %s with fd: %d, prog_fd: %d",
  273. strerror(errno), fd, prog_fd);
  274. return -1;
  275. }
  276. return 0;
  277. }