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