go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/l2plugin.go (about) 1 // Copyright (c) 2021 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 //go:generate descriptor-adapter --descriptor-name BridgeDomain --value-type *vpp_l2.BridgeDomain --meta-type *idxvpp.OnlyIndex --import "go.ligato.io/vpp-agent/v3/pkg/idxvpp" --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2" --output-dir "descriptor" 16 //go:generate descriptor-adapter --descriptor-name BDInterface --value-type *vpp_l2.BridgeDomain_Interface --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2" --output-dir "descriptor" 17 //go:generate descriptor-adapter --descriptor-name FIB --value-type *vpp_l2.FIBEntry --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2" --output-dir "descriptor" 18 //go:generate descriptor-adapter --descriptor-name XConnect --value-type *vpp_l2.XConnectPair --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2" --output-dir "descriptor" 19 20 package l2plugin 21 22 import ( 23 "github.com/pkg/errors" 24 "go.ligato.io/cn-infra/v2/health/statuscheck" 25 "go.ligato.io/cn-infra/v2/infra" 26 27 "go.ligato.io/vpp-agent/v3/pkg/idxvpp" 28 "go.ligato.io/vpp-agent/v3/plugins/govppmux" 29 kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 30 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin" 31 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/descriptor" 32 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/descriptor/adapter" 33 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls" 34 35 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2101" 36 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2106" 37 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2202" 38 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2210" 39 ) 40 41 // L2Plugin configures VPP bridge domains, L2 FIBs and xConnects using GoVPP. 42 type L2Plugin struct { 43 Deps 44 45 // handlers 46 l2Handler vppcalls.L2VppAPI 47 48 // descriptors 49 bdDescriptor *descriptor.BridgeDomainDescriptor 50 bdIfaceDescriptor *descriptor.BDInterfaceDescriptor 51 fibDescriptor *descriptor.FIBDescriptor 52 xcDescriptor *descriptor.XConnectDescriptor 53 54 // index maps 55 bdIndex idxvpp.NameToIndex 56 } 57 58 // Deps lists dependencies of the L2 plugin. 59 type Deps struct { 60 infra.PluginDeps 61 KVScheduler kvs.KVScheduler 62 VPP govppmux.API 63 IfPlugin ifplugin.API 64 StatusCheck statuscheck.PluginStatusWriter // optional 65 } 66 67 // Init registers L2-related descriptors. 68 func (p *L2Plugin) Init() (err error) { 69 // init handlers 70 p.l2Handler = vppcalls.CompatibleL2VppHandler(p.VPP, p.IfPlugin.GetInterfaceIndex(), p.bdIndex, p.Log) 71 if p.l2Handler == nil { 72 return errors.Errorf("could not find compatible L2VppHandler") 73 } 74 75 // init and register bridge domain descriptor 76 p.bdDescriptor = descriptor.NewBridgeDomainDescriptor(p.l2Handler, p.Log) 77 bdDescriptor := adapter.NewBridgeDomainDescriptor(p.bdDescriptor.GetDescriptor()) 78 err = p.KVScheduler.RegisterKVDescriptor(bdDescriptor) 79 if err != nil { 80 return err 81 } 82 83 // obtain read-only references to BD index map 84 var withIndex bool 85 metadataMap := p.KVScheduler.GetMetadataMap(bdDescriptor.Name) 86 p.bdIndex, withIndex = metadataMap.(idxvpp.NameToIndex) 87 if !withIndex { 88 return errors.New("missing index with bridge domain metadata") 89 } 90 91 // we set l2Handler again here, because bdIndex was nil before 92 p.l2Handler = vppcalls.CompatibleL2VppHandler(p.VPP, p.IfPlugin.GetInterfaceIndex(), p.bdIndex, p.Log) 93 94 // init & register descriptors 95 p.bdIfaceDescriptor = descriptor.NewBDInterfaceDescriptor(p.bdIndex, p.l2Handler, p.Log) 96 bdIfaceDescriptor := adapter.NewBDInterfaceDescriptor(p.bdIfaceDescriptor.GetDescriptor()) 97 err = p.KVScheduler.RegisterKVDescriptor(bdIfaceDescriptor) 98 if err != nil { 99 return err 100 } 101 102 p.fibDescriptor = descriptor.NewFIBDescriptor(p.l2Handler, p.Log) 103 fibDescriptor := adapter.NewFIBDescriptor(p.fibDescriptor.GetDescriptor()) 104 err = p.KVScheduler.RegisterKVDescriptor(fibDescriptor) 105 if err != nil { 106 return err 107 } 108 109 p.xcDescriptor = descriptor.NewXConnectDescriptor(p.l2Handler, p.Log) 110 xcDescriptor := adapter.NewXConnectDescriptor(p.xcDescriptor.GetDescriptor()) 111 err = p.KVScheduler.RegisterKVDescriptor(xcDescriptor) 112 if err != nil { 113 return err 114 } 115 116 return nil 117 } 118 119 // AfterInit registers plugin with StatusCheck. 120 func (p *L2Plugin) AfterInit() error { 121 if p.StatusCheck != nil { 122 p.StatusCheck.Register(p.PluginName, nil) 123 } 124 return nil 125 } 126 127 // GetBDIndex return bridge domain index. 128 func (p *L2Plugin) GetBDIndex() idxvpp.NameToIndex { 129 return p.bdIndex 130 }