tap-win32.c 21.8 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  TAP-Win32 -- A kernel driver to provide virtual tap device functionality
 *               on Windows.  Originally derived from the CIPE-Win32
 *               project by Damion K. Wilson, with extensive modifications by
 *               James Yonan.
 *
 *  All source code which derives from the CIPE-Win32 project is
 *  Copyright (C) Damion K. Wilson, 2003, and is released under the
 *  GPL version 2 (see below).
 *
 *  All other source code is Copyright (C) James Yonan, 2003-2004,
 *  and is released under the GPL version 2 (see below).
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (see the file COPYING included with this
26
 *  distribution); if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
27
 */
28

P
Paolo Bonzini 已提交
29
#include "tap_int.h"
30

P
pbrook 已提交
31
#include "qemu-common.h"
32
#include "clients.h"            /* net_init_tap */
P
Paolo Bonzini 已提交
33
#include "net/net.h"
34
#include "net/tap.h"            /* tap_has_ufo, ... */
35
#include "sysemu/sysemu.h"
36
#include "qemu/error-report.h"
B
bellard 已提交
37 38
#include <stdio.h>
#include <windows.h>
F
Filip Navara 已提交
39
#include <winioctl.h>
B
bellard 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

//=============
// TAP IOCTLs
//=============

#define TAP_CONTROL_CODE(request,method) \
  CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS)

#define TAP_IOCTL_GET_MAC               TAP_CONTROL_CODE (1, METHOD_BUFFERED)
#define TAP_IOCTL_GET_VERSION           TAP_CONTROL_CODE (2, METHOD_BUFFERED)
#define TAP_IOCTL_GET_MTU               TAP_CONTROL_CODE (3, METHOD_BUFFERED)
#define TAP_IOCTL_GET_INFO              TAP_CONTROL_CODE (4, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_POINT_TO_POINT TAP_CONTROL_CODE (5, METHOD_BUFFERED)
#define TAP_IOCTL_SET_MEDIA_STATUS      TAP_CONTROL_CODE (6, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_MASQ      TAP_CONTROL_CODE (7, METHOD_BUFFERED)
#define TAP_IOCTL_GET_LOG_LINE          TAP_CONTROL_CODE (8, METHOD_BUFFERED)
#define TAP_IOCTL_CONFIG_DHCP_SET_OPT   TAP_CONTROL_CODE (9, METHOD_BUFFERED)

//=================
// Registry keys
//=================

#define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"

#define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"

//======================
// Filesystem prefixes
//======================

#define USERMODEDEVICEDIR "\\\\.\\Global\\"
#define TAPSUFFIX         ".tap"


//======================
// Compile time configuration
//======================

A
aurel32 已提交
78
//#define DEBUG_TAP_WIN32
B
bellard 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92

#define TUN_ASYNCHRONOUS_WRITES 1

#define TUN_BUFFER_SIZE 1560
#define TUN_MAX_BUFFER_COUNT 32

/*
 * The data member "buffer" must be the first element in the tun_buffer
 * structure. See the function, tap_win32_free_buffer.
 */
typedef struct tun_buffer_s {
    unsigned char buffer [TUN_BUFFER_SIZE];
    unsigned long read_size;
    struct tun_buffer_s* next;
A
Anthony Liguori 已提交
93
} tun_buffer_t;
B
bellard 已提交
94 95 96 97 98 99 100

typedef struct tap_win32_overlapped {
    HANDLE handle;
    HANDLE read_event;
    HANDLE write_event;
    HANDLE output_queue_semaphore;
    HANDLE free_list_semaphore;
B
bellard 已提交
101
    HANDLE tap_semaphore;
B
bellard 已提交
102 103 104 105
    CRITICAL_SECTION output_queue_cs;
    CRITICAL_SECTION free_list_cs;
    OVERLAPPED read_overlapped;
    OVERLAPPED write_overlapped;
A
Anthony Liguori 已提交
106 107 108 109 110
    tun_buffer_t buffers[TUN_MAX_BUFFER_COUNT];
    tun_buffer_t* free_list;
    tun_buffer_t* output_queue_front;
    tun_buffer_t* output_queue_back;
} tap_win32_overlapped_t;
B
bellard 已提交
111

A
Anthony Liguori 已提交
112
static tap_win32_overlapped_t tap_overlapped;
B
bellard 已提交
113

