On Mon, Jul 11, 2016 at 11:56:47PM -0700, Justin Pettit wrote:
> These will have callers later.
> 
> Signed-off-by: Justin Pettit <jpet...@ovn.org>

>  lib/packets.c | 38 ++++++++++++++++++++++++++++++++++++++
>  lib/packets.h |  3 +++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/lib/packets.c b/lib/packets.c
> index 9e4d0e7..9b34961 100644
> --- a/lib/packets.c
> +++ b/lib/packets.c
> @@ -693,6 +693,44 @@ struct in6_addr ipv6_addr_bitand(const struct in6_addr 
> *a,
>      return dst;
>  }
>  

We usually put the return type on a separate line (here and a second
time later);

> +struct in6_addr ipv6_addr_bitxor(const struct in6_addr *a,
> +                                 const struct in6_addr *b)
> +{

I tend to declare loop variables inside the loop these days:
> +    int i;
> +    struct in6_addr dst;
> +
> +#ifdef s6_addr32
> +    for (i=0; i<4; i++) {
> +        dst.s6_addr32[i] = a->s6_addr32[i] ^ b->s6_addr32[i];
> +    }
> +#else
> +    for (i=0; i<16; i++) {
> +        dst.s6_addr[i] = a->s6_addr[i] ^ b->s6_addr[i];
> +    }
> +#endif
> +
> +    return dst;
> +}
> +
> +bool ipv6_is_zero(const struct in6_addr *a)
> +{
> +#ifdef s6_addr32
> +    for (int i = 0; i < 4; i++) {
> +        if (a->s6_addr32[i]) {
> +            return false;
> +        }
> +    }
> +#else
> +    for (int i = 0; i < 16; i++) {
> +        if (a->s6_addr[i]) {
> +            return false;
> +        }
> +    }
> +#endif
> +
> +    return true;
> +}

But what if we do the whole thing this way?

#ifdef s6_addr32
#define s6_addrX s6_addr32
#define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 4; VAR++)
#else
#define s6_addrX s6_addr
#define IPV6_FOR_EACH(VAR) for (int VAR = 0; VAR < 16; VAR++)
#endif

struct in6_addr
ipv6_addr_bitand(const struct in6_addr *a, const struct in6_addr *b)
{
    struct in6_addr dst;
    IPV6_FOR_EACH (i) {
        dst.s6_addrX[i] = a->s6_addrX[i] & b->s6_addrX[i];
    }
    return dst;
}

struct in6_addr
ipv6_addr_bitxor(const struct in6_addr *a, const struct in6_addr *b)
{
    struct in6_addr dst;
    IPV6_FOR_EACH (i) {
        dst.s6_addrX[i] = a->s6_addrX[i] ^ b->s6_addrX[i];
    }
    return dst;
}

bool
ipv6_is_zero(const struct in6_addr *a)
{
    IPV6_FOR_EACH (i) {
        if (a->s6_addrX[i]) {
            return false;
        }
    }
    return true;
}

or

#ifdef s6_addr32
#define s6_addrX s6_addr32
#define s6_n_addrX 4
#else
#define s6_addrX s6_addr
#define s6_n_addrX 16
#endif

struct in6_addr
ipv6_addr_bitand(const struct in6_addr *a, const struct in6_addr *b)
{
    struct in6_addr dst;
    for (int i = 0; i < s6_n_addrX; i++) {
        dst.s6_addrX[i] = a->s6_addrX[i] & b->s6_addrX[i];
    }
    return dst;
}

struct in6_addr
ipv6_addr_bitxor(const struct in6_addr *a, const struct in6_addr *b)
{
    struct in6_addr dst;
    for (int i = 0; i < s6_n_addrX; i++) {
        dst.s6_addrX[i] = a->s6_addrX[i] ^ b->s6_addrX[i];
    }
    return dst;
}

bool
ipv6_is_zero(const struct in6_addr *a)
{
    for (int i = 0; i < s6_n_addrX; i++) {
        if (a->s6_addrX[i]) {
            return false;
        }
    }
    return true;
}
_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to