chiark / gitweb /
sd-rtnl: rework rtnl type system
[elogind.git] / src / libsystemd / sd-rtnl / rtnl-internal.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
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
24 #include <linux/netlink.h>
25
26 #include "refcnt.h"
27 #include "prioq.h"
28 #include "list.h"
29
30 #include "sd-rtnl.h"
31
32 #include "rtnl-types.h"
33
34 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
35
36 #define RTNL_WQUEUE_MAX 1024
37 #define RTNL_RQUEUE_MAX 64*1024
38
39 #define RTNL_CONTAINER_DEPTH 32
40
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 };
48
49 struct match_callback {
50         sd_rtnl_message_handler_t callback;
51         uint16_t type;
52         void *userdata;
53
54         LIST_FIELDS(struct match_callback, match_callbacks);
55 };
56
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
67         sd_rtnl_message **rqueue;
68         unsigned rqueue_size;
69
70         sd_rtnl_message **wqueue;
71         unsigned wqueue_size;
72
73         bool processing:1;
74
75         uint32_t serial;
76
77         struct Prioq *reply_callbacks_prioq;
78         Hashmap *reply_callbacks;
79
80         LIST_HEAD(struct match_callback, match_callbacks);
81
82         pid_t original_pid;
83
84         sd_event_source *io_event_source;
85         sd_event_source *time_event_source;
86         sd_event_source *exit_event_source;
87         sd_event *event;
88 };
89
90 struct sd_rtnl_message {
91         RefCount n_ref;
92
93         sd_rtnl *rtnl;
94
95         struct nlmsghdr *hdr;
96         const struct NLTypeSystem *(container_type_system[RTNL_CONTAINER_DEPTH]); /* the type of the container and all its parents */
97         size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
98         unsigned n_containers; /* number of containers */
99         size_t next_rta_offset; /* offset from hdr to next rta */
100         size_t *rta_offset_tb[RTNL_CONTAINER_DEPTH];
101         unsigned short rta_tb_size[RTNL_CONTAINER_DEPTH];
102         bool sealed:1;
103 };
104
105 int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t type);
106
107 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
108 int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret);
109
110 int rtnl_message_read_internal(sd_rtnl_message *m, unsigned short type, void **data);
111 int rtnl_message_parse(sd_rtnl_message *m,
112                        size_t **rta_offset_tb,
113                        unsigned short *rta_tb_size,
114                        int max,
115                        struct rtattr *rta,
116                        unsigned int rt_len);
117
118 /* Make sure callbacks don't destroy the rtnl connection */
119 #define RTNL_DONT_DESTROY(rtnl) \
120         _cleanup_rtnl_unref_ _unused_ sd_rtnl *_dont_destroy_##rtnl = sd_rtnl_ref(rtnl)