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

     1  //  Copyright (c) 2019 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 vpp2101
    16  
    17  import (
    18  	"errors"
    19  	"net"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ethernet_types"
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/interface_types"
    23  	vpp_l2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/l2"
    24  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    25  )
    26  
    27  // AddL2FIB creates L2 FIB table entry.
    28  func (h *FIBVppHandler) AddL2FIB(fib *l2.FIBEntry) error {
    29  	return h.l2fibAddDel(fib, true)
    30  }
    31  
    32  // DeleteL2FIB removes existing L2 FIB table entry.
    33  func (h *FIBVppHandler) DeleteL2FIB(fib *l2.FIBEntry) error {
    34  	return h.l2fibAddDel(fib, false)
    35  }
    36  
    37  func (h *FIBVppHandler) l2fibAddDel(fib *l2.FIBEntry, isAdd bool) (err error) {
    38  	// get bridge domain metadata
    39  	bdMeta, found := h.bdIndexes.LookupByName(fib.BridgeDomain)
    40  	if !found {
    41  		return errors.New("failed to get bridge domain metadata")
    42  	}
    43  
    44  	// get outgoing interface index
    45  	swIfIndex := ^uint32(0) // ~0 is used by DROP entries
    46  	if fib.Action == l2.FIBEntry_FORWARD {
    47  		ifaceMeta, found := h.ifIndexes.LookupByName(fib.OutgoingInterface)
    48  		if !found {
    49  			return errors.New("failed to get interface metadata")
    50  		}
    51  		swIfIndex = ifaceMeta.GetIndex()
    52  	}
    53  
    54  	// parse MAC address
    55  	var mac []byte
    56  	if fib.PhysAddress != "" {
    57  		mac, err = net.ParseMAC(fib.PhysAddress)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	var macAddr ethernet_types.MacAddress
    64  	copy(macAddr[:], mac)
    65  
    66  	// add L2 FIB
    67  	req := &vpp_l2.L2fibAddDel{
    68  		IsAdd:     isAdd,
    69  		Mac:       macAddr,
    70  		BdID:      bdMeta.GetIndex(),
    71  		SwIfIndex: interface_types.InterfaceIndex(swIfIndex),
    72  		BviMac:    fib.BridgedVirtualInterface,
    73  		StaticMac: fib.StaticConfig,
    74  		FilterMac: fib.Action == l2.FIBEntry_DROP,
    75  	}
    76  	reply := &vpp_l2.L2fibAddDelReply{}
    77  
    78  	if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
    79  		return err
    80  	}
    81  
    82  	return nil
    83  }