Subject: [PATCH 3/3] link: Add partial support for bond interfaces

From: Alexey Abramov <[email-masked]>
Date: Sat, 8 Oct 2022 18:53:14 +0200

* ip/link.scm (link-add): Support specific arguments for `bond' type link.
(bond-type-args): New function. Implement mode, miimon and lacp-rate
configuration for bonds.
(alist->keyword+value): New function.
* netlink/constant.scm: Define bond specific information. Define constants for
bond modes.
---
 ip/link.scm          | 65 ++++++++++++++++++++++++++++++++++++++++++++
 netlink/constant.scm | 30 ++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/ip/link.scm b/ip/link.scm
index 70ab79f..f6144c0 100644
--- a/ip/link.scm
+++ b/ip/link.scm
@@ -249,6 +249,70 @@ criteria."
       (close-socket sock)
       (answer-ok? (last answer)))))
 
+(define* (bond-type-args #:key (mode #f) (miimon #f) (lacp-active #f) (lacp-rate #f)
+                         (primary #f) (primary-reselect #f))
+  `(,@(if mode
+          (list
+           (make-route-attr IFLA_BOND_MODE
+             (match mode
+               ("balance-rr" (make-u8-route-attr BOND_MODE_ROUNDROBIN))
+               ("active-backup" (make-u8-route-attr BOND_MODE_ACTIVEBACKUP))
+               ("balance-xor" (make-u8-route-attr BOND_MODE_XOR))
+               ("broadcast" (make-u8-route-attr BOND_MODE_BROADCAST))
+               ("802.3ad" (make-u8-route-attr BOND_MODE_8023AD))
+               ("balance-tlb" (make-u8-route-attr BOND_MODE_TLB))
+               ("balance-alb" (make-u8-route-attr BOND_MODE_ALB))
+               (_ (raise (condition
+                          (&message
+                           (message "Bond field `mode' can be defined as balance-rr|active-backup|balance-xor|broadcast|802.3ad|balance-tlb|balance-alb" ))))))))
+          '())
+    ,@(if miimon
+          (list
+           (make-route-attr IFLA_BOND_MIIMON
+             (make-u32-route-attr miimon)))
+          '())
+    ,@(if primary
+          (list
+           (make-route-attr IFLA_BOND_PRIMARY
+             (make-u32-route-attr (link-name->index primary))))
+          '())
+    ,@(if primary-reselect
+          (list
+           (make-route-attr IFLA_BOND_PRIMARY_RESELECT
+             (match primary-reselect
+               ("always" (make-u8-route-attr BOND_PRIMARY_RESELECT_ALWAYS))
+               ("better" (make-u8-route-attr BOND_PRIMARY_RESELECT_BETTER))
+               ("failure" (make-u8-route-attr BOND_PRIMARY_RESELECT_FAILURE))
+               (_ (raise (condition
+                          (&message
+                           (message "Bond field `primary-reselect' can be defined as always|better|failure" ))))))))
+          '())
+    ,@(if lacp-active
+          (list
+           (make-route-attr IFLA_BOND_AD_LACP_ACTIVE
+             (match lacp-active
+               ("on" (make-u8-route-attr BOND_AD_LACP_ACTIVE_ON))
+               ("off" (make-u8-route-attr BOND_AD_LACP_ACTIVE_OFF))
+               (_ (raise (condition
+                          (&message
+                           (message "Bond field `lacp-active' can be defined as off|on" ))))))))
+          '())
+    ,@(if lacp-rate
+          (list
+           (make-route-attr IFLA_BOND_AD_LACP_RATE
+             (match lacp-rate
+               ("slow" (make-u8-route-attr 0))
+               ("fast" (make-u8-route-attr 1))
+               (_ (raise (condition
+                          (&message
+                           (message "Bond field `lacp-rate' can be defined as slow|fast"))))))))
+          '())))
+
+(define (alist->keyword+value alist)
+  (fold (match-lambda*
+          (((k . v) r)
+           (cons* (symbol->keyword k) v r))) '() alist))
+
 (define* (link-add name type #:key (type-args '()))
   (define request-num (random 65535))
   (define type-data
@@ -268,6 +332,7 @@ criteria."
                              (make-string-route-attr
                                (assoc-ref type-args 'peer)))))))
                '())))
+      ("bond" (apply bond-type-args (alist->keyword+value type-args)))
       ;; TODO: unsupported for now
       (_ '())))
   (define message
diff --git a/netlink/constant.scm b/netlink/constant.scm
index ce5e15e..7c72fc1 100644
--- a/netlink/constant.scm
+++ b/netlink/constant.scm
@@ -82,6 +82,36 @@
   IFLA_VLAN_UNSPEC IFLA_VLAN_ID IFLA_VLAN_FLAGS IFLA_VLAN_EGRESS_QOS
   IFLA_VLAN_INGRESS_QOS IFLA_VLAN_PROTOCOL)
 
+(define-enum int->bond-linkinfo
+  IFLA_BOND_UNSPEC IFLA_BOND_MODE IFLA_BOND_ACTIVE_SLAVE IFLA_BOND_MIIMON
+  IFLA_BOND_UPDELAY IFLA_BOND_DOWNDELAY IFLA_BOND_USE_CARRIER IFLA_BOND_ARP_INTERVAL
+  IFLA_BOND_ARP_IP_TARGET IFLA_BOND_ARP_VALIDATE IFLA_BOND_ARP_ALL_TARGETS
+  IFLA_BOND_PRIMARY IFLA_BOND_PRIMARY_RESELECT IFLA_BOND_FAIL_OVER_MAC IFLA_BOND_XMIT_HASH_POLICY
+  IFLA_BOND_RESEND_IGMP IFLA_BOND_NUM_PEER_NOTIF IFLA_BOND_ALL_SLAVES_ACTIVE
+  IFLA_BOND_MIN_LINKS IFLA_BOND_LP_INTERVAL IFLA_BOND_PACKETS_PER_SLAVE
+  IFLA_BOND_AD_LACP_RATE IFLA_BOND_AD_SELECT IFLA_BOND_AD_INFO IFLA_BOND_AD_ACTOR_SYS_PRIO
+  IFLA_BOND_AD_USER_PORT_KEY IFLA_BOND_AD_ACTOR_SYSTEM IFLA_BOND_TLB_DYNAMIC_LB
+  IFLA_BOND_PEER_NOTIF_DELAY IFLA_BOND_AD_LACP_ACTIVE IFLA_BOND_MISSED_MAX IFLA_BOND_NS_IP6_TARGET)
+
+;; see iproute2/ip/iplink_bond.c for mode_tbl
+(define-public BOND_MODE_ROUNDROBIN 0)
+(define-public BOND_MODE_ACTIVEBACKUP 1)
+(define-public BOND_MODE_XOR 2)
+(define-public BOND_MODE_BROADCAST 3)
+(define-public BOND_MODE_8023AD 4)
+(define-public BOND_MODE_TLB 5)
+;; TLB + RLB (receive load balancing)
+(define-public BOND_MODE_ALB 6)
+
+;; see iproute2/ip/iplink_bond.c primary_reselect_tbl
+(define-public BOND_PRIMARY_RESELECT_ALWAYS 0)
+(define-public BOND_PRIMARY_RESELECT_BETTER 1)
+(define-public BOND_PRIMARY_RESELECT_FAILURE 2)
+
+;; see iproute2/ip/iplink_bond.c for lacp_active_tbl
+(define-public BOND_AD_LACP_ACTIVE_OFF 0)
+(define-public BOND_AD_LACP_ACTIVE_ON 1)
+
 (define-enum int->addr-attr-kind
   IFA_UNSPEC IFA_ADDRESS IFA_LOCAL IFA_LABEL IFA_BROADCAST
   IFA_ANYCAST IFA_CACHEINFO IFA_MULTICAST IFA_FLAGS
-- 
2.36.1