go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/ifplugin/descriptor/interface_dummy.go (about)

     1  // Copyright (c) 2020 Pantheon.tech
     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 descriptor
    16  
    17  import (
    18  	"github.com/pkg/errors"
    19  	"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/linuxcalls"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/ifaceidx"
    22  	nslinuxcalls "go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin/linuxcalls"
    23  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces"
    24  )
    25  
    26  // createDummyIf creates dummy interface.
    27  func (d *InterfaceDescriptor) createDummyIf(
    28  	nsCtx nslinuxcalls.NamespaceMgmtCtx, linuxIf *interfaces.Interface,
    29  ) (md *ifaceidx.LinuxIfMetadata, err error) {
    30  	hostName := getHostIfName(linuxIf)
    31  	agentPrefix := d.serviceLabel.GetAgentPrefix()
    32  
    33  	// move to the namespace with the interface
    34  	revert, err := d.nsPlugin.SwitchToNamespace(nsCtx, linuxIf.Namespace)
    35  	if err != nil {
    36  		d.log.Error("switch to namespace failed:", err)
    37  		return nil, err
    38  	}
    39  	defer revert()
    40  
    41  	// create a new Dummy interface
    42  	err = d.ifHandler.AddDummyInterface(hostName)
    43  	if err != nil {
    44  		return nil, errors.WithMessagef(err,
    45  			"failed to create dummy interface %s", hostName)
    46  	}
    47  
    48  	// add alias
    49  	err = d.ifHandler.SetInterfaceAlias(hostName, agentPrefix+linuxcalls.GetDummyIfAlias(linuxIf))
    50  	if err != nil {
    51  		return nil, errors.WithMessagef(err,
    52  			"error setting alias for Dummy interface %s", hostName)
    53  	}
    54  
    55  	// build metadata
    56  	link, err := d.ifHandler.GetLinkByName(hostName)
    57  	if err != nil {
    58  		return nil, errors.WithMessagef(err, "error getting link %s", hostName)
    59  	}
    60  
    61  	return &ifaceidx.LinuxIfMetadata{
    62  		Namespace:    linuxIf.Namespace,
    63  		LinuxIfIndex: link.Attrs().Index,
    64  		HostIfName:   hostName,
    65  	}, nil
    66  }
    67  
    68  // deleteDummyIf removes dummy interface.
    69  func (d *InterfaceDescriptor) deleteDummyIf(linuxIf *interfaces.Interface) error {
    70  	hostName := getHostIfName(linuxIf)
    71  	err := d.ifHandler.DeleteInterface(hostName)
    72  	if err != nil {
    73  		d.log.Error(err)
    74  		return err
    75  	}
    76  	return nil
    77  }