go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/natplugin/vppcalls/nat_vppcalls.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  package vppcalls
    16  
    17  import (
    18  	govppapi "go.fd.io/govpp/api"
    19  	"go.ligato.io/cn-infra/v2/idxmap"
    20  	"go.ligato.io/cn-infra/v2/logging"
    21  
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    24  	nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
    25  )
    26  
    27  // NatVppAPI provides methods for managing VPP NAT configuration.
    28  type NatVppAPI interface {
    29  	NatVppRead
    30  
    31  	// Enable NAT44 plugin and apply the given set of options.
    32  	EnableNAT44Plugin(opts Nat44InitOpts) error
    33  	// DisableNAT44Plugin disables NAT44 plugin.
    34  	DisableNAT44Plugin() error
    35  	// SetNat44Forwarding configures NAT44 forwarding.
    36  	SetNat44Forwarding(enableFwd bool) error
    37  	// EnableNat44Interface enables NAT44 feature for provided interface
    38  	EnableNat44Interface(iface string, isInside, isOutput bool) error
    39  	// DisableNat44Interface disables NAT feature for provided interface
    40  	DisableNat44Interface(iface string, isInside, isOutput bool) error
    41  	// AddNat44AddressPool adds new IPV4 address pool into the NAT pools.
    42  	AddNat44AddressPool(vrf uint32, firstIP, lastIP string, twiceNat bool) error
    43  	// DelNat44AddressPool removes existing IPv4 address pool from the NAT pools.
    44  	DelNat44AddressPool(vrf uint32, firstIP, lastIP string, twiceNat bool) error
    45  	// SetVirtualReassemblyIPv4 configures NAT virtual reassembly for IPv4 packets.
    46  	SetVirtualReassemblyIPv4(vrCfg *nat.VirtualReassembly) error
    47  	// SetVirtualReassemblyIPv6 configures NAT virtual reassembly for IPv6 packets.
    48  	SetVirtualReassemblyIPv6(vrCfg *nat.VirtualReassembly) error
    49  	// AddNat44IdentityMapping adds new NAT44 identity mapping
    50  	AddNat44IdentityMapping(mapping *nat.DNat44_IdentityMapping, dnatLabel string) error
    51  	// DelNat44IdentityMapping removes NAT44 identity mapping
    52  	DelNat44IdentityMapping(mapping *nat.DNat44_IdentityMapping, dnatLabel string) error
    53  	// AddNat44StaticMapping creates new NAT44 static mapping entry.
    54  	AddNat44StaticMapping(mapping *nat.DNat44_StaticMapping, dnatLabel string) error
    55  	// DelNat44StaticMapping removes existing NAT44 static mapping entry.
    56  	DelNat44StaticMapping(mapping *nat.DNat44_StaticMapping, dnatLabel string) error
    57  }
    58  
    59  // NatVppRead provides read methods for VPP NAT configuration.
    60  type NatVppRead interface {
    61  	// WithLegacyStartupConf returns true if the loaded VPP NAT plugin is still using
    62  	// the legacy startup NAT configuration (this is the case for VPP <= 20.09).
    63  	WithLegacyStartupConf() bool
    64  	// DefaultNat44GlobalConfig returns default global configuration.
    65  	DefaultNat44GlobalConfig() *nat.Nat44Global
    66  	// Nat44GlobalConfigDump dumps global NAT44 config in NB format.
    67  	// If dumpDeprecated is true, dumps deprecated NAT44 global config as well.
    68  	Nat44GlobalConfigDump(dumpDeprecated bool) (*nat.Nat44Global, error)
    69  	// DNat44Dump dumps all configured DNAT-44 configurations ordered by label.
    70  	DNat44Dump() ([]*nat.DNat44, error)
    71  	// Nat44InterfacesDump dumps NAT44 config of all NAT44-enabled interfaces.
    72  	Nat44InterfacesDump() ([]*nat.Nat44Interface, error)
    73  	// Nat44AddressPoolsDump dumps all configured NAT44 address pools.
    74  	Nat44AddressPoolsDump() ([]*nat.Nat44AddressPool, error)
    75  }
    76  
    77  // Previously these options were configured for NAT44 plugin via the startup configuration file.
    78  // As of VPP 21.01 it is possible to configure/change them in run-time (by disabling and then
    79  // re-enabling the plugin with changed options).
    80  // These are just some of the supported options. For full list of what VPP allows to configure,
    81  // see nat44_plugin_enable_disable binary API.
    82  type Nat44InitOpts struct {
    83  	// Endpoint dependent mode uses 6-tuple: (source IP address, source port, target IP address,
    84  	// target port, protocol, FIB table index) as session hash table key, whereas
    85  	// in the endpoint independent mode only 4-tuple (source IP address, source port, protocol, FIB table index)
    86  	// is used.
    87  	EndpointDependent bool
    88  	// Track connection (e.g. TCP states, timeout).
    89  	// In the dynamic mode the connection tracking is essential and performed by default.
    90  	// With StaticMappingOnly=true it is disabled and has to be turned on explicitly if needed.
    91  	ConnectionTracking bool
    92  	// If enabled only static translations are performed (i.e. no dynamic session entries).
    93  	StaticMappingOnly bool
    94  	// Policy-based packet processing and address translation.
    95  	// Not supported in the endpoint-dependent mode.
    96  	OutToInDPO bool
    97  }
    98  
    99  var handler = vpp.RegisterHandler(vpp.HandlerDesc{
   100  	Name:       "nat",
   101  	HandlerAPI: (*NatVppAPI)(nil),
   102  })
   103  
   104  func AddNatHandlerVersion(version vpp.Version, msgs []govppapi.Message,
   105  	h func(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, dhcpIdx idxmap.NamedMapping, log logging.Logger) NatVppAPI,
   106  ) {
   107  	handler.AddVersion(vpp.HandlerVersion{
   108  		Version: version,
   109  		Check: func(c vpp.Client) error {
   110  			ch, err := c.NewAPIChannel()
   111  			if err != nil {
   112  				return err
   113  			}
   114  			return ch.CheckCompatiblity(msgs...)
   115  		},
   116  		NewHandler: func(c vpp.Client, a ...interface{}) vpp.HandlerAPI {
   117  			return h(c, a[0].(ifaceidx.IfaceMetadataIndex), a[1].(idxmap.NamedMapping), a[2].(logging.Logger))
   118  		},
   119  	})
   120  }
   121  
   122  func CompatibleNatVppHandler(c vpp.Client, ifIdx ifaceidx.IfaceMetadataIndex, dhcpIdx idxmap.NamedMapping, log logging.Logger) NatVppAPI {
   123  	if v := handler.FindCompatibleVersion(c); v != nil {
   124  		return v.NewHandler(c, ifIdx, dhcpIdx, log).(NatVppAPI)
   125  	}
   126  	return nil
   127  }