A
Anthony Liguori 已提交
114
static tun_buffer_t* get_buffer_from_free_list(tap_win32_overlapped_t* const overlapped)
B
bellard 已提交
115
{
A
Anthony Liguori 已提交
116
    tun_buffer_t* buffer = NULL;
B
bellard 已提交
117 118 119 120 121 122 123 124 125 126
    WaitForSingleObject(overlapped->free_list_semaphore, INFINITE);
    EnterCriticalSection(&overlapped->free_list_cs);
    buffer = overlapped->free_list;
//    assert(buffer != NULL);
    overlapped->free_list = buffer->next;
    LeaveCriticalSection(&overlapped->free_list_cs);
    buffer->next = NULL;
    return buffer;
}

A
Anthony Liguori 已提交
127
static void put_buffer_on_free_list(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
B
bellard 已提交
128 129 130 131 132 133 134 135
{
    EnterCriticalSection(&overlapped->free_list_cs);
    buffer->next = overlapped->free_list;
    overlapped->free_list = buffer;
    LeaveCriticalSection(&overlapped->free_list_cs);
    ReleaseSemaphore(overlapped->free_list_semaphore, 1, NULL);
}

A
Anthony Liguori 已提交
136
static tun_buffer_t* get_buffer_from_output_queue(tap_win32_overlapped_t* const overlapped, const int block)
B
bellard 已提交
137
{
A
Anthony Liguori 已提交
138
    tun_buffer_t* buffer = NULL;
B
bellard 已提交
139 140 141
    DWORD result, timeout = block ? INFINITE : 0L;

    // Non-blocking call
142
    result = WaitForSingleObject(overlapped->output_queue_semaphore, timeout);
B
bellard 已提交
143

144 145
    switch (result)
    {
B
bellard 已提交
146
        // The semaphore object was signaled.
147
        case WAIT_OBJECT_0:
B
bellard 已提交
148 149 150 151 152 153 154 155 156 157
            EnterCriticalSection(&overlapped->output_queue_cs);

            buffer = overlapped->output_queue_front;
            overlapped->output_queue_front = buffer->next;

            if(overlapped->output_queue_front == NULL) {
                overlapped->output_queue_back = NULL;
            }

            LeaveCriticalSection(&overlapped->output_queue_cs);
158
            break;
B
bellard 已提交
159 160

        // Semaphore was nonsignaled, so a time-out occurred.
161
        case WAIT_TIMEOUT:
B
bellard 已提交
162
            // Cannot open another window.
163
            break;
B
bellard 已提交
164 165 166 167 168
    }

    return buffer;
}

A
Anthony Liguori 已提交
169
static tun_buffer_t* get_buffer_from_output_queue_immediate (tap_win32_overlapped_t* const overlapped)
B
bellard 已提交
170 171 172 173
{
    return get_buffer_from_output_queue(overlapped, 0);
}

A
Anthony Liguori 已提交
174
static void put_buffer_on_output_queue(tap_win32_overlapped_t* const overlapped, tun_buffer_t* const buffer)
B
bellard 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
{
    EnterCriticalSection(&overlapped->output_queue_cs);

    if(overlapped->output_queue_front == NULL && overlapped->output_queue_back == NULL) {
        overlapped->output_queue_front = overlapped->output_queue_back = buffer;
    } else {
        buffer->next = NULL;
        overlapped->output_queue_back->next = buffer;
        overlapped->output_queue_back = buffer;
    }

    LeaveCriticalSection(&overlapped->output_queue_cs);

    ReleaseSemaphore(overlapped->output_queue_semaphore, 1, NULL);
}


static int is_tap_win32_dev(const char *guid)
{
    HKEY netcard_key;
    LONG status;
    DWORD len;
    int i = 0;

    status = RegOpenKeyEx(
        HKEY_LOCAL_MACHINE,
        ADAPTER_KEY,
        0,
        KEY_READ,
        &netcard_key);

    if (status != ERROR_SUCCESS) {
        return FALSE;
    }

    for (;;) {
        char enum_name[256];
        char unit_string[256];
        HKEY unit_key;
        char component_id_string[] = "ComponentId";
        char component_id[256];
        char net_cfg_instance_id_string[] = "NetCfgInstanceId";
        char net_cfg_instance_id[256];
        DWORD data_type;

        len = sizeof (enum_name);
        status = RegEnumKeyEx(
            netcard_key,
            i,
            enum_name,
            &len,
            NULL,
            NULL,
            NULL,
            NULL);

        if (status == ERROR_NO_MORE_ITEMS)
            break;
        else if (status != ERROR_SUCCESS) {
            return FALSE;
        }

        snprintf (unit_string, sizeof(unit_string), "%s\\%s",
                  ADAPTER_KEY, enum_name);

        status = RegOpenKeyEx(
            HKEY_LOCAL_MACHINE,
            unit_string,
            0,
            KEY_READ,
            &unit_key);

        if (status != ERROR_SUCCESS) {
            return FALSE;
        } else {
            len = sizeof (component_id);
            status = RegQueryValueEx(
                unit_key,
                component_id_string,
                NULL,
                &data_type,
B
blueswir1 已提交
256
                (LPBYTE)component_id,
B
bellard 已提交
257 258 259 260 261 262 263 264 265
                &len);

            if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
                len = sizeof (net_cfg_instance_id);
                status = RegQueryValueEx(
                    unit_key,
                    net_cfg_instance_id_string,
                    NULL,
                    &data_type,
B
blueswir1 已提交
266
                    (LPBYTE)net_cfg_instance_id,
B
bellard 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                    &len);

                if (status == ERROR_SUCCESS && data_type == REG_SZ) {
                    if (/* !strcmp (component_id, TAP_COMPONENT_ID) &&*/
                        !strcmp (net_cfg_instance_id, guid)) {
                        RegCloseKey (unit_key);
                        RegCloseKey (netcard_key);
                        return TRUE;
                    }
                }
            }
            RegCloseKey (unit_key);
        }
        ++i;
    }

    RegCloseKey (netcard_key);
    return FALSE;
}

