You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tap.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <linux_api.h>
  22. #include <ipxe/list.h>
  23. #include <ipxe/linux.h>
  24. #include <ipxe/malloc.h>
  25. #include <ipxe/device.h>
  26. #include <ipxe/netdevice.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/socket.h>
  31. /* This hack prevents pre-2.6.32 headers from redefining struct sockaddr */
  32. #define _SYS_SOCKET_H
  33. #define __GLIBC__ 2
  34. #include <linux/socket.h>
  35. #undef __GLIBC__
  36. #include <linux/if.h>
  37. #include <linux/if_ether.h>
  38. #include <linux/if_tun.h>
  39. #define RX_BUF_SIZE 1536
  40. #define RX_QUOTA 4
  41. /** @file
  42. *
  43. * The TAP driver.
  44. *
  45. * The TAP is a Virtual Ethernet network device.
  46. */
  47. struct tap_nic {
  48. /** Tap interface name */
  49. char * interface;
  50. /** File descriptor of the opened tap device */
  51. int fd;
  52. };
  53. /** Open the TAP device */
  54. static int tap_open(struct net_device * netdev)
  55. {
  56. struct tap_nic * nic = netdev->priv;
  57. struct ifreq ifr;
  58. int ret;
  59. nic->fd = linux_open("/dev/net/tun", O_RDWR);
  60. if (nic->fd < 0) {
  61. DBGC(nic, "tap %p open('/dev/net/tun') = %d (%s)\n", nic, nic->fd, linux_strerror(linux_errno));
  62. return nic->fd;
  63. }
  64. memset(&ifr, 0, sizeof(ifr));
  65. /* IFF_NO_PI for no extra packet information */
  66. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  67. strncpy(ifr.ifr_name, nic->interface, IFNAMSIZ);
  68. DBGC(nic, "tap %p interface = '%s'\n", nic, nic->interface);
  69. ret = linux_ioctl(nic->fd, TUNSETIFF, &ifr);
  70. if (ret != 0) {
  71. DBGC(nic, "tap %p ioctl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  72. linux_close(nic->fd);
  73. return ret;
  74. }
  75. /* Set nonblocking mode to make tap_poll easier */
  76. ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
  77. if (ret != 0) {
  78. DBGC(nic, "tap %p fcntl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
  79. linux_close(nic->fd);
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. /** Close the TAP device */
  85. static void tap_close(struct net_device *netdev)
  86. {
  87. struct tap_nic * nic = netdev->priv;
  88. linux_close(nic->fd);
  89. }
  90. /**
  91. * Transmit an ethernet packet.
  92. *
  93. * The packet can be written to the TAP device and marked as complete immediately.
  94. */
  95. static int tap_transmit(struct net_device *netdev, struct io_buffer *iobuf)
  96. {
  97. struct tap_nic * nic = netdev->priv;
  98. int rc;
  99. /* Pad and align packet */
  100. iob_pad(iobuf, ETH_ZLEN);
  101. rc = linux_write(nic->fd, iobuf->data, iobuf->tail - iobuf->data);
  102. DBGC2(nic, "tap %p wrote %d bytes\n", nic, rc);
  103. netdev_tx_complete(netdev, iobuf);
  104. return 0;
  105. }
  106. /** Poll for new packets */
  107. static void tap_poll(struct net_device *netdev)
  108. {
  109. struct tap_nic * nic = netdev->priv;
  110. struct pollfd pfd;
  111. struct io_buffer * iobuf;
  112. unsigned int quota = RX_QUOTA;
  113. int r;
  114. pfd.fd = nic->fd;
  115. pfd.events = POLLIN;
  116. if (linux_poll(&pfd, 1, 0) == -1) {
  117. DBGC(nic, "tap %p poll failed (%s)\n", nic, linux_strerror(linux_errno));
  118. return;
  119. }
  120. if ((pfd.revents & POLLIN) == 0)
  121. return;
  122. /* At this point we know there is at least one new packet to be read */
  123. iobuf = alloc_iob(RX_BUF_SIZE);
  124. if (! iobuf)
  125. goto allocfail;
  126. while (quota-- &&
  127. ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0)) {
  128. DBGC2(nic, "tap %p read %d bytes\n", nic, r);
  129. iob_put(iobuf, r);
  130. netdev_rx(netdev, iobuf);
  131. iobuf = alloc_iob(RX_BUF_SIZE);
  132. if (! iobuf)
  133. goto allocfail;
  134. }
  135. free_iob(iobuf);
  136. return;
  137. allocfail:
  138. DBGC(nic, "tap %p alloc_iob failed\n", nic);
  139. }
  140. /**
  141. * Set irq.
  142. *
  143. * Not used on linux, provide a dummy implementation.
  144. */
  145. static void tap_irq(struct net_device *netdev, int enable)
  146. {
  147. struct tap_nic *nic = netdev->priv;
  148. DBGC(nic, "tap %p irq enable = %d\n", nic, enable);
  149. }
  150. /** Tap operations */
  151. static struct net_device_operations tap_operations = {
  152. .open = tap_open,
  153. .close = tap_close,
  154. .transmit = tap_transmit,
  155. .poll = tap_poll,
  156. .irq = tap_irq,
  157. };
  158. /** Handle a device request for the tap driver */
  159. static int tap_probe(struct linux_device *device, struct linux_device_request *request)
  160. {
  161. struct linux_setting *if_setting;
  162. struct net_device *netdev;
  163. struct tap_nic *nic;
  164. int rc;
  165. netdev = alloc_etherdev(sizeof(*nic));
  166. if (! netdev)
  167. return -ENOMEM;
  168. netdev_init(netdev, &tap_operations);
  169. nic = netdev->priv;
  170. linux_set_drvdata(device, netdev);
  171. netdev->dev = &device->dev;
  172. memset(nic, 0, sizeof(*nic));
  173. /* Look for the mandatory if setting */
  174. if_setting = linux_find_setting("if", &request->settings);
  175. /* No if setting */
  176. if (! if_setting) {
  177. printf("tap missing a mandatory if setting\n");
  178. rc = -EINVAL;
  179. goto err_settings;
  180. }
  181. nic->interface = if_setting->value;
  182. snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
  183. nic->interface );
  184. device->dev.desc.bus_type = BUS_TYPE_TAP;
  185. if_setting->applied = 1;
  186. /* Apply rest of the settings */
  187. linux_apply_settings(&request->settings, &netdev->settings.settings);
  188. /* Register network device */
  189. if ((rc = register_netdev(netdev)) != 0)
  190. goto err_register;
  191. netdev_link_up(netdev);
  192. return 0;
  193. err_settings:
  194. unregister_netdev(netdev);
  195. err_register:
  196. netdev_nullify(netdev);
  197. netdev_put(netdev);
  198. return rc;
  199. }
  200. /** Remove the device */
  201. static void tap_remove(struct linux_device *device)
  202. {
  203. struct net_device *netdev = linux_get_drvdata(device);
  204. unregister_netdev(netdev);
  205. netdev_nullify(netdev);
  206. netdev_put(netdev);
  207. }
  208. /** Tap linux_driver */
  209. struct linux_driver tap_driver __linux_driver = {
  210. .name = "tap",
  211. .probe = tap_probe,
  212. .remove = tap_remove,
  213. .can_probe = 1,
  214. };