]>
Commit | Line | Data |
---|---|---|
784d3a42 | 1 | /* VPN Related functions |
896014f4 DL |
2 | * Copyright (C) 2017 6WIND |
3 | * | |
4 | * This file is part of FRRouting | |
5 | * | |
6 | * FRRouting is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRRouting is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
784d3a42 PG |
20 | |
21 | #include <zebra.h> | |
22 | #include "command.h" | |
23 | #include "prefix.h" | |
24 | #include "lib/json.h" | |
25 | ||
26 | #include "bgpd/bgpd.h" | |
27 | #include "bgpd/bgp_route.h" | |
28 | #include "bgpd/bgp_table.h" | |
29 | #include "bgpd/bgp_attr.h" | |
30 | #include "bgpd/bgp_mplsvpn.h" | |
31 | #include "bgpd/bgp_vpn.h" | |
dc387b0f | 32 | #include "bgpd/bgp_updgrp.h" |
784d3a42 | 33 | |
d62a17ae | 34 | int show_adj_route_vpn(struct vty *vty, struct peer *peer, |
35 | struct prefix_rd *prd, afi_t afi, safi_t safi, | |
9f049418 | 36 | bool use_json) |
784d3a42 | 37 | { |
d62a17ae | 38 | struct bgp *bgp; |
39 | struct bgp_table *table; | |
40 | struct bgp_node *rn; | |
41 | struct bgp_node *rm; | |
d62a17ae | 42 | int rd_header; |
43 | int header = 1; | |
44 | json_object *json = NULL; | |
45 | json_object *json_scode = NULL; | |
46 | json_object *json_ocode = NULL; | |
dc387b0f | 47 | json_object *json_adv = NULL; |
d62a17ae | 48 | json_object *json_routes = NULL; |
dc387b0f LK |
49 | char rd_str[BUFSIZ]; |
50 | unsigned long output_count = 0; | |
d62a17ae | 51 | |
52 | bgp = bgp_get_default(); | |
53 | if (bgp == NULL) { | |
54 | if (!use_json) | |
55 | vty_out(vty, "No BGP process is configured\n"); | |
16307668 RW |
56 | else |
57 | vty_out(vty, "{}\n"); | |
d62a17ae | 58 | return CMD_WARNING; |
59 | } | |
60 | ||
61 | if (use_json) { | |
62 | json_scode = json_object_new_object(); | |
63 | json_ocode = json_object_new_object(); | |
d62a17ae | 64 | json = json_object_new_object(); |
dc387b0f | 65 | json_adv = json_object_new_object(); |
d62a17ae | 66 | |
67 | json_object_string_add(json_scode, "suppressed", "s"); | |
68 | json_object_string_add(json_scode, "damped", "d"); | |
69 | json_object_string_add(json_scode, "history", "h"); | |
70 | json_object_string_add(json_scode, "valid", "*"); | |
71 | json_object_string_add(json_scode, "best", ">"); | |
72 | json_object_string_add(json_scode, "internal", "i"); | |
73 | ||
74 | json_object_string_add(json_ocode, "igp", "i"); | |
75 | json_object_string_add(json_ocode, "egp", "e"); | |
76 | json_object_string_add(json_ocode, "incomplete", "?"); | |
77 | } | |
78 | ||
dcc1615e | 79 | for (rn = bgp_table_top(bgp->rib[afi][safi]); rn; |
d62a17ae | 80 | rn = bgp_route_next(rn)) { |
81 | if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0) | |
82 | continue; | |
83 | ||
67009e22 | 84 | table = bgp_node_get_bgp_table_info(rn); |
c4936a1a DS |
85 | if (table == NULL) |
86 | continue; | |
d62a17ae | 87 | |
f5cfc290 LK |
88 | /* |
89 | * Initialize variables for each RD | |
90 | * All prefixes under an RD is aggregated within "json_routes" | |
91 | */ | |
c4936a1a | 92 | rd_header = 1; |
dc387b0f | 93 | memset(rd_str, 0, sizeof(rd_str)); |
f5cfc290 | 94 | json_routes = NULL; |
c4936a1a DS |
95 | |
96 | for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) { | |
dc387b0f LK |
97 | struct bgp_adj_out *adj = NULL; |
98 | struct attr *attr = NULL; | |
99 | struct peer_af *paf = NULL; | |
100 | ||
101 | RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out) | |
102 | SUBGRP_FOREACH_PEER (adj->subgroup, paf) { | |
103 | if (paf->peer != peer || !adj->attr) | |
104 | continue; | |
105 | ||
106 | attr = adj->attr; | |
107 | break; | |
108 | } | |
109 | ||
110 | if (bgp_node_get_bgp_path_info(rm) == NULL) | |
c4936a1a DS |
111 | continue; |
112 | ||
10d5be75 LK |
113 | if (!attr) |
114 | continue; | |
115 | ||
c4936a1a DS |
116 | if (header) { |
117 | if (use_json) { | |
118 | json_object_int_add( | |
119 | json, "bgpTableVersion", 0); | |
120 | json_object_string_add( | |
121 | json, "bgpLocalRouterId", | |
122 | inet_ntoa(bgp->router_id)); | |
dc387b0f LK |
123 | json_object_int_add( |
124 | json, | |
125 | "defaultLocPrf", | |
126 | bgp->default_local_pref); | |
127 | json_object_int_add( | |
128 | json, "localAS", | |
129 | bgp->as); | |
c4936a1a DS |
130 | json_object_object_add(json, |
131 | "bgpStatusCodes", | |
132 | json_scode); | |
133 | json_object_object_add(json, | |
134 | "bgpOriginCodes", | |
135 | json_ocode); | |
136 | } else { | |
137 | vty_out(vty, | |
138 | "BGP table version is 0, local router ID is %s\n", | |
139 | inet_ntoa(bgp->router_id)); | |
dc387b0f LK |
140 | vty_out(vty, "Default local pref %u, ", |
141 | bgp->default_local_pref); | |
142 | vty_out(vty, "local AS %u\n", bgp->as); | |
c4936a1a DS |
143 | vty_out(vty, |
144 | "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n"); | |
145 | vty_out(vty, | |
146 | "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n"); | |
147 | vty_out(vty, V4_HEADER); | |
148 | } | |
149 | header = 0; | |
150 | } | |
d62a17ae | 151 | |
c4936a1a DS |
152 | if (rd_header) { |
153 | uint16_t type; | |
154 | struct rd_as rd_as = {0}; | |
155 | struct rd_ip rd_ip = {0}; | |
784d3a42 | 156 | #if ENABLE_BGP_VNC |
c4936a1a | 157 | struct rd_vnc_eth rd_vnc_eth = {0}; |
784d3a42 | 158 | #endif |
c4936a1a DS |
159 | uint8_t *pnt; |
160 | ||
161 | pnt = rn->p.u.val; | |
162 | ||
163 | /* Decode RD type. */ | |
164 | type = decode_rd_type(pnt); | |
165 | /* Decode RD value. */ | |
166 | if (type == RD_TYPE_AS) | |
167 | decode_rd_as(pnt + 2, &rd_as); | |
168 | else if (type == RD_TYPE_AS4) | |
169 | decode_rd_as4(pnt + 2, &rd_as); | |
170 | else if (type == RD_TYPE_IP) | |
171 | decode_rd_ip(pnt + 2, &rd_ip); | |
784d3a42 | 172 | #if ENABLE_BGP_VNC |
c4936a1a DS |
173 | else if (type == RD_TYPE_VNC_ETH) |
174 | decode_rd_vnc_eth(pnt, &rd_vnc_eth); | |
784d3a42 | 175 | #endif |
c4936a1a | 176 | if (use_json) { |
dc387b0f LK |
177 | json_routes = json_object_new_object(); |
178 | ||
c4936a1a DS |
179 | if (type == RD_TYPE_AS |
180 | || type == RD_TYPE_AS4) | |
dc387b0f | 181 | sprintf(rd_str, "%u:%d", |
c4936a1a DS |
182 | rd_as.as, rd_as.val); |
183 | else if (type == RD_TYPE_IP) | |
dc387b0f | 184 | sprintf(rd_str, "%s:%d", |
c4936a1a DS |
185 | inet_ntoa(rd_ip.ip), |
186 | rd_ip.val); | |
187 | json_object_string_add( | |
188 | json_routes, | |
dc387b0f | 189 | "rd", rd_str); |
c4936a1a DS |
190 | } else { |
191 | vty_out(vty, "Route Distinguisher: "); | |
192 | ||
193 | if (type == RD_TYPE_AS | |
194 | || type == RD_TYPE_AS4) | |
195 | vty_out(vty, "%u:%d", rd_as.as, | |
196 | rd_as.val); | |
197 | else if (type == RD_TYPE_IP) | |
198 | vty_out(vty, "%s:%d", | |
199 | inet_ntoa(rd_ip.ip), | |
200 | rd_ip.val); | |
784d3a42 | 201 | #if ENABLE_BGP_VNC |
c4936a1a DS |
202 | else if (type == RD_TYPE_VNC_ETH) |
203 | vty_out(vty, | |
204 | "%u:%02x:%02x:%02x:%02x:%02x:%02x", | |
205 | rd_vnc_eth.local_nve_id, | |
206 | rd_vnc_eth.macaddr | |
207 | .octet[0], | |
208 | rd_vnc_eth.macaddr | |
209 | .octet[1], | |
210 | rd_vnc_eth.macaddr | |
211 | .octet[2], | |
212 | rd_vnc_eth.macaddr | |
213 | .octet[3], | |
214 | rd_vnc_eth.macaddr | |
215 | .octet[4], | |
216 | rd_vnc_eth.macaddr | |
217 | .octet[5]); | |
784d3a42 PG |
218 | #endif |
219 | ||
c4936a1a | 220 | vty_out(vty, "\n"); |
d62a17ae | 221 | } |
c4936a1a DS |
222 | rd_header = 0; |
223 | } | |
dc387b0f LK |
224 | route_vty_out_tmp(vty, &rm->p, attr, |
225 | safi, use_json, | |
226 | json_routes); | |
227 | output_count++; | |
d62a17ae | 228 | } |
dc387b0f | 229 | |
f5cfc290 | 230 | if (use_json && json_routes) |
dc387b0f | 231 | json_object_object_add(json_adv, rd_str, json_routes); |
d62a17ae | 232 | } |
dc387b0f | 233 | |
d62a17ae | 234 | if (use_json) { |
dc387b0f LK |
235 | json_object_object_add(json, "advertisedRoutes", json_adv); |
236 | json_object_int_add(json, | |
237 | "totalPrefixCounter", output_count); | |
9d303b37 DL |
238 | vty_out(vty, "%s\n", json_object_to_json_string_ext( |
239 | json, JSON_C_TO_STRING_PRETTY)); | |
d62a17ae | 240 | json_object_free(json); |
dc387b0f LK |
241 | } else |
242 | vty_out(vty, "\nTotal number of prefixes %ld\n", output_count); | |
243 | ||
d62a17ae | 244 | return CMD_SUCCESS; |
784d3a42 | 245 | } |