- frr_each
- frr_each_safe
- frr_each_from
+ - frr_with_mutex
+ - frr_with_privs
- LIST_FOREACH
- LIST_FOREACH_SAFE
- SLIST_FOREACH
socket_close(&bvrf->bg_mhop);
socket_close(&bvrf->bg_shop6);
socket_close(&bvrf->bg_mhop6);
+ socket_close(&bvrf->bg_echo);
+ socket_close(&bvrf->bg_echov6);
/* free context */
XFREE(MTYPE_BFDD_VRF, bvrf);
{
int sd;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, vrf_id, NULL);
}
if (sd == -1)
{
int sd;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC, vrf_id, NULL);
}
if (sd == -1)
&& bs->key.vrfname[0])
device_to_bind = (const char *)bs->key.vrfname;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET, SOCK_DGRAM, PF_UNSPEC,
bs->vrf->vrf_id, device_to_bind);
}
&& bs->key.vrfname[0])
device_to_bind = (const char *)bs->key.vrfname;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC,
bs->vrf->vrf_id, device_to_bind);
}
{
int sd;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC, vrf_id, NULL);
}
if (sd == -1)
{
int sd;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
sd = vrf_socket(AF_INET6, SOCK_DGRAM, PF_UNSPEC, vrf_id, NULL);
}
if (sd == -1)
{
int s;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
s = vrf_socket(AF_INET, SOCK_DGRAM, 0, vrf_id, NULL);
}
if (s == -1)
{
int s;
- frr_elevate_privs(&bglobal.bfdd_privs) {
+ frr_with_privs(&bglobal.bfdd_privs) {
s = vrf_socket(AF_INET6, SOCK_DGRAM, 0, vrf_id, NULL);
}
if (s == -1)
* on various buffers. Those need to be transferred or dropped,
* otherwise we'll get spurious failures during session establishment.
*/
- pthread_mutex_lock(&peer->io_mtx);
- pthread_mutex_lock(&from_peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx, &from_peer->io_mtx) {
fd = peer->fd;
peer->fd = from_peer->fd;
from_peer->fd = fd;
ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work,
ringbuf_remain(from_peer->ibuf_work));
}
- pthread_mutex_unlock(&from_peer->io_mtx);
- pthread_mutex_unlock(&peer->io_mtx);
peer->as = from_peer->as;
peer->v_holdtime = from_peer->v_holdtime;
BGP_TIMER_OFF(peer->t_routeadv);
/* Clear input and output buffer. */
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
if (peer->ibuf)
stream_fifo_clean(peer->ibuf);
if (peer->obuf)
peer->curr = NULL;
}
}
- pthread_mutex_unlock(&peer->io_mtx);
/* Close of file descriptor. */
if (peer->fd >= 0) {
struct frr_pthread *fpt = bgp_pth_io;
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
status = bgp_write(peer);
reschedule = (stream_fifo_head(peer->obuf) != NULL);
}
- pthread_mutex_unlock(&peer->io_mtx);
/* no problem */
if (CHECK_FLAG(status, BGP_IO_TRANS_ERR)) {
struct frr_pthread *fpt = bgp_pth_io;
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
status = bgp_read(peer);
}
- pthread_mutex_unlock(&peer->io_mtx);
/* error checking phase */
if (CHECK_FLAG(status, BGP_IO_TRANS_ERR)) {
assert(ringbuf_get(ibw, pktbuf, pktsize) == pktsize);
stream_put(pkt, pktbuf, pktsize);
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
stream_fifo_push(peer->ibuf, pkt);
}
- pthread_mutex_unlock(&peer->io_mtx);
added_pkt = true;
} else
*/
assert(peerhash_mtx);
- pthread_mutex_lock(peerhash_mtx);
- {
+ frr_with_mutex(peerhash_mtx) {
holder.peer = peer;
if (!hash_lookup(peerhash, &holder)) {
struct pkat *pkat = pkat_new(peer);
}
SET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
}
- pthread_mutex_unlock(peerhash_mtx);
bgp_keepalives_wake();
}
*/
assert(peerhash_mtx);
- pthread_mutex_lock(peerhash_mtx);
- {
+ frr_with_mutex(peerhash_mtx) {
holder.peer = peer;
struct pkat *res = hash_release(peerhash, &holder);
if (res) {
}
UNSET_FLAG(peer->thread_flags, PEER_THREAD_KEEPALIVES_ON);
}
- pthread_mutex_unlock(peerhash_mtx);
}
void bgp_keepalives_wake(void)
{
- pthread_mutex_lock(peerhash_mtx);
- {
+ frr_with_mutex(peerhash_mtx) {
pthread_cond_signal(peerhash_cond);
}
- pthread_mutex_unlock(peerhash_mtx);
}
int bgp_keepalives_stop(struct frr_pthread *fpt, void **result)
int ret = -1;
#if HAVE_DECL_TCP_MD5SIG
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
ret = bgp_md5_set_socket(socket, su, prefixlen, password);
}
#endif /* HAVE_TCP_MD5SIG */
* Set or unset the password on the listen socket(s). Outbound
* connections are taken care of in bgp_connect() below.
*/
- frr_elevate_privs(&bgpd_privs)
- {
+ frr_with_privs(&bgpd_privs) {
for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
if (listener->su.sa.sa_family
== peer->su.sa.sa_family) {
struct bgp_listener *listener;
/* Set or unset the password on the listen socket(s). */
- frr_elevate_privs(&bgpd_privs)
- {
+ frr_with_privs(&bgpd_privs) {
for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, listener))
if (listener->su.sa.sa_family == p->family) {
prefix2sockunion(p, &su);
zlog_debug("Peer address not learnt: Returning from connect");
return 0;
}
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
/* Make socket for the peer. */
peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
bgp_get_bound_name(peer));
sockopt_reuseport(peer->fd);
#ifdef IPTOS_PREC_INTERNETCONTROL
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
if (sockunion_family(&peer->su) == AF_INET)
setsockopt_ipv4_tos(peer->fd,
IPTOS_PREC_INTERNETCONTROL);
sockopt_reuseaddr(sock);
sockopt_reuseport(sock);
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
#ifdef IPTOS_PREC_INTERNETCONTROL
if (sa->sa_family == AF_INET)
snprintf(port_str, sizeof(port_str), "%d", port);
port_str[sizeof(port_str) - 1] = '\0';
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
bgp->vrf_id);
}
if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
continue;
- frr_elevate_privs(&bgpd_privs) {
+ frr_with_privs(&bgpd_privs) {
sock = vrf_socket(ainfo->ai_family,
ainfo->ai_socktype,
ainfo->ai_protocol, bgp->vrf_id,
*/
static void bgp_packet_add(struct peer *peer, struct stream *s)
{
- pthread_mutex_lock(&peer->io_mtx);
- stream_fifo_push(peer->obuf, s);
- pthread_mutex_unlock(&peer->io_mtx);
+ frr_with_mutex(&peer->io_mtx) {
+ stream_fifo_push(peer->obuf, s);
+ }
}
static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
struct stream *s;
/* Lock I/O mutex to prevent other threads from pushing packets */
- pthread_mutex_lock(&peer->io_mtx);
+ frr_mutex_lock_autounlock(&peer->io_mtx);
/* ============================================== */
/* Allocate new stream. */
stream_fifo_push(peer->obuf, s);
bgp_write_notify(peer);
-
- /* ============================================== */
- pthread_mutex_unlock(&peer->io_mtx);
}
/*
bgp_size_t size;
char notify_data_length[2];
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
peer->curr = stream_fifo_pop(peer->ibuf);
}
- pthread_mutex_unlock(&peer->io_mtx);
if (peer->curr == NULL) // no packets to process, hmm...
return 0;
if (fsm_update_result != FSM_PEER_TRANSFERRED
&& fsm_update_result != FSM_PEER_STOPPED) {
- pthread_mutex_lock(&peer->io_mtx);
- {
+ frr_with_mutex(&peer->io_mtx) {
// more work to do, come back later
if (peer->ibuf->count > 0)
thread_add_timer_msec(
bm->master, bgp_process_packet, peer, 0,
&peer->t_process_packet);
}
- pthread_mutex_unlock(&peer->io_mtx);
}
return 0;
* since this peer is not on the I/O thread, this lock is not strictly
* necessary, but serves as a reminder to those who may meddle...
*/
- pthread_mutex_lock(&rfd->peer->io_mtx);
- {
+ frr_with_mutex(&rfd->peer->io_mtx) {
// we don't need any I/O related facilities
if (rfd->peer->ibuf)
stream_fifo_free(rfd->peer->ibuf);
rfd->peer->obuf_work = NULL;
rfd->peer->ibuf_work = NULL;
}
- pthread_mutex_unlock(&rfd->peer->io_mtx);
{ /* base code assumes have valid host pointer */
char buf[BUFSIZ];
* is not strictly necessary, but serves as a reminder
* to those who may meddle...
*/
- pthread_mutex_lock(&vncHD1VR.peer->io_mtx);
- {
+ frr_with_mutex(&vncHD1VR.peer->io_mtx) {
// we don't need any I/O related facilities
if (vncHD1VR.peer->ibuf)
stream_fifo_free(vncHD1VR.peer->ibuf);
vncHD1VR.peer->obuf_work = NULL;
vncHD1VR.peer->ibuf_work = NULL;
}
- pthread_mutex_unlock(&vncHD1VR.peer->io_mtx);
/* base code assumes have valid host pointer */
vncHD1VR.peer->host =
-o x"${frr_ac_lcaps}" = x"yes"; then
AC_DEFINE([HAVE_CAPABILITIES], [1], [capabilities])
fi
+
+ case "$host_os" in
+ linux*)
+ if test "$frr_ac_lcaps" != "yes"; then
+ AC_MSG_ERROR([libcap and/or its headers were not found. Running FRR without libcap support built in causes a huge performance penalty.])
+ fi
+ ;;
+ esac
+else
+ case "$host_os" in
+ linux*)
+ AC_MSG_WARN([Running FRR without libcap support built in causes a huge performance penalty.])
+ ;;
+ esac
fi
AC_SUBST([LIBCAP])
rcu
lists
logging
+ locking
hooks
cli
modules
--- /dev/null
+Locking
+=======
+
+FRR ships two small wrappers around ``pthread_mutex_lock()`` /
+``pthread_mutex_unlock``. Use ``#include "frr_pthread.h"`` to get these
+macros.
+
+.. c:function:: frr_with_mutex(pthread_mutex_t *mutex)
+
+ Begin a C statement block that is executed with the mutex locked. Any
+ exit from the block (``break``, ``return``, ``goto``, end of block) will
+ cause the mutex to be unlocked::
+
+ int somefunction(int option)
+ {
+ frr_with_mutex(&my_mutex) {
+ /* mutex will be locked */
+
+ if (!option)
+ /* mutex will be unlocked before return */
+ return -1;
+
+ if (something(option))
+ /* mutex will be unlocked before goto */
+ goto out_err;
+
+ somethingelse();
+
+ /* mutex will be unlocked at end of block */
+ }
+
+ return 0;
+
+ out_err:
+ somecleanup();
+ return -1;
+ }
+
+ This is a macro that internally uses a ``for`` loop. It is explicitly
+ acceptable to use ``break`` to get out of the block. Even though a single
+ statement works correctly, FRR coding style requires that this macro always
+ be used with a ``{ ... }`` block.
+
+.. c:function:: frr_mutex_lock_autounlock(pthread_mutex_t *mutex)
+
+ Lock mutex and unlock at the end of the current C statement block::
+
+ int somefunction(int option)
+ {
+ frr_mutex_lock_autounlock(&my_mutex);
+ /* mutex will be locked */
+
+ ...
+ if (error)
+ /* mutex will be unlocked before return */
+ return -1;
+ ...
+
+ /* mutex will be unlocked before return */
+ return 0;
+ }
+
+ This is a macro that internally creates a variable with a destructor.
+ When the variable goes out of scope (i.e. the block ends), the mutex is
+ released.
+
+ .. warning::
+
+ This macro should only used when :c:func:`frr_with_mutex` would
+ result in excessively/weirdly nested code. This generally is an
+ indicator that the code might be trying to do too many things with
+ the lock held. Try any possible venues to reduce the amount of
+ code covered by the lock and move to :c:func:`frr_with_mutex`.
doc/developer/index.rst \
doc/developer/library.rst \
doc/developer/lists.rst \
+ doc/developer/locking.rst \
doc/developer/logging.rst \
doc/developer/maintainer-release-build.rst \
doc/developer/memtypes.rst \
ability to turn on/off debugs from the CLI and it is expected that the
developer will use this convention to allow control of their debugs.
+Custom syntax-like block macros
+-------------------------------
+
+FRR uses some macros that behave like the ``for`` or ``if`` C keywords. These
+macros follow these patterns:
+
+- loop-style macros are named ``frr_each_*`` (and ``frr_each``)
+- single run macros are named ``frr_with_*``
+- to avoid confusion, ``frr_with_*`` macros must always use a ``{ ... }``
+ block even if the block only contains one statement. The ``frr_each``
+ constructs are assumed to be well-known enough to use normal ``for`` rules.
+- ``break``, ``return`` and ``goto`` all work correctly. For loop-style
+ macros, ``continue`` works correctly too.
+
+Both the ``each`` and ``with`` keywords are inspired by other (more
+higher-level) programming languages that provide these constructs.
+
+There are also some older iteration macros, e.g. ``ALL_LIST_ELEMENTS`` and
+``FOREACH_AFI_SAFI``. These macros in some cases do **not** fulfill the above
+pattern (e.g. ``break`` does not work in ``FOREACH_AFI_SAFI`` because it
+expands to 2 nested loops.)
+
Static Analysis and Sanitizers
------------------------------
Clang/LLVM and GCC come with a variety of tools that can be used to help find
int hincl = 1;
#endif
- frr_elevate_privs(&eigrpd_privs) {
+ frr_with_privs(&eigrpd_privs) {
eigrp_sock = vrf_socket(
AF_INET, SOCK_RAW, IPPROTO_EIGRPIGP, vrf->vrf_id,
vrf->vrf_id != VRF_DEFAULT ? vrf->name : NULL);
{
int retval = ISIS_OK;
- frr_elevate_privs(&isisd_privs) {
+ frr_with_privs(&isisd_privs) {
retval = open_bpf_dev(circuit);
{
int retval = ISIS_OK;
- frr_elevate_privs(&isisd_privs) {
+ frr_with_privs(&isisd_privs) {
retval = open_dlpi_dev(circuit);
{
int retval = ISIS_OK;
- frr_elevate_privs(&isisd_privs) {
+ frr_with_privs(&isisd_privs) {
retval = open_packet_socket(circuit);
sock_set_bindany(fd, 1);
break;
}
- frr_elevate_privs(&ldpd_privs) {
+ frr_with_privs(&ldpd_privs) {
if (sock_set_reuse(fd, 1) == -1) {
close(fd);
return (-1);
sock_set_bindany(int fd, int enable)
{
#ifdef HAVE_SO_BINDANY
- frr_elevate_privs(&ldpd_privs) {
+ frr_with_privs(&ldpd_privs) {
if (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &enable,
sizeof(int)) < 0) {
log_warn("%s: error setting SO_BINDANY", __func__);
}
return (0);
#elif defined(IP_BINDANY)
- frr_elevate_privs(&ldpd_privs) {
+ frr_with_privs(&ldpd_privs) {
if (setsockopt(fd, IPPROTO_IP, IP_BINDANY, &enable, sizeof(int))
< 0) {
log_warn("%s: error setting IP_BINDANY", __func__);
#if HAVE_DECL_TCP_MD5SIG
addr2sa(af, addr, 0, &su);
- frr_elevate_privs(&ldpe_privs) {
+ frr_with_privs(&ldpe_privs) {
ret = sockopt_tcp_signature(fd, &su, password);
save_errno = errno;
}
#define macro_inline static inline __attribute__((unused))
#define macro_pure static inline __attribute__((unused, pure))
+
+/* variadic macros, use like:
+ * #define V_0() ...
+ * #define V_1(x) ...
+ * #define V(...) MACRO_VARIANT(V, ##__VA_ARGS__)(__VA_ARGS__)
+ */
+#define _MACRO_VARIANT(A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10, N, ...) N
+
+#define _CONCAT2(a, b) a ## b
+#define _CONCAT(a, b) _CONCAT2(a,b)
+
+#define MACRO_VARIANT(NAME, ...) \
+ _CONCAT(NAME, _MACRO_VARIANT(0, ##__VA_ARGS__, \
+ _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0))
+
+#define NAMECTR(name) _CONCAT(name, __COUNTER__)
+
+/* per-arg repeat macros, use like:
+ * #define PERARG(n) ...n...
+ * #define FOO(...) MACRO_REPEAT(PERARG, ##__VA_ARGS__)
+ */
+
+#define _MACRO_REPEAT_0(NAME)
+#define _MACRO_REPEAT_1(NAME, A1) \
+ NAME(A1)
+#define _MACRO_REPEAT_2(NAME, A1, A2) \
+ NAME(A1) NAME(A2)
+#define _MACRO_REPEAT_3(NAME, A1, A2, A3) \
+ NAME(A1) NAME(A2) NAME(A3)
+#define _MACRO_REPEAT_4(NAME, A1, A2, A3, A4) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4)
+#define _MACRO_REPEAT_5(NAME, A1, A2, A3, A4, A5) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5)
+#define _MACRO_REPEAT_6(NAME, A1, A2, A3, A4, A5, A6) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6)
+#define _MACRO_REPEAT_7(NAME, A1, A2, A3, A4, A5, A6, A7) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7)
+#define _MACRO_REPEAT_8(NAME, A1, A2, A3, A4, A5, A6, A7, A8) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7) NAME(A8)
+
+#define MACRO_REPEAT(NAME, ...) \
+ MACRO_VARIANT(_MACRO_REPEAT, ##__VA_ARGS__)(NAME, ##__VA_ARGS__)
+
/*
* for warnings on macros, put in the macro content like this:
* #define MACRO BLA CPP_WARN("MACRO has been deprecated")
#include "command.h"
#include "json.h"
#include "linklist.h"
+#include "frr_pthread.h"
DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
{
uint32_t i = 0;
- pthread_mutex_lock(&refs_mtx);
- {
+ frr_with_mutex(&refs_mtx) {
while (ref[i].code != END_FERR) {
hash_get(refs, &ref[i], hash_alloc_intern);
i++;
}
}
- pthread_mutex_unlock(&refs_mtx);
}
struct log_ref *log_ref_get(uint32_t code)
struct log_ref *ref;
holder.code = code;
- pthread_mutex_lock(&refs_mtx);
- {
+ frr_with_mutex(&refs_mtx) {
ref = hash_lookup(refs, &holder);
}
- pthread_mutex_unlock(&refs_mtx);
return ref;
}
if (json)
top = json_object_new_object();
- pthread_mutex_lock(&refs_mtx);
- {
+ frr_with_mutex(&refs_mtx) {
errlist = code ? list_new() : hash_to_list(refs);
}
- pthread_mutex_unlock(&refs_mtx);
if (code) {
ref = log_ref_get(code);
void log_ref_init(void)
{
- pthread_mutex_lock(&refs_mtx);
- {
+ frr_with_mutex(&refs_mtx) {
refs = hash_create(ferr_hash_key, ferr_hash_cmp,
"Error Reference Texts");
}
- pthread_mutex_unlock(&refs_mtx);
}
void log_ref_fini(void)
{
- pthread_mutex_lock(&refs_mtx);
- {
+ frr_with_mutex(&refs_mtx) {
hash_clean(refs, NULL);
hash_free(refs);
refs = NULL;
}
- pthread_mutex_unlock(&refs_mtx);
}
void log_ref_vty_init(void)
void frr_pthread_init(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
frr_pthread_list = list_new();
frr_pthread_list->del = (void (*)(void *))&frr_pthread_destroy;
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
void frr_pthread_finish(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
list_delete(&frr_pthread_list);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
struct frr_pthread *frr_pthread_new(struct frr_pthread_attr *attr,
pthread_mutex_init(fpt->running_cond_mtx, NULL);
pthread_cond_init(fpt->running_cond, NULL);
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
listnode_add(frr_pthread_list, fpt);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
return fpt;
}
void frr_pthread_wait_running(struct frr_pthread *fpt)
{
- pthread_mutex_lock(fpt->running_cond_mtx);
- {
+ frr_with_mutex(fpt->running_cond_mtx) {
while (!fpt->running)
pthread_cond_wait(fpt->running_cond,
fpt->running_cond_mtx);
}
- pthread_mutex_unlock(fpt->running_cond_mtx);
}
void frr_pthread_notify_running(struct frr_pthread *fpt)
{
- pthread_mutex_lock(fpt->running_cond_mtx);
- {
+ frr_with_mutex(fpt->running_cond_mtx) {
fpt->running = true;
pthread_cond_signal(fpt->running_cond);
}
- pthread_mutex_unlock(fpt->running_cond_mtx);
}
int frr_pthread_stop(struct frr_pthread *fpt, void **result)
void frr_pthread_stop_all(void)
{
- pthread_mutex_lock(&frr_pthread_list_mtx);
- {
+ frr_with_mutex(&frr_pthread_list_mtx) {
struct listnode *n;
struct frr_pthread *fpt;
for (ALL_LIST_ELEMENTS_RO(frr_pthread_list, n, fpt))
frr_pthread_stop(fpt, NULL);
}
- pthread_mutex_unlock(&frr_pthread_list_mtx);
}
/*
#define pthread_condattr_setclock(A, B)
#endif
+/* mutex auto-lock/unlock */
+
+/* variant 1:
+ * (for short blocks, multiple mutexes supported)
+ * break & return can be used for aborting the block
+ *
+ * frr_with_mutex(&mtx, &mtx2) {
+ * if (error)
+ * break;
+ * ...
+ * }
+ */
+#define _frr_with_mutex(mutex) \
+ *NAMECTR(_mtx_) __attribute__(( \
+ unused, cleanup(_frr_mtx_unlock))) = _frr_mtx_lock(mutex), \
+ /* end */
+
+#define frr_with_mutex(...) \
+ for (pthread_mutex_t MACRO_REPEAT(_frr_with_mutex, ##__VA_ARGS__) \
+ *_once = NULL; _once == NULL; _once = (void *)1) \
+ /* end */
+
+/* variant 2:
+ * (more suitable for long blocks, no extra indentation)
+ *
+ * frr_mutex_lock_autounlock(&mtx);
+ * ...
+ */
+#define frr_mutex_lock_autounlock(mutex) \
+ pthread_mutex_t *NAMECTR(_mtx_) \
+ __attribute__((unused, cleanup(_frr_mtx_unlock))) = \
+ _frr_mtx_lock(mutex) \
+ /* end */
+
+static inline pthread_mutex_t *_frr_mtx_lock(pthread_mutex_t *mutex)
+{
+ pthread_mutex_lock(mutex);
+ return mutex;
+}
+
+static inline void _frr_mtx_unlock(pthread_mutex_t **mutex)
+{
+ if (!*mutex)
+ return;
+ pthread_mutex_unlock(*mutex);
+ *mutex = NULL;
+}
+
#ifdef __cplusplus
}
#endif
#include "vty.h"
#include "command.h"
#include "libfrr.h"
+#include "frr_pthread.h"
DEFINE_MTYPE_STATIC(LIB, HASH, "Hash")
DEFINE_MTYPE_STATIC(LIB, HASH_BACKET, "Hash Bucket")
hash->name = name ? XSTRDUP(MTYPE_HASH, name) : NULL;
hash->stats.empty = hash->size;
- pthread_mutex_lock(&_hashes_mtx);
- {
+ frr_with_mutex(&_hashes_mtx) {
if (!_hashes)
_hashes = list_new();
listnode_add(_hashes, hash);
}
- pthread_mutex_unlock(&_hashes_mtx);
return hash;
}
void hash_free(struct hash *hash)
{
- pthread_mutex_lock(&_hashes_mtx);
- {
+ frr_with_mutex(&_hashes_mtx) {
if (_hashes) {
listnode_delete(_hashes, hash);
if (_hashes->count == 0) {
}
}
}
- pthread_mutex_unlock(&_hashes_mtx);
XFREE(MTYPE_HASH, hash->name);
#include "lib_errors.h"
#include "lib/hook.h"
#include "printfrr.h"
+#include "frr_pthread.h"
#ifndef SUNOS_5
#include <sys/un.h>
void zlog_filter_clear(void)
{
- pthread_mutex_lock(&loglock);
- zlog_filter_count = 0;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_filter_count = 0;
+ }
}
int zlog_filter_add(const char *filter)
{
- pthread_mutex_lock(&loglock);
+ frr_with_mutex(&loglock) {
+ if (zlog_filter_count >= ZLOG_FILTERS_MAX)
+ return 1;
- int ret = 0;
+ if (zlog_filter_lookup(filter) != -1)
+ /* Filter already present */
+ return -1;
- if (zlog_filter_count >= ZLOG_FILTERS_MAX) {
- ret = 1;
- goto done;
- }
+ strlcpy(zlog_filters[zlog_filter_count], filter,
+ sizeof(zlog_filters[0]));
- if (zlog_filter_lookup(filter) != -1) {
- /* Filter already present */
- ret = -1;
- goto done;
- }
+ if (zlog_filters[zlog_filter_count][0] == '\0')
+ /* Filter was either empty or didn't get copied
+ * correctly
+ */
+ return -1;
- strlcpy(zlog_filters[zlog_filter_count], filter,
- sizeof(zlog_filters[0]));
-
- if (zlog_filters[zlog_filter_count][0] == '\0') {
- /* Filter was either empty or didn't get copied correctly */
- ret = -1;
- goto done;
+ zlog_filter_count++;
}
-
- zlog_filter_count++;
-
-done:
- pthread_mutex_unlock(&loglock);
- return ret;
+ return 0;
}
int zlog_filter_del(const char *filter)
{
- pthread_mutex_lock(&loglock);
-
- int found_idx = zlog_filter_lookup(filter);
- int last_idx = zlog_filter_count - 1;
- int ret = 0;
+ frr_with_mutex(&loglock) {
+ int found_idx = zlog_filter_lookup(filter);
+ int last_idx = zlog_filter_count - 1;
- if (found_idx == -1) {
- /* Didn't find the filter to delete */
- ret = -1;
- goto done;
- }
-
- /* Adjust the filter array */
- memmove(zlog_filters[found_idx], zlog_filters[found_idx + 1],
- (last_idx - found_idx) * sizeof(zlog_filters[0]));
+ if (found_idx == -1)
+ /* Didn't find the filter to delete */
+ return -1;
- zlog_filter_count--;
+ /* Adjust the filter array */
+ memmove(zlog_filters[found_idx], zlog_filters[found_idx + 1],
+ (last_idx - found_idx) * sizeof(zlog_filters[0]));
-done:
- pthread_mutex_unlock(&loglock);
- return ret;
+ zlog_filter_count--;
+ }
+ return 0;
}
/* Dump all filters to buffer, delimited by new line */
int zlog_filter_dump(char *buf, size_t max_size)
{
- pthread_mutex_lock(&loglock);
-
- int ret = 0;
int len = 0;
- for (int i = 0; i < zlog_filter_count; i++) {
- ret = snprintf(buf + len, max_size - len, " %s\n",
- zlog_filters[i]);
- len += ret;
- if ((ret < 0) || ((size_t)len >= max_size)) {
- len = -1;
- goto done;
+ frr_with_mutex(&loglock) {
+ for (int i = 0; i < zlog_filter_count; i++) {
+ int ret;
+ ret = snprintf(buf + len, max_size - len, " %s\n",
+ zlog_filters[i]);
+ len += ret;
+ if ((ret < 0) || ((size_t)len >= max_size))
+ return -1;
}
}
-done:
- pthread_mutex_unlock(&loglock);
return len;
}
/* va_list version of zlog. */
void vzlog(int priority, const char *format, va_list args)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
char proto_str[32] = "";
int original_errno = errno;
if (msg != buf)
XFREE(MTYPE_TMP, msg);
errno = original_errno;
- pthread_mutex_unlock(&loglock);
}
int vzlog_test(int priority)
{
- pthread_mutex_lock(&loglock);
-
- int ret = 0;
+ frr_mutex_lock_autounlock(&loglock);
struct zlog *zl = zlog_default;
/* When zlog_default is also NULL, use stderr for logging. */
if (zl == NULL)
- ret = 1;
+ return 1;
/* Syslog output */
else if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
- ret = 1;
+ return 1;
/* File output. */
else if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
- ret = 1;
+ return 1;
/* stdout output. */
else if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
- ret = 1;
+ return 1;
/* Terminal monitor. */
else if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
- ret = 1;
-
- pthread_mutex_unlock(&loglock);
+ return 1;
- return ret;
+ return 0;
}
/*
openlog(progname, syslog_flags, zl->facility);
- pthread_mutex_lock(&loglock);
- zlog_default = zl;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_default = zl;
+ }
#ifdef HAVE_GLIBC_BACKTRACE
/* work around backtrace() using lazily resolved dynamically linked
void closezlog(void)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
+
struct zlog *zl = zlog_default;
closelog();
XFREE(MTYPE_ZLOG, zl);
zlog_default = NULL;
- pthread_mutex_unlock(&loglock);
}
/* Called from command.c. */
void zlog_set_level(zlog_dest_t dest, int log_level)
{
- pthread_mutex_lock(&loglock);
- zlog_default->maxlvl[dest] = log_level;
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zlog_default->maxlvl[dest] = log_level;
+ }
}
int zlog_set_file(const char *filename, int log_level)
if (fp == NULL) {
ret = 0;
} else {
- pthread_mutex_lock(&loglock);
- zl = zlog_default;
-
- /* Set flags. */
- zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
- zl->maxlvl[ZLOG_DEST_FILE] = log_level;
- zl->fp = fp;
- logfile_fd = fileno(fp);
- pthread_mutex_unlock(&loglock);
+ frr_with_mutex(&loglock) {
+ zl = zlog_default;
+
+ /* Set flags. */
+ zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
+ zl->maxlvl[ZLOG_DEST_FILE] = log_level;
+ zl->fp = fp;
+ logfile_fd = fileno(fp);
+ }
}
return ret;
/* Reset opend file. */
int zlog_reset_file(void)
{
- pthread_mutex_lock(&loglock);
+ frr_mutex_lock_autounlock(&loglock);
struct zlog *zl = zlog_default;
XFREE(MTYPE_ZLOG, zl->filename);
zl->filename = NULL;
- pthread_mutex_unlock(&loglock);
-
return 1;
}
#include "command.h"
#include "debug.h"
#include "db.h"
+#include "frr_pthread.h"
#include "northbound.h"
#include "northbound_cli.h"
#include "northbound_db.h"
{
int ret = -1;
- pthread_mutex_lock(&running_config_mgmt_lock.mtx);
- {
+ frr_with_mutex(&running_config_mgmt_lock.mtx) {
if (!running_config_mgmt_lock.locked) {
running_config_mgmt_lock.locked = true;
running_config_mgmt_lock.owner_client = client;
ret = 0;
}
}
- pthread_mutex_unlock(&running_config_mgmt_lock.mtx);
return ret;
}
{
int ret = -1;
- pthread_mutex_lock(&running_config_mgmt_lock.mtx);
- {
+ frr_with_mutex(&running_config_mgmt_lock.mtx) {
if (running_config_mgmt_lock.locked
&& running_config_mgmt_lock.owner_client == client
&& running_config_mgmt_lock.owner_user == user) {
ret = 0;
}
}
- pthread_mutex_unlock(&running_config_mgmt_lock.mtx);
return ret;
}
{
int ret = -1;
- pthread_mutex_lock(&running_config_mgmt_lock.mtx);
- {
+ frr_with_mutex(&running_config_mgmt_lock.mtx) {
if (!running_config_mgmt_lock.locked
|| (running_config_mgmt_lock.owner_client == client
&& running_config_mgmt_lock.owner_user == user))
ret = 0;
}
- pthread_mutex_unlock(&running_config_mgmt_lock.mtx);
return ret;
}
#include "log.h"
#include "privs.h"
#include "memory.h"
+#include "frr_pthread.h"
#include "lib_errors.h"
#include "lib/queue.h"
* Serialize 'raise' operations; particularly important for
* OSes where privs are process-wide.
*/
- pthread_mutex_lock(&(privs->mutex));
- {
+ frr_with_mutex(&(privs->mutex)) {
/* Locate ref-counting object to use */
refs = get_privs_refs(privs);
refs->raised_in_funcname = funcname;
}
}
- pthread_mutex_unlock(&(privs->mutex));
return privs;
}
/* Serialize 'lower privs' operation - particularly important
* when OS privs are process-wide.
*/
- pthread_mutex_lock(&(*privs)->mutex);
- {
+ frr_with_mutex(&(*privs)->mutex) {
refs = get_privs_refs(*privs);
if (--(refs->refcount) == 0) {
refs->raised_in_funcname = NULL;
}
}
- pthread_mutex_unlock(&(*privs)->mutex);
*privs = NULL;
}
/*
* Wrapper around zprivs, to be used as:
- * frr_elevate_privs(&privs) {
+ * frr_with_privs(&privs) {
* ... code ...
* if (error)
* break; -- break can be used to get out of the block
* ... code ...
* }
*
- * The argument to frr_elevate_privs() can be NULL to leave privileges as-is
+ * The argument to frr_with_privs() can be NULL to leave privileges as-is
* (mostly useful for conditional privilege-raising, i.e.:)
- * frr_elevate_privs(cond ? &privs : NULL) {}
+ * frr_with_privs(cond ? &privs : NULL) {}
*
* NB: The code block is always executed, regardless of whether privileges
* could be raised or not, or whether NULL was given or not. This is fully
const char *funcname);
extern void _zprivs_lower(struct zebra_privs_t **privs);
-#define frr_elevate_privs(privs) \
+#define frr_with_privs(privs) \
for (struct zebra_privs_t *_once = NULL, \
*_privs __attribute__( \
(unused, cleanup(_zprivs_lower))) = \
#include "network.h"
#include "prefix.h"
#include "log.h"
+#include "frr_pthread.h"
#include "lib_errors.h"
DEFINE_MTYPE_STATIC(LIB, STREAM, "Stream")
void stream_fifo_push_safe(struct stream_fifo *fifo, struct stream *s)
{
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
stream_fifo_push(fifo, s);
}
- pthread_mutex_unlock(&fifo->mtx);
}
/* Delete first stream from fifo. */
{
struct stream *ret;
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
ret = stream_fifo_pop(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
return ret;
}
{
struct stream *ret;
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
ret = stream_fifo_head(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
return ret;
}
void stream_fifo_clean_safe(struct stream_fifo *fifo)
{
- pthread_mutex_lock(&fifo->mtx);
- {
+ frr_with_mutex(&fifo->mtx) {
stream_fifo_clean(fifo);
}
- pthread_mutex_unlock(&fifo->mtx);
}
size_t stream_fifo_count_safe(struct stream_fifo *fifo)
#include "network.h"
#include "jhash.h"
#include "frratomic.h"
+#include "frr_pthread.h"
#include "lib_errors.h"
DEFINE_MTYPE_STATIC(LIB, THREAD, "Thread")
tmp.funcname = "TOTAL";
tmp.types = filter;
- pthread_mutex_lock(&masters_mtx);
- {
+ frr_with_mutex(&masters_mtx) {
for (ALL_LIST_ELEMENTS_RO(masters, ln, m)) {
const char *name = m->name ? m->name : "main";
vty_out(vty, "\n");
}
}
- pthread_mutex_unlock(&masters_mtx);
vty_out(vty, "\n");
vty_out(vty, "Total thread statistics\n");
struct thread_master *m;
struct listnode *ln;
- pthread_mutex_lock(&masters_mtx);
- {
+ frr_with_mutex(&masters_mtx) {
for (ALL_LIST_ELEMENTS_RO(masters, ln, m)) {
- pthread_mutex_lock(&m->mtx);
- {
+ frr_with_mutex(&m->mtx) {
void *args[2] = {tmp, m->cpu_record};
hash_iterate(
m->cpu_record,
void *))cpu_record_hash_clear,
args);
}
- pthread_mutex_unlock(&m->mtx);
}
}
- pthread_mutex_unlock(&masters_mtx);
}
static uint8_t parse_filter(const char *filterstr)
struct listnode *node;
struct thread_master *m;
- pthread_mutex_lock(&masters_mtx);
- {
+ frr_with_mutex(&masters_mtx) {
for (ALL_LIST_ELEMENTS_RO(masters, node, m)) {
show_thread_poll_helper(vty, m);
}
}
- pthread_mutex_unlock(&masters_mtx);
return CMD_SUCCESS;
}
sizeof(struct pollfd) * rv->handler.pfdsize);
/* add to list of threadmasters */
- pthread_mutex_lock(&masters_mtx);
- {
+ frr_with_mutex(&masters_mtx) {
if (!masters)
masters = list_new();
listnode_add(masters, rv);
}
- pthread_mutex_unlock(&masters_mtx);
return rv;
}
void thread_master_set_name(struct thread_master *master, const char *name)
{
- pthread_mutex_lock(&master->mtx);
- {
+ frr_with_mutex(&master->mtx) {
XFREE(MTYPE_THREAD_MASTER, master->name);
master->name = XSTRDUP(MTYPE_THREAD_MASTER, name);
}
- pthread_mutex_unlock(&master->mtx);
}
#define THREAD_UNUSED_DEPTH 10
*/
void thread_master_free_unused(struct thread_master *m)
{
- pthread_mutex_lock(&m->mtx);
- {
+ frr_with_mutex(&m->mtx) {
struct thread *t;
while ((t = thread_list_pop(&m->unuse)))
thread_free(m, t);
}
- pthread_mutex_unlock(&m->mtx);
}
/* Stop thread scheduler. */
{
struct thread *t;
- pthread_mutex_lock(&masters_mtx);
- {
+ frr_with_mutex(&masters_mtx) {
listnode_delete(masters, m);
if (masters->count == 0) {
list_delete(&masters);
}
}
- pthread_mutex_unlock(&masters_mtx);
thread_array_free(m, m->read);
thread_array_free(m, m->write);
{
int64_t remain;
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
remain = monotime_until(&thread->u.sands, NULL) / 1000LL;
}
- pthread_mutex_unlock(&thread->mtx);
return remain < 0 ? 0 : remain;
}
struct timeval thread_timer_remain(struct thread *thread)
{
struct timeval remain;
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
monotime_until(&thread->u.sands, &remain);
}
- pthread_mutex_unlock(&thread->mtx);
return remain;
}
struct thread **thread_array;
assert(fd >= 0 && fd < m->fd_limit);
- pthread_mutex_lock(&m->mtx);
- {
- if (t_ptr
- && *t_ptr) // thread is already scheduled; don't reschedule
- {
- pthread_mutex_unlock(&m->mtx);
- return NULL;
- }
+ frr_with_mutex(&m->mtx) {
+ if (t_ptr && *t_ptr)
+ // thread is already scheduled; don't reschedule
+ break;
/* default to a new pollfd */
nfds_t queuepos = m->handler.pfdcount;
m->handler.pfdcount++;
if (thread) {
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
thread->u.fd = fd;
thread_array[thread->u.fd] = thread;
}
- pthread_mutex_unlock(&thread->mtx);
if (t_ptr) {
*t_ptr = thread;
AWAKEN(m);
}
- pthread_mutex_unlock(&m->mtx);
return thread;
}
assert(type == THREAD_TIMER);
assert(time_relative);
- pthread_mutex_lock(&m->mtx);
- {
- if (t_ptr
- && *t_ptr) // thread is already scheduled; don't reschedule
- {
- pthread_mutex_unlock(&m->mtx);
+ frr_with_mutex(&m->mtx) {
+ if (t_ptr && *t_ptr)
+ // thread is already scheduled; don't reschedule
return NULL;
- }
thread = thread_get(m, type, func, arg, debugargpass);
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
monotime(&thread->u.sands);
timeradd(&thread->u.sands, time_relative,
&thread->u.sands);
thread->ref = t_ptr;
}
}
- pthread_mutex_unlock(&thread->mtx);
AWAKEN(m);
}
- pthread_mutex_unlock(&m->mtx);
return thread;
}
void *arg, int val,
struct thread **t_ptr, debugargdef)
{
- struct thread *thread;
+ struct thread *thread = NULL;
assert(m != NULL);
- pthread_mutex_lock(&m->mtx);
- {
- if (t_ptr
- && *t_ptr) // thread is already scheduled; don't reschedule
- {
- pthread_mutex_unlock(&m->mtx);
- return NULL;
- }
+ frr_with_mutex(&m->mtx) {
+ if (t_ptr && *t_ptr)
+ // thread is already scheduled; don't reschedule
+ break;
thread = thread_get(m, THREAD_EVENT, func, arg, debugargpass);
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
thread->u.val = val;
thread_list_add_tail(&m->event, thread);
}
- pthread_mutex_unlock(&thread->mtx);
if (t_ptr) {
*t_ptr = thread;
AWAKEN(m);
}
- pthread_mutex_unlock(&m->mtx);
return thread;
}
{
assert(master->owner == pthread_self());
- pthread_mutex_lock(&master->mtx);
- {
+ frr_with_mutex(&master->mtx) {
struct cancel_req *cr =
XCALLOC(MTYPE_TMP, sizeof(struct cancel_req));
cr->eventobj = arg;
listnode_add(master->cancel_req, cr);
do_thread_cancel(master);
}
- pthread_mutex_unlock(&master->mtx);
}
/**
assert(master->owner == pthread_self());
- pthread_mutex_lock(&master->mtx);
- {
+ frr_with_mutex(&master->mtx) {
struct cancel_req *cr =
XCALLOC(MTYPE_TMP, sizeof(struct cancel_req));
cr->thread = thread;
listnode_add(master->cancel_req, cr);
do_thread_cancel(master);
}
- pthread_mutex_unlock(&master->mtx);
}
/**
assert(!(thread && eventobj) && (thread || eventobj));
assert(master->owner != pthread_self());
- pthread_mutex_lock(&master->mtx);
- {
+ frr_with_mutex(&master->mtx) {
master->canceled = false;
if (thread) {
while (!master->canceled)
pthread_cond_wait(&master->cancel_cond, &master->mtx);
}
- pthread_mutex_unlock(&master->mtx);
}
/* ------------------------------------------------------------------------- */
int thread_should_yield(struct thread *thread)
{
int result;
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
result = monotime_since(&thread->real, NULL)
> (int64_t)thread->yield;
}
- pthread_mutex_unlock(&thread->mtx);
return result;
}
void thread_set_yield_time(struct thread *thread, unsigned long yield_time)
{
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
thread->yield = yield_time;
}
- pthread_mutex_unlock(&thread->mtx);
}
void thread_getrusage(RUSAGE_T *r)
struct thread *thread;
/* Get or allocate new thread to execute. */
- pthread_mutex_lock(&m->mtx);
- {
+ frr_with_mutex(&m->mtx) {
thread = thread_get(m, THREAD_EVENT, func, arg, debugargpass);
/* Set its event value. */
- pthread_mutex_lock(&thread->mtx);
- {
+ frr_with_mutex(&thread->mtx) {
thread->add_type = THREAD_EXECUTE;
thread->u.val = val;
thread->ref = &thread;
}
- pthread_mutex_unlock(&thread->mtx);
}
- pthread_mutex_unlock(&m->mtx);
/* Execute thread doing all accounting. */
thread_call(thread);
if (!pathname)
return CMD_WARNING_CONFIG_FAILED;
- frr_elevate_privs(vrf_daemon_privs) {
+ frr_with_privs(vrf_daemon_privs) {
ret = vrf_netns_handler_create(vty, vrf, pathname,
NS_UNKNOWN, NS_UNKNOWN);
}
/* Make ospf6d's server socket. */
int ospf6_serv_sock(void)
{
- frr_elevate_privs(&ospf6d_privs) {
+ frr_with_privs(&ospf6d_privs) {
ospf6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
if (ospf6_sock < 0) {
/* silently return since VRF is not ready */
return -1;
}
- frr_elevate_privs(&ospfd_privs) {
+ frr_with_privs(&ospfd_privs) {
ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP,
ospf->vrf_id, ospf->name);
if (ospf_sock < 0) {
old_vrf_id);
if (old_vrf_id != ospf->vrf_id) {
- frr_elevate_privs(&ospfd_privs) {
+ frr_with_privs(&ospfd_privs) {
/* stop zebra redist to us for old vrf */
zclient_send_dereg_requests(zclient,
old_vrf_id);
pbrms->ruleno = pbr_nht_get_next_rule(seqno);
pbrms->parent = pbrm;
pbrms->reason =
- PBR_MAP_INVALID_SRCDST |
+ PBR_MAP_INVALID_EMPTY |
PBR_MAP_INVALID_NO_NEXTHOPS;
QOBJ_REG(pbrms, pbr_map_sequence);
}
}
-static void pbr_map_sequence_check_src_dst_valid(struct pbr_map_sequence *pbrms)
+static void pbr_map_sequence_check_not_empty(struct pbr_map_sequence *pbrms)
{
- if (!pbrms->src && !pbrms->dst)
- pbrms->reason |= PBR_MAP_INVALID_SRCDST;
+ if (!pbrms->src && !pbrms->dst && !pbrms->mark)
+ pbrms->reason |= PBR_MAP_INVALID_EMPTY;
}
/*
{
pbr_map_sequence_check_nexthops_valid(pbrms);
- pbr_map_sequence_check_src_dst_valid(pbrms);
+ pbr_map_sequence_check_not_empty(pbrms);
}
static bool pbr_map_check_valid_internal(struct pbr_map *pbrm)
*/
struct prefix *src;
struct prefix *dst;
+ uint32_t mark;
/*
* Family of the src/dst. Needed when deleting since we clear them
#define PBR_MAP_INVALID_NEXTHOP (1 << 1)
#define PBR_MAP_INVALID_NO_NEXTHOPS (1 << 2)
#define PBR_MAP_INVALID_BOTH_NHANDGRP (1 << 3)
-#define PBR_MAP_INVALID_SRCDST (1 << 4)
+#define PBR_MAP_INVALID_EMPTY (1 << 4)
uint64_t reason;
QOBJ_FIELDS
return CMD_SUCCESS;
}
+DEFPY(pbr_map_match_mark, pbr_map_match_mark_cmd,
+ "[no] match mark (1-4294967295)$mark",
+ NO_STR
+ "Match the rest of the command\n"
+ "Choose the mark value to use\n"
+ "mark\n")
+{
+ struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
+
+#ifndef GNU_LINUX
+ vty_out(vty, "pbr marks are not supported on this platform");
+ return CMD_WARNING_CONFIG_FAILED;
+#endif
+
+ if (!no) {
+ if (pbrms->mark == (uint32_t) mark)
+ return CMD_SUCCESS;
+ pbrms->mark = (uint32_t) mark;
+ } else {
+ pbrms->mark = 0;
+ }
+
+ pbr_map_check(pbrms);
+
+ return CMD_SUCCESS;
+ }
+
DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
"[no] set nexthop-group NHGNAME$name",
NO_STR
vty_out(vty, "\tDST Match: %s\n",
prefix2str(pbrms->dst, buf,
sizeof(buf)));
+ if (pbrms->mark)
+ vty_out(vty, "\tMARK Match: %u\n", pbrms->mark);
if (pbrms->nhgrp_name) {
vty_out(vty,
vty_out(vty, " match dst-ip %s\n",
prefix2str(pbrms->dst, buff, sizeof(buff)));
+ if (pbrms->mark)
+ vty_out(vty, " match mark %u\n", pbrms->mark);
+
if (pbrms->nhgrp_name)
vty_out(vty, " set nexthop-group %s\n", pbrms->nhgrp_name);
install_element(INTERFACE_NODE, &pbr_policy_cmd);
install_element(PBRMAP_NODE, &pbr_map_match_src_cmd);
install_element(PBRMAP_NODE, &pbr_map_match_dst_cmd);
+ install_element(PBRMAP_NODE, &pbr_map_match_mark_cmd);
install_element(PBRMAP_NODE, &pbr_map_nexthop_group_cmd);
install_element(PBRMAP_NODE, &pbr_map_nexthop_cmd);
install_element(VIEW_NODE, &show_pbr_cmd);
stream_putw(s, 0); /* src port */
pbr_encode_pbr_map_sequence_prefix(s, pbrms->dst, family);
stream_putw(s, 0); /* dst port */
- stream_putl(s, 0); /* fwmark */
+ stream_putl(s, pbrms->mark);
if (pbrms->nhgrp_name)
stream_putl(s, pbr_nht_get_table(pbrms->nhgrp_name));
else if (pbrms->nhg)
* We need to create the VRF table for the pim mroute_socket
*/
if (pim->vrf_id != VRF_DEFAULT) {
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
data = pim->vrf->data.l.table_id;
err = setsockopt(pim->mroute_socket, IPPROTO_IP,
}
}
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
opt = enable ? MRT_INIT : MRT_DONE;
/*
* *BSD *cares* about what value we pass down
{
int fd;
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
}
}
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
/* bind to well known TCP port */
rc = bind(sock, (struct sockaddr *)&sin, socklen);
}
{
int fd;
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
fd = socket(AF_INET, SOCK_RAW, protocol);
{
const int on = 1;
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)))
zlog_err("%s: Could not turn on IP_HDRINCL option: %s",
int ret = 0;
#ifdef SO_BINDTODEVICE
- frr_elevate_privs(&pimd_privs) {
+ frr_with_privs(&pimd_privs) {
ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifp->name,
strlen(ifp->name));
/* Make datagram socket. */
if (vrf->vrf_id != VRF_DEFAULT)
vrf_dev = vrf->name;
- frr_elevate_privs(&ripd_privs) {
+ frr_with_privs(&ripd_privs) {
sock = vrf_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, vrf->vrf_id,
vrf_dev);
if (sock < 0) {
#endif
setsockopt_so_recvbuf(sock, RIP_UDP_RCV_BUF);
- frr_elevate_privs(&ripd_privs) {
+ frr_with_privs(&ripd_privs) {
if ((ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)))
< 0) {
zlog_err("%s: Can't bind socket %d to %s port %d: %s",
* While this is bogus, privs are available and easy to use
* for this call as a workaround.
*/
- frr_elevate_privs(&ripngd_privs) {
+ frr_with_privs(&ripngd_privs) {
ret = setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(char *)&mreq, sizeof(mreq));
/* Make datagram socket. */
if (vrf->vrf_id != VRF_DEFAULT)
vrf_dev = vrf->name;
- frr_elevate_privs(&ripngd_privs)
- {
+ frr_with_privs(&ripngd_privs) {
sock = vrf_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
vrf->vrf_id, vrf_dev);
if (sock < 0) {
#endif /* SIN6_LEN */
ripaddr.sin6_port = htons(RIPNG_PORT_DEFAULT);
- frr_elevate_privs(&ripngd_privs) {
+ frr_with_privs(&ripngd_privs) {
ret = bind(sock, (struct sockaddr *)&ripaddr, sizeof(ripaddr));
if (ret < 0) {
zlog_err("Can't bind ripng socket: %s.",
((test_privs.current_state() == ZPRIVS_RAISED) ? "Raised" : "Lowered")
printf("%s\n", PRIV_STATE());
- frr_elevate_privs(&test_privs) {
+ frr_with_privs(&test_privs) {
printf("%s\n", PRIV_STATE());
}
/* but these should continue to work... */
printf("%s\n", PRIV_STATE());
- frr_elevate_privs(&test_privs) {
+ frr_with_privs(&test_privs) {
printf("%s\n", PRIV_STATE());
}
# if/while/etc brace do not go on next line, unless defining a do while loop,
# or if that brace on the next line is for something else
- if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
+ if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)frr_(each|with)[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
my $pre_ctx = "$1$2";
my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
}
# Check relative indent for conditionals and blocks.
- if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
+ if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)frr_(each|with)[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
($stat, $cond, $line_nr_next, $remain_next, $off_next) =
ctx_statement_block($linenr, $realcnt, 0)
if (!defined $stat);
}
}
+ if (!defined $suppress_ifbraces{$linenr - 1} &&
+ $line =~ /\b(frr_with_)/) {
+ my ($level, $endln, @chunks) =
+ ctx_statement_full($linenr, $realcnt, $-[0]);
+
+ # Check the condition.
+ my ($cond, $block) = @{$chunks[0]};
+ #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
+ if (defined $cond) {
+ substr($block, 0, length($cond), '');
+ }
+
+ if ($level == 0 && $block !~ /^\s*\{/) {
+ my $herectx = $here . "\n";
+ my $cnt = statement_rawlines($block);
+
+ for (my $n = 0; $n < $cnt; $n++) {
+ $herectx .= raw_line($linenr, $n) . "\n";
+ }
+
+ WARN("BRACES",
+ "braces {} are mandatory for frr_with_* blocks\n" . $herectx);
+ }
+ }
+
# check for single line unbalanced braces
if ($sline =~ /^.\s*\}\s*else\s*$/ ||
$sline =~ /^.\s*else\s*\{\s*$/) {
--- /dev/null
+@@
+expression E;
+iterator name frr_with_mutex;
+@@
+
+- pthread_mutex_lock(E);
++ frr_with_mutex(E) {
+- {
+ ...
+- }
+- pthread_mutex_unlock(E);
++ }
+
+
+@@
+expression E;
+@@
+
+- pthread_mutex_lock(E);
++ frr_with_mutex(E) {
+ ...
+- pthread_mutex_unlock(E);
++ }
identifier change;
identifier end;
expression E, f, g;
-iterator name frr_elevate_privs;
+iterator name frr_with_privs;
@@
- if (E.change(ZPRIVS_RAISE))
- f;
-+ frr_elevate_privs(&E) {
++ frr_with_privs(&E) {
<+...
- goto end;
+ break;
@@
identifier change, errno, safe_strerror, exit;
expression E, f1, f2, f3, ret, fn;
-iterator name frr_elevate_privs;
+iterator name frr_with_privs;
@@
if (E.change(ZPRIVS_RAISE))
@@
identifier change;
expression E, f1, f2, f3, ret;
-iterator name frr_elevate_privs;
+iterator name frr_with_privs;
@@
if (E.change(ZPRIVS_RAISE))
@@
identifier change;
expression E, f, g;
-iterator name frr_elevate_privs;
+iterator name frr_with_privs;
@@
- if (E.change(ZPRIVS_RAISE))
- f;
-+ frr_elevate_privs(&E) {
++ frr_with_privs(&E) {
...
- if (E.change(ZPRIVS_LOWER))
- g;
int ret;
bool failed = false;
- frr_elevate_privs(&vrrp_privs)
- {
+ frr_with_privs(&vrrp_privs) {
r->sock_rx = socket(r->family, SOCK_RAW, IPPROTO_VRRP);
r->sock_tx = socket(r->family, SOCK_RAW, IPPROTO_VRRP);
}
setsockopt_ipv4_multicast_loop(r->sock_tx, 0);
/* Bind Rx socket to exact interface */
- frr_elevate_privs(&vrrp_privs)
- {
+ frr_with_privs(&vrrp_privs) {
ret = setsockopt(r->sock_rx, SOL_SOCKET,
SO_BINDTODEVICE, r->vr->ifp->name,
strlen(r->vr->ifp->name));
setsockopt_ipv6_multicast_loop(r->sock_tx, 0);
/* Bind Rx socket to exact interface */
- frr_elevate_privs(&vrrp_privs)
- {
+ frr_with_privs(&vrrp_privs) {
ret = setsockopt(r->sock_rx, SOL_SOCKET,
SO_BINDTODEVICE, r->vr->ifp->name,
strlen(r->vr->ifp->name));
/* Create the socket descriptor */
/* FIXME: why ETH_P_RARP? */
errno = 0;
- frr_elevate_privs(&vrrp_privs) {
+ frr_with_privs(&vrrp_privs) {
garp_fd = socket(PF_PACKET, SOCK_RAW | SOCK_CLOEXEC,
htons(ETH_P_RARP));
}
void vrrp_ndisc_init(void)
{
- frr_elevate_privs(&vrrp_privs)
- {
+ frr_with_privs(&vrrp_privs) {
ndisc_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IPV6));
}
size_t needed, lastneeded = 0;
char *buf = NULL;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(af, SOCK_DGRAM, 0);
}
}
calculate_lifc_len:
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
lifn.lifn_family = af;
lifn.lifn_flags = LIFC_NOXMIT;
/* we want NOXMIT interfaces too */
lifconf.lifc_len = needed;
lifconf.lifc_buf = buf;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = ioctl(sock, SIOCGLIFCONF, &lifconf);
}
ifdata.ifr_data = (caddr_t)&ecmd;
/* use ioctl to get IP address of an interface */
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
interface->vrf_id,
NULL);
int ret;
int err = 0;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
zlog_err("Cannot create UDP socket: %s",
int ret;
int err = 0;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = vrf_socket(AF_INET, SOCK_DGRAM, 0, vrf_id, NULL);
if (sock < 0) {
zlog_err("Cannot create UDP socket: %s",
int ret;
int err = 0;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock < 0) {
zlog_err("Cannot create IPv6 datagram socket: %s",
int ret;
int err;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
int ret;
int err;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock < 0) {
{
FILE *fp;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
fp = fopen(proc_ipv4_forwarding, "w");
{
FILE *fp;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
fp = fopen(proc_ipv4_forwarding, "w");
{
FILE *fp;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
fp = fopen(proc_ipv6_forwarding, "w");
{
FILE *fp;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
fp = fopen(proc_ipv6_forwarding, "w");
strioctl.ic_len = ND_BUFFER_SIZE;
strioctl.ic_dp = nd_buf;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if ((fd = open(device, O_RDWR)) < 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"failed to open device %s - %s", device,
int ipforwarding = 1;
len = sizeof ipforwarding;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"Can't set ipforwarding on");
int ipforwarding = 0;
len = sizeof ipforwarding;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"Can't set ipforwarding on");
int ip6forwarding = 0;
len = sizeof ip6forwarding;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (sysctl(mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
"can't get ip6forwarding value");
int ip6forwarding = 1;
len = sizeof ip6forwarding;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len)
< 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
int ip6forwarding = 0;
len = sizeof ip6forwarding;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len)
< 0) {
flog_err_sys(EC_LIB_SYSTEM_CALL,
int save_errno;
int sock;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
save_errno = errno;
}
/* Try force option (linux >= 2.6.14) and fall back to normal set */
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE,
&nl_rcvbufsize,
sizeof(nl_rcvbufsize));
int sock;
int namelen;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = ns_socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE, ns_id);
if (sock < 0) {
zlog_err("Can't open %s socket: %s", nl->name,
FILE *f;
snprintf(fname, MAXPATHLEN, "%s/%s_%u", frr_vtydir, "netlink", counter);
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
f = fopen(fname, "w");
}
if (f) {
FILE *f;
long file_bytes = -1;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
f = fopen(fname, "r");
}
if (f) {
n->nlmsg_flags);
/* Send message to netlink interface. */
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
status = sendmsg(nl->sock, &msg, 0);
save_errno = errno;
}
snl.nl_family = AF_NETLINK;
/* Raise capabilities and send message, then lower capabilities. */
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = sendto(nl->sock, (void *)n, n->nlmsg_len, 0,
(struct sockaddr *)&snl, sizeof snl);
}
/* Make routing socket. */
static void routing_socket(struct zebra_ns *zns)
{
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
routing_sock = ns_socket(AF_ROUTE, SOCK_RAW, 0, zns->ns_id);
dplane_routing_sock =
type = dplane_ctx_get_type(ctx);
old_type = dplane_ctx_get_old_type(ctx);
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
if (dplane_ctx_get_op(ctx) == DPLANE_OP_ROUTE_DELETE) {
if (!RSYSTEM_ROUTE(type))
int ret = 0;
struct icmp6_filter filter;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
sock = ns_socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ns_id);
snprintf(fname, MAXPATHLEN, "%s/%u", frr_vtydir, command);
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
}
stream_flush(copy, fd);
"FPM protobuf message format is not available");
return;
}
+ flog_warn(EC_ZEBRA_PROTOBUF_NOT_AVAILABLE,
+ "FPM protobuf message format is deprecated and scheduled to be removed. "
+ "Please convert to using netlink format or contact dev@lists.frrouting.org with your use case.");
zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF;
return;
}
hdr.rtm_mpls = MPLS_OP_SWAP;
}
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = writev(kr_state.fd, iov, iovcnt);
}
hdr.rtm_mpls = MPLS_OP_SWAP;
}
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = writev(kr_state.fd, iov, iovcnt);
}
if (netnspath == NULL)
return;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ns_id = zebra_ns_id_get(netnspath);
}
if (ns_id == NS_UNKNOWN)
ns_map_nsid_with_external(ns_id, false);
return;
}
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ret = vrf_netns_handler_create(NULL, vrf, netnspath,
ns_id_external, ns_id);
}
netnspath = zns_info->netnspath;
if (--zns_info->retries == 0)
stop_retry = 1;
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
err = ns_switch_to_netns(netnspath);
}
if (err < 0)
return zebra_ns_continue_read(zns_info, stop_retry);
/* go back to default ns */
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
err = ns_switchback_to_initial();
}
if (err < 0)
struct nexthop *newhop;
struct interface *ifp;
rib_dest_t *dest;
+ struct zebra_vrf *zvrf;
if ((nexthop->type == NEXTHOP_TYPE_IPV4)
|| nexthop->type == NEXTHOP_TYPE_IPV6)
}
/* Lookup table. */
table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
- if (!table) {
+ /* get zvrf */
+ zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
+ if (!table || !zvrf) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug("\t%s: Table not found",
__PRETTY_FUNCTION__);
/* However, do not resolve over default route unless explicitly
* allowed. */
if (is_default_prefix(&rn->p)
- && !rnh_resolve_via_default(p.family)) {
+ && !rnh_resolve_via_default(zvrf, p.family)) {
if (IS_ZEBRA_DEBUG_RIB_DETAILED)
zlog_debug(
"\t:%s: Resolved against default route",
dzns = zebra_ns_alloc();
- frr_elevate_privs(&zserv_privs) {
+ frr_with_privs(&zserv_privs) {
ns_id = zebra_ns_id_get_default();
}
ns_id_external = ns_map_nsid_with_external(ns_id, true);
#include "vrf.h"
#include "workqueue.h"
#include "nexthop_group_private.h"
+#include "frr_pthread.h"
#include "zebra/zebra_router.h"
#include "zebra/connected.h"
TAILQ_INIT(&ctxlist);
/* Take lock controlling queue of results */
- pthread_mutex_lock(&dplane_mutex);
- {
+ frr_with_mutex(&dplane_mutex) {
/* Dequeue list of context structs */
dplane_ctx_list_append(&ctxlist, &rib_dplane_q);
}
- pthread_mutex_unlock(&dplane_mutex);
/* Dequeue context block */
ctx = dplane_ctx_dequeue(&ctxlist);
static int rib_dplane_results(struct dplane_ctx_q *ctxlist)
{
/* Take lock controlling queue of results */
- pthread_mutex_lock(&dplane_mutex);
- {
+ frr_with_mutex(&dplane_mutex) {
/* Enqueue context blocks */
dplane_ctx_list_append(&rib_dplane_q, ctxlist);
}
- pthread_mutex_unlock(&dplane_mutex);
/* Ensure event is signalled to zebra main pthread */
thread_add_event(zrouter.master, rib_process_dplane_results, NULL, 0,
static void print_rnh(struct route_node *rn, struct vty *vty);
static int zebra_client_cleanup_rnh(struct zserv *client);
-int zebra_rnh_ip_default_route = 0;
-int zebra_rnh_ipv6_default_route = 0;
-
void zebra_rnh_init(void)
{
hook_register(zserv_client_close, zebra_client_cleanup_rnh);
* match route to be exact if so specified
*/
if (is_default_prefix(&rn->p)
- && !rnh_resolve_via_default(rn->p.family)) {
+ && !rnh_resolve_via_default(zvrf, rn->p.family)) {
if (IS_ZEBRA_DEBUG_NHT_DETAILED)
zlog_debug(
"\tNot allowed to resolve through default prefix");
return 0;
}
+
+int rnh_resolve_via_default(struct zebra_vrf *zvrf, int family)
+{
+ if (((family == AF_INET) && zvrf->zebra_rnh_ip_default_route)
+ || ((family == AF_INET6) && zvrf->zebra_rnh_ipv6_default_route))
+ return 1;
+ else
+ return 0;
+}
extern "C" {
#endif
-extern int zebra_rnh_ip_default_route;
-extern int zebra_rnh_ipv6_default_route;
-
extern void zebra_rnh_init(void);
-static inline int rnh_resolve_via_default(int family)
-{
- if (((family == AF_INET) && zebra_rnh_ip_default_route)
- || ((family == AF_INET6) && zebra_rnh_ipv6_default_route))
- return 1;
- else
- return 0;
-}
-
static inline const char *rnh_type2str(rnh_type_t type)
{
switch (type) {
rnh_type_t type, struct prefix *p);
extern char *rnh_str(struct rnh *rnh, char *buf, int size);
+extern int rnh_resolve_via_default(struct zebra_vrf *zvrf, int family);
+
#ifdef __cplusplus
}
#endif
if (zvrf_id(zvrf) == VRF_DEFAULT) {
if (zvrf->l3vni)
vty_out(vty, "vni %u\n", zvrf->l3vni);
+ if (zvrf->zebra_rnh_ip_default_route)
+ vty_out(vty, "ip nht resolve-via-default\n");
+
+ if (zvrf->zebra_rnh_ipv6_default_route)
+ vty_out(vty, "ipv6 nht resolve-via-default\n");
} else {
vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
if (zvrf->l3vni)
? " prefix-routes-only"
: "");
zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
+ if (zvrf->zebra_rnh_ip_default_route)
+ vty_out(vty, " ip nht resolve-via-default\n");
+
+ if (zvrf->zebra_rnh_ipv6_default_route)
+ vty_out(vty, " ipv6 nht resolve-via-default\n");
}
+
zebra_routemap_config_write_protocol(vty, zvrf);
if (zvrf_id(zvrf) != VRF_DEFAULT)
#if defined(HAVE_RTADV)
struct rtadv rtadv;
#endif /* HAVE_RTADV */
+
+ int zebra_rnh_ip_default_route;
+ int zebra_rnh_ipv6_default_route;
};
#define PROTO_RM_NAME(zvrf, afi, rtype) zvrf->proto_rm[afi][rtype].name
#define NHT_RM_NAME(zvrf, afi, rtype) zvrf->nht_rm[afi][rtype].name
if (!zvrf)
return CMD_WARNING;
- if (zebra_rnh_ip_default_route)
+ if (zvrf->zebra_rnh_ip_default_route)
return CMD_SUCCESS;
- zebra_rnh_ip_default_route = 1;
+ zvrf->zebra_rnh_ip_default_route = 1;
zebra_evaluate_rnh(zvrf, AFI_IP, 1, RNH_NEXTHOP_TYPE, NULL);
return CMD_SUCCESS;
if (!zvrf)
return CMD_WARNING;
- if (!zebra_rnh_ip_default_route)
+ if (!zvrf->zebra_rnh_ip_default_route)
return CMD_SUCCESS;
- zebra_rnh_ip_default_route = 0;
+ zvrf->zebra_rnh_ip_default_route = 0;
zebra_evaluate_rnh(zvrf, AFI_IP, 1, RNH_NEXTHOP_TYPE, NULL);
return CMD_SUCCESS;
}
if (!zvrf)
return CMD_WARNING;
- if (zebra_rnh_ipv6_default_route)
+ if (zvrf->zebra_rnh_ipv6_default_route)
return CMD_SUCCESS;
- zebra_rnh_ipv6_default_route = 1;
+ zvrf->zebra_rnh_ipv6_default_route = 1;
zebra_evaluate_rnh(zvrf, AFI_IP6, 1, RNH_NEXTHOP_TYPE, NULL);
return CMD_SUCCESS;
}
if (!zvrf)
return CMD_WARNING;
- if (!zebra_rnh_ipv6_default_route)
+ if (!zvrf->zebra_rnh_ipv6_default_route)
return CMD_SUCCESS;
- zebra_rnh_ipv6_default_route = 0;
+ zvrf->zebra_rnh_ipv6_default_route = 0;
zebra_evaluate_rnh(zvrf, AFI_IP6, 1, RNH_NEXTHOP_TYPE, NULL);
return CMD_SUCCESS;
}
if (allow_delete)
vty_out(vty, "allow-external-route-update\n");
- if (zebra_rnh_ip_default_route)
- vty_out(vty, "ip nht resolve-via-default\n");
-
- if (zebra_rnh_ipv6_default_route)
- vty_out(vty, "ipv6 nht resolve-via-default\n");
-
if (zrouter.ribq->spec.hold != ZEBRA_RIB_PROCESS_HOLD_TIME)
vty_out(vty, "zebra work-queue %u\n", zrouter.ribq->spec.hold);
cache = stream_fifo_new();
- pthread_mutex_lock(&client->obuf_mtx);
- {
+ frr_with_mutex(&client->obuf_mtx) {
while (stream_fifo_head(client->obuf_fifo))
stream_fifo_push(cache,
stream_fifo_pop(client->obuf_fifo));
}
- pthread_mutex_unlock(&client->obuf_mtx);
if (cache->tail) {
msg = cache->tail;
memory_order_relaxed);
/* publish read packets on client's input queue */
- pthread_mutex_lock(&client->ibuf_mtx);
- {
+ frr_with_mutex(&client->ibuf_mtx) {
while (cache->head)
stream_fifo_push(client->ibuf_fifo,
stream_fifo_pop(cache));
}
- pthread_mutex_unlock(&client->ibuf_mtx);
/* Schedule job to process those packets */
zserv_event(client, ZSERV_PROCESS_MESSAGES);
uint32_t p2p = zrouter.packets_to_process;
bool need_resched = false;
- pthread_mutex_lock(&client->ibuf_mtx);
- {
+ frr_with_mutex(&client->ibuf_mtx) {
uint32_t i;
for (i = 0; i < p2p && stream_fifo_head(client->ibuf_fifo);
++i) {
if (stream_fifo_head(client->ibuf_fifo))
need_resched = true;
}
- pthread_mutex_unlock(&client->ibuf_mtx);
while (stream_fifo_head(cache)) {
msg = stream_fifo_pop(cache);
int zserv_send_message(struct zserv *client, struct stream *msg)
{
- pthread_mutex_lock(&client->obuf_mtx);
- {
+ frr_with_mutex(&client->obuf_mtx) {
stream_fifo_push(client->obuf_fifo, msg);
}
- pthread_mutex_unlock(&client->obuf_mtx);
zserv_client_event(client, ZSERV_CLIENT_WRITE);
setsockopt_so_recvbuf(zsock, 1048576);
setsockopt_so_sendbuf(zsock, 1048576);
- frr_elevate_privs((sa.ss_family != AF_UNIX) ? &zserv_privs : NULL) {
+ frr_with_privs((sa.ss_family != AF_UNIX) ? &zserv_privs : NULL) {
ret = bind(zsock, (struct sockaddr *)&sa, sa_len);
}
if (ret < 0) {