]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_community_alias.c
Merge pull request #7811 from kuldeepkash/multicast_pim_dr_nondr
[mirror_frr.git] / bgpd / bgp_community_alias.c
1 /* BGP community, large-community aliasing.
2 *
3 * Copyright (C) 2021 Donatas Abraitis <donatas.abraitis@gmail.com>
4 *
5 * This file is part of FRRouting (FRR).
6 *
7 * FRR is free software; you can redistribute it and/or modify it under the
8 * terms of the GNU General Public License as published by the Free Software
9 * Foundation; either version 2, or (at your option) any later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "zebra.h"
22
23 #include "memory.h"
24 #include "lib/jhash.h"
25 #include "frrstr.h"
26
27 #include "bgpd/bgpd.h"
28 #include "bgpd/bgp_community_alias.h"
29
30 static struct hash *bgp_ca_alias_hash;
31 static struct hash *bgp_ca_community_hash;
32
33 static unsigned int bgp_ca_community_hash_key(const void *p)
34 {
35 const struct community_alias *ca = p;
36
37 return jhash(ca->community, sizeof(ca->community), 0);
38 }
39
40 static bool bgp_ca_community_hash_cmp(const void *p1, const void *p2)
41 {
42 const struct community_alias *ca1 = p1;
43 const struct community_alias *ca2 = p2;
44
45 return (strcmp(ca1->community, ca2->community) == 0);
46 }
47
48 static unsigned int bgp_ca_alias_hash_key(const void *p)
49 {
50 const struct community_alias *ca = p;
51
52 return jhash(ca->alias, sizeof(ca->alias), 0);
53 }
54
55 static bool bgp_ca_alias_hash_cmp(const void *p1, const void *p2)
56 {
57 const struct community_alias *ca1 = p1;
58 const struct community_alias *ca2 = p2;
59
60 return (strcmp(ca1->alias, ca2->alias) == 0);
61 }
62
63 static void *bgp_community_alias_alloc(void *p)
64 {
65 const struct community_alias *ca = p;
66 struct communtiy_alias *new;
67
68 new = XCALLOC(MTYPE_COMMUNITY_ALIAS, sizeof(struct community_alias));
69 memcpy(new, ca, sizeof(struct community_alias));
70
71 return new;
72 }
73
74 void bgp_community_alias_init(void)
75 {
76 bgp_ca_community_hash = hash_create(bgp_ca_community_hash_key,
77 bgp_ca_community_hash_cmp,
78 "BGP community alias (community)");
79 bgp_ca_alias_hash =
80 hash_create(bgp_ca_alias_hash_key, bgp_ca_alias_hash_cmp,
81 "BGP community alias (alias)");
82 }
83
84 void bgp_community_alias_finish(void)
85 {
86 hash_free(bgp_ca_community_hash);
87 hash_free(bgp_ca_alias_hash);
88 }
89
90 static void bgp_community_alias_show_iterator(struct hash_bucket *hb,
91 struct vty *vty)
92 {
93 struct community_alias *ca = hb->data;
94
95 vty_out(vty, "bgp community alias %s %s\n", ca->community, ca->alias);
96 }
97
98 int bgp_community_alias_write(struct vty *vty)
99 {
100 hash_iterate(bgp_ca_community_hash,
101 (void (*)(struct hash_bucket *,
102 void *))bgp_community_alias_show_iterator,
103 vty);
104 return 1;
105 }
106
107 void bgp_ca_community_insert(struct community_alias *ca)
108 {
109 hash_get(bgp_ca_community_hash, ca, bgp_community_alias_alloc);
110 }
111
112 void bgp_ca_alias_insert(struct community_alias *ca)
113 {
114 hash_get(bgp_ca_alias_hash, ca, bgp_community_alias_alloc);
115 }
116
117 void bgp_ca_community_delete(struct community_alias *ca)
118 {
119 struct community_alias *data = hash_release(bgp_ca_community_hash, ca);
120
121 XFREE(MTYPE_COMMUNITY_ALIAS, data);
122 }
123
124 void bgp_ca_alias_delete(struct community_alias *ca)
125 {
126 struct community_alias *data = hash_release(bgp_ca_alias_hash, ca);
127
128 XFREE(MTYPE_COMMUNITY_ALIAS, data);
129 }
130
131 struct community_alias *bgp_ca_community_lookup(struct community_alias *ca)
132 {
133 return hash_lookup(bgp_ca_community_hash, ca);
134 }
135
136 struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca)
137 {
138 return hash_lookup(bgp_ca_alias_hash, ca);
139 }
140
141 const char *bgp_community2alias(char *community)
142 {
143 struct community_alias ca;
144 struct community_alias *find;
145
146 memset(&ca, 0, sizeof(ca));
147 strlcpy(ca.community, community, sizeof(ca.community));
148
149 find = bgp_ca_community_lookup(&ca);
150 if (find)
151 return find->alias;
152
153 return community;
154 }
155
156 const char *bgp_alias2community(char *alias)
157 {
158 struct community_alias ca;
159 struct community_alias *find;
160
161 memset(&ca, 0, sizeof(ca));
162 strlcpy(ca.alias, alias, sizeof(ca.alias));
163
164 find = bgp_ca_alias_lookup(&ca);
165 if (find)
166 return find->community;
167
168 return alias;
169 }
170
171 /* Communities structs have `->str` which is used
172 * for vty outputs and extended BGP community lists
173 * with regexp.
174 * This is a helper to convert already aliased version
175 * of communities into numerical-only format.
176 */
177 char *bgp_alias2community_str(const char *str)
178 {
179 char **aliases;
180 char *comstr;
181 int num, i;
182
183 frrstr_split(str, " ", &aliases, &num);
184 const char *communities[num];
185
186 for (i = 0; i < num; i++)
187 communities[i] = bgp_alias2community(aliases[i]);
188
189 comstr = frrstr_join(communities, num, " ");
190
191 for (i = 0; i < num; i++)
192 XFREE(MTYPE_TMP, aliases[i]);
193 XFREE(MTYPE_TMP, aliases);
194
195 return comstr;
196 }
197
198 static int bgp_community_alias_vector_walker(struct hash_bucket *bucket,
199 void *data)
200 {
201 vector *comps = data;
202 struct community_alias *alias = bucket->data;
203
204 vector_set(*comps, XSTRDUP(MTYPE_COMPLETION, alias->alias));
205
206 return 1;
207 }
208
209 static void bgp_community_alias_cmd_completion(vector comps,
210 struct cmd_token *token)
211 {
212 hash_walk(bgp_ca_alias_hash, bgp_community_alias_vector_walker, &comps);
213 }
214
215 static const struct cmd_variable_handler community_alias_handlers[] = {
216 {.varname = "alias_name",
217 .completions = bgp_community_alias_cmd_completion},
218 {.tokenname = "ALIAS_NAME",
219 .completions = bgp_community_alias_cmd_completion},
220 {.completions = NULL}};
221
222 void bgp_community_alias_command_completion_setup(void)
223 {
224 cmd_variable_handler_register(community_alias_handlers);
225 }