]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/nl.h
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * Use this as a good size to allocate generic netlink messages
30 #define PAGE_SIZE 4096
32 #define NLMSG_GOOD_SIZE (2*PAGE_SIZE)
33 #define NLMSG_TAIL(nmsg) ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
34 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
35 #define NLA_NEXT_ATTR(attr) ((void *)((char *)attr) + NLA_ALIGN(attr->nla_len))
38 * struct nl_handler : the handler for netlink sockets, this structure
39 * is used all along the netlink socket life cycle to specify the
40 * netlink socket to be used.
42 * @fd: the file descriptor of the netlink socket
43 * @seq: the sequence number of the netlink messages
44 * @local: the bind address
45 * @peer: the peer address
50 struct sockaddr_nl local
;
51 struct sockaddr_nl peer
;
55 * struct nlmsg : the netlink message structure. This message is to be used to
56 * be allocated with netlink_alloc.
58 * @nlmsghdr: a pointer to a netlink message header
59 * @cap: capacity of the netlink message, this is the initially allocated size
60 * and later operations (e.g. reserve and put) can not exceed this limit.
63 struct nlmsghdr
*nlmsghdr
;
68 * netlink_open : open a netlink socket, the function will
69 * fill the handler with the right value
71 * @handler: a netlink handler to be used all along the netlink
73 * @protocol: specify the protocol to be used when opening the
76 * Return 0 on success, < 0 otherwise
78 int netlink_open(struct nl_handler
*handler
, int protocol
);
81 * netlink_close : close a netlink socket, after this call,
82 * the handler is no longer valid
84 * @handler: a handler to the netlink socket
86 * Returns 0 on success, < 0 otherwise
88 int netlink_close(struct nl_handler
*handler
);
91 * netlink_rcv : receive a netlink message from the kernel.
92 * It is up to the caller to manage the allocation of the
95 * @handler: a handler to the netlink socket
96 * @nlmsg: a netlink message
98 * Returns 0 on success, < 0 otherwise
100 int netlink_rcv(struct nl_handler
*handler
, struct nlmsg
*nlmsg
);
103 * netlink_send: send a netlink message to the kernel. It is up
104 * to the caller to manage the allocate of the netlink message
106 * @handler: a handler to the netlink socket
107 * @nlmsg: a netlink message
109 * Returns 0 on success, < 0 otherwise
111 int netlink_send(struct nl_handler
*handler
, struct nlmsg
*nlmsg
);
114 * netlink_transaction: send a request to the kernel and read the response.
115 * This is useful for transactional protocol. It is up to the caller
116 * to manage the allocation of the netlink message.
118 * @handler: a handler to a opened netlink socket
119 * @request: a netlink message pointer containing the request
120 * @answer: a netlink message pointer to receive the result
122 * Returns 0 on success, < 0 otherwise
124 int netlink_transaction(struct nl_handler
*handler
,
125 struct nlmsg
*request
, struct nlmsg
*anwser
);
128 * nla_put_string: copy a null terminated string to a netlink message
131 * @nlmsg: the netlink message to be filled
132 * @attr: the attribute name of the string
133 * @string: a null terminated string to be copied to the netlink message
135 * Returns 0 on success, < 0 otherwise
137 int nla_put_string(struct nlmsg
*nlmsg
, int attr
, const char *string
);
140 * nla_put_buffer: copy a buffer with a specified size to a netlink
143 * @nlmsg: the netlink message to be filled
144 * @attr: the attribute name of the string
145 * @data: a pointer to a buffer
146 * @size: the size of the buffer
148 * Returns 0 on success, < 0 otherwise
150 int nla_put_buffer(struct nlmsg
*nlmsg
, int attr
,
151 const void *data
, size_t size
);
154 * nla_put_u32: copy an integer to a netlink message attribute
156 * @nlmsg: the netlink message to be filled
157 * @attr: the attribute name of the integer
158 * @string: an integer to be copied to the netlink message
160 * Returns 0 on success, < 0 otherwise
162 int nla_put_u32(struct nlmsg
*nlmsg
, int attr
, int value
);
165 * nla_put_u16: copy an integer to a netlink message attribute
167 * @nlmsg: the netlink message to be filled
168 * @attr: the attribute name of the unsigned 16-bit value
169 * @value: 16-bit attribute data value to be copied to the netlink message
171 * Returns 0 on success, < 0 otherwise
173 int nla_put_u16(struct nlmsg
*nlmsg
, int attr
, unsigned short value
);
176 * nla_put_attr: add an attribute name to a netlink
178 * @nlmsg: the netlink message to be filled
179 * @attr: the attribute name of the integer
181 * Returns 0 on success, < 0 otherwise
183 int nla_put_attr(struct nlmsg
*nlmsg
, int attr
);
186 * nla_begin_nested: begin the nesting attribute
188 * @nlmsg: the netlink message to be filled
189 * @attr: the netsted attribute name
191 * Returns current nested pointer to be reused
194 struct rtattr
*nla_begin_nested(struct nlmsg
*nlmsg
, int attr
);
197 * nla_end_nested: end the nesting attribute
199 * @nlmsg: the netlink message
200 * @nested: the nested pointer
202 * Returns the current
204 void nla_end_nested(struct nlmsg
*nlmsg
, struct rtattr
*attr
);
207 * nlmsg_allocate : allocate a netlink message. The netlink format message
208 * is a header, a padding, a payload and a padding again.
209 * When a netlink message is allocated, the size specify the
210 * payload we want. So the real size of the allocated message
211 * is sizeof(header) + sizeof(padding) + payloadsize + sizeof(padding),
212 * in other words, the function will allocate more than specified. When
213 * the buffer is allocated, the content is zeroed.
214 * The function will also fill the field nlmsg_len with NLMSG_HDRLEN.
215 * If the allocation must be for the specified size, just use malloc.
217 * @size: the capacity of the payload to be allocated
219 * Returns a pointer to the newly allocated netlink message, NULL otherwise
221 struct nlmsg
*nlmsg_alloc(size_t size
);
224 * nlmsg_alloc_reserve: like nlmsg_alloc(), but reserve the whole payload
225 * after allocated, that is, the field nlmsg_len be set to the capacity
226 * of nlmsg. Often used to allocate a message for the reply.
228 * @size: the capacity of the payload to be allocated.
230 struct nlmsg
*nlmsg_alloc_reserve(size_t size
);
233 * Reserve room for additional data at the tail of a netlink message
235 * @nlmsg: the netlink message
236 * @len: length of additional data to reserve room for
238 * Returns a pointer to newly reserved room or NULL
240 void *nlmsg_reserve(struct nlmsg
*nlmsg
, size_t len
);
243 * nlmsg_free : free a previously allocate message
245 * @nlmsg: the netlink message to be freed
247 void nlmsg_free(struct nlmsg
*nlmsg
);
250 * nlmsg_data : returns a pointer to the data contained in the netlink message
252 * @nlmsg : the netlink message to get the data
254 * Returns a pointer to the netlink data or NULL if there is no data
256 void *nlmsg_data(struct nlmsg
*nlmsg
);