go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/vppcalls/vpp2106/interface_vppcalls.go (about)

     1  //  Copyright (c) 2021 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 vpp2106
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/pkg/errors"
    21  
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/interface_types"
    23  	vpp_l2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/l2"
    24  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    25  )
    26  
    27  // AddInterfaceToBridgeDomain puts interface into bridge domain.
    28  func (h *BridgeDomainVppHandler) AddInterfaceToBridgeDomain(bdIdx uint32, ifaceCfg *l2.BridgeDomain_Interface) error {
    29  	ifaceMeta, found := h.ifIndexes.LookupByName(ifaceCfg.Name)
    30  	if !found {
    31  		return errors.New("failed to get interface metadata")
    32  	}
    33  	if err := h.addDelInterfaceToBridgeDomain(bdIdx, ifaceCfg, ifaceMeta.GetIndex(), true); err != nil {
    34  		return err
    35  	}
    36  	return nil
    37  }
    38  
    39  // DeleteInterfaceFromBridgeDomain removes interface from bridge domain.
    40  func (h *BridgeDomainVppHandler) DeleteInterfaceFromBridgeDomain(bdIdx uint32, ifaceCfg *l2.BridgeDomain_Interface) error {
    41  	ifaceMeta, found := h.ifIndexes.LookupByName(ifaceCfg.Name)
    42  	if !found {
    43  		return errors.New("failed to get interface metadata")
    44  	}
    45  	if err := h.addDelInterfaceToBridgeDomain(bdIdx, ifaceCfg, ifaceMeta.GetIndex(), false); err != nil {
    46  		return err
    47  	}
    48  	return nil
    49  }
    50  
    51  func (h *BridgeDomainVppHandler) addDelInterfaceToBridgeDomain(bdIdx uint32, ifaceCfg *l2.BridgeDomain_Interface,
    52  	ifIdx uint32, add bool) error {
    53  	req := &vpp_l2.SwInterfaceSetL2Bridge{
    54  		BdID:        bdIdx,
    55  		RxSwIfIndex: interface_types.InterfaceIndex(ifIdx),
    56  		Shg:         uint8(ifaceCfg.SplitHorizonGroup),
    57  		Enable:      add,
    58  	}
    59  	// Set as BVI.
    60  	if ifaceCfg.BridgedVirtualInterface {
    61  		req.PortType = vpp_l2.L2_API_PORT_TYPE_BVI
    62  	}
    63  	reply := &vpp_l2.SwInterfaceSetL2BridgeReply{}
    64  
    65  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    66  		return fmt.Errorf("%s returned error: %v", reply.GetMessageName(), err)
    67  	}
    68  
    69  	return nil
    70  }