]> git.proxmox.com Git - mirror_frr.git/blobdiff - ldpd/lde_lib.c
Merge pull request #13631 from donaldsharp/fix_some_ping_issues
[mirror_frr.git] / ldpd / lde_lib.c
index 33bb6c0fc73b23af94bcf3b007a2522da5a3fdd2..04bff9015856d6bfb87697f8e9f8b9f5c286d31b 100644 (file)
@@ -1,20 +1,9 @@
+// SPDX-License-Identifier: ISC
 /*     $OpenBSD$ */
 
 /*
  * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
 #include <zebra.h>
 #include "mpls.h"
 
 static __inline int     fec_compare(const struct fec *, const struct fec *);
-static int              lde_nbr_is_nexthop(struct fec_node *,
-                           struct lde_nbr *);
-static void             fec_free(void *);
-static struct fec_node *fec_add(struct fec *fec);
-static struct fec_nh   *fec_nh_add(struct fec_node *, int, union ldpd_addr *,
+static int      lde_nbr_is_nexthop(struct fec_node *, struct lde_nbr *);
+static void     fec_free(void *);
+static struct fec_node  *fec_add(struct fec *fec);
+static struct fec_nh    *fec_nh_add(struct fec_node *, int, union ldpd_addr *,
                            ifindex_t, uint8_t, unsigned short);
-static void             fec_nh_del(struct fec_nh *);
+static void     fec_nh_del(struct fec_nh *);
 
 RB_GENERATE(fec_tree, fec, entry, fec_compare)
 
-struct fec_tree                 ft = RB_INITIALIZER(&ft);
-struct thread          *gc_timer;
+struct fec_tree         ft = RB_INITIALIZER(&ft);
+struct event *gc_timer;
 
 /* FEC tree functions */
 void
@@ -58,11 +46,9 @@ fec_compare(const struct fec *a, const struct fec *b)
 
        switch (a->type) {
        case FEC_TYPE_IPV4:
-               if (ntohl(a->u.ipv4.prefix.s_addr) <
-                   ntohl(b->u.ipv4.prefix.s_addr))
+               if (ntohl(a->u.ipv4.prefix.s_addr) < ntohl(b->u.ipv4.prefix.s_addr))
                        return (-1);
-               if (ntohl(a->u.ipv4.prefix.s_addr) >
-                   ntohl(b->u.ipv4.prefix.s_addr))
+               if (ntohl(a->u.ipv4.prefix.s_addr) > ntohl(b->u.ipv4.prefix.s_addr))
                        return (1);
                if (a->u.ipv4.prefixlen < b->u.ipv4.prefixlen)
                        return (-1);
@@ -90,11 +76,9 @@ fec_compare(const struct fec *a, const struct fec *b)
                        return (-1);
                if (a->u.pwid.pwid > b->u.pwid.pwid)
                        return (1);
-               if (ntohl(a->u.pwid.lsr_id.s_addr) <
-                   ntohl(b->u.pwid.lsr_id.s_addr))
+               if (ntohl(a->u.pwid.lsr_id.s_addr) < ntohl(b->u.pwid.lsr_id.s_addr))
                        return (-1);
-               if (ntohl(a->u.pwid.lsr_id.s_addr) >
-                   ntohl(b->u.pwid.lsr_id.s_addr))
+               if (ntohl(a->u.pwid.lsr_id.s_addr) > ntohl(b->u.pwid.lsr_id.s_addr))
                        return (1);
                return (0);
        }
@@ -178,7 +162,7 @@ rt_dump(pid_t pid)
                        rtctl.prefix.v6 = fn->fec.u.ipv6.prefix;
                        rtctl.prefixlen = fn->fec.u.ipv6.prefixlen;
                        break;
-               default:
+               case FEC_TYPE_PWID:
                        continue;
                }
 
@@ -272,8 +256,7 @@ fec_add(struct fec *fec)
                fn->pw_remote_status = PW_FORWARDING;
 
        if (fec_insert(&ft, &fn->fec))
