go.ligato.io/vpp-agent/v3@v3.5.0/plugins/configurator/plugin.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 configurator
    16  
    17  import (
    18  	"go.ligato.io/cn-infra/v2/infra"
    19  	"go.ligato.io/cn-infra/v2/rpc/grpc"
    20  	"go.ligato.io/cn-infra/v2/servicelabel"
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/govppmux"
    24  	iflinuxplugin "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin"
    25  	iflinuxcalls "go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/linuxcalls"
    26  	l3linuxcalls "go.ligato.io/vpp-agent/v3/plugins/linux/l3plugin/linuxcalls"
    27  	"go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
    28  	"go.ligato.io/vpp-agent/v3/plugins/netalloc"
    29  	"go.ligato.io/vpp-agent/v3/plugins/orchestrator"
    30  	abfvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/abfplugin/vppcalls"
    31  	"go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin"
    32  	aclvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/aclplugin/vppcalls"
    33  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
    34  	ifvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    35  	ipsecvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ipsecplugin/vppcalls"
    36  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin"
    37  	l2vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls"
    38  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin"
    39  	l3vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    40  	natvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
    41  	puntvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/puntplugin/vppcalls"
    42  	wireguardvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/wireguardplugin/vppcalls"
    43  	pb "go.ligato.io/vpp-agent/v3/proto/ligato/configurator"
    44  	"go.ligato.io/vpp-agent/v3/proto/ligato/linux"
    45  	"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
    46  )
    47  
    48  // Default Go routine count for linux configuration retrieval
    49  const defaultGoRoutineCount = 10
    50  
    51  // Plugin registers VPP GRPC services in *grpc.Server.
    52  type Plugin struct {
    53  	Deps
    54  
    55  	configurator configuratorServer
    56  }
    57  
    58  // Deps - dependencies of Plugin
    59  type Deps struct {
    60  	infra.PluginDeps
    61  	GRPCServer    grpc.Server
    62  	Dispatch      orchestrator.Dispatcher
    63  	VPP           govppmux.API
    64  	ServiceLabel  servicelabel.ReaderAPI
    65  	AddrAlloc     netalloc.AddressAllocator
    66  	VPPACLPlugin  aclplugin.API
    67  	VPPIfPlugin   ifplugin.API
    68  	VPPL2Plugin   *l2plugin.L2Plugin
    69  	VPPL3Plugin   l3plugin.API
    70  	LinuxIfPlugin iflinuxplugin.API
    71  	NsPlugin      nsplugin.API
    72  }
    73  
    74  // Init sets plugin child loggers
    75  func (p *Plugin) Init() error {
    76  	p.configurator.log = p.Log.NewLogger("configurator")
    77  	p.configurator.dumpService.log = p.Log.NewLogger("dump")
    78  	p.configurator.notifyService.log = p.Log.NewLogger("notify")
    79  	p.configurator.notifyService.init()
    80  	p.configurator.dispatch = p.Dispatch
    81  
    82  	if err := p.initHandlers(); err != nil {
    83  		return err
    84  	}
    85  
    86  	grpcServer := p.GRPCServer.GetServer()
    87  	if grpcServer != nil {
    88  		pb.RegisterConfiguratorServiceServer(grpcServer, &p.configurator)
    89  	}
    90  
    91  	if p.VPPIfPlugin != nil {
    92  		p.VPPIfPlugin.SetNotifyService(func(notification *vpp.Notification) {
    93  			p.sendNotification(notification)
    94  		})
    95  	}
    96  	if p.LinuxIfPlugin != nil {
    97  		p.LinuxIfPlugin.SetNotifyService(func(notification *linux.Notification) {
    98  			p.sendNotification(notification)
    99  		})
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func (p *Plugin) sendNotification(notification proto.Message) {
   106  	switch n := notification.(type) {
   107  	case *vpp.Notification:
   108  		p.configurator.notifyService.pushNotification(&pb.Notification{
   109  			Notification: &pb.Notification_VppNotification{
   110  				VppNotification: n,
   111  			},
   112  		})
   113  	case *linux.Notification:
   114  		p.configurator.notifyService.pushNotification(&pb.Notification{
   115  			Notification: &pb.Notification_LinuxNotification{
   116  				LinuxNotification: n,
   117  			},
   118  		})
   119  	default:
   120  		p.Log.Warnf("unknown notification type: %v", notification)
   121  	}
   122  }
   123  
   124  // Close does nothing.
   125  func (p *Plugin) Close() error {
   126  	return nil
   127  }
   128  
   129  // helper method initializes all VPP/Linux plugin handlers
   130  func (p *Plugin) initHandlers() (err error) {
   131  	// VPP Indexes
   132  	ifIndexes := p.VPPIfPlugin.GetInterfaceIndex()
   133  	dhcpIndexes := p.VPPIfPlugin.GetDHCPIndex()
   134  	bdIndexes := p.VPPL2Plugin.GetBDIndex()
   135  	aclIndexes := p.VPPACLPlugin.GetACLIndex() // TODO: make ACL optional
   136  	vrfIndexes := p.VPPL3Plugin.GetVRFIndex()
   137  
   138  	// Linux Indexes
   139  	linuxIfIndexes := p.LinuxIfPlugin.GetInterfaceIndex()
   140  
   141  	// VPP handlers
   142  	p.configurator.ifHandler = ifvppcalls.CompatibleInterfaceVppHandler(p.VPP, p.Log)
   143  	if p.configurator.ifHandler == nil {
   144  		p.Log.Info("VPP Interface handler is not available, it will be skipped")
   145  	}
   146  	p.configurator.l2Handler = l2vppcalls.CompatibleL2VppHandler(p.VPP, ifIndexes, bdIndexes, p.Log)
   147  	if p.configurator.l2Handler == nil {
   148  		p.Log.Info("VPP L2 handler is not available, it will be skipped")
   149  	}
   150  	p.configurator.l3Handler = l3vppcalls.CompatibleL3VppHandler(p.VPP, ifIndexes, vrfIndexes, p.AddrAlloc, p.Log)
   151  	if p.configurator.l3Handler == nil {
   152  		p.Log.Info("VPP L3 handler is not available, it will be skipped")
   153  	}
   154  	p.configurator.ipsecHandler = ipsecvppcalls.CompatibleIPSecVppHandler(p.VPP, ifIndexes, p.Log)
   155  	if p.configurator.ipsecHandler == nil {
   156  		p.Log.Info("VPP IPSec handler is not available, it will be skipped")
   157  	}
   158  	// plugins
   159  	p.configurator.abfHandler = abfvppcalls.CompatibleABFHandler(p.VPP, aclIndexes, ifIndexes, p.Log)
   160  	if p.configurator.abfHandler == nil {
   161  		p.Log.Info("VPP ABF handler is not available, it will be skipped")
   162  	}
   163  	p.configurator.aclHandler = aclvppcalls.CompatibleACLHandler(p.VPP, ifIndexes)
   164  	if p.configurator.aclHandler == nil {
   165  		p.Log.Info("VPP ACL handler is not available, it will be skipped")
   166  	}
   167  	p.configurator.natHandler = natvppcalls.CompatibleNatVppHandler(p.VPP, ifIndexes, dhcpIndexes, p.Log)
   168  	if p.configurator.natHandler == nil {
   169  		p.Log.Info("VPP NAT handler is not available, it will be skipped")
   170  	}
   171  	p.configurator.puntHandler = puntvppcalls.CompatiblePuntVppHandler(p.VPP, ifIndexes, p.Log)
   172  	if p.configurator.puntHandler == nil {
   173  		p.Log.Info("VPP Punt handler is not available, it will be skipped")
   174  	}
   175  	p.configurator.wireguardHandler = wireguardvppcalls.CompatibleWgVppHandler(p.VPP, ifIndexes, p.Log)
   176  	if p.configurator.wireguardHandler == nil {
   177  		p.Log.Info("VPP Wg handler is not available, it will be skipped")
   178  	}
   179  
   180  	// Linux handlers
   181  	p.configurator.linuxIfHandler = iflinuxcalls.NewNetLinkHandler(p.NsPlugin, linuxIfIndexes,
   182  		p.ServiceLabel.GetAgentPrefix(), defaultGoRoutineCount, p.Log)
   183  	p.configurator.linuxL3Handler = l3linuxcalls.NewNetLinkHandler(p.NsPlugin, linuxIfIndexes, defaultGoRoutineCount, p.Log)
   184  
   185  	return nil
   186  }