guile-netlink/netlink/constant.scm

constant.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (netlink constant)
19
               #:export (define-enum))
20
21
(define-syntax define-enum-aux
22
  (syntax-rules ()
23
    ((_ num (name n))
24
     (define-public name n))
25
    ((_ num name)
26
     (define-public name num))
27
    ((_ num (name n) rest ...)
28
     (begin
29
       (define-public name n)
30
       (define-enum-aux (+ n 1) rest ...)))
31
    ((_ num name rest ...)
32
     (begin
33
       (define-public name num)
34
       (define-enum-aux (+ num 1) rest ...)))))
35
36
(define-syntax define-enum
37
  (lambda (x)
38
    (define (spec-names specs)
39
      (map
40
        (lambda (spec)
41
          (syntax-case spec ()
42
            ((name val) #'name)
43
            (name #'name)))
44
        specs))
45
46
    (define (getter num name)
47
      #`((= #,num #,name) (quote #,name)))
48
49
    (syntax-case x ()
50
      ((_ integer->symbol name-spec ...)
51
       #`(begin
52
           (define-enum-aux 0 name-spec ...)
53
           (define-public (integer->symbol num)
54
             (cond
55
               #,@(map (lambda (s) (getter #'num s)) (spec-names #'(name-spec ...))))))))))
56
57
(define-enum int->attr-kind
58
             IFLA_UNSPEC IFLA_ADDRESS IFLA_BROADCAST IFLA_IFNAME IFLA_MTU
59
             IFLA_LINK IFLA_QDISC IFLA_STATS IFLA_COST IFLA_PRIORITY
60
             IFLA_MASTER IFLA_WIRELESS IFLA_PROTIFO IFLA_TXQLEN IFLA_MAP
61
             IFLA_WEIGHT IFLA_OPERSTATE IFLA_LINKMODE IFLA_LINKINFO
62
             IFLA_NET_NS_PID IFLA_IFALIAS IFLA_NUM_VF IFLA_VFINFO_LIST
63
             IFLA_STATS64 IFLA_VF_PORTS IFLA_PORT_SELF IFLA_AF_SPEC
64
             IFLA_GROUP IFLA_NET_NS_FD IFLA_EXT_MASK IFLA_PROMISCUITY
65
             IFLA_NUM_TX_QUEUES IFLA_NUM_RX_QUEUES IFLA_CARRIER
66
             IFLA_PHYS_PORT_ID IFLA_CARRIER_CHANGES IFLA_PHYS_SWITCH_ID
67
             IFLA_LINK_NETNSID IFLA_PHYS_PORT_NAME IFLA_PROTO_DOWN
68
             IFLA_GSO_MAX_SEGS IFLA_GSO_MAX_SIZE IFLA_PAD IFLA_XDP
69
             IFLA_EVENT IFLA_NEW_NETNSID IFLA_IF_NETNSID IFLA_CARRIER_UP_COUNT
70
             IFLA_CARRIER_DOWN_COUNT IFLA_NEW_IFINDEX IFLA_MIN_MTU IFLA_MAX_MTU
71
             IFLA_PROP_LIST IFLA_ALT_IFNAME IFLA_PERM_ADDRESS)
72
(define-public IFLA_TARGET_NETNSID IFLA_IF_NETNSID)
73
74
(define-public AF_NETLINK 16)
75
(define-public AF_PACKET 17)
76
77
(define-enum int->protocol
78
             NETLINK_ROUTE NETLINK_UNUSED NETLINK_USERSOCK NETLINK_FIREWALL
79
             NETLINK_SOCK_DIAG NETLINK_NFLOG NETLINK_XFRM NETLINK_SELINUX
80
             NETLINK_ISCSI NETLINK_AUDIT NETLINK_FIB_LOOKUP NETLINK_CONNECTOR
81
             NETLINK_NETFILTER NETLINK_IP6_FW NETLINK_DNRTMSG NETLINK_KOBJECT_UEVENT
82
             NETLINK_GENERIC NETLINK_DM NETLINK_SCSITRANSPORT NETLINK_ECRYPTFS
83
             NETLINK_RDMA NETLINK_CRYPTO NETLINK_SMC)
84
(define-public NETLINK_INET_DIAG NETLINK_SOCK_DIAG)
85
86
(define-enum int->message-kind
87
  (NLMSG_NOOP 1)
88
  NLMSG_ERROR
89
  NLMSG_DONE
90
  NLMSG_OVERUN
91
  (RTM_NEWLINK 16)
92
  RTM_DELLINK
93
  RTM_GETLINK
94
  RTM_SETLINK)
95
96
(define-public NLM_F_REQUEST #x01)
97
(define-public NLM_F_MULTI #x02)
98
(define-public NLM_F_ACK #x04)
99
(define-public NLM_F_ECHO #x08)
100
(define-public NLM_F_DUMP_INTR #x10)
101
(define-public NLM_F_DUMP_FILTERED #x20)
102
103
(define-public NLM_F_ROOT #x100)
104
(define-public NLM_F_MATCH #x200)
105
(define-public NLM_F_ATOMIC #x400)
106
(define-public NLM_F_DUMP (logior NLM_F_ROOT NLM_F_MATCH))
107
108
(define-public NLM_F_REPLACE #x100)
109
(define-public NLM_F_EXCL #x200)
110
(define-public NLM_F_CREATE #x400)
111
(define-public NLM_F_APPEND #x800)
112
113
(define-public NLM_F_NONREC #x100)
114
115
(define-public NLM_F_CAPPED #x100)
116
(define-public NLM_F_ACK_TLVS #x200)
117
118
;; operstate
119
(define-enum int->operstate
120
             IF_OPER_UNKNOWN IF_OPER_NOTPRESENT IF_OPER_DOWN
121
             IF_OPER_LOWERLAYERDOWN IF_OPER_TESTING IF_OPER_DORMANT
122
             IF_OPER_UP)
123