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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 #include <vlibsocket/api.h>
20 #include <vppinfra/error.h>
22 #include <vlibapi/vat_helper_macros.h>
24 //TODO: Move that to vat/plugin_api.c
25 //////////////////////////
26 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
28 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
29 ip46_type_t type = va_arg (*args, ip46_type_t);
30 if ((type != IP46_TYPE_IP6) &&
31 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
32 ip46_address_mask_ip4(ip46);
34 } else if ((type != IP46_TYPE_IP4) &&
35 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
40 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
42 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
43 u8 *len = va_arg (*args, u8 *);
44 ip46_type_t type = va_arg (*args, ip46_type_t);
47 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
51 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
52 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
61 /////////////////////////
63 #define vl_msg_id(n,h) n,
65 #include <lb/lb.api.h>
66 /* We'll want to know how many messages IDs we need... */
67 VL_MSG_FIRST_AVAILABLE,
71 /* define message structures */
73 #include <lb/lb.api.h>
76 /* declare message handlers for each api */
78 #define vl_endianfun /* define message structures */
79 #include <lb/lb.api.h>
82 /* instantiate all the print functions we know about */
83 #define vl_print(handle, ...)
85 #include <lb/lb.api.h>
88 /* Get the API version number. */
89 #define vl_api_version(n,v) static u32 api_version=(v);
90 #include <lb/lb.api.h>
94 /* API message ID base */
99 lb_test_main_t lb_test_main;
101 #define foreach_standard_reply_retval_handler \
103 _(lb_add_del_vip_reply) \
104 _(lb_add_del_as_reply)
107 static void vl_api_##n##_t_handler \
108 (vl_api_##n##_t * mp) \
110 vat_main_t * vam = lb_test_main.vat_main; \
111 i32 retval = ntohl(mp->retval); \
112 if (vam->async_mode) { \
113 vam->async_errors += (retval < 0); \
115 vam->retval = retval; \
116 vam->result_ready = 1; \
119 foreach_standard_reply_retval_handler;
123 * Table of message reply handlers, must include boilerplate handlers
126 #define foreach_vpe_api_reply_msg \
127 _(LB_CONF_REPLY, lb_conf_reply) \
128 _(LB_ADD_DEL_VIP_REPLY, lb_add_del_vip_reply) \
129 _(LB_ADD_DEL_AS_REPLY, lb_add_del_as_reply)
131 static int api_lb_conf (vat_main_t * vam)
133 unformat_input_t *i = vam->input;
135 vl_api_lb_conf_t mps, *mp;
137 if (!unformat(i, "%U %U %u %u",
138 unformat_ip4_address, &mps.ip4_src_address,
139 unformat_ip6_address, mps.ip6_src_address,
140 &mps.sticky_buckets_per_core,
141 &mps.flow_timeout)) {
142 errmsg ("invalid arguments\n");
146 M(LB_CONF, lb_conf); S; W;
152 static int api_lb_add_del_vip (vat_main_t * vam)
154 unformat_input_t * i = vam->input;
156 vl_api_lb_add_del_vip_t mps, *mp;
160 if (!unformat(i, "%U",
161 unformat_ip46_prefix, mps.ip_prefix, &mps.prefix_length, IP46_TYPE_ANY)) {
162 errmsg ("invalid prefix\n");
166 if (unformat(i, "gre4")) {
168 } else if (unformat(i, "gre6")) {
171 errmsg ("no encap\n");
175 if (!unformat(i, "%d", &mps.new_flows_table_length)) {
176 errmsg ("no table lentgh\n");
180 if (unformat(i, "del")) {
184 M(LB_ADD_DEL_VIP, lb_add_del_vip); S; W;
189 static int api_lb_add_del_as (vat_main_t * vam)
191 unformat_input_t * i = vam->input;
193 vl_api_lb_add_del_as_t mps, *mp;
196 if (!unformat(i, "%U %U",
197 unformat_ip46_prefix, mps.vip_ip_prefix, &mps.vip_prefix_length, IP46_TYPE_ANY,
198 unformat_ip46_address, mps.as_address)) {
199 errmsg ("invalid prefix or address\n");
203 if (unformat(i, "del")) {
207 M(LB_ADD_DEL_AS, lb_add_del_as); S; W;
213 * List of messages that the api test plugin sends,
214 * and that the data plane plugin processes
216 #define foreach_vpe_api_msg \
217 _(lb_conf, "<ip4-src-addr> <ip6-src-address> <sticky_buckets_per_core> <flow_timeout>") \
218 _(lb_add_del_vip, "<ip-prefix> [gre4|gre6] <new_table_len> [del]") \
219 _(lb_add_del_as, "<vip-ip-prefix> <address> [del]")
222 lb_vat_api_hookup (vat_main_t *vam)
224 lb_test_main_t * lbtm = &lb_test_main;
225 /* Hook up handlers for replies from the data plane plug-in */
227 vl_msg_api_set_handlers((VL_API_##N + lbtm->msg_id_base), \
229 vl_api_##n##_t_handler, \
231 vl_api_##n##_t_endian, \
232 vl_api_##n##_t_print, \
233 sizeof(vl_api_##n##_t), 1);
234 foreach_vpe_api_reply_msg;
237 /* API messages we can send */
238 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
243 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
248 clib_error_t * vat_plugin_register (vat_main_t *vam)
250 lb_test_main_t * lbtm = &lb_test_main;
254 lbtm->vat_main = vam;
256 /* Ask the vpp engine for the first assigned message-id */
257 name = format (0, "lb_%08x%c", api_version, 0);
258 lbtm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
260 if (lbtm->msg_id_base != (u16) ~0)
261 lb_vat_api_hookup (vam);