go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2202/vxlan_gpe_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 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types" 23 vpp_gpe "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/vxlan_gpe" 24 ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces" 25 ) 26 27 func (h *InterfaceVppHandler) vxLanGpeAddDelTunnel(isAdd bool, vxLan *ifs.VxlanLink, vrf, multicastIf uint32) (uint32, error) { 28 req := &vpp_gpe.VxlanGpeAddDelTunnel{ 29 McastSwIfIndex: interface_types.InterfaceIndex(multicastIf), 30 EncapVrfID: vrf, 31 DecapVrfID: vxLan.Gpe.DecapVrfId, 32 Protocol: ip_types.IPProto(vxLan.Gpe.Protocol), 33 Vni: vxLan.Vni, 34 IsAdd: isAdd, 35 } 36 37 if vxLan.SrcAddress == vxLan.DstAddress { 38 return 0, fmt.Errorf("source and destination addresses must not be the same") 39 } 40 srcAddr := net.ParseIP(vxLan.SrcAddress).To4() 41 dstAddr := net.ParseIP(vxLan.DstAddress).To4() 42 if srcAddr == nil && dstAddr == nil { 43 srcAddr = net.ParseIP(vxLan.SrcAddress).To16() 44 dstAddr = net.ParseIP(vxLan.DstAddress).To16() 45 if srcAddr == nil || dstAddr == nil { 46 return 0, fmt.Errorf("invalid VXLAN address, src: %s, dst: %s", srcAddr, dstAddr) 47 } 48 } else if srcAddr == nil && dstAddr != nil || srcAddr != nil && dstAddr == nil { 49 return 0, fmt.Errorf("IP version mismatch for VXLAN destination and source IP addresses") 50 } 51 52 req.Local, _ = IPToAddress(srcAddr.String()) 53 req.Remote, _ = IPToAddress(dstAddr.String()) 54 55 reply := &vpp_gpe.VxlanGpeAddDelTunnelReply{} 56 57 if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil { 58 return 0, err 59 } 60 return uint32(reply.SwIfIndex), nil 61 } 62 63 // AddVxLanGpeTunnel adds new VxLAN-GPE interface. 64 func (h *InterfaceVppHandler) AddVxLanGpeTunnel(ifName string, vrf, multicastIf uint32, vxLan *ifs.VxlanLink) (uint32, error) { 65 swIfIndex, err := h.vxLanGpeAddDelTunnel(true, vxLan, vrf, multicastIf) 66 if err != nil { 67 return 0, err 68 } 69 return swIfIndex, h.SetInterfaceTag(ifName, swIfIndex) 70 } 71 72 // DeleteVxLanGpeTunnel removes VxLAN-GPE interface. 73 func (h *InterfaceVppHandler) DeleteVxLanGpeTunnel(ifName string, vxLan *ifs.VxlanLink) error { 74 swIfIndex, err := h.vxLanGpeAddDelTunnel(false, vxLan, 0, 0) 75 if err != nil { 76 return err 77 } 78 return h.RemoveInterfaceTag(ifName, swIfIndex) 79 }