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