2 * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
3 * on Windows. Originally derived from the CIPE-Win32
4 * project by Damion K. Wilson, with extensive modifications by
7 * All source code which derives from the CIPE-Win32 project is
8 * Copyright (C) Damion K. Wilson, 2003, and is released under the
9 * GPL version 2 (see below).
11 * All other source code is Copyright (C) James Yonan, 2003-2004,
12 * and is released under the GPL version 2 (see below).
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program (see the file COPYING included with this
26 * distribution); if not, see <http://www.gnu.org/licenses/>.
31 #include "qemu-common.h"
32 #include "clients.h" /* net_init_tap */
35 #include "qemu-error.h"
44 #define TAP_CONTROL_CODE(request,method) \
45 CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)
47 #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE (1, METHOD_BUFFERED)
48 #define TAP_IOCTL_GET_VERSION TAP_CONTROL_CODE (2, METHOD_BUFFERED)
49 #define TAP_IOCTL_GET_MTU TAP_CONTROL_CODE (3, METHOD_BUFFERED)
50 #define TAP_IOCTL_GET_INFO TAP_CONTROL_CODE (4, METHOD_BUFFERED)
51 #define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
52 #define TAP_IOCTL_SET_MEDIA_STATUS TAP_CONTROL_CODE (6, METHOD_BUFFERED)
53 #define TAP_IOCTL_CONFIG_DHCP_MASQ TAP_CONTROL_CODE (7, METHOD_BUFFERED)
54 #define TAP_IOCTL_GET_LOG_LINE TAP_CONTROL_CODE (8, METHOD_BUFFERED)
55 #define TAP_IOCTL_CONFIG_DHCP_SET_OPT TAP_CONTROL_CODE (9, METHOD_BUFFERED)
61 #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
63 #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"
65 //======================
66 // Filesystem prefixes
67 //======================
69 #define USERMODEDEVICEDIR "\\\\.\\Global\\"
70 #define TAPSUFFIX ".tap"
73 //======================
74 // Compile time configuration
75 //======================
77 //#define DEBUG_TAP_WIN32
79 #define TUN_ASYNCHRONOUS_WRITES 1
81 #define TUN_BUFFER_SIZE 1560
82 #define TUN_MAX_BUFFER_COUNT 32
85 * The data member "buffer" must be the first element in the tun_buffer
86 * structure. See the function, tap_win32_free_buffer.
88 typedef struct tun_buffer_s
{
89 unsigned char buffer
[TUN_BUFFER_SIZE
];
90 unsigned long read_size
;
91 struct tun_buffer_s
* next
;
94 typedef struct tap_win32_overlapped
{
98 HANDLE output_queue_semaphore
;
99 HANDLE free_list_semaphore
;
100 HANDLE tap_semaphore
;
101 CRITICAL_SECTION output_queue_cs
;
102 CRITICAL_SECTION free_list_cs
;
103 OVERLAPPED read_overlapped
;
104 OVERLAPPED write_overlapped
;
105 tun_buffer_t buffers
[TUN_MAX_BUFFER_COUNT
];
106 tun_buffer_t
* free_list
;
107 tun_buffer_t
* output_queue_front
;
108 tun_buffer_t
* output_queue_back
;
109 } tap_win32_overlapped_t
;
111 static tap_win32_overlapped_t tap_overlapped
;
113 static tun_buffer_t
* get_buffer_from_free_list(tap_win32_overlapped_t
* const overlapped
)
115 tun_buffer_t
* buffer
= NULL
;
116 WaitForSingleObject(overlapped
->free_list_semaphore
, INFINITE
);
117 EnterCriticalSection(&overlapped
->free_list_cs
);
118 buffer
= overlapped
->free_list
;
119 // assert(buffer != NULL);
120 overlapped
->free_list
= buffer
->next
;
121 LeaveCriticalSection(&overlapped
->free_list_cs
);
126 static void put_buffer_on_free_list(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
128 EnterCriticalSection(&overlapped
->free_list_cs
);
129 buffer
->next
= overlapped
->free_list
;
130 overlapped
->free_list
= buffer
;
131 LeaveCriticalSection(&overlapped
->free_list_cs
);
132 ReleaseSemaphore(overlapped
->free_list_semaphore
, 1, NULL
);
135 static tun_buffer_t
* get_buffer_from_output_queue(tap_win32_overlapped_t
* const overlapped
, const int block
)
137 tun_buffer_t
* buffer
= NULL
;
138 DWORD result
, timeout
= block
? INFINITE
: 0L;
141 result
= WaitForSingleObject(overlapped
->output_queue_semaphore
, timeout
);
145 // The semaphore object was signaled.
147 EnterCriticalSection(&overlapped
->output_queue_cs
);
149 buffer
= overlapped
->output_queue_front
;
150 overlapped
->output_queue_front
= buffer
->next
;
152 if(overlapped
->output_queue_front
== NULL
) {
153 overlapped
->output_queue_back
= NULL
;
156 LeaveCriticalSection(&overlapped
->output_queue_cs
);
159 // Semaphore was nonsignaled, so a time-out occurred.
161 // Cannot open another window.
168 static tun_buffer_t
* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t
* const overlapped
)
170 return get_buffer_from_output_queue(overlapped
, 0);
173 static void put_buffer_on_output_queue(tap_win32_overlapped_t
* const overlapped
, tun_buffer_t
* const buffer
)
175 EnterCriticalSection(&overlapped
->output_queue_cs
);
177 if(overlapped
->output_queue_front
== NULL
&& overlapped
->output_queue_back
== NULL
) {
178 overlapped
->output_queue_front
= overlapped
->output_queue_back
= buffer
;
181 overlapped
->output_queue_back
->next
= buffer
;
182 overlapped
->output_queue_back
= buffer
;
185 LeaveCriticalSection(&overlapped
->output_queue_cs
);
187 ReleaseSemaphore(overlapped
->output_queue_semaphore
, 1, NULL
);
191 static int is_tap_win32_dev(const char *guid
)
198 status
= RegOpenKeyEx(
205 if (status
!= ERROR_SUCCESS
) {
211 char unit_string
[256];
213 char component_id_string
[] = "ComponentId";
214 char component_id
[256];
215 char net_cfg_instance_id_string
[] = "NetCfgInstanceId";
216 char net_cfg_instance_id
[256];
219 len
= sizeof (enum_name
);
220 status
= RegEnumKeyEx(
230 if (status
== ERROR_NO_MORE_ITEMS
)
232 else if (status
!= ERROR_SUCCESS
) {
236 snprintf (unit_string
, sizeof(unit_string
), "%s\\%s",
237 ADAPTER_KEY
, enum_name
);
239 status
= RegOpenKeyEx(
246 if (status
!= ERROR_SUCCESS
) {
249 len
= sizeof (component_id
);
250 status
= RegQueryValueEx(
255 (LPBYTE
)component_id
,
258 if (!(status
!= ERROR_SUCCESS
|| data_type
!= REG_SZ
)) {
259 len
= sizeof (net_cfg_instance_id
);
260 status
= RegQueryValueEx(
262 net_cfg_instance_id_string
,
265 (LPBYTE
)net_cfg_instance_id
,
268 if (status
== ERROR_SUCCESS
&& data_type
== REG_SZ
) {
269 if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
270 !strcmp (net_cfg_instance_id
, guid
)) {
271 RegCloseKey (unit_key
);
272 RegCloseKey (netcard_key
);
277 RegCloseKey (unit_key
);
282 RegCloseKey (netcard_key
);
286 static int get_device_guid(
290 int actual_name_size
)
293 HKEY control_net_key
;
298 status
= RegOpenKeyEx(
300 NETWORK_CONNECTIONS_KEY
,
305 if (status
!= ERROR_SUCCESS
) {
312 char connection_string
[256];
316 const char name_string
[] = "Name";
318 len
= sizeof (enum_name
);
319 status
= RegEnumKeyEx(
329 if (status
== ERROR_NO_MORE_ITEMS
)
331 else if (status
!= ERROR_SUCCESS
) {
335 snprintf(connection_string
,
336 sizeof(connection_string
),
337 "%s\\%s\\Connection",
338 NETWORK_CONNECTIONS_KEY
, enum_name
);
340 status
= RegOpenKeyEx(
347 if (status
== ERROR_SUCCESS
) {
348 len
= sizeof (name_data
);
349 status
= RegQueryValueEx(
357 if (status
!= ERROR_SUCCESS
|| name_type
!= REG_SZ
) {
361 if (is_tap_win32_dev(enum_name
)) {
362 snprintf(name
, name_size
, "%s", enum_name
);
364 if (strcmp(actual_name
, "") != 0) {
365 if (strcmp(name_data
, actual_name
) != 0) {
366 RegCloseKey (connection_key
);
372 snprintf(actual_name
, actual_name_size
, "%s", name_data
);
379 RegCloseKey (connection_key
);
384 RegCloseKey (control_net_key
);
392 static int tap_win32_set_status(HANDLE handle
, int status
)
394 unsigned long len
= 0;
396 return DeviceIoControl(handle
, TAP_IOCTL_SET_MEDIA_STATUS
,
397 &status
, sizeof (status
),
398 &status
, sizeof (status
), &len
, NULL
);
401 static void tap_win32_overlapped_init(tap_win32_overlapped_t
* const overlapped
, const HANDLE handle
)
403 overlapped
->handle
= handle
;
405 overlapped
->read_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
406 overlapped
->write_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
408 overlapped
->read_overlapped
.Offset
= 0;
409 overlapped
->read_overlapped
.OffsetHigh
= 0;
410 overlapped
->read_overlapped
.hEvent
= overlapped
->read_event
;
412 overlapped
->write_overlapped
.Offset
= 0;
413 overlapped
->write_overlapped
.OffsetHigh
= 0;
414 overlapped
->write_overlapped
.hEvent
= overlapped
->write_event
;
416 InitializeCriticalSection(&overlapped
->output_queue_cs
);
417 InitializeCriticalSection(&overlapped
->free_list_cs
);
419 overlapped
->output_queue_semaphore
= CreateSemaphore(
420 NULL
, // default security attributes
422 TUN_MAX_BUFFER_COUNT
, // maximum count
423 NULL
); // unnamed semaphore
425 if(!overlapped
->output_queue_semaphore
) {
426 fprintf(stderr
, "error creating output queue semaphore!\n");
429 overlapped
->free_list_semaphore
= CreateSemaphore(
430 NULL
, // default security attributes
431 TUN_MAX_BUFFER_COUNT
, // initial count
432 TUN_MAX_BUFFER_COUNT
, // maximum count
433 NULL
); // unnamed semaphore
435 if(!overlapped
->free_list_semaphore
) {
436 fprintf(stderr
, "error creating free list semaphore!\n");
439 overlapped
->free_list
= overlapped
->output_queue_front
= overlapped
->output_queue_back
= NULL
;
443 for(index
= 0; index
< TUN_MAX_BUFFER_COUNT
; index
++) {
444 tun_buffer_t
* element
= &overlapped
->buffers
[index
];
445 element
->next
= overlapped
->free_list
;
446 overlapped
->free_list
= element
;
449 /* To count buffers, initially no-signal. */
450 overlapped
->tap_semaphore
= CreateSemaphore(NULL
, 0, TUN_MAX_BUFFER_COUNT
, NULL
);
451 if(!overlapped
->tap_semaphore
)
452 fprintf(stderr
, "error creating tap_semaphore.\n");
455 static int tap_win32_write(tap_win32_overlapped_t
*overlapped
,
456 const void *buffer
, unsigned long size
)
458 unsigned long write_size
;
462 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->write_overlapped
,
465 if (!result
&& GetLastError() == ERROR_IO_INCOMPLETE
)
466 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
468 result
= WriteFile(overlapped
->handle
, buffer
, size
,
469 &write_size
, &overlapped
->write_overlapped
);
472 switch (error
= GetLastError())
474 case ERROR_IO_PENDING
:
475 #ifndef TUN_ASYNCHRONOUS_WRITES
476 WaitForSingleObject(overlapped
->write_event
, INFINITE
);
487 static DWORD WINAPI
tap_win32_thread_entry(LPVOID param
)
489 tap_win32_overlapped_t
*overlapped
= (tap_win32_overlapped_t
*)param
;
490 unsigned long read_size
;
493 tun_buffer_t
* buffer
= get_buffer_from_free_list(overlapped
);
497 result
= ReadFile(overlapped
->handle
,
499 sizeof(buffer
->buffer
),
501 &overlapped
->read_overlapped
);
503 dwError
= GetLastError();
504 if (dwError
== ERROR_IO_PENDING
) {
505 WaitForSingleObject(overlapped
->read_event
, INFINITE
);
506 result
= GetOverlappedResult( overlapped
->handle
, &overlapped
->read_overlapped
,
509 #ifdef DEBUG_TAP_WIN32
511 dwError
= GetLastError();
512 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
513 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
514 (LPTSTR
) & lpBuffer
, 0, NULL
);
515 fprintf(stderr
, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError
, lpBuffer
);
516 LocalFree( lpBuffer
);
520 #ifdef DEBUG_TAP_WIN32
522 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
523 NULL
, dwError
, MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
524 (LPTSTR
) & lpBuffer
, 0, NULL
);
525 fprintf(stderr
, "Tap-Win32: Error ReadFile %d - %s\n", dwError
, lpBuffer
);
526 LocalFree( lpBuffer
);
532 buffer
->read_size
= read_size
;
533 put_buffer_on_output_queue(overlapped
, buffer
);
534 ReleaseSemaphore(overlapped
->tap_semaphore
, 1, NULL
);
535 buffer
= get_buffer_from_free_list(overlapped
);
542 static int tap_win32_read(tap_win32_overlapped_t
*overlapped
,
543 uint8_t **pbuf
, int max_size
)
547 tun_buffer_t
* buffer
= get_buffer_from_output_queue_immediate(overlapped
);
550 *pbuf
= buffer
->buffer
;
551 size
= (int)buffer
->read_size
;
552 if(size
> max_size
) {
560 static void tap_win32_free_buffer(tap_win32_overlapped_t
*overlapped
,
563 tun_buffer_t
* buffer
= (tun_buffer_t
*)pbuf
;
564 put_buffer_on_free_list(overlapped
, buffer
);
567 static int tap_win32_open(tap_win32_overlapped_t
**phandle
,
568 const char *prefered_name
)
570 char device_path
[256];
571 char device_guid
[0x100];
575 char name_buffer
[0x100] = {0, };
584 if (prefered_name
!= NULL
)
585 snprintf(name_buffer
, sizeof(name_buffer
), "%s", prefered_name
);
587 rc
= get_device_guid(device_guid
, sizeof(device_guid
), name_buffer
, sizeof(name_buffer
));
591 snprintf (device_path
, sizeof(device_path
), "%s%s%s",
596 handle
= CreateFile (
598 GENERIC_READ
| GENERIC_WRITE
,
602 FILE_ATTRIBUTE_SYSTEM
| FILE_FLAG_OVERLAPPED
,
605 if (handle
== INVALID_HANDLE_VALUE
) {
609 bret
= DeviceIoControl(handle
, TAP_IOCTL_GET_VERSION
,
610 &version
, sizeof (version
),
611 &version
, sizeof (version
), &version_len
, NULL
);
618 if (!tap_win32_set_status(handle
, TRUE
)) {
622 tap_win32_overlapped_init(&tap_overlapped
, handle
);
624 *phandle
= &tap_overlapped
;
626 CreateThread(NULL
, 0, tap_win32_thread_entry
,
627 (LPVOID
)&tap_overlapped
, 0, &idThread
);
631 /********************************************/
633 typedef struct TAPState
{
635 tap_win32_overlapped_t
*handle
;
638 static void tap_cleanup(NetClientState
*nc
)
640 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
642 qemu_del_wait_object(s
->handle
->tap_semaphore
, NULL
, NULL
);
644 /* FIXME: need to kill thread and close file handle:
649 static ssize_t
tap_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
651 TAPState
*s
= DO_UPCAST(TAPState
, nc
, nc
);
653 return tap_win32_write(s
->handle
, buf
, size
);
656 static void tap_win32_send(void *opaque
)
658 TAPState
*s
= opaque
;
663 size
= tap_win32_read(s
->handle
, &buf
, max_size
);
665 qemu_send_packet(&s
->nc
, buf
, size
);
666 tap_win32_free_buffer(s
->handle
, buf
);
670 static NetClientInfo net_tap_win32_info
= {
671 .type
= NET_CLIENT_OPTIONS_KIND_TAP
,
672 .size
= sizeof(TAPState
),
673 .receive
= tap_receive
,
674 .cleanup
= tap_cleanup
,
677 static int tap_win32_init(NetClientState
*peer
, const char *model
,
678 const char *name
, const char *ifname
)
682 tap_win32_overlapped_t
*handle
;
684 if (tap_win32_open(&handle
, ifname
) < 0) {
685 printf("tap: Could not open '%s'\n", ifname
);
689 nc
= qemu_new_net_client(&net_tap_win32_info
, peer
, model
, name
);
691 s
= DO_UPCAST(TAPState
, nc
, nc
);
693 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
694 "tap: ifname=%s", ifname
);
698 qemu_add_wait_object(s
->handle
->tap_semaphore
, tap_win32_send
, s
);
703 int net_init_tap(const NetClientOptions
*opts
, const char *name
,
704 NetClientState
*peer
)
706 const NetdevTapOptions
*tap
;
708 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_TAP
);
711 if (!tap
->has_ifname
) {
712 error_report("tap: no interface name");
716 if (tap_win32_init(peer
, "tap", name
, tap
->ifname
) == -1) {
723 int tap_has_ufo(NetClientState
*nc
)
728 int tap_has_vnet_hdr(NetClientState
*nc
)
733 int tap_probe_vnet_hdr_len(int fd
, int len
)
738 void tap_fd_set_vnet_hdr_len(int fd
, int len
)
742 void tap_using_vnet_hdr(NetClientState
*nc
, int using_vnet_hdr
)
746 void tap_set_offload(NetClientState
*nc
, int csum
, int tso4
,
747 int tso6
, int ecn
, int ufo
)
751 struct vhost_net
*tap_get_vhost_net(NetClientState
*nc
)
756 int tap_has_vnet_hdr_len(NetClientState
*nc
, int len
)
761 void tap_set_vnet_hdr_len(NetClientState
*nc
, int len
)