Add buffer pointer-to-index and index-to-pointer array functions
[vpp.git] / src / plugins / kubeproxy / kp_api.c
1 /*
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:
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 "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.
14  */
15
16 #include <kubeproxy/kp.h>
17
18 #include <vppinfra/byte_order.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21
22 #define vl_msg_id(n,h) n,
23 typedef enum {
24 #include <kubeproxy/kp.api.h>
25     /* We'll want to know how many messages IDs we need... */
26     VL_MSG_FIRST_AVAILABLE,
27 } vl_msg_id_t;
28 #undef vl_msg_id
29
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <kubeproxy/kp.api.h>
34 #undef vl_typedefs
35
36 /* define generated endian-swappers */
37 #define vl_endianfun
38 #include <kubeproxy/kp.api.h>
39 #undef vl_endianfun
40
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42
43 /* Get the API version number */
44 #define vl_api_version(n,v) static u32 api_version=(v);
45 #include <kubeproxy/kp.api.h>
46 #undef vl_api_version
47
48 #define vl_msg_name_crc_list
49 #include <kubeproxy/kp.api.h>
50 #undef vl_msg_name_crc_list
51
52
53 #define REPLY_MSG_ID_BASE kpm->msg_id_base
54 #include <vlibapi/api_helper_macros.h>
55
56 static void
57 setup_message_id_table (kp_main_t * kpm, api_main_t * am)
58 {
59 #define _(id,n,crc) \
60   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + kpm->msg_id_base);
61   foreach_vl_msg_name_crc_kp;
62 #undef _
63 }
64
65 /* Macro to finish up custom dump fns */
66 #define FINISH                                  \
67     vec_add1 (s, 0);                            \
68     vl_print (handle, (char *)s);               \
69     vec_free (s);                               \
70     return handle;
71
72 static void
73 vl_api_kp_conf_t_handler
74 (vl_api_kp_conf_t * mp)
75 {
76   kp_main_t *kpm = &kp_main;
77   vl_api_kp_conf_reply_t * rmp;
78   int rv = 0;
79
80   rv = kp_conf(mp->sticky_buckets_per_core,
81                mp->flow_timeout);
82
83  REPLY_MACRO (VL_API_KP_CONF_REPLY);
84 }
85
86 static void *vl_api_kp_conf_t_print
87 (vl_api_kp_conf_t *mp, void * handle)
88 {
89   u8 * s;
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);
93   FINISH;
94 }
95
96
97 static void
98 vl_api_kp_add_del_vip_t_handler
99 (vl_api_kp_add_del_vip_t * mp)
100 {
101   kp_main_t *kpm = &kp_main;
102   vl_api_kp_conf_reply_t * rmp;
103   int rv = 0;
104   ip46_address_t prefix;
105   u8 prefix_length = mp->prefix_length;
106
107   if (mp->is_ipv6 == 0)
108     {
109       prefix_length += 96;
110       memcpy(&prefix.ip4, mp->ip_prefix, sizeof(prefix.ip4));
111       prefix.pad[0] = prefix.pad[1] = prefix.pad[2] = 0;
112     }
113   else
114     {
115       memcpy(&prefix.ip6, mp->ip_prefix, sizeof(prefix.ip6));
116     }
117
118   if (mp->is_del) {
119     u32 vip_index;
120     if (!(rv = kp_vip_find_index(&prefix, prefix_length, &vip_index)))
121       rv = kp_vip_del(vip_index);
122   } else {
123     u32 vip_index;
124     kp_vip_type_t type;
125     if (mp->is_ipv6 == 0) {
126       type = mp->is_nat4?KP_VIP_TYPE_IP4_NAT44:KP_VIP_TYPE_IP4_NAT46;
127     } else {
128       type = mp->is_nat4?KP_VIP_TYPE_IP6_NAT64:KP_VIP_TYPE_IP6_NAT66;
129     }
130
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));
135   }
136  REPLY_MACRO (VL_API_KP_CONF_REPLY);
137 }
138
139 static void *vl_api_kp_add_del_vip_t_print
140 (vl_api_kp_add_del_vip_t *mp, void * handle)
141 {
142   u8 * s;
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");
152   FINISH;
153 }
154
155 static void
156 vl_api_kp_add_del_pod_t_handler
157 (vl_api_kp_add_del_pod_t * mp)
158 {
159   kp_main_t *kpm = &kp_main;
160   vl_api_kp_conf_reply_t * rmp;
161   int rv = 0;
162   u32 vip_index;
163
164   ip46_address_t vip_ip_prefix;
165   u8 vip_prefix_length = mp->vip_prefix_length;
166
167   if (mp->vip_is_ipv6 == 0)
168     {
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;
173     }
174   else
175     {
176       memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
177              sizeof(vip_ip_prefix.ip6));
178     }
179
180   ip46_address_t pod_address;
181
182   if (mp->pod_is_ipv6 == 0)
183     {
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;
187     }
188   else
189     {
190       memcpy(&pod_address.ip6, mp->pod_address,
191              sizeof(pod_address.ip6));
192     }
193
194   if ((rv = kp_vip_find_index(&vip_ip_prefix, vip_prefix_length, &vip_index)))
195     goto done;
196
197   if (mp->is_del)
198     rv = kp_vip_del_pods(vip_index, &pod_address, 1);
199   else
200     rv = kp_vip_add_pods(vip_index, &pod_address, 1);
201
202 done:
203  REPLY_MACRO (VL_API_KP_CONF_REPLY);
204 }
205
206 static void *vl_api_kp_add_del_pod_t_print
207 (vl_api_kp_add_del_pod_t *mp, void * handle)
208 {
209   u8 * s;
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");
216   FINISH;
217 }
218
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)
224
225 static clib_error_t * kp_api_init (vlib_main_t * vm)
226 {
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);
231
232 #define _(N,n)                                                  \
233     vl_msg_api_set_handlers((VL_API_##N + kpm->msg_id_base),     \
234                            #n,                  \
235                            vl_api_##n##_t_handler,              \
236                            vl_noop_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;
241 #undef _
242
243   /* Add our API messages to the global name_crc hash table */
244   setup_message_id_table (kpm, &api_main);
245
246   return 0;
247 }
248
249 VLIB_INIT_FUNCTION (kp_api_init);