-               log_warnx("failed to add %s to ft tree",
-                   log_fec(&fn->fec));
+               log_warnx("failed to add %s to ft tree", log_fec(&fn->fec));
 
        return (fn);
 }
@@ -349,14 +332,14 @@ lde_kernel_insert(struct fec *fec, int af, union ldpd_addr *nexthop,
                 * installing in kernel and sending to peer
                 */
                iface = if_lookup(ldeconf, ifindex);
-               if ((ldeconf->flags & F_LDPD_ORDERED_CONTROL) &&
+               if (CHECK_FLAG(ldeconf->flags, F_LDPD_ORDERED_CONTROL) &&
                    !connected && iface != NULL && fec->type != FEC_TYPE_PWID)
-                       fnh->flags |= F_FEC_NH_DEFER;
+                       SET_FLAG(fnh->flags, F_FEC_NH_DEFER);
        }
 
-       fnh->flags |= F_FEC_NH_NEW;
+       SET_FLAG(fnh->flags, F_FEC_NH_NEW);
        if (connected)
-               fnh->flags |= F_FEC_NH_CONNECTED;
+               SET_FLAG(fnh->flags, F_FEC_NH_CONNECTED);
 }
 
 void
@@ -399,22 +382,22 @@ lde_kernel_update(struct fec *fec)
                return;
 
        LIST_FOREACH_SAFE(fnh, &fn->nexthops, entry, safe) {
-               if (fnh->flags & F_FEC_NH_NEW) {
-                       fnh->flags &= ~F_FEC_NH_NEW;
+               if (CHECK_FLAG(fnh->flags, F_FEC_NH_NEW)) {
+                       UNSET_FLAG(fnh->flags, F_FEC_NH_NEW);
                        /*
                         * if LDP configured on interface or a static route
                         * clear flag else treat fec as a connected route
                         */
-                       if (ldeconf->flags & F_LDPD_ENABLED) {
+                       if (CHECK_FLAG(ldeconf->flags, F_LDPD_ENABLED)) {
                                iface = if_lookup(ldeconf,fnh->ifindex);
-                               if (fnh->flags & F_FEC_NH_CONNECTED ||
+                               if (CHECK_FLAG(fnh->flags, F_FEC_NH_CONNECTED) ||
                                    iface ||
                                    fnh->route_type == ZEBRA_ROUTE_STATIC)
-                                       fnh->flags &=~F_FEC_NH_NO_LDP;
+                                       UNSET_FLAG(fnh->flags, F_FEC_NH_NO_LDP);
                                else
-                                       fnh->flags |= F_FEC_NH_NO_LDP;
+                                       SET_FLAG(fnh->flags, F_FEC_NH_NO_LDP);
                        } else
-                               fnh->flags |= F_FEC_NH_NO_LDP;
+                               SET_FLAG(fnh->flags, F_FEC_NH_NO_LDP);
                } else {
                        lde_send_delete_klabel(fn, fnh);
                        fec_nh_del(fnh);
@@ -505,7 +488,7 @@ lde_check_mapping(struct map *map, struct lde_nbr *ln, int rcvd_label_mapping)
                    fec.u.ipv6.prefixlen) != FILTER_PERMIT)
                        return;
                break;
-       default:
+       case FEC_TYPE_PWID:
                break;
        }
 
@@ -521,7 +504,7 @@ lde_check_mapping(struct map *map, struct lde_nbr *ln, int rcvd_label_mapping)
 
        /* RFC 4447 control word and status tlv negotiation */
        if (map->type == MAP_TYPE_PWID && l2vpn_pw_negotiate(ln, fn, map)) {
-               if (rcvd_label_mapping && map->flags & F_MAP_PW_STATUS)
+               if (rcvd_label_mapping && CHECK_FLAG(map->flags, F_MAP_PW_STATUS))
                        fn->pw_remote_status = map->pw_status;
 
                return;
@@ -545,8 +528,7 @@ lde_check_mapping(struct map *map, struct lde_nbr *ln, int rcvd_label_mapping)
                         * the possibility of multipath.
                         */
                        LIST_FOREACH(fnh, &fn->nexthops, entry) {
-                               if (lde_address_find(ln, fnh->af,
-                                   &fnh->nexthop) == NULL)
+                               if (lde_address_find(ln, fnh->af, &fnh->nexthop) == NULL)
                                        continue;
 
                                lde_send_delete_klabel(fn, fnh);
@@ -572,9 +554,9 @@ lde_check_mapping(struct map *map, struct lde_nbr *ln, int rcvd_label_mapping)
                         * NH so clear flag and send labelmap msg to
                         * peer
                         */
-                       if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
+                       if (CHECK_FLAG(ldeconf->flags, F_LDPD_ORDERED_CONTROL)) {
                                send_map = true;
-                               fnh->flags &= ~F_FEC_NH_DEFER;
+                               UNSET_FLAG(fnh->flags, F_FEC_NH_DEFER);
                        }
                        fnh->remote_label = map->label;
                        if (fn->local_label != NO_LABEL)
@@ -586,9 +568,9 @@ lde_check_mapping(struct map *map, struct lde_nbr *ln, int rcvd_label_mapping)
                                continue;
 
                        pw->remote_group = map->fec.pwid.group_id;
-                       if (map->flags & F_MAP_PW_IFMTU)
+                       if (CHECK_FLAG(map->flags, F_MAP_PW_IFMTU))
                                pw->remote_mtu = map->fec.pwid.ifmtu;
-                       if (rcvd_label_mapping && map->flags & F_MAP_PW_STATUS) {
+                       if (rcvd_label_mapping && CHECK_FLAG(map->flags, F_MAP_PW_STATUS)) {
                                pw->remote_status = map->pw_status;
                                fn->pw_remote_status = map->pw_status;
                        }
@@ -665,7 +647,7 @@ lde_check_request(struct map *map, struct lde_nbr *ln)
                        lde_send_notification(ln, S_LOOP_DETECTED, map->msg_id,
                            htons(MSG_TYPE_LABELREQUEST));
                        return;
-               default:
+               case FEC_TYPE_PWID:
                        break;
                }
        }
@@ -737,7 +719,7 @@ lde_check_release(struct map *map, struct lde_nbr *ln)
        /* wildcard label release */
        if (map->type == MAP_TYPE_WILDCARD ||
            map->type == MAP_TYPE_TYPED_WCARD ||
-           (map->type == MAP_TYPE_PWID && !(map->flags & F_MAP_PW_ID))) {
+           (map->type == MAP_TYPE_PWID && !CHECK_FLAG(map->flags, F_MAP_PW_ID))) {
                lde_check_release_wcard(map, ln);
                return;
        }
@@ -829,7 +811,7 @@ lde_check_withdraw(struct map *map, struct lde_nbr *ln)
        /* wildcard label withdraw */
        if (map->type == MAP_TYPE_WILDCARD ||
            map->type == MAP_TYPE_TYPED_WCARD ||
-           (map->type == MAP_TYPE_PWID && !(map->flags & F_MAP_PW_ID))) {
+           (map->type == MAP_TYPE_PWID && !CHECK_FLAG(map->flags, F_MAP_PW_ID))) {
                lde_check_withdraw_wcard(map, ln);
                return;
        }
@@ -879,15 +861,14 @@ lde_check_withdraw(struct map *map, struct lde_nbr *ln)
                return;
 
        /* Ordered Control: additional withdraw steps */
-       if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
+       if (CHECK_FLAG(ldeconf->flags, F_LDPD_ORDERED_CONTROL)) {
                /* LWd.8: for each neighbor other that src of withdraw msg */
                RB_FOREACH(lnbr, nbr_tree, &lde_nbrs) {
                        if (ln->peerid == lnbr->peerid)
                                continue;
 
                        /* LWd.9: check if previously sent a label mapping */
-                       me = (struct lde_map *)fec_find(&lnbr->sent_map,
-                           &fn->fec);
+                       me = (struct lde_map *)fec_find(&lnbr->sent_map, &fn->fec);
 
                        /*
                         * LWd.10: does label sent to peer "map" to withdraw
@@ -926,8 +907,7 @@ lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
                        switch (f->type) {
                        case FEC_TYPE_IPV4:
                        case FEC_TYPE_IPV6:
-                               if (!lde_address_find(ln, fnh->af,
-                                   &fnh->nexthop))
+                               if (!lde_address_find(ln, fnh->af, &fnh->nexthop))
                                        continue;
                                break;
                        case FEC_TYPE_PWID:
@@ -940,8 +920,7 @@ lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
                        default:
                                break;
                        }
-                       if (map->label != NO_LABEL && map->label !=
-                           fnh->remote_label)
+                       if (map->label != NO_LABEL && map->label != fnh->remote_label)
                                continue;
 
                        lde_send_delete_klabel(fn, fnh);
@@ -952,8 +931,7 @@ lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
                lde_rlfa_update_clients(f, ln, MPLS_INVALID_LABEL);
 
                /* LWd.3: check previously received label mapping */
-               if (me && (map->label == NO_LABEL ||
-                   map->label == me->map.label))
+               if (me && (map->label == NO_LABEL || map->label == me->map.label))
                        /*
                         * LWd.4: remove record of previously received
                         * label mapping
@@ -964,7 +942,7 @@ lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
                        continue;
 
                /* Ordered Control: additional withdraw steps */
-               if (ldeconf->flags & F_LDPD_ORDERED_CONTROL) {
+               if (CHECK_FLAG(ldeconf->flags, F_LDPD_ORDERED_CONTROL)) {
                        /*
                         * LWd.8: for each neighbor other that src of
                         *  withdraw msg
@@ -976,16 +954,14 @@ lde_check_withdraw_wcard(struct map *map, struct lde_nbr *ln)
                                /* LWd.9: check if previously sent a label
                                 * mapping
                                 */
-                               me = (struct lde_map *)fec_find(
-                                   &lnbr->sent_map, &fn->fec);
+                               me = (struct lde_map *)fec_find(&lnbr->sent_map, &fn->fec);
                                /*
                                 * LWd.10: does label sent to peer "map" to
                                 *  withdraw label
                                 */
                                if (me && lde_nbr_is_nexthop(fn, lnbr))
                                        /* LWd.11: send label withdraw */
-                                       lde_send_labelwithdraw(lnbr, fn, NULL,
-                                           NULL);
+                                       lde_send_labelwithdraw(lnbr, fn, NULL, NULL);
                        }
                }
        }
@@ -1037,8 +1013,7 @@ lde_wildcard_apply(struct map *wcard, struct fec *fec, struct lde_map *me)
 /* gabage collector timer: timer to remove dead entries from the LIB */
 
 /* ARGSUSED */
-int
-lde_gc_timer(struct thread *thread)
+void lde_gc_timer(struct event *thread)
 {
        struct fec      *fec, *safe;
        struct fec_node *fn;
@@ -1064,20 +1039,17 @@ lde_gc_timer(struct thread *thread)
                log_debug("%s: %u entries removed", __func__, count);
 
        lde_gc_start_timer();
-
-       return (0);
 }
 
 void
 lde_gc_start_timer(void)
 {
-       thread_cancel(&gc_timer);
-       thread_add_timer(master, lde_gc_timer, NULL, LDE_GC_INTERVAL,
-                        &gc_timer);
+       EVENT_OFF(gc_timer);
+       event_add_timer(master, lde_gc_timer, NULL, LDE_GC_INTERVAL, &gc_timer);
 }
 
 void
 lde_gc_stop_timer(void)
 {
-       thread_cancel(&gc_timer);
+       EVENT_OFF(gc_timer);
 }