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