mengdemao / lazybsd

用户态网络库实现
https://mengdemao.github.io/lazybsd/
BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

lazybsd_veth实现 #13

Open mengdemao opened 4 months ago

mengdemao commented 4 months ago

lazybsd虚拟网卡实现

lazybsd_veth_attach --> lazybsd_veth_setup_interface

参考f-stack实现

static int
ff_veth_setup_interface(struct ff_veth_softc *sc, struct ff_port_cfg *cfg)
{
    struct ifnet *ifp;

    ifp = sc->ifp = if_alloc(IFT_ETHER);

    ifp->if_init = ff_veth_init;
    ifp->if_softc = sc;

    if_initname(ifp, sc->host_ifname, IF_DUNIT_NONE);
    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    ifp->if_ioctl = ff_veth_ioctl;
    ifp->if_start = ff_veth_start;
    ifp->if_transmit = ff_veth_transmit;
    ifp->if_qflush = ff_veth_qflush;
    ether_ifattach(ifp, sc->mac);

    if (cfg->hw_features.rx_csum) {
        ifp->if_capabilities |= IFCAP_RXCSUM;
    }
    if (cfg->hw_features.tx_csum_ip) {
        ifp->if_capabilities |= IFCAP_TXCSUM;
        ifp->if_hwassist |= CSUM_IP;
    }
    if (cfg->hw_features.tx_csum_l4) {
        ifp->if_hwassist |= CSUM_DELAY_DATA;
    }
    if (cfg->hw_features.tx_tso) {
        ifp->if_capabilities |= IFCAP_TSO;
        ifp->if_hwassist |= CSUM_TSO;
    }

    ifp->if_capenable = ifp->if_capabilities;

    sc->host_ctx = ff_dpdk_register_if((void *)sc, (void *)sc->ifp, cfg);
    if (sc->host_ctx == NULL) {
        printf("%s: Failed to register dpdk interface\n", sc->host_ifname);
        return -1;
    }

    // Set IP
    int ret = ff_veth_setaddr(sc);
    if (ret != 0) {
        printf("ff_veth_setaddr failed\n");
    }
    ret = ff_veth_set_gateway(sc);
    if (ret != 0) {
        printf("ff_veth_set_gateway failed\n");
    }

    if (sc->nb_vip) {
        ret = ff_veth_setvaddr(sc, cfg);
    }

#ifdef INET6
    // Set IPv6
    if (cfg->addr6_str) {
        ret = ff_veth_setaddr6(sc);
        if (ret != 0) {
            printf("ff_veth_setaddr6 failed\n");
        }

        if (cfg->gateway6_str) {
            ret = ff_veth_set_gateway6(sc);
            if (ret != 0) {
                printf("ff_veth_set_gateway6 failed\n");
            }
        }
    }

    if (sc->nb_vip6) {
        ret = ff_veth_setvaddr6(sc, cfg);
    }
#endif /* INET6 */

    return (0);
}