int tap_open(char *ifname, int ifname_size, int *vnet_hdr,

             int vnet_hdr_required, int mq_required)

{

    struct ifreq ifr;

    int fd, ret;

    int len = sizeof(struct virtio_net_hdr);



    TFR(fd = open(PATH_NET_TUN, O_RDWR));

    if (fd < 0) {

        error_report("could not open %s: %m", PATH_NET_TUN);

        return -1;

    }

    memset(&ifr, 0, sizeof(ifr));

    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;



    if (*vnet_hdr) {

        unsigned int features;



        if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&

            features & IFF_VNET_HDR) {

            *vnet_hdr = 1;

            ifr.ifr_flags |= IFF_VNET_HDR;

        } else {

            *vnet_hdr = 0;

        }



        if (vnet_hdr_required && !*vnet_hdr) {

            error_report("vnet_hdr=1 requested, but no kernel "

                         "support for IFF_VNET_HDR available");

            close(fd);

            return -1;

        }

        /*

         * Make sure vnet header size has the default value: for a persistent

         * tap it might have been modified e.g. by another instance of qemu.

         * Ignore errors since old kernels do not support this ioctl: in this

         * case the header size implicitly has the correct value.

         */

        ioctl(fd, TUNSETVNETHDRSZ, &len);

    }



    if (mq_required) {

        unsigned int features;



        if ((ioctl(fd, TUNGETFEATURES, &features) != 0) ||

            !(features & IFF_MULTI_QUEUE)) {

            error_report("multiqueue required, but no kernel "

                         "support for IFF_MULTI_QUEUE available");

            close(fd);

            return -1;

        } else {

            ifr.ifr_flags |= IFF_MULTI_QUEUE;

        }

    }



    if (ifname[0] != '\0')

        pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);

    else

        pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");

    ret = ioctl(fd, TUNSETIFF, (void *) &ifr);

    if (ret != 0) {

        if (ifname[0] != '\0') {

            error_report("could not configure %s (%s): %m", PATH_NET_TUN, ifr.ifr_name);

        } else {

            error_report("could not configure %s: %m", PATH_NET_TUN);

        }

        close(fd);

        return -1;

    }

    pstrcpy(ifname, ifname_size, ifr.ifr_name);

    fcntl(fd, F_SETFL, O_NONBLOCK);

    return fd;

}

int tap_open(char *ifname, int ifname_size, int *vnet_hdr,

             int vnet_hdr_required, int mq_required)

