tap-bsd.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "tap_int.h"
  27. #include "qemu/cutils.h"
  28. #include "qemu/error-report.h"
  29. #if defined(__NetBSD__) || defined(__FreeBSD__)
  30. #include <sys/ioctl.h>
  31. #include <net/if.h>
  32. #include <net/if_tap.h>
  33. #endif
  34. #ifndef __FreeBSD__
  35. int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
  36. int vnet_hdr_required, int mq_required, Error **errp)
  37. {
  38. int fd;
  39. #ifdef TAPGIFNAME
  40. struct ifreq ifr;
  41. #else
  42. char *dev;
  43. struct stat s;
  44. #endif
  45. /* if no ifname is given, always start the search from tap0/tun0. */
  46. int i;
  47. char dname[100];
  48. for (i = 0; i < 10; i++) {
  49. if (*ifname) {
  50. snprintf(dname, sizeof dname, "/dev/%s", ifname);
  51. } else {
  52. snprintf(dname, sizeof dname, "/dev/tap%d", i);
  53. }
  54. fd = RETRY_ON_EINTR(open(dname, O_RDWR));
  55. if (fd >= 0) {
  56. break;
  57. }
  58. else if (errno == ENXIO || errno == ENOENT) {
  59. break;
  60. }
  61. if (*ifname) {
  62. break;
  63. }
  64. }
  65. if (fd < 0) {
  66. error_setg_errno(errp, errno, "could not open %s", dname);
  67. return -1;
  68. }
  69. #ifdef TAPGIFNAME
  70. if (ioctl(fd, TAPGIFNAME, (void *)&ifr) < 0) {
  71. error_setg_errno(errp, errno, "could not get tap name");
  72. return -1;
  73. }
  74. pstrcpy(ifname, ifname_size, ifr.ifr_name);
  75. #else
  76. if (fstat(fd, &s) < 0) {
  77. error_setg_errno(errp, errno, "could not stat %s", dname);
  78. return -1;
  79. }
  80. dev = devname(s.st_rdev, S_IFCHR);
  81. pstrcpy(ifname, ifname_size, dev);
  82. #endif
  83. if (*vnet_hdr) {
  84. /* BSD doesn't have IFF_VNET_HDR */
  85. *vnet_hdr = 0;
  86. if (vnet_hdr_required && !*vnet_hdr) {
  87. error_setg(errp, "vnet_hdr=1 requested, but no kernel "
  88. "support for IFF_VNET_HDR available");
  89. close(fd);
  90. return -1;
  91. }
  92. }
  93. g_unix_set_fd_nonblocking(fd, true, NULL);
  94. return fd;
  95. }
  96. #else /* __FreeBSD__ */
  97. #define PATH_NET_TAP "/dev/tap"
  98. static int tap_open_clone(char *ifname, int ifname_size, Error **errp)
  99. {
  100. int fd, s, ret;
  101. struct ifreq ifr;
  102. fd = RETRY_ON_EINTR(open(PATH_NET_TAP, O_RDWR));
  103. if (fd < 0) {
  104. error_setg_errno(errp, errno, "could not open %s", PATH_NET_TAP);
  105. return -1;
  106. }
  107. memset(&ifr, 0, sizeof(ifr));
  108. ret = ioctl(fd, TAPGIFNAME, (void *)&ifr);
  109. if (ret < 0) {
  110. error_setg_errno(errp, errno, "could not get tap interface name");
  111. close(fd);
  112. return -1;
  113. }
  114. if (ifname[0] != '\0') {
  115. /* User requested the interface to have a specific name */
  116. s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  117. if (s < 0) {
  118. error_setg_errno(errp, errno,
  119. "could not open socket to set interface name");
  120. close(fd);
  121. return -1;
  122. }
  123. ifr.ifr_data = ifname;
  124. ret = ioctl(s, SIOCSIFNAME, (void *)&ifr);
  125. close(s);
  126. if (ret < 0) {
  127. error_setg(errp, "could not set tap interface name");
  128. close(fd);
  129. return -1;
  130. }
  131. } else {
  132. pstrcpy(ifname, ifname_size, ifr.ifr_name);
  133. }
  134. return fd;
  135. }
  136. int tap_open(char *ifname, int ifname_size, int *vnet_hdr,
  137. int vnet_hdr_required, int mq_required, Error **errp)
  138. {
  139. int fd = -1;
  140. /* If the specified tap device already exists just use it. */
  141. if (ifname[0] != '\0') {
  142. char dname[100];
  143. snprintf(dname, sizeof dname, "/dev/%s", ifname);
  144. fd = RETRY_ON_EINTR(open(dname, O_RDWR));
  145. if (fd < 0 && errno != ENOENT) {
  146. error_setg_errno(errp, errno, "could not open %s", dname);
  147. return -1;
  148. }
  149. }
  150. if (fd < 0) {
  151. /* Tap device not specified or does not exist. */
  152. if ((fd = tap_open_clone(ifname, ifname_size, errp)) < 0) {
  153. return -1;
  154. }
  155. }
  156. if (*vnet_hdr) {
  157. /* BSD doesn't have IFF_VNET_HDR */
  158. *vnet_hdr = 0;
  159. if (vnet_hdr_required && !*vnet_hdr) {
  160. error_setg(errp, "vnet_hdr=1 requested, but no kernel "
  161. "support for IFF_VNET_HDR available");
  162. goto error;
  163. }
  164. }
  165. if (mq_required) {
  166. error_setg(errp, "mq_required requested, but no kernel support"
  167. " for IFF_MULTI_QUEUE available");
  168. goto error;
  169. }
  170. g_unix_set_fd_nonblocking(fd, true, NULL);
  171. return fd;
  172. error:
  173. close(fd);
  174. return -1;
  175. }
  176. #endif /* __FreeBSD__ */
  177. void tap_set_sndbuf(int fd, const NetdevTapOptions *tap, Error **errp)
  178. {
  179. }
  180. int tap_probe_vnet_hdr(int fd, Error **errp)
  181. {
  182. return 0;
  183. }
  184. int tap_probe_has_ufo(int fd)
  185. {
  186. return 0;
  187. }
  188. int tap_probe_vnet_hdr_len(int fd, int len)
  189. {
  190. return 0;
  191. }
  192. void tap_fd_set_vnet_hdr_len(int fd, int len)
  193. {
  194. }
  195. int tap_fd_set_vnet_le(int fd, int is_le)
  196. {
  197. return -EINVAL;
  198. }
  199. int tap_fd_set_vnet_be(int fd, int is_be)
  200. {
  201. return -EINVAL;
  202. }
  203. void tap_fd_set_offload(int fd, int csum, int tso4,
  204. int tso6, int ecn, int ufo)
  205. {
  206. }
  207. int tap_fd_enable(int fd)
  208. {
  209. return -1;
  210. }
  211. int tap_fd_disable(int fd)
  212. {
  213. return -1;
  214. }
  215. int tap_fd_get_ifname(int fd, char *ifname)
  216. {
  217. return -1;
  218. }
  219. int tap_fd_set_steering_ebpf(int fd, int prog_fd)
  220. {
  221. return -1;
  222. }