]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_protobuf.c
Merge remote-tracking branch 'frr/master' into table-hash-ospf6-lsdb-refactor
[mirror_frr.git] / zebra / zebra_fpm_protobuf.c
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 *
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
23 */
24 #include <zebra.h>
25
26 #include "log.h"
27 #include "rib.h"
28 #include "zserv.h"
29 #include "zebra_vrf.h"
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 */
42 static Fpm__DeleteRoute *
43 create_delete_route_message (qpb_allocator_t *allocator, rib_dest_t *dest,
44 struct route_entry *re)
45 {
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;
70 }
71
72 /*
73 * add_nexthop
74 */
75 static inline int
76 add_nexthop (qpb_allocator_t *allocator, Fpm__AddRoute *msg, rib_dest_t *dest,
77 struct nexthop *nexthop)
78 {
79 uint32_t if_index;
80 union g_addr *gateway, *src;
81
82 gateway = src = NULL;
83
84 if_index = nexthop->ifindex;
85
86 if (nexthop->type == NEXTHOP_TYPE_IPV4
87 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
88 {
89 gateway = &nexthop->gate;
90 if (nexthop->src.ipv4.s_addr)
91 src = &nexthop->src;
92 }
93
94 if (nexthop->type == NEXTHOP_TYPE_IPV6
95 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
96 {
97 gateway = &nexthop->gate;
98 }
99
100 if (nexthop->type == NEXTHOP_TYPE_IFINDEX)
101 {
102 if (nexthop->src.ipv4.s_addr)
103 src = &nexthop->src;
104 }
105
106 if (!gateway && if_index == 0)
107 return 0;
108
109 /*
110 * We have a valid nexthop.
111 */
112 {
113 Fpm__Nexthop *pb_nh;
114 pb_nh = QPB_ALLOC(allocator, typeof(*pb_nh));
115 if (!pb_nh) {
116 assert(0);
117 return 0;
118 }
119
120 fpm__nexthop__init(pb_nh);
121
122 if (if_index != 0) {
123 pb_nh->if_id = qpb_if_identifier_create (allocator, if_index);
124 }
125
126 if (gateway) {
127 pb_nh->address = qpb_l3_address_create (allocator, gateway,
128 rib_dest_af(dest));
129 }
130
131 msg->nexthops[msg->n_nexthops++] = pb_nh;
132 }
133
134 // TODO: Use src.
135
136 return 1;
137 }
138
139 /*
140 * create_add_route_message
141 */
142 static Fpm__AddRoute *
143 create_add_route_message (qpb_allocator_t *allocator, rib_dest_t *dest,
144 struct route_entry *re)
145 {
146 Fpm__AddRoute *msg;
147 int discard;
148 struct nexthop *nexthop;
149 uint num_nhs, u;
150 struct nexthop *nexthops[MULTIPATH_NUM];
151
152 msg = QPB_ALLOC(allocator, typeof(*msg));
153 if (!msg) {
154 assert(0);
155 return NULL;
156 }
157
158 fpm__add_route__init(msg);
159
160 msg->vrf_id = zvrf_id(rib_dest_vrf(dest));
161
162 qpb_address_family_set (&msg->address_family, rib_dest_af(dest));
163
164 /*
165 * XXX Hardcode subaddress family for now.
166 */
167 msg->sub_address_family = QPB__SUB_ADDRESS_FAMILY__UNICAST;
168 msg->key = fpm_route_key_create (allocator, rib_dest_prefix(dest));
169 qpb_protocol_set (&msg->protocol, re->type);
170
171 if ((re->flags & ZEBRA_FLAG_BLACKHOLE) || (re->flags & ZEBRA_FLAG_REJECT))
172 discard = 1;
173 else
174 discard = 0;
175
176 if (discard)
177 {
178 if (re->flags & ZEBRA_FLAG_BLACKHOLE) {
179 msg->route_type = FPM__ROUTE_TYPE__BLACKHOLE;
180 } else if (re->flags & ZEBRA_FLAG_REJECT) {
181 msg->route_type = FPM__ROUTE_TYPE__UNREACHABLE;
182 } else {
183 assert (0);
184 }
185 return msg;
186 }
187 else {
188 msg->route_type = FPM__ROUTE_TYPE__NORMAL;
189 }
190
191 msg->metric = re->metric;
192
193 /*
194 * Figure out the set of nexthops to be added to the message.
195 */
196 num_nhs = 0;
197 for (ALL_NEXTHOPS (re->nexthop, nexthop))
198 {
199 if (num_nhs >= multipath_num)
200 break;
201
202 if (num_nhs >= ZEBRA_NUM_OF(nexthops))
203 break;
204
205 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
206 continue;
207
208 if (!CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
209 continue;
210
211 nexthops[num_nhs] = nexthop;
212 num_nhs++;
213 }
214
215 if (!num_nhs) {
216 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
217 assert(0);
218 return NULL;
219 }
220
221 /*
222 * And add them to the message.
223 */
224 if (!(msg->nexthops = qpb_alloc_ptr_array(allocator, num_nhs))) {
225 assert(0);
226 return NULL;
227 }
228
229 msg->n_nexthops = 0;
230 for (u = 0; u < num_nhs; u++) {
231 if (!add_nexthop(allocator, msg, dest, nexthops[u])) {
232 assert(0);
233 return NULL;
234 }
235 }
236
237 assert(msg->n_nexthops == num_nhs);
238
239 return msg;
240 }
241
242 /*
243 * create_route_message
244 */
245 static Fpm__Message *
246 create_route_message (qpb_allocator_t *allocator, rib_dest_t *dest,
247 struct route_entry *re)
248 {
249 Fpm__Message *msg;
250
251 msg = QPB_ALLOC(allocator, typeof(*msg));
252 if (!msg) {
253 assert(0);
254 return NULL;
255 }
256
257 fpm__message__init(msg);
258
259 if (!re) {
260 msg->type = FPM__MESSAGE__TYPE__DELETE_ROUTE;
261 msg->delete_route = create_delete_route_message(allocator, dest, re);
262 if (!msg->delete_route) {
263 assert(0);
264 return NULL;
265 }
266 return msg;
267 }
268
269 msg->type = FPM__MESSAGE__TYPE__ADD_ROUTE;
270 msg->add_route = create_add_route_message(allocator, dest, re);
271 if (!msg->add_route) {
272 assert(0);
273 return NULL;
274 }
275
276 return msg;
277 }
278
279 /*
280 * zfpm_protobuf_encode_route
281 *
282 * Create a protobuf message corresponding to the given route in the
283 * given buffer space.
284 *
285 * Returns the number of bytes written to the buffer. 0 or a negative
286 * value indicates an error.
287 */
288 int
289 zfpm_protobuf_encode_route (rib_dest_t *dest, struct route_entry *re,
290 uint8_t *in_buf, size_t in_buf_len)
291 {
292 Fpm__Message *msg;
293 QPB_DECLARE_STACK_ALLOCATOR (allocator, 4096);
294 size_t len;
295
296 QPB_INIT_STACK_ALLOCATOR (allocator);
297
298 msg = create_route_message(&allocator, dest, re);
299 if (!msg) {
300 assert(0);
301 return 0;
302 }
303
304 len = fpm__message__pack(msg, (uint8_t *) in_buf);
305 assert(len <= in_buf_len);
306
307 QPB_RESET_STACK_ALLOCATOR (allocator);
308 return len;
309 }