go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ipfixplugin/ipfixplugin.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 IPFIX  --value-type *vpp_ipfix.IPFIX --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipfix" --output-dir "descriptor"
    16  //go:generate descriptor-adapter --descriptor-name FlowProbeFeature  --value-type *vpp_ipfix.FlowProbeFeature --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipfix" --output-dir "descriptor"
    17  //go:generate descriptor-adapter --descriptor-name FlowProbeParams  --value-type *vpp_ipfix.FlowProbeParams --import "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/ipfix" --output-dir "descriptor"
    18  
    19  package ipfixplugin
    20  
    21  import (
    22  	"errors"
    23  
    24  	"go.ligato.io/cn-infra/v2/infra"
    25  
    26  	"go.ligato.io/vpp-agent/v3/plugins/govppmux"
    27  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
    29  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/descriptor"
    30  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls"
    31  
    32  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls/vpp2101"
    33  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls/vpp2106"
    34  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls/vpp2202"
    35  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ipfixplugin/vppcalls/vpp2210"
    36  )
    37  
    38  // IPFIXPlugin is a plugin that manages IPFIX configuration in VPP.
    39  // IPFIX - IP Flow Information Export (IPFIX).
    40  // It allows to:
    41  //   - configure export of Flowprobe information;
    42  //   - configure Flowprobe Params;
    43  //   - enable/disable Flowprobe Feature for an interface.
    44  //
    45  // Things to rememmber:
    46  //   - Flowprobe Feature can not be configured for any interface,
    47  //     if Flowprobe Params were not set.
    48  //   - Flowprobe Params can not be changed,
    49  //     if Flowprobe Feature was enabled for at least one interface.
    50  type IPFIXPlugin struct {
    51  	Deps
    52  
    53  	// VPP handler.
    54  	ipfixHandler vppcalls.IpfixVppAPI
    55  }
    56  
    57  // Deps represents dependencies for the plugin.
    58  type Deps struct {
    59  	infra.PluginDeps
    60  	KVScheduler kvs.KVScheduler
    61  	VPP         govppmux.API
    62  	IfPlugin    ifplugin.API
    63  }
    64  
    65  // Init initializes IPFIX plugin.
    66  func (p *IPFIXPlugin) Init() (err error) {
    67  	// Even with IPFIX being part of a core of VPP, without Flowprobe plugin
    68  	// there would be no information to export, hence no point in this plugin.
    69  	if !p.VPP.IsPluginLoaded("flowprobe") {
    70  		p.Log.Warnf("VPP plugin Flowprobe was disabled by VPP")
    71  		return nil
    72  	}
    73  
    74  	p.ipfixHandler = vppcalls.CompatibleIpfixVppHandler(p.VPP, p.IfPlugin.GetInterfaceIndex(), p.Log)
    75  	if p.ipfixHandler == nil {
    76  		return errors.New("IPFIX VPP handler is not available")
    77  	}
    78  
    79  	ipfixDescriptor := descriptor.NewIPFIXDescriptor(p.ipfixHandler, p.Log)
    80  	err = p.KVScheduler.RegisterKVDescriptor(ipfixDescriptor)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	fpFeatureDescriptor := descriptor.NewFPFeatureDescriptor(p.ipfixHandler, p.Log)
    86  	err = p.KVScheduler.RegisterKVDescriptor(fpFeatureDescriptor)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	// Descriptor for Flowprobe Params will use `fpFeatureMM` to check
    92  	// if Flowprobe Params can be updated. If at least one item is in this
    93  	// map, than there is at least one interface with Flowprobe Feature
    94  	// enabled, hence Flowprobe Params update is not allowed.
    95  	fpFeatureMM := p.KVScheduler.GetMetadataMap(fpFeatureDescriptor.Name)
    96  
    97  	fpParamsDescriptor := descriptor.NewFPParamsDescriptor(p.ipfixHandler, fpFeatureMM, p.Log)
    98  	err = p.KVScheduler.RegisterKVDescriptor(fpParamsDescriptor)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return nil
   104  }