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