go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2210/bond_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  	vpp_bond "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/bond"
    19  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/interface_types"
    20  	ifs "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    21  )
    22  
    23  func (h *InterfaceVppHandler) AddBondInterface(ifName string, mac string, bondLink *ifs.BondLink) (uint32, error) {
    24  	req := &vpp_bond.BondCreate2{
    25  		ID:   bondLink.Id,
    26  		Mode: getBondMode(bondLink.Mode),
    27  		Lb:   getLoadBalance(bondLink.Lb),
    28  	}
    29  	if mac != "" {
    30  		parsedMac, err := ParseMAC(mac)
    31  		if err != nil {
    32  			return 0, err
    33  		}
    34  		req.UseCustomMac = true
    35  		req.MacAddress = parsedMac
    36  	}
    37  
    38  	reply := &vpp_bond.BondCreate2Reply{}
    39  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    40  		return 0, err
    41  	}
    42  
    43  	return uint32(reply.SwIfIndex), h.SetInterfaceTag(ifName, uint32(reply.SwIfIndex))
    44  }
    45  
    46  func (h *InterfaceVppHandler) DeleteBondInterface(ifName string, ifIdx uint32) error {
    47  	req := &vpp_bond.BondDelete{
    48  		SwIfIndex: interface_types.InterfaceIndex(ifIdx),
    49  	}
    50  	reply := &vpp_bond.BondDeleteReply{}
    51  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    52  		return err
    53  	}
    54  
    55  	return h.RemoveInterfaceTag(ifName, ifIdx)
    56  }
    57  
    58  func (h *InterfaceVppHandler) AttachInterfaceToBond(ifIdx, bondIfIdx uint32, isPassive, isLongTimeout bool) error {
    59  	req := &vpp_bond.BondAddMember{
    60  		SwIfIndex:     interface_types.InterfaceIndex(ifIdx),
    61  		BondSwIfIndex: interface_types.InterfaceIndex(bondIfIdx),
    62  		IsPassive:     isPassive,
    63  		IsLongTimeout: isLongTimeout,
    64  	}
    65  	reply := &vpp_bond.BondAddMemberReply{}
    66  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func (h *InterfaceVppHandler) DetachInterfaceFromBond(ifIdx uint32) error {
    74  	req := &vpp_bond.BondDetachMember{
    75  		SwIfIndex: interface_types.InterfaceIndex(ifIdx),
    76  	}
    77  	reply := &vpp_bond.BondDetachMemberReply{}
    78  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  func getBondMode(mode ifs.BondLink_Mode) vpp_bond.BondMode {
    85  	switch mode {
    86  	case ifs.BondLink_ROUND_ROBIN:
    87  		return vpp_bond.BOND_API_MODE_ROUND_ROBIN
    88  	case ifs.BondLink_ACTIVE_BACKUP:
    89  		return vpp_bond.BOND_API_MODE_ACTIVE_BACKUP
    90  	case ifs.BondLink_XOR:
    91  		return vpp_bond.BOND_API_MODE_XOR
    92  	case ifs.BondLink_BROADCAST:
    93  		return vpp_bond.BOND_API_MODE_BROADCAST
    94  	case ifs.BondLink_LACP:
    95  		return vpp_bond.BOND_API_MODE_LACP
    96  	default:
    97  		// UNKNOWN
    98  		return 0
    99  	}
   100  }
   101  
   102  func getLoadBalance(lb ifs.BondLink_LoadBalance) vpp_bond.BondLbAlgo {
   103  	switch lb {
   104  	case ifs.BondLink_L34:
   105  		return vpp_bond.BOND_API_LB_ALGO_L34
   106  	case ifs.BondLink_L23:
   107  		return vpp_bond.BOND_API_LB_ALGO_L23
   108  	case ifs.BondLink_RR:
   109  		return vpp_bond.BOND_API_LB_ALGO_RR
   110  	case ifs.BondLink_BC:
   111  		return vpp_bond.BOND_API_LB_ALGO_BC
   112  	case ifs.BondLink_AB:
   113  		return vpp_bond.BOND_API_LB_ALGO_AB
   114  	default:
   115  		// L2
   116  		return 0
   117  	}
   118  }