static int get_device_guid(
    char *name,
    int name_size,
    char *actual_name,
    int actual_name_size)
{
    LONG status;
    HKEY control_net_key;
    DWORD len;
    int i = 0;
    int stop = 0;

    status = RegOpenKeyEx(
        HKEY_LOCAL_MACHINE,
        NETWORK_CONNECTIONS_KEY,
        0,
        KEY_READ,
        &control_net_key);

    if (status != ERROR_SUCCESS) {
        return -1;
    }

    while (!stop)
    {
        char enum_name[256];
        char connection_string[256];
        HKEY connection_key;
        char name_data[256];
        DWORD name_type;
        const char name_string[] = "Name";

        len = sizeof (enum_name);
        status = RegEnumKeyEx(
            control_net_key,
            i,
            enum_name,
            &len,
            NULL,
            NULL,
            NULL,
            NULL);

        if (status == ERROR_NO_MORE_ITEMS)
            break;
        else if (status != ERROR_SUCCESS) {
            return -1;
        }

336
        snprintf(connection_string,
B
bellard 已提交
337 338 339 340 341 342 343 344 345 346
             sizeof(connection_string),
             "%s\\%s\\Connection",
             NETWORK_CONNECTIONS_KEY, enum_name);

        status = RegOpenKeyEx(
            HKEY_LOCAL_MACHINE,
            connection_string,
            0,
            KEY_READ,
            &connection_key);
347

B
bellard 已提交
348 349 350 351 352 353 354
        if (status == ERROR_SUCCESS) {
            len = sizeof (name_data);
            status = RegQueryValueEx(
                connection_key,
                name_string,
                NULL,
                &name_type,
B
blueswir1 已提交
355
                (LPBYTE)name_data,
B
bellard 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
                &len);

            if (status != ERROR_SUCCESS || name_type != REG_SZ) {
                    return -1;
            }
            else {
                if (is_tap_win32_dev(enum_name)) {
                    snprintf(name, name_size, "%s", enum_name);
                    if (actual_name) {
                        if (strcmp(actual_name, "") != 0) {
                            if (strcmp(name_data, actual_name) != 0) {
                                RegCloseKey (connection_key);
                                ++i;
                                continue;
                            }
                        }
                        else {
                            snprintf(actual_name, actual_name_size, "%s", name_data);
                        }
                    }
                    stop = 1;
                }
            }

            RegCloseKey (connection_key);
        }
        ++i;
    }

    RegCloseKey (control_net_key);

    if (stop == 0)
        return -1;

    return 0;
}

static int tap_win32_set_status(HANDLE handle, int status)
{
    unsigned long len = 0;

    return DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS,
                &status, sizeof (status),
                &status, sizeof (status), &len, NULL);
}

