1 /* Zebra's client library.
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 * Copyright (C) 2005 Andrew J. Schorr
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "nexthop_group.h"
41 #include "lib_errors.h"
43 DEFINE_MTYPE_STATIC(LIB
, ZCLIENT
, "Zclient")
44 DEFINE_MTYPE_STATIC(LIB
, REDIST_INST
, "Redistribution instance IDs")
46 /* Zebra client events. */
47 enum event
{ ZCLIENT_SCHEDULE
, ZCLIENT_READ
, ZCLIENT_CONNECT
};
49 /* Prototype for event manager. */
50 static void zclient_event(enum event
, struct zclient
*);
52 struct zclient_options zclient_options_default
= {.receive_notify
= false};
54 struct sockaddr_storage zclient_addr
;
55 socklen_t zclient_addr_len
;
57 /* This file local debug flag. */
58 static int zclient_debug
;
60 /* Allocate zclient structure. */
61 struct zclient
*zclient_new(struct thread_master
*master
,
62 struct zclient_options
*opt
)
64 struct zclient
*zclient
;
66 MAX(ZEBRA_MAX_PACKET_SIZ
, sizeof(struct zapi_route
));
68 zclient
= XCALLOC(MTYPE_ZCLIENT
, sizeof(struct zclient
));
70 zclient
->ibuf
= stream_new(stream_size
);
71 zclient
->obuf
= stream_new(stream_size
);
72 zclient
->wb
= buffer_new(0);
73 zclient
->master
= master
;
75 zclient
->receive_notify
= opt
->receive_notify
;
80 /* This function is only called when exiting, because
81 many parts of the code do not check for I/O errors, so they could
82 reference an invalid pointer if the structure was ever freed.
84 Free zclient structure. */
85 void zclient_free(struct zclient
*zclient
)
88 stream_free(zclient
->ibuf
);
90 stream_free(zclient
->obuf
);
92 buffer_free(zclient
->wb
);
94 XFREE(MTYPE_ZCLIENT
, zclient
);
97 unsigned short *redist_check_instance(struct redist_proto
*red
,
98 unsigned short instance
)
100 struct listnode
*node
;
106 for (ALL_LIST_ELEMENTS_RO(red
->instances
, node
, id
))
113 void redist_add_instance(struct redist_proto
*red
, unsigned short instance
)
120 red
->instances
= list_new();
122 in
= XMALLOC(MTYPE_REDIST_INST
, sizeof(unsigned short));
124 listnode_add(red
->instances
, in
);
127 void redist_del_instance(struct redist_proto
*red
, unsigned short instance
)
131 id
= redist_check_instance(red
, instance
);
135 listnode_delete(red
->instances
, id
);
136 XFREE(MTYPE_REDIST_INST
, id
);
137 if (!red
->instances
->count
) {
139 list_delete(&red
->instances
);
143 /* Stop zebra client services. */
144 void zclient_stop(struct zclient
*zclient
)
150 zlog_debug("zclient stopped");
153 THREAD_OFF(zclient
->t_read
);
154 THREAD_OFF(zclient
->t_connect
);
155 THREAD_OFF(zclient
->t_write
);
158 stream_reset(zclient
->ibuf
);
159 stream_reset(zclient
->obuf
);
161 /* Empty the write buffer. */
162 buffer_reset(zclient
->wb
);
165 if (zclient
->sock
>= 0) {
166 close(zclient
->sock
);
171 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
172 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
173 vrf_bitmap_free(zclient
->redist
[afi
][i
]);
174 zclient
->redist
[afi
][i
] = VRF_BITMAP_NULL
;
177 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
180 vrf_bitmap_free(zclient
->default_information
[afi
]);
181 zclient
->default_information
[afi
] = VRF_BITMAP_NULL
;
185 void zclient_reset(struct zclient
*zclient
)
189 zclient_stop(zclient
);
191 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
193 &zclient
->mi_redist
[afi
][zclient
->redist_default
],
196 zclient_init(zclient
, zclient
->redist_default
, zclient
->instance
,
201 * Connect to zebra daemon.
202 * @param zclient a pointer to zclient structure
203 * @return socket fd just to make sure that connection established
207 int zclient_socket_connect(struct zclient
*zclient
)
212 /* We should think about IPv6 connection. */
213 sock
= socket(zclient_addr
.ss_family
, SOCK_STREAM
, 0);
218 setsockopt_so_sendbuf(sock
, 1048576);
220 /* Connect to zebra. */
221 ret
= connect(sock
, (struct sockaddr
*)&zclient_addr
, zclient_addr_len
);
224 zlog_debug("%s connect failure: %d(%s)",
225 __PRETTY_FUNCTION__
, errno
,
226 safe_strerror(errno
));
231 zclient
->sock
= sock
;
235 static int zclient_failed(struct zclient
*zclient
)
238 zclient_stop(zclient
);
239 zclient_event(ZCLIENT_CONNECT
, zclient
);
243 static int zclient_flush_data(struct thread
*thread
)
245 struct zclient
*zclient
= THREAD_ARG(thread
);
247 zclient
->t_write
= NULL
;
248 if (zclient
->sock
< 0)
250 switch (buffer_flush_available(zclient
->wb
, zclient
->sock
)) {
254 "%s: buffer_flush_available failed on zclient fd %d, closing",
255 __func__
, zclient
->sock
);
256 return zclient_failed(zclient
);
259 zclient
->t_write
= NULL
;
260 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
261 zclient
->sock
, &zclient
->t_write
);
269 int zclient_send_message(struct zclient
*zclient
)
271 if (zclient
->sock
< 0)
273 switch (buffer_write(zclient
->wb
, zclient
->sock
,
274 STREAM_DATA(zclient
->obuf
),
275 stream_get_endp(zclient
->obuf
))) {
277 flog_err(EC_LIB_ZAPI_SOCKET
,
278 "%s: buffer_write failed to zclient fd %d, closing",
279 __func__
, zclient
->sock
);
280 return zclient_failed(zclient
);
283 THREAD_OFF(zclient
->t_write
);
286 thread_add_write(zclient
->master
, zclient_flush_data
, zclient
,
287 zclient
->sock
, &zclient
->t_write
);
293 void zclient_create_header(struct stream
*s
, uint16_t command
, vrf_id_t vrf_id
)
295 /* length placeholder, caller can update */
296 stream_putw(s
, ZEBRA_HEADER_SIZE
);
297 stream_putc(s
, ZEBRA_HEADER_MARKER
);
298 stream_putc(s
, ZSERV_VERSION
);
299 stream_putl(s
, vrf_id
);
300 stream_putw(s
, command
);
303 int zclient_read_header(struct stream
*s
, int sock
, uint16_t *size
,
304 uint8_t *marker
, uint8_t *version
, vrf_id_t
*vrf_id
,
307 if (stream_read(s
, sock
, ZEBRA_HEADER_SIZE
) != ZEBRA_HEADER_SIZE
)
310 STREAM_GETW(s
, *size
);
311 *size
-= ZEBRA_HEADER_SIZE
;
312 STREAM_GETC(s
, *marker
);
313 STREAM_GETC(s
, *version
);
314 STREAM_GETL(s
, *vrf_id
);
315 STREAM_GETW(s
, *cmd
);
317 if (*version
!= ZSERV_VERSION
|| *marker
!= ZEBRA_HEADER_MARKER
) {
319 EC_LIB_ZAPI_MISSMATCH
,
320 "%s: socket %d version mismatch, marker %d, version %d",
321 __func__
, sock
, *marker
, *version
);
325 if (*size
&& stream_read(s
, sock
, *size
) != *size
)
333 bool zapi_parse_header(struct stream
*zmsg
, struct zmsghdr
*hdr
)
335 STREAM_GETW(zmsg
, hdr
->length
);
336 STREAM_GETC(zmsg
, hdr
->marker
);
337 STREAM_GETC(zmsg
, hdr
->version
);
338 STREAM_GETL(zmsg
, hdr
->vrf_id
);
339 STREAM_GETW(zmsg
, hdr
->command
);
345 /* Send simple Zebra message. */
346 static int zebra_message_send(struct zclient
*zclient
, int command
,
351 /* Get zclient output buffer. */
355 /* Send very simple command only Zebra message. */
356 zclient_create_header(s
, command
, vrf_id
);
358 return zclient_send_message(zclient
);
361 static int zebra_hello_send(struct zclient
*zclient
)
365 if (zclient
->redist_default
) {
369 /* The VRF ID in the HELLO message is always 0. */
370 zclient_create_header(s
, ZEBRA_HELLO
, VRF_DEFAULT
);
371 stream_putc(s
, zclient
->redist_default
);
372 stream_putw(s
, zclient
->instance
);
373 if (zclient
->receive_notify
)
378 stream_putw_at(s
, 0, stream_get_endp(s
));
379 return zclient_send_message(zclient
);
385 void zclient_send_vrf_label(struct zclient
*zclient
, vrf_id_t vrf_id
, afi_t afi
,
386 mpls_label_t label
, enum lsp_types_t ltype
)
393 zclient_create_header(s
, ZEBRA_VRF_LABEL
, vrf_id
);
394 stream_putl(s
, label
);
396 stream_putc(s
, ltype
);
397 stream_putw_at(s
, 0, stream_get_endp(s
));
398 zclient_send_message(zclient
);
401 /* Send register requests to zebra daemon for the information in a VRF. */
402 void zclient_send_reg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
407 /* If not connected to the zebra yet. */
408 if (zclient
->sock
< 0)
412 zlog_debug("%s: send register messages for VRF %u", __func__
,
415 /* We need router-id information. */
416 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_ADD
, vrf_id
);
418 /* We need interface information. */
419 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, vrf_id
);
421 /* Set unwanted redistribute route. */
422 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
423 vrf_bitmap_set(zclient
->redist
[afi
][zclient
->redist_default
],
426 /* Flush all redistribute request. */
427 if (vrf_id
== VRF_DEFAULT
) {
428 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
429 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
430 if (!zclient
->mi_redist
[afi
][i
].enabled
)
433 struct listnode
*node
;
436 for (ALL_LIST_ELEMENTS_RO(
437 zclient
->mi_redist
[afi
][i
]
440 if (!(i
== zclient
->redist_default
441 && *id
== zclient
->instance
))
442 zebra_redistribute_send(
443 ZEBRA_REDISTRIBUTE_ADD
,
444 zclient
, afi
, i
, *id
,
450 /* Resend all redistribute request. */
451 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
452 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
453 if (i
!= zclient
->redist_default
454 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
456 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
,
460 /* If default information is needed. */
461 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
462 zebra_redistribute_default_send(
463 ZEBRA_REDISTRIBUTE_DEFAULT_ADD
, zclient
, afi
,
468 /* Send unregister requests to zebra daemon for the information in a VRF. */
469 void zclient_send_dereg_requests(struct zclient
*zclient
, vrf_id_t vrf_id
)
474 /* If not connected to the zebra yet. */
475 if (zclient
->sock
< 0)
479 zlog_debug("%s: send deregister messages for VRF %u", __func__
,
482 /* We need router-id information. */
483 zebra_message_send(zclient
, ZEBRA_ROUTER_ID_DELETE
, vrf_id
);
485 zebra_message_send(zclient
, ZEBRA_INTERFACE_DELETE
, vrf_id
);
487 /* Set unwanted redistribute route. */
488 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
489 vrf_bitmap_unset(zclient
->redist
[afi
][zclient
->redist_default
],
492 /* Flush all redistribute request. */
493 if (vrf_id
== VRF_DEFAULT
) {
494 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
495 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
496 if (!zclient
->mi_redist
[afi
][i
].enabled
)
499 struct listnode
*node
;
502 for (ALL_LIST_ELEMENTS_RO(
503 zclient
->mi_redist
[afi
][i
]
506 if (!(i
== zclient
->redist_default
507 && *id
== zclient
->instance
))
508 zebra_redistribute_send(
509 ZEBRA_REDISTRIBUTE_DELETE
,
510 zclient
, afi
, i
, *id
,
516 /* Flush all redistribute request. */
517 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
518 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
519 if (i
!= zclient
->redist_default
520 && vrf_bitmap_check(zclient
->redist
[afi
][i
],
522 zebra_redistribute_send(
523 ZEBRA_REDISTRIBUTE_DELETE
, zclient
, afi
,
526 /* If default information is needed. */
527 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
528 zebra_redistribute_default_send(
529 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
, zclient
, afi
,
534 /* Send request to zebra daemon to start or stop RA. */
535 void zclient_send_interface_radv_req(struct zclient
*zclient
, vrf_id_t vrf_id
,
536 struct interface
*ifp
, int enable
,
541 /* If not connected to the zebra yet. */
542 if (zclient
->sock
< 0)
545 /* Form and send message. */
550 zclient_create_header(s
, ZEBRA_INTERFACE_ENABLE_RADV
, vrf_id
);
552 zclient_create_header(s
, ZEBRA_INTERFACE_DISABLE_RADV
, vrf_id
);
554 stream_putl(s
, ifp
->ifindex
);
555 stream_putl(s
, ra_interval
);
557 stream_putw_at(s
, 0, stream_get_endp(s
));
559 zclient_send_message(zclient
);
562 int zclient_send_interface_protodown(struct zclient
*zclient
, vrf_id_t vrf_id
,
563 struct interface
*ifp
, bool down
)
567 if (zclient
->sock
< 0)
572 zclient_create_header(s
, ZEBRA_INTERFACE_SET_PROTODOWN
, vrf_id
);
573 stream_putl(s
, ifp
->ifindex
);
574 stream_putc(s
, !!down
);
575 stream_putw_at(s
, 0, stream_get_endp(s
));
576 zclient_send_message(zclient
);
581 /* Make connection to zebra daemon. */
582 int zclient_start(struct zclient
*zclient
)
585 zlog_info("zclient_start is called");
587 /* If already connected to the zebra. */
588 if (zclient
->sock
>= 0)
591 /* Check connect thread. */
592 if (zclient
->t_connect
)
595 if (zclient_socket_connect(zclient
) < 0) {
597 zlog_debug("zclient connection fail");
599 zclient_event(ZCLIENT_CONNECT
, zclient
);
603 if (set_nonblocking(zclient
->sock
) < 0)
604 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: set_nonblocking(%d) failed",
605 __func__
, zclient
->sock
);
607 /* Clear fail count. */
610 zlog_debug("zclient connect success with socket [%d]",
613 /* Create read thread. */
614 zclient_event(ZCLIENT_READ
, zclient
);
616 zebra_hello_send(zclient
);
618 zebra_message_send(zclient
, ZEBRA_INTERFACE_ADD
, VRF_DEFAULT
);
620 /* Inform the successful connection. */
621 if (zclient
->zebra_connected
)
622 (*zclient
->zebra_connected
)(zclient
);
627 /* Initialize zebra client. Argument redist_default is unwanted
628 redistribute route type. */
629 void zclient_init(struct zclient
*zclient
, int redist_default
,
630 unsigned short instance
, struct zebra_privs_t
*privs
)
634 /* Set -1 to the default socket value. */
636 zclient
->privs
= privs
;
638 /* Clear redistribution flags. */
639 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
640 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++)
641 zclient
->redist
[afi
][i
] = vrf_bitmap_init();
643 /* Set unwanted redistribute route. bgpd does not need BGP route
645 zclient
->redist_default
= redist_default
;
646 zclient
->instance
= instance
;
647 /* Pending: make afi(s) an arg. */
648 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
649 redist_add_instance(&zclient
->mi_redist
[afi
][redist_default
],
652 /* Set default-information redistribute to zero. */
653 zclient
->default_information
[afi
] = vrf_bitmap_init();
657 zlog_debug("scheduling zclient connection");
659 zclient_event(ZCLIENT_SCHEDULE
, zclient
);
662 /* This function is a wrapper function for calling zclient_start from
663 timer or event thread. */
664 static int zclient_connect(struct thread
*t
)
666 struct zclient
*zclient
;
668 zclient
= THREAD_ARG(t
);
669 zclient
->t_connect
= NULL
;
672 zlog_debug("zclient_connect is called");
674 return zclient_start(zclient
);
677 int zclient_send_rnh(struct zclient
*zclient
, int command
, struct prefix
*p
,
678 bool exact_match
, vrf_id_t vrf_id
)
684 zclient_create_header(s
, command
, vrf_id
);
685 stream_putc(s
, (exact_match
) ? 1 : 0);
687 stream_putw(s
, PREFIX_FAMILY(p
));
688 stream_putc(s
, p
->prefixlen
);
689 switch (PREFIX_FAMILY(p
)) {
691 stream_put_in_addr(s
, &p
->u
.prefix4
);
694 stream_put(s
, &(p
->u
.prefix6
), 16);
699 stream_putw_at(s
, 0, stream_get_endp(s
));
701 return zclient_send_message(zclient
);
705 * "xdr_encode"-like interface that allows daemon (client) to send
706 * a message to zebra server for a route that needs to be
707 * added/deleted to the kernel. Info about the route is specified
708 * by the caller in a struct zapi_route. zapi_route_encode() then writes
709 * the info down the zclient socket using the stream_* functions.
711 * The corresponding read ("xdr_decode") function on the server
712 * side is zapi_route_decode().
714 * 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
715 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
716 * | Length (2) | Command | Route Type |
717 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
718 * | ZEBRA Flags | Message Flags | Prefix length |
719 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
720 * | Destination IPv4 Prefix for route |
721 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
726 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
727 * described, as per the Nexthop count. Each nexthop described as:
730 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
731 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
732 * | IPv4 Nexthop address or Interface Index number |
733 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
735 * Alternatively, if the route is a blackhole route, then Nexthop count
736 * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole
739 * The original struct zapi_route_*() infrastructure was built around
740 * the traditional (32-bit "gate OR ifindex") nexthop data unit.
741 * A special encoding can be used to feed onlink (64-bit "gate AND ifindex")
742 * nexthops into zapi_route_encode() using the same zapi_route structure.
743 * This is done by setting zapi_route fields as follows:
744 * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK
745 * - .nexthop_num == .ifindex_num
746 * - .nexthop and .ifindex are filled with gate and ifindex parts of
747 * each compound nexthop, both in the same order
749 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
752 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
755 * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value
757 * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value
759 * XXX: No attention paid to alignment.
761 int zclient_route_send(uint8_t cmd
, struct zclient
*zclient
,
762 struct zapi_route
*api
)
764 if (zapi_route_encode(cmd
, zclient
->obuf
, api
) < 0)
766 return zclient_send_message(zclient
);
769 static int zapi_nexthop_labels_cmp(const struct zapi_nexthop
*next1
,
770 const struct zapi_nexthop
*next2
)
772 if (next1
->label_num
> next2
->label_num
)
775 if (next1
->label_num
< next2
->label_num
)
778 return memcmp(next1
->labels
, next2
->labels
, next1
->label_num
);
781 static int zapi_nexthop_cmp_no_labels(const struct zapi_nexthop
*next1
,
782 const struct zapi_nexthop
*next2
)
786 if (next1
->vrf_id
< next2
->vrf_id
)
789 if (next1
->vrf_id
> next2
->vrf_id
)
792 if (next1
->type
< next2
->type
)
795 if (next1
->type
> next2
->type
)
798 switch (next1
->type
) {
799 case NEXTHOP_TYPE_IPV4
:
800 case NEXTHOP_TYPE_IPV6
:
801 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
806 case NEXTHOP_TYPE_IPV4_IFINDEX
:
807 case NEXTHOP_TYPE_IPV6_IFINDEX
:
808 ret
= nexthop_g_addr_cmp(next1
->type
, &next1
->gate
,
812 /* Intentional Fall-Through */
813 case NEXTHOP_TYPE_IFINDEX
:
814 if (next1
->ifindex
< next2
->ifindex
)
817 if (next1
->ifindex
> next2
->ifindex
)
820 case NEXTHOP_TYPE_BLACKHOLE
:
821 if (next1
->bh_type
< next2
->bh_type
)
824 if (next1
->bh_type
> next2
->bh_type
)
832 static int zapi_nexthop_cmp(const void *item1
, const void *item2
)
836 const struct zapi_nexthop
*next1
= item1
;
837 const struct zapi_nexthop
*next2
= item2
;
839 ret
= zapi_nexthop_cmp_no_labels(next1
, next2
);
843 ret
= zapi_nexthop_labels_cmp(next1
, next2
);
848 static void zapi_nexthop_group_sort(struct zapi_nexthop
*nh_grp
,
849 uint16_t nexthop_num
)
851 qsort(nh_grp
, nexthop_num
, sizeof(struct zapi_nexthop
),
855 int zapi_route_encode(uint8_t cmd
, struct stream
*s
, struct zapi_route
*api
)
857 struct zapi_nexthop
*api_nh
;
862 zclient_create_header(s
, cmd
, api
->vrf_id
);
864 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
865 flog_err(EC_LIB_ZAPI_ENCODE
,
866 "%s: Specified route type (%u) is not a legal value\n",
867 __PRETTY_FUNCTION__
, api
->type
);
870 stream_putc(s
, api
->type
);
872 stream_putw(s
, api
->instance
);
873 stream_putl(s
, api
->flags
);
874 stream_putc(s
, api
->message
);
876 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
877 flog_err(EC_LIB_ZAPI_ENCODE
,
878 "%s: Specified route SAFI (%u) is not a legal value\n",
879 __PRETTY_FUNCTION__
, api
->safi
);
882 stream_putc(s
, api
->safi
);
884 /* Put prefix information. */
885 stream_putc(s
, api
->prefix
.family
);
886 psize
= PSIZE(api
->prefix
.prefixlen
);
887 stream_putc(s
, api
->prefix
.prefixlen
);
888 stream_write(s
, (uint8_t *)&api
->prefix
.u
.prefix
, psize
);
890 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
891 psize
= PSIZE(api
->src_prefix
.prefixlen
);
892 stream_putc(s
, api
->src_prefix
.prefixlen
);
893 stream_write(s
, (uint8_t *)&api
->src_prefix
.prefix
, psize
);
897 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
898 /* limit the number of nexthops if necessary */
899 if (api
->nexthop_num
> MULTIPATH_NUM
) {
900 char buf
[PREFIX2STR_BUFFER
];
902 prefix2str(&api
->prefix
, buf
, sizeof(buf
));
905 "%s: prefix %s: can't encode %u nexthops (maximum is %u)",
906 __func__
, buf
, api
->nexthop_num
, MULTIPATH_NUM
);
910 zapi_nexthop_group_sort(api
->nexthops
, api
->nexthop_num
);
912 stream_putw(s
, api
->nexthop_num
);
914 for (i
= 0; i
< api
->nexthop_num
; i
++) {
915 api_nh
= &api
->nexthops
[i
];
917 stream_putl(s
, api_nh
->vrf_id
);
918 stream_putc(s
, api_nh
->type
);
919 stream_putc(s
, api_nh
->onlink
);
920 switch (api_nh
->type
) {
921 case NEXTHOP_TYPE_BLACKHOLE
:
922 stream_putc(s
, api_nh
->bh_type
);
924 case NEXTHOP_TYPE_IPV4
:
925 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
927 case NEXTHOP_TYPE_IPV4_IFINDEX
:
928 stream_put_in_addr(s
, &api_nh
->gate
.ipv4
);
929 stream_putl(s
, api_nh
->ifindex
);
931 case NEXTHOP_TYPE_IFINDEX
:
932 stream_putl(s
, api_nh
->ifindex
);
934 case NEXTHOP_TYPE_IPV6
:
935 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
938 case NEXTHOP_TYPE_IPV6_IFINDEX
:
939 stream_write(s
, (uint8_t *)&api_nh
->gate
.ipv6
,
941 stream_putl(s
, api_nh
->ifindex
);
945 /* MPLS labels for BGP-LU or Segment Routing */
946 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
947 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
948 char buf
[PREFIX2STR_BUFFER
];
949 prefix2str(&api
->prefix
, buf
,
951 flog_err(EC_LIB_ZAPI_ENCODE
,
952 "%s: prefix %s: can't encode "
953 "%u labels (maximum is %u)",
960 stream_putc(s
, api_nh
->label_num
);
961 stream_put(s
, &api_nh
->labels
[0],
963 * sizeof(mpls_label_t
));
966 /* Router MAC for EVPN routes. */
967 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
968 stream_put(s
, &(api_nh
->rmac
),
969 sizeof(struct ethaddr
));
974 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
975 stream_putc(s
, api
->distance
);
976 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
977 stream_putl(s
, api
->metric
);
978 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
979 stream_putl(s
, api
->tag
);
980 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
981 stream_putl(s
, api
->mtu
);
982 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
983 stream_putl(s
, api
->tableid
);
985 /* Put length at the first point of the stream. */
986 stream_putw_at(s
, 0, stream_get_endp(s
));
991 int zapi_route_decode(struct stream
*s
, struct zapi_route
*api
)
993 struct zapi_nexthop
*api_nh
;
996 memset(api
, 0, sizeof(*api
));
998 /* Type, flags, message. */
999 STREAM_GETC(s
, api
->type
);
1000 if (api
->type
>= ZEBRA_ROUTE_MAX
) {
1001 flog_err(EC_LIB_ZAPI_ENCODE
,
1002 "%s: Specified route type: %d is not a legal value\n",
1003 __PRETTY_FUNCTION__
, api
->type
);
1007 STREAM_GETW(s
, api
->instance
);
1008 STREAM_GETL(s
, api
->flags
);
1009 STREAM_GETC(s
, api
->message
);
1010 STREAM_GETC(s
, api
->safi
);
1011 if (api
->safi
< SAFI_UNICAST
|| api
->safi
>= SAFI_MAX
) {
1012 flog_err(EC_LIB_ZAPI_ENCODE
,
1013 "%s: Specified route SAFI (%u) is not a legal value\n",
1014 __PRETTY_FUNCTION__
, api
->safi
);
1019 STREAM_GETC(s
, api
->prefix
.family
);
1020 STREAM_GETC(s
, api
->prefix
.prefixlen
);
1021 switch (api
->prefix
.family
) {
1023 if (api
->prefix
.prefixlen
> IPV4_MAX_PREFIXLEN
) {
1026 "%s: V4 prefixlen is %d which should not be more than 32",
1027 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1032 if (api
->prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1035 "%s: v6 prefixlen is %d which should not be more than 128",
1036 __PRETTY_FUNCTION__
, api
->prefix
.prefixlen
);
1041 flog_err(EC_LIB_ZAPI_ENCODE
,
1042 "%s: Specified family %d is not v4 or v6",
1043 __PRETTY_FUNCTION__
, api
->prefix
.family
);
1046 STREAM_GET(&api
->prefix
.u
.prefix
, s
, PSIZE(api
->prefix
.prefixlen
));
1048 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_SRCPFX
)) {
1049 api
->src_prefix
.family
= AF_INET6
;
1050 STREAM_GETC(s
, api
->src_prefix
.prefixlen
);
1051 if (api
->src_prefix
.prefixlen
> IPV6_MAX_PREFIXLEN
) {
1054 "%s: SRC Prefix prefixlen received: %d is too large",
1055 __PRETTY_FUNCTION__
, api
->src_prefix
.prefixlen
);
1058 STREAM_GET(&api
->src_prefix
.prefix
, s
,
1059 PSIZE(api
->src_prefix
.prefixlen
));
1061 if (api
->prefix
.family
!= AF_INET6
1062 || api
->src_prefix
.prefixlen
== 0) {
1065 "%s: SRC prefix specified in some manner that makes no sense",
1066 __PRETTY_FUNCTION__
);
1072 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_NEXTHOP
)) {
1073 STREAM_GETW(s
, api
->nexthop_num
);
1074 if (api
->nexthop_num
> MULTIPATH_NUM
) {
1075 flog_err(EC_LIB_ZAPI_ENCODE
,
1076 "%s: invalid number of nexthops (%u)",
1077 __func__
, api
->nexthop_num
);
1081 for (i
= 0; i
< api
->nexthop_num
; i
++) {
1082 api_nh
= &api
->nexthops
[i
];
1084 STREAM_GETL(s
, api_nh
->vrf_id
);
1085 STREAM_GETC(s
, api_nh
->type
);
1086 STREAM_GETC(s
, api_nh
->onlink
);
1087 switch (api_nh
->type
) {
1088 case NEXTHOP_TYPE_BLACKHOLE
:
1089 STREAM_GETC(s
, api_nh
->bh_type
);
1091 case NEXTHOP_TYPE_IPV4
:
1092 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1095 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1096 STREAM_GET(&api_nh
->gate
.ipv4
.s_addr
, s
,
1098 STREAM_GETL(s
, api_nh
->ifindex
);
1100 case NEXTHOP_TYPE_IFINDEX
:
1101 STREAM_GETL(s
, api_nh
->ifindex
);
1103 case NEXTHOP_TYPE_IPV6
:
1104 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1106 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1107 STREAM_GET(&api_nh
->gate
.ipv6
, s
, 16);
1108 STREAM_GETL(s
, api_nh
->ifindex
);
1112 /* MPLS labels for BGP-LU or Segment Routing */
1113 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_LABEL
)) {
1114 STREAM_GETC(s
, api_nh
->label_num
);
1116 if (api_nh
->label_num
> MPLS_MAX_LABELS
) {
1119 "%s: invalid number of MPLS labels (%u)",
1120 __func__
, api_nh
->label_num
);
1124 STREAM_GET(&api_nh
->labels
[0], s
,
1126 * sizeof(mpls_label_t
));
1129 /* Router MAC for EVPN routes. */
1130 if (CHECK_FLAG(api
->flags
, ZEBRA_FLAG_EVPN_ROUTE
))
1131 stream_get(&(api_nh
->rmac
), s
,
1132 sizeof(struct ethaddr
));
1137 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_DISTANCE
))
1138 STREAM_GETC(s
, api
->distance
);
1139 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_METRIC
))
1140 STREAM_GETL(s
, api
->metric
);
1141 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TAG
))
1142 STREAM_GETL(s
, api
->tag
);
1143 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_MTU
))
1144 STREAM_GETL(s
, api
->mtu
);
1145 if (CHECK_FLAG(api
->message
, ZAPI_MESSAGE_TABLEID
))
1146 STREAM_GETL(s
, api
->tableid
);
1153 static void zapi_encode_prefix(struct stream
*s
, struct prefix
*p
,
1159 memset(&any
, 0, sizeof(any
));
1160 any
.family
= family
;
1164 stream_putc(s
, p
->family
);
1165 stream_putc(s
, p
->prefixlen
);
1166 stream_put(s
, &p
->u
.prefix
, prefix_blen(p
));
1169 int zapi_pbr_rule_encode(uint8_t cmd
, struct stream
*s
, struct pbr_rule
*zrule
)
1172 zclient_create_header(s
, cmd
, zrule
->vrf_id
);
1175 * We are sending one item at a time at the moment
1179 stream_putl(s
, zrule
->seq
);
1180 stream_putl(s
, zrule
->priority
);
1181 stream_putl(s
, zrule
->unique
);
1183 zapi_encode_prefix(s
, &(zrule
->filter
.src_ip
),
1184 zrule
->filter
.src_ip
.family
);
1185 stream_putw(s
, zrule
->filter
.src_port
); /* src port */
1186 zapi_encode_prefix(s
, &(zrule
->filter
.dst_ip
),
1187 zrule
->filter
.src_ip
.family
);
1188 stream_putw(s
, zrule
->filter
.dst_port
); /* dst port */
1189 stream_putw(s
, zrule
->filter
.fwmark
); /* fwmark */
1191 stream_putl(s
, zrule
->action
.table
);
1192 stream_putl(s
, zrule
->ifindex
);
1194 /* Put length at the first point of the stream. */
1195 stream_putw_at(s
, 0, stream_get_endp(s
));
1200 bool zapi_route_notify_decode(struct stream
*s
, struct prefix
*p
,
1202 enum zapi_route_notify_owner
*note
)
1206 STREAM_GET(note
, s
, sizeof(*note
));
1208 STREAM_GETC(s
, p
->family
);
1209 STREAM_GETC(s
, p
->prefixlen
);
1210 STREAM_GET(&p
->u
.prefix
, s
, prefix_blen(p
));
1221 bool zapi_rule_notify_decode(struct stream
*s
, uint32_t *seqno
,
1222 uint32_t *priority
, uint32_t *unique
,
1224 enum zapi_rule_notify_owner
*note
)
1226 uint32_t prio
, seq
, uni
;
1229 STREAM_GET(note
, s
, sizeof(*note
));
1231 STREAM_GETL(s
, seq
);
1232 STREAM_GETL(s
, prio
);
1233 STREAM_GETL(s
, uni
);
1234 STREAM_GETL(s
, ifi
);
1237 zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__
, seq
, prio
,
1250 bool zapi_ipset_notify_decode(struct stream
*s
, uint32_t *unique
,
1251 enum zapi_ipset_notify_owner
*note
)
1255 STREAM_GET(note
, s
, sizeof(*note
));
1257 STREAM_GETL(s
, uni
);
1260 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1269 bool zapi_ipset_entry_notify_decode(struct stream
*s
, uint32_t *unique
,
1271 enum zapi_ipset_entry_notify_owner
*note
)
1275 STREAM_GET(note
, s
, sizeof(*note
));
1277 STREAM_GETL(s
, uni
);
1279 STREAM_GET(ipset_name
, s
, ZEBRA_IPSET_NAME_SIZE
);
1282 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1291 bool zapi_iptable_notify_decode(struct stream
*s
,
1293 enum zapi_iptable_notify_owner
*note
)
1297 STREAM_GET(note
, s
, sizeof(*note
));
1299 STREAM_GETL(s
, uni
);
1302 zlog_debug("%s: %u", __PRETTY_FUNCTION__
, uni
);
1311 struct nexthop
*nexthop_from_zapi_nexthop(struct zapi_nexthop
*znh
)
1313 struct nexthop
*n
= nexthop_new();
1315 n
->type
= znh
->type
;
1316 n
->vrf_id
= znh
->vrf_id
;
1317 n
->ifindex
= znh
->ifindex
;
1318 n
->gate
= znh
->gate
;
1321 * This function currently handles labels
1323 if (znh
->label_num
) {
1324 nexthop_add_labels(n
, ZEBRA_LSP_NONE
, znh
->label_num
,
1331 bool zapi_nexthop_update_decode(struct stream
*s
, struct zapi_route
*nhr
)
1335 memset(nhr
, 0, sizeof(*nhr
));
1337 STREAM_GETW(s
, nhr
->prefix
.family
);
1338 STREAM_GETC(s
, nhr
->prefix
.prefixlen
);
1339 switch (nhr
->prefix
.family
) {
1341 STREAM_GET(&nhr
->prefix
.u
.prefix4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
1344 STREAM_GET(&nhr
->prefix
.u
.prefix6
, s
, IPV6_MAX_BYTELEN
);
1350 STREAM_GETC(s
, nhr
->type
);
1351 STREAM_GETW(s
, nhr
->instance
);
1352 STREAM_GETC(s
, nhr
->distance
);
1353 STREAM_GETL(s
, nhr
->metric
);
1354 STREAM_GETC(s
, nhr
->nexthop_num
);
1356 for (i
= 0; i
< nhr
->nexthop_num
; i
++) {
1357 STREAM_GETL(s
, nhr
->nexthops
[i
].vrf_id
);
1358 STREAM_GETC(s
, nhr
->nexthops
[i
].type
);
1359 switch (nhr
->nexthops
[i
].type
) {
1360 case NEXTHOP_TYPE_IPV4
:
1361 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1362 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv4
.s_addr
, s
,
1364 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1366 case NEXTHOP_TYPE_IFINDEX
:
1367 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1369 case NEXTHOP_TYPE_IPV6
:
1370 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1371 STREAM_GET(&nhr
->nexthops
[i
].gate
.ipv6
, s
,
1373 STREAM_GETL(s
, nhr
->nexthops
[i
].ifindex
);
1375 case NEXTHOP_TYPE_BLACKHOLE
:
1378 STREAM_GETC(s
, nhr
->nexthops
[i
].label_num
);
1379 if (nhr
->nexthops
[i
].label_num
> MPLS_MAX_LABELS
) {
1380 flog_err(EC_LIB_ZAPI_ENCODE
,
1381 "%s: invalid number of MPLS labels (%u)",
1382 __func__
, nhr
->nexthops
[i
].label_num
);
1385 if (nhr
->nexthops
[i
].label_num
)
1386 STREAM_GET(&nhr
->nexthops
[i
].labels
[0], s
,
1387 nhr
->nexthops
[i
].label_num
1388 * sizeof(mpls_label_t
));
1397 * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE
1398 * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will
1399 * then set/unset redist[type] in the client handle (a struct zserv) for the
1402 int zebra_redistribute_send(int command
, struct zclient
*zclient
, afi_t afi
,
1403 int type
, unsigned short instance
, vrf_id_t vrf_id
)
1410 zclient_create_header(s
, command
, vrf_id
);
1411 stream_putc(s
, afi
);
1412 stream_putc(s
, type
);
1413 stream_putw(s
, instance
);
1415 stream_putw_at(s
, 0, stream_get_endp(s
));
1417 return zclient_send_message(zclient
);
1420 int zebra_redistribute_default_send(int command
, struct zclient
*zclient
,
1421 afi_t afi
, vrf_id_t vrf_id
)
1428 zclient_create_header(s
, command
, vrf_id
);
1429 stream_putc(s
, afi
);
1431 stream_putw_at(s
, 0, stream_get_endp(s
));
1433 return zclient_send_message(zclient
);
1436 /* Get prefix in ZServ format; family should be filled in on prefix */
1437 static void zclient_stream_get_prefix(struct stream
*s
, struct prefix
*p
)
1439 size_t plen
= prefix_blen(p
);
1446 stream_get(&p
->u
.prefix
, s
, plen
);
1448 p
->prefixlen
= MIN(plen
* 8, c
);
1454 /* Router-id update from zebra daemon. */
1455 void zebra_router_id_update_read(struct stream
*s
, struct prefix
*rid
)
1457 /* Fetch interface address. */
1458 STREAM_GETC(s
, rid
->family
);
1460 zclient_stream_get_prefix(s
, rid
);
1466 /* Interface addition from zebra daemon. */
1468 * The format of the message sent with type ZEBRA_INTERFACE_ADD or
1469 * ZEBRA_INTERFACE_DELETE from zebra to the client is:
1471 * 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
1472 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1478 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1480 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1482 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1487 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1491 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1496 * | parent ifindex |
1497 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1498 * | Link Layer Type |
1499 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1500 * | Harware Address Length |
1501 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1502 * | Hardware Address if HW lenght different from 0 |
1503 * | ... max INTERFACE_HWADDR_MAX |
1504 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1505 * | Link_params? | Whether a link-params follows: 1 or 0.
1506 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1507 * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized |
1508 * | .... (struct if_link_params). |
1509 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1512 static void zclient_vrf_add(struct zclient
*zclient
, vrf_id_t vrf_id
)
1515 char vrfname_tmp
[VRF_NAMSIZ
];
1516 struct vrf_data data
;
1518 stream_get(&data
, zclient
->ibuf
, sizeof(struct vrf_data
));
1519 /* Read interface name. */
1520 stream_get(vrfname_tmp
, zclient
->ibuf
, VRF_NAMSIZ
);
1522 /* Lookup/create vrf by vrf_id. */
1523 vrf
= vrf_get(vrf_id
, vrfname_tmp
);
1524 vrf
->data
.l
.table_id
= data
.l
.table_id
;
1525 memcpy(vrf
->data
.l
.netns_name
, data
.l
.netns_name
, NS_NAMSIZ
);
1526 /* overwrite default vrf */
1527 if (vrf_id
== VRF_DEFAULT
)
1528 vrf_set_default_name(vrfname_tmp
, false);
1532 static void zclient_vrf_delete(struct zclient
*zclient
, vrf_id_t vrf_id
)
1536 /* Lookup vrf by vrf_id. */
1537 vrf
= vrf_lookup_by_id(vrf_id
);
1540 * If a routing protocol doesn't know about a
1541 * vrf that is about to be deleted. There is
1542 * no point in attempting to delete it.
1550 struct interface
*zebra_interface_add_read(struct stream
*s
, vrf_id_t vrf_id
)
1552 struct interface
*ifp
;
1553 char ifname_tmp
[INTERFACE_NAMSIZ
];
1555 /* Read interface name. */
1556 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1558 /* Lookup/create interface by name. */
1559 ifp
= if_get_by_name(ifname_tmp
, vrf_id
);
1561 zebra_interface_if_set_value(s
, ifp
);
1567 * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN)
1568 * from zebra server. The format of this message is the same as
1569 * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see
1570 * comments for zebra_interface_add_read), except that no sockaddr_dl
1571 * is sent at the tail of the message.
1573 struct interface
*zebra_interface_state_read(struct stream
*s
, vrf_id_t vrf_id
)
1575 struct interface
*ifp
;
1576 char ifname_tmp
[INTERFACE_NAMSIZ
];
1578 /* Read interface name. */
1579 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
1581 /* Lookup this by interface index. */
1582 ifp
= if_lookup_by_name(ifname_tmp
, vrf_id
);
1584 flog_err(EC_LIB_ZAPI_ENCODE
,
1585 "INTERFACE_STATE: Cannot find IF %s in VRF %d",
1586 ifname_tmp
, vrf_id
);
1590 zebra_interface_if_set_value(s
, ifp
);
1595 static void link_params_set_value(struct stream
*s
, struct if_link_params
*iflp
)
1601 iflp
->lp_status
= stream_getl(s
);
1602 iflp
->te_metric
= stream_getl(s
);
1603 iflp
->max_bw
= stream_getf(s
);
1604 iflp
->max_rsv_bw
= stream_getf(s
);
1605 uint32_t bwclassnum
= stream_getl(s
);
1608 for (i
= 0; i
< bwclassnum
&& i
< MAX_CLASS_TYPE
; i
++)
1609 iflp
->unrsv_bw
[i
] = stream_getf(s
);
1612 EC_LIB_ZAPI_MISSMATCH
,
1613 "%s: received %d > %d (MAX_CLASS_TYPE) bw entries"
1614 " - outdated library?",
1615 __func__
, bwclassnum
, MAX_CLASS_TYPE
);
1617 iflp
->admin_grp
= stream_getl(s
);
1618 iflp
->rmt_as
= stream_getl(s
);
1619 iflp
->rmt_ip
.s_addr
= stream_get_ipv4(s
);
1621 iflp
->av_delay
= stream_getl(s
);
1622 iflp
->min_delay
= stream_getl(s
);
1623 iflp
->max_delay
= stream_getl(s
);
1624 iflp
->delay_var
= stream_getl(s
);
1626 iflp
->pkt_loss
= stream_getf(s
);
1627 iflp
->res_bw
= stream_getf(s
);
1628 iflp
->ava_bw
= stream_getf(s
);
1629 iflp
->use_bw
= stream_getf(s
);
1632 struct interface
*zebra_interface_link_params_read(struct stream
*s
,
1635 struct if_link_params
*iflp
;
1640 ifindex
= stream_getl(s
);
1642 struct interface
*ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1645 flog_err(EC_LIB_ZAPI_ENCODE
,
1646 "%s: unknown ifindex %u, shouldn't happen", __func__
,
1651 if ((iflp
= if_link_params_get(ifp
)) == NULL
)
1654 link_params_set_value(s
, iflp
);
1659 void zebra_interface_if_set_value(struct stream
*s
, struct interface
*ifp
)
1661 uint8_t link_params_status
= 0;
1662 ifindex_t old_ifindex
;
1664 old_ifindex
= ifp
->ifindex
;
1665 /* Read interface's index. */
1666 if_set_index(ifp
, stream_getl(s
));
1667 ifp
->status
= stream_getc(s
);
1669 /* Read interface's value. */
1670 ifp
->flags
= stream_getq(s
);
1671 ifp
->ptm_enable
= stream_getc(s
);
1672 ifp
->ptm_status
= stream_getc(s
);
1673 ifp
->metric
= stream_getl(s
);
1674 ifp
->speed
= stream_getl(s
);
1675 ifp
->mtu
= stream_getl(s
);
1676 ifp
->mtu6
= stream_getl(s
);
1677 ifp
->bandwidth
= stream_getl(s
);
1678 ifp
->link_ifindex
= stream_getl(s
);
1679 ifp
->ll_type
= stream_getl(s
);
1680 ifp
->hw_addr_len
= stream_getl(s
);
1681 if (ifp
->hw_addr_len
)
1682 stream_get(ifp
->hw_addr
, s
,
1683 MIN(ifp
->hw_addr_len
, INTERFACE_HWADDR_MAX
));
1685 /* Read Traffic Engineering status */
1686 link_params_status
= stream_getc(s
);
1687 /* Then, Traffic Engineering parameters if any */
1688 if (link_params_status
) {
1689 struct if_link_params
*iflp
= if_link_params_get(ifp
);
1690 link_params_set_value(s
, iflp
);
1693 nexthop_group_interface_state_change(ifp
, old_ifindex
);
1696 size_t zebra_interface_link_params_write(struct stream
*s
,
1697 struct interface
*ifp
)
1700 struct if_link_params
*iflp
;
1703 if (s
== NULL
|| ifp
== NULL
|| ifp
->link_params
== NULL
)
1706 iflp
= ifp
->link_params
;
1709 w
+= stream_putl(s
, iflp
->lp_status
);
1711 w
+= stream_putl(s
, iflp
->te_metric
);
1712 w
+= stream_putf(s
, iflp
->max_bw
);
1713 w
+= stream_putf(s
, iflp
->max_rsv_bw
);
1715 w
+= stream_putl(s
, MAX_CLASS_TYPE
);
1716 for (i
= 0; i
< MAX_CLASS_TYPE
; i
++)
1717 w
+= stream_putf(s
, iflp
->unrsv_bw
[i
]);
1719 w
+= stream_putl(s
, iflp
->admin_grp
);
1720 w
+= stream_putl(s
, iflp
->rmt_as
);
1721 w
+= stream_put_in_addr(s
, &iflp
->rmt_ip
);
1723 w
+= stream_putl(s
, iflp
->av_delay
);
1724 w
+= stream_putl(s
, iflp
->min_delay
);
1725 w
+= stream_putl(s
, iflp
->max_delay
);
1726 w
+= stream_putl(s
, iflp
->delay_var
);
1728 w
+= stream_putf(s
, iflp
->pkt_loss
);
1729 w
+= stream_putf(s
, iflp
->res_bw
);
1730 w
+= stream_putf(s
, iflp
->ava_bw
);
1731 w
+= stream_putf(s
, iflp
->use_bw
);
1737 * format of message for address additon is:
1741 * | type | ZEBRA_INTERFACE_ADDRESS_ADD or
1742 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE
1751 * | ifc_flags | flags for connected address
1759 * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs.
1767 static int memconstant(const void *s
, int c
, size_t n
)
1769 const uint8_t *p
= s
;
1778 struct connected
*zebra_interface_address_read(int type
, struct stream
*s
,
1782 struct interface
*ifp
;
1783 struct connected
*ifc
;
1784 struct prefix p
, d
, *dp
;
1788 memset(&p
, 0, sizeof(p
));
1789 memset(&d
, 0, sizeof(d
));
1791 /* Get interface index. */
1792 ifindex
= stream_getl(s
);
1795 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1797 flog_err(EC_LIB_ZAPI_ENCODE
,
1798 "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d",
1799 (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) ? "ADD" : "DEL",
1805 ifc_flags
= stream_getc(s
);
1807 /* Fetch interface address. */
1808 d
.family
= p
.family
= stream_getc(s
);
1809 plen
= prefix_blen(&d
);
1811 zclient_stream_get_prefix(s
, &p
);
1813 /* Fetch destination address. */
1814 stream_get(&d
.u
.prefix
, s
, plen
);
1816 /* N.B. NULL destination pointers are encoded as all zeroes */
1817 dp
= memconstant(&d
.u
.prefix
, 0, plen
) ? NULL
: &d
;
1819 if (type
== ZEBRA_INTERFACE_ADDRESS_ADD
) {
1820 ifc
= connected_lookup_prefix_exact(ifp
, &p
);
1822 /* N.B. NULL destination pointers are encoded as all
1824 ifc
= connected_add_by_prefix(ifp
, &p
, dp
);
1827 ifc
->flags
= ifc_flags
;
1828 if (ifc
->destination
)
1829 ifc
->destination
->prefixlen
=
1830 ifc
->address
->prefixlen
;
1831 else if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
1832 /* carp interfaces on OpenBSD with 0.0.0.0/0 as
1834 char buf
[PREFIX_STRLEN
];
1837 "warning: interface %s address %s with peer flag set, but no peer address!",
1839 prefix2str(ifc
->address
, buf
,
1841 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
1845 assert(type
== ZEBRA_INTERFACE_ADDRESS_DELETE
);
1846 ifc
= connected_delete_by_prefix(ifp
, &p
);
1853 * format of message for neighbor connected address is:
1857 * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or
1858 * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE
1873 * | addr_len | len of addr.
1876 struct nbr_connected
*
1877 zebra_interface_nbr_address_read(int type
, struct stream
*s
, vrf_id_t vrf_id
)
1879 unsigned int ifindex
;
1880 struct interface
*ifp
;
1882 struct nbr_connected
*ifc
;
1884 /* Get interface index. */
1885 ifindex
= stream_getl(s
);
1888 ifp
= if_lookup_by_index(ifindex
, vrf_id
);
1890 flog_err(EC_LIB_ZAPI_ENCODE
,
1891 "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d",
1892 (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) ? "ADD"
1898 p
.family
= stream_getc(s
);
1899 stream_get(&p
.u
.prefix
, s
, prefix_blen(&p
));
1900 p
.prefixlen
= stream_getc(s
);
1902 if (type
== ZEBRA_INTERFACE_NBR_ADDRESS_ADD
) {
1903 /* Currently only supporting P2P links, so any new RA source
1905 considered as the replacement of the previously learnt
1906 Link-Local address. */
1907 if (!(ifc
= listnode_head(ifp
->nbr_connected
))) {
1908 ifc
= nbr_connected_new();
1909 ifc
->address
= prefix_new();
1911 listnode_add(ifp
->nbr_connected
, ifc
);
1914 prefix_copy(ifc
->address
, &p
);
1916 assert(type
== ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
);
1918 ifc
= nbr_connected_check(ifp
, &p
);
1920 listnode_delete(ifp
->nbr_connected
, ifc
);
1926 struct interface
*zebra_interface_vrf_update_read(struct stream
*s
,
1928 vrf_id_t
*new_vrf_id
)
1930 char ifname
[INTERFACE_NAMSIZ
];
1931 struct interface
*ifp
;
1934 /* Read interface name. */
1935 stream_get(ifname
, s
, INTERFACE_NAMSIZ
);
1937 /* Lookup interface. */
1938 ifp
= if_lookup_by_name(ifname
, vrf_id
);
1940 flog_err(EC_LIB_ZAPI_ENCODE
,
1941 "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d",
1946 /* Fetch new VRF Id. */
1947 new_id
= stream_getw(s
);
1949 *new_vrf_id
= new_id
;
1953 /* filter unwanted messages until the expected one arrives */
1954 static int zclient_read_sync_response(struct zclient
*zclient
,
1955 uint16_t expected_cmd
)
1967 cmd
= expected_cmd
+ 1;
1968 while (ret
== 0 && cmd
!= expected_cmd
) {
1972 /* wait until response arrives */
1974 FD_SET(zclient
->sock
, &readfds
);
1975 select(zclient
->sock
+ 1, &readfds
, NULL
, NULL
, NULL
);
1976 if (!FD_ISSET(zclient
->sock
, &readfds
))
1979 ret
= zclient_read_header(s
, zclient
->sock
, &size
, &marker
,
1980 &version
, &vrf_id
, &cmd
);
1982 zlog_debug("%s: Response (%d bytes) received", __func__
,
1986 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: Invalid Sync Message Reply",
1994 * Connect to label manager in a syncronous way
1996 * It first writes the request to zcient output buffer and then
1997 * immediately reads the answer from the input buffer.
1999 * @param zclient Zclient used to connect to label manager (zebra)
2000 * @param async Synchronous (0) or asynchronous (1) operation
2001 * @result Result of response
2003 int lm_label_manager_connect(struct zclient
*zclient
, int async
)
2008 uint16_t cmd
= async
? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
:
2009 ZEBRA_LABEL_MANAGER_CONNECT
;
2012 zlog_debug("Connecting to Label Manager (LM)");
2014 if (zclient
->sock
< 0) {
2015 zlog_debug("%s: invalid zclient socket", __func__
);
2022 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2025 stream_putc(s
, zclient
->redist_default
);
2027 stream_putw(s
, zclient
->instance
);
2029 /* Put length at the first point of the stream. */
2030 stream_putw_at(s
, 0, stream_get_endp(s
));
2032 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2034 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2035 close(zclient
->sock
);
2040 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2041 close(zclient
->sock
);
2046 zlog_debug("LM connect request sent (%d bytes)", ret
);
2052 if (zclient_read_sync_response(zclient
, cmd
)
2058 /* read instance and proto */
2059 uint8_t proto
= stream_getc(s
);
2060 uint16_t instance
= stream_getw(s
);
2063 if (proto
!= zclient
->redist_default
)
2066 "Wrong proto (%u) in LM connect response. Should be %u",
2067 proto
, zclient
->redist_default
);
2068 if (instance
!= zclient
->instance
)
2071 "Wrong instId (%u) in LM connect response. Should be %u",
2072 instance
, zclient
->instance
);
2075 result
= stream_getc(s
);
2077 zlog_debug("LM connect-response received, result %u", result
);
2083 * Asynchronous label chunk request
2085 * @param zclient Zclient used to connect to label manager (zebra)
2086 * @param keep Avoid garbage collection
2087 * @param chunk_size Amount of labels requested
2088 * @param base Base for the label chunk. if MPLS_LABEL_BASE_ANY we do not care
2089 * @result 0 on success, -1 otherwise
2091 int zclient_send_get_label_chunk(struct zclient
*zclient
, uint8_t keep
,
2092 uint32_t chunk_size
, uint32_t base
)
2097 zlog_debug("Getting Label Chunk");
2099 if (zclient
->sock
< 0)
2105 zclient_create_header(s
, ZEBRA_GET_LABEL_CHUNK
, VRF_DEFAULT
);
2107 stream_putc(s
, zclient
->redist_default
);
2109 stream_putw(s
, zclient
->instance
);
2110 stream_putc(s
, keep
);
2111 stream_putl(s
, chunk_size
);
2112 stream_putl(s
, base
);
2114 /* Put length at the first point of the stream. */
2115 stream_putw_at(s
, 0, stream_get_endp(s
));
2117 return zclient_send_message(zclient
);
2121 * Function to request a label chunk in a syncronous way
2123 * It first writes the request to zlcient output buffer and then
2124 * immediately reads the answer from the input buffer.
2126 * @param zclient Zclient used to connect to label manager (zebra)
2127 * @param keep Avoid garbage collection
2128 * @param chunk_size Amount of labels requested
2129 * @param start To write first assigned chunk label to
2130 * @param end To write last assigned chunk label to
2131 * @result 0 on success, -1 otherwise
2133 int lm_get_label_chunk(struct zclient
*zclient
, uint8_t keep
, uint32_t base
,
2134 uint32_t chunk_size
, uint32_t *start
, uint32_t *end
)
2138 uint8_t response_keep
;
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
);
2155 stream_putc(s
, keep
);
2157 stream_putl(s
, chunk_size
);
2158 /* requested chunk base */
2159 stream_putl(s
, base
);
2160 /* Put length at the first point of the stream. */
2161 stream_putw_at(s
, 0, stream_get_endp(s
));
2163 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2165 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2166 close(zclient
->sock
);
2171 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock closed");
2172 close(zclient
->sock
);
2177 zlog_debug("Label chunk request (%d bytes) sent", ret
);
2180 if (zclient_read_sync_response(zclient
, ZEBRA_GET_LABEL_CHUNK
) != 0)
2183 /* parse response */
2186 /* read proto and instance */
2187 uint8_t proto
= stream_getc(s
);
2188 uint16_t instance
= stream_getw(s
);
2191 if (proto
!= zclient
->redist_default
)
2192 flog_err(EC_LIB_ZAPI_ENCODE
,
2193 "Wrong proto (%u) in get chunk response. Should be %u",
2194 proto
, zclient
->redist_default
);
2195 if (instance
!= zclient
->instance
)
2196 flog_err(EC_LIB_ZAPI_ENCODE
,
2197 "Wrong instId (%u) in get chunk response Should be %u",
2198 instance
, zclient
->instance
);
2200 /* if we requested a specific chunk and it could not be allocated, the
2201 * response message will end here
2203 if (!STREAM_READABLE(s
)) {
2204 zlog_info("Unable to assign Label Chunk to %s instance %u",
2205 zebra_route_string(proto
), instance
);
2210 response_keep
= stream_getc(s
);
2211 /* start and end labels */
2212 *start
= stream_getl(s
);
2213 *end
= stream_getl(s
);
2215 /* not owning this response */
2216 if (keep
!= response_keep
) {
2219 "Invalid Label chunk: %u - %u, keeps mismatch %u != %u",
2220 *start
, *end
, keep
, response_keep
);
2223 if (*start
> *end
|| *start
< MPLS_LABEL_UNRESERVED_MIN
2224 || *end
> MPLS_LABEL_UNRESERVED_MAX
) {
2225 flog_err(EC_LIB_ZAPI_ENCODE
, "Invalid Label chunk: %u - %u",
2231 zlog_debug("Label Chunk assign: %u - %u (%u)", *start
, *end
,
2238 * Function to release a label chunk
2240 * @param zclient Zclient used to connect to label manager (zebra)
2241 * @param start First label of chunk
2242 * @param end Last label of chunk
2243 * @result 0 on success, -1 otherwise
2245 int lm_release_label_chunk(struct zclient
*zclient
, uint32_t start
,
2252 zlog_debug("Releasing Label Chunk %u - %u", start
, end
);
2254 if (zclient
->sock
< 0)
2260 zclient_create_header(s
, ZEBRA_RELEASE_LABEL_CHUNK
, VRF_DEFAULT
);
2263 stream_putc(s
, zclient
->redist_default
);
2265 stream_putw(s
, zclient
->instance
);
2267 stream_putl(s
, start
);
2269 stream_putl(s
, end
);
2271 /* Put length at the first point of the stream. */
2272 stream_putw_at(s
, 0, stream_get_endp(s
));
2274 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2276 flog_err(EC_LIB_ZAPI_SOCKET
, "Can't write to zclient sock");
2277 close(zclient
->sock
);
2282 flog_err(EC_LIB_ZAPI_SOCKET
, "Zclient sock connection closed");
2283 close(zclient
->sock
);
2292 * Connect to table manager in a syncronous way
2294 * It first writes the request to zcient output buffer and then
2295 * immediately reads the answer from the input buffer.
2297 * @param zclient Zclient used to connect to table manager (zebra)
2298 * @result Result of response
2300 int tm_table_manager_connect(struct zclient
*zclient
)
2307 zlog_debug("Connecting to Table Manager");
2309 if (zclient
->sock
< 0)
2315 zclient_create_header(s
, ZEBRA_TABLE_MANAGER_CONNECT
, VRF_DEFAULT
);
2318 stream_putc(s
, zclient
->redist_default
);
2320 stream_putw(s
, zclient
->instance
);
2322 /* Put length at the first point of the stream. */
2323 stream_putw_at(s
, 0, stream_get_endp(s
));
2325 ret
= zclient_send_message(zclient
);
2330 zlog_debug("%s: Table manager connect request sent", __func__
);
2333 if (zclient_read_sync_response(zclient
, ZEBRA_TABLE_MANAGER_CONNECT
)
2339 STREAM_GETC(s
, result
);
2342 "%s: Table Manager connect response received, result %u",
2351 * Function to request a table chunk in a syncronous way
2353 * It first writes the request to zclient output buffer and then
2354 * immediately reads the answer from the input buffer.
2356 * @param zclient Zclient used to connect to table manager (zebra)
2357 * @param chunk_size Amount of table requested
2358 * @param start to write first assigned chunk table RT ID to
2359 * @param end To write last assigned chunk table RT ID to
2360 * @result 0 on success, -1 otherwise
2362 int tm_get_table_chunk(struct zclient
*zclient
, uint32_t chunk_size
,
2363 uint32_t *start
, uint32_t *end
)
2369 zlog_debug("Getting Table Chunk");
2371 if (zclient
->sock
< 0)
2377 zclient_create_header(s
, ZEBRA_GET_TABLE_CHUNK
, VRF_DEFAULT
);
2379 stream_putl(s
, chunk_size
);
2380 /* Put length at the first point of the stream. */
2381 stream_putw_at(s
, 0, stream_get_endp(s
));
2383 ret
= writen(zclient
->sock
, s
->data
, stream_get_endp(s
));
2385 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: can't write to zclient->sock",
2387 close(zclient
->sock
);
2392 flog_err(EC_LIB_ZAPI_SOCKET
,
2393 "%s: zclient->sock connection closed", __func__
);
2394 close(zclient
->sock
);
2399 zlog_debug("%s: Table chunk request (%d bytes) sent", __func__
,
2403 if (zclient_read_sync_response(zclient
, ZEBRA_GET_TABLE_CHUNK
) != 0)
2407 /* start and end table IDs */
2408 STREAM_GETL(s
, *start
);
2409 STREAM_GETL(s
, *end
);
2412 zlog_debug("Table Chunk assign: %u - %u ", *start
, *end
);
2420 * Function to release a table chunk
2422 * @param zclient Zclient used to connect to table manager (zebra)
2423 * @param start First label of table
2424 * @param end Last label of chunk
2425 * @result 0 on success, -1 otherwise
2427 int tm_release_table_chunk(struct zclient
*zclient
, uint32_t start
,
2433 zlog_debug("Releasing Table Chunk");
2435 if (zclient
->sock
< 0)
2441 zclient_create_header(s
, ZEBRA_RELEASE_TABLE_CHUNK
, VRF_DEFAULT
);
2444 stream_putl(s
, start
);
2446 stream_putl(s
, end
);
2448 /* Put length at the first point of the stream. */
2449 stream_putw_at(s
, 0, stream_get_endp(s
));
2451 return zclient_send_message(zclient
);
2454 int zebra_send_mpls_labels(struct zclient
*zclient
, int cmd
,
2455 struct zapi_labels
*zl
)
2457 if (zapi_labels_encode(zclient
->obuf
, cmd
, zl
) < 0)
2459 return zclient_send_message(zclient
);
2462 int zapi_labels_encode(struct stream
*s
, int cmd
, struct zapi_labels
*zl
)
2466 zclient_create_header(s
, cmd
, VRF_DEFAULT
);
2467 stream_putc(s
, zl
->type
);
2468 stream_putw(s
, zl
->prefix
.family
);
2469 stream_put_prefix(s
, &zl
->prefix
);
2470 switch (zl
->prefix
.family
) {
2472 stream_put_in_addr(s
, &zl
->nexthop
.ipv4
);
2475 stream_write(s
, (uint8_t *)&zl
->nexthop
.ipv6
, 16);
2478 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2481 stream_putl(s
, zl
->ifindex
);
2482 stream_putc(s
, zl
->distance
);
2483 stream_putl(s
, zl
->local_label
);
2484 stream_putl(s
, zl
->remote_label
);
2486 /* Put length at the first point of the stream. */
2487 stream_putw_at(s
, 0, stream_get_endp(s
));
2492 int zapi_labels_decode(struct stream
*s
, struct zapi_labels
*zl
)
2496 memset(zl
, 0, sizeof(*zl
));
2499 STREAM_GETC(s
, zl
->type
);
2500 STREAM_GETW(s
, zl
->prefix
.family
);
2501 STREAM_GETC(s
, zl
->prefix
.prefixlen
);
2502 psize
= PSIZE(zl
->prefix
.prefixlen
);
2504 switch (zl
->prefix
.family
) {
2506 if (zl
->prefix
.prefixlen
> IPV4_MAX_BITLEN
) {
2508 "%s: Specified prefix length %d is greater than a v4 address can support",
2509 __PRETTY_FUNCTION__
, zl
->prefix
.prefixlen
);
2512 STREAM_GET(&zl
->prefix
.u
.prefix4
.s_addr
, s
, psize
);
2513 STREAM_GET(&zl
->nexthop
.ipv4
.s_addr
, s
, IPV4_MAX_BYTELEN
);
2516 if (zl
->prefix
.prefixlen
> IPV6_MAX_BITLEN
) {
2518 "%s: Specified prefix length %d is greater than a v6 address can support",
2519 __PRETTY_FUNCTION__
, zl
->prefix
.prefixlen
);
2522 STREAM_GET(&zl
->prefix
.u
.prefix6
, s
, psize
);
2523 STREAM_GET(&zl
->nexthop
.ipv6
, s
, 16);
2526 zlog_debug("%s: Specified AF %d is not supported for this call",
2527 __PRETTY_FUNCTION__
, zl
->prefix
.family
);
2530 STREAM_GETL(s
, zl
->ifindex
);
2531 STREAM_GETC(s
, zl
->distance
);
2532 STREAM_GETL(s
, zl
->local_label
);
2533 STREAM_GETL(s
, zl
->remote_label
);
2540 int zebra_send_pw(struct zclient
*zclient
, int command
, struct zapi_pw
*pw
)
2548 zclient_create_header(s
, command
, VRF_DEFAULT
);
2549 stream_write(s
, pw
->ifname
, IF_NAMESIZE
);
2550 stream_putl(s
, pw
->ifindex
);
2553 stream_putl(s
, pw
->type
);
2556 stream_putl(s
, pw
->af
);
2559 stream_put_in_addr(s
, &pw
->nexthop
.ipv4
);
2562 stream_write(s
, (uint8_t *)&pw
->nexthop
.ipv6
, 16);
2565 flog_err(EC_LIB_ZAPI_ENCODE
, "%s: unknown af", __func__
);
2570 stream_putl(s
, pw
->local_label
);
2571 stream_putl(s
, pw
->remote_label
);
2574 stream_putc(s
, pw
->flags
);
2576 /* Protocol specific fields */
2577 stream_write(s
, &pw
->data
, sizeof(union pw_protocol_fields
));
2579 /* Put length at the first point of the stream. */
2580 stream_putw_at(s
, 0, stream_get_endp(s
));
2582 return zclient_send_message(zclient
);
2586 * Receive PW status update from Zebra and send it to LDE process.
2588 void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS
, struct zapi_pw_status
*pw
)
2592 memset(pw
, 0, sizeof(struct zapi_pw_status
));
2596 stream_get(pw
->ifname
, s
, IF_NAMESIZE
);
2597 pw
->ifindex
= stream_getl(s
);
2598 pw
->status
= stream_getl(s
);
2601 static void zclient_capability_decode(ZAPI_CALLBACK_ARGS
)
2603 struct zclient_capabilities cap
;
2604 struct stream
*s
= zclient
->ibuf
;
2606 uint8_t mpls_enabled
;
2608 STREAM_GETL(s
, vrf_backend
);
2609 vrf_configure_backend(vrf_backend
);
2611 memset(&cap
, 0, sizeof(cap
));
2612 STREAM_GETC(s
, mpls_enabled
);
2613 cap
.mpls_enabled
= !!mpls_enabled
;
2614 STREAM_GETL(s
, cap
.ecmp
);
2615 STREAM_GETC(s
, cap
.role
);
2617 if (zclient
->zebra_capabilities
)
2618 (*zclient
->zebra_capabilities
)(&cap
);
2624 /* Zebra client message read function. */
2625 static int zclient_read(struct thread
*thread
)
2628 uint16_t length
, command
;
2629 uint8_t marker
, version
;
2631 struct zclient
*zclient
;
2633 /* Get socket to zebra. */
2634 zclient
= THREAD_ARG(thread
);
2635 zclient
->t_read
= NULL
;
2637 /* Read zebra header (if we don't have it already). */
2638 if ((already
= stream_get_endp(zclient
->ibuf
)) < ZEBRA_HEADER_SIZE
) {
2640 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2641 ZEBRA_HEADER_SIZE
- already
))
2646 "zclient connection closed socket [%d].",
2648 return zclient_failed(zclient
);
2650 if (nbyte
!= (ssize_t
)(ZEBRA_HEADER_SIZE
- already
)) {
2651 /* Try again later. */
2652 zclient_event(ZCLIENT_READ
, zclient
);
2655 already
= ZEBRA_HEADER_SIZE
;
2658 /* Reset to read from the beginning of the incoming packet. */
2659 stream_set_getp(zclient
->ibuf
, 0);
2661 /* Fetch header values. */
2662 length
= stream_getw(zclient
->ibuf
);
2663 marker
= stream_getc(zclient
->ibuf
);
2664 version
= stream_getc(zclient
->ibuf
);
2665 vrf_id
= stream_getl(zclient
->ibuf
);
2666 command
= stream_getw(zclient
->ibuf
);
2668 if (marker
!= ZEBRA_HEADER_MARKER
|| version
!= ZSERV_VERSION
) {
2670 EC_LIB_ZAPI_MISSMATCH
,
2671 "%s: socket %d version mismatch, marker %d, version %d",
2672 __func__
, zclient
->sock
, marker
, version
);
2673 return zclient_failed(zclient
);
2676 if (length
< ZEBRA_HEADER_SIZE
) {
2677 flog_err(EC_LIB_ZAPI_MISSMATCH
,
2678 "%s: socket %d message length %u is less than %d ",
2679 __func__
, zclient
->sock
, length
, ZEBRA_HEADER_SIZE
);
2680 return zclient_failed(zclient
);
2684 if (length
> STREAM_SIZE(zclient
->ibuf
)) {
2688 "%s: message size %u exceeds buffer size %lu, expanding...",
2690 (unsigned long)STREAM_SIZE(zclient
->ibuf
));
2691 ns
= stream_new(length
);
2692 stream_copy(ns
, zclient
->ibuf
);
2693 stream_free(zclient
->ibuf
);
2697 /* Read rest of zebra packet. */
2698 if (already
< length
) {
2700 if (((nbyte
= stream_read_try(zclient
->ibuf
, zclient
->sock
,
2706 "zclient connection closed socket [%d].",
2708 return zclient_failed(zclient
);
2710 if (nbyte
!= (ssize_t
)(length
- already
)) {
2711 /* Try again later. */
2712 zclient_event(ZCLIENT_READ
, zclient
);
2717 length
-= ZEBRA_HEADER_SIZE
;
2720 zlog_debug("zclient 0x%p command %s VRF %u",
2721 (void *)zclient
, zserv_command_string(command
),
2725 case ZEBRA_CAPABILITIES
:
2726 zclient_capability_decode(command
, zclient
, length
, vrf_id
);
2728 case ZEBRA_ROUTER_ID_UPDATE
:
2729 if (zclient
->router_id_update
)
2730 (*zclient
->router_id_update
)(command
, zclient
, length
,
2734 zclient_vrf_add(zclient
, vrf_id
);
2736 case ZEBRA_VRF_DELETE
:
2737 zclient_vrf_delete(zclient
, vrf_id
);
2739 case ZEBRA_INTERFACE_ADD
:
2740 if (zclient
->interface_add
)
2741 (*zclient
->interface_add
)(command
, zclient
, length
,
2744 case ZEBRA_INTERFACE_DELETE
:
2745 if (zclient
->interface_delete
)
2746 (*zclient
->interface_delete
)(command
, zclient
, length
,
2749 case ZEBRA_INTERFACE_ADDRESS_ADD
:
2750 if (zclient
->interface_address_add
)
2751 (*zclient
->interface_address_add
)(command
, zclient
,
2754 case ZEBRA_INTERFACE_ADDRESS_DELETE
:
2755 if (zclient
->interface_address_delete
)
2756 (*zclient
->interface_address_delete
)(command
, zclient
,
2759 case ZEBRA_INTERFACE_BFD_DEST_UPDATE
:
2760 if (zclient
->interface_bfd_dest_update
)
2761 (*zclient
->interface_bfd_dest_update
)(command
, zclient
,
2764 case ZEBRA_INTERFACE_NBR_ADDRESS_ADD
:
2765 if (zclient
->interface_nbr_address_add
)
2766 (*zclient
->interface_nbr_address_add
)(command
, zclient
,
2769 case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
:
2770 if (zclient
->interface_nbr_address_delete
)
2771 (*zclient
->interface_nbr_address_delete
)(
2772 command
, zclient
, length
, vrf_id
);
2774 case ZEBRA_INTERFACE_UP
:
2775 if (zclient
->interface_up
)
2776 (*zclient
->interface_up
)(command
, zclient
, length
,
2779 case ZEBRA_INTERFACE_DOWN
:
2780 if (zclient
->interface_down
)
2781 (*zclient
->interface_down
)(command
, zclient
, length
,
2784 case ZEBRA_INTERFACE_VRF_UPDATE
:
2785 if (zclient
->interface_vrf_update
)
2786 (*zclient
->interface_vrf_update
)(command
, zclient
,
2789 case ZEBRA_NEXTHOP_UPDATE
:
2791 zlog_debug("zclient rcvd nexthop update");
2792 if (zclient
->nexthop_update
)
2793 (*zclient
->nexthop_update
)(command
, zclient
, length
,
2796 case ZEBRA_IMPORT_CHECK_UPDATE
:
2798 zlog_debug("zclient rcvd import check update");
2799 if (zclient
->import_check_update
)
2800 (*zclient
->import_check_update
)(command
, zclient
,
2803 case ZEBRA_BFD_DEST_REPLAY
:
2804 if (zclient
->bfd_dest_replay
)
2805 (*zclient
->bfd_dest_replay
)(command
, zclient
, length
,
2808 case ZEBRA_REDISTRIBUTE_ROUTE_ADD
:
2809 if (zclient
->redistribute_route_add
)
2810 (*zclient
->redistribute_route_add
)(command
, zclient
,
2813 case ZEBRA_REDISTRIBUTE_ROUTE_DEL
:
2814 if (zclient
->redistribute_route_del
)
2815 (*zclient
->redistribute_route_del
)(command
, zclient
,
2818 case ZEBRA_INTERFACE_LINK_PARAMS
:
2819 if (zclient
->interface_link_params
)
2820 (*zclient
->interface_link_params
)(command
, zclient
,
2823 case ZEBRA_FEC_UPDATE
:
2825 zlog_debug("zclient rcvd fec update");
2826 if (zclient
->fec_update
)
2827 (*zclient
->fec_update
)(command
, zclient
, length
);
2829 case ZEBRA_LOCAL_ES_ADD
:
2830 if (zclient
->local_es_add
)
2831 (*zclient
->local_es_add
)(command
, zclient
, length
,
2834 case ZEBRA_LOCAL_ES_DEL
:
2835 if (zclient
->local_es_del
)
2836 (*zclient
->local_es_del
)(command
, zclient
, length
,
2840 if (zclient
->local_vni_add
)
2841 (*zclient
->local_vni_add
)(command
, zclient
, length
,
2845 if (zclient
->local_vni_del
)
2846 (*zclient
->local_vni_del
)(command
, zclient
, length
,
2849 case ZEBRA_L3VNI_ADD
:
2850 if (zclient
->local_l3vni_add
)
2851 (*zclient
->local_l3vni_add
)(command
, zclient
, length
,
2854 case ZEBRA_L3VNI_DEL
:
2855 if (zclient
->local_l3vni_del
)
2856 (*zclient
->local_l3vni_del
)(command
, zclient
, length
,
2859 case ZEBRA_MACIP_ADD
:
2860 if (zclient
->local_macip_add
)
2861 (*zclient
->local_macip_add
)(command
, zclient
, length
,
2864 case ZEBRA_MACIP_DEL
:
2865 if (zclient
->local_macip_del
)
2866 (*zclient
->local_macip_del
)(command
, zclient
, length
,
2869 case ZEBRA_IP_PREFIX_ROUTE_ADD
:
2870 if (zclient
->local_ip_prefix_add
)
2871 (*zclient
->local_ip_prefix_add
)(command
, zclient
,
2874 case ZEBRA_IP_PREFIX_ROUTE_DEL
:
2875 if (zclient
->local_ip_prefix_del
)
2876 (*zclient
->local_ip_prefix_del
)(command
, zclient
,
2879 case ZEBRA_PW_STATUS_UPDATE
:
2880 if (zclient
->pw_status_update
)
2881 (*zclient
->pw_status_update
)(command
, zclient
, length
,
2884 case ZEBRA_ROUTE_NOTIFY_OWNER
:
2885 if (zclient
->route_notify_owner
)
2886 (*zclient
->route_notify_owner
)(command
, zclient
, length
,
2889 case ZEBRA_RULE_NOTIFY_OWNER
:
2890 if (zclient
->rule_notify_owner
)
2891 (*zclient
->rule_notify_owner
)(command
, zclient
, length
,
2894 case ZEBRA_GET_LABEL_CHUNK
:
2895 if (zclient
->label_chunk
)
2896 (*zclient
->label_chunk
)(command
, zclient
, length
,
2899 case ZEBRA_IPSET_NOTIFY_OWNER
:
2900 if (zclient
->ipset_notify_owner
)
2901 (*zclient
->ipset_notify_owner
)(command
, zclient
, length
,
2904 case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
:
2905 if (zclient
->ipset_entry_notify_owner
)
2906 (*zclient
->ipset_entry_notify_owner
)(command
,
2910 case ZEBRA_IPTABLE_NOTIFY_OWNER
:
2911 if (zclient
->iptable_notify_owner
)
2912 (*zclient
->iptable_notify_owner
)(command
,
2916 case ZEBRA_VXLAN_SG_ADD
:
2917 if (zclient
->vxlan_sg_add
)
2918 (*zclient
->vxlan_sg_add
)(command
, zclient
, length
,
2921 case ZEBRA_VXLAN_SG_DEL
:
2922 if (zclient
->vxlan_sg_del
)
2923 (*zclient
->vxlan_sg_del
)(command
, zclient
, length
,
2930 if (zclient
->sock
< 0)
2931 /* Connection was closed during packet processing. */
2934 /* Register read thread. */
2935 stream_reset(zclient
->ibuf
);
2936 zclient_event(ZCLIENT_READ
, zclient
);
2941 void zclient_redistribute(int command
, struct zclient
*zclient
, afi_t afi
,
2942 int type
, unsigned short instance
, vrf_id_t vrf_id
)
2946 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2947 if (redist_check_instance(
2948 &zclient
->mi_redist
[afi
][type
], instance
))
2950 redist_add_instance(&zclient
->mi_redist
[afi
][type
],
2953 if (!redist_check_instance(
2954 &zclient
->mi_redist
[afi
][type
], instance
))
2956 redist_del_instance(&zclient
->mi_redist
[afi
][type
],
2961 if (command
== ZEBRA_REDISTRIBUTE_ADD
) {
2962 if (vrf_bitmap_check(zclient
->redist
[afi
][type
],
2965 vrf_bitmap_set(zclient
->redist
[afi
][type
], vrf_id
);
2967 if (!vrf_bitmap_check(zclient
->redist
[afi
][type
],
2970 vrf_bitmap_unset(zclient
->redist
[afi
][type
], vrf_id
);
2974 if (zclient
->sock
> 0)
2975 zebra_redistribute_send(command
, zclient
, afi
, type
, instance
,
2980 void zclient_redistribute_default(int command
, struct zclient
*zclient
,
2981 afi_t afi
, vrf_id_t vrf_id
)
2984 if (command
== ZEBRA_REDISTRIBUTE_DEFAULT_ADD
) {
2985 if (vrf_bitmap_check(zclient
->default_information
[afi
], vrf_id
))
2987 vrf_bitmap_set(zclient
->default_information
[afi
], vrf_id
);
2989 if (!vrf_bitmap_check(zclient
->default_information
[afi
],
2992 vrf_bitmap_unset(zclient
->default_information
[afi
], vrf_id
);
2995 if (zclient
->sock
> 0)
2996 zebra_redistribute_default_send(command
, zclient
, afi
, vrf_id
);
2999 static void zclient_event(enum event event
, struct zclient
*zclient
)
3002 case ZCLIENT_SCHEDULE
:
3003 thread_add_event(zclient
->master
, zclient_connect
, zclient
, 0,
3004 &zclient
->t_connect
);
3006 case ZCLIENT_CONNECT
:
3009 "zclient connect failures: %d schedule interval is now %d",
3010 zclient
->fail
, zclient
->fail
< 3 ? 10 : 60);
3011 thread_add_timer(zclient
->master
, zclient_connect
, zclient
,
3012 zclient
->fail
< 3 ? 10 : 60,
3013 &zclient
->t_connect
);
3016 zclient
->t_read
= NULL
;
3017 thread_add_read(zclient
->master
, zclient_read
, zclient
,
3018 zclient
->sock
, &zclient
->t_read
);
3023 void zclient_interface_set_master(struct zclient
*client
,
3024 struct interface
*master
,
3025 struct interface
*slave
)
3032 zclient_create_header(s
, ZEBRA_INTERFACE_SET_MASTER
, master
->vrf_id
);
3034 stream_putl(s
, master
->vrf_id
);
3035 stream_putl(s
, master
->ifindex
);
3036 stream_putl(s
, slave
->vrf_id
);
3037 stream_putl(s
, slave
->ifindex
);
3039 stream_putw_at(s
, 0, stream_get_endp(s
));
3040 zclient_send_message(client
);