]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_community_alias.c
Merge pull request #9068 from ton31337/fix/reduce_converge_time
[mirror_frr.git] / bgpd / bgp_community_alias.c
CommitLineData
ed0e57e3
DA
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 "memory.h"
22#include "lib/jhash.h"
23
24#include "bgpd/bgpd.h"
25#include "bgpd/bgp_community_alias.h"
26
27static struct hash *bgp_ca_alias_hash;
28static struct hash *bgp_ca_community_hash;
29
30static unsigned int bgp_ca_community_hash_key(const void *p)
31{
32 const struct community_alias *ca = p;
33
34 return jhash(ca->community, sizeof(ca->community), 0);
35}
36
37static bool bgp_ca_community_hash_cmp(const void *p1, const void *p2)
38{
39 const struct community_alias *ca1 = p1;
40 const struct community_alias *ca2 = p2;
41
42 return (strncmp(ca1->community, ca2->community,
43 sizeof(struct community_alias))
44 == 0);
45}
46
47static unsigned int bgp_ca_alias_hash_key(const void *p)
48{
49 const struct community_alias *ca = p;
50
51 return jhash(ca->alias, sizeof(ca->alias), 0);
52}
53
54static bool bgp_ca_alias_hash_cmp(const void *p1, const void *p2)
55{
56 const struct community_alias *ca1 = p1;
57 const struct community_alias *ca2 = p2;
58
59 return (strncmp(ca1->alias, ca2->alias, sizeof(struct community_alias))
60 == 0);
61}
62
63static 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
74void 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
84void bgp_community_alias_finish(void)
85{
86 hash_free(bgp_ca_community_hash);
87 hash_free(bgp_ca_alias_hash);
88}
89
90static 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
98int 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
107void bgp_ca_community_insert(struct community_alias *ca)
108{
109 hash_get(bgp_ca_community_hash, ca, bgp_community_alias_alloc);
110}
111
112void bgp_ca_alias_insert(struct community_alias *ca)
113{
114 hash_get(bgp_ca_alias_hash, ca, bgp_community_alias_alloc);
115}
116
117void 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
124void 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
131struct community_alias *bgp_ca_community_lookup(struct community_alias *ca)
132{
133 return hash_lookup(bgp_ca_community_hash, ca);
134}
135
136struct community_alias *bgp_ca_alias_lookup(struct community_alias *ca)
137{
138 return hash_lookup(bgp_ca_alias_hash, ca);
139}
140
141const 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}