{

    struct ifreq ifr;

    int fd, ret;

    int len = sizeof(struct virtio_net_hdr);



    TFR(fd = open(PATH_NET_TUN, O_RDWR));

    if (fd < 0) {

        error_report("could not open %s: %m", PATH_NET_TUN);

        return -1;

    }

    memset(&ifr, 0, sizeof(ifr));

    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;



    if (*vnet_hdr) {

        unsigned int features;



        if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&

            features & IFF_VNET_HDR) {

            *vnet_hdr = 1;

            ifr.ifr_flags |= IFF_VNET_HDR;

        } else {

            *vnet_hdr = 0;

        }



        if (vnet_hdr_required && !*vnet_hdr) {

            error_report("vnet_hdr=1 requested, but no kernel "

                         "support for IFF_VNET_HDR available");

            close(fd);

            return -1;

        }

        











        ioctl(fd, TUNSETVNETHDRSZ, &len);

    }



    if (mq_required) {

        unsigned int features;



        if ((ioctl(fd, TUNGETFEATURES, &features) != 0) ||

            !(features & IFF_MULTI_QUEUE)) {

            error_report("multiqueue required, but no kernel "

                         "support for IFF_MULTI_QUEUE available");

            close(fd);

            return -1;

        } else {

            ifr.ifr_flags |= IFF_MULTI_QUEUE;

        }

    }



    if (ifname[0] != '\0')

        pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);

    else

        pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");

    ret = ioctl(fd, TUNSETIFF, (void *) &ifr);

    if (ret != 0) {

        if (ifname[0] != '\0') {

            error_report("could not configure %s (%s): %m", PATH_NET_TUN, ifr.ifr_name);

        } else {

            error_report("could not configure %s: %m", PATH_NET_TUN);

        }

        close(fd);

        return -1;

    }

    pstrcpy(ifname, ifname_size, ifr.ifr_name);

    fcntl(fd, F_SETFL, O_NONBLOCK);

    return fd;

}

    if (*vnet_hdr) {         unsigned int features;         unsigned int features;
int FUNC_0(char *VAR_0, int VAR_1, int *VAR_2,

             int VAR_3, int VAR_4)

{

    struct ifreq VAR_5;

    int VAR_6, VAR_7;

    int VAR_8 = sizeof(struct virtio_net_hdr);



    TFR(VAR_6 = open(PATH_NET_TUN, O_RDWR));

    if (VAR_6 < 0) {

        error_report("could not open %s: %m", PATH_NET_TUN);

        return -1;

    }

    memset(&VAR_5, 0, sizeof(VAR_5));

    VAR_5.ifr_flags = IFF_TAP | IFF_NO_PI;



    if (*VAR_2) {

        unsigned int VAR_10;



        if (ioctl(VAR_6, TUNGETFEATURES, &VAR_10) == 0 &&

            VAR_10 & IFF_VNET_HDR) {

            *VAR_2 = 1;

            VAR_5.ifr_flags |= IFF_VNET_HDR;

        } else {

            *VAR_2 = 0;

        }



        if (VAR_3 && !*VAR_2) {

            error_report("VAR_2=1 requested, but no kernel "

                         "support for IFF_VNET_HDR available");

            close(VAR_6);

            return -1;

        }

        











        ioctl(VAR_6, TUNSETVNETHDRSZ, &VAR_8);

    }



    if (VAR_4) {

        unsigned int VAR_10;



        if ((ioctl(VAR_6, TUNGETFEATURES, &VAR_10) != 0) ||

            !(VAR_10 & IFF_MULTI_QUEUE)) {

            error_report("multiqueue required, but no kernel "

                         "support for IFF_MULTI_QUEUE available");

            close(VAR_6);

            return -1;

        } else {

            VAR_5.ifr_flags |= IFF_MULTI_QUEUE;

        }

    }



    if (VAR_0[0] != '\0')

        pstrcpy(VAR_5.ifr_name, IFNAMSIZ, VAR_0);

    else

        pstrcpy(VAR_5.ifr_name, IFNAMSIZ, "tap%d");

    VAR_7 = ioctl(VAR_6, TUNSETIFF, (void *) &VAR_5);

    if (VAR_7 != 0) {

        if (VAR_0[0] != '\0') {

            error_report("could not configure %s (%s): %m", PATH_NET_TUN, VAR_5.ifr_name);

        } else {

            error_report("could not configure %s: %m", PATH_NET_TUN);

        }

        close(VAR_6);

        return -1;

    }

    pstrcpy(VAR_0, VAR_1, VAR_5.ifr_name);

    fcntl(VAR_6, F_SETFL, O_NONBLOCK);

    return VAR_6;

}

int FUNC_0(char *VAR_0, int VAR_1, int *VAR_2,
int VAR_3, int VAR_4)
{ struct ifreq VAR_5; int VAR_6, VAR_7; int VAR_8 = sizeof(struct virtio_net_hdr); TFR(VAR_6 = open(PATH_NET_TUN, O_RDWR)); if (VAR_6 < 0) { error_report("could not open %s: %m", PATH_NET_TUN); return -1; } memset(&VAR_5, 0, sizeof(VAR_5)); VAR_5.ifr_flags = IFF_TAP | IFF_NO_PI; if (*VAR_2) { unsigned int VAR_10; if (ioctl(VAR_6, TUNGETFEATURES, &VAR_10) == 0 &&
VAR_10 & IFF_VNET_HDR) { *VAR_2 = 1; VAR_5.ifr_flags |= IFF_VNET_HDR; } else { *VAR_2 = 0; } if (VAR_3 && !*VAR_2) { error_report("VAR_2=1 requested, but no kernel "
"support for IFF_VNET_HDR available"); close(VAR_6); return -1; } ioctl(VAR_6, TUNSETVNETHDRSZ, &VAR_8); } if (VAR_4) { unsigned int VAR_10; if ((ioctl(VAR_6, TUNGETFEATURES, &VAR_10) != 0) ||
!(VAR_10 & IFF_MULTI_QUEUE)) { error_report("multiqueue required, but no kernel "
"support for IFF_MULTI_QUEUE available"); close(VAR_6); return -1; } else { VAR_5.ifr_flags |= IFF_MULTI_QUEUE; } } if (VAR_0[0] != '\0')
pstrcpy(VAR_5.ifr_name, IFNAMSIZ, VAR_0); else
pstrcpy(VAR_5.ifr_name, IFNAMSIZ, "tap%d"); VAR_7 = ioctl(VAR_6, TUNSETIFF, (void *) &VAR_5); if (VAR_7 != 0) { if (VAR_0[0] != '\0') { error_report("could not configure %s (%s): %m", PATH_NET_TUN, VAR_5.ifr_name); } else { error_report("could not configure %s: %m", PATH_NET_TUN); } close(VAR_6); return -1; } pstrcpy(VAR_0, VAR_1, VAR_5.ifr_name); fcntl(VAR_6, F_SETFL, O_NONBLOCK); return VAR_6; }