]> git.proxmox.com Git - mirror_frr.git/blame - zebra/kernel_netlink.h
*: auto-convert to SPDX License IDs
[mirror_frr.git] / zebra / kernel_netlink.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
1fdc9eae 2/* Declarations and definitions for kernel interaction over netlink
3 * Copyright (C) 2016 Cumulus Networks, Inc.
1fdc9eae 4 */
5
6#ifndef _ZEBRA_KERNEL_NETLINK_H
7#define _ZEBRA_KERNEL_NETLINK_H
8
51e94aa7
EDP
9#ifdef __cplusplus
10extern "C" {
11#endif
12
1fdc9eae 13#ifdef HAVE_NETLINK
14
73b8a68e
DS
15#define RTM_NHA(h) \
16 ((struct rtattr *)(((char *)(h)) + NLMSG_ALIGN(sizeof(struct nhmsg))))
17
18
9ed7517b 19#define NL_RCV_PKT_BUF_SIZE 32768
1fdc9eae 20#define NL_PKT_BUF_SIZE 8192
21
312a6bee
JU
22/*
23 * nl_attr_put - add an attribute to the Netlink message.
24 *
25 * Returns true if the attribute could be added to the message (fits into the
26 * buffer), otherwise false is returned.
27 */
28extern bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type,
29 const void *data, unsigned int alen);
94d70a65
DS
30extern bool nl_attr_put8(struct nlmsghdr *n, unsigned int maxlen, int type,
31 uint8_t data);
312a6bee
JU
32extern bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type,
33 uint16_t data);
34extern bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type,
35 uint32_t data);
36
37/*
38 * nl_attr_nest - start an attribute nest.
39 *
40 * Returns a valid pointer to the beginning of the nest if the attribute
41 * describing the nest could be added to the message (fits into the buffer),
42 * otherwise NULL is returned.
43 */
44extern struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen,
45 int type);
46
47/*
48 * nl_attr_nest_end - finalize nesting of attributes.
49 *
50 * Updates the length field of the attribute header to include the appeneded
51 * attributes. Returns a total length of the Netlink message.
52 */
53extern int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
54
55/*
56 * nl_attr_rtnh - append a rtnexthop record to the Netlink message.
57 *
58 * Returns a valid pointer to the rtnexthop struct if it could be added to
59 * the message (fits into the buffer), otherwise NULL is returned.
60 */
61extern struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen);
62
63/*
64 * nl_attr_rtnh_end - finalize adding a rtnexthop record.
65 *
66 * Updates the length field of the rtnexthop to include the appeneded
67 * attributes.
68 */
69extern void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh);
70
d62a17ae 71extern void netlink_parse_rtattr(struct rtattr **tb, int max,
72 struct rtattr *rta, int len);
4bcdb608
NA
73extern void netlink_parse_rtattr_flags(struct rtattr **tb, int max,
74 struct rtattr *rta, int len,
75 unsigned short flags);
87da6a60
SW
76extern void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
77 struct rtattr *rta);
f8f3e484
CS
78/*
79 * nl_addraw_l copies raw form the netlink message buffer into netlink
80 * message header pointer. It ensures the aligned data buffer does not
81 * override past max length.
82 * return value is 0 if its successful
83 */
84extern bool nl_addraw_l(struct nlmsghdr *n, unsigned int maxlen,
85 const void *data, unsigned int len);
86/*
87 * nl_rta_put - add an additional optional attribute(rtattr) to the
88 * Netlink message buffer.
89 *
90 * Returns true if the attribute could be added to the message (fits into the
91 * buffer), otherwise false is returned.
92 */
93extern bool nl_rta_put(struct rtattr *rta, unsigned int maxlen, int type,
94 const void *data, int alen);
95extern bool nl_rta_put16(struct rtattr *rta, unsigned int maxlen, int type,
96 uint16_t data);
97extern bool nl_rta_put64(struct rtattr *rta, unsigned int maxlen, int type,
98 uint64_t data);
99/*
100 * nl_rta_nest - start an additional optional attribute (rtattr) nest.
101 *
102 * Returns a valid pointer to the beginning of the nest if the attribute
103 * describing the nest could be added to the message (fits into the buffer),
104 * otherwise NULL is returned.
105 */
106extern struct rtattr *nl_rta_nest(struct rtattr *rta, unsigned int maxlen,
107 int type);
108/*
109 * nl_rta_nest_end - finalize nesting of an aditionl optionl attributes.
110 *
111 * Updates the length field of the attribute header to include the appeneded
112 * attributes. Returns a total length of the Netlink message.
113 */
114extern int nl_rta_nest_end(struct rtattr *rta, struct rtattr *nest);
d62a17ae 115extern const char *nl_msg_type_to_str(uint16_t msg_type);
d7c0a89a
QY
116extern const char *nl_rtproto_to_str(uint8_t rtproto);
117extern const char *nl_family_to_str(uint8_t family);
118extern const char *nl_rttype_to_str(uint8_t rttype);
1fdc9eae 119
2414abd3 120extern int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
2cf7651f 121 struct nlsock *nl,
8b962e77 122 const struct zebra_dplane_info *dp_info,
9bfadae8 123 int count, bool startup);
2414abd3
DS
124extern int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns, int startup);
125extern int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
d62a17ae 126 struct nlmsghdr *n, struct nlsock *nl,
9bfadae8 127 struct zebra_ns *zns, bool startup);
fd3f8e52 128extern int netlink_request(struct nlsock *nl, void *req);
1fdc9eae 129
e63c7622
JU
130enum netlink_msg_status {
131 FRR_NETLINK_SUCCESS,
132 FRR_NETLINK_ERROR,
133 FRR_NETLINK_QUEUED,
134};
135
136struct nl_batch;
137
138/*
139 * netlink_batch_add_msg - add message to the netlink batch using dplane
140 * context object.
141 *
142 * @ctx: Dataplane context
143 * @msg_encoder: A function that encodes dplane context object into
144 * netlink message. Should take dplane context object,
145 * pointer to a buffer and buffer's length as parameters
146 * and should return -1 on error, 0 on buffer overflow or
147 * size of the encoded message.
f6feb48b
JU
148 * @ignore_res: Whether the result of this message should be ignored.
149 * This should be used in some 'update' cases where we
150 * need to send two messages for one context object.
e63c7622
JU
151 *
152 * Return: Status of the message.
153 */
154extern enum netlink_msg_status netlink_batch_add_msg(
155 struct nl_batch *bth, struct zebra_dplane_ctx *ctx,
156 ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t),
f6feb48b 157 bool ignore_res);
e63c7622 158
531c92b8
JU
159/*
160 * Vty/cli apis
161 */
162extern int netlink_config_write_helper(struct vty *vty);
163
164/*
165 * Configure size of the batch buffer and sending threshold. If 'unset', reset
166 * to default value.
167 */
168extern void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold,
169 bool set);
e63c7622 170
d4000d7b 171extern struct nlsock *kernel_netlink_nlsock_lookup(int sock);
1fdc9eae 172#endif /* HAVE_NETLINK */
173
51e94aa7
EDP
174#ifdef __cplusplus
175}
176#endif
177
1fdc9eae 178#endif /* _ZEBRA_KERNEL_NETLINK_H */