go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/puntplugin/vppcalls/punt_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  	"errors"
    19  
    20  	govppapi "go.fd.io/govpp/api"
    21  	"go.ligato.io/cn-infra/v2/logging"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	punt "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/punt"
    26  )
    27  
    28  var (
    29  	ErrUnsupported = errors.New("unsupported")
    30  )
    31  
    32  // PuntDetails includes punt model and socket path from VPP.
    33  type PuntDetails struct {
    34  	PuntData   *punt.ToHost
    35  	SocketPath string
    36  }
    37  
    38  // ReasonDetails includes reason model and its matching ID from VPP.
    39  type ReasonDetails struct {
    40  	Reason *punt.Reason
    41  	ID     uint32
    42  }
    43  
    44  // ExceptionDetails include punt model and socket path from VPP.
    45  type ExceptionDetails struct {
    46  	Exception  *punt.Exception
    47  	SocketPath string
    48  }
    49  
    50  // PuntVppAPI provides methods for managing VPP punt configuration.
    51  type PuntVppAPI interface {
    52  	PuntVPPRead
    53  
    54  	// AddPunt configures new punt to the host from the VPP
    55  	AddPunt(punt *punt.ToHost) error
    56  	// DeletePunt removes or unregisters punt entry
    57  	DeletePunt(punt *punt.ToHost) error
    58  	// RegisterPuntSocket registers new punt to unix domain socket entry
    59  	RegisterPuntSocket(puntCfg *punt.ToHost) (string, error)
    60  	// DeregisterPuntSocket removes existing punt to socket registration
    61  	DeregisterPuntSocket(puntCfg *punt.ToHost) error
    62  	// AddPuntRedirect adds new punt IP redirect entry
    63  	AddPuntRedirect(punt *punt.IPRedirect) error
    64  	// DeletePuntRedirect removes existing redirect entry
    65  	DeletePuntRedirect(punt *punt.IPRedirect) error
    66  	// AddPuntException registers new punt exception
    67  	AddPuntException(punt *punt.Exception) (string, error)
    68  	// DeletePuntException deregisters punt exception entry
    69  	DeletePuntException(punt *punt.Exception) error
    70  }
    71  
    72  // PuntVPPRead provides read methods for punt
    73  type PuntVPPRead interface {
    74  	// DumpRegisteredPuntSockets returns all punt socket registrations known to the VPP agent
    75  	DumpRegisteredPuntSockets() ([]*PuntDetails, error)
    76  	// DumpExceptions dumps punt exceptions
    77  	DumpExceptions() ([]*ExceptionDetails, error)
    78  	// DumpPuntReasons returns all known punt reasons from VPP
    79  	DumpPuntReasons() ([]*ReasonDetails, error)
    80  	// DumpPuntRedirect dump IP redirect punts
    81  	DumpPuntRedirect() ([]*punt.IPRedirect, error)
    82  }
    83  
    84  var Handler = vpp.RegisterHandler(vpp.HandlerDesc{
    85  	Name:       "punt",
    86  	HandlerAPI: (*PuntVppAPI)(nil),
    87  })
    88  
    89  type NewHandlerFunc func(ch govppapi.Channel, idx ifaceidx.IfaceMetadataIndex, log logging.Logger) PuntVppAPI
    90  
    91  func AddHandlerVersion(version vpp.Version, msgs []govppapi.Message, h NewHandlerFunc) {
    92  	Handler.AddVersion(vpp.HandlerVersion{
    93  		Version: version,
    94  		Check: func(c vpp.Client) error {
    95  			ch, err := c.NewAPIChannel()
    96  			if err != nil {
    97  				return err
    98  			}
    99  			return ch.CheckCompatiblity(msgs...)
   100  		},
   101  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
   102  			ch, err := c.NewAPIChannel()
   103  			if err != nil {
   104  				return err
   105  			}
   106  			return h(ch, a[0].(ifaceidx.IfaceMetadataIndex), a[1].(logging.Logger))
   107  		},
   108  	})
   109  }
   110  
   111  func CompatiblePuntVppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) PuntVppAPI {
   112  	if v := Handler.FindCompatibleVersion(c); v != nil {
   113  		return v.NewHandler(c, ifIdx, log).(PuntVppAPI)
   114  	}
   115  	return nil
   116  }