A
Anthony Liguori 已提交
402
static void tap_win32_overlapped_init(tap_win32_overlapped_t* const overlapped, const HANDLE handle)
B
bellard 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
{
    overlapped->handle = handle;

    overlapped->read_event = CreateEvent(NULL, FALSE, FALSE, NULL);
    overlapped->write_event = CreateEvent(NULL, FALSE, FALSE, NULL);

    overlapped->read_overlapped.Offset = 0;
    overlapped->read_overlapped.OffsetHigh = 0;
    overlapped->read_overlapped.hEvent = overlapped->read_event;

    overlapped->write_overlapped.Offset = 0;
    overlapped->write_overlapped.OffsetHigh = 0;
    overlapped->write_overlapped.hEvent = overlapped->write_event;

    InitializeCriticalSection(&overlapped->output_queue_cs);
    InitializeCriticalSection(&overlapped->free_list_cs);

420
    overlapped->output_queue_semaphore = CreateSemaphore(
B
bellard 已提交
421 422 423 424 425 426 427 428 429
        NULL,   // default security attributes
        0,   // initial count
        TUN_MAX_BUFFER_COUNT,   // maximum count
        NULL);  // unnamed semaphore

    if(!overlapped->output_queue_semaphore)  {
        fprintf(stderr, "error creating output queue semaphore!\n");
    }

430
    overlapped->free_list_semaphore = CreateSemaphore(
B
bellard 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444
        NULL,   // default security attributes
        TUN_MAX_BUFFER_COUNT,   // initial count
        TUN_MAX_BUFFER_COUNT,   // maximum count
        NULL);  // unnamed semaphore

    if(!overlapped->free_list_semaphore)  {
        fprintf(stderr, "error creating free list semaphore!\n");
    }

    overlapped->free_list = overlapped->output_queue_front = overlapped->output_queue_back = NULL;

    {
        unsigned index;
        for(index = 0; index < TUN_MAX_BUFFER_COUNT; index++) {
A
Anthony Liguori 已提交
445
            tun_buffer_t* element = &overlapped->buffers[index];
B
bellard 已提交
446 447 448 449
            element->next = overlapped->free_list;
            overlapped->free_list = element;
        }
    }
B
bellard 已提交
450 451 452 453
    /* To count buffers, initially no-signal. */
    overlapped->tap_semaphore = CreateSemaphore(NULL, 0, TUN_MAX_BUFFER_COUNT, NULL);
    if(!overlapped->tap_semaphore)
        fprintf(stderr, "error creating tap_semaphore.\n");
B
bellard 已提交
454 455
}

A
Anthony Liguori 已提交
456
static int tap_win32_write(tap_win32_overlapped_t *overlapped,
B
bellard 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470
                           const void *buffer, unsigned long size)
{
    unsigned long write_size;
    BOOL result;
    DWORD error;

    result = GetOverlappedResult( overlapped->handle, &overlapped->write_overlapped,
                                  &write_size, FALSE);

    if (!result && GetLastError() == ERROR_IO_INCOMPLETE)
        WaitForSingleObject(overlapped->write_event, INFINITE);

    result = WriteFile(overlapped->handle, buffer, size,
                       &write_size, &overlapped->write_overlapped);
471

472
    if (!result) {
B
bellard 已提交
473
        switch (error = GetLastError())
474 475
        {
        case ERROR_IO_PENDING:
B
bellard 已提交
476 477 478 479 480 481
#ifndef TUN_ASYNCHRONOUS_WRITES
            WaitForSingleObject(overlapped->write_event, INFINITE);
#endif
            break;
        default:
            return -1;
482
        }
B
bellard 已提交
483 484
    }

P
Pavel Dovgaluk 已提交
485
    return write_size;
B
bellard 已提交
486 487 488 489
}

