examples/l3fwd: improve destination lookup readability
authorStephen Hemminger <stephen@networkplumber.org>
Sun, 26 Jan 2020 01:09:11 +0000 (17:09 -0800)
committerThomas Monjalon <thomas@monjalon.net>
Sun, 16 Feb 2020 20:07:55 +0000 (21:07 +0100)
The functions to lookup IPv4 and IPv6 were both using opaque
pointers (void *) when they should use a typed pointer instead.
The IP headers are not modified during lookup.

Get rid of unnecessary cast on the return from the function.
Replace complex trigraph expression with simple if to improve
readability.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
examples/l3fwd/l3fwd_lpm.c

index aff80fe..c97cf0b 100644 (file)
@@ -75,27 +75,31 @@ static struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
 static struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
 
 static inline uint16_t
-lpm_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
+lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr *ipv4_hdr,
+                     uint16_t portid,
+                     struct rte_lpm *ipv4_l3fwd_lookup_struct)
 {
+       uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
        uint32_t next_hop;
-       struct rte_lpm *ipv4_l3fwd_lookup_struct =
-               (struct rte_lpm *)lookup_struct;
 
-       return (uint16_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
-               rte_be_to_cpu_32(((struct rte_ipv4_hdr *)ipv4_hdr)->dst_addr),
-               &next_hop) == 0) ? next_hop : portid);
+       if (rte_lpm_lookup(ipv4_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
+               return next_hop;
+       else
+               return portid;
 }
 
 static inline uint16_t
-lpm_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
+lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr *ipv6_hdr,
+                     uint16_t portid,
+                     struct rte_lpm6 *ipv6_l3fwd_lookup_struct)
 {
+       const uint8_t *dst_ip = ipv6_hdr->dst_addr;
        uint32_t next_hop;
-       struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
-               (struct rte_lpm6 *)lookup_struct;
 
-       return (uint16_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
-                       ((struct rte_ipv6_hdr *)ipv6_hdr)->dst_addr,
-                       &next_hop) == 0) ?  next_hop : portid);
+       if (rte_lpm6_lookup(ipv6_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
+               return next_hop;
+       else
+               return portid;
 }
 
 static __rte_always_inline uint16_t