Skip to content

Commit 577d13e

Browse files
author
Mark Stapp
committed
lib,zebra: replace VRF_GET_ID macro
Remove a macro and replace it with a simple function. The macro returned from the calling function in error paths; but since the application code wasn't involved, it wasn't able to clean up memory allocations. Replace the macro with a simple function that returns to the application code; the application can then handle errors itself. Signed-off-by: Mark Stapp <[email protected]>
1 parent 0975ac5 commit 577d13e

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

lib/vrf.h

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,32 @@ extern const char *vrf_id_to_name(vrf_id_t vrf_id);
108108

109109
#define VRF_LOGNAME(V) V ? V->name : "Unknown"
110110

111-
#define VRF_GET_ID(V, NAME, USE_JSON) \
112-
do { \
113-
struct vrf *_vrf; \
114-
if (!(_vrf = vrf_lookup_by_name(NAME))) { \
115-
if (USE_JSON) { \
116-
vty_out(vty, "{}\n"); \
117-
} else { \
118-
vty_out(vty, "%% VRF %s not found\n", NAME); \
119-
} \
120-
return CMD_WARNING; \
121-
} \
122-
if (_vrf->vrf_id == VRF_UNKNOWN) { \
123-
if (USE_JSON) { \
124-
vty_out(vty, "{}\n"); \
125-
} else { \
126-
vty_out(vty, "%% VRF %s not active\n", NAME); \
127-
} \
128-
return CMD_WARNING; \
129-
} \
130-
(V) = _vrf->vrf_id; \
131-
} while (0)
111+
/* Utility for lookups in a cli/vty context */
112+
static inline bool vrf_get_id(struct vty *vty, vrf_id_t *pvrfid, const char *name,
113+
bool use_json)
114+
{
115+
struct vrf *_vrf;
116+
117+
if (!(_vrf = vrf_lookup_by_name(name))) {
118+
if (use_json)
119+
vty_out(vty, "{}\n");
120+
else
121+
vty_out(vty, "%% VRF %s not found\n", name);
122+
123+
return false;
124+
}
125+
if (_vrf->vrf_id == VRF_UNKNOWN) {
126+
if (use_json)
127+
vty_out(vty, "{}\n");
128+
else
129+
vty_out(vty, "%% VRF %s not active\n", name);
130+
131+
return false;
132+
}
133+
*pvrfid = _vrf->vrf_id;
134+
135+
return true;
136+
}
132137

133138
/*
134139
* Check whether the VRF is enabled.

zebra/router-id.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ DEFUN (show_ip_router_id,
258258
is_ipv6 = argv_find(argv, argc, "ipv6", &idx);
259259

260260
if (argv_find(argv, argc, "NAME", &idx)) {
261-
VRF_GET_ID(vrf_id, argv[idx]->arg, false);
261+
if (!vrf_get_id(vty, &vrf_id, argv[idx]->arg, false))
262+
return CMD_WARNING;
262263
vrf_name = argv[idx]->arg;
263264
}
264265

zebra/zebra_routemap.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ static int show_proto_rm(struct vty *vty, int af_type, const char *vrf_all,
179179
} else {
180180
vrf_id_t vrf_id = VRF_DEFAULT;
181181

182-
if (vrf_name)
183-
VRF_GET_ID(vrf_id, vrf_name, false);
182+
if (vrf_name) {
183+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
184+
return CMD_WARNING;
185+
}
184186

185187
zvrf = zebra_vrf_lookup_by_id(vrf_id);
186188
if (!zvrf)
@@ -202,8 +204,10 @@ static int show_nht_rm(struct vty *vty, int af_type, const char *vrf_all,
202204
json_object *json_vrfs = NULL;
203205

204206
/* Check for single vrf name. Note that this macro returns on error. */
205-
if (vrf_all == NULL && vrf_name != NULL)
206-
VRF_GET_ID(vrf_id, vrf_name, false);
207+
if (vrf_all == NULL && vrf_name != NULL) {
208+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
209+
return CMD_WARNING;
210+
}
207211

208212
if (use_json) {
209213
json = json_object_new_object();

zebra/zebra_vty.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,10 @@ DEFPY (show_ip_nht,
10131013

10141014
return CMD_SUCCESS;
10151015
}
1016-
if (vrf_name)
1017-
VRF_GET_ID(vrf_id, vrf_name, false);
1016+
if (vrf_name) {
1017+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
1018+
return CMD_WARNING;
1019+
}
10181020

10191021
memset(&prefix, 0, sizeof(prefix));
10201022
if (addr) {
@@ -1700,8 +1702,10 @@ DEFPY (show_route,
17001702
} else {
17011703
vrf_id_t vrf_id = VRF_DEFAULT;
17021704

1703-
if (vrf_name)
1704-
VRF_GET_ID(vrf_id, vrf_name, !!json);
1705+
if (vrf_name) {
1706+
if (!vrf_get_id(vty, &vrf_id, vrf_name, !!json))
1707+
return CMD_WARNING;
1708+
}
17051709
vrf = vrf_lookup_by_id(vrf_id);
17061710
if (!vrf)
17071711
return CMD_SUCCESS;
@@ -1869,8 +1873,10 @@ DEFPY (show_route_detail,
18691873
} else {
18701874
vrf_id_t vrf_id = VRF_DEFAULT;
18711875

1872-
if (vrf_name)
1873-
VRF_GET_ID(vrf_id, vrf_name, false);
1876+
if (vrf_name) {
1877+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
1878+
return CMD_WARNING;
1879+
}
18741880

18751881
if (table_id)
18761882
table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id, table_id);
@@ -1969,8 +1975,10 @@ DEFPY (show_route_summary,
19691975
} else {
19701976
vrf_id_t vrf_id = VRF_DEFAULT;
19711977

1972-
if (vrf_name)
1973-
VRF_GET_ID(vrf_id, vrf_name, false);
1978+
if (vrf_name) {
1979+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
1980+
return CMD_WARNING;
1981+
}
19741982

19751983
if (table_id == 0)
19761984
table = zebra_vrf_table(afi, safi, vrf_id);
@@ -2022,8 +2030,10 @@ DEFPY_HIDDEN (show_route_zebra_dump,
20222030
} else {
20232031
vrf_id_t vrf_id = VRF_DEFAULT;
20242032

2025-
if (vrf_name)
2026-
VRF_GET_ID(vrf_id, vrf_name, false);
2033+
if (vrf_name) {
2034+
if (!vrf_get_id(vty, &vrf_id, vrf_name, false))
2035+
return CMD_WARNING;
2036+
}
20272037

20282038
table = zebra_vrf_table(afi, safi, vrf_id);
20292039

0 commit comments

Comments
 (0)