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

     1  //  Copyright (c) 2022 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 vpp2202
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface_types"
    22  	vpp_vxlan "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/vxlan"
    23  	ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    24  )
    25  
    26  func (h *InterfaceVppHandler) addDelVxLanTunnel(vxLan *ifs.VxlanLink, vrf, multicastIf uint32, isAdd bool) (swIdx uint32, err error) {
    27  	req := &vpp_vxlan.VxlanAddDelTunnel{
    28  		IsAdd:          isAdd,
    29  		Vni:            vxLan.Vni,
    30  		DecapNextIndex: 0xFFFFFFFF,
    31  		Instance:       ^uint32(0),
    32  		EncapVrfID:     vrf,
    33  		McastSwIfIndex: interface_types.InterfaceIndex(multicastIf),
    34  	}
    35  
    36  	srcAddr := net.ParseIP(vxLan.SrcAddress).To4()
    37  	dstAddr := net.ParseIP(vxLan.DstAddress).To4()
    38  	if srcAddr == nil && dstAddr == nil {
    39  		srcAddr = net.ParseIP(vxLan.SrcAddress).To16()
    40  		dstAddr = net.ParseIP(vxLan.DstAddress).To16()
    41  		if srcAddr == nil || dstAddr == nil {
    42  			return 0, fmt.Errorf("invalid VXLAN address, src: %s, dst: %s", srcAddr, dstAddr)
    43  		}
    44  	} else if srcAddr == nil && dstAddr != nil || srcAddr != nil && dstAddr == nil {
    45  		return 0, fmt.Errorf("IP version mismatch for VXLAN destination and source IP addresses")
    46  	}
    47  
    48  	req.SrcAddress, _ = IPToAddress(srcAddr.String())
    49  	req.DstAddress, _ = IPToAddress(dstAddr.String())
    50  
    51  	reply := &vpp_vxlan.VxlanAddDelTunnelReply{}
    52  	if err = h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    53  		return 0, err
    54  	}
    55  
    56  	return uint32(reply.SwIfIndex), nil
    57  }
    58  
    59  // AddVxLanTunnel implements VxLan handler.
    60  func (h *InterfaceVppHandler) AddVxLanTunnel(ifName string, vrf, multicastIf uint32, vxLan *ifs.VxlanLink) (swIndex uint32, err error) {
    61  	swIfIdx, err := h.addDelVxLanTunnel(vxLan, vrf, multicastIf, true)
    62  	if err != nil {
    63  		return 0, err
    64  	}
    65  	return swIfIdx, h.SetInterfaceTag(ifName, swIfIdx)
    66  }
    67  
    68  // DeleteVxLanTunnel implements VxLan handler.
    69  func (h *InterfaceVppHandler) DeleteVxLanTunnel(ifName string, idx, vrf uint32, vxLan *ifs.VxlanLink) error {
    70  	// Multicast does not need to be set
    71  	if _, err := h.addDelVxLanTunnel(vxLan, vrf, 0, false); err != nil {
    72  		return err
    73  	}
    74  	return h.RemoveInterfaceTag(ifName, idx)
    75  }