go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/vppcalls/vpp2210/vppcalls_handler.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 vpp2210 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 "go.ligato.io/vpp-agent/v3/pkg/idxvpp" 25 vpp2210 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/ip_types" 27 l2ba "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/l2" 28 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx" 29 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls" 30 ) 31 32 func init() { 33 vppcalls.AddHandlerVersion(vpp2210.Version, l2ba.AllMessages(), NewL2VppHandler) 34 } 35 36 type L2VppHandler struct { 37 *BridgeDomainVppHandler 38 *FIBVppHandler 39 *XConnectVppHandler 40 } 41 42 func NewL2VppHandler(ch govppapi.Channel, 43 ifIdx ifaceidx.IfaceMetadataIndex, bdIdx idxvpp.NameToIndex, log logging.Logger, 44 ) vppcalls.L2VppAPI { 45 return &L2VppHandler{ 46 BridgeDomainVppHandler: newBridgeDomainVppHandler(ch, ifIdx, log), 47 FIBVppHandler: newFIBVppHandler(ch, ifIdx, bdIdx, log), 48 XConnectVppHandler: newXConnectVppHandler(ch, ifIdx, log), 49 } 50 } 51 52 // BridgeDomainVppHandler is accessor for bridge domain-related vppcalls methods. 53 type BridgeDomainVppHandler struct { 54 callsChannel govppapi.Channel 55 ifIndexes ifaceidx.IfaceMetadataIndex 56 log logging.Logger 57 } 58 59 // FIBVppHandler is accessor for FIB-related vppcalls methods. 60 type FIBVppHandler struct { 61 callsChannel govppapi.Channel 62 ifIndexes ifaceidx.IfaceMetadataIndex 63 bdIndexes idxvpp.NameToIndex 64 log logging.Logger 65 } 66 67 // XConnectVppHandler is accessor for cross-connect-related vppcalls methods. 68 type XConnectVppHandler struct { 69 callsChannel govppapi.Channel 70 ifIndexes ifaceidx.IfaceMetadataIndex 71 log logging.Logger 72 } 73 74 // NewBridgeDomainVppHandler creates new instance of bridge domain vppcalls handler. 75 func newBridgeDomainVppHandler(ch govppapi.Channel, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) *BridgeDomainVppHandler { 76 return &BridgeDomainVppHandler{ 77 callsChannel: ch, 78 ifIndexes: ifIdx, 79 log: log, 80 } 81 } 82 83 // NewFIBVppHandler creates new instance of FIB vppcalls handler. 84 func newFIBVppHandler(ch govppapi.Channel, ifIdx ifaceidx.IfaceMetadataIndex, bdIndexes idxvpp.NameToIndex, log logging.Logger) *FIBVppHandler { 85 return &FIBVppHandler{ 86 callsChannel: ch, 87 ifIndexes: ifIdx, 88 bdIndexes: bdIndexes, 89 log: log, 90 } 91 } 92 93 // NewXConnectVppHandler creates new instance of cross connect vppcalls handler. 94 func newXConnectVppHandler(ch govppapi.Channel, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) *XConnectVppHandler { 95 return &XConnectVppHandler{ 96 callsChannel: ch, 97 ifIndexes: ifIdx, 98 log: log, 99 } 100 } 101 102 func ipToAddress(ipstr string) (addr ip_types.Address, err error) { 103 netIP := net.ParseIP(ipstr) 104 if netIP == nil { 105 return ip_types.Address{}, fmt.Errorf("invalid IP: %q", ipstr) 106 } 107 if ip4 := netIP.To4(); ip4 == nil { 108 addr.Af = ip_types.ADDRESS_IP6 109 var ip6addr ip_types.IP6Address 110 copy(ip6addr[:], netIP.To16()) 111 addr.Un.SetIP6(ip6addr) 112 } else { 113 addr.Af = ip_types.ADDRESS_IP4 114 var ip4addr ip_types.IP4Address 115 copy(ip4addr[:], ip4) 116 addr.Un.SetIP4(ip4addr) 117 } 118 return 119 }