1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP community, large-community aliasing.
*
* Copyright (C) 2021 Donatas Abraitis <donatas.abraitis@gmail.com>
*/
#include "zebra.h"
#include "memory.h"
#include "lib/jhash.h"
#include "frrstr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_community_alias.h"
static struct hash *bgp_ca_alias_hash;
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;
return (strcmp(ca1->community, ca2->community) == 0);
}
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;
return (strcmp(ca1->alias, ca2->alias) == 0);
}
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)");
}
static void bgp_ca_free(void *ca)
{
XFREE(MTYPE_COMMUNITY_ALIAS, ca);
}
void bgp_community_alias_finish(void)
{
hash_clean_and_free(&bgp_ca_community_hash, bgp_ca_free);
hash_clean_and_free(&bgp_ca_alias_hash, bgp_ca_free);
}
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)
{
(void)hash_get(bgp_ca_community_hash, ca, bgp_community_alias_alloc);
}
void bgp_ca_alias_insert(struct community_alias *ca)
{
(void)hash_get(bgp_ca_alias_hash, ca, bgp_community_alias_alloc);
}
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;
}
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.
*/
char *bgp_alias2community_str(const char *str)
{
char **aliases;
char *comstr;
int num, i;
frrstr_split(str, " ", &aliases, &num);
const char *communities[num];
for (i = 0; i < num; i++)
communities[i] = bgp_alias2community(aliases[i]);
comstr = frrstr_join(communities, num, " ");
for (i = 0; i < num; i++)
XFREE(MTYPE_TMP, aliases[i]);
XFREE(MTYPE_TMP, aliases);
return comstr;
}
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);
}
|