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

     1  package vpp2101
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  
     7  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/interface_types"
     8  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types"
     9  	vpp_gpe "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/vxlan_gpe"
    10  	ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    11  )
    12  
    13  func (h *InterfaceVppHandler) vxLanGpeAddDelTunnel(isAdd bool, vxLan *ifs.VxlanLink, vrf, multicastIf uint32) (uint32, error) {
    14  	req := &vpp_gpe.VxlanGpeAddDelTunnel{
    15  		McastSwIfIndex: interface_types.InterfaceIndex(multicastIf),
    16  		EncapVrfID:     vrf,
    17  		DecapVrfID:     vxLan.Gpe.DecapVrfId,
    18  		Protocol:       ip_types.IPProto(vxLan.Gpe.Protocol),
    19  		Vni:            vxLan.Vni,
    20  		IsAdd:          isAdd,
    21  	}
    22  
    23  	if vxLan.SrcAddress == vxLan.DstAddress {
    24  		return 0, fmt.Errorf("source and destination addresses must not be the same")
    25  	}
    26  	srcAddr := net.ParseIP(vxLan.SrcAddress).To4()
    27  	dstAddr := net.ParseIP(vxLan.DstAddress).To4()
    28  	if srcAddr == nil && dstAddr == nil {
    29  		srcAddr = net.ParseIP(vxLan.SrcAddress).To16()
    30  		dstAddr = net.ParseIP(vxLan.DstAddress).To16()
    31  		if srcAddr == nil || dstAddr == nil {
    32  			return 0, fmt.Errorf("invalid VXLAN address, src: %s, dst: %s", srcAddr, dstAddr)
    33  		}
    34  	} else if srcAddr == nil && dstAddr != nil || srcAddr != nil && dstAddr == nil {
    35  		return 0, fmt.Errorf("IP version mismatch for VXLAN destination and source IP addresses")
    36  	}
    37  
    38  	req.Local, _ = IPToAddress(srcAddr.String())
    39  	req.Remote, _ = IPToAddress(dstAddr.String())
    40  
    41  	reply := &vpp_gpe.VxlanGpeAddDelTunnelReply{}
    42  
    43  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    44  		return 0, err
    45  	}
    46  	return uint32(reply.SwIfIndex), nil
    47  }
    48  
    49  // AddVxLanGpeTunnel adds new VxLAN-GPE interface.
    50  func (h *InterfaceVppHandler) AddVxLanGpeTunnel(ifName string, vrf, multicastIf uint32, vxLan *ifs.VxlanLink) (uint32, error) {
    51  	swIfIndex, err := h.vxLanGpeAddDelTunnel(true, vxLan, vrf, multicastIf)
    52  	if err != nil {
    53  		return 0, err
    54  	}
    55  	return swIfIndex, h.SetInterfaceTag(ifName, swIfIndex)
    56  }
    57  
    58  // DeleteVxLanGpeTunnel removes VxLAN-GPE interface.
    59  func (h *InterfaceVppHandler) DeleteVxLanGpeTunnel(ifName string, vxLan *ifs.VxlanLink) error {
    60  	swIfIndex, err := h.vxLanGpeAddDelTunnel(false, vxLan, 0, 0)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	return h.RemoveInterfaceTag(ifName, swIfIndex)
    65  }