go.ligato.io/vpp-agent/v3@v3.5.0/plugins/linux/l3plugin/linuxcalls/netlink_api.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 linuxcalls
    16  
    17  import (
    18  	"github.com/vishvananda/netlink"
    19  	"go.ligato.io/cn-infra/v2/logging"
    20  
    21  	"go.ligato.io/vpp-agent/v3/plugins/linux/ifplugin/ifaceidx"
    22  	"go.ligato.io/vpp-agent/v3/plugins/linux/nsplugin"
    23  	"go.ligato.io/vpp-agent/v3/proto/ligato/linux"
    24  )
    25  
    26  // ArpDetails is an object combining linux ARP data based on proto
    27  // model with additional metadata
    28  type ArpDetails struct {
    29  	ARP  *linux.ARPEntry `json:"linux_arp"`
    30  	Meta *ArpMeta        `json:"linux_arp_meta"`
    31  }
    32  
    33  // ArpMeta represents linux ARP metadata
    34  type ArpMeta struct {
    35  	InterfaceIndex uint32 `json:"interface_index"`
    36  	IPFamily       uint32 `json:"ip_family"`
    37  	VNI            uint32 `json:"vni"`
    38  }
    39  
    40  // RouteDetails is an object combining linux route data based on proto
    41  // model with additional metadata
    42  type RouteDetails struct {
    43  	Route *linux.Route
    44  	Meta  *RouteMeta
    45  }
    46  
    47  // RouteMeta represents linux Route metadata
    48  type RouteMeta struct {
    49  	InterfaceIndex uint32        `json:"interface_index"`
    50  	NetlinkScope   netlink.Scope `json:"link_scope"`
    51  	Protocol       uint32        `json:"protocol"`
    52  	MTU            uint32        `json:"mtu"`
    53  	Table          uint32        `json:"table"`
    54  }
    55  
    56  // NetlinkAPI interface covers all methods inside linux calls package needed
    57  // to manage linux ARP entries and routes.
    58  type NetlinkAPI interface {
    59  	NetlinkAPIWrite
    60  	NetlinkAPIRead
    61  }
    62  
    63  // NetlinkAPIWrite interface covers write methods inside linux calls package
    64  // needed to manage linux ARP entries and routes.
    65  type NetlinkAPIWrite interface {
    66  	/* ARP */
    67  	// SetARPEntry adds/modifies existing linux ARP entry.
    68  	SetARPEntry(arpEntry *netlink.Neigh) error
    69  	// DelARPEntry removes linux ARP entry.
    70  	DelARPEntry(arpEntry *netlink.Neigh) error
    71  
    72  	/* Routes */
    73  	// AddRoute adds new linux static route.
    74  	AddRoute(route *netlink.Route) error
    75  	// ReplaceRoute changes existing linux static route.
    76  	ReplaceRoute(route *netlink.Route) error
    77  	// DelRoute removes linux static route.
    78  	DelRoute(route *netlink.Route) error
    79  }
    80  
    81  // NetlinkAPIRead interface covers read methods inside linux calls package
    82  // needed to manage linux ARP entries and routes.
    83  type NetlinkAPIRead interface {
    84  	// GetARPEntries reads all configured static ARP entries for given interface.
    85  	// <interfaceIdx> works as filter, if set to zero, all arp entries in the namespace
    86  	// are returned.
    87  	GetARPEntries(interfaceIdx int) ([]netlink.Neigh, error)
    88  
    89  	// DumpARPEntries reads all ARP entries and returns them as details
    90  	// with proto-modeled ARP data and additional metadata
    91  	DumpARPEntries() ([]*ArpDetails, error)
    92  
    93  	// GetRoutes reads all configured static routes inside the given table
    94  	// and with the given outgoing interface.
    95  	// <interfaceIdx> works as filter, if set to zero, all routes in the namespace
    96  	// are returned.
    97  	// Zero <table> represents the main routing table.
    98  	GetRoutes(interfaceIdx, table int) (v4Routes, v6Routes []netlink.Route, err error)
    99  
   100  	// DumpRoutes reads all route entries and returns them as details
   101  	// with proto-modeled route data and additional metadata
   102  	DumpRoutes() ([]*RouteDetails, error)
   103  }
   104  
   105  // NetLinkHandler is accessor for Netlink methods.
   106  type NetLinkHandler struct {
   107  	nsPlugin  nsplugin.API
   108  	ifIndexes ifaceidx.LinuxIfMetadataIndex
   109  
   110  	// parallelization of the Retrieve operation
   111  	goRoutineCount int
   112  
   113  	log logging.Logger
   114  }
   115  
   116  // NewNetLinkHandler creates new instance of Netlink handler.
   117  func NewNetLinkHandler(nsPlugin nsplugin.API, ifIndexes ifaceidx.LinuxIfMetadataIndex, goRoutineCount int,
   118  	log logging.Logger) *NetLinkHandler {
   119  	return &NetLinkHandler{
   120  		nsPlugin:       nsPlugin,
   121  		ifIndexes:      ifIndexes,
   122  		goRoutineCount: goRoutineCount,
   123  		log:            log,
   124  	}
   125  }