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>
22 #include <vlibsocket/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 setup_message_id_table (lb_main_t * lbm, api_main_t * am)
58 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
59 foreach_vl_msg_name_crc_lb;
63 /* Macro to finish up custom dump fns */
66 vl_print (handle, (char *)s); \
71 * A handy macro to set up a message reply.
72 * Assumes that the following variables are available:
73 * mp - pointer to request message
74 * rmp - pointer to reply message type
78 #define REPLY_MACRO(t) \
80 unix_shared_memory_queue_t * q = \
81 vl_api_client_index_to_input_queue (mp->client_index); \
85 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
86 rmp->_vl_msg_id = ntohs((t)+lbm->msg_id_base); \
87 rmp->context = mp->context; \
88 rmp->retval = ntohl(rv); \
90 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
94 vl_api_lb_conf_t_handler
95 (vl_api_lb_conf_t * mp)
97 lb_main_t *lbm = &lb_main;
98 vl_api_lb_conf_reply_t * rmp;
101 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
102 (ip6_address_t *)mp->ip6_src_address,
103 mp->sticky_buckets_per_core,
106 REPLY_MACRO (VL_API_LB_CONF_REPLY);
109 static void *vl_api_lb_conf_t_print
110 (vl_api_lb_conf_t *mp, void * handle)
113 s = format (0, "SCRIPT: lb_conf ");
114 s = format (s, "%U ", format_ip4_address, (ip4_address_t *)&mp->ip4_src_address);
115 s = format (s, "%U ", format_ip6_address, (ip6_address_t *)mp->ip6_src_address);
116 s = format (s, "%u ", mp->sticky_buckets_per_core);
117 s = format (s, "%u ", mp->flow_timeout);
123 vl_api_lb_add_del_vip_t_handler
124 (vl_api_lb_add_del_vip_t * mp)
126 lb_main_t *lbm = &lb_main;
127 vl_api_lb_conf_reply_t * rmp;
129 ip46_address_t prefix;
130 memcpy(&prefix.ip6, mp->ip_prefix, sizeof(prefix.ip6));
134 if (!(rv = lb_vip_find_index(&prefix, mp->prefix_length, &vip_index)))
135 rv = lb_vip_del(vip_index);
139 if (ip46_prefix_is_ip4(&prefix, mp->prefix_length)) {
140 type = mp->is_gre4?LB_VIP_TYPE_IP4_GRE4:LB_VIP_TYPE_IP4_GRE6;
142 type = mp->is_gre4?LB_VIP_TYPE_IP6_GRE4:LB_VIP_TYPE_IP6_GRE6;
145 rv = lb_vip_add(&prefix, mp->prefix_length, type,
146 mp->new_flows_table_length, &vip_index);
148 REPLY_MACRO (VL_API_LB_CONF_REPLY);
151 static void *vl_api_lb_add_del_vip_t_print
152 (vl_api_lb_add_del_vip_t *mp, void * handle)
155 s = format (0, "SCRIPT: lb_add_del_vip ");
156 s = format (s, "%U ", format_ip46_prefix,
157 (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
158 s = format (s, "%s ", mp->is_gre4?"gre4":"gre6");
159 s = format (s, "%u ", mp->new_flows_table_length);
160 s = format (s, "%s ", mp->is_del?"del":"add");
165 vl_api_lb_add_del_as_t_handler
166 (vl_api_lb_add_del_as_t * mp)
168 lb_main_t *lbm = &lb_main;
169 vl_api_lb_conf_reply_t * rmp;
172 if ((rv = lb_vip_find_index((ip46_address_t *)mp->vip_ip_prefix,
173 mp->vip_prefix_length, &vip_index)))
177 rv = lb_vip_del_ass(vip_index, (ip46_address_t *)mp->as_address, 1);
179 rv = lb_vip_add_ass(vip_index, (ip46_address_t *)mp->as_address, 1);
182 REPLY_MACRO (VL_API_LB_CONF_REPLY);
185 static void *vl_api_lb_add_del_as_t_print
186 (vl_api_lb_add_del_as_t *mp, void * handle)
189 s = format (0, "SCRIPT: lb_add_del_as ");
190 s = format (s, "%U ", format_ip46_prefix,
191 (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
192 s = format (s, "%U ", format_ip46_address,
193 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
194 s = format (s, "%s ", mp->is_del?"del":"add");
198 /* List of message types that this plugin understands */
199 #define foreach_lb_plugin_api_msg \
200 _(LB_CONF, lb_conf) \
201 _(LB_ADD_DEL_VIP, lb_add_del_vip) \
202 _(LB_ADD_DEL_AS, lb_add_del_as)
204 static clib_error_t * lb_api_init (vlib_main_t * vm)
206 lb_main_t *lbm = &lb_main;
207 u8 *name = format (0, "lb_%08x%c", api_version, 0);
208 lbm->msg_id_base = vl_msg_api_get_msg_ids
209 ((char *) name, VL_MSG_FIRST_AVAILABLE);
212 vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base), \
214 vl_api_##n##_t_handler, \
216 vl_api_##n##_t_endian, \
217 vl_api_##n##_t_print, \
218 sizeof(vl_api_##n##_t), 1);
219 foreach_lb_plugin_api_msg;
222 /* Add our API messages to the global name_crc hash table */
223 setup_message_id_table (lbm, &api_main);
228 VLIB_INIT_FUNCTION (lb_api_init);