]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_vpn.c
Merge remote-tracking branch 'origin/master' into evpn_plus_struct_attr
[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
33 int
34 show_adj_route_vpn (struct vty *vty, struct peer *peer, struct prefix_rd *prd,
35 afi_t afi, safi_t safi, u_char use_json)
36 {
37 struct bgp *bgp;
38 struct bgp_table *table;
39 struct bgp_node *rn;
40 struct bgp_node *rm;
41 struct attr *attr;
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_routes = NULL;
48 json_object *json_array = NULL;
49
50 bgp = bgp_get_default ();
51 if (bgp == NULL)
52 {
53 if (!use_json)
54 vty_out (vty, "No BGP process is configured\n");
55 return CMD_WARNING;
56 }
57
58 if (use_json)
59 {
60 json_scode = json_object_new_object();
61 json_ocode = json_object_new_object();
62 json_routes = json_object_new_object();
63 json = json_object_new_object();
64
65 json_object_string_add(json_scode, "suppressed", "s");
66 json_object_string_add(json_scode, "damped", "d");
67 json_object_string_add(json_scode, "history", "h");
68 json_object_string_add(json_scode, "valid", "*");
69 json_object_string_add(json_scode, "best", ">");
70 json_object_string_add(json_scode, "internal", "i");
71
72 json_object_string_add(json_ocode, "igp", "i");
73 json_object_string_add(json_ocode, "egp", "e");
74 json_object_string_add(json_ocode, "incomplete", "?");
75 }
76
77 for (rn = bgp_table_top (bgp->rib[afi][SAFI_MPLS_VPN]); rn;
78 rn = bgp_route_next (rn))
79 {
80 if (prd && memcmp (rn->p.u.val, prd->val, 8) != 0)
81 continue;
82
83 if ((table = rn->info) != NULL)
84 {
85 if (use_json)
86 json_array = json_object_new_array();
87 else
88 json_array = NULL;
89
90 rd_header = 1;
91
92 for (rm = bgp_table_top (table); rm; rm = bgp_route_next (rm))
93 {
94 if ((attr = rm->info) != NULL)
95 {
96 if (header)
97 {
98 if (use_json)
99 {
100 json_object_int_add(json, "bgpTableVersion", 0);
101 json_object_string_add(json, "bgpLocalRouterId", inet_ntoa (bgp->router_id));
102 json_object_object_add(json, "bgpStatusCodes", json_scode);
103 json_object_object_add(json, "bgpOriginCodes", json_ocode);
104 }
105 else
106 {
107 vty_out (vty, "BGP table version is 0, local router ID is %s\n",
108 inet_ntoa(bgp->router_id));
109 vty_out (vty,
110 "Status codes: s suppressed, d damped, h history, * valid, > best, i - internal\n");
111 vty_out (vty, "Origin codes: i - IGP, e - EGP, ? - incomplete\n\n");
112 vty_out (vty, V4_HEADER);
113 }
114 header = 0;
115 }
116
117 if (rd_header)
118 {
119 u_int16_t type;
120 struct rd_as rd_as;
121 struct rd_ip rd_ip = {0};
122 #if ENABLE_BGP_VNC
123 struct rd_vnc_eth rd_vnc_eth = {0};
124 #endif
125 u_char *pnt;
126
127 pnt = rn->p.u.val;
128
129 /* Decode RD type. */
130 type = decode_rd_type (pnt);
131 /* Decode RD value. */
132 if (type == RD_TYPE_AS)
133 decode_rd_as (pnt + 2, &rd_as);
134 else if (type == RD_TYPE_AS4)
135 decode_rd_as4 (pnt + 2, &rd_as);
136 else if (type == RD_TYPE_IP)
137 decode_rd_ip (pnt + 2, &rd_ip);
138 #if ENABLE_BGP_VNC
139 else if (type == RD_TYPE_VNC_ETH)
140 decode_rd_vnc_eth (pnt, &rd_vnc_eth);
141 #endif
142
143 if (use_json)
144 {
145 char buffer[BUFSIZ];
146 if (type == RD_TYPE_AS || type == RD_TYPE_AS4)
147 sprintf (buffer, "%u:%d", rd_as.as, rd_as.val);
148 else if (type == RD_TYPE_IP)
149 sprintf (buffer, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
150 json_object_string_add(json_routes, "routeDistinguisher", buffer);
151 }
152 else
153 {
154 vty_out (vty, "Route Distinguisher: ");
155
156 if (type == RD_TYPE_AS || type == RD_TYPE_AS4)
157 vty_out (vty, "%u:%d", rd_as.as, rd_as.val);
158 else if (type == RD_TYPE_IP)
159 vty_out (vty, "%s:%d", inet_ntoa (rd_ip.ip), rd_ip.val);
160 #if ENABLE_BGP_VNC
161 else if (type == RD_TYPE_VNC_ETH)
162 vty_out (vty, "%u:%02x:%02x:%02x:%02x:%02x:%02x",
163 rd_vnc_eth.local_nve_id,
164 rd_vnc_eth.macaddr.octet[0],
165 rd_vnc_eth.macaddr.octet[1],
166 rd_vnc_eth.macaddr.octet[2],
167 rd_vnc_eth.macaddr.octet[3],
168 rd_vnc_eth.macaddr.octet[4],
169 rd_vnc_eth.macaddr.octet[5]);
170 #endif
171
172 vty_out (vty, "\n");
173 }
174 rd_header = 0;
175 }
176 route_vty_out_tmp (vty, &rm->p, attr, SAFI_MPLS_VPN, use_json, json_array);
177 }
178 }
179 if (use_json)
180 {
181 struct prefix *p;
182 char buf_a[BUFSIZ];
183 char buf_b[BUFSIZ];
184 p = &rm->p;
185 sprintf(buf_a, "%s/%d", inet_ntop (p->family, &p->u.prefix, buf_b, BUFSIZ), p->prefixlen);
186 json_object_object_add(json_routes, buf_a, json_array);
187 }
188 }
189 }
190 if (use_json)
191 {
192 json_object_object_add(json, "routes", json_routes);
193 vty_out (vty, "%s\n",
194 json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY));
195 json_object_free(json);
196 }
197 return CMD_SUCCESS;
198 }
199