1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
28 #include "socket-util.h"
29 #include "rtnl-util.h"
30 #include "event-util.h"
32 #include "rtnl-internal.h"
34 static void test_message_link_bridge(sd_rtnl *rtnl) {
35 _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
38 assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 1) >= 0);
39 assert_se(sd_rtnl_message_link_set_family(message, PF_BRIDGE) >= 0);
40 assert_se(sd_rtnl_message_open_container(message, IFLA_PROTINFO) >= 0);
41 assert_se(sd_rtnl_message_append_u32(message, IFLA_BRPORT_COST, 10) >= 0);
42 assert_se(sd_rtnl_message_close_container(message) >= 0);
44 assert_se(sd_rtnl_message_rewind(message) >= 0);
46 assert_se(sd_rtnl_message_enter_container(message, IFLA_PROTINFO) >= 0);
47 assert_se(sd_rtnl_message_read_u32(message, IFLA_BRPORT_COST, &cost) >= 0);
48 assert_se(cost == 10);
49 assert_se(sd_rtnl_message_exit_container(message) >= 0);
52 static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
53 _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
54 const char *mac = "98:fe:94:3f:c6:18", *name = "test";
55 char buffer[ETHER_ADDR_TO_STRING_MAX];
56 unsigned int mtu = 1450, mtu_out;
58 struct ether_addr mac_out;
60 /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
61 assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
62 assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
63 assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
64 assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
66 assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
67 assert_se(sd_rtnl_message_rewind(message) >= 0);
69 assert_se(sd_rtnl_message_read_string(message, IFLA_IFNAME, &name_out) >= 0);
70 assert_se(streq(name, name_out));
72 assert_se(sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &mac_out) >= 0);
73 assert_se(streq(mac, ether_addr_to_string(&mac_out, buffer)));
75 assert_se(sd_rtnl_message_read_u32(message, IFLA_MTU, &mtu_out) >= 0);
76 assert_se(mtu == mtu_out);
79 static void test_link_get(sd_rtnl *rtnl, int ifindex) {
82 unsigned int mtu = 1500;
86 struct ether_addr eth_data;
88 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
92 assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0);
93 assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0);
94 assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0);
97 assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
98 assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0);
99 assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0);
100 assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0);
101 assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0);
103 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
105 assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
107 assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
108 assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
109 assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
111 assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
112 assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
113 assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
114 assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
115 assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
117 assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, ð_data) == 0);
119 assert_se(sd_rtnl_flush(rtnl) >= 0);
120 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
121 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
125 static void test_address_get(sd_rtnl *rtnl, int ifindex) {
128 struct in_addr in_data;
129 struct ifa_cacheinfo cache;
132 assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
135 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
137 assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
138 assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
139 assert_se(sd_rtnl_message_read_string(r, IFA_LABEL, &label) == 0);
140 assert_se(sd_rtnl_message_read_cache_info(r, IFA_CACHEINFO, &cache) == 0);
142 assert_se(sd_rtnl_flush(rtnl) >= 0);
143 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
144 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
148 static void test_route(void) {
149 _cleanup_rtnl_message_unref_ sd_rtnl_message *req;
150 struct in_addr addr, addr_data;
151 uint32_t index = 2, u32_data;
154 r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC);
156 log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
160 addr.s_addr = htonl(INADDR_LOOPBACK);
162 r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
164 log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
168 r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
170 log_error_errno(r, "Could not append RTA_OIF attribute: %m");
174 assert_se(sd_rtnl_message_rewind(req) >= 0);
176 assert_se(sd_rtnl_message_read_in_addr(req, RTA_GATEWAY, &addr_data) >= 0);
177 assert_se(addr_data.s_addr == addr.s_addr);
179 assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) >= 0);
180 assert_se(u32_data == index);
182 assert_se((req = sd_rtnl_message_unref(req)) == NULL);
185 static void test_multiple(void) {
186 sd_rtnl *rtnl1, *rtnl2;
188 assert_se(sd_rtnl_open(&rtnl1, 0) >= 0);
189 assert_se(sd_rtnl_open(&rtnl2, 0) >= 0);
191 rtnl1 = sd_rtnl_unref(rtnl1);
192 rtnl2 = sd_rtnl_unref(rtnl2);
195 static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
196 char *ifname = userdata;
202 log_info("got link info about %s", ifname);
205 assert_se(sd_rtnl_message_read_string(m, IFLA_IFNAME, &data) >= 0);
206 assert_se(streq(data, "lo"));
211 static void test_event_loop(int ifindex) {
212 _cleanup_event_unref_ sd_event *event = NULL;
213 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
214 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
217 ifname = strdup("lo2");
220 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
221 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
223 assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
225 assert_se(sd_event_default(&event) >= 0);
227 assert_se(sd_rtnl_attach_event(rtnl, event, 0) >= 0);
229 assert_se(sd_event_run(event, 0) >= 0);
231 assert_se(sd_rtnl_detach_event(rtnl) >= 0);
233 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
236 static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
237 int *counter = userdata;
242 r = sd_rtnl_message_get_errno(m);
244 log_info_errno(r, "%d left in pipe. got reply: %m", *counter);
251 static void test_async(int ifindex) {
252 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
253 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL, *r = NULL;
257 ifname = strdup("lo");
260 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
262 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
264 assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
266 assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
267 assert_se(sd_rtnl_process(rtnl, &r) >= 0);
269 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
272 static void test_pipe(int ifindex) {
273 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
274 _cleanup_rtnl_message_unref_ sd_rtnl_message *m1 = NULL, *m2 = NULL;
277 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
279 assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
280 assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);
283 assert_se(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
286 assert_se(sd_rtnl_call_async(rtnl, m2, &pipe_handler, &counter, 0, NULL) >= 0);
288 while (counter > 0) {
289 assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
290 assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
293 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
296 static void test_container(void) {
297 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
300 const char *string_data;
302 assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
304 assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
305 assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "vlan") >= 0);
306 assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
307 assert_se(sd_rtnl_message_close_container(m) >= 0);
308 assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "vlan") >= 0);
309 assert_se(sd_rtnl_message_close_container(m) >= 0);
310 assert_se(sd_rtnl_message_close_container(m) == -EINVAL);
312 assert_se(sd_rtnl_message_rewind(m) >= 0);
314 assert_se(sd_rtnl_message_enter_container(m, IFLA_LINKINFO) >= 0);
315 assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
316 assert_se(streq("vlan", string_data));
318 assert_se(sd_rtnl_message_enter_container(m, IFLA_INFO_DATA) >= 0);
319 assert_se(sd_rtnl_message_read_u16(m, IFLA_VLAN_ID, &u16_data) >= 0);
320 assert_se(sd_rtnl_message_exit_container(m) >= 0);
322 assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
323 assert_se(streq("vlan", string_data));
324 assert_se(sd_rtnl_message_exit_container(m) >= 0);
326 assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) < 0);
328 assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
331 static void test_match(void) {
332 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
334 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
336 assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
337 assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
339 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
340 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
341 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
343 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
346 static void test_get_addresses(sd_rtnl *rtnl) {
347 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
350 assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0);
352 assert_se(sd_rtnl_call(rtnl, req, 0, &reply) >= 0);
354 for (m = reply; m; m = sd_rtnl_message_next(m)) {
356 unsigned char scope, flags;
359 assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
360 assert_se(type == RTM_NEWADDR);
362 assert_se(sd_rtnl_message_addr_get_ifindex(m, &ifindex) >= 0);
363 assert_se(sd_rtnl_message_addr_get_family(m, &family) >= 0);
364 assert_se(sd_rtnl_message_addr_get_scope(m, &scope) >= 0);
365 assert_se(sd_rtnl_message_addr_get_flags(m, &flags) >= 0);
367 assert_se(ifindex > 0);
368 assert_se(family == AF_INET || family == AF_INET6);
370 log_info("got IPv%u address on ifindex %i", family == AF_INET ? 4: 6, ifindex);
374 static void test_message(void) {
375 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
377 assert_se(rtnl_message_new_synthetic_error(-ETIMEDOUT, 1, &m) >= 0);
378 assert_se(sd_rtnl_message_get_errno(m) == -ETIMEDOUT);
385 const char *string_data;
399 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
402 if_loopback = (int) if_nametoindex("lo");
403 assert_se(if_loopback > 0);
405 test_async(if_loopback);
407 test_pipe(if_loopback);
409 test_event_loop(if_loopback);
411 test_link_configure(rtnl, if_loopback);
413 test_get_addresses(rtnl);
415 test_message_link_bridge(rtnl);
417 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
420 assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
421 assert_se(type == RTM_GETLINK);
423 assert_se(sd_rtnl_message_read_string(m, IFLA_IFNAME, &string_data) == -EPERM);
425 assert_se(sd_rtnl_call(rtnl, m, 0, &r) == 1);
426 assert_se(sd_rtnl_message_get_type(r, &type) >= 0);
427 assert_se(type == RTM_NEWLINK);
429 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
431 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM);
432 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
433 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
435 test_link_get(rtnl, if_loopback);
436 test_address_get(rtnl, if_loopback);
438 assert_se(sd_rtnl_flush(rtnl) >= 0);
439 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
440 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
441 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);