bgpd: add as notation support for bgp rpki service

Rpki service uses AS number:
- some show commands use the as number as paramter.
use asnotation support.
- the as number of entries is displayed based on the
asnotation mode of the chose bgp instance.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
This commit is contained in:
Philippe Guibert 2022-11-25 17:51:33 +01:00
parent c1aa9e7f90
commit 6ccfd1030b

View File

@ -96,6 +96,7 @@ struct rpki_for_each_record_arg {
unsigned int *prefix_amount; unsigned int *prefix_amount;
as_t as; as_t as;
json_object *json; json_object *json;
enum asnotation_mode asnotation;
}; };
static int start(void); static int start(void);
@ -120,7 +121,7 @@ static void rpki_delete_all_cache_nodes(void);
static int add_tcp_cache(const char *host, const char *port, static int add_tcp_cache(const char *host, const char *port,
const uint8_t preference, const char *bindaddr); const uint8_t preference, const char *bindaddr);
static void print_record(const struct pfx_record *record, struct vty *vty, static void print_record(const struct pfx_record *record, struct vty *vty,
json_object *json); json_object *json, enum asnotation_mode asnotation);
static bool is_synchronized(void); static bool is_synchronized(void);
static bool is_running(void); static bool is_running(void);
static bool is_stopping(void); static bool is_stopping(void);
@ -285,7 +286,7 @@ static void rpki_delete_all_cache_nodes(void)
} }
static void print_record(const struct pfx_record *record, struct vty *vty, static void print_record(const struct pfx_record *record, struct vty *vty,
json_object *json) json_object *json, enum asnotation_mode asnotation)
{ {
char ip[INET6_ADDRSTRLEN]; char ip[INET6_ADDRSTRLEN];
json_object *json_record = NULL; json_object *json_record = NULL;
@ -293,8 +294,10 @@ static void print_record(const struct pfx_record *record, struct vty *vty,
lrtr_ip_addr_to_str(&record->prefix, ip, sizeof(ip)); lrtr_ip_addr_to_str(&record->prefix, ip, sizeof(ip));
if (!json) { if (!json) {
vty_out(vty, "%-40s %3u - %3u %10u\n", ip, record->min_len, vty_out(vty, "%-40s %3u - %3u ", ip, record->min_len,
record->max_len, record->asn); record->max_len);
vty_out(vty, ASN_FORMAT(asnotation), &record->asn);
vty_out(vty, "\n");
} else { } else {
json_record = json_object_new_object(); json_record = json_object_new_object();
json_object_string_add(json_record, "prefix", ip); json_object_string_add(json_record, "prefix", ip);
@ -302,7 +305,7 @@ static void print_record(const struct pfx_record *record, struct vty *vty,
record->min_len); record->min_len);
json_object_int_add(json_record, "prefixLenMax", json_object_int_add(json_record, "prefixLenMax",
record->max_len); record->max_len);
json_object_int_add(json_record, "asn", record->asn); asn_asn2json(json_record, "asn", record->asn, asnotation);
json_object_array_add(json, json_record); json_object_array_add(json, json_record);
} }
} }
@ -314,7 +317,7 @@ static void print_record_by_asn(const struct pfx_record *record, void *data)
if (record->asn == arg->as) { if (record->asn == arg->as) {
(*arg->prefix_amount)++; (*arg->prefix_amount)++;
print_record(record, vty, arg->json); print_record(record, vty, arg->json, arg->asnotation);
} }
} }
@ -325,7 +328,7 @@ static void print_record_cb(const struct pfx_record *record, void *data)
(*arg->prefix_amount)++; (*arg->prefix_amount)++;
print_record(record, vty, arg->json); print_record(record, vty, arg->json, arg->asnotation);
} }
static struct rtr_mgr_group *get_groups(void) static struct rtr_mgr_group *get_groups(void)
@ -743,6 +746,7 @@ static void print_prefix_table_by_asn(struct vty *vty, as_t as,
arg.vty = vty; arg.vty = vty;
arg.as = as; arg.as = as;
arg.json = NULL; arg.json = NULL;
arg.asnotation = bgp_get_asnotation(bgp_lookup_by_vrf_id(VRF_DEFAULT));
if (!group) { if (!group) {
if (!json) if (!json)
@ -795,6 +799,7 @@ static void print_prefix_table(struct vty *vty, json_object *json)
arg.vty = vty; arg.vty = vty;
arg.json = NULL; arg.json = NULL;
arg.asnotation = bgp_get_asnotation(bgp_lookup_by_vrf_id(VRF_DEFAULT));
if (!group) { if (!group) {
if (!json) if (!json)
@ -1352,7 +1357,7 @@ DEFPY (show_rpki_prefix_table,
DEFPY (show_rpki_as_number, DEFPY (show_rpki_as_number,
show_rpki_as_number_cmd, show_rpki_as_number_cmd,
"show rpki as-number (1-4294967295)$by_asn [json$uj]", "show rpki as-number ASNUM$by_asn [json$uj]",
SHOW_STR SHOW_STR
RPKI_OUTPUT_STRING RPKI_OUTPUT_STRING
"Lookup by ASN in prefix table\n" "Lookup by ASN in prefix table\n"
@ -1360,23 +1365,28 @@ DEFPY (show_rpki_as_number,
JSON_STR) JSON_STR)
{ {
struct json_object *json = NULL; struct json_object *json = NULL;
as_t as;
if (!is_synchronized()) { if (!is_synchronized()) {
if (!uj) if (!uj)
vty_out(vty, "No Connection to RPKI cache server.\n"); vty_out(vty, "No Connection to RPKI cache server.\n");
return CMD_WARNING; return CMD_WARNING;
} }
if (!asn_str2asn(by_asn, &as)) {
if (!uj)
vty_out(vty, "Invalid AS value: %s.\n", by_asn);
return CMD_WARNING;
}
if (uj) if (uj)
json = json_object_new_object(); json = json_object_new_object();
print_prefix_table_by_asn(vty, by_asn, json); print_prefix_table_by_asn(vty, as, json);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
DEFPY (show_rpki_prefix, DEFPY (show_rpki_prefix,
show_rpki_prefix_cmd, show_rpki_prefix_cmd,
"show rpki prefix <A.B.C.D/M|X:X::X:X/M> [(1-4294967295)$asn] [json$uj]", "show rpki prefix <A.B.C.D/M|X:X::X:X/M> [ASNUM$asn] [json$uj]",
SHOW_STR SHOW_STR
RPKI_OUTPUT_STRING RPKI_OUTPUT_STRING
"Lookup IP prefix and optionally ASN in prefix table\n" "Lookup IP prefix and optionally ASN in prefix table\n"
@ -1387,6 +1397,8 @@ DEFPY (show_rpki_prefix,
{ {
json_object *json = NULL; json_object *json = NULL;
json_object *json_records = NULL; json_object *json_records = NULL;
as_t as;
enum asnotation_mode asnotation;
if (!is_synchronized()) { if (!is_synchronized()) {
if (!uj) if (!uj)
@ -1407,13 +1419,19 @@ DEFPY (show_rpki_prefix,
return CMD_WARNING; return CMD_WARNING;
} }
if (asn && !asn_str2asn(asn, &as)) {
if (!uj)
vty_out(vty, "Invalid AS value: %s.\n", asn);
return CMD_WARNING;
}
struct pfx_record *matches = NULL; struct pfx_record *matches = NULL;
unsigned int match_count = 0; unsigned int match_count = 0;
enum pfxv_state result; enum pfxv_state result;
if (pfx_table_validate_r(rtr_config->pfx_table, &matches, &match_count, if (pfx_table_validate_r(rtr_config->pfx_table, &matches, &match_count,
asn, &addr, prefix->prefixlen, &result) as, &addr, prefix->prefixlen,
!= PFX_SUCCESS) { &result) != PFX_SUCCESS) {
if (!json) if (!json)
vty_out(vty, "Prefix lookup failed\n"); vty_out(vty, "Prefix lookup failed\n");
return CMD_WARNING; return CMD_WARNING;
@ -1430,13 +1448,14 @@ DEFPY (show_rpki_prefix,
json_object_object_add(json, "prefixes", json_records); json_object_object_add(json, "prefixes", json_records);
} }
asnotation = bgp_get_asnotation(bgp_lookup_by_vrf_id(VRF_DEFAULT));
for (size_t i = 0; i < match_count; ++i) { for (size_t i = 0; i < match_count; ++i) {
const struct pfx_record *record = &matches[i]; const struct pfx_record *record = &matches[i];
if (record->max_len >= prefix->prefixlen if (record->max_len >= prefix->prefixlen &&
&& ((asn != 0 && (uint32_t)asn == record->asn) ((as != 0 && (uint32_t)as == record->asn) || as == 0)) {
|| asn == 0)) { print_record(&matches[i], vty, json_records,
print_record(&matches[i], vty, json_records); asnotation);
} }
} }