1. 程式人生 > 實用技巧 >kata tap

kata tap

#
# Internetworking model
# Determines how the VM should be connected to the
# the container network interface
# Options:
#
#   - macvtap
#     Used when the Container network interface can be bridged using
#     macvtap.
#
#   - none
#     Used when customize network. Only creates a tap device. No veth pair.
# #
- tcfilter # Uses tc filter rules to redirect traffic from the network interface # provided by plugin to a tap interface connected to the VM. #

internetworking_model="tcfilter"

# If enabled, the runtime will not create a network namespace for shim and hypervisor processes.
# This option may have some potential impacts to your host. It should only be used when you know what you
're doing. # `disable_new_netns` conflicts with `enable_netmon` # `disable_new_netns` conflicts with `internetworking_model=tcfilter` and `internetworking_model=macvtap`. It works only # with `internetworking_model=none`. The tap device will be in the host network namespace and can connect to a bridge # (like OVS) directly. # If you are
using docker, `disable_new_netns` only works with `docker run --net=none` # (default: false) #disable_new_netns = true

func createEndpoint(netInfo NetworkInfo, idx int, model NetInterworkingModel, link netlink.Link) (Endpoint, error) {
        var endpoint Endpoint
        // TODO: This is the incoming interface
        // based on the incoming interface we should create
        // an appropriate EndPoint based on interface type
        // This should be a switch

        // Check if interface is a physical interface. Do not create
        // tap interface/bridge if it is.
        isPhysical, err := isPhysicalIface(netInfo.Iface.Name)
        if err != nil {
                return nil, err
        }

        if isPhysical {
                networkLogger().WithField("interface", netInfo.Iface.Name).Info("Physical network interface found")
                endpoint, err = createPhysicalEndpoint(netInfo)
        } else {
                var socketPath string

                // Check if this is a dummy interface which has a vhost-user socket associated with it
                socketPath, err = vhostUserSocketPath(netInfo)
                if err != nil {
                        return nil, err
                }

                if socketPath != "" {
                        networkLogger().WithField("interface", netInfo.Iface.Name).Info("VhostUser network interface found")
                        endpoint, err = createVhostUserEndpoint(netInfo, socketPath)
                } else if netInfo.Iface.Type == "macvlan" {
                        networkLogger().Infof("macvlan interface found")
                        endpoint, err = createBridgedMacvlanNetworkEndpoint(idx, netInfo.Iface.Name, model)
                } else if netInfo.Iface.Type == "macvtap" {
                        networkLogger().Infof("macvtap interface found")
                        endpoint, err = createMacvtapNetworkEndpoint(netInfo)
                } else if netInfo.Iface.Type == "tap" {
                        networkLogger().Info("tap interface found")
                        endpoint, err = createTapNetworkEndpoint(idx, netInfo.Iface.Name)
                } else if netInfo.Iface.Type == "tuntap" {
                        if link != nil {
                                switch link.(*netlink.Tuntap).Mode {
                                case 0:
                                        // mount /sys/class/net to get links
                                        return nil, fmt.Errorf("Network device mode not determined correctly. Mount sysfs in caller")
                                case 1:
                                        return nil, fmt.Errorf("tun networking device not yet supported")
                                case 2:
                                        networkLogger().Info("tuntap tap interface found")
                                        endpoint, err = createTuntapNetworkEndpoint(idx, netInfo.Iface.Name, netInfo.Iface.HardwareAddr, model)
                                default:
                                        return nil, fmt.Errorf("tuntap network %v mode unsupported", link.(*netlink.Tuntap).Mode)
                                }
                        }
                } else if netInfo.Iface.Type == "veth" {
                        endpoint, err = createVethNetworkEndpoint(idx, netInfo.Iface.Name, model)
                } else if netInfo.Iface.Type == "ipvlan" {
                        endpoint, err = createIPVlanNetworkEndpoint(idx, netInfo.Iface.Name)
                } else {
                        return nil, fmt.Errorf("Unsupported network interface: %s", netInfo.Iface.Type)
                }
        }

        return endpoint, err
}

func createTuntapNetworkEndpoint(idx int, ifName string, hwName net.HardwareAddr, internetworkingModel NetInterworkingModel) (*TuntapEndpoint, error) {
        if idx < 0 {
                return &TuntapEndpoint{}, fmt.Errorf("invalid network endpoint index: %d", idx)
        }

        netPair, err := createNetworkInterfacePair(idx, ifName, internetworkingModel)
        if err != nil {
                return nil, err
        }

        endpoint := &TuntapEndpoint{
                NetPair: netPair,
                TuntapInterface: TuntapInterface{
                        Name: fmt.Sprintf("eth%d", idx),
                        TAPIface: NetworkInterface{
                                Name:     fmt.Sprintf("tap%d_kata", idx),
                                HardAddr: fmt.Sprintf("%s", hwName), //nolint:gosimple
                        },
                },
                EndpointType: TuntapEndpointType,
        }

        if ifName != "" {
                endpoint.TuntapInterface.Name = ifName
        }

        return endpoint, nil
}

tcFilterNetModelStr = "tcfilter"

//SetModel change the model string value
func (n *NetInterworkingModel) SetModel(modelName string) error {
        switch modelName {
        case defaultNetModelStr:
                *n = DefaultNetInterworkingModel
                return nil
        case macvtapNetModelStr:
                *n = NetXConnectMacVtapModel
                return nil
        case tcFilterNetModelStr:
                *n = NetXConnectTCFilterModel
                return nil
        case noneNetModelStr:
                *n = NetXConnectNoneModel
                return nil
        }
        return fmt.Errorf("Unknown type %s", modelName)
}