static DWORD WINAPI tap_win32_thread_entry(LPVOID param)
{
A
Anthony Liguori 已提交
490
    tap_win32_overlapped_t *overlapped = (tap_win32_overlapped_t*)param;
B
bellard 已提交
491 492 493
    unsigned long read_size;
    BOOL result;
    DWORD dwError;
A
Anthony Liguori 已提交
494
    tun_buffer_t* buffer = get_buffer_from_free_list(overlapped);
B
bellard 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509


    for (;;) {
        result = ReadFile(overlapped->handle,
                          buffer->buffer,
                          sizeof(buffer->buffer),
                          &read_size,
                          &overlapped->read_overlapped);
        if (!result) {
            dwError = GetLastError();
            if (dwError == ERROR_IO_PENDING) {
                WaitForSingleObject(overlapped->read_event, INFINITE);
                result = GetOverlappedResult( overlapped->handle, &overlapped->read_overlapped,
                                              &read_size, FALSE);
                if (!result) {
A
aurel32 已提交
510
#ifdef DEBUG_TAP_WIN32
B
bellard 已提交
511 512 513 514 515 516 517 518 519 520
                    LPVOID lpBuffer;
                    dwError = GetLastError();
                    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                                   NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                   (LPTSTR) & lpBuffer, 0, NULL );
                    fprintf(stderr, "Tap-Win32: Error GetOverlappedResult %d - %s\n", dwError, lpBuffer);
                    LocalFree( lpBuffer );
#endif
                }
            } else {
A
aurel32 已提交
521
#ifdef DEBUG_TAP_WIN32
B
bellard 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534
                LPVOID lpBuffer;
                FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                               NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                               (LPTSTR) & lpBuffer, 0, NULL );
                fprintf(stderr, "Tap-Win32: Error ReadFile %d - %s\n", dwError, lpBuffer);
                LocalFree( lpBuffer );
#endif
            }
        }

        if(read_size > 0) {
            buffer->read_size = read_size;
            put_buffer_on_output_queue(overlapped, buffer);
B
bellard 已提交
535
            ReleaseSemaphore(overlapped->tap_semaphore, 1, NULL);
B
bellard 已提交
536 537 538 539 540 541 542
            buffer = get_buffer_from_free_list(overlapped);
        }
    }

    return 0;
}

A
Anthony Liguori 已提交
543
static int tap_win32_read(tap_win32_overlapped_t *overlapped,
B
bellard 已提交
544 545 546 547
                          uint8_t **pbuf, int max_size)
{
    int size = 0;

A
Anthony Liguori 已提交
548
    tun_buffer_t* buffer = get_buffer_from_output_queue_immediate(overlapped);
B
bellard 已提交
549 550 551 552 553 554 555 556 557 558 559 560

    if(buffer != NULL) {
        *pbuf = buffer->buffer;
        size = (int)buffer->read_size;
        if(size > max_size) {
            size = max_size;
        }
    }

    return size;
}

A
Anthony Liguori 已提交
561
static void tap_win32_free_buffer(tap_win32_overlapped_t *overlapped,
B
blueswir1 已提交
562
                                  uint8_t *pbuf)
B
bellard 已提交
563
{
A
Anthony Liguori 已提交
564
    tun_buffer_t* buffer = (tun_buffer_t*)pbuf;
B
bellard 已提交
565 566 567
    put_buffer_on_free_list(overlapped, buffer);
}

A
Anthony Liguori 已提交
568
static int tap_win32_open(tap_win32_overlapped_t **phandle,
S
Stefan Weil 已提交
569
                          const char *preferred_name)
B
bellard 已提交
570 571 572 573 574 575 576 577 578 579 580 581
{
    char device_path[256];
    char device_guid[0x100];
    int rc;
    HANDLE handle;
    BOOL bret;
    char name_buffer[0x100] = {0, };
    struct {
        unsigned long major;
        unsigned long minor;
        unsigned long debug;
    } version;
B
blueswir1 已提交
582
    DWORD version_len;
B
bellard 已提交
583 584
    DWORD idThread;

S
Stefan Weil 已提交
585 586 587
    if (preferred_name != NULL) {
        snprintf(name_buffer, sizeof(name_buffer), "%s", preferred_name);
    }
B
bellard 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

    rc = get_device_guid(device_guid, sizeof(device_guid), name_buffer, sizeof(name_buffer));
    if (rc)
        return -1;

    snprintf (device_path, sizeof(device_path), "%s%s%s",
              USERMODEDEVICEDIR,
              device_guid,
              TAPSUFFIX);

    handle = CreateFile (
        device_path,
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
        0 );

    if (handle == INVALID_HANDLE_VALUE) {
        return -1;
    }

    bret = DeviceIoControl(handle, TAP_IOCTL_GET_VERSION,
                           &version, sizeof (version),
                           &version, sizeof (version), &version_len, NULL);

    if (bret == FALSE) {
        CloseHandle(handle);
        return -1;
    }

    if (!tap_win32_set_status(handle, TRUE)) {
        return -1;
    }

    tap_win32_overlapped_init(&tap_overlapped, handle);

    *phandle = &tap_overlapped;

B
Blue Swirl 已提交
628 629
    CreateThread(NULL, 0, tap_win32_thread_entry,
                 (LPVOID)&tap_overlapped, 0, &idThread);
B
bellard 已提交
630 631 632 633 634 635
    return 0;
}

