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 void redist_del_all_instances(struct redist_proto
*red
)
148 struct listnode
*ln
, *nn
;
154 for (ALL_LIST_ELEMENTS(red
->instances
, ln
, nn
, id
))
155 redist_del_instance(red
, *id
);
158 /* Stop zebra client services. */
159 void zclient_stop(struct zclient
*zclient
)
165 zlog_debug("zclient stopped");
168 THREAD_OFF(zclient
->t_read
);
169 THREAD_OFF(zclient
->t_connect
);
170 THREAD_OFF(zclient
->t_write
);
173 stream_reset(zclient
->ibuf
);
174 stream_reset(zclient
->obuf
);
176 /* Empty the write buffer. */
177 buffer_reset(zclient
->wb
);
180 if (zclient
->sock
>= 0) {
181 close(zclient
->sock
);
186 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
187 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
188 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
189 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
192 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
195 vrf_bitmap_free(zclient
->default_information
[afi
]);
196 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
200 void zclient_reset(struct zclient
*zclient
)
204 zclient_stop(zclient
);
206 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
208 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
211 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
216 * Connect to zebra daemon.
217 * @param zclient a pointer to zclient structure
218 * @return socket fd just to make sure that connection established
222 int zclient_socket_connect(struct zclient
*zclient
)
227 /* We should think about IPv6 connection. */
228 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
233 setsockopt_so_sendbuf(sock
, 1048576);
235 /* Connect to zebra. */
236 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
239 zlog_debug("%s connect failure: %d(%s)",
240 __PRETTY_FUNCTION__
, errno
,
241 safe_strerror(errno
));
246 zclient
->sock
= sock
;
250 static int zclient_failed(struct zclient
*zclient
)
253 zclient_stop(zclient
);
254 zclient_event(ZCLIENT_CONNECT
, zclient
);
258 static int zclient_flush_data(struct thread
*thread
)
260 struct zclient
*zclient
= THREAD_ARG(thread
);
262 zclient
->t_write
= NULL
;
263 if (zclient
->sock
< 0)
265 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
269 "%s: buffer_flush_available failed on zclient fd %d, closing",
270 __func__
, zclient
->sock
);
271 return zclient_failed(zclient
);
274 zclient
->t_write
= NULL
;
275 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
276 zclient
->sock
, &zclient
->t_write
);
284 int zclient_send_message(struct zclient
*zclient
)
286 if (zclient
->sock
< 0)
288 switch (buffer_write(zclient
->wb
, zclient
->sock
,
289 STREAM_DATA(zclient
->obuf
),
290 stream_get_endp(zclient
->obuf
))) {
292 flog_err(EC_LIB_ZAPI_SOCKET
,
293 "%s: buffer_write failed to zclient fd %d, closing",
294 __func__
, zclient
->sock
);
295 return zclient_failed(zclient
);
298 THREAD_OFF(zclient
->t_write
);
301 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
302 zclient
->sock
, &zclient
->t_write
);
309 * If we add more data to this structure please ensure that
310 * struct zmsghdr in lib/zclient.h is updated as appropriate.
312 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
314 /* length placeholder, caller can update */
315 stream_putw(s
, ZEBRA_HEADER_SIZE
);
316 stream_putc(s
, ZEBRA_HEADER_MARKER
);
317 stream_putc(s
, ZSERV_VERSION
);
318 stream_putl(s
, vrf_id
);
319 stream_putw(s
, command
);
322 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
323 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
326 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
329 STREAM_GETW(s
, *size
);
330 *size
-= ZEBRA_HEADER_SIZE
;
331 STREAM_GETC(s
, *marker
);
332 STREAM_GETC(s
, *version
);
333 STREAM_GETL(s
, *vrf_id
);
334 STREAM_GETW(s
, *cmd
);
336 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
338 EC_LIB_ZAPI_MISSMATCH
,
339 "%s: socket %d version mismatch, marker %d, version %d",
340 __func__
, sock
, *marker
, *version
);
344 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
352 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
354 STREAM_GETW(zmsg
, hdr
->length
);
355 STREAM_GETC(zmsg
, hdr
->marker
);
356 STREAM_GETC(zmsg
, hdr
->version
);
357 STREAM_GETL(zmsg
, hdr
->vrf_id
);
358 STREAM_GETW(zmsg
, hdr
->command
);
364 /* Send simple Zebra message. */
365 static int zebra_message_send(struct zclient
*zclient
, int command
,
370 /* Get zclient output buffer. */
374 /* Send very simple command only Zebra message. */
375 zclient_create_header(s
, command
, vrf_id
);
377 return zclient_send_message(zclient
);
380 static int zebra_hello_send(struct zclient
*zclient
)
384 if (zclient
->redist_default
) {
388 /* The VRF ID in the HELLO message is always 0. */
389 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
390 stream_putc(s
, zclient
->redist_default
);
391 stream_putw(s
, zclient
->instance
);
392 if (zclient
->receive_notify
)
397 stream_putw_at(s
, 0, stream_get_endp(s
));
398 return zclient_send_message(zclient
);
404 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
405 mpls_label_t label
, enum lsp_types_t ltype
)
412 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
413 stream_putl(s
, label
);
415 stream_putc(s
, ltype
);
416 stream_putw_at(s
, 0, stream_get_endp(s
));
417 zclient_send_message(zclient
);
420 /* Send register requests to zebra daemon for the information in a VRF. */
421 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
426 /* If not connected to the zebra yet. */
427 if (zclient
->sock
< 0)
431 zlog_debug("%s: send register messages for VRF %u", __func__
,
434 /* We need router-id information. */
435 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
437 /* We need interface information. */
438 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
440 /* Set unwanted redistribute route. */
441 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
442 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
445 /* Flush all redistribute request. */
446 if (vrf_id
== VRF_DEFAULT
) {
447 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
448 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
449 if (!zclient
->mi_redist
[afi
][i
].enabled
)
452 struct listnode
*node
;
455 for (ALL_LIST_ELEMENTS_RO(
456 zclient
->mi_redist
[afi
][i
]
459 if (!(i
== zclient
->redist_default
460 && *id
== zclient
->instance
))
461 zebra_redistribute_send(
462 ZEBRA_REDISTRIBUTE_ADD
,
463 zclient
, afi
, i
, *id
,
469 /* Resend all redistribute request. */
470 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
471 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
472 if (i
!= zclient
->redist_default
473 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
475 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
479 /* If default information is needed. */
480 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
481 zebra_redistribute_default_send(
482 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
487 /* Send unregister requests to zebra daemon for the information in a VRF. */
488 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
493 /* If not connected to the zebra yet. */
494 if (zclient
->sock
< 0)
498 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
501 /* We need router-id information. */
502 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
504 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
506 /* Set unwanted redistribute route. */
507 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
508 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
511 /* Flush all redistribute request. */
512 if (vrf_id
== VRF_DEFAULT
) {
513 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
514 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
515 if (!zclient
->mi_redist
[afi
][i
].enabled
)
518 struct listnode
*node
;
521 for (ALL_LIST_ELEMENTS_RO(
522 zclient
->mi_redist
[afi
][i
]
525 if (!(i
== zclient
->redist_default
526 && *id
== zclient
->instance
))
527 zebra_redistribute_send(
528 ZEBRA_REDISTRIBUTE_DELETE
,
529 zclient
, afi
, i
, *id
,
535 /* Flush all redistribute request. */
536 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
537 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
538 if (i
!= zclient
->redist_default
539 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
541 zebra_redistribute_send(
542 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
545 /* If default information is needed. */
546 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
547 zebra_redistribute_default_send(
548 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
553 /* Send request to zebra daemon to start or stop RA. */
554 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
555 struct interface
*ifp
, int enable
,
560 /* If not connected to the zebra yet. */
561 if (zclient
->sock
< 0)
564 /* Form and send message. */
569 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
571 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
573 stream_putl(s
, ifp
->ifindex
);
574 stream_putl(s
, ra_interval
);
576 stream_putw_at(s
, 0, stream_get_endp(s
));
578 zclient_send_message(zclient
);
581 int zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
582 struct interface
*ifp
, bool down
)
586 if (zclient
->sock
< 0)
591 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
592 stream_putl(s
, ifp
->ifindex
);
593 stream_putc(s
, !!down
);
594 stream_putw_at(s
, 0, stream_get_endp(s
));
595 zclient_send_message(zclient
);
600 /* Make connection to zebra daemon. */
601 int zclient_start(struct zclient
*zclient
)
604 zlog_info("zclient_start is called");
606 /* If already connected to the zebra. */
607 if (zclient
->sock
>= 0)
610 /* Check connect thread. */
611 if (zclient
->t_connect
)
614 if (zclient_socket_connect(zclient
) < 0) {
616 zlog_debug("zclient connection fail");
618 zclient_event(ZCLIENT_CONNECT
, zclient
);
622 if (set_nonblocking(zclient
->sock
) < 0)
623 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
624 __func__
, zclient
->sock
);
626 /* Clear fail count. */
629 zlog_debug("zclient connect success with socket [%d]",
632 /* Create read thread. */
633 zclient_event(ZCLIENT_READ
, zclient
);
635 zebra_hello_send(zclient
);
637 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
639 /* Inform the successful connection. */
640 if (zclient
->zebra_connected
)
641 (*zclient
->zebra_connected
)(zclient
);
646 /* Initialize zebra client. Argument redist_default is unwanted
647 redistribute route type. */
648 void zclient_init(struct zclient
*zclient
, int redist_default
,
649 unsigned short instance
, struct zebra_privs_t
*privs
)
653 /* Set -1 to the default socket value. */
655 zclient
->privs
= privs
;
657 /* Clear redistribution flags. */
658 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
659 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
660 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
662 /* Set unwanted redistribute route. bgpd does not need BGP route
664 zclient
->redist_default
= redist_default
;
665 zclient
->instance
= instance
;
666 /* Pending: make afi(s) an arg. */
667 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
668 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
671 /* Set default-information redistribute to zero. */
672 zclient
->default_information
[afi
] = vrf_bitmap_init();
676 zlog_debug("scheduling zclient connection");
678 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
681 /* This function is a wrapper function for calling zclient_start from
682 timer or event thread. */
683 static int zclient_connect(struct thread
*t
)
685 struct zclient
*zclient
;
687 zclient
= THREAD_ARG(t
);
688 zclient
->t_connect
= NULL
;
691 zlog_debug("zclient_connect is called");
693 return zclient_start(zclient
);
696 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
697 bool exact_match
, vrf_id_t vrf_id
)
703 zclient_create_header(s
, command
, vrf_id
);
704 stream_putc(s
, (exact_match
) ? 1 : 0);
706 stream_putw(s
, PREFIX_FAMILY(p
));
707 stream_putc(s
, p
->prefixlen
);
708 switch (PREFIX_FAMILY(p
)) {
710 stream_put_in_addr(s
, &p
->u
.prefix4
);
713 stream_put(s
, &(p
->u
.prefix6
), 16);
718 stream_putw_at(s
, 0, stream_get_endp(s
));
720 return zclient_send_message(zclient
);
724 * "xdr_encode"-like interface that allows daemon (client) to send
725 * a message to zebra server for a route that needs to be
726 * added/deleted to the kernel. Info about the route is specified
727 * by the caller in a struct zapi_route. zapi_route_encode() then writes
728 * the info down the zclient socket using the stream_* functions.
730 * The corresponding read ("xdr_decode") function on the server
731 * side is zapi_route_decode().
733 * 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
734 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
735 * | Length (2) | Command | Route Type |
736 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
737 * | ZEBRA Flags | Message Flags | Prefix length |
738 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
739 * | Destination IPv4 Prefix for route |
740 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
745 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
746 * described, as per the Nexthop count. Each nexthop described as:
749 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
750 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
751 * | IPv4 Nexthop address or Interface Index number |
752 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
754 * Alternatively, if the route is a blackhole route, then Nexthop count
755 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
758 * The original struct zapi_route_*() infrastructure was built around
759 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
760 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
761 * nexthops into zapi_route_encode() using the same zapi_route structure.
762 * This is done by setting zapi_route fields as follows:
763 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
764 * - .nexthop_num == .ifindex_num
765 * - .nexthop and .ifindex are filled with gate and ifindex parts of
766 * each compound nexthop, both in the same order
768 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
771 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
774 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
776 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
778 * XXX: No attention paid to alignment.
780 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
781 struct zapi_route
*api
)
783 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
785 return zclient_send_message(zclient
);
788 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
789 const struct zapi_nexthop
*next2
)
791 if (next1
->label_num
> next2
->label_num
)
794 if (next1
->label_num
< next2
->label_num
)
797 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
800 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
801 const struct zapi_nexthop
*next2
)
805 if (next1
->vrf_id
< next2
->vrf_id
)
808 if (next1
->vrf_id
> next2
->vrf_id
)
811 if (next1
->type
< next2
->type
)
814 if (next1
->type
> next2
->type
)
817 if (next1
->weight
< next2
->weight
)
820 if (next1
->weight
> next2
->weight
)
823 switch (next1
->type
) {
824 case NEXTHOP_TYPE_IPV4
:
825 case NEXTHOP_TYPE_IPV6
:
826 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
831 case NEXTHOP_TYPE_IPV4_IFINDEX
:
832 case NEXTHOP_TYPE_IPV6_IFINDEX
:
833 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
837 /* Intentional Fall-Through */
838 case NEXTHOP_TYPE_IFINDEX
:
839 if (next1
->ifindex
< next2
->ifindex
)
842 if (next1
->ifindex
> next2
->ifindex
)
845 case NEXTHOP_TYPE_BLACKHOLE
:
846 if (next1
->bh_type
< next2
->bh_type
)
849 if (next1
->bh_type
> next2
->bh_type
)
857 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
861 const struct zapi_nexthop
*next1
= item1
;
862 const struct zapi_nexthop
*next2
= item2
;
864 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
868 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
873 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
874 uint16_t nexthop_num
)
876 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
881 * Encode a single zapi nexthop
883 int zapi_nexthop_encode(struct stream
*s
, const struct zapi_nexthop
*api_nh
,
887 int nh_flags
= api_nh
->flags
;
889 stream_putl(s
, api_nh
->vrf_id
);
890 stream_putc(s
, api_nh
->type
);
892 /* If needed, set 'labelled nexthop' flag */
893 if (api_nh
->label_num
> 0) {
894 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
896 /* Validate label count */
897 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
904 SET_FLAG(nh_flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
);
906 /* Note that we're only encoding a single octet */
907 stream_putc(s
, nh_flags
);
909 switch (api_nh
->type
) {
910 case NEXTHOP_TYPE_BLACKHOLE
:
911 stream_putc(s
, api_nh
->bh_type
);
913 case NEXTHOP_TYPE_IPV4
:
914 case NEXTHOP_TYPE_IPV4_IFINDEX
:
915 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
916 stream_putl(s
, api_nh
->ifindex
);
918 case NEXTHOP_TYPE_IFINDEX
:
919 stream_putl(s
, api_nh
->ifindex
);
921 case NEXTHOP_TYPE_IPV6
:
922 case NEXTHOP_TYPE_IPV6_IFINDEX
:
923 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
925 stream_putl(s
, api_nh
->ifindex
);
929 /* We only encode labels if we have >0 - we use
930 * the per-nexthop flag above to signal that the count
931 * is present in the payload.
933 if (api_nh
->label_num
> 0) {
934 stream_putc(s
, api_nh
->label_num
);
935 stream_put(s
, &api_nh
->labels
[0],
936 api_nh
->label_num
* sizeof(mpls_label_t
));
940 stream_putl(s
, api_nh
->weight
);
942 /* Router MAC for EVPN routes. */
943 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
944 stream_put(s
, &(api_nh
->rmac
),
945 sizeof(struct ethaddr
));
951 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
953 struct zapi_nexthop
*api_nh
;
958 zclient_create_header(s
, cmd
, api
->vrf_id
);
960 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
961 flog_err(EC_LIB_ZAPI_ENCODE
,
962 "%s: Specified route type (%u) is not a legal value\n",
963 __PRETTY_FUNCTION__
, api
->type
);
966 stream_putc(s
, api
->type
);
968 stream_putw(s
, api
->instance
);
969 stream_putl(s
, api
->flags
);
970 stream_putc(s
, api
->message
);
972 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
973 flog_err(EC_LIB_ZAPI_ENCODE
,
974 "%s: Specified route SAFI (%u) is not a legal value\n",
975 __PRETTY_FUNCTION__
, api
->safi
);
978 stream_putc(s
, api
->safi
);
980 /* Put prefix information. */
981 stream_putc(s
, api
->prefix
.family
);
982 psize
= PSIZE(api
->prefix
.prefixlen
);
983 stream_putc(s
, api
->prefix
.prefixlen
);
984 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
986 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
987 psize
= PSIZE(api
->src_prefix
.prefixlen
);
988 stream_putc(s
, api
->src_prefix
.prefixlen
);
989 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
993 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
994 /* limit the number of nexthops if necessary */
995 if (api
->nexthop_num
> MULTIPATH_NUM
) {
996 char buf
[PREFIX2STR_BUFFER
];
998 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1001 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
1002 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
1006 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
1008 stream_putw(s
, api
->nexthop_num
);
1010 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1011 api_nh
= &api
->nexthops
[i
];
1013 /* MPLS labels for BGP-LU or Segment Routing */
1014 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1015 char buf
[PREFIX2STR_BUFFER
];
1017 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
1019 flog_err(EC_LIB_ZAPI_ENCODE
,
1020 "%s: prefix %s: can't encode %u labels (maximum is %u)",
1027 if (zapi_nexthop_encode(s
, api_nh
, api
->flags
) != 0)
1033 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1034 stream_putc(s
, api
->distance
);
1035 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1036 stream_putl(s
, api
->metric
);
1037 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1038 stream_putl(s
, api
->tag
);
1039 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1040 stream_putl(s
, api
->mtu
);
1041 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1042 stream_putl(s
, api
->tableid
);
1044 /* Put length at the first point of the stream. */
1045 stream_putw_at(s
, 0, stream_get_endp(s
));
1051 * Decode a single zapi nexthop object
1053 static int zapi_nexthop_decode(struct stream
*s
, struct zapi_nexthop
*api_nh
,
1058 STREAM_GETL(s
, api_nh
->vrf_id
);
1059 STREAM_GETC(s
, api_nh
->type
);
1061 /* Note that we're only using a single octet of flags */
1062 STREAM_GETC(s
, api_nh
->flags
);
1064 switch (api_nh
->type
) {
1065 case NEXTHOP_TYPE_BLACKHOLE
:
1066 STREAM_GETC(s
, api_nh
->bh_type
);
1068 case NEXTHOP_TYPE_IPV4
:
1069 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1070 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1072 STREAM_GETL(s
, api_nh
->ifindex
);
1074 case NEXTHOP_TYPE_IFINDEX
:
1075 STREAM_GETL(s
, api_nh
->ifindex
);
1077 case NEXTHOP_TYPE_IPV6
:
1078 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1079 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1080 STREAM_GETL(s
, api_nh
->ifindex
);
1084 /* MPLS labels for BGP-LU or Segment Routing */
1085 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
)) {
1086 STREAM_GETC(s
, api_nh
->label_num
);
1087 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1090 "%s: invalid number of MPLS labels (%u)",
1091 __func__
, api_nh
->label_num
);
1095 STREAM_GET(&api_nh
->labels
[0], s
,
1096 api_nh
->label_num
* sizeof(mpls_label_t
));
1099 if (CHECK_FLAG(api_nh
->flags
, ZAPI_NEXTHOP_FLAG_WEIGHT
))
1100 STREAM_GETL(s
, api_nh
->weight
);
1102 /* Router MAC for EVPN routes. */
1103 if (CHECK_FLAG(api_flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1104 STREAM_GET(&(api_nh
->rmac
), s
,
1105 sizeof(struct ethaddr
));
1115 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
1117 struct zapi_nexthop
*api_nh
;
1120 memset(api
, 0, sizeof(*api
));
1122 /* Type, flags, message. */
1123 STREAM_GETC(s
, api
->type
);
1124 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1125 flog_err(EC_LIB_ZAPI_ENCODE
,
1126 "%s: Specified route type: %d is not a legal value\n",
1127 __PRETTY_FUNCTION__
, api
->type
);
1131 STREAM_GETW(s
, api
->instance
);
1132 STREAM_GETL(s
, api
->flags
);
1133 STREAM_GETC(s
, api
->message
);
1134 STREAM_GETC(s
, api
->safi
);
1135 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1136 flog_err(EC_LIB_ZAPI_ENCODE
,
1137 "%s: Specified route SAFI (%u) is not a legal value\n",
1138 __PRETTY_FUNCTION__
, api
->safi
);
1143 STREAM_GETC(s
, api
->prefix
.family
);
1144 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1145 switch (api
->prefix
.family
) {
1147 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1150 "%s: V4 prefixlen is %d which should not be more than 32",
1151 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1156 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1159 "%s: v6 prefixlen is %d which should not be more than 128",
1160 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1165 flog_err(EC_LIB_ZAPI_ENCODE
,
1166 "%s: Specified family %d is not v4 or v6",
1167 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1170 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1172 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1173 api
->src_prefix
.family
= AF_INET6
;
1174 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1175 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1178 "%s: SRC Prefix prefixlen received: %d is too large",
1179 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1182 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1183 PSIZE(api
->src_prefix
.prefixlen
));
1185 if (api
->prefix
.family
!= AF_INET6
1186 || api
->src_prefix
.prefixlen
== 0) {
1189 "%s: SRC prefix specified in some manner that makes no sense",
1190 __PRETTY_FUNCTION__
);
1196 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1197 STREAM_GETW(s
, api
->nexthop_num
);
1198 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1199 flog_err(EC_LIB_ZAPI_ENCODE
,
1200 "%s: invalid number of nexthops (%u)",
1201 __func__
, api
->nexthop_num
);
1205 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1206 api_nh
= &api
->nexthops
[i
];
1208 if (zapi_nexthop_decode(s
, api_nh
, api
->flags
) != 0)
1214 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1215 STREAM_GETC(s
, api
->distance
);
1216 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1217 STREAM_GETL(s
, api
->metric
);
1218 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1219 STREAM_GETL(s
, api
->tag
);
1220 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1221 STREAM_GETL(s
, api
->mtu
);
1222 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1223 STREAM_GETL(s
, api
->tableid
);
1230 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1236 memset(&any
, 0, sizeof(any
));
1237 any
.family
= family
;
1241 stream_putc(s
, p
->family
);
1242 stream_putc(s
, p
->prefixlen
);
1243 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1246 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1249 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1252 * We are sending one item at a time at the moment
1256 stream_putl(s
, zrule
->seq
);
1257 stream_putl(s
, zrule
->priority
);
1258 stream_putl(s
, zrule
->unique
);
1260 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1261 zrule
->filter
.src_ip
.family
);
1262 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1263 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1264 zrule
->filter
.src_ip
.family
);
1265 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1266 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1268 stream_putl(s
, zrule
->action
.table
);
1269 stream_putl(s
, zrule
->ifindex
);
1271 /* Put length at the first point of the stream. */
1272 stream_putw_at(s
, 0, stream_get_endp(s
));
1277 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1279 enum zapi_route_notify_owner
*note
)
1283 STREAM_GET(note
, s
, sizeof(*note
));
1285 STREAM_GETC(s
, p
->family
);
1286 STREAM_GETC(s
, p
->prefixlen
);
1287 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1298 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1299 uint32_t *priority
, uint32_t *unique
,
1301 enum zapi_rule_notify_owner
*note
)
1303 uint32_t prio
, seq
, uni
;
1306 STREAM_GET(note
, s
, sizeof(*note
));
1308 STREAM_GETL(s
, seq
);
1309 STREAM_GETL(s
, prio
);
1310 STREAM_GETL(s
, uni
);
1311 STREAM_GETL(s
, ifi
);
1314 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1327 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1328 enum zapi_ipset_notify_owner
*note
)
1332 STREAM_GET(note
, s
, sizeof(*note
));
1334 STREAM_GETL(s
, uni
);
1337 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1346 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1348 enum zapi_ipset_entry_notify_owner
*note
)
1352 STREAM_GET(note
, s
, sizeof(*note
));
1354 STREAM_GETL(s
, uni
);
1356 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1359 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1368 bool zapi_iptable_notify_decode(struct stream
*s
,
1370 enum zapi_iptable_notify_owner
*note
)
1374 STREAM_GET(note
, s
, sizeof(*note
));
1376 STREAM_GETL(s
, uni
);
1379 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1388 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1390 struct nexthop
*n
= nexthop_new();
1392 n
->type
= znh
->type
;
1393 n
->vrf_id
= znh
->vrf_id
;
1394 n
->ifindex
= znh
->ifindex
;
1395 n
->gate
= znh
->gate
;
1398 * This function currently handles labels
1400 if (znh
->label_num
) {
1401 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1409 * Convert nexthop to zapi nexthop
1411 int zapi_nexthop_from_nexthop(struct zapi_nexthop
*znh
,
1412 const struct nexthop
*nh
)
1416 memset(znh
, 0, sizeof(*znh
));
1418 znh
->type
= nh
->type
;
1419 znh
->vrf_id
= nh
->vrf_id
;
1420 znh
->ifindex
= nh
->ifindex
;
1421 znh
->gate
= nh
->gate
;
1423 if (nh
->nh_label
&& (nh
->nh_label
->num_labels
> 0)) {
1424 for (i
= 0; i
< nh
->nh_label
->num_labels
; i
++)
1425 znh
->labels
[i
] = nh
->nh_label
->label
[i
];
1428 SET_FLAG(znh
->flags
, ZAPI_NEXTHOP_FLAG_LABEL
);
1435 * Decode the nexthop-tracking update message
1437 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1441 memset(nhr
, 0, sizeof(*nhr
));
1443 STREAM_GETW(s
, nhr
->prefix
.family
);
1444 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1445 switch (nhr
->prefix
.family
) {
1447 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1450 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1456 STREAM_GETC(s
, nhr
->type
);
1457 STREAM_GETW(s
, nhr
->instance
);
1458 STREAM_GETC(s
, nhr
->distance
);
1459 STREAM_GETL(s
, nhr
->metric
);
1460 STREAM_GETC(s
, nhr
->nexthop_num
);
1462 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1463 if (zapi_nexthop_decode(s
, &(nhr
->nexthops
[i
]), 0) != 0)
1472 bool zapi_error_decode(struct stream
*s
, enum zebra_error_types
*error
)
1474 memset(error
, 0, sizeof(*error
));
1476 STREAM_GET(error
, s
, sizeof(*error
));
1479 zlog_debug("%s: type: %s", __func__
,
1480 zebra_error_type2str(*error
));
1488 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1489 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1490 * then set/unset redist[type] in the client handle (a struct zserv) for the
1493 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1494 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1501 zclient_create_header(s
, command
, vrf_id
);
1502 stream_putc(s
, afi
);
1503 stream_putc(s
, type
);
1504 stream_putw(s
, instance
);
1506 stream_putw_at(s
, 0, stream_get_endp(s
));
1508 return zclient_send_message(zclient
);
1511 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1512 afi_t afi
, vrf_id_t vrf_id
)
1519 zclient_create_header(s
, command
, vrf_id
);
1520 stream_putc(s
, afi
);
1522 stream_putw_at(s
, 0, stream_get_endp(s
));
1524 return zclient_send_message(zclient
);
1527 /* Get prefix in ZServ format; family should be filled in on prefix */
1528 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1530 size_t plen
= prefix_blen(p
);
1537 stream_get(&p
->u
.prefix
, s
, plen
);
1539 p
->prefixlen
= MIN(plen
* 8, c
);
1545 /* Router-id update from zebra daemon. */
1546 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1548 /* Fetch interface address. */
1549 STREAM_GETC(s
, rid
->family
);
1551 zclient_stream_get_prefix(s
, rid
);
1557 /* Interface addition from zebra daemon. */
1559 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1560 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1562 * 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
1563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1571 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1573 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1580 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1587 * | parent ifindex |
1588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1589 * | Link Layer Type |
1590 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1591 * | Harware Address Length |
1592 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1593 * | Hardware Address if HW lenght different from 0 |
1594 * | ... max INTERFACE_HWADDR_MAX |
1595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1596 * | Link_params? | Whether a link-params follows: 1 or 0.
1597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1598 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1599 * | .... (struct if_link_params). |
1600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1603 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1606 char vrfname_tmp
[VRF_NAMSIZ
];
1607 struct vrf_data data
;
1609 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1610 /* Read interface name. */
1611 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1613 /* Lookup/create vrf by vrf_id. */
1614 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1615 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1616 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1617 /* overwrite default vrf */
1618 if (vrf_id
== VRF_DEFAULT
)
1619 vrf_set_default_name(vrfname_tmp
, false);
1623 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1627 /* Lookup vrf by vrf_id. */
1628 vrf
= vrf_lookup_by_id(vrf_id
);
1631 * If a routing protocol doesn't know about a
1632 * vrf that is about to be deleted. There is
1633 * no point in attempting to delete it.
1641 static void zclient_interface_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1643 struct interface
*ifp
;
1644 char ifname_tmp
[INTERFACE_NAMSIZ
];
1645 struct stream
*s
= zclient
->ibuf
;
1647 /* Read interface name. */
1648 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1650 /* Lookup/create interface by name. */
1651 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1653 zebra_interface_if_set_value(s
, ifp
);
1655 if_new_via_zapi(ifp
);
1659 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1660 * from zebra server. The format of this message is the same as
1661 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
1662 * except that no sockaddr_dl is sent at the tail of the message.
1664 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1666 struct interface
*ifp
;
1667 char ifname_tmp
[INTERFACE_NAMSIZ
];
1669 /* Read interface name. */
1670 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1672 /* Lookup this by interface index. */
1673 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1675 flog_err(EC_LIB_ZAPI_ENCODE
,
1676 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1677 ifname_tmp
, vrf_id
);
1681 zebra_interface_if_set_value(s
, ifp
);
1686 static void zclient_interface_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1688 struct interface
*ifp
;
1689 struct stream
*s
= zclient
->ibuf
;
1691 ifp
= zebra_interface_state_read(s
, vrf_id
);
1696 if_destroy_via_zapi(ifp
);
1700 static void zclient_interface_up(struct zclient
*zclient
, vrf_id_t vrf_id
)
1702 struct interface
*ifp
;
1703 struct stream
*s
= zclient
->ibuf
;
1705 ifp
= zebra_interface_state_read(s
, vrf_id
);
1710 if_up_via_zapi(ifp
);
1713 static void zclient_interface_down(struct zclient
*zclient
, vrf_id_t vrf_id
)
1715 struct interface
*ifp
;
1716 struct stream
*s
= zclient
->ibuf
;
1718 ifp
= zebra_interface_state_read(s
, vrf_id
);
1723 if_down_via_zapi(ifp
);
1726 static void zclient_handle_error(ZAPI_CALLBACK_ARGS
)
1728 enum zebra_error_types error
;
1729 struct stream
*s
= zclient
->ibuf
;
1731 zapi_error_decode(s
, &error
);
1733 if (zclient
->handle_error
)
1734 (*zclient
->handle_error
)(error
);
1737 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1743 iflp
->lp_status
= stream_getl(s
);
1744 iflp
->te_metric
= stream_getl(s
);
1745 iflp
->max_bw
= stream_getf(s
);
1746 iflp
->max_rsv_bw
= stream_getf(s
);
1747 uint32_t bwclassnum
= stream_getl(s
);
1750 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1751 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1754 EC_LIB_ZAPI_MISSMATCH
,
1755 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1756 " - outdated library?",
1757 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1759 iflp
->admin_grp
= stream_getl(s
);
1760 iflp
->rmt_as
= stream_getl(s
);
1761 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1763 iflp
->av_delay
= stream_getl(s
);
1764 iflp
->min_delay
= stream_getl(s
);
1765 iflp
->max_delay
= stream_getl(s
);
1766 iflp
->delay_var
= stream_getl(s
);
1768 iflp
->pkt_loss
= stream_getf(s
);
1769 iflp
->res_bw
= stream_getf(s
);
1770 iflp
->ava_bw
= stream_getf(s
);
1771 iflp
->use_bw
= stream_getf(s
);
1774 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1777 struct if_link_params
*iflp
;
1782 ifindex
= stream_getl(s
);
1784 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1787 flog_err(EC_LIB_ZAPI_ENCODE
,
1788 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1793 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1796 link_params_set_value(s
, iflp
);
1801 static void zebra_interface_if_set_value(struct stream
*s
,
1802 struct interface
*ifp
)
1804 uint8_t link_params_status
= 0;
1805 ifindex_t old_ifindex
;
1807 old_ifindex
= ifp
->ifindex
;
1808 /* Read interface's index. */
1809 if_set_index(ifp
, stream_getl(s
));
1810 ifp
->status
= stream_getc(s
);
1812 /* Read interface's value. */
1813 ifp
->flags
= stream_getq(s
);
1814 ifp
->ptm_enable
= stream_getc(s
);
1815 ifp
->ptm_status
= stream_getc(s
);
1816 ifp
->metric
= stream_getl(s
);
1817 ifp
->speed
= stream_getl(s
);
1818 ifp
->mtu
= stream_getl(s
);
1819 ifp
->mtu6
= stream_getl(s
);
1820 ifp
->bandwidth
= stream_getl(s
);
1821 ifp
->link_ifindex
= stream_getl(s
);
1822 ifp
->ll_type
= stream_getl(s
);
1823 ifp
->hw_addr_len
= stream_getl(s
);
1824 if (ifp
->hw_addr_len
)
1825 stream_get(ifp
->hw_addr
, s
,
1826 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1828 /* Read Traffic Engineering status */
1829 link_params_status
= stream_getc(s
);
1830 /* Then, Traffic Engineering parameters if any */
1831 if (link_params_status
) {
1832 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1833 link_params_set_value(s
, iflp
);
1836 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1839 size_t zebra_interface_link_params_write(struct stream
*s
,
1840 struct interface
*ifp
)
1843 struct if_link_params
*iflp
;
1846 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1849 iflp
= ifp
->link_params
;
1852 w
+= stream_putl(s
, iflp
->lp_status
);
1854 w
+= stream_putl(s
, iflp
->te_metric
);
1855 w
+= stream_putf(s
, iflp
->max_bw
);
1856 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1858 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1859 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1860 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1862 w
+= stream_putl(s
, iflp
->admin_grp
);
1863 w
+= stream_putl(s
, iflp
->rmt_as
);
1864 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1866 w
+= stream_putl(s
, iflp
->av_delay
);
1867 w
+= stream_putl(s
, iflp
->min_delay
);
1868 w
+= stream_putl(s
, iflp
->max_delay
);
1869 w
+= stream_putl(s
, iflp
->delay_var
);
1871 w
+= stream_putf(s
, iflp
->pkt_loss
);
1872 w
+= stream_putf(s
, iflp
->res_bw
);
1873 w
+= stream_putf(s
, iflp
->ava_bw
);
1874 w
+= stream_putf(s
, iflp
->use_bw
);
1880 * format of message for address additon is:
1884 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1885 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1894 * | ifc_flags | flags for connected address
1902 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1910 static int memconstant(const void *s
, int c
, size_t n
)
1912 const uint8_t *p
= s
;
1921 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1925 struct interface
*ifp
;
1926 struct connected
*ifc
;
1927 struct prefix p
, d
, *dp
;
1931 memset(&p
, 0, sizeof(p
));
1932 memset(&d
, 0, sizeof(d
));
1934 /* Get interface index. */
1935 ifindex
= stream_getl(s
);
1938 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1940 flog_err(EC_LIB_ZAPI_ENCODE
,
1941 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1942 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1948 ifc_flags
= stream_getc(s
);
1950 /* Fetch interface address. */
1951 d
.family
= p
.family
= stream_getc(s
);
1952 plen
= prefix_blen(&d
);
1954 zclient_stream_get_prefix(s
, &p
);
1956 /* Fetch destination address. */
1957 stream_get(&d
.u
.prefix
, s
, plen
);
1959 /* N.B. NULL destination pointers are encoded as all zeroes */
1960 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1962 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1963 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1965 /* N.B. NULL destination pointers are encoded as all
1967 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1970 ifc
->flags
= ifc_flags
;
1971 if (ifc
->destination
)
1972 ifc
->destination
->prefixlen
=
1973 ifc
->address
->prefixlen
;
1974 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1975 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1977 char buf
[PREFIX_STRLEN
];
1980 "warning: interface %s address %s with peer flag set, but no peer address!",
1982 prefix2str(ifc
->address
, buf
,
1984 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1988 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1989 ifc
= connected_delete_by_prefix(ifp
, &p
);
1996 * format of message for neighbor connected address is:
2000 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
2001 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
2016 * | addr_len | len of addr.
2019 struct nbr_connected
*
2020 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
2022 unsigned int ifindex
;
2023 struct interface
*ifp
;
2025 struct nbr_connected
*ifc
;
2027 /* Get interface index. */
2028 ifindex
= stream_getl(s
);
2031 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
2033 flog_err(EC_LIB_ZAPI_ENCODE
,
2034 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
2035 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
2041 p
.family
= stream_getc(s
);
2042 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
2043 p
.prefixlen
= stream_getc(s
);
2045 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
2046 /* Currently only supporting P2P links, so any new RA source
2048 considered as the replacement of the previously learnt
2049 Link-Local address. */
2050 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
2051 ifc
= nbr_connected_new();
2052 ifc
->address
= prefix_new();
2054 listnode_add(ifp
->nbr_connected
, ifc
);
2057 prefix_copy(ifc
->address
, &p
);
2059 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
2061 ifc
= nbr_connected_check(ifp
, &p
);
2063 listnode_delete(ifp
->nbr_connected
, ifc
);
2069 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
2071 vrf_id_t
*new_vrf_id
)
2073 char ifname
[INTERFACE_NAMSIZ
];
2074 struct interface
*ifp
;
2077 /* Read interface name. */
2078 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
2080 /* Lookup interface. */
2081 ifp
= if_lookup_by_name(ifname
, vrf_id
);
2083 flog_err(EC_LIB_ZAPI_ENCODE
,
2084 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
2089 /* Fetch new VRF Id. */
2090 new_id
= stream_getl(s
);
2092 *new_vrf_id
= new_id
;
2096 /* filter unwanted messages until the expected one arrives */
2097 static int zclient_read_sync_response(struct zclient
*zclient
,
2098 uint16_t expected_cmd
)
2110 cmd
= expected_cmd
+ 1;
2111 while (ret
== 0 && cmd
!= expected_cmd
) {
2115 /* wait until response arrives */
2117 FD_SET(zclient
->sock
, &readfds
);
2118 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2119 if (!FD_ISSET(zclient
->sock
, &readfds
))
2122 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2123 &version
, &vrf_id
, &cmd
);
2125 zlog_debug("%s: Response (%d bytes) received", __func__
,
2129 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2137 * Connect to label manager in a syncronous way
2139 * It first writes the request to zcient output buffer and then
2140 * immediately reads the answer from the input buffer.
2142 * @param zclient Zclient used to connect to label manager (zebra)
2143 * @param async Synchronous (0) or asynchronous (1) operation
2144 * @result Result of response
2146 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2151 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2152 ZEBRA_LABEL_MANAGER_CONNECT
;
2155 zlog_debug("Connecting to Label Manager (LM)");
2157 if (zclient
->sock
< 0) {
2158 zlog_debug("%s: invalid zclient socket", __func__
);
2165 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2168 stream_putc(s
, zclient
->redist_default
);
2170 stream_putw(s
, zclient
->instance
);
2172 /* Put length at the first point of the stream. */
2173 stream_putw_at(s
, 0, stream_get_endp(s
));
2175 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2177 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2178 close(zclient
->sock
);
2183 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2184 close(zclient
->sock
);
2189 zlog_debug("LM connect request sent (%d bytes)", ret
);
2195 if (zclient_read_sync_response(zclient
, cmd
)
2201 /* read instance and proto */
2202 uint8_t proto
= stream_getc(s
);
2203 uint16_t instance
= stream_getw(s
);
2206 if (proto
!= zclient
->redist_default
)
2209 "Wrong proto (%u) in LM connect response. Should be %u",
2210 proto
, zclient
->redist_default
);
2211 if (instance
!= zclient
->instance
)
2214 "Wrong instId (%u) in LM connect response. Should be %u",
2215 instance
, zclient
->instance
);
2218 result
= stream_getc(s
);
2220 zlog_debug("LM connect-response received, result %u", result
);
2226 * Asynchronous label chunk request
2228 * @param zclient Zclient used to connect to label manager (zebra)
2229 * @param keep Avoid garbage collection
2230 * @param chunk_size Amount of labels requested
2231 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2232 * @result 0 on success, -1 otherwise
2234 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2235 uint32_t chunk_size
, uint32_t base
)
2240 zlog_debug("Getting Label Chunk");
2242 if (zclient
->sock
< 0)
2248 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2250 stream_putc(s
, zclient
->redist_default
);
2252 stream_putw(s
, zclient
->instance
);
2253 stream_putc(s
, keep
);
2254 stream_putl(s
, chunk_size
);
2255 stream_putl(s
, base
);
2257 /* Put length at the first point of the stream. */
2258 stream_putw_at(s
, 0, stream_get_endp(s
));
2260 return zclient_send_message(zclient
);
2264 * Function to request a label chunk in a syncronous way
2266 * It first writes the request to zlcient output buffer and then
2267 * immediately reads the answer from the input buffer.
2269 * @param zclient Zclient used to connect to label manager (zebra)
2270 * @param keep Avoid garbage collection
2271 * @param chunk_size Amount of labels requested
2272 * @param start To write first assigned chunk label to
2273 * @param end To write last assigned chunk label to
2274 * @result 0 on success, -1 otherwise
2276 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2277 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2281 uint8_t response_keep
;
2284 zlog_debug("Getting Label Chunk");
2286 if (zclient
->sock
< 0)
2292 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2294 stream_putc(s
, zclient
->redist_default
);
2296 stream_putw(s
, zclient
->instance
);
2298 stream_putc(s
, keep
);
2300 stream_putl(s
, chunk_size
);
2301 /* requested chunk base */
2302 stream_putl(s
, base
);
2303 /* Put length at the first point of the stream. */
2304 stream_putw_at(s
, 0, stream_get_endp(s
));
2306 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2308 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2309 close(zclient
->sock
);
2314 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2315 close(zclient
->sock
);
2320 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2323 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2326 /* parse response */
2329 /* read proto and instance */
2330 uint8_t proto
= stream_getc(s
);
2331 uint16_t instance
= stream_getw(s
);
2334 if (proto
!= zclient
->redist_default
)
2335 flog_err(EC_LIB_ZAPI_ENCODE
,
2336 "Wrong proto (%u) in get chunk response. Should be %u",
2337 proto
, zclient
->redist_default
);
2338 if (instance
!= zclient
->instance
)
2339 flog_err(EC_LIB_ZAPI_ENCODE
,
2340 "Wrong instId (%u) in get chunk response Should be %u",
2341 instance
, zclient
->instance
);
2343 /* if we requested a specific chunk and it could not be allocated, the
2344 * response message will end here
2346 if (!STREAM_READABLE(s
)) {
2347 zlog_info("Unable to assign Label Chunk to %s instance %u",
2348 zebra_route_string(proto
), instance
);
2353 response_keep
= stream_getc(s
);
2354 /* start and end labels */
2355 *start
= stream_getl(s
);
2356 *end
= stream_getl(s
);
2358 /* not owning this response */
2359 if (keep
!= response_keep
) {
2362 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2363 *start
, *end
, keep
, response_keep
);
2366 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2367 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2368 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2374 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2381 * Function to release a label chunk
2383 * @param zclient Zclient used to connect to label manager (zebra)
2384 * @param start First label of chunk
2385 * @param end Last label of chunk
2386 * @result 0 on success, -1 otherwise
2388 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2395 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2397 if (zclient
->sock
< 0)
2403 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2406 stream_putc(s
, zclient
->redist_default
);
2408 stream_putw(s
, zclient
->instance
);
2410 stream_putl(s
, start
);
2412 stream_putl(s
, end
);
2414 /* Put length at the first point of the stream. */
2415 stream_putw_at(s
, 0, stream_get_endp(s
));
2417 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2419 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2420 close(zclient
->sock
);
2425 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2426 close(zclient
->sock
);
2435 * Connect to table manager in a syncronous way
2437 * It first writes the request to zcient output buffer and then
2438 * immediately reads the answer from the input buffer.
2440 * @param zclient Zclient used to connect to table manager (zebra)
2441 * @result Result of response
2443 int tm_table_manager_connect(struct zclient
*zclient
)
2450 zlog_debug("Connecting to Table Manager");
2452 if (zclient
->sock
< 0)
2458 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2461 stream_putc(s
, zclient
->redist_default
);
2463 stream_putw(s
, zclient
->instance
);
2465 /* Put length at the first point of the stream. */
2466 stream_putw_at(s
, 0, stream_get_endp(s
));
2468 ret
= zclient_send_message(zclient
);
2473 zlog_debug("%s: Table manager connect request sent", __func__
);
2476 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2482 STREAM_GETC(s
, result
);
2485 "%s: Table Manager connect response received, result %u",
2494 * Function to request a table chunk in a syncronous way
2496 * It first writes the request to zclient output buffer and then
2497 * immediately reads the answer from the input buffer.
2499 * @param zclient Zclient used to connect to table manager (zebra)
2500 * @param chunk_size Amount of table requested
2501 * @param start to write first assigned chunk table RT ID to
2502 * @param end To write last assigned chunk table RT ID to
2503 * @result 0 on success, -1 otherwise
2505 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2506 uint32_t *start
, uint32_t *end
)
2512 zlog_debug("Getting Table Chunk");
2514 if (zclient
->sock
< 0)
2520 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2522 stream_putl(s
, chunk_size
);
2523 /* Put length at the first point of the stream. */
2524 stream_putw_at(s
, 0, stream_get_endp(s
));
2526 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2528 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2530 close(zclient
->sock
);
2535 flog_err(EC_LIB_ZAPI_SOCKET
,
2536 "%s: zclient->sock connection closed", __func__
);
2537 close(zclient
->sock
);
2542 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2546 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2550 /* start and end table IDs */
2551 STREAM_GETL(s
, *start
);
2552 STREAM_GETL(s
, *end
);
2555 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2563 * Function to release a table chunk
2565 * @param zclient Zclient used to connect to table manager (zebra)
2566 * @param start First label of table
2567 * @param end Last label of chunk
2568 * @result 0 on success, -1 otherwise
2570 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2576 zlog_debug("Releasing Table Chunk");
2578 if (zclient
->sock
< 0)
2584 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2587 stream_putl(s
, start
);
2589 stream_putl(s
, end
);
2591 /* Put length at the first point of the stream. */
2592 stream_putw_at(s
, 0, stream_get_endp(s
));
2594 return zclient_send_message(zclient
);
2597 int zebra_send_mpls_labels(struct zclient
*zclient
, int cmd
,
2598 struct zapi_labels
*zl
)
2600 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
2602 return zclient_send_message(zclient
);
2605 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
2607 struct zapi_nexthop_label
*znh
;
2611 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2612 stream_putc(s
, zl
->message
);
2613 stream_putc(s
, zl
->type
);
2614 stream_putl(s
, zl
->local_label
);
2616 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2617 stream_putw(s
, zl
->route
.prefix
.family
);
2618 stream_put_prefix(s
, &zl
->route
.prefix
);
2619 stream_putc(s
, zl
->route
.type
);
2620 stream_putw(s
, zl
->route
.instance
);
2623 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
2626 "%s: label %u: can't encode %u nexthops (maximum is %u)",
2627 __func__
, zl
->local_label
, zl
->nexthop_num
,
2631 stream_putw(s
, zl
->nexthop_num
);
2633 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2634 znh
= &zl
->nexthops
[i
];
2636 stream_putc(s
, znh
->type
);
2637 stream_putw(s
, znh
->family
);
2638 switch (znh
->family
) {
2640 stream_put_in_addr(s
, &znh
->address
.ipv4
);
2643 stream_write(s
, (uint8_t *)&znh
->address
.ipv6
, 16);
2648 stream_putl(s
, znh
->ifindex
);
2649 stream_putl(s
, znh
->label
);
2652 /* Put length at the first point of the stream. */
2653 stream_putw_at(s
, 0, stream_get_endp(s
));
2658 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
2660 struct zapi_nexthop_label
*znh
;
2662 memset(zl
, 0, sizeof(*zl
));
2665 STREAM_GETC(s
, zl
->message
);
2666 STREAM_GETC(s
, zl
->type
);
2667 STREAM_GETL(s
, zl
->local_label
);
2669 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2672 STREAM_GETW(s
, zl
->route
.prefix
.family
);
2673 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
2675 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
2676 switch (zl
->route
.prefix
.family
) {
2678 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2680 "%s: Specified prefix length %d is greater than a v4 address can support",
2681 __PRETTY_FUNCTION__
,
2682 zl
->route
.prefix
.prefixlen
);
2685 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
2689 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2691 "%s: Specified prefix length %d is greater than a v6 address can support",
2692 __PRETTY_FUNCTION__
,
2693 zl
->route
.prefix
.prefixlen
);
2696 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
2699 flog_err(EC_LIB_ZAPI_ENCODE
,
2700 "%s: Specified family %u is not v4 or v6",
2701 __PRETTY_FUNCTION__
, zl
->route
.prefix
.family
);
2705 STREAM_GETC(s
, zl
->route
.type
);
2706 STREAM_GETW(s
, zl
->route
.instance
);
2709 STREAM_GETW(s
, zl
->nexthop_num
);
2711 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
2714 "%s: Prefix %pFX has %d nexthops, but we can only use the first %d",
2715 __func__
, &zl
->route
.prefix
, zl
->nexthop_num
,
2719 zl
->nexthop_num
= MIN(MULTIPATH_NUM
, zl
->nexthop_num
);
2721 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2722 znh
= &zl
->nexthops
[i
];
2724 STREAM_GETC(s
, znh
->type
);
2725 STREAM_GETW(s
, znh
->family
);
2726 switch (znh
->family
) {
2728 STREAM_GET(&znh
->address
.ipv4
.s_addr
, s
,
2732 STREAM_GET(&znh
->address
.ipv6
, s
, 16);
2737 STREAM_GETL(s
, znh
->ifindex
);
2738 STREAM_GETL(s
, znh
->label
);
2746 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2754 zclient_create_header(s
, command
, VRF_DEFAULT
);
2755 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2756 stream_putl(s
, pw
->ifindex
);
2759 stream_putl(s
, pw
->type
);
2762 stream_putl(s
, pw
->af
);
2765 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2768 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2771 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2776 stream_putl(s
, pw
->local_label
);
2777 stream_putl(s
, pw
->remote_label
);
2780 stream_putc(s
, pw
->flags
);
2782 /* Protocol specific fields */
2783 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2785 /* Put length at the first point of the stream. */
2786 stream_putw_at(s
, 0, stream_get_endp(s
));
2788 return zclient_send_message(zclient
);
2792 * Receive PW status update from Zebra and send it to LDE process.
2794 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
2798 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2802 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2803 pw
->ifindex
= stream_getl(s
);
2804 pw
->status
= stream_getl(s
);
2807 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
2809 struct zclient_capabilities cap
;
2810 struct stream
*s
= zclient
->ibuf
;
2812 uint8_t mpls_enabled
;
2814 STREAM_GETL(s
, vrf_backend
);
2815 vrf_configure_backend(vrf_backend
);
2817 memset(&cap
, 0, sizeof(cap
));
2818 STREAM_GETC(s
, mpls_enabled
);
2819 cap
.mpls_enabled
= !!mpls_enabled
;
2820 STREAM_GETL(s
, cap
.ecmp
);
2821 STREAM_GETC(s
, cap
.role
);
2823 if (zclient
->zebra_capabilities
)
2824 (*zclient
->zebra_capabilities
)(&cap
);
2830 void zclient_send_mlag_register(struct zclient
*client
, uint32_t bit_map
)
2837 zclient_create_header(s
, ZEBRA_MLAG_CLIENT_REGISTER
, VRF_DEFAULT
);
2838 stream_putl(s
, bit_map
);
2840 stream_putw_at(s
, 0, stream_get_endp(s
));
2841 zclient_send_message(client
);
2844 void zclient_send_mlag_deregister(struct zclient
*client
)
2846 zebra_message_send(client
, ZEBRA_MLAG_CLIENT_UNREGISTER
, VRF_DEFAULT
);
2849 void zclient_send_mlag_data(struct zclient
*client
, struct stream
*client_s
)
2856 zclient_create_header(s
, ZEBRA_MLAG_FORWARD_MSG
, VRF_DEFAULT
);
2857 stream_put(s
, client_s
->data
, client_s
->endp
);
2859 stream_putw_at(s
, 0, stream_get_endp(s
));
2860 zclient_send_message(client
);
2863 static void zclient_mlag_process_up(ZAPI_CALLBACK_ARGS
)
2865 if (zclient
->mlag_process_up
)
2866 (*zclient
->mlag_process_up
)();
2869 static void zclient_mlag_process_down(ZAPI_CALLBACK_ARGS
)
2871 if (zclient
->mlag_process_down
)
2872 (*zclient
->mlag_process_down
)();
2875 static void zclient_mlag_handle_msg(ZAPI_CALLBACK_ARGS
)
2877 if (zclient
->mlag_handle_msg
)
2878 (*zclient
->mlag_handle_msg
)(zclient
->ibuf
, length
);
2881 /* Zebra client message read function. */
2882 static int zclient_read(struct thread
*thread
)
2885 uint16_t length
, command
;
2886 uint8_t marker
, version
;
2888 struct zclient
*zclient
;
2890 /* Get socket to zebra. */
2891 zclient
= THREAD_ARG(thread
);
2892 zclient
->t_read
= NULL
;
2894 /* Read zebra header (if we don't have it already). */
2895 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2897 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2898 ZEBRA_HEADER_SIZE
- already
))
2903 "zclient connection closed socket [%d].",
2905 return zclient_failed(zclient
);
2907 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2908 /* Try again later. */
2909 zclient_event(ZCLIENT_READ
, zclient
);
2912 already
= ZEBRA_HEADER_SIZE
;
2915 /* Reset to read from the beginning of the incoming packet. */
2916 stream_set_getp(zclient
->ibuf
, 0);
2918 /* Fetch header values. */
2919 length
= stream_getw(zclient
->ibuf
);
2920 marker
= stream_getc(zclient
->ibuf
);
2921 version
= stream_getc(zclient
->ibuf
);
2922 vrf_id
= stream_getl(zclient
->ibuf
);
2923 command
= stream_getw(zclient
->ibuf
);
2925 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2927 EC_LIB_ZAPI_MISSMATCH
,
2928 "%s: socket %d version mismatch, marker %d, version %d",
2929 __func__
, zclient
->sock
, marker
, version
);
2930 return zclient_failed(zclient
);
2933 if (length
< ZEBRA_HEADER_SIZE
) {
2934 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2935 "%s: socket %d message length %u is less than %d ",
2936 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2937 return zclient_failed(zclient
);
2941 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2945 "%s: message size %u exceeds buffer size %lu, expanding...",
2947 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2948 ns
= stream_new(length
);
2949 stream_copy(ns
, zclient
->ibuf
);
2950 stream_free(zclient
->ibuf
);
2954 /* Read rest of zebra packet. */
2955 if (already
< length
) {
2957 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2963 "zclient connection closed socket [%d].",
2965 return zclient_failed(zclient
);
2967 if (nbyte
!= (ssize_t
)(length
- already
)) {
2968 /* Try again later. */
2969 zclient_event(ZCLIENT_READ
, zclient
);
2974 length
-= ZEBRA_HEADER_SIZE
;
2977 zlog_debug("zclient 0x%p command %s VRF %u",
2978 (void *)zclient
, zserv_command_string(command
),
2982 case ZEBRA_CAPABILITIES
:
2983 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2985 case ZEBRA_ROUTER_ID_UPDATE
:
2986 if (zclient
->router_id_update
)
2987 (*zclient
->router_id_update
)(command
, zclient
, length
,
2991 zclient_vrf_add(zclient
, vrf_id
);
2993 case ZEBRA_VRF_DELETE
:
2994 zclient_vrf_delete(zclient
, vrf_id
);
2996 case ZEBRA_INTERFACE_ADD
:
2997 zclient_interface_add(zclient
, vrf_id
);
2999 case ZEBRA_INTERFACE_DELETE
:
3000 zclient_interface_delete(zclient
, vrf_id
);
3002 case ZEBRA_INTERFACE_ADDRESS_ADD
:
3003 if (zclient
->interface_address_add
)
3004 (*zclient
->interface_address_add
)(command
, zclient
,
3007 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
3008 if (zclient
->interface_address_delete
)
3009 (*zclient
->interface_address_delete
)(command
, zclient
,
3012 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
3013 if (zclient
->interface_bfd_dest_update
)
3014 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
3017 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
3018 if (zclient
->interface_nbr_address_add
)
3019 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
3022 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
3023 if (zclient
->interface_nbr_address_delete
)
3024 (*zclient
->interface_nbr_address_delete
)(
3025 command
, zclient
, length
, vrf_id
);
3027 case ZEBRA_INTERFACE_UP
:
3028 zclient_interface_up(zclient
, vrf_id
);
3030 case ZEBRA_INTERFACE_DOWN
:
3031 zclient_interface_down(zclient
, vrf_id
);
3033 case ZEBRA_INTERFACE_VRF_UPDATE
:
3034 if (zclient
->interface_vrf_update
)
3035 (*zclient
->interface_vrf_update
)(command
, zclient
,
3038 case ZEBRA_NEXTHOP_UPDATE
:
3040 zlog_debug("zclient rcvd nexthop update");
3041 if (zclient
->nexthop_update
)
3042 (*zclient
->nexthop_update
)(command
, zclient
, length
,
3045 case ZEBRA_IMPORT_CHECK_UPDATE
:
3047 zlog_debug("zclient rcvd import check update");
3048 if (zclient
->import_check_update
)
3049 (*zclient
->import_check_update
)(command
, zclient
,
3052 case ZEBRA_BFD_DEST_REPLAY
:
3053 if (zclient
->bfd_dest_replay
)
3054 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
3057 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
3058 if (zclient
->redistribute_route_add
)
3059 (*zclient
->redistribute_route_add
)(command
, zclient
,
3062 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
3063 if (zclient
->redistribute_route_del
)
3064 (*zclient
->redistribute_route_del
)(command
, zclient
,
3067 case ZEBRA_INTERFACE_LINK_PARAMS
:
3068 if (zclient
->interface_link_params
)
3069 (*zclient
->interface_link_params
)(command
, zclient
,
3072 case ZEBRA_FEC_UPDATE
:
3074 zlog_debug("zclient rcvd fec update");
3075 if (zclient
->fec_update
)
3076 (*zclient
->fec_update
)(command
, zclient
, length
);
3078 case ZEBRA_LOCAL_ES_ADD
:
3079 if (zclient
->local_es_add
)
3080 (*zclient
->local_es_add
)(command
, zclient
, length
,
3083 case ZEBRA_LOCAL_ES_DEL
:
3084 if (zclient
->local_es_del
)
3085 (*zclient
->local_es_del
)(command
, zclient
, length
,
3089 if (zclient
->local_vni_add
)
3090 (*zclient
->local_vni_add
)(command
, zclient
, length
,
3094 if (zclient
->local_vni_del
)
3095 (*zclient
->local_vni_del
)(command
, zclient
, length
,
3098 case ZEBRA_L3VNI_ADD
:
3099 if (zclient
->local_l3vni_add
)
3100 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
3103 case ZEBRA_L3VNI_DEL
:
3104 if (zclient
->local_l3vni_del
)
3105 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
3108 case ZEBRA_MACIP_ADD
:
3109 if (zclient
->local_macip_add
)
3110 (*zclient
->local_macip_add
)(command
, zclient
, length
,
3113 case ZEBRA_MACIP_DEL
:
3114 if (zclient
->local_macip_del
)
3115 (*zclient
->local_macip_del
)(command
, zclient
, length
,
3118 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
3119 if (zclient
->local_ip_prefix_add
)
3120 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
3123 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
3124 if (zclient
->local_ip_prefix_del
)
3125 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
3128 case ZEBRA_PW_STATUS_UPDATE
:
3129 if (zclient
->pw_status_update
)
3130 (*zclient
->pw_status_update
)(command
, zclient
, length
,
3133 case ZEBRA_ROUTE_NOTIFY_OWNER
:
3134 if (zclient
->route_notify_owner
)
3135 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
3138 case ZEBRA_RULE_NOTIFY_OWNER
:
3139 if (zclient
->rule_notify_owner
)
3140 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
3143 case ZEBRA_GET_LABEL_CHUNK
:
3144 if (zclient
->label_chunk
)
3145 (*zclient
->label_chunk
)(command
, zclient
, length
,
3148 case ZEBRA_IPSET_NOTIFY_OWNER
:
3149 if (zclient
->ipset_notify_owner
)
3150 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
3153 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
3154 if (zclient
->ipset_entry_notify_owner
)
3155 (*zclient
->ipset_entry_notify_owner
)(command
,
3159 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
3160 if (zclient
->iptable_notify_owner
)
3161 (*zclient
->iptable_notify_owner
)(command
,
3165 case ZEBRA_VXLAN_SG_ADD
:
3166 if (zclient
->vxlan_sg_add
)
3167 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
3170 case ZEBRA_VXLAN_SG_DEL
:
3171 if (zclient
->vxlan_sg_del
)
3172 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
3175 case ZEBRA_MLAG_PROCESS_UP
:
3176 zclient_mlag_process_up(command
, zclient
, length
, vrf_id
);
3178 case ZEBRA_MLAG_PROCESS_DOWN
:
3179 zclient_mlag_process_down(command
, zclient
, length
, vrf_id
);
3181 case ZEBRA_MLAG_FORWARD_MSG
:
3182 zclient_mlag_handle_msg(command
, zclient
, length
, vrf_id
);
3185 zclient_handle_error(command
, zclient
, length
, vrf_id
);
3190 if (zclient
->sock
< 0)
3191 /* Connection was closed during packet processing. */
3194 /* Register read thread. */
3195 stream_reset(zclient
->ibuf
);
3196 zclient_event(ZCLIENT_READ
, zclient
);
3201 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
3202 int type
, unsigned short instance
, vrf_id_t vrf_id
)
3206 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3207 if (redist_check_instance(
3208 &zclient
->mi_redist
[afi
][type
], instance
))
3210 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3213 if (!redist_check_instance(
3214 &zclient
->mi_redist
[afi
][type
], instance
))
3216 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3221 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3222 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
3225 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3227 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
3230 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3234 if (zclient
->sock
> 0)
3235 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
3240 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
3241 afi_t afi
, vrf_id_t vrf_id
)
3244 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
3245 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
3247 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
3249 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
3252 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
3255 if (zclient
->sock
> 0)
3256 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
3259 static void zclient_event(enum event event
, struct zclient
*zclient
)
3262 case ZCLIENT_SCHEDULE
:
3263 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
3264 &zclient
->t_connect
);
3266 case ZCLIENT_CONNECT
:
3269 "zclient connect failures: %d schedule interval is now %d",
3270 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
3271 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
3272 zclient
->fail
< 3 ? 10 : 60,
3273 &zclient
->t_connect
);
3276 zclient
->t_read
= NULL
;
3277 thread_add_read(zclient
->master
, zclient_read
, zclient
,
3278 zclient
->sock
, &zclient
->t_read
);
3283 void zclient_interface_set_master(struct zclient
*client
,
3284 struct interface
*master
,
3285 struct interface
*slave
)
3292 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
3294 stream_putl(s
, master
->vrf_id
);
3295 stream_putl(s
, master
->ifindex
);
3296 stream_putl(s
, slave
->vrf_id
);
3297 stream_putl(s
, slave
->ifindex
);
3299 stream_putw_at(s
, 0, stream_get_endp(s
));
3300 zclient_send_message(client
);
3304 * Send capabilities message to zebra
3306 int32_t zclient_capabilities_send(uint32_t cmd
, struct zclient
*zclient
,
3307 struct zapi_cap
*api
)
3312 if (zclient
== NULL
)
3317 zclient_create_header(s
, cmd
, 0);
3318 stream_putl(s
, api
->cap
);
3321 case ZEBRA_CLIENT_GR_CAPABILITIES
:
3322 case ZEBRA_CLIENT_RIB_STALE_TIME
:
3323 stream_putl(s
, api
->stale_removal_time
);
3324 stream_putl(s
, api
->vrf_id
);
3326 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
3327 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
3328 stream_putl(s
, api
->afi
);
3329 stream_putl(s
, api
->safi
);
3330 stream_putl(s
, api
->vrf_id
);
3332 case ZEBRA_CLIENT_GR_DISABLE
:
3333 stream_putl(s
, api
->vrf_id
);
3337 /* Put length at the first point of the stream */
3338 stream_putw_at(s
, 0, stream_get_endp(s
));
3340 return zclient_send_message(zclient
);
3344 * Process capabilities message from zebra
3346 int32_t zapi_capabilities_decode(struct stream
*s
, struct zapi_cap
*api
)
3349 memset(api
, 0, sizeof(*api
));
3351 STREAM_GETL(s
, api
->cap
);
3353 case ZEBRA_CLIENT_GR_CAPABILITIES
:
3354 case ZEBRA_CLIENT_RIB_STALE_TIME
:
3355 STREAM_GETL(s
, api
->stale_removal_time
);
3356 STREAM_GETL(s
, api
->vrf_id
);
3358 case ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE
:
3359 case ZEBRA_CLIENT_ROUTE_UPDATE_PENDING
:
3360 STREAM_GETL(s
, api
->afi
);
3361 STREAM_GETL(s
, api
->safi
);
3362 STREAM_GETL(s
, api
->vrf_id
);
3364 case ZEBRA_CLIENT_GR_DISABLE
:
3365 STREAM_GETL(s
, api
->vrf_id
);