session: remove ipv6 lookup threading assert
[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.api_enum.h>
28 #include <ct6/ct6.api_types.h>
29
30 #define REPLY_MSG_ID_BASE cmp->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
32
33 ct6_main_t ct6_main;
34
35 /* Action function shared between message handler and debug CLI */
36
37 static void
38 ct6_feature_init (ct6_main_t * cmp)
39 {
40   u32 nworkers = vlib_num_workers ();
41
42   if (cmp->feature_initialized)
43     return;
44
45   clib_bihash_init_48_8 (&cmp->session_hash, "ct6 session table",
46                          cmp->session_hash_buckets, cmp->session_hash_memory);
47   cmp->feature_initialized = 1;
48   vec_validate (cmp->sessions, nworkers);
49   vec_validate_init_empty (cmp->first_index, nworkers, ~0);
50   vec_validate_init_empty (cmp->last_index, nworkers, ~0);
51 }
52
53 int
54 ct6_in2out_enable_disable (ct6_main_t * cmp, u32 sw_if_index,
55                            int enable_disable)
56 {
57   vnet_sw_interface_t *sw;
58   int rv = 0;
59
60   ct6_feature_init (cmp);
61
62   /* Utterly wrong? */
63   if (pool_is_free_index (cmp->vnet_main->interface_main.sw_interfaces,
64                           sw_if_index))
65     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
66
67   /* Not a physical port? */
68   sw = vnet_get_sw_interface (cmp->vnet_main, sw_if_index);
69   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
70     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
71
72   vnet_feature_enable_disable ("interface-output", "ct6-in2out",
73                                sw_if_index, enable_disable, 0, 0);
74
75   return rv;
76 }
77
78 int
79 ct6_out2in_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 ("ip6-unicast", "ct6-out2in",
98                                sw_if_index, enable_disable, 0, 0);
99
100   return rv;
101 }
102
103 static clib_error_t *
104 set_ct6_enable_disable_command_fn (vlib_main_t * vm,
105                                    unformat_input_t * input,
106                                    vlib_cli_command_t * cmd)
107 {
108   ct6_main_t *cmp = &ct6_main;
109   u32 sw_if_index = ~0;
110   int enable_disable = 1;
111   u32 inside = ~0;
112   int rv;
113
114   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
115     {
116       if (unformat (input, "disable"))
117         enable_disable = 0;
118       else if (unformat (input, "%U", unformat_vnet_sw_interface,
119                          cmp->vnet_main, &sw_if_index))
120         ;
121       else if (unformat (input, "inside") || unformat (input, "in"))
122         inside = 1;
123       else if (unformat (input, "outside") || unformat (input, "out"))
124         inside = 0;
125       else
126         break;
127     }
128
129   if (inside == ~0)
130     return clib_error_return (0, "Please specify inside or outside");
131
132   if (sw_if_index == ~0)
133     return clib_error_return (0, "Please specify an interface...");
134
135   if (inside == 1)
136     rv = ct6_in2out_enable_disable (cmp, sw_if_index, enable_disable);
137   else
138     rv = ct6_out2in_enable_disable (cmp, sw_if_index, enable_disable);
139
140   switch (rv)
141     {
142     case 0:
143       break;
144
145     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
146       return clib_error_return
147         (0, "Invalid interface, only works on physical ports");
148       break;
149
150     default:
151       return clib_error_return (0, "ct6_enable_disable returned %d", rv);
152     }
153   return 0;
154 }
155
156 VLIB_CLI_COMMAND (set_ct6_command, static) =
157 {
158   .path = "set ct6",
159   .short_help =
160   "set ct6 [inside|outside] <interface-name> [disable]",
161   .function = set_ct6_enable_disable_command_fn,
162 };
163
164 /* API message handler */
165 static void vl_api_ct6_enable_disable_t_handler
166   (vl_api_ct6_enable_disable_t * mp)
167 {
168   vl_api_ct6_enable_disable_reply_t *rmp;
169   ct6_main_t *cmp = &ct6_main;
170   int rv;
171
172   VALIDATE_SW_IF_INDEX (mp);
173
174   if (mp->is_inside)
175     rv = ct6_in2out_enable_disable (cmp, ntohl (mp->sw_if_index),
176                                     (int) (mp->enable_disable));
177   else
178     rv = ct6_out2in_enable_disable (cmp, ntohl (mp->sw_if_index),
179                                     (int) (mp->enable_disable));
180
181   BAD_SW_IF_INDEX_LABEL;
182   REPLY_MACRO (VL_API_CT6_ENABLE_DISABLE_REPLY);
183 }
184
185 #include <ct6/ct6.api.c>
186 static clib_error_t *
187 ct6_init (vlib_main_t * vm)
188 {
189   ct6_main_t *cmp = &ct6_main;
190   clib_error_t *error = 0;
191
192   cmp->vlib_main = vm;
193   cmp->vnet_main = vnet_get_main ();
194
195   /* Ask for a correctly-sized block of API message decode slots */
196   cmp->msg_id_base = setup_message_id_table ();
197
198   /*
199    * Set default parameters...
200    * 256K sessions
201    * 64K buckets
202    * 2 minute inactivity timer
203    * 10000 concurrent sessions
204    */
205   cmp->session_hash_memory = 16ULL << 20;
206   cmp->session_hash_buckets = 64 << 10;
207   cmp->session_timeout_interval = 120.0;
208   cmp->max_sessions_per_worker = 10000;
209
210   /* ... so the packet generator can feed the in2out node ... */
211   ethernet_setup_node (vm, ct6_in2out_node.index);
212   return error;
213 }
214
215 VLIB_INIT_FUNCTION (ct6_init);
216
217 VNET_FEATURE_INIT (ct6out2in, static) =
218 {
219   .arc_name = "ip6-unicast",
220   .node_name = "ct6-out2in",
221   .runs_before = VNET_FEATURES ("ip6-lookup"),
222 };
223
224 VNET_FEATURE_INIT (ct6in2out, static) = {
225   .arc_name = "interface-output",
226   .node_name = "ct6-in2out",
227   .runs_before = VNET_FEATURES ("interface-output-arc-end"),
228 };
229
230 VLIB_PLUGIN_REGISTER () =
231 {
232   .version = VPP_BUILD_VER,
233   .description = "IPv6 Connection Tracker",
234 };
235
236 u8 *
237 format_ct6_session (u8 * s, va_list * args)
238 {
239   ct6_main_t *cmp = va_arg (*args, ct6_main_t *);
240   int i = va_arg (*args, int);
241   ct6_session_t *s0 = va_arg (*args, ct6_session_t *);
242   int verbose = va_arg (*args, int);
243   clib_bihash_kv_48_8_t kvp0;
244
245   if (s0 == 0)
246     {
247       s = format (s, "\n%6s%6s%40s%6s%40s%6s",
248                   "Sess", "Prot", "Src", "Sport", "Dst", "Dport");
249       return s;
250     }
251
252   s = format (s, "\n%6d%6d%40U%6u%40U%6u",
253               s0 - cmp->sessions[i], s0->key.proto,
254               format_ip6_address, &s0->key.src,
255               clib_net_to_host_u16 (s0->key.sport),
256               format_ip6_address, &s0->key.dst,
257               clib_net_to_host_u16 (s0->key.dport));
258
259   clib_memcpy_fast (&kvp0, s0, sizeof (ct6_session_key_t));
260
261   if (clib_bihash_search_48_8 (&cmp->session_hash, &kvp0, &kvp0) < 0)
262     {
263       s = format (s, " LOOKUP FAIL!");
264     }
265   else
266     {
267       if (kvp0.value == s0 - cmp->sessions[s0->thread_index])
268         {
269           s = format (s, " OK");
270           if (verbose > 1)
271             {
272               s = format (s, " next %d prev %d", s0->next_index,
273                           s0->prev_index);
274               s = format (s, " hits %d expires %.2f", s0->hits, s0->expires);
275             }
276         }
277       else
278         s = format (s, " BOGUS LOOKUP RESULT!");
279     }
280
281   return s;
282 }
283
284 static clib_error_t *
285 show_ct6_command_fn_command_fn (vlib_main_t * vm,
286                                 unformat_input_t * input,
287                                 vlib_cli_command_t * cmd)
288 {
289   ct6_main_t *cmp = &ct6_main;
290   ct6_session_t *s0;
291   int verbose = 0;
292   u8 *s = 0;
293   int i;
294
295   if (!cmp->feature_initialized)
296     return clib_error_return (0, "ip6 connection tracking not enabled...");
297
298   if (unformat (input, "verbose %d", &verbose))
299     ;
300   else if (unformat (input, "verbose"))
301     verbose = 1;
302
303   for (i = 0; i < vec_len (cmp->sessions); i++)
304     {
305       s = format (s, "Thread %d: %d sessions\n", i,
306                   pool_elts (cmp->sessions[i]));
307
308       if (verbose == 0)
309         continue;
310
311       s =
312         format (s, "%U", format_ct6_session, cmp,
313                 0 /* pool */ , 0 /* header */ , verbose);
314
315       pool_foreach (s0, cmp->sessions[i])
316        {
317         s = format (s, "%U", format_ct6_session, cmp, i, s0, verbose);
318       }
319     }
320   vlib_cli_output (cmp->vlib_main, "%v", s);
321   vec_free (s);
322   return 0;
323 }
324
325 VLIB_CLI_COMMAND (show_ct6_command_fn_command, static) =
326 {
327   .path = "show ip6 connection-tracker",
328   .short_help = "show ip6 connection-tracker",
329   .function = show_ct6_command_fn_command_fn,
330 };
331
332 static void
333 increment_v6_address (ip6_address_t * a)
334 {
335   u64 v0, v1;
336
337   v0 = clib_net_to_host_u64 (a->as_u64[0]);
338   v1 = clib_net_to_host_u64 (a->as_u64[1]);
339
340   v1 += 1;
341   if (v1 == 0)
342     v0 += 1;
343   a->as_u64[0] = clib_net_to_host_u64 (v0);
344   a->as_u64[1] = clib_net_to_host_u64 (v1);
345 }
346
347
348 static clib_error_t *
349 test_ct6_command_fn_command_fn (vlib_main_t * vm,
350                                 unformat_input_t * input,
351                                 vlib_cli_command_t * cmd)
352 {
353   ct6_main_t *cmp = &ct6_main;
354   clib_bihash_kv_48_8_t kvp0;
355   ct6_session_key_t *key0;
356   ct6_session_t *s0;
357   u8 src[16], dst[16];
358   u32 recycled = 0, created = 0;
359   int i, num_sessions = 5;
360   u32 midpt_index;
361   u8 *s = 0;
362
363   cmp->max_sessions_per_worker = 4;
364
365   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
366     {
367       if (unformat (input, "num-sessions %d", &num_sessions))
368         ;
369       else
370         if (unformat
371             (input, "max-sessions %d", &cmp->max_sessions_per_worker))
372         ;
373       else
374         break;
375     }
376
377   ct6_feature_init (cmp);
378
379   /* Set up starting src/dst addresses */
380   memset (src, 0, sizeof (src));
381   memset (dst, 0, sizeof (dst));
382
383   src[0] = 0xdb;
384   dst[0] = 0xbe;
385
386   src[15] = 1;
387   dst[15] = 1;
388
389   /*
390    * See if we know about this flow.
391    * Key set up for the out2in path, the performant case
392    */
393   key0 = (ct6_session_key_t *) & kvp0;
394   memset (&kvp0, 0, sizeof (kvp0));
395
396   for (i = 0; i < num_sessions; i++)
397     {
398       clib_memcpy_fast (&key0->src, src, sizeof (src));
399       clib_memcpy_fast (&key0->dst, dst, sizeof (dst));
400       key0->as_u64[4] = 0;
401       key0->as_u64[5] = 0;
402       key0->sport = clib_host_to_net_u16 (1234);
403       key0->dport = clib_host_to_net_u16 (4321);
404       key0->proto = 17;         /* udp, fwiw */
405
406       s0 = ct6_create_or_recycle_session
407         (cmp, &kvp0, 3.0 /* now */ , 0 /* thread index */ ,
408          &recycled, &created);
409
410       s = format (s, "%U (%d, %d)", format_ct6_session, cmp,
411                   0 /* thread index */ , s0, 1 /* verbose */ ,
412                   recycled, created);
413       vlib_cli_output (vm, "%v", s);
414       vec_free (s);
415       increment_v6_address ((ip6_address_t *) src);
416       recycled = 0;
417       created = 0;
418     }
419
420   pool_foreach (s0, cmp->sessions[0])
421    {
422     s = format (s, "%U", format_ct6_session, cmp, 0, s0, 1 /* verbose */);
423   }
424
425   vlib_cli_output (vm, "\nEnd state: first index %d last index %d\n%v",
426                    cmp->first_index[0], cmp->last_index[0], s);
427
428   vec_free (s);
429
430   midpt_index = cmp->max_sessions_per_worker / 3;
431
432   s0 = pool_elt_at_index (cmp->sessions[0], midpt_index);
433   vlib_cli_output (vm, "\nSimulate LRU hit on session %d",
434                    s0 - cmp->sessions[0]);
435
436   ct6_update_session_hit (cmp, s0, 234.0);
437
438   pool_foreach (s0, cmp->sessions[0])
439    {
440     s = format (s, "%U", format_ct6_session, cmp, 0, s0, 1 /* verbose */);
441   }
442
443   vlib_cli_output (vm, "\nEnd state: first index %d last index %d\n%v",
444                    cmp->first_index[0], cmp->last_index[0], s);
445
446   vec_free (s);
447
448   return 0;
449 }
450
451 VLIB_CLI_COMMAND (test_ct6_command_fn_command, static) =
452 {
453   .path = "test ip6 connection-tracker",
454   .short_help = "test ip6 connection-tracker",
455   .function = test_ct6_command_fn_command_fn,
456 };
457
458 static clib_error_t *
459 ct6_config (vlib_main_t * vm, unformat_input_t * input)
460 {
461   ct6_main_t *cmp = &ct6_main;
462
463   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
464     {
465       if (unformat (input, "session-hash-buckets %u",
466                     &cmp->session_hash_buckets))
467         ;
468       else if (unformat (input, "session-hash-memory %U",
469                          unformat_memory_size, &cmp->session_hash_memory))
470         ;
471       else if (unformat (input, "session-timeout %f",
472                          &cmp->session_timeout_interval))
473         ;
474       else
475         {
476           return clib_error_return (0, "unknown input '%U'",
477                                     format_unformat_error, input);
478         }
479     }
480   return 0;
481 }
482
483 VLIB_CONFIG_FUNCTION (ct6_config, "ct6");
484
485 /*
486  * fd.io coding-style-patch-verification: ON
487  *
488  * Local Variables:
489  * eval: (c-set-style "gnu")
490  * End:
491  */