]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_nexthop.c
*: list_delete_and_null() -> list_delete()
[mirror_frr.git] / bgpd / bgp_nexthop.c
index 79463ee142e978c591ad369677dd68132636ce36..ce7f5b40f5db58a0cf570fd0df205667fe32efaa 100644 (file)
@@ -80,12 +80,14 @@ static void bgp_nexthop_cache_reset(struct bgp_table *table)
        struct bgp_node *rn;
        struct bgp_nexthop_cache *bnc;
 
-       for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn))
-               if ((bnc = rn->info) != NULL) {
+       for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
+               bnc = bgp_nexthop_get_node_info(rn);
+               if (bnc != NULL) {
                        bnc_free(bnc);
-                       rn->info = NULL;
+                       bgp_nexthop_set_node_info(rn, NULL);
                        bgp_unlock_node(rn);
                }
+       }
 }
 
 static void *bgp_tip_hash_alloc(void *p)
@@ -169,20 +171,62 @@ void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
        }
 }
 
+/* BGP own address structure */
+struct bgp_addr {
+       struct in_addr addr;
+       struct list *ifp_name_list;
+};
+
+static void show_address_entry(struct hash_backet *backet, void *args)
+{
+       struct vty *vty = (struct vty *)args;
+       struct bgp_addr *addr = (struct bgp_addr *)backet->data;
+       char *name;
+       struct listnode *node;
+
+       vty_out(vty, "addr: %s, count: %d : ", inet_ntoa(addr->addr),
+               addr->ifp_name_list->count);
+
+       for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
+               vty_out(vty, " %s,", name);
+       }
+
+       vty_out(vty, "\n");
+}
+
+void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp)
+{
+       hash_iterate(bgp->address_hash,
+                    (void (*)(struct hash_backet *, void *))show_address_entry,
+                    vty);
+}
+
+static void bgp_address_hash_string_del(void *val)
+{
+       char *data = val;
+
+       XFREE(MTYPE_TMP, data);
+}
+
 static void *bgp_address_hash_alloc(void *p)
 {
        const struct in_addr *val = (const struct in_addr *)p;
        struct bgp_addr *addr;
 
        addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
-       addr->refcnt = 0;
        addr->addr.s_addr = val->s_addr;
 
+       addr->ifp_name_list = list_new();
+       addr->ifp_name_list->del = bgp_address_hash_string_del;
+
        return addr;
 }
 
-static void bgp_address_hash_free(void *addr)
+static void bgp_address_hash_free(void *data)
 {
+       struct bgp_addr *addr = data;
+
+       list_delete(&addr->ifp_name_list);
        XFREE(MTYPE_BGP_ADDR, addr);
 }
 
@@ -217,24 +261,35 @@ void bgp_address_destroy(struct bgp *bgp)
        bgp->address_hash = NULL;
 }
 
-static void bgp_address_add(struct bgp *bgp, struct prefix *p)
+static void bgp_address_add(struct bgp *bgp, struct connected *ifc,
+                           struct prefix *p)
 {
        struct bgp_addr tmp;
        struct bgp_addr *addr;
+       struct listnode *node;
+       char *name;
 
        tmp.addr = p->u.prefix4;
 
        addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
-       if (!addr)
-               return;
 
-       addr->refcnt++;
+       for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
+               if (strcmp(ifc->ifp->name, name) == 0)
+                       break;
+       }
+       if (!node) {
+               name = XSTRDUP(MTYPE_TMP, ifc->ifp->name);
+               listnode_add(addr->ifp_name_list, name);
+       }
 }
 
-static void bgp_address_del(struct bgp *bgp, struct prefix *p)
+static void bgp_address_del(struct bgp *bgp, struct connected *ifc,
+                           struct prefix *p)
 {
        struct bgp_addr tmp;
        struct bgp_addr *addr;
+       struct listnode *node;
+       char *name;
 
        tmp.addr = p->u.prefix4;
 
@@ -243,10 +298,17 @@ static void bgp_address_del(struct bgp *bgp, struct prefix *p)
        if (addr == NULL)
                return;
 
-       addr->refcnt--;
+       for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
+               if (strcmp(ifc->ifp->name, name) == 0)
+                       break;
+       }
 
