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

     1  // Copyright (c) 2018 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  //go:generate descriptor-adapter --descriptor-name ARP --value-type *linux_l3.ARPEntry --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/l3" --output-dir "descriptor"
    16  //go:generate descriptor-adapter --descriptor-name Route --value-type *linux_l3.Route --import "go.ligato.io/vpp-agent/v3/proto/ligato/linux/l3" --output-dir "descriptor"
    17  
    18  package l3plugin
    19  
    20  import (
    21  	"go.ligato.io/cn-infra/v2/infra"
    22  
    23  	kvs "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api"
    24  
    25  	"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin"
    26  	"go.ligato.io/vpp-agent/v3/plugins/linux/l3plugin/descriptor"
    27  	"go.ligato.io/vpp-agent/v3/plugins/linux/l3plugin/linuxcalls"
    28  	"go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
    29  	"go.ligato.io/vpp-agent/v3/plugins/netalloc"
    30  )
    31  
    32  const (
    33  	// by default, at most 10 go routines will split the configured namespaces
    34  	// to execute the Retrieve operation in parallel.
    35  	defaultGoRoutinesCnt = 10
    36  )
    37  
    38  // L3Plugin configures Linux routes and ARP entries using Netlink API.
    39  type L3Plugin struct {
    40  	Deps
    41  
    42  	// From configuration file
    43  	disabled bool
    44  
    45  	// system handlers
    46  	l3Handler linuxcalls.NetlinkAPI
    47  }
    48  
    49  // Deps lists dependencies of the interface p.
    50  type Deps struct {
    51  	infra.PluginDeps
    52  	KVScheduler kvs.KVScheduler
    53  	NsPlugin    nsplugin.API
    54  	IfPlugin    ifplugin.API
    55  	AddrAlloc   netalloc.AddressAllocator
    56  }
    57  
    58  // Config holds the l3plugin configuration.
    59  type Config struct {
    60  	Disabled      bool `json:"disabled"`
    61  	GoRoutinesCnt int  `json:"go-routines-count"`
    62  }
    63  
    64  // Init initializes and registers descriptors for Linux ARPs and Routes.
    65  func (p *L3Plugin) Init() error {
    66  	// parse configuration file
    67  	config, err := p.retrieveConfig()
    68  	if err != nil {
    69  		return err
    70  	}
    71  	p.Log.Debugf("Linux L3 plugin config: %+v", config)
    72  	if config.Disabled {
    73  		p.disabled = true
    74  		p.Log.Infof("Disabling Linux L3 plugin")
    75  		return nil
    76  	}
    77  
    78  	// init handlers
    79  	p.l3Handler = linuxcalls.NewNetLinkHandler(p.NsPlugin, p.IfPlugin.GetInterfaceIndex(), defaultGoRoutinesCnt, p.Log)
    80  
    81  	// init & register descriptors
    82  	arpDescriptor := descriptor.NewARPDescriptor(
    83  		p.KVScheduler, p.IfPlugin, p.NsPlugin, p.AddrAlloc, p.l3Handler, p.Log, config.GoRoutinesCnt)
    84  
    85  	routeDescriptor := descriptor.NewRouteDescriptor(
    86  		p.KVScheduler, p.IfPlugin, p.NsPlugin, p.AddrAlloc, p.l3Handler, p.Log, config.GoRoutinesCnt)
    87  
    88  	err = p.Deps.KVScheduler.RegisterKVDescriptor(arpDescriptor)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	err = p.Deps.KVScheduler.RegisterKVDescriptor(routeDescriptor)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  // Close does nothing here.
   101  func (p *L3Plugin) Close() error {
   102  	return nil
   103  }
   104  
   105  // retrieveConfig loads L3Plugin configuration file.
   106  func (p *L3Plugin) retrieveConfig() (*Config, error) {
   107  	config := &Config{
   108  		// default configuration
   109  		GoRoutinesCnt: defaultGoRoutinesCnt,
   110  	}
   111  	found, err := p.Cfg.LoadValue(config)
   112  	if !found {
   113  		p.Log.Debug("Linux L3Plugin config not found")
   114  		return config, nil
   115  	}
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	return config, err
   120  }