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.
16 #include <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
20 #include <vppinfra/byte_order.h>
21 #include <vppinfra/string.h>
22 #include <vpp/api/types.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vpp/app/version.h>
26 #include <vnet/format_fns.h>
27 #include <vnet/ip/ip_types_api.h>
29 /* define message IDs */
30 #include <lb/lb.api_enum.h>
31 #include <lb/lb.api_types.h>
33 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35 #define REPLY_MSG_ID_BASE lbm->msg_id_base
36 #include <vlibapi/api_helper_macros.h>
38 /* Macro to finish up custom dump fns */
41 vl_print (handle, (char *)s); \
46 vl_api_lb_conf_t_handler
47 (vl_api_lb_conf_t * mp)
49 lb_main_t *lbm = &lb_main;
50 vl_api_lb_conf_reply_t * rmp;
51 u32 sticky_buckets_per_core, flow_timeout;
54 sticky_buckets_per_core = mp->sticky_buckets_per_core == ~0
55 ? lbm->per_cpu_sticky_buckets
56 : ntohl(mp->sticky_buckets_per_core);
57 flow_timeout = mp->flow_timeout == ~0
59 : ntohl(mp->flow_timeout);
61 rv = lb_conf((ip4_address_t *)&mp->ip4_src_address,
62 (ip6_address_t *)&mp->ip6_src_address,
63 sticky_buckets_per_core, flow_timeout);
65 REPLY_MACRO (VL_API_LB_CONF_REPLY);
69 vl_api_lb_add_del_vip_t_handler
70 (vl_api_lb_add_del_vip_t * mp)
72 lb_main_t *lbm = &lb_main;
73 vl_api_lb_conf_reply_t * rmp;
75 lb_vip_add_args_t args;
77 /* if port == 0, it means all-port VIP */
83 ip_address_decode (&mp->pfx.address, &(args.prefix));
87 if (!(rv = lb_vip_find_index(&(args.prefix), mp->pfx.len,
88 mp->protocol, ntohs(mp->port), &vip_index)))
89 rv = lb_vip_del(vip_index);
92 lb_vip_type_t type = 0;
94 if (ip46_prefix_is_ip4(&(args.prefix), mp->pfx.len)) {
95 if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
96 type = LB_VIP_TYPE_IP4_GRE4;
97 else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
98 type = LB_VIP_TYPE_IP4_GRE6;
99 else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
100 type = LB_VIP_TYPE_IP4_L3DSR;
101 else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
102 type = LB_VIP_TYPE_IP4_NAT4;
104 if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
105 type = LB_VIP_TYPE_IP6_GRE4;
106 else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
107 type = LB_VIP_TYPE_IP6_GRE6;
108 else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
109 type = LB_VIP_TYPE_IP6_NAT6;
112 args.plen = mp->pfx.len;
113 args.protocol = mp->protocol;
114 args.port = ntohs(mp->port);
116 args.new_length = ntohl(mp->new_flows_table_length);
118 if (mp->encap == LB_API_ENCAP_TYPE_L3DSR) {
119 args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
121 else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4)
122 ||(mp->encap == LB_API_ENCAP_TYPE_NAT6)) {
123 args.encap_args.srv_type = mp->type;
124 args.encap_args.target_port = ntohs(mp->target_port);
127 rv = lb_vip_add(args, &vip_index);
129 REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
133 vl_api_lb_add_del_as_t_handler
134 (vl_api_lb_add_del_as_t * mp)
136 lb_main_t *lbm = &lb_main;
137 vl_api_lb_conf_reply_t * rmp;
140 ip46_address_t vip_ip_prefix;
141 ip46_address_t as_address;
143 /* if port == 0, it means all-port VIP */
148 ip_address_decode (&mp->pfx.address, &vip_ip_prefix);
149 ip_address_decode (&mp->as_address, &as_address);
151 if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->pfx.len,
152 mp->protocol, ntohs(mp->port), &vip_index)))
156 rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
158 rv = lb_vip_add_ass(vip_index, &as_address, 1);
161 REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
165 vl_api_lb_vip_dump_t_handler
166 (vl_api_lb_vip_dump_t * mp)
169 vl_api_registration_t *reg;
170 reg = vl_api_client_index_to_registration (mp->client_index);
174 lb_main_t *lbm = &lb_main;
175 vl_api_lb_vip_details_t * rmp;
179 /* construct vip list */
180 pool_foreach (vip, lbm->vips) {
181 /* Hide placeholder VIP */
182 if (vip != lbm->vips) {
183 msg_size = sizeof (*rmp);
184 rmp = vl_msg_api_alloc (msg_size);
185 memset (rmp, 0, msg_size);
187 htons (VL_API_LB_VIP_DETAILS + lbm->msg_id_base);
188 rmp->context = mp->context;
190 ip_address_encode(&vip->prefix, IP46_TYPE_ANY, &rmp->vip.pfx.address);
191 rmp->vip.pfx.len = vip->plen;
192 rmp->vip.protocol = htonl (vip->protocol);
193 rmp->vip.port = htons(vip->port);
194 rmp->encap = htonl(vip->type);
195 rmp->dscp = vip->encap_args.dscp;
196 rmp->srv_type = vip->encap_args.srv_type;
197 rmp->target_port = htons(vip->encap_args.target_port);
198 rmp->flow_table_length = htonl(vip->new_flow_table_mask + 1);
200 vl_api_send_msg (reg, (u8 *) rmp);
207 static void send_lb_as_details
208 (vl_api_registration_t * reg, u32 context, lb_vip_t * vip)
210 vl_api_lb_as_details_t *rmp;
211 lb_main_t *lbm = &lb_main;
216 /* construct as list under this vip */
219 pool_foreach (as_index, vip->as_indexes) {
220 /* Hide placeholder As for specific VIP */
221 if (*as_index != 0) {
222 as = &lbm->ass[*as_index];
223 msg_size = sizeof (*rmp);
224 rmp = vl_msg_api_alloc (msg_size);
225 memset (rmp, 0, msg_size);
227 htons (VL_API_LB_AS_DETAILS + lbm->msg_id_base);
228 rmp->context = context;
229 ip_address_encode(&vip->prefix, IP46_TYPE_ANY, (vl_api_address_t *)&rmp->vip.pfx.address);
230 rmp->vip.pfx.len = vip->plen;
231 rmp->vip.protocol = htonl (vip->protocol);
232 rmp->vip.port = htons(vip->port);
233 ip_address_encode(&as->address, IP46_TYPE_ANY, &rmp->app_srv);
234 rmp->flags = as->flags;
235 rmp->in_use_since = htonl(as->last_used);
237 vl_api_send_msg (reg, (u8 *) rmp);
246 vl_api_lb_as_dump_t_handler
247 (vl_api_lb_as_dump_t * mp)
249 lb_main_t *lbm = &lb_main;
252 ip46_address_t prefix;
254 vl_api_registration_t *reg;
255 reg = vl_api_client_index_to_registration (mp->client_index);
259 clib_memcpy(&prefix.ip6, mp->pfx.address.un.ip6, sizeof(mp->pfx.address.un.ip6));
261 dump_all = (prefix.ip6.as_u64[0] == 0) && (prefix.ip6.as_u64[1] == 0);
264 pool_foreach (vip, lbm->vips)
267 || ((prefix.as_u64[0] == vip->prefix.as_u64[0])
268 && (prefix.as_u64[1] == vip->prefix.as_u64[1])
269 && (mp->protocol == vip->protocol)
270 && (mp->port == vip->port)) )
272 send_lb_as_details(reg, mp->context, vip);
279 vl_api_lb_flush_vip_t_handler
280 (vl_api_lb_flush_vip_t * mp)
282 lb_main_t *lbm = &lb_main;
284 ip46_address_t vip_prefix;
287 vl_api_lb_flush_vip_reply_t * rmp;
294 memcpy (&(vip_prefix.ip6), mp->pfx.address.un.ip6, sizeof(vip_prefix.ip6));
296 vip_plen = mp->pfx.len;
298 rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
299 ntohs(mp->port), &vip_index);
301 rv = lb_flush_vip_as(vip_index, ~0);
303 REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
306 static void vl_api_lb_add_del_intf_nat4_t_handler
307 (vl_api_lb_add_del_intf_nat4_t * mp)
309 lb_main_t *lbm = &lb_main;
310 vl_api_lb_add_del_intf_nat4_reply_t *rmp;
311 u32 sw_if_index = ntohl (mp->sw_if_index);
315 is_del = !mp->is_add;
317 VALIDATE_SW_IF_INDEX (mp);
319 rv = lb_nat4_interface_add_del(sw_if_index, is_del);
321 BAD_SW_IF_INDEX_LABEL;
323 REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT4_REPLY);
326 static void vl_api_lb_add_del_intf_nat6_t_handler
327 (vl_api_lb_add_del_intf_nat6_t * mp)
329 lb_main_t *lbm = &lb_main;
330 vl_api_lb_add_del_intf_nat6_reply_t *rmp;
331 u32 sw_if_index = ntohl (mp->sw_if_index);
335 is_del = !mp->is_add;
337 VALIDATE_SW_IF_INDEX (mp);
339 rv = lb_nat6_interface_add_del(sw_if_index, is_del);
341 BAD_SW_IF_INDEX_LABEL;
343 REPLY_MACRO (VL_API_LB_ADD_DEL_INTF_NAT6_REPLY);
346 #include <lb/lb.api.c>
347 static clib_error_t * lb_api_init (vlib_main_t * vm)
349 lb_main_t * lbm = &lb_main;
352 lbm->vnet_main = vnet_get_main();
354 /* Ask for a correctly-sized block of API message decode slots */
355 lbm->msg_id_base = setup_message_id_table ();
360 VLIB_INIT_FUNCTION (lb_api_init);