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
,
216 void netlink_set_batch_buffer_size(uint32_t size
, uint32_t threshold
, bool set
)
219 size
= NL_DEFAULT_BATCH_BUFSIZE
;
220 threshold
= NL_DEFAULT_BATCH_SEND_THRESHOLD
;
223 atomic_store_explicit(&nl_batch_bufsize
, size
, memory_order_relaxed
);
224 atomic_store_explicit(&nl_batch_send_threshold
, threshold
,
225 memory_order_relaxed
);
228 int netlink_talk_filter(struct nlmsghdr
*h
, ns_id_t ns_id
, int startup
)
231 * This is an error condition that must be handled during
234 * The netlink_talk_filter function is used for communication
235 * down the netlink_cmd pipe and we are expecting
236 * an ack being received. So if we get here
237 * then we did not receive the ack and instead
238 * received some other message in an unexpected
241 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u", __func__
,
242 h
->nlmsg_type
, nl_msg_type_to_str(h
->nlmsg_type
), ns_id
);
246 static int netlink_recvbuf(struct nlsock
*nl
, uint32_t newsize
)
249 socklen_t newlen
= sizeof(newsize
);
250 socklen_t oldlen
= sizeof(oldsize
);
253 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &oldsize
, &oldlen
);
255 flog_err_sys(EC_LIB_SOCKET
,
256 "Can't get %s receive buffer size: %s", nl
->name
,
257 safe_strerror(errno
));
261 /* Try force option (linux >= 2.6.14) and fall back to normal set */
262 frr_with_privs(&zserv_privs
) {
263 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUFFORCE
,
264 &rcvbufsize
, sizeof(rcvbufsize
));
267 ret
= setsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &rcvbufsize
,
270 flog_err_sys(EC_LIB_SOCKET
,
271 "Can't set %s receive buffer size: %s", nl
->name
,
272 safe_strerror(errno
));
276 ret
= getsockopt(nl
->sock
, SOL_SOCKET
, SO_RCVBUF
, &newsize
, &newlen
);
278 flog_err_sys(EC_LIB_SOCKET
,
279 "Can't get %s receive buffer size: %s", nl
->name
,
280 safe_strerror(errno
));
286 /* Make socket for Linux netlink interface. */
287 static int netlink_socket(struct nlsock
*nl
, unsigned long groups
,
291 struct sockaddr_nl snl
;
295 frr_with_privs(&zserv_privs
) {
296 sock
= ns_socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
, ns_id
);
298 zlog_err("Can't open %s socket: %s", nl
->name
,
299 safe_strerror(errno
));
303 memset(&snl
, 0, sizeof(snl
));
304 snl
.nl_family
= AF_NETLINK
;
305 snl
.nl_groups
= groups
;
307 /* Bind the socket to the netlink structure for anything. */
308 ret
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
312 zlog_err("Can't bind %s socket to group 0x%x: %s", nl
->name
,
313 snl
.nl_groups
, safe_strerror(errno
));
318 /* multiple netlink sockets will have different nl_pid */
319 namelen
= sizeof(snl
);
320 ret
= getsockname(sock
, (struct sockaddr
*)&snl
, (socklen_t
*)&namelen
);
321 if (ret
< 0 || namelen
!= sizeof(snl
)) {
322 flog_err_sys(EC_LIB_SOCKET
, "Can't get %s socket name: %s",
323 nl
->name
, safe_strerror(errno
));
330 nl
->buflen
= NL_RCV_PKT_BUF_SIZE
;
331 nl
->buf
= XMALLOC(MTYPE_NL_BUF
, nl
->buflen
);
337 * Dispatch an incoming netlink message; used by the zebra main pthread's
338 * netlink event reader.
340 static int netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
344 * When we handle new message types here
345 * because we are starting to install them
346 * then lets check the netlink_install_filter
347 * and see if we should add the corresponding
348 * allow through entry there.
349 * Probably not needed to do but please
352 switch (h
->nlmsg_type
) {
354 return netlink_route_change(h
, ns_id
, startup
);
356 return netlink_route_change(h
, ns_id
, startup
);
358 return netlink_link_change(h
, ns_id
, startup
);
360 return netlink_link_change(h
, ns_id
, startup
);
364 return netlink_neigh_change(h
, ns_id
);
366 return netlink_rule_change(h
, ns_id
, startup
);
368 return netlink_rule_change(h
, ns_id
, startup
);
370 return netlink_nexthop_change(h
, ns_id
, startup
);
372 return netlink_nexthop_change(h
, ns_id
, startup
);
374 /* Messages handled in the dplane thread */
383 * If we have received this message then
384 * we have made a mistake during development
385 * and we need to write some code to handle
386 * this message type or not ask for
387 * it to be sent up to us
389 flog_err(EC_ZEBRA_UNKNOWN_NLMSG
,
390 "Unknown netlink nlmsg_type %s(%d) vrf %u",
391 nl_msg_type_to_str(h
->nlmsg_type
), h
->nlmsg_type
,
399 * Dispatch an incoming netlink message; used by the dataplane pthread's
400 * netlink event reader code.
402 static int dplane_netlink_information_fetch(struct nlmsghdr
*h
, ns_id_t ns_id
,
406 * Dispatch the incoming messages that the dplane pthread handles
408 switch (h
->nlmsg_type
) {
411 return netlink_interface_addr_dplane(h
, ns_id
, startup
);
415 return netlink_netconf_change(h
, ns_id
, startup
);
417 /* TODO -- other messages for the dplane socket and pthread */
429 static void kernel_read(struct thread
*thread
)
431 struct zebra_ns
*zns
= (struct zebra_ns
*)THREAD_ARG(thread
);
432 struct zebra_dplane_info dp_info
;
434 /* Capture key info from ns struct */
435 zebra_dplane_info_from_zns(&dp_info
, zns
, false);
437 netlink_parse_info(netlink_information_fetch
, &zns
->netlink
, &dp_info
,
440 thread_add_read(zrouter
.master
, kernel_read
, zns
, zns
->netlink
.sock
,
445 * Called by the dplane pthread to read incoming OS messages and dispatch them.
447 int kernel_dplane_read(struct zebra_dplane_info
*info
)
449 struct nlsock
*nl
= kernel_netlink_nlsock_lookup(info
->sock
);
451 netlink_parse_info(dplane_netlink_information_fetch
, nl
, info
, 5,
458 * Filter out messages from self that occur on listener socket,
459 * caused by our actions on the command socket(s)
461 * When we add new Netlink message types we probably
462 * do not need to add them here as that we are filtering
463 * on the routes we actually care to receive( which is rarer
464 * then the normal course of operations). We are intentionally
465 * allowing some messages from ourselves through
466 * ( I'm looking at you Interface based netlink messages )
467 * so that we only have to write one way to handle incoming
468 * address add/delete and xxxNETCONF changes.
470 static void netlink_install_filter(int sock
, uint32_t pid
, uint32_t dplane_pid
)
473 * BPF_JUMP instructions and where you jump to are based upon
474 * 0 as being the next statement. So count from 0. Writing
475 * this down because every time I look at this I have to
478 struct sock_filter filter
[] = {
481 * if (nlmsg_pid == pid ||
482 * nlmsg_pid == dplane_pid) {
483 * if (the incoming nlmsg_type ==
484 * RTM_NEWADDR || RTM_DELADDR || RTM_NEWNETCONF ||
490 * keep this netlink message
493 * 0: Load the nlmsg_pid into the BPF register
495 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_W
,
496 offsetof(struct nlmsghdr
, nlmsg_pid
)),
500 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(pid
), 1, 0),
502 * 2: Compare to dplane pid
504 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htonl(dplane_pid
), 0, 6),
506 * 3: Load the nlmsg_type into BPF register
508 BPF_STMT(BPF_LD
| BPF_ABS
| BPF_H
,
509 offsetof(struct nlmsghdr
, nlmsg_type
)),
511 * 4: Compare to RTM_NEWADDR
513 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWADDR
), 4, 0),
515 * 5: Compare to RTM_DELADDR
517 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELADDR
), 3, 0),
519 * 6: Compare to RTM_NEWNETCONF
521 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_NEWNETCONF
), 2,
524 * 7: Compare to RTM_DELNETCONF
526 BPF_JUMP(BPF_JMP
| BPF_JEQ
| BPF_K
, htons(RTM_DELNETCONF
), 1,
529 * 8: This is the end state of we want to skip the
532 BPF_STMT(BPF_RET
| BPF_K
, 0),
533 /* 9: This is the end state of we want to keep
536 BPF_STMT(BPF_RET
| BPF_K
, 0xffff),
539 struct sock_fprog prog
= {
540 .len
= array_size(filter
), .filter
= filter
,
543 if (setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_FILTER
, &prog
, sizeof(prog
))
545 flog_err_sys(EC_LIB_SOCKET
, "Can't install socket filter: %s",
546 safe_strerror(errno
));
549 void netlink_parse_rtattr_flags(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
550 int len
, unsigned short flags
)
554 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
555 while (RTA_OK(rta
, len
)) {
556 type
= rta
->rta_type
& ~flags
;
557 if ((type
<= max
) && (!tb
[type
]))
559 rta
= RTA_NEXT(rta
, len
);
563 void netlink_parse_rtattr(struct rtattr
**tb
, int max
, struct rtattr
*rta
,
566 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
567 while (RTA_OK(rta
, len
)) {
568 if (rta
->rta_type
<= max
)
569 tb
[rta
->rta_type
] = rta
;
570 rta
= RTA_NEXT(rta
, len
);
575 * netlink_parse_rtattr_nested() - Parses a nested route attribute
576 * @tb: Pointer to array for storing rtattr in.
577 * @max: Max number to store.
578 * @rta: Pointer to rtattr to look for nested items in.
580 void netlink_parse_rtattr_nested(struct rtattr
**tb
, int max
,
583 netlink_parse_rtattr(tb
, max
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
586 bool nl_attr_put(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
587 const void *data
, unsigned int alen
)
592 len
= RTA_LENGTH(alen
);
594 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
597 rta
= (struct rtattr
*)(((char *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
598 rta
->rta_type
= type
;
602 memcpy(RTA_DATA(rta
), data
, alen
);
606 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
611 bool nl_attr_put8(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
614 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint8_t));
617 bool nl_attr_put16(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
620 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint16_t));
623 bool nl_attr_put32(struct nlmsghdr
*n
, unsigned int maxlen
, int type
,
626 return nl_attr_put(n
, maxlen
, type
, &data
, sizeof(uint32_t));
629 struct rtattr
*nl_attr_nest(struct nlmsghdr
*n
, unsigned int maxlen
, int type
)
631 struct rtattr
*nest
= NLMSG_TAIL(n
);
633 if (!nl_attr_put(n
, maxlen
, type
, NULL
, 0))
636 nest
->rta_type
|= NLA_F_NESTED
;
640 int nl_attr_nest_end(struct nlmsghdr
*n
, struct rtattr
*nest
)
642 nest
->rta_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)nest
;
646 struct rtnexthop
*nl_attr_rtnh(struct nlmsghdr
*n
, unsigned int maxlen
)
648 struct rtnexthop
*rtnh
= (struct rtnexthop
*)NLMSG_TAIL(n
);
650 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTNH_ALIGN(sizeof(struct rtnexthop
))
654 memset(rtnh
, 0, sizeof(struct rtnexthop
));
656 NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(sizeof(struct rtnexthop
));
661 void nl_attr_rtnh_end(struct nlmsghdr
*n
, struct rtnexthop
*rtnh
)
663 rtnh
->rtnh_len
= (uint8_t *)NLMSG_TAIL(n
) - (uint8_t *)rtnh
;
666 const char *nl_msg_type_to_str(uint16_t msg_type
)
668 return lookup_msg(nlmsg_str
, msg_type
, "");
671 const char *nl_rtproto_to_str(uint8_t rtproto
)
673 return lookup_msg(rtproto_str
, rtproto
, "");
676 const char *nl_family_to_str(uint8_t family
)
678 return lookup_msg(family_str
, family
, "");
681 const char *nl_rttype_to_str(uint8_t rttype
)
683 return lookup_msg(rttype_str
, rttype
, "");
686 #define NLA_OK(nla, len) \
687 ((len) >= (int)sizeof(struct nlattr) \
688 && (nla)->nla_len >= sizeof(struct nlattr) \
689 && (nla)->nla_len <= (len))
690 #define NLA_NEXT(nla, attrlen) \
691 ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
692 (struct nlattr *)(((char *)(nla)) + NLA_ALIGN((nla)->nla_len)))
693 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
694 #define NLA_DATA(nla) ((struct nlattr *)(((char *)(nla)) + NLA_LENGTH(0)))
696 #define ERR_NLA(err, inner_len) \
697 ((struct nlattr *)(((char *)(err)) \
698 + NLMSG_ALIGN(sizeof(struct nlmsgerr)) \
699 + NLMSG_ALIGN((inner_len))))
701 static void netlink_parse_nlattr(struct nlattr
**tb
, int max
,
702 struct nlattr
*nla
, int len
)
704 while (NLA_OK(nla
, len
)) {
705 if (nla
->nla_type
<= max
)
706 tb
[nla
->nla_type
] = nla
;
707 nla
= NLA_NEXT(nla
, len
);
711 static void netlink_parse_extended_ack(struct nlmsghdr
*h
)
713 struct nlattr
*tb
[NLMSGERR_ATTR_MAX
+ 1] = {};
714 const struct nlmsgerr
*err
= (const struct nlmsgerr
*)NLMSG_DATA(h
);
715 const struct nlmsghdr
*err_nlh
= NULL
;
716 /* Length not including nlmsghdr */
718 /* Inner error netlink message length */
719 uint32_t inner_len
= 0;
720 const char *msg
= NULL
;
723 if (!(h
->nlmsg_flags
& NLM_F_CAPPED
))
724 inner_len
= (uint32_t)NLMSG_PAYLOAD(&err
->msg
, 0);
726 len
= (uint32_t)(NLMSG_PAYLOAD(h
, sizeof(struct nlmsgerr
)) - inner_len
);
728 netlink_parse_nlattr(tb
, NLMSGERR_ATTR_MAX
, ERR_NLA(err
, inner_len
),
731 if (tb
[NLMSGERR_ATTR_MSG
])
732 msg
= (const char *)NLA_DATA(tb
[NLMSGERR_ATTR_MSG
]);
734 if (tb
[NLMSGERR_ATTR_OFFS
]) {
735 off
= *(uint32_t *)NLA_DATA(tb
[NLMSGERR_ATTR_OFFS
]);
737 if (off
> h
->nlmsg_len
) {
738 zlog_err("Invalid offset for NLMSGERR_ATTR_OFFS");
739 } else if (!(h
->nlmsg_flags
& NLM_F_CAPPED
)) {
741 * Header of failed message
742 * we are not doing anything currently with it
743 * but noticing it for later.
746 zlog_debug("%s: Received %s extended Ack", __func__
,
747 nl_msg_type_to_str(err_nlh
->nlmsg_type
));
751 if (msg
&& *msg
!= '\0') {
752 bool is_err
= !!err
->error
;
755 zlog_err("Extended Error: %s", msg
);
757 flog_warn(EC_ZEBRA_NETLINK_EXTENDED_WARNING
,
758 "Extended Warning: %s", msg
);
763 * netlink_send_msg - send a netlink message of a certain size.
765 * Returns -1 on error. Otherwise, it returns the number of bytes sent.
767 static ssize_t
netlink_send_msg(const struct nlsock
*nl
, void *buf
,
770 struct sockaddr_nl snl
= {};
771 struct iovec iov
= {};
772 struct msghdr msg
= {};
777 iov
.iov_len
= buflen
;
779 msg
.msg_namelen
= sizeof(snl
);
783 snl
.nl_family
= AF_NETLINK
;
785 /* Send message to netlink interface. */
786 frr_with_privs(&zserv_privs
) {
787 status
= sendmsg(nl
->sock
, &msg
, 0);
791 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND
) {
792 zlog_debug("%s: >> netlink message dump [sent]", __func__
);
794 nl_dump(buf
, buflen
);
796 zlog_hexdump(buf
, buflen
);
797 #endif /* NETLINK_DEBUG */
801 flog_err_sys(EC_LIB_SOCKET
, "%s error: %s", __func__
,
802 safe_strerror(save_errno
));
810 * netlink_recv_msg - receive a netlink message.
812 * Returns -1 on error, 0 if read would block or the number of bytes received.
814 static int netlink_recv_msg(struct nlsock
*nl
, struct msghdr
*msg
)
819 iov
.iov_base
= nl
->buf
;
820 iov
.iov_len
= nl
->buflen
;
827 bytes
= recv(nl
->sock
, NULL
, 0, MSG_PEEK
| MSG_TRUNC
);
829 if (bytes
>= 0 && (size_t)bytes
> nl
->buflen
) {
830 nl
->buf
= XREALLOC(MTYPE_NL_BUF
, nl
->buf
, bytes
);
832 iov
.iov_base
= nl
->buf
;
833 iov
.iov_len
= nl
->buflen
;
836 status
= recvmsg(nl
->sock
, msg
, 0);
837 } while (status
== -1 && errno
== EINTR
);
840 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
842 flog_err(EC_ZEBRA_RECVMSG_OVERRUN
, "%s recvmsg overrun: %s",
843 nl
->name
, safe_strerror(errno
));
845 * In this case we are screwed. There is no good way to recover
846 * zebra at this point.
852 flog_err_sys(EC_LIB_SOCKET
, "%s EOF", nl
->name
);
856 if (msg
->msg_namelen
!= sizeof(struct sockaddr_nl
)) {
857 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
858 "%s sender address length error: length %d", nl
->name
,
863 if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_RECV
) {
864 zlog_debug("%s: << netlink message dump [recv]", __func__
);
866 nl_dump(nl
->buf
, status
);
868 zlog_hexdump(nl
->buf
, status
);
869 #endif /* NETLINK_DEBUG */
876 * netlink_parse_error - parse a netlink error message
878 * Returns 1 if this message is acknowledgement, 0 if this error should be
879 * ignored, -1 otherwise.
881 static int netlink_parse_error(const struct nlsock
*nl
, struct nlmsghdr
*h
,
882 bool is_cmd
, bool startup
)
884 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(h
);
885 int errnum
= err
->error
;
886 int msg_type
= err
->msg
.nlmsg_type
;
888 if (h
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct nlmsgerr
))) {
889 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
890 "%s error: message truncated", nl
->name
);
895 * Parse the extended information before we actually handle it. At this
896 * point in time we do not do anything other than report the issue.
898 if (h
->nlmsg_flags
& NLM_F_ACK_TLVS
)
899 netlink_parse_extended_ack(h
);
901 /* If the error field is zero, then this is an ACK. */
902 if (err
->error
== 0) {
903 if (IS_ZEBRA_DEBUG_KERNEL
) {
904 zlog_debug("%s: %s ACK: type=%s(%u), seq=%u, pid=%u",
906 nl_msg_type_to_str(err
->msg
.nlmsg_type
),
907 err
->msg
.nlmsg_type
, err
->msg
.nlmsg_seq
,
914 /* Deal with errors that occur because of races in link handling. */
916 && ((msg_type
== RTM_DELROUTE
917 && (-errnum
== ENODEV
|| -errnum
== ESRCH
))
918 || (msg_type
== RTM_NEWROUTE
919 && (-errnum
== ENETDOWN
|| -errnum
== EEXIST
)))) {
920 if (IS_ZEBRA_DEBUG_KERNEL
)
921 zlog_debug("%s: error: %s type=%s(%u), seq=%u, pid=%u",
922 nl
->name
, safe_strerror(-errnum
),
923 nl_msg_type_to_str(msg_type
), msg_type
,
924 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
929 * We see RTM_DELNEIGH when shutting down an interface with an IPv4
930 * link-local. The kernel should have already deleted the neighbor so
931 * do not log these as an error.
933 if (msg_type
== RTM_DELNEIGH
934 || (is_cmd
&& msg_type
== RTM_NEWROUTE
935 && (-errnum
== ESRCH
|| -errnum
== ENETUNREACH
))) {
937 * This is known to happen in some situations, don't log as
940 if (IS_ZEBRA_DEBUG_KERNEL
)
941 zlog_debug("%s error: %s, type=%s(%u), seq=%u, pid=%u",
942 nl
->name
, safe_strerror(-errnum
),
943 nl_msg_type_to_str(msg_type
), msg_type
,
944 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
946 if ((msg_type
!= RTM_GETNEXTHOP
) || !startup
)
947 flog_err(EC_ZEBRA_UNEXPECTED_MESSAGE
,
948 "%s error: %s, type=%s(%u), seq=%u, pid=%u",
949 nl
->name
, safe_strerror(-errnum
),
950 nl_msg_type_to_str(msg_type
), msg_type
,
951 err
->msg
.nlmsg_seq
, err
->msg
.nlmsg_pid
);
960 * Receive message from netlink interface and pass those information
961 * to the given function.
963 * filter -> Function to call to read the results
964 * nl -> netlink socket information
965 * zns -> The zebra namespace data
966 * count -> How many we should read in, 0 means as much as possible
967 * startup -> Are we reading in under startup conditions? passed to
970 int netlink_parse_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int),
971 struct nlsock
*nl
, const struct zebra_dplane_info
*zns
,
972 int count
, bool startup
)
980 struct sockaddr_nl snl
;
981 struct msghdr msg
= {.msg_name
= (void *)&snl
,
982 .msg_namelen
= sizeof(snl
)};
985 if (count
&& read_in
>= count
)
988 status
= netlink_recv_msg(nl
, &msg
);
991 else if (status
== 0)
995 for (h
= (struct nlmsghdr
*)nl
->buf
;
996 (status
>= 0 && NLMSG_OK(h
, (unsigned int)status
));
997 h
= NLMSG_NEXT(h
, status
)) {
998 /* Finish of reading. */
999 if (h
->nlmsg_type
== NLMSG_DONE
)
1002 /* Error handling. */
1003 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1004 int err
= netlink_parse_error(
1005 nl
, h
, zns
->is_cmd
, startup
);
1008 if (!(h
->nlmsg_flags
& NLM_F_MULTI
))
1015 /* OK we got netlink message. */
1016 if (IS_ZEBRA_DEBUG_KERNEL
)
1018 "%s: %s type %s(%u), len=%d, seq=%u, pid=%u",
1020 nl_msg_type_to_str(h
->nlmsg_type
),
1021 h
->nlmsg_type
, h
->nlmsg_len
,
1022 h
->nlmsg_seq
, h
->nlmsg_pid
);
1026 * Ignore messages that maybe sent from
1027 * other actors besides the kernel
1029 if (snl
.nl_pid
!= 0) {
1030 zlog_debug("Ignoring message from pid %u",
1035 error
= (*filter
)(h
, zns
->ns_id
, startup
);
1037 zlog_debug("%s filter function error",
1043 /* After error care. */
1044 if (msg
.msg_flags
& MSG_TRUNC
) {
1045 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1046 "%s error: message truncated", nl
->name
);
1050 flog_err(EC_ZEBRA_NETLINK_LENGTH_ERROR
,
1051 "%s error: data remnant size %d", nl
->name
,
1062 * sendmsg() to netlink socket then recvmsg().
1063 * Calls netlink_parse_info to parse returned data
1065 * filter -> The filter to read final results from kernel
1066 * nlmsghdr -> The data to send to the kernel
1067 * dp_info -> The dataplane and netlink socket information
1068 * startup -> Are we reading in under startup conditions
1069 * This is passed through eventually to filter.
1071 static int netlink_talk_info(int (*filter
)(struct nlmsghdr
*, ns_id_t
,
1074 struct zebra_dplane_info
*dp_info
, bool startup
)
1078 nl
= kernel_netlink_nlsock_lookup(dp_info
->sock
);
1079 n
->nlmsg_seq
= dp_info
->seq
;
1080 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1082 if (IS_ZEBRA_DEBUG_KERNEL
)
1084 "netlink_talk: %s type %s(%u), len=%d seq=%u flags 0x%x",
1085 nl
->name
, nl_msg_type_to_str(n
->nlmsg_type
),
1086 n
->nlmsg_type
, n
->nlmsg_len
, n
->nlmsg_seq
,
1089 if (netlink_send_msg(nl
, n
, n
->nlmsg_len
) == -1)
1093 * Get reply from netlink socket.
1094 * The reply should either be an acknowlegement or an error.
1096 return netlink_parse_info(filter
, nl
, dp_info
, 0, startup
);
1100 * Synchronous version of netlink_talk_info. Converts args to suit the
1101 * common version, which is suitable for both sync and async use.
1103 int netlink_talk(int (*filter
)(struct nlmsghdr
*, ns_id_t
, int startup
),
1104 struct nlmsghdr
*n
, struct nlsock
*nl
, struct zebra_ns
*zns
,
1107 struct zebra_dplane_info dp_info
;
1109 /* Increment sequence number before capturing snapshot of ns socket
1114 /* Capture info in intermediate info struct */
1115 zebra_dplane_info_from_zns(&dp_info
, zns
, (nl
== &(zns
->netlink_cmd
)));
1117 return netlink_talk_info(filter
, n
, &dp_info
, startup
);
1120 /* Issue request message to kernel via netlink socket. GET messages
1121 * are issued through this interface.
1123 int netlink_request(struct nlsock
*nl
, void *req
)
1125 struct nlmsghdr
*n
= (struct nlmsghdr
*)req
;
1127 /* Check netlink socket. */
1129 flog_err_sys(EC_LIB_SOCKET
, "%s socket isn't active.",
1134 /* Fill common fields for all requests. */
1135 n
->nlmsg_pid
= nl
->snl
.nl_pid
;
1136 n
->nlmsg_seq
= ++nl
->seq
;
1138 if (netlink_send_msg(nl
, req
, n
->nlmsg_len
) == -1)
1144 static int nl_batch_read_resp(struct nl_batch
*bth
)
1147 struct sockaddr_nl snl
;
1148 struct msghdr msg
= {};
1151 struct zebra_dplane_ctx
*ctx
;
1154 nl
= kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1156 msg
.msg_name
= (void *)&snl
;
1157 msg
.msg_namelen
= sizeof(snl
);
1160 * The responses are not batched, so we need to read and process one
1161 * message at a time.
1164 status
= netlink_recv_msg(nl
, &msg
);
1166 * status == -1 is a full on failure somewhere
1167 * since we don't know where the problem happened
1168 * we must mark all as failed
1170 * Else we mark everything as worked
1173 if (status
== -1 || status
== 0) {
1174 while ((ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
))) !=
1177 dplane_ctx_set_status(
1179 ZEBRA_DPLANE_REQUEST_FAILURE
);
1180 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1185 h
= (struct nlmsghdr
*)nl
->buf
;
1189 * Find the corresponding context object. Received responses are
1190 * in the same order as requests we sent, so we can simply
1191 * iterate over the context list and match responses with
1192 * requests at same time.
1195 ctx
= dplane_ctx_get_head(&(bth
->ctx_list
));
1198 * This is a situation where we have gotten
1199 * into a bad spot. We need to know that
1200 * this happens( does it? )
1203 "%s:WARNING Received netlink Response for an error and no Contexts to associate with it",
1209 * 'update' context objects take two consecutive
1212 if (dplane_ctx_is_update(ctx
) &&
1213 dplane_ctx_get_ns(ctx
)->seq
+ 1 == seq
) {
1215 * This is the situation where we get a response
1216 * to a message that should be ignored.
1222 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1223 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1225 /* We have found corresponding context object. */
1226 if (dplane_ctx_get_ns(ctx
)->seq
== seq
)
1229 if (dplane_ctx_get_ns(ctx
)->seq
> seq
)
1231 "%s:WARNING Received %u is less than any context on the queue ctx->seq %u",
1233 dplane_ctx_get_ns(ctx
)->seq
);
1238 * If we ignore the message due to an update
1239 * above we should still fricking decode the
1240 * message for our operator to understand
1243 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1246 zlog_debug("%s: netlink error message seq=%d %d",
1247 __func__
, h
->nlmsg_seq
, err
);
1252 * We received a message with the sequence number that isn't
1253 * associated with any dplane context object.
1256 if (IS_ZEBRA_DEBUG_KERNEL
)
1258 "%s: skipping unassociated response, seq number %d NS %u",
1259 __func__
, h
->nlmsg_seq
,
1264 if (h
->nlmsg_type
== NLMSG_ERROR
) {
1265 int err
= netlink_parse_error(nl
, h
, bth
->zns
->is_cmd
,
1269 dplane_ctx_set_status(
1270 ctx
, ZEBRA_DPLANE_REQUEST_FAILURE
);
1272 if (IS_ZEBRA_DEBUG_KERNEL
)
1273 zlog_debug("%s: netlink error message seq=%d ",
1274 __func__
, h
->nlmsg_seq
);
1279 * If we get here then we did not receive neither the ack nor
1280 * the error and instead received some other message in an
1283 if (IS_ZEBRA_DEBUG_KERNEL
)
1284 zlog_debug("%s: ignoring message type 0x%04x(%s) NS %u",
1285 __func__
, h
->nlmsg_type
,
1286 nl_msg_type_to_str(h
->nlmsg_type
),
1293 static void nl_batch_reset(struct nl_batch
*bth
)
1295 bth
->buf_head
= bth
->buf
;
1300 TAILQ_INIT(&(bth
->ctx_list
));
1303 static void nl_batch_init(struct nl_batch
*bth
, struct dplane_ctx_q
*ctx_out_q
)
1306 * If the size of the buffer has changed, free and then allocate a new
1310 atomic_load_explicit(&nl_batch_bufsize
, memory_order_relaxed
);
1311 if (bufsize
!= nl_batch_tx_bufsize
) {
1312 if (nl_batch_tx_buf
)
1313 XFREE(MTYPE_NL_BUF
, nl_batch_tx_buf
);
1315 nl_batch_tx_buf
= XCALLOC(MTYPE_NL_BUF
, bufsize
);
1316 nl_batch_tx_bufsize
= bufsize
;
1319 bth
->buf
= nl_batch_tx_buf
;
1320 bth
->bufsiz
= bufsize
;
1321 bth
->limit
= atomic_load_explicit(&nl_batch_send_threshold
,
1322 memory_order_relaxed
);
1324 bth
->ctx_out_q
= ctx_out_q
;
1326 nl_batch_reset(bth
);
1329 static void nl_batch_send(struct nl_batch
*bth
)
1331 struct zebra_dplane_ctx
*ctx
;
1334 if (bth
->curlen
!= 0 && bth
->zns
!= NULL
) {
1336 kernel_netlink_nlsock_lookup(bth
->zns
->sock
);
1338 if (IS_ZEBRA_DEBUG_KERNEL
)
1339 zlog_debug("%s: %s, batch size=%zu, msg cnt=%zu",
1340 __func__
, nl
->name
, bth
->curlen
,
1343 if (netlink_send_msg(nl
, bth
->buf
, bth
->curlen
) == -1)
1347 if (nl_batch_read_resp(bth
) == -1)
1352 /* Move remaining contexts to the outbound queue. */
1354 ctx
= dplane_ctx_dequeue(&(bth
->ctx_list
));
1359 dplane_ctx_set_status(ctx
,
1360 ZEBRA_DPLANE_REQUEST_FAILURE
);
1362 dplane_ctx_enqueue_tail(bth
->ctx_out_q
, ctx
);
1365 nl_batch_reset(bth
);
1368 enum netlink_msg_status
netlink_batch_add_msg(
1369 struct nl_batch
*bth
, struct zebra_dplane_ctx
*ctx
,
1370 ssize_t (*msg_encoder
)(struct zebra_dplane_ctx
*, void *, size_t),
1375 struct nlmsghdr
*msgh
;
1378 size
= (*msg_encoder
)(ctx
, bth
->buf_head
, bth
->bufsiz
- bth
->curlen
);
1381 * If there was an error while encoding the message (other than buffer
1382 * overflow) then return an error.
1385 return FRR_NETLINK_ERROR
;
1388 * If the message doesn't fit entirely in the buffer then send the batch
1393 size
= (*msg_encoder
)(ctx
, bth
->buf_head
,
1394 bth
->bufsiz
- bth
->curlen
);
1396 * If the message doesn't fit in the empty buffer then just
1400 return FRR_NETLINK_ERROR
;
1403 seq
= dplane_ctx_get_ns(ctx
)->seq
;
1404 nl
= kernel_netlink_nlsock_lookup(dplane_ctx_get_ns_sock(ctx
));
1409 msgh
= (struct nlmsghdr
*)bth
->buf_head
;
1410 msgh
->nlmsg_seq
= seq
;
1411 msgh
->nlmsg_pid
= nl
->snl
.nl_pid
;
1413 bth
->zns
= dplane_ctx_get_ns(ctx
);
1414 bth
->buf_head
= ((char *)bth
->buf_head
) + size
;
1415 bth
->curlen
+= size
;
1418 return FRR_NETLINK_QUEUED
;
1421 static enum netlink_msg_status
nl_put_msg(struct nl_batch
*bth
,
1422 struct zebra_dplane_ctx
*ctx
)
1424 if (dplane_ctx_is_skip_kernel(ctx
))
1425 return FRR_NETLINK_SUCCESS
;
1427 switch (dplane_ctx_get_op(ctx
)) {
1429 case DPLANE_OP_ROUTE_INSTALL
:
1430 case DPLANE_OP_ROUTE_UPDATE
:
1431 case DPLANE_OP_ROUTE_DELETE
:
1432 return netlink_put_route_update_msg(bth
, ctx
);
1434 case DPLANE_OP_NH_INSTALL
:
1435 case DPLANE_OP_NH_UPDATE
:
1436 case DPLANE_OP_NH_DELETE
:
1437 return netlink_put_nexthop_update_msg(bth
, ctx
);
1439 case DPLANE_OP_LSP_INSTALL
:
1440 case DPLANE_OP_LSP_UPDATE
:
1441 case DPLANE_OP_LSP_DELETE
:
1442 return netlink_put_lsp_update_msg(bth
, ctx
);
1444 case DPLANE_OP_PW_INSTALL
:
1445 case DPLANE_OP_PW_UNINSTALL
:
1446 return netlink_put_pw_update_msg(bth
, ctx
);
1448 case DPLANE_OP_ADDR_INSTALL
:
1449 case DPLANE_OP_ADDR_UNINSTALL
:
1450 return netlink_put_address_update_msg(bth
, ctx
);
1452 case DPLANE_OP_MAC_INSTALL
:
1453 case DPLANE_OP_MAC_DELETE
:
1454 return netlink_put_mac_update_msg(bth
, ctx
);
1456 case DPLANE_OP_NEIGH_INSTALL
:
1457 case DPLANE_OP_NEIGH_UPDATE
:
1458 case DPLANE_OP_NEIGH_DELETE
:
1459 case DPLANE_OP_VTEP_ADD
:
1460 case DPLANE_OP_VTEP_DELETE
:
1461 case DPLANE_OP_NEIGH_DISCOVER
:
1462 case DPLANE_OP_NEIGH_IP_INSTALL
:
1463 case DPLANE_OP_NEIGH_IP_DELETE
:
1464 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
1465 return netlink_put_neigh_update_msg(bth
, ctx
);
1467 case DPLANE_OP_RULE_ADD
:
1468 case DPLANE_OP_RULE_DELETE
:
1469 case DPLANE_OP_RULE_UPDATE
:
1470 return netlink_put_rule_update_msg(bth
, ctx
);
1472 case DPLANE_OP_SYS_ROUTE_ADD
:
1473 case DPLANE_OP_SYS_ROUTE_DELETE
:
1474 case DPLANE_OP_ROUTE_NOTIFY
:
1475 case DPLANE_OP_LSP_NOTIFY
:
1476 case DPLANE_OP_BR_PORT_UPDATE
:
1477 return FRR_NETLINK_SUCCESS
;
1479 case DPLANE_OP_IPTABLE_ADD
:
1480 case DPLANE_OP_IPTABLE_DELETE
:
1481 case DPLANE_OP_IPSET_ADD
:
1482 case DPLANE_OP_IPSET_DELETE
:
1483 case DPLANE_OP_IPSET_ENTRY_ADD
:
1484 case DPLANE_OP_IPSET_ENTRY_DELETE
:
1485 return FRR_NETLINK_ERROR
;
1487 case DPLANE_OP_GRE_SET
:
1488 return netlink_put_gre_set_msg(bth
, ctx
);
1490 case DPLANE_OP_INTF_ADDR_ADD
:
1491 case DPLANE_OP_INTF_ADDR_DEL
:
1492 case DPLANE_OP_INTF_NETCONFIG
:
1493 case DPLANE_OP_NONE
:
1494 return FRR_NETLINK_ERROR
;
1496 case DPLANE_OP_INTF_INSTALL
:
1497 case DPLANE_OP_INTF_UPDATE
:
1498 case DPLANE_OP_INTF_DELETE
:
1499 return netlink_put_intf_update_msg(bth
, ctx
);
1502 return FRR_NETLINK_ERROR
;
1505 void kernel_update_multi(struct dplane_ctx_q
*ctx_list
)
1507 struct nl_batch batch
;
1508 struct zebra_dplane_ctx
*ctx
;
1509 struct dplane_ctx_q handled_list
;
1510 enum netlink_msg_status res
;
1512 TAILQ_INIT(&handled_list
);
1513 nl_batch_init(&batch
, &handled_list
);
1516 ctx
= dplane_ctx_dequeue(ctx_list
);
1520 if (batch
.zns
!= NULL
1521 && batch
.zns
->ns_id
!= dplane_ctx_get_ns(ctx
)->ns_id
)
1522 nl_batch_send(&batch
);
1525 * Assume all messages will succeed and then mark only the ones
1528 dplane_ctx_set_status(ctx
, ZEBRA_DPLANE_REQUEST_SUCCESS
);
1530 res
= nl_put_msg(&batch
, ctx
);
1532 dplane_ctx_enqueue_tail(&(batch
.ctx_list
), ctx
);
1533 if (res
== FRR_NETLINK_ERROR
)
1534 dplane_ctx_set_status(ctx
,
1535 ZEBRA_DPLANE_REQUEST_FAILURE
);
1537 if (batch
.curlen
> batch
.limit
)
1538 nl_batch_send(&batch
);
1541 nl_batch_send(&batch
);
1543 TAILQ_INIT(ctx_list
);
1544 dplane_ctx_list_append(ctx_list
, &handled_list
);
1547 struct nlsock
*kernel_netlink_nlsock_lookup(int sock
)
1549 struct nlsock lookup
, *retval
;
1554 retval
= hash_lookup(nlsock_hash
, &lookup
);
1560 /* Insert nlsock entry into hash */
1561 static void kernel_netlink_nlsock_insert(struct nlsock
*nls
)
1564 (void)hash_get(nlsock_hash
, nls
, hash_alloc_intern
);
1568 /* Remove nlsock entry from hash */
1569 static void kernel_netlink_nlsock_remove(struct nlsock
*nls
)
1572 (void)hash_release(nlsock_hash
, nls
);
1576 static uint32_t kernel_netlink_nlsock_key(const void *arg
)
1578 const struct nlsock
*nl
= arg
;
1583 static bool kernel_netlink_nlsock_hash_equal(const void *arg1
, const void *arg2
)
1585 const struct nlsock
*nl1
= arg1
;
1586 const struct nlsock
*nl2
= arg2
;
1588 if (nl1
->sock
== nl2
->sock
)
1594 /* Exported interface function. This function simply calls
1595 netlink_socket (). */
1596 void kernel_init(struct zebra_ns
*zns
)
1598 uint32_t groups
, dplane_groups
;
1599 #if defined SOL_NETLINK
1604 * Initialize netlink sockets
1606 * If RTMGRP_XXX exists use that, but at some point
1607 * I think the kernel developers realized that
1608 * keeping track of all the different values would
1609 * lead to confusion, so we need to convert the
1610 * RTNLGRP_XXX to a bit position for ourself
1612 groups
= RTMGRP_LINK
|
1614 RTMGRP_IPV4_IFADDR
|
1616 RTMGRP_IPV6_IFADDR
|
1617 RTMGRP_IPV4_MROUTE
|
1619 ((uint32_t) 1 << (RTNLGRP_IPV4_RULE
- 1)) |
1620 ((uint32_t) 1 << (RTNLGRP_IPV6_RULE
- 1)) |
1621 ((uint32_t) 1 << (RTNLGRP_NEXTHOP
- 1));
1623 dplane_groups
= (RTMGRP_LINK
|
1624 RTMGRP_IPV4_IFADDR
|
1625 RTMGRP_IPV6_IFADDR
|
1626 ((uint32_t) 1 << (RTNLGRP_IPV4_NETCONF
- 1)) |
1627 ((uint32_t) 1 << (RTNLGRP_IPV6_NETCONF
- 1)) |
1628 ((uint32_t) 1 << (RTNLGRP_MPLS_NETCONF
- 1)));
1631 snprintf(zns
->netlink
.name
, sizeof(zns
->netlink
.name
),
1632 "netlink-listen (NS %u)", zns
->ns_id
);
1633 zns
->netlink
.sock
= -1;
1634 if (netlink_socket(&zns
->netlink
, groups
, zns
->ns_id
) < 0) {
1635 zlog_err("Failure to create %s socket",
1640 kernel_netlink_nlsock_insert(&zns
->netlink
);
1642 snprintf(zns
->netlink_cmd
.name
, sizeof(zns
->netlink_cmd
.name
),
1643 "netlink-cmd (NS %u)", zns
->ns_id
);
1644 zns
->netlink_cmd
.sock
= -1;
1645 if (netlink_socket(&zns
->netlink_cmd
, 0, zns
->ns_id
) < 0) {
1646 zlog_err("Failure to create %s socket",
1647 zns
->netlink_cmd
.name
);
1651 kernel_netlink_nlsock_insert(&zns
->netlink_cmd
);
1653 /* Outbound socket for dplane programming of the host OS. */
1654 snprintf(zns
->netlink_dplane_out
.name
,
1655 sizeof(zns
->netlink_dplane_out
.name
), "netlink-dp (NS %u)",
1657 zns
->netlink_dplane_out
.sock
= -1;
1658 if (netlink_socket(&zns
->netlink_dplane_out
, 0, zns
->ns_id
) < 0) {
1659 zlog_err("Failure to create %s socket",
1660 zns
->netlink_dplane_out
.name
);
1664 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_out
);
1666 /* Inbound socket for OS events coming to the dplane. */
1667 snprintf(zns
->netlink_dplane_in
.name
,
1668 sizeof(zns
->netlink_dplane_in
.name
), "netlink-dp-in (NS %u)",
1670 zns
->netlink_dplane_in
.sock
= -1;
1671 if (netlink_socket(&zns
->netlink_dplane_in
, dplane_groups
,
1673 zlog_err("Failure to create %s socket",
1674 zns
->netlink_dplane_in
.name
);
1678 kernel_netlink_nlsock_insert(&zns
->netlink_dplane_in
);
1681 * SOL_NETLINK is not available on all platforms yet
1682 * apparently. It's in bits/socket.h which I am not
1683 * sure that we want to pull into our build system.
1685 #if defined SOL_NETLINK
1687 * Let's tell the kernel that we want to receive extended
1688 * ACKS over our command socket(s)
1691 ret
= setsockopt(zns
->netlink_cmd
.sock
, SOL_NETLINK
, NETLINK_EXT_ACK
,
1695 zlog_notice("Registration for extended cmd ACK failed : %d %s",
1696 errno
, safe_strerror(errno
));
1699 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1700 NETLINK_EXT_ACK
, &one
, sizeof(one
));
1703 zlog_notice("Registration for extended dp ACK failed : %d %s",
1704 errno
, safe_strerror(errno
));
1707 * Trim off the payload of the original netlink message in the
1708 * acknowledgment. This option is available since Linux 4.2, so if
1709 * setsockopt fails, ignore the error.
1712 ret
= setsockopt(zns
->netlink_dplane_out
.sock
, SOL_NETLINK
,
1713 NETLINK_CAP_ACK
, &one
, sizeof(one
));
1716 "Registration for reduced ACK packet size failed, probably running an early kernel");
1719 /* Register kernel socket. */
1720 if (fcntl(zns
->netlink
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1721 flog_err_sys(EC_LIB_SOCKET
, "Can't set %s socket flags: %s",
1722 zns
->netlink
.name
, safe_strerror(errno
));
1724 if (fcntl(zns
->netlink_cmd
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1725 zlog_err("Can't set %s socket error: %s(%d)",
1726 zns
->netlink_cmd
.name
, safe_strerror(errno
), errno
);
1728 if (fcntl(zns
->netlink_dplane_out
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1729 zlog_err("Can't set %s socket error: %s(%d)",
1730 zns
->netlink_dplane_out
.name
, safe_strerror(errno
),
1733 if (fcntl(zns
->netlink_dplane_in
.sock
, F_SETFL
, O_NONBLOCK
) < 0)
1734 zlog_err("Can't set %s socket error: %s(%d)",
1735 zns
->netlink_dplane_in
.name
, safe_strerror(errno
),
1738 /* Set receive buffer size if it's set from command line */
1740 netlink_recvbuf(&zns
->netlink
, rcvbufsize
);
1741 netlink_recvbuf(&zns
->netlink_cmd
, rcvbufsize
);
1742 netlink_recvbuf(&zns
->netlink_dplane_out
, rcvbufsize
);
1743 netlink_recvbuf(&zns
->netlink_dplane_in
, rcvbufsize
);
1746 /* Set filter for inbound sockets, to exclude events we've generated
1749 netlink_install_filter(zns
->netlink
.sock
, zns
->netlink_cmd
.snl
.nl_pid
,
1750 zns
->netlink_dplane_out
.snl
.nl_pid
);
1752 netlink_install_filter(zns
->netlink_dplane_in
.sock
,
1753 zns
->netlink_cmd
.snl
.nl_pid
,
1754 zns
->netlink_dplane_out
.snl
.nl_pid
);
1756 zns
->t_netlink
= NULL
;
1758 thread_add_read(zrouter
.master
, kernel_read
, zns
,
1759 zns
->netlink
.sock
, &zns
->t_netlink
);
1764 /* Helper to clean up an nlsock */
1765 static void kernel_nlsock_fini(struct nlsock
*nls
)
1767 if (nls
&& nls
->sock
>= 0) {
1768 kernel_netlink_nlsock_remove(nls
);
1771 XFREE(MTYPE_NL_BUF
, nls
->buf
);
1776 void kernel_terminate(struct zebra_ns
*zns
, bool complete
)
1778 thread_cancel(&zns
->t_netlink
);
1780 kernel_nlsock_fini(&zns
->netlink
);
1782 kernel_nlsock_fini(&zns
->netlink_cmd
);
1784 kernel_nlsock_fini(&zns
->netlink_dplane_in
);
1786 /* During zebra shutdown, we need to leave the dataplane socket
1787 * around until all work is done.
1790 kernel_nlsock_fini(&zns
->netlink_dplane_out
);
1794 * Global init for platform-/OS-specific things
1796 void kernel_router_init(void)
1798 /* Init nlsock hash and lock */
1799 pthread_mutex_init(&nlsock_mutex
, NULL
);
1800 nlsock_hash
= hash_create_size(8, kernel_netlink_nlsock_key
,
1801 kernel_netlink_nlsock_hash_equal
,
1802 "Netlink Socket Hash");
1806 * Global deinit for platform-/OS-specific things
1808 void kernel_router_terminate(void)
1810 pthread_mutex_destroy(&nlsock_mutex
);
1812 hash_free(nlsock_hash
);
1816 #endif /* HAVE_NETLINK */