go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/srplugin/vppcalls/vpp2101/vppcalls_handlers.go (about) 1 // Copyright (c) 2019 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 vpp2101 16 17 import ( 18 "fmt" 19 "net" 20 21 govppapi "go.fd.io/govpp/api" 22 "go.ligato.io/cn-infra/v2/logging" 23 24 core_vppcalls "go.ligato.io/vpp-agent/v3/plugins/govppmux/vppcalls" 25 core_vpp2101 "go.ligato.io/vpp-agent/v3/plugins/govppmux/vppcalls/vpp2101" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp" 27 vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101" 28 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip_types" 29 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/sr" 30 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/vpe" 31 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx" 32 "go.ligato.io/vpp-agent/v3/plugins/vpp/srplugin/vppcalls" 33 ) 34 35 func init() { 36 msgs := vpp.Messages( 37 sr.AllMessages, 38 vpe.AllMessages, // using also vpe -> need to have correct vpp version also for vpe 39 ) 40 vppcalls.AddHandlerVersion(vpp2101.Version, msgs.AllMessages(), NewSRv6VppHandler) 41 } 42 43 // SRv6VppHandler is accessor for SRv6-related vppcalls methods 44 type SRv6VppHandler struct { 45 core_vppcalls.VppCoreAPI 46 47 log logging.Logger 48 callsChannel govppapi.Channel 49 ifIndexes ifaceidx.IfaceMetadataIndex 50 } 51 52 // NewSRv6VppHandler creates new instance of SRv6 vppcalls handler 53 func NewSRv6VppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) vppcalls.SRv6VppAPI { 54 vppChan, err := c.NewAPIChannel() 55 if err != nil { 56 logging.Warnf("failed to create API channel") 57 return nil 58 } 59 return &SRv6VppHandler{ 60 callsChannel: vppChan, 61 ifIndexes: ifIdx, 62 log: log, 63 VppCoreAPI: core_vpp2101.NewVpeHandler(c), 64 } 65 } 66 67 func addressToIP(address ip_types.Address) net.IP { 68 if address.Af == ip_types.ADDRESS_IP6 { 69 ipAddr := address.Un.GetIP6() 70 return net.IP(ipAddr[:]).To16() 71 } 72 ipAddr := address.Un.GetIP4() 73 return net.IP(ipAddr[:]).To4() 74 } 75 76 // parseIPv6 parses string <str> to IPv6 address (including IPv4 address converted to IPv6 address) 77 func parseIPv6(str string) (net.IP, error) { 78 ip := net.ParseIP(str) 79 if ip == nil { 80 return nil, fmt.Errorf(" %q is not ip address", str) 81 } 82 ipv6 := ip.To16() 83 if ipv6 == nil { 84 return nil, fmt.Errorf(" %q is not ipv6 address", str) 85 } 86 return ipv6, nil 87 } 88 89 func IPToAddress(ipstr string) (addr ip_types.Address, err error) { 90 netIP := net.ParseIP(ipstr) 91 if netIP == nil { 92 return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipstr) 93 } 94 if ip4 := netIP.To4(); ip4 == nil { 95 addr.Af = ip_types.ADDRESS_IP6 96 var ip6addr ip_types.IP6Address 97 copy(ip6addr[:], netIP.To16()) 98 addr.Un.SetIP6(ip6addr) 99 } else { 100 addr.Af = ip_types.ADDRESS_IP4 101 var ip4addr ip_types.IP4Address 102 copy(ip4addr[:], ip4.To4()) 103 addr.Un.SetIP4(ip4addr) 104 } 105 return 106 }