45dd2807834232a96fafdf80ef99b1c51ecbc152
[vpp.git] / vpp / app / l2t.c
1 /*
2  * Copyright (c) 2015 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 #include <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/ethernet/ethernet.h>
18
19 #if DPDK == 0
20 #include <vnet/devices/pci/ixge.h>
21 #else
22 #include <vnet/devices/dpdk/dpdk.h>
23 #endif
24
25 #include <vppinfra/error.h>
26 #include <vppinfra/hash.h>
27 #include <app/l2t.h>
28
29 l2t_main_t l2t_main;
30
31 /* $$$$ unused?
32  * get_interface_ethernet_address
33  * paints the ethernet address for a given interface
34  * into the supplied destination
35  */
36 void
37 get_interface_ethernet_address (l2t_main_t * lm, u8 * dst, u32 sw_if_index)
38 {
39   ethernet_main_t *em = ethernet_get_main (lm->vlib_main);
40   ethernet_interface_t *ei;
41   vnet_hw_interface_t *hi;
42
43   hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
44   ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
45   clib_memcpy (dst, ei->address, sizeof (ei->address));
46 }
47
48 /* packet trace format function */
49 u8 *
50 format_l2t_trace (u8 * s, va_list * args)
51 {
52   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54   l2t_trace_t *t = va_arg (*args, l2t_trace_t *);
55
56   if (t->is_user_to_network)
57     s = format (s, "L2T: %U (client) -> %U (our) session %d",
58                 format_ip6_address, &t->client_address,
59                 format_ip6_address, &t->our_address, t->session_index);
60   else
61     s = format (s, "L2T: %U (our) -> %U (client) session %d)",
62                 format_ip6_address, &t->our_address,
63                 format_ip6_address, &t->client_address, t->session_index);
64   return s;
65 }
66
67 u8 *
68 format_l2t_session (u8 * s, va_list * args)
69 {
70   l2t_session_t *session = va_arg (*args, l2t_session_t *);
71   l2t_main_t *lm = &l2t_main;
72   u32 counter_index;
73   vlib_counter_t v;
74
75   s = format (s, "[%d] %U (our) %U (client) vlan-id %d rx_sw_if_index %d\n",
76               session - lm->sessions,
77               format_ip6_address, &session->our_address,
78               format_ip6_address, &session->client_address,
79               clib_net_to_host_u16 (session->vlan_id), session->sw_if_index);
80
81   s = format (s, "   local cookie %llx remote cookie %llx\n",
82               clib_net_to_host_u64 (session->local_cookie),
83               clib_net_to_host_u64 (session->remote_cookie));
84
85   if (session->cookie_flags & L2TP_COOKIE_ROLLOVER_LOCAL)
86     {
87       s = format (s, "   local rollover cookie %llx\n",
88                   clib_net_to_host_u64 (session->lcl_ro_cookie));
89     }
90
91   s = format (s, "   local session-id %d remote session-id %d\n",
92               clib_net_to_host_u32 (session->local_session_id),
93               clib_net_to_host_u32 (session->remote_session_id));
94
95   s = format (s, "   l2 specific sublayer %s\n",
96               session->l2_sublayer_present ? "preset" : "absent");
97
98   counter_index =
99     session_index_to_counter_index (session - lm->sessions,
100                                     SESSION_COUNTER_USER_TO_NETWORK);
101
102   vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
103   if (v.packets != 0)
104     s = format (s, "   user-to-net: %llu pkts %llu bytes\n",
105                 v.packets, v.bytes);
106
107   vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);
108
109   if (v.packets != 0)
110     s = format (s, "   net-to-user: %llu pkts %llu bytes\n",
111                 v.packets, v.bytes);
112   return s;
113 }
114
115 static clib_error_t *
116 show_session_summary_command_fn (vlib_main_t * vm,
117                                  unformat_input_t * input,
118                                  vlib_cli_command_t * cmd)
119 {
120   l2t_main_t *lm = &l2t_main;
121
122   vlib_cli_output (vm, "%d active sessions\n", pool_elts (lm->sessions));
123
124   return 0;
125 }
126
127 /* *INDENT-OFF* */
128 static VLIB_CLI_COMMAND (show_session_summary_command) = {
129   .path = "show session",
130   .short_help = "show session summary",
131   .function = show_session_summary_command_fn,
132 };
133 /* *INDENT-ON* */
134
135 static clib_error_t *
136 show_session_detail_command_fn (vlib_main_t * vm,
137                                 unformat_input_t * input,
138                                 vlib_cli_command_t * cmd)
139 {
140   l2t_session_t *session;
141   l2t_main_t *lm = &l2t_main;
142
143   /* *INDENT-OFF* */
144   pool_foreach (session, lm->sessions,
145   ({
146     vlib_cli_output (vm, "%U", format_l2t_session, session);
147   }));
148   /* *INDENT-ON* */
149
150   return 0;
151 }
152
153 /* *INDENT-OFF* */
154 static VLIB_CLI_COMMAND (show_session_detail_command) = {
155   .path = "show session detail",
156   .short_help = "show session table detail",
157   .function = show_session_detail_command_fn,
158 };
159 /* *INDENT-ON* */
160
161 static clib_error_t *
162 test_counters_command_fn (vlib_main_t * vm,
163                           unformat_input_t * input, vlib_cli_command_t * cmd)
164 {
165   l2t_session_t *session;
166   l2t_main_t *lm = &l2t_main;
167   u32 session_index;
168   u32 counter_index;
169   u32 nincr = 0;
170
171   /* *INDENT-OFF* */
172   pool_foreach (session, lm->sessions,
173   ({
174     session_index = session - lm->sessions;
175     counter_index =
176       session_index_to_counter_index (session_index,
177                                       SESSION_COUNTER_USER_TO_NETWORK);
178     vlib_increment_combined_counter (&lm->counter_main,
179                                      counter_index,
180                                      1/*pkt*/, 1111 /*bytes*/);
181     vlib_increment_combined_counter (&lm->counter_main,
182                                      counter_index+1,
183                                      1/*pkt*/, 2222 /*bytes*/);
184     nincr++;
185   }));
186   /* *INDENT-ON* */
187   vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
188
189   return 0;
190 }
191
192 /* *INDENT-OFF* */
193 static VLIB_CLI_COMMAND (test_counters_command) = {
194   .path = "test counters",
195   .short_help = "increment all active counters",
196   .function = test_counters_command_fn,
197 };
198 /* *INDENT-ON* */
199
200 static clib_error_t *
201 clear_counters_command_fn (vlib_main_t * vm,
202                            unformat_input_t * input, vlib_cli_command_t * cmd)
203 {
204   l2t_session_t *session;
205   l2t_main_t *lm = &l2t_main;
206   u32 session_index;
207   u32 counter_index;
208   u32 nincr = 0;
209
210   /* *INDENT-OFF* */
211   pool_foreach (session, lm->sessions,
212   ({
213     session_index = session - lm->sessions;
214     counter_index =
215       session_index_to_counter_index (session_index,
216                                       SESSION_COUNTER_USER_TO_NETWORK);
217     vlib_zero_combined_counter (&lm->counter_main, counter_index);
218     vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
219     nincr++;
220   }));
221   /* *INDENT-ON* */
222   vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
223
224   return 0;
225 }
226
227 /* *INDENT-OFF* */
228 static VLIB_CLI_COMMAND (clear_counters_command) = {
229   .path = "clear counters",
230   .short_help = "clear all active counters",
231   .function = clear_counters_command_fn,
232 };
233 /* *INDENT-ON* */
234
235 static clib_error_t *
236 l2tp_session_add_command_fn (vlib_main_t * vm,
237                              unformat_input_t * input,
238                              vlib_cli_command_t * cmd)
239 {
240   ip6_address_t client_address, our_address;
241   ip6_address_t *dst_address_copy, *src_address_copy;
242   unformat_input_t _line_input, *line_input = &_line_input;
243   u32 vlan_id;
244   u32 sw_if_index = (u32) ~ 0;
245   l2t_main_t *lm = &l2t_main;
246   l2t_session_t *s;
247   uword *p;
248   vnet_hw_interface_t *hi;
249   vnet_sw_interface_t *si;
250   u32 next_index;
251   uword vlan_and_sw_if_index_key;
252   u32 counter_index;
253   u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
254   u32 local_session_id = 1, remote_session_id = 1;
255   int our_address_set = 0, client_address_set = 0;
256   int l2_sublayer_present = 0;
257
258   /* Get a line of input. */
259   if (!unformat_user (input, unformat_line_input, line_input))
260     return 0;
261
262   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
263     {
264       if (unformat (line_input, "client %U",
265                     unformat_ip6_address, &client_address))
266         client_address_set = 1;
267       else if (unformat (line_input, "our %U",
268                          unformat_ip6_address, &our_address))
269         our_address_set = 1;
270       else if (unformat (line_input, "vlan %d", &vlan_id))
271         ;
272       else if (unformat (line_input, "l2-interface %U",
273                          unformat_vnet_sw_interface,
274                          vnet_get_main (), &sw_if_index))
275         ;
276       else if (unformat (line_input, "interface %U",
277                          unformat_vnet_sw_interface,
278                          vnet_get_main (), &sw_if_index))
279         ;
280       else if (unformat (line_input, "local-cookie %llx", &local_cookie))
281         ;
282       else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
283         ;
284       else if (unformat (line_input, "local-session-id %d",
285                          &local_session_id))
286         ;
287       else if (unformat (line_input, "remote-session-id %d",
288                          &remote_session_id))
289         ;
290       else if (unformat (line_input, "l2-sublayer-present"))
291         l2_sublayer_present = 1;
292       else
293         return clib_error_return (0, "parse error: '%U'",
294                                   format_unformat_error, line_input);
295     }
296
297   unformat_free (line_input);
298
299   if (sw_if_index == (u32) ~ 0)
300     return clib_error_return (0, "l2-interface not specified");
301   if (our_address_set == 0)
302     return clib_error_return (0, "our address not specified");
303   if (client_address_set == 0)
304     return clib_error_return (0, "client address not specified");
305
306   remote_session_id = clib_host_to_net_u32 (remote_session_id);
307   local_session_id = clib_host_to_net_u32 (local_session_id);
308
309   switch (lm->lookup_type)
310     {
311     case L2T_LOOKUP_SRC_ADDRESS:
312       p = hash_get_mem (lm->session_by_src_address, &client_address);
313       if (p)
314         return clib_error_return
315           (0, "Session w/ client address %U already exists",
316            format_ip6_address, &client_address);
317       break;
318
319     case L2T_LOOKUP_DST_ADDRESS:
320       p = hash_get_mem (lm->session_by_dst_address, &our_address);
321       if (p)
322         return clib_error_return
323           (0, "Session w/ our address %U already exists",
324            format_ip6_address, &our_address);
325       break;
326
327     case L2T_LOOKUP_SESSION_ID:
328       p = hash_get (lm->session_by_session_id, local_session_id);
329       if (p)
330         return clib_error_return
331           (0,
332            "Session w/ local session id %d already exists",
333            clib_net_to_host_u32 (local_session_id));
334       break;
335
336     default:
337       ASSERT (0);
338     }
339
340   pool_get (lm->sessions, s);
341   memset (s, 0, sizeof (*s));
342   clib_memcpy (&s->our_address, &our_address, sizeof (s->our_address));
343   clib_memcpy (&s->client_address, &client_address,
344                sizeof (s->client_address));
345   s->sw_if_index = sw_if_index;
346   s->vlan_id = clib_host_to_net_u16 (vlan_id);
347   s->local_cookie = clib_host_to_net_u64 (local_cookie);
348   l2tp_session_set_remote_cookie (s, remote_cookie);
349   s->local_session_id = local_session_id;
350   s->remote_session_id = remote_session_id;
351   s->l2_sublayer_present = l2_sublayer_present;
352
353   hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
354   si = vnet_get_sup_sw_interface (lm->vnet_main, sw_if_index);
355
356   next_index = vlib_node_add_next (vm, l2t_ip6_node.index,
357                                    hi->output_node_index);
358   s->l2_output_next_index = next_index;
359   s->l2_output_sw_if_index = si->sw_if_index;
360
361   /* Setup hash table entries */
362   switch (lm->lookup_type)
363     {
364     case L2T_LOOKUP_SRC_ADDRESS:
365       src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
366       clib_memcpy (src_address_copy, &client_address,
367                    sizeof (*src_address_copy));
368       hash_set_mem (lm->session_by_src_address, src_address_copy,
369                     s - lm->sessions);
370       break;
371     case L2T_LOOKUP_DST_ADDRESS:
372       dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
373       clib_memcpy (dst_address_copy, &our_address,
374                    sizeof (*dst_address_copy));
375       hash_set_mem (lm->session_by_dst_address, dst_address_copy,
376                     s - lm->sessions);
377       break;
378     case L2T_LOOKUP_SESSION_ID:
379       hash_set (lm->session_by_session_id, local_session_id,
380                 s - lm->sessions);
381       break;
382
383     default:
384       ASSERT (0);
385     }
386
387   vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | sw_if_index;
388   hash_set (lm->session_by_vlan_and_rx_sw_if_index,
389             vlan_and_sw_if_index_key, s - lm->sessions);
390
391   /* validate counters */
392   counter_index =
393     session_index_to_counter_index (s - lm->sessions,
394                                     SESSION_COUNTER_USER_TO_NETWORK);
395   vlib_validate_counter (&lm->counter_main, counter_index);
396   vlib_validate_counter (&lm->counter_main, counter_index + 1);
397
398   /* Set promiscuous mode on the l2 interface */
399   ethernet_set_flags (lm->vnet_main, hi->hw_if_index,
400                       ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
401   vnet_hw_interface_rx_redirect_to_node (lm->vnet_main, hi->hw_if_index,
402                                          l2t_l2_node.index);
403   return 0;
404 }
405
406 /* *INDENT-OFF* */
407 static VLIB_CLI_COMMAND (l2tp_session_add_command) = {
408   .path = "l2tp session add",
409   .short_help =
410   "l2tp session add client <ip6> our <ip6> vlan <id> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec> l2-interface <int>",
411   .function = l2tp_session_add_command_fn,
412 };
413 /* *INDENT-ON* */
414
415 static clib_error_t *
416 l2tp_session_del_command_fn (vlib_main_t * vm,
417                              unformat_input_t * input,
418                              vlib_cli_command_t * cmd)
419 {
420   l2t_main_t *lm = &l2t_main;
421   u32 session_index;
422   l2t_session_t *s;
423   hash_pair_t *hp;
424   void *key;
425   uword vlan_and_sw_if_index_key;
426
427   if (!unformat (input, "%d", &session_index))
428     return clib_error_return (0, "missing session index: '%U'",
429                               format_unformat_error, input);
430
431   if (pool_is_free_index (lm->sessions, session_index))
432     return clib_error_return (0, "session %d not in use", session_index);
433
434   s = pool_elt_at_index (lm->sessions, session_index);
435
436   switch (lm->lookup_type)
437     {
438     case L2T_LOOKUP_SRC_ADDRESS:
439       hp = hash_get_pair_mem (lm->session_by_src_address, &s->client_address);
440       if (hp)
441         {
442           key = (void *) (hp->key);
443           hash_unset_mem (lm->session_by_src_address, &s->client_address);
444           clib_mem_free (key);
445         }
446       else
447         clib_warning ("session %d src address key %U AWOL",
448                       s - lm->sessions,
449                       format_ip6_address, &s->client_address);
450       break;
451
452     case L2T_LOOKUP_DST_ADDRESS:
453       hp = hash_get_pair_mem (lm->session_by_dst_address, &s->our_address);
454       if (hp)
455         {
456           key = (void *) (hp->key);
457           hash_unset_mem (lm->session_by_dst_address, &s->our_address);
458           clib_mem_free (key);
459         }
460       else
461         clib_warning ("session %d dst address key %U AWOL",
462                       s - lm->sessions, format_ip6_address, &s->our_address);
463       break;
464
465     case L2T_LOOKUP_SESSION_ID:
466       hash_unset (lm->session_by_session_id, s->local_session_id);
467       break;
468
469     default:
470       ASSERT (0);
471     }
472
473   vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | s->sw_if_index;
474
475   hash_unset (lm->session_by_vlan_and_rx_sw_if_index,
476               vlan_and_sw_if_index_key);
477
478   pool_put (lm->sessions, s);
479   return 0;
480 }
481
482 /* *INDENT-OFF* */
483 static VLIB_CLI_COMMAND (l2tp_session_del_command) = {
484   .path = "l2tp session delete",
485   .short_help =
486   "l2tp session delete <session-id>",
487   .function = l2tp_session_del_command_fn,
488 };
489 /* *INDENT-ON* */
490
491 static clib_error_t *
492 l2tp_session_cookie_command_fn (vlib_main_t * vm,
493                                 unformat_input_t * input,
494                                 vlib_cli_command_t * cmd)
495 {
496   l2t_main_t *lm = &l2t_main;
497   u32 session_index;
498   l2t_session_t *s;
499   u64 lcl_ro_cookie = (u64) ~ 0, rem_ro_cookie = (u64) ~ 0;
500   u8 cookie_flags = 0;
501
502   if (!unformat (input, "%d", &session_index))
503     return clib_error_return (0, "missing session index: '%U'",
504                               format_unformat_error, input);
505
506   if (pool_is_free_index (lm->sessions, session_index))
507     return clib_error_return (0, "session %d not in use", session_index);
508
509   s = pool_elt_at_index (lm->sessions, session_index);
510
511   if (unformat (input, "commit"))
512     {
513       if (!s->cookie_flags)
514         {
515           return clib_error_return (0, "no rollover cookie ready to commit");
516         }
517       else
518         {
519           l2tp_session_cookie_commit (s);
520           return 0;
521         }
522     }
523   if (!unformat (input, "rollover"))
524     return clib_error_return (0, "missing 'commit|rollover': '%U'",
525                               format_unformat_error, input);
526   if (unformat (input, "local %llx", &lcl_ro_cookie))
527     {
528       cookie_flags |= L2TP_COOKIE_ROLLOVER_LOCAL;
529       l2tp_session_set_local_rollover_cookie (s, lcl_ro_cookie);
530     }
531   if (unformat (input, "remote %llx", &rem_ro_cookie))
532     {
533       cookie_flags |= L2TP_COOKIE_ROLLOVER_REMOTE;
534       l2tp_session_set_remote_cookie (s, rem_ro_cookie);
535     }
536   if (!cookie_flags)
537     return clib_error_return (0, "no rollover cookie specified");
538
539   return 0;
540 }
541
542 /* *INDENT-OFF* */
543 static VLIB_CLI_COMMAND (l2tp_session_cookie_command) = {
544   .path = "l2tp session cookie",
545   .short_help =
546   "l2tp session cookie <session id> commit|rollover [local <hex>] [remote <hex>]",
547   .function = l2tp_session_cookie_command_fn,
548 };
549 /* *INDENT-ON* */
550
551 /*
552  * fd.io coding-style-patch-verification: ON
553  *
554  * Local Variables:
555  * eval: (c-set-style "gnu")
556  * End:
557  */