utils: Introduce and use inet_prefix_reset()

Initializing @inet_prefix using C initializers or memset() seems
inefficient and unnecessary: only small part of ->data[] field will be
used to store address corresponding to ->family.

Instead initialize ->flags with zero and assume no other fields accessed
before checking corresponding bits in ->flags. For example special
helpers (e.g. is_addrtype_*()) can be used to ensure that @inet_prefix
contains valid ip or ipv6 address.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Serhey Popovych 2018-02-12 22:17:56 +02:00 committed by David Ahern
parent 5a809da4f5
commit 9cc173d485
3 changed files with 10 additions and 4 deletions

View File

@ -65,6 +65,11 @@ enum {
ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI
}; };
static inline void inet_prefix_reset(inet_prefix *p)
{
p->flags = 0;
}
static inline bool is_addrtype_inet(const inet_prefix *p) static inline bool is_addrtype_inet(const inet_prefix *p)
{ {
return p->flags & ADDRTYPE_INET; return p->flags & ADDRTYPE_INET;

View File

@ -71,7 +71,7 @@ static int geneve_parse_opt(struct link_util *lu, int argc, char **argv,
bool set_op = (n->nlmsg_type == RTM_NEWLINK && bool set_op = (n->nlmsg_type == RTM_NEWLINK &&
!(n->nlmsg_flags & NLM_F_CREATE)); !(n->nlmsg_flags & NLM_F_CREATE));
daddr.flags = 0; inet_prefix_reset(&daddr);
while (argc > 0) { while (argc > 0) {
if (!matches(*argv, "id") || if (!matches(*argv, "id") ||

View File

@ -74,8 +74,7 @@ static void check_duparg(__u64 *attrs, int type, const char *key,
static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv, static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
struct nlmsghdr *n) struct nlmsghdr *n)
{ {
inet_prefix saddr; inet_prefix saddr, daddr;
inet_prefix daddr;
__u32 vni = 0; __u32 vni = 0;
__u8 learning = 1; __u8 learning = 1;
__u16 dstport = 0; __u16 dstport = 0;
@ -85,7 +84,9 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
!(n->nlmsg_flags & NLM_F_CREATE)); !(n->nlmsg_flags & NLM_F_CREATE));
saddr.family = daddr.family = AF_UNSPEC; saddr.family = daddr.family = AF_UNSPEC;
saddr.flags = daddr.flags = 0;
inet_prefix_reset(&saddr);
inet_prefix_reset(&daddr);
while (argc > 0) { while (argc > 0) {
if (!matches(*argv, "id") || if (!matches(*argv, "id") ||