]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
Merge pull request #5150 from qlyoung/bgp-vector-io-4
[mirror_frr.git] / bgpd / bgp_vpn.c
1 /* VPN Related functions
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 */
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"
32 #include "bgpd/bgp_updgrp.h"
33
34 int show_adj_route_vpn(struct vty *vty, struct peer *peer,
35 struct prefix_rd *prd, afi_t afi, safi_t safi,
36 bool use_json)
37 {
38 struct bgp *bgp;
39 struct bgp_table *table;
40 struct bgp_node *rn;
41 struct bgp_node *rm;
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;
47 json_object *json_adv = NULL;
48 json_object *json_routes = NULL;
49 char rd_str[BUFSIZ];
50 unsigned long output_count = 0;
51
52 bgp = bgp_get_default();
53 if (bgp == NULL) {
54 if (!use_json)
55 vty_out(vty, "No BGP process is configured\n");
56 else
57 vty_out(vty, "{}\n");
58 return CMD_WARNING;
59 }
60
61 if (use_json) {
62 json_scode = json_object_new_object();
63 json_ocode = json_object_new_object();
64 json = json_object_new_object();
65 json_adv = json_object_new_object();
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
79 for (rn = bgp_table_top(bgp->rib[afi][safi]); rn;
80 rn = bgp_route_next(rn)) {
81 if (prd && memcmp(rn->p.u.val, prd->val, 8) != 0)
82 continue;
83
84 table = bgp_node_get_bgp_table_info(rn);
85 if (table == NULL)
86 continue;
87
88
89 rd_header = 1;
90 memset(rd_str, 0, sizeof(rd_str));
91
92 for (rm = bgp_table_top(table); rm; rm = bgp_route_next(rm)) {
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)
107 continue;
108
109 if (header) {
110 if (use_json) {
111 json_object_int_add(
112 json, "bgpTableVersion", 0);
113 json_object_string_add(
114 json, "bgpLocalRouterId",
115 inet_ntoa(bgp->router_id));
116 json_object_int_add(
117 json,
118 "defaultLocPrf",
119 bgp->default_local_pref);
120 json_object_int_add(
121 json, "localAS",
122 bgp->as);
123 json_object_object_add(json,
124 "bgpStatusCodes",
125 json_scode);
126 json_object_object_add(json,
127 "bgpOriginCodes",
128 json_ocode);
129 } else {
130 vty_out(vty,
131 "BGP table version is 0, local router ID is %s\n",
132 inet_ntoa(bgp->router_id));
133 vty_out(vty, "Default local pref %u, ",
134 bgp->default_local_pref);
135 vty_out(vty, "local AS %u\n", bgp->as);
136 vty_out(vty,
137 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
138 vty_out(vty,
139 "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
140 vty_out(vty, V4_HEADER);
141 }
142 header = 0;
143 }
144
145 if (rd_header) {
146 uint16_t type;
147 struct rd_as rd_as = {0};
148 struct rd_ip rd_ip = {0};
149 #if ENABLE_BGP_VNC
150 struct rd_vnc_eth rd_vnc_eth = {0};
151 #endif
152 uint8_t *pnt;
153
154 pnt = rn->p.u.val;
155
156 /* Decode RD type. */
157 type = decode_rd_type(pnt);
158 /* Decode RD value. */
159 if (type == RD_TYPE_AS)
160 decode_rd_as(pnt + 2, &rd_as);
161 else if (type == RD_TYPE_AS4)
162 decode_rd_as4(pnt + 2, &rd_as);
163 else if (type == RD_TYPE_IP)
164 decode_rd_ip(pnt + 2, &rd_ip);
165 #if ENABLE_BGP_VNC
166 else if (type == RD_TYPE_VNC_ETH)
167 decode_rd_vnc_eth(pnt, &rd_vnc_eth);
168 #endif
169 if (use_json) {
170 json_routes = json_object_new_object();
171
172 if (type == RD_TYPE_AS
173 || type == RD_TYPE_AS4)
174 sprintf(rd_str, "%u:%d",
175 rd_as.as, rd_as.val);
176 else if (type == RD_TYPE_IP)
177 sprintf(rd_str, "%s:%d",
178 inet_ntoa(rd_ip.ip),
179 rd_ip.val);
180 json_object_string_add(
181 json_routes,
182 "rd", rd_str);
183 } else {
184 vty_out(vty, "Route Distinguisher: ");
185
186 if (type == RD_TYPE_AS
187 || type == RD_TYPE_AS4)
188 vty_out(vty, "%u:%d", rd_as.as,
189 rd_as.val);
190 else if (type == RD_TYPE_IP)
191 vty_out(vty, "%s:%d",
192 inet_ntoa(rd_ip.ip),
193 rd_ip.val);
194 #if ENABLE_BGP_VNC
195 else if (type == RD_TYPE_VNC_ETH)
196 vty_out(vty,
197 "%u:%02x:%02x:%02x:%02x:%02x:%02x",
198 rd_vnc_eth.local_nve_id,
199 rd_vnc_eth.macaddr
200 .octet[0],
201 rd_vnc_eth.macaddr
202 .octet[1],
203 rd_vnc_eth.macaddr
204 .octet[2],
205 rd_vnc_eth.macaddr
206 .octet[3],
207 rd_vnc_eth.macaddr
208 .octet[4],
209 rd_vnc_eth.macaddr
210 .octet[5]);
211 #endif
212
213 vty_out(vty, "\n");
214 }
215 rd_header = 0;
216 }
217 route_vty_out_tmp(vty, &rm->p, attr,
218 safi, use_json,
219 json_routes);
220 output_count++;
221 }
222
223 if (use_json)
224 json_object_object_add(json_adv, rd_str, json_routes);
225 }
226
227 if (use_json) {
228 json_object_object_add(json, "advertisedRoutes", json_adv);
229 json_object_int_add(json,
230 "totalPrefixCounter", output_count);
231 vty_out(vty, "%s\n", json_object_to_json_string_ext(
232 json, JSON_C_TO_STRING_PRETTY));
233 json_object_free(json);
234 } else
235 vty_out(vty, "\nTotal number of prefixes %ld\n", output_count);
236
237 return CMD_SUCCESS;
238 }