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

     1  //  Copyright (c) 2022 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 vpp2202
    16  
    17  import (
    18  	"encoding/base64"
    19  
    20  	vpp_wg "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/wireguard"
    21  	wg "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/wireguard"
    22  )
    23  
    24  // DumpWgPeers implements wg handler.
    25  func (h *WgVppHandler) DumpWgPeers() (peerList []*wg.Peer, err error) {
    26  	// index of ^uint32(0) dumps all peers
    27  	req := &vpp_wg.WireguardPeersDump{PeerIndex: ^uint32(0)}
    28  	requestCtx := h.callsChannel.SendMultiRequest(req)
    29  
    30  	var vppPeerList []*vpp_wg.WireguardPeersDetails
    31  	for {
    32  		vppPeerDetails := &vpp_wg.WireguardPeersDetails{}
    33  		stop, err := requestCtx.ReceiveReply(vppPeerDetails)
    34  		if stop {
    35  			break
    36  		}
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		vppPeerList = append(vppPeerList, vppPeerDetails)
    41  	}
    42  
    43  	for _, vppPeerDetails := range vppPeerList {
    44  		peerDetails := &wg.Peer{
    45  			Port:                uint32(vppPeerDetails.Peer.Port),
    46  			PersistentKeepalive: uint32(vppPeerDetails.Peer.PersistentKeepalive),
    47  			Flags:               uint32(vppPeerDetails.Peer.Flags),
    48  		}
    49  
    50  		peerDetails.PublicKey = base64.StdEncoding.EncodeToString(vppPeerDetails.Peer.PublicKey)
    51  
    52  		for _, prefix := range vppPeerDetails.Peer.AllowedIps {
    53  			peerDetails.AllowedIps = append(peerDetails.AllowedIps, prefix.String())
    54  		}
    55  
    56  		ifName, _, exists := h.ifIndexes.LookupBySwIfIndex(uint32(vppPeerDetails.Peer.SwIfIndex))
    57  		if !exists {
    58  			h.log.Warnf("Wireguard peers dump: interface name for index %d not found", vppPeerDetails.Peer.SwIfIndex)
    59  			continue
    60  		}
    61  
    62  		peerDetails.WgIfName = ifName
    63  
    64  		endpointAddr := vppPeerDetails.Peer.Endpoint.ToIP()
    65  		if !endpointAddr.IsUnspecified() {
    66  			peerDetails.Endpoint = endpointAddr.String()
    67  		}
    68  
    69  		peerList = append(peerList, peerDetails)
    70  	}
    71  
    72  	return
    73  }