/********************************************/

 typedef struct TAPState {
636
     NetClientState nc;
A
Anthony Liguori 已提交
637
     tap_win32_overlapped_t *handle;
B
bellard 已提交
638 639
 } TAPState;

640
static void tap_cleanup(NetClientState *nc)
641
{
642
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
643 644 645 646 647 648 649 650

    qemu_del_wait_object(s->handle->tap_semaphore, NULL, NULL);

    /* FIXME: need to kill thread and close file handle:
       tap_win32_close(s);
    */
}

651
static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
B
bellard 已提交
652
{
653
    TAPState *s = DO_UPCAST(TAPState, nc, nc);
B
bellard 已提交
654

655
    return tap_win32_write(s->handle, buf, size);
B
bellard 已提交
656 657
}

B
bellard 已提交
658
static void tap_win32_send(void *opaque)
B
bellard 已提交
659
{
B
bellard 已提交
660
    TAPState *s = opaque;
B
bellard 已提交
661 662 663 664 665 666
    uint8_t *buf;
    int max_size = 4096;
    int size;

    size = tap_win32_read(s->handle, &buf, max_size);
    if (size > 0) {
667
        qemu_send_packet(&s->nc, buf, size);
B
bellard 已提交
668 669 670 671
        tap_win32_free_buffer(s->handle, buf);
    }
}

672
static bool tap_has_ufo(NetClientState *nc)
673 674 675 676
{
    return false;
}

677
static bool tap_has_vnet_hdr(NetClientState *nc)
678 679 680 681 682 683 684 685 686 687 688 689 690
{
    return false;
}

int tap_probe_vnet_hdr_len(int fd, int len)
{
    return 0;
}

void tap_fd_set_vnet_hdr_len(int fd, int len)
{
}

691
static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
692 693 694
{
}

695
static void tap_set_offload(NetClientState *nc, int csum, int tso4,
696 697 698 699 700 701 702 703 704
                     int tso6, int ecn, int ufo)
{
}

struct vhost_net *tap_get_vhost_net(NetClientState *nc)
{
    return NULL;
}

705
static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
706 707 708 709
{
    return false;
}

710
static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
711 712 713 714
{
    abort();
}

715
static NetClientInfo net_tap_win32_info = {
716
    .type = NET_CLIENT_OPTIONS_KIND_TAP,
717 718 719
    .size = sizeof(TAPState),
    .receive = tap_receive,
    .cleanup = tap_cleanup,
720 721 722 723 724 725
    .has_ufo = tap_has_ufo,
    .has_vnet_hdr = tap_has_vnet_hdr,
    .has_vnet_hdr_len = tap_has_vnet_hdr_len,
    .using_vnet_hdr = tap_using_vnet_hdr,
    .set_offload = tap_set_offload,
    .set_vnet_hdr_len = tap_set_vnet_hdr_len,
726 727
};

728
static int tap_win32_init(NetClientState *peer, const char *model,
729
                          const char *name, const char *ifname)
B
bellard 已提交
730
{
731
    NetClientState *nc;
B
bellard 已提交
732
    TAPState *s;
733
    tap_win32_overlapped_t *handle;
734

735
    if (tap_win32_open(&handle, ifname) < 0) {
B
bellard 已提交
736 737 738 739
        printf("tap: Could not open '%s'\n", ifname);
        return -1;
    }

740
    nc = qemu_new_net_client(&net_tap_win32_info, peer, model, name);
741

742 743
    s = DO_UPCAST(TAPState, nc, nc);

744
    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
B
bellard 已提交
745
             "tap: ifname=%s", ifname);
746

747 748
    s->handle = handle;

B
bellard 已提交
749
    qemu_add_wait_object(s->handle->tap_semaphore, tap_win32_send, s);
750

B
bellard 已提交
751 752
    return 0;
}
753

754
int net_init_tap(const NetClientOptions *opts, const char *name,
755
                 NetClientState *peer)
756
{
757
    const NetdevTapOptions *tap;
758

759 760
    assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
    tap = opts->tap;
761

762
    if (!tap->has_ifname) {
763
        error_report("tap: no interface name");
764 765 766
        return -1;
    }

767
    if (tap_win32_init(peer, "tap", name, tap->ifname) == -1) {
768 769 770 771 772 773
        return -1;
    }

    return 0;
}

774 775 776 777 778 779 780 781 782
int tap_enable(NetClientState *nc)
{
    abort();
}

int tap_disable(NetClientState *nc)
{
    abort();
}