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
19 #include <errno.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <linux_api.h>
23 #include <ipxe/list.h>
24 #include <ipxe/linux.h>
25 #include <ipxe/malloc.h>
26 #include <ipxe/device.h>
27 #include <ipxe/netdevice.h>
28 #include <ipxe/iobuf.h>
29 #include <ipxe/ethernet.h>
30 #include <ipxe/settings.h>
31 #include <ipxe/socket.h>
32
33 /* This hack prevents pre-2.6.32 headers from redefining struct sockaddr */
34 #define _SYS_SOCKET_H
35 #define __GLIBC__ 2
36 #include <linux/socket.h>
37 #undef __GLIBC__
38 #include <linux/if.h>
39 #include <linux/if_ether.h>
40 #include <linux/if_tun.h>
41
42 #define RX_BUF_SIZE 1536
43 #define RX_QUOTA 4
44
45 /** @file
46 *
47 * The TAP driver.
48 *
49 * The TAP is a Virtual Ethernet network device.
50 */
51
52 struct tap_nic {
53 /** Tap interface name */
54 char * interface;
55 /** File descriptor of the opened tap device */
56 int fd;
57 };
58
59 /** Open the TAP device */
tap_open(struct net_device * netdev)60 static int tap_open(struct net_device * netdev)
61 {
62 struct tap_nic * nic = netdev->priv;
63 struct ifreq ifr;
64 int ret;
65
66 nic->fd = linux_open("/dev/net/tun", O_RDWR);
67 if (nic->fd < 0) {
68 DBGC(nic, "tap %p open('/dev/net/tun') = %d (%s)\n", nic, nic->fd, linux_strerror(linux_errno));
69 return nic->fd;
70 }
71
72 memset(&ifr, 0, sizeof(ifr));
73 /* IFF_NO_PI for no extra packet information */
74 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
75 strncpy(ifr.ifr_name, nic->interface, IFNAMSIZ);
76 DBGC(nic, "tap %p interface = '%s'\n", nic, nic->interface);
77
78 ret = linux_ioctl(nic->fd, TUNSETIFF, &ifr);
79
80 if (ret != 0) {
81 DBGC(nic, "tap %p ioctl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
82 linux_close(nic->fd);
83 return ret;
84 }
85
86 /* Set nonblocking mode to make tap_poll easier */
87 ret = linux_fcntl(nic->fd, F_SETFL, O_NONBLOCK);
88
89 if (ret != 0) {
90 DBGC(nic, "tap %p fcntl(%d, ...) = %d (%s)\n", nic, nic->fd, ret, linux_strerror(linux_errno));
91 linux_close(nic->fd);
92 return ret;
93 }
94
95 return 0;
96 }
97
98 /** Close the TAP device */
tap_close(struct net_device * netdev)99 static void tap_close(struct net_device *netdev)
100 {
101 struct tap_nic * nic = netdev->priv;
102 linux_close(nic->fd);
103 }
104
105 /**
106 * Transmit an ethernet packet.
107 *
108 * The packet can be written to the TAP device and marked as complete immediately.
109 */
tap_transmit(struct net_device * netdev,struct io_buffer * iobuf)110 static int tap_transmit(struct net_device *netdev, struct io_buffer *iobuf)
111 {
112 struct tap_nic * nic = netdev->priv;
113 int rc;
114
115 /* Pad and align packet */
116 iob_pad(iobuf, ETH_ZLEN);
117
118 rc = linux_write(nic->fd, iobuf->data, iobuf->tail - iobuf->data);
119 DBGC2(nic, "tap %p wrote %d bytes\n", nic, rc);
120 netdev_tx_complete(netdev, iobuf);
121
122 return 0;
123 }
124
125 /** Poll for new packets */
tap_poll(struct net_device * netdev)126 static void tap_poll(struct net_device *netdev)
127 {
128 struct tap_nic * nic = netdev->priv;
129 struct pollfd pfd;
130 struct io_buffer * iobuf;
131 unsigned int quota = RX_QUOTA;
132 int r;
133
134 pfd.fd = nic->fd;
135 pfd.events = POLLIN;
136 if (linux_poll(&pfd, 1, 0) == -1) {
137 DBGC(nic, "tap %p poll failed (%s)\n", nic, linux_strerror(linux_errno));
138 return;
139 }
140 if ((pfd.revents & POLLIN) == 0)
141 return;
142
143 /* At this point we know there is at least one new packet to be read */
144
145 iobuf = alloc_iob(RX_BUF_SIZE);
146 if (! iobuf)
147 goto allocfail;
148
149 while (quota-- &&
150 ((r = linux_read(nic->fd, iobuf->data, RX_BUF_SIZE)) > 0)) {
151 DBGC2(nic, "tap %p read %d bytes\n", nic, r);
152
153 iob_put(iobuf, r);
154 netdev_rx(netdev, iobuf);
155
156 iobuf = alloc_iob(RX_BUF_SIZE);
157 if (! iobuf)
158 goto allocfail;
159 }
160
161 free_iob(iobuf);
162 return;
163
164 allocfail:
165 DBGC(nic, "tap %p alloc_iob failed\n", nic);
166 }
167
168 /**
169 * Set irq.
170 *
171 * Not used on linux, provide a dummy implementation.
172 */
tap_irq(struct net_device * netdev,int enable)173 static void tap_irq(struct net_device *netdev, int enable)
174 {
175 struct tap_nic *nic = netdev->priv;
176
177 DBGC(nic, "tap %p irq enable = %d\n", nic, enable);
178 }
179
180 /** Tap operations */
181 static struct net_device_operations tap_operations = {
182 .open = tap_open,
183 .close = tap_close,
184 .transmit = tap_transmit,
185 .poll = tap_poll,
186 .irq = tap_irq,
187 };
188
189 /** Handle a device request for the tap driver */
tap_probe(struct linux_device * device,struct linux_device_request * request)190 static int tap_probe(struct linux_device *device, struct linux_device_request *request)
191 {
192 struct linux_setting *if_setting;
193 struct net_device *netdev;
194 struct tap_nic *nic;
195 int rc;
196
197 netdev = alloc_etherdev(sizeof(*nic));
198 if (! netdev)
199 return -ENOMEM;
200
201 netdev_init(netdev, &tap_operations);
202 nic = netdev->priv;
203 linux_set_drvdata(device, netdev);
204 netdev->dev = &device->dev;
205 memset(nic, 0, sizeof(*nic));
206
207 /* Look for the mandatory if setting */
208 if_setting = linux_find_setting("if", &request->settings);
209
210 /* No if setting */
211 if (! if_setting) {
212 printf("tap missing a mandatory if setting\n");
213 rc = -EINVAL;
214 goto err_settings;
215 }
216
217 nic->interface = if_setting->value;
218 snprintf ( device->dev.name, sizeof ( device->dev.name ), "%s",
219 nic->interface );
220 device->dev.desc.bus_type = BUS_TYPE_TAP;
221 if_setting->applied = 1;
222
223 /* Apply rest of the settings */
224 linux_apply_settings(&request->settings, &netdev->settings.settings);
225
226 /* Register network device */
227 if ((rc = register_netdev(netdev)) != 0)
228 goto err_register;
229
230 netdev_link_up(netdev);
231
232 return 0;
233
234 err_settings:
235 unregister_netdev(netdev);
236 err_register:
237 netdev_nullify(netdev);
238 netdev_put(netdev);
239 return rc;
240 }
241
242 /** Remove the device */
tap_remove(struct linux_device * device)243 static void tap_remove(struct linux_device *device)
244 {
245 struct net_device *netdev = linux_get_drvdata(device);
246 unregister_netdev(netdev);
247 netdev_nullify(netdev);
248 netdev_put(netdev);
249 }
250
251 /** Tap linux_driver */
252 struct linux_driver tap_driver __linux_driver = {
253 .name = "tap",
254 .probe = tap_probe,
255 .remove = tap_remove,
256 .can_probe = 1,
257 };
258