FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
kp_api.c
Go to the documentation of this file.
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... */
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
55 
56 static void
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
75 {
76  kp_main_t *kpm = &kp_main;
77  vl_api_kp_conf_reply_t * rmp;
78  int rv = 0;
79 
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
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) {
127  } else {
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 
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
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 
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 
226 {
227  kp_main_t *kpm = &kp_main;
228  u8 *name = format (0, "kp_%08x%c", api_version, 0);
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);
241 #undef _
242 
243  /* Add our API messages to the global name_crc hash table */
245 
246  return 0;
247 }
248 
Configure Kube-proxy global parameters.
Definition: kp.api:26
u8 * format_ip46_prefix(u8 *s, va_list *args)
Definition: kp.c:71
vl_msg_id_t
Definition: kp_api.c:23
static void * vl_api_kp_add_del_pod_t_print(vl_api_kp_add_del_pod_t *mp, void *handle)
Definition: kp_api.c:207
u32 sticky_buckets_per_core
Definition: kp.api:30
format_function_t format_ip46_address
Definition: format.h:61
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
static clib_error_t * kp_api_init(vlib_main_t *vm)
Definition: kp_api.c:225
static void * vl_api_kp_conf_t_print(vl_api_kp_conf_t *mp, void *handle)
Definition: kp_api.c:87
kp_vip_type_t
kube-proxy supports IPv4 and IPv6 traffic and NAT4 and NAT6.
Definition: kp.h:171
int kp_vip_add(ip46_address_t *prefix, u8 plen, kp_vip_type_t type, u32 new_length, u32 *vip_index, u16 port, u16 target_port, u16 node_port)
Definition: kp.c:725
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
int kp_vip_del(u32 vip_index)
Definition: kp.c:810
static void * vl_api_kp_add_del_vip_t_print(vl_api_kp_add_del_vip_t *mp, void *handle)
Definition: kp_api.c:140
Definition: kp.h:352
u32 new_flows_table_length
Definition: kp.api:58
#define foreach_kp_plugin_api_msg
Definition: kp_api.c:220
int kp_vip_del_pods(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: kp.c:670
u32 flow_timeout
Definition: kp.api:31
#define REPLY_MACRO(t)
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:199
u8 vip_ip_prefix[16]
Definition: kp.api:75
vlib_main_t * vm
Definition: buffer.c:294
static void vl_api_kp_add_del_vip_t_handler(vl_api_kp_add_del_vip_t *mp)
Definition: kp_api.c:99
static void vl_api_kp_add_del_pod_t_handler(vl_api_kp_add_del_pod_t *mp)
Definition: kp_api.c:157
u8 pod_address[16]
Definition: kp.api:78
Add a pod for a given VIP.
Definition: kp.api:72
static void setup_message_id_table(kp_main_t *kpm, api_main_t *am)
Definition: kp_api.c:57
unsigned int u32
Definition: types.h:88
static void vl_api_kp_conf_t_handler(vl_api_kp_conf_t *mp)
Definition: kp_api.c:74
u16 msg_id_base
API dynamically registered base ID.
Definition: kp.h:422
kp_main_t kp_main
Definition: kp.c:28
unsigned char u8
Definition: types.h:56
Add a virtual address (or prefix)
Definition: kp.api:48
int kp_vip_add_pods(u32 vip_index, ip46_address_t *addresses, u32 n)
Definition: kp.c:481
#define FINISH
Definition: kp_api.c:66
int kp_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
Definition: kp.c:454
api_main_t api_main
Definition: api_shared.c:35
int kp_conf(u32 per_cpu_sticky_buckets, u32 flow_timeout)
Fix global kube-proxy parameters.
Definition: kp.c:421
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:872