attrs.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 route attrs) |
19 | #:use-module (ice-9 match) |
20 | #:use-module (netlink constant) |
21 | #:use-module (netlink data) |
22 | #:use-module (rnrs bytevectors) |
23 | #:use-module (srfi srfi-9) |
24 | #:export(make-route-attr |
25 | route-attr? |
26 | route-attr-kind |
27 | route-attr-data |
28 | route-attr-size |
29 | make-u8-route-attr |
30 | make-u16-route-attr |
31 | make-u32-route-attr |
32 | make-s32-route-attr |
33 | make-string-route-attr |
34 | make-ethernet-route-attr |
35 | make-bv-route-attr |
36 | deserialize-route-attr |
37 | deserialize-route-attr-data-string |
38 | deserialize-route-attr-data-u8 |
39 | deserialize-route-attr-data-u32 |
40 | deserialize-route-attr-data-s32 |
41 | deserialize-route-attr-data-ethernet |
42 | deserialize-route-attr-data-bv |
43 | %default-route-attr-decoder)) |
44 | |
45 | (define-data-type route-attr |
46 | attr-type-size |
47 | (lambda (attr pos bv) |
48 | (match attr |
49 | (($ route-attr-type type data) |
50 | (bytevector-u16-set! bv pos (attr-type-size attr) (native-endianness)) |
51 | (bytevector-u16-set! bv (+ pos 2) type (native-endianness)) |
52 | (serialize data (+ pos 4) bv)))) |
53 | (type route-attr-kind route-attr-type-type) |
54 | (data route-attr-data route-attr-type-data)) |
55 | |
56 | (define (attr-type-size attr) |
57 | (+ 4 (data-size (route-attr-type-data attr)))) |
58 | |
59 | (define (make-u8-route-attr num) |
60 | (make-nl-data |
61 | num |
62 | (const 1) |
63 | (lambda (data pos bv) |
64 | (bytevector-u8-set! bv pos data)))) |
65 | |
66 | (define (make-u16-route-attr num) |
67 | (make-nl-data |
68 | num |
69 | (const 2) |
70 | (lambda (data pos bv) |
71 | (bytevector-u16-set! bv pos data (native-endianness))))) |
72 | |
73 | (define (make-u32-route-attr num) |
74 | (make-nl-data |
75 | num |
76 | (const 4) |
77 | (lambda (data pos bv) |
78 | (bytevector-u32-set! bv pos data (native-endianness))))) |
79 | |
80 | (define (make-s32-route-attr num) |
81 | (make-nl-data |
82 | num |
83 | (const 4) |
84 | (lambda (data pos bv) |
85 | (bytevector-s32-set! bv pos data (native-endianness))))) |
86 | |
87 | (define (make-string-route-attr str) |
88 | (make-nl-data |
89 | str |
90 | (lambda (str) (bytevector-length (string->utf8 str))) |
91 | (lambda (data pos bv) |
92 | (let ((s (string->utf8 data))) |
93 | (bytevector-copy! s 0 bv pos (bytevector-length s)))))) |
94 | |
95 | (define (ethernet->bv addr) |
96 | (u8-list->bytevector (map (lambda (n) (string->number n 16)) |
97 | (string-split addr #\:)))) |
98 | (define (make-ethernet-route-attr addr) |
99 | (make-nl-data |
100 | addr |
101 | (lambda (addr) (bytevector-length (ethernet->bv addr))) |
102 | (lambda (data pos bv) |
103 | (let ((a (ethernet->bv data))) |
104 | (bytevector-copy! a 0 bv pos (bytevector-length a)))))) |
105 | |
106 | (define (make-bv-route-attr bv) |
107 | (make-nl-data |
108 | bv |
109 | (lambda (bv) (bytevector-length bv)) |
110 | (lambda (data pos bv) |
111 | (bytevector-copy! data 0 bv pos (bytevector-length data))))) |
112 | |
113 | (define (deserialize-route-attr decoder bv pos) |
114 | (let* ((len (bytevector-u16-ref bv pos (native-endianness))) |
115 | (type (bytevector-u16-ref bv (+ pos 2) (native-endianness))) |
116 | (deserialize (get-next-deserialize decoder 'attr type)) |
117 | (data-bv (make-bytevector (- len 4)))) |
118 | (bytevector-copy! bv (+ pos 4) data-bv 0 (- len 4)) |
119 | (make-route-attr |
120 | type |
121 | (deserialize decoder data-bv 0)))) |
122 | |
123 | (define (deserialize-route-attr-data-string decoder bv pos) |
124 | (make-string-route-attr (utf8->string bv))) |
125 | |
126 | (define (deserialize-route-attr-data-u32 decoder bv pos) |
127 | (make-u32-route-attr (bytevector-u32-ref bv pos (native-endianness)))) |
128 | |
129 | (define (deserialize-route-attr-data-s32 decoder bv pos) |
130 | (make-s32-route-attr (bytevector-s32-ref bv pos (native-endianness)))) |
131 | |
132 | (define (deserialize-route-attr-data-u8 decoder bv pos) |
133 | (make-u8-route-attr (bytevector-u8-ref bv pos))) |
134 | |
135 | (define (deserialize-route-attr-data-bv decoder bv pos) |
136 | (make-bv-route-attr bv)) |
137 | |
138 | (define (deserialize-route-attr-data-ethernet decoder bv pos) |
139 | (make-ethernet-route-attr |
140 | (string-join (map (lambda (n) (number->string n 16)) |
141 | (bytevector->u8-list bv)) |
142 | ":"))) |
143 | |
144 | (define %default-route-attr-decoder |
145 | `((,IFLA_IFNAME . ,deserialize-route-attr-data-string) |
146 | (,IFLA_QDISC . ,deserialize-route-attr-data-string) |
147 | (,IFLA_IFALIAS . ,deserialize-route-attr-data-string) |
148 | (,IFLA_PHYS_PORT_NAME . ,deserialize-route-attr-data-string) |
149 | (,IFLA_MTU . ,deserialize-route-attr-data-u32) |
150 | (,IFLA_TXQLEN . ,deserialize-route-attr-data-u32) |
151 | (,IFLA_LINK . ,deserialize-route-attr-data-u32) |
152 | (,IFLA_WEIGHT . ,deserialize-route-attr-data-u32) |
153 | (,IFLA_MASTER . ,deserialize-route-attr-data-u32) |
154 | (,IFLA_NUM_VF . ,deserialize-route-attr-data-u32) |
155 | (,IFLA_PROMISCUITY . ,deserialize-route-attr-data-u32) |
156 | (,IFLA_NUM_TX_QUEUES . ,deserialize-route-attr-data-u32) |
157 | (,IFLA_NUM_RX_QUEUES . ,deserialize-route-attr-data-u32) |
158 | (,IFLA_GSO_MAX_SEGS . ,deserialize-route-attr-data-u32) |
159 | (,IFLA_GSO_MAX_SIZE . ,deserialize-route-attr-data-u32) |
160 | (,IFLA_GROUP . ,deserialize-route-attr-data-u32) |
161 | (,IFLA_CARRIER_CHANGES . ,deserialize-route-attr-data-u32) |
162 | (,IFLA_NET_NS_PID . ,deserialize-route-attr-data-u32) |
163 | (,IFLA_NET_NS_FD . ,deserialize-route-attr-data-u32) |
164 | (,IFLA_NEW_IFINDEX . ,deserialize-route-attr-data-u32) |
165 | (,IFLA_MIN_MTU . ,deserialize-route-attr-data-u32) |
166 | (,IFLA_MAX_MTU . ,deserialize-route-attr-data-u32) |
167 | (,IFLA_CARRIER_UP_COUNT . ,deserialize-route-attr-data-u32) |
168 | (,IFLA_CARRIER_DOWN_COUNT . ,deserialize-route-attr-data-u32) |
169 | (,IFLA_OPERSTATE . ,deserialize-route-attr-data-u8) |
170 | (,IFLA_LINKMODE . ,deserialize-route-attr-data-u8) |
171 | (,IFLA_CARRIER . ,deserialize-route-attr-data-u8) |
172 | (,IFLA_PROTO_DOWN . ,deserialize-route-attr-data-u8) |
173 | (,IFLA_ADDRESS . ,deserialize-route-attr-data-ethernet) |
174 | (,IFLA_BROADCAST . ,deserialize-route-attr-data-ethernet) |
175 | (,IFLA_PERM_ADDRESS . ,deserialize-route-attr-data-ethernet) |
176 | (default . ,deserialize-route-attr-data-bv))) |
177 |