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 <vlibapi/api.h>
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
24 #define vl_msg_id(n,h) n,
26 #include <lb/lb.api.h>
27 /* We'll want to know how many messages IDs we need... */
28 VL_MSG_FIRST_AVAILABLE,
33 /* define message structures */
35 #include <lb/lb.api.h>
38 /* define generated endian-swappers */
40 #include <lb/lb.api.h>
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <lb/lb.api.h>
50 #define vl_msg_name_crc_list
51 #include <lb/lb.api.h>
52 #undef vl_msg_name_crc_list
55 #define REPLY_MSG_ID_BASE lbm->msg_id_base
56 #include <vlibapi/api_helper_macros.h>
59 setup_message_id_table (lb_main_t * lbm, api_main_t * am)
62 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
63 foreach_vl_msg_name_crc_lb;
67 /* Macro to finish up custom dump fns */
70 vl_print (handle, (char *)s); \
75 vl_api_lb_conf_t_handler
76 (vl_api_lb_conf_t * mp)
78 lb_main_t *lbm = &lb_main;
79 vl_api_lb_conf_reply_t * rmp;
82 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
83 (ip6_address_t *)mp->ip6_src_address,
84 mp->sticky_buckets_per_core,
87 REPLY_MACRO (VL_API_LB_CONF_REPLY);
90 static void *vl_api_lb_conf_t_print
91 (vl_api_lb_conf_t *mp, void * handle)
94 s = format (0, "SCRIPT: lb_conf ");
95 s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
96 s = format (s, "%U ", format_ip6_address, (ip6_address_t *)mp->ip6_src_address);
97 s = format (s, "%u ", mp->sticky_buckets_per_core);
98 s = format (s, "%u ", mp->flow_timeout);
104 vl_api_lb_add_del_vip_t_handler
105 (vl_api_lb_add_del_vip_t * mp)
107 lb_main_t *lbm = &lb_main;
108 vl_api_lb_conf_reply_t * rmp;
110 lb_vip_add_args_t args;
112 memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
116 if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length, &vip_index)))
117 rv = lb_vip_del(vip_index);
120 lb_vip_type_t type = 0;
122 if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
123 if (mp->encap == LB_ENCAP_TYPE_GRE4)
124 type = LB_VIP_TYPE_IP4_GRE4;
125 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
126 type = LB_VIP_TYPE_IP4_GRE6;
127 else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
128 type = LB_VIP_TYPE_IP4_L3DSR;
129 else if (mp->encap == LB_ENCAP_TYPE_NAT4)
130 type = LB_VIP_TYPE_IP4_NAT4;
132 if (mp->encap == LB_ENCAP_TYPE_GRE4)
133 type = LB_VIP_TYPE_IP6_GRE4;
134 else if (mp->encap == LB_ENCAP_TYPE_GRE6)
135 type = LB_VIP_TYPE_IP6_GRE6;
136 else if (mp->encap == LB_ENCAP_TYPE_NAT6)
137 type = LB_VIP_TYPE_IP6_NAT6;
140 args.plen = mp->prefix_length;
142 args.new_length = ntohl(mp->new_flows_table_length);
144 if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
145 args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
147 else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
148 ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
149 args.encap_args.srv_type = mp->type;
150 args.encap_args.port = ntohs(mp->port);
151 args.encap_args.target_port = ntohs(mp->target_port);
152 args.encap_args.node_port = ntohs(mp->node_port);
155 rv = lb_vip_add(args, &vip_index);
157 REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
160 static void *vl_api_lb_add_del_vip_t_print
161 (vl_api_lb_add_del_vip_t *mp, void * handle)
164 s = format (0, "SCRIPT: lb_add_del_vip ");
165 s = format (s, "%U ", format_ip46_prefix,
166 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
168 s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
169 : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
170 : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
171 : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
174 if (mp->encap==LB_ENCAP_TYPE_L3DSR)
176 s = format (s, "dscp %u ", mp->dscp);
179 if ((mp->encap==LB_ENCAP_TYPE_NAT4)
180 || (mp->encap==LB_ENCAP_TYPE_NAT6))
182 s = format (s, "type %u ", mp->type);
183 s = format (s, "port %u ", mp->port);
184 s = format (s, "target_port %u ", mp->target_port);
185 s = format (s, "node_port %u ", mp->node_port);
188 s = format (s, "%u ", mp->new_flows_table_length);
189 s = format (s, "%s ", mp->is_del?"del":"add");
194 vl_api_lb_add_del_as_t_handler
195 (vl_api_lb_add_del_as_t * mp)
197 lb_main_t *lbm = &lb_main;
198 vl_api_lb_conf_reply_t * rmp;
201 ip46_address_t vip_ip_prefix;
203 memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
204 sizeof(vip_ip_prefix.ip6));
206 ip46_address_t as_address;
208 memcpy(&as_address.ip6, mp->as_address,
209 sizeof(as_address.ip6));
211 if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length, &vip_index)))
215 rv = lb_vip_del_ass(vip_index, &as_address, 1);
217 rv = lb_vip_add_ass(vip_index, &as_address, 1);
220 REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
223 static void *vl_api_lb_add_del_as_t_print
224 (vl_api_lb_add_del_as_t *mp, void * handle)
227 s = format (0, "SCRIPT: lb_add_del_as ");
228 s = format (s, "%U ", format_ip46_prefix,
229 (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
230 s = format (s, "%U ", format_ip46_address,
231 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
232 s = format (s, "%s ", mp->is_del?"del":"add");
236 /* List of message types that this plugin understands */
237 #define foreach_lb_plugin_api_msg \
238 _(LB_CONF, lb_conf) \
239 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
240 _(LB_ADD_DEL_AS, lb_add_del_as)
242 static clib_error_t * lb_api_init (vlib_main_t * vm)
244 lb_main_t *lbm = &lb_main;
245 u8 *name = format (0, "lb_%08x%c", api_version, 0);
246 lbm->msg_id_base = vl_msg_api_get_msg_ids
247 ((char *) name, VL_MSG_FIRST_AVAILABLE);
250 vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
252 vl_api_##n##_t_handler, \
254 vl_api_##n##_t_endian, \
255 vl_api_##n##_t_print, \
256 sizeof(vl_api_##n##_t), 1);
257 foreach_lb_plugin_api_msg;
260 /* Add our API messages to the global name_crc hash table */
261 setup_message_id_table (lbm, &api_main);
266 VLIB_INIT_FUNCTION (lb_api_init);