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

     1  //  Copyright (c) 2020 Doc.ai 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  	wg "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/wireguard"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  )
    26  
    27  type WgVppAPI interface {
    28  	WgVppRead
    29  
    30  	// Set peer via binary API
    31  	AddPeer(peer *wg.Peer) (uint32, error)
    32  	// Remove peer via binary API
    33  	RemovePeer(peer_idx uint32) error
    34  }
    35  
    36  // WgVPPRead provides read methods for wireguard
    37  type WgVppRead interface {
    38  	// DumpWgPeer returns a peers state
    39  	DumpWgPeers() (peerList []*wg.Peer, err error)
    40  }
    41  
    42  var Handler = vpp.RegisterHandler(vpp.HandlerDesc{
    43  	Name:       "wireguard",
    44  	HandlerAPI: (*WgVppAPI)(nil),
    45  })
    46  
    47  type NewHandlerFunc func(ch govppapi.Channel, ifDdx ifaceidx.IfaceMetadataIndex, log logging.Logger) WgVppAPI
    48  
    49  func AddHandlerVersion(version vpp.Version, msgs []govppapi.Message, h NewHandlerFunc) {
    50  	Handler.AddVersion(vpp.HandlerVersion{
    51  		Version: version,
    52  		Check: func(c vpp.Client) error {
    53  			ch, err := c.NewAPIChannel()
    54  			if err != nil {
    55  				return err
    56  			}
    57  			return ch.CheckCompatiblity(msgs...)
    58  		},
    59  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
    60  			ch, err := c.NewAPIChannel()
    61  			if err != nil {
    62  				return err
    63  			}
    64  			return h(ch, a[0].(ifaceidx.IfaceMetadataIndex), a[1].(logging.Logger))
    65  		},
    66  	})
    67  }
    68  
    69  func CompatibleWgVppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, log logging.Logger) WgVppAPI {
    70  	if v := Handler.FindCompatibleVersion(c); v != nil {
    71  		return v.NewHandler(c, ifIdx, log).(WgVppAPI)
    72  	}
    73  	return nil
    74  }