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

     1  //  Copyright (c) 2022 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 vpp2202
    16  
    17  import (
    18  	"net"
    19  
    20  	"github.com/pkg/errors"
    21  
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/stnplugin/vppcalls"
    24  
    25  	vpp_stn "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/stn"
    26  	stn "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/stn"
    27  )
    28  
    29  // DumpSTNRules implements STN handler, it returns all STN rules present on the VPP
    30  func (h *StnVppHandler) DumpSTNRules() ([]*vppcalls.StnDetails, error) {
    31  	var stnDetails []*vppcalls.StnDetails
    32  
    33  	req := &vpp_stn.StnRulesDump{}
    34  	reqCtx := h.callsChannel.SendMultiRequest(req)
    35  	for {
    36  		msg := &vpp_stn.StnRulesDetails{}
    37  		stop, err := reqCtx.ReceiveReply(msg)
    38  		if stop {
    39  			break
    40  		}
    41  		if err != nil {
    42  			return nil, errors.Errorf("error reading STN rules from the VPP: %v", err)
    43  		}
    44  		ifName, _, found := h.ifIndexes.LookupBySwIfIndex(uint32(msg.SwIfIndex))
    45  		if !found {
    46  			h.log.Warnf("STN dump: interface name not found for index %d", msg.SwIfIndex)
    47  		}
    48  
    49  		var stnIP net.IP
    50  		if msg.IPAddress.Af == ip_types.ADDRESS_IP4 {
    51  			stnAddr := msg.IPAddress.Un.GetIP4()
    52  			stnIP = net.IP(stnAddr[:])
    53  		} else {
    54  			stnAddr := msg.IPAddress.Un.GetIP6()
    55  			stnIP = net.IP(stnAddr[:])
    56  		}
    57  
    58  		stnRule := &stn.Rule{
    59  			IpAddress: stnIP.String(),
    60  			Interface: ifName,
    61  		}
    62  		stnMeta := &vppcalls.StnMeta{
    63  			IfIdx: uint32(msg.SwIfIndex),
    64  		}
    65  
    66  		stnDetails = append(stnDetails, &vppcalls.StnDetails{
    67  			Rule: stnRule,
    68  			Meta: stnMeta,
    69  		})
    70  	}
    71  
    72  	return stnDetails, nil
    73  }