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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/devices/pci/ixge.h>
22 #include <vnet/devices/dpdk/dpdk.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/hash.h>
32 * get_interface_ethernet_address
33 * paints the ethernet address for a given interface
34 * into the supplied destination
37 get_interface_ethernet_address (l2t_main_t * lm, u8 * dst, u32 sw_if_index)
39 ethernet_main_t *em = ethernet_get_main (lm->vlib_main);
40 ethernet_interface_t *ei;
41 vnet_hw_interface_t *hi;
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));
48 /* packet trace format function */
50 format_l2t_trace (u8 * s, va_list * args)
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 *);
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);
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);
68 format_l2t_session (u8 * s, va_list * args)
70 l2t_session_t *session = va_arg (*args, l2t_session_t *);
71 l2t_main_t *lm = &l2t_main;
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);
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));
85 if (session->cookie_flags & L2TP_COOKIE_ROLLOVER_LOCAL)
87 s = format (s, " local rollover cookie %llx\n",
88 clib_net_to_host_u64 (session->lcl_ro_cookie));
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));
95 s = format (s, " l2 specific sublayer %s\n",
96 session->l2_sublayer_present ? "preset" : "absent");
99 session_index_to_counter_index (session - lm->sessions,
100 SESSION_COUNTER_USER_TO_NETWORK);
102 vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
104 s = format (s, " user-to-net: %llu pkts %llu bytes\n",
107 vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);
110 s = format (s, " net-to-user: %llu pkts %llu bytes\n",
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)
120 l2t_main_t *lm = &l2t_main;
122 vlib_cli_output (vm, "%d active sessions\n", pool_elts (lm->sessions));
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,
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)
140 l2t_session_t *session;
141 l2t_main_t *lm = &l2t_main;
144 pool_foreach (session, lm->sessions,
146 vlib_cli_output (vm, "%U", format_l2t_session, session);
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,
161 static clib_error_t *
162 test_counters_command_fn (vlib_main_t * vm,
163 unformat_input_t * input, vlib_cli_command_t * cmd)
165 l2t_session_t *session;
166 l2t_main_t *lm = &l2t_main;
172 pool_foreach (session, lm->sessions,
174 session_index = session - lm->sessions;
176 session_index_to_counter_index (session_index,
177 SESSION_COUNTER_USER_TO_NETWORK);
178 vlib_increment_combined_counter (&lm->counter_main,
180 1/*pkt*/, 1111 /*bytes*/);
181 vlib_increment_combined_counter (&lm->counter_main,
183 1/*pkt*/, 2222 /*bytes*/);
187 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
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,
200 static clib_error_t *
201 clear_counters_command_fn (vlib_main_t * vm,
202 unformat_input_t * input, vlib_cli_command_t * cmd)
204 l2t_session_t *session;
205 l2t_main_t *lm = &l2t_main;
211 pool_foreach (session, lm->sessions,
213 session_index = session - lm->sessions;
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);
222 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
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,
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)
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;
244 u32 sw_if_index = (u32) ~ 0;
245 l2t_main_t *lm = &l2t_main;
248 vnet_hw_interface_t *hi;
249 vnet_sw_interface_t *si;
251 uword vlan_and_sw_if_index_key;
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 clib_error_t *error = NULL;
259 /* Get a line of input. */
260 if (!unformat_user (input, unformat_line_input, line_input))
263 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
265 if (unformat (line_input, "client %U",
266 unformat_ip6_address, &client_address))
267 client_address_set = 1;
268 else if (unformat (line_input, "our %U",
269 unformat_ip6_address, &our_address))
271 else if (unformat (line_input, "vlan %d", &vlan_id))
273 else if (unformat (line_input, "l2-interface %U",
274 unformat_vnet_sw_interface,
275 vnet_get_main (), &sw_if_index))
277 else if (unformat (line_input, "interface %U",
278 unformat_vnet_sw_interface,
279 vnet_get_main (), &sw_if_index))
281 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
283 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
285 else if (unformat (line_input, "local-session-id %d",
288 else if (unformat (line_input, "remote-session-id %d",
291 else if (unformat (line_input, "l2-sublayer-present"))
292 l2_sublayer_present = 1;
295 error = clib_error_return (0, "parse error: '%U'",
296 format_unformat_error, line_input);
297 unformat_free (line_input);
302 unformat_free (line_input);
304 if (sw_if_index == (u32) ~ 0)
305 return clib_error_return (0, "l2-interface not specified");
306 if (our_address_set == 0)
307 return clib_error_return (0, "our address not specified");
308 if (client_address_set == 0)
309 return clib_error_return (0, "client address not specified");
311 remote_session_id = clib_host_to_net_u32 (remote_session_id);
312 local_session_id = clib_host_to_net_u32 (local_session_id);
314 switch (lm->lookup_type)
316 case L2T_LOOKUP_SRC_ADDRESS:
317 p = hash_get_mem (lm->session_by_src_address, &client_address);
319 return clib_error_return
320 (0, "Session w/ client address %U already exists",
321 format_ip6_address, &client_address);
324 case L2T_LOOKUP_DST_ADDRESS:
325 p = hash_get_mem (lm->session_by_dst_address, &our_address);
327 return clib_error_return
328 (0, "Session w/ our address %U already exists",
329 format_ip6_address, &our_address);
332 case L2T_LOOKUP_SESSION_ID:
333 p = hash_get (lm->session_by_session_id, local_session_id);
335 return clib_error_return
337 "Session w/ local session id %d already exists",
338 clib_net_to_host_u32 (local_session_id));
345 pool_get (lm->sessions, s);
346 memset (s, 0, sizeof (*s));
347 clib_memcpy (&s->our_address, &our_address, sizeof (s->our_address));
348 clib_memcpy (&s->client_address, &client_address,
349 sizeof (s->client_address));
350 s->sw_if_index = sw_if_index;
351 s->vlan_id = clib_host_to_net_u16 (vlan_id);
352 s->local_cookie = clib_host_to_net_u64 (local_cookie);
353 l2tp_session_set_remote_cookie (s, remote_cookie);
354 s->local_session_id = local_session_id;
355 s->remote_session_id = remote_session_id;
356 s->l2_sublayer_present = l2_sublayer_present;
358 hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
359 si = vnet_get_sup_sw_interface (lm->vnet_main, sw_if_index);
361 next_index = vlib_node_add_next (vm, l2t_ip6_node.index,
362 hi->output_node_index);
363 s->l2_output_next_index = next_index;
364 s->l2_output_sw_if_index = si->sw_if_index;
366 /* Setup hash table entries */
367 switch (lm->lookup_type)
369 case L2T_LOOKUP_SRC_ADDRESS:
370 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
371 clib_memcpy (src_address_copy, &client_address,
372 sizeof (*src_address_copy));
373 hash_set_mem (lm->session_by_src_address, src_address_copy,
376 case L2T_LOOKUP_DST_ADDRESS:
377 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
378 clib_memcpy (dst_address_copy, &our_address,
379 sizeof (*dst_address_copy));
380 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
383 case L2T_LOOKUP_SESSION_ID:
384 hash_set (lm->session_by_session_id, local_session_id,
392 vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | sw_if_index;
393 hash_set (lm->session_by_vlan_and_rx_sw_if_index,
394 vlan_and_sw_if_index_key, s - lm->sessions);
396 /* validate counters */
398 session_index_to_counter_index (s - lm->sessions,
399 SESSION_COUNTER_USER_TO_NETWORK);
400 vlib_validate_counter (&lm->counter_main, counter_index);
401 vlib_validate_counter (&lm->counter_main, counter_index + 1);
403 /* Set promiscuous mode on the l2 interface */
404 ethernet_set_flags (lm->vnet_main, hi->hw_if_index,
405 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
406 vnet_hw_interface_rx_redirect_to_node (lm->vnet_main, hi->hw_if_index,
412 static VLIB_CLI_COMMAND (l2tp_session_add_command) = {
413 .path = "l2tp session add",
415 "l2tp session add client <ip6> our <ip6> vlan <id> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec> l2-interface <int>",
416 .function = l2tp_session_add_command_fn,
420 static clib_error_t *
421 l2tp_session_del_command_fn (vlib_main_t * vm,
422 unformat_input_t * input,
423 vlib_cli_command_t * cmd)
425 l2t_main_t *lm = &l2t_main;
430 uword vlan_and_sw_if_index_key;
432 if (!unformat (input, "%d", &session_index))
433 return clib_error_return (0, "missing session index: '%U'",
434 format_unformat_error, input);
436 if (pool_is_free_index (lm->sessions, session_index))
437 return clib_error_return (0, "session %d not in use", session_index);
439 s = pool_elt_at_index (lm->sessions, session_index);
441 switch (lm->lookup_type)
443 case L2T_LOOKUP_SRC_ADDRESS:
444 hp = hash_get_pair_mem (lm->session_by_src_address, &s->client_address);
447 key = (void *) (hp->key);
448 hash_unset_mem (lm->session_by_src_address, &s->client_address);
452 clib_warning ("session %d src address key %U AWOL",
454 format_ip6_address, &s->client_address);
457 case L2T_LOOKUP_DST_ADDRESS:
458 hp = hash_get_pair_mem (lm->session_by_dst_address, &s->our_address);
461 key = (void *) (hp->key);
462 hash_unset_mem (lm->session_by_dst_address, &s->our_address);
466 clib_warning ("session %d dst address key %U AWOL",
467 s - lm->sessions, format_ip6_address, &s->our_address);
470 case L2T_LOOKUP_SESSION_ID:
471 hash_unset (lm->session_by_session_id, s->local_session_id);
478 vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | s->sw_if_index;
480 hash_unset (lm->session_by_vlan_and_rx_sw_if_index,
481 vlan_and_sw_if_index_key);
483 pool_put (lm->sessions, s);
488 static VLIB_CLI_COMMAND (l2tp_session_del_command) = {
489 .path = "l2tp session delete",
491 "l2tp session delete <session-id>",
492 .function = l2tp_session_del_command_fn,
496 static clib_error_t *
497 l2tp_session_cookie_command_fn (vlib_main_t * vm,
498 unformat_input_t * input,
499 vlib_cli_command_t * cmd)
501 l2t_main_t *lm = &l2t_main;
504 u64 lcl_ro_cookie = (u64) ~ 0, rem_ro_cookie = (u64) ~ 0;
507 if (!unformat (input, "%d", &session_index))
508 return clib_error_return (0, "missing session index: '%U'",
509 format_unformat_error, input);
511 if (pool_is_free_index (lm->sessions, session_index))
512 return clib_error_return (0, "session %d not in use", session_index);
514 s = pool_elt_at_index (lm->sessions, session_index);
516 if (unformat (input, "commit"))
518 if (!s->cookie_flags)
520 return clib_error_return (0, "no rollover cookie ready to commit");
524 l2tp_session_cookie_commit (s);
528 if (!unformat (input, "rollover"))
529 return clib_error_return (0, "missing 'commit|rollover': '%U'",
530 format_unformat_error, input);
531 if (unformat (input, "local %llx", &lcl_ro_cookie))
533 cookie_flags |= L2TP_COOKIE_ROLLOVER_LOCAL;
534 l2tp_session_set_local_rollover_cookie (s, lcl_ro_cookie);
536 if (unformat (input, "remote %llx", &rem_ro_cookie))
538 cookie_flags |= L2TP_COOKIE_ROLLOVER_REMOTE;
539 l2tp_session_set_remote_cookie (s, rem_ro_cookie);
542 return clib_error_return (0, "no rollover cookie specified");
548 static VLIB_CLI_COMMAND (l2tp_session_cookie_command) = {
549 .path = "l2tp session cookie",
551 "l2tp session cookie <session id> commit|rollover [local <hex>] [remote <hex>]",
552 .function = l2tp_session_cookie_command_fn,
557 * fd.io coding-style-patch-verification: ON
560 * eval: (c-set-style "gnu")