]>
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 | |
c4936a1a DS |
88 | |
89 | rd_header = 1; | |
dc387b0f | 90 | memset(rd_str, 0, sizeof(rd_str)); |
c4936a1a DS |
91 | |
92 | for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) { | |
dc387b0f LK |
93 | struct bgp_adj_out *adj = NULL; |
94 | struct attr *attr = NULL; | |
95 | struct peer_af *paf = NULL; | |
96 | ||
97 | RB_FOREACH (adj, bgp_adj_out_rb, &rm->adj_out) | |
98 | SUBGRP_FOREACH_PEER (adj->subgroup, paf) { | |
99 | if (paf->peer != peer || !adj->attr) | |
100 | continue; | |
101 | ||
102 | attr = adj->attr; | |
103 | break; | |
104 | } | |
105 | ||
106 | if (bgp_node_get_bgp_path_info(rm) == NULL) | |
c4936a1a DS |
107 | continue; |
108 | ||
10d5be75 LK |
109 | if (!attr) |
110 | continue; | |
111 | ||
c4936a1a DS |
112 | if (header) { |
113 | if (use_json) { | |
114 | json_object_int_add( | |
115 | json, "bgpTableVersion", 0); | |
116 | json_object_string_add( | |
117 | json, "bgpLocalRouterId", | |
118 | inet_ntoa(bgp->router_id)); | |
dc387b0f LK |
119 | json_object_int_add( |
120 | json, | |
121 | "defaultLocPrf", | |
122 | bgp->default_local_pref); | |
123 | json_object_int_add( | |
124 | json, "localAS", | |
125 | bgp->as); | |
c4936a1a DS |
126 | json_object_object_add(json, |
127 | "bgpStatusCodes", | |
128 | json_scode); | |
129 | json_object_object_add(json, | |
130 | "bgpOriginCodes", | |
131 | json_ocode); | |
132 | } else { | |
133 | vty_out(vty, | |
134 | "BGP table version is 0, local router ID is %s\n", | |
135 | inet_ntoa(bgp->router_id)); | |
dc387b0f LK |
136 | vty_out(vty, "Default local pref %u, ", |
137 | bgp->default_local_pref); | |
138 | vty_out(vty, "local AS %u\n", bgp->as); | |
c4936a1a DS |
139 | vty_out(vty, |
140 | "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n"); | |
141 | vty_out(vty, | |
142 | "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n"); | |
143 | vty_out(vty, V4_HEADER); | |
144 | } | |
145 | header = 0; | |
146 | } | |
d62a17ae | 147 | |
c4936a1a DS |
148 | if (rd_header) { |
149 | uint16_t type; | |
150 | struct rd_as rd_as = {0}; | |
151 | struct rd_ip rd_ip = {0}; | |
784d3a42 | 152 | #if ENABLE_BGP_VNC |
c4936a1a | 153 | struct rd_vnc_eth rd_vnc_eth = {0}; |
784d3a42 | 154 | #endif |
c4936a1a DS |
155 | uint8_t *pnt; |
156 | ||
157 | pnt = rn->p.u.val; | |
158 | ||
159 | /* Decode RD type. */ | |
160 | type = decode_rd_type(pnt); | |
161 | /* Decode RD value. */ | |
162 | if (type == RD_TYPE_AS) | |
163 | decode_rd_as(pnt + 2, &rd_as); | |
164 | else if (type == RD_TYPE_AS4) | |
165 | decode_rd_as4(pnt + 2, &rd_as); | |
166 | else if (type == RD_TYPE_IP) | |
167 | decode_rd_ip(pnt + 2, &rd_ip); | |
784d3a42 | 168 | #if ENABLE_BGP_VNC |
c4936a1a DS |
169 | else if (type == RD_TYPE_VNC_ETH) |
170 | decode_rd_vnc_eth(pnt, &rd_vnc_eth); | |
784d3a42 | 171 | #endif |
c4936a1a | 172 | if (use_json) { |
dc387b0f LK |
173 | json_routes = json_object_new_object(); |
174 | ||
c4936a1a DS |
175 | if (type == RD_TYPE_AS |
176 | || type == RD_TYPE_AS4) | |
dc387b0f | 177 | sprintf(rd_str, "%u:%d", |
c4936a1a DS |
178 | rd_as.as, rd_as.val); |
179 | else if (type == RD_TYPE_IP) | |
dc387b0f | 180 | sprintf(rd_str, "%s:%d", |
c4936a1a DS |
181 | inet_ntoa(rd_ip.ip), |
182 | rd_ip.val); | |
183 | json_object_string_add( | |
184 | json_routes, | |
dc387b0f | 185 | "rd", rd_str); |
c4936a1a DS |
186 | } else { |
187 | vty_out(vty, "Route Distinguisher: "); | |
188 | ||
189 | if (type == RD_TYPE_AS | |
190 | || type == RD_TYPE_AS4) | |
191 | vty_out(vty, "%u:%d", rd_as.as, | |
192 | rd_as.val); | |
193 | else if (type == RD_TYPE_IP) | |
194 | vty_out(vty, "%s:%d", | |
195 | inet_ntoa(rd_ip.ip), | |
196 | rd_ip.val); | |
784d3a42 | 197 | #if ENABLE_BGP_VNC |
c4936a1a DS |
198 | else if (type == RD_TYPE_VNC_ETH) |
199 | vty_out(vty, | |
200 | "%u:%02x:%02x:%02x:%02x:%02x:%02x", | |
201 | rd_vnc_eth.local_nve_id, | |
202 | rd_vnc_eth.macaddr | |
203 | .octet[0], | |
204 | rd_vnc_eth.macaddr | |
205 | .octet[1], | |
206 | rd_vnc_eth.macaddr | |
207 | .octet[2], | |
208 | rd_vnc_eth.macaddr | |
209 | .octet[3], | |
210 | rd_vnc_eth.macaddr | |
211 | .octet[4], | |
212 | rd_vnc_eth.macaddr | |
213 | .octet[5]); | |
784d3a42 PG |
214 | #endif |
215 | ||
c4936a1a | 216 | vty_out(vty, "\n"); |
d62a17ae | 217 | } |
c4936a1a DS |
218 | rd_header = 0; |
219 | } | |
dc387b0f LK |
220 | route_vty_out_tmp(vty, &rm->p, attr, |
221 | safi, use_json, | |
222 | json_routes); | |
223 | output_count++; | |
d62a17ae | 224 | } |
dc387b0f LK |
225 | |
226 | if (use_json) | |
227 | json_object_object_add(json_adv, rd_str, json_routes); | |
d62a17ae | 228 | } |
dc387b0f | 229 | |
d62a17ae | 230 | if (use_json) { |
dc387b0f LK |
231 | json_object_object_add(json, "advertisedRoutes", json_adv); |
232 | json_object_int_add(json, | |
233 | "totalPrefixCounter", output_count); | |
9d303b37 DL |
234 | vty_out(vty, "%s\n", json_object_to_json_string_ext( |
235 | json, JSON_C_TO_STRING_PRETTY)); | |
d62a17ae | 236 | json_object_free(json); |
dc387b0f LK |
237 | } else |
238 | vty_out(vty, "\nTotal number of prefixes %ld\n", output_count); | |
239 | ||
d62a17ae | 240 | return CMD_SUCCESS; |
784d3a42 | 241 | } |