go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/stnplugin/vppcalls/stn_vppcalls.go (about)

     1  // Copyright (c) 2018 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 vppcalls
    16  
    17  import (
    18  	govppapi "go.fd.io/govpp/api"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    24  	stn "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/stn"
    25  )
    26  
    27  // StnDetails contains a proto-modelled STN data and VPP specific metadata
    28  type StnDetails struct {
    29  	Rule *stn.Rule
    30  	Meta *StnMeta
    31  }
    32  
    33  // StnMeta contains an index of the interface defined by name in the STN rule
    34  type StnMeta struct {
    35  	IfIdx uint32
    36  }
    37  
    38  // StnVppAPI provides methods for managing STN rules
    39  type StnVppAPI interface {
    40  	StnVppRead
    41  
    42  	// AddSTNRule calls StnAddDelRule bin API with IsAdd=1
    43  	AddSTNRule(stnRule *stn.Rule) error
    44  	// DeleteSTNRule calls StnAddDelRule bin API with IsAdd=0
    45  	DeleteSTNRule(stnRule *stn.Rule) error
    46  }
    47  
    48  // StnVppRead provides read methods for STN rules
    49  type StnVppRead interface {
    50  	// DumpSTNRules returns a list of all STN rules configured on the VPP
    51  	DumpSTNRules() ([]*StnDetails, error)
    52  }
    53  
    54  var handler = vpp.RegisterHandler(vpp.HandlerDesc{
    55  	Name:       "stn",
    56  	HandlerAPI: (*StnVppAPI)(nil),
    57  })
    58  
    59  type NewHandlerFunc func(ch govppapi.Channel, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) StnVppAPI
    60  
    61  func AddStnHandlerVersion(version vpp.Version, msgs []govppapi.Message, h NewHandlerFunc) {
    62  	handler.AddVersion(vpp.HandlerVersion{
    63  		Version: version,
    64  		Check: func(c vpp.Client) error {
    65  			ch, err := c.NewAPIChannel()
    66  			if err != nil {
    67  				return err
    68  			}
    69  			return ch.CheckCompatiblity(msgs...)
    70  		},
    71  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
    72  			ch, err := c.NewAPIChannel()
    73  			if err != nil {
    74  				return err
    75  			}
    76  			return h(ch, a[0].(ifaceidx.IfaceMetadataIndex), a[1].(logging.Logger))
    77  		},
    78  	})
    79  }
    80  
    81  func CompatibleStnVppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) StnVppAPI {
    82  	if v := handler.FindCompatibleVersion(c); v != nil {
    83  		return v.NewHandler(c, ifIdx, log).(StnVppAPI)
    84  	}
    85  	return nil
    86  }