Skip to content

Commit

Permalink
route: add nexthop type
Browse files Browse the repository at this point in the history
  • Loading branch information
zstas committed Oct 17, 2022
1 parent 1bb4162 commit 3d46c64
Show file tree
Hide file tree
Showing 17 changed files with 902 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ test-suite.log
/src/nl-neigh-delete
/src/nl-neigh-list
/src/nl-neightbl-list
/src/nl-nh-list
/src/nl-pktloc-lookup
/src/nl-qdisc-add
/src/nl-qdisc-delete
Expand Down
7 changes: 7 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ libnlinclude_netlink_route_HEADERS = \
include/netlink/route/neightbl.h \
include/netlink/route/netconf.h \
include/netlink/route/nexthop.h \
include/netlink/route/nh.h \
include/netlink/route/pktloc.h \
include/netlink/route/qdisc.h \
include/netlink/route/route.h \
Expand Down Expand Up @@ -252,6 +253,7 @@ noinst_HEADERS = \
include/linux-private/linux/netfilter/nfnetlink_log.h \
include/linux-private/linux/netfilter/nfnetlink_queue.h \
include/linux-private/linux/netlink.h \
include/linux-private/linux/nexthop.h \
include/linux-private/linux/pkt_cls.h \
include/linux-private/linux/pkt_sched.h \
include/linux-private/linux/rtnetlink.h \
Expand Down Expand Up @@ -434,6 +436,7 @@ lib_libnl_route_3_la_SOURCES = \
lib/route/neightbl.c \
lib/route/netconf.c \
lib/route/nexthop.c \
lib/route/nh.c \
lib/route/nexthop_encap.c \
lib/route/nh_encap_mpls.c \
lib/route/pktloc.c \
Expand Down Expand Up @@ -629,6 +632,7 @@ src_lib_libnl_cli_3_la_SOURCES = \
src/lib/exp.c \
src/lib/link.c \
src/lib/neigh.c \
src/lib/nh.c \
src/lib/qdisc.c \
src/lib/route.c \
src/lib/rule.c \
Expand Down Expand Up @@ -712,6 +716,7 @@ cli_programs = \
src/nl-neigh-delete \
src/nl-neigh-list \
src/nl-neightbl-list \
src/nl-nh-list \
src/nl-pktloc-lookup \
src/nl-qdisc-add \
src/nl-qdisc-delete \
Expand Down Expand Up @@ -811,6 +816,8 @@ src_nl_neigh_list_CPPFLAGS = $(src_cppflags)
src_nl_neigh_list_LDADD = $(src_ldadd)
src_nl_neightbl_list_CPPFLAGS = $(src_cppflags)
src_nl_neightbl_list_LDADD = $(src_ldadd)
src_nl_nh_list_CPPFLAGS = $(src_cppflags)
src_nl_nh_list_LDADD = $(src_ldadd)
src_nl_pktloc_lookup_CPPFLAGS = $(src_cppflags)
src_nl_pktloc_lookup_LDADD = $(src_ldadd)
src_nl_qdisc_add_CPPFLAGS = $(src_cppflags)
Expand Down
2 changes: 2 additions & 0 deletions include/linux-private/linux/neighbour.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum {
NDA_MASTER,
NDA_LINK_NETNSID,
NDA_SRC_VNI,
NDA_PROTOCOL, /* Originator of entry */
NDA_NH_ID,
__NDA_MAX
};

Expand Down
104 changes: 104 additions & 0 deletions include/linux-private/linux/nexthop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _LINUX_NEXTHOP_H
#define _LINUX_NEXTHOP_H

#include <linux/types.h>

struct nhmsg {
unsigned char nh_family;
unsigned char nh_scope; /* return only */
unsigned char nh_protocol; /* Routing protocol that installed nh */
unsigned char resvd;
unsigned int nh_flags; /* RTNH_F flags */
};

/* entry in a nexthop group */
struct nexthop_grp {
__u32 id; /* nexthop id - must exist */
__u8 weight; /* weight of this nexthop */
__u8 resvd1;
__u16 resvd2;
};

enum {
NEXTHOP_GRP_TYPE_MPATH, /* hash-threshold nexthop group
* default type if not specified
*/
NEXTHOP_GRP_TYPE_RES, /* resilient nexthop group */
__NEXTHOP_GRP_TYPE_MAX,
};

#define NEXTHOP_GRP_TYPE_MAX (__NEXTHOP_GRP_TYPE_MAX - 1)

