go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2106/bond_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  	vpp_bond "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/bond"
    19  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/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.BondCreate{
    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.BondCreateReply{}
    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 getBondMode(mode ifs.BondLink_Mode) vpp_bond.BondMode {
    59  	switch mode {
    60  	case ifs.BondLink_ROUND_ROBIN:
    61  		return vpp_bond.BOND_API_MODE_ROUND_ROBIN
    62  	case ifs.BondLink_ACTIVE_BACKUP:
    63  		return vpp_bond.BOND_API_MODE_ACTIVE_BACKUP
    64  	case ifs.BondLink_XOR:
    65  		return vpp_bond.BOND_API_MODE_XOR
    66  	case ifs.BondLink_BROADCAST:
    67  		return vpp_bond.BOND_API_MODE_BROADCAST
    68  	case ifs.BondLink_LACP:
    69  		return vpp_bond.BOND_API_MODE_LACP
    70  	default:
    71  		// UNKNOWN
    72  		return 0
    73  	}
    74  }
    75  
    76  func (h *InterfaceVppHandler) AttachInterfaceToBond(ifIdx, bondIfIdx uint32, isPassive, isLongTimeout bool) error {
    77  	req := &vpp_bond.BondEnslave{
    78  		SwIfIndex:     interface_types.InterfaceIndex(ifIdx),
    79  		BondSwIfIndex: interface_types.InterfaceIndex(bondIfIdx),
    80  		IsPassive:     isPassive,
    81  		IsLongTimeout: isLongTimeout,
    82  	}
    83  	reply := &vpp_bond.BondEnslaveReply{}
    84  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    85  		return err
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func (h *InterfaceVppHandler) DetachInterfaceFromBond(ifIdx uint32) error {
    92  	req := &vpp_bond.BondDetachSlave{
    93  		SwIfIndex: interface_types.InterfaceIndex(ifIdx),
    94  	}
    95  	reply := &vpp_bond.BondDetachSlaveReply{}
    96  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    97  		return err
    98  	}
    99  	return nil
   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  }