Repair Doxygen build infrastructure
[vpp.git] / plugins / acl-plugin / acl / l2sess.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  *------------------------------------------------------------------
17  * l2sess.c - simple MAC-swap API / debug CLI handling
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <acl/l2sess.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 #include <vppinfra/timing_wheel.h>
29
30 #include <vnet/l2/l2_output.h>
31 #include <vnet/l2/l2_input.h>
32
33 void
34 l2sess_vlib_plugin_register (vlib_main_t * vm, void* hh,
35                       int from_early_init)
36 {
37   l2sess_main_t *sm = &l2sess_main;
38   vnet_plugin_handoff_t * h = hh;
39   memset (sm, 0, sizeof (*sm));
40
41   sm->vlib_main = vm;
42   sm->vnet_main = h->vnet_main;
43   sm->ethernet_main = h->ethernet_main;
44 }
45
46 void
47 l2sess_init_next_features_input (vlib_main_t * vm, l2sess_main_t * sm)
48 {
49 #define _(node_name, node_var, is_out, is_ip6, is_track) \
50   if (!is_out) feat_bitmap_init_next_nodes(vm, node_var.index, L2INPUT_N_FEAT,  l2input_get_feat_names (), sm->node_var ## _input_next_node_index);
51   foreach_l2sess_node
52 #undef _
53 }
54
55 void
56 l2sess_add_our_next_nodes (vlib_main_t * vm, l2sess_main_t * sm,
57                            u8 * prev_node_name, int add_output_nodes)
58 {
59   vlib_node_t *n;
60   n = vlib_get_node_by_name (vm, prev_node_name);
61 #define _(node_name, node_var, is_out, is_ip6, is_track) \
62   if (is_out == add_output_nodes) { \
63     u32 idx = vlib_node_add_next_with_slot(vm, n->index, node_var.index, ~0); \
64     if (is_track) { \
65       sm->next_slot_track_node_by_is_ip6_is_out[is_ip6][is_out] = idx; \
66     } \
67   }
68   foreach_l2sess_node
69 #undef _
70 }
71
72 void
73 l2sess_setup_nodes (void)
74 {
75   vlib_main_t *vm = vlib_get_main ();
76   l2sess_main_t *sm = &l2sess_main;
77
78   l2sess_init_next_features_input (vm, sm);
79
80   l2sess_add_our_next_nodes (vm, sm, (u8 *) "l2-input-classify", 0);
81   l2sess_add_our_next_nodes (vm, sm, (u8 *) "l2-output-classify", 1);
82
83 }
84
85 static char *
86 get_l4_proto_str (int is_ip6, uint8_t l4_proto)
87 {
88   switch (l4_proto)
89     {
90     case 6:
91       return "tcp";
92     case 17:
93       return "udp";
94     case 1:
95       return "icmp";
96     case 58:
97       return "icmp6";
98     default:
99       return "<?l4-unknown?>";
100     }
101 }
102
103 static clib_error_t *
104 l2sess_show_command_fn (vlib_main_t * vm,
105                         unformat_input_t * input, vlib_cli_command_t * cmd)
106 {
107   l2sess_main_t *sm = &l2sess_main;
108   clib_time_t *ct = &vm->clib_time;
109   l2s_session_t *s;
110   u64 now = clib_cpu_time_now ();
111
112   vlib_cli_output (vm, "Timing wheel info: \n%U", format_timing_wheel,
113                    &sm->timing_wheel, 255);
114
115   pool_foreach (s, sm->sessions, (
116                                    {
117                                    f64 ctime =
118                                    (now -
119                                     s->create_time) * ct->seconds_per_clock;
120                                    f64 atime0 =
121                                    (now -
122                                     s->side[0].active_time) *
123                                    ct->seconds_per_clock;
124                                    f64 atime1 =
125                                    (now -
126                                     s->side[1].active_time) *
127                                    ct->seconds_per_clock;
128 /*
129     f64 ctime = (s->create_time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
130     f64 atime0 = (s->side[0].active_time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
131     f64 atime1 = (s->side[1].active_time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
132 */
133                                    u8 * out0 =
134                                    format (0,
135                                            "%5d: create time: %U pkts/bytes/active time: [ %ld %ld %U : %ld %ld %U ]\n",
136                                            (s - sm->sessions),
137                                            format_time_interval, "h:m:s:u",
138                                            ctime, s->side[0].n_packets,
139                                            s->side[0].n_bytes,
140                                            format_time_interval, "h:m:s:u",
141                                            atime0, s->side[1].n_packets,
142                                            s->side[1].n_bytes,
143                                            format_time_interval, "h:m:s:u",
144                                            atime1); u8 * out1 = 0;
145                                    if (s->is_ip6)
146                                    {
147                                    out1 =
148                                    format (0, "%s %U :%u <-> %U :%u",
149                                            get_l4_proto_str (s->is_ip6,
150                                                              s->l4_proto),
151                                            format_ip6_address,
152                                            &s->side[0].addr.ip6,
153                                            s->side[0].port,
154                                            format_ip6_address,
155                                            &s->side[1].addr.ip6,
156                                            s->side[1].port);}
157                                    else
158                                    {
159                                    out1 =
160                                    format (0, "%s %U :%u <-> %U :%u",
161                                            get_l4_proto_str (s->is_ip6,
162                                                              s->l4_proto),
163                                            format_ip4_address,
164                                            &s->side[0].addr.ip4,
165                                            s->side[0].port,
166                                            format_ip4_address,
167                                            &s->side[1].addr.ip4,
168                                            s->side[1].port);}
169                                    vlib_cli_output (vm, "%s       %s", out0,
170                                                     out1); vec_free (out0);
171                                    vec_free (out1);}
172                 ));
173   return 0;
174 }
175
176 static clib_error_t *
177 l2sess_show_count_command_fn (vlib_main_t * vm,
178                               unformat_input_t * input,
179                               vlib_cli_command_t * cmd)
180 {
181   l2sess_main_t *sm = &l2sess_main;
182
183   vlib_cli_output (vm, "Timing wheel info: \n%U", format_timing_wheel,
184                    &sm->timing_wheel, 255);
185   vlib_cli_output (vm, "session pool len: %d, pool elts: %d",
186                    pool_len (sm->sessions), pool_elts (sm->sessions));
187   vlib_cli_output (vm,
188                    "attempted to delete sessions which were already free: %d",
189                    sm->counter_attempted_delete_free_session);
190   return 0;
191 }
192
193
194 /* *INDENT-OFF* */
195 VLIB_CLI_COMMAND (l2sess_show_command, static) = {
196     .path = "show l2sess",
197     .short_help = "show l2sess",
198     .function = l2sess_show_command_fn,
199 };
200
201 VLIB_CLI_COMMAND (l2sess_show_count_command, static) = {
202     .path = "show l2sess count",
203     .short_help = "show l2sess count",
204     .function = l2sess_show_count_command_fn,
205 };
206 /* *INDENT-OFF* */
207
208 static inline u64
209 time_sec_to_clock( clib_time_t *ct, f64 sec)
210 {
211   return (u64)(((f64)sec)/ct->seconds_per_clock);
212 }
213
214 static clib_error_t * l2sess_init (vlib_main_t * vm)
215 {
216   l2sess_main_t * sm = &l2sess_main;
217   clib_error_t * error = 0;
218   u64 cpu_time_now = clib_cpu_time_now();
219
220
221   clib_time_t *ct = &vm->clib_time;
222   sm->udp_session_idle_timeout = time_sec_to_clock(ct, UDP_SESSION_IDLE_TIMEOUT_SEC);
223   sm->tcp_session_idle_timeout = time_sec_to_clock(ct, TCP_SESSION_IDLE_TIMEOUT_SEC);
224   sm->tcp_session_transient_timeout = time_sec_to_clock(ct, TCP_SESSION_TRANSIENT_TIMEOUT_SEC);
225
226   /* The min sched time of 10e-1 causes erroneous behavior... */
227   sm->timing_wheel.min_sched_time = 10e-2;
228   sm->timing_wheel.max_sched_time = 3600.0*48.0;
229   timing_wheel_init (&sm->timing_wheel, cpu_time_now, vm->clib_time.clocks_per_second);
230   sm->timer_wheel_next_expiring_time = 0;
231   sm->timer_wheel_tick = time_sec_to_clock(ct, sm->timing_wheel.min_sched_time);
232   /* Pre-allocate expired nodes. */
233   vec_alloc (sm->data_from_advancing_timing_wheel, 32);
234
235   l2sess_setup_nodes();
236   l2output_init_output_node_vec (&sm->output_next_nodes.output_node_index_vec);
237
238   return error;
239 }
240
241 VLIB_INIT_FUNCTION (l2sess_init);
242
243