]> git.proxmox.com Git - mirror_frr.git/blob - zebra/kernel_netlink.h
zebra: Use a bool for startup indications
[mirror_frr.git] / zebra / kernel_netlink.h
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 *
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
19 */
20
21 #ifndef _ZEBRA_KERNEL_NETLINK_H
22 #define _ZEBRA_KERNEL_NETLINK_H
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #ifdef HAVE_NETLINK
29
30 #define NL_RCV_PKT_BUF_SIZE 32768
31 #define NL_PKT_BUF_SIZE 8192
32
33 /*
34 * nl_attr_put - add an attribute to the Netlink message.
35 *
36 * Returns true if the attribute could be added to the message (fits into the
37 * buffer), otherwise false is returned.
38 */
39 extern bool nl_attr_put(struct nlmsghdr *n, unsigned int maxlen, int type,
40 const void *data, unsigned int alen);
41 extern bool nl_attr_put8(struct nlmsghdr *n, unsigned int maxlen, int type,
42 uint8_t data);
43 extern bool nl_attr_put16(struct nlmsghdr *n, unsigned int maxlen, int type,
44 uint16_t data);
45 extern bool nl_attr_put32(struct nlmsghdr *n, unsigned int maxlen, int type,
46 uint32_t data);
47
48 /*
49 * nl_attr_nest - start an attribute nest.
50 *
51 * Returns a valid pointer to the beginning of the nest if the attribute
52 * describing the nest could be added to the message (fits into the buffer),
53 * otherwise NULL is returned.
54 */
55 extern struct rtattr *nl_attr_nest(struct nlmsghdr *n, unsigned int maxlen,
56 int type);
57
58 /*
59 * nl_attr_nest_end - finalize nesting of attributes.
60 *
61 * Updates the length field of the attribute header to include the appeneded
62 * attributes. Returns a total length of the Netlink message.
63 */
64 extern int nl_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest);
65
66 /*
67 * nl_attr_rtnh - append a rtnexthop record to the Netlink message.
68 *
69 * Returns a valid pointer to the rtnexthop struct if it could be added to
70 * the message (fits into the buffer), otherwise NULL is returned.
71 */
72 extern struct rtnexthop *nl_attr_rtnh(struct nlmsghdr *n, unsigned int maxlen);
73
74 /*
75 * nl_attr_rtnh_end - finalize adding a rtnexthop record.
76 *
77 * Updates the length field of the rtnexthop to include the appeneded
78 * attributes.
79 */
80 extern void nl_attr_rtnh_end(struct nlmsghdr *n, struct rtnexthop *rtnh);
81
82 extern void netlink_parse_rtattr(struct rtattr **tb, int max,
83 struct rtattr *rta, int len);
84 extern void netlink_parse_rtattr_flags(struct rtattr **tb, int max,
85 struct rtattr *rta, int len,
86 unsigned short flags);
87 extern void netlink_parse_rtattr_nested(struct rtattr **tb, int max,
88 struct rtattr *rta);
89 extern const char *nl_msg_type_to_str(uint16_t msg_type);
90 extern const char *nl_rtproto_to_str(uint8_t rtproto);
91 extern const char *nl_family_to_str(uint8_t family);
92 extern const char *nl_rttype_to_str(uint8_t rttype);
93
94 extern int netlink_parse_info(int (*filter)(struct nlmsghdr *, ns_id_t, int),
95 const struct nlsock *nl,
96 const struct zebra_dplane_info *dp_info,
97 int count, bool startup);
98 extern int netlink_talk_filter(struct nlmsghdr *h, ns_id_t ns, int startup);
99 extern int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
100 struct nlmsghdr *n, struct nlsock *nl,
101 struct zebra_ns *zns, bool startup);
102 extern int netlink_request(struct nlsock *nl, void *req);
103
104 enum netlink_msg_status {
105 FRR_NETLINK_SUCCESS,
106 FRR_NETLINK_ERROR,
107 FRR_NETLINK_QUEUED,
108 };
109
110 struct nl_batch;
111
112 /*
113 * netlink_batch_add_msg - add message to the netlink batch using dplane
114 * context object.
115 *
116 * @ctx: Dataplane context
117 * @msg_encoder: A function that encodes dplane context object into
118 * netlink message. Should take dplane context object,
119 * pointer to a buffer and buffer's length as parameters
120 * and should return -1 on error, 0 on buffer overflow or
121 * size of the encoded message.
122 * @ignore_res: Whether the result of this message should be ignored.
123 * This should be used in some 'update' cases where we
124 * need to send two messages for one context object.
125 *
126 * Return: Status of the message.
127 */
128 extern enum netlink_msg_status netlink_batch_add_msg(
129 struct nl_batch *bth, struct zebra_dplane_ctx *ctx,
130 ssize_t (*msg_encoder)(struct zebra_dplane_ctx *, void *, size_t),
131 bool ignore_res);
132
133 /*
134 * Vty/cli apis
135 */
136 extern int netlink_config_write_helper(struct vty *vty);
137
138 /*
139 * Configure size of the batch buffer and sending threshold. If 'unset', reset
140 * to default value.
141 */
142 extern void netlink_set_batch_buffer_size(uint32_t size, uint32_t threshold,
143 bool set);
144
145 #endif /* HAVE_NETLINK */
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif /* _ZEBRA_KERNEL_NETLINK_H */