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.
18 #include <vppinfra/byte_order.h>
19 #include <vppinfra/string.h>
20 #include <vlibapi/api.h>
21 #include <vlibapi/api.h>
22 #include <vlibmemory/api.h>
25 #define vl_msg_id(n,h) n,
27 #include <lb/lb.api.h>
28 /* We'll want to know how many messages IDs we need... */
29 VL_MSG_FIRST_AVAILABLE,
34 /* define message structures */
36 #include <lb/lb.api.h>
39 /* define generated endian-swappers */
41 #include <lb/lb.api.h>
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <lb/lb.api.h>
51 #define vl_msg_name_crc_list
52 #include <lb/lb.api.h>
53 #undef vl_msg_name_crc_list
56 #define REPLY_MSG_ID_BASE lbm->msg_id_base
57 #include <vlibapi/api_helper_macros.h>
60 setup_message_id_table (lb_main_t * lbm, api_main_t * am)
63 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
64 foreach_vl_msg_name_crc_lb;
68 /* Macro to finish up custom dump fns */
71 vl_print (handle, (char *)s); \
76 vl_api_lb_conf_t_handler
77 (vl_api_lb_conf_t * mp)
79 lb_main_t *lbm = &lb_main;
80 vl_api_lb_conf_reply_t * rmp;
83 if (mp->sticky_buckets_per_core == ~0) {
84 mp->sticky_buckets_per_core = lbm->per_cpu_sticky_buckets;
86 if (mp->flow_timeout == ~0) {
87 mp->flow_timeout = lbm->flow_timeout;
90 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
91 (ip6_address_t *)&mp->ip6_src_address,
92 mp->sticky_buckets_per_core,
95 REPLY_MACRO (VL_API_LB_CONF_REPLY);
98 static void *vl_api_lb_conf_t_print
99 (vl_api_lb_conf_t *mp, void * handle)
102 s = format (0, "SCRIPT: lb_conf ");
103 s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
104 s = format (s, "%U ", format_ip6_address, (ip6_address_t *)&mp->ip6_src_address);
105 s = format (s, "%u ", mp->sticky_buckets_per_core);
106 s = format (s, "%u ", mp->flow_timeout);
112 vl_api_lb_add_del_vip_t_handler
113 (vl_api_lb_add_del_vip_t * mp)
115 lb_main_t *lbm = &lb_main;
116 vl_api_lb_conf_reply_t * rmp;
118 lb_vip_add_args_t args;
120 /* if port == 0, it means all-port VIP */
126 memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
130 if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length,
131 mp->protocol, ntohs(mp->port), &vip_index)))
132 rv = lb_vip_del(vip_index);
135 lb_vip_type_t type = 0;
137 if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
138 if (mp->encap == LB_ENCAP_TYPE_GRE4)
139 type = LB_VIP_TYPE_IP4_GRE4;
140 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
141 type = LB_VIP_TYPE_IP4_GRE6;
142 else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
143 type = LB_VIP_TYPE_IP4_L3DSR;
144 else if (mp->encap == LB_ENCAP_TYPE_NAT4)
145 type = LB_VIP_TYPE_IP4_NAT4;
147 if (mp->encap == LB_ENCAP_TYPE_GRE4)
148 type = LB_VIP_TYPE_IP6_GRE4;
149 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
150 type = LB_VIP_TYPE_IP6_GRE6;
151 else if (mp->encap == LB_ENCAP_TYPE_NAT6)
152 type = LB_VIP_TYPE_IP6_NAT6;
155 args.plen = mp->prefix_length;
156 args.protocol = mp->protocol;
157 args.port = ntohs(mp->port);
159 args.new_length = ntohl(mp->new_flows_table_length);
161 if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
162 args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
164 else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
165 ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
166 args.encap_args.srv_type = mp->type;
167 args.encap_args.target_port = ntohs(mp->target_port);
170 rv = lb_vip_add(args, &vip_index);
172 REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
175 static void *vl_api_lb_add_del_vip_t_print
176 (vl_api_lb_add_del_vip_t *mp, void * handle)
179 s = format (0, "SCRIPT: lb_add_del_vip ");
180 s = format (s, "%U ", format_ip46_prefix,
181 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
183 s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
184 : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
185 : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
186 : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
189 if (mp->encap==LB_ENCAP_TYPE_L3DSR)
191 s = format (s, "dscp %u ", mp->dscp);
194 if ((mp->encap==LB_ENCAP_TYPE_NAT4)
195 || (mp->encap==LB_ENCAP_TYPE_NAT6))
197 s = format (s, "type %u ", mp->type);
198 s = format (s, "port %u ", mp->port);
199 s = format (s, "target_port %u ", mp->target_port);
202 s = format (s, "%u ", mp->new_flows_table_length);
203 s = format (s, "%s ", mp->is_del?"del":"add");
208 vl_api_lb_add_del_as_t_handler
209 (vl_api_lb_add_del_as_t * mp)
211 lb_main_t *lbm = &lb_main;
212 vl_api_lb_conf_reply_t * rmp;
215 ip46_address_t vip_ip_prefix;
217 memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
218 sizeof(vip_ip_prefix.ip6));
220 ip46_address_t as_address;
222 memcpy(&as_address.ip6, mp->as_address,
223 sizeof(as_address.ip6));
225 if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length,
226 mp->protocol, ntohs(mp->port), &vip_index)))
230 rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
232 rv = lb_vip_add_ass(vip_index, &as_address, 1);
235 REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
238 static void *vl_api_lb_add_del_as_t_print
239 (vl_api_lb_add_del_as_t *mp, void * handle)
242 s = format (0, "SCRIPT: lb_add_del_as ");
243 s = format (s, "%U ", format_ip46_prefix,
244 (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
245 s = format (s, "%U ", format_ip46_address,
246 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
247 s = format (s, "%s ", mp->is_del?"del":"add");
252 vl_api_lb_flush_vip_t_handler
253 (vl_api_lb_flush_vip_t * mp)
255 lb_main_t *lbm = &lb_main;
257 ip46_address_t vip_prefix;
260 vl_api_lb_flush_vip_reply_t * rmp;
267 memcpy (&(vip_prefix.ip6), mp->ip_prefix, sizeof(vip_prefix.ip6));
269 vip_plen = mp->prefix_length;
271 rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
272 ntohs(mp->port), &vip_index);
274 rv = lb_flush_vip_as(vip_index, ~0);
276 REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
279 static void *vl_api_lb_flush_vip_t_print
280 (vl_api_lb_flush_vip_t *mp, void * handle)
283 s = format (0, "SCRIPT: lb_add_del_vip ");
284 s = format (s, "%U ", format_ip46_prefix,
285 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
287 s = format (s, "protocol %u ", mp->protocol);
288 s = format (s, "port %u ", mp->port);
293 /* List of message types that this plugin understands */
294 #define foreach_lb_plugin_api_msg \
295 _(LB_CONF, lb_conf) \
296 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
297 _(LB_ADD_DEL_AS, lb_add_del_as) \
298 _(LB_FLUSH_VIP, lb_flush_vip)
300 static clib_error_t * lb_api_init (vlib_main_t * vm)
302 lb_main_t *lbm = &lb_main;
303 u8 *name = format (0, "lb_%08x%c", api_version, 0);
304 lbm->msg_id_base = vl_msg_api_get_msg_ids
305 ((char *) name, VL_MSG_FIRST_AVAILABLE);
308 vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
310 vl_api_##n##_t_handler, \
312 vl_api_##n##_t_endian, \
313 vl_api_##n##_t_print, \
314 sizeof(vl_api_##n##_t), 1);
315 foreach_lb_plugin_api_msg;
318 /* Add our API messages to the global name_crc hash table */
319 setup_message_id_table (lbm, &api_main);
324 VLIB_INIT_FUNCTION (lb_api_init);