[PATCH v2] babel: Implement IPv6 prefix compression on outgoing updates

Toke Høiland-Jørgensen toke at toke.dk
Tue Jun 6 14:52:15 CEST 2017


Previously, the Babel protocol would never use prefix compression on outgoing
updates (but would parse it on incoming ones). This adds compression of IPv6
addresses of outgoing updates.

The compression only works to the extent that the FIB is walked in lexicographic
order; i.e. a prefix is only compressed if it shares bytes with the previous
prefix in the same packet.

Signed-off-by: Toke Høiland-Jørgensen <toke at toke.dk>
---
 proto/babel/packets.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/proto/babel/packets.c b/proto/babel/packets.c
index 0f70898e..8e014955 100644
--- a/proto/babel/packets.c
+++ b/proto/babel/packets.c
@@ -133,6 +133,8 @@ struct babel_write_state {
   u8 router_id_seen;
   u8 next_hop_v4_seen;
   u8 next_hop_v6_seen;
+  u8 def_ip6_prefix[16];	/* Previous IPv6 prefix in network order (for compression) */
+  u8 def_ip6_pxlen;
 };
 
 
@@ -150,6 +152,14 @@ struct babel_write_state {
 
 #define NET_SIZE(n) BYTES(net_pxlen(n))
 
+static inline uint bytes_equal(u8 *b1, u8 *b2, uint maxlen)
+{
+  uint i;
+  for(i = 0; *b1 == *b2 && i < maxlen; i++, b1++, b2++)
+    ;
+  return i;
+}
+
 
 static inline u16
 get_time16(const void *p)
@@ -682,9 +692,31 @@ babel_write_update(struct babel_tlv *hdr, union babel_msg *m,
   }
   else
   {
+    u8 omit, buf[16] = {};
     tlv->ae = BABEL_AE_IP6;
     tlv->plen = net6_pxlen(&msg->net);
-    put_ip6_px(tlv->addr, &msg->net);
+
+    put_ip6_px(buf, &msg->net);
+    omit = bytes_equal(buf, state->def_ip6_prefix, state->def_ip6_pxlen/8);
+    DBG("%d bytes common between %I and previous prefix\n", omit,
+	net6_prefix(&msg->net));
+
+    if (0 < omit)
+    {
+      omit = MIN(omit, tlv->plen/8);
+      memcpy(tlv->addr, buf+omit, BYTES(tlv->plen) - omit);
+
+      tlv->omitted = omit;
+      tlv->length -= omit;
+      len -= omit;
+    }
+    else
+    {
+      put_ip6_px(tlv->addr, &msg->net);
+      tlv->flags |= BABEL_FLAG_DEF_PREFIX;
+      put_ip6_px(state->def_ip6_prefix, &msg->net);
+      state->def_ip6_pxlen = tlv->plen;
+    }
   }
 
   put_time16(&tlv->interval, msg->interval);
-- 
2.13.0



More information about the Bird-users mailing list