]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_mac.c
Merge pull request #7481 from donaldsharp/memory_options_are_long
[mirror_frr.git] / bgpd / bgp_mac.c
1 /*
2 * BGPd - Mac hash code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more 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 #include <zebra.h>
21
22 #include <jhash.h>
23 #include <hash.h>
24 #include <prefix.h>
25 #include <memory.h>
26
27 #include "bgpd/bgpd.h"
28 #include "bgpd/bgp_mac.h"
29 #include "bgpd/bgp_memory.h"
30 #include "bgpd/bgp_route.h"
31 #include "bgpd/bgp_packet.h"
32 #include "bgpd/bgp_rd.h"
33 #include "bgpd/bgp_debug.h"
34 #include "bgpd/bgp_evpn_private.h"
35
36 DEFINE_MTYPE_STATIC(BGPD, BSM, "Mac Hash Entry");
37 DEFINE_MTYPE_STATIC(BGPD, BSM_STRING, "Mac Hash Entry Intf String");
38
39 struct bgp_self_mac {
40 struct ethaddr macaddr;
41 struct list *ifp_list;
42 };
43
44 static unsigned int bgp_mac_hash_key_make(const void *data)
45 {
46 const struct bgp_self_mac *bsm = data;
47
48 return jhash(&bsm->macaddr, ETH_ALEN, 0xa5a5dead);
49 }
50
51 static bool bgp_mac_hash_cmp(const void *d1, const void *d2)
52 {
53 const struct bgp_self_mac *bsm1 = d1;
54 const struct bgp_self_mac *bsm2 = d2;
55
56 if (memcmp(&bsm1->macaddr, &bsm2->macaddr, ETH_ALEN) == 0)
57 return true;
58
59 return false;
60 }
61
62 void bgp_mac_init(void)
63 {
64 bm->self_mac_hash = hash_create(bgp_mac_hash_key_make, bgp_mac_hash_cmp,
65 "BGP MAC Hash");
66 }
67
68 static void bgp_mac_hash_free(void *data)
69 {
70 struct bgp_self_mac *bsm = data;
71
72 if (bsm->ifp_list)
73 list_delete(&bsm->ifp_list);
74
75 XFREE(MTYPE_BSM, bsm);
76 }
77
78 void bgp_mac_finish(void)
79 {
80 hash_clean(bm->self_mac_hash, bgp_mac_hash_free);
81 hash_free(bm->self_mac_hash);
82 }
83
84 static void bgp_mac_hash_interface_string_del(void *val)
85 {
86 char *data = val;
87
88 XFREE(MTYPE_BSM_STRING, data);
89 }
90
91 static void *bgp_mac_hash_alloc(void *p)
92 {
93 const struct bgp_self_mac *orig = p;
94 struct bgp_self_mac *bsm;
95
96 bsm = XCALLOC(MTYPE_BSM, sizeof(struct bgp_self_mac));
97 memcpy(&bsm->macaddr, &orig->macaddr, ETH_ALEN);
98
99 bsm->ifp_list = list_new();
100 bsm->ifp_list->del = bgp_mac_hash_interface_string_del;
101
102 return bsm;
103 }
104
105 struct bgp_mac_find_internal {
106 struct bgp_self_mac *bsm;
107 const char *ifname;
108 };
109
110 static void bgp_mac_find_ifp_internal(struct hash_bucket *bucket, void *arg)
111 {
112 struct bgp_mac_find_internal *bmfi = arg;
113 struct bgp_self_mac *bsm = bucket->data;
114 struct listnode *node;
115 char *name;
116
117 for (ALL_LIST_ELEMENTS_RO(bsm->ifp_list, node, name)) {
118 if (strcmp(name, bmfi->ifname) == 0) {
119 bmfi->bsm = bsm;
120 return;
121 }
122 }
123 }
124
125 static struct bgp_self_mac *bgp_mac_find_interface_name(const char *ifname)
126 {
127 struct bgp_mac_find_internal bmfi;
128
129 bmfi.bsm = NULL;
130 bmfi.ifname = ifname;
131 hash_iterate(bm->self_mac_hash, bgp_mac_find_ifp_internal, &bmfi);
132
133 return bmfi.bsm;
134 }
135
136 static void bgp_process_mac_rescan_table(struct bgp *bgp, struct peer *peer,
137 struct bgp_table *table,
138 struct ethaddr *macaddr)
139 {
140 struct bgp_dest *pdest, *dest;
141 struct bgp_path_info *pi;
142
143 for (pdest = bgp_table_top(table); pdest;
144 pdest = bgp_route_next(pdest)) {
145 struct bgp_table *sub = pdest->info;
146 const struct prefix *pdest_p = bgp_dest_get_prefix(pdest);
147
148 if (!sub)
149 continue;
150
151 for (dest = bgp_table_top(sub); dest;
152 dest = bgp_route_next(dest)) {
153 bool dest_affected;
154 const struct prefix *p = bgp_dest_get_prefix(dest);
155 struct prefix_evpn *pevpn = (struct prefix_evpn *)dest;
156 struct prefix_rd prd;
157 uint32_t num_labels = 0;
158 mpls_label_t *label_pnt = NULL;
159 struct bgp_route_evpn evpn;
160
161 if (pevpn->family == AF_EVPN
162 && pevpn->prefix.route_type == BGP_EVPN_MAC_IP_ROUTE
163 && memcmp(&p->u.prefix_evpn.macip_addr.mac, macaddr,
164 ETH_ALEN)
165 == 0)
166 dest_affected = true;
167 else
168 dest_affected = false;
169
170 for (pi = dest->info; pi; pi = pi->next) {
171 if (pi->peer == peer)
172 break;
173 }
174
175 if (!pi)
176 continue;
177
178 /*
179 * If the mac address is not the same then
180 * we don't care and since we are looking
181 */
182 if ((memcmp(&pi->attr->rmac, macaddr, ETH_ALEN) != 0)
183 && !dest_affected)
184 continue;
185
186 if (pi->extra)
187 num_labels = pi->extra->num_labels;
188 if (num_labels)
189 label_pnt = &pi->extra->label[0];
190
191 prd.family = AF_UNSPEC;
192 prd.prefixlen = 64;
193 memcpy(&prd.val, pdest_p->u.val, 8);
194
195 if (CHECK_FLAG(pi->flags, BGP_PATH_REMOVED)) {
196 if (bgp_debug_update(peer, p, NULL, 1)) {
197 char pfx_buf[BGP_PRD_PATH_STRLEN];
198
199 bgp_debug_rdpfxpath2str(
200 AFI_L2VPN, SAFI_EVPN, &prd,
201 p, label_pnt, num_labels,
202 pi->addpath_rx_id ? 1 : 0,
203 pi->addpath_rx_id, pfx_buf,
204 sizeof(pfx_buf));
205 zlog_debug(
206 "%s skip update of %s marked as removed",
207 peer->host, pfx_buf);
208 }
209 continue;
210 }
211
212 memcpy(&evpn, &pi->attr->evpn_overlay, sizeof(evpn));
213 int32_t ret = bgp_update(peer, p,
214 pi->addpath_rx_id,
215 pi->attr, AFI_L2VPN, SAFI_EVPN,
216 ZEBRA_ROUTE_BGP,
217 BGP_ROUTE_NORMAL, &prd,
218 label_pnt, num_labels,
219 1, &evpn);
220
221 if (ret < 0)
222 bgp_dest_unlock_node(dest);
223 }
224 }
225 }
226
227 static void bgp_mac_rescan_evpn_table(struct bgp *bgp, struct ethaddr *macaddr)
228 {
229 struct listnode *node;
230 struct peer *peer;
231 safi_t safi;
232 afi_t afi;
233
234 afi = AFI_L2VPN;
235 safi = SAFI_EVPN;
236 for (ALL_LIST_ELEMENTS_RO(bgp->peer, node, peer)) {
237
238 if (CHECK_FLAG(peer->sflags, PEER_STATUS_GROUP))
239 continue;
240
241 if (peer->status != Established)
242 continue;
243
244 if (CHECK_FLAG(peer->af_flags[afi][safi],
245 PEER_FLAG_SOFT_RECONFIG)) {
246 if (bgp_debug_update(peer, NULL, NULL, 1))
247 zlog_debug("Processing EVPN MAC interface change on peer %s (inbound, soft-reconfig)",
248 peer->host);
249
250 bgp_soft_reconfig_in(peer, afi, safi);
251 } else {
252 struct bgp_table *table = bgp->rib[afi][safi];
253
254 if (bgp_debug_update(peer, NULL, NULL, 1))
255 zlog_debug("Processing EVPN MAC interface change on peer %s",
256 peer->host);
257 bgp_process_mac_rescan_table(bgp, peer, table, macaddr);
258 }
259 }
260 }
261
262 static void bgp_mac_rescan_all_evpn_tables(struct ethaddr *macaddr)
263 {
264 struct listnode *node;
265 struct bgp *bgp;
266
267 for (ALL_LIST_ELEMENTS_RO(bm->bgp, node, bgp)) {
268 struct bgp_table *table = bgp->rib[AFI_L2VPN][SAFI_EVPN];
269
270 if (table)
271 bgp_mac_rescan_evpn_table(bgp, macaddr);
272 }
273 }
274
275 static void bgp_mac_remove_ifp_internal(struct bgp_self_mac *bsm, char *ifname,
276 struct ethaddr *macaddr)
277 {
278 struct listnode *node = NULL;
279 char *name;
280
281 for (ALL_LIST_ELEMENTS_RO(bsm->ifp_list, node, name)) {
282 if (strcmp(name, ifname) == 0)
283 break;
284 }
285
286 if (node) {
287 list_delete_node(bsm->ifp_list, node);
288 XFREE(MTYPE_BSM_STRING, name);
289 }
290
291 if (bsm->ifp_list->count == 0) {
292 struct ethaddr mac = *macaddr;
293
294 hash_release(bm->self_mac_hash, bsm);
295 list_delete(&bsm->ifp_list);
296 XFREE(MTYPE_BSM, bsm);
297
298 bgp_mac_rescan_all_evpn_tables(&mac);
299 }
300 }
301
302 void bgp_mac_add_mac_entry(struct interface *ifp)
303 {
304 struct bgp_self_mac lookup;
305 struct bgp_self_mac *bsm;
306 struct bgp_self_mac *old_bsm;
307 char *ifname;
308
309 memcpy(&lookup.macaddr, &ifp->hw_addr, ETH_ALEN);
310 bsm = hash_get(bm->self_mac_hash, &lookup, bgp_mac_hash_alloc);
311
312 /*
313 * Does this happen to be a move
314 */
315 old_bsm = bgp_mac_find_interface_name(ifp->name);
316 ifname = XSTRDUP(MTYPE_BSM_STRING, ifp->name);
317
318 if (bsm->ifp_list->count == 0) {
319
320 listnode_add(bsm->ifp_list, ifname);
321 if (old_bsm)
322 bgp_mac_remove_ifp_internal(old_bsm, ifname,
323 &old_bsm->macaddr);
324 } else {
325 /*
326 * If old mac address is the same as the new,
327 * then there is nothing to do here
328 */
329 if (old_bsm == bsm) {
330 XFREE(MTYPE_BSM_STRING, ifname);
331 return;
332 }
333
334 if (old_bsm)
335 bgp_mac_remove_ifp_internal(old_bsm, ifp->name,
336 &old_bsm->macaddr);
337
338 listnode_add(bsm->ifp_list, ifname);
339 }
340
341 bgp_mac_rescan_all_evpn_tables(&bsm->macaddr);
342 }
343
344 void bgp_mac_del_mac_entry(struct interface *ifp)
345 {
346 struct bgp_self_mac lookup;
347 struct bgp_self_mac *bsm;
348
349 memcpy(&lookup.macaddr, &ifp->hw_addr, ETH_ALEN);
350 bsm = hash_lookup(bm->self_mac_hash, &lookup);
351 if (!bsm)
352 return;
353
354 /*
355 * Write code to allow old mac address to no-longer
356 * win if we happen to have received it from a peer.
357 */
358 bgp_mac_remove_ifp_internal(bsm, ifp->name, &bsm->macaddr);
359 }
360
361 /* This API checks MAC address against any of local
362 * assigned (SVIs) MAC address.
363 * An example: router-mac attribute in any of evpn update
364 * requires to compare against local mac.
365 */
366 bool bgp_mac_exist(const struct ethaddr *mac)
367 {
368 struct bgp_self_mac lookup;
369 struct bgp_self_mac *bsm;
370 static uint8_t tmp [ETHER_ADDR_STRLEN] = {0};
371
372 if (memcmp(mac, &tmp, ETH_ALEN) == 0)
373 return false;
374
375 memcpy(&lookup.macaddr, mac, ETH_ALEN);
376 bsm = hash_lookup(bm->self_mac_hash, &lookup);
377 if (!bsm)
378 return false;
379
380 return true;
381 }
382
383 /* This API checks EVPN type-2 prefix and comapares
384 * mac against any of local assigned (SVIs) MAC
385 * address.
386 */
387 bool bgp_mac_entry_exists(const struct prefix *p)
388 {
389 const struct prefix_evpn *pevpn = (const struct prefix_evpn *)p;
390
391 if (pevpn->family != AF_EVPN)
392 return false;
393
394 if (pevpn->prefix.route_type != BGP_EVPN_MAC_IP_ROUTE)
395 return false;
396
397 return bgp_mac_exist(&p->u.prefix_evpn.macip_addr.mac);
398
399 return true;
400 }
401
402 static void bgp_mac_show_mac_entry(struct hash_bucket *bucket, void *arg)
403 {
404 struct vty *vty = arg;
405 struct bgp_self_mac *bsm = bucket->data;
406 struct listnode *node;
407 char *name;
408 char buf_mac[ETHER_ADDR_STRLEN];
409
410 vty_out(vty, "Mac Address: %s ",
411 prefix_mac2str(&bsm->macaddr, buf_mac, sizeof(buf_mac)));
412
413 for (ALL_LIST_ELEMENTS_RO(bsm->ifp_list, node, name))
414 vty_out(vty, "%s ", name);
415
416 vty_out(vty, "\n");
417 }
418
419 void bgp_mac_dump_table(struct vty *vty)
420 {
421 hash_iterate(bm->self_mac_hash, bgp_mac_show_mac_entry, vty);
422 }