go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2101/span_vppcalls.go (about)

     1  package vpp2101
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/interface_types"
     7  	vpp_span "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/span"
     8  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
     9  )
    10  
    11  // SetSpan enables or disables SPAN on interface
    12  func (h *InterfaceVppHandler) setSpan(ifIdxFrom, ifIdxTo uint32, state uint8, isL2 bool) error {
    13  	req := &vpp_span.SwInterfaceSpanEnableDisable{
    14  		SwIfIndexFrom: interface_types.InterfaceIndex(ifIdxFrom),
    15  		SwIfIndexTo:   interface_types.InterfaceIndex(ifIdxTo),
    16  		State:         vpp_span.SpanState(state),
    17  		IsL2:          isL2,
    18  	}
    19  	reply := &vpp_span.SwInterfaceSpanEnableDisableReply{}
    20  
    21  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    22  		return err
    23  	}
    24  
    25  	return nil
    26  }
    27  
    28  // AddSpan enables SPAN on interface
    29  func (h *InterfaceVppHandler) AddSpan(ifIdxFrom, ifIdxTo uint32, direction uint8, isL2 bool) error {
    30  	return h.setSpan(ifIdxFrom, ifIdxTo, direction, isL2)
    31  }
    32  
    33  // DelSpan disables SPAN on interface
    34  func (h *InterfaceVppHandler) DelSpan(ifIdxFrom, ifIdxTo uint32, isL2 bool) error {
    35  	return h.setSpan(ifIdxFrom, ifIdxTo, 0, isL2)
    36  }
    37  
    38  // DumpSpan dumps all SPAN table
    39  func (h *InterfaceVppHandler) DumpSpan() ([]*vppcalls.InterfaceSpanDetails, error) {
    40  	var spans []*vppcalls.InterfaceSpanDetails
    41  
    42  	isL2Spans, err := h.dumpSpan(&vpp_span.SwInterfaceSpanDump{IsL2: true})
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	spans = append(spans, isL2Spans...)
    47  
    48  	isNotL2Spans, err := h.dumpSpan(&vpp_span.SwInterfaceSpanDump{IsL2: false})
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	spans = append(spans, isNotL2Spans...)
    53  
    54  	return spans, nil
    55  }
    56  
    57  // dumpIsL2Span returns only SPANs with or without L2 set
    58  func (h *InterfaceVppHandler) dumpSpan(msg *vpp_span.SwInterfaceSpanDump) ([]*vppcalls.InterfaceSpanDetails, error) {
    59  	var spans []*vppcalls.InterfaceSpanDetails
    60  
    61  	reqCtx := h.callsChannel.SendMultiRequest(msg)
    62  	for {
    63  		spanDetails := &vpp_span.SwInterfaceSpanDetails{}
    64  		stop, err := reqCtx.ReceiveReply(spanDetails)
    65  		if stop {
    66  			break
    67  		}
    68  		if err != nil {
    69  			return nil, fmt.Errorf("failed to dump span: %v", err)
    70  		}
    71  
    72  		spanData := &vppcalls.InterfaceSpanDetails{
    73  			SwIfIndexFrom: uint32(spanDetails.SwIfIndexFrom),
    74  			SwIfIndexTo:   uint32(spanDetails.SwIfIndexTo),
    75  			Direction:     uint8(spanDetails.State),
    76  			IsL2:          spanDetails.IsL2,
    77  		}
    78  		spans = append(spans, spanData)
    79  	}
    80  	return spans, nil
    81  }