dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / src / vnet / l2tp / l2tp.c
1 /*
2  * l2tp.c : L2TPv3 tunnel support
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
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 <vppinfra/error.h>
19 #include <vppinfra/hash.h>
20 #include <vnet/vnet.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/ethernet/ethernet.h>
24 #include <vnet/l2tp/l2tp.h>
25
26 l2t_main_t l2t_main;
27
28 /* packet trace format function */
29 u8 *
30 format_l2t_trace (u8 * s, va_list * args)
31 {
32   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34   l2t_trace_t *t = va_arg (*args, l2t_trace_t *);
35
36   if (t->is_user_to_network)
37     s = format (s, "L2T: %U (client) -> %U (our) session %d",
38                 format_ip6_address, &t->client_address,
39                 format_ip6_address, &t->our_address, t->session_index);
40   else
41     s = format (s, "L2T: %U (our) -> %U (client) session %d)",
42                 format_ip6_address, &t->our_address,
43                 format_ip6_address, &t->client_address, t->session_index);
44   return s;
45 }
46
47 u8 *
48 format_l2t_session (u8 * s, va_list * args)
49 {
50   l2t_session_t *session = va_arg (*args, l2t_session_t *);
51   l2t_main_t *lm = &l2t_main;
52   u32 counter_index;
53   vlib_counter_t v;
54
55   s = format (s, "[%d] %U (our) %U (client) %U (sw_if_index %d)\n",
56               session - lm->sessions,
57               format_ip6_address, &session->our_address,
58               format_ip6_address, &session->client_address,
59               format_vnet_sw_interface_name, lm->vnet_main,
60               vnet_get_sw_interface (lm->vnet_main, session->sw_if_index),
61               session->sw_if_index);
62
63   s = format (s, "   local cookies %016llx %016llx remote cookie %016llx\n",
64               clib_net_to_host_u64 (session->local_cookie[0]),
65               clib_net_to_host_u64 (session->local_cookie[1]),
66               clib_net_to_host_u64 (session->remote_cookie));
67
68   s = format (s, "   local session-id %d remote session-id %d\n",
69               clib_net_to_host_u32 (session->local_session_id),
70               clib_net_to_host_u32 (session->remote_session_id));
71
72   s = format (s, "   l2 specific sublayer %s\n",
73               session->l2_sublayer_present ? "preset" : "absent");
74
75   counter_index =
76     session_index_to_counter_index (session - lm->sessions,
77                                     SESSION_COUNTER_USER_TO_NETWORK);
78
79   vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
80   if (v.packets != 0)
81     s = format (s, "   user-to-net: %llu pkts %llu bytes\n",
82                 v.packets, v.bytes);
83
84   vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);
85
86   if (v.packets != 0)
87     s = format (s, "   net-to-user: %llu pkts %llu bytes\n",
88                 v.packets, v.bytes);
89   return s;
90 }
91
92 static clib_error_t *
93 show_l2tp_command_fn (vlib_main_t * vm,
94                       unformat_input_t * input, vlib_cli_command_t * cmd)
95 {
96   l2t_session_t *session;
97   l2t_main_t *lm = &l2t_main;
98   char *keystr = 0;
99   int verbose = 0;
100
101   if (unformat (input, "verbose") || unformat (input, "v"))
102     verbose = 1;
103
104   if (pool_elts (lm->sessions) == 0)
105     vlib_cli_output (vm, "No l2tp sessions...");
106   else
107     vlib_cli_output (vm, "%u l2tp sessions...", pool_elts (lm->sessions));
108
109   if (verbose)
110     {
111       switch (lm->lookup_type)
112         {
113         case L2T_LOOKUP_SRC_ADDRESS:
114           keystr = "src address";
115           break;
116
117         case L2T_LOOKUP_DST_ADDRESS:
118           keystr = "dst address";
119           break;
120
121         case L2T_LOOKUP_SESSION_ID:
122           keystr = "session id";
123           break;
124
125         default:
126           keystr = "BOGUS!";
127           break;
128         }
129
130       vlib_cli_output (vm, "L2tp session lookup on %s", keystr);
131
132       /* *INDENT-OFF* */
133       pool_foreach (session, lm->sessions,
134       ({
135         vlib_cli_output (vm, "%U", format_l2t_session, session);
136       }));
137       /* *INDENT-ON* */
138     }
139
140   return 0;
141 }
142
143 /* *INDENT-OFF* */
144 VLIB_CLI_COMMAND (show_session_detail_command, static) = {
145   .path = "show l2tpv3",
146   .short_help = "show l2tpv3 [verbose]",
147   .function = show_l2tp_command_fn,
148 };
149 /* *INDENT-ON* */
150
151 static clib_error_t *
152 test_counters_command_fn (vlib_main_t * vm,
153                           unformat_input_t * input, vlib_cli_command_t * cmd)
154 {
155   l2t_session_t *session;
156   l2t_main_t *lm = &l2t_main;
157   u32 session_index;
158   u32 counter_index;
159   u32 nincr = 0;
160   u32 cpu_index = os_get_cpu_number ();
161
162   /* *INDENT-OFF* */
163   pool_foreach (session, lm->sessions,
164   ({
165     session_index = session - lm->sessions;
166     counter_index =
167       session_index_to_counter_index (session_index,
168                                       SESSION_COUNTER_USER_TO_NETWORK);
169     vlib_increment_combined_counter (&lm->counter_main,
170                                      cpu_index,
171                                      counter_index,
172                                      1/*pkt*/, 1111 /*bytes*/);
173     vlib_increment_combined_counter (&lm->counter_main,
174                                      cpu_index,
175                                      counter_index+1,
176                                      1/*pkt*/, 2222 /*bytes*/);
177     nincr++;
178
179   }));
180   /* *INDENT-ON* */
181   vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
182
183   return 0;
184 }
185
186 /* *INDENT-OFF* */
187 VLIB_CLI_COMMAND (test_counters_command, static) = {
188     .path = "test counters",
189     .short_help = "increment all active counters",
190     .function = test_counters_command_fn,
191 };
192 /* *INDENT-ON* */
193
194 static clib_error_t *
195 clear_counters_command_fn (vlib_main_t * vm,
196                            unformat_input_t * input, vlib_cli_command_t * cmd)
197 {
198   l2t_session_t *session;
199   l2t_main_t *lm = &l2t_main;
200   u32 session_index;
201   u32 counter_index;
202   u32 nincr = 0;
203
204   /* *INDENT-OFF* */
205   pool_foreach (session, lm->sessions,
206   ({
207     session_index = session - lm->sessions;
208     counter_index =
209       session_index_to_counter_index (session_index,
210                                       SESSION_COUNTER_USER_TO_NETWORK);
211     vlib_zero_combined_counter (&lm->counter_main, counter_index);
212     vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
213     nincr++;
214   }));
215   /* *INDENT-ON* */
216   vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
217
218   return 0;
219 }
220
221 /* *INDENT-OFF* */
222 VLIB_CLI_COMMAND (clear_counters_command, static) = {
223   .path = "clear counters",
224   .short_help = "clear all active counters",
225   .function = clear_counters_command_fn,
226 };
227 /* *INDENT-ON* */
228
229 static u8 *
230 format_l2tpv3_name (u8 * s, va_list * args)
231 {
232   l2t_main_t *lm = &l2t_main;
233   u32 i = va_arg (*args, u32);
234   u32 show_dev_instance = ~0;
235
236   if (i < vec_len (lm->dev_inst_by_real))
237     show_dev_instance = lm->dev_inst_by_real[i];
238
239   if (show_dev_instance != ~0)
240     i = show_dev_instance;
241
242   return format (s, "l2tpv3_tunnel%d", i);
243 }
244
245 static int
246 l2tpv3_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
247 {
248   l2t_main_t *lm = &l2t_main;
249
250   vec_validate_init_empty (lm->dev_inst_by_real, hi->dev_instance, ~0);
251
252   lm->dev_inst_by_real[hi->dev_instance] = new_dev_instance;
253
254   return 0;
255 }
256
257 static uword
258 dummy_interface_tx (vlib_main_t * vm,
259                     vlib_node_runtime_t * node, vlib_frame_t * frame)
260 {
261   clib_warning ("you shouldn't be here, leaking buffers...");
262   return frame->n_vectors;
263 }
264
265 /* *INDENT-OFF* */
266 VNET_DEVICE_CLASS (l2tpv3_device_class,static) = {
267   .name = "L2TPv3",
268   .format_device_name = format_l2tpv3_name,
269   .name_renumber = l2tpv3_name_renumber,
270   .tx_function = dummy_interface_tx,
271 };
272 /* *INDENT-ON* */
273
274 static u8 *
275 format_l2tp_header_with_length (u8 * s, va_list * args)
276 {
277   u32 dev_instance = va_arg (*args, u32);
278   s = format (s, "unimplemented dev %u", dev_instance);
279   return s;
280 }
281
282 /* *INDENT-OFF* */
283 VNET_HW_INTERFACE_CLASS (l2tpv3_hw_class) = {
284   .name = "L2TPV3",
285   .format_header = format_l2tp_header_with_length,
286   .build_rewrite = default_build_rewrite,
287   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
288 };
289 /* *INDENT-ON* */
290
291 int
292 create_l2tpv3_ipv6_tunnel (l2t_main_t * lm,
293                            ip6_address_t * client_address,
294                            ip6_address_t * our_address,
295                            u32 local_session_id,
296                            u32 remote_session_id,
297                            u64 local_cookie,
298                            u64 remote_cookie,
299                            int l2_sublayer_present,
300                            u32 encap_fib_index, u32 * sw_if_index)
301 {
302   l2t_session_t *s = 0;
303   vnet_main_t *vnm = lm->vnet_main;
304   vnet_hw_interface_t *hi;
305   uword *p = (uword *) ~ 0;
306   u32 hw_if_index;
307   l2tpv3_header_t l2tp_hdr;
308   ip6_address_t *dst_address_copy, *src_address_copy;
309   u32 counter_index;
310
311   remote_session_id = clib_host_to_net_u32 (remote_session_id);
312   local_session_id = clib_host_to_net_u32 (local_session_id);
313
314   switch (lm->lookup_type)
315     {
316     case L2T_LOOKUP_SRC_ADDRESS:
317       p = hash_get_mem (lm->session_by_src_address, client_address);
318       break;
319
320     case L2T_LOOKUP_DST_ADDRESS:
321       p = hash_get_mem (lm->session_by_dst_address, our_address);
322       break;
323
324     case L2T_LOOKUP_SESSION_ID:
325       p = hash_get (lm->session_by_session_id, local_session_id);
326       break;
327
328     default:
329       ASSERT (0);
330     }
331
332   /* adding a session: session must not already exist */
333   if (p)
334     return VNET_API_ERROR_INVALID_VALUE;
335
336   pool_get (lm->sessions, s);
337   memset (s, 0, sizeof (*s));
338   clib_memcpy (&s->our_address, our_address, sizeof (s->our_address));
339   clib_memcpy (&s->client_address, client_address,
340                sizeof (s->client_address));
341   s->local_cookie[0] = clib_host_to_net_u64 (local_cookie);
342   s->remote_cookie = clib_host_to_net_u64 (remote_cookie);
343   s->local_session_id = local_session_id;
344   s->remote_session_id = remote_session_id;
345   s->l2_sublayer_present = l2_sublayer_present;
346   /* precompute l2tp header size */
347   s->l2tp_hdr_size = l2_sublayer_present ?
348     sizeof (l2tpv3_header_t) :
349     sizeof (l2tpv3_header_t) - sizeof (l2tp_hdr.l2_specific_sublayer);
350   s->admin_up = 0;
351   s->encap_fib_index = encap_fib_index;
352
353   /* Setup hash table entries */
354   switch (lm->lookup_type)
355     {
356     case L2T_LOOKUP_SRC_ADDRESS:
357       src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
358       clib_memcpy (src_address_copy, client_address,
359                    sizeof (*src_address_copy));
360       hash_set_mem (lm->session_by_src_address, src_address_copy,
361                     s - lm->sessions);
362       break;
363     case L2T_LOOKUP_DST_ADDRESS:
364       dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
365       clib_memcpy (dst_address_copy, our_address, sizeof (*dst_address_copy));
366       hash_set_mem (lm->session_by_dst_address, dst_address_copy,
367                     s - lm->sessions);
368       break;
369     case L2T_LOOKUP_SESSION_ID:
370       hash_set (lm->session_by_session_id, local_session_id,
371                 s - lm->sessions);
372       break;
373
374     default:
375       ASSERT (0);
376     }
377
378   /* validate counters */
379   counter_index =
380     session_index_to_counter_index (s - lm->sessions,
381                                     SESSION_COUNTER_USER_TO_NETWORK);
382   vlib_validate_combined_counter (&lm->counter_main, counter_index);
383   vlib_validate_combined_counter (&lm->counter_main, counter_index + 1);
384
385   if (vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) > 0)
386     {
387       hw_if_index = lm->free_l2tpv3_tunnel_hw_if_indices
388         [vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) - 1];
389       _vec_len (lm->free_l2tpv3_tunnel_hw_if_indices) -= 1;
390
391       hi = vnet_get_hw_interface (vnm, hw_if_index);
392       hi->dev_instance = s - lm->sessions;
393       hi->hw_instance = hi->dev_instance;
394     }
395   else
396     {
397       hw_if_index = vnet_register_interface
398         (vnm, l2tpv3_device_class.index, s - lm->sessions,
399          l2tpv3_hw_class.index, s - lm->sessions);
400       hi = vnet_get_hw_interface (vnm, hw_if_index);
401       hi->output_node_index = l2t_encap_node.index;
402       /* $$$$ initialize custom dispositions, if needed */
403     }
404
405   s->hw_if_index = hw_if_index;
406   s->sw_if_index = hi->sw_if_index;
407
408   if (sw_if_index)
409     *sw_if_index = hi->sw_if_index;
410
411   return 0;
412 }
413
414 static clib_error_t *
415 create_l2tpv3_tunnel_command_fn (vlib_main_t * vm,
416                                  unformat_input_t * input,
417                                  vlib_cli_command_t * cmd)
418 {
419   ip6_address_t client_address, our_address;
420   unformat_input_t _line_input, *line_input = &_line_input;
421   l2t_main_t *lm = &l2t_main;
422   u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
423   u32 local_session_id = 1, remote_session_id = 1;
424   int our_address_set = 0, client_address_set = 0;
425   int l2_sublayer_present = 0;
426   int rv;
427   u32 sw_if_index;
428   u32 encap_fib_id = ~0;
429   u32 encap_fib_index = ~0;
430
431   /* Get a line of input. */
432   if (!unformat_user (input, unformat_line_input, line_input))
433     return 0;
434
435   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
436     {
437       if (unformat (line_input, "client %U",
438                     unformat_ip6_address, &client_address))
439         client_address_set = 1;
440       else if (unformat (line_input, "our %U",
441                          unformat_ip6_address, &our_address))
442         our_address_set = 1;
443       else if (unformat (line_input, "local-cookie %llx", &local_cookie))
444         ;
445       else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
446         ;
447       else if (unformat (line_input, "local-session-id %d",
448                          &local_session_id))
449         ;
450       else if (unformat (line_input, "remote-session-id %d",
451                          &remote_session_id))
452         ;
453       else if (unformat (line_input, "fib-id %d", &encap_fib_id))
454         ;
455       else if (unformat (line_input, "l2-sublayer-present"))
456         l2_sublayer_present = 1;
457       else
458         return clib_error_return (0, "parse error: '%U'",
459                                   format_unformat_error, line_input);
460     }
461
462   unformat_free (line_input);
463
464   if (encap_fib_id != ~0)
465     {
466       uword *p;
467       ip6_main_t *im = &ip6_main;
468       if (!(p = hash_get (im->fib_index_by_table_id, encap_fib_id)))
469         return clib_error_return (0, "No fib with id %d", encap_fib_id);
470       encap_fib_index = p[0];
471     }
472   else
473     {
474       encap_fib_index = ~0;
475     }
476
477   if (our_address_set == 0)
478     return clib_error_return (0, "our address not specified");
479   if (client_address_set == 0)
480     return clib_error_return (0, "client address not specified");
481
482   rv = create_l2tpv3_ipv6_tunnel (lm, &client_address, &our_address,
483                                   local_session_id, remote_session_id,
484                                   local_cookie, remote_cookie,
485                                   l2_sublayer_present,
486                                   encap_fib_index, &sw_if_index);
487   switch (rv)
488     {
489     case 0:
490       vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
491                        vnet_get_main (), sw_if_index);
492       break;
493     case VNET_API_ERROR_INVALID_VALUE:
494       return clib_error_return (0, "session already exists...");
495
496     case VNET_API_ERROR_NO_SUCH_ENTRY:
497       return clib_error_return (0, "session does not exist...");
498
499     default:
500       return clib_error_return (0, "l2tp_session_add_del returned %d", rv);
501     }
502
503   return 0;
504 }
505
506 /* *INDENT-OFF* */
507 VLIB_CLI_COMMAND (create_l2tpv3_tunnel_command, static) =
508 {
509   .path = "create l2tpv3 tunnel",
510   .short_help =
511   "create l2tpv3 tunnel client <ip6> our <ip6> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec>",
512   .function = create_l2tpv3_tunnel_command_fn,
513 };
514 /* *INDENT-ON* */
515
516 int
517 l2tpv3_set_tunnel_cookies (l2t_main_t * lm,
518                            u32 sw_if_index,
519                            u64 new_local_cookie, u64 new_remote_cookie)
520 {
521   l2t_session_t *s;
522   vnet_hw_interface_t *hi;
523   vnet_main_t *vnm = vnet_get_main ();
524   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
525
526   if (pool_is_free_index (lm->sessions, hi->dev_instance))
527     return VNET_API_ERROR_INVALID_VALUE;
528
529   s = pool_elt_at_index (lm->sessions, hi->dev_instance);
530
531   s->local_cookie[1] = s->local_cookie[0];
532   s->local_cookie[0] = clib_host_to_net_u64 (new_local_cookie);
533   s->remote_cookie = clib_host_to_net_u64 (new_remote_cookie);
534
535   return 0;
536 }
537
538
539 static clib_error_t *
540 set_l2tp_tunnel_cookie_command_fn (vlib_main_t * vm,
541                                    unformat_input_t * input,
542                                    vlib_cli_command_t * cmd)
543 {
544   l2t_main_t *lm = &l2t_main;
545   vnet_main_t *vnm = vnet_get_main ();
546   u32 sw_if_index = ~0;
547   u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
548
549   int rv;
550
551   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
552     {
553       if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
554                     &sw_if_index))
555         ;
556       else if (unformat (input, "local %llx", &local_cookie))
557         ;
558       else if (unformat (input, "remote %llx", &remote_cookie))
559         ;
560       else
561         break;
562     }
563   if (sw_if_index == ~0)
564     return clib_error_return (0, "unknown interface");
565   if (local_cookie == ~0)
566     return clib_error_return (0, "local cookie required");
567   if (remote_cookie == ~0)
568     return clib_error_return (0, "remote cookie required");
569
570   rv = l2tpv3_set_tunnel_cookies (lm, sw_if_index,
571                                   local_cookie, remote_cookie);
572
573   switch (rv)
574     {
575     case 0:
576       break;
577
578     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
579       return clib_error_return (0, "invalid interface");
580
581     default:
582       return clib_error_return (0, "l2tp_session_set_cookies returned %d",
583                                 rv);
584     }
585
586   return 0;
587 }
588
589 /* *INDENT-OFF* */
590 VLIB_CLI_COMMAND (set_l2tp_tunnel_cookie_command, static) =
591 {
592   .path = "set l2tpv3 tunnel cookie",
593   .short_help =
594   "set l2tpv3 tunnel cookie <intfc> local <hex> remote <hex>",
595   .function = set_l2tp_tunnel_cookie_command_fn,
596 };
597 /* *INDENT-ON* */
598
599 int
600 l2tpv3_interface_enable_disable (vnet_main_t * vnm,
601                                  u32 sw_if_index, int enable_disable)
602 {
603
604   if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
605     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
606
607   vnet_feature_enable_disable ("ip6-unicast", "l2tp-decap", sw_if_index,
608                                enable_disable, 0, 0);
609   return 0;
610 }
611
612 /* Enable/disable L2TPv3 intercept on IP6 fowarding path */
613 static clib_error_t *
614 set_ip6_l2tpv3 (vlib_main_t * vm,
615                 unformat_input_t * input, vlib_cli_command_t * cmd)
616 {
617   u32 sw_if_index = ~0;
618   int is_add = 1;
619   int rv;
620   vnet_main_t *vnm = vnet_get_main ();
621
622   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
623     {
624       if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
625                     &sw_if_index))
626         ;
627       else if (unformat (input, "del"))
628         is_add = 0;
629       else
630         break;
631     }
632
633   if (sw_if_index == ~0)
634     return clib_error_return (0, "interface required");
635
636   rv = l2tpv3_interface_enable_disable (vnm, sw_if_index, is_add);
637
638   switch (rv)
639     {
640     case 0:
641       break;
642
643     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
644       return clib_error_return (0, "invalid interface");
645
646     default:
647       return clib_error_return (0,
648                                 "l2tp_interface_enable_disable returned %d",
649                                 rv);
650     }
651   return 0;
652 }
653
654 /* *INDENT-OFF* */
655 VLIB_CLI_COMMAND (set_interface_ip6_l2tpv3, static) =
656 {
657   .path = "set interface ip6 l2tpv3",
658   .function = set_ip6_l2tpv3,
659   .short_help = "set interface ip6 l2tpv3 <intfc> [del]",
660 };
661 /* *INDENT-ON* */
662
663 static clib_error_t *
664 l2tp_config (vlib_main_t * vm, unformat_input_t * input)
665 {
666   l2t_main_t *lm = &l2t_main;
667
668   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
669     {
670       if (unformat (input, "lookup-v6-src"))
671         lm->lookup_type = L2T_LOOKUP_SRC_ADDRESS;
672       else if (unformat (input, "lookup-v6-dst"))
673         lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
674       else if (unformat (input, "lookup-session-id"))
675         lm->lookup_type = L2T_LOOKUP_SESSION_ID;
676       else
677         return clib_error_return (0, "unknown input `%U'",
678                                   format_unformat_error, input);
679     }
680   return 0;
681 }
682
683 VLIB_CONFIG_FUNCTION (l2tp_config, "l2tp");
684
685
686 clib_error_t *
687 l2tp_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
688 {
689   l2t_main_t *lm = &l2t_main;
690   vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
691   if (hi->hw_class_index != l2tpv3_hw_class.index)
692     return 0;
693
694   u32 session_index = hi->dev_instance;
695   l2t_session_t *s = pool_elt_at_index (lm->sessions, session_index);
696   s->admin_up = ! !(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
697   return 0;
698 }
699
700 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2tp_sw_interface_up_down);
701
702 clib_error_t *
703 l2tp_init (vlib_main_t * vm)
704 {
705   l2t_main_t *lm = &l2t_main;
706   ip_main_t *im = &ip_main;
707   ip_protocol_info_t *pi;
708
709   lm->vnet_main = vnet_get_main ();
710   lm->vlib_main = vm;
711   lm->lookup_type = L2T_LOOKUP_DST_ADDRESS;
712
713   lm->session_by_src_address = hash_create_mem
714     (0, sizeof (ip6_address_t) /* key bytes */ ,
715      sizeof (u32) /* value bytes */ );
716   lm->session_by_dst_address = hash_create_mem
717     (0, sizeof (ip6_address_t) /* key bytes */ ,
718      sizeof (u32) /* value bytes */ );
719   lm->session_by_session_id = hash_create (0, sizeof (uword));
720
721   pi = ip_get_protocol_info (im, IP_PROTOCOL_L2TP);
722   pi->unformat_pg_edit = unformat_pg_l2tp_header;
723
724   /* insure these nodes are included in build */
725   l2tp_encap_init (vm);
726   l2tp_decap_init ();
727
728   return 0;
729 }
730
731 VLIB_INIT_FUNCTION (l2tp_init);
732
733 /*
734  * fd.io coding-style-patch-verification: ON
735  *
736  * Local Variables:
737  * eval: (c-set-style "gnu")
738  * End:
739  */