]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
BSD: Add support for kernel route metric
authorOndrej Zajicek <santiago@crfreenet.org>
Fri, 13 Jan 2023 12:17:46 +0000 (13:17 +0100)
committerOndrej Zajicek <santiago@crfreenet.org>
Fri, 13 Jan 2023 12:17:46 +0000 (13:17 +0100)
Add support for kernel route metric/priority, exported as krt_metric
attribute, like in Linux. This should also fix issues with overwriting
or removing system routes.

sysdep/bsd/krt-sock.Y
sysdep/bsd/krt-sock.c
sysdep/bsd/krt-sys.h

index 8581bd43b232ada9a8de76da078d5a7ce7f47762..a03d6df5f884b02a3bc348a61a6f1caf44c67660 100644 (file)
@@ -10,7 +10,7 @@ CF_HDR
 
 CF_DECLS
 
-CF_KEYWORDS(KERNEL, TABLE)
+CF_KEYWORDS(KERNEL, TABLE, METRIC)
 
 CF_GRAMMAR
 
@@ -25,6 +25,14 @@ kern_sys_item:
 
        THIS_KRT->sys.table_id = $3;
    }
+ | METRIC expr {
+     if ($2 && !krt_max_metric)
+       cf_error("Kernel route metric not supported");
+     if ($2 > krt_max_metric)
+       cf_error("Kernel table id must be in range 0-%u", krt_max_metric);
+
+     THIS_KRT->sys.metric = $2;
+   }
  ;
 
 CF_CODE
index 47f5cf595fc4e9d2da251c543bd4d6f134cd05c8..540c246f3af5a344151103b70ced96a993ac23cd 100644 (file)
@@ -47,6 +47,11 @@ const int rt_default_ecmp = 0;
  * table_id is specified explicitly as sysctl scan argument, while in FreeBSD it
  * is handled implicitly by changing default table using setfib() syscall.
  *
+ * OpenBSD allows to use route metric. The behavior is controlled by these macro
+ * KRT_USE_METRIC, which enables use of rtm_priority in route send/recevive.
+ * There is also KRT_DEFAULT_METRIC and KRT_MAX_METRIC for default and maximum
+ * metric values.
+ *
  * KRT_SHARED_SOCKET   - use shared kernel socked instead of one for each krt_proto
  * KRT_USE_SETFIB_SCAN - use setfib() for sysctl() route scan
  * KRT_USE_SETFIB_SOCK - use SO_SETFIB socket option for kernel sockets
@@ -63,6 +68,9 @@ const int rt_default_ecmp = 0;
 
 #ifdef __OpenBSD__
 #define KRT_MAX_TABLES (RT_TABLEID_MAX+1)
+#define KRT_USE_METRIC
+#define KRT_MAX_METRIC 255
+#define KRT_DEFAULT_METRIC 56
 #define KRT_SHARED_SOCKET
 #define KRT_USE_SYSCTL_7
 #endif
@@ -71,6 +79,14 @@ const int rt_default_ecmp = 0;
 #define KRT_MAX_TABLES 1
 #endif
 
+#ifndef KRT_MAX_METRIC
+#define KRT_MAX_METRIC 0
+#endif
+
+#ifndef KRT_DEFAULT_METRIC
+#define KRT_DEFAULT_METRIC 0
+#endif
+
 
 /* Dynamic max number of tables */
 
@@ -143,6 +159,10 @@ static struct krt_proto *krt_table_map[KRT_MAX_TABLES][2];
 #endif
 
 
+/* Make it available to parser code */
+const uint krt_max_metric = KRT_MAX_METRIC;
+
+
 /* Route socket message processing */
 
 int
@@ -231,6 +251,10 @@ krt_send_route(struct krt_proto *p, int cmd, rte *e)
   msg.rtm.rtm_tableid = KRT_CF->sys.table_id;
 #endif
 
+#ifdef KRT_USE_METRIC
+  msg.rtm.rtm_priority = KRT_CF->sys.metric;
+#endif
+
 #ifdef RTF_REJECT
   if(a->dest == RTD_UNREACHABLE)
     msg.rtm.rtm_flags |= RTF_REJECT;
@@ -586,7 +610,7 @@ krt_read_route(struct ks_msg *msg, struct krt_proto *p, int scan)
   e = rte_get_temp(&a, p->p.main_source);
   e->net = net;
 
-  ea_list *ea = alloca(sizeof(ea_list) + 1 * sizeof(eattr));
+  ea_list *ea = alloca(sizeof(ea_list) + 2 * sizeof(eattr));
   *ea = (ea_list) { .count = 1, .next = e->attrs->eattrs };
   e->attrs->eattrs = ea;
 
@@ -596,6 +620,15 @@ krt_read_route(struct ks_msg *msg, struct krt_proto *p, int scan)
     .u.data = src2,
   };
 
+#ifdef KRT_USE_METRIC
+  ea->count++;
+  ea->attrs[1] = (eattr) {
+    .id = EA_KRT_METRIC,
+    .type = EAF_TYPE_INT,
+    .u.data = msg->rtm.rtm_priority,
+  };
+#endif
+
   if (scan)
     krt_got_route(p, e, src);
   else
@@ -1155,7 +1188,7 @@ krt_sys_shutdown(struct krt_proto *p)
 int
 krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o)
 {
-  return n->sys.table_id == o->sys.table_id;
+  return (n->sys.table_id == o->sys.table_id) && (n->sys.metric == o->sys.metric);
 }
 
 void
@@ -1168,11 +1201,13 @@ krt_sys_preconfig(struct config *c UNUSED)
 void krt_sys_init_config(struct krt_config *c)
 {
   c->sys.table_id = 0; /* Default table */
+  c->sys.metric = KRT_DEFAULT_METRIC;
 }
 
 void krt_sys_copy_config(struct krt_config *d, struct krt_config *s)
 {
   d->sys.table_id = s->sys.table_id;
+  d->sys.metric = s->sys.metric;
 }
 
 
index 5750188439f099ec6f0086b1907d2df3816b02e2..198373c095eeeb8ca5388206a024df6847a71220 100644 (file)
@@ -32,9 +32,11 @@ static inline void kif_sys_copy_config(struct kif_config *d UNUSED, struct kif_c
 /* Kernel routes */
 
 extern uint krt_max_tables;
+extern const uint krt_max_metric;
 
 struct krt_params {
   int table_id;                                /* Kernel table ID we sync with */
+  u32 metric;                          /* Kernel metric used for all routes */
 };
 
 struct krt_state {