ip: comparing IP prefixes should not modify them 15/34215/3
authorNeale Ranns <neale@graphiant.com>
Mon, 25 Oct 2021 09:47:09 +0000 (09:47 +0000)
committerNeale Ranns <neale@graphiant.com>
Thu, 18 Nov 2021 14:06:02 +0000 (14:06 +0000)
Type: improvement

make the ip_prefix_cmp take const paramenters.
plus some other miscellaneous functions.

Signed-off-by: Neale Ranns <neale@graphiant.com>
Change-Id: Ib69bacfb09483a8a8f8b89900c92d3d55c354ac6

src/vat/ip_types.c
src/vnet/ip/ip.c
src/vnet/ip/ip.h
src/vnet/ip/ip_types.c
src/vnet/ip/ip_types.h

index 8edcb13..f4dcc96 100644 (file)
@@ -344,23 +344,24 @@ ip_prefix_copy (void *dst, void *src)
 }
 
 int
-ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
+ip_prefix_cmp (const ip_prefix_t *ipp1, const ip_prefix_t *ipp2)
 {
+  ip_prefix_t p1 = *ipp1, p2 = *ipp2;
   int cmp = 0;
 
-  ip_prefix_normalize (p1);
-  ip_prefix_normalize (p2);
+  ip_prefix_normalize (&p1);
+  ip_prefix_normalize (&p2);
 
-  cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
+  cmp = ip_address_cmp (&ip_prefix_addr (&p1), &ip_prefix_addr (&p2));
   if (cmp == 0)
     {
-      if (ip_prefix_len (p1) < ip_prefix_len (p2))
+      if (ip_prefix_len (&p1) < ip_prefix_len (&p2))
        {
          cmp = 1;
        }
       else
        {
-         if (ip_prefix_len (p1) > ip_prefix_len (p2))
+         if (ip_prefix_len (&p1) > ip_prefix_len (&p2))
            cmp = 2;
        }
     }
index 5d0c770..0a602b4 100644 (file)
 
 u32 ip_flow_hash_router_id;
 
+ethernet_type_t
+ip_address_family_to_ether_type (ip_address_family_t af)
+{
+  switch (af)
+    {
+    case AF_IP4:
+      return (ETHERNET_TYPE_IP4);
+    case AF_IP6:
+      return (ETHERNET_TYPE_IP6);
+    }
+  ASSERT (0);
+  return (ETHERNET_TYPE_IP4);
+}
+
 u8
 ip_is_zero (ip46_address_t * ip46_address, u8 is_ip4)
 {
index 8768907..131d687 100644 (file)
@@ -287,6 +287,8 @@ void ip_feature_enable_disable (ip_address_family_t af,
                                void *feature_config,
                                u32 n_feature_config_bytes);
 
+ethernet_type_t ip_address_family_to_ether_type (ip_address_family_t af);
+
 always_inline u32 vlib_buffer_get_ip4_fib_index (vlib_buffer_t * b);
 always_inline u32 vlib_buffer_get_ip6_fib_index (vlib_buffer_t * b);
 always_inline u32
index 3e5eceb..323cf4d 100644 (file)
@@ -287,6 +287,13 @@ ip_address_to_fib_prefix (const ip_address_t * addr, fib_prefix_t * prefix)
   prefix->___fp___pad = 0;
 }
 
+void
+ip_address_to_prefix (const ip_address_t *addr, ip_prefix_t *prefix)
+{
+  prefix->len = (addr->version == AF_IP4 ? 32 : 128);
+  clib_memcpy (&prefix->addr, addr, sizeof (prefix->addr));
+}
+
 void
 ip_address_increment (ip_address_t * ip)
 {
@@ -380,23 +387,24 @@ ip_prefix_copy (void *dst, void *src)
 }
 
 int
-ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
+ip_prefix_cmp (const ip_prefix_t *ipp1, const ip_prefix_t *ipp2)
 {
+  ip_prefix_t p1 = *ipp1, p2 = *ipp2;
   int cmp = 0;
 
-  ip_prefix_normalize (p1);
-  ip_prefix_normalize (p2);
+  ip_prefix_normalize (&p1);
+  ip_prefix_normalize (&p2);
 
-  cmp = ip_address_cmp (&ip_prefix_addr (p1), &ip_prefix_addr (p2));
+  cmp = ip_address_cmp (&ip_prefix_addr (&p1), &ip_prefix_addr (&p2));
   if (cmp == 0)
     {
-      if (ip_prefix_len (p1) < ip_prefix_len (p2))
+      if (ip_prefix_len (&p1) < ip_prefix_len (&p2))
        {
          cmp = 1;
        }
       else
        {
-         if (ip_prefix_len (p1) > ip_prefix_len (p2))
+         if (ip_prefix_len (&p1) > ip_prefix_len (&p2))
            cmp = 2;
        }
     }
index 83a0f6a..e4d89eb 100644 (file)
@@ -126,11 +126,13 @@ typedef struct ip_prefix
 #define ip_prefix_v4(_a) ip_addr_v4(&ip_prefix_addr(_a))
 #define ip_prefix_v6(_a) ip_addr_v6(&ip_prefix_addr(_a))
 
-extern int ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2);
+extern int ip_prefix_cmp (const ip_prefix_t *p1, const ip_prefix_t *p2);
 extern void ip_prefix_normalize (ip_prefix_t * a);
 
 extern void ip_address_to_fib_prefix (const ip_address_t * addr,
                                      fib_prefix_t * prefix);
+extern void ip_address_to_prefix (const ip_address_t *addr,
+                                 ip_prefix_t *prefix);
 extern void ip_prefix_to_fib_prefix (const ip_prefix_t * ipp,
                                     fib_prefix_t * fibp);
 extern u8 *format_ip_prefix (u8 * s, va_list * args);