]>
Commit | Line | Data |
---|---|---|
fb0aa886 AS |
1 | /* |
2 | * zebra_fpm_protobuf.c | |
3 | * | |
4 | * @copyright Copyright (C) 2016 Sproute Networks, Inc. | |
5 | * | |
6 | * @author Avneesh Sachdev <avneesh@sproute.com> | |
7 | * | |
8 | * This file is part of Quagga. | |
9 | * | |
10 | * Quagga is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2, or (at your option) any | |
13 | * later version. | |
14 | * | |
15 | * Quagga is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
896014f4 DL |
20 | * You should have received a copy of the GNU General Public License along |
21 | * with this program; see the file COPYING; if not, write to the Free Software | |
22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
fb0aa886 AS |
23 | */ |
24 | #include <zebra.h> | |
25 | ||
26 | #include "log.h" | |
27 | #include "rib.h" | |
518acd6f DL |
28 | #include "zserv.h" |
29 | #include "zebra_vrf.h" | |
fb0aa886 AS |
30 | |
31 | #include "qpb/qpb.pb-c.h" | |
32 | #include "qpb/qpb.h" | |
33 | #include "qpb/qpb_allocator.h" | |
34 | #include "qpb/linear_allocator.h" | |
35 | #include "fpm/fpm_pb.h" | |
36 | ||
37 | #include "zebra_fpm_private.h" | |
38 | ||
39 | /* | |
40 | * create_delete_route_message | |
41 | */ | |
d62a17ae | 42 | static Fpm__DeleteRoute *create_delete_route_message(qpb_allocator_t *allocator, |
43 | rib_dest_t *dest, | |
44 | struct route_entry *re) | |
fb0aa886 | 45 | { |
d62a17ae | 46 | Fpm__DeleteRoute *msg; |
47 | ||
48 | msg = QPB_ALLOC(allocator, typeof(*msg)); | |
49 | if (!msg) { | |
50 | assert(0); | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | fpm__delete_route__init(msg); | |
55 | msg->vrf_id = zvrf_id(rib_dest_vrf(dest)); | |
56 | ||
57 | qpb_address_family_set(&msg->address_family, rib_dest_af(dest)); | |
58 | ||
59 | /* | |
60 | * XXX Hardcode subaddress family for now. | |
61 | */ | |
62 | msg->sub_address_family = QPB__SUB_ADDRESS_FAMILY__UNICAST; | |
63 | msg->key = fpm_route_key_create(allocator, rib_dest_prefix(dest)); | |
64 | if (!msg->key) { | |
65 | assert(0); | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | return msg; | |
fb0aa886 AS |
70 | } |
71 | ||
72 | /* | |
73 | * add_nexthop | |
74 | */ | |
d62a17ae | 75 | static inline int add_nexthop(qpb_allocator_t *allocator, Fpm__AddRoute *msg, |
76 | rib_dest_t *dest, struct nexthop *nexthop) | |
fb0aa886 | 77 | { |
d62a17ae | 78 | uint32_t if_index; |
79 | union g_addr *gateway, *src; | |
80 | ||
81 | gateway = src = NULL; | |
82 | ||
83 | if_index = nexthop->ifindex; | |
84 | ||
85 | if (nexthop->type == NEXTHOP_TYPE_IPV4 | |
86 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) { | |
87 | gateway = &nexthop->gate; | |
88 | if (nexthop->src.ipv4.s_addr) | |
89 | src = &nexthop->src; | |
90 | } | |
91 | ||
92 | if (nexthop->type == NEXTHOP_TYPE_IPV6 | |
93 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) { | |
94 | gateway = &nexthop->gate; | |
95 | } | |
96 | ||
97 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX) { | |
98 | if (nexthop->src.ipv4.s_addr) | |
99 | src = &nexthop->src; | |
100 | } | |
101 | ||
102 | if (!gateway && if_index == 0) | |
103 | return 0; | |
104 | ||
105 | /* | |
106 | * We have a valid nexthop. | |
107 | */ | |
108 | { | |
109 | Fpm__Nexthop *pb_nh; | |
110 | pb_nh = QPB_ALLOC(allocator, typeof(*pb_nh)); | |
111 | if (!pb_nh) { | |
112 | assert(0); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | fpm__nexthop__init(pb_nh); | |
117 | ||
118 | if (if_index != 0) { | |
119 | pb_nh->if_id = | |
120 | qpb_if_identifier_create(allocator, if_index); | |
121 | } | |
122 | ||
123 | if (gateway) { | |
124 | pb_nh->address = qpb_l3_address_create( | |
125 | allocator, gateway, rib_dest_af(dest)); | |
126 | } | |
127 | ||
128 | msg->nexthops[msg->n_nexthops++] = pb_nh; | |
129 | } | |
130 | ||
131 | // TODO: Use src. | |
132 | ||
133 | return 1; | |
fb0aa886 AS |
134 | } |
135 | ||
136 | /* | |
137 | * create_add_route_message | |
138 | */ | |
d62a17ae | 139 | static Fpm__AddRoute *create_add_route_message(qpb_allocator_t *allocator, |
140 | rib_dest_t *dest, | |
141 | struct route_entry *re) | |
fb0aa886 | 142 | { |
d62a17ae | 143 | Fpm__AddRoute *msg; |
144 | int discard; | |
145 | struct nexthop *nexthop; | |
146 | uint num_nhs, u; | |
147 | struct nexthop *nexthops[MULTIPATH_NUM]; | |
148 | ||
149 | msg = QPB_ALLOC(allocator, typeof(*msg)); | |
150 | if (!msg) { | |
151 | assert(0); | |
152 | return NULL; | |
153 | } | |
154 | ||
155 | fpm__add_route__init(msg); | |
156 | ||
157 | msg->vrf_id = zvrf_id(rib_dest_vrf(dest)); | |
158 | ||
159 | qpb_address_family_set(&msg->address_family, rib_dest_af(dest)); | |
160 | ||
161 | /* | |
162 | * XXX Hardcode subaddress family for now. | |
163 | */ | |
164 | msg->sub_address_family = QPB__SUB_ADDRESS_FAMILY__UNICAST; | |
165 | msg->key = fpm_route_key_create(allocator, rib_dest_prefix(dest)); | |
166 | qpb_protocol_set(&msg->protocol, re->type); | |
167 | ||
168 | if ((re->flags & ZEBRA_FLAG_BLACKHOLE) | |
169 | || (re->flags & ZEBRA_FLAG_REJECT)) | |
170 | discard = 1; | |
171 | else | |
172 | discard = 0; | |
173 | ||
174 | if (discard) { | |
175 | if (re->flags & ZEBRA_FLAG_BLACKHOLE) { | |
176 | msg->route_type = FPM__ROUTE_TYPE__BLACKHOLE; | |
177 | } else if (re->flags & ZEBRA_FLAG_REJECT) { | |
178 | msg->route_type = FPM__ROUTE_TYPE__UNREACHABLE; | |
179 | } else { | |
180 | assert(0); | |
181 | } | |
182 | return msg; | |
183 | } else { | |
184 | msg->route_type = FPM__ROUTE_TYPE__NORMAL; | |
185 | } | |
186 | ||
187 | msg->metric = re->metric; | |
188 | ||
189 | /* | |
190 | * Figure out the set of nexthops to be added to the message. | |
191 | */ | |
192 | num_nhs = 0; | |
193 | for (ALL_NEXTHOPS(re->nexthop, nexthop)) { | |
194 | if (num_nhs >= multipath_num) | |
195 | break; | |
196 | ||
197 | if (num_nhs >= ZEBRA_NUM_OF(nexthops)) | |
198 | break; | |
199 | ||
200 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
201 | continue; | |
202 | ||
203 | if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) | |
204 | continue; | |
205 | ||
206 | nexthops[num_nhs] = nexthop; | |
207 | num_nhs++; | |
208 | } | |
209 | ||
210 | if (!num_nhs) { | |
211 | zfpm_debug("netlink_encode_route(): No useful nexthop."); | |
212 | assert(0); | |
213 | return NULL; | |
214 | } | |
215 | ||
216 | /* | |
217 | * And add them to the message. | |
218 | */ | |
219 | if (!(msg->nexthops = qpb_alloc_ptr_array(allocator, num_nhs))) { | |
220 | assert(0); | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | msg->n_nexthops = 0; | |
225 | for (u = 0; u < num_nhs; u++) { | |
226 | if (!add_nexthop(allocator, msg, dest, nexthops[u])) { | |
227 | assert(0); | |
228 | return NULL; | |
229 | } | |
230 | } | |
231 | ||
232 | assert(msg->n_nexthops == num_nhs); | |
233 | ||
234 | return msg; | |
fb0aa886 AS |
235 | } |
236 | ||
237 | /* | |
238 | * create_route_message | |
239 | */ | |
d62a17ae | 240 | static Fpm__Message *create_route_message(qpb_allocator_t *allocator, |
241 | rib_dest_t *dest, | |
242 | struct route_entry *re) | |
fb0aa886 | 243 | { |
d62a17ae | 244 | Fpm__Message *msg; |
245 | ||
246 | msg = QPB_ALLOC(allocator, typeof(*msg)); | |
247 | if (!msg) { | |
248 | assert(0); | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | fpm__message__init(msg); | |
253 | ||
254 | if (!re) { | |
255 | msg->type = FPM__MESSAGE__TYPE__DELETE_ROUTE; | |
256 | msg->delete_route = | |
257 | create_delete_route_message(allocator, dest, re); | |
258 | if (!msg->delete_route) { | |
259 | assert(0); | |
260 | return NULL; | |
261 | } | |
262 | return msg; | |
263 | } | |
264 | ||
265 | msg->type = FPM__MESSAGE__TYPE__ADD_ROUTE; | |
266 | msg->add_route = create_add_route_message(allocator, dest, re); | |
267 | if (!msg->add_route) { | |
268 | assert(0); | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | return msg; | |
fb0aa886 AS |
273 | } |
274 | ||
275 | /* | |
276 | * zfpm_protobuf_encode_route | |
277 | * | |
278 | * Create a protobuf message corresponding to the given route in the | |
279 | * given buffer space. | |
280 | * | |
281 | * Returns the number of bytes written to the buffer. 0 or a negative | |
282 | * value indicates an error. | |
283 | */ | |
d62a17ae | 284 | int zfpm_protobuf_encode_route(rib_dest_t *dest, struct route_entry *re, |
285 | uint8_t *in_buf, size_t in_buf_len) | |
fb0aa886 | 286 | { |
d62a17ae | 287 | Fpm__Message *msg; |
288 | QPB_DECLARE_STACK_ALLOCATOR(allocator, 4096); | |
289 | size_t len; | |
fb0aa886 | 290 | |
d62a17ae | 291 | QPB_INIT_STACK_ALLOCATOR(allocator); |
fb0aa886 | 292 | |
d62a17ae | 293 | msg = create_route_message(&allocator, dest, re); |
294 | if (!msg) { | |
295 | assert(0); | |
296 | return 0; | |
297 | } | |
fb0aa886 | 298 | |
d62a17ae | 299 | len = fpm__message__pack(msg, (uint8_t *)in_buf); |
300 | assert(len <= in_buf_len); | |
fb0aa886 | 301 | |
d62a17ae | 302 | QPB_RESET_STACK_ALLOCATOR(allocator); |
303 | return len; | |
fb0aa886 | 304 | } |