2 * Copyright (c) 2016 Intel 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 "POD IS" BPODIS,
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 <vppinfra/error.h>
20 #include <kubeproxy/kp.h>
22 #define __plugin_msg_base kp_test_main.msg_id_base
23 #include <vlibapi/vat_helper_macros.h>
25 //TODO: Move that to vat/plugin_api.c
26 //////////////////////////
27 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
29 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
30 ip46_type_t type = va_arg (*args, ip46_type_t);
31 if ((type != IP46_TYPE_IP6) &&
32 unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
33 ip46_address_mask_ip4(ip46);
35 } else if ((type != IP46_TYPE_IP4) &&
36 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
41 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
43 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
44 u8 *len = va_arg (*args, u8 *);
45 ip46_type_t type = va_arg (*args, ip46_type_t);
48 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
52 ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
53 } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
62 /////////////////////////
64 #define vl_msg_id(n,h) n,
66 #include <kubeproxy/kp.api.h>
67 /* We'll want to know how many messages IDs we need... */
68 VL_MSG_FIRST_AVAILABLE,
72 /* define message structures */
74 #include <kubeproxy/kp.api.h>
77 /* declare message handlers for each api */
79 #define vl_endianfun /* define message structures */
80 #include <kubeproxy/kp.api.h>
83 /* instantiate all the print functions we know about */
84 #define vl_print(handle, ...)
86 #include <kubeproxy/kp.api.h>
89 /* Get the API version number. */
90 #define vl_api_version(n,v) static u32 api_version=(v);
91 #include <kubeproxy/kp.api.h>
95 /* API message ID base */
100 kp_test_main_t kp_test_main;
102 #define foreach_standard_reply_retval_handler \
104 _(kp_add_del_vip_reply) \
105 _(kp_add_del_pod_reply)
108 static void vl_api_##n##_t_handler \
109 (vl_api_##n##_t * mp) \
111 vat_main_t * vam = kp_test_main.vat_main; \
112 i32 retval = ntohl(mp->retval); \
113 if (vam->async_mode) { \
114 vam->async_errors += (retval < 0); \
116 vam->retval = retval; \
117 vam->result_ready = 1; \
120 foreach_standard_reply_retval_handler;
124 * Table of message reply handlers, must include boilerplate handlers
127 #define foreach_vpe_api_reply_msg \
128 _(KP_CONF_REPLY, kp_conf_reply) \
129 _(KP_ADD_DEL_VIP_REPLY, kp_add_del_vip_reply) \
130 _(KP_ADD_DEL_POD_REPLY, kp_add_del_pod_reply)
132 static int api_kp_conf (vat_main_t * vam)
134 unformat_input_t *i = vam->input;
135 vl_api_kp_conf_t mps, *mp;
138 if (!unformat(i, "%u %u",
139 &mps.sticky_buckets_per_core,
140 &mps.flow_timeout)) {
141 errmsg ("invalid arguments\n");
151 static int api_kp_add_del_vip (vat_main_t * vam)
153 unformat_input_t * i = vam->input;
154 vl_api_kp_add_del_vip_t mps, *mp;
159 if (!unformat(i, "%U",
160 unformat_ip46_prefix, mps.ip_prefix, &mps.prefix_length, IP46_TYPE_ANY)) {
161 errmsg ("invalid prefix\n");
165 if (unformat(i, "nat4")) {
167 } else if (unformat(i, "nat6")) {
174 if (!unformat(i, "%d", &mps.new_flows_table_length)) {
175 errmsg ("no table lentgh\n");
179 if (unformat(i, "del")) {
183 M(KP_ADD_DEL_VIP, mp);
189 static int api_kp_add_del_pod (vat_main_t * vam)
191 unformat_input_t * i = vam->input;
192 vl_api_kp_add_del_pod_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.pod_address)) {
199 errmsg ("invalid prefix or address\n");
203 if (unformat(i, "del")) {
207 M(KP_ADD_DEL_POD, mp);
214 * List of messages that the api test plugin sends,
215 * and that the data plane plugin processes
217 #define foreach_vpe_api_msg \
218 _(kp_conf, "<sticky_buckets_per_core> <flow_timeout>") \
219 _(kp_add_del_vip, "<ip-prefix> <port> <target_port> <node_port> " \
220 "[nat4|nat6] <new_table_len> [del]") \
221 _(kp_add_del_pod, "<vip-ip-prefix> <address> [del]")
224 kp_vat_api_hookup (vat_main_t *vam)
226 kp_test_main_t * kptm = &kp_test_main;
227 /* Hook up handlers for replies from the data plane plug-in */
229 vl_msg_api_set_handlers((VL_API_##N + kptm->msg_id_base), \
231 vl_api_##n##_t_handler, \
233 vl_api_##n##_t_endian, \
234 vl_api_##n##_t_print, \
235 sizeof(vl_api_##n##_t), 1);
236 foreach_vpe_api_reply_msg;
239 /* API messages we can send */
240 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
245 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
250 clib_error_t * vat_plugin_register (vat_main_t *vam)
252 kp_test_main_t * kptm = &kp_test_main;
256 kptm->vat_main = vam;
258 /* Ask the vpp engine for the first assigned message-id */
259 name = format (0, "kp_%08x%c", api_version, 0);
260 kptm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
262 if (kptm->msg_id_base != (u16) ~0)
263 kp_vat_api_hookup (vam);