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>
27 #include "socket-util.h"
28 #include "rtnl-util.h"
29 #include "event-util.h"
31 #include "rtnl-internal.h"
33 static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
34 _cleanup_rtnl_message_unref_ sd_rtnl_message *message;
36 const char *mac = "98:fe:94:3f:c6:18", *name = "test";
37 unsigned int mtu = 1450;
40 /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
41 assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
42 assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
43 assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
44 assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
46 assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
48 assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
49 assert_se(type == IFLA_IFNAME);
50 assert_se(streq(name, (char *) data));
52 assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
53 assert_se(type == IFLA_ADDRESS);
54 assert_se(streq(mac, ether_ntoa(data)));
56 assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
57 assert_se(type == IFLA_MTU);
58 assert_se(mtu == *(unsigned int *) data);
61 static void test_link_get(sd_rtnl *rtnl, int ifindex) {
64 unsigned int mtu = 1500;
65 unsigned int *mtu_reply;
71 struct ether_addr eth_data;
73 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
77 assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0);
78 assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0);
79 assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0);
82 assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
83 assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0);
84 assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0);
85 assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0);
86 assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0);
88 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
91 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
92 assert_se(type == IFLA_CARRIER);
94 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
95 assert_se(type == IFLA_OPERSTATE);
97 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
98 assert_se(type == IFLA_LINKMODE);
101 assert_se(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1);
102 assert_se(type == IFLA_MTU);
103 assert_se(*mtu_reply == mtu);
105 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
106 assert_se(type == IFLA_GROUP);
108 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
109 assert_se(type == IFLA_TXQLEN);
111 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
112 assert_se(type == IFLA_NUM_TX_QUEUES);
114 assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
115 assert_se(type == IFLA_NUM_RX_QUEUES);
117 while (sd_rtnl_message_read(r, &type, &data) > 0) {
120 // assert_se(*(unsigned int *) data == 65536);
123 // assert_se(streq((char *) data, "noqueue"));
126 assert_se(streq((char *) data, "lo"));
131 assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
133 assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
134 assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
135 assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
137 assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
138 assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
139 assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
140 assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
141 assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
143 assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, ð_data) == 0);
145 assert_se(sd_rtnl_flush(rtnl) >= 0);
146 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
147 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
151 static void test_address_get(sd_rtnl *rtnl, int ifindex) {
154 struct in_addr in_data;
157 assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
160 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);
162 assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
163 assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
164 assert_se(sd_rtnl_message_read_string(r, IFA_LABEL, &label) == 0);
166 assert_se(sd_rtnl_flush(rtnl) >= 0);
167 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
168 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
172 static void test_route(void) {
173 _cleanup_rtnl_message_unref_ sd_rtnl_message *req;
182 r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET);
184 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
188 addr.s_addr = htonl(INADDR_LOOPBACK);
190 r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
192 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
196 r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
198 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
202 assert_se(rtnl_message_seal(NULL, req) >= 0);
204 assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
205 assert_se(type == RTA_GATEWAY);
206 assert_se(((struct in_addr *)data)->s_addr == addr.s_addr);
208 assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
209 assert_se(type == RTA_OIF);
210 assert_se(*(uint32_t *) data == index);
212 rtm = NLMSG_DATA(req->hdr);
213 r = rtnl_message_parse(req,
218 RTM_PAYLOAD(req->hdr));
220 assert_se(sd_rtnl_message_read_u32(req, RTA_GATEWAY, &u32_data) == 0);
221 assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) == 0);
223 assert_se((req = sd_rtnl_message_unref(req)) == NULL);
226 static void test_multiple(void) {
227 sd_rtnl *rtnl1, *rtnl2;
229 assert_se(sd_rtnl_open(&rtnl1, 0) >= 0);
230 assert_se(sd_rtnl_open(&rtnl2, 0) >= 0);
232 rtnl1 = sd_rtnl_unref(rtnl1);
233 rtnl2 = sd_rtnl_unref(rtnl2);
236 static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
239 char *ifname = userdata;
244 log_info("got link info about %s", ifname);
247 while (sd_rtnl_message_read(m, &type, &data) > 0) {
250 // assert_se(*(unsigned int *) data == 65536);
253 // assert_se(streq((char *) data, "noqueue"));
256 assert_se(streq((char *) data, "lo"));
264 static void test_event_loop(int ifindex) {
265 _cleanup_event_unref_ sd_event *event = NULL;
266 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
267 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
270 ifname = strdup("lo2");
273 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
274 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
276 assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
278 assert_se(sd_event_default(&event) >= 0);
280 assert_se(sd_rtnl_attach_event(rtnl, event, 0) >= 0);
282 assert_se(sd_event_run(event, 0) >= 0);
284 assert_se(sd_rtnl_detach_event(rtnl) >= 0);
286 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
289 static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
290 int *counter = userdata;
294 log_info("got reply, %d left in pipe", *counter);
296 return sd_rtnl_message_get_errno(m);
299 static void test_async(int ifindex) {
300 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
301 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL, *r = NULL;
305 ifname = strdup("lo");
308 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
310 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
312 assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
314 assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
315 assert_se(sd_rtnl_process(rtnl, &r) >= 0);
317 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
320 static void test_pipe(int ifindex) {
321 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
322 _cleanup_rtnl_message_unref_ sd_rtnl_message *m1 = NULL, *m2 = NULL;
325 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
327 assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
328 assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);
331 assert_se(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
334 assert_se(sd_rtnl_call_async(rtnl, m2, &pipe_handler, &counter, 0, NULL) >= 0);
336 while (counter > 0) {
337 assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
338 assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
341 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
344 static void test_container(void) {
345 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
350 struct ifinfomsg *ifi;
352 assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
354 assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
355 assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP);
356 assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
357 assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0);
358 assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) == -ENOTSUP);
359 assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
360 assert_se(sd_rtnl_message_close_container(m) >= 0);
361 assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
362 assert_se(sd_rtnl_message_close_container(m) >= 0);
363 assert_se(sd_rtnl_message_close_container(m) == -EINVAL);
365 assert_se(rtnl_message_seal(NULL, m) >= 0);
367 assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
368 assert_se(type == IFLA_LINKINFO);
369 assert_se(data == NULL);
371 assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
372 assert_se(type == IFLA_INFO_KIND);
373 assert_se(streq("kind", (char *)data));
374 assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
375 assert_se(type == IFLA_INFO_DATA);
376 assert_se(data == NULL);
377 assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
378 assert_se(type == IFLA_VLAN_ID);
379 assert_se(*(uint16_t *)data == 100);
380 assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
381 assert_se(sd_rtnl_message_exit_container(m) >= 0);
382 assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
383 assert_se(type == IFLA_INFO_KIND);
384 assert_se(streq("kind", (char *)data));
385 assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
386 assert_se(sd_rtnl_message_exit_container(m) >= 0);
389 ifi = NLMSG_DATA(m->hdr);
390 r = rtnl_message_parse(m,
395 IFLA_PAYLOAD(m->hdr));
399 assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0);
401 assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
404 static void test_match(void) {
405 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
407 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
409 assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
410 assert_se(sd_rtnl_add_match(rtnl, RTM_NEWLINK, &link_handler, NULL) >= 0);
412 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
413 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 1);
414 assert_se(sd_rtnl_remove_match(rtnl, RTM_NEWLINK, &link_handler, NULL) == 0);
416 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
435 assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
438 if_loopback = (int) if_nametoindex("lo");
439 assert_se(if_loopback > 0);
441 test_async(if_loopback);
443 test_pipe(if_loopback);
445 test_event_loop(if_loopback);
447 test_link_configure(rtnl, if_loopback);
449 assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
452 assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
453 assert_se(type == RTM_GETLINK);
455 assert_se(sd_rtnl_message_read(m, &type, &data) == -EPERM);
457 assert_se(sd_rtnl_call(rtnl, m, 0, &r) == 1);
458 assert_se(sd_rtnl_message_get_type(r, &type) >= 0);
459 assert_se(type == RTM_NEWLINK);
461 assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
462 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
464 assert_se(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM);
465 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
466 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
468 test_link_get(rtnl, if_loopback);
469 test_address_get(rtnl, if_loopback);
471 assert_se(sd_rtnl_flush(rtnl) >= 0);
472 assert_se((m = sd_rtnl_message_unref(m)) == NULL);
473 assert_se((r = sd_rtnl_message_unref(r)) == NULL);
474 assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);