Add VAT support for LB
[vpp.git] / src / plugins / lb / api.c
1 /*
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:
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 "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.
14  */
15
16 #include <lb/lb.h>
17
18 #include <vppinfra/byte_order.h>
19 #include <vlibapi/api.h>
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22
23
24 #define vl_msg_id(n,h) n,
25 typedef enum {
26 #include <lb/lb.api.h>
27     /* We'll want to know how many messages IDs we need... */
28     VL_MSG_FIRST_AVAILABLE,
29 } vl_msg_id_t;
30 #undef vl_msg_id
31
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <lb/lb.api.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <lb/lb.api.h>
41 #undef vl_endianfun
42
43 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <lb/lb.api.h>
48 #undef vl_api_version
49
50 #define vl_msg_name_crc_list
51 #include <lb/lb.api.h>
52 #undef vl_msg_name_crc_list
53
54
55 #define REPLY_MSG_ID_BASE lbm->msg_id_base
56 #include <vlibapi/api_helper_macros.h>
57
58 static void
59 setup_message_id_table (lb_main_t * lbm, api_main_t * am)
60 {
61 #define _(id,n,crc) \
62   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + lbm->msg_id_base);
63   foreach_vl_msg_name_crc_lb;
64 #undef _
65 }
66
67 /* Macro to finish up custom dump fns */
68 #define FINISH                                  \
69     vec_add1 (s, 0);                            \
70     vl_print (handle, (char *)s);               \
71     vec_free (s);                               \
72     return handle;
73
74 static void
75 vl_api_lb_conf_t_handler
76 (vl_api_lb_conf_t * mp)
77 {
78   lb_main_t *lbm = &lb_main;
79   vl_api_lb_conf_reply_t * rmp;
80   int rv = 0;
81
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,
85                mp->flow_timeout);
86
87  REPLY_MACRO (VL_API_LB_CONF_REPLY);
88 }
89
90 static void *vl_api_lb_conf_t_print
91 (vl_api_lb_conf_t *mp, void * handle)
92 {
93   u8 * s;
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);
99   FINISH;
100 }
101
102
103 static void
104 vl_api_lb_add_del_vip_t_handler
105 (vl_api_lb_add_del_vip_t * mp)
106 {
107   lb_main_t *lbm = &lb_main;
108   vl_api_lb_conf_reply_t * rmp;
109   int rv = 0;
110   lb_vip_add_args_t args;
111
112   /* if port == 0, it means all-port VIP */
113   if (mp->port == 0)
114     {
115       mp->protocol = ~0;
116     }
117
118   memcpy (&(args.prefix.ip6), mp->ip_prefix, sizeof(args.prefix.ip6));
119
120   if (mp->is_del) {
121     u32 vip_index;
122     if (!(rv = lb_vip_find_index(&(args.prefix), mp->prefix_length,
123                                  mp->protocol, ntohs(mp->port), &vip_index)))
124       rv = lb_vip_del(vip_index);
125   } else {
126     u32 vip_index;
127     lb_vip_type_t type = 0;
128
129     if (ip46_prefix_is_ip4(&(args.prefix), mp->prefix_length)) {
130         if (mp->encap == LB_ENCAP_TYPE_GRE4)
131             type = LB_VIP_TYPE_IP4_GRE4;
132         else if (mp->encap == LB_ENCAP_TYPE_GRE6)
133             type = LB_VIP_TYPE_IP4_GRE6;
134         else if (mp->encap == LB_ENCAP_TYPE_L3DSR)
135             type = LB_VIP_TYPE_IP4_L3DSR;
136         else if (mp->encap == LB_ENCAP_TYPE_NAT4)
137             type = LB_VIP_TYPE_IP4_NAT4;
138     } else {
139         if (mp->encap == LB_ENCAP_TYPE_GRE4)
140             type = LB_VIP_TYPE_IP6_GRE4;
141         else if (mp->encap == LB_ENCAP_TYPE_GRE6)
142             type = LB_VIP_TYPE_IP6_GRE6;
143         else if (mp->encap == LB_ENCAP_TYPE_NAT6)
144             type = LB_VIP_TYPE_IP6_NAT6;
145     }
146
147     args.plen = mp->prefix_length;
148     args.protocol = mp->protocol;
149     args.port = ntohs(mp->port);
150     args.type = type;
151     args.new_length = ntohl(mp->new_flows_table_length);
152
153     if (mp->encap == LB_ENCAP_TYPE_L3DSR) {
154         args.encap_args.dscp = (u8)(mp->dscp & 0x3F);
155       }
156     else if ((mp->encap == LB_ENCAP_TYPE_NAT4)
157             ||(mp->encap == LB_ENCAP_TYPE_NAT6)) {
158         args.encap_args.srv_type = mp->type;
159         args.encap_args.target_port = ntohs(mp->target_port);
160       }
161
162     rv = lb_vip_add(args, &vip_index);
163   }
164  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
165 }
166
167 static void *vl_api_lb_add_del_vip_t_print
168 (vl_api_lb_add_del_vip_t *mp, void * handle)
169 {
170   u8 * s;
171   s = format (0, "SCRIPT: lb_add_del_vip ");
172   s = format (s, "%U ", format_ip46_prefix,
173               (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
174
175   s = format (s, "%s ", (mp->encap == LB_ENCAP_TYPE_GRE4)? "gre4"
176               : (mp->encap == LB_ENCAP_TYPE_GRE6)? "gre6"
177               : (mp->encap == LB_ENCAP_TYPE_NAT4)? "nat4"
178               : (mp->encap == LB_ENCAP_TYPE_NAT6)? "nat6"
179               : "l3dsr");
180
181   if (mp->encap==LB_ENCAP_TYPE_L3DSR)
182     {
183       s = format (s, "dscp %u ", mp->dscp);
184     }
185
186   if ((mp->encap==LB_ENCAP_TYPE_NAT4)
187       || (mp->encap==LB_ENCAP_TYPE_NAT6))
188     {
189       s = format (s, "type %u ", mp->type);
190       s = format (s, "port %u ", mp->port);
191       s = format (s, "target_port %u ", mp->target_port);
192     }
193
194   s = format (s, "%u ", mp->new_flows_table_length);
195   s = format (s, "%s ", mp->is_del?"del":"add");
196   FINISH;
197 }
198
199 static void
200 vl_api_lb_add_del_as_t_handler
201 (vl_api_lb_add_del_as_t * mp)
202 {
203   lb_main_t *lbm = &lb_main;
204   vl_api_lb_conf_reply_t * rmp;
205   int rv = 0;
206   u32 vip_index;
207   ip46_address_t vip_ip_prefix;
208
209   memcpy(&vip_ip_prefix.ip6, mp->vip_ip_prefix,
210          sizeof(vip_ip_prefix.ip6));
211
212   ip46_address_t as_address;
213
214   memcpy(&as_address.ip6, mp->as_address,
215          sizeof(as_address.ip6));
216
217   if ((rv = lb_vip_find_index(&vip_ip_prefix, mp->vip_prefix_length,
218                               mp->protocol, ntohs(mp->port), &vip_index)))
219     goto done;
220
221   if (mp->is_del)
222     rv = lb_vip_del_ass(vip_index, &as_address, 1, mp->is_flush);
223   else
224     rv = lb_vip_add_ass(vip_index, &as_address, 1);
225
226 done:
227  REPLY_MACRO (VL_API_LB_ADD_DEL_AS_REPLY);
228 }
229
230 static void *vl_api_lb_add_del_as_t_print
231 (vl_api_lb_add_del_as_t *mp, void * handle)
232 {
233   u8 * s;
234   s = format (0, "SCRIPT: lb_add_del_as ");
235   s = format (s, "%U ", format_ip46_prefix,
236               (ip46_address_t *)mp->vip_ip_prefix, mp->vip_prefix_length, IP46_TYPE_ANY);
237   s = format (s, "%U ", format_ip46_address,
238                 (ip46_address_t *)mp->as_address, IP46_TYPE_ANY);
239   s = format (s, "%s ", mp->is_del?"del":"add");
240   FINISH;
241 }
242
243 static void
244 vl_api_lb_flush_vip_t_handler
245 (vl_api_lb_flush_vip_t * mp)
246 {
247   lb_main_t *lbm = &lb_main;
248   int rv = 0;
249   ip46_address_t vip_prefix;
250   u8 vip_plen;
251   u32 vip_index;
252   vl_api_lb_flush_vip_reply_t * rmp;
253
254   if (mp->port == 0)
255     {
256       mp->protocol = ~0;
257     }
258
259   memcpy (&(vip_prefix.ip6), mp->ip_prefix, sizeof(vip_prefix.ip6));
260
261   vip_plen = mp->prefix_length;
262
263   rv = lb_vip_find_index(&vip_prefix, vip_plen, mp->protocol,
264                          ntohs(mp->port), &vip_index);
265
266   rv = lb_flush_vip_as(vip_index, ~0);
267
268  REPLY_MACRO (VL_API_LB_FLUSH_VIP_REPLY);
269 }
270
271 static void *vl_api_lb_flush_vip_t_print
272 (vl_api_lb_flush_vip_t *mp, void * handle)
273 {
274   u8 * s;
275   s = format (0, "SCRIPT: lb_add_del_vip ");
276   s = format (s, "%U ", format_ip46_prefix,
277               (ip46_address_t *)mp->ip_prefix, mp->prefix_length, IP46_TYPE_ANY);
278
279   s = format (s, "protocol %u ", mp->protocol);
280   s = format (s, "port %u ", mp->port);
281
282   FINISH;
283 }
284
285 /* List of message types that this plugin understands */
286 #define foreach_lb_plugin_api_msg            \
287 _(LB_CONF, lb_conf)                          \
288 _(LB_ADD_DEL_VIP, lb_add_del_vip)            \
289 _(LB_ADD_DEL_AS, lb_add_del_as)              \
290 _(LB_FLUSH_VIP, lb_flush_vip)
291
292 static clib_error_t * lb_api_init (vlib_main_t * vm)
293 {
294   lb_main_t *lbm = &lb_main;
295   u8 *name = format (0, "lb_%08x%c", api_version, 0);
296   lbm->msg_id_base = vl_msg_api_get_msg_ids
297       ((char *) name, VL_MSG_FIRST_AVAILABLE);
298
299 #define _(N,n)                                                  \
300     vl_msg_api_set_handlers((VL_API_##N + lbm->msg_id_base),     \
301                            #n,                  \
302                            vl_api_##n##_t_handler,              \
303                            vl_noop_handler,                     \
304                            vl_api_##n##_t_endian,               \
305                            vl_api_##n##_t_print,                \
306                            sizeof(vl_api_##n##_t), 1);
307   foreach_lb_plugin_api_msg;
308 #undef _
309
310   /* Add our API messages to the global name_crc hash table */
311   setup_message_id_table (lbm, &api_main);
312
313   return 0;
314 }
315
316 VLIB_INIT_FUNCTION (lb_api_init);