]>
Commit | Line | Data |
---|---|---|
f80ec39e RW |
1 | /* |
2 | * Copyright (C) 2018 NetDEF, Inc. | |
3 | * Renato Westphal | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "if.h" | |
23 | #include "vrf.h" | |
24 | #include "log.h" | |
25 | #include "prefix.h" | |
26 | #include "table.h" | |
27 | #include "command.h" | |
28 | #include "routemap.h" | |
29 | #include "northbound.h" | |
30 | #include "libfrr.h" | |
31 | ||
32 | #include "ripd/ripd.h" | |
33 | #include "ripd/rip_nb.h" | |
34 | #include "ripd/rip_debug.h" | |
35 | #include "ripd/rip_interface.h" | |
36 | ||
37 | /* | |
38 | * XPath: /frr-ripd:ripd/instance | |
39 | */ | |
40 | const void *ripd_instance_get_next(const void *parent_list_entry, | |
41 | const void *list_entry) | |
42 | { | |
43 | struct rip *rip = (struct rip *)list_entry; | |
44 | ||
45 | if (list_entry == NULL) | |
46 | rip = RB_MIN(rip_instance_head, &rip_instances); | |
47 | else | |
48 | rip = RB_NEXT(rip_instance_head, rip); | |
49 | ||
50 | return rip; | |
51 | } | |
52 | ||
53 | int ripd_instance_get_keys(const void *list_entry, struct yang_list_keys *keys) | |
54 | { | |
55 | const struct rip *rip = list_entry; | |
56 | ||
57 | keys->num = 1; | |
58 | strlcpy(keys->key[0], rip->vrf_name, sizeof(keys->key[0])); | |
59 | ||
60 | return NB_OK; | |
61 | } | |
62 | ||
63 | const void *ripd_instance_lookup_entry(const void *parent_list_entry, | |
64 | const struct yang_list_keys *keys) | |
65 | { | |
66 | const char *vrf_name = keys->key[0]; | |
67 | ||
68 | return rip_lookup_by_vrf_name(vrf_name); | |
69 | } | |
70 | ||
71 | /* | |
72 | * XPath: /frr-ripd:ripd/instance/state/neighbors/neighbor | |
73 | */ | |
74 | const void * | |
75 | ripd_instance_state_neighbors_neighbor_get_next(const void *parent_list_entry, | |
76 | const void *list_entry) | |
77 | { | |
78 | const struct rip *rip = parent_list_entry; | |
79 | struct listnode *node; | |
80 | ||
81 | if (list_entry == NULL) | |
82 | node = listhead(rip->peer_list); | |
83 | else | |
84 | node = listnextnode((struct listnode *)list_entry); | |
85 | ||
86 | return node; | |
87 | } | |
88 | ||
89 | int ripd_instance_state_neighbors_neighbor_get_keys(const void *list_entry, | |
90 | struct yang_list_keys *keys) | |
91 | { | |
92 | const struct listnode *node = list_entry; | |
93 | const struct rip_peer *peer = listgetdata(node); | |
94 | ||
95 | keys->num = 1; | |
96 | (void)inet_ntop(AF_INET, &peer->addr, keys->key[0], | |
97 | sizeof(keys->key[0])); | |
98 | ||
99 | return NB_OK; | |
100 | } | |
101 | ||
102 | const void *ripd_instance_state_neighbors_neighbor_lookup_entry( | |
103 | const void *parent_list_entry, const struct yang_list_keys *keys) | |
104 | { | |
105 | const struct rip *rip = parent_list_entry; | |
106 | struct in_addr address; | |
107 | struct rip_peer *peer; | |
108 | struct listnode *node; | |
109 | ||
110 | yang_str2ipv4(keys->key[0], &address); | |
111 | ||
112 | for (ALL_LIST_ELEMENTS_RO(rip->peer_list, node, peer)) { | |
113 | if (IPV4_ADDR_SAME(&peer->addr, &address)) | |
114 | return node; | |
115 | } | |
116 | ||
117 | return NULL; | |
118 | } | |
119 | ||
120 | /* | |
121 | * XPath: /frr-ripd:ripd/instance/state/neighbors/neighbor/address | |
122 | */ | |
123 | struct yang_data * | |
124 | ripd_instance_state_neighbors_neighbor_address_get_elem(const char *xpath, | |
125 | const void *list_entry) | |
126 | { | |
127 | const struct listnode *node = list_entry; | |
128 | const struct rip_peer *peer = listgetdata(node); | |
129 | ||
130 | return yang_data_new_ipv4(xpath, &peer->addr); | |
131 | } | |
132 | ||
133 | /* | |
134 | * XPath: /frr-ripd:ripd/instance/state/neighbors/neighbor/last-update | |
135 | */ | |
136 | struct yang_data *ripd_instance_state_neighbors_neighbor_last_update_get_elem( | |
137 | const char *xpath, const void *list_entry) | |
138 | { | |
139 | /* TODO: yang:date-and-time is tricky */ | |
140 | return NULL; | |
141 | } | |
142 | ||
143 | /* | |
144 | * XPath: /frr-ripd:ripd/instance/state/neighbors/neighbor/bad-packets-rcvd | |
145 | */ | |
146 | struct yang_data * | |
147 | ripd_instance_state_neighbors_neighbor_bad_packets_rcvd_get_elem( | |
148 | const char *xpath, const void *list_entry) | |
149 | { | |
150 | const struct listnode *node = list_entry; | |
151 | const struct rip_peer *peer = listgetdata(node); | |
152 | ||
153 | return yang_data_new_uint32(xpath, peer->recv_badpackets); | |
154 | } | |
155 | ||
156 | /* | |
157 | * XPath: /frr-ripd:ripd/instance/state/neighbors/neighbor/bad-routes-rcvd | |
158 | */ | |
159 | struct yang_data * | |
160 | ripd_instance_state_neighbors_neighbor_bad_routes_rcvd_get_elem( | |
161 | const char *xpath, const void *list_entry) | |
162 | { | |
163 | const struct listnode *node = list_entry; | |
164 | const struct rip_peer *peer = listgetdata(node); | |
165 | ||
166 | return yang_data_new_uint32(xpath, peer->recv_badroutes); | |
167 | } | |
168 | ||
169 | /* | |
170 | * XPath: /frr-ripd:ripd/instance/state/routes/route | |
171 | */ | |
172 | const void * | |
173 | ripd_instance_state_routes_route_get_next(const void *parent_list_entry, | |
174 | const void *list_entry) | |
175 | { | |
176 | const struct rip *rip = parent_list_entry; | |
177 | struct route_node *rn; | |
178 | ||
179 | if (list_entry == NULL) | |
180 | rn = route_top(rip->table); | |
181 | else | |
182 | rn = route_next((struct route_node *)list_entry); | |
183 | while (rn && rn->info == NULL) | |
184 | rn = route_next(rn); | |
185 | ||
186 | return rn; | |
187 | } | |
188 | ||
189 | int ripd_instance_state_routes_route_get_keys(const void *list_entry, | |
190 | struct yang_list_keys *keys) | |
191 | { | |
192 | const struct route_node *rn = list_entry; | |
193 | ||
194 | keys->num = 1; | |
195 | (void)prefix2str(&rn->p, keys->key[0], sizeof(keys->key[0])); | |
196 | ||
197 | return NB_OK; | |
198 | } | |
199 | ||
200 | const void * | |
201 | ripd_instance_state_routes_route_lookup_entry(const void *parent_list_entry, | |
202 | const struct yang_list_keys *keys) | |
203 | { | |
204 | const struct rip *rip = parent_list_entry; | |
205 | struct prefix prefix; | |
206 | struct route_node *rn; | |
207 | ||
208 | yang_str2ipv4p(keys->key[0], &prefix); | |
209 | ||
210 | rn = route_node_lookup(rip->table, &prefix); | |
211 | if (!rn || !rn->info) | |
212 | return NULL; | |
213 | ||
214 | route_unlock_node(rn); | |
215 | ||
216 | return rn; | |
217 | } | |
218 | ||
219 | /* | |
220 | * XPath: /frr-ripd:ripd/instance/state/routes/route/prefix | |
221 | */ | |
222 | struct yang_data * | |
223 | ripd_instance_state_routes_route_prefix_get_elem(const char *xpath, | |
224 | const void *list_entry) | |
225 | { | |
226 | const struct route_node *rn = list_entry; | |
227 | const struct rip_info *rinfo = listnode_head(rn->info); | |
228 | ||
229 | return yang_data_new_ipv4p(xpath, &rinfo->rp->p); | |
230 | } | |
231 | ||
232 | /* | |
233 | * XPath: /frr-ripd:ripd/instance/state/routes/route/next-hop | |
234 | */ | |
235 | struct yang_data * | |
236 | ripd_instance_state_routes_route_next_hop_get_elem(const char *xpath, | |
237 | const void *list_entry) | |
238 | { | |
239 | const struct route_node *rn = list_entry; | |
240 | const struct rip_info *rinfo = listnode_head(rn->info); | |
241 | ||
242 | switch (rinfo->nh.type) { | |
243 | case NEXTHOP_TYPE_IPV4: | |
244 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
245 | return yang_data_new_ipv4(xpath, &rinfo->nh.gate.ipv4); | |
246 | default: | |
247 | return NULL; | |
248 | } | |
249 | } | |
250 | ||
251 | /* | |
252 | * XPath: /frr-ripd:ripd/instance/state/routes/route/interface | |
253 | */ | |
254 | struct yang_data * | |
255 | ripd_instance_state_routes_route_interface_get_elem(const char *xpath, | |
256 | const void *list_entry) | |
257 | { | |
258 | const struct route_node *rn = list_entry; | |
259 | const struct rip_info *rinfo = listnode_head(rn->info); | |
260 | const struct rip *rip = rip_info_get_instance(rinfo); | |
261 | ||
262 | switch (rinfo->nh.type) { | |
263 | case NEXTHOP_TYPE_IFINDEX: | |
264 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
265 | return yang_data_new_string( | |
266 | xpath, | |
267 | ifindex2ifname(rinfo->nh.ifindex, rip->vrf->vrf_id)); | |
268 | default: | |
269 | return NULL; | |
270 | } | |
271 | } | |
272 | ||
273 | /* | |
274 | * XPath: /frr-ripd:ripd/instance/state/routes/route/metric | |
275 | */ | |
276 | struct yang_data * | |
277 | ripd_instance_state_routes_route_metric_get_elem(const char *xpath, | |
278 | const void *list_entry) | |
279 | { | |
280 | const struct route_node *rn = list_entry; | |
281 | const struct rip_info *rinfo = listnode_head(rn->info); | |
282 | ||
283 | return yang_data_new_uint8(xpath, rinfo->metric); | |
284 | } |