1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-13 10:02:38 +00:00

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
This commit is contained in:
Alexander V. Chernikov 2023-01-29 15:53:34 +00:00
parent 0600af1ff1
commit 3ebccb20d5
2 changed files with 15 additions and 7 deletions

View File

@ -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 */

View File

@ -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,