Line data Source code
1 : /*
2 : * Copyright (c) 2016 Cisco and/or its affiliates.
3 : * Licensed under the Apache License, Version 2.0 (the "License");
4 : * you may not use this file except in compliance with the License.
5 : * You may obtain a copy of the License at:
6 : *
7 : * http://www.apache.org/licenses/LICENSE-2.0
8 : *
9 : * Unless required by applicable law or agreed to in writing, software
10 : * distributed under the License is distributed on an "AS IS" BASIS,
11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : * See the License for the specific language governing permissions and
13 : * limitations under the License.
14 : */
15 :
16 : #include <vnet/vnet.h>
17 : #include <vnet/plugin/plugin.h>
18 : #include <lb/lb.h>
19 :
20 : #include <vppinfra/byte_order.h>
21 : #include <vppinfra/string.h>
22 : #include <vpp/api/types.h>
23 : #include <vlibapi/api.h>
24 : #include <vlibmemory/api.h>
25 : #include <vpp/app/version.h>
26 : #include <vnet/format_fns.h>
27 : #include <vnet/ip/ip_types_api.h>
28 :
29 : /* define message IDs */
30 : #include <lb/lb.api_enum.h>
31 : #include <lb/lb.api_types.h>
32 :
33 :
34 : #define REPLY_MSG_ID_BASE lbm->msg_id_base
35 : #include <vlibapi/api_helper_macros.h>
36 :
37 : #define FINISH \
38 : vec_add1 (s, 0); \
39 : vlib_cli_output (handle, (char *) s); \
40 : vec_free (s); \
41 : return handle;
42 :
43 : static void
44 1 : vl_api_lb_conf_t_handler
45 : (vl_api_lb_conf_t * mp)
46 : {
47 1 : lb_main_t *lbm = &lb_main;
48 : vl_api_lb_conf_reply_t * rmp;
49 : u32 sticky_buckets_per_core, flow_timeout;
50 1 : int rv = 0;
51 :
52 2 : sticky_buckets_per_core = mp->sticky_buckets_per_core == ~0
53 : ? lbm->per_cpu_sticky_buckets
54 1 : : ntohl(mp->sticky_buckets_per_core);
55 2 : flow_timeout = mp->flow_timeout == ~0
56 : ? lbm->flow_timeout
57 1 : : ntohl(mp->flow_timeout);
58 :
59 1 : rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
60 1 : (ip6_address_t *)&mp->ip6_src_address,
61 : sticky_buckets_per_core, flow_timeout);
62 :
63 1 : REPLY_MACRO (VL_API_LB_CONF_REPLY);
64 : }
65 :
66 : static void
67 0 : vl_api_lb_add_del_vip_t_handler
68 : (vl_api_lb_add_del_vip_t * mp)
69 : {
70 0 : lb_main_t *lbm = &lb_main;
71 : vl_api_lb_conf_reply_t * rmp;
72 0 : int rv = 0;
73 0 : lb_vip_add_args_t args = {};
74 :
75 : /* if port == 0, it means all-port VIP */
76 0 : if (mp->port == 0)
77 : {
78 0 : mp->protocol = ~0;
79 : }
80 :
81 0 : ip_address_decode (&mp->pfx.address, &(args.prefix));
82 :
83 0 : if (mp->is_del) {
84 : u32 vip_index;
85 0 : if (!(rv = lb_vip_find_index(&(args.prefix), mp->pfx.len,
86 0 : mp->protocol, ntohs(mp->port), &vip_index)))
87 0 : rv = lb_vip_del(vip_index);
88 : } else {
89 : u32 vip_index;
90 0 : lb_vip_type_t type = 0;
91 :
92 0 : if (ip46_prefix_is_ip4(&(args.prefix), mp->pfx.len)) {
93 0 : if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
94 0 : type = LB_VIP_TYPE_IP4_GRE4;
95 0 : else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
96 0 : type = LB_VIP_TYPE_IP4_GRE6;
97 0 : else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
98 0 : type = LB_VIP_TYPE_IP4_L3DSR;
99 0 : else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
100 0 : type = LB_VIP_TYPE_IP4_NAT4;
101 : } else {
102 0 : if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
103 0 : type = LB_VIP_TYPE_IP6_GRE4;
104 0 : else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
105 0 : type = LB_VIP_TYPE_IP6_GRE6;
106 0 : else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
107 0 : type = LB_VIP_TYPE_IP6_NAT6;
108 : }
109 :
110 0 : args.plen = mp->pfx.len;
111 0 : args.protocol = mp->protocol;
112 0 : args.port = ntohs(mp->port);
113 0 : args.type = type;
114 0 : args.new_length = ntohl(mp->new_flows_table_length);
115 :
116 0 : if (mp->encap == LB_API_ENCAP_TYPE_L3DSR) {
117 0 : args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
118 : }
119 0 : else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4)
120 0 : ||(mp->encap == LB_API_ENCAP_TYPE_NAT6)) {
121 0 : args.encap_args.srv_type = mp->type;
122 0 : args.encap_args.target_port = ntohs(mp->target_port);
123 : }
124 :
125 0 : rv = lb_vip_add(args, &vip_index);
126 : }
127 0 : REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
128 : }
129 :
130 : static void
131 0 : vl_api_lb_add_del_vip_v2_t_handler (vl_api_lb_add_del_vip_v2_t *mp)
132 : {
133 0 : lb_main_t *lbm = &lb_main;
134 : vl_api_lb_conf_reply_t *rmp;
135 0 : int rv = 0;
136 0 : lb_vip_add_args_t args = {};
137 :
138 : /* if port == 0, it means all-port VIP */
139 0 : if (mp->port == 0)
140 : {
141 0 : mp->protocol = ~0;
142 : }
143 :
144 0 : ip_address_decode (&mp->pfx.address, &(args.prefix));
145 :
146 0 : if (mp->is_del)
147 : {
148 : u32 vip_index;
149 0 : if (!(rv = lb_vip_find_index (&(args.prefix), mp->pfx.len, mp->protocol,
150 0 : ntohs (mp->port), &vip_index)))
151 0 : rv = lb_vip_del (vip_index);
152 : }
153 : else
154 : {
155 : u32 vip_index;
156 0 : lb_vip_type_t type = 0;
157 :
158 0 : if (ip46_prefix_is_ip4 (&(args.prefix), mp->pfx.len))
159 : {
160 0 : if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
161 0 : type = LB_VIP_TYPE_IP4_GRE4;
162 0 : else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
163 0 : type = LB_VIP_TYPE_IP4_GRE6;
164 0 : else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
165 0 : type = LB_VIP_TYPE_IP4_L3DSR;
166 0 : else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
167 0 : type = LB_VIP_TYPE_IP4_NAT4;
168 : }
169 : else
170 : {
171 0 : if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
172 0 : type = LB_VIP_TYPE_IP6_GRE4;
173 0 : else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
174 0 : type = LB_VIP_TYPE_IP6_GRE6;
175 0 : else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
176 0 : type = LB_VIP_TYPE_IP6_NAT6;
177 : }
178 :
179 0 : args.plen = mp->pfx.len;
180 0 : args.protocol = mp->protocol;
181 0 : args.port = ntohs (mp->port);
182 0 : args.type = type;
183 0 : args.new_length = ntohl (mp->new_flows_table_length);
184 :
185 0 : if (mp->src_ip_sticky)
186 0 : args.src_ip_sticky = 1;
187 :
188 0 : if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
189 : {
190 0 : args.encap_args.dscp = (u8) (mp->dscp & 0x3F);
191 : }
192 0 : else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4) ||
193 0 : (mp->encap == LB_API_ENCAP_TYPE_NAT6))
194 : {
195 0 : args.encap_args.srv_type = mp->type;
196 0 : args.encap_args.target_port = ntohs (mp->target_port);
197 : }
198 :
199 0 : rv = lb_vip_add (args, &vip_index);
200 : }
201 0 : REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_V2_REPLY);
202 : }
203 :
204 : static void
205 0 : vl_api_lb_add_del_as_t_handler
206 : (vl_api_lb_add_del_as_t * mp)
207 : {
208 0 : lb_main_t *lbm = &lb_main;
209 : vl_api_lb_conf_reply_t * rmp;
210 0 : int rv = 0;
211 : u32 vip_index;
212 : ip46_address_t vip_ip_prefix;
213 : ip46_address_t as_address;
214 :
215 : /* if port == 0, it means all-port VIP */
216 0 : if (mp->port == 0)
217 : {
218 0 : mp->protocol = ~0;
219 : }
220 0 : ip_address_decode (&mp->pfx.address, &vip_ip_prefix);
221 0 : ip_address_decode (&mp->as_address, &as_address);
222 :
223 0 : if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
224 0 : mp->protocol, ntohs(mp->port), &vip_index)))
225 0 : goto done;
226 :
227 0 : if (mp->is_del)
228 0 : rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
229 : else
230 0 : rv = lb_vip_add_ass(vip_index, &as_address, 1);
231 :
232 0 : done:
233 0 : REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
234 : }
235 :
236 : static void
237 2 : vl_api_lb_vip_dump_t_handler
238 : (vl_api_lb_vip_dump_t * mp)
239 : {
240 :
241 : vl_api_registration_t *reg;
242 2 : reg = vl_api_client_index_to_registration (mp->client_index);
243 2 : if (!reg)
244 0 : return;
245 :
246 2 : lb_main_t *lbm = &lb_main;
247 : vl_api_lb_vip_details_t * rmp;
248 2 : int msg_size = 0;
249 2 : lb_vip_t *vip = 0;
250 :
251 : /* construct vip list */
252 5 : pool_foreach (vip, lbm->vips) {
253 : /* Hide placeholder VIP */
254 3 : if (vip != lbm->vips) {
255 1 : msg_size = sizeof (*rmp);
256 1 : rmp = vl_msg_api_alloc (msg_size);
257 1 : memset (rmp, 0, msg_size);
258 1 : rmp->_vl_msg_id =
259 1 : htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
260 1 : rmp->context = mp->context;
261 :
262 1 : ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
263 1 : rmp->vip.pfx.len = vip->plen;
264 1 : rmp->vip.protocol = htonl (vip->protocol);
265 1 : rmp->vip.port = htons(vip->port);
266 1 : rmp->encap = htonl(vip->type);
267 1 : rmp->dscp = vip->encap_args.dscp;
268 1 : rmp->srv_type = vip->encap_args.srv_type;
269 1 : rmp->target_port = htons(vip->encap_args.target_port);
270 1 : rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
271 :
272 1 : vl_api_send_msg (reg, (u8 *) rmp);
273 : }
274 : }
275 :
276 :
277 : }
278 :
279 3 : static void send_lb_as_details
280 : (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
281 : {
282 : vl_api_lb_as_details_t *rmp;
283 3 : lb_main_t *lbm = &lb_main;
284 3 : int msg_size = 0;
285 : u32 *as_index;
286 :
287 : /* construct as list under this vip */
288 : lb_as_t *as;
289 :
290 4 : pool_foreach (as_index, vip->as_indexes) {
291 : /* Hide placeholder As for specific VIP */
292 1 : if (*as_index != 0) {
293 1 : as = &lbm->ass[*as_index];
294 1 : msg_size = sizeof (*rmp);
295 1 : rmp = vl_msg_api_alloc (msg_size);
296 1 : memset (rmp, 0, msg_size);
297 1 : rmp->_vl_msg_id =
298 1 : htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
299 1 : rmp->context = context;
300 1 : ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
301 1 : rmp->vip.pfx.len = vip->plen;
302 1 : rmp->vip.protocol = htonl (vip->protocol);
303 1 : rmp->vip.port = htons(vip->port);
304 1 : ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
305 1 : rmp->flags = as->flags;
306 1 : rmp->in_use_since = htonl(as->last_used);
307 :
308 1 : vl_api_send_msg (reg, (u8 *) rmp);
309 : }
310 : }
311 :
312 :
313 3 : }
314 :
315 : static void
316 2 : vl_api_lb_as_dump_t_handler
317 : (vl_api_lb_as_dump_t * mp)
318 : {
319 2 : lb_main_t *lbm = &lb_main;
320 2 : lb_vip_t *vip = 0;
321 2 : u8 dump_all = 0;
322 : ip46_address_t prefix;
323 :
324 : vl_api_registration_t *reg;
325 2 : reg = vl_api_client_index_to_registration (mp->client_index);
326 2 : if (!reg)
327 0 : return;
328 :
329 2 : clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
330 :
331 2 : dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
332 :
333 : /* *INDENT-OFF* */
334 5 : pool_foreach (vip, lbm->vips)
335 : {
336 3 : if ( dump_all
337 0 : || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
338 0 : && (prefix.as_u64[1] == vip->prefix.as_u64[1])
339 0 : && (mp->protocol == vip->protocol)
340 0 : && (mp->port == vip->port)) )
341 : {
342 3 : send_lb_as_details(reg, mp->context, vip);
343 : }
344 : }
345 : /* *INDENT-ON* */
346 : }
347 :
348 : static void
349 0 : vl_api_lb_flush_vip_t_handler
350 : (vl_api_lb_flush_vip_t * mp)
351 : {
352 0 : lb_main_t *lbm = &lb_main;
353 0 : int rv = 0;
354 : ip46_address_t vip_prefix;
355 : u8 vip_plen;
356 : u32 vip_index;
357 : vl_api_lb_flush_vip_reply_t * rmp;
358 :
359 0 : if (mp->port == 0)
360 : {
361 0 : mp->protocol = ~0;
362 : }
363 :
364 0 : memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
365 :
366 0 : vip_plen = mp->pfx.len;
367 :
368 0 : rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
369 0 : ntohs(mp->port), &vip_index);
370 :
371 0 : rv = lb_flush_vip_as(vip_index, ~0);
372 :
373 0 : REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
374 : }
375 :
376 0 : static void vl_api_lb_add_del_intf_nat4_t_handler
377 : (vl_api_lb_add_del_intf_nat4_t * mp)
378 : {
379 0 : lb_main_t *lbm = &lb_main;
380 : vl_api_lb_add_del_intf_nat4_reply_t *rmp;
381 0 : u32 sw_if_index = ntohl (mp->sw_if_index);
382 : u8 is_del;
383 0 : int rv = 0;
384 :
385 0 : is_del = !mp->is_add;
386 :
387 0 : VALIDATE_SW_IF_INDEX (mp);
388 :
389 0 : rv = lb_nat4_interface_add_del(sw_if_index, is_del);
390 :
391 0 : BAD_SW_IF_INDEX_LABEL;
392 :
393 0 : REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT4_REPLY);
394 : }
395 :
396 0 : static void vl_api_lb_add_del_intf_nat6_t_handler
397 : (vl_api_lb_add_del_intf_nat6_t * mp)
398 : {
399 0 : lb_main_t *lbm = &lb_main;
400 : vl_api_lb_add_del_intf_nat6_reply_t *rmp;
401 0 : u32 sw_if_index = ntohl (mp->sw_if_index);
402 : u8 is_del;
403 0 : int rv = 0;
404 :
405 0 : is_del = !mp->is_add;
406 :
407 0 : VALIDATE_SW_IF_INDEX (mp);
408 :
409 0 : rv = lb_nat6_interface_add_del(sw_if_index, is_del);
410 :
411 0 : BAD_SW_IF_INDEX_LABEL;
412 :
413 0 : REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT6_REPLY);
414 : }
415 :
416 : #include <lb/lb.api.c>
417 559 : static clib_error_t * lb_api_init (vlib_main_t * vm)
418 : {
419 559 : lb_main_t * lbm = &lb_main;
420 :
421 559 : lbm->vlib_main = vm;
422 559 : lbm->vnet_main = vnet_get_main();
423 :
424 : /* Ask for a correctly-sized block of API message decode slots */
425 559 : lbm->msg_id_base = setup_message_id_table ();
426 :
427 559 : return 0;
428 : }
429 :
430 1119 : VLIB_INIT_FUNCTION (lb_api_init);
|