[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1516641978-24269-2-git-send-email-serhe.popovych@gmail.com>
Date: Mon, 22 Jan 2018 19:26:11 +0200
From: Serhey Popovych <serhe.popovych@...il.com>
To: netdev@...r.kernel.org
Subject: [PATCH iproute2-next 1/8] utils: Introduce get_addr_rta()
It is used to get address from netlink attribute to
inet_prefix data structure.
Use memcpy() with constant value to let complier optimize
by replacing a call by inlining load/store instructions.
Signed-off-by: Serhey Popovych <serhe.popovych@...il.com>
---
include/utils.h | 1 +
lib/utils.c | 82 ++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 64 insertions(+), 19 deletions(-)
diff --git a/include/utils.h b/include/utils.h
index f562547..b618663 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -120,6 +120,7 @@ int get_prefix_1(inet_prefix *dst, char *arg, int family);
int get_addr(inet_prefix *dst, const char *arg, int family);
int get_prefix(inet_prefix *dst, char *arg, int family);
int mask2bits(__u32 netmask);
+int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family);
int get_addr_ila(__u64 *val, const char *arg);
int read_prop(const char *dev, char *prop, long *value);
diff --git a/lib/utils.c b/lib/utils.c
index e20b60e..7bbb1eb 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -534,6 +534,28 @@ int get_addr64(__u64 *ap, const char *cp)
return 1;
}
+static void set_address_type(inet_prefix *addr)
+{
+ switch (addr->family) {
+ case AF_INET:
+ if (!addr->data[0])
+ addr->flags |= ADDRTYPE_INET_UNSPEC;
+ else if (IN_MULTICAST(ntohl(addr->data[0])))
+ addr->flags |= ADDRTYPE_INET_MULTI;
+ else
+ addr->flags |= ADDRTYPE_INET;
+ break;
+ case AF_INET6:
+ if (IN6_IS_ADDR_UNSPECIFIED(addr->data))
+ addr->flags |= ADDRTYPE_INET_UNSPEC;
+ else if (IN6_IS_ADDR_MULTICAST(addr->data))
+ addr->flags |= ADDRTYPE_INET_MULTI;
+ else
+ addr->flags |= ADDRTYPE_INET;
+ break;
+ }
+}
+
static int __get_addr_1(inet_prefix *addr, const char *name, int family)
{
memset(addr, 0, sizeof(*addr));
@@ -627,25 +649,7 @@ int get_addr_1(inet_prefix *addr, const char *name, int family)
if (ret)
return ret;
- switch (addr->family) {
- case AF_INET:
- if (!addr->data[0])
- addr->flags |= ADDRTYPE_INET_UNSPEC;
- else if (IN_MULTICAST(ntohl(addr->data[0])))
- addr->flags |= ADDRTYPE_INET_MULTI;
- else
- addr->flags |= ADDRTYPE_INET;
- break;
- case AF_INET6:
- if (IN6_IS_ADDR_UNSPECIFIED(addr->data))
- addr->flags |= ADDRTYPE_INET_UNSPEC;
- else if (IN6_IS_ADDR_MULTICAST(addr->data))
- addr->flags |= ADDRTYPE_INET_MULTI;
- else
- addr->flags |= ADDRTYPE_INET;
- break;
- }
-
+ set_address_type(addr);
return 0;
}
@@ -734,6 +738,46 @@ int get_addr(inet_prefix *dst, const char *arg, int family)
return 0;
}
+int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family)
+{
+ const int len = RTA_PAYLOAD(rta);
+ const void *data = RTA_DATA(rta);
+
+ switch (len) {
+ case 4:
+ dst->family = AF_INET;
+ dst->bytelen = 4;
+ memcpy(dst->data, data, 4);
+ break;
+ case 16:
+ dst->family = AF_INET6;
+ dst->bytelen = 16;
+ memcpy(dst->data, data, 16);
+ break;
+ case 2:
+ dst->family = AF_DECnet;
+ dst->bytelen = 2;
+ memcpy(dst->data, data, 2);
+ break;
+ case 10:
+ dst->family = AF_IPX;
+ dst->bytelen = 10;
+ memcpy(dst->data, data, 10);
+ break;
+ default:
+ return -1;
+ }
+
+ if (family != AF_UNSPEC && family != dst->family)
+ return -2;
+
+ dst->bitlen = -1;
+ dst->flags = 0;
+
+ set_address_type(dst);
+ return 0;
+}
+
int get_prefix(inet_prefix *dst, char *arg, int family)
{
if (family == AF_PACKET) {
--
1.7.10.4
Powered by blists - more mailing lists