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