git: 3ebccb20d564 - main - netlink: allow path weight manipulations for single-path routes.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Sun, 29 Jan 2023 16:04:30 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=3ebccb20d56455f4bef1366f942680d1b60828f6

commit 3ebccb20d56455f4bef1366f942680d1b60828f6
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-01-29 15:53:34 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-01-29 16:04:10 +0000

    netlink: allow path weight manipulations for single-path routes.
    
    Add support for the scenario when user adds/deletes paths for a single
     prefix one-by-one, all with different weights.
    This change adds a new FreeBSD-specific RTA attribute, NL_RTA_WEIGHT.
    When dumping non-multipath routes, this attribute is added if the
     route weight is not RT_DEFAULT_WEIGHT.
    When adding a new route, this attribute is parsed as a relative path
     weight.
    
    MFC after:      2 weeks
---
 sys/netlink/route/route.h |  3 ++-
 sys/netlink/route/rt.c    | 19 +++++++++++++------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/sys/netlink/route/route.h b/sys/netlink/route/route.h
index 6e1ef6cbf0c6..b4b326b2b06b 100644
--- a/sys/netlink/route/route.h
+++ b/sys/netlink/route/route.h
@@ -157,8 +157,9 @@ enum rtattr_type_t {
 	NL_RTA_FLOW		= 11, /* not supported */
 	NL_RTA_CACHEINFO	= 12, /* not supported */
 	NL_RTA_SESSION		= 13, /* not supported / deprecated */
+	NL_RTA_WEIGHT		= 13, /* u32, FreeBSD specific, path weight */
 	NL_RTA_MP_ALGO		= 14, /* not supported / deprecated */
-	NL_RTA_RTFLAGS		= 14, /* u32, FreeBSD specific, */
+	NL_RTA_RTFLAGS		= 14, /* u32, FreeBSD specific, path flags (RTF_)*/
 	NL_RTA_TABLE		= 15, /* u32, fibnum */
 	NL_RTA_MARK		= 16, /* not supported */
 	NL_RTA_MFC_STATS	= 17, /* not supported */
diff --git a/sys/netlink/route/rt.c b/sys/netlink/route/rt.c
index 5e7f00f28b0b..59b34c53ad4b 100644
--- a/sys/netlink/route/rt.c
+++ b/sys/netlink/route/rt.c
@@ -208,14 +208,15 @@ dump_rc_nhg(struct nl_writer *nw, const struct nhgrp_object *nhg, struct rtmsg *
 #endif
 
 static void
-dump_rc_nhop(struct nl_writer *nw, const struct nhop_object *nh, struct rtmsg *rtm)
+dump_rc_nhop(struct nl_writer *nw, const struct route_nhop_data *rnd, struct rtmsg *rtm)
 {
 #ifdef ROUTE_MPATH
-	if (NH_IS_NHGRP(nh)) {
-		dump_rc_nhg(nw, (const struct nhgrp_object *)nh, rtm);
+	if (NH_IS_NHGRP(rnd->rnd_nhop)) {
+		dump_rc_nhg(nw, rnd->rnd_nhgrp, rtm);
 		return;
 	}
 #endif
+	const struct nhop_object *nh = rnd->rnd_nhop;
 	uint32_t rtflags = nhop_get_rtflags(nh);
 
 	/*
@@ -243,6 +244,9 @@ dump_rc_nhop(struct nl_writer *nw, const struct nhop_object *nh, struct rtmsg *r
 
 	/* In any case, fill outgoing interface */
 	nlattr_add_u32(nw, NL_RTA_OIF, nh->nh_ifp->if_index);
+
+	if (rnd->rnd_weight != RT_DEFAULT_WEIGHT)
+		nlattr_add_u32(nw, NL_RTA_WEIGHT, rnd->rnd_weight);
 }
 
 /*
@@ -309,7 +313,7 @@ dump_px(uint32_t fibnum, const struct nlmsghdr *hdr,
 	rtm = nlattr_restore_offset(nw, rtm_off, struct rtmsg);
 	if (plen > 0)
 		rtm->rtm_dst_len = plen;
-	dump_rc_nhop(nw, rnd->rnd_nhop, rtm);
+	dump_rc_nhop(nw, rnd, rtm);
 
 	if (nlmsg_end(nw))
 		return (0);
@@ -438,6 +442,7 @@ struct nl_parsed_route {
 	uint32_t		rta_table;
 	uint32_t		rta_rtflags;
 	uint32_t		rta_nh_id;
+	uint32_t		rta_weight;
 	uint32_t		rtax_mtu;
 	uint8_t			rtm_family;
 	uint8_t			rtm_dst_len;
@@ -457,6 +462,7 @@ static const struct nlattr_parser nla_p_rtmsg[] = {
 	{ .type = NL_RTA_GATEWAY, .off = _OUT(rta_gw), .cb = nlattr_get_ip },
 	{ .type = NL_RTA_METRICS, .arg = &metrics_parser, .cb = nlattr_get_nested },
 	{ .type = NL_RTA_MULTIPATH, .off = _OUT(rta_multipath), .cb = nlattr_get_multipath },
+	{ .type = NL_RTA_WEIGHT, .off = _OUT(rta_weight), .cb = nlattr_get_uint32 },
 	{ .type = NL_RTA_RTFLAGS, .off = _OUT(rta_rtflags), .cb = nlattr_get_uint32 },
 	{ .type = NL_RTA_TABLE, .off = _OUT(rta_table), .cb = nlattr_get_uint32 },
 	{ .type = NL_RTA_VIA, .off = _OUT(rta_gw), .cb = nlattr_get_ipvia },
@@ -849,8 +855,9 @@ rtnl_handle_newroute(struct nlmsghdr *hdr, struct nlpcb *nlp,
 		}
 	}
 
-	int weight = NH_IS_NHGRP(nh) ? 0 : RT_DEFAULT_WEIGHT;
-	struct route_nhop_data rnd = { .rnd_nhop = nh, .rnd_weight = weight };
+	if (!NH_IS_NHGRP(nh) && attrs.rta_weight == 0)
+		attrs.rta_weight = RT_DEFAULT_WEIGHT;
+	struct route_nhop_data rnd = { .rnd_nhop = nh, .rnd_weight = attrs.rta_weight };
 	int op_flags = get_op_flags(hdr->nlmsg_flags);
 
 	error = rib_add_route_px(attrs.rta_table, attrs.rta_dst, attrs.rtm_dst_len,