enum {
NHA_UNSPEC,
NHA_ID, /* u32; id for nexthop. id == 0 means auto-assign */

NHA_GROUP, /* array of nexthop_grp */
NHA_GROUP_TYPE, /* u16 one of NEXTHOP_GRP_TYPE */
/* if NHA_GROUP attribute is added, no other attributes can be set */

NHA_BLACKHOLE, /* flag; nexthop used to blackhole packets */
/* if NHA_BLACKHOLE is added, OIF, GATEWAY, ENCAP can not be set */

NHA_OIF, /* u32; nexthop device */
NHA_GATEWAY, /* be32 (IPv4) or in6_addr (IPv6) gw address */
NHA_ENCAP_TYPE, /* u16; lwt encap type */
NHA_ENCAP, /* lwt encap data */

/* NHA_OIF can be appended to dump request to return only
* nexthops using given device
*/
NHA_GROUPS, /* flag; only return nexthop groups in dump */
NHA_MASTER, /* u32; only return nexthops with given master dev */

NHA_FDB, /* flag; nexthop belongs to a bridge fdb */
/* if NHA_FDB is added, OIF, BLACKHOLE, ENCAP cannot be set */

/* nested; resilient nexthop group attributes */
NHA_RES_GROUP,
/* nested; nexthop bucket attributes */
NHA_RES_BUCKET,

__NHA_MAX,
};

#define NHA_MAX (__NHA_MAX - 1)

enum {
NHA_RES_GROUP_UNSPEC,
/* Pad attribute for 64-bit alignment. */
NHA_RES_GROUP_PAD = NHA_RES_GROUP_UNSPEC,

/* u16; number of nexthop buckets in a resilient nexthop group */
NHA_RES_GROUP_BUCKETS,
/* clock_t as u32; nexthop bucket idle timer (per-group) */
NHA_RES_GROUP_IDLE_TIMER,
/* clock_t as u32; nexthop unbalanced timer */
NHA_RES_GROUP_UNBALANCED_TIMER,
/* clock_t as u64; nexthop unbalanced time */
NHA_RES_GROUP_UNBALANCED_TIME,

__NHA_RES_GROUP_MAX,
};

#define NHA_RES_GROUP_MAX (__NHA_RES_GROUP_MAX - 1)

enum {
NHA_RES_BUCKET_UNSPEC,
/* Pad attribute for 64-bit alignment. */
NHA_RES_BUCKET_PAD = NHA_RES_BUCKET_UNSPEC,

/* u16; nexthop bucket index */
NHA_RES_BUCKET_INDEX,
/* clock_t as u64; nexthop bucket idle time */
NHA_RES_BUCKET_IDLE_TIME,
/* u32; nexthop id assigned to the nexthop bucket */
NHA_RES_BUCKET_NH_ID,

__NHA_RES_BUCKET_MAX,
};

#define NHA_RES_BUCKET_MAX (__NHA_RES_BUCKET_MAX - 1)

#endif
11 changes: 10 additions & 1 deletion include/linux-private/linux/rtnetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ enum {
RTM_GETCHAIN,
#define RTM_GETCHAIN RTM_GETCHAIN

__RTM_MAX,
RTM_NEWNEXTHOP = 104,
#define RTM_NEWNEXTHOP RTM_NEWNEXTHOP
RTM_DELNEXTHOP,
#define RTM_DELNEXTHOP RTM_DELNEXTHOP
RTM_GETNEXTHOP,
#define RTM_GETNEXTHOP RTM_GETNEXTHOP

__RTM_MAX,
#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1)
};

Expand Down Expand Up @@ -702,6 +709,8 @@ enum rtnetlink_groups {
#define RTNLGRP_IPV4_MROUTE_R RTNLGRP_IPV4_MROUTE_R
RTNLGRP_IPV6_MROUTE_R,
#define RTNLGRP_IPV6_MROUTE_R RTNLGRP_IPV6_MROUTE_R
RTNLGRP_NEXTHOP,
#define RTNLGRP_NEXTHOP RTNLGRP_NEXTHOP
__RTNLGRP_MAX
};
#define RTNLGRP_MAX (__RTNLGRP_MAX - 1)
Expand Down
5 changes: 5 additions & 0 deletions include/netlink-private/nl-auto.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ void rtnl_route_nh_free(struct rtnl_nexthop *);
#define _nl_auto_rtnl_nexthop _nl_auto(_nl_auto_rtnl_nexthop_fcn)
_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_nexthop *, _nl_auto_rtnl_nexthop_fcn, rtnl_route_nh_free);

