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.
16 #include <kubeproxy/kp.h>
18 #include <vppinfra/byte_order.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
22 #define vl_msg_id(n,h) n,
24 #include <kubeproxy/kp.api.h>
25 /* We'll want to know how many messages IDs we need... */
26 VL_MSG_FIRST_AVAILABLE,
31 /* define message structures */
33 #include <kubeproxy/kp.api.h>
36 /* define generated endian-swappers */
38 #include <kubeproxy/kp.api.h>
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 /* Get the API version number */
44 #define vl_api_version(n,v) static u32 api_version=(v);
45 #include <kubeproxy/kp.api.h>
48 #define vl_msg_name_crc_list
49 #include <kubeproxy/kp.api.h>
50 #undef vl_msg_name_crc_list
53 #define REPLY_MSG_ID_BASE kpm->msg_id_base
54 #include <vlibapi/api_helper_macros.h>
57 setup_message_id_table (kp_main_t * kpm, api_main_t * am)
60 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + kpm->msg_id_base);
61 foreach_vl_msg_name_crc_kp;
65 /* Macro to finish up custom dump fns */
68 vl_print (handle, (char *)s); \
73 vl_api_kp_conf_t_handler
74 (vl_api_kp_conf_t * mp)
76 kp_main_t *kpm = &kp_main;
77 vl_api_kp_conf_reply_t * rmp;
80 rv = kp_conf(mp->sticky_buckets_per_core,
83 REPLY_MACRO (VL_API_KP_CONF_REPLY);
86 static void *vl_api_kp_conf_t_print
87 (vl_api_kp_conf_t *mp, void * handle)
90 s = format (0, "SCRIPT: kp_conf ");
91 s = format (s, "%u ", mp->sticky_buckets_per_core);
92 s = format (s, "%u ", mp->flow_timeout);
98 vl_api_kp_add_del_vip_t_handler
99 (vl_api_kp_add_del_vip_t * mp)
101 kp_main_t *kpm = &kp_main;
102 vl_api_kp_conf_reply_t * rmp;
104 ip46_address_t prefix;
105 u8 prefix_length = mp->prefix_length;
107 if (mp->is_ipv6 == 0)
110 memcpy(&prefix.ip4, mp->ip_prefix, sizeof(prefix.ip4));
111 prefix.pad[0] = prefix.pad[1] = prefix.pad[2] = 0;
115 memcpy(&prefix.ip6, mp->ip_prefix, sizeof(prefix.ip6));
120 if (!(rv = kp_vip_find_index(&prefix, prefix_length, &vip_index)))
121 rv = kp_vip_del(vip_index);
125 if (mp->is_ipv6 == 0) {
126 type = mp->is_nat4?KP_VIP_TYPE_IP4_NAT44:KP_VIP_TYPE_IP4_NAT46;
128 type = mp->is_nat4?KP_VIP_TYPE_IP6_NAT64:KP_VIP_TYPE_IP6_NAT66;
131 rv = kp_vip_add(&prefix, prefix_length, type,
132 ntohl(mp->new_flows_table_length), &vip_index,
133 ntohs(mp->port), ntohs(mp->target_port),
134 ntohs(mp->node_port));
136 REPLY_MACRO (VL_API_KP_CONF_REPLY);
139 static void *vl_api_kp_add_del_vip_t_print
140 (vl_api_kp_add_del_vip_t *mp, void * handle)
143 s = format (0, "SCRIPT: kp_add_del_vip ");
144 s = format (s, "%U ", format_ip46_prefix,
145 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
146 s = format (s, "port %u ", mp->port);
147 s = format (s, "target_port %u ", mp->target_port);
148 s = format (s, "node_port %u ", mp->node_port);
149 s = format (s, "%s ", mp->is_nat4?"nat4":"nat6");
150 s = format (s, "%u ", mp->new_flows_table_length);
151 s = format (s, "%s ", mp->is_del?"del":"add");
156 vl_api_kp_add_del_pod_t_handler
157 (vl_api_kp_add_del_pod_t * mp)
159 kp_main_t *kpm = &kp_main;
160 vl_api_kp_conf_reply_t * rmp;
164 ip46_address_t vip_ip_prefix;
165 u8 vip_prefix_length = mp->vip_prefix_length;
167 if (mp->vip_is_ipv6 == 0)
169 vip_prefix_length += 96;
170 memcpy(&vip_ip_prefix.ip4, mp->vip_ip_prefix,
171 sizeof(vip_ip_prefix.ip4));
172 vip_ip_prefix.pad[0] = vip_ip_prefix.pad[1] = vip_ip_prefix.pad[2] = 0;
176 memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
177 sizeof(vip_ip_prefix.ip6));
180 ip46_address_t pod_address;
182 if (mp->pod_is_ipv6 == 0)
184 memcpy(&pod_address.ip4, mp->pod_address,
185 sizeof(pod_address.ip4));
186 pod_address.pad[0] = pod_address.pad[1] = pod_address.pad[2] = 0;
190 memcpy(&pod_address.ip6, mp->pod_address,
191 sizeof(pod_address.ip6));
194 if ((rv = kp_vip_find_index(&vip_ip_prefix, vip_prefix_length, &vip_index)))
198 rv = kp_vip_del_pods(vip_index, &pod_address, 1);
200 rv = kp_vip_add_pods(vip_index, &pod_address, 1);
203 REPLY_MACRO (VL_API_KP_CONF_REPLY);
206 static void *vl_api_kp_add_del_pod_t_print
207 (vl_api_kp_add_del_pod_t *mp, void * handle)
210 s = format (0, "SCRIPT: kp_add_del_pod ");
211 s = format (s, "%U ", format_ip46_prefix,
212 (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
213 s = format (s, "%U ", format_ip46_address,
214 (ip46_address_t *)mp->pod_address, IP46_TYPE_ANY);
215 s = format (s, "%s ", mp->is_del?"del":"add");
219 /* List of message types that this plugin understands */
220 #define foreach_kp_plugin_api_msg \
221 _(KP_CONF, kp_conf) \
222 _(KP_ADD_DEL_VIP, kp_add_del_vip) \
223 _(KP_ADD_DEL_POD, kp_add_del_pod)
225 static clib_error_t * kp_api_init (vlib_main_t * vm)
227 kp_main_t *kpm = &kp_main;
228 u8 *name = format (0, "kp_%08x%c", api_version, 0);
229 kpm->msg_id_base = vl_msg_api_get_msg_ids
230 ((char *) name, VL_MSG_FIRST_AVAILABLE);
233 vl_msg_api_set_handlers((VL_API_##N + kpm->msg_id_base), \
235 vl_api_##n##_t_handler, \
237 vl_api_##n##_t_endian, \
238 vl_api_##n##_t_print, \
239 sizeof(vl_api_##n##_t), 1);
240 foreach_kp_plugin_api_msg;
243 /* Add our API messages to the global name_crc hash table */
244 setup_message_id_table (kpm, &api_main);
249 VLIB_INIT_FUNCTION (kp_api_init);