ospfd: refactor range commands

* Update the "range" helpers to accept an area pointer instead of
  an area ID;
* Always call ospf_area_display_format_set() after every "range"
  command to ensure consistency.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
This commit is contained in:
Renato Westphal 2023-03-07 21:13:53 -03:00
parent c2b55093a1
commit 4312d66f9f
3 changed files with 38 additions and 47 deletions

View file

@ -169,16 +169,11 @@ static int ospf_area_actively_attached(struct ospf_area *area)
return area->act_ints; return area->act_ints;
} }
int ospf_area_range_set(struct ospf *ospf, struct in_addr area_id, int ospf_area_range_set(struct ospf *ospf, struct ospf_area *area,
struct prefix_ipv4 *p, int advertise) struct prefix_ipv4 *p, int advertise)
{ {
struct ospf_area *area;
struct ospf_area_range *range; struct ospf_area_range *range;
area = ospf_area_get(ospf, area_id);
if (area == NULL)
return 0;
range = ospf_area_range_lookup(area, p); range = ospf_area_range_lookup(area, p);
if (range != NULL) { if (range != NULL) {
if (!CHECK_FLAG(advertise, OSPF_AREA_RANGE_ADVERTISE)) if (!CHECK_FLAG(advertise, OSPF_AREA_RANGE_ADVERTISE))
@ -204,16 +199,11 @@ int ospf_area_range_set(struct ospf *ospf, struct in_addr area_id,
return 1; return 1;
} }
int ospf_area_range_cost_set(struct ospf *ospf, struct in_addr area_id, int ospf_area_range_cost_set(struct ospf *ospf, struct ospf_area *area,
struct prefix_ipv4 *p, uint32_t cost) struct prefix_ipv4 *p, uint32_t cost)
{ {
struct ospf_area *area;
struct ospf_area_range *range; struct ospf_area_range *range;
area = ospf_area_get(ospf, area_id);
if (area == NULL)
return 0;
range = ospf_area_range_lookup(area, p); range = ospf_area_range_lookup(area, p);
if (range == NULL) if (range == NULL)
return 0; return 0;
@ -227,16 +217,11 @@ int ospf_area_range_cost_set(struct ospf *ospf, struct in_addr area_id,
return 1; return 1;
} }
int ospf_area_range_unset(struct ospf *ospf, struct in_addr area_id, int ospf_area_range_unset(struct ospf *ospf, struct ospf_area *area,
struct prefix_ipv4 *p) struct prefix_ipv4 *p)
{ {
struct ospf_area *area;
struct route_node *rn; struct route_node *rn;
area = ospf_area_lookup_by_area_id(ospf, area_id);
if (area == NULL)
return 0;
rn = route_node_lookup(area->ranges, (struct prefix *)p); rn = route_node_lookup(area->ranges, (struct prefix *)p);
if (rn == NULL) if (rn == NULL)
return 0; return 0;
@ -249,13 +234,11 @@ int ospf_area_range_unset(struct ospf *ospf, struct in_addr area_id,
return 1; return 1;
} }
int ospf_area_range_substitute_set(struct ospf *ospf, struct in_addr area_id, int ospf_area_range_substitute_set(struct ospf *ospf, struct ospf_area *area,
struct prefix_ipv4 *p, struct prefix_ipv4 *s) struct prefix_ipv4 *p, struct prefix_ipv4 *s)
{ {
struct ospf_area *area;
struct ospf_area_range *range; struct ospf_area_range *range;
area = ospf_area_get(ospf, area_id);
range = ospf_area_range_lookup(area, p); range = ospf_area_range_lookup(area, p);
if (range != NULL) { if (range != NULL) {
@ -276,16 +259,11 @@ int ospf_area_range_substitute_set(struct ospf *ospf, struct in_addr area_id,
return 1; return 1;
} }
int ospf_area_range_substitute_unset(struct ospf *ospf, struct in_addr area_id, int ospf_area_range_substitute_unset(struct ospf *ospf, struct ospf_area *area,
struct prefix_ipv4 *p) struct prefix_ipv4 *p)
{ {
struct ospf_area *area;
struct ospf_area_range *range; struct ospf_area_range *range;
area = ospf_area_lookup_by_area_id(ospf, area_id);
if (area == NULL)
return 0;
range = ospf_area_range_lookup(area, p); range = ospf_area_range_lookup(area, p);
if (range == NULL) if (range == NULL)
return 0; return 0;

View file

@ -51,16 +51,16 @@ extern struct ospf_area_range *ospf_some_area_range_match(struct prefix_ipv4 *);
extern struct ospf_area_range * extern struct ospf_area_range *
ospf_area_range_lookup_next(struct ospf_area *, struct in_addr *, int); ospf_area_range_lookup_next(struct ospf_area *, struct in_addr *, int);
extern int ospf_area_range_set(struct ospf *, struct in_addr, extern int ospf_area_range_set(struct ospf *, struct ospf_area *,
struct prefix_ipv4 *, int); struct prefix_ipv4 *, int);
extern int ospf_area_range_cost_set(struct ospf *, struct in_addr, extern int ospf_area_range_cost_set(struct ospf *, struct ospf_area *,
struct prefix_ipv4 *, uint32_t); struct prefix_ipv4 *, uint32_t);
extern int ospf_area_range_unset(struct ospf *, struct in_addr, extern int ospf_area_range_unset(struct ospf *, struct ospf_area *,
struct prefix_ipv4 *); struct prefix_ipv4 *);
extern int ospf_area_range_substitute_set(struct ospf *, struct in_addr, extern int ospf_area_range_substitute_set(struct ospf *, struct ospf_area *,
struct prefix_ipv4 *, struct prefix_ipv4 *,
struct prefix_ipv4 *); struct prefix_ipv4 *);
extern int ospf_area_range_substitute_unset(struct ospf *, struct in_addr, extern int ospf_area_range_substitute_unset(struct ospf *, struct ospf_area *,
struct prefix_ipv4 *); struct prefix_ipv4 *);
extern struct ospf_area_range *ospf_area_range_match_any(struct ospf *, extern struct ospf_area_range *ospf_area_range_match_any(struct ospf *,
struct prefix_ipv4 *); struct prefix_ipv4 *);

View file

@ -611,6 +611,7 @@ DEFUN (ospf_area_range,
"Advertised metric for this range\n") "Advertised metric for this range\n")
{ {
VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
struct ospf_area *area;
int idx_ipv4_number = 1; int idx_ipv4_number = 1;
int idx_ipv4_prefixlen = 3; int idx_ipv4_prefixlen = 3;
int idx_cost = 6; int idx_cost = 6;
@ -622,12 +623,13 @@ DEFUN (ospf_area_range,
VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg); VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg);
str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p); str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p);
ospf_area_range_set(ospf, area_id, &p, OSPF_AREA_RANGE_ADVERTISE); area = ospf_area_get(ospf, area_id);
ospf_area_display_format_set(ospf, ospf_area_get(ospf, area_id), ospf_area_display_format_set(ospf, area, format);
format);
ospf_area_range_set(ospf, area, &p, OSPF_AREA_RANGE_ADVERTISE);
if (argc > 5) { if (argc > 5) {
cost = strtoul(argv[idx_cost]->arg, NULL, 10); cost = strtoul(argv[idx_cost]->arg, NULL, 10);
ospf_area_range_cost_set(ospf, area_id, &p, cost); ospf_area_range_cost_set(ospf, area, &p, cost);
} }
return CMD_SUCCESS; return CMD_SUCCESS;
@ -647,6 +649,7 @@ DEFUN (ospf_area_range_cost,
"Network prefix to be announced instead of range\n") "Network prefix to be announced instead of range\n")
{ {
VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
struct ospf_area *area;
int idx_ipv4_number = 1; int idx_ipv4_number = 1;
int idx_ipv4_prefixlen = 3; int idx_ipv4_prefixlen = 3;
int idx = 4; int idx = 4;
@ -658,19 +661,19 @@ DEFUN (ospf_area_range_cost,
VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg); VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg);
str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p); str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p);
ospf_area_range_set(ospf, area_id, &p, OSPF_AREA_RANGE_ADVERTISE); area = ospf_area_get(ospf, area_id);
ospf_area_display_format_set(ospf, ospf_area_get(ospf, area_id), ospf_area_display_format_set(ospf, area, format);
format);
ospf_area_range_set(ospf, area, &p, OSPF_AREA_RANGE_ADVERTISE);
if (argv_find(argv, argc, "cost", &idx)) { if (argv_find(argv, argc, "cost", &idx)) {
cost = strtoul(argv[idx + 1]->arg, NULL, 10); cost = strtoul(argv[idx + 1]->arg, NULL, 10);
ospf_area_range_cost_set(ospf, area_id, &p, cost); ospf_area_range_cost_set(ospf, area, &p, cost);
} }
idx = 4; idx = 4;
if (argv_find(argv, argc, "substitute", &idx)) { if (argv_find(argv, argc, "substitute", &idx)) {
str2prefix_ipv4(argv[idx + 1]->arg, &s); str2prefix_ipv4(argv[idx + 1]->arg, &s);
ospf_area_range_substitute_set(ospf, area_id, &p, &s); ospf_area_range_substitute_set(ospf, area, &p, &s);
} }
return CMD_SUCCESS; return CMD_SUCCESS;
@ -687,6 +690,7 @@ DEFUN (ospf_area_range_not_advertise,
"DoNotAdvertise this range\n") "DoNotAdvertise this range\n")
{ {
VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
struct ospf_area *area;
int idx_ipv4_number = 1; int idx_ipv4_number = 1;
int idx_ipv4_prefixlen = 3; int idx_ipv4_prefixlen = 3;
struct prefix_ipv4 p; struct prefix_ipv4 p;
@ -696,10 +700,11 @@ DEFUN (ospf_area_range_not_advertise,
VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg); VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg);
str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p); str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p);
ospf_area_range_set(ospf, area_id, &p, 0); area = ospf_area_get(ospf, area_id);
ospf_area_display_format_set(ospf, ospf_area_get(ospf, area_id), ospf_area_display_format_set(ospf, area, format);
format);
ospf_area_range_substitute_unset(ospf, area_id, &p); ospf_area_range_set(ospf, area, &p, 0);
ospf_area_range_substitute_unset(ospf, area, &p);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -721,6 +726,7 @@ DEFUN (no_ospf_area_range,
"DoNotAdvertise this range\n") "DoNotAdvertise this range\n")
{ {
VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
struct ospf_area *area;
int idx_ipv4_number = 2; int idx_ipv4_number = 2;
int idx_ipv4_prefixlen = 4; int idx_ipv4_prefixlen = 4;
struct prefix_ipv4 p; struct prefix_ipv4 p;
@ -730,7 +736,10 @@ DEFUN (no_ospf_area_range,
VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg); VTY_GET_OSPF_AREA_ID(area_id, format, argv[idx_ipv4_number]->arg);
str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p); str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p);
ospf_area_range_unset(ospf, area_id, &p); area = ospf_area_get(ospf, area_id);
ospf_area_display_format_set(ospf, area, format);
ospf_area_range_unset(ospf, area, &p);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -748,6 +757,7 @@ DEFUN (no_ospf_area_range_substitute,
"Network prefix to be announced instead of range\n") "Network prefix to be announced instead of range\n")
{ {
VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf); VTY_DECLVAR_INSTANCE_CONTEXT(ospf, ospf);
struct ospf_area *area;
int idx_ipv4_number = 2; int idx_ipv4_number = 2;
int idx_ipv4_prefixlen = 4; int idx_ipv4_prefixlen = 4;
int idx_ipv4_prefixlen_2 = 6; int idx_ipv4_prefixlen_2 = 6;
@ -759,7 +769,10 @@ DEFUN (no_ospf_area_range_substitute,
str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p); str2prefix_ipv4(argv[idx_ipv4_prefixlen]->arg, &p);
str2prefix_ipv4(argv[idx_ipv4_prefixlen_2]->arg, &s); str2prefix_ipv4(argv[idx_ipv4_prefixlen_2]->arg, &s);
ospf_area_range_substitute_unset(ospf, area_id, &p); area = ospf_area_get(ospf, area_id);
ospf_area_display_format_set(ospf, area, format);
ospf_area_range_substitute_unset(ospf, area, &p);
return CMD_SUCCESS; return CMD_SUCCESS;
} }