go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/aclplugin/aclplugin.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 ACL --value-type *vpp_acl.ACL --meta-type *aclidx.ACLMetadata --import "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/aclidx" --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/acl" --output-dir "descriptor" 16 17 package aclplugin 18 19 import ( 20 "github.com/pkg/errors" 21 "go.ligato.io/cn-infra/v2/health/statuscheck" 22 "go.ligato.io/cn-infra/v2/infra" 23 24 "go.ligato.io/vpp-agent/v3/plugins/govppmux" 25 kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/aclidx" 27 "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/descriptor" 28 "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/descriptor/adapter" 29 "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls" 30 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin" 31 32 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls/vpp2101" 33 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls/vpp2106" 34 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls/vpp2202" 35 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls/vpp2210" 36 ) 37 38 // ACLPlugin is a plugin that manages ACLs. 39 type ACLPlugin struct { 40 Deps 41 42 aclHandler vppcalls.ACLVppAPI 43 aclDescriptor *descriptor.ACLDescriptor 44 aclInterfaceDescriptor *descriptor.ACLToInterfaceDescriptor 45 46 // index maps 47 aclIndex aclidx.ACLMetadataIndex 48 } 49 50 // Deps represents dependencies for the plugin. 51 type Deps struct { 52 infra.PluginDeps 53 Scheduler kvs.KVScheduler 54 VPP govppmux.API 55 IfPlugin ifplugin.API 56 StatusCheck statuscheck.PluginStatusWriter // optional 57 } 58 59 // Init initializes ACL plugin. 60 func (p *ACLPlugin) Init() (err error) { 61 if !p.VPP.IsPluginLoaded("acl") { 62 p.Log.Warnf("VPP plugin ACL was disabled by VPP") 63 return nil 64 } 65 66 // init handlers 67 p.aclHandler = vppcalls.CompatibleACLHandler(p.VPP, p.IfPlugin.GetInterfaceIndex()) 68 if p.aclHandler == nil { 69 return errors.New("aclHandler is not available") 70 } 71 72 // init & register descriptors 73 p.aclDescriptor = descriptor.NewACLDescriptor(p.aclHandler, p.IfPlugin, p.Log) 74 aclDescriptor := adapter.NewACLDescriptor(p.aclDescriptor.GetDescriptor()) 75 err = p.Scheduler.RegisterKVDescriptor(aclDescriptor) 76 if err != nil { 77 return err 78 } 79 80 // obtain read-only references to index maps 81 var withIndex bool 82 metadataMap := p.Scheduler.GetMetadataMap(aclDescriptor.Name) 83 p.aclIndex, withIndex = metadataMap.(aclidx.ACLMetadataIndex) 84 if !withIndex { 85 return errors.New("missing index with ACL metadata") 86 } 87 88 p.aclInterfaceDescriptor = descriptor.NewACLToInterfaceDescriptor(p.aclIndex, p.aclHandler, p.Log) 89 aclInterfaceDescriptor := p.aclInterfaceDescriptor.GetDescriptor() 90 err = p.Scheduler.RegisterKVDescriptor(aclInterfaceDescriptor) 91 if err != nil { 92 return err 93 } 94 95 return nil 96 } 97 98 // AfterInit registers plugin with StatusCheck. 99 func (p *ACLPlugin) AfterInit() error { 100 if p.StatusCheck != nil { 101 p.StatusCheck.Register(p.PluginName, nil) 102 } 103 return nil 104 } 105 106 // GetInterfaceIndex gives read-only access to map with metadata of all configured 107 // VPP interfaces. 108 func (p *ACLPlugin) GetACLIndex() aclidx.ACLMetadataIndex { 109 return p.aclIndex 110 }