struct rtnl_nh;
void rtnl_nh_put(struct rtnl_nh *);
#define _nl_auto_rtnl_nh _nl_auto(_nl_auto_rtnl_nh_fcn)
_NL_AUTO_DEFINE_FCN_TYPED0(struct rtnl_nh *, _nl_auto_rtnl_nh_fcn, rtnl_nh_put);

struct nl_cache;
void nl_cache_put(struct nl_cache *);
#define _nl_auto_nl_cache _nl_auto(_nl_auto_nl_cache_fcn)
Expand Down
29 changes: 29 additions & 0 deletions include/netlink-private/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ struct rtnl_neigh
uint8_t n_type;
struct nl_addr *n_lladdr;
struct nl_addr *n_dst;
uint32_t n_nhid;
uint32_t n_probes;
struct rtnl_ncacheinfo n_cacheinfo;
uint32_t n_state_mask;
Expand Down Expand Up @@ -323,6 +324,34 @@ struct rtnl_nexthop
struct rtnl_nh_encap * rtnh_encap;
};

typedef struct nl_nh_group_info {
uint32_t nh_id;
uint8_t weight;
} nl_nh_group_info_t;

typedef struct nl_nh_group {
int ce_refcnt;
int size;
nl_nh_group_info_t * entries;
} nl_nh_group_t;

struct rtnl_nh
{
NLHDR_COMMON

uint8_t nh_family;
uint8_t nh_scope;
uint8_t nh_protocol;
uint32_t nh_flags;

uint32_t nh_id;
uint32_t nh_group_type;
nl_nh_group_t * nh_group;
uint32_t nh_oif;
struct nl_addr * nh_gateway;
uint32_t nh_master;
};

struct rtnl_route
{
NLHDR_COMMON
Expand Down
23 changes: 23 additions & 0 deletions include/netlink/cli/nh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* Copyright (c) 2022 Stanislav Zaikin <[email protected]>
*/

#ifndef __NETLINK_CLI_NH_H_
#define __NETLINK_CLI_NH_H_

#include <netlink/route/nh.h>
#include <netlink/cli/utils.h>

#ifdef __cplusplus
extern "C" {
#endif

extern struct rtnl_nh *nl_cli_nh_alloc(void);
extern struct nl_cache *nl_cli_nh_alloc_cache(struct nl_sock *);

#ifdef __cplusplus
}
#endif

#endif
3 changes: 3 additions & 0 deletions include/netlink/route/neighbour.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ extern int rtnl_neigh_get_vlan(struct rtnl_neigh *);
extern void rtnl_neigh_set_master(struct rtnl_neigh *, int);
extern int rtnl_neigh_get_master(struct rtnl_neigh *);

extern void rtnl_neigh_set_nhid(struct rtnl_neigh *, uint32_t);
extern uint32_t rtnl_neigh_get_nhid(struct rtnl_neigh *);

#ifdef __cplusplus
}
#endif
Expand Down
47 changes: 47 additions & 0 deletions include/netlink/route/nh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* Copyright (c) 2022 Stanislav Zaikin <[email protected] >
*/

#ifndef NETLINK_ROUTE_NH_H_
#define NETLINK_ROUTE_NH_H_

#include <netlink/netlink.h>
#include <netlink/addr.h>

#ifdef __cplusplus
extern "C" {
#endif

struct rtnl_nh;

extern int rtnl_nh_alloc_cache(struct nl_sock *sk, int family, struct nl_cache **result);
extern struct rtnl_nh * rtnl_nh_alloc(void);
extern struct rtnl_nh * rtnl_nh_clone(struct rtnl_nh *);
extern void rtnl_nh_put(struct rtnl_nh *);

extern struct rtnl_nh *rtnl_nh_get(struct nl_cache *cache, int nhid);

extern int rtnl_nh_compare(struct rtnl_nh *, struct rtnl_nh *,
uint32_t, int);

extern void rtnl_nh_dump(struct rtnl_nh *, struct nl_dump_params *);

extern int rtnl_nh_set_gateway(struct rtnl_nh *, struct nl_addr *);
extern struct nl_addr * rtnl_nh_get_gateway(struct rtnl_nh *);

extern int rtnl_nh_set_fdb(struct rtnl_nh *, int value);
extern int rtnl_nh_get_fdb(struct rtnl_nh *);

extern uint32_t rtnl_nh_get_group_entry(struct rtnl_nh *, int n);
extern int rtnl_nh_get_group_size(struct rtnl_nh *);

extern uint32_t rtnl_nh_get_id(struct rtnl_nh *);
extern int rtnl_nh_get_oif(struct rtnl_nh *);

#ifdef __cplusplus
}
#endif

#endif

Loading

0 comments on commit 3d46c64

Please sign in to comment.