VPP-1576: fix a set of coverity warnings
[vpp.git] / src / plugins / ct6 / ct6.c
1 /*
2  * ct6.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <ct6/ct6.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25
26 /* define message IDs */
27 #include <ct6/ct6_msg_enum.h>
28
29 /* define message structures */
30 #define vl_typedefs
31 #include <ct6/ct6_all_api_h.h>
32 #undef vl_typedefs
33
34 /* define generated endian-swappers */
35 #define vl_endianfun
36 #include <ct6/ct6_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <ct6/ct6_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <ct6/ct6_all_api_h.h>
48 #undef vl_api_version
49
50 #define REPLY_MSG_ID_BASE cmp->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
52
53 ct6_main_t ct6_main;
54
55 /* List of message types that this plugin understands */
56
57 #define foreach_ct6_plugin_api_msg                           \
58 _(CT6_ENABLE_DISABLE, ct6_enable_disable)
59
60 /* Action function shared between message handler and debug CLI */
61
62 static void
63 ct6_feature_init (ct6_main_t * cmp)
64 {
65   u32 nworkers = vlib_num_workers ();
66
67   if (cmp->feature_initialized)
68     return;
69
70   clib_bihash_init_48_8 (&cmp->session_hash, "ct6 session table",
71                          cmp->session_hash_buckets, cmp->session_hash_memory);
72   cmp->feature_initialized = 1;
73   vec_validate (cmp->sessions, nworkers);
74   vec_validate_init_empty (cmp->first_index, nworkers, ~0);
75   vec_validate_init_empty (cmp->last_index, nworkers, ~0);
76 }
77
78 int
79 ct6_in2out_enable_disable (ct6_main_t * cmp, u32 sw_if_index,
80                            int enable_disable)
81 {
82   vnet_sw_interface_t *sw;
83   int rv = 0;
84
85   ct6_feature_init (cmp);
86
87   /* Utterly wrong? */
88   if (pool_is_free_index (cmp->vnet_main->interface_main.sw_interfaces,
89                           sw_if_index))
90     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
91
92   /* Not a physical port? */
93   sw = vnet_get_sw_interface (cmp->vnet_main, sw_if_index);
94   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
95     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
96
97   vnet_feature_enable_disable ("interface-output", "ct6-in2out",
98                                sw_if_index, enable_disable, 0, 0);
99
100   return rv;
101 }
102
103 int
104 ct6_out2in_enable_disable (ct6_main_t * cmp, u32 sw_if_index,
105                            int enable_disable)
106 {
107   vnet_sw_interface_t *sw;
108   int rv = 0;
109
110   ct6_feature_init (cmp);
111
112   /* Utterly wrong? */
113   if (pool_is_free_index (cmp->vnet_main->interface_main.sw_interfaces,
114                           sw_if_index))
115     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
116
117   /* Not a physical port? */
118   sw = vnet_get_sw_interface (cmp->vnet_main, sw_if_index);
119   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
120     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
121
122   vnet_feature_enable_disable ("ip6-unicast", "ct6-out2in",
123                                sw_if_index, enable_disable, 0, 0);
124
125   return rv;
126 }
127
128 static clib_error_t *
129 set_ct6_enable_disable_command_fn (vlib_main_t * vm,
130                                    unformat_input_t * input,
131                                    vlib_cli_command_t * cmd)
132 {
133   ct6_main_t *cmp = &ct6_main;
134   u32 sw_if_index = ~0;
135   int enable_disable = 1;
136   u32 inside = ~0;
137   int rv;
138
139   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
140     {
141       if (unformat (input, "disable"))
142         enable_disable = 0;
143       else if (unformat (input, "%U", unformat_vnet_sw_interface,
144                          cmp->vnet_main, &sw_if_index))
145         ;
146       else if (unformat (input, "inside") || unformat (input, "in"))
147         inside = 1;
148       else if (unformat (input, "outside") || unformat (input, "out"))
149         inside = 0;
150       else
151         break;
152     }
153
154   if (inside == ~0)
155     return clib_error_return (0, "Please specify inside or outside");
156
157   if (sw_if_index == ~0)
158     return clib_error_return (0, "Please specify an interface...");
159
160   if (inside == 1)
161     rv = ct6_in2out_enable_disable (cmp, sw_if_index, enable_disable);
162   else
163     rv = ct6_out2in_enable_disable (cmp, sw_if_index, enable_disable);
164
165   switch (rv)
166     {
167     case 0:
168       break;
169
170     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
171       return clib_error_return
172         (0, "Invalid interface, only works on physical ports");
173       break;
174
175     default:
176       return clib_error_return (0, "ct6_enable_disable returned %d", rv);
177     }
178   return 0;
179 }
180
181 /* *INDENT-OFF* */
182 VLIB_CLI_COMMAND (set_ct6_command, static) =
183 {
184   .path = "set ct6",
185   .short_help =
186   "set ct6 [inside|outside] <interface-name> [disable]",
187   .function = set_ct6_enable_disable_command_fn,
188 };
189 /* *INDENT-ON* */
190
191 /* API message handler */
192 static void vl_api_ct6_enable_disable_t_handler
193   (vl_api_ct6_enable_disable_t * mp)
194 {
195   vl_api_ct6_enable_disable_reply_t *rmp;
196   ct6_main_t *cmp = &ct6_main;
197   int rv;
198
199   VALIDATE_SW_IF_INDEX (mp);
200
201   if (mp->is_inside)
202     rv = ct6_in2out_enable_disable (cmp, ntohl (mp->sw_if_index),
203                                     (int) (mp->enable_disable));
204   else
205     rv = ct6_out2in_enable_disable (cmp, ntohl (mp->sw_if_index),
206                                     (int) (mp->enable_disable));
207
208   BAD_SW_IF_INDEX_LABEL;
209   REPLY_MACRO (VL_API_CT6_ENABLE_DISABLE_REPLY);
210 }
211
212 /* Set up the API message handling tables */
213 static clib_error_t *
214 ct6_plugin_api_hookup (vlib_main_t * vm)
215 {
216   ct6_main_t *cmp = &ct6_main;
217 #define _(N,n)                                                  \
218     vl_msg_api_set_handlers((VL_API_##N + cmp->msg_id_base),     \
219                            #n,                                  \
220                            vl_api_##n##_t_handler,              \
221                            vl_noop_handler,                     \
222                            vl_api_##n##_t_endian,               \
223                            vl_api_##n##_t_print,                \
224                            sizeof(vl_api_##n##_t), 1);
225   foreach_ct6_plugin_api_msg;
226 #undef _
227
228   return 0;
229 }
230
231 #define vl_msg_name_crc_list
232 #include <ct6/ct6_all_api_h.h>
233 #undef vl_msg_name_crc_list
234
235 static void
236 setup_message_id_table (ct6_main_t * cmp, api_main_t * am)
237 {
238 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n  #crc, id + cmp->msg_id_base);
239   foreach_vl_msg_name_crc_ct6;
240 #undef _
241 }
242
243 static clib_error_t *
244 ct6_init (vlib_main_t * vm)
245 {
246   ct6_main_t *cmp = &ct6_main;
247   clib_error_t *error = 0;
248   u8 *name;
249
250   cmp->vlib_main = vm;
251   cmp->vnet_main = vnet_get_main ();
252
253   name = format (0, "ct6_%08x%c", api_version, 0);
254
255   /* Ask for a correctly-sized block of API message decode slots */
256   cmp->msg_id_base = vl_msg_api_get_msg_ids
257     ((char *) name, VL_MSG_FIRST_AVAILABLE);
258
259   error = ct6_plugin_api_hookup (vm);
260
261   /* Add our API messages to the global name_crc hash table */
262   setup_message_id_table (cmp, &api_main);
263
264   vec_free (name);
265
266   /*
267    * Set default parameters...
268    * 256K sessions
269    * 64K buckets
270    * 2 minute inactivity timer
271    * 10000 concurrent sessions
272    */
273   cmp->session_hash_memory = 16ULL << 20;
274   cmp->session_hash_buckets = 64 << 10;
275   cmp->session_timeout_interval = 120.0;
276   cmp->max_sessions_per_worker = 10000;
277
278   /* ... so the packet generator can feed the in2out node ... */
279   ethernet_setup_node (vm, ct6_in2out_node.index);
280   return error;
281 }
282
283 VLIB_INIT_FUNCTION (ct6_init);
284
285 /* *INDENT-OFF* */
286 VNET_FEATURE_INIT (ct6out2in, static) =
287 {
288   .arc_name = "ip6-unicast",
289   .node_name = "ct6-out2in",
290   .runs_before = VNET_FEATURES ("ip6-lookup"),
291 };
292 /* *INDENT-ON */
293
294 /* *INDENT-OFF* */
295 VNET_FEATURE_INIT (ct6in2out, static) =
296 {
297   .arc_name = "interface-output",
298   .node_name = "ct6-in2out",
299   .runs_before = VNET_FEATURES ("interface-tx"),
300 };
301 /* *INDENT-ON */
302
303 /* *INDENT-OFF* */
304 VLIB_PLUGIN_REGISTER () =
305 {
306   .version = VPP_BUILD_VER,
307   .description = "ipv6 connection tracker",
308 };
309 /* *INDENT-ON* */
310
311 u8 *
312 format_ct6_session (u8 * s, va_list * args)
313 {
314   ct6_main_t *cmp = va_arg (*args, ct6_main_t *);
315   int i = va_arg (*args, int);
316   ct6_session_t *s0 = va_arg (*args, ct6_session_t *);
317   int verbose = va_arg (*args, int);
318   clib_bihash_kv_48_8_t kvp0;
319
320   if (s0 == 0)
321     {
322       s = format (s, "\n%6s%6s%40s%6s%40s%6s",
323                   "Sess", "Prot", "Src", "Sport", "Dst", "Dport");
324       return s;
325     }
326
327   s = format (s, "\n%6d%6d%40U%6u%40U%6u",
328               s0 - cmp->sessions[i], s0->key.proto,
329               format_ip6_address, &s0->key.src,
330               clib_net_to_host_u16 (s0->key.sport),
331               format_ip6_address, &s0->key.dst,
332               clib_net_to_host_u16 (s0->key.dport));
333
334   clib_memcpy_fast (&kvp0, s0, sizeof (ct6_session_key_t));
335
336   if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
337     {
338       s = format (s, " LOOKUP FAIL!");
339     }
340   else
341     {
342       if (kvp0.value == s0 - cmp->sessions[s0->thread_index])
343         {
344           s = format (s, " OK");
345           if (verbose > 1)
346             {
347               s = format (s, " next %d prev %d", s0->next_index,
348                           s0->prev_index);
349               s = format (s, " hits %d expires %.2f", s0->hits, s0->expires);
350             }
351         }
352       else
353         s = format (s, " BOGUS LOOKUP RESULT!");
354     }
355
356   return s;
357 }
358
359 static clib_error_t *
360 show_ct6_command_fn_command_fn (vlib_main_t * vm,
361                                 unformat_input_t * input,
362                                 vlib_cli_command_t * cmd)
363 {
364   ct6_main_t *cmp = &ct6_main;
365   ct6_session_t *s0;
366   int verbose = 0;
367   u8 *s = 0;
368   int i;
369
370   if (!cmp->feature_initialized)
371     return clib_error_return (0, "ip6 connection tracking not enabled...");
372
373   if (unformat (input, "verbose %d", &verbose))
374     ;
375   else if (unformat (input, "verbose"))
376     verbose = 1;
377
378   for (i = 0; i < vec_len (cmp->sessions); i++)
379     {
380       s = format (s, "Thread %d: %d sessions\n", i,
381                   pool_elts (cmp->sessions[i]));
382
383       if (verbose == 0)
384         continue;
385
386       s =
387         format (s, "%U", format_ct6_session, cmp,
388                 0 /* pool */ , 0 /* header */ , verbose);
389
390       /* *INDENT-OFF* */
391       pool_foreach (s0, cmp->sessions[i],
392       ({
393         s = format (s, "%U", format_ct6_session, cmp, i, s0, verbose);
394       }));
395       /* *INDENT-ON* */
396     }
397   vlib_cli_output (cmp->vlib_main, "%v", s);
398   vec_free (s);
399   return 0;
400 }
401
402 /* *INDENT-OFF* */
403 VLIB_CLI_COMMAND (show_ct6_command_fn_command, static) =
404 {
405   .path = "show ip6 connection-tracker",
406   .short_help = "show ip6 connection-tracker",
407   .function = show_ct6_command_fn_command_fn,
408 };
409 /* *INDENT-ON* */
410
411 static void
412 increment_v6_address (ip6_address_t * a)
413 {
414   u64 v0, v1;
415
416   v0 = clib_net_to_host_u64 (a->as_u64[0]);
417   v1 = clib_net_to_host_u64 (a->as_u64[1]);
418
419   v1 += 1;
420   if (v1 == 0)
421     v0 += 1;
422   a->as_u64[0] = clib_net_to_host_u64 (v0);
423   a->as_u64[1] = clib_net_to_host_u64 (v1);
424 }
425
426
427 static clib_error_t *
428 test_ct6_command_fn_command_fn (vlib_main_t * vm,
429                                 unformat_input_t * input,
430                                 vlib_cli_command_t * cmd)
431 {
432   ct6_main_t *cmp = &ct6_main;
433   clib_bihash_kv_48_8_t kvp0;
434   ct6_session_key_t *key0;
435   ct6_session_t *s0;
436   u8 src[16], dst[16];
437   u32 recycled = 0, created = 0;
438   int i, num_sessions = 5;
439   u32 midpt_index;
440   u8 *s = 0;
441
442   cmp->max_sessions_per_worker = 4;
443
444   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
445     {
446       if (unformat (input, "num-sessions %d", &num_sessions))
447         ;
448       else
449         if (unformat
450             (input, "max-sessions %d", &cmp->max_sessions_per_worker))
451         ;
452       else
453         break;
454     }
455
456   ct6_feature_init (cmp);
457
458   /* Set up starting src/dst addresses */
459   memset (src, 0, sizeof (src));
460   memset (dst, 0, sizeof (dst));
461
462   src[0] = 0xdb;
463   dst[0] = 0xbe;
464
465   src[15] = 1;
466   dst[15] = 1;
467
468   /*
469    * See if we know about this flow.
470    * Key set up for the out2in path, the performant case
471    */
472   key0 = (ct6_session_key_t *) & kvp0;
473   memset (&kvp0, 0, sizeof (kvp0));
474
475   for (i = 0; i < num_sessions; i++)
476     {
477       clib_memcpy_fast (&key0->src, src, sizeof (src));
478       clib_memcpy_fast (&key0->dst, dst, sizeof (dst));
479       key0->as_u64[4] = 0;
480       key0->as_u64[5] = 0;
481       key0->sport = clib_host_to_net_u16 (1234);
482       key0->dport = clib_host_to_net_u16 (4321);
483       key0->proto = 17;         /* udp, fwiw */
484
485       s0 = ct6_create_or_recycle_session
486         (cmp, &kvp0, 3.0 /* now */ , 0 /* thread index */ ,
487          &recycled, &created);
488
489       s = format (s, "%U (%d, %d)", format_ct6_session, cmp,
490                   0 /* thread index */ , s0, 1 /* verbose */ ,
491                   recycled, created);
492       vlib_cli_output (vm, "%v", s);
493       vec_free (s);
494       increment_v6_address ((ip6_address_t *) src);
495       recycled = 0;
496       created = 0;
497     }
498
499   /* *INDENT-OFF* */
500   pool_foreach (s0, cmp->sessions[0],
501   ({
502     s = format (s, "%U", format_ct6_session, cmp, 0, s0, 1 /* verbose */);
503   }));
504   /* *INDENT-ON* */
505
506   vlib_cli_output (vm, "\nEnd state: first index %d last index %d\n%v",
507                    cmp->first_index[0], cmp->last_index[0], s);
508
509   vec_free (s);
510
511   midpt_index = cmp->max_sessions_per_worker / 3;
512
513   s0 = pool_elt_at_index (cmp->sessions[0], midpt_index);
514   vlib_cli_output (vm, "\nSimulate LRU hit on session %d",
515                    s0 - cmp->sessions[0]);
516
517   ct6_update_session_hit (cmp, s0, 234.0);
518
519   /* *INDENT-OFF* */
520   pool_foreach (s0, cmp->sessions[0],
521   ({
522     s = format (s, "%U", format_ct6_session, cmp, 0, s0, 1 /* verbose */);
523   }));
524   /* *INDENT-ON* */
525
526   vlib_cli_output (vm, "\nEnd state: first index %d last index %d\n%v",
527                    cmp->first_index[0], cmp->last_index[0], s);
528
529   vec_free (s);
530
531   return 0;
532 }
533
534 /* *INDENT-OFF* */
535 VLIB_CLI_COMMAND (test_ct6_command_fn_command, static) =
536 {
537   .path = "test ip6 connection-tracker",
538   .short_help = "test ip6 connection-tracker",
539   .function = test_ct6_command_fn_command_fn,
540 };
541 /* *INDENT-ON* */
542
543 static clib_error_t *
544 ct6_config (vlib_main_t * vm, unformat_input_t * input)
545 {
546   ct6_main_t *cmp = &ct6_main;
547
548   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
549     {
550       if (unformat (input, "session-hash-buckets %u",
551                     &cmp->session_hash_buckets))
552         ;
553       else if (unformat (input, "session-hash-memory %U",
554                          unformat_memory_size, &cmp->session_hash_memory))
555         ;
556       else if (unformat (input, "session-timeout %f",
557                          &cmp->session_timeout_interval))
558         ;
559       else
560         {
561           return clib_error_return (0, "unknown input '%U'",
562                                     format_unformat_error, input);
563         }
564     }
565   return 0;
566 }
567
568 VLIB_CONFIG_FUNCTION (ct6_config, "ct6");
569
570 /*
571  * fd.io coding-style-patch-verification: ON
572  *
573  * Local Variables:
574  * eval: (c-set-style "gnu")
575  * End:
576  */