1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
41 #include "lib_errors.h"
43 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
44 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
46 /* Zebra client events. */
47 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
49 /* Prototype for event manager. */
50 static void zclient_event(enum event
, struct zclient
*);
52 struct zclient_options zclient_options_default
= {.receive_notify
= false};
54 struct sockaddr_storage zclient_addr
;
55 socklen_t zclient_addr_len
;
57 /* This file local debug flag. */
58 static int zclient_debug
;
60 /* Allocate zclient structure. */
61 struct zclient
*zclient_new(struct thread_master
*master
,
62 struct zclient_options
*opt
)
64 struct zclient
*zclient
;
65 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
67 zclient
->ibuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
68 zclient
->obuf
= stream_new(ZEBRA_MAX_PACKET_SIZ
);
69 zclient
->wb
= buffer_new(0);
70 zclient
->master
= master
;
72 zclient
->receive_notify
= opt
->receive_notify
;
77 /* This function is only called when exiting, because
78 many parts of the code do not check for I/O errors, so they could
79 reference an invalid pointer if the structure was ever freed.
81 Free zclient structure. */
82 void zclient_free(struct zclient
*zclient
)
85 stream_free(zclient
->ibuf
);
87 stream_free(zclient
->obuf
);
89 buffer_free(zclient
->wb
);
91 XFREE(MTYPE_ZCLIENT
, zclient
);
94 unsigned short *redist_check_instance(struct redist_proto
*red
,
95 unsigned short instance
)
97 struct listnode
*node
;
103 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
110 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
117 red
->instances
= list_new();
119 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
121 listnode_add(red
->instances
, in
);
124 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
128 id
= redist_check_instance(red
, instance
);
132 listnode_delete(red
->instances
, id
);
133 XFREE(MTYPE_REDIST_INST
, id
);
134 if (!red
->instances
->count
) {
136 list_delete(&red
->instances
);
140 /* Stop zebra client services. */
141 void zclient_stop(struct zclient
*zclient
)
147 zlog_debug("zclient stopped");
150 THREAD_OFF(zclient
->t_read
);
151 THREAD_OFF(zclient
->t_connect
);
152 THREAD_OFF(zclient
->t_write
);
155 stream_reset(zclient
->ibuf
);
156 stream_reset(zclient
->obuf
);
158 /* Empty the write buffer. */
159 buffer_reset(zclient
->wb
);
162 if (zclient
->sock
>= 0) {
163 close(zclient
->sock
);
168 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
169 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
170 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
171 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
174 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
177 vrf_bitmap_free(zclient
->default_information
[afi
]);
178 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
182 void zclient_reset(struct zclient
*zclient
)
186 zclient_stop(zclient
);
188 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
190 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
193 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
198 * Connect to zebra daemon.
199 * @param zclient a pointer to zclient structure
200 * @return socket fd just to make sure that connection established
204 int zclient_socket_connect(struct zclient
*zclient
)
209 /* We should think about IPv6 connection. */
210 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
215 setsockopt_so_sendbuf(sock
, 1048576);
217 /* Connect to zebra. */
218 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
221 zlog_debug("%s connect failure: %d(%s)",
222 __PRETTY_FUNCTION__
, errno
,
223 safe_strerror(errno
));
228 zclient
->sock
= sock
;
232 static int zclient_failed(struct zclient
*zclient
)
235 zclient_stop(zclient
);
236 zclient_event(ZCLIENT_CONNECT
, zclient
);
240 static int zclient_flush_data(struct thread
*thread
)
242 struct zclient
*zclient
= THREAD_ARG(thread
);
244 zclient
->t_write
= NULL
;
245 if (zclient
->sock
< 0)
247 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
251 "%s: buffer_flush_available failed on zclient fd %d, closing",
252 __func__
, zclient
->sock
);
253 return zclient_failed(zclient
);
256 zclient
->t_write
= NULL
;
257 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
258 zclient
->sock
, &zclient
->t_write
);
266 int zclient_send_message(struct zclient
*zclient
)
268 if (zclient
->sock
< 0)
270 switch (buffer_write(zclient
->wb
, zclient
->sock
,
271 STREAM_DATA(zclient
->obuf
),
272 stream_get_endp(zclient
->obuf
))) {
274 flog_err(EC_LIB_ZAPI_SOCKET
,
275 "%s: buffer_write failed to zclient fd %d, closing",
276 __func__
, zclient
->sock
);
277 return zclient_failed(zclient
);
280 THREAD_OFF(zclient
->t_write
);
283 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
284 zclient
->sock
, &zclient
->t_write
);
290 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
292 /* length placeholder, caller can update */
293 stream_putw(s
, ZEBRA_HEADER_SIZE
);
294 stream_putc(s
, ZEBRA_HEADER_MARKER
);
295 stream_putc(s
, ZSERV_VERSION
);
296 stream_putl(s
, vrf_id
);
297 stream_putw(s
, command
);
300 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
301 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
304 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
307 STREAM_GETW(s
, *size
);
308 *size
-= ZEBRA_HEADER_SIZE
;
309 STREAM_GETC(s
, *marker
);
310 STREAM_GETC(s
, *version
);
311 STREAM_GETL(s
, *vrf_id
);
312 STREAM_GETW(s
, *cmd
);
314 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
316 EC_LIB_ZAPI_MISSMATCH
,
317 "%s: socket %d version mismatch, marker %d, version %d",
318 __func__
, sock
, *marker
, *version
);
322 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
329 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
331 STREAM_GETW(zmsg
, hdr
->length
);
332 STREAM_GETC(zmsg
, hdr
->marker
);
333 STREAM_GETC(zmsg
, hdr
->version
);
334 STREAM_GETL(zmsg
, hdr
->vrf_id
);
335 STREAM_GETW(zmsg
, hdr
->command
);
341 /* Send simple Zebra message. */
342 static int zebra_message_send(struct zclient
*zclient
, int command
,
347 /* Get zclient output buffer. */
351 /* Send very simple command only Zebra message. */
352 zclient_create_header(s
, command
, vrf_id
);
354 return zclient_send_message(zclient
);
357 static int zebra_hello_send(struct zclient
*zclient
)
361 if (zclient
->redist_default
) {
365 /* The VRF ID in the HELLO message is always 0. */
366 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
367 stream_putc(s
, zclient
->redist_default
);
368 stream_putw(s
, zclient
->instance
);
369 if (zclient
->receive_notify
)
374 stream_putw_at(s
, 0, stream_get_endp(s
));
375 return zclient_send_message(zclient
);
381 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
382 mpls_label_t label
, enum lsp_types_t ltype
)
389 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
390 stream_putl(s
, label
);
392 stream_putc(s
, ltype
);
393 stream_putw_at(s
, 0, stream_get_endp(s
));
394 zclient_send_message(zclient
);
397 /* Send register requests to zebra daemon for the information in a VRF. */
398 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
403 /* If not connected to the zebra yet. */
404 if (zclient
->sock
< 0)
408 zlog_debug("%s: send register messages for VRF %u", __func__
,
411 /* We need router-id information. */
412 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
414 /* We need interface information. */
415 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
417 /* Set unwanted redistribute route. */
418 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
419 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
422 /* Flush all redistribute request. */
423 if (vrf_id
== VRF_DEFAULT
) {
424 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
425 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
426 if (!zclient
->mi_redist
[afi
][i
].enabled
)
429 struct listnode
*node
;
432 for (ALL_LIST_ELEMENTS_RO(
433 zclient
->mi_redist
[afi
][i
]
436 if (!(i
== zclient
->redist_default
437 && *id
== zclient
->instance
))
438 zebra_redistribute_send(
439 ZEBRA_REDISTRIBUTE_ADD
,
440 zclient
, afi
, i
, *id
,
446 /* Resend all redistribute request. */
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
448 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
449 if (i
!= zclient
->redist_default
450 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
452 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
456 /* If default information is needed. */
457 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
458 zebra_redistribute_default_send(
459 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
464 /* Send unregister requests to zebra daemon for the information in a VRF. */
465 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
470 /* If not connected to the zebra yet. */
471 if (zclient
->sock
< 0)
475 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
478 /* We need router-id information. */
479 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
481 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
483 /* Set unwanted redistribute route. */
484 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
485 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
488 /* Flush all redistribute request. */
489 if (vrf_id
== VRF_DEFAULT
) {
490 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
491 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
492 if (!zclient
->mi_redist
[afi
][i
].enabled
)
495 struct listnode
*node
;
498 for (ALL_LIST_ELEMENTS_RO(
499 zclient
->mi_redist
[afi
][i
]
502 if (!(i
== zclient
->redist_default
503 && *id
== zclient
->instance
))
504 zebra_redistribute_send(
505 ZEBRA_REDISTRIBUTE_DELETE
,
506 zclient
, afi
, i
, *id
,
512 /* Flush all redistribute request. */
513 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
514 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
515 if (i
!= zclient
->redist_default
516 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
518 zebra_redistribute_send(
519 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
522 /* If default information is needed. */
523 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
524 zebra_redistribute_default_send(
525 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
530 /* Send request to zebra daemon to start or stop RA. */
531 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
532 struct interface
*ifp
, int enable
,
537 /* If not connected to the zebra yet. */
538 if (zclient
->sock
< 0)
541 /* Form and send message. */
546 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
548 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
550 stream_putl(s
, ifp
->ifindex
);
551 stream_putl(s
, ra_interval
);
553 stream_putw_at(s
, 0, stream_get_endp(s
));
555 zclient_send_message(zclient
);
558 /* Make connection to zebra daemon. */
559 int zclient_start(struct zclient
*zclient
)
562 zlog_info("zclient_start is called");
564 /* If already connected to the zebra. */
565 if (zclient
->sock
>= 0)
568 /* Check connect thread. */
569 if (zclient
->t_connect
)
572 if (zclient_socket_connect(zclient
) < 0) {
574 zlog_debug("zclient connection fail");
576 zclient_event(ZCLIENT_CONNECT
, zclient
);
580 if (set_nonblocking(zclient
->sock
) < 0)
581 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
582 __func__
, zclient
->sock
);
584 /* Clear fail count. */
587 zlog_debug("zclient connect success with socket [%d]",
590 /* Create read thread. */
591 zclient_event(ZCLIENT_READ
, zclient
);
593 zebra_hello_send(zclient
);
595 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
597 /* Inform the successful connection. */
598 if (zclient
->zebra_connected
)
599 (*zclient
->zebra_connected
)(zclient
);
604 /* Initialize zebra client. Argument redist_default is unwanted
605 redistribute route type. */
606 void zclient_init(struct zclient
*zclient
, int redist_default
,
607 unsigned short instance
, struct zebra_privs_t
*privs
)
611 /* Set -1 to the default socket value. */
613 zclient
->privs
= privs
;
615 /* Clear redistribution flags. */
616 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
617 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
618 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
620 /* Set unwanted redistribute route. bgpd does not need BGP route
622 zclient
->redist_default
= redist_default
;
623 zclient
->instance
= instance
;
624 /* Pending: make afi(s) an arg. */
625 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
626 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
629 /* Set default-information redistribute to zero. */
630 zclient
->default_information
[afi
] = vrf_bitmap_init();
634 zlog_debug("scheduling zclient connection");
636 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
639 /* This function is a wrapper function for calling zclient_start from
640 timer or event thread. */
641 static int zclient_connect(struct thread
*t
)
643 struct zclient
*zclient
;
645 zclient
= THREAD_ARG(t
);
646 zclient
->t_connect
= NULL
;
649 zlog_debug("zclient_connect is called");
651 return zclient_start(zclient
);
654 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
655 bool exact_match
, vrf_id_t vrf_id
)
661 zclient_create_header(s
, command
, vrf_id
);
662 stream_putc(s
, (exact_match
) ? 1 : 0);
664 stream_putw(s
, PREFIX_FAMILY(p
));
665 stream_putc(s
, p
->prefixlen
);
666 switch (PREFIX_FAMILY(p
)) {
668 stream_put_in_addr(s
, &p
->u
.prefix4
);
671 stream_put(s
, &(p
->u
.prefix6
), 16);
676 stream_putw_at(s
, 0, stream_get_endp(s
));
678 return zclient_send_message(zclient
);
682 * "xdr_encode"-like interface that allows daemon (client) to send
683 * a message to zebra server for a route that needs to be
684 * added/deleted to the kernel. Info about the route is specified
685 * by the caller in a struct zapi_route. zapi_route_encode() then writes
686 * the info down the zclient socket using the stream_* functions.
688 * The corresponding read ("xdr_decode") function on the server
689 * side is zapi_route_decode().
691 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | Length (2) | Command | Route Type |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | ZEBRA Flags | Message Flags | Prefix length |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 * | Destination IPv4 Prefix for route |
698 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
703 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
704 * described, as per the Nexthop count. Each nexthop described as:
707 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709 * | IPv4 Nexthop address or Interface Index number |
710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
712 * Alternatively, if the route is a blackhole route, then Nexthop count
713 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
716 * The original struct zapi_route_*() infrastructure was built around
717 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
718 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
719 * nexthops into zapi_route_encode() using the same zapi_route structure.
720 * This is done by setting zapi_route fields as follows:
721 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
722 * - .nexthop_num == .ifindex_num
723 * - .nexthop and .ifindex are filled with gate and ifindex parts of
724 * each compound nexthop, both in the same order
726 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
729 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
732 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
734 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
736 * XXX: No attention paid to alignment.
738 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
739 struct zapi_route
*api
)
741 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
743 return zclient_send_message(zclient
);
746 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
748 struct zapi_nexthop
*api_nh
;
753 zclient_create_header(s
, cmd
, api
->vrf_id
);
755 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
756 flog_err(EC_LIB_ZAPI_ENCODE
,
757 "%s: Specified route type (%u) is not a legal value\n",
758 __PRETTY_FUNCTION__
, api
->type
);
761 stream_putc(s
, api
->type
);
763 stream_putw(s
, api
->instance
);
764 stream_putl(s
, api
->flags
);
765 stream_putc(s
, api
->message
);
767 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
768 flog_err(EC_LIB_ZAPI_ENCODE
,
769 "%s: Specified route SAFI (%u) is not a legal value\n",
770 __PRETTY_FUNCTION__
, api
->safi
);
773 stream_putc(s
, api
->safi
);
775 /* Put prefix information. */
776 stream_putc(s
, api
->prefix
.family
);
777 psize
= PSIZE(api
->prefix
.prefixlen
);
778 stream_putc(s
, api
->prefix
.prefixlen
);
779 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
781 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
782 psize
= PSIZE(api
->src_prefix
.prefixlen
);
783 stream_putc(s
, api
->src_prefix
.prefixlen
);
784 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
788 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
789 /* limit the number of nexthops if necessary */
790 if (api
->nexthop_num
> MULTIPATH_NUM
) {
791 char buf
[PREFIX2STR_BUFFER
];
793 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
796 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
797 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
801 stream_putw(s
, api
->nexthop_num
);
803 for (i
= 0; i
< api
->nexthop_num
; i
++) {
804 api_nh
= &api
->nexthops
[i
];
806 stream_putl(s
, api_nh
->vrf_id
);
807 stream_putc(s
, api_nh
->type
);
808 stream_putc(s
, api_nh
->onlink
);
809 switch (api_nh
->type
) {
810 case NEXTHOP_TYPE_BLACKHOLE
:
811 stream_putc(s
, api_nh
->bh_type
);
813 case NEXTHOP_TYPE_IPV4
:
814 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
816 case NEXTHOP_TYPE_IPV4_IFINDEX
:
817 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
818 stream_putl(s
, api_nh
->ifindex
);
820 case NEXTHOP_TYPE_IFINDEX
:
821 stream_putl(s
, api_nh
->ifindex
);
823 case NEXTHOP_TYPE_IPV6
:
824 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
827 case NEXTHOP_TYPE_IPV6_IFINDEX
:
828 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
830 stream_putl(s
, api_nh
->ifindex
);
834 /* MPLS labels for BGP-LU or Segment Routing */
835 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
836 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
837 char buf
[PREFIX2STR_BUFFER
];
838 prefix2str(&api
->prefix
, buf
,
840 flog_err(EC_LIB_ZAPI_ENCODE
,
841 "%s: prefix %s: can't encode "
842 "%u labels (maximum is %u)",
849 stream_putc(s
, api_nh
->label_num
);
850 stream_put(s
, &api_nh
->labels
[0],
852 * sizeof(mpls_label_t
));
855 /* Router MAC for EVPN routes. */
856 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
857 stream_put(s
, &(api_nh
->rmac
),
858 sizeof(struct ethaddr
));
863 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
864 stream_putc(s
, api
->distance
);
865 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
866 stream_putl(s
, api
->metric
);
867 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
868 stream_putl(s
, api
->tag
);
869 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
870 stream_putl(s
, api
->mtu
);
871 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
872 stream_putl(s
, api
->tableid
);
874 /* Put length at the first point of the stream. */
875 stream_putw_at(s
, 0, stream_get_endp(s
));
880 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
882 struct zapi_nexthop
*api_nh
;
885 memset(api
, 0, sizeof(*api
));
887 /* Type, flags, message. */
888 STREAM_GETC(s
, api
->type
);
889 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
890 flog_err(EC_LIB_ZAPI_ENCODE
,
891 "%s: Specified route type: %d is not a legal value\n",
892 __PRETTY_FUNCTION__
, api
->type
);
896 STREAM_GETW(s
, api
->instance
);
897 STREAM_GETL(s
, api
->flags
);
898 STREAM_GETC(s
, api
->message
);
899 STREAM_GETC(s
, api
->safi
);
900 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
901 flog_err(EC_LIB_ZAPI_ENCODE
,
902 "%s: Specified route SAFI (%u) is not a legal value\n",
903 __PRETTY_FUNCTION__
, api
->safi
);
908 STREAM_GETC(s
, api
->prefix
.family
);
909 STREAM_GETC(s
, api
->prefix
.prefixlen
);
910 switch (api
->prefix
.family
) {
912 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
915 "%s: V4 prefixlen is %d which should not be more than 32",
916 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
921 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
924 "%s: v6 prefixlen is %d which should not be more than 128",
925 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
930 flog_err(EC_LIB_ZAPI_ENCODE
,
931 "%s: Specified family %d is not v4 or v6",
932 __PRETTY_FUNCTION__
, api
->prefix
.family
);
935 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
937 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
938 api
->src_prefix
.family
= AF_INET6
;
939 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
940 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
943 "%s: SRC Prefix prefixlen received: %d is too large",
944 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
947 STREAM_GET(&api
->src_prefix
.prefix
, s
,
948 PSIZE(api
->src_prefix
.prefixlen
));
950 if (api
->prefix
.family
!= AF_INET6
951 || api
->src_prefix
.prefixlen
== 0) {
954 "%s: SRC prefix specified in some manner that makes no sense",
955 __PRETTY_FUNCTION__
);
961 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
962 STREAM_GETW(s
, api
->nexthop_num
);
963 if (api
->nexthop_num
> MULTIPATH_NUM
) {
964 flog_err(EC_LIB_ZAPI_ENCODE
,
965 "%s: invalid number of nexthops (%u)",
966 __func__
, api
->nexthop_num
);
970 for (i
= 0; i
< api
->nexthop_num
; i
++) {
971 api_nh
= &api
->nexthops
[i
];
973 STREAM_GETL(s
, api_nh
->vrf_id
);
974 STREAM_GETC(s
, api_nh
->type
);
975 STREAM_GETC(s
, api_nh
->onlink
);
976 switch (api_nh
->type
) {
977 case NEXTHOP_TYPE_BLACKHOLE
:
978 STREAM_GETC(s
, api_nh
->bh_type
);
980 case NEXTHOP_TYPE_IPV4
:
981 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
984 case NEXTHOP_TYPE_IPV4_IFINDEX
:
985 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
987 STREAM_GETL(s
, api_nh
->ifindex
);
989 case NEXTHOP_TYPE_IFINDEX
:
990 STREAM_GETL(s
, api_nh
->ifindex
);
992 case NEXTHOP_TYPE_IPV6
:
993 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
995 case NEXTHOP_TYPE_IPV6_IFINDEX
:
996 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
997 STREAM_GETL(s
, api_nh
->ifindex
);
1001 /* MPLS labels for BGP-LU or Segment Routing */
1002 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1003 STREAM_GETC(s
, api_nh
->label_num
);
1005 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1008 "%s: invalid number of MPLS labels (%u)",
1009 __func__
, api_nh
->label_num
);
1013 STREAM_GET(&api_nh
->labels
[0], s
,
1015 * sizeof(mpls_label_t
));
1018 /* Router MAC for EVPN routes. */
1019 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1020 stream_get(&(api_nh
->rmac
), s
,
1021 sizeof(struct ethaddr
));
1026 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1027 STREAM_GETC(s
, api
->distance
);
1028 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1029 STREAM_GETL(s
, api
->metric
);
1030 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1031 STREAM_GETL(s
, api
->tag
);
1032 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1033 STREAM_GETL(s
, api
->mtu
);
1034 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1035 STREAM_GETL(s
, api
->tableid
);
1041 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1047 memset(&any
, 0, sizeof(any
));
1048 any
.family
= family
;
1052 stream_putc(s
, p
->family
);
1053 stream_putc(s
, p
->prefixlen
);
1054 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1057 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1060 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1063 * We are sending one item at a time at the moment
1067 stream_putl(s
, zrule
->seq
);
1068 stream_putl(s
, zrule
->priority
);
1069 stream_putl(s
, zrule
->unique
);
1071 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1072 zrule
->filter
.src_ip
.family
);
1073 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1074 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1075 zrule
->filter
.src_ip
.family
);
1076 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1077 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1079 stream_putl(s
, zrule
->action
.table
);
1080 stream_putl(s
, zrule
->ifindex
);
1082 /* Put length at the first point of the stream. */
1083 stream_putw_at(s
, 0, stream_get_endp(s
));
1088 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1090 enum zapi_route_notify_owner
*note
)
1094 STREAM_GET(note
, s
, sizeof(*note
));
1096 STREAM_GETC(s
, p
->family
);
1097 STREAM_GETC(s
, p
->prefixlen
);
1098 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1109 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1110 uint32_t *priority
, uint32_t *unique
,
1112 enum zapi_rule_notify_owner
*note
)
1114 uint32_t prio
, seq
, uni
;
1117 STREAM_GET(note
, s
, sizeof(*note
));
1119 STREAM_GETL(s
, seq
);
1120 STREAM_GETL(s
, prio
);
1121 STREAM_GETL(s
, uni
);
1122 STREAM_GETL(s
, ifi
);
1125 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1138 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1139 enum zapi_ipset_notify_owner
*note
)
1143 STREAM_GET(note
, s
, sizeof(*note
));
1145 STREAM_GETL(s
, uni
);
1148 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1157 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1159 enum zapi_ipset_entry_notify_owner
*note
)
1163 STREAM_GET(note
, s
, sizeof(*note
));
1165 STREAM_GETL(s
, uni
);
1167 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1170 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1179 bool zapi_iptable_notify_decode(struct stream
*s
,
1181 enum zapi_iptable_notify_owner
*note
)
1185 STREAM_GET(note
, s
, sizeof(*note
));
1187 STREAM_GETL(s
, uni
);
1190 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1199 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1201 struct nexthop
*n
= nexthop_new();
1203 n
->type
= znh
->type
;
1204 n
->vrf_id
= znh
->vrf_id
;
1205 n
->ifindex
= znh
->ifindex
;
1206 n
->gate
= znh
->gate
;
1209 * This function currently handles labels
1211 if (znh
->label_num
) {
1212 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1219 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1223 memset(nhr
, 0, sizeof(*nhr
));
1225 STREAM_GETW(s
, nhr
->prefix
.family
);
1226 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1227 switch (nhr
->prefix
.family
) {
1229 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1232 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1238 STREAM_GETC(s
, nhr
->type
);
1239 STREAM_GETW(s
, nhr
->instance
);
1240 STREAM_GETC(s
, nhr
->distance
);
1241 STREAM_GETL(s
, nhr
->metric
);
1242 STREAM_GETC(s
, nhr
->nexthop_num
);
1244 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1245 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1246 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1247 switch (nhr
->nexthops
[i
].type
) {
1248 case NEXTHOP_TYPE_IPV4
:
1249 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1250 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1252 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1254 case NEXTHOP_TYPE_IFINDEX
:
1255 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1257 case NEXTHOP_TYPE_IPV6
:
1258 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1259 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1261 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1263 case NEXTHOP_TYPE_BLACKHOLE
:
1266 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1267 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1268 flog_err(EC_LIB_ZAPI_ENCODE
,
1269 "%s: invalid number of MPLS labels (%u)",
1270 __func__
, nhr
->nexthops
[i
].label_num
);
1273 if (nhr
->nexthops
[i
].label_num
)
1274 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1275 nhr
->nexthops
[i
].label_num
1276 * sizeof(mpls_label_t
));
1285 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1286 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1287 * then set/unset redist[type] in the client handle (a struct zserv) for the
1290 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1291 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1298 zclient_create_header(s
, command
, vrf_id
);
1299 stream_putc(s
, afi
);
1300 stream_putc(s
, type
);
1301 stream_putw(s
, instance
);
1303 stream_putw_at(s
, 0, stream_get_endp(s
));
1305 return zclient_send_message(zclient
);
1308 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1309 afi_t afi
, vrf_id_t vrf_id
)
1316 zclient_create_header(s
, command
, vrf_id
);
1317 stream_putc(s
, afi
);
1319 stream_putw_at(s
, 0, stream_get_endp(s
));
1321 return zclient_send_message(zclient
);
1324 /* Get prefix in ZServ format; family should be filled in on prefix */
1325 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1327 size_t plen
= prefix_blen(p
);
1334 stream_get(&p
->u
.prefix
, s
, plen
);
1336 p
->prefixlen
= MIN(plen
* 8, c
);
1342 /* Router-id update from zebra daemon. */
1343 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1345 /* Fetch interface address. */
1346 STREAM_GETC(s
, rid
->family
);
1348 zclient_stream_get_prefix(s
, rid
);
1354 /* Interface addition from zebra daemon. */
1356 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1357 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1359 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1360 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1379 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1381 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1384 * | Link Layer Type |
1385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1386 * | Harware Address Length |
1387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1388 * | Hardware Address if HW lenght different from 0 |
1389 * | ... max INTERFACE_HWADDR_MAX |
1390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1391 * | Link_params? | Whether a link-params follows: 1 or 0.
1392 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1393 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1394 * | .... (struct if_link_params). |
1395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1398 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1401 char vrfname_tmp
[VRF_NAMSIZ
];
1402 struct vrf_data data
;
1404 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1405 /* Read interface name. */
1406 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1408 /* Lookup/create vrf by vrf_id. */
1409 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1410 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1411 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1412 /* overwrite default vrf */
1413 if (vrf_id
== VRF_DEFAULT
)
1414 vrf_set_default_name(vrfname_tmp
, false);
1418 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1422 /* Lookup vrf by vrf_id. */
1423 vrf
= vrf_lookup_by_id(vrf_id
);
1426 * If a routing protocol doesn't know about a
1427 * vrf that is about to be deleted. There is
1428 * no point in attempting to delete it.
1436 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1438 struct interface
*ifp
;
1439 char ifname_tmp
[INTERFACE_NAMSIZ
];
1441 /* Read interface name. */
1442 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1444 /* Lookup/create interface by name. */
1445 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1447 zebra_interface_if_set_value(s
, ifp
);
1453 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1454 * from zebra server. The format of this message is the same as
1455 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1456 * comments for zebra_interface_add_read), except that no sockaddr_dl
1457 * is sent at the tail of the message.
1459 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1461 struct interface
*ifp
;
1462 char ifname_tmp
[INTERFACE_NAMSIZ
];
1464 /* Read interface name. */
1465 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1467 /* Lookup this by interface index. */
1468 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1470 flog_err(EC_LIB_ZAPI_ENCODE
,
1471 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1472 ifname_tmp
, vrf_id
);
1476 zebra_interface_if_set_value(s
, ifp
);
1481 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1487 iflp
->lp_status
= stream_getl(s
);
1488 iflp
->te_metric
= stream_getl(s
);
1489 iflp
->max_bw
= stream_getf(s
);
1490 iflp
->max_rsv_bw
= stream_getf(s
);
1491 uint32_t bwclassnum
= stream_getl(s
);
1494 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1495 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1498 EC_LIB_ZAPI_MISSMATCH
,
1499 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1500 " - outdated library?",
1501 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1503 iflp
->admin_grp
= stream_getl(s
);
1504 iflp
->rmt_as
= stream_getl(s
);
1505 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1507 iflp
->av_delay
= stream_getl(s
);
1508 iflp
->min_delay
= stream_getl(s
);
1509 iflp
->max_delay
= stream_getl(s
);
1510 iflp
->delay_var
= stream_getl(s
);
1512 iflp
->pkt_loss
= stream_getf(s
);
1513 iflp
->res_bw
= stream_getf(s
);
1514 iflp
->ava_bw
= stream_getf(s
);
1515 iflp
->use_bw
= stream_getf(s
);
1518 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1521 struct if_link_params
*iflp
;
1526 ifindex
= stream_getl(s
);
1528 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1531 flog_err(EC_LIB_ZAPI_ENCODE
,
1532 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1537 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1540 link_params_set_value(s
, iflp
);
1545 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1547 uint8_t link_params_status
= 0;
1548 ifindex_t old_ifindex
;
1550 old_ifindex
= ifp
->ifindex
;
1551 /* Read interface's index. */
1552 if_set_index(ifp
, stream_getl(s
));
1553 ifp
->status
= stream_getc(s
);
1555 /* Read interface's value. */
1556 ifp
->flags
= stream_getq(s
);
1557 ifp
->ptm_enable
= stream_getc(s
);
1558 ifp
->ptm_status
= stream_getc(s
);
1559 ifp
->metric
= stream_getl(s
);
1560 ifp
->speed
= stream_getl(s
);
1561 ifp
->mtu
= stream_getl(s
);
1562 ifp
->mtu6
= stream_getl(s
);
1563 ifp
->bandwidth
= stream_getl(s
);
1564 ifp
->ll_type
= stream_getl(s
);
1565 ifp
->hw_addr_len
= stream_getl(s
);
1566 if (ifp
->hw_addr_len
)
1567 stream_get(ifp
->hw_addr
, s
,
1568 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1570 /* Read Traffic Engineering status */
1571 link_params_status
= stream_getc(s
);
1572 /* Then, Traffic Engineering parameters if any */
1573 if (link_params_status
) {
1574 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1575 link_params_set_value(s
, iflp
);
1578 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1581 size_t zebra_interface_link_params_write(struct stream
*s
,
1582 struct interface
*ifp
)
1585 struct if_link_params
*iflp
;
1588 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1591 iflp
= ifp
->link_params
;
1594 w
+= stream_putl(s
, iflp
->lp_status
);
1596 w
+= stream_putl(s
, iflp
->te_metric
);
1597 w
+= stream_putf(s
, iflp
->max_bw
);
1598 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1600 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1601 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1602 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1604 w
+= stream_putl(s
, iflp
->admin_grp
);
1605 w
+= stream_putl(s
, iflp
->rmt_as
);
1606 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1608 w
+= stream_putl(s
, iflp
->av_delay
);
1609 w
+= stream_putl(s
, iflp
->min_delay
);
1610 w
+= stream_putl(s
, iflp
->max_delay
);
1611 w
+= stream_putl(s
, iflp
->delay_var
);
1613 w
+= stream_putf(s
, iflp
->pkt_loss
);
1614 w
+= stream_putf(s
, iflp
->res_bw
);
1615 w
+= stream_putf(s
, iflp
->ava_bw
);
1616 w
+= stream_putf(s
, iflp
->use_bw
);
1622 * format of message for address additon is:
1626 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1627 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1636 * | ifc_flags | flags for connected address
1644 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1652 static int memconstant(const void *s
, int c
, size_t n
)
1654 const uint8_t *p
= s
;
1663 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1667 struct interface
*ifp
;
1668 struct connected
*ifc
;
1669 struct prefix p
, d
, *dp
;
1673 memset(&p
, 0, sizeof(p
));
1674 memset(&d
, 0, sizeof(d
));
1676 /* Get interface index. */
1677 ifindex
= stream_getl(s
);
1680 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1682 flog_err(EC_LIB_ZAPI_ENCODE
,
1683 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1684 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1690 ifc_flags
= stream_getc(s
);
1692 /* Fetch interface address. */
1693 d
.family
= p
.family
= stream_getc(s
);
1694 plen
= prefix_blen(&d
);
1696 zclient_stream_get_prefix(s
, &p
);
1698 /* Fetch destination address. */
1699 stream_get(&d
.u
.prefix
, s
, plen
);
1701 /* N.B. NULL destination pointers are encoded as all zeroes */
1702 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1704 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1705 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1707 /* N.B. NULL destination pointers are encoded as all
1709 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1712 ifc
->flags
= ifc_flags
;
1713 if (ifc
->destination
)
1714 ifc
->destination
->prefixlen
=
1715 ifc
->address
->prefixlen
;
1716 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1717 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1719 char buf
[PREFIX_STRLEN
];
1722 "warning: interface %s address %s with peer flag set, but no peer address!",
1724 prefix2str(ifc
->address
, buf
,
1726 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1730 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1731 ifc
= connected_delete_by_prefix(ifp
, &p
);
1738 * format of message for neighbor connected address is:
1742 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1743 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1758 * | addr_len | len of addr.
1761 struct nbr_connected
*
1762 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1764 unsigned int ifindex
;
1765 struct interface
*ifp
;
1767 struct nbr_connected
*ifc
;
1769 /* Get interface index. */
1770 ifindex
= stream_getl(s
);
1773 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1775 flog_err(EC_LIB_ZAPI_ENCODE
,
1776 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1777 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1783 p
.family
= stream_getc(s
);
1784 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1785 p
.prefixlen
= stream_getc(s
);
1787 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1788 /* Currently only supporting P2P links, so any new RA source
1790 considered as the replacement of the previously learnt
1791 Link-Local address. */
1792 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1793 ifc
= nbr_connected_new();
1794 ifc
->address
= prefix_new();
1796 listnode_add(ifp
->nbr_connected
, ifc
);
1799 prefix_copy(ifc
->address
, &p
);
1801 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1803 ifc
= nbr_connected_check(ifp
, &p
);
1805 listnode_delete(ifp
->nbr_connected
, ifc
);
1811 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1813 vrf_id_t
*new_vrf_id
)
1815 char ifname
[INTERFACE_NAMSIZ
];
1816 struct interface
*ifp
;
1819 /* Read interface name. */
1820 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
1822 /* Lookup interface. */
1823 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1825 flog_err(EC_LIB_ZAPI_ENCODE
,
1826 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1831 /* Fetch new VRF Id. */
1832 new_id
= stream_getw(s
);
1834 *new_vrf_id
= new_id
;
1838 /* filter unwanted messages until the expected one arrives */
1839 static int zclient_read_sync_response(struct zclient
*zclient
,
1840 uint16_t expected_cmd
)
1852 cmd
= expected_cmd
+ 1;
1853 while (ret
== 0 && cmd
!= expected_cmd
) {
1857 /* wait until response arrives */
1859 FD_SET(zclient
->sock
, &readfds
);
1860 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1861 if (!FD_ISSET(zclient
->sock
, &readfds
))
1864 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1865 &version
, &vrf_id
, &cmd
);
1867 zlog_debug("%s: Response (%d bytes) received", __func__
,
1871 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
1879 * Connect to label manager in a syncronous way
1881 * It first writes the request to zcient output buffer and then
1882 * immediately reads the answer from the input buffer.
1884 * @param zclient Zclient used to connect to label manager (zebra)
1885 * @param async Synchronous (0) or asynchronous (1) operation
1886 * @result Result of response
1888 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
1893 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
1894 ZEBRA_LABEL_MANAGER_CONNECT
;
1897 zlog_debug("Connecting to Label Manager (LM)");
1899 if (zclient
->sock
< 0) {
1900 zlog_debug("%s: invalid zclient socket", __func__
);
1907 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
1910 stream_putc(s
, zclient
->redist_default
);
1912 stream_putw(s
, zclient
->instance
);
1914 /* Put length at the first point of the stream. */
1915 stream_putw_at(s
, 0, stream_get_endp(s
));
1917 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
1919 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
1920 close(zclient
->sock
);
1925 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
1926 close(zclient
->sock
);
1931 zlog_debug("LM connect request sent (%d bytes)", ret
);
1937 if (zclient_read_sync_response(zclient
, cmd
)
1943 /* read instance and proto */
1944 uint8_t proto
= stream_getc(s
);
1945 uint16_t instance
= stream_getw(s
);
1948 if (proto
!= zclient
->redist_default
)
1951 "Wrong proto (%u) in LM connect response. Should be %u",
1952 proto
, zclient
->redist_default
);
1953 if (instance
!= zclient
->instance
)
1956 "Wrong instId (%u) in LM connect response. Should be %u",
1957 instance
, zclient
->instance
);
1960 result
= stream_getc(s
);
1962 zlog_debug("LM connect-response received, result %u", result
);
1968 * Asynchronous label chunk request
1970 * @param zclient Zclient used to connect to label manager (zebra)
1971 * @param keep Avoid garbage collection
1972 * @param chunk_size Amount of labels requested
1973 * @result 0 on success, -1 otherwise
1975 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
1976 uint32_t chunk_size
)
1981 zlog_debug("Getting Label Chunk");
1983 if (zclient
->sock
< 0)
1989 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
1991 stream_putc(s
, zclient
->redist_default
);
1993 stream_putw(s
, zclient
->instance
);
1994 stream_putc(s
, keep
);
1995 stream_putl(s
, chunk_size
);
1997 /* Put length at the first point of the stream. */
1998 stream_putw_at(s
, 0, stream_get_endp(s
));
2000 return zclient_send_message(zclient
);
2004 * Function to request a label chunk in a syncronous way
2006 * It first writes the request to zlcient output buffer and then
2007 * immediately reads the answer from the input buffer.
2009 * @param zclient Zclient used to connect to label manager (zebra)
2010 * @param keep Avoid garbage collection
2011 * @param chunk_size Amount of labels requested
2012 * @param start To write first assigned chunk label to
2013 * @param end To write last assigned chunk label to
2014 * @result 0 on success, -1 otherwise
2016 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2017 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2021 uint8_t response_keep
;
2024 zlog_debug("Getting Label Chunk");
2026 if (zclient
->sock
< 0)
2032 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2034 stream_putc(s
, zclient
->redist_default
);
2036 stream_putw(s
, zclient
->instance
);
2038 stream_putc(s
, keep
);
2040 stream_putl(s
, chunk_size
);
2041 /* Put length at the first point of the stream. */
2042 stream_putw_at(s
, 0, stream_get_endp(s
));
2044 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2046 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2047 close(zclient
->sock
);
2052 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2053 close(zclient
->sock
);
2058 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2061 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2064 /* parse response */
2067 /* read proto and instance */
2068 uint8_t proto
= stream_getc(s
);
2069 uint16_t instance
= stream_getw(s
);
2072 if (proto
!= zclient
->redist_default
)
2073 flog_err(EC_LIB_ZAPI_ENCODE
,
2074 "Wrong proto (%u) in get chunk response. Should be %u",
2075 proto
, zclient
->redist_default
);
2076 if (instance
!= zclient
->instance
)
2077 flog_err(EC_LIB_ZAPI_ENCODE
,
2078 "Wrong instId (%u) in get chunk response Should be %u",
2079 instance
, zclient
->instance
);
2082 response_keep
= stream_getc(s
);
2083 /* start and end labels */
2084 *start
= stream_getl(s
);
2085 *end
= stream_getl(s
);
2087 /* not owning this response */
2088 if (keep
!= response_keep
) {
2091 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2092 *start
, *end
, keep
, response_keep
);
2095 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2096 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2097 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2103 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2110 * Function to release a label chunk
2112 * @param zclient Zclient used to connect to label manager (zebra)
2113 * @param start First label of chunk
2114 * @param end Last label of chunk
2115 * @result 0 on success, -1 otherwise
2117 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2124 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2126 if (zclient
->sock
< 0)
2132 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2135 stream_putc(s
, zclient
->redist_default
);
2137 stream_putw(s
, zclient
->instance
);
2139 stream_putl(s
, start
);
2141 stream_putl(s
, end
);
2143 /* Put length at the first point of the stream. */
2144 stream_putw_at(s
, 0, stream_get_endp(s
));
2146 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2148 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2149 close(zclient
->sock
);
2154 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2155 close(zclient
->sock
);
2164 * Connect to table manager in a syncronous way
2166 * It first writes the request to zcient output buffer and then
2167 * immediately reads the answer from the input buffer.
2169 * @param zclient Zclient used to connect to table manager (zebra)
2170 * @result Result of response
2172 int tm_table_manager_connect(struct zclient
*zclient
)
2179 zlog_debug("Connecting to Table Manager");
2181 if (zclient
->sock
< 0)
2187 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2190 stream_putc(s
, zclient
->redist_default
);
2192 stream_putw(s
, zclient
->instance
);
2194 /* Put length at the first point of the stream. */
2195 stream_putw_at(s
, 0, stream_get_endp(s
));
2197 ret
= zclient_send_message(zclient
);
2202 zlog_debug("%s: Table manager connect request sent", __func__
);
2205 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2211 STREAM_GETC(s
, result
);
2214 "%s: Table Manager connect response received, result %u",
2223 * Function to request a table chunk in a syncronous way
2225 * It first writes the request to zclient output buffer and then
2226 * immediately reads the answer from the input buffer.
2228 * @param zclient Zclient used to connect to table manager (zebra)
2229 * @param chunk_size Amount of table requested
2230 * @param start to write first assigned chunk table RT ID to
2231 * @param end To write last assigned chunk table RT ID to
2232 * @result 0 on success, -1 otherwise
2234 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2235 uint32_t *start
, uint32_t *end
)
2241 zlog_debug("Getting Table Chunk");
2243 if (zclient
->sock
< 0)
2249 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2251 stream_putl(s
, chunk_size
);
2252 /* Put length at the first point of the stream. */
2253 stream_putw_at(s
, 0, stream_get_endp(s
));
2255 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2257 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2259 close(zclient
->sock
);
2264 flog_err(EC_LIB_ZAPI_SOCKET
,
2265 "%s: zclient->sock connection closed", __func__
);
2266 close(zclient
->sock
);
2271 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2275 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2279 /* start and end table IDs */
2280 STREAM_GETL(s
, *start
);
2281 STREAM_GETL(s
, *end
);
2284 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2291 * Function to release a table chunk
2293 * @param zclient Zclient used to connect to table manager (zebra)
2294 * @param start First label of table
2295 * @param end Last label of chunk
2296 * @result 0 on success, -1 otherwise
2298 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2304 zlog_debug("Releasing Table Chunk");
2306 if (zclient
->sock
< 0)
2312 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2315 stream_putl(s
, start
);
2317 stream_putl(s
, end
);
2319 /* Put length at the first point of the stream. */
2320 stream_putw_at(s
, 0, stream_get_endp(s
));
2322 return zclient_send_message(zclient
);
2326 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2334 zclient_create_header(s
, command
, VRF_DEFAULT
);
2335 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2336 stream_putl(s
, pw
->ifindex
);
2339 stream_putl(s
, pw
->type
);
2342 stream_putl(s
, pw
->af
);
2345 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2348 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2351 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2356 stream_putl(s
, pw
->local_label
);
2357 stream_putl(s
, pw
->remote_label
);
2360 stream_putc(s
, pw
->flags
);
2362 /* Protocol specific fields */
2363 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2365 /* Put length at the first point of the stream. */
2366 stream_putw_at(s
, 0, stream_get_endp(s
));
2368 return zclient_send_message(zclient
);
2372 * Receive PW status update from Zebra and send it to LDE process.
2374 void zebra_read_pw_status_update(int command
, struct zclient
*zclient
,
2375 zebra_size_t length
, vrf_id_t vrf_id
,
2376 struct zapi_pw_status
*pw
)
2380 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2384 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2385 pw
->ifindex
= stream_getl(s
);
2386 pw
->status
= stream_getl(s
);
2389 static void zclient_capability_decode(int command
, struct zclient
*zclient
,
2390 zebra_size_t length
, vrf_id_t vrf_id
)
2392 struct zclient_capabilities cap
;
2393 struct stream
*s
= zclient
->ibuf
;
2395 uint8_t mpls_enabled
;
2397 STREAM_GETL(s
, vrf_backend
);
2398 vrf_configure_backend(vrf_backend
);
2400 memset(&cap
, 0, sizeof(cap
));
2401 STREAM_GETC(s
, mpls_enabled
);
2402 cap
.mpls_enabled
= !!mpls_enabled
;
2403 STREAM_GETL(s
, cap
.ecmp
);
2404 STREAM_GETC(s
, cap
.role
);
2406 if (zclient
->zebra_capabilities
)
2407 (*zclient
->zebra_capabilities
)(&cap
);
2413 /* Zebra client message read function. */
2414 static int zclient_read(struct thread
*thread
)
2417 uint16_t length
, command
;
2418 uint8_t marker
, version
;
2420 struct zclient
*zclient
;
2422 /* Get socket to zebra. */
2423 zclient
= THREAD_ARG(thread
);
2424 zclient
->t_read
= NULL
;
2426 /* Read zebra header (if we don't have it already). */
2427 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2429 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2430 ZEBRA_HEADER_SIZE
- already
))
2435 "zclient connection closed socket [%d].",
2437 return zclient_failed(zclient
);
2439 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2440 /* Try again later. */
2441 zclient_event(ZCLIENT_READ
, zclient
);
2444 already
= ZEBRA_HEADER_SIZE
;
2447 /* Reset to read from the beginning of the incoming packet. */
2448 stream_set_getp(zclient
->ibuf
, 0);
2450 /* Fetch header values. */
2451 length
= stream_getw(zclient
->ibuf
);
2452 marker
= stream_getc(zclient
->ibuf
);
2453 version
= stream_getc(zclient
->ibuf
);
2454 vrf_id
= stream_getl(zclient
->ibuf
);
2455 command
= stream_getw(zclient
->ibuf
);
2457 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2459 EC_LIB_ZAPI_MISSMATCH
,
2460 "%s: socket %d version mismatch, marker %d, version %d",
2461 __func__
, zclient
->sock
, marker
, version
);
2462 return zclient_failed(zclient
);
2465 if (length
< ZEBRA_HEADER_SIZE
) {
2466 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2467 "%s: socket %d message length %u is less than %d ",
2468 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2469 return zclient_failed(zclient
);
2473 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2477 "%s: message size %u exceeds buffer size %lu, expanding...",
2479 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2480 ns
= stream_new(length
);
2481 stream_copy(ns
, zclient
->ibuf
);
2482 stream_free(zclient
->ibuf
);
2486 /* Read rest of zebra packet. */
2487 if (already
< length
) {
2489 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2495 "zclient connection closed socket [%d].",
2497 return zclient_failed(zclient
);
2499 if (nbyte
!= (ssize_t
)(length
- already
)) {
2500 /* Try again later. */
2501 zclient_event(ZCLIENT_READ
, zclient
);
2506 length
-= ZEBRA_HEADER_SIZE
;
2509 zlog_debug("zclient 0x%p command %s VRF %u",
2510 (void *)zclient
, zserv_command_string(command
),
2514 case ZEBRA_CAPABILITIES
:
2515 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2517 case ZEBRA_ROUTER_ID_UPDATE
:
2518 if (zclient
->router_id_update
)
2519 (*zclient
->router_id_update
)(command
, zclient
, length
,
2523 zclient_vrf_add(zclient
, vrf_id
);
2525 case ZEBRA_VRF_DELETE
:
2526 zclient_vrf_delete(zclient
, vrf_id
);
2528 case ZEBRA_INTERFACE_ADD
:
2529 if (zclient
->interface_add
)
2530 (*zclient
->interface_add
)(command
, zclient
, length
,
2533 case ZEBRA_INTERFACE_DELETE
:
2534 if (zclient
->interface_delete
)
2535 (*zclient
->interface_delete
)(command
, zclient
, length
,
2538 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2539 if (zclient
->interface_address_add
)
2540 (*zclient
->interface_address_add
)(command
, zclient
,
2543 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2544 if (zclient
->interface_address_delete
)
2545 (*zclient
->interface_address_delete
)(command
, zclient
,
2548 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2549 if (zclient
->interface_bfd_dest_update
)
2550 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2553 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2554 if (zclient
->interface_nbr_address_add
)
2555 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2558 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2559 if (zclient
->interface_nbr_address_delete
)
2560 (*zclient
->interface_nbr_address_delete
)(
2561 command
, zclient
, length
, vrf_id
);
2563 case ZEBRA_INTERFACE_UP
:
2564 if (zclient
->interface_up
)
2565 (*zclient
->interface_up
)(command
, zclient
, length
,
2568 case ZEBRA_INTERFACE_DOWN
:
2569 if (zclient
->interface_down
)
2570 (*zclient
->interface_down
)(command
, zclient
, length
,
2573 case ZEBRA_INTERFACE_VRF_UPDATE
:
2574 if (zclient
->interface_vrf_update
)
2575 (*zclient
->interface_vrf_update
)(command
, zclient
,
2578 case ZEBRA_NEXTHOP_UPDATE
:
2580 zlog_debug("zclient rcvd nexthop update");
2581 if (zclient
->nexthop_update
)
2582 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2585 case ZEBRA_IMPORT_CHECK_UPDATE
:
2587 zlog_debug("zclient rcvd import check update");
2588 if (zclient
->import_check_update
)
2589 (*zclient
->import_check_update
)(command
, zclient
,
2592 case ZEBRA_BFD_DEST_REPLAY
:
2593 if (zclient
->bfd_dest_replay
)
2594 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2597 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2598 if (zclient
->redistribute_route_add
)
2599 (*zclient
->redistribute_route_add
)(command
, zclient
,
2602 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2603 if (zclient
->redistribute_route_del
)
2604 (*zclient
->redistribute_route_del
)(command
, zclient
,
2607 case ZEBRA_INTERFACE_LINK_PARAMS
:
2608 if (zclient
->interface_link_params
)
2609 (*zclient
->interface_link_params
)(command
, zclient
,
2612 case ZEBRA_FEC_UPDATE
:
2614 zlog_debug("zclient rcvd fec update");
2615 if (zclient
->fec_update
)
2616 (*zclient
->fec_update
)(command
, zclient
, length
);
2618 case ZEBRA_LOCAL_ES_ADD
:
2619 if (zclient
->local_es_add
)
2620 (*zclient
->local_es_add
)(command
, zclient
, length
,
2623 case ZEBRA_LOCAL_ES_DEL
:
2624 if (zclient
->local_es_del
)
2625 (*zclient
->local_es_del
)(command
, zclient
, length
,
2629 if (zclient
->local_vni_add
)
2630 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2634 if (zclient
->local_vni_del
)
2635 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2638 case ZEBRA_L3VNI_ADD
:
2639 if (zclient
->local_l3vni_add
)
2640 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2643 case ZEBRA_L3VNI_DEL
:
2644 if (zclient
->local_l3vni_del
)
2645 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2648 case ZEBRA_MACIP_ADD
:
2649 if (zclient
->local_macip_add
)
2650 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2653 case ZEBRA_MACIP_DEL
:
2654 if (zclient
->local_macip_del
)
2655 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2658 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2659 if (zclient
->local_ip_prefix_add
)
2660 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2663 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2664 if (zclient
->local_ip_prefix_del
)
2665 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2668 case ZEBRA_PW_STATUS_UPDATE
:
2669 if (zclient
->pw_status_update
)
2670 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2673 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2674 if (zclient
->route_notify_owner
)
2675 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2678 case ZEBRA_RULE_NOTIFY_OWNER
:
2679 if (zclient
->rule_notify_owner
)
2680 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2683 case ZEBRA_GET_LABEL_CHUNK
:
2684 if (zclient
->label_chunk
)
2685 (*zclient
->label_chunk
)(command
, zclient
, length
,
2688 case ZEBRA_IPSET_NOTIFY_OWNER
:
2689 if (zclient
->ipset_notify_owner
)
2690 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2693 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2694 if (zclient
->ipset_entry_notify_owner
)
2695 (*zclient
->ipset_entry_notify_owner
)(command
,
2699 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2700 if (zclient
->iptable_notify_owner
)
2701 (*zclient
->iptable_notify_owner
)(command
,
2705 case ZEBRA_VXLAN_SG_ADD
:
2706 if (zclient
->vxlan_sg_add
)
2707 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
2710 case ZEBRA_VXLAN_SG_DEL
:
2711 if (zclient
->vxlan_sg_del
)
2712 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
2719 if (zclient
->sock
< 0)
2720 /* Connection was closed during packet processing. */
2723 /* Register read thread. */
2724 stream_reset(zclient
->ibuf
);
2725 zclient_event(ZCLIENT_READ
, zclient
);
2730 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2731 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2735 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2736 if (redist_check_instance(
2737 &zclient
->mi_redist
[afi
][type
], instance
))
2739 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2742 if (!redist_check_instance(
2743 &zclient
->mi_redist
[afi
][type
], instance
))
2745 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2750 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2751 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2754 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2756 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2759 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2763 if (zclient
->sock
> 0)
2764 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2769 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2770 afi_t afi
, vrf_id_t vrf_id
)
2773 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2774 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
2776 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
2778 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
2781 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
2784 if (zclient
->sock
> 0)
2785 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
2788 static void zclient_event(enum event event
, struct zclient
*zclient
)
2791 case ZCLIENT_SCHEDULE
:
2792 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
2793 &zclient
->t_connect
);
2795 case ZCLIENT_CONNECT
:
2798 "zclient connect failures: %d schedule interval is now %d",
2799 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
2800 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
2801 zclient
->fail
< 3 ? 10 : 60,
2802 &zclient
->t_connect
);
2805 zclient
->t_read
= NULL
;
2806 thread_add_read(zclient
->master
, zclient_read
, zclient
,
2807 zclient
->sock
, &zclient
->t_read
);
2812 void zclient_interface_set_master(struct zclient
*client
,
2813 struct interface
*master
,
2814 struct interface
*slave
)
2821 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
2823 stream_putl(s
, master
->vrf_id
);
2824 stream_putl(s
, master
->ifindex
);
2825 stream_putl(s
, slave
->vrf_id
);
2826 stream_putl(s
, slave
->ifindex
);
2828 stream_putw_at(s
, 0, stream_get_endp(s
));
2829 zclient_send_message(client
);