summaryrefslogtreecommitdiffstats
path: root/zebra/kernel_netlink.c
diff options
context:
space:
mode:
authorJakub Urbańczyk <xthaid@gmail.com>2020-06-08 23:37:26 +0200
committerJakub Urbańczyk <xthaid@gmail.com>2020-06-13 22:53:24 +0200
commit312a6beed67017e53679673a763c7693514cdd7e (patch)
tree9afc490bb0d334007aa9f7a8673c8d44e8d81187 /zebra/kernel_netlink.c
parentMerge pull request #6563 from donaldsharp/scale_tests (diff)
downloadfrr-312a6beed67017e53679673a763c7693514cdd7e.tar.xz
frr-312a6beed67017e53679673a763c7693514cdd7e.zip
zebra: clean up netlink api
* Rename netlink utility functions like addattr to be less ambiguous * Replace rta_attr_* functions with nl_attr_* since they introduced inconsistencies in the code * Add helper functions for adding rtnexthop struct to the Netlink message Signed-off-by: Jakub Urbańczyk <xthaid@gmail.com>
Diffstat (limited to 'zebra/kernel_netlink.c')
-rw-r--r--zebra/kernel_netlink.c72
1 files changed, 28 insertions, 44 deletions
diff --git a/zebra/kernel_netlink.c b/zebra/kernel_netlink.c
index a1f7014ce..75a8e9f17 100644
--- a/zebra/kernel_netlink.c
+++ b/zebra/kernel_netlink.c
@@ -527,8 +527,8 @@ void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
netlink_parse_rtattr(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta));
}
-int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
- const void *data, unsigned int alen)
+bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type,
+ const void *data, unsigned int alen)
{
int len;
struct rtattr *rta;
@@ -536,7 +536,7 @@ int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
len = RTA_LENGTH(alen);
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
- return -1;
+ return false;
rta = (struct rtattr *)(((char *)n) + NLMSG_ALIGN(n->nlmsg_len));
rta->rta_type = type;
@@ -549,72 +549,56 @@ int addattr_l(struct nlmsghdr *n, unsigned int maxlen, int type,
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
- return 0;
-}
-
-int rta_addattr_l(struct rtattr *rta, unsigned int maxlen, int type,
- const void *data, unsigned int alen)
-{
- unsigned int len;
- struct rtattr *subrta;
-
- len = RTA_LENGTH(alen);
-
- if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen)
- return -1;
-
- subrta = (struct rtattr *)(((char *)rta) + RTA_ALIGN(rta->rta_len));
- subrta->rta_type = type;
- subrta->rta_len = len;
-
- if (data)
- memcpy(RTA_DATA(subrta), data, alen);
- else
- assert(alen == 0);
-
- rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
-
- return 0;
+ return true;
}
-int addattr16(struct nlmsghdr *n, unsigned int maxlen, int type, uint16_t data)
+bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type,
+ uint16_t data)
{
- return addattr_l(n, maxlen, type, &data, sizeof(uint16_t));
+ return nl_attr_put(n, maxlen, type, &data, sizeof(uint16_t));
}
-int addattr32(struct nlmsghdr *n, unsigned int maxlen, int type, int data)
+bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type,
+ uint32_t data)
{
- return addattr_l(n, maxlen, type, &data, sizeof(uint32_t));
+ return nl_attr_put(n, maxlen, type, &data, sizeof(uint32_t));
}
-struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
+struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen, int type)
{
struct rtattr *nest = NLMSG_TAIL(n);
- addattr_l(n, maxlen, type, NULL, 0);
+ if (!nl_attr_put(n, maxlen, type, NULL, 0))
+ return NULL;
+
nest->rta_type |= NLA_F_NESTED;
return nest;
}
-int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
+int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
{
nest->rta_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)nest;
return n->nlmsg_len;
}
-struct rtattr *rta_nest(struct rtattr *rta, int maxlen, int type)
+struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen)
{
- struct rtattr *nest = RTA_TAIL(rta);
+ struct rtnexthop *rtnh = (struct rtnexthop *)NLMSG_TAIL(n);
- rta_addattr_l(rta, maxlen, type, NULL, 0);
- nest->rta_type |= NLA_F_NESTED;
- return nest;
+ if (NLMSG_ALIGN(n->nlmsg_len) + RTNH_ALIGN(sizeof(struct rtnexthop))
+ > maxlen)
+ return NULL;
+
+ memset(rtnh, 0, sizeof(struct rtnexthop));
+ n->nlmsg_len =
+ NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(sizeof(struct rtnexthop));
+
+ return rtnh;
}
-int rta_nest_end(struct rtattr *rta, struct rtattr *nest)
+void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh)
{
- nest->rta_len = (uint8_t *)RTA_TAIL(rta) - (uint8_t *)nest;
- return rta->rta_len;
+ rtnh->rtnh_len = (uint8_t *)NLMSG_TAIL(n) - (uint8_t *)rtnh;
}
const char *nl_msg_type_to_str(uint16_t msg_type)