Add skeleton of Linux CP Netlink Listener

Register lcp_nl_init() which adds interface pair add/del callbacks.

lcb_nl_pair_add_cb: Initiate netlink listener for first interface in its
  netns. If subsequent adds are in other netns, issue a warning. Keep
  refcount.
lcb_nl_pair_del_cb: Remove listener when the last interface pair is
  removed.

Socket is opened, file is added to VPP's epoll, with lcp_nl_read_cb()
and lcp_nl_error_cb() callbacks installed.
- lcp_nl_read_cb() calls lcp_nl_callback() which pushes netlink messages
  onto a queue and issues NL_EVENT_READ event, any socket read error
  issues NL_EVENT_READ_ERR event.
- lcp_nl_error_cb() simply issues NL_EVENT_READ_ERR event.

Then, initialize a process node called lcp_nl_process(), which handles:
- NL_EVENT_READ and call lcp_nl_process_msgs()
  - if messages are left in the queue, reschedule consumption after M
    msecs. This allows new netlink messages to continuously be read from
    the kernel, even if we have lots of messages to consume.
- NL_EVENT_READ_ERR and close/reopens the netlink socket.

lcp_nl_process_msgs() processes up to N messages and/or for up to M
msecs, whichever comes first. For each, calling lcp_nl_dispatch().

lcp_nl_dispatch() ultimately just throws the message away after
logging it with format_nl_object()
This commit is contained in:
Pim van Pelt
2021-08-23 13:48:38 +02:00
parent f2ab33341f
commit c4e3043ea1
3 changed files with 682 additions and 0 deletions

90
lcpng_netlink.h Normal file
View File

@ -0,0 +1,90 @@
/*
* Copyright (c) 2021 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vlib/vlib.h>
#include <plugins/lcpng/lcpng.h>
#include <netlink/msg.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/route/link.h>
#include <netlink/route/route.h>
#include <netlink/route/neighbour.h>
#include <netlink/route/addr.h>
#include <netlink/route/link/vlan.h>
typedef enum nl_event_type_t_
{
NL_EVENT_READ,
NL_EVENT_READ_ERR,
} nl_event_type_t;
#define NL_RX_BUF_SIZE_DEF (1 << 27) /* 128 MB */
#define NL_TX_BUF_SIZE_DEF (1 << 18) /* 256 kB */
#define NL_BATCH_SIZE_DEF (1 << 11) /* 2048 */
#define NL_BATCH_DELAY_MS_DEF 50 /* 50 ms, max 20 batch/s */
#define NL_DBG(...) vlib_log_debug (lcp_nl_main.nl_logger, __VA_ARGS__);
#define NL_INFO(...) vlib_log_info (lcp_nl_main.nl_logger, __VA_ARGS__);
#define NL_NOTICE(...) vlib_log_notice (lcp_nl_main.nl_logger, __VA_ARGS__);
#define NL_WARN(...) vlib_log_warn (lcp_nl_main.nl_logger, __VA_ARGS__);
#define NL_ERROR(...) vlib_log_err (lcp_nl_main.nl_logger, __VA_ARGS__);
/* struct type to hold context on the netlink message being processed.
*/
typedef struct nl_msg_info
{
struct nl_msg *msg;
f64 ts;
} nl_msg_info_t;
typedef struct lcp_nl_netlink_namespace
{
struct nl_sock *sk_route;
nl_msg_info_t *nl_msg_queue;
uword clib_file_index; // clib file that holds the netlink socket for this
// namespace
u32 clib_file_lcp_refcnt; // number of interfaces watched in the this netlink
// namespace
u8 netns_name[LCP_NS_LEN]; // namespace name (can be empty, for 'self')
} lcp_nl_netlink_namespace_t;
typedef struct lcp_nl_main
{
vlib_log_class_t nl_logger;
/* TODO(pim): nl_ns should become a list, one for each unique namespace we
* created LCP pairs in.
*/
lcp_nl_netlink_namespace_t nl_ns;
u32 rx_buf_size;
u32 tx_buf_size;
u32 batch_size;
u32 batch_delay_ms;
} lcp_nl_main_t;
static void lcp_nl_open_socket (u8 *ns);
static void lcp_nl_close_socket (void);
u8 *format_nl_object (u8 *s, va_list *args);
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/