]>
Commit | Line | Data |
---|---|---|
65f568bb TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
a2cdd907 LP |
3 | #pragma once |
4 | ||
65f568bb TG |
5 | /*** |
6 | This file is part of systemd. | |
7 | ||
8 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
9 | ||
10 | systemd is free software; you can redistribute it and/or modify it | |
11 | under the terms of the GNU Lesser General Public License as published by | |
12 | the Free Software Foundation; either version 2.1 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | systemd is distributed in the hope that it will be useful, but | |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | Lesser General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU Lesser General Public License | |
21 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
22 | ***/ | |
23 | ||
a33dece5 LP |
24 | #include <linux/netlink.h> |
25 | ||
65f568bb | 26 | #include "refcnt.h" |
e16bcf98 | 27 | #include "prioq.h" |
8cec01b9 | 28 | #include "list.h" |
e16bcf98 TG |
29 | |
30 | #include "sd-rtnl.h" | |
31 | ||
d8e538ec TG |
32 | #include "rtnl-types.h" |
33 | ||
59a7a684 | 34 | #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) |
3815f36f TG |
35 | |
36 | #define RTNL_WQUEUE_MAX 1024 | |
37 | #define RTNL_RQUEUE_MAX 64*1024 | |
38 | ||
39 | #define RTNL_CONTAINER_DEPTH 32 | |
40 | ||
e16bcf98 TG |
41 | struct reply_callback { |
42 | sd_rtnl_message_handler_t callback; | |
43 | void *userdata; | |
44 | usec_t timeout; | |
45 | uint64_t serial; | |
46 | unsigned prioq_idx; | |
47 | }; | |
65f568bb | 48 | |
8cec01b9 TG |
49 | struct match_callback { |
50 | sd_rtnl_message_handler_t callback; | |
23a7f0f7 | 51 | uint16_t type; |
8cec01b9 TG |
52 | void *userdata; |
53 | ||
54 | LIST_FIELDS(struct match_callback, match_callbacks); | |
55 | }; | |
56 | ||
65f568bb TG |
57 | struct sd_rtnl { |
58 | RefCount n_ref; | |
59 | ||
60 | int fd; | |
61 | ||
62 | union { | |
63 | struct sockaddr sa; | |
64 | struct sockaddr_nl nl; | |
65 | } sockaddr; | |
66 | ||
4555ec72 TG |
67 | sd_rtnl_message **rqueue; |
68 | unsigned rqueue_size; | |
77768cba | 69 | size_t rqueue_allocated; |
4555ec72 | 70 | |
4e996881 TG |
71 | sd_rtnl_message **rqueue_partial; |
72 | unsigned rqueue_partial_size; | |
73 | size_t rqueue_partial_allocated; | |
74 | ||
4555ec72 TG |
75 | sd_rtnl_message **wqueue; |
76 | unsigned wqueue_size; | |
77768cba | 77 | size_t wqueue_allocated; |
4555ec72 | 78 | |
a88f77c4 TG |
79 | struct nlmsghdr *rbuffer; |
80 | size_t rbuffer_allocated; | |
81 | ||
4555ec72 TG |
82 | bool processing:1; |
83 | ||
d4bbdb77 TG |
84 | uint32_t serial; |
85 | ||
e16bcf98 TG |
86 | struct Prioq *reply_callbacks_prioq; |
87 | Hashmap *reply_callbacks; | |
88 | ||
8cec01b9 TG |
89 | LIST_HEAD(struct match_callback, match_callbacks); |
90 | ||
adf412b9 | 91 | pid_t original_pid; |
b4f2a5b1 TG |
92 | |
93 | sd_event_source *io_event_source; | |
94 | sd_event_source *time_event_source; | |
6203e07a | 95 | sd_event_source *exit_event_source; |
b4f2a5b1 | 96 | sd_event *event; |
65f568bb | 97 | }; |
98dd77e8 | 98 | |
3815f36f TG |
99 | struct sd_rtnl_message { |
100 | RefCount n_ref; | |
4555ec72 | 101 | |
4fb7242c TG |
102 | sd_rtnl *rtnl; |
103 | ||
3815f36f | 104 | struct nlmsghdr *hdr; |
d8e538ec | 105 | const struct NLTypeSystem *(container_type_system[RTNL_CONTAINER_DEPTH]); /* the type of the container and all its parents */ |
3815f36f TG |
106 | size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */ |
107 | unsigned n_containers; /* number of containers */ | |
108 | size_t next_rta_offset; /* offset from hdr to next rta */ | |
3dd215e0 TG |
109 | size_t *rta_offset_tb[RTNL_CONTAINER_DEPTH]; |
110 | unsigned short rta_tb_size[RTNL_CONTAINER_DEPTH]; | |
3815f36f | 111 | bool sealed:1; |
3f42446d | 112 | bool broadcast:1; |
1403f45a TG |
113 | |
114 | sd_rtnl_message *next; /* next in a chain of multi-part messages */ | |
3815f36f | 115 | }; |
9d0db178 | 116 | |
d8e538ec | 117 | int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type); |
9d0db178 | 118 | |
65f568bb | 119 | int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m); |
1b89cf56 TG |
120 | int socket_read_message(sd_rtnl *nl); |
121 | ||
122 | int rtnl_rqueue_make_room(sd_rtnl *rtnl); | |
4e996881 | 123 | int rtnl_rqueue_partial_make_room(sd_rtnl *rtnl); |
e16bcf98 | 124 | |
44caa5e7 SS |
125 | int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data); |
126 | int rtnl_message_parse(sd_rtnl_message *m, | |
127 | size_t **rta_offset_tb, | |
128 | unsigned short *rta_tb_size, | |
129 | int max, | |
130 | struct rtattr *rta, | |
131 | unsigned int rt_len); | |
132 | ||
e16bcf98 TG |
133 | /* Make sure callbacks don't destroy the rtnl connection */ |
134 | #define RTNL_DONT_DESTROY(rtnl) \ | |
cf6a8911 | 135 | _cleanup_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl) |