2023-02-08 13:17:09 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-04-21 21:34:12 +02:00
|
|
|
/* BGP community, large-community aliasing.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Donatas Abraitis <donatas.abraitis@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2022-04-04 18:02:14 +02:00
|
|
|
#include "zebra.h"
|
|
|
|
|
2021-04-21 21:34:12 +02:00
|
|
|
#include "memory.h"
|
|
|
|
#include "lib/jhash.h"
|
2021-08-09 12:34:57 +02:00
|
|
|
#include "frrstr.h"
|
2021-04-21 21:34:12 +02:00
|
|
|
|
|
|
|
#include "bgpd/bgpd.h"
|
|
|
|
#include "bgpd/bgp_community_alias.h"
|
|
|
|
|
2024-11-18 22:29:53 +01:00
|
|
|
struct hash *bgp_ca_alias_hash;
|
2021-04-21 21:34:12 +02:00
|
|
|
static struct hash *bgp_ca_community_hash;
|
|
|
|
|
|
|
|
static unsigned int bgp_ca_community_hash_key(const void *p)
|
|
|
|
{
|
|
|
|
const struct community_alias *ca = p;
|
|
|
|
|
|
|
|
return jhash(ca->community, sizeof(ca->community), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bgp_ca_community_hash_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const struct community_alias *ca1 = p1;
|
|
|
|
const struct community_alias *ca2 = p2;
|
|
|
|
|
2022-02-02 22:34:03 +01:00
|
|
|
return (strcmp(ca1->community, ca2->community) == 0);
|
2021-04-21 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int bgp_ca_alias_hash_key(const void *p)
|
|
|
|
{
|
|
|
|
const struct community_alias *ca = p;
|
|
|
|
|
|
|
|
return jhash(ca->alias, sizeof(ca->alias), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bgp_ca_alias_hash_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
const struct community_alias *ca1 = p1;
|
|
|
|
const struct community_alias *ca2 = p2;
|
|
|
|
|
2022-02-02 22:34:03 +01:00
|
|
|
return (strcmp(ca1->alias, ca2->alias) == 0);
|
2021-04-21 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *bgp_community_alias_alloc(void *p)
|
|
|
|
{
|
|
|
|
const struct community_alias *ca = p;
|
|
|
|
struct communtiy_alias *new;
|
|
|
|
|
|
|
|
new = XCALLOC(MTYPE_COMMUNITY_ALIAS, sizeof(struct community_alias));
|
|
|
|
memcpy(new, ca, sizeof(struct community_alias));
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bgp_community_alias_init(void)
|
|
|
|
{
|
|
|
|
bgp_ca_community_hash = hash_create(bgp_ca_community_hash_key,
|
|
|
|
bgp_ca_community_hash_cmp,
|
|
|
|
"BGP community alias (community)");
|
|
|
|
bgp_ca_alias_hash =
|
|
|
|
hash_create(bgp_ca_alias_hash_key, bgp_ca_alias_hash_cmp,
|
|
|
|
"BGP community alias (alias)");
|
|
|
|
}
|
|
|
|
|
2022-07-16 15:18:07 +02:00
|
|
|
static void bgp_ca_free(void *ca)
|
|
|
|
{
|
|
|
|
XFREE(MTYPE_COMMUNITY_ALIAS, ca);
|
|
|
|
}
|
|
|
|
|
2021-04-21 21:34:12 +02:00
|
|
|
void bgp_community_alias_finish(void)
|
|
|
|
{
|
2023-03-21 13:54:21 +01:00
|
|
|
hash_clean_and_free(&bgp_ca_community_hash, bgp_ca_free);
|
|
|
|
hash_clean_and_free(&bgp_ca_alias_hash, bgp_ca_free);
|
2021-04-21 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bgp_community_alias_show_iterator(struct hash_bucket *hb,
|
|
|
|
struct vty *vty)
|
|
|
|
{
|
|
|
|
struct community_alias *ca = hb->data;
|
|
|
|
|
|
|
|
vty_out(vty, "bgp community alias %s %s\n", ca->community, ca->alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bgp_community_alias_write(struct vty *vty)
|
|
|
|
{
|
|
|
|
hash_iterate(bgp_ca_community_hash,
|
|
|
|
(void (*)(struct hash_bucket *,
|
|
|
|
void *))bgp_community_alias_show_iterator,
|
|
|
|
vty);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bgp_ca_community_insert(struct community_alias *ca)
|
|
|
|
{
|
*: remove the checking returned value for hash_get()
Firstly, *keep no change* for `hash_get()` with NULL
`alloc_func`.
Only focus on cases with non-NULL `alloc_func` of
`hash_get()`.
Since `hash_get()` with non-NULL `alloc_func` parameter
shall not fail, just ignore the returned value of it.
The returned value must not be NULL.
So in this case, remove the unnecessary checking NULL
or not for the returned value and add `void` in front
of it.
Importantly, also *keep no change* for the two cases with
non-NULL `alloc_func` -
1) Use `assert(<returned_data> == <searching_data>)` to
ensure it is a created node, not a found node.
Refer to `isis_vertex_queue_insert()` of isisd, there
are many examples of this case in isid.
2) Use `<returned_data> != <searching_data>` to judge it
is a found node, then free <searching_data>.
Refer to `aspath_intern()` of bgpd, there are many
examples of this case in bgpd.
Here, <returned_data> is the returned value from `hash_get()`,
and <searching_data> is the data, which is to be put into
hash table.
Signed-off-by: anlan_cs <vic.lan@pica8.com>
2022-04-21 08:37:12 +02:00
|
|
|
(void)hash_get(bgp_ca_community_hash, ca, bgp_community_alias_alloc);
|
2021-04-21 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bgp_ca_alias_insert(struct community_alias *ca)
|
|
|
|
{
|
*: remove the checking returned value for hash_get()
Firstly, *keep no change* for `hash_get()` with NULL
`alloc_func`.
Only focus on cases with non-NULL `alloc_func` of
`hash_get()`.
Since `hash_get()` with non-NULL `alloc_func` parameter
shall not fail, just ignore the returned value of it.
The returned value must not be NULL.
So in this case, remove the unnecessary checking NULL
or not for the returned value and add `void` in front
of it.
Importantly, also *keep no change* for the two cases with
non-NULL `alloc_func` -
1) Use `assert(<returned_data> == <searching_data>)` to
ensure it is a created node, not a found node.
Refer to `isis_vertex_queue_insert()` of isisd, there
are many examples of this case in isid.
2) Use `<returned_data> != <searching_data>` to judge it
is a found node, then free <searching_data>.
Refer to `aspath_intern()` of bgpd, there are many
examples of this case in bgpd.
Here, <returned_data> is the returned value from `hash_get()`,
and <searching_data> is the data, which is to be put into
hash table.
Signed-off-by: anlan_cs <vic.lan@pica8.com>
2022-04-21 08:37:12 +02:00
|
|
|
(void)hash_get(bgp_ca_alias_hash, ca, bgp_community_alias_alloc);
|
2021-04-21 21:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bgp_ca_community_delete(struct community_alias *ca)
|
|
|
|
{
|
|
|
|
struct community_alias *data = hash_release(bgp_ca_community_hash, ca);
|
|
|
|
|
|
|
|
XFREE(MTYPE_COMMUNITY_ALIAS, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bgp_ca_alias_delete(struct community_alias *ca)
|
|
|
|
{
|
|
|
|
struct community_alias *data = hash_release(bgp_ca_alias_hash, ca);
|
|
|
|
|
|
|
|
XFREE(MTYPE_COMMUNITY_ALIAS, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct community_alias *bgp_ca_community_lookup(struct community_alias *ca)
|
|
|
|
{
|
|
|
|
return hash_lookup(bgp_ca_community_hash, ca);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca)
|
|
|
|
{
|
|
|
|
return hash_lookup(bgp_ca_alias_hash, ca);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *bgp_community2alias(char *community)
|
|
|
|
{
|
|
|
|
struct community_alias ca;
|
|
|
|
struct community_alias *find;
|
|
|
|
|
|
|
|
memset(&ca, 0, sizeof(ca));
|
|
|
|
strlcpy(ca.community, community, sizeof(ca.community));
|
|
|
|
|
|
|
|
find = bgp_ca_community_lookup(&ca);
|
|
|
|
if (find)
|
|
|
|
return find->alias;
|
|
|
|
|
|
|
|
return community;
|
|
|
|
}
|
2021-07-16 15:52:53 +02:00
|
|
|
|
2021-08-09 12:34:57 +02:00
|
|
|
const char *bgp_alias2community(char *alias)
|
|
|
|
{
|
|
|
|
struct community_alias ca;
|
|
|
|
struct community_alias *find;
|
|
|
|
|
|
|
|
memset(&ca, 0, sizeof(ca));
|
|
|
|
strlcpy(ca.alias, alias, sizeof(ca.alias));
|
|
|
|
|
|
|
|
find = bgp_ca_alias_lookup(&ca);
|
|
|
|
if (find)
|
|
|
|
return find->community;
|
|
|
|
|
|
|
|
return alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Communities structs have `->str` which is used
|
|
|
|
* for vty outputs and extended BGP community lists
|
|
|
|
* with regexp.
|
|
|
|
* This is a helper to convert already aliased version
|
|
|
|
* of communities into numerical-only format.
|
|
|
|
*/
|
2021-08-11 11:09:15 +02:00
|
|
|
char *bgp_alias2community_str(const char *str)
|
2021-08-09 12:34:57 +02:00
|
|
|
{
|
|
|
|
char **aliases;
|
2021-08-11 11:09:15 +02:00
|
|
|
char *comstr;
|
|
|
|
int num, i;
|
2021-08-09 12:34:57 +02:00
|
|
|
|
|
|
|
frrstr_split(str, " ", &aliases, &num);
|
2021-08-11 11:09:15 +02:00
|
|
|
const char *communities[num];
|
2021-08-09 12:34:57 +02:00
|
|
|
|
2021-08-11 11:09:15 +02:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
communities[i] = bgp_alias2community(aliases[i]);
|
|
|
|
|
|
|
|
comstr = frrstr_join(communities, num, " ");
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++)
|
2021-08-09 12:34:57 +02:00
|
|
|
XFREE(MTYPE_TMP, aliases[i]);
|
|
|
|
XFREE(MTYPE_TMP, aliases);
|
|
|
|
|
2021-08-11 11:09:15 +02:00
|
|
|
return comstr;
|
2021-08-09 12:34:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 15:52:53 +02:00
|
|
|
static int bgp_community_alias_vector_walker(struct hash_bucket *bucket,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
vector *comps = data;
|
|
|
|
struct community_alias *alias = bucket->data;
|
|
|
|
|
|
|
|
vector_set(*comps, XSTRDUP(MTYPE_COMPLETION, alias->alias));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bgp_community_alias_cmd_completion(vector comps,
|
|
|
|
struct cmd_token *token)
|
|
|
|
{
|
|
|
|
hash_walk(bgp_ca_alias_hash, bgp_community_alias_vector_walker, &comps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct cmd_variable_handler community_alias_handlers[] = {
|
|
|
|
{.varname = "alias_name",
|
|
|
|
.completions = bgp_community_alias_cmd_completion},
|
|
|
|
{.tokenname = "ALIAS_NAME",
|
|
|
|
.completions = bgp_community_alias_cmd_completion},
|
|
|
|
{.completions = NULL}};
|
|
|
|
|
|
|
|
void bgp_community_alias_command_completion_setup(void)
|
|
|
|
{
|
|
|
|
cmd_variable_handler_register(community_alias_handlers);
|
|
|
|
}
|