go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2106/tap_vppcalls.go (about)

     1  //  Copyright (c) 2021 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package vpp2106
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/interface_types"
    22  	vpp_tapv2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/tapv2"
    23  	ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    24  )
    25  
    26  func (h *InterfaceVppHandler) AddTapInterface(ifName string, tapIf *ifs.TapLink) (swIfIdx uint32, err error) {
    27  	if tapIf == nil || tapIf.HostIfName == "" {
    28  		return 0, errors.New("host interface name was not provided for the TAP interface")
    29  	}
    30  
    31  	if tapIf.Version == 1 {
    32  		return 0, errors.New("tap version 1 has been deprecated")
    33  	} else if tapIf.Version == 2 {
    34  		var flags vpp_tapv2.TapFlags
    35  		if tapIf.EnableGso {
    36  			flags |= vpp_tapv2.TAP_API_FLAG_GSO
    37  		}
    38  		if tapIf.EnableTunnel {
    39  			flags |= vpp_tapv2.TAP_API_FLAG_TUN
    40  		}
    41  
    42  		// Configure fast virtio-based TAP interface
    43  		req := &vpp_tapv2.TapCreateV2{
    44  			ID:            ^uint32(0),
    45  			NumRxQueues:   1,
    46  			HostIfName:    tapIf.HostIfName,
    47  			HostIfNameSet: true,
    48  			UseRandomMac:  true,
    49  			RxRingSz:      uint16(tapIf.RxRingSize),
    50  			TxRingSz:      uint16(tapIf.TxRingSize),
    51  			TapFlags:      flags,
    52  		}
    53  
    54  		reply := &vpp_tapv2.TapCreateV2Reply{}
    55  		if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    56  			return 0, err
    57  		}
    58  		swIfIdx = uint32(reply.SwIfIndex)
    59  	} else {
    60  		return 0, fmt.Errorf("invalid tap version (%v)", tapIf.Version)
    61  	}
    62  
    63  	return swIfIdx, h.SetInterfaceTag(ifName, swIfIdx)
    64  }
    65  
    66  func (h *InterfaceVppHandler) DeleteTapInterface(ifName string, idx uint32, version uint32) error {
    67  	if version == 1 {
    68  		return errors.New("tap version 1 has been deprecated")
    69  	} else if version == 2 {
    70  		req := &vpp_tapv2.TapDeleteV2{
    71  			SwIfIndex: interface_types.InterfaceIndex(idx),
    72  		}
    73  
    74  		reply := &vpp_tapv2.TapDeleteV2Reply{}
    75  		if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    76  			return err
    77  		}
    78  	} else {
    79  		return fmt.Errorf("invalid tap version (%v)", version)
    80  	}
    81  
    82  	return h.RemoveInterfaceTag(ifName, idx)
    83  }