]>
Commit | Line | Data |
---|---|---|
24268cd0 PR |
1 | /* |
2 | * Zebra EVPN for VxLAN code | |
3 | * Copyright (C) 2016, 2017 Cumulus Networks, Inc. | |
4 | * | |
5 | * This file is part of FRR. | |
6 | * | |
7 | * FRR is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * FRR is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with FRR; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | #include "hash.h" | |
b2998086 | 26 | #include "interface.h" |
24268cd0 | 27 | #include "jhash.h" |
24268cd0 PR |
28 | #include "memory.h" |
29 | #include "prefix.h" | |
24268cd0 | 30 | #include "vlan.h" |
b2998086 | 31 | #include "json.h" |
24268cd0 | 32 | |
b2998086 | 33 | #include "zebra/zserv.h" |
24268cd0 | 34 | #include "zebra/debug.h" |
b2998086 | 35 | #include "zebra/zebra_router.h" |
24268cd0 | 36 | #include "zebra/zebra_memory.h" |
b2998086 | 37 | #include "zebra/zebra_errors.h" |
24268cd0 | 38 | #include "zebra/zebra_vrf.h" |
b2998086 | 39 | #include "zebra/zebra_evpn.h" |
24268cd0 | 40 | #include "zebra/zebra_evpn_mh.h" |
b2998086 PR |
41 | #include "zebra/zebra_evpn_mac.h" |
42 | #include "zebra/zebra_evpn_neigh.h" | |
24268cd0 | 43 | |
24268cd0 | 44 | DEFINE_MTYPE_STATIC(ZEBRA, MAC, "EVPN MAC"); |
24268cd0 PR |
45 | |
46 | /* | |
47 | * Return number of valid MACs in an EVPN's MAC hash table - all | |
48 | * remote MACs and non-internal (auto) local MACs count. | |
49 | */ | |
b2998086 | 50 | uint32_t num_valid_macs(zebra_evpn_t *zevpn) |
24268cd0 PR |
51 | { |
52 | unsigned int i; | |
53 | uint32_t num_macs = 0; | |
54 | struct hash *hash; | |
55 | struct hash_bucket *hb; | |
56 | zebra_mac_t *mac; | |
57 | ||
58 | hash = zevpn->mac_table; | |
59 | if (!hash) | |
60 | return num_macs; | |
61 | for (i = 0; i < hash->size; i++) { | |
62 | for (hb = hash->index[i]; hb; hb = hb->next) { | |
63 | mac = (zebra_mac_t *)hb->data; | |
64 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) | |
65 | || CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL) | |
66 | || !CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) | |
67 | num_macs++; | |
68 | } | |
69 | } | |
70 | ||
71 | return num_macs; | |
72 | } | |
73 | ||
b2998086 | 74 | uint32_t num_dup_detected_macs(zebra_evpn_t *zevpn) |
24268cd0 PR |
75 | { |
76 | unsigned int i; | |
77 | uint32_t num_macs = 0; | |
78 | struct hash *hash; | |
79 | struct hash_bucket *hb; | |
80 | zebra_mac_t *mac; | |
81 | ||
82 | hash = zevpn->mac_table; | |
83 | if (!hash) | |
84 | return num_macs; | |
85 | for (i = 0; i < hash->size; i++) { | |
86 | for (hb = hash->index[i]; hb; hb = hb->next) { | |
87 | mac = (zebra_mac_t *)hb->data; | |
88 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) | |
89 | num_macs++; | |
90 | } | |
91 | } | |
92 | ||
93 | return num_macs; | |
94 | } | |
95 | ||
b2998086 PR |
96 | /* |
97 | * Install remote MAC into the forwarding plane. | |
98 | */ | |
99 | int zebra_evpn_rem_mac_install(zebra_evpn_t *zevpn, zebra_mac_t *mac, | |
100 | bool was_static) | |
24268cd0 | 101 | { |
b2998086 PR |
102 | const struct zebra_if *zif, *br_zif; |
103 | const struct zebra_l2info_vxlan *vxl; | |
104 | bool sticky; | |
105 | enum zebra_dplane_result res; | |
106 | const struct interface *br_ifp; | |
107 | vlanid_t vid; | |
108 | uint32_t nhg_id; | |
109 | struct in_addr vtep_ip; | |
24268cd0 | 110 | |
b2998086 PR |
111 | if (!(mac->flags & ZEBRA_MAC_REMOTE)) |
112 | return 0; | |
113 | ||
114 | zif = zevpn->vxlan_if->info; | |
115 | if (!zif) | |
116 | return -1; | |
117 | ||
118 | br_ifp = zif->brslave_info.br_if; | |
119 | if (br_ifp == NULL) | |
120 | return -1; | |
121 | ||
122 | vxl = &zif->l2info.vxl; | |
123 | ||
124 | sticky = !!CHECK_FLAG(mac->flags, | |
125 | (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW)); | |
126 | ||
127 | /* If nexthop group for the FDB entry is inactive (not programmed in | |
128 | * the dataplane) the MAC entry cannot be installed | |
129 | */ | |
130 | if (mac->es) { | |
131 | if (!(mac->es->flags & ZEBRA_EVPNES_NHG_ACTIVE)) | |
132 | return -1; | |
133 | nhg_id = mac->es->nhg_id; | |
134 | vtep_ip.s_addr = 0; | |
135 | } else { | |
136 | nhg_id = 0; | |
137 | vtep_ip = mac->fwd_info.r_vtep_ip; | |
24268cd0 PR |
138 | } |
139 | ||
b2998086 PR |
140 | br_zif = (const struct zebra_if *)(br_ifp->info); |
141 | ||
142 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif)) | |
143 | vid = vxl->access_vlan; | |
144 | else | |
145 | vid = 0; | |
146 | ||
147 | res = dplane_rem_mac_add(zevpn->vxlan_if, br_ifp, vid, &mac->macaddr, | |
148 | vtep_ip, sticky, nhg_id, was_static); | |
149 | if (res != ZEBRA_DPLANE_REQUEST_FAILURE) | |
150 | return 0; | |
151 | else | |
152 | return -1; | |
24268cd0 PR |
153 | } |
154 | ||
b2998086 PR |
155 | /* |
156 | * Uninstall remote MAC from the forwarding plane. | |
157 | */ | |
158 | int zebra_evpn_rem_mac_uninstall(zebra_evpn_t *zevpn, zebra_mac_t *mac) | |
24268cd0 | 159 | { |
b2998086 PR |
160 | const struct zebra_if *zif, *br_zif; |
161 | const struct zebra_l2info_vxlan *vxl; | |
162 | struct in_addr vtep_ip; | |
163 | const struct interface *ifp, *br_ifp; | |
164 | vlanid_t vid; | |
165 | enum zebra_dplane_result res; | |
24268cd0 | 166 | |
b2998086 PR |
167 | if (!(mac->flags & ZEBRA_MAC_REMOTE)) |
168 | return 0; | |
169 | ||
170 | if (!zevpn->vxlan_if) { | |
171 | if (IS_ZEBRA_DEBUG_VXLAN) | |
172 | zlog_debug( | |
173 | "VNI %u hash %p couldn't be uninstalled - no intf", | |
174 | zevpn->vni, zevpn); | |
175 | return -1; | |
176 | } | |
24268cd0 | 177 | |
b2998086 PR |
178 | zif = zevpn->vxlan_if->info; |
179 | if (!zif) | |
180 | return -1; | |
24268cd0 | 181 | |
b2998086 PR |
182 | br_ifp = zif->brslave_info.br_if; |
183 | if (br_ifp == NULL) | |
184 | return -1; | |
24268cd0 | 185 | |
b2998086 | 186 | vxl = &zif->l2info.vxl; |
24268cd0 | 187 | |
b2998086 | 188 | br_zif = (const struct zebra_if *)br_ifp->info; |
24268cd0 | 189 | |
b2998086 PR |
190 | if (IS_ZEBRA_IF_BRIDGE_VLAN_AWARE(br_zif)) |
191 | vid = vxl->access_vlan; | |
192 | else | |
193 | vid = 0; | |
24268cd0 | 194 | |
b2998086 PR |
195 | ifp = zevpn->vxlan_if; |
196 | vtep_ip = mac->fwd_info.r_vtep_ip; | |
197 | ||
198 | res = dplane_rem_mac_del(ifp, br_ifp, vid, &mac->macaddr, vtep_ip); | |
199 | if (res != ZEBRA_DPLANE_REQUEST_FAILURE) | |
200 | return 0; | |
201 | else | |
202 | return -1; | |
24268cd0 PR |
203 | } |
204 | ||
b2998086 PR |
205 | /* |
206 | * Decrement neighbor refcount of MAC; uninstall and free it if | |
207 | * appropriate. | |
24268cd0 | 208 | */ |
b2998086 | 209 | void zebra_evpn_deref_ip2mac(zebra_evpn_t *zevpn, zebra_mac_t *mac) |
24268cd0 | 210 | { |
b2998086 PR |
211 | if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) |
212 | return; | |
24268cd0 | 213 | |
b2998086 PR |
214 | /* If all remote neighbors referencing a remote MAC go away, |
215 | * we need to uninstall the MAC. | |
24268cd0 | 216 | */ |
b2998086 PR |
217 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) |
218 | && remote_neigh_count(mac) == 0) { | |
219 | zebra_evpn_rem_mac_uninstall(zevpn, mac); | |
220 | zebra_evpn_es_mac_deref_entry(mac); | |
221 | UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE); | |
222 | } | |
223 | ||
224 | /* If no neighbors, delete the MAC. */ | |
225 | if (list_isempty(mac->neigh_list)) | |
226 | zebra_evpn_mac_del(zevpn, mac); | |
227 | } | |
228 | ||
d9d3455e PR |
229 | static void zebra_evpn_mac_get_access_info(zebra_mac_t *mac, |
230 | struct interface **ifpP, | |
231 | vlanid_t *vid) | |
b2998086 PR |
232 | { |
233 | /* if the mac is associated with an ES we must get the access | |
234 | * info from the ES | |
24268cd0 | 235 | */ |
b2998086 PR |
236 | if (mac->es) { |
237 | struct zebra_if *zif; | |
238 | ||
239 | /* get the access port from the es */ | |
240 | *ifpP = mac->es->zif ? mac->es->zif->ifp : NULL; | |
241 | /* get the vlan from the EVPN */ | |
242 | if (mac->zevpn->vxlan_if) { | |
243 | zif = mac->zevpn->vxlan_if->info; | |
244 | *vid = zif->l2info.vxl.access_vlan; | |
245 | } else { | |
246 | *vid = 0; | |
247 | } | |
248 | } else { | |
249 | struct zebra_ns *zns; | |
250 | ||
251 | *vid = mac->fwd_info.local.vid; | |
252 | zns = zebra_ns_lookup(NS_DEFAULT); | |
253 | *ifpP = if_lookup_by_index_per_ns(zns, | |
254 | mac->fwd_info.local.ifindex); | |
255 | } | |
256 | } | |
257 | ||
258 | static int zebra_evpn_dad_mac_auto_recovery_exp(struct thread *t) | |
259 | { | |
260 | struct zebra_vrf *zvrf = NULL; | |
261 | zebra_mac_t *mac = NULL; | |
262 | zebra_evpn_t *zevpn = NULL; | |
263 | struct listnode *node = NULL; | |
264 | zebra_neigh_t *nbr = NULL; | |
265 | char buf[ETHER_ADDR_STRLEN]; | |
266 | ||
267 | mac = THREAD_ARG(t); | |
268 | ||
269 | /* since this is asynchronous we need sanity checks*/ | |
270 | zvrf = vrf_info_lookup(mac->zevpn->vrf_id); | |
271 | if (!zvrf) | |
272 | return 0; | |
273 | ||
274 | zevpn = zevpn_lookup(mac->zevpn->vni); | |
275 | if (!zevpn) | |
276 | return 0; | |
277 | ||
278 | mac = zebra_evpn_mac_lookup(zevpn, &mac->macaddr); | |
279 | if (!mac) | |
280 | return 0; | |
281 | ||
282 | if (IS_ZEBRA_DEBUG_VXLAN) | |
283 | zlog_debug( | |
284 | "%s: duplicate addr mac %s flags 0x%x learn count %u host count %u auto recovery expired", | |
285 | __func__, | |
286 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
287 | mac->flags, mac->dad_count, listcount(mac->neigh_list)); | |
288 | ||
289 | /* Remove all IPs as duplicate associcated with this MAC */ | |
290 | for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) { | |
291 | if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE)) { | |
292 | if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) | |
293 | ZEBRA_NEIGH_SET_INACTIVE(nbr); | |
294 | else if (CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_REMOTE)) | |
295 | zebra_evpn_rem_neigh_install( | |
296 | zevpn, nbr, false /*was_static*/); | |
297 | } | |
24268cd0 | 298 | |
24268cd0 PR |
299 | UNSET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE); |
300 | nbr->dad_count = 0; | |
301 | nbr->detect_start_time.tv_sec = 0; | |
b2998086 PR |
302 | nbr->dad_dup_detect_time = 0; |
303 | } | |
304 | ||
305 | UNSET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE); | |
306 | mac->dad_count = 0; | |
307 | mac->detect_start_time.tv_sec = 0; | |
308 | mac->detect_start_time.tv_usec = 0; | |
309 | mac->dad_dup_detect_time = 0; | |
310 | mac->dad_mac_auto_recovery_timer = NULL; | |
311 | ||
312 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { | |
313 | /* Inform to BGP */ | |
314 | if (zebra_evpn_mac_send_add_to_client(zevpn->vni, &mac->macaddr, | |
315 | mac->flags, mac->loc_seq, | |
316 | mac->es)) | |
317 | return -1; | |
318 | ||
319 | /* Process all neighbors associated with this MAC. */ | |
320 | zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0, | |
321 | 0 /*es_change*/); | |
322 | ||
323 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) { | |
324 | zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac); | |
325 | ||
326 | /* Install the entry. */ | |
327 | zebra_evpn_rem_mac_install(zevpn, mac, false /* was_static */); | |
24268cd0 | 328 | } |
b2998086 | 329 | |
24268cd0 PR |
330 | return 0; |
331 | } | |
332 | ||
d9d3455e PR |
333 | static void zebra_evpn_dup_addr_detect_for_mac(struct zebra_vrf *zvrf, |
334 | zebra_mac_t *mac, | |
335 | struct in_addr vtep_ip, | |
336 | bool do_dad, bool *is_dup_detect, | |
337 | bool is_local) | |
24268cd0 PR |
338 | { |
339 | zebra_neigh_t *nbr; | |
340 | struct listnode *node = NULL; | |
341 | struct timeval elapsed = {0, 0}; | |
342 | char buf[ETHER_ADDR_STRLEN]; | |
343 | char buf1[INET6_ADDRSTRLEN]; | |
344 | bool reset_params = false; | |
345 | ||
346 | if (!(zvrf->dup_addr_detect && do_dad)) | |
347 | return; | |
348 | ||
349 | /* MAC is detected as duplicate, | |
350 | * Local MAC event -> hold on advertising to BGP. | |
351 | * Remote MAC event -> hold on installing it. | |
352 | */ | |
353 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) { | |
354 | if (IS_ZEBRA_DEBUG_VXLAN) | |
355 | zlog_debug( | |
356 | "%s: duplicate addr MAC %s flags 0x%x skip update to client, learn count %u recover time %u", | |
357 | __func__, | |
358 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
359 | mac->flags, mac->dad_count, | |
360 | zvrf->dad_freeze_time); | |
361 | ||
362 | /* For duplicate MAC do not update | |
363 | * client but update neigh due to | |
364 | * this MAC update. | |
365 | */ | |
366 | if (zvrf->dad_freeze) | |
367 | *is_dup_detect = true; | |
368 | ||
369 | return; | |
370 | } | |
371 | ||
372 | /* Check if detection time (M-secs) expired. | |
373 | * Reset learn count and detection start time. | |
374 | */ | |
375 | monotime_since(&mac->detect_start_time, &elapsed); | |
376 | reset_params = (elapsed.tv_sec > zvrf->dad_time); | |
377 | if (is_local && !reset_params) { | |
378 | /* RFC-7432: A PE/VTEP that detects a MAC mobility | |
379 | * event via LOCAL learning starts an M-second timer. | |
380 | * | |
381 | * NOTE: This is the START of the probe with count is | |
382 | * 0 during LOCAL learn event. | |
383 | * (mac->dad_count == 0 || elapsed.tv_sec >= zvrf->dad_time) | |
384 | */ | |
385 | reset_params = !mac->dad_count; | |
386 | } | |
387 | ||
388 | if (reset_params) { | |
389 | if (IS_ZEBRA_DEBUG_VXLAN) | |
390 | zlog_debug( | |
391 | "%s: duplicate addr MAC %s flags 0x%x detection time passed, reset learn count %u", | |
392 | __func__, | |
393 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
394 | mac->flags, mac->dad_count); | |
395 | ||
396 | mac->dad_count = 0; | |
397 | /* Start dup. addr detection (DAD) start time, | |
398 | * ONLY during LOCAL learn. | |
399 | */ | |
400 | if (is_local) | |
401 | monotime(&mac->detect_start_time); | |
402 | ||
403 | } else if (!is_local) { | |
404 | /* For REMOTE MAC, increment detection count | |
405 | * ONLY while in probe window, once window passed, | |
406 | * next local learn event should trigger DAD. | |
407 | */ | |
408 | mac->dad_count++; | |
409 | } | |
410 | ||
411 | /* For LOCAL MAC learn event, once count is reset above via either | |
412 | * initial/start detection time or passed the probe time, the count | |
413 | * needs to be incremented. | |
414 | */ | |
415 | if (is_local) | |
416 | mac->dad_count++; | |
417 | ||
418 | if (mac->dad_count >= zvrf->dad_max_moves) { | |
419 | flog_warn(EC_ZEBRA_DUP_MAC_DETECTED, | |
420 | "VNI %u: MAC %s detected as duplicate during %s VTEP %s", | |
421 | mac->zevpn->vni, | |
422 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
423 | is_local ? "local update, last" : | |
424 | "remote update, from", inet_ntoa(vtep_ip)); | |
425 | ||
426 | SET_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE); | |
427 | ||
428 | /* Capture Duplicate detection time */ | |
429 | mac->dad_dup_detect_time = monotime(NULL); | |
430 | ||
431 | /* Mark all IPs/Neighs as duplicate | |
432 | * associcated with this MAC | |
433 | */ | |
434 | for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, nbr)) { | |
435 | ||
436 | /* Ony Mark IPs which are Local */ | |
437 | if (!CHECK_FLAG(nbr->flags, ZEBRA_NEIGH_LOCAL)) | |
438 | continue; | |
439 | ||
440 | SET_FLAG(nbr->flags, ZEBRA_NEIGH_DUPLICATE); | |
441 | ||
442 | nbr->dad_dup_detect_time = monotime(NULL); | |
443 | ||
444 | flog_warn(EC_ZEBRA_DUP_IP_INHERIT_DETECTED, | |
445 | "VNI %u: MAC %s IP %s detected as duplicate during %s update, inherit duplicate from MAC", | |
446 | mac->zevpn->vni, | |
447 | prefix_mac2str(&mac->macaddr, | |
448 | buf, sizeof(buf)), | |
449 | ipaddr2str(&nbr->ip, buf1, sizeof(buf1)), | |
450 | is_local ? "local" : "remote"); | |
451 | } | |
452 | ||
453 | /* Start auto recovery timer for this MAC */ | |
454 | THREAD_OFF(mac->dad_mac_auto_recovery_timer); | |
455 | if (zvrf->dad_freeze && zvrf->dad_freeze_time) { | |
456 | if (IS_ZEBRA_DEBUG_VXLAN) | |
457 | zlog_debug( | |
458 | "%s: duplicate addr MAC %s flags 0x%x auto recovery time %u start", | |
459 | __func__, | |
460 | prefix_mac2str(&mac->macaddr, buf, | |
461 | sizeof(buf)), | |
462 | mac->flags, zvrf->dad_freeze_time); | |
463 | ||
464 | thread_add_timer(zrouter.master, | |
b2998086 | 465 | zebra_evpn_dad_mac_auto_recovery_exp, |
24268cd0 PR |
466 | mac, zvrf->dad_freeze_time, |
467 | &mac->dad_mac_auto_recovery_timer); | |
468 | } | |
469 | ||
470 | /* In case of local update, do not inform to client (BGPd), | |
471 | * upd_neigh for neigh sequence change. | |
472 | */ | |
473 | if (zvrf->dad_freeze) | |
474 | *is_dup_detect = true; | |
475 | } | |
476 | } | |
477 | ||
b2998086 PR |
478 | /* |
479 | * Print a specific MAC entry. | |
480 | */ | |
481 | void zebra_evpn_print_mac(zebra_mac_t *mac, void *ctxt, json_object *json) | |
24268cd0 | 482 | { |
b2998086 PR |
483 | struct vty *vty; |
484 | zebra_neigh_t *n = NULL; | |
485 | struct listnode *node = NULL; | |
486 | char buf1[ETHER_ADDR_STRLEN]; | |
487 | char buf2[INET6_ADDRSTRLEN]; | |
488 | struct zebra_vrf *zvrf; | |
489 | struct timeval detect_start_time = {0, 0}; | |
490 | char timebuf[MONOTIME_STRLEN]; | |
491 | char thread_buf[THREAD_TIMER_STRLEN]; | |
24268cd0 | 492 | |
b2998086 PR |
493 | zvrf = zebra_vrf_get_evpn(); |
494 | if (!zvrf) | |
24268cd0 PR |
495 | return; |
496 | ||
b2998086 PR |
497 | vty = (struct vty *)ctxt; |
498 | prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1)); | |
24268cd0 | 499 | |
b2998086 PR |
500 | if (json) { |
501 | json_object *json_mac = json_object_new_object(); | |
24268cd0 | 502 | |
b2998086 PR |
503 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { |
504 | struct interface *ifp; | |
505 | vlanid_t vid; | |
24268cd0 | 506 | |
b2998086 PR |
507 | zebra_evpn_mac_get_access_info(mac, &ifp, &vid); |
508 | json_object_string_add(json_mac, "type", "local"); | |
509 | if (ifp) { | |
510 | json_object_string_add(json_mac, "intf", | |
511 | ifp->name); | |
512 | json_object_int_add(json_mac, "ifindex", | |
513 | ifp->ifindex); | |
514 | } | |
515 | if (vid) | |
516 | json_object_int_add(json_mac, "vlan", vid); | |
517 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) { | |
518 | json_object_string_add(json_mac, "type", "remote"); | |
519 | json_object_string_add( | |
520 | json_mac, "remoteVtep", | |
521 | inet_ntoa(mac->fwd_info.r_vtep_ip)); | |
522 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) | |
523 | json_object_string_add(json_mac, "type", "auto"); | |
24268cd0 | 524 | |
b2998086 PR |
525 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) |
526 | json_object_boolean_true_add(json_mac, "stickyMac"); | |
24268cd0 | 527 | |
b2998086 PR |
528 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) |
529 | json_object_boolean_true_add(json_mac, | |
530 | "defaultGateway"); | |
24268cd0 | 531 | |
b2998086 PR |
532 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)) |
533 | json_object_boolean_true_add(json_mac, | |
534 | "remoteGatewayMac"); | |
24268cd0 | 535 | |
b2998086 PR |
536 | json_object_int_add(json_mac, "localSequence", mac->loc_seq); |
537 | json_object_int_add(json_mac, "remoteSequence", mac->rem_seq); | |
24268cd0 | 538 | |
b2998086 PR |
539 | json_object_int_add(json_mac, "detectionCount", mac->dad_count); |
540 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) | |
541 | json_object_boolean_true_add(json_mac, "isDuplicate"); | |
542 | else | |
543 | json_object_boolean_false_add(json_mac, "isDuplicate"); | |
24268cd0 | 544 | |
b2998086 PR |
545 | json_object_int_add(json_mac, "syncNeighCount", |
546 | mac->sync_neigh_cnt); | |
547 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE)) | |
548 | json_object_boolean_true_add(json_mac, "localInactive"); | |
549 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY)) | |
550 | json_object_boolean_true_add(json_mac, "peerProxy"); | |
551 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE)) | |
552 | json_object_boolean_true_add(json_mac, "peerActive"); | |
553 | if (mac->hold_timer) | |
554 | json_object_string_add( | |
555 | json_mac, "peerActiveHold", | |
556 | thread_timer_to_hhmmss(thread_buf, | |
557 | sizeof(thread_buf), | |
558 | mac->hold_timer)); | |
559 | if (mac->es) | |
560 | json_object_string_add(json_mac, "esi", | |
561 | mac->es->esi_str); | |
562 | /* print all the associated neigh */ | |
563 | if (!listcount(mac->neigh_list)) | |
564 | json_object_string_add(json_mac, "neighbors", "none"); | |
565 | else { | |
566 | json_object *json_active_nbrs = json_object_new_array(); | |
567 | json_object *json_inactive_nbrs = | |
568 | json_object_new_array(); | |
569 | json_object *json_nbrs = json_object_new_object(); | |
24268cd0 | 570 | |
b2998086 PR |
571 | for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) { |
572 | if (IS_ZEBRA_NEIGH_ACTIVE(n)) | |
573 | json_object_array_add( | |
574 | json_active_nbrs, | |
575 | json_object_new_string( | |
576 | ipaddr2str( | |
577 | &n->ip, buf2, | |
578 | sizeof(buf2)))); | |
579 | else | |
580 | json_object_array_add( | |
581 | json_inactive_nbrs, | |
582 | json_object_new_string( | |
583 | ipaddr2str( | |
584 | &n->ip, buf2, | |
585 | sizeof(buf2)))); | |
586 | } | |
24268cd0 | 587 | |
b2998086 PR |
588 | json_object_object_add(json_nbrs, "active", |
589 | json_active_nbrs); | |
590 | json_object_object_add(json_nbrs, "inactive", | |
591 | json_inactive_nbrs); | |
592 | json_object_object_add(json_mac, "neighbors", | |
593 | json_nbrs); | |
24268cd0 | 594 | } |
24268cd0 | 595 | |
b2998086 PR |
596 | json_object_object_add(json, buf1, json_mac); |
597 | } else { | |
598 | vty_out(vty, "MAC: %s\n", buf1); | |
24268cd0 | 599 | |
b2998086 PR |
600 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { |
601 | struct interface *ifp; | |
602 | vlanid_t vid; | |
24268cd0 | 603 | |
b2998086 | 604 | zebra_evpn_mac_get_access_info(mac, &ifp, &vid); |
24268cd0 | 605 | |
b2998086 PR |
606 | if (mac->es) |
607 | vty_out(vty, " ESI: %s\n", mac->es->esi_str); | |
24268cd0 | 608 | |
b2998086 PR |
609 | if (ifp) |
610 | vty_out(vty, " Intf: %s(%u)", ifp->name, | |
611 | ifp->ifindex); | |
612 | else | |
613 | vty_out(vty, " Intf: -"); | |
614 | vty_out(vty, " VLAN: %u", vid); | |
615 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) { | |
616 | if (mac->es) | |
617 | vty_out(vty, " Remote ES: %s", | |
618 | mac->es->esi_str); | |
619 | else | |
620 | vty_out(vty, " Remote VTEP: %s", | |
621 | inet_ntoa(mac->fwd_info.r_vtep_ip)); | |
622 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) { | |
623 | vty_out(vty, " Auto Mac "); | |
624 | } | |
24268cd0 | 625 | |
b2998086 PR |
626 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) |
627 | vty_out(vty, " Sticky Mac "); | |
24268cd0 | 628 | |
b2998086 PR |
629 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW)) |
630 | vty_out(vty, " Default-gateway Mac "); | |
631 | ||
632 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW)) | |
633 | vty_out(vty, " Remote-gateway Mac "); | |
634 | ||
635 | vty_out(vty, "\n"); | |
636 | vty_out(vty, " Sync-info: neigh#: %u", mac->sync_neigh_cnt); | |
637 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE)) | |
24268cd0 | 638 | vty_out(vty, " local-inactive"); |
b2998086 | 639 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY)) |
24268cd0 | 640 | vty_out(vty, " peer-proxy"); |
b2998086 | 641 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE)) |
24268cd0 | 642 | vty_out(vty, " peer-active"); |
b2998086 | 643 | if (mac->hold_timer) |
24268cd0 | 644 | vty_out(vty, " (ht: %s)", |
b2998086 PR |
645 | thread_timer_to_hhmmss(thread_buf, |
646 | sizeof(thread_buf), | |
647 | mac->hold_timer)); | |
648 | vty_out(vty, "\n"); | |
649 | vty_out(vty, " Local Seq: %u Remote Seq: %u", mac->loc_seq, | |
650 | mac->rem_seq); | |
24268cd0 | 651 | vty_out(vty, "\n"); |
24268cd0 | 652 | |
b2998086 | 653 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) { |
24268cd0 | 654 | vty_out(vty, " Duplicate, detected at %s", |
b2998086 | 655 | time_to_string(mac->dad_dup_detect_time, |
24268cd0 | 656 | timebuf)); |
b2998086 PR |
657 | } else if (mac->dad_count) { |
658 | monotime_since(&mac->detect_start_time, | |
24268cd0 PR |
659 | &detect_start_time); |
660 | if (detect_start_time.tv_sec <= zvrf->dad_time) { | |
b2998086 | 661 | time_to_string(mac->detect_start_time.tv_sec, |
24268cd0 PR |
662 | timebuf); |
663 | vty_out(vty, | |
664 | " Duplicate detection started at %s, detection count %u\n", | |
b2998086 | 665 | timebuf, mac->dad_count); |
24268cd0 PR |
666 | } |
667 | } | |
24268cd0 | 668 | |
b2998086 PR |
669 | /* print all the associated neigh */ |
670 | vty_out(vty, " Neighbors:\n"); | |
671 | if (!listcount(mac->neigh_list)) | |
672 | vty_out(vty, " No Neighbors\n"); | |
673 | else { | |
674 | for (ALL_LIST_ELEMENTS_RO(mac->neigh_list, node, n)) { | |
675 | vty_out(vty, " %s %s\n", | |
676 | ipaddr2str(&n->ip, buf2, sizeof(buf2)), | |
677 | (IS_ZEBRA_NEIGH_ACTIVE(n) | |
678 | ? "Active" | |
679 | : "Inactive")); | |
680 | } | |
681 | } | |
24268cd0 | 682 | |
b2998086 | 683 | vty_out(vty, "\n"); |
24268cd0 PR |
684 | } |
685 | } | |
686 | ||
b2998086 PR |
687 | static char *zebra_evpn_print_mac_flags(zebra_mac_t *mac, char *flags_buf, |
688 | uint32_t flags_buf_sz) | |
24268cd0 | 689 | { |
b2998086 PR |
690 | snprintf(flags_buf, flags_buf_sz, "%s%s%s%s", |
691 | mac->sync_neigh_cnt ? | |
692 | "N" : "", | |
693 | (mac->flags & ZEBRA_MAC_ES_PEER_ACTIVE) ? | |
24268cd0 | 694 | "P" : "", |
b2998086 | 695 | (mac->flags & ZEBRA_MAC_ES_PEER_PROXY) ? |
24268cd0 | 696 | "X" : "", |
b2998086 | 697 | (mac->flags & ZEBRA_MAC_LOCAL_INACTIVE) ? |
24268cd0 PR |
698 | "I" : ""); |
699 | ||
700 | return flags_buf; | |
701 | } | |
702 | ||
703 | /* | |
b2998086 | 704 | * Print MAC hash entry - called for display of all MACs. |
24268cd0 | 705 | */ |
b2998086 | 706 | void zebra_evpn_print_mac_hash(struct hash_bucket *bucket, void *ctxt) |
24268cd0 PR |
707 | { |
708 | struct vty *vty; | |
b2998086 PR |
709 | json_object *json_mac_hdr = NULL, *json_mac = NULL; |
710 | zebra_mac_t *mac; | |
24268cd0 | 711 | char buf1[ETHER_ADDR_STRLEN]; |
b2998086 | 712 | struct mac_walk_ctx *wctx = ctxt; |
24268cd0 PR |
713 | char flags_buf[6]; |
714 | ||
715 | vty = wctx->vty; | |
b2998086 PR |
716 | json_mac_hdr = wctx->json; |
717 | mac = (zebra_mac_t *)bucket->data; | |
718 | ||
719 | prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1)); | |
720 | ||
721 | if (json_mac_hdr) | |
722 | json_mac = json_object_new_object(); | |
24268cd0 | 723 | |
b2998086 PR |
724 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { |
725 | struct interface *ifp; | |
726 | vlanid_t vid; | |
24268cd0 | 727 | |
b2998086 | 728 | if (wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) |
24268cd0 PR |
729 | return; |
730 | ||
b2998086 PR |
731 | zebra_evpn_mac_get_access_info(mac, &ifp, &vid); |
732 | if (json_mac_hdr == NULL) { | |
733 | vty_out(vty, "%-17s %-6s %-5s %-30s", buf1, "local", | |
734 | zebra_evpn_print_mac_flags(mac, flags_buf, | |
735 | sizeof(flags_buf)), | |
736 | ifp ? ifp->name : "-"); | |
737 | } else { | |
738 | json_object_string_add(json_mac, "type", "local"); | |
739 | if (ifp) | |
740 | json_object_string_add(json_mac, "intf", | |
741 | ifp->name); | |
742 | } | |
743 | if (vid) { | |
744 | if (json_mac_hdr == NULL) | |
745 | vty_out(vty, " %-5u", vid); | |
746 | else | |
747 | json_object_int_add(json_mac, "vlan", vid); | |
748 | } else /* No vid? fill out the space */ | |
749 | if (json_mac_hdr == NULL) | |
750 | vty_out(vty, " %-5s", ""); | |
751 | if (json_mac_hdr == NULL) { | |
752 | vty_out(vty, " %u/%u", mac->loc_seq, mac->rem_seq); | |
753 | vty_out(vty, "\n"); | |
24268cd0 | 754 | } else { |
b2998086 PR |
755 | json_object_int_add(json_mac, "localSequence", |
756 | mac->loc_seq); | |
757 | json_object_int_add(json_mac, "remoteSequence", | |
758 | mac->rem_seq); | |
759 | json_object_int_add(json_mac, "detectionCount", | |
760 | mac->dad_count); | |
761 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) | |
762 | json_object_boolean_true_add(json_mac, | |
24268cd0 PR |
763 | "isDuplicate"); |
764 | else | |
b2998086 | 765 | json_object_boolean_false_add(json_mac, |
24268cd0 | 766 | "isDuplicate"); |
b2998086 | 767 | json_object_object_add(json_mac_hdr, buf1, json_mac); |
24268cd0 | 768 | } |
b2998086 | 769 | |
24268cd0 | 770 | wctx->count++; |
b2998086 PR |
771 | |
772 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) { | |
773 | ||
774 | if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) | |
775 | && !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, | |
776 | &wctx->r_vtep_ip)) | |
24268cd0 PR |
777 | return; |
778 | ||
b2998086 PR |
779 | if (json_mac_hdr == NULL) { |
780 | if ((wctx->flags & SHOW_REMOTE_MAC_FROM_VTEP) | |
781 | && (wctx->count == 0)) { | |
782 | vty_out(vty, "\nVNI %u\n\n", wctx->zevpn->vni); | |
783 | vty_out(vty, "%-17s %-6s %-5s%-30s %-5s %s\n", | |
784 | "MAC", "Type", "Flags", | |
785 | "Intf/Remote ES/VTEP", "VLAN", | |
786 | "Seq #'s"); | |
24268cd0 | 787 | } |
b2998086 PR |
788 | vty_out(vty, "%-17s %-6s %-5s %-30s %-5s %u/%u\n", buf1, |
789 | "remote", | |
790 | zebra_evpn_print_mac_flags(mac, flags_buf, | |
791 | sizeof(flags_buf)), | |
792 | mac->es ? mac->es->esi_str | |
793 | : inet_ntoa(mac->fwd_info.r_vtep_ip), | |
794 | "", mac->loc_seq, mac->rem_seq); | |
795 | } else { | |
796 | json_object_string_add(json_mac, "type", "remote"); | |
797 | json_object_string_add( | |
798 | json_mac, "remoteVtep", | |
799 | inet_ntoa(mac->fwd_info.r_vtep_ip)); | |
800 | json_object_object_add(json_mac_hdr, buf1, json_mac); | |
801 | json_object_int_add(json_mac, "localSequence", | |
802 | mac->loc_seq); | |
803 | json_object_int_add(json_mac, "remoteSequence", | |
804 | mac->rem_seq); | |
805 | json_object_int_add(json_mac, "detectionCount", | |
806 | mac->dad_count); | |
807 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) | |
808 | json_object_boolean_true_add(json_mac, | |
809 | "isDuplicate"); | |
810 | else | |
811 | json_object_boolean_false_add(json_mac, | |
812 | "isDuplicate"); | |
813 | } | |
24268cd0 | 814 | |
b2998086 PR |
815 | wctx->count++; |
816 | } | |
24268cd0 PR |
817 | } |
818 | ||
819 | /* | |
b2998086 | 820 | * Print MAC hash entry in detail - called for display of all MACs. |
24268cd0 | 821 | */ |
b2998086 | 822 | void zebra_evpn_print_mac_hash_detail(struct hash_bucket *bucket, void *ctxt) |
24268cd0 | 823 | { |
b2998086 PR |
824 | struct vty *vty; |
825 | json_object *json_mac_hdr = NULL; | |
826 | zebra_mac_t *mac; | |
827 | struct mac_walk_ctx *wctx = ctxt; | |
828 | char buf1[ETHER_ADDR_STRLEN]; | |
24268cd0 | 829 | |
b2998086 PR |
830 | vty = wctx->vty; |
831 | json_mac_hdr = wctx->json; | |
832 | mac = (zebra_mac_t *)bucket->data; | |
833 | if (!mac) | |
24268cd0 PR |
834 | return; |
835 | ||
b2998086 PR |
836 | wctx->count++; |
837 | prefix_mac2str(&mac->macaddr, buf1, sizeof(buf1)); | |
24268cd0 | 838 | |
b2998086 | 839 | zebra_evpn_print_mac(mac, vty, json_mac_hdr); |
24268cd0 PR |
840 | } |
841 | ||
842 | /* | |
b2998086 | 843 | * Inform BGP about local MACIP. |
24268cd0 | 844 | */ |
b2998086 PR |
845 | int zebra_evpn_macip_send_msg_to_client(vni_t vni, struct ethaddr *macaddr, |
846 | struct ipaddr *ip, uint8_t flags, | |
847 | uint32_t seq, int state, | |
848 | struct zebra_evpn_es *es, uint16_t cmd) | |
24268cd0 | 849 | { |
b2998086 PR |
850 | char buf[ETHER_ADDR_STRLEN]; |
851 | char buf2[INET6_ADDRSTRLEN]; | |
852 | int ipa_len; | |
853 | struct zserv *client = NULL; | |
854 | struct stream *s = NULL; | |
855 | esi_t *esi = es ? &es->esi : zero_esi; | |
24268cd0 | 856 | |
b2998086 PR |
857 | client = zserv_find_client(ZEBRA_ROUTE_BGP, 0); |
858 | /* BGP may not be running. */ | |
859 | if (!client) | |
860 | return 0; | |
24268cd0 | 861 | |
b2998086 | 862 | s = stream_new(ZEBRA_MAX_PACKET_SIZ); |
24268cd0 | 863 | |
b2998086 PR |
864 | zclient_create_header(s, cmd, zebra_vrf_get_evpn_id()); |
865 | stream_putl(s, vni); | |
866 | stream_put(s, macaddr->octet, ETH_ALEN); | |
867 | if (ip) { | |
868 | ipa_len = 0; | |
869 | if (IS_IPADDR_V4(ip)) | |
870 | ipa_len = IPV4_MAX_BYTELEN; | |
871 | else if (IS_IPADDR_V6(ip)) | |
872 | ipa_len = IPV6_MAX_BYTELEN; | |
24268cd0 | 873 | |
b2998086 PR |
874 | stream_putl(s, ipa_len); /* IP address length */ |
875 | if (ipa_len) | |
876 | stream_put(s, &ip->ip.addr, ipa_len); /* IP address */ | |
877 | } else | |
878 | stream_putl(s, 0); /* Just MAC. */ | |
24268cd0 | 879 | |
b2998086 PR |
880 | if (cmd == ZEBRA_MACIP_ADD) { |
881 | stream_putc(s, flags); /* sticky mac/gateway mac */ | |
882 | stream_putl(s, seq); /* sequence number */ | |
883 | stream_put(s, esi, sizeof(esi_t)); | |
24268cd0 | 884 | } else { |
b2998086 PR |
885 | stream_putl(s, state); /* state - active/inactive */ |
886 | } | |
24268cd0 | 887 | |
24268cd0 | 888 | |
b2998086 PR |
889 | /* Write packet size. */ |
890 | stream_putw_at(s, 0, stream_get_endp(s)); | |
24268cd0 | 891 | |
b2998086 PR |
892 | if (IS_ZEBRA_DEBUG_VXLAN) |
893 | zlog_debug( | |
894 | "Send MACIP %s f 0x%x MAC %s IP %s seq %u L2-VNI %u ESI %s to %s", | |
895 | (cmd == ZEBRA_MACIP_ADD) ? "Add" : "Del", flags, | |
896 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
897 | ipaddr2str(ip, buf2, sizeof(buf2)), seq, vni, | |
898 | es ? es->esi_str : "-", | |
899 | zebra_route_string(client->proto)); | |
24268cd0 | 900 | |
b2998086 PR |
901 | if (cmd == ZEBRA_MACIP_ADD) |
902 | client->macipadd_cnt++; | |
903 | else | |
904 | client->macipdel_cnt++; | |
24268cd0 | 905 | |
b2998086 PR |
906 | return zserv_send_message(client, s); |
907 | } | |
24268cd0 | 908 | |
b2998086 PR |
909 | static unsigned int mac_hash_keymake(const void *p) |
910 | { | |
911 | const zebra_mac_t *pmac = p; | |
912 | const void *pnt = (void *)pmac->macaddr.octet; | |
24268cd0 | 913 | |
b2998086 | 914 | return jhash(pnt, ETH_ALEN, 0xa5a5a55a); |
24268cd0 PR |
915 | } |
916 | ||
917 | /* | |
b2998086 | 918 | * Compare two MAC addresses. |
24268cd0 | 919 | */ |
b2998086 | 920 | static bool mac_cmp(const void *p1, const void *p2) |
24268cd0 | 921 | { |
b2998086 PR |
922 | const zebra_mac_t *pmac1 = p1; |
923 | const zebra_mac_t *pmac2 = p2; | |
24268cd0 | 924 | |
b2998086 PR |
925 | if (pmac1 == NULL && pmac2 == NULL) |
926 | return true; | |
24268cd0 | 927 | |
b2998086 PR |
928 | if (pmac1 == NULL || pmac2 == NULL) |
929 | return false; | |
24268cd0 | 930 | |
b2998086 PR |
931 | return (memcmp(pmac1->macaddr.octet, pmac2->macaddr.octet, ETH_ALEN) |
932 | == 0); | |
933 | } | |
24268cd0 | 934 | |
b2998086 PR |
935 | /* |
936 | * Callback to allocate MAC hash entry. | |
937 | */ | |
938 | static void *zebra_evpn_mac_alloc(void *p) | |
939 | { | |
940 | const zebra_mac_t *tmp_mac = p; | |
941 | zebra_mac_t *mac; | |
24268cd0 | 942 | |
b2998086 PR |
943 | mac = XCALLOC(MTYPE_MAC, sizeof(zebra_mac_t)); |
944 | *mac = *tmp_mac; | |
24268cd0 | 945 | |
b2998086 PR |
946 | return ((void *)mac); |
947 | } | |
24268cd0 | 948 | |
b2998086 PR |
949 | /* |
950 | * Add MAC entry. | |
951 | */ | |
952 | zebra_mac_t *zebra_evpn_mac_add(zebra_evpn_t *zevpn, struct ethaddr *macaddr) | |
953 | { | |
954 | zebra_mac_t tmp_mac; | |
955 | zebra_mac_t *mac = NULL; | |
24268cd0 | 956 | |
b2998086 PR |
957 | memset(&tmp_mac, 0, sizeof(zebra_mac_t)); |
958 | memcpy(&tmp_mac.macaddr, macaddr, ETH_ALEN); | |
959 | mac = hash_get(zevpn->mac_table, &tmp_mac, zebra_evpn_mac_alloc); | |
960 | assert(mac); | |
24268cd0 | 961 | |
b2998086 PR |
962 | mac->zevpn = zevpn; |
963 | mac->dad_mac_auto_recovery_timer = NULL; | |
24268cd0 | 964 | |
b2998086 PR |
965 | mac->neigh_list = list_new(); |
966 | mac->neigh_list->cmp = neigh_list_cmp; | |
24268cd0 | 967 | |
b2998086 PR |
968 | if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) { |
969 | char buf[ETHER_ADDR_STRLEN]; | |
24268cd0 | 970 | |
b2998086 PR |
971 | zlog_debug("%s: MAC %s flags 0x%x", __func__, |
972 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
973 | mac->flags); | |
974 | } | |
975 | return mac; | |
24268cd0 PR |
976 | } |
977 | ||
978 | /* | |
b2998086 | 979 | * Delete MAC entry. |
24268cd0 | 980 | */ |
b2998086 | 981 | int zebra_evpn_mac_del(zebra_evpn_t *zevpn, zebra_mac_t *mac) |
24268cd0 | 982 | { |
b2998086 | 983 | zebra_mac_t *tmp_mac; |
24268cd0 | 984 | |
b2998086 PR |
985 | if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) { |
986 | char buf[ETHER_ADDR_STRLEN]; | |
987 | ||
988 | zlog_debug("%s: MAC %s flags 0x%x", __func__, | |
989 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
990 | mac->flags); | |
24268cd0 PR |
991 | } |
992 | ||
b2998086 PR |
993 | /* force de-ref any ES entry linked to the MAC */ |
994 | zebra_evpn_es_mac_deref_entry(mac); | |
24268cd0 | 995 | |
b2998086 PR |
996 | /* Cancel proxy hold timer */ |
997 | zebra_evpn_mac_stop_hold_timer(mac); | |
24268cd0 | 998 | |
b2998086 PR |
999 | /* Cancel auto recovery */ |
1000 | THREAD_OFF(mac->dad_mac_auto_recovery_timer); | |
24268cd0 | 1001 | |
b2998086 | 1002 | list_delete(&mac->neigh_list); |
24268cd0 | 1003 | |
b2998086 PR |
1004 | /* Free the VNI hash entry and allocated memory. */ |
1005 | tmp_mac = hash_release(zevpn->mac_table, mac); | |
1006 | XFREE(MTYPE_MAC, tmp_mac); | |
24268cd0 | 1007 | |
b2998086 PR |
1008 | return 0; |
1009 | } | |
24268cd0 | 1010 | |
b2998086 PR |
1011 | static bool zebra_evpn_check_mac_del_from_db(struct mac_walk_ctx *wctx, |
1012 | zebra_mac_t *mac) | |
1013 | { | |
1014 | if ((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_LOCAL)) | |
1015 | return true; | |
1016 | else if ((wctx->flags & DEL_REMOTE_MAC) | |
1017 | && (mac->flags & ZEBRA_MAC_REMOTE)) | |
1018 | return true; | |
1019 | else if ((wctx->flags & DEL_REMOTE_MAC_FROM_VTEP) | |
1020 | && (mac->flags & ZEBRA_MAC_REMOTE) | |
1021 | && IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &wctx->r_vtep_ip)) | |
1022 | return true; | |
1023 | else if ((wctx->flags & DEL_LOCAL_MAC) && (mac->flags & ZEBRA_MAC_AUTO) | |
1024 | && !listcount(mac->neigh_list)) { | |
1025 | if (IS_ZEBRA_DEBUG_VXLAN) { | |
1026 | char buf[ETHER_ADDR_STRLEN]; | |
24268cd0 | 1027 | |
24268cd0 | 1028 | zlog_debug( |
b2998086 PR |
1029 | "%s: Del MAC %s flags 0x%x", __func__, |
1030 | prefix_mac2str(&mac->macaddr, buf, sizeof(buf)), | |
1031 | mac->flags); | |
1032 | } | |
1033 | wctx->uninstall = 0; | |
24268cd0 | 1034 | |
b2998086 PR |
1035 | return true; |
1036 | } | |
24268cd0 | 1037 | |
b2998086 PR |
1038 | return false; |
1039 | } | |
24268cd0 | 1040 | |
b2998086 PR |
1041 | /* |
1042 | * Free MAC hash entry (callback) | |
1043 | */ | |
1044 | static void zebra_evpn_mac_del_hash_entry(struct hash_bucket *bucket, void *arg) | |
1045 | { | |
1046 | struct mac_walk_ctx *wctx = arg; | |
1047 | zebra_mac_t *mac = bucket->data; | |
24268cd0 | 1048 | |
b2998086 PR |
1049 | if (zebra_evpn_check_mac_del_from_db(wctx, mac)) { |
1050 | if (wctx->upd_client && (mac->flags & ZEBRA_MAC_LOCAL)) { | |
1051 | zebra_evpn_mac_send_del_to_client(wctx->zevpn->vni, | |
1052 | &mac->macaddr, | |
1053 | mac->flags, false); | |
1054 | } | |
1055 | if (wctx->uninstall) { | |
1056 | if (zebra_evpn_mac_is_static(mac)) | |
1057 | zebra_evpn_sync_mac_dp_install( | |
1058 | mac, false /* set_inactive */, | |
1059 | true /* force_clear_static */, | |
1060 | __func__); | |
24268cd0 | 1061 | |
b2998086 PR |
1062 | if (mac->flags & ZEBRA_MAC_REMOTE) |
1063 | zebra_evpn_rem_mac_uninstall(wctx->zevpn, mac); | |
24268cd0 | 1064 | } |
b2998086 PR |
1065 | |
1066 | zebra_evpn_mac_del(wctx->zevpn, mac); | |
24268cd0 PR |
1067 | } |
1068 | ||
24268cd0 PR |
1069 | return; |
1070 | } | |
1071 | ||
24268cd0 | 1072 | /* |
b2998086 | 1073 | * Delete all MAC entries for this EVPN. |
24268cd0 | 1074 | */ |
b2998086 PR |
1075 | void zebra_evpn_mac_del_all(zebra_evpn_t *zevpn, int uninstall, int upd_client, |
1076 | uint32_t flags) | |
24268cd0 | 1077 | { |
b2998086 | 1078 | struct mac_walk_ctx wctx; |
24268cd0 | 1079 | |
b2998086 | 1080 | if (!zevpn->mac_table) |
24268cd0 PR |
1081 | return; |
1082 | ||
b2998086 PR |
1083 | memset(&wctx, 0, sizeof(struct mac_walk_ctx)); |
1084 | wctx.zevpn = zevpn; | |
1085 | wctx.uninstall = uninstall; | |
1086 | wctx.upd_client = upd_client; | |
1087 | wctx.flags = flags; | |
24268cd0 | 1088 | |
b2998086 PR |
1089 | hash_iterate(zevpn->mac_table, zebra_evpn_mac_del_hash_entry, &wctx); |
1090 | } | |
24268cd0 | 1091 | |
b2998086 PR |
1092 | /* |
1093 | * Look up MAC hash entry. | |
1094 | */ | |
1095 | zebra_mac_t *zebra_evpn_mac_lookup(zebra_evpn_t *zevpn, struct ethaddr *mac) | |
1096 | { | |
1097 | zebra_mac_t tmp; | |
1098 | zebra_mac_t *pmac; | |
24268cd0 | 1099 | |
b2998086 PR |
1100 | memset(&tmp, 0, sizeof(tmp)); |
1101 | memcpy(&tmp.macaddr, mac, ETH_ALEN); | |
1102 | pmac = hash_lookup(zevpn->mac_table, &tmp); | |
24268cd0 | 1103 | |
b2998086 PR |
1104 | return pmac; |
1105 | } | |
24268cd0 | 1106 | |
b2998086 PR |
1107 | /* |
1108 | * Inform BGP about local MAC addition. | |
1109 | */ | |
1110 | int zebra_evpn_mac_send_add_to_client(vni_t vni, struct ethaddr *macaddr, | |
1111 | uint32_t mac_flags, uint32_t seq, | |
1112 | struct zebra_evpn_es *es) | |
1113 | { | |
1114 | uint8_t flags = 0; | |
24268cd0 | 1115 | |
b2998086 PR |
1116 | if (CHECK_FLAG(mac_flags, ZEBRA_MAC_LOCAL_INACTIVE)) { |
1117 | /* host reachability has not been verified locally */ | |
24268cd0 | 1118 | |
b2998086 PR |
1119 | /* if no ES peer is claiming reachability we can't advertise the |
1120 | * entry | |
1121 | */ | |
1122 | if (!CHECK_FLAG(mac_flags, ZEBRA_MAC_ES_PEER_ACTIVE)) | |
1123 | return 0; | |
24268cd0 | 1124 | |
b2998086 PR |
1125 | /* ES peers are claiming reachability; we will |
1126 | * advertise the entry but with a proxy flag | |
24268cd0 | 1127 | */ |
b2998086 PR |
1128 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT); |
1129 | } | |
24268cd0 | 1130 | |
b2998086 PR |
1131 | if (CHECK_FLAG(mac_flags, ZEBRA_MAC_STICKY)) |
1132 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY); | |
1133 | if (CHECK_FLAG(mac_flags, ZEBRA_MAC_DEF_GW)) | |
1134 | SET_FLAG(flags, ZEBRA_MACIP_TYPE_GW); | |
24268cd0 | 1135 | |
b2998086 PR |
1136 | return zebra_evpn_macip_send_msg_to_client(vni, macaddr, NULL, flags, |
1137 | seq, ZEBRA_NEIGH_ACTIVE, es, | |
1138 | ZEBRA_MACIP_ADD); | |
1139 | } | |
24268cd0 | 1140 | |
b2998086 PR |
1141 | /* |
1142 | * Inform BGP about local MAC deletion. | |
1143 | */ | |
1144 | int zebra_evpn_mac_send_del_to_client(vni_t vni, struct ethaddr *macaddr, | |
1145 | uint32_t flags, bool force) | |
1146 | { | |
1147 | if (!force) { | |
1148 | if (CHECK_FLAG(flags, ZEBRA_MAC_LOCAL_INACTIVE) | |
1149 | && !CHECK_FLAG(flags, ZEBRA_MAC_ES_PEER_ACTIVE)) | |
1150 | /* the host was not advertised - nothing to delete */ | |
1151 | return 0; | |
24268cd0 PR |
1152 | } |
1153 | ||
b2998086 PR |
1154 | return zebra_evpn_macip_send_msg_to_client( |
1155 | vni, macaddr, NULL, 0 /* flags */, 0 /* seq */, | |
1156 | ZEBRA_NEIGH_ACTIVE, NULL, ZEBRA_MACIP_DEL); | |
24268cd0 PR |
1157 | } |
1158 | ||
1159 | /* | |
b2998086 | 1160 | * wrapper to create a MAC hash table |
24268cd0 | 1161 | */ |
b2998086 | 1162 | struct hash *zebra_mac_db_create(const char *desc) |
24268cd0 | 1163 | { |
b2998086 | 1164 | return hash_create(mac_hash_keymake, mac_cmp, desc); |
24268cd0 PR |
1165 | } |
1166 | ||
b2998086 PR |
1167 | /* program sync mac flags in the dataplane */ |
1168 | void zebra_evpn_sync_mac_dp_install(zebra_mac_t *mac, bool set_inactive, | |
1169 | bool force_clear_static, const char *caller) | |
24268cd0 | 1170 | { |
b2998086 PR |
1171 | char macbuf[ETHER_ADDR_STRLEN]; |
1172 | struct interface *ifp; | |
1173 | bool sticky; | |
1174 | bool set_static; | |
1175 | zebra_evpn_t *zevpn = mac->zevpn; | |
1176 | vlanid_t vid; | |
1177 | struct zebra_if *zif; | |
1178 | struct interface *br_ifp; | |
24268cd0 | 1179 | |
b2998086 PR |
1180 | /* get the access vlan from the vxlan_device */ |
1181 | zebra_evpn_mac_get_access_info(mac, &ifp, &vid); | |
24268cd0 | 1182 | |
b2998086 PR |
1183 | if (!ifp) { |
1184 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1185 | zlog_debug( | |
1186 | "%s: dp-install sync-mac vni %u mac %s es %s 0x%x %sskipped, no access-port", | |
1187 | caller, zevpn->vni, | |
1188 | prefix_mac2str(&mac->macaddr, macbuf, | |
1189 | sizeof(macbuf)), | |
1190 | mac->es ? mac->es->esi_str : "-", mac->flags, | |
1191 | set_inactive ? "inactive " : ""); | |
24268cd0 | 1192 | return; |
b2998086 | 1193 | } |
24268cd0 | 1194 | |
b2998086 PR |
1195 | zif = ifp->info; |
1196 | br_ifp = zif->brslave_info.br_if; | |
1197 | if (!br_ifp) { | |
1198 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1199 | zlog_debug( | |
1200 | "%s: dp-install sync-mac vni %u mac %s es %s 0x%x %sskipped, no br", | |
1201 | caller, zevpn->vni, | |
1202 | prefix_mac2str(&mac->macaddr, macbuf, | |
1203 | sizeof(macbuf)), | |
1204 | mac->es ? mac->es->esi_str : "-", mac->flags, | |
1205 | set_inactive ? "inactive " : ""); | |
1206 | return; | |
1207 | } | |
24268cd0 | 1208 | |
b2998086 PR |
1209 | sticky = !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY); |
1210 | if (force_clear_static) | |
1211 | set_static = false; | |
1212 | else | |
1213 | set_static = zebra_evpn_mac_is_static(mac); | |
24268cd0 | 1214 | |
b2998086 PR |
1215 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
1216 | zlog_debug( | |
1217 | "dp-install sync-mac vni %u mac %s es %s 0x%x %s%s", | |
1218 | zevpn->vni, | |
1219 | prefix_mac2str(&mac->macaddr, macbuf, sizeof(macbuf)), | |
1220 | mac->es ? mac->es->esi_str : "-", mac->flags, | |
1221 | set_static ? "static " : "", | |
1222 | set_inactive ? "inactive " : ""); | |
24268cd0 | 1223 | |
b2998086 PR |
1224 | dplane_local_mac_add(ifp, br_ifp, vid, &mac->macaddr, sticky, |
1225 | set_static, set_inactive); | |
24268cd0 PR |
1226 | } |
1227 | ||
b2998086 PR |
1228 | void zebra_evpn_mac_send_add_del_to_client(zebra_mac_t *mac, bool old_bgp_ready, |
1229 | bool new_bgp_ready) | |
24268cd0 | 1230 | { |
b2998086 PR |
1231 | if (new_bgp_ready) |
1232 | zebra_evpn_mac_send_add_to_client(mac->zevpn->vni, | |
1233 | &mac->macaddr, mac->flags, | |
1234 | mac->loc_seq, mac->es); | |
1235 | else if (old_bgp_ready) | |
1236 | zebra_evpn_mac_send_del_to_client(mac->zevpn->vni, | |
1237 | &mac->macaddr, mac->flags, | |
1238 | true /* force */); | |
24268cd0 PR |
1239 | } |
1240 | ||
b2998086 PR |
1241 | /* MAC hold timer is used to age out peer-active flag. |
1242 | * | |
1243 | * During this wait time we expect the dataplane component or an | |
1244 | * external neighmgr daemon to probe existing hosts to independently | |
1245 | * establish their presence on the ES. | |
1246 | */ | |
1247 | static int zebra_evpn_mac_hold_exp_cb(struct thread *t) | |
24268cd0 | 1248 | { |
b2998086 PR |
1249 | zebra_mac_t *mac; |
1250 | bool old_bgp_ready; | |
1251 | bool new_bgp_ready; | |
1252 | bool old_static; | |
1253 | bool new_static; | |
1254 | char macbuf[ETHER_ADDR_STRLEN]; | |
24268cd0 PR |
1255 | |
1256 | mac = THREAD_ARG(t); | |
b2998086 PR |
1257 | /* the purpose of the hold timer is to age out the peer-active |
1258 | * flag | |
1259 | */ | |
1260 | if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE)) | |
24268cd0 PR |
1261 | return 0; |
1262 | ||
b2998086 PR |
1263 | old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); |
1264 | old_static = zebra_evpn_mac_is_static(mac); | |
1265 | UNSET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE); | |
1266 | new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
1267 | new_static = zebra_evpn_mac_is_static(mac); | |
24268cd0 | 1268 | |
b2998086 | 1269 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
24268cd0 | 1270 | zlog_debug( |
b2998086 PR |
1271 | "sync-mac vni %u mac %s es %s 0x%x hold expired", |
1272 | mac->zevpn->vni, | |
1273 | prefix_mac2str(&mac->macaddr, macbuf, sizeof(macbuf)), | |
1274 | mac->es ? mac->es->esi_str : "-", mac->flags); | |
24268cd0 | 1275 | |
b2998086 PR |
1276 | /* re-program the local mac in the dataplane if the mac is no |
1277 | * longer static | |
1278 | */ | |
1279 | if (old_static != new_static) | |
1280 | zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */, | |
1281 | false /* force_clear_static */, | |
1282 | __func__); | |
24268cd0 | 1283 | |
b2998086 PR |
1284 | /* inform bgp if needed */ |
1285 | if (old_bgp_ready != new_bgp_ready) | |
1286 | zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready, | |
1287 | new_bgp_ready); | |
24268cd0 PR |
1288 | |
1289 | return 0; | |
1290 | } | |
1291 | ||
b2998086 | 1292 | static inline void zebra_evpn_mac_start_hold_timer(zebra_mac_t *mac) |
24268cd0 | 1293 | { |
b2998086 | 1294 | char macbuf[ETHER_ADDR_STRLEN]; |
24268cd0 | 1295 | |
b2998086 PR |
1296 | if (mac->hold_timer) |
1297 | return; | |
24268cd0 | 1298 | |
b2998086 | 1299 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
24268cd0 | 1300 | zlog_debug( |
b2998086 PR |
1301 | "sync-mac vni %u mac %s es %s 0x%x hold started", |
1302 | mac->zevpn->vni, | |
1303 | prefix_mac2str(&mac->macaddr, macbuf, sizeof(macbuf)), | |
1304 | mac->es ? mac->es->esi_str : "-", mac->flags); | |
1305 | thread_add_timer(zrouter.master, zebra_evpn_mac_hold_exp_cb, mac, | |
1306 | zmh_info->mac_hold_time, &mac->hold_timer); | |
24268cd0 PR |
1307 | } |
1308 | ||
b2998086 | 1309 | void zebra_evpn_mac_stop_hold_timer(zebra_mac_t *mac) |
24268cd0 | 1310 | { |
b2998086 | 1311 | char macbuf[ETHER_ADDR_STRLEN]; |
24268cd0 | 1312 | |
b2998086 PR |
1313 | if (!mac->hold_timer) |
1314 | return; | |
24268cd0 | 1315 | |
b2998086 PR |
1316 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
1317 | zlog_debug( | |
1318 | "sync-mac vni %u mac %s es %s 0x%x hold stopped", | |
1319 | mac->zevpn->vni, | |
1320 | prefix_mac2str(&mac->macaddr, macbuf, sizeof(macbuf)), | |
1321 | mac->es ? mac->es->esi_str : "-", mac->flags); | |
1322 | THREAD_OFF(mac->hold_timer); | |
24268cd0 PR |
1323 | } |
1324 | ||
b2998086 | 1325 | void zebra_evpn_sync_mac_del(zebra_mac_t *mac) |
24268cd0 | 1326 | { |
b2998086 PR |
1327 | char macbuf[ETHER_ADDR_STRLEN]; |
1328 | bool old_static; | |
1329 | bool new_static; | |
24268cd0 | 1330 | |
b2998086 PR |
1331 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
1332 | zlog_debug( | |
1333 | "sync-mac del vni %u mac %s es %s seq %d f 0x%x", | |
1334 | mac->zevpn->vni, | |
1335 | prefix_mac2str(&mac->macaddr, macbuf, sizeof(macbuf)), | |
1336 | mac->es ? mac->es->esi_str : "-", mac->loc_seq, | |
1337 | mac->flags); | |
1338 | old_static = zebra_evpn_mac_is_static(mac); | |
1339 | UNSET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY); | |
1340 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE)) | |
1341 | zebra_evpn_mac_start_hold_timer(mac); | |
1342 | new_static = zebra_evpn_mac_is_static(mac); | |
24268cd0 | 1343 | |
b2998086 PR |
1344 | if (old_static != new_static) |
1345 | /* program the local mac in the kernel */ | |
1346 | zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */, | |
1347 | false /* force_clear_static */, | |
1348 | __func__); | |
24268cd0 PR |
1349 | } |
1350 | ||
b2998086 PR |
1351 | static inline bool zebra_evpn_mac_is_bgp_seq_ok(zebra_evpn_t *zevpn, |
1352 | zebra_mac_t *mac, uint32_t seq, | |
1353 | uint16_t ipa_len, | |
1354 | struct ipaddr *ipaddr) | |
24268cd0 | 1355 | { |
b2998086 PR |
1356 | char macbuf[ETHER_ADDR_STRLEN]; |
1357 | char ipbuf[INET6_ADDRSTRLEN]; | |
1358 | uint32_t tmp_seq; | |
24268cd0 | 1359 | |
b2998086 PR |
1360 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) |
1361 | tmp_seq = mac->loc_seq; | |
1362 | else | |
1363 | tmp_seq = mac->rem_seq; | |
24268cd0 | 1364 | |
b2998086 PR |
1365 | if (seq < tmp_seq) { |
1366 | /* if the mac was never advertised to bgp we must accept | |
1367 | * whatever sequence number bgp sends | |
1368 | * XXX - check with Vivek | |
1369 | */ | |
1370 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL) | |
1371 | && !zebra_evpn_mac_is_ready_for_bgp(mac->flags)) { | |
1372 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1373 | zlog_debug( | |
1374 | "sync-macip accept vni %u mac %s%s%s lower seq %u f 0x%x", | |
1375 | zevpn->vni, | |
1376 | prefix_mac2str(&mac->macaddr, macbuf, | |
1377 | sizeof(macbuf)), | |
1378 | ipa_len ? " IP " : "", | |
1379 | ipa_len ? ipaddr2str(ipaddr, ipbuf, | |
1380 | sizeof(ipbuf)) | |
1381 | : "", | |
1382 | tmp_seq, mac->flags); | |
1383 | return true; | |
1384 | } | |
24268cd0 | 1385 | |
b2998086 PR |
1386 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
1387 | zlog_debug( | |
1388 | "sync-macip ignore vni %u mac %s%s%s as existing has higher seq %u f 0x%x", | |
1389 | zevpn->vni, | |
1390 | prefix_mac2str(&mac->macaddr, macbuf, | |
1391 | sizeof(macbuf)), | |
1392 | ipa_len ? " IP " : "", | |
1393 | ipa_len ? ipaddr2str(ipaddr, ipbuf, | |
1394 | sizeof(ipbuf)) | |
1395 | : "", | |
1396 | tmp_seq, mac->flags); | |
1397 | return false; | |
24268cd0 PR |
1398 | } |
1399 | ||
b2998086 | 1400 | return true; |
24268cd0 PR |
1401 | } |
1402 | ||
b2998086 PR |
1403 | zebra_mac_t * |
1404 | zebra_evpn_proc_sync_mac_update(zebra_evpn_t *zevpn, struct ethaddr *macaddr, | |
1405 | uint16_t ipa_len, struct ipaddr *ipaddr, | |
1406 | uint8_t flags, uint32_t seq, esi_t *esi, | |
1407 | struct sync_mac_ip_ctx *ctx) | |
24268cd0 | 1408 | { |
b2998086 PR |
1409 | zebra_mac_t *mac; |
1410 | bool inform_bgp = false; | |
1411 | bool inform_dataplane = false; | |
1412 | bool seq_change = false; | |
1413 | bool es_change = false; | |
1414 | uint32_t tmp_seq; | |
1415 | char macbuf[ETHER_ADDR_STRLEN]; | |
1416 | char ipbuf[INET6_ADDRSTRLEN]; | |
1417 | bool old_local = false; | |
1418 | bool old_bgp_ready; | |
1419 | bool new_bgp_ready; | |
24268cd0 | 1420 | |
b2998086 PR |
1421 | mac = zebra_evpn_mac_lookup(zevpn, macaddr); |
1422 | if (!mac) { | |
1423 | /* if it is a new local path we need to inform both | |
1424 | * the control protocol and the data-plane | |
1425 | */ | |
1426 | inform_bgp = true; | |
1427 | inform_dataplane = true; | |
1428 | ctx->mac_created = true; | |
1429 | ctx->mac_inactive = true; | |
24268cd0 | 1430 | |
b2998086 PR |
1431 | /* create the MAC and associate it with the dest ES */ |
1432 | mac = zebra_evpn_mac_add(zevpn, macaddr); | |
1433 | zebra_evpn_es_mac_ref(mac, esi); | |
24268cd0 | 1434 | |
b2998086 PR |
1435 | /* local mac activated by an ES peer */ |
1436 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL); | |
1437 | /* if mac-only route setup peer flags */ | |
1438 | if (!ipa_len) { | |
1439 | if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT)) | |
1440 | SET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_PROXY); | |
1441 | else | |
1442 | SET_FLAG(mac->flags, ZEBRA_MAC_ES_PEER_ACTIVE); | |
1443 | } | |
1444 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE); | |
1445 | old_bgp_ready = false; | |
1446 | new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
1447 | } else { | |
1448 | uint32_t old_flags; | |
1449 | uint32_t new_flags; | |
1450 | bool old_static; | |
1451 | bool new_static; | |
1452 | bool sticky; | |
1453 | bool remote_gw; | |
24268cd0 | 1454 | |
b2998086 PR |
1455 | old_flags = mac->flags; |
1456 | sticky = !!CHECK_FLAG(old_flags, ZEBRA_MAC_STICKY); | |
1457 | remote_gw = !!CHECK_FLAG(old_flags, ZEBRA_MAC_REMOTE_DEF_GW); | |
1458 | if (sticky || remote_gw) { | |
1459 | if (IS_ZEBRA_DEBUG_EVPN_MH_NEIGH) | |
1460 | zlog_debug( | |
1461 | "Ignore sync-macip vni %u mac %s%s%s%s%s", | |
1462 | zevpn->vni, | |
1463 | prefix_mac2str(macaddr, macbuf, | |
1464 | sizeof(macbuf)), | |
1465 | ipa_len ? " IP " : "", | |
1466 | ipa_len ? ipaddr2str(ipaddr, ipbuf, | |
1467 | sizeof(ipbuf)) | |
1468 | : "", | |
1469 | sticky ? " sticky" : "", | |
1470 | remote_gw ? " remote_gw" : ""); | |
1471 | ctx->ignore_macip = true; | |
1472 | return NULL; | |
1473 | } | |
1474 | if (!zebra_evpn_mac_is_bgp_seq_ok(zevpn, mac, seq, ipa_len, | |
1475 | ipaddr)) { | |
1476 | ctx->ignore_macip = true; | |
1477 | return NULL; | |
1478 | } | |
24268cd0 | 1479 | |
b2998086 PR |
1480 | old_local = !!CHECK_FLAG(old_flags, ZEBRA_MAC_LOCAL); |
1481 | old_static = zebra_evpn_mac_is_static(mac); | |
24268cd0 | 1482 | |
b2998086 PR |
1483 | /* re-build the mac flags */ |
1484 | new_flags = 0; | |
1485 | SET_FLAG(new_flags, ZEBRA_MAC_LOCAL); | |
1486 | /* retain old local activity flag */ | |
1487 | if (old_flags & ZEBRA_MAC_LOCAL) { | |
1488 | new_flags |= (old_flags & ZEBRA_MAC_LOCAL_INACTIVE); | |
1489 | } else { | |
1490 | new_flags |= ZEBRA_MAC_LOCAL_INACTIVE; | |
1491 | ctx->mac_inactive = true; | |
1492 | } | |
1493 | if (ipa_len) { | |
1494 | /* if mac-ip route do NOT update the peer flags | |
1495 | * i.e. retain only flags as is | |
1496 | */ | |
1497 | new_flags |= (old_flags & ZEBRA_MAC_ALL_PEER_FLAGS); | |
1498 | } else { | |
1499 | /* if mac-only route update peer flags */ | |
1500 | if (CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_PROXY_ADVERT)) { | |
1501 | SET_FLAG(new_flags, ZEBRA_MAC_ES_PEER_PROXY); | |
1502 | /* if the mac was peer-active previously we | |
1503 | * need to keep the flag and start the | |
1504 | * holdtimer on it. the peer-active flag is | |
1505 | * cleared on holdtimer expiry. | |
1506 | */ | |
1507 | if (CHECK_FLAG(old_flags, | |
1508 | ZEBRA_MAC_ES_PEER_ACTIVE)) { | |
1509 | SET_FLAG(new_flags, | |
1510 | ZEBRA_MAC_ES_PEER_ACTIVE); | |
1511 | zebra_evpn_mac_start_hold_timer(mac); | |
1512 | } | |
1513 | } else { | |
1514 | SET_FLAG(new_flags, ZEBRA_MAC_ES_PEER_ACTIVE); | |
1515 | /* stop hold timer if a peer has verified | |
1516 | * reachability | |
1517 | */ | |
1518 | zebra_evpn_mac_stop_hold_timer(mac); | |
1519 | } | |
1520 | } | |
1521 | mac->rem_seq = 0; | |
1522 | memset(&mac->fwd_info, 0, sizeof(mac->fwd_info)); | |
1523 | mac->flags = new_flags; | |
24268cd0 | 1524 | |
b2998086 PR |
1525 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC && (old_flags != new_flags)) |
1526 | zlog_debug( | |
1527 | "sync-mac vni %u mac %s old_f 0x%x new_f 0x%x", | |
1528 | zevpn->vni, | |
1529 | prefix_mac2str(macaddr, macbuf, sizeof(macbuf)), | |
1530 | old_flags, mac->flags); | |
24268cd0 | 1531 | |
b2998086 PR |
1532 | /* update es */ |
1533 | es_change = zebra_evpn_es_mac_ref(mac, esi); | |
1534 | /* if mac dest change - inform both sides */ | |
1535 | if (es_change) { | |
1536 | inform_bgp = true; | |
1537 | inform_dataplane = true; | |
1538 | ctx->mac_inactive = true; | |
1539 | } | |
1540 | /* if peer-flag is being set notify dataplane that the | |
1541 | * entry must not be expired because of local inactivity | |
1542 | */ | |
1543 | new_static = zebra_evpn_mac_is_static(mac); | |
1544 | if (old_static != new_static) | |
1545 | inform_dataplane = true; | |
24268cd0 | 1546 | |
b2998086 PR |
1547 | old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(old_flags); |
1548 | new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
1549 | if (old_bgp_ready != new_bgp_ready) | |
1550 | inform_bgp = true; | |
1551 | } | |
24268cd0 | 1552 | |
24268cd0 | 1553 | |
b2998086 PR |
1554 | /* update sequence number; if that results in a new local sequence |
1555 | * inform bgp | |
1556 | */ | |
1557 | tmp_seq = MAX(mac->loc_seq, seq); | |
1558 | if (tmp_seq != mac->loc_seq) { | |
1559 | mac->loc_seq = tmp_seq; | |
1560 | seq_change = true; | |
1561 | inform_bgp = true; | |
1562 | } | |
24268cd0 | 1563 | |
b2998086 PR |
1564 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) |
1565 | zlog_debug("sync-mac %s vni %u mac %s es %s seq %d f 0x%x%s%s", | |
1566 | ctx->mac_created ? "created" : "updated", zevpn->vni, | |
1567 | prefix_mac2str(macaddr, macbuf, sizeof(macbuf)), | |
1568 | mac->es ? mac->es->esi_str : "-", mac->loc_seq, | |
1569 | mac->flags, inform_bgp ? " inform_bgp" : "", | |
1570 | inform_dataplane ? " inform_dp" : ""); | |
24268cd0 | 1571 | |
b2998086 PR |
1572 | if (inform_bgp) |
1573 | zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready, | |
1574 | new_bgp_ready); | |
24268cd0 | 1575 | |
b2998086 PR |
1576 | /* neighs using the mac may need to be re-sent to |
1577 | * bgp with updated info | |
1578 | */ | |
1579 | if (seq_change || es_change || !old_local) | |
1580 | zebra_evpn_process_neigh_on_local_mac_change( | |
1581 | zevpn, mac, seq_change, es_change); | |
24268cd0 | 1582 | |
b2998086 PR |
1583 | if (inform_dataplane) { |
1584 | if (ipa_len) | |
1585 | /* if the mac is being created as a part of MAC-IP | |
1586 | * route wait for the neigh to be updated or | |
1587 | * created before programming the mac | |
1588 | */ | |
1589 | ctx->mac_dp_update_deferred = true; | |
1590 | else | |
1591 | /* program the local mac in the kernel. when the ES | |
1592 | * change we need to force the dataplane to reset | |
1593 | * the activity as we are yet to establish activity | |
1594 | * locally | |
1595 | */ | |
1596 | zebra_evpn_sync_mac_dp_install( | |
1597 | mac, ctx->mac_inactive, | |
1598 | false /* force_clear_static */, __func__); | |
1599 | } | |
24268cd0 | 1600 | |
b2998086 | 1601 | return mac; |
24268cd0 PR |
1602 | } |
1603 | ||
b2998086 PR |
1604 | /* update local fowarding info. return true if a dest-ES change |
1605 | * is detected | |
1606 | */ | |
d9d3455e PR |
1607 | static bool zebra_evpn_local_mac_update_fwd_info(zebra_mac_t *mac, |
1608 | struct interface *ifp, | |
1609 | vlanid_t vid) | |
24268cd0 | 1610 | { |
b2998086 PR |
1611 | struct zebra_if *zif = ifp->info; |
1612 | bool es_change; | |
24268cd0 | 1613 | |
b2998086 | 1614 | memset(&mac->fwd_info, 0, sizeof(mac->fwd_info)); |
24268cd0 | 1615 | |
b2998086 | 1616 | es_change = zebra_evpn_es_mac_ref_entry(mac, zif->es_info.es); |
24268cd0 | 1617 | |
b2998086 PR |
1618 | if (!mac->es) { |
1619 | /* if es is set fwd_info is not-relevant/taped-out */ | |
1620 | mac->fwd_info.local.ifindex = ifp->ifindex; | |
1621 | mac->fwd_info.local.vid = vid; | |
24268cd0 PR |
1622 | } |
1623 | ||
b2998086 | 1624 | return es_change; |
24268cd0 PR |
1625 | } |
1626 | ||
24268cd0 | 1627 | /* Notify Local MACs to the clienti, skips GW MAC */ |
b2998086 PR |
1628 | static void zebra_evpn_send_mac_hash_entry_to_client(struct hash_bucket *bucket, |
1629 | void *arg) | |
24268cd0 PR |
1630 | { |
1631 | struct mac_walk_ctx *wctx = arg; | |
1632 | zebra_mac_t *zmac = bucket->data; | |
1633 | ||
1634 | if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_DEF_GW)) | |
1635 | return; | |
1636 | ||
1637 | if (CHECK_FLAG(zmac->flags, ZEBRA_MAC_LOCAL)) | |
b2998086 PR |
1638 | zebra_evpn_mac_send_add_to_client(wctx->zevpn->vni, |
1639 | &zmac->macaddr, zmac->flags, | |
1640 | zmac->loc_seq, zmac->es); | |
24268cd0 PR |
1641 | } |
1642 | ||
1643 | /* Iterator to Notify Local MACs of a EVPN */ | |
b2998086 | 1644 | void zebra_evpn_send_mac_list_to_client(zebra_evpn_t *zevpn) |
24268cd0 PR |
1645 | { |
1646 | struct mac_walk_ctx wctx; | |
1647 | ||
1648 | if (!zevpn->mac_table) | |
1649 | return; | |
1650 | ||
1651 | memset(&wctx, 0, sizeof(struct mac_walk_ctx)); | |
1652 | wctx.zevpn = zevpn; | |
1653 | ||
b2998086 PR |
1654 | hash_iterate(zevpn->mac_table, zebra_evpn_send_mac_hash_entry_to_client, |
1655 | &wctx); | |
24268cd0 PR |
1656 | } |
1657 | ||
b2998086 | 1658 | void zebra_evpn_rem_mac_del(zebra_evpn_t *zevpn, zebra_mac_t *mac) |
24268cd0 | 1659 | { |
b2998086 PR |
1660 | zebra_evpn_process_neigh_on_remote_mac_del(zevpn, mac); |
1661 | /* the remote sequence number in the auto mac entry | |
1662 | * needs to be reset to 0 as the mac entry may have | |
1663 | * been removed on all VTEPs (including | |
1664 | * the originating one) | |
1665 | */ | |
1666 | mac->rem_seq = 0; | |
24268cd0 | 1667 | |
b2998086 PR |
1668 | /* If all remote neighbors referencing a remote MAC |
1669 | * go away, we need to uninstall the MAC. | |
1670 | */ | |
1671 | if (remote_neigh_count(mac) == 0) { | |
1672 | zebra_evpn_rem_mac_uninstall(zevpn, mac); | |
1673 | zebra_evpn_es_mac_deref_entry(mac); | |
1674 | UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE); | |
24268cd0 PR |
1675 | } |
1676 | ||
b2998086 PR |
1677 | if (list_isempty(mac->neigh_list)) |
1678 | zebra_evpn_mac_del(zevpn, mac); | |
1679 | else | |
1680 | SET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
24268cd0 PR |
1681 | } |
1682 | ||
b2998086 PR |
1683 | /* Print Duplicate MAC */ |
1684 | void zebra_evpn_print_dad_mac_hash(struct hash_bucket *bucket, void *ctxt) | |
24268cd0 | 1685 | { |
b2998086 | 1686 | zebra_mac_t *mac; |
24268cd0 | 1687 | |
b2998086 PR |
1688 | mac = (zebra_mac_t *)bucket->data; |
1689 | if (!mac) | |
1690 | return; | |
24268cd0 | 1691 | |
b2998086 PR |
1692 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) |
1693 | zebra_evpn_print_mac_hash(bucket, ctxt); | |
24268cd0 PR |
1694 | } |
1695 | ||
b2998086 PR |
1696 | /* Print Duplicate MAC in detail */ |
1697 | void zebra_evpn_print_dad_mac_hash_detail(struct hash_bucket *bucket, | |
1698 | void *ctxt) | |
24268cd0 | 1699 | { |
b2998086 | 1700 | zebra_mac_t *mac; |
24268cd0 | 1701 | |
b2998086 PR |
1702 | mac = (zebra_mac_t *)bucket->data; |
1703 | if (!mac) | |
1704 | return; | |
24268cd0 | 1705 | |
b2998086 PR |
1706 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) |
1707 | zebra_evpn_print_mac_hash_detail(bucket, ctxt); | |
24268cd0 | 1708 | } |
19fdd1be PR |
1709 | |
1710 | int process_mac_remote_macip_add(zebra_evpn_t *zevpn, struct zebra_vrf *zvrf, | |
1711 | struct ethaddr *macaddr, uint16_t ipa_len, | |
1712 | struct ipaddr *ipaddr, zebra_mac_t **macp, | |
1713 | struct in_addr vtep_ip, uint8_t flags, | |
1714 | uint32_t seq, esi_t *esi) | |
1715 | { | |
1716 | char buf[ETHER_ADDR_STRLEN]; | |
1717 | char buf1[INET6_ADDRSTRLEN]; | |
1718 | uint32_t tmp_seq; | |
1719 | bool sticky; | |
1720 | bool remote_gw; | |
1721 | int update_mac = 0; | |
1722 | bool do_dad = false; | |
1723 | bool is_dup_detect = false; | |
1724 | esi_t *old_esi; | |
1725 | bool old_static = false; | |
1726 | zebra_mac_t *mac; | |
1727 | ||
1728 | sticky = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_STICKY); | |
1729 | remote_gw = !!CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW); | |
1730 | ||
1731 | mac = zebra_evpn_mac_lookup(zevpn, macaddr); | |
1732 | ||
1733 | /* Ignore if the mac is already present as a gateway mac */ | |
1734 | if (mac && CHECK_FLAG(mac->flags, ZEBRA_MAC_DEF_GW) | |
1735 | && CHECK_FLAG(flags, ZEBRA_MACIP_TYPE_GW)) { | |
1736 | if (IS_ZEBRA_DEBUG_VXLAN) | |
1737 | zlog_debug( | |
1738 | "Ignore remote MACIP ADD VNI %u MAC %s%s%s as MAC is already configured as gateway MAC", | |
1739 | zevpn->vni, | |
1740 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
1741 | ipa_len ? " IP " : "", | |
1742 | ipa_len ? ipaddr2str(ipaddr, buf1, sizeof(buf1)) | |
1743 | : ""); | |
1744 | return -1; | |
1745 | } | |
1746 | ||
1747 | old_esi = (mac && mac->es) ? &mac->es->esi : zero_esi; | |
1748 | ||
1749 | /* check if the remote MAC is unknown or has a change. | |
1750 | * If so, that needs to be updated first. Note that client could | |
1751 | * install MAC and MACIP separately or just install the latter. | |
1752 | */ | |
1753 | if (!mac || !CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) | |
1754 | || sticky != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY) | |
1755 | || remote_gw != !!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW) | |
1756 | || !IPV4_ADDR_SAME(&mac->fwd_info.r_vtep_ip, &vtep_ip) | |
1757 | || memcmp(old_esi, esi, sizeof(esi_t)) || seq != mac->rem_seq) | |
1758 | update_mac = 1; | |
1759 | ||
1760 | if (update_mac) { | |
1761 | if (!mac) { | |
1762 | mac = zebra_evpn_mac_add(zevpn, macaddr); | |
1763 | if (!mac) { | |
1764 | zlog_warn( | |
1765 | "Failed to add MAC %s VNI %u Remote VTEP %s", | |
1766 | prefix_mac2str(macaddr, buf, | |
1767 | sizeof(buf)), | |
1768 | zevpn->vni, inet_ntoa(vtep_ip)); | |
1769 | return -1; | |
1770 | } | |
1771 | ||
1772 | zebra_evpn_es_mac_ref(mac, esi); | |
1773 | ||
1774 | /* Is this MAC created for a MACIP? */ | |
1775 | if (ipa_len) | |
1776 | SET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
1777 | } else { | |
1778 | zebra_evpn_es_mac_ref(mac, esi); | |
1779 | ||
1780 | /* When host moves but changes its (MAC,IP) | |
1781 | * binding, BGP may install a MACIP entry that | |
1782 | * corresponds to "older" location of the host | |
1783 | * in transient situations (because {IP1,M1} | |
1784 | * is a different route from {IP1,M2}). Check | |
1785 | * the sequence number and ignore this update | |
1786 | * if appropriate. | |
1787 | */ | |
1788 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) | |
1789 | tmp_seq = mac->loc_seq; | |
1790 | else | |
1791 | tmp_seq = mac->rem_seq; | |
1792 | ||
1793 | if (seq < tmp_seq) { | |
1794 | if (IS_ZEBRA_DEBUG_VXLAN) | |
1795 | zlog_debug( | |
1796 | "Ignore remote MACIP ADD VNI %u MAC %s%s%s as existing MAC has higher seq %u flags 0x%x", | |
1797 | zevpn->vni, | |
1798 | prefix_mac2str(macaddr, buf, | |
1799 | sizeof(buf)), | |
1800 | ipa_len ? " IP " : "", | |
1801 | ipa_len ? ipaddr2str( | |
1802 | ipaddr, buf1, | |
1803 | sizeof(buf1)) | |
1804 | : "", | |
1805 | tmp_seq, mac->flags); | |
1806 | return -1; | |
1807 | } | |
1808 | } | |
1809 | ||
1810 | /* Check MAC's curent state is local (this is the case | |
1811 | * where MAC has moved from L->R) and check previous | |
1812 | * detection started via local learning. | |
1813 | * RFC-7432: A PE/VTEP that detects a MAC mobility | |
1814 | * event via local learning starts an M-second timer. | |
1815 | * | |
1816 | * VTEP-IP or seq. change alone is not considered | |
1817 | * for dup. detection. | |
1818 | * | |
1819 | * MAC is already marked duplicate set dad, then | |
1820 | * is_dup_detect will be set to not install the entry. | |
1821 | */ | |
1822 | if ((!CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) | |
1823 | && mac->dad_count) | |
1824 | || CHECK_FLAG(mac->flags, ZEBRA_MAC_DUPLICATE)) | |
1825 | do_dad = true; | |
1826 | ||
1827 | /* Remove local MAC from BGP. */ | |
1828 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { | |
1829 | /* force drop the sync flags */ | |
1830 | old_static = zebra_evpn_mac_is_static(mac); | |
1831 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1832 | zlog_debug( | |
1833 | "sync-mac->remote vni %u mac %s es %s seq %d f 0x%x", | |
1834 | zevpn->vni, | |
1835 | prefix_mac2str(macaddr, buf, | |
1836 | sizeof(buf)), | |
1837 | mac->es ? mac->es->esi_str : "-", | |
1838 | mac->loc_seq, mac->flags); | |
1839 | zebra_evpn_mac_clear_sync_info(mac); | |
1840 | zebra_evpn_mac_send_del_to_client(zevpn->vni, macaddr, | |
1841 | mac->flags, | |
1842 | false /* force */); | |
1843 | } | |
1844 | ||
1845 | /* Set "auto" and "remote" forwarding info. */ | |
1846 | UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS); | |
1847 | memset(&mac->fwd_info, 0, sizeof(mac->fwd_info)); | |
1848 | SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE); | |
1849 | mac->fwd_info.r_vtep_ip = vtep_ip; | |
1850 | ||
1851 | if (sticky) | |
1852 | SET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
1853 | else | |
1854 | UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
1855 | ||
1856 | if (remote_gw) | |
1857 | SET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW); | |
1858 | else | |
1859 | UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE_DEF_GW); | |
1860 | ||
1861 | zebra_evpn_dup_addr_detect_for_mac( | |
1862 | zvrf, mac, mac->fwd_info.r_vtep_ip, do_dad, | |
1863 | &is_dup_detect, false); | |
1864 | ||
1865 | if (!is_dup_detect) { | |
1866 | zebra_evpn_process_neigh_on_remote_mac_add(zevpn, mac); | |
1867 | /* Install the entry. */ | |
1868 | zebra_evpn_rem_mac_install(zevpn, mac, old_static); | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | /* Update seq number. */ | |
1873 | mac->rem_seq = seq; | |
1874 | ||
1875 | /* If there is no IP, return after clearing AUTO flag of MAC. */ | |
1876 | if (!ipa_len) { | |
1877 | UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
1878 | return -1; | |
1879 | } | |
1880 | *macp = mac; | |
1881 | return 0; | |
1882 | } | |
d9d3455e PR |
1883 | |
1884 | int zebra_evpn_add_update_local_mac(struct zebra_vrf *zvrf, zebra_evpn_t *zevpn, | |
1885 | struct interface *ifp, | |
1886 | struct ethaddr *macaddr, vlanid_t vid, | |
1887 | bool sticky, bool local_inactive, | |
1888 | bool dp_static) | |
1889 | { | |
1890 | zebra_mac_t *mac; | |
1891 | char buf[ETHER_ADDR_STRLEN]; | |
1892 | bool mac_sticky = false; | |
1893 | bool inform_client = false; | |
1894 | bool upd_neigh = false; | |
1895 | bool is_dup_detect = false; | |
1896 | struct in_addr vtep_ip = {.s_addr = 0}; | |
1897 | bool es_change = false; | |
1898 | bool new_bgp_ready; | |
1899 | /* assume inactive if not present or if not local */ | |
1900 | bool old_local_inactive = true; | |
1901 | bool old_bgp_ready = false; | |
1902 | bool inform_dataplane = false; | |
1903 | bool new_static = false; | |
1904 | ||
1905 | /* Check if we need to create or update or it is a NO-OP. */ | |
1906 | mac = zebra_evpn_mac_lookup(zevpn, macaddr); | |
1907 | if (!mac) { | |
1908 | if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1909 | zlog_debug( | |
1910 | "ADD %sMAC %s intf %s(%u) VID %u -> VNI %u%s", | |
1911 | sticky ? "sticky " : "", | |
1912 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
1913 | ifp->name, ifp->ifindex, vid, zevpn->vni, | |
1914 | local_inactive ? " local-inactive" : ""); | |
1915 | ||
1916 | mac = zebra_evpn_mac_add(zevpn, macaddr); | |
1917 | if (!mac) { | |
1918 | flog_err( | |
1919 | EC_ZEBRA_MAC_ADD_FAILED, | |
1920 | "Failed to add MAC %s intf %s(%u) VID %u VNI %u", | |
1921 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
1922 | ifp->name, ifp->ifindex, vid, zevpn->vni); | |
1923 | return -1; | |
1924 | } | |
1925 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL); | |
1926 | es_change = zebra_evpn_local_mac_update_fwd_info(mac, ifp, vid); | |
1927 | if (sticky) | |
1928 | SET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
1929 | inform_client = true; | |
1930 | } else { | |
1931 | if (IS_ZEBRA_DEBUG_VXLAN || IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
1932 | zlog_debug( | |
1933 | "UPD %sMAC %s intf %s(%u) VID %u -> VNI %u %scurFlags 0x%x", | |
1934 | sticky ? "sticky " : "", | |
1935 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
1936 | ifp->name, ifp->ifindex, vid, zevpn->vni, | |
1937 | local_inactive ? "local-inactive " : "", | |
1938 | mac->flags); | |
1939 | ||
1940 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) { | |
1941 | struct interface *old_ifp; | |
1942 | vlanid_t old_vid; | |
1943 | bool old_static; | |
1944 | ||
1945 | zebra_evpn_mac_get_access_info(mac, &old_ifp, &old_vid); | |
1946 | old_bgp_ready = | |
1947 | zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
1948 | old_local_inactive = | |
1949 | !!(mac->flags & ZEBRA_MAC_LOCAL_INACTIVE); | |
1950 | old_static = zebra_evpn_mac_is_static(mac); | |
1951 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) | |
1952 | mac_sticky = true; | |
1953 | ||
1954 | /* | |
1955 | * Update any changes and if changes are relevant to | |
1956 | * BGP, note it. | |
1957 | */ | |
1958 | if (mac_sticky == sticky && old_ifp == ifp | |
1959 | && old_vid == vid | |
1960 | && old_local_inactive == local_inactive | |
1961 | && dp_static == old_static) { | |
1962 | if (IS_ZEBRA_DEBUG_VXLAN) | |
1963 | zlog_debug( | |
1964 | " Add/Update %sMAC %s intf %s(%u) VID %u -> VNI %u%s, " | |
1965 | "entry exists and has not changed ", | |
1966 | sticky ? "sticky " : "", | |
1967 | prefix_mac2str(macaddr, buf, | |
1968 | sizeof(buf)), | |
1969 | ifp->name, ifp->ifindex, vid, | |
1970 | zevpn->vni, | |
1971 | local_inactive | |
1972 | ? " local_inactive" | |
1973 | : ""); | |
1974 | return 0; | |
1975 | } | |
1976 | if (mac_sticky != sticky) { | |
1977 | if (sticky) | |
1978 | SET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
1979 | else | |
1980 | UNSET_FLAG(mac->flags, | |
1981 | ZEBRA_MAC_STICKY); | |
1982 | inform_client = true; | |
1983 | } | |
1984 | ||
1985 | es_change = zebra_evpn_local_mac_update_fwd_info( | |
1986 | mac, ifp, vid); | |
1987 | /* If an es_change is detected we need to advertise | |
1988 | * the route with a sequence that is one | |
1989 | * greater. This is need to indicate a mac-move | |
1990 | * to the ES peers | |
1991 | */ | |
1992 | if (es_change) { | |
1993 | mac->loc_seq = mac->loc_seq + 1; | |
1994 | /* force drop the peer/sync info as it is | |
1995 | * simply no longer relevant | |
1996 | */ | |
1997 | if (CHECK_FLAG(mac->flags, | |
1998 | ZEBRA_MAC_ALL_PEER_FLAGS)) { | |
1999 | zebra_evpn_mac_clear_sync_info(mac); | |
2000 | new_static = | |
2001 | zebra_evpn_mac_is_static(mac); | |
2002 | /* if we clear peer-flags we | |
2003 | * also need to notify the dataplane | |
2004 | * to drop the static flag | |
2005 | */ | |
2006 | if (old_static != new_static) | |
2007 | inform_dataplane = true; | |
2008 | } | |
2009 | } | |
2010 | } else if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE) | |
2011 | || CHECK_FLAG(mac->flags, ZEBRA_MAC_AUTO)) { | |
2012 | bool do_dad = false; | |
2013 | ||
2014 | /* | |
2015 | * MAC has either moved or was "internally" created due | |
2016 | * to a neighbor learn and is now actually learnt. If | |
2017 | * it was learnt as a remote sticky MAC, this is an | |
2018 | * operator error. | |
2019 | */ | |
2020 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_STICKY)) { | |
2021 | flog_warn( | |
2022 | EC_ZEBRA_STICKY_MAC_ALREADY_LEARNT, | |
2023 | "MAC %s already learnt as remote sticky MAC behind VTEP %s VNI %u", | |
2024 | prefix_mac2str(macaddr, buf, | |
2025 | sizeof(buf)), | |
2026 | inet_ntoa(mac->fwd_info.r_vtep_ip), | |
2027 | zevpn->vni); | |
2028 | return 0; | |
2029 | } | |
2030 | ||
2031 | /* If an actual move, compute MAC's seq number */ | |
2032 | if (CHECK_FLAG(mac->flags, ZEBRA_MAC_REMOTE)) { | |
2033 | mac->loc_seq = | |
2034 | MAX(mac->rem_seq + 1, mac->loc_seq); | |
2035 | vtep_ip = mac->fwd_info.r_vtep_ip; | |
2036 | /* Trigger DAD for remote MAC */ | |
2037 | do_dad = true; | |
2038 | } | |
2039 | ||
2040 | UNSET_FLAG(mac->flags, ZEBRA_MAC_REMOTE); | |
2041 | UNSET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
2042 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL); | |
2043 | es_change = zebra_evpn_local_mac_update_fwd_info( | |
2044 | mac, ifp, vid); | |
2045 | if (sticky) | |
2046 | SET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
2047 | else | |
2048 | UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
2049 | /* | |
2050 | * We have to inform BGP of this MAC as well as process | |
2051 | * all neighbors. | |
2052 | */ | |
2053 | inform_client = true; | |
2054 | upd_neigh = true; | |
2055 | ||
2056 | zebra_evpn_dup_addr_detect_for_mac( | |
2057 | zvrf, mac, vtep_ip, do_dad, &is_dup_detect, | |
2058 | true); | |
2059 | if (is_dup_detect) { | |
2060 | inform_client = false; | |
2061 | upd_neigh = false; | |
2062 | } | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | /* if the dataplane thinks the entry is sync but it is | |
2067 | * not sync in zebra we need to re-install to fixup | |
2068 | */ | |
2069 | if (dp_static) { | |
2070 | new_static = zebra_evpn_mac_is_static(mac); | |
2071 | if (!new_static) | |
2072 | inform_dataplane = true; | |
2073 | } | |
2074 | ||
2075 | if (local_inactive) | |
2076 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE); | |
2077 | else | |
2078 | UNSET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE); | |
2079 | ||
2080 | new_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
2081 | /* if local-activity has changed we need update bgp | |
2082 | * even if bgp already knows about the mac | |
2083 | */ | |
2084 | if ((old_local_inactive != local_inactive) | |
2085 | || (new_bgp_ready != old_bgp_ready)) { | |
2086 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
2087 | zlog_debug( | |
2088 | "local mac vni %u mac %s es %s seq %d f 0x%x%s", | |
2089 | zevpn->vni, | |
2090 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
2091 | mac->es ? mac->es->esi_str : "", mac->loc_seq, | |
2092 | mac->flags, | |
2093 | local_inactive ? " local-inactive" : ""); | |
2094 | inform_client = true; | |
2095 | } | |
2096 | ||
2097 | if (es_change) { | |
2098 | inform_client = true; | |
2099 | upd_neigh = true; | |
2100 | } | |
2101 | ||
2102 | /* Inform dataplane if required. */ | |
2103 | if (inform_dataplane) | |
2104 | zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */, | |
2105 | false /* force_clear_static */, | |
2106 | __func__); | |
2107 | ||
2108 | /* Inform BGP if required. */ | |
2109 | if (inform_client) | |
2110 | zebra_evpn_mac_send_add_del_to_client(mac, old_bgp_ready, | |
2111 | new_bgp_ready); | |
2112 | ||
2113 | /* Process all neighbors associated with this MAC, if required. */ | |
2114 | if (upd_neigh) | |
2115 | zebra_evpn_process_neigh_on_local_mac_change(zevpn, mac, 0, | |
2116 | es_change); | |
2117 | ||
2118 | return 0; | |
2119 | } | |
ad6ca5f4 PR |
2120 | |
2121 | int zebra_evpn_del_local_mac(zebra_evpn_t *zevpn, struct ethaddr *macaddr, | |
2122 | struct interface *ifp) | |
2123 | { | |
2124 | zebra_mac_t *mac; | |
2125 | char buf[ETHER_ADDR_STRLEN]; | |
2126 | bool old_bgp_ready; | |
2127 | bool new_bgp_ready; | |
2128 | /* If entry doesn't exist, nothing to do. */ | |
2129 | mac = zebra_evpn_mac_lookup(zevpn, macaddr); | |
2130 | if (!mac) | |
2131 | return 0; | |
2132 | ||
2133 | /* Is it a local entry? */ | |
2134 | if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL)) | |
2135 | return 0; | |
2136 | ||
2137 | if (IS_ZEBRA_DEBUG_VXLAN) | |
2138 | zlog_debug( | |
2139 | "DEL MAC %s intf %s(%u) VID %u -> VNI %u seq %u flags 0x%x nbr count %u", | |
2140 | prefix_mac2str(macaddr, buf, sizeof(buf)), ifp->name, | |
2141 | ifp->ifindex, mac->fwd_info.local.vid, zevpn->vni, | |
2142 | mac->loc_seq, mac->flags, listcount(mac->neigh_list)); | |
2143 | ||
2144 | old_bgp_ready = zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
2145 | if (zebra_evpn_mac_is_static(mac)) { | |
2146 | /* this is a synced entry and can only be removed when the | |
2147 | * es-peers stop advertising it. | |
2148 | */ | |
2149 | memset(&mac->fwd_info, 0, sizeof(mac->fwd_info)); | |
2150 | ||
2151 | if (IS_ZEBRA_DEBUG_EVPN_MH_MAC) | |
2152 | zlog_debug( | |
2153 | "re-add sync-mac vni %u mac %s es %s seq %d f 0x%x", | |
2154 | zevpn->vni, | |
2155 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
2156 | mac->es ? mac->es->esi_str : "-", mac->loc_seq, | |
2157 | mac->flags); | |
2158 | ||
2159 | /* inform-bgp about change in local-activity if any */ | |
2160 | if (!CHECK_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE)) { | |
2161 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL_INACTIVE); | |
2162 | new_bgp_ready = | |
2163 | zebra_evpn_mac_is_ready_for_bgp(mac->flags); | |
2164 | zebra_evpn_mac_send_add_del_to_client( | |
2165 | mac, old_bgp_ready, new_bgp_ready); | |
2166 | } | |
2167 | ||
2168 | /* re-install the entry in the kernel */ | |
2169 | zebra_evpn_sync_mac_dp_install(mac, false /* set_inactive */, | |
2170 | false /* force_clear_static */, | |
2171 | __func__); | |
2172 | ||
2173 | return 0; | |
2174 | } | |
2175 | ||
2176 | /* Update all the neigh entries associated with this mac */ | |
2177 | zebra_evpn_process_neigh_on_local_mac_del(zevpn, mac); | |
2178 | ||
2179 | /* Remove MAC from BGP. */ | |
2180 | zebra_evpn_mac_send_del_to_client(zevpn->vni, macaddr, mac->flags, | |
2181 | false /* force */); | |
2182 | ||
2183 | zebra_evpn_es_mac_deref_entry(mac); | |
2184 | ||
2185 | /* | |
2186 | * If there are no neigh associated with the mac delete the mac | |
2187 | * else mark it as AUTO for forward reference | |
2188 | */ | |
2189 | if (!listcount(mac->neigh_list)) { | |
2190 | zebra_evpn_mac_del(zevpn, mac); | |
2191 | } else { | |
2192 | UNSET_FLAG(mac->flags, ZEBRA_MAC_ALL_LOCAL_FLAGS); | |
2193 | UNSET_FLAG(mac->flags, ZEBRA_MAC_STICKY); | |
2194 | SET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
2195 | } | |
2196 | ||
2197 | return 0; | |
2198 | } | |
7bce3535 PR |
2199 | |
2200 | int zebra_evpn_mac_gw_macip_add(struct interface *ifp, zebra_evpn_t *zevpn, | |
2201 | struct ipaddr *ip, zebra_mac_t **macp, | |
2202 | struct ethaddr *macaddr, vlanid_t vlan_id) | |
2203 | { | |
2204 | char buf[ETHER_ADDR_STRLEN]; | |
2205 | zebra_mac_t *mac; | |
2206 | ||
2207 | mac = zebra_evpn_mac_lookup(zevpn, macaddr); | |
2208 | if (!mac) { | |
2209 | mac = zebra_evpn_mac_add(zevpn, macaddr); | |
2210 | if (!mac) { | |
2211 | flog_err(EC_ZEBRA_MAC_ADD_FAILED, | |
2212 | "Failed to add MAC %s intf %s(%u) VID %u", | |
2213 | prefix_mac2str(macaddr, buf, sizeof(buf)), | |
2214 | ifp->name, ifp->ifindex, vlan_id); | |
2215 | return -1; | |
2216 | } | |
2217 | } | |
2218 | ||
2219 | /* Set "local" forwarding info. */ | |
2220 | SET_FLAG(mac->flags, ZEBRA_MAC_LOCAL); | |
2221 | SET_FLAG(mac->flags, ZEBRA_MAC_AUTO); | |
2222 | SET_FLAG(mac->flags, ZEBRA_MAC_DEF_GW); | |
2223 | memset(&mac->fwd_info, 0, sizeof(mac->fwd_info)); | |
2224 | mac->fwd_info.local.ifindex = ifp->ifindex; | |
2225 | mac->fwd_info.local.vid = vlan_id; | |
2226 | ||
2227 | *macp = mac; | |
2228 | ||
2229 | return 0; | |
2230 | } |