From 63eaefa86c29fd8a3ceb63ec597f3ec1c4d200f9 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Tue, 21 Jun 2022 16:13:08 -0300 Subject: [PATCH] zebra: rtnetlink: flow attr per gateway attr in multipath updates Signed-off-by: Ricardo --- zebra/debug_nl.c | 5 ++-- zebra/rt_netlink.c | 66 ++++++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/zebra/debug_nl.c b/zebra/debug_nl.c index 89ef7a2076..f8b866cd25 100644 --- a/zebra/debug_nl.c +++ b/zebra/debug_nl.c @@ -1083,8 +1083,9 @@ next_rta: plen = RTA_PAYLOAD(rta); zlog_debug(" rta [len=%d (payload=%zu) type=(%d) %s]", rta->rta_len, - plen, rta->rta_type, rtm_rta2str(rta->rta_type)); - switch (rta->rta_type) { + plen, rta->rta_type & NLA_TYPE_MASK, + rtm_rta2str(rta->rta_type & NLA_TYPE_MASK)); + switch (rta->rta_type & NLA_TYPE_MASK) { case RTA_IIF: case RTA_OIF: case RTA_PRIORITY: diff --git a/zebra/rt_netlink.c b/zebra/rt_netlink.c index 93b2d94671..22f2d742e2 100644 --- a/zebra/rt_netlink.c +++ b/zebra/rt_netlink.c @@ -1638,6 +1638,27 @@ static bool _netlink_route_build_singlepath(const struct prefix *p, return true; } +/* This function appends tag value as rtnl flow attribute + * to the given netlink msg only if value is less than 256. + * Used only if SUPPORT_REALMS enabled. + * + * @param nlmsg: nlmsghdr structure to fill in. + * @param maxlen: The size allocated for the message. + * @param tag: The route tag. + * + * The function returns true if the flow attribute could + * be added to the message, otherwise false is returned. + */ +static inline bool _netlink_set_tag(struct nlmsghdr *n, unsigned int maxlen, + route_tag_t tag) +{ + if (tag > 0 && tag <= 255) { + if (!nl_attr_put32(n, maxlen, RTA_FLOW, tag)) + return false; + } + return true; +} + /* This function takes a nexthop as argument and * appends to the given netlink msg. If the nexthop * defines a preferred source, the src parameter @@ -1656,12 +1677,10 @@ static bool _netlink_route_build_singlepath(const struct prefix *p, * The function returns true if the nexthop could be added * to the message, otherwise false is returned. */ -static bool _netlink_route_build_multipath(const struct prefix *p, - const char *routedesc, int bytelen, - const struct nexthop *nexthop, - struct nlmsghdr *nlmsg, - size_t req_size, struct rtmsg *rtmsg, - const union g_addr **src) +static bool _netlink_route_build_multipath( + const struct prefix *p, const char *routedesc, int bytelen, + const struct nexthop *nexthop, struct nlmsghdr *nlmsg, size_t req_size, + struct rtmsg *rtmsg, const union g_addr **src, route_tag_t tag) { char label_buf[256]; struct vrf *vrf; @@ -1767,6 +1786,9 @@ static bool _netlink_route_build_multipath(const struct prefix *p, if (nexthop->weight) rtnh->rtnh_hops = nexthop->weight - 1; + if (!_netlink_set_tag(nlmsg, req_size, tag)) + return false; + nl_attr_rtnh_end(nlmsg, rtnh); return true; } @@ -1801,7 +1823,7 @@ _netlink_mpls_build_multipath(const struct prefix *p, const char *routedesc, bytelen = (family == AF_INET ? 4 : 16); return _netlink_route_build_multipath(p, routedesc, bytelen, nhlfe->nexthop, nlmsg, req_size, - rtmsg, src); + rtmsg, src, 0); } static void _netlink_mpls_debug(int cmd, uint32_t label, const char *routedesc) @@ -1925,6 +1947,7 @@ ssize_t netlink_route_multipath_msg_encode(int cmd, const struct prefix *p, *src_p; uint32_t table_id; struct nlsock *nl; + route_tag_t tag = 0; struct { struct nlmsghdr n; @@ -1996,20 +2019,12 @@ ssize_t netlink_route_multipath_msg_encode(int cmd, return 0; #if defined(SUPPORT_REALMS) - { - route_tag_t tag; - - if (cmd == RTM_DELROUTE) - tag = dplane_ctx_get_old_tag(ctx); - else - tag = dplane_ctx_get_tag(ctx); - - if (tag > 0 && tag <= 255) { - if (!nl_attr_put32(&req->n, datalen, RTA_FLOW, tag)) - return 0; - } - } + if (cmd == RTM_DELROUTE) + tag = dplane_ctx_get_old_tag(ctx); + else + tag = dplane_ctx_get_tag(ctx); #endif + /* Table corresponding to this route. */ table_id = dplane_ctx_get_table(ctx); if (table_id < 256) @@ -2032,8 +2047,11 @@ ssize_t netlink_route_multipath_msg_encode(int cmd, * prefix information to tell the kernel to schwack * it. */ - if (cmd == RTM_DELROUTE) + if (cmd == RTM_DELROUTE) { + if (!_netlink_set_tag(&req->n, datalen, tag)) + return 0; return NLMSG_ALIGN(req->n.nlmsg_len); + } if (dplane_ctx_get_mtu(ctx) || dplane_ctx_get_nh_mtu(ctx)) { struct rtattr *nest; @@ -2148,6 +2166,9 @@ ssize_t netlink_route_multipath_msg_encode(int cmd, ? "recursive, single-path" : "single-path"; + if (!_netlink_set_tag(&req->n, datalen, tag)) + return 0; + if (!_netlink_route_build_singlepath( p, routedesc, bytelen, nexthop, &req->n, &req->r, datalen, cmd)) @@ -2207,7 +2228,8 @@ ssize_t netlink_route_multipath_msg_encode(int cmd, if (!_netlink_route_build_multipath( p, routedesc, bytelen, nexthop, - &req->n, datalen, &req->r, &src1)) + &req->n, datalen, &req->r, &src1, + tag)) return 0; if (!setsrc && src1) { -- 2.39.5