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
);
296 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
298 /* length placeholder, caller can update */
299 stream_putw(s
, ZEBRA_HEADER_SIZE
);
300 stream_putc(s
, ZEBRA_HEADER_MARKER
);
301 stream_putc(s
, ZSERV_VERSION
);
302 stream_putl(s
, vrf_id
);
303 stream_putw(s
, command
);
306 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
307 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
310 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
313 STREAM_GETW(s
, *size
);
314 *size
-= ZEBRA_HEADER_SIZE
;
315 STREAM_GETC(s
, *marker
);
316 STREAM_GETC(s
, *version
);
317 STREAM_GETL(s
, *vrf_id
);
318 STREAM_GETW(s
, *cmd
);
320 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
322 EC_LIB_ZAPI_MISSMATCH
,
323 "%s: socket %d version mismatch, marker %d, version %d",
324 __func__
, sock
, *marker
, *version
);
328 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
336 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
338 STREAM_GETW(zmsg
, hdr
->length
);
339 STREAM_GETC(zmsg
, hdr
->marker
);
340 STREAM_GETC(zmsg
, hdr
->version
);
341 STREAM_GETL(zmsg
, hdr
->vrf_id
);
342 STREAM_GETW(zmsg
, hdr
->command
);
348 /* Send simple Zebra message. */
349 static int zebra_message_send(struct zclient
*zclient
, int command
,
354 /* Get zclient output buffer. */
358 /* Send very simple command only Zebra message. */
359 zclient_create_header(s
, command
, vrf_id
);
361 return zclient_send_message(zclient
);
364 static int zebra_hello_send(struct zclient
*zclient
)
368 if (zclient
->redist_default
) {
372 /* The VRF ID in the HELLO message is always 0. */
373 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
374 stream_putc(s
, zclient
->redist_default
);
375 stream_putw(s
, zclient
->instance
);
376 if (zclient
->receive_notify
)
381 stream_putw_at(s
, 0, stream_get_endp(s
));
382 return zclient_send_message(zclient
);
388 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
389 mpls_label_t label
, enum lsp_types_t ltype
)
396 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
397 stream_putl(s
, label
);
399 stream_putc(s
, ltype
);
400 stream_putw_at(s
, 0, stream_get_endp(s
));
401 zclient_send_message(zclient
);
404 /* Send register requests to zebra daemon for the information in a VRF. */
405 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
410 /* If not connected to the zebra yet. */
411 if (zclient
->sock
< 0)
415 zlog_debug("%s: send register messages for VRF %u", __func__
,
418 /* We need router-id information. */
419 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
421 /* We need interface information. */
422 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
424 /* Set unwanted redistribute route. */
425 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
426 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
429 /* Flush all redistribute request. */
430 if (vrf_id
== VRF_DEFAULT
) {
431 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
432 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
433 if (!zclient
->mi_redist
[afi
][i
].enabled
)
436 struct listnode
*node
;
439 for (ALL_LIST_ELEMENTS_RO(
440 zclient
->mi_redist
[afi
][i
]
443 if (!(i
== zclient
->redist_default
444 && *id
== zclient
->instance
))
445 zebra_redistribute_send(
446 ZEBRA_REDISTRIBUTE_ADD
,
447 zclient
, afi
, i
, *id
,
453 /* Resend all redistribute request. */
454 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
455 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
456 if (i
!= zclient
->redist_default
457 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
459 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
463 /* If default information is needed. */
464 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
465 zebra_redistribute_default_send(
466 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
471 /* Send unregister requests to zebra daemon for the information in a VRF. */
472 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
477 /* If not connected to the zebra yet. */
478 if (zclient
->sock
< 0)
482 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
485 /* We need router-id information. */
486 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
488 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
490 /* Set unwanted redistribute route. */
491 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
492 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
495 /* Flush all redistribute request. */
496 if (vrf_id
== VRF_DEFAULT
) {
497 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
498 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
499 if (!zclient
->mi_redist
[afi
][i
].enabled
)
502 struct listnode
*node
;
505 for (ALL_LIST_ELEMENTS_RO(
506 zclient
->mi_redist
[afi
][i
]
509 if (!(i
== zclient
->redist_default
510 && *id
== zclient
->instance
))
511 zebra_redistribute_send(
512 ZEBRA_REDISTRIBUTE_DELETE
,
513 zclient
, afi
, i
, *id
,
519 /* Flush all redistribute request. */
520 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
521 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
522 if (i
!= zclient
->redist_default
523 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
525 zebra_redistribute_send(
526 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
529 /* If default information is needed. */
530 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
531 zebra_redistribute_default_send(
532 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
537 /* Send request to zebra daemon to start or stop RA. */
538 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
539 struct interface
*ifp
, int enable
,
544 /* If not connected to the zebra yet. */
545 if (zclient
->sock
< 0)
548 /* Form and send message. */
553 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
555 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
557 stream_putl(s
, ifp
->ifindex
);
558 stream_putl(s
, ra_interval
);
560 stream_putw_at(s
, 0, stream_get_endp(s
));
562 zclient_send_message(zclient
);
565 int zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
566 struct interface
*ifp
, bool down
)
570 if (zclient
->sock
< 0)
575 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
576 stream_putl(s
, ifp
->ifindex
);
577 stream_putc(s
, !!down
);
578 stream_putw_at(s
, 0, stream_get_endp(s
));
579 zclient_send_message(zclient
);
584 /* Make connection to zebra daemon. */
585 int zclient_start(struct zclient
*zclient
)
588 zlog_info("zclient_start is called");
590 /* If already connected to the zebra. */
591 if (zclient
->sock
>= 0)
594 /* Check connect thread. */
595 if (zclient
->t_connect
)
598 if (zclient_socket_connect(zclient
) < 0) {
600 zlog_debug("zclient connection fail");
602 zclient_event(ZCLIENT_CONNECT
, zclient
);
606 if (set_nonblocking(zclient
->sock
) < 0)
607 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
608 __func__
, zclient
->sock
);
610 /* Clear fail count. */
613 zlog_debug("zclient connect success with socket [%d]",
616 /* Create read thread. */
617 zclient_event(ZCLIENT_READ
, zclient
);
619 zebra_hello_send(zclient
);
621 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
623 /* Inform the successful connection. */
624 if (zclient
->zebra_connected
)
625 (*zclient
->zebra_connected
)(zclient
);
630 /* Initialize zebra client. Argument redist_default is unwanted
631 redistribute route type. */
632 void zclient_init(struct zclient
*zclient
, int redist_default
,
633 unsigned short instance
, struct zebra_privs_t
*privs
)
637 /* Set -1 to the default socket value. */
639 zclient
->privs
= privs
;
641 /* Clear redistribution flags. */
642 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
643 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
644 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
646 /* Set unwanted redistribute route. bgpd does not need BGP route
648 zclient
->redist_default
= redist_default
;
649 zclient
->instance
= instance
;
650 /* Pending: make afi(s) an arg. */
651 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
652 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
655 /* Set default-information redistribute to zero. */
656 zclient
->default_information
[afi
] = vrf_bitmap_init();
660 zlog_debug("scheduling zclient connection");
662 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
665 /* This function is a wrapper function for calling zclient_start from
666 timer or event thread. */
667 static int zclient_connect(struct thread
*t
)
669 struct zclient
*zclient
;
671 zclient
= THREAD_ARG(t
);
672 zclient
->t_connect
= NULL
;
675 zlog_debug("zclient_connect is called");
677 return zclient_start(zclient
);
680 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
681 bool exact_match
, vrf_id_t vrf_id
)
687 zclient_create_header(s
, command
, vrf_id
);
688 stream_putc(s
, (exact_match
) ? 1 : 0);
690 stream_putw(s
, PREFIX_FAMILY(p
));
691 stream_putc(s
, p
->prefixlen
);
692 switch (PREFIX_FAMILY(p
)) {
694 stream_put_in_addr(s
, &p
->u
.prefix4
);
697 stream_put(s
, &(p
->u
.prefix6
), 16);
702 stream_putw_at(s
, 0, stream_get_endp(s
));
704 return zclient_send_message(zclient
);
708 * "xdr_encode"-like interface that allows daemon (client) to send
709 * a message to zebra server for a route that needs to be
710 * added/deleted to the kernel. Info about the route is specified
711 * by the caller in a struct zapi_route. zapi_route_encode() then writes
712 * the info down the zclient socket using the stream_* functions.
714 * The corresponding read ("xdr_decode") function on the server
715 * side is zapi_route_decode().
717 * 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
718 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
719 * | Length (2) | Command | Route Type |
720 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
721 * | ZEBRA Flags | Message Flags | Prefix length |
722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723 * | Destination IPv4 Prefix for route |
724 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
729 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
730 * described, as per the Nexthop count. Each nexthop described as:
733 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
734 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
735 * | IPv4 Nexthop address or Interface Index number |
736 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
738 * Alternatively, if the route is a blackhole route, then Nexthop count
739 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
742 * The original struct zapi_route_*() infrastructure was built around
743 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
744 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
745 * nexthops into zapi_route_encode() using the same zapi_route structure.
746 * This is done by setting zapi_route fields as follows:
747 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
748 * - .nexthop_num == .ifindex_num
749 * - .nexthop and .ifindex are filled with gate and ifindex parts of
750 * each compound nexthop, both in the same order
752 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
755 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
758 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
760 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
762 * XXX: No attention paid to alignment.
764 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
765 struct zapi_route
*api
)
767 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
769 return zclient_send_message(zclient
);
772 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
773 const struct zapi_nexthop
*next2
)
775 if (next1
->label_num
> next2
->label_num
)
778 if (next1
->label_num
< next2
->label_num
)
781 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
784 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
785 const struct zapi_nexthop
*next2
)
789 if (next1
->vrf_id
< next2
->vrf_id
)
792 if (next1
->vrf_id
> next2
->vrf_id
)
795 if (next1
->type
< next2
->type
)
798 if (next1
->type
> next2
->type
)
801 switch (next1
->type
) {
802 case NEXTHOP_TYPE_IPV4
:
803 case NEXTHOP_TYPE_IPV6
:
804 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
809 case NEXTHOP_TYPE_IPV4_IFINDEX
:
810 case NEXTHOP_TYPE_IPV6_IFINDEX
:
811 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
815 /* Intentional Fall-Through */
816 case NEXTHOP_TYPE_IFINDEX
:
817 if (next1
->ifindex
< next2
->ifindex
)
820 if (next1
->ifindex
> next2
->ifindex
)
823 case NEXTHOP_TYPE_BLACKHOLE
:
824 if (next1
->bh_type
< next2
->bh_type
)
827 if (next1
->bh_type
> next2
->bh_type
)
835 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
839 const struct zapi_nexthop
*next1
= item1
;
840 const struct zapi_nexthop
*next2
= item2
;
842 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
846 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
851 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
852 uint16_t nexthop_num
)
854 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
858 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
860 struct zapi_nexthop
*api_nh
;
865 zclient_create_header(s
, cmd
, api
->vrf_id
);
867 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
868 flog_err(EC_LIB_ZAPI_ENCODE
,
869 "%s: Specified route type (%u) is not a legal value\n",
870 __PRETTY_FUNCTION__
, api
->type
);
873 stream_putc(s
, api
->type
);
875 stream_putw(s
, api
->instance
);
876 stream_putl(s
, api
->flags
);
877 stream_putc(s
, api
->message
);
879 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
880 flog_err(EC_LIB_ZAPI_ENCODE
,
881 "%s: Specified route SAFI (%u) is not a legal value\n",
882 __PRETTY_FUNCTION__
, api
->safi
);
885 stream_putc(s
, api
->safi
);
887 /* Put prefix information. */
888 stream_putc(s
, api
->prefix
.family
);
889 psize
= PSIZE(api
->prefix
.prefixlen
);
890 stream_putc(s
, api
->prefix
.prefixlen
);
891 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
893 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
894 psize
= PSIZE(api
->src_prefix
.prefixlen
);
895 stream_putc(s
, api
->src_prefix
.prefixlen
);
896 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
900 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
901 /* limit the number of nexthops if necessary */
902 if (api
->nexthop_num
> MULTIPATH_NUM
) {
903 char buf
[PREFIX2STR_BUFFER
];
905 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
908 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
909 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
913 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
915 stream_putw(s
, api
->nexthop_num
);
917 for (i
= 0; i
< api
->nexthop_num
; i
++) {
918 api_nh
= &api
->nexthops
[i
];
920 stream_putl(s
, api_nh
->vrf_id
);
921 stream_putc(s
, api_nh
->type
);
922 stream_putc(s
, api_nh
->onlink
);
923 switch (api_nh
->type
) {
924 case NEXTHOP_TYPE_BLACKHOLE
:
925 stream_putc(s
, api_nh
->bh_type
);
927 case NEXTHOP_TYPE_IPV4
:
928 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
930 case NEXTHOP_TYPE_IPV4_IFINDEX
:
931 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
932 stream_putl(s
, api_nh
->ifindex
);
934 case NEXTHOP_TYPE_IFINDEX
:
935 stream_putl(s
, api_nh
->ifindex
);
937 case NEXTHOP_TYPE_IPV6
:
938 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
941 case NEXTHOP_TYPE_IPV6_IFINDEX
:
942 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
944 stream_putl(s
, api_nh
->ifindex
);
948 /* MPLS labels for BGP-LU or Segment Routing */
949 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
950 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
951 char buf
[PREFIX2STR_BUFFER
];
952 prefix2str(&api
->prefix
, buf
,
954 flog_err(EC_LIB_ZAPI_ENCODE
,
955 "%s: prefix %s: can't encode "
956 "%u labels (maximum is %u)",
963 stream_putc(s
, api_nh
->label_num
);
964 stream_put(s
, &api_nh
->labels
[0],
966 * sizeof(mpls_label_t
));
969 /* Router MAC for EVPN routes. */
970 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
971 stream_put(s
, &(api_nh
->rmac
),
972 sizeof(struct ethaddr
));
977 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
978 stream_putc(s
, api
->distance
);
979 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
980 stream_putl(s
, api
->metric
);
981 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
982 stream_putl(s
, api
->tag
);
983 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
984 stream_putl(s
, api
->mtu
);
985 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
986 stream_putl(s
, api
->tableid
);
988 /* Put length at the first point of the stream. */
989 stream_putw_at(s
, 0, stream_get_endp(s
));
994 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
996 struct zapi_nexthop
*api_nh
;
999 memset(api
, 0, sizeof(*api
));
1001 /* Type, flags, message. */
1002 STREAM_GETC(s
, api
->type
);
1003 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1004 flog_err(EC_LIB_ZAPI_ENCODE
,
1005 "%s: Specified route type: %d is not a legal value\n",
1006 __PRETTY_FUNCTION__
, api
->type
);
1010 STREAM_GETW(s
, api
->instance
);
1011 STREAM_GETL(s
, api
->flags
);
1012 STREAM_GETC(s
, api
->message
);
1013 STREAM_GETC(s
, api
->safi
);
1014 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1015 flog_err(EC_LIB_ZAPI_ENCODE
,
1016 "%s: Specified route SAFI (%u) is not a legal value\n",
1017 __PRETTY_FUNCTION__
, api
->safi
);
1022 STREAM_GETC(s
, api
->prefix
.family
);
1023 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1024 switch (api
->prefix
.family
) {
1026 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1029 "%s: V4 prefixlen is %d which should not be more than 32",
1030 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1035 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1038 "%s: v6 prefixlen is %d which should not be more than 128",
1039 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1044 flog_err(EC_LIB_ZAPI_ENCODE
,
1045 "%s: Specified family %d is not v4 or v6",
1046 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1049 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1051 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1052 api
->src_prefix
.family
= AF_INET6
;
1053 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1054 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1057 "%s: SRC Prefix prefixlen received: %d is too large",
1058 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1061 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1062 PSIZE(api
->src_prefix
.prefixlen
));
1064 if (api
->prefix
.family
!= AF_INET6
1065 || api
->src_prefix
.prefixlen
== 0) {
1068 "%s: SRC prefix specified in some manner that makes no sense",
1069 __PRETTY_FUNCTION__
);
1075 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1076 STREAM_GETW(s
, api
->nexthop_num
);
1077 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1078 flog_err(EC_LIB_ZAPI_ENCODE
,
1079 "%s: invalid number of nexthops (%u)",
1080 __func__
, api
->nexthop_num
);
1084 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1085 api_nh
= &api
->nexthops
[i
];
1087 STREAM_GETL(s
, api_nh
->vrf_id
);
1088 STREAM_GETC(s
, api_nh
->type
);
1089 STREAM_GETC(s
, api_nh
->onlink
);
1090 switch (api_nh
->type
) {
1091 case NEXTHOP_TYPE_BLACKHOLE
:
1092 STREAM_GETC(s
, api_nh
->bh_type
);
1094 case NEXTHOP_TYPE_IPV4
:
1095 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1098 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1099 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1101 STREAM_GETL(s
, api_nh
->ifindex
);
1103 case NEXTHOP_TYPE_IFINDEX
:
1104 STREAM_GETL(s
, api_nh
->ifindex
);
1106 case NEXTHOP_TYPE_IPV6
:
1107 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1109 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1110 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1111 STREAM_GETL(s
, api_nh
->ifindex
);
1115 /* MPLS labels for BGP-LU or Segment Routing */
1116 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1117 STREAM_GETC(s
, api_nh
->label_num
);
1119 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1122 "%s: invalid number of MPLS labels (%u)",
1123 __func__
, api_nh
->label_num
);
1127 STREAM_GET(&api_nh
->labels
[0], s
,
1129 * sizeof(mpls_label_t
));
1132 /* Router MAC for EVPN routes. */
1133 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1134 stream_get(&(api_nh
->rmac
), s
,
1135 sizeof(struct ethaddr
));
1140 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1141 STREAM_GETC(s
, api
->distance
);
1142 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1143 STREAM_GETL(s
, api
->metric
);
1144 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1145 STREAM_GETL(s
, api
->tag
);
1146 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1147 STREAM_GETL(s
, api
->mtu
);
1148 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1149 STREAM_GETL(s
, api
->tableid
);
1156 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1162 memset(&any
, 0, sizeof(any
));
1163 any
.family
= family
;
1167 stream_putc(s
, p
->family
);
1168 stream_putc(s
, p
->prefixlen
);
1169 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1172 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1175 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1178 * We are sending one item at a time at the moment
1182 stream_putl(s
, zrule
->seq
);
1183 stream_putl(s
, zrule
->priority
);
1184 stream_putl(s
, zrule
->unique
);
1186 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1187 zrule
->filter
.src_ip
.family
);
1188 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1189 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1190 zrule
->filter
.src_ip
.family
);
1191 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1192 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1194 stream_putl(s
, zrule
->action
.table
);
1195 stream_putl(s
, zrule
->ifindex
);
1197 /* Put length at the first point of the stream. */
1198 stream_putw_at(s
, 0, stream_get_endp(s
));
1203 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1205 enum zapi_route_notify_owner
*note
)
1209 STREAM_GET(note
, s
, sizeof(*note
));
1211 STREAM_GETC(s
, p
->family
);
1212 STREAM_GETC(s
, p
->prefixlen
);
1213 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1224 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1225 uint32_t *priority
, uint32_t *unique
,
1227 enum zapi_rule_notify_owner
*note
)
1229 uint32_t prio
, seq
, uni
;
1232 STREAM_GET(note
, s
, sizeof(*note
));
1234 STREAM_GETL(s
, seq
);
1235 STREAM_GETL(s
, prio
);
1236 STREAM_GETL(s
, uni
);
1237 STREAM_GETL(s
, ifi
);
1240 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1253 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1254 enum zapi_ipset_notify_owner
*note
)
1258 STREAM_GET(note
, s
, sizeof(*note
));
1260 STREAM_GETL(s
, uni
);
1263 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1272 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1274 enum zapi_ipset_entry_notify_owner
*note
)
1278 STREAM_GET(note
, s
, sizeof(*note
));
1280 STREAM_GETL(s
, uni
);
1282 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1285 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1294 bool zapi_iptable_notify_decode(struct stream
*s
,
1296 enum zapi_iptable_notify_owner
*note
)
1300 STREAM_GET(note
, s
, sizeof(*note
));
1302 STREAM_GETL(s
, uni
);
1305 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1314 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1316 struct nexthop
*n
= nexthop_new();
1318 n
->type
= znh
->type
;
1319 n
->vrf_id
= znh
->vrf_id
;
1320 n
->ifindex
= znh
->ifindex
;
1321 n
->gate
= znh
->gate
;
1324 * This function currently handles labels
1326 if (znh
->label_num
) {
1327 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1334 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1338 memset(nhr
, 0, sizeof(*nhr
));
1340 STREAM_GETW(s
, nhr
->prefix
.family
);
1341 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1342 switch (nhr
->prefix
.family
) {
1344 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1347 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1353 STREAM_GETC(s
, nhr
->type
);
1354 STREAM_GETW(s
, nhr
->instance
);
1355 STREAM_GETC(s
, nhr
->distance
);
1356 STREAM_GETL(s
, nhr
->metric
);
1357 STREAM_GETC(s
, nhr
->nexthop_num
);
1359 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1360 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1361 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1362 switch (nhr
->nexthops
[i
].type
) {
1363 case NEXTHOP_TYPE_IPV4
:
1364 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1365 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1367 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1369 case NEXTHOP_TYPE_IFINDEX
:
1370 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1372 case NEXTHOP_TYPE_IPV6
:
1373 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1374 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1376 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1378 case NEXTHOP_TYPE_BLACKHOLE
:
1381 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1382 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1383 flog_err(EC_LIB_ZAPI_ENCODE
,
1384 "%s: invalid number of MPLS labels (%u)",
1385 __func__
, nhr
->nexthops
[i
].label_num
);
1388 if (nhr
->nexthops
[i
].label_num
)
1389 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1390 nhr
->nexthops
[i
].label_num
1391 * sizeof(mpls_label_t
));
1400 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1401 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1402 * then set/unset redist[type] in the client handle (a struct zserv) for the
1405 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1406 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1413 zclient_create_header(s
, command
, vrf_id
);
1414 stream_putc(s
, afi
);
1415 stream_putc(s
, type
);
1416 stream_putw(s
, instance
);
1418 stream_putw_at(s
, 0, stream_get_endp(s
));
1420 return zclient_send_message(zclient
);
1423 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1424 afi_t afi
, vrf_id_t vrf_id
)
1431 zclient_create_header(s
, command
, vrf_id
);
1432 stream_putc(s
, afi
);
1434 stream_putw_at(s
, 0, stream_get_endp(s
));
1436 return zclient_send_message(zclient
);
1439 /* Get prefix in ZServ format; family should be filled in on prefix */
1440 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1442 size_t plen
= prefix_blen(p
);
1449 stream_get(&p
->u
.prefix
, s
, plen
);
1451 p
->prefixlen
= MIN(plen
* 8, c
);
1457 /* Router-id update from zebra daemon. */
1458 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1460 /* Fetch interface address. */
1461 STREAM_GETC(s
, rid
->family
);
1463 zclient_stream_get_prefix(s
, rid
);
1469 /* Interface addition from zebra daemon. */
1471 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1472 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1474 * 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
1475 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1481 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1492 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1494 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1496 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1498 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1499 * | parent ifindex |
1500 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1501 * | Link Layer Type |
1502 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1503 * | Harware Address Length |
1504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1505 * | Hardware Address if HW lenght different from 0 |
1506 * | ... max INTERFACE_HWADDR_MAX |
1507 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1508 * | Link_params? | Whether a link-params follows: 1 or 0.
1509 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1510 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1511 * | .... (struct if_link_params). |
1512 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1515 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1518 char vrfname_tmp
[VRF_NAMSIZ
];
1519 struct vrf_data data
;
1521 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1522 /* Read interface name. */
1523 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1525 /* Lookup/create vrf by vrf_id. */
1526 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1527 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1528 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1529 /* overwrite default vrf */
1530 if (vrf_id
== VRF_DEFAULT
)
1531 vrf_set_default_name(vrfname_tmp
, false);
1535 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1539 /* Lookup vrf by vrf_id. */
1540 vrf
= vrf_lookup_by_id(vrf_id
);
1543 * If a routing protocol doesn't know about a
1544 * vrf that is about to be deleted. There is
1545 * no point in attempting to delete it.
1553 static void zclient_interface_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1555 struct interface
*ifp
;
1556 char ifname_tmp
[INTERFACE_NAMSIZ
];
1557 struct stream
*s
= zclient
->ibuf
;
1559 /* Read interface name. */
1560 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1562 /* Lookup/create interface by name. */
1563 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1565 zebra_interface_if_set_value(s
, ifp
);
1567 if_new_via_zapi(ifp
);
1571 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1572 * from zebra server. The format of this message is the same as
1573 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE,
1574 * except that no sockaddr_dl is sent at the tail of the message.
1576 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1578 struct interface
*ifp
;
1579 char ifname_tmp
[INTERFACE_NAMSIZ
];
1581 /* Read interface name. */
1582 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1584 /* Lookup this by interface index. */
1585 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1587 flog_err(EC_LIB_ZAPI_ENCODE
,
1588 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1589 ifname_tmp
, vrf_id
);
1593 zebra_interface_if_set_value(s
, ifp
);
1598 static void zclient_interface_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1600 struct interface
*ifp
;
1601 struct stream
*s
= zclient
->ibuf
;
1603 ifp
= zebra_interface_state_read(s
, vrf_id
);
1608 if_destroy_via_zapi(ifp
);
1612 static void zclient_interface_up(struct zclient
*zclient
, vrf_id_t vrf_id
)
1614 struct interface
*ifp
;
1615 struct stream
*s
= zclient
->ibuf
;
1617 ifp
= zebra_interface_state_read(s
, vrf_id
);
1622 if_up_via_zapi(ifp
);
1625 static void zclient_interface_down(struct zclient
*zclient
, vrf_id_t vrf_id
)
1627 struct interface
*ifp
;
1628 struct stream
*s
= zclient
->ibuf
;
1630 ifp
= zebra_interface_state_read(s
, vrf_id
);
1635 if_down_via_zapi(ifp
);
1638 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1644 iflp
->lp_status
= stream_getl(s
);
1645 iflp
->te_metric
= stream_getl(s
);
1646 iflp
->max_bw
= stream_getf(s
);
1647 iflp
->max_rsv_bw
= stream_getf(s
);
1648 uint32_t bwclassnum
= stream_getl(s
);
1651 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1652 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1655 EC_LIB_ZAPI_MISSMATCH
,
1656 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1657 " - outdated library?",
1658 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1660 iflp
->admin_grp
= stream_getl(s
);
1661 iflp
->rmt_as
= stream_getl(s
);
1662 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1664 iflp
->av_delay
= stream_getl(s
);
1665 iflp
->min_delay
= stream_getl(s
);
1666 iflp
->max_delay
= stream_getl(s
);
1667 iflp
->delay_var
= stream_getl(s
);
1669 iflp
->pkt_loss
= stream_getf(s
);
1670 iflp
->res_bw
= stream_getf(s
);
1671 iflp
->ava_bw
= stream_getf(s
);
1672 iflp
->use_bw
= stream_getf(s
);
1675 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1678 struct if_link_params
*iflp
;
1683 ifindex
= stream_getl(s
);
1685 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1688 flog_err(EC_LIB_ZAPI_ENCODE
,
1689 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1694 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1697 link_params_set_value(s
, iflp
);
1702 static void zebra_interface_if_set_value(struct stream
*s
,
1703 struct interface
*ifp
)
1705 uint8_t link_params_status
= 0;
1706 ifindex_t old_ifindex
;
1708 old_ifindex
= ifp
->ifindex
;
1709 /* Read interface's index. */
1710 if_set_index(ifp
, stream_getl(s
));
1711 ifp
->status
= stream_getc(s
);
1713 /* Read interface's value. */
1714 ifp
->flags
= stream_getq(s
);
1715 ifp
->ptm_enable
= stream_getc(s
);
1716 ifp
->ptm_status
= stream_getc(s
);
1717 ifp
->metric
= stream_getl(s
);
1718 ifp
->speed
= stream_getl(s
);
1719 ifp
->mtu
= stream_getl(s
);
1720 ifp
->mtu6
= stream_getl(s
);
1721 ifp
->bandwidth
= stream_getl(s
);
1722 ifp
->link_ifindex
= stream_getl(s
);
1723 ifp
->ll_type
= stream_getl(s
);
1724 ifp
->hw_addr_len
= stream_getl(s
);
1725 if (ifp
->hw_addr_len
)
1726 stream_get(ifp
->hw_addr
, s
,
1727 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1729 /* Read Traffic Engineering status */
1730 link_params_status
= stream_getc(s
);
1731 /* Then, Traffic Engineering parameters if any */
1732 if (link_params_status
) {
1733 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1734 link_params_set_value(s
, iflp
);
1737 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1740 size_t zebra_interface_link_params_write(struct stream
*s
,
1741 struct interface
*ifp
)
1744 struct if_link_params
*iflp
;
1747 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1750 iflp
= ifp
->link_params
;
1753 w
+= stream_putl(s
, iflp
->lp_status
);
1755 w
+= stream_putl(s
, iflp
->te_metric
);
1756 w
+= stream_putf(s
, iflp
->max_bw
);
1757 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1759 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1760 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1761 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1763 w
+= stream_putl(s
, iflp
->admin_grp
);
1764 w
+= stream_putl(s
, iflp
->rmt_as
);
1765 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1767 w
+= stream_putl(s
, iflp
->av_delay
);
1768 w
+= stream_putl(s
, iflp
->min_delay
);
1769 w
+= stream_putl(s
, iflp
->max_delay
);
1770 w
+= stream_putl(s
, iflp
->delay_var
);
1772 w
+= stream_putf(s
, iflp
->pkt_loss
);
1773 w
+= stream_putf(s
, iflp
->res_bw
);
1774 w
+= stream_putf(s
, iflp
->ava_bw
);
1775 w
+= stream_putf(s
, iflp
->use_bw
);
1781 * format of message for address additon is:
1785 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1786 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1795 * | ifc_flags | flags for connected address
1803 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1811 static int memconstant(const void *s
, int c
, size_t n
)
1813 const uint8_t *p
= s
;
1822 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1826 struct interface
*ifp
;
1827 struct connected
*ifc
;
1828 struct prefix p
, d
, *dp
;
1832 memset(&p
, 0, sizeof(p
));
1833 memset(&d
, 0, sizeof(d
));
1835 /* Get interface index. */
1836 ifindex
= stream_getl(s
);
1839 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1841 flog_err(EC_LIB_ZAPI_ENCODE
,
1842 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1843 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1849 ifc_flags
= stream_getc(s
);
1851 /* Fetch interface address. */
1852 d
.family
= p
.family
= stream_getc(s
);
1853 plen
= prefix_blen(&d
);
1855 zclient_stream_get_prefix(s
, &p
);
1857 /* Fetch destination address. */
1858 stream_get(&d
.u
.prefix
, s
, plen
);
1860 /* N.B. NULL destination pointers are encoded as all zeroes */
1861 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1863 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1864 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1866 /* N.B. NULL destination pointers are encoded as all
1868 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1871 ifc
->flags
= ifc_flags
;
1872 if (ifc
->destination
)
1873 ifc
->destination
->prefixlen
=
1874 ifc
->address
->prefixlen
;
1875 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1876 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1878 char buf
[PREFIX_STRLEN
];
1881 "warning: interface %s address %s with peer flag set, but no peer address!",
1883 prefix2str(ifc
->address
, buf
,
1885 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1889 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1890 ifc
= connected_delete_by_prefix(ifp
, &p
);
1897 * format of message for neighbor connected address is:
1901 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1902 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1917 * | addr_len | len of addr.
1920 struct nbr_connected
*
1921 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1923 unsigned int ifindex
;
1924 struct interface
*ifp
;
1926 struct nbr_connected
*ifc
;
1928 /* Get interface index. */
1929 ifindex
= stream_getl(s
);
1932 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1934 flog_err(EC_LIB_ZAPI_ENCODE
,
1935 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1936 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1942 p
.family
= stream_getc(s
);
1943 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1944 p
.prefixlen
= stream_getc(s
);
1946 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1947 /* Currently only supporting P2P links, so any new RA source
1949 considered as the replacement of the previously learnt
1950 Link-Local address. */
1951 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1952 ifc
= nbr_connected_new();
1953 ifc
->address
= prefix_new();
1955 listnode_add(ifp
->nbr_connected
, ifc
);
1958 prefix_copy(ifc
->address
, &p
);
1960 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1962 ifc
= nbr_connected_check(ifp
, &p
);
1964 listnode_delete(ifp
->nbr_connected
, ifc
);
1970 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1972 vrf_id_t
*new_vrf_id
)
1974 char ifname
[INTERFACE_NAMSIZ
];
1975 struct interface
*ifp
;
1978 /* Read interface name. */
1979 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
1981 /* Lookup interface. */
1982 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1984 flog_err(EC_LIB_ZAPI_ENCODE
,
1985 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1990 /* Fetch new VRF Id. */
1991 new_id
= stream_getw(s
);
1993 *new_vrf_id
= new_id
;
1997 /* filter unwanted messages until the expected one arrives */
1998 static int zclient_read_sync_response(struct zclient
*zclient
,
1999 uint16_t expected_cmd
)
2011 cmd
= expected_cmd
+ 1;
2012 while (ret
== 0 && cmd
!= expected_cmd
) {
2016 /* wait until response arrives */
2018 FD_SET(zclient
->sock
, &readfds
);
2019 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
2020 if (!FD_ISSET(zclient
->sock
, &readfds
))
2023 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
2024 &version
, &vrf_id
, &cmd
);
2026 zlog_debug("%s: Response (%d bytes) received", __func__
,
2030 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
2038 * Connect to label manager in a syncronous way
2040 * It first writes the request to zcient output buffer and then
2041 * immediately reads the answer from the input buffer.
2043 * @param zclient Zclient used to connect to label manager (zebra)
2044 * @param async Synchronous (0) or asynchronous (1) operation
2045 * @result Result of response
2047 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2052 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2053 ZEBRA_LABEL_MANAGER_CONNECT
;
2056 zlog_debug("Connecting to Label Manager (LM)");
2058 if (zclient
->sock
< 0) {
2059 zlog_debug("%s: invalid zclient socket", __func__
);
2066 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2069 stream_putc(s
, zclient
->redist_default
);
2071 stream_putw(s
, zclient
->instance
);
2073 /* Put length at the first point of the stream. */
2074 stream_putw_at(s
, 0, stream_get_endp(s
));
2076 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2078 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2079 close(zclient
->sock
);
2084 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2085 close(zclient
->sock
);
2090 zlog_debug("LM connect request sent (%d bytes)", ret
);
2096 if (zclient_read_sync_response(zclient
, cmd
)
2102 /* read instance and proto */
2103 uint8_t proto
= stream_getc(s
);
2104 uint16_t instance
= stream_getw(s
);
2107 if (proto
!= zclient
->redist_default
)
2110 "Wrong proto (%u) in LM connect response. Should be %u",
2111 proto
, zclient
->redist_default
);
2112 if (instance
!= zclient
->instance
)
2115 "Wrong instId (%u) in LM connect response. Should be %u",
2116 instance
, zclient
->instance
);
2119 result
= stream_getc(s
);
2121 zlog_debug("LM connect-response received, result %u", result
);
2127 * Asynchronous label chunk request
2129 * @param zclient Zclient used to connect to label manager (zebra)
2130 * @param keep Avoid garbage collection
2131 * @param chunk_size Amount of labels requested
2132 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2133 * @result 0 on success, -1 otherwise
2135 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2136 uint32_t chunk_size
, uint32_t base
)
2141 zlog_debug("Getting Label Chunk");
2143 if (zclient
->sock
< 0)
2149 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2151 stream_putc(s
, zclient
->redist_default
);
2153 stream_putw(s
, zclient
->instance
);
2154 stream_putc(s
, keep
);
2155 stream_putl(s
, chunk_size
);
2156 stream_putl(s
, base
);
2158 /* Put length at the first point of the stream. */
2159 stream_putw_at(s
, 0, stream_get_endp(s
));
2161 return zclient_send_message(zclient
);
2165 * Function to request a label chunk in a syncronous way
2167 * It first writes the request to zlcient output buffer and then
2168 * immediately reads the answer from the input buffer.
2170 * @param zclient Zclient used to connect to label manager (zebra)
2171 * @param keep Avoid garbage collection
2172 * @param chunk_size Amount of labels requested
2173 * @param start To write first assigned chunk label to
2174 * @param end To write last assigned chunk label to
2175 * @result 0 on success, -1 otherwise
2177 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2178 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2182 uint8_t response_keep
;
2185 zlog_debug("Getting Label Chunk");
2187 if (zclient
->sock
< 0)
2193 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2195 stream_putc(s
, zclient
->redist_default
);
2197 stream_putw(s
, zclient
->instance
);
2199 stream_putc(s
, keep
);
2201 stream_putl(s
, chunk_size
);
2202 /* requested chunk base */
2203 stream_putl(s
, base
);
2204 /* Put length at the first point of the stream. */
2205 stream_putw_at(s
, 0, stream_get_endp(s
));
2207 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2209 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2210 close(zclient
->sock
);
2215 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2216 close(zclient
->sock
);
2221 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2224 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2227 /* parse response */
2230 /* read proto and instance */
2231 uint8_t proto
= stream_getc(s
);
2232 uint16_t instance
= stream_getw(s
);
2235 if (proto
!= zclient
->redist_default
)
2236 flog_err(EC_LIB_ZAPI_ENCODE
,
2237 "Wrong proto (%u) in get chunk response. Should be %u",
2238 proto
, zclient
->redist_default
);
2239 if (instance
!= zclient
->instance
)
2240 flog_err(EC_LIB_ZAPI_ENCODE
,
2241 "Wrong instId (%u) in get chunk response Should be %u",
2242 instance
, zclient
->instance
);
2244 /* if we requested a specific chunk and it could not be allocated, the
2245 * response message will end here
2247 if (!STREAM_READABLE(s
)) {
2248 zlog_info("Unable to assign Label Chunk to %s instance %u",
2249 zebra_route_string(proto
), instance
);
2254 response_keep
= stream_getc(s
);
2255 /* start and end labels */
2256 *start
= stream_getl(s
);
2257 *end
= stream_getl(s
);
2259 /* not owning this response */
2260 if (keep
!= response_keep
) {
2263 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2264 *start
, *end
, keep
, response_keep
);
2267 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2268 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2269 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2275 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2282 * Function to release a label chunk
2284 * @param zclient Zclient used to connect to label manager (zebra)
2285 * @param start First label of chunk
2286 * @param end Last label of chunk
2287 * @result 0 on success, -1 otherwise
2289 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2296 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2298 if (zclient
->sock
< 0)
2304 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2307 stream_putc(s
, zclient
->redist_default
);
2309 stream_putw(s
, zclient
->instance
);
2311 stream_putl(s
, start
);
2313 stream_putl(s
, end
);
2315 /* Put length at the first point of the stream. */
2316 stream_putw_at(s
, 0, stream_get_endp(s
));
2318 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2320 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2321 close(zclient
->sock
);
2326 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2327 close(zclient
->sock
);
2336 * Connect to table manager in a syncronous way
2338 * It first writes the request to zcient output buffer and then
2339 * immediately reads the answer from the input buffer.
2341 * @param zclient Zclient used to connect to table manager (zebra)
2342 * @result Result of response
2344 int tm_table_manager_connect(struct zclient
*zclient
)
2351 zlog_debug("Connecting to Table Manager");
2353 if (zclient
->sock
< 0)
2359 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2362 stream_putc(s
, zclient
->redist_default
);
2364 stream_putw(s
, zclient
->instance
);
2366 /* Put length at the first point of the stream. */
2367 stream_putw_at(s
, 0, stream_get_endp(s
));
2369 ret
= zclient_send_message(zclient
);
2374 zlog_debug("%s: Table manager connect request sent", __func__
);
2377 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2383 STREAM_GETC(s
, result
);
2386 "%s: Table Manager connect response received, result %u",
2395 * Function to request a table chunk in a syncronous way
2397 * It first writes the request to zclient output buffer and then
2398 * immediately reads the answer from the input buffer.
2400 * @param zclient Zclient used to connect to table manager (zebra)
2401 * @param chunk_size Amount of table requested
2402 * @param start to write first assigned chunk table RT ID to
2403 * @param end To write last assigned chunk table RT ID to
2404 * @result 0 on success, -1 otherwise
2406 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2407 uint32_t *start
, uint32_t *end
)
2413 zlog_debug("Getting Table Chunk");
2415 if (zclient
->sock
< 0)
2421 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2423 stream_putl(s
, chunk_size
);
2424 /* Put length at the first point of the stream. */
2425 stream_putw_at(s
, 0, stream_get_endp(s
));
2427 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2429 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2431 close(zclient
->sock
);
2436 flog_err(EC_LIB_ZAPI_SOCKET
,
2437 "%s: zclient->sock connection closed", __func__
);
2438 close(zclient
->sock
);
2443 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2447 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2451 /* start and end table IDs */
2452 STREAM_GETL(s
, *start
);
2453 STREAM_GETL(s
, *end
);
2456 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2464 * Function to release a table chunk
2466 * @param zclient Zclient used to connect to table manager (zebra)
2467 * @param start First label of table
2468 * @param end Last label of chunk
2469 * @result 0 on success, -1 otherwise
2471 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2477 zlog_debug("Releasing Table Chunk");
2479 if (zclient
->sock
< 0)
2485 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2488 stream_putl(s
, start
);
2490 stream_putl(s
, end
);
2492 /* Put length at the first point of the stream. */
2493 stream_putw_at(s
, 0, stream_get_endp(s
));
2495 return zclient_send_message(zclient
);
2498 int zebra_send_mpls_labels(struct zclient
*zclient
, int cmd
,
2499 struct zapi_labels
*zl
)
2501 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
2503 return zclient_send_message(zclient
);
2506 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
2508 struct zapi_nexthop_label
*znh
;
2512 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2513 stream_putc(s
, zl
->message
);
2514 stream_putc(s
, zl
->type
);
2515 stream_putl(s
, zl
->local_label
);
2517 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2518 stream_putw(s
, zl
->route
.prefix
.family
);
2519 stream_put_prefix(s
, &zl
->route
.prefix
);
2520 stream_putc(s
, zl
->route
.type
);
2521 stream_putw(s
, zl
->route
.instance
);
2524 if (zl
->nexthop_num
> MULTIPATH_NUM
) {
2527 "%s: label %u: can't encode %u nexthops (maximum is %u)",
2528 __func__
, zl
->local_label
, zl
->nexthop_num
,
2532 stream_putw(s
, zl
->nexthop_num
);
2534 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2535 znh
= &zl
->nexthops
[i
];
2537 stream_putc(s
, znh
->type
);
2538 stream_putw(s
, znh
->family
);
2539 switch (znh
->family
) {
2541 stream_put_in_addr(s
, &znh
->address
.ipv4
);
2544 stream_write(s
, (uint8_t *)&znh
->address
.ipv6
, 16);
2549 stream_putl(s
, znh
->ifindex
);
2550 stream_putl(s
, znh
->label
);
2553 /* Put length at the first point of the stream. */
2554 stream_putw_at(s
, 0, stream_get_endp(s
));
2559 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
2561 struct zapi_nexthop_label
*znh
;
2563 memset(zl
, 0, sizeof(*zl
));
2566 STREAM_GETC(s
, zl
->message
);
2567 STREAM_GETC(s
, zl
->type
);
2568 STREAM_GETL(s
, zl
->local_label
);
2570 if (CHECK_FLAG(zl
->message
, ZAPI_LABELS_FTN
)) {
2573 STREAM_GETW(s
, zl
->route
.prefix
.family
);
2574 STREAM_GETC(s
, zl
->route
.prefix
.prefixlen
);
2576 psize
= PSIZE(zl
->route
.prefix
.prefixlen
);
2577 switch (zl
->route
.prefix
.family
) {
2579 if (zl
->route
.prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2581 "%s: Specified prefix length %d is greater than a v4 address can support",
2582 __PRETTY_FUNCTION__
,
2583 zl
->route
.prefix
.prefixlen
);
2586 STREAM_GET(&zl
->route
.prefix
.u
.prefix4
.s_addr
, s
,
2590 if (zl
->route
.prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2592 "%s: Specified prefix length %d is greater than a v6 address can support",
2593 __PRETTY_FUNCTION__
,
2594 zl
->route
.prefix
.prefixlen
);
2597 STREAM_GET(&zl
->route
.prefix
.u
.prefix6
, s
, psize
);
2600 flog_err(EC_LIB_ZAPI_ENCODE
,
2601 "%s: Specified family %u is not v4 or v6",
2602 __PRETTY_FUNCTION__
, zl
->route
.prefix
.family
);
2606 STREAM_GETC(s
, zl
->route
.type
);
2607 STREAM_GETW(s
, zl
->route
.instance
);
2610 STREAM_GETW(s
, zl
->nexthop_num
);
2611 for (int i
= 0; i
< zl
->nexthop_num
; i
++) {
2612 znh
= &zl
->nexthops
[i
];
2614 STREAM_GETC(s
, znh
->type
);
2615 STREAM_GETW(s
, znh
->family
);
2616 switch (znh
->family
) {
2618 STREAM_GET(&znh
->address
.ipv4
.s_addr
, s
,
2622 STREAM_GET(&znh
->address
.ipv6
, s
, 16);
2627 STREAM_GETL(s
, znh
->ifindex
);
2628 STREAM_GETL(s
, znh
->label
);
2636 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2644 zclient_create_header(s
, command
, VRF_DEFAULT
);
2645 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2646 stream_putl(s
, pw
->ifindex
);
2649 stream_putl(s
, pw
->type
);
2652 stream_putl(s
, pw
->af
);
2655 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2658 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2661 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2666 stream_putl(s
, pw
->local_label
);
2667 stream_putl(s
, pw
->remote_label
);
2670 stream_putc(s
, pw
->flags
);
2672 /* Protocol specific fields */
2673 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2675 /* Put length at the first point of the stream. */
2676 stream_putw_at(s
, 0, stream_get_endp(s
));
2678 return zclient_send_message(zclient
);
2682 * Receive PW status update from Zebra and send it to LDE process.
2684 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
2688 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2692 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2693 pw
->ifindex
= stream_getl(s
);
2694 pw
->status
= stream_getl(s
);
2697 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
2699 struct zclient_capabilities cap
;
2700 struct stream
*s
= zclient
->ibuf
;
2702 uint8_t mpls_enabled
;
2704 STREAM_GETL(s
, vrf_backend
);
2705 vrf_configure_backend(vrf_backend
);
2707 memset(&cap
, 0, sizeof(cap
));
2708 STREAM_GETC(s
, mpls_enabled
);
2709 cap
.mpls_enabled
= !!mpls_enabled
;
2710 STREAM_GETL(s
, cap
.ecmp
);
2711 STREAM_GETC(s
, cap
.role
);
2713 if (zclient
->zebra_capabilities
)
2714 (*zclient
->zebra_capabilities
)(&cap
);
2720 /* Zebra client message read function. */
2721 static int zclient_read(struct thread
*thread
)
2724 uint16_t length
, command
;
2725 uint8_t marker
, version
;
2727 struct zclient
*zclient
;
2729 /* Get socket to zebra. */
2730 zclient
= THREAD_ARG(thread
);
2731 zclient
->t_read
= NULL
;
2733 /* Read zebra header (if we don't have it already). */
2734 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2736 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2737 ZEBRA_HEADER_SIZE
- already
))
2742 "zclient connection closed socket [%d].",
2744 return zclient_failed(zclient
);
2746 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2747 /* Try again later. */
2748 zclient_event(ZCLIENT_READ
, zclient
);
2751 already
= ZEBRA_HEADER_SIZE
;
2754 /* Reset to read from the beginning of the incoming packet. */
2755 stream_set_getp(zclient
->ibuf
, 0);
2757 /* Fetch header values. */
2758 length
= stream_getw(zclient
->ibuf
);
2759 marker
= stream_getc(zclient
->ibuf
);
2760 version
= stream_getc(zclient
->ibuf
);
2761 vrf_id
= stream_getl(zclient
->ibuf
);
2762 command
= stream_getw(zclient
->ibuf
);
2764 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2766 EC_LIB_ZAPI_MISSMATCH
,
2767 "%s: socket %d version mismatch, marker %d, version %d",
2768 __func__
, zclient
->sock
, marker
, version
);
2769 return zclient_failed(zclient
);
2772 if (length
< ZEBRA_HEADER_SIZE
) {
2773 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2774 "%s: socket %d message length %u is less than %d ",
2775 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2776 return zclient_failed(zclient
);
2780 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2784 "%s: message size %u exceeds buffer size %lu, expanding...",
2786 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2787 ns
= stream_new(length
);
2788 stream_copy(ns
, zclient
->ibuf
);
2789 stream_free(zclient
->ibuf
);
2793 /* Read rest of zebra packet. */
2794 if (already
< length
) {
2796 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2802 "zclient connection closed socket [%d].",
2804 return zclient_failed(zclient
);
2806 if (nbyte
!= (ssize_t
)(length
- already
)) {
2807 /* Try again later. */
2808 zclient_event(ZCLIENT_READ
, zclient
);
2813 length
-= ZEBRA_HEADER_SIZE
;
2816 zlog_debug("zclient 0x%p command %s VRF %u",
2817 (void *)zclient
, zserv_command_string(command
),
2821 case ZEBRA_CAPABILITIES
:
2822 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2824 case ZEBRA_ROUTER_ID_UPDATE
:
2825 if (zclient
->router_id_update
)
2826 (*zclient
->router_id_update
)(command
, zclient
, length
,
2830 zclient_vrf_add(zclient
, vrf_id
);
2832 case ZEBRA_VRF_DELETE
:
2833 zclient_vrf_delete(zclient
, vrf_id
);
2835 case ZEBRA_INTERFACE_ADD
:
2836 zclient_interface_add(zclient
, vrf_id
);
2838 case ZEBRA_INTERFACE_DELETE
:
2839 zclient_interface_delete(zclient
, vrf_id
);
2841 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2842 if (zclient
->interface_address_add
)
2843 (*zclient
->interface_address_add
)(command
, zclient
,
2846 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2847 if (zclient
->interface_address_delete
)
2848 (*zclient
->interface_address_delete
)(command
, zclient
,
2851 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2852 if (zclient
->interface_bfd_dest_update
)
2853 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2856 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2857 if (zclient
->interface_nbr_address_add
)
2858 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2861 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2862 if (zclient
->interface_nbr_address_delete
)
2863 (*zclient
->interface_nbr_address_delete
)(
2864 command
, zclient
, length
, vrf_id
);
2866 case ZEBRA_INTERFACE_UP
:
2867 zclient_interface_up(zclient
, vrf_id
);
2869 case ZEBRA_INTERFACE_DOWN
:
2870 zclient_interface_down(zclient
, vrf_id
);
2872 case ZEBRA_INTERFACE_VRF_UPDATE
:
2873 if (zclient
->interface_vrf_update
)
2874 (*zclient
->interface_vrf_update
)(command
, zclient
,
2877 case ZEBRA_NEXTHOP_UPDATE
:
2879 zlog_debug("zclient rcvd nexthop update");
2880 if (zclient
->nexthop_update
)
2881 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2884 case ZEBRA_IMPORT_CHECK_UPDATE
:
2886 zlog_debug("zclient rcvd import check update");
2887 if (zclient
->import_check_update
)
2888 (*zclient
->import_check_update
)(command
, zclient
,
2891 case ZEBRA_BFD_DEST_REPLAY
:
2892 if (zclient
->bfd_dest_replay
)
2893 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2896 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2897 if (zclient
->redistribute_route_add
)
2898 (*zclient
->redistribute_route_add
)(command
, zclient
,
2901 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2902 if (zclient
->redistribute_route_del
)
2903 (*zclient
->redistribute_route_del
)(command
, zclient
,
2906 case ZEBRA_INTERFACE_LINK_PARAMS
:
2907 if (zclient
->interface_link_params
)
2908 (*zclient
->interface_link_params
)(command
, zclient
,
2911 case ZEBRA_FEC_UPDATE
:
2913 zlog_debug("zclient rcvd fec update");
2914 if (zclient
->fec_update
)
2915 (*zclient
->fec_update
)(command
, zclient
, length
);
2917 case ZEBRA_LOCAL_ES_ADD
:
2918 if (zclient
->local_es_add
)
2919 (*zclient
->local_es_add
)(command
, zclient
, length
,
2922 case ZEBRA_LOCAL_ES_DEL
:
2923 if (zclient
->local_es_del
)
2924 (*zclient
->local_es_del
)(command
, zclient
, length
,
2928 if (zclient
->local_vni_add
)
2929 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2933 if (zclient
->local_vni_del
)
2934 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2937 case ZEBRA_L3VNI_ADD
:
2938 if (zclient
->local_l3vni_add
)
2939 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2942 case ZEBRA_L3VNI_DEL
:
2943 if (zclient
->local_l3vni_del
)
2944 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2947 case ZEBRA_MACIP_ADD
:
2948 if (zclient
->local_macip_add
)
2949 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2952 case ZEBRA_MACIP_DEL
:
2953 if (zclient
->local_macip_del
)
2954 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2957 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2958 if (zclient
->local_ip_prefix_add
)
2959 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2962 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2963 if (zclient
->local_ip_prefix_del
)
2964 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2967 case ZEBRA_PW_STATUS_UPDATE
:
2968 if (zclient
->pw_status_update
)
2969 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2972 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2973 if (zclient
->route_notify_owner
)
2974 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2977 case ZEBRA_RULE_NOTIFY_OWNER
:
2978 if (zclient
->rule_notify_owner
)
2979 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2982 case ZEBRA_GET_LABEL_CHUNK
:
2983 if (zclient
->label_chunk
)
2984 (*zclient
->label_chunk
)(command
, zclient
, length
,
2987 case ZEBRA_IPSET_NOTIFY_OWNER
:
2988 if (zclient
->ipset_notify_owner
)
2989 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2992 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2993 if (zclient
->ipset_entry_notify_owner
)
2994 (*zclient
->ipset_entry_notify_owner
)(command
,
2998 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2999 if (zclient
->iptable_notify_owner
)
3000 (*zclient
->iptable_notify_owner
)(command
,
3004 case ZEBRA_VXLAN_SG_ADD
:
3005 if (zclient
->vxlan_sg_add
)
3006 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
3009 case ZEBRA_VXLAN_SG_DEL
:
3010 if (zclient
->vxlan_sg_del
)
3011 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
3018 if (zclient
->sock
< 0)
3019 /* Connection was closed during packet processing. */
3022 /* Register read thread. */
3023 stream_reset(zclient
->ibuf
);
3024 zclient_event(ZCLIENT_READ
, zclient
);
3029 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
3030 int type
, unsigned short instance
, vrf_id_t vrf_id
)
3034 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3035 if (redist_check_instance(
3036 &zclient
->mi_redist
[afi
][type
], instance
))
3038 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
3041 if (!redist_check_instance(
3042 &zclient
->mi_redist
[afi
][type
], instance
))
3044 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
3049 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
3050 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
3053 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
3055 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
3058 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
3062 if (zclient
->sock
> 0)
3063 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
3068 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
3069 afi_t afi
, vrf_id_t vrf_id
)
3072 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
3073 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
3075 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
3077 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
3080 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
3083 if (zclient
->sock
> 0)
3084 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
3087 static void zclient_event(enum event event
, struct zclient
*zclient
)
3090 case ZCLIENT_SCHEDULE
:
3091 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
3092 &zclient
->t_connect
);
3094 case ZCLIENT_CONNECT
:
3097 "zclient connect failures: %d schedule interval is now %d",
3098 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
3099 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
3100 zclient
->fail
< 3 ? 10 : 60,
3101 &zclient
->t_connect
);
3104 zclient
->t_read
= NULL
;
3105 thread_add_read(zclient
->master
, zclient_read
, zclient
,
3106 zclient
->sock
, &zclient
->t_read
);
3111 void zclient_interface_set_master(struct zclient
*client
,
3112 struct interface
*master
,
3113 struct interface
*slave
)
3120 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
3122 stream_putl(s
, master
->vrf_id
);
3123 stream_putl(s
, master
->ifindex
);
3124 stream_putl(s
, slave
->vrf_id
);
3125 stream_putl(s
, slave
->ifindex
);
3127 stream_putw_at(s
, 0, stream_get_endp(s
));
3128 zclient_send_message(client
);