1 /* Kernel communication using netlink interface.
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
29 #include "connected.h"
38 #include "lib_errors.h"
41 #include "zebra/zebra_router.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
45 #include "zebra/debug.h"
46 #include "zebra/kernel_netlink.h"
47 #include "zebra/rt_netlink.h"
48 #include "zebra/if_netlink.h"
49 #include "zebra/rule_netlink.h"
50 #include "zebra/netconf_netlink.h"
51 #include "zebra/zebra_errors.h"
53 #ifndef SO_RCVBUFFORCE
54 #define SO_RCVBUFFORCE (33)
57 /* Hack for GNU libc version 2. */
59 #define MSG_TRUNC 0x20
60 #endif /* MSG_TRUNC */
63 #define NLMSG_TAIL(nmsg) \
64 ((struct rtattr *)(((uint8_t *)(nmsg)) \
65 + NLMSG_ALIGN((nmsg)->nlmsg_len)))
69 #define RTA_TAIL(rta) \
70 ((struct rtattr *)(((uint8_t *)(rta)) + RTA_ALIGN((rta)->rta_len)))
73 #ifndef RTNL_FAMILY_IP6MR
74 #define RTNL_FAMILY_IP6MR 129
77 #ifndef RTPROT_MROUTED
78 #define RTPROT_MROUTED 17
81 #define NL_DEFAULT_BATCH_BUFSIZE (16 * NL_PKT_BUF_SIZE)
84 * We limit the batch's size to a number smaller than the length of the
85 * underlying buffer since the last message that wouldn't fit the batch would go
86 * over the upper boundary and then it would have to be encoded again into a new
87 * buffer. If the difference between the limit and the length of the buffer is
88 * big enough (bigger than the biggest Netlink message) then this situation
91 #define NL_DEFAULT_BATCH_SEND_THRESHOLD (15 * NL_PKT_BUF_SIZE)
93 static const struct message nlmsg_str
[] = {{RTM_NEWROUTE
, "RTM_NEWROUTE"},
94 {RTM_DELROUTE
, "RTM_DELROUTE"},
95 {RTM_GETROUTE
, "RTM_GETROUTE"},
96 {RTM_NEWLINK
, "RTM_NEWLINK"},
97 {RTM_SETLINK
, "RTM_SETLINK"},
98 {RTM_DELLINK
, "RTM_DELLINK"},
99 {RTM_GETLINK
, "RTM_GETLINK"},
100 {RTM_NEWADDR
, "RTM_NEWADDR"},
101 {RTM_DELADDR
, "RTM_DELADDR"},
102 {RTM_GETADDR
, "RTM_GETADDR"},
103 {RTM_NEWNEIGH
, "RTM_NEWNEIGH"},
104 {RTM_DELNEIGH
, "RTM_DELNEIGH"},
105 {RTM_GETNEIGH
, "RTM_GETNEIGH"},
106 {RTM_NEWRULE
, "RTM_NEWRULE"},
107 {RTM_DELRULE
, "RTM_DELRULE"},
108 {RTM_GETRULE
, "RTM_GETRULE"},
109 {RTM_NEWNEXTHOP
, "RTM_NEWNEXTHOP"},
110 {RTM_DELNEXTHOP
, "RTM_DELNEXTHOP"},
111 {RTM_GETNEXTHOP
, "RTM_GETNEXTHOP"},
112 {RTM_NEWNETCONF
, "RTM_NEWNETCONF"},
113 {RTM_DELNETCONF
, "RTM_DELNETCONF"},
116 static const struct message rtproto_str
[] = {
117 {RTPROT_REDIRECT
, "redirect"},
118 {RTPROT_KERNEL
, "kernel"},
119 {RTPROT_BOOT
, "boot"},
120 {RTPROT_STATIC
, "static"},
121 {RTPROT_GATED
, "GateD"},
122 {RTPROT_RA
, "router advertisement"},
124 {RTPROT_ZEBRA
, "Zebra"},
126 {RTPROT_BIRD
, "BIRD"},
127 #endif /* RTPROT_BIRD */
128 {RTPROT_MROUTED
, "mroute"},
130 {RTPROT_OSPF
, "OSPF"},
131 {RTPROT_ISIS
, "IS-IS"},
133 {RTPROT_RIPNG
, "RIPNG"},
134 {RTPROT_ZSTATIC
, "static"},
137 static const struct message family_str
[] = {{AF_INET
, "ipv4"},
139 {AF_BRIDGE
, "bridge"},
140 {RTNL_FAMILY_IPMR
, "ipv4MR"},
141 {RTNL_FAMILY_IP6MR
, "ipv6MR"},
144 static const struct message rttype_str
[] = {{RTN_UNSPEC
, "none"},
145 {RTN_UNICAST
, "unicast"},
146 {RTN_LOCAL
, "local"},
147 {RTN_BROADCAST
, "broadcast"},
148 {RTN_ANYCAST
, "anycast"},
149 {RTN_MULTICAST
, "multicast"},
150 {RTN_BLACKHOLE
, "blackhole"},
151 {RTN_UNREACHABLE
, "unreachable"},
152 {RTN_PROHIBIT
, "prohibited"},
153 {RTN_THROW
, "throw"},
155 {RTN_XRESOLVE
, "resolver"},
158 extern struct thread_master
*master
;
160 extern struct zebra_privs_t zserv_privs
;
162 DEFINE_MTYPE_STATIC(ZEBRA
, NL_BUF
, "Zebra Netlink buffers");
164 /* Hashtable and mutex to allow lookup of nlsock structs by socket/fd value.
165 * We have both the main and dplane pthreads using these structs, so we have
166 * to protect the hash with a lock.
168 static struct hash
*nlsock_hash
;
169 pthread_mutex_t nlsock_mutex
;
171 /* Lock and unlock wrappers for nlsock hash */
172 #define NLSOCK_LOCK() pthread_mutex_lock(&nlsock_mutex)
173 #define NLSOCK_UNLOCK() pthread_mutex_unlock(&nlsock_mutex)
175 size_t nl_batch_tx_bufsize
;
176 char *nl_batch_tx_buf
;
178 _Atomic
uint32_t nl_batch_bufsize
= NL_DEFAULT_BATCH_BUFSIZE
;
179 _Atomic
uint32_t nl_batch_send_threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
190 const struct zebra_dplane_info
*zns
;
192 struct dplane_ctx_q ctx_list
;
195 * Pointer to the queue of completed contexts outbound back
196 * towards the dataplane module.
198 struct dplane_ctx_q
*ctx_out_q
;
201 int netlink_config_write_helper(struct vty
*vty
)
204 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
205 uint32_t threshold
= atomic_load_explicit(&nl_batch_send_threshold
,
206 memory_order_relaxed
);
208 if (size
!= NL_DEFAULT_BATCH_BUFSIZE
209 || threshold
!= NL_DEFAULT_BATCH_SEND_THRESHOLD
)
210 vty_out(vty
, "zebra kernel netlink batch-tx-buf %u %u\n", size
,
213 if (if_netlink_frr_protodown_r_bit_is_set())
214 vty_out(vty
, "zebra protodown reason-bit %u\n",
215 if_netlink_get_frr_protodown_r_bit());
220 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
223 size
= NL_DEFAULT_BATCH_BUFSIZE
;
224 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
227 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
228 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
229 memory_order_relaxed
);
232 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
235 * This is an error condition that must be handled during
238 * The netlink_talk_filter function is used for communication
239 * down the netlink_cmd pipe and we are expecting
240 * an ack being received. So if we get here
241 * then we did not receive the ack and instead
242 * received some other message in an unexpected
245 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
246 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
250 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
253 socklen_t newlen
= sizeof(newsize
);
254 socklen_t oldlen
= sizeof(oldsize
);
257 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
259 flog_err_sys(EC_LIB_SOCKET
,
260 "Can't get %s receive buffer size: %s", nl
->name
,
261 safe_strerror(errno
));
265 /* Try force option (linux >= 2.6.14) and fall back to normal set */
266 frr_with_privs(&zserv_privs
) {
267 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
268 &rcvbufsize
, sizeof(rcvbufsize
));
271 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
274 flog_err_sys(EC_LIB_SOCKET
,
275 "Can't set %s receive buffer size: %s", nl
->name
,
276 safe_strerror(errno
));
280 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
282 flog_err_sys(EC_LIB_SOCKET
,
283 "Can't get %s receive buffer size: %s", nl
->name
,
284 safe_strerror(errno
));
290 /* Make socket for Linux netlink interface. */
291 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
295 struct sockaddr_nl snl
;
299 frr_with_privs(&zserv_privs
) {
300 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
302 zlog_err("Can't open %s socket: %s", nl
->name
,
303 safe_strerror(errno
));
307 memset(&snl
, 0, sizeof(snl
));
308 snl
.nl_family
= AF_NETLINK
;
309 snl
.nl_groups
= groups
;
311 /* Bind the socket to the netlink structure for anything. */
312 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
316 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
317 snl
.nl_groups
, safe_strerror(errno
));
322 /* multiple netlink sockets will have different nl_pid */
323 namelen
= sizeof(snl
);
324 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
325 if (ret
< 0 || namelen
!= sizeof(snl
)) {
326 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
327 nl
->name
, safe_strerror(errno
));
334 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
335 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
341 * Dispatch an incoming netlink message; used by the zebra main pthread's
342 * netlink event reader.
344 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
348 * When we handle new message types here
349 * because we are starting to install them
350 * then lets check the netlink_install_filter
351 * and see if we should add the corresponding
352 * allow through entry there.
353 * Probably not needed to do but please
356 switch (h
->nlmsg_type
) {
358 return netlink_route_change(h
, ns_id
, startup
);
360 return netlink_route_change(h
, ns_id
, startup
);
362 return netlink_link_change(h
, ns_id
, startup
);
364 return netlink_link_change(h
, ns_id
, startup
);
368 return netlink_neigh_change(h
, ns_id
);
370 return netlink_rule_change(h
, ns_id
, startup
);
372 return netlink_rule_change(h
, ns_id
, startup
);
374 return netlink_nexthop_change(h
, ns_id
, startup
);
376 return netlink_nexthop_change(h
, ns_id
, startup
);
378 /* Messages handled in the dplane thread */
387 * If we have received this message then
388 * we have made a mistake during development
389 * and we need to write some code to handle
390 * this message type or not ask for
391 * it to be sent up to us
393 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
394 "Unknown netlink nlmsg_type %s(%d) vrf %u",
395 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
403 * Dispatch an incoming netlink message; used by the dataplane pthread's
404 * netlink event reader code.
406 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
410 * Dispatch the incoming messages that the dplane pthread handles
412 switch (h
->nlmsg_type
) {
415 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
419 return netlink_netconf_change(h
, ns_id
, startup
);
421 /* TODO -- other messages for the dplane socket and pthread */
433 static void kernel_read(struct thread
*thread
)
435 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
436 struct zebra_dplane_info dp_info
;
438 /* Capture key info from ns struct */
439 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
441 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
444 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
449 * Called by the dplane pthread to read incoming OS messages and dispatch them.
451 int kernel_dplane_read(struct zebra_dplane_info
*info
)
453 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
455 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
462 * Filter out messages from self that occur on listener socket,
463 * caused by our actions on the command socket(s)
465 * When we add new Netlink message types we probably
466 * do not need to add them here as that we are filtering
467 * on the routes we actually care to receive( which is rarer
468 * then the normal course of operations). We are intentionally
469 * allowing some messages from ourselves through
470 * ( I'm looking at you Interface based netlink messages )
471 * so that we only have to write one way to handle incoming
472 * address add/delete and xxxNETCONF changes.
474 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
477 * BPF_JUMP instructions and where you jump to are based upon
478 * 0 as being the next statement. So count from 0. Writing
479 * this down because every time I look at this I have to
482 struct sock_filter filter
[] = {
485 * if (nlmsg_pid == pid ||
486 * nlmsg_pid == dplane_pid) {
487 * if (the incoming nlmsg_type ==
488 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
494 * keep this netlink message
497 * 0: Load the nlmsg_pid into the BPF register
499 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
500 offsetof(struct nlmsghdr
, nlmsg_pid
)),
504 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
506 * 2: Compare to dplane pid
508 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
510 * 3: Load the nlmsg_type into BPF register
512 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
513 offsetof(struct nlmsghdr
, nlmsg_type
)),
515 * 4: Compare to RTM_NEWADDR
517 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
519 * 5: Compare to RTM_DELADDR
521 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
523 * 6: Compare to RTM_NEWNETCONF
525 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
528 * 7: Compare to RTM_DELNETCONF
530 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
533 * 8: This is the end state of we want to skip the
536 BPF_STMT(BPF_RET
| BPF_K
, 0),
537 /* 9: This is the end state of we want to keep
540 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
543 struct sock_fprog prog
= {
544 .len
= array_size(filter
), .filter
= filter
,
547 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
549 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
550 safe_strerror(errno
));
553 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
554 int len
, unsigned short flags
)
558 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
559 while (RTA_OK(rta
, len
)) {
560 type
= rta
->rta_type
& ~flags
;
561 if ((type
<= max
) && (!tb
[type
]))
563 rta
= RTA_NEXT(rta
, len
);
567 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
570 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
571 while (RTA_OK(rta
, len
)) {
572 if (rta
->rta_type
<= max
)
573 tb
[rta
->rta_type
] = rta
;
574 rta
= RTA_NEXT(rta
, len
);
579 * netlink_parse_rtattr_nested() - Parses a nested route attribute
580 * @tb: Pointer to array for storing rtattr in.
581 * @max: Max number to store.
582 * @rta: Pointer to rtattr to look for nested items in.
584 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
587 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
590 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
591 const void *data
, unsigned int alen
)
596 len
= RTA_LENGTH(alen
);
598 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
601 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
602 rta
->rta_type
= type
;
606 memcpy(RTA_DATA(rta
), data
, alen
);
610 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
615 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
618 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
621 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
624 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
627 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
630 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
633 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
635 struct rtattr
*nest
= NLMSG_TAIL(n
);
637 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
640 nest
->rta_type
|= NLA_F_NESTED
;
644 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
646 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
650 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
652 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
654 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
658 memset(rtnh
, 0, sizeof(struct rtnexthop
));
660 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
665 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
667 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
670 const char *nl_msg_type_to_str(uint16_t msg_type
)
672 return lookup_msg(nlmsg_str
, msg_type
, "");
675 const char *nl_rtproto_to_str(uint8_t rtproto
)
677 return lookup_msg(rtproto_str
, rtproto
, "");
680 const char *nl_family_to_str(uint8_t family
)
682 return lookup_msg(family_str
, family
, "");
685 const char *nl_rttype_to_str(uint8_t rttype
)
687 return lookup_msg(rttype_str
, rttype
, "");
690 #define NLA_OK(nla, len) \
691 ((len) >= (int)sizeof(struct nlattr) \
692 && (nla)->nla_len >= sizeof(struct nlattr) \
693 && (nla)->nla_len <= (len))
694 #define NLA_NEXT(nla, attrlen) \
695 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
696 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
697 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
698 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
700 #define ERR_NLA(err, inner_len) \
701 ((struct nlattr *)(((char *)(err)) \
702 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
703 + NLMSG_ALIGN((inner_len))))
705 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
706 struct nlattr
*nla
, int len
)
708 while (NLA_OK(nla
, len
)) {
709 if (nla
->nla_type
<= max
)
710 tb
[nla
->nla_type
] = nla
;
711 nla
= NLA_NEXT(nla
, len
);
715 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
717 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
718 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
719 const struct nlmsghdr
*err_nlh
= NULL
;
720 /* Length not including nlmsghdr */
722 /* Inner error netlink message length */
723 uint32_t inner_len
= 0;
724 const char *msg
= NULL
;
727 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
728 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
730 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
732 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
735 if (tb
[NLMSGERR_ATTR_MSG
])
736 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
738 if (tb
[NLMSGERR_ATTR_OFFS
]) {
739 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
741 if (off
> h
->nlmsg_len
) {
742 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
743 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
745 * Header of failed message
746 * we are not doing anything currently with it
747 * but noticing it for later.
750 zlog_debug("%s: Received %s extended Ack", __func__
,
751 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
755 if (msg
&& *msg
!= '\0') {
756 bool is_err
= !!err
->error
;
759 zlog_err("Extended Error: %s", msg
);
761 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
762 "Extended Warning: %s", msg
);
767 * netlink_send_msg - send a netlink message of a certain size.
769 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
771 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
774 struct sockaddr_nl snl
= {};
775 struct iovec iov
= {};
776 struct msghdr msg
= {};
781 iov
.iov_len
= buflen
;
783 msg
.msg_namelen
= sizeof(snl
);
787 snl
.nl_family
= AF_NETLINK
;
789 /* Send message to netlink interface. */
790 frr_with_privs(&zserv_privs
) {
791 status
= sendmsg(nl
->sock
, &msg
, 0);
795 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
796 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
798 nl_dump(buf
, buflen
);
800 zlog_hexdump(buf
, buflen
);
801 #endif /* NETLINK_DEBUG */
805 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
806 safe_strerror(save_errno
));
814 * netlink_recv_msg - receive a netlink message.
816 * Returns -1 on error, 0 if read would block or the number of bytes received.
818 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
823 iov
.iov_base
= nl
->buf
;
824 iov
.iov_len
= nl
->buflen
;
831 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
833 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
834 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
836 iov
.iov_base
= nl
->buf
;
837 iov
.iov_len
= nl
->buflen
;
840 status
= recvmsg(nl
->sock
, msg
, 0);
841 } while (status
== -1 && errno
== EINTR
);
844 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
846 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
847 nl
->name
, safe_strerror(errno
));
849 * In this case we are screwed. There is no good way to recover
850 * zebra at this point.
856 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
860 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
861 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
862 "%s sender address length error: length %d", nl
->name
,
867 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
868 zlog_debug("%s: << netlink message dump [recv]", __func__
);
870 nl_dump(nl
->buf
, status
);
872 zlog_hexdump(nl
->buf
, status
);
873 #endif /* NETLINK_DEBUG */
880 * netlink_parse_error - parse a netlink error message
882 * Returns 1 if this message is acknowledgement, 0 if this error should be
883 * ignored, -1 otherwise.
885 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
886 bool is_cmd
, bool startup
)
888 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
889 int errnum
= err
->error
;
890 int msg_type
= err
->msg
.nlmsg_type
;
892 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
893 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
894 "%s error: message truncated", nl
->name
);
899 * Parse the extended information before we actually handle it. At this
900 * point in time we do not do anything other than report the issue.
902 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
903 netlink_parse_extended_ack(h
);
905 /* If the error field is zero, then this is an ACK. */
906 if (err
->error
== 0) {
907 if (IS_ZEBRA_DEBUG_KERNEL
) {
908 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
910 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
911 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
918 /* Deal with errors that occur because of races in link handling. */
920 && ((msg_type
== RTM_DELROUTE
921 && (-errnum
== ENODEV
|| -errnum
== ESRCH
))
922 || (msg_type
== RTM_NEWROUTE
923 && (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)))) {
924 if (IS_ZEBRA_DEBUG_KERNEL
)
925 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
926 nl
->name
, safe_strerror(-errnum
),
927 nl_msg_type_to_str(msg_type
), msg_type
,
928 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
933 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
934 * link-local. The kernel should have already deleted the neighbor so
935 * do not log these as an error.
937 if (msg_type
== RTM_DELNEIGH
938 || (is_cmd
&& msg_type
== RTM_NEWROUTE
939 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
941 * This is known to happen in some situations, don't log as
944 if (IS_ZEBRA_DEBUG_KERNEL
)
945 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
946 nl
->name
, safe_strerror(-errnum
),
947 nl_msg_type_to_str(msg_type
), msg_type
,
948 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
950 if ((msg_type
!= RTM_GETNEXTHOP
) || !startup
)
951 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
952 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
953 nl
->name
, safe_strerror(-errnum
),
954 nl_msg_type_to_str(msg_type
), msg_type
,
955 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
964 * Receive message from netlink interface and pass those information
965 * to the given function.
967 * filter -> Function to call to read the results
968 * nl -> netlink socket information
969 * zns -> The zebra namespace data
970 * count -> How many we should read in, 0 means as much as possible
971 * startup -> Are we reading in under startup conditions? passed to
974 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
975 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
976 int count
, bool startup
)
984 struct sockaddr_nl snl
;
985 struct msghdr msg
= {.msg_name
= (void *)&snl
,
986 .msg_namelen
= sizeof(snl
)};
989 if (count
&& read_in
>= count
)
992 status
= netlink_recv_msg(nl
, &msg
);
995 else if (status
== 0)
999 for (h
= (struct nlmsghdr
*)nl
->buf
;
1000 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
1001 h
= NLMSG_NEXT(h
, status
)) {
1002 /* Finish of reading. */
1003 if (h
->nlmsg_type
== NLMSG_DONE
)
1006 /* Error handling. */
1007 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1008 int err
= netlink_parse_error(
1009 nl
, h
, zns
->is_cmd
, startup
);
1012 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1020 * What is the right thing to do? The kernel
1021 * is telling us that the dump request was interrupted
1022 * and we more than likely are out of luck and have
1023 * missed data from the kernel. At this point in time
1024 * lets just note that this is happening.
1026 if (h
->nlmsg_flags
& NLM_F_DUMP_INTR
)
1028 EC_ZEBRA_NETLINK_BAD_SEQUENCE
,
1029 "netlink recvmsg: The Dump request was interrupted");
1031 /* OK we got netlink message. */
1032 if (IS_ZEBRA_DEBUG_KERNEL
)
1034 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1036 nl_msg_type_to_str(h
->nlmsg_type
),
1037 h
->nlmsg_type
, h
->nlmsg_len
,
1038 h
->nlmsg_seq
, h
->nlmsg_pid
);
1042 * Ignore messages that maybe sent from
1043 * other actors besides the kernel
1045 if (snl
.nl_pid
!= 0) {
1046 zlog_debug("Ignoring message from pid %u",
1051 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1053 zlog_debug("%s filter function error",
1059 /* After error care. */
1060 if (msg
.msg_flags
& MSG_TRUNC
) {
1061 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1062 "%s error: message truncated", nl
->name
);
1066 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1067 "%s error: data remnant size %d", nl
->name
,
1078 * sendmsg() to netlink socket then recvmsg().
1079 * Calls netlink_parse_info to parse returned data
1081 * filter -> The filter to read final results from kernel
1082 * nlmsghdr -> The data to send to the kernel
1083 * dp_info -> The dataplane and netlink socket information
1084 * startup -> Are we reading in under startup conditions
1085 * This is passed through eventually to filter.
1087 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1090 struct zebra_dplane_info
*dp_info
, bool startup
)
1094 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1095 n
->nlmsg_seq
= dp_info
->seq
;
1096 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1098 if (IS_ZEBRA_DEBUG_KERNEL
)
1100 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1101 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1102 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1105 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1109 * Get reply from netlink socket.
1110 * The reply should either be an acknowlegement or an error.
1112 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1116 * Synchronous version of netlink_talk_info. Converts args to suit the
1117 * common version, which is suitable for both sync and async use.
1119 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1120 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1123 struct zebra_dplane_info dp_info
;
1125 /* Increment sequence number before capturing snapshot of ns socket
1130 /* Capture info in intermediate info struct */
1131 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1133 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1136 /* Issue request message to kernel via netlink socket. GET messages
1137 * are issued through this interface.
1139 int netlink_request(struct nlsock
*nl
, void *req
)
1141 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1143 /* Check netlink socket. */
1145 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1150 /* Fill common fields for all requests. */
1151 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1152 n
->nlmsg_seq
= ++nl
->seq
;
1154 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1160 static int nl_batch_read_resp(struct nl_batch
*bth
)
1163 struct sockaddr_nl snl
;
1164 struct msghdr msg
= {};
1167 struct zebra_dplane_ctx
*ctx
;
1170 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1172 msg
.msg_name
= (void *)&snl
;
1173 msg
.msg_namelen
= sizeof(snl
);
1176 * The responses are not batched, so we need to read and process one
1177 * message at a time.
1180 status
= netlink_recv_msg(nl
, &msg
);
1182 * status == -1 is a full on failure somewhere
1183 * since we don't know where the problem happened
1184 * we must mark all as failed
1186 * Else we mark everything as worked
1189 if (status
== -1 || status
== 0) {
1190 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1193 dplane_ctx_set_status(
1195 ZEBRA_DPLANE_REQUEST_FAILURE
);
1196 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1201 h
= (struct nlmsghdr
*)nl
->buf
;
1205 * Find the corresponding context object. Received responses are
1206 * in the same order as requests we sent, so we can simply
1207 * iterate over the context list and match responses with
1208 * requests at same time.
1211 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1214 * This is a situation where we have gotten
1215 * into a bad spot. We need to know that
1216 * this happens( does it? )
1219 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1225 * 'update' context objects take two consecutive
1228 if (dplane_ctx_is_update(ctx
) &&
1229 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1231 * This is the situation where we get a response
1232 * to a message that should be ignored.
1238 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1239 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1241 /* We have found corresponding context object. */
1242 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1245 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1247 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1249 dplane_ctx_get_ns(ctx
)->seq
);
1254 * If we ignore the message due to an update
1255 * above we should still fricking decode the
1256 * message for our operator to understand
1259 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1262 zlog_debug("%s: netlink error message seq=%d %d",
1263 __func__
, h
->nlmsg_seq
, err
);
1268 * We received a message with the sequence number that isn't
1269 * associated with any dplane context object.
1272 if (IS_ZEBRA_DEBUG_KERNEL
)
1274 "%s: skipping unassociated response, seq number %d NS %u",
1275 __func__
, h
->nlmsg_seq
,
1280 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1281 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1285 dplane_ctx_set_status(
1286 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1288 if (IS_ZEBRA_DEBUG_KERNEL
)
1289 zlog_debug("%s: netlink error message seq=%d ",
1290 __func__
, h
->nlmsg_seq
);
1295 * If we get here then we did not receive neither the ack nor
1296 * the error and instead received some other message in an
1299 if (IS_ZEBRA_DEBUG_KERNEL
)
1300 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1301 __func__
, h
->nlmsg_type
,
1302 nl_msg_type_to_str(h
->nlmsg_type
),
1309 static void nl_batch_reset(struct nl_batch
*bth
)
1311 bth
->buf_head
= bth
->buf
;
1316 TAILQ_INIT(&(bth
->ctx_list
));
1319 static void nl_batch_init(struct nl_batch
*bth
, struct dplane_ctx_q
*ctx_out_q
)
1322 * If the size of the buffer has changed, free and then allocate a new
1326 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1327 if (bufsize
!= nl_batch_tx_bufsize
) {
1328 if (nl_batch_tx_buf
)
1329 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1331 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1332 nl_batch_tx_bufsize
= bufsize
;
1335 bth
->buf
= nl_batch_tx_buf
;
1336 bth
->bufsiz
= bufsize
;
1337 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1338 memory_order_relaxed
);
1340 bth
->ctx_out_q
= ctx_out_q
;
1342 nl_batch_reset(bth
);
1345 static void nl_batch_send(struct nl_batch
*bth
)
1347 struct zebra_dplane_ctx
*ctx
;
1350 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1352 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1354 if (IS_ZEBRA_DEBUG_KERNEL
)
1355 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1356 __func__
, nl
->name
, bth
->curlen
,
1359 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1363 if (nl_batch_read_resp(bth
) == -1)
1368 /* Move remaining contexts to the outbound queue. */
1370 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1375 dplane_ctx_set_status(ctx
,
1376 ZEBRA_DPLANE_REQUEST_FAILURE
);
1378 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1381 nl_batch_reset(bth
);
1384 enum netlink_msg_status
netlink_batch_add_msg(
1385 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1386 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1391 struct nlmsghdr
*msgh
;
1394 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1397 * If there was an error while encoding the message (other than buffer
1398 * overflow) then return an error.
1401 return FRR_NETLINK_ERROR
;
1404 * If the message doesn't fit entirely in the buffer then send the batch
1409 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1410 bth
->bufsiz
- bth
->curlen
);
1412 * If the message doesn't fit in the empty buffer then just
1416 return FRR_NETLINK_ERROR
;
1419 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1420 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1425 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1426 msgh
->nlmsg_seq
= seq
;
1427 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1429 bth
->zns
= dplane_ctx_get_ns(ctx
);
1430 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1431 bth
->curlen
+= size
;
1434 return FRR_NETLINK_QUEUED
;
1437 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1438 struct zebra_dplane_ctx
*ctx
)
1440 if (dplane_ctx_is_skip_kernel(ctx
))
1441 return FRR_NETLINK_SUCCESS
;
1443 switch (dplane_ctx_get_op(ctx
)) {
1445 case DPLANE_OP_ROUTE_INSTALL
:
1446 case DPLANE_OP_ROUTE_UPDATE
:
1447 case DPLANE_OP_ROUTE_DELETE
:
1448 return netlink_put_route_update_msg(bth
, ctx
);
1450 case DPLANE_OP_NH_INSTALL
:
1451 case DPLANE_OP_NH_UPDATE
:
1452 case DPLANE_OP_NH_DELETE
:
1453 return netlink_put_nexthop_update_msg(bth
, ctx
);
1455 case DPLANE_OP_LSP_INSTALL
:
1456 case DPLANE_OP_LSP_UPDATE
:
1457 case DPLANE_OP_LSP_DELETE
:
1458 return netlink_put_lsp_update_msg(bth
, ctx
);
1460 case DPLANE_OP_PW_INSTALL
:
1461 case DPLANE_OP_PW_UNINSTALL
:
1462 return netlink_put_pw_update_msg(bth
, ctx
);
1464 case DPLANE_OP_ADDR_INSTALL
:
1465 case DPLANE_OP_ADDR_UNINSTALL
:
1466 return netlink_put_address_update_msg(bth
, ctx
);
1468 case DPLANE_OP_MAC_INSTALL
:
1469 case DPLANE_OP_MAC_DELETE
:
1470 return netlink_put_mac_update_msg(bth
, ctx
);
1472 case DPLANE_OP_NEIGH_INSTALL
:
1473 case DPLANE_OP_NEIGH_UPDATE
:
1474 case DPLANE_OP_NEIGH_DELETE
:
1475 case DPLANE_OP_VTEP_ADD
:
1476 case DPLANE_OP_VTEP_DELETE
:
1477 case DPLANE_OP_NEIGH_DISCOVER
:
1478 case DPLANE_OP_NEIGH_IP_INSTALL
:
1479 case DPLANE_OP_NEIGH_IP_DELETE
:
1480 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1481 return netlink_put_neigh_update_msg(bth
, ctx
);
1483 case DPLANE_OP_RULE_ADD
:
1484 case DPLANE_OP_RULE_DELETE
:
1485 case DPLANE_OP_RULE_UPDATE
:
1486 return netlink_put_rule_update_msg(bth
, ctx
);
1488 case DPLANE_OP_SYS_ROUTE_ADD
:
1489 case DPLANE_OP_SYS_ROUTE_DELETE
:
1490 case DPLANE_OP_ROUTE_NOTIFY
:
1491 case DPLANE_OP_LSP_NOTIFY
:
1492 case DPLANE_OP_BR_PORT_UPDATE
:
1493 return FRR_NETLINK_SUCCESS
;
1495 case DPLANE_OP_IPTABLE_ADD
:
1496 case DPLANE_OP_IPTABLE_DELETE
:
1497 case DPLANE_OP_IPSET_ADD
:
1498 case DPLANE_OP_IPSET_DELETE
:
1499 case DPLANE_OP_IPSET_ENTRY_ADD
:
1500 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1501 return FRR_NETLINK_ERROR
;
1503 case DPLANE_OP_GRE_SET
:
1504 return netlink_put_gre_set_msg(bth
, ctx
);
1506 case DPLANE_OP_INTF_ADDR_ADD
:
1507 case DPLANE_OP_INTF_ADDR_DEL
:
1508 case DPLANE_OP_INTF_NETCONFIG
:
1509 case DPLANE_OP_NONE
:
1510 return FRR_NETLINK_ERROR
;
1512 case DPLANE_OP_INTF_INSTALL
:
1513 case DPLANE_OP_INTF_UPDATE
:
1514 case DPLANE_OP_INTF_DELETE
:
1515 return netlink_put_intf_update_msg(bth
, ctx
);
1518 return FRR_NETLINK_ERROR
;
1521 void kernel_update_multi(struct dplane_ctx_q
*ctx_list
)
1523 struct nl_batch batch
;
1524 struct zebra_dplane_ctx
*ctx
;
1525 struct dplane_ctx_q handled_list
;
1526 enum netlink_msg_status res
;
1528 TAILQ_INIT(&handled_list
);
1529 nl_batch_init(&batch
, &handled_list
);
1532 ctx
= dplane_ctx_dequeue(ctx_list
);
1536 if (batch
.zns
!= NULL
1537 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1538 nl_batch_send(&batch
);
1541 * Assume all messages will succeed and then mark only the ones
1544 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1546 res
= nl_put_msg(&batch
, ctx
);
1548 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1549 if (res
== FRR_NETLINK_ERROR
)
1550 dplane_ctx_set_status(ctx
,
1551 ZEBRA_DPLANE_REQUEST_FAILURE
);
1553 if (batch
.curlen
> batch
.limit
)
1554 nl_batch_send(&batch
);
1557 nl_batch_send(&batch
);
1559 TAILQ_INIT(ctx_list
);
1560 dplane_ctx_list_append(ctx_list
, &handled_list
);
1563 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1565 struct nlsock lookup
, *retval
;
1570 retval
= hash_lookup(nlsock_hash
, &lookup
);
1576 /* Insert nlsock entry into hash */
1577 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1580 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1584 /* Remove nlsock entry from hash */
1585 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1588 (void)hash_release(nlsock_hash
, nls
);
1592 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1594 const struct nlsock
*nl
= arg
;
1599 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1601 const struct nlsock
*nl1
= arg1
;
1602 const struct nlsock
*nl2
= arg2
;
1604 if (nl1
->sock
== nl2
->sock
)
1610 /* Exported interface function. This function simply calls
1611 netlink_socket (). */
1612 void kernel_init(struct zebra_ns
*zns
)
1614 uint32_t groups
, dplane_groups
;
1615 #if defined SOL_NETLINK
1620 * Initialize netlink sockets
1622 * If RTMGRP_XXX exists use that, but at some point
1623 * I think the kernel developers realized that
1624 * keeping track of all the different values would
1625 * lead to confusion, so we need to convert the
1626 * RTNLGRP_XXX to a bit position for ourself
1628 groups
= RTMGRP_LINK
|
1630 RTMGRP_IPV4_IFADDR
|
1632 RTMGRP_IPV6_IFADDR
|
1633 RTMGRP_IPV4_MROUTE
|
1635 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1636 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1637 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1));
1639 dplane_groups
= (RTMGRP_LINK
|
1640 RTMGRP_IPV4_IFADDR
|
1641 RTMGRP_IPV6_IFADDR
|
1642 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1643 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1644 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1647 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1648 "netlink-listen (NS %u)", zns
->ns_id
);
1649 zns
->netlink
.sock
= -1;
1650 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1651 zlog_err("Failure to create %s socket",
1656 kernel_netlink_nlsock_insert(&zns
->netlink
);
1658 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1659 "netlink-cmd (NS %u)", zns
->ns_id
);
1660 zns
->netlink_cmd
.sock
= -1;
1661 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1662 zlog_err("Failure to create %s socket",
1663 zns
->netlink_cmd
.name
);
1667 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1669 /* Outbound socket for dplane programming of the host OS. */
1670 snprintf(zns
->netlink_dplane_out
.name
,
1671 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1673 zns
->netlink_dplane_out
.sock
= -1;
1674 if (netlink_socket(&zns
->netlink_dplane_out
, 0, zns
->ns_id
) < 0) {
1675 zlog_err("Failure to create %s socket",
1676 zns
->netlink_dplane_out
.name
);
1680 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1682 /* Inbound socket for OS events coming to the dplane. */
1683 snprintf(zns
->netlink_dplane_in
.name
,
1684 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1686 zns
->netlink_dplane_in
.sock
= -1;
1687 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
,
1689 zlog_err("Failure to create %s socket",
1690 zns
->netlink_dplane_in
.name
);
1694 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1697 * SOL_NETLINK is not available on all platforms yet
1698 * apparently. It's in bits/socket.h which I am not
1699 * sure that we want to pull into our build system.
1701 #if defined SOL_NETLINK
1703 * Let's tell the kernel that we want to receive extended
1704 * ACKS over our command socket(s)
1707 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1711 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1712 errno
, safe_strerror(errno
));
1715 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1716 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1719 zlog_notice("Registration for extended dp ACK failed : %d %s",
1720 errno
, safe_strerror(errno
));
1723 * Trim off the payload of the original netlink message in the
1724 * acknowledgment. This option is available since Linux 4.2, so if
1725 * setsockopt fails, ignore the error.
1728 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1729 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1732 "Registration for reduced ACK packet size failed, probably running an early kernel");
1735 /* Register kernel socket. */
1736 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1737 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1738 zns
->netlink
.name
, safe_strerror(errno
));
1740 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1741 zlog_err("Can't set %s socket error: %s(%d)",
1742 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1744 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1745 zlog_err("Can't set %s socket error: %s(%d)",
1746 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1749 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1750 zlog_err("Can't set %s socket error: %s(%d)",
1751 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1754 /* Set receive buffer size if it's set from command line */
1756 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1757 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1758 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1759 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1762 /* Set filter for inbound sockets, to exclude events we've generated
1765 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1766 zns
->netlink_dplane_out
.snl
.nl_pid
);
1768 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1769 zns
->netlink_cmd
.snl
.nl_pid
,
1770 zns
->netlink_dplane_out
.snl
.nl_pid
);
1772 zns
->t_netlink
= NULL
;
1774 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1775 zns
->netlink
.sock
, &zns
->t_netlink
);
1780 /* Helper to clean up an nlsock */
1781 static void kernel_nlsock_fini(struct nlsock
*nls
)
1783 if (nls
&& nls
->sock
>= 0) {
1784 kernel_netlink_nlsock_remove(nls
);
1787 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1792 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1794 thread_cancel(&zns
->t_netlink
);
1796 kernel_nlsock_fini(&zns
->netlink
);
1798 kernel_nlsock_fini(&zns
->netlink_cmd
);
1800 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1802 /* During zebra shutdown, we need to leave the dataplane socket
1803 * around until all work is done.
1806 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1810 * Global init for platform-/OS-specific things
1812 void kernel_router_init(void)
1814 /* Init nlsock hash and lock */
1815 pthread_mutex_init(&nlsock_mutex
, NULL
);
1816 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
1817 kernel_netlink_nlsock_hash_equal
,
1818 "Netlink Socket Hash");
1822 * Global deinit for platform-/OS-specific things
1824 void kernel_router_terminate(void)
1826 pthread_mutex_destroy(&nlsock_mutex
);
1828 hash_free(nlsock_hash
);
1832 #endif /* HAVE_NETLINK */