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 static void zebra_interface_if_set_value(struct stream
*s
,
53 struct interface
*ifp
);
55 struct zclient_options zclient_options_default
= {.receive_notify
= false};
57 struct sockaddr_storage zclient_addr
;
58 socklen_t zclient_addr_len
;
60 /* This file local debug flag. */
61 static int zclient_debug
;
63 /* Allocate zclient structure. */
64 struct zclient
*zclient_new(struct thread_master
*master
,
65 struct zclient_options
*opt
)
67 struct zclient
*zclient
;
69 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
71 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
73 zclient
->ibuf
= stream_new(stream_size
);
74 zclient
->obuf
= stream_new(stream_size
);
75 zclient
->wb
= buffer_new(0);
76 zclient
->master
= master
;
78 zclient
->receive_notify
= opt
->receive_notify
;
83 /* This function is only called when exiting, because
84 many parts of the code do not check for I/O errors, so they could
85 reference an invalid pointer if the structure was ever freed.
87 Free zclient structure. */
88 void zclient_free(struct zclient
*zclient
)
91 stream_free(zclient
->ibuf
);
93 stream_free(zclient
->obuf
);
95 buffer_free(zclient
->wb
);
97 XFREE(MTYPE_ZCLIENT
, zclient
);
100 unsigned short *redist_check_instance(struct redist_proto
*red
,
101 unsigned short instance
)
103 struct listnode
*node
;
109 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
116 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
123 red
->instances
= list_new();
125 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
127 listnode_add(red
->instances
, in
);
130 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
134 id
= redist_check_instance(red
, instance
);
138 listnode_delete(red
->instances
, id
);
139 XFREE(MTYPE_REDIST_INST
, id
);
140 if (!red
->instances
->count
) {
142 list_delete(&red
->instances
);
146 /* Stop zebra client services. */
147 void zclient_stop(struct zclient
*zclient
)
153 zlog_debug("zclient stopped");
156 THREAD_OFF(zclient
->t_read
);
157 THREAD_OFF(zclient
->t_connect
);
158 THREAD_OFF(zclient
->t_write
);
161 stream_reset(zclient
->ibuf
);
162 stream_reset(zclient
->obuf
);
164 /* Empty the write buffer. */
165 buffer_reset(zclient
->wb
);
168 if (zclient
->sock
>= 0) {
169 close(zclient
->sock
);
174 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
175 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
176 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
177 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
180 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
183 vrf_bitmap_free(zclient
->default_information
[afi
]);
184 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
188 void zclient_reset(struct zclient
*zclient
)
192 zclient_stop(zclient
);
194 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
196 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
199 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
204 * Connect to zebra daemon.
205 * @param zclient a pointer to zclient structure
206 * @return socket fd just to make sure that connection established
210 int zclient_socket_connect(struct zclient
*zclient
)
215 /* We should think about IPv6 connection. */
216 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
221 setsockopt_so_sendbuf(sock
, 1048576);
223 /* Connect to zebra. */
224 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
227 zlog_debug("%s connect failure: %d(%s)",
228 __PRETTY_FUNCTION__
, errno
,
229 safe_strerror(errno
));
234 zclient
->sock
= sock
;
238 static int zclient_failed(struct zclient
*zclient
)
241 zclient_stop(zclient
);
242 zclient_event(ZCLIENT_CONNECT
, zclient
);
246 static int zclient_flush_data(struct thread
*thread
)
248 struct zclient
*zclient
= THREAD_ARG(thread
);
250 zclient
->t_write
= NULL
;
251 if (zclient
->sock
< 0)
253 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
257 "%s: buffer_flush_available failed on zclient fd %d, closing",
258 __func__
, zclient
->sock
);
259 return zclient_failed(zclient
);
262 zclient
->t_write
= NULL
;
263 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
264 zclient
->sock
, &zclient
->t_write
);
272 int zclient_send_message(struct zclient
*zclient
)
274 if (zclient
->sock
< 0)
276 switch (buffer_write(zclient
->wb
, zclient
->sock
,
277 STREAM_DATA(zclient
->obuf
),
278 stream_get_endp(zclient
->obuf
))) {
280 flog_err(EC_LIB_ZAPI_SOCKET
,
281 "%s: buffer_write failed to zclient fd %d, closing",
282 __func__
, zclient
->sock
);
283 return zclient_failed(zclient
);
286 THREAD_OFF(zclient
->t_write
);
289 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
290 zclient
->sock
, &zclient
->t_write
);
297 * If we add more data to this structure please ensure that
298 * struct zmsghdr in lib/zclient.h is updated as appropriate.
300 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
302 /* length placeholder, caller can update */
303 stream_putw(s
, ZEBRA_HEADER_SIZE
);
304 stream_putc(s
, ZEBRA_HEADER_MARKER
);
305 stream_putc(s
, ZSERV_VERSION
);
306 stream_putl(s
, vrf_id
);
307 stream_putw(s
, command
);
310 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
311 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
314 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
317 STREAM_GETW(s
, *size
);
318 *size
-= ZEBRA_HEADER_SIZE
;
319 STREAM_GETC(s
, *marker
);
320 STREAM_GETC(s
, *version
);
321 STREAM_GETL(s
, *vrf_id
);
322 STREAM_GETW(s
, *cmd
);
324 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
326 EC_LIB_ZAPI_MISSMATCH
,
327 "%s: socket %d version mismatch, marker %d, version %d",
328 __func__
, sock
, *marker
, *version
);
332 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
340 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
342 STREAM_GETW(zmsg
, hdr
->length
);
343 STREAM_GETC(zmsg
, hdr
->marker
);
344 STREAM_GETC(zmsg
, hdr
->version
);
345 STREAM_GETL(zmsg
, hdr
->vrf_id
);
346 STREAM_GETW(zmsg
, hdr
->command
);
352 /* Send simple Zebra message. */
353 static int zebra_message_send(struct zclient
*zclient
, int command
,
358 /* Get zclient output buffer. */
362 /* Send very simple command only Zebra message. */
363 zclient_create_header(s
, command
, vrf_id
);
365 return zclient_send_message(zclient
);
368 static int zebra_hello_send(struct zclient
*zclient
)
372 if (zclient
->redist_default
) {
376 /* The VRF ID in the HELLO message is always 0. */
377 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
378 stream_putc(s
, zclient
->redist_default
);
379 stream_putw(s
, zclient
->instance
);
380 if (zclient
->receive_notify
)
385 stream_putw_at(s
, 0, stream_get_endp(s
));
386 return zclient_send_message(zclient
);
392 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
393 mpls_label_t label
, enum lsp_types_t ltype
)
400 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
401 stream_putl(s
, label
);
403 stream_putc(s
, ltype
);
404 stream_putw_at(s
, 0, stream_get_endp(s
));
405 zclient_send_message(zclient
);
408 /* Send register requests to zebra daemon for the information in a VRF. */
409 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
414 /* If not connected to the zebra yet. */
415 if (zclient
->sock
< 0)
419 zlog_debug("%s: send register messages for VRF %u", __func__
,
422 /* We need router-id information. */
423 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
425 /* We need interface information. */
426 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
428 /* Set unwanted redistribute route. */
429 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
430 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
433 /* Flush all redistribute request. */
434 if (vrf_id
== VRF_DEFAULT
) {
435 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
436 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
437 if (!zclient
->mi_redist
[afi
][i
].enabled
)
440 struct listnode
*node
;
443 for (ALL_LIST_ELEMENTS_RO(
444 zclient
->mi_redist
[afi
][i
]
447 if (!(i
== zclient
->redist_default
448 && *id
== zclient
->instance
))
449 zebra_redistribute_send(
450 ZEBRA_REDISTRIBUTE_ADD
,
451 zclient
, afi
, i
, *id
,
457 /* Resend all redistribute request. */
458 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
459 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
460 if (i
!= zclient
->redist_default
461 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
463 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
467 /* If default information is needed. */
468 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
469 zebra_redistribute_default_send(
470 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
475 /* Send unregister requests to zebra daemon for the information in a VRF. */
476 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
481 /* If not connected to the zebra yet. */
482 if (zclient
->sock
< 0)
486 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
489 /* We need router-id information. */
490 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
492 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
494 /* Set unwanted redistribute route. */
495 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
496 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
499 /* Flush all redistribute request. */
500 if (vrf_id
== VRF_DEFAULT
) {
501 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
502 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
503 if (!zclient
->mi_redist
[afi
][i
].enabled
)
506 struct listnode
*node
;
509 for (ALL_LIST_ELEMENTS_RO(
510 zclient
->mi_redist
[afi
][i
]
513 if (!(i
== zclient
->redist_default
514 && *id
== zclient
->instance
))
515 zebra_redistribute_send(
516 ZEBRA_REDISTRIBUTE_DELETE
,
517 zclient
, afi
, i
, *id
,
523 /* Flush all redistribute request. */
524 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
525 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
526 if (i
!= zclient
->redist_default
527 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
529 zebra_redistribute_send(
530 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
533 /* If default information is needed. */
534 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
535 zebra_redistribute_default_send(
536 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
541 /* Send request to zebra daemon to start or stop RA. */
542 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
543 struct interface
*ifp
, int enable
,
548 /* If not connected to the zebra yet. */
549 if (zclient
->sock
< 0)
552 /* Form and send message. */
557 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
559 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
561 stream_putl(s
, ifp
->ifindex
);
562 stream_putl(s
, ra_interval
);
564 stream_putw_at(s
, 0, stream_get_endp(s
));
566 zclient_send_message(zclient
);
569 int zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
570 struct interface
*ifp
, bool down
)
574 if (zclient
->sock
< 0)
579 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
580 stream_putl(s
, ifp
->ifindex
);
581 stream_putc(s
, !!down
);
582 stream_putw_at(s
, 0, stream_get_endp(s
));
583 zclient_send_message(zclient
);
588 /* Make connection to zebra daemon. */
589 int zclient_start(struct zclient
*zclient
)
592 zlog_info("zclient_start is called");
594 /* If already connected to the zebra. */
595 if (zclient
->sock
>= 0)
598 /* Check connect thread. */
599 if (zclient
->t_connect
)
602 if (zclient_socket_connect(zclient
) < 0) {
604 zlog_debug("zclient connection fail");
606 zclient_event(ZCLIENT_CONNECT
, zclient
);
610 if (set_nonblocking(zclient
->sock
) < 0)
611 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
612 __func__
, zclient
->sock
);
614 /* Clear fail count. */
617 zlog_debug("zclient connect success with socket [%d]",
620 /* Create read thread. */
621 zclient_event(ZCLIENT_READ
, zclient
);
623 zebra_hello_send(zclient
);
625 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
627 /* Inform the successful connection. */
628 if (zclient
->zebra_connected
)
629 (*zclient
->zebra_connected
)(zclient
);
634 /* Initialize zebra client. Argument redist_default is unwanted
635 redistribute route type. */
636 void zclient_init(struct zclient
*zclient
, int redist_default
,
637 unsigned short instance
, struct zebra_privs_t
*privs
)
641 /* Set -1 to the default socket value. */
643 zclient
->privs
= privs
;
645 /* Clear redistribution flags. */
646 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
647 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
648 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
650 /* Set unwanted redistribute route. bgpd does not need BGP route
652 zclient
->redist_default
= redist_default
;
653 zclient
->instance
= instance
;
654 /* Pending: make afi(s) an arg. */
655 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
656 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
659 /* Set default-information redistribute to zero. */
660 zclient
->default_information
[afi
] = vrf_bitmap_init();
664 zlog_debug("scheduling zclient connection");
666 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
669 /* This function is a wrapper function for calling zclient_start from
670 timer or event thread. */
671 static int zclient_connect(struct thread
*t
)
673 struct zclient
*zclient
;
675 zclient
= THREAD_ARG(t
);
676 zclient
->t_connect
= NULL
;
679 zlog_debug("zclient_connect is called");
681 return zclient_start(zclient
);
684 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
685 bool exact_match
, vrf_id_t vrf_id
)
691 zclient_create_header(s
, command
, vrf_id
);
692 stream_putc(s
, (exact_match
) ? 1 : 0);
694 stream_putw(s
, PREFIX_FAMILY(p
));
695 stream_putc(s
, p
->prefixlen
);
696 switch (PREFIX_FAMILY(p
)) {
698 stream_put_in_addr(s
, &p
->u
.prefix4
);
701 stream_put(s
, &(p
->u
.prefix6
), 16);
706 stream_putw_at(s
, 0, stream_get_endp(s
));
708 return zclient_send_message(zclient
);
712 * "xdr_encode"-like interface that allows daemon (client) to send
713 * a message to zebra server for a route that needs to be
714 * added/deleted to the kernel. Info about the route is specified
715 * by the caller in a struct zapi_route. zapi_route_encode() then writes
716 * the info down the zclient socket using the stream_* functions.
718 * The corresponding read ("xdr_decode") function on the server
719 * side is zapi_route_decode().
721 * 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
722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723 * | Length (2) | Command | Route Type |
724 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725 * | ZEBRA Flags | Message Flags | Prefix length |
726 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
727 * | Destination IPv4 Prefix for route |
728 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
733 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
734 * described, as per the Nexthop count. Each nexthop described as:
737 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
738 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
739 * | IPv4 Nexthop address or Interface Index number |
740 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
742 * Alternatively, if the route is a blackhole route, then Nexthop count
743 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
746 * The original struct zapi_route_*() infrastructure was built around
747 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
748 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
749 * nexthops into zapi_route_encode() using the same zapi_route structure.
750 * This is done by setting zapi_route fields as follows:
751 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
752 * - .nexthop_num == .ifindex_num
753 * - .nexthop and .ifindex are filled with gate and ifindex parts of
754 * each compound nexthop, both in the same order
756 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
759 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
762 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
764 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
766 * XXX: No attention paid to alignment.
768 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
769 struct zapi_route
*api
)
771 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
773 return zclient_send_message(zclient
);
776 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
777 const struct zapi_nexthop
*next2
)
779 if (next1
->label_num
> next2
->label_num
)
782 if (next1
->label_num
< next2
->label_num
)
785 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
788 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
789 const struct zapi_nexthop
*next2
)
793 if (next1
->vrf_id
< next2
->vrf_id
)
796 if (next1
->vrf_id
> next2
->vrf_id
)
799 if (next1
->type
< next2
->type
)
802 if (next1
->type
> next2
->type
)
805 if (next1
->weight
< next2
->weight
)
808 if (next1
->weight
> next2
->weight
)
811 switch (next1
->type
) {
812 case NEXTHOP_TYPE_IPV4
:
813 case NEXTHOP_TYPE_IPV6
:
814 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
819 case NEXTHOP_TYPE_IPV4_IFINDEX
:
820 case NEXTHOP_TYPE_IPV6_IFINDEX
:
821 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
825 /* Intentional Fall-Through */
826 case NEXTHOP_TYPE_IFINDEX
:
827 if (next1
->ifindex
< next2
->ifindex
)
830 if (next1
->ifindex
> next2
->ifindex
)
833 case NEXTHOP_TYPE_BLACKHOLE
:
834 if (next1
->bh_type
< next2
->bh_type
)
837 if (next1
->bh_type
> next2
->bh_type
)
845 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
849 const struct zapi_nexthop
*next1
= item1
;
850 const struct zapi_nexthop
*next2
= item2
;
852 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
856 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
861 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
862 uint16_t nexthop_num
)
864 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
869 * Encode a single zapi nexthop
871 int zapi_nexthop_encode(struct stream
*s
, const struct zapi_nexthop
*api_nh
,
875 int nh_flags
= api_nh
->flags
;
877 stream_putl(s
, api_nh
->vrf_id
);
878 stream_putc(s
, api_nh
->type
);
880 /* If needed, set 'labelled nexthop' flag */
881 if (api_nh
->label_num
> 0) {
882 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
884 /* Validate label count */
885 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
892 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
);
894 /* Note that we're only encoding a single octet */
895 stream_putc(s
, nh_flags
);
897 switch (api_nh
->type
) {
898 case NEXTHOP_TYPE_BLACKHOLE
:
899 stream_putc(s
, api_nh
->bh_type
);
901 case NEXTHOP_TYPE_IPV4
:
902 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
904 case NEXTHOP_TYPE_IPV4_IFINDEX
:
905 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
906 stream_putl(s
, api_nh
->ifindex
);
908 case NEXTHOP_TYPE_IFINDEX
:
909 stream_putl(s
, api_nh
->ifindex
);
911 case NEXTHOP_TYPE_IPV6
:
912 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
915 case NEXTHOP_TYPE_IPV6_IFINDEX
:
916 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
918 stream_putl(s
, api_nh
->ifindex
);
922 /* We only encode labels if we have >0 - we use
923 * the per-nexthop flag above to signal that the count
924 * is present in the payload.
926 if (api_nh
->label_num
> 0) {
927 stream_putc(s
, api_nh
->label_num
);
928 stream_put(s
, &api_nh
->labels
[0],
929 api_nh
->label_num
* sizeof(mpls_label_t
));
933 stream_putl(s
, api_nh
->weight
);
935 /* Router MAC for EVPN routes. */
936 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
937 stream_put(s
, &(api_nh
->rmac
),
938 sizeof(struct ethaddr
));
944 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
946 struct zapi_nexthop
*api_nh
;
951 zclient_create_header(s
, cmd
, api
->vrf_id
);
953 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
954 flog_err(EC_LIB_ZAPI_ENCODE
,
955 "%s: Specified route type (%u) is not a legal value\n",
956 __PRETTY_FUNCTION__
, api
->type
);
959 stream_putc(s
, api
->type
);
961 stream_putw(s
, api
->instance
);
962 stream_putl(s
, api
->flags
);
963 stream_putc(s
, api
->message
);
965 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
966 flog_err(EC_LIB_ZAPI_ENCODE
,
967 "%s: Specified route SAFI (%u) is not a legal value\n",
968 __PRETTY_FUNCTION__
, api
->safi
);
971 stream_putc(s
, api
->safi
);
973 /* Put prefix information. */
974 stream_putc(s
, api
->prefix
.family
);
975 psize
= PSIZE(api
->prefix
.prefixlen
);
976 stream_putc(s
, api
->prefix
.prefixlen
);
977 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
979 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
980 psize
= PSIZE(api
->src_prefix
.prefixlen
);
981 stream_putc(s
, api
->src_prefix
.prefixlen
);
982 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
986 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
987 /* limit the number of nexthops if necessary */
988 if (api
->nexthop_num
> MULTIPATH_NUM
) {
989 char buf
[PREFIX2STR_BUFFER
];
991 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
994 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
995 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
999 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
1001 stream_putw(s
, api
->nexthop_num
);
1003 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1004 api_nh
= &api
->nexthops
[i
];
1006 /* MPLS labels for BGP-LU or Segment Routing */
1007 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1008 char buf
[PREFIX2STR_BUFFER
];
1010 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1012 flog_err(EC_LIB_ZAPI_ENCODE
,
1013 "%s: prefix %s: can't encode %u labels (maximum is %u)",
1020 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
) != 0)
1026 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1027 stream_putc(s
, api
->distance
);
1028 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1029 stream_putl(s
, api
->metric
);
1030 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1031 stream_putl(s
, api
->tag
);
1032 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1033 stream_putl(s
, api
->mtu
);
1034 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1035 stream_putl(s
, api
->tableid
);
1037 /* Put length at the first point of the stream. */
1038 stream_putw_at(s
, 0, stream_get_endp(s
));
1044 * Decode a single zapi nexthop object
1046 static int zapi_nexthop_decode(struct stream
*s
, struct zapi_nexthop
*api_nh
,
1051 STREAM_GETL(s
, api_nh
->vrf_id
);
1052 STREAM_GETC(s
, api_nh
->type
);
1054 /* Note that we're only using a single octet of flags */
1055 STREAM_GETC(s
, api_nh
->flags
);
1057 switch (api_nh
->type
) {
1058 case NEXTHOP_TYPE_BLACKHOLE
:
1059 STREAM_GETC(s
, api_nh
->bh_type
);
1061 case NEXTHOP_TYPE_IPV4
:
1062 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1065 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1066 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1068 STREAM_GETL(s
, api_nh
->ifindex
);
1070 case NEXTHOP_TYPE_IFINDEX
:
1071 STREAM_GETL(s
, api_nh
->ifindex
);
1073 case NEXTHOP_TYPE_IPV6
:
1074 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1076 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1077 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1078 STREAM_GETL(s
, api_nh
->ifindex
);
1082 /* MPLS labels for BGP-LU or Segment Routing */
1083 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)) {
1084 STREAM_GETC(s
, api_nh
->label_num
);
1085 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1088 "%s: invalid number of MPLS labels (%u)",
1089 __func__
, api_nh
->label_num
);
1093 STREAM_GET(&api_nh
->labels
[0], s
,
1094 api_nh
->label_num
* sizeof(mpls_label_t
));
1097 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1098 STREAM_GETL(s
, api_nh
->weight
);
1100 /* Router MAC for EVPN routes. */
1101 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1102 STREAM_GET(&(api_nh
->rmac
), s
,
1103 sizeof(struct ethaddr
));
1113 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1115 struct zapi_nexthop
*api_nh
;
1118 memset(api
, 0, sizeof(*api
));
1120 /* Type, flags, message. */
1121 STREAM_GETC(s
, api
->type
);
1122 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1123 flog_err(EC_LIB_ZAPI_ENCODE
,
1124 "%s: Specified route type: %d is not a legal value\n",
1125 __PRETTY_FUNCTION__
, api
->type
);
1129 STREAM_GETW(s
, api
->instance
);
1130 STREAM_GETL(s
, api
->flags
);
1131 STREAM_GETC(s
, api
->message
);
1132 STREAM_GETC(s
, api
->safi
);
1133 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1134 flog_err(EC_LIB_ZAPI_ENCODE
,
1135 "%s: Specified route SAFI (%u) is not a legal value\n",
1136 __PRETTY_FUNCTION__
, api
->safi
);
1141 STREAM_GETC(s
, api
->prefix
.family
);
1142 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1143 switch (api
->prefix
.family
) {
1145 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1148 "%s: V4 prefixlen is %d which should not be more than 32",
1149 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1154 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1157 "%s: v6 prefixlen is %d which should not be more than 128",
1158 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1163 flog_err(EC_LIB_ZAPI_ENCODE
,
1164 "%s: Specified family %d is not v4 or v6",
1165 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1168 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1170 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1171 api
->src_prefix
.family
= AF_INET6
;
1172 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1173 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1176 "%s: SRC Prefix prefixlen received: %d is too large",
1177 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1180 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1181 PSIZE(api
->src_prefix
.prefixlen
));
1183 if (api
->prefix
.family
!= AF_INET6
1184 || api
->src_prefix
.prefixlen
== 0) {
1187 "%s: SRC prefix specified in some manner that makes no sense",
1188 __PRETTY_FUNCTION__
);
1194 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1195 STREAM_GETW(s
, api
->nexthop_num
);
1196 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1197 flog_err(EC_LIB_ZAPI_ENCODE
,
1198 "%s: invalid number of nexthops (%u)",
1199 __func__
, api
->nexthop_num
);
1203 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1204 api_nh
= &api
->nexthops
[i
];
1206 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
) != 0)
1212 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1213 STREAM_GETC(s
, api
->distance
);
1214 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1215 STREAM_GETL(s
, api
->metric
);
1216 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1217 STREAM_GETL(s
, api
->tag
);
1218 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1219 STREAM_GETL(s
, api
->mtu
);
1220 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1221 STREAM_GETL(s
, api
->tableid
);
1228 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1234 memset(&any
, 0, sizeof(any
));
1235 any
.family
= family
;
1239 stream_putc(s
, p
->family
);
1240 stream_putc(s
, p
->prefixlen
);
1241 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1244 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1247 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1250 * We are sending one item at a time at the moment
1254 stream_putl(s
, zrule
->seq
);
1255 stream_putl(s
, zrule
->priority
);
1256 stream_putl(s
, zrule
->unique
);
1258 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1259 zrule
->filter
.src_ip
.family
);
1260 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1261 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1262 zrule
->filter
.src_ip
.family
);
1263 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1264 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1266 stream_putl(s
, zrule
->action
.table
);
1267 stream_putl(s
, zrule
->ifindex
);
1269 /* Put length at the first point of the stream. */
1270 stream_putw_at(s
, 0, stream_get_endp(s
));
1275 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1277 enum zapi_route_notify_owner
*note
)
1281 STREAM_GET(note
, s
, sizeof(*note
));
1283 STREAM_GETC(s
, p
->family
);
1284 STREAM_GETC(s
, p
->prefixlen
);
1285 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1296 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1297 uint32_t *priority
, uint32_t *unique
,
1299 enum zapi_rule_notify_owner
*note
)
1301 uint32_t prio
, seq
, uni
;
1304 STREAM_GET(note
, s
, sizeof(*note
));
1306 STREAM_GETL(s
, seq
);
1307 STREAM_GETL(s
, prio
);
1308 STREAM_GETL(s
, uni
);
1309 STREAM_GETL(s
, ifi
);
1312 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1325 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1326 enum zapi_ipset_notify_owner
*note
)
1330 STREAM_GET(note
, s
, sizeof(*note
));
1332 STREAM_GETL(s
, uni
);
1335 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1344 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1346 enum zapi_ipset_entry_notify_owner
*note
)
1350 STREAM_GET(note
, s
, sizeof(*note
));
1352 STREAM_GETL(s
, uni
);
1354 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1357 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1366 bool zapi_iptable_notify_decode(struct stream
*s
,
1368 enum zapi_iptable_notify_owner
*note
)
1372 STREAM_GET(note
, s
, sizeof(*note
));
1374 STREAM_GETL(s
, uni
);
1377 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1386 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1388 struct nexthop
*n
= nexthop_new();
1390 n
->type
= znh
->type
;
1391 n
->vrf_id
= znh
->vrf_id
;
1392 n
->ifindex
= znh
->ifindex
;
1393 n
->gate
= znh
->gate
;
1396 * This function currently handles labels
1398 if (znh
->label_num
) {
1399 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1407 * Convert nexthop to zapi nexthop
1409 int zapi_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1410 const struct nexthop
*nh
)
1414 memset(znh
, 0, sizeof(*znh
));
1416 znh
->type
= nh
->type
;
1417 znh
->vrf_id
= nh
->vrf_id
;
1418 znh
->ifindex
= nh
->ifindex
;
1419 znh
->gate
= nh
->gate
;
1421 if (nh
->nh_label
&& (nh
->nh_label
->num_labels
> 0)) {
1422 for (i
= 0; i
< nh
->nh_label
->num_labels
; i
++)
1423 znh
->labels
[i
] = nh
->nh_label
->label
[i
];
1426 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1433 * Decode the nexthop-tracking update message
1435 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1439 memset(nhr
, 0, sizeof(*nhr
));
1441 STREAM_GETW(s
, nhr
->prefix
.family
);
1442 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1443 switch (nhr
->prefix
.family
) {
1445 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1448 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1454 STREAM_GETC(s
, nhr
->type
);
1455 STREAM_GETW(s
, nhr
->instance
);
1456 STREAM_GETC(s
, nhr
->distance
);
1457 STREAM_GETL(s
, nhr
->metric
);
1458 STREAM_GETC(s
, nhr
->nexthop_num
);
1460 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1461 if (zapi_nexthop_decode(s
, &(nhr
->nexthops
[i
]), 0) != 0)
1470 bool zapi_error_decode(struct stream
*s
, enum zebra_error_types
*error
)
1472 memset(error
, 0, sizeof(*error
));
1474 STREAM_GET(error
, s
, sizeof(*error
));
1477 zlog_debug("%s: type: %s", __func__
,
1478 zebra_error_type2str(*error
));
1486 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1487 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1488 * then set/unset redist[type] in the client handle (a struct zserv) for the
1491 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1492 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1499 zclient_create_header(s
, command
, vrf_id
);
1500 stream_putc(s
, afi
);
1501 stream_putc(s
, type
);
1502 stream_putw(s
, instance
);
1504 stream_putw_at(s
, 0, stream_get_endp(s
));
1506 return zclient_send_message(zclient
);
1509 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1510 afi_t afi
, vrf_id_t vrf_id
)
1517 zclient_create_header(s
, command
, vrf_id
);
1518 stream_putc(s
, afi
);
1520 stream_putw_at(s
, 0, stream_get_endp(s
));
1522 return zclient_send_message(zclient
);
1525 /* Get prefix in ZServ format; family should be filled in on prefix */
1526 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1528 size_t plen
= prefix_blen(p
);
1535 stream_get(&p
->u
.prefix
, s
, plen
);
1537 p
->prefixlen
= MIN(plen
* 8, c
);
1543 /* Router-id update from zebra daemon. */
1544 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1546 /* Fetch interface address. */
1547 STREAM_GETC(s
, rid
->family
);
1549 zclient_stream_get_prefix(s
, rid
);
1555 /* Interface addition from zebra daemon. */
1557 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1558 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1560 * 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
1561 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1574 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1580 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1585 * | parent ifindex |
1586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1587 * | Link Layer Type |
1588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1589 * | Harware Address Length |
1590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1591 * | Hardware Address if HW lenght different from 0 |
1592 * | ... max INTERFACE_HWADDR_MAX |
1593 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1594 * | Link_params? | Whether a link-params follows: 1 or 0.
1595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1596 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1597 * | .... (struct if_link_params). |
1598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1601 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1604 char vrfname_tmp
[VRF_NAMSIZ
];
1605 struct vrf_data data
;
1607 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1608 /* Read interface name. */
1609 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1611 /* Lookup/create vrf by vrf_id. */
1612 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1613 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1614 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1615 /* overwrite default vrf */
1616 if (vrf_id
== VRF_DEFAULT
)
1617 vrf_set_default_name(vrfname_tmp
, false);
1621 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1625 /* Lookup vrf by vrf_id. */
1626 vrf
= vrf_lookup_by_id(vrf_id
);
1629 * If a routing protocol doesn't know about a
1630 * vrf that is about to be deleted. There is
1631 * no point in attempting to delete it.
1639 static void zclient_interface_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1641 struct interface
*ifp
;
1642 char ifname_tmp
[INTERFACE_NAMSIZ
];
1643 struct stream
*s
= zclient
->ibuf
;
1645 /* Read interface name. */
1646 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1648 /* Lookup/create interface by name. */
1649 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1651 zebra_interface_if_set_value(s
, ifp
);
1653 if_new_via_zapi(ifp
);
1657 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1658 * from zebra server. The format of this message is the same as
1659 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
1660 * except that no sockaddr_dl is sent at the tail of the message.
1662 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1664 struct interface
*ifp
;
1665 char ifname_tmp
[INTERFACE_NAMSIZ
];
1667 /* Read interface name. */
1668 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1670 /* Lookup this by interface index. */
1671 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1673 flog_err(EC_LIB_ZAPI_ENCODE
,
1674 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1675 ifname_tmp
, vrf_id
);
1679 zebra_interface_if_set_value(s
, ifp
);
1684 static void zclient_interface_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1686 struct interface
*ifp
;
1687 struct stream
*s
= zclient
->ibuf
;
1689 ifp
= zebra_interface_state_read(s
, vrf_id
);
1694 if_destroy_via_zapi(ifp
);
1698 static void zclient_interface_up(struct zclient
*zclient
, vrf_id_t vrf_id
)
1700 struct interface
*ifp
;
1701 struct stream
*s
= zclient
->ibuf
;
1703 ifp
= zebra_interface_state_read(s
, vrf_id
);
1708 if_up_via_zapi(ifp
);
1711 static void zclient_interface_down(struct zclient
*zclient
, vrf_id_t vrf_id
)
1713 struct interface
*ifp
;
1714 struct stream
*s
= zclient
->ibuf
;
1716 ifp
= zebra_interface_state_read(s
, vrf_id
);
1721 if_down_via_zapi(ifp
);
1724 static void zclient_handle_error(ZAPI_CALLBACK_ARGS
)
1726 enum zebra_error_types error
;
1727 struct stream
*s
= zclient
->ibuf
;
1729 zapi_error_decode(s
, &error
);
1731 if (zclient
->handle_error
)
1732 (*zclient
->handle_error
)(error
);
1735 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1741 iflp
->lp_status
= stream_getl(s
);
1742 iflp
->te_metric
= stream_getl(s
);
1743 iflp
->max_bw
= stream_getf(s
);
1744 iflp
->max_rsv_bw
= stream_getf(s
);
1745 uint32_t bwclassnum
= stream_getl(s
);
1748 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1749 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1752 EC_LIB_ZAPI_MISSMATCH
,
1753 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1754 " - outdated library?",
1755 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1757 iflp
->admin_grp
= stream_getl(s
);
1758 iflp
->rmt_as
= stream_getl(s
);
1759 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1761 iflp
->av_delay
= stream_getl(s
);
1762 iflp
->min_delay
= stream_getl(s
);
1763 iflp
->max_delay
= stream_getl(s
);
1764 iflp
->delay_var
= stream_getl(s
);
1766 iflp
->pkt_loss
= stream_getf(s
);
1767 iflp
->res_bw
= stream_getf(s
);
1768 iflp
->ava_bw
= stream_getf(s
);
1769 iflp
->use_bw
= stream_getf(s
);
1772 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1775 struct if_link_params
*iflp
;
1780 ifindex
= stream_getl(s
);
1782 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1785 flog_err(EC_LIB_ZAPI_ENCODE
,
1786 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1791 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1794 link_params_set_value(s
, iflp
);
1799 static void zebra_interface_if_set_value(struct stream
*s
,
1800 struct interface
*ifp
)
1802 uint8_t link_params_status
= 0;
1803 ifindex_t old_ifindex
;
1805 old_ifindex
= ifp
->ifindex
;
1806 /* Read interface's index. */
1807 if_set_index(ifp
, stream_getl(s
));
1808 ifp
->status
= stream_getc(s
);
1810 /* Read interface's value. */
1811 ifp
->flags
= stream_getq(s
);
1812 ifp
->ptm_enable
= stream_getc(s
);
1813 ifp
->ptm_status
= stream_getc(s
);
1814 ifp
->metric
= stream_getl(s
);
1815 ifp
->speed
= stream_getl(s
);
1816 ifp
->mtu
= stream_getl(s
);
1817 ifp
->mtu6
= stream_getl(s
);
1818 ifp
->bandwidth
= stream_getl(s
);
1819 ifp
->link_ifindex
= stream_getl(s
);
1820 ifp
->ll_type
= stream_getl(s
);
1821 ifp
->hw_addr_len
= stream_getl(s
);
1822 if (ifp
->hw_addr_len
)
1823 stream_get(ifp
->hw_addr
, s
,
1824 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1826 /* Read Traffic Engineering status */
1827 link_params_status
= stream_getc(s
);
1828 /* Then, Traffic Engineering parameters if any */
1829 if (link_params_status
) {
1830 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1831 link_params_set_value(s
, iflp
);
1834 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1837 size_t zebra_interface_link_params_write(struct stream
*s
,
1838 struct interface
*ifp
)
1841 struct if_link_params
*iflp
;
1844 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1847 iflp
= ifp
->link_params
;
1850 w
+= stream_putl(s
, iflp
->lp_status
);
1852 w
+= stream_putl(s
, iflp
->te_metric
);
1853 w
+= stream_putf(s
, iflp
->max_bw
);
1854 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1856 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1857 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1858 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1860 w
+= stream_putl(s
, iflp
->admin_grp
);
1861 w
+= stream_putl(s
, iflp
->rmt_as
);
1862 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1864 w
+= stream_putl(s
, iflp
->av_delay
);
1865 w
+= stream_putl(s
, iflp
->min_delay
);
1866 w
+= stream_putl(s
, iflp
->max_delay
);
1867 w
+= stream_putl(s
, iflp
->delay_var
);
1869 w
+= stream_putf(s
, iflp
->pkt_loss
);
1870 w
+= stream_putf(s
, iflp
->res_bw
);
1871 w
+= stream_putf(s
, iflp
->ava_bw
);
1872 w
+= stream_putf(s
, iflp
->use_bw
);
1878 * format of message for address additon is:
1882 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1883 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1892 * | ifc_flags | flags for connected address
1900 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1908 static int memconstant(const void *s
, int c
, size_t n
)
1910 const uint8_t *p
= s
;
1919 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1923 struct interface
*ifp
;
1924 struct connected
*ifc
;
1925 struct prefix p
, d
, *dp
;
1929 memset(&p
, 0, sizeof(p
));
1930 memset(&d
, 0, sizeof(d
));
1932 /* Get interface index. */
1933 ifindex
= stream_getl(s
);
1936 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1938 flog_err(EC_LIB_ZAPI_ENCODE
,
1939 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1940 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1946 ifc_flags
= stream_getc(s
);
1948 /* Fetch interface address. */
1949 d
.family
= p
.family
= stream_getc(s
);
1950 plen
= prefix_blen(&d
);
1952 zclient_stream_get_prefix(s
, &p
);
1954 /* Fetch destination address. */
1955 stream_get(&d
.u
.prefix
, s
, plen
);
1957 /* N.B. NULL destination pointers are encoded as all zeroes */
1958 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1960 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1961 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1963 /* N.B. NULL destination pointers are encoded as all
1965 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1968 ifc
->flags
= ifc_flags
;
1969 if (ifc
->destination
)
1970 ifc
->destination
->prefixlen
=
1971 ifc
->address
->prefixlen
;
1972 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1973 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1975 char buf
[PREFIX_STRLEN
];
1978 "warning: interface %s address %s with peer flag set, but no peer address!",
1980 prefix2str(ifc
->address
, buf
,
1982 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1986 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1987 ifc
= connected_delete_by_prefix(ifp
, &p
);
1994 * format of message for neighbor connected address is:
1998 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1999 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2014 * | addr_len | len of addr.
2017 struct nbr_connected
*
2018 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
2020 unsigned int ifindex
;
2021 struct interface
*ifp
;
2023 struct nbr_connected
*ifc
;
2025 /* Get interface index. */
2026 ifindex
= stream_getl(s
);
2029 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2031 flog_err(EC_LIB_ZAPI_ENCODE
,
2032 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2033 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
2039 p
.family
= stream_getc(s
);
2040 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
2041 p
.prefixlen
= stream_getc(s
);
2043 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
2044 /* Currently only supporting P2P links, so any new RA source
2046 considered as the replacement of the previously learnt
2047 Link-Local address. */
2048 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
2049 ifc
= nbr_connected_new();
2050 ifc
->address
= prefix_new();
2052 listnode_add(ifp
->nbr_connected
, ifc
);
2055 prefix_copy(ifc
->address
, &p
);
2057 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
2059 ifc
= nbr_connected_check(ifp
, &p
);
2061 listnode_delete(ifp
->nbr_connected
, ifc
);
2067 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
2069 vrf_id_t
*new_vrf_id
)
2071 char ifname
[INTERFACE_NAMSIZ
];
2072 struct interface
*ifp
;
2075 /* Read interface name. */
2076 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
2078 /* Lookup interface. */
2079 ifp
= if_lookup_by_name(ifname
, vrf_id
);
2081 flog_err(EC_LIB_ZAPI_ENCODE
,
2082 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2087 /* Fetch new VRF Id. */
2088 new_id
= stream_getl(s
);
2090 *new_vrf_id
= new_id
;
2094 /* filter unwanted messages until the expected one arrives */
2095 static int zclient_read_sync_response(struct zclient
*zclient
,
2096 uint16_t expected_cmd
)
2108 cmd
= expected_cmd
+ 1;
2109 while (ret
== 0 && cmd
!= expected_cmd
) {
2113 /* wait until response arrives */
2115 FD_SET(zclient
->sock
, &readfds
);
2116 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2117 if (!FD_ISSET(zclient
->sock
, &readfds
))
2120 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2121 &version
, &vrf_id
, &cmd
);
2123 zlog_debug("%s: Response (%d bytes) received", __func__
,
2127 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2135 * Connect to label manager in a syncronous way
2137 * It first writes the request to zcient output buffer and then
2138 * immediately reads the answer from the input buffer.
2140 * @param zclient Zclient used to connect to label manager (zebra)
2141 * @param async Synchronous (0) or asynchronous (1) operation
2142 * @result Result of response
2144 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2149 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2150 ZEBRA_LABEL_MANAGER_CONNECT
;
2153 zlog_debug("Connecting to Label Manager (LM)");
2155 if (zclient
->sock
< 0) {
2156 zlog_debug("%s: invalid zclient socket", __func__
);
2163 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2166 stream_putc(s
, zclient
->redist_default
);
2168 stream_putw(s
, zclient
->instance
);
2170 /* Put length at the first point of the stream. */
2171 stream_putw_at(s
, 0, stream_get_endp(s
));
2173 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2175 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2176 close(zclient
->sock
);
2181 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2182 close(zclient
->sock
);
2187 zlog_debug("LM connect request sent (%d bytes)", ret
);
2193 if (zclient_read_sync_response(zclient
, cmd
)
2199 /* read instance and proto */
2200 uint8_t proto
= stream_getc(s
);
2201 uint16_t instance
= stream_getw(s
);
2204 if (proto
!= zclient
->redist_default
)
2207 "Wrong proto (%u) in LM connect response. Should be %u",
2208 proto
, zclient
->redist_default
);
2209 if (instance
!= zclient
->instance
)
2212 "Wrong instId (%u) in LM connect response. Should be %u",
2213 instance
, zclient
->instance
);
2216 result
= stream_getc(s
);
2218 zlog_debug("LM connect-response received, result %u", result
);
2224 * Asynchronous label chunk request
2226 * @param zclient Zclient used to connect to label manager (zebra)
2227 * @param keep Avoid garbage collection
2228 * @param chunk_size Amount of labels requested
2229 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2230 * @result 0 on success, -1 otherwise
2232 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2233 uint32_t chunk_size
, uint32_t base
)
2238 zlog_debug("Getting Label Chunk");
2240 if (zclient
->sock
< 0)
2246 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2248 stream_putc(s
, zclient
->redist_default
);
2250 stream_putw(s
, zclient
->instance
);
2251 stream_putc(s
, keep
);
2252 stream_putl(s
, chunk_size
);
2253 stream_putl(s
, base
);
2255 /* Put length at the first point of the stream. */
2256 stream_putw_at(s
, 0, stream_get_endp(s
));
2258 return zclient_send_message(zclient
);
2262 * Function to request a label chunk in a syncronous way
2264 * It first writes the request to zlcient output buffer and then
2265 * immediately reads the answer from the input buffer.
2267 * @param zclient Zclient used to connect to label manager (zebra)
2268 * @param keep Avoid garbage collection
2269 * @param chunk_size Amount of labels requested
2270 * @param start To write first assigned chunk label to
2271 * @param end To write last assigned chunk label to
2272 * @result 0 on success, -1 otherwise
2274 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2275 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2279 uint8_t response_keep
;
2282 zlog_debug("Getting Label Chunk");
2284 if (zclient
->sock
< 0)
2290 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2292 stream_putc(s
, zclient
->redist_default
);
2294 stream_putw(s
, zclient
->instance
);
2296 stream_putc(s
, keep
);
2298 stream_putl(s
, chunk_size
);
2299 /* requested chunk base */
2300 stream_putl(s
, base
);
2301 /* Put length at the first point of the stream. */
2302 stream_putw_at(s
, 0, stream_get_endp(s
));
2304 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2306 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2307 close(zclient
->sock
);
2312 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2313 close(zclient
->sock
);
2318 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2321 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2324 /* parse response */
2327 /* read proto and instance */
2328 uint8_t proto
= stream_getc(s
);
2329 uint16_t instance
= stream_getw(s
);
2332 if (proto
!= zclient
->redist_default
)
2333 flog_err(EC_LIB_ZAPI_ENCODE
,
2334 "Wrong proto (%u) in get chunk response. Should be %u",
2335 proto
, zclient
->redist_default
);
2336 if (instance
!= zclient
->instance
)
2337 flog_err(EC_LIB_ZAPI_ENCODE
,
2338 "Wrong instId (%u) in get chunk response Should be %u",
2339 instance
, zclient
->instance
);
2341 /* if we requested a specific chunk and it could not be allocated, the
2342 * response message will end here
2344 if (!STREAM_READABLE(s
)) {
2345 zlog_info("Unable to assign Label Chunk to %s instance %u",
2346 zebra_route_string(proto
), instance
);
2351 response_keep
= stream_getc(s
);
2352 /* start and end labels */
2353 *start
= stream_getl(s
);
2354 *end
= stream_getl(s
);
2356 /* not owning this response */
2357 if (keep
!= response_keep
) {
2360 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2361 *start
, *end
, keep
, response_keep
);
2364 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2365 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2366 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2372 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2379 * Function to release a label chunk
2381 * @param zclient Zclient used to connect to label manager (zebra)
2382 * @param start First label of chunk
2383 * @param end Last label of chunk
2384 * @result 0 on success, -1 otherwise
2386 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2393 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2395 if (zclient
->sock
< 0)
2401 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2404 stream_putc(s
, zclient
->redist_default
);
2406 stream_putw(s
, zclient
->instance
);
2408 stream_putl(s
, start
);
2410 stream_putl(s
, end
);
2412 /* Put length at the first point of the stream. */
2413 stream_putw_at(s
, 0, stream_get_endp(s
));
2415 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2417 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2418 close(zclient
->sock
);
2423 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2424 close(zclient
->sock
);
2433 * Connect to table manager in a syncronous way
2435 * It first writes the request to zcient output buffer and then
2436 * immediately reads the answer from the input buffer.
2438 * @param zclient Zclient used to connect to table manager (zebra)
2439 * @result Result of response
2441 int tm_table_manager_connect(struct zclient
*zclient
)
2448 zlog_debug("Connecting to Table Manager");
2450 if (zclient
->sock
< 0)
2456 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2459 stream_putc(s
, zclient
->redist_default
);
2461 stream_putw(s
, zclient
->instance
);
2463 /* Put length at the first point of the stream. */
2464 stream_putw_at(s
, 0, stream_get_endp(s
));
2466 ret
= zclient_send_message(zclient
);
2471 zlog_debug("%s: Table manager connect request sent", __func__
);
2474 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2480 STREAM_GETC(s
, result
);
2483 "%s: Table Manager connect response received, result %u",
2492 * Function to request a table chunk in a syncronous way
2494 * It first writes the request to zclient output buffer and then
2495 * immediately reads the answer from the input buffer.
2497 * @param zclient Zclient used to connect to table manager (zebra)
2498 * @param chunk_size Amount of table requested
2499 * @param start to write first assigned chunk table RT ID to
2500 * @param end To write last assigned chunk table RT ID to
2501 * @result 0 on success, -1 otherwise
2503 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2504 uint32_t *start
, uint32_t *end
)
2510 zlog_debug("Getting Table Chunk");
2512 if (zclient
->sock
< 0)
2518 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2520 stream_putl(s
, chunk_size
);
2521 /* Put length at the first point of the stream. */
2522 stream_putw_at(s
, 0, stream_get_endp(s
));
2524 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2526 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2528 close(zclient
->sock
);
2533 flog_err(EC_LIB_ZAPI_SOCKET
,
2534 "%s: zclient->sock connection closed", __func__
);
2535 close(zclient
->sock
);
2540 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2544 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2548 /* start and end table IDs */
2549 STREAM_GETL(s
, *start
);
2550 STREAM_GETL(s
, *end
);
2553 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2561 * Function to release a table chunk
2563 * @param zclient Zclient used to connect to table manager (zebra)
2564 * @param start First label of table
2565 * @param end Last label of chunk
2566 * @result 0 on success, -1 otherwise
2568 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2574 zlog_debug("Releasing Table Chunk");
2576 if (zclient
->sock
< 0)
2582 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2585 stream_putl(s
, start
);
2587 stream_putl(s
, end
);
2589 /* Put length at the first point of the stream. */
2590 stream_putw_at(s
, 0, stream_get_endp(s
));
2592 return zclient_send_message(zclient
);
2595 int zebra_send_mpls_labels(struct zclient
*zclient
, int cmd
,
2596 struct zapi_labels
*zl
)
2598 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
2600 return zclient_send_message(zclient
);
2603 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
2605 struct zapi_nexthop_label
*znh
;
2609 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2610 stream_putc(s
, zl
->message
);
2611 stream_putc(s
, zl
->type
);
2612 stream_putl(s
, zl
->local_label
);
2614 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2615 stream_putw(s
, zl
->route
.prefix
.family
);
2616 stream_put_prefix(s
, &zl
->route
.prefix
);
2617 stream_putc(s
, zl
->route
.type
);
2618 stream_putw(s
, zl
->route
.instance
);
2621 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
2624 "%s: label %u: can't encode %u nexthops (maximum is %u)",
2625 __func__
, zl
->local_label
, zl
->nexthop_num
,
2629 stream_putw(s
, zl
->nexthop_num
);
2631 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2632 znh
= &zl
->nexthops
[i
];
2634 stream_putc(s
, znh
->type
);
2635 stream_putw(s
, znh
->family
);
2636 switch (znh
->family
) {
2638 stream_put_in_addr(s
, &znh
->address
.ipv4
);
2641 stream_write(s
, (uint8_t *)&znh
->address
.ipv6
, 16);
2646 stream_putl(s
, znh
->ifindex
);
2647 stream_putl(s
, znh
->label
);
2650 /* Put length at the first point of the stream. */
2651 stream_putw_at(s
, 0, stream_get_endp(s
));
2656 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
2658 struct zapi_nexthop_label
*znh
;
2660 memset(zl
, 0, sizeof(*zl
));
2663 STREAM_GETC(s
, zl
->message
);
2664 STREAM_GETC(s
, zl
->type
);
2665 STREAM_GETL(s
, zl
->local_label
);
2667 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2670 STREAM_GETW(s
, zl
->route
.prefix
.family
);
2671 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
2673 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
2674 switch (zl
->route
.prefix
.family
) {
2676 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2678 "%s: Specified prefix length %d is greater than a v4 address can support",
2679 __PRETTY_FUNCTION__
,
2680 zl
->route
.prefix
.prefixlen
);
2683 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
2687 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2689 "%s: Specified prefix length %d is greater than a v6 address can support",
2690 __PRETTY_FUNCTION__
,
2691 zl
->route
.prefix
.prefixlen
);
2694 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
2697 flog_err(EC_LIB_ZAPI_ENCODE
,
2698 "%s: Specified family %u is not v4 or v6",
2699 __PRETTY_FUNCTION__
, zl
->route
.prefix
.family
);
2703 STREAM_GETC(s
, zl
->route
.type
);
2704 STREAM_GETW(s
, zl
->route
.instance
);
2707 STREAM_GETW(s
, zl
->nexthop_num
);
2709 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
2712 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
2713 __func__
, &zl
->route
.prefix
, zl
->nexthop_num
,
2717 zl
->nexthop_num
= MIN(MULTIPATH_NUM
, zl
->nexthop_num
);
2719 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2720 znh
= &zl
->nexthops
[i
];
2722 STREAM_GETC(s
, znh
->type
);
2723 STREAM_GETW(s
, znh
->family
);
2724 switch (znh
->family
) {
2726 STREAM_GET(&znh
->address
.ipv4
.s_addr
, s
,
2730 STREAM_GET(&znh
->address
.ipv6
, s
, 16);
2735 STREAM_GETL(s
, znh
->ifindex
);
2736 STREAM_GETL(s
, znh
->label
);
2744 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2752 zclient_create_header(s
, command
, VRF_DEFAULT
);
2753 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2754 stream_putl(s
, pw
->ifindex
);
2757 stream_putl(s
, pw
->type
);
2760 stream_putl(s
, pw
->af
);
2763 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2766 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2769 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2774 stream_putl(s
, pw
->local_label
);
2775 stream_putl(s
, pw
->remote_label
);
2778 stream_putc(s
, pw
->flags
);
2780 /* Protocol specific fields */
2781 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2783 /* Put length at the first point of the stream. */
2784 stream_putw_at(s
, 0, stream_get_endp(s
));
2786 return zclient_send_message(zclient
);
2790 * Receive PW status update from Zebra and send it to LDE process.
2792 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
2796 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2800 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2801 pw
->ifindex
= stream_getl(s
);
2802 pw
->status
= stream_getl(s
);
2805 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
2807 struct zclient_capabilities cap
;
2808 struct stream
*s
= zclient
->ibuf
;
2810 uint8_t mpls_enabled
;
2812 STREAM_GETL(s
, vrf_backend
);
2813 vrf_configure_backend(vrf_backend
);
2815 memset(&cap
, 0, sizeof(cap
));
2816 STREAM_GETC(s
, mpls_enabled
);
2817 cap
.mpls_enabled
= !!mpls_enabled
;
2818 STREAM_GETL(s
, cap
.ecmp
);
2819 STREAM_GETC(s
, cap
.role
);
2821 if (zclient
->zebra_capabilities
)
2822 (*zclient
->zebra_capabilities
)(&cap
);
2828 void zclient_send_mlag_register(struct zclient
*client
, uint32_t bit_map
)
2835 zclient_create_header(s
, ZEBRA_MLAG_CLIENT_REGISTER
, VRF_DEFAULT
);
2836 stream_putl(s
, bit_map
);
2838 stream_putw_at(s
, 0, stream_get_endp(s
));
2839 zclient_send_message(client
);
2842 void zclient_send_mlag_deregister(struct zclient
*client
)
2844 zebra_message_send(client
, ZEBRA_MLAG_CLIENT_UNREGISTER
, VRF_DEFAULT
);
2847 void zclient_send_mlag_data(struct zclient
*client
, struct stream
*client_s
)
2854 zclient_create_header(s
, ZEBRA_MLAG_FORWARD_MSG
, VRF_DEFAULT
);
2855 stream_put(s
, client_s
->data
, client_s
->endp
);
2857 stream_putw_at(s
, 0, stream_get_endp(s
));
2858 zclient_send_message(client
);
2861 static void zclient_mlag_process_up(ZAPI_CALLBACK_ARGS
)
2863 if (zclient
->mlag_process_up
)
2864 (*zclient
->mlag_process_up
)();
2867 static void zclient_mlag_process_down(ZAPI_CALLBACK_ARGS
)
2869 if (zclient
->mlag_process_down
)
2870 (*zclient
->mlag_process_down
)();
2873 static void zclient_mlag_handle_msg(ZAPI_CALLBACK_ARGS
)
2875 if (zclient
->mlag_handle_msg
)
2876 (*zclient
->mlag_handle_msg
)(zclient
->ibuf
, length
);
2879 /* Zebra client message read function. */
2880 static int zclient_read(struct thread
*thread
)
2883 uint16_t length
, command
;
2884 uint8_t marker
, version
;
2886 struct zclient
*zclient
;
2888 /* Get socket to zebra. */
2889 zclient
= THREAD_ARG(thread
);
2890 zclient
->t_read
= NULL
;
2892 /* Read zebra header (if we don't have it already). */
2893 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2895 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2896 ZEBRA_HEADER_SIZE
- already
))
2901 "zclient connection closed socket [%d].",
2903 return zclient_failed(zclient
);
2905 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2906 /* Try again later. */
2907 zclient_event(ZCLIENT_READ
, zclient
);
2910 already
= ZEBRA_HEADER_SIZE
;
2913 /* Reset to read from the beginning of the incoming packet. */
2914 stream_set_getp(zclient
->ibuf
, 0);
2916 /* Fetch header values. */
2917 length
= stream_getw(zclient
->ibuf
);
2918 marker
= stream_getc(zclient
->ibuf
);
2919 version
= stream_getc(zclient
->ibuf
);
2920 vrf_id
= stream_getl(zclient
->ibuf
);
2921 command
= stream_getw(zclient
->ibuf
);
2923 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2925 EC_LIB_ZAPI_MISSMATCH
,
2926 "%s: socket %d version mismatch, marker %d, version %d",
2927 __func__
, zclient
->sock
, marker
, version
);
2928 return zclient_failed(zclient
);
2931 if (length
< ZEBRA_HEADER_SIZE
) {
2932 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2933 "%s: socket %d message length %u is less than %d ",
2934 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2935 return zclient_failed(zclient
);
2939 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2943 "%s: message size %u exceeds buffer size %lu, expanding...",
2945 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2946 ns
= stream_new(length
);
2947 stream_copy(ns
, zclient
->ibuf
);
2948 stream_free(zclient
->ibuf
);
2952 /* Read rest of zebra packet. */
2953 if (already
< length
) {
2955 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2961 "zclient connection closed socket [%d].",
2963 return zclient_failed(zclient
);
2965 if (nbyte
!= (ssize_t
)(length
- already
)) {
2966 /* Try again later. */
2967 zclient_event(ZCLIENT_READ
, zclient
);
2972 length
-= ZEBRA_HEADER_SIZE
;
2975 zlog_debug("zclient 0x%p command %s VRF %u",
2976 (void *)zclient
, zserv_command_string(command
),
2980 case ZEBRA_CAPABILITIES
:
2981 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2983 case ZEBRA_ROUTER_ID_UPDATE
:
2984 if (zclient
->router_id_update
)
2985 (*zclient
->router_id_update
)(command
, zclient
, length
,
2989 zclient_vrf_add(zclient
, vrf_id
);
2991 case ZEBRA_VRF_DELETE
:
2992 zclient_vrf_delete(zclient
, vrf_id
);
2994 case ZEBRA_INTERFACE_ADD
:
2995 zclient_interface_add(zclient
, vrf_id
);
2997 case ZEBRA_INTERFACE_DELETE
:
2998 zclient_interface_delete(zclient
, vrf_id
);
3000 case ZEBRA_INTERFACE_ADDRESS_ADD
:
3001 if (zclient
->interface_address_add
)
3002 (*zclient
->interface_address_add
)(command
, zclient
,
3005 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
3006 if (zclient
->interface_address_delete
)
3007 (*zclient
->interface_address_delete
)(command
, zclient
,
3010 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
3011 if (zclient
->interface_bfd_dest_update
)
3012 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
3015 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
3016 if (zclient
->interface_nbr_address_add
)
3017 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
3020 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
3021 if (zclient
->interface_nbr_address_delete
)
3022 (*zclient
->interface_nbr_address_delete
)(
3023 command
, zclient
, length
, vrf_id
);
3025 case ZEBRA_INTERFACE_UP
:
3026 zclient_interface_up(zclient
, vrf_id
);
3028 case ZEBRA_INTERFACE_DOWN
:
3029 zclient_interface_down(zclient
, vrf_id
);
3031 case ZEBRA_INTERFACE_VRF_UPDATE
:
3032 if (zclient
->interface_vrf_update
)
3033 (*zclient
->interface_vrf_update
)(command
, zclient
,
3036 case ZEBRA_NEXTHOP_UPDATE
:
3038 zlog_debug("zclient rcvd nexthop update");
3039 if (zclient
->nexthop_update
)
3040 (*zclient
->nexthop_update
)(command
, zclient
, length
,
3043 case ZEBRA_IMPORT_CHECK_UPDATE
:
3045 zlog_debug("zclient rcvd import check update");
3046 if (zclient
->import_check_update
)
3047 (*zclient
->import_check_update
)(command
, zclient
,
3050 case ZEBRA_BFD_DEST_REPLAY
:
3051 if (zclient
->bfd_dest_replay
)
3052 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
3055 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
3056 if (zclient
->redistribute_route_add
)
3057 (*zclient
->redistribute_route_add
)(command
, zclient
,
3060 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
3061 if (zclient
->redistribute_route_del
)
3062 (*zclient
->redistribute_route_del
)(command
, zclient
,
3065 case ZEBRA_INTERFACE_LINK_PARAMS
:
3066 if (zclient
->interface_link_params
)
3067 (*zclient
->interface_link_params
)(command
, zclient
,
3070 case ZEBRA_FEC_UPDATE
:
3072 zlog_debug("zclient rcvd fec update");
3073 if (zclient
->fec_update
)
3074 (*zclient
->fec_update
)(command
, zclient
, length
);
3076 case ZEBRA_LOCAL_ES_ADD
:
3077 if (zclient
->local_es_add
)
3078 (*zclient
->local_es_add
)(command
, zclient
, length
,
3081 case ZEBRA_LOCAL_ES_DEL
:
3082 if (zclient
->local_es_del
)
3083 (*zclient
->local_es_del
)(command
, zclient
, length
,
3087 if (zclient
->local_vni_add
)
3088 (*zclient
->local_vni_add
)(command
, zclient
, length
,
3092 if (zclient
->local_vni_del
)
3093 (*zclient
->local_vni_del
)(command
, zclient
, length
,
3096 case ZEBRA_L3VNI_ADD
:
3097 if (zclient
->local_l3vni_add
)
3098 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
3101 case ZEBRA_L3VNI_DEL
:
3102 if (zclient
->local_l3vni_del
)
3103 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
3106 case ZEBRA_MACIP_ADD
:
3107 if (zclient
->local_macip_add
)
3108 (*zclient
->local_macip_add
)(command
, zclient
, length
,
3111 case ZEBRA_MACIP_DEL
:
3112 if (zclient
->local_macip_del
)
3113 (*zclient
->local_macip_del
)(command
, zclient
, length
,
3116 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
3117 if (zclient
->local_ip_prefix_add
)
3118 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
3121 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
3122 if (zclient
->local_ip_prefix_del
)
3123 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
3126 case ZEBRA_PW_STATUS_UPDATE
:
3127 if (zclient
->pw_status_update
)
3128 (*zclient
->pw_status_update
)(command
, zclient
, length
,
3131 case ZEBRA_ROUTE_NOTIFY_OWNER
:
3132 if (zclient
->route_notify_owner
)
3133 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
3136 case ZEBRA_RULE_NOTIFY_OWNER
:
3137 if (zclient
->rule_notify_owner
)
3138 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
3141 case ZEBRA_GET_LABEL_CHUNK
:
3142 if (zclient
->label_chunk
)
3143 (*zclient
->label_chunk
)(command
, zclient
, length
,
3146 case ZEBRA_IPSET_NOTIFY_OWNER
:
3147 if (zclient
->ipset_notify_owner
)
3148 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
3151 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
3152 if (zclient
->ipset_entry_notify_owner
)
3153 (*zclient
->ipset_entry_notify_owner
)(command
,
3157 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
3158 if (zclient
->iptable_notify_owner
)
3159 (*zclient
->iptable_notify_owner
)(command
,
3163 case ZEBRA_VXLAN_SG_ADD
:
3164 if (zclient
->vxlan_sg_add
)
3165 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
3168 case ZEBRA_VXLAN_SG_DEL
:
3169 if (zclient
->vxlan_sg_del
)
3170 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
3173 case ZEBRA_MLAG_PROCESS_UP
:
3174 zclient_mlag_process_up(command
, zclient
, length
, vrf_id
);
3176 case ZEBRA_MLAG_PROCESS_DOWN
:
3177 zclient_mlag_process_down(command
, zclient
, length
, vrf_id
);
3179 case ZEBRA_MLAG_FORWARD_MSG
:
3180 zclient_mlag_handle_msg(command
, zclient
, length
, vrf_id
);
3183 zclient_handle_error(command
, zclient
, length
, vrf_id
);
3188 if (zclient
->sock
< 0)
3189 /* Connection was closed during packet processing. */
3192 /* Register read thread. */
3193 stream_reset(zclient
->ibuf
);
3194 zclient_event(ZCLIENT_READ
, zclient
);
3199 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
3200 int type
, unsigned short instance
, vrf_id_t vrf_id
)
3204 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3205 if (redist_check_instance(
3206 &zclient
->mi_redist
[afi
][type
], instance
))
3208 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3211 if (!redist_check_instance(
3212 &zclient
->mi_redist
[afi
][type
], instance
))
3214 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3219 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3220 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
3223 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3225 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
3228 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3232 if (zclient
->sock
> 0)
3233 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
3238 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
3239 afi_t afi
, vrf_id_t vrf_id
)
3242 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
3243 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
3245 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
3247 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
3250 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
3253 if (zclient
->sock
> 0)
3254 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
3257 static void zclient_event(enum event event
, struct zclient
*zclient
)
3260 case ZCLIENT_SCHEDULE
:
3261 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
3262 &zclient
->t_connect
);
3264 case ZCLIENT_CONNECT
:
3267 "zclient connect failures: %d schedule interval is now %d",
3268 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
3269 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
3270 zclient
->fail
< 3 ? 10 : 60,
3271 &zclient
->t_connect
);
3274 zclient
->t_read
= NULL
;
3275 thread_add_read(zclient
->master
, zclient_read
, zclient
,
3276 zclient
->sock
, &zclient
->t_read
);
3281 void zclient_interface_set_master(struct zclient
*client
,
3282 struct interface
*master
,
3283 struct interface
*slave
)
3290 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
3292 stream_putl(s
, master
->vrf_id
);
3293 stream_putl(s
, master
->ifindex
);
3294 stream_putl(s
, slave
->vrf_id
);
3295 stream_putl(s
, slave
->ifindex
);
3297 stream_putw_at(s
, 0, stream_get_endp(s
));
3298 zclient_send_message(client
);