-       if (addr->refcnt == 0) {
+       if (node)
+               list_delete_node(addr->ifp_name_list, node);
+
+       if (addr->ifp_name_list->count == 0) {
                hash_release(bgp->address_hash, addr);
+               list_delete(&addr->ifp_name_list);
                XFREE(MTYPE_BGP_ADDR, addr);
        }
 }
@@ -274,18 +336,18 @@ void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
                if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
                        return;
 
-               bgp_address_add(bgp, addr);
+               bgp_address_add(bgp, ifc, addr);
 
                rn = bgp_node_get(bgp->connected_table[AFI_IP],
                                  (struct prefix *)&p);
-               if (rn->info) {
-                       bc = rn->info;
+               bc = bgp_connected_get_node_info(rn);
+               if (bc)
                        bc->refcnt++;
-               else {
+               else {
                        bc = XCALLOC(MTYPE_BGP_CONN,
                                     sizeof(struct bgp_connected_ref));
                        bc->refcnt = 1;
-                       rn->info = bc;
+                       bgp_connected_set_node_info(rn, bc);
                }
 
                for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
@@ -310,14 +372,15 @@ void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
 
                rn = bgp_node_get(bgp->connected_table[AFI_IP6],
                                  (struct prefix *)&p);
-               if (rn->info) {
-                       bc = rn->info;
+
+               bc = bgp_connected_get_node_info(rn);
+               if (bc)
                        bc->refcnt++;
-               else {
+               else {
                        bc = XCALLOC(MTYPE_BGP_CONN,
                                     sizeof(struct bgp_connected_ref));
                        bc->refcnt = 1;
-                       rn->info = bc;
+                       bgp_connected_set_node_info(rn, bc);
                }
        }
 }
@@ -326,35 +389,21 @@ void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
 {
        struct prefix p;
        struct prefix *addr;
-       struct bgp_node *rn;
+       struct bgp_node *rn = NULL;
        struct bgp_connected_ref *bc;
 
        addr = ifc->address;
 
        p = *(CONNECTED_PREFIX(ifc));
+       apply_mask(&p);
        if (addr->family == AF_INET) {
-               apply_mask_ipv4((struct prefix_ipv4 *)&p);
-
                if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
                        return;
 
-               bgp_address_del(bgp, addr);
+               bgp_address_del(bgp, ifc, addr);
 
                rn = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
-               if (!rn)
-                       return;
-
-               bc = rn->info;
-               bc->refcnt--;
-               if (bc->refcnt == 0) {
-                       XFREE(MTYPE_BGP_CONN, bc);
-                       rn->info = NULL;
-               }
-               bgp_unlock_node(rn);
-               bgp_unlock_node(rn);
        } else if (addr->family == AF_INET6) {
-               apply_mask_ipv6((struct prefix_ipv6 *)&p);
-
                if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
                        return;
 
@@ -363,17 +412,35 @@ void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
 
                rn = bgp_node_lookup(bgp->connected_table[AFI_IP6],
                                     (struct prefix *)&p);
-               if (!rn)
-                       return;
+       }
 
-               bc = rn->info;
-               bc->refcnt--;
-               if (bc->refcnt == 0) {
-                       XFREE(MTYPE_BGP_CONN, bc);
-                       rn->info = NULL;
-               }
-               bgp_unlock_node(rn);
-               bgp_unlock_node(rn);
+       if (!rn)
+               return;
+
+       bc = bgp_connected_get_node_info(rn);
+       bc->refcnt--;
+       if (bc->refcnt == 0) {
+               XFREE(MTYPE_BGP_CONN, bc);
+               bgp_connected_set_node_info(rn, NULL);
+       }
+       bgp_unlock_node(rn);
+       bgp_unlock_node(rn);
+}
+
+static void bgp_connected_cleanup(struct route_table *table,
+                                 struct route_node *rn)
+{
+       struct bgp_connected_ref *bc;
+       struct bgp_node *bn = bgp_node_from_rnode(rn);
+
+       bc = bgp_connected_get_node_info(bn);
+       if (!bc)
+               return;
+
+       bc->refcnt--;
+       if (bc->refcnt == 0) {
+               XFREE(MTYPE_BGP_CONN, bc);
+               bgp_connected_set_node_info(bn, NULL);
        }
 }
 
@@ -435,7 +502,7 @@ int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
        struct bgp_node *rn1, *rn2;
        struct peer_af *paf;
        struct prefix p, np;
-       struct bgp *bgp = NULL;
+       struct bgp *bgp;
 
        np.family = AF_INET;
        np.prefixlen = IPV4_MAX_BITLEN;
@@ -444,8 +511,6 @@ int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
        p.family = AF_INET;
        p.prefixlen = IPV4_MAX_BITLEN;
 
-       rn1 = rn2 = NULL;
-
        bgp = SUBGRP_INST(subgrp);
        rn1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
        if (!rn1)
@@ -527,35 +592,35 @@ static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, int detail)
 
                for (rn = bgp_table_top(bgp->nexthop_cache_table[afi]); rn;
                     rn = bgp_route_next(rn)) {
-                       if ((bnc = rn->info) != NULL) {
-                               if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
-                                       vty_out(vty,
-                                               " %s valid [IGP metric %d], #paths %d\n",
-                                               inet_ntop(rn->p.family,
-                                                         &rn->p.u.prefix, buf,
-                                                         sizeof(buf)),
-                                               bnc->metric, bnc->path_count);
-
-                                       if (!detail)
-                                               continue;
-
-                                       bgp_show_nexthops_detail(vty, bgp, bnc);
-
-                               } else {
-                                       vty_out(vty, " %s invalid\n",
-                                               inet_ntop(rn->p.family,
-                                                         &rn->p.u.prefix, buf,
-                                                         sizeof(buf)));
-                                       if (CHECK_FLAG(bnc->flags,
-                                                      BGP_NEXTHOP_CONNECTED))
-                                               vty_out(vty,
-                                                       "  Must be Connected\n");
+                       bnc = bgp_nexthop_get_node_info(rn);
+                       if (!bnc)
+                               continue;
+
+                       if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
+                               vty_out(vty,
+                                       " %s valid [IGP metric %d], #paths %d\n",
+                                       inet_ntop(rn->p.family,
+                                                 &rn->p.u.prefix, buf,
+                                                 sizeof(buf)),
+                                       bnc->metric, bnc->path_count);
+
+                               if (!detail)
+                                       continue;
+
+                               bgp_show_nexthops_detail(vty, bgp, bnc);
+
+                       } else {
+                               vty_out(vty, " %s invalid\n",
+                                       inet_ntop(rn->p.family,
+                                                 &rn->p.u.prefix, buf,
+                                                 sizeof(buf)));
+                               if (CHECK_FLAG(bnc->flags,
+                                              BGP_NEXTHOP_CONNECTED))
+                                       vty_out(vty, "  Must be Connected\n");
                                }
-                               tbuf = time(NULL)
-                                      - (bgp_clock() - bnc->last_update);
-                               vty_out(vty, "  Last update: %s", ctime(&tbuf));
-                               vty_out(vty, "\n");
-                       }
+                       tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
+                       vty_out(vty, "  Last update: %s", ctime(&tbuf));
+                       vty_out(vty, "\n");
                }
        }
 }
@@ -632,10 +697,11 @@ void bgp_scan_init(struct bgp *bgp)
 
        for (afi = AFI_IP; afi < AFI_MAX; afi++) {
                bgp->nexthop_cache_table[afi] =
-                       bgp_table_init(afi, SAFI_UNICAST);
-               bgp->connected_table[afi] = bgp_table_init(afi, SAFI_UNICAST);
+                       bgp_table_init(bgp, afi, SAFI_UNICAST);
+               bgp->connected_table[afi] = bgp_table_init(bgp, afi,
+                       SAFI_UNICAST);
                bgp->import_check_table[afi] =
-                       bgp_table_init(afi, SAFI_UNICAST);
+                       bgp_table_init(bgp, afi, SAFI_UNICAST);
        }
 }
 
@@ -655,6 +721,8 @@ void bgp_scan_finish(struct bgp *bgp)
                bgp_table_unlock(bgp->nexthop_cache_table[afi]);
                bgp->nexthop_cache_table[afi] = NULL;
 
+               bgp->connected_table[afi]->route_table->cleanup =
+                       bgp_connected_cleanup;
                bgp_table_unlock(bgp->connected_table[afi]);
                bgp->connected_